Skip to main content

Overview

Standard Agents provides utility functions that wrap common ThreadState operations. While you can call ThreadState methods directly, these utilities are exported from @standardagents/builder for convenience and backwards compatibility.
Most utilities are now methods on ThreadState. You can use either the standalone functions or call methods directly on state.

queueTool

Queue a tool call for execution in the current thread.
Parameters:
  • state: The ThreadState context
  • toolName: The name of the tool to call
  • args: Arguments to pass to the tool
Example:

invokeTool

Invoke a tool directly and wait for the result.
Parameters:
  • toolName: The name of the tool to invoke
  • args: Arguments to pass to the tool
Returns: Promise<ToolResult>

injectMessage

Inject a message into the thread conversation.
Options:
Examples:

getMessages

Retrieve message history from the thread.
Options: Example:

emit (Custom Events)

Send custom events to WebSocket clients.
Parameters:
  • event: Event type name
  • data: Event payload (must be JSON-serializable)
Event Structure (received by clients):
Examples:

Frontend Integration

Listen for events on the frontend using @standardagents/react:

forceTurn (Execution Control)

Force the next execution turn to a specific side (for dual_ai agents).
Parameters:
  • side: The side to force ('a' or 'b')
This is primarily used for dual_ai agents to control turn order.

stop (Execution Control)

Stop the current execution after the current operation completes.

scheduleEffect

Schedule an effect for delayed or background execution.
Parameters:
  • name: Effect name (file in agents/effects/)
  • args: Arguments passed to effect handler
  • delay: Delay in milliseconds (default: 0)
Returns: Effect ID (UUID) for later cancellation

Complete Example

Here’s a comprehensive example showing multiple utilities working together:

Best Practices

Always wrap async operations in try-catch blocks:
Avoid excessive event emissions that could overwhelm WebSocket clients:
When retrieving large message histories, use pagination:
When using execution-specific features, check if execution is active:

Troubleshooting

Solution: Ensure you’re using ThreadProvider and that you’ve subscribed to the correct event type name.
Solution: Tools are queued for sequential execution. Ensure the current tool completes successfully and doesn’t stop execution.
Solution: Check that messages exist in the thread and that limit/offset parameters are correct.

Next Steps

ThreadState

Understand the ThreadState interface

Tools

Create custom tools

Hooks

Use utilities in lifecycle hooks

React Integration

Connect frontend to backend events