Tool Result

Tool Result

AgentScript provides a special type for tool results that allows to control the execution flow.

API Reference

ToolResult<TOutput>

A union type of the tool's output type and ToolAwaitResult.

type ToolResult<TOutput> = TOutput | ToolAwaitResult;

TOOL_AWAIT_RESULT

A special symbol that indicates that the tool is waiting for events.

import { TOOL_AWAIT_RESULT } from 'agentscript-ai/core';
 
if (result === TOOL_AWAIT_RESULT) {
    // Tool is waiting for events
}

toolResultHelper

A helper object for managing tool results.

Methods

  • toolResultHelper(output): Returns the output of the tool
  • toolResultHelper.await(): Returns the TOOL_AWAIT_RESULT symbol

Example

import { defineTool, toolResultHelper } from 'agentscript-ai/core';
import * as s from 'agentscript-ai/schema';
 
const myTool = defineTool({
    name: 'myTool',
    description: 'A tool that might wait for events.',
    input: {
        message: s.string(),
    },
    output: s.string(),
    event: s.string(),
    handler: async ({ input, events, result }) => {
        if (events.length > 0) {
            return input.message + events[0].payload;
        }
 
        return result.await();
    },
});

In this example, the tool will wait for an event if no events are present, and return the result when the event is received.