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.state: The ThreadState contexttoolName: The name of the tool to callargs: Arguments to pass to the tool
invokeTool
Invoke a tool directly and wait for the result.toolName: The name of the tool to invokeargs: Arguments to pass to the tool
Promise<ToolResult>
injectMessage
Inject a message into the thread conversation.- System Context
- Silent Message
getMessages
Retrieve message history from the thread.
Example:
emit (Custom Events)
Send custom events to WebSocket clients.event: Event type namedata: Event payload (must be JSON-serializable)
- Progress Updates
- Status Updates
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).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.name: Effect name (file inagents/effects/)args: Arguments passed to effect handlerdelay: Delay in milliseconds (default: 0)
Complete Example
Here’s a comprehensive example showing multiple utilities working together:Best Practices
Error Handling
Error Handling
Always wrap async operations in try-catch blocks:
Throttle Event Emissions
Throttle Event Emissions
Avoid excessive event emissions that could overwhelm WebSocket clients:
Message Pagination
Message Pagination
When retrieving large message histories, use pagination:
Check Execution State
Check Execution State
When using execution-specific features, check if execution is active:
Troubleshooting
Events not received by frontend
Events not received by frontend
Solution: Ensure you’re using
ThreadProvider and that you’ve subscribed to the correct event type name.Tool not executing after queueTool
Tool not executing after queueTool
Solution: Tools are queued for sequential execution. Ensure the current tool completes successfully and doesn’t stop execution.
getMessages returns empty
getMessages returns empty
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