langchain.js
    Preparing search index...

    Class SubagentManager<ToolCall>

    Manages subagent execution state.

    Tracks subagents from the moment they are invoked (AI message with tool calls) through streaming to completion (tool message result).

    Type Parameters

    Index

    Constructors

    Methods

    • Add a serialized message to a subagent from stream events.

      This method handles the raw serialized message data from SSE events. Uses MessageTupleManager for proper chunk concatenation, matching how the main stream handles messages.

      Parameters

      • namespaceId: string

        The namespace ID (pregel task ID) from the stream

      • serialized: Message<DefaultToolCall>

        The serialized message from the stream

      • Optionalmetadata: Record<string, unknown>

        Optional metadata from the stream event

      Returns void

    • Clear all subagent state.

      Returns void

    • Complete a subagent with a result.

      Called when a tool message is received for the subagent.

      Parameters

      • toolCallId: string

        The tool call ID of the subagent

      • result: string

        The result content

      • status: "error" | "complete" = "complete"

        The final status (complete or error)

      Returns void

    • Get a specific subagent by tool call ID.

      Parameters

      • toolCallId: string

      Returns undefined | SubagentStreamInterface<Record<string, unknown>, ToolCall, string>

    • Get all subagents as a Map. Filters out incomplete/phantom subagents that lack subagent_type.

      Returns Map<string, SubagentStreamInterface<Record<string, unknown>, ToolCall, string>>

    • Get all subagents triggered by a specific AI message.

      Parameters

      • messageId: string

        The ID of the AI message.

      Returns SubagentStreamInterface<Record<string, unknown>, ToolCall, string>[]

      Array of subagent streams triggered by that message.

    • Get the tool call ID for a given namespace ID. Returns the namespace ID itself if no mapping exists.

      Parameters

      • namespaceId: string

      Returns string

    • Check if any subagents are currently tracked.

      Returns boolean

    • Check if a tool call is a subagent invocation.

      Parameters

      • toolName: string

      Returns boolean

    • Mark a subagent as running and update its namespace.

      Called when update events are received with a namespace indicating which subagent is streaming.

      Parameters

      • toolCallId: string

        The tool call ID of the subagent

      • Optionaloptions: { namespace?: string[] }

        Additional update options

      Returns void

    • Mark a subagent as running using a namespace ID. Resolves the namespace ID to the actual tool call ID via the mapping.

      Parameters

      • namespaceId: string

        The namespace ID (pregel task ID) from the subgraph

      • Optionalnamespace: string[]

        The full namespace array

      Returns void

    • Try to match a subgraph to a pending subagent by description. Creates a mapping from namespace ID to tool call ID if a match is found.

      Uses a multi-pass matching strategy:

      1. Exact description match
      2. Description contains/partial match
      3. Any unmapped pending subagent (fallback)

      Parameters

      • namespaceId: string

        The namespace ID (pregel task ID) from the subgraph

      • description: string

        The description from the subgraph's initial message

      Returns undefined | string

      The matched tool call ID, or undefined if no match

    • Process a tool message to complete a subagent.

      Parameters

      • toolCallId: string

        The tool call ID from the tool message

      • content: string

        The result content

      • status: "error" | "success" = "success"

        Whether the tool execution was successful

      Returns void

    • Reconstruct subagent state from historical messages.

      This method parses an array of messages (typically from thread history) to identify subagent executions and their results. It's used to restore subagent state after:

      • Page refresh (when stream has already completed)
      • Loading thread history
      • Navigating between threads

      The reconstruction process:

      1. Find AI messages with tool calls matching subagent tool names
      2. Find corresponding tool messages with results
      3. Create SubagentStream entries with "complete" status

      Note: Internal subagent messages (their streaming conversation) are not reconstructed since they are not persisted in the main thread state.

      Parameters

      • messages: Message<DefaultToolCall>[]

        Array of messages from thread history

      • Optionaloptions: { skipIfPopulated?: boolean }

        Optional configuration

        • OptionalskipIfPopulated?: boolean

          If true, skip reconstruction if subagents already exist

      Returns void

    • Register new subagent(s) from AI message tool calls.

      Called when an AI message is received with tool calls. Creates pending subagent entries for each subagent tool call.

      Parameters

      • toolCalls: { args: string | Record<string, unknown>; id?: string; name: string }[]

        The tool calls from an AI message

      • OptionalaiMessageId: null | string

        The ID of the AI message that triggered the tool calls

      Returns void

    • Update subagent values from a values stream event.

      Called when a values event is received from a subagent's namespace. This populates the subagent's state values, making them accessible via the values property.

      Parameters

      • namespaceId: string

        The namespace ID (pregel task ID) from the stream

      • values: Record<string, unknown>

        The state values from the stream event

      Returns void