Parser

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 AST
    • code (string | string[], required): The AgentScript code to parse
    • Returns a Script object:
      • code (string, optional): The original code
      • ast (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 LLM
    • response (string, required): The response from the LLM
    • Returns an object with:
      • plan (string): The plan extracted from the response
      • code (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 node
  • comment (string, optional): Comment for the node

VariableDeclaration

Represents a variable declaration.

  • type: 'var'
  • name (string): Name of the variable
  • value (Expression, optional): Value of the variable

FunctionCall

Represents a function call.

  • type: 'call'
  • func (Expression): Function to call
  • args (Expression[]): Arguments to pass to the function

NewExpression

Represents a new expression.

  • type: 'new'
  • func (Expression): Function to call
  • args (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 access
  • obj (Expression): Object to access the property on

Assignment

Represents an assignment expression.

  • type: 'assign'
  • left (Expression): Left side of the assignment
  • right (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 property
  • value (Expression): Value of the property

Type Unions

  • Expression: A union type of all expression types
  • Statement: A union type of all statement types
  • AstNode: A union type of all AST node types
  • Script: Represents the parsed script
    • code (string, optional): The original code
    • ast (AstNode[]): An array of AST nodes representing the code