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 update

  • state (Partial): An object containing the state properties to update. Can include:

    • messages: Array of messages to add or update

    • memory: Partial memory object to merge with existing memory

    • goto: Node ID to override the next execution point

    • Any other state properties defined in your agent

Usage Examples

Update Memory

Add Messages

Update Multiple State Properties

Common Use Cases

  1. Tool State Updates: Tools can update state after execution

  2. External Event Handling: Update state based on external events

  3. 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 identifier

  • memory: The current memory state

  • messages: Array of conversation messages

  • sessionType: 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

  1. Browser-use requests tool execution via socket

  2. ToolExecutor receives request with CDP URL

  3. CDP info is injected into state (state.cdp)

  4. Tool connects to browser using CDP URL from state.cdp.url

  5. Tool performs automation and returns results

  6. State updates are automatically applied

Security Considerations

  • Tools must have allowExecutionRequests: true to be executable via ToolExecutor

  • CDP 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:

  • Memory - Learn about memory schemas and management

  • Events - Handle agent events and triggers

  • Debugging - Debug state updates and transitions

  • Tools - Create tools that update state

Last updated