import { ... } from "@langchain/langgraph-sdk/react";Calculates the depth of a subagent based on its namespace. Counts the number of "tools:" segments in the namespace.
Extracts the parent tool call ID from a namespace.
For nested subagents, the namespace looks like: ["tools:parent_id", "tools:child_id", ...]
Extracts the tool call ID from a namespace path.
Namespaces follow the pattern: ["tools:call_abc123", "model_request:xyz", ...] This function extracts "call_abc123" from the first "tools:" segment.
Checks if a namespace indicates a subagent/subgraph message.
Subagent namespaces contain a "tools:" segment indicating they originate from a tool call that spawned a subgraph.
A React hook that provides seamless integration with LangGraph streaming capabilities.
The useStream hook handles all the complexities of streaming, state management, and branching logic,
letting you focus on building great chat experiences. It provides automatic state management for
messages, interrupts, loading states, subagent streams, and errors.
When using createAgent from @langchain/langgraph, you can pass typeof agent as the
type parameter to automatically infer tool call types:
Transport used to stream the thread.
Only applicable for custom endpoints using toLangGraphEventStream or toLangGraphEventStreamResponse.
Manages subagent execution state.
Tracks subagents from the moment they are invoked (AI message with tool calls) through streaming to completion (tool message result).
Minimal interface matching the structure of AgentTypeConfig from @langchain/langgraph. This allows type inference from ReactAgent without requiring the langchain dependency.
Minimal interface matching the structure of a CompiledSubAgent from deepagents. Used for structural type matching without importing deepagents.
Minimal interface matching the structure of DeepAgentTypeConfig from deepagents. Extends AgentTypeConfigLike to include subagent type information.
Minimal interface matching the structure of a SubAgent from deepagents. Used for structural type matching without importing deepagents.
Base interface for a single subagent stream. Tracks the lifecycle of a subagent from invocation to completion.
Extends StreamBase to share common properties with UseStream, allowing subagents to be treated similarly to the main stream.
Prefer using SubagentStream which supports passing an agent type directly for automatic type inference.
Represents a tool call that initiated a subagent.
Transport used to stream the thread.
Only applicable for custom endpoints using toLangGraphEventStream or toLangGraphEventStreamResponse.
Base stream interface shared by all stream types.
Contains core properties for state management, messaging, and stream control that are common to CompiledStateGraph, ReactAgent, and DeepAgent streams.
This interface provides the foundation that all stream types build upon:
values, isLoading, error)messages)interrupt)submit, stop)branch, history)Stream interface for ReactAgent instances created with createAgent.
Extends UseGraphStream with tool calling capabilities. Tool calls are automatically typed based on the agent's tools configuration.
Use this interface when streaming from an agent created with createAgent.
For subagent streaming capabilities, use UseDeepAgentStream with createDeepAgent.
This interface is subject to change.
Options for configuring an agent stream.
Use this options interface when calling useStream with a ReactAgent
created via createAgent.
This interface is subject to change.
Stream interface for DeepAgent instances created with createDeepAgent.
Extends UseAgentStream with subagent streaming capabilities. Subagent streams are automatically typed based on the agent's subagent configuration, enabling type-safe access to subagent state and messages.
Use this interface when streaming from an agent created with createDeepAgent
that orchestrates multiple specialized subagents.
This interface is subject to change.
Options for configuring a deep agent stream.
Use this options interface when calling useStream with a DeepAgent
created via createDeepAgent. Includes all agent options plus
subagent-specific configuration.
Base state type for subagents. All subagents have at least a messages array, similar to the main agent.
Default subagent state map used when no specific subagent types are provided. Maps any string key to Record<string, unknown>.
Default tool call type when no specific tool definitions are provided.
Extract the AgentTypeConfig from an agent-like type.
Extract the DeepAgentTypeConfig from a DeepAgent-like type.
Helper type to extract middleware from a SubAgent definition. Handles both mutable and readonly middleware arrays.
Extract the tool call type from a StateType's messages property. This is the canonical way to get typed tool calls in useStream.
Tool call types are now extracted from the messages property of StateType, rather than being specified separately in the Bag.
Extract tool calls type from an agent's tools. Converts the tools array to a discriminated union of tool calls.
This handles both tuple types (e.g., readonly [Tool1, Tool2]) and
array-of-union types (e.g., readonly (Tool1 | Tool2)[]) which is how
createAgent captures tool types.
Extract the Subagents array type from a DeepAgent.
Helper type to extract a subagent by name from a DeepAgent.
Extract all subagent names as a string union from a DeepAgent.
Infer the state type for a specific subagent by extracting and merging its middleware state schemas, plus the base agent state (messages).
Check if a type is agent-like (has ~agentTypes phantom property).
This property is present on ReactAgent instances created with createAgent.
Check if a type is a DeepAgent (has ~deepAgentTypes phantom property).
This property is present on DeepAgent instances created with createDeepAgent.
Create a map of subagent names to their state types.
This is useful for type-safe getSubagentsByType calls.
The execution status of a subagent.
"pending" - The subagent has been invoked but hasn't started processing yet.
This is the initial state when a tool call is detected but before any
streaming events are received from the subgraph.
"running" - The subagent is actively executing and streaming updates.
The subagent transitions to this state when the first update event is
received from its namespace.
"complete" - The subagent has finished execution successfully.
A tool message with the result has been received, and the result
property contains the final output.
"error" - The subagent encountered an error during execution.
The error property on the SubagentStream contains error details.
Represents a single subagent stream.
Supports two usage patterns:
import type { agent } from "./agent";
// Automatically infers state and tool call types from the agent
const subagent: SubagentStream<typeof agent> = ...;
type ResearcherState = { research_notes: string };
const researcher: SubagentStream<ResearcherState, MyToolCall> = ...;Infer a tool call type from a single tool.
Works with tools created via tool() from @langchain/core/tools.
For DynamicStructuredTool, this extracts the input type from the _call method, which is the most reliable source as it's the pre-computed input type.
Infer a union of tool call types from an array of tools.
Works with tools created via tool() from @langchain/core/tools.
The lifecycle state of a tool call.
pending: Tool call received, awaiting resultcompleted: Tool execution finished successfullyerror: Tool execution failed (result.status === "error")Represents a tool call paired with its result. Useful for rendering tool invocations and their outputs together.
Infer the Bag type from an agent, defaulting to the provided Bag.
Currently returns the provided Bag for all types. Can be extended in the future to extract Bag from agent types.
Infer the node names from a compiled graph.
Extracts the ~NodeType phantom property from CompiledGraph instances,
providing a union of all node names defined in the graph.
Infer the state type from an agent, graph, or direct state type.
Detection order:
~agentTypes) → InferAgentState~RunOutput) → Extract RunOutput~OutputType) → Extract OutputTypeInfer subagent state map from a DeepAgent.
For DeepAgent, creates a map of subagent names to their state types. For non-DeepAgent, returns DefaultSubagentStates.
Infer tool call types from an agent.
For agents, extracts typed tool calls from the agent's tools. For non-agents, returns DefaultToolCall.
Resolves the appropriate stream interface based on the agent/graph type.
This type automatically selects the correct stream interface based on
the type of agent or graph passed to useStream:
DeepAgent (~deepAgentTypes) → UseDeepAgentStream
ReactAgent (~agentTypes) → UseAgentStream
CompiledGraph (~RunOutput/~OutputType) → UseGraphStream
~NodeType for type-safe accessDefault → UseGraphStream
Resolves the appropriate options interface based on the agent/graph type.
This type automatically selects the correct options interface based on the type of agent or graph:
DeepAgent → UseDeepAgentStreamOptions
filterSubagentMessages optionReactAgent → UseAgentStreamOptions
CompiledGraph / Default → UseGraphStreamOptions