Agent API
The Agent class is the core component of MindedJS that orchestrates conversations, manages state, and coordinates flows. This document covers the initialization options and key methods available on the Agent instance.
Constructor
The Agent constructor accepts configuration options to customize its behavior:
new Agent({
memorySchema: z.ZodSchema,
config: MindedSDKConfig,
tools: Tool[],
platformToken?: string,
memorySaver?: BaseCheckpointSaver,
interruptSessionManager?: InterruptSessionManager,
parseSessionIdFromTrigger?: (trigger: any) => string,
});Options
memorySchema (required): Zod schema defining the agent's memory structure
config (required): SDK configuration including flows and LLM settings
tools (required): Array of custom tools available to the agent
platformToken: Optional token for platform authentication
memorySaver: Optional custom checkpoint saver for conversation memory
interruptSessionManager: Optional custom interrupt session manager
parseSessionIdFromTrigger: Optional function to extract session ID from triggers (defaults to
triggerBody.sessionId)
Key Methods
updateState
Updates the state of an active session. This method allows you to programmatically modify the conversation state, including messages, memory, and other state properties.
Signature
Parameters
sessionId(string): The unique identifier of the session to updatestate(Partial): An object containing the state properties to update. Can include:messages: Array of messages to add or updatememory: Partial memory object to merge with existing memorygoto: Node ID to override the next execution pointAny other state properties defined in your agent
Usage Examples
Update Memory
Add Messages
Update Multiple State Properties
Common Use Cases
Tool State Updates: Tools can update state after execution
External Event Handling: Update state based on external events
Voice Session Corrections: Update messages during voice conversations
getState
Retrieves the current state of a session.
Signature
Parameters
sessionId(string): The unique identifier of the session
Returns
A State object containing:
sessionId: The session identifiermemory: The current memory statemessages: Array of conversation messagessessionType: The type of session (TEXT, VOICE, etc.)Other state properties
Usage Example
compiledGraph
Access to the underlying LangGraph compiled graph for advanced operations.
Usage Example
State Management Best Practices
1. Partial Updates
Always use partial updates to avoid overwriting existing state:
2. Atomic Operations
Group related updates together:
3. State Validation
The state updates are validated against your memory schema:
4. Tool State Updates
Tools should return state updates rather than calling updateState directly when possible:
Tool Execution API
ToolExecutor
The ToolExecutor class enables standalone tool execution, particularly useful for browser automation and external tool invocations.
Setting Up Tool Execution
Tool Configuration for Execution
Tools must explicitly opt-in to be executable via requests:
Executing Tools
CDP URL and Browser Automation
When tools are executed in a browser-use context, the Chrome DevTools Protocol (CDP) URL is available in the state:
Accessing CDP URL in Tools
Browser-Use Integration Flow
Browser-use requests tool execution via socket
ToolExecutor receives request with CDP URL
CDP info is injected into state (
state.cdp)Tool connects to browser using CDP URL from
state.cdp.urlTool performs automation and returns results
State updates are automatically applied
Security Considerations
Tools must have
allowExecutionRequests: trueto be executable via ToolExecutorCDP URLs are only available during browser-use execution context
Tools should validate CDP URL availability before attempting browser operations
Always handle connection failures gracefully
Integration with Other Features
With Browser Use Tools
When using browser automation tools, state updates happen automatically through the ToolExecutor:
With Voice Sessions
Voice sessions use updateState for real-time corrections:
With Events
State can be updated in event handlers:
Error Handling
Always handle potential errors when updating state:
Related Documentation
Last updated