Parser
The parser module is responsible for converting the generated AgentScript code into an Abstract Syntax Tree (AST).
parseScript
The parseScript function takes a string of AgentScript code and returns an AST.
Parameters
parseScript(code): Parses the code into an ASTcode(string | string[], required): The AgentScript code to parse- Returns a
Scriptobject:code(string, optional): The original codeast(AstNode[]): An array of AST nodes representing the code
Example
import { parseScript } from 'agentscript-ai/core';
const code = `
const a = 1;
const b = add(a, 2);
`;
const script = parseScript(code);
console.log(script.ast);parseCodeResponse
The parseCodeResponse function takes the response from the LLM and extracts the plan and the code.
Parameters
parseCodeResponse(response): Parses the response from the LLMresponse(string, required): The response from the LLM- Returns an object with:
plan(string): The plan extracted from the responsecode(string): The code extracted from the response
Example
import { parseCodeResponse } from 'agentscript-ai/core';
const response = `
This is a plan:
1. Create a variable a with value 1
2. Create a variable b with value of a + 2
\`\`\`typescript
const a = 1;
const b = add(a, 2);
\`\`\`
`;
const { plan, code } = parseCodeResponse(response);
console.log(plan);
console.log(code);AST Types
The AST is represented by the following types:
AstNodeBase
Base interface for all AST nodes.
type(string): Type of the nodecomment(string, optional): Comment for the node
VariableDeclaration
Represents a variable declaration.
type: 'var'name(string): Name of the variablevalue(Expression, optional): Value of the variable
FunctionCall
Represents a function call.
type: 'call'func(Expression): Function to callargs(Expression[]): Arguments to pass to the function
NewExpression
Represents a new expression.
type: 'new'func(Expression): Function to callargs(Expression[]): Arguments to pass to the constructor
Literal
Represents a literal value.
type: 'literal'value(unknown): Value of the literal
Identifier
Represents an identifier.
type: 'ident'name(string): Name of the identifier
MemberExpression
Represents a member expression.
type: 'member'prop(Expression): Property to accessobj(Expression): Object to access the property on
Assignment
Represents an assignment expression.
type: 'assign'left(Expression): Left side of the assignmentright(Expression): Right side of the assignment
ObjectExpression
Represents an object expression.
type: 'obj'props(ObjectProperty[]): Properties of the object
ArrayExpression
Represents an array expression.
type: 'arr'items(Expression[]): Items in the array
ObjectProperty
Represents a property of an object.
key(Expression): Key of the propertyvalue(Expression): Value of the property
Type Unions
Expression: A union type of all expression typesStatement: A union type of all statement typesAstNode: A union type of all AST node typesScript: Represents the parsed scriptcode(string, optional): The original codeast(AstNode[]): An array of AST nodes representing the code