Create Agent

createAgent

The createAgent function takes a partial Agent object with schema definitions, and produces full Agent object that contains a script, plan and an execution state.

API Reference

createAgent(params)

Creates an agent object that can be executed.

Parameters

  • params:
    • script (Script, required): The script that represents the agent logic
    • tools (object, optional): The tools object for the agent
    • output (s.Schema, optional): An schema of the expected output
    • input (object, optional): An object with schemas of input values, with names as object keys
    • state (AgentState, optional): State of the agent, this is set by the executeAgent

Returns

Returns a full Agent object with the following properties:

  • tools: Tools that are passed into the method
  • input: Input schema for the agent
  • output: Output schema for the agent
  • script: Parsed agent code AST
  • plan: Execution plan summary
  • state: Execution state for this agent
  • id: Unique id of the agent

Example

import { parseScript, createAgent, defineTool } from 'agentscript-ai/core';
import * as s from 'agentscript-ai/schema';
 
const add = defineTool({
    name: 'add',
    description: 'Add two numbers.',
    input: {
        a: s.number(),
        b: s.number(),
    },
    output: s.number(),
    handler: ({input}) => input.a + input.b,
});
 
const script = parseScript('const result = add(1, 2);');
const agent = createAgent({
    tools: {
        add
    },
    script
});