import { Checkpoint, Config, Interrupt, Metadata, ThreadState } from "../schema.js"; import { AIMessage as AIMessage$1, DefaultToolCall, Message, ToolCallWithResult } from "../types.messages.js"; import { CheckpointsStreamEvent, CustomStreamEvent, DebugStreamEvent, EventsStreamEvent, MetadataStreamEvent, StreamMode, TasksStreamEvent, ToolsStreamEvent, UpdatesStreamEvent } from "../types.stream.js"; import { Command, DisconnectMode, Durability, MultitaskStrategy, OnCompletionBehavior } from "../types.js"; import { ClientConfig } from "../client/base.js"; import { Client } from "../client/index.js"; import { BagTemplate } from "../types.template.js"; import { AnyHeadlessToolImplementation, OnToolCallback } from "../headless-tools.js"; import { BaseMessage } from "@langchain/core/messages"; import { InferInteropZodInput } from "@langchain/core/utils/types"; //#region src/ui/types.d.ts /** * Represents a tool call that initiated a subagent. * * @template SubagentName - The subagent name type. When inferred from a * DeepAgent, this is a union of all subagent names (e.g. `"researcher" | "writer"`), * making `args.subagent_type` a typed discriminant. */ interface SubagentToolCall { /** The tool call ID */ id: string; /** The name of the tool (typically "task") */ name: string; /** The arguments passed to the tool */ args: { /** The task description for the subagent */description?: string; /** The type of subagent to use */ subagent_type?: SubagentName; /** Additional custom arguments */ [key: string]: unknown; }; } /** * 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. */ type SubagentStatus = "pending" | "running" | "complete" | "error"; /** * Default subagent state map used when no specific subagent types are provided. * Maps any string key to Record. */ type DefaultSubagentStates = Record>; /** * Base interface for stream-like objects. * Contains common properties shared between UseStream and SubagentStream. * * @template StateType - The type of the stream's state values. * @template ToolCall - The type of tool calls in messages. * @template InterruptType - The type of interrupt values. * @template SubagentStates - A map of subagent names to their state types. * Use `SubagentStateMap` to infer from a DeepAgent. */ interface StreamBase, ToolCall = DefaultToolCall, InterruptType = unknown, SubagentStates extends Record = DefaultSubagentStates> { /** * The current state values of the stream. */ values: StateType; /** * Last seen error from the stream. */ error: unknown; /** * Whether the stream is currently running. */ isLoading: boolean; /** * Messages accumulated during the stream. */ messages: Message[]; /** * Tool calls paired with their results. * Useful for rendering tool invocations and their outputs together. */ toolCalls: ToolCallWithResult[]; /** * Get tool calls for a specific AI message. * * @param message - The AI message to get tool calls for. * @returns Array of tool calls initiated by the message. */ getToolCalls: (message: AIMessage$1) => ToolCallWithResult[]; /** * Get the interrupt value for the stream if interrupted. * Convenience alias for `interrupts[0]`. */ interrupt: Interrupt | undefined; /** * All current interrupts from the stream. * When using Send() fan-out with per-task interrupt() calls, * multiple interrupts may be pending simultaneously. */ interrupts: Interrupt[]; /** * All currently active and completed subagent streams. * Keyed by tool call ID for easy lookup. */ subagents: Map>; /** * Currently active subagents (where status === "running"). */ activeSubagents: SubagentStreamInterface[]; /** * Get subagent stream by tool call ID. * * @param toolCallId - The tool call ID that initiated the subagent. * @returns The subagent stream, or undefined if not found. */ getSubagent: (toolCallId: string) => SubagentStreamInterface | undefined; /** * Get all subagents of a specific type. * When called with a literal type name that matches a key in SubagentStates, * returns streams with properly inferred state types. * * @param type - The subagent_type to filter by. * @returns Array of matching subagent streams with inferred state types. * * @example * ```ts * // With DeepAgent type inference * const stream = useStream(...); * const researchers = stream.getSubagentsByType("researcher"); * // researchers[0].values is typed with ResearcherMiddleware state * ``` */ getSubagentsByType: { (type: TName): SubagentStreamInterface[]; (type: string): SubagentStreamInterface, ToolCall>[]; }; /** * Get all subagents triggered by a specific AI message. * * Useful for rendering subagent activities grouped by the AI message * (and therefore conversation turn) that spawned them. * * @param messageId - The ID of the AI message that triggered the subagents. * @returns Array of subagent streams triggered by that message. * * @example * ```tsx * // Render subagents after each AI message that triggered them * {stream.messages.map((msg) => ( *
* * {msg.type === "ai" && "tool_calls" in msg && ( * * )} *
* ))} * ``` */ getSubagentsByMessage: (messageId: string) => SubagentStreamInterface[]; /** * Switch to a different thread, clearing the current stream state. * Pass `null` to reset to no thread (a new thread will be created on next submit). * * @param newThreadId - The thread ID to switch to, or `null` to start fresh. */ switchThread: (newThreadId: string | null) => void; } /** * Subagent API surface parameterised by the subagent interface type. * * Framework adapters supply a class-message variant of * `SubagentStreamInterface` (where `messages` is `BaseMessage[]` * from `@langchain/core`) so that consumers always work with class * instances. The default parameter keeps the SDK's plain `Message` * interface for direct SDK usage. * * @template Iface - The subagent stream interface to expose. * Defaults to {@link SubagentStreamInterface} with default generic * parameters. */ interface SubagentApi { subagents: Map; activeSubagents: Iface[]; getSubagent: (toolCallId: string) => Iface | undefined; getSubagentsByType: (type: string) => Iface[]; getSubagentsByMessage: (messageId: string) => Iface[]; } /** * 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 {@link SubagentStream} which supports passing an agent type * directly for automatic type inference. * * @template StateType - The state type of the subagent. Defaults to Record * since different subagents may have different state types. Can be narrowed using * DeepAgent type helpers like `InferSubagentByName` when the specific subagent is known. * @template ToolCall - The type of tool calls in messages. * @template SubagentName - The subagent name union type. When inferred from a DeepAgent, * enables typed `toolCall.args.subagent_type`. */ interface SubagentStreamInterface, ToolCall = DefaultToolCall, SubagentName extends string = string> extends StreamBase { /** Unique identifier (the tool call ID) */ id: string; /** The tool call that invoked this subagent */ toolCall: SubagentToolCall; /** Current execution status */ status: SubagentStatus; /** Final result content (when complete) */ result: string | null; /** Namespace path for this subagent execution */ namespace: string[]; /** Tool call ID of parent subagent (for nested subagents) */ parentId: string | null; /** Nesting depth (0 = called by main agent, 1 = called by subagent, etc.) */ depth: number; /** When the subagent started */ startedAt: Date | null; /** When the subagent completed */ completedAt: Date | null; } /** * Represents a single subagent stream. * * Supports two usage patterns: * * 1. **Agent type inference** (recommended): Pass a DeepAgent type directly and * let TypeScript infer the correct state and tool call types. * * ```typescript * import type { agent } from "./agent"; * * // Automatically infers state and tool call types from the agent * const subagent: SubagentStream = ...; * ``` * * 2. **Explicit generics**: Pass state and tool call types manually. * * ```typescript * type ResearcherState = { research_notes: string }; * const researcher: SubagentStream = ...; * ``` * * @template T - Either a DeepAgent/Agent type for automatic inference, * or a state type (Record) for explicit typing. Defaults to Record. * @template ToolCall - The type of tool calls in messages. * Only used when T is a state type. Defaults to DefaultToolCall. */ type SubagentStream, ToolCall = DefaultToolCall> = IsDeepAgentLike extends true ? SubagentStreamInterface>[InferSubagentNames], InferAgentToolCalls, InferSubagentNames> : IsAgentLike extends true ? SubagentStreamInterface, InferAgentToolCalls> : SubagentStreamInterface; /** * Minimal interface matching the structure of AgentTypeConfig from @langchain/langgraph. * This allows type inference from ReactAgent without requiring the langchain dependency. */ interface AgentTypeConfigLike { Response: unknown; State: unknown; Context: unknown; Middleware: unknown; Tools: unknown; } /** * Check if a type is agent-like (has `~agentTypes` phantom property). * This property is present on `ReactAgent` instances created with `createAgent`. */ type IsAgentLike = T extends { "~agentTypes": AgentTypeConfigLike; } ? true : false; /** * Extract the AgentTypeConfig from an agent-like type. * * @example * ```ts * const agent = createAgent({ ... }); * type Config = ExtractAgentConfig; * // Config is the AgentTypeConfig with Response, State, Context, Middleware, Tools * ``` */ type ExtractAgentConfig = T extends { "~agentTypes": infer Config; } ? Config extends AgentTypeConfigLike ? Config : never : never; /** * Minimal interface to structurally match AgentMiddleware from langchain. * We can't import AgentMiddleware due to circular dependencies, so we match * against its structure to extract type information. */ interface AgentMiddlewareLike { name: string; stateSchema?: TSchema; "~middlewareTypes"?: { Schema: TSchema; ContextSchema: TContextSchema; FullContext: TFullContext; Tools: TTools; }; } /** * Helper type to extract state from a single middleware instance. * Uses structural matching against AgentMiddleware to extract the state schema * type parameter, similar to how langchain's InferMiddlewareState works. */ type SafeInferInteropZodInput = InferInteropZodInput extends never ? {} : InferInteropZodInput; type InferMiddlewareState = T extends AgentMiddlewareLike ? TSchema extends Record ? SafeInferInteropZodInput : {} : T extends { stateSchema: infer S; } ? SafeInferInteropZodInput : {}; /** * Helper type to detect if a type is `any`. * Uses the fact that `any` is both a subtype and supertype of all types. */ type IsAny = 0 extends 1 & T ? true : false; /** * Helper type to extract and merge states from an array of middleware. * Recursively processes each middleware and intersects their state types. * * Handles both readonly and mutable arrays/tuples explicitly. * * @example * ```ts * type States = InferMiddlewareStatesFromArray; * // Returns intersection of all middleware state types * ``` */ type InferMiddlewareStatesFromArray = IsAny extends true ? {} : T extends undefined | null ? {} : T extends readonly [] ? {} : T extends [] ? {} : T extends readonly [infer First, ...infer Rest extends readonly unknown[]] ? InferMiddlewareState & InferMiddlewareStatesFromArray : T extends [infer First, ...infer Rest extends unknown[]] ? InferMiddlewareState & InferMiddlewareStatesFromArray : T extends readonly (infer U)[] ? InferMiddlewareState : T extends (infer U)[] ? InferMiddlewareState : {}; /** * Infer the complete merged state from an agent, including: * - The agent's own state schema (via State) * - All middleware states (via Middleware) * * This is the SDK equivalent of langchain's `InferAgentState` type. * * @example * ```ts * const agent = createAgent({ * middleware: [todoListMiddleware()], * // ... * }); * * type State = InferAgentState; * // State includes { todos: Todo[], ... } * ``` */ /** * Base agent state that all agents have by default. * This includes the messages array which is fundamental to agent operation. * The ToolCall type parameter allows proper typing of tool calls in messages. */ type BaseAgentState = { messages: Message[]; }; /** * Conditionally adds `structuredResponse` to the agent state when * `responseFormat` is provided to `createAgent`. * * The sentinel type `ResponseFormatUndefined` (from langchain) has a * `__responseFormatUndefined` brand property. When the Response type * carries that brand, no `structuredResponse` key is added. */ type InferStructuredResponse = Response extends { __responseFormatUndefined: true; } ? {} : Response extends Record ? { structuredResponse: Response; } : {}; type InferAgentState = T extends { "~agentTypes": unknown; } ? ExtractAgentConfig extends never ? {} : BaseAgentState> & (ExtractAgentConfig["State"] extends undefined ? {} : SafeInferInteropZodInput["State"]>) & InferMiddlewareStatesFromArray["Middleware"]> & InferStructuredResponse["Response"]> : T extends { "~RunOutput": infer RunOutput; } ? RunOutput : T extends { messages: unknown; } ? T : {}; /** * Helper type to infer schema input type, supporting both Zod v3 and v4. * Self-contained to avoid cross-package type resolution issues with * InferInteropZodInput from @langchain/core. * - Zod v4 uses `_zod.input` property * - Zod v3 uses `_input` property */ type InferToolSchemaInput = S extends { _zod: { input: infer Args; }; } ? Args : S extends { _input: infer Args; } ? Args : never; /** * Helper type to extract the input type from a DynamicStructuredTool. * * Tries the following in order: * 1. `_call` method signature (may fail when `_call` is `protected`) * 2. `schema` property with self-contained Zod v3/v4 inference */ type InferToolInput = T extends { _call: (arg: infer Args, ...rest: any[]) => any; } ? Args : T extends { schema: infer S; } ? InferToolSchemaInput : never; /** * Helper type to check if a type is a literal string (not generic `string`). * Returns true only for literal types like "get_weather", false for `string`. */ type IsLiteralString = string extends T ? false : T extends string ? true : false; /** * Extract a tool call type from a single tool. * Works with tools created via `tool()` from `@langchain/core/tools`. * * This extracts the literal name type from DynamicStructuredTool's NameT parameter * and the args type from the _call method or schema's input property. * * Note: Only tools with literal string names (e.g., "get_weather") are included. * Tools with generic `name: string` are filtered out to ensure discriminated * union narrowing works correctly in TypeScript. */ type ToolCallFromAgentTool = T extends { name: infer N; } ? N extends string ? IsLiteralString extends true ? InferToolInput extends infer Args ? Args extends never ? never : Args extends Record ? { name: N; args: Args; id?: string; type?: "tool_call"; } : never : never : never : never : never; /** * 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. * * @example * ```ts * const agent = createAgent({ tools: [getWeather, search], ... }); * type ToolCalls = InferAgentToolCalls; * // ToolCalls is: * // | { name: "get_weather"; args: { location: string }; id?: string } * // | { name: "search"; args: { query: string }; id?: string } * ``` */ type InferAgentToolCalls = ExtractAgentConfig["Tools"] extends readonly (infer Tool)[] ? ToolCallFromAgentTool extends never ? DefaultToolCall : ToolCallFromAgentTool : DefaultToolCall; /** * Minimal interface matching the structure of a SubAgent from deepagents. * Used for structural type matching without importing deepagents. */ interface SubAgentLike { name: string; description: string; middleware?: readonly AgentMiddlewareLike[]; } /** * Minimal interface matching the structure of a CompiledSubAgent from deepagents. * Used for structural type matching without importing deepagents. */ interface CompiledSubAgentLike { name: string; description: string; runnable: unknown; } /** * Minimal interface matching the structure of DeepAgentTypeConfig from deepagents. * Extends AgentTypeConfigLike to include subagent type information. */ interface DeepAgentTypeConfigLike extends AgentTypeConfigLike { Subagents: unknown; } /** * Check if a type is a DeepAgent (has `~deepAgentTypes` phantom property). * This property is present on DeepAgent instances created with `createDeepAgent`. */ type IsDeepAgentLike = T extends { "~deepAgentTypes": DeepAgentTypeConfigLike; } ? true : false; /** * Extract the DeepAgentTypeConfig from a DeepAgent-like type. * * @example * ```ts * const agent = createDeepAgent({ subagents: [...] }); * type Config = ExtractDeepAgentConfig; * // Config includes { Subagents: [...] } * ``` */ type ExtractDeepAgentConfig = T extends { "~deepAgentTypes": infer Config; } ? Config extends DeepAgentTypeConfigLike ? Config : never : never; /** * Helper type to extract middleware from a SubAgent definition. * Handles both mutable and readonly middleware arrays. */ type ExtractSubAgentMiddleware = T extends { middleware?: infer M; } ? M extends readonly AgentMiddlewareLike[] ? M : M extends AgentMiddlewareLike[] ? M : readonly [] : readonly []; /** * Extract the Subagents array type from a DeepAgent. * * @example * ```ts * const agent = createDeepAgent({ subagents: [researcher, writer] as const }); * type Subagents = InferDeepAgentSubagents; * // Subagents is the readonly tuple of subagent definitions * ``` */ type InferDeepAgentSubagents = ExtractDeepAgentConfig extends never ? never : ExtractDeepAgentConfig["Subagents"]; /** * Helper type to extract a subagent by name from a DeepAgent. * * @typeParam T - The DeepAgent to extract from * @typeParam TName - The name of the subagent to extract * * @example * ```ts * const agent = createDeepAgent({ * subagents: [ * { name: "researcher", description: "...", middleware: [ResearchMiddleware] } * ] as const, * }); * * type Researcher = InferSubagentByName; * ``` */ type InferSubagentByName = InferDeepAgentSubagents extends readonly (infer SA)[] ? SA extends { name: TName; } ? SA : never : never; /** * Base state type for subagents. * All subagents have at least a messages array, similar to the main agent. * * @template ToolCall - The tool call type for messages. Defaults to DefaultToolCall. */ type BaseSubagentState = { messages: Message[]; }; /** * Infer the state type for a specific subagent by extracting and merging * its middleware state schemas, plus the base agent state (messages). * * @typeParam T - The DeepAgent to extract from * @typeParam TName - The name of the subagent * @typeParam ToolCall - The tool call type for messages. Defaults to DefaultToolCall. * * @example * ```ts * const agent = createDeepAgent({ * subagents: [ * { name: "researcher", middleware: [ResearchMiddleware] } * ] as const, * }); * * type ResearcherState = InferSubagentState; * // ResearcherState includes { messages: Message[], ...ResearchMiddleware state } * ``` */ type InferSubagentState = InferSubagentByName extends never ? Record : InferSubagentByName extends infer SA ? BaseSubagentState & InferMiddlewareStatesFromArray> : Record; /** * Extract all subagent names as a string union from a DeepAgent. * * @example * ```ts * const agent = createDeepAgent({ * subagents: [ * { name: "researcher", ... }, * { name: "writer", ... } * ] as const, * }); * * type SubagentNames = InferSubagentNames; * // SubagentNames = "researcher" | "writer" * ``` */ type InferSubagentNames = InferDeepAgentSubagents extends readonly (infer SA)[] ? SA extends { name: infer N; } ? N extends string ? N : never : never : never; /** * Create a map of subagent names to their state types. * This is useful for type-safe `getSubagentsByType` calls. * * @typeParam T - The DeepAgent to extract from * @typeParam ToolCall - The tool call type for messages. Defaults to DefaultToolCall. * * @example * ```ts * const agent = createDeepAgent({ * subagents: [ * { name: "researcher", middleware: [ResearchMiddleware] }, * { name: "writer", middleware: [WriterMiddleware] } * ] as const, * }); * * type StateMap = SubagentStateMap; * // StateMap = { researcher: ResearchState; writer: WriterState } * ``` */ type SubagentStateMap = { [K in InferSubagentNames]: InferSubagentState }; /** * Extract the tool call type parameter from an AIMessage in a message union. * Returns `never` if the message is not an AIMessage or uses DefaultToolCall. * * The key distinction: custom tool calls have literal `name` types (e.g., "get_weather"), * while DefaultToolCall has `name: string`. We check if `string extends TC["name"]` - * if true, it's DefaultToolCall; if false, it's a custom type with literal names. */ type ExtractToolCallFromMessageUnion = M extends AIMessage$1 ? TC extends { name: infer N; } ? string extends N ? never : TC : never : never; /** * Extract the tool call type from a StateType's messages property. * This is the primary way to specify tool call types when using useStream. * * @example * ```ts * // Define state with typed messages * type MyToolCalls = * | { name: "get_weather"; args: { location: string }; id?: string } * | { name: "search"; args: { query: string }; id?: string }; * * interface MyState { * messages: Message[]; * } * * // ExtractToolCallsFromState = MyToolCalls * ``` */ type ExtractToolCallsFromState> = StateType extends { messages: infer Messages; } ? Messages extends readonly (infer M)[] ? ExtractToolCallFromMessageUnion : Messages extends (infer M)[] ? ExtractToolCallFromMessageUnion : never : never; type MessageMetadata> = { /** * The ID of the message used. */ messageId: string; /** * The first thread state the message was seen in. */ firstSeenState: ThreadState | undefined; /** * The branch of the message. */ branch: string | undefined; /** * The list of branches this message is part of. * This is useful for displaying branching controls. */ branchOptions: string[] | undefined; /** * Metadata sent alongside the message during run streaming. * @remarks This metadata only exists temporarily in browser memory during streaming and is not persisted after completion. */ streamMetadata: Record | undefined; }; type GetUpdateType> = Bag extends { UpdateType: unknown; } ? Bag["UpdateType"] : Partial; /** * Widens an update type so that its `messages` field also accepts * `@langchain/core` {@link BaseMessage} class instances (single or array). * * Framework SDKs apply this to `submit` so callers can write: * ```ts * stream.submit({ messages: new HumanMessage("hello") }); * stream.submit({ messages: [new HumanMessage("hello")] }); * ``` */ type AcceptBaseMessages = T extends Record ? { [K in keyof T]: K extends "messages" ? T[K] | BaseMessage | BaseMessage[] : T[K] } : T; type GetConfigurableType = Bag extends { ConfigurableType: Record; } ? Bag["ConfigurableType"] : Record; type GetInterruptType = Bag extends { InterruptType: unknown; } ? Bag["InterruptType"] : unknown; type GetCustomEventType = Bag extends { CustomEventType: unknown; } ? Bag["CustomEventType"] : unknown; /** * 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. * * @example * ```ts * // Define state with typed messages * type MyToolCalls = * | { name: "get_weather"; args: { location: string }; id?: string } * | { name: "search"; args: { query: string }; id?: string }; * * interface MyState { * messages: Message[]; * } * * // GetToolCallsType = MyToolCalls * ``` */ type GetToolCallsType> = ExtractToolCallsFromState extends never ? DefaultToolCall : ExtractToolCallsFromState; interface RunCallbackMeta { run_id: string; thread_id: string; } interface UseStreamThread> { data: ThreadState[] | null | undefined; error: unknown; isLoading: boolean; mutate: (mutateId?: string) => Promise[] | null | undefined>; } interface UseStreamOptions = Record, Bag extends BagTemplate = BagTemplate> { /** * The ID of the assistant to use. */ assistantId: string; /** * Client used to send requests. */ client?: Client; /** * The URL of the API to use. */ apiUrl?: ClientConfig["apiUrl"]; /** * The API key to use. */ apiKey?: ClientConfig["apiKey"]; /** * Custom call options, such as custom fetch implementation. */ callerOptions?: ClientConfig["callerOptions"]; /** * Default headers to send with requests. */ defaultHeaders?: ClientConfig["defaultHeaders"]; /** * Specify the key within the state that contains messages. * Defaults to "messages". * * @default "messages" */ messagesKey?: string; /** * Callback that is called when an error occurs. */ onError?: (error: unknown, run: RunCallbackMeta | undefined) => void; /** * Callback that is called when the stream is finished. * * If you declare no parameters (side effects only), the SDK skips an extra * post-stream `getHistory` when branching history is disabled, so loading * ends as soon as the run stream completes. */ onFinish?: (state: ThreadState, run: RunCallbackMeta | undefined) => void; /** * Callback that is called when a new stream is created. */ onCreated?: (run: RunCallbackMeta) => void; /** * Callback that is called when an update event is received. */ onUpdateEvent?: (data: UpdatesStreamEvent>["data"], options: { namespace: string[] | undefined; mutate: (update: Partial | ((prev: StateType) => Partial)) => void; }) => void; /** * Callback that is called when a custom event is received. */ onCustomEvent?: (data: CustomStreamEvent>["data"], options: { namespace: string[] | undefined; mutate: (update: Partial | ((prev: StateType) => Partial)) => void; }) => void; /** * Callback that is called when a metadata event is received. */ onMetadataEvent?: (data: MetadataStreamEvent["data"]) => void; /** * Callback that is called when a LangChain event is received. * @see https://langchain-ai.github.io/langgraph/cloud/how-tos/stream_events/#stream-graph-in-events-mode for more details. */ onLangChainEvent?: (data: EventsStreamEvent["data"]) => void; /** * Callback that is called when a debug event is received. * @internal This API is experimental and subject to change. */ onDebugEvent?: (data: DebugStreamEvent["data"], options: { namespace: string[] | undefined; }) => void; /** * Callback that is called when a checkpoints event is received. */ onCheckpointEvent?: (data: CheckpointsStreamEvent["data"], options: { namespace: string[] | undefined; }) => void; /** * Callback that is called when a tasks event is received. */ onTaskEvent?: (data: TasksStreamEvent>["data"], options: { namespace: string[] | undefined; }) => void; /** * Callback that is called when a tool lifecycle event is received. */ onToolEvent?: (data: ToolsStreamEvent["data"], options: { namespace: string[] | undefined; mutate: (update: Partial | ((prev: StateType) => Partial)) => void; }) => void; /** * Callback that is called when the stream is stopped by the user. * Provides a mutate function to update the stream state immediately * without requiring a server roundtrip. * * @example * ```typescript * onStop: ({ mutate }) => { * mutate((prev) => ({ * ...prev, * ui: prev.ui?.map(component => * component.props.isLoading * ? { ...component, props: { ...component.props, stopped: true, isLoading: false }} * : component * ) * })); * } * ``` */ onStop?: (options: { mutate: (update: Partial | ((prev: StateType) => Partial)) => void; }) => void; /** * The ID of the thread to fetch history and current values from. */ threadId?: string | null; /** * Callback that is called when the thread ID is updated (ie when a new thread is created). */ onThreadId?: (threadId: string) => void; /** Will reconnect the stream on mount */ reconnectOnMount?: boolean | (() => RunMetadataStorage); /** * Initial values to display immediately when loading a thread. * Useful for displaying cached thread data while official history loads. * These values will be replaced when official thread data is fetched. * * Note: UI components from initialValues will render immediately if they're * predefined in LoadExternalComponent's components prop, providing instant * cached UI display without server fetches. */ initialValues?: StateType | null; /** * Whether to fetch the history of the thread. * If true, the history will be fetched from the server. Defaults to 10 entries. * If false, only the last state will be fetched from the server. * @default true */ fetchStateHistory?: boolean | { limit: number; }; /** * Manage the thread state externally. */ thread?: UseStreamThread; /** * Throttle the stream. * If a number is provided, the stream will be throttled to the given number of milliseconds. * If `true`, updates are batched in a single macrotask. * If `false`, updates are not throttled or batched. * @default true */ throttle?: number | boolean; /** * Headless tool implementations to execute locally when the agent interrupts * with a schema-only `tool({ ... })` call from LangChain. */ tools?: AnyHeadlessToolImplementation[]; /** * Callback for headless tool lifecycle events. */ onTool?: OnToolCallback; } /** * Union of all stream options types. * * Used internally by the implementation to accept any options type. * This allows the implementation functions to handle options from * any agent type while maintaining type safety at the public API level. * * @internal */ type AnyStreamOptions = Record, Bag extends BagTemplate = BagTemplate> = UseStreamOptions & { subagentToolNames?: string[]; filterSubagentMessages?: boolean; toMessage?: (chunk: BaseMessage) => Message | BaseMessage; tools?: AnyHeadlessToolImplementation[]; onTool?: OnToolCallback; }; interface RunMetadataStorage { getItem(key: `lg:stream:${string}`): string | null; setItem(key: `lg:stream:${string}`, value: string): void; removeItem(key: `lg:stream:${string}`): void; } type ConfigWithConfigurable> = Config & { configurable?: ConfigurableType; }; interface SubmitOptions = Record, ContextType extends Record = Record> { config?: ConfigWithConfigurable; context?: ContextType; checkpoint?: Omit | null; command?: Command; interruptBefore?: "*" | string[]; interruptAfter?: "*" | string[]; metadata?: Metadata; multitaskStrategy?: MultitaskStrategy; onCompletion?: OnCompletionBehavior; onDisconnect?: DisconnectMode; feedbackKeys?: string[]; streamMode?: Array; runId?: string; optimisticValues?: Partial | ((prev: StateType) => Partial); /** * Whether or not to stream the nodes of any subgraphs called * by the assistant. * @default false */ streamSubgraphs?: boolean; /** * Mark the stream as resumable. All events emitted during the run will be temporarily persisted * in order to be re-emitted if the stream is re-joined. * @default false */ streamResumable?: boolean; /** * Whether to checkpoint during the run (or only at the end/interruption). * - `"async"`: Save checkpoint asynchronously while the next step executes (default). * - `"sync"`: Save checkpoint synchronously before the next step starts. * - `"exit"`: Save checkpoint only when the graph exits. * @default "async" */ durability?: Durability; /** * The ID to use when creating a new thread. When provided, this ID will be used * for thread creation when threadId is `null` or `undefined`. * This enables optimistic UI updates where you know the thread ID * before the thread is actually created. */ threadId?: string; /** * Callback that is called when an error occurs during this specific submit call. * Unlike the hook-level `onError`, this allows handling errors on a per-submit basis, * e.g. to show a retry button or a specific error message to the user. */ onError?: (error: unknown, run: RunCallbackMeta | undefined) => void; } /** * Payload for the `stream` method of the `UseStreamTransport` interface. * @template StateType - The type of the stream's state values. * @template Bag - The type of the stream's bag values. */ interface UseStreamTransportPayload = Record, Bag extends BagTemplate = BagTemplate> { input: GetUpdateType | null | undefined; context: GetConfigurableType | undefined; command: Command | undefined; config: ConfigWithConfigurable> | undefined; streamSubgraphs?: boolean; signal: AbortSignal; } /** * Transport used to stream the thread. * Only applicable for custom endpoints using `toLangGraphEventStream` or `toLangGraphEventStreamResponse`. */ interface UseStreamTransport = Record, Bag extends BagTemplate = BagTemplate> { stream: (payload: UseStreamTransportPayload) => Promise>; } type UseStreamCustomOptions = Record, Bag extends BagTemplate = BagTemplate> = Pick, "messagesKey" | "threadId" | "onThreadId" | "onError" | "onFinish" | "onCreated" | "onUpdateEvent" | "onCustomEvent" | "onMetadataEvent" | "onLangChainEvent" | "onDebugEvent" | "onCheckpointEvent" | "onTaskEvent" | "onStop" | "initialValues" | "throttle" | "onToolEvent" | "tools" | "onTool"> & { transport: UseStreamTransport; }; /** * Union of all custom stream options types. * * Used internally by the implementation to accept any custom options type. * This allows the implementation functions to handle options from * any agent type while maintaining type safety at the public API level. * * @internal */ type AnyStreamCustomOptions = Record, Bag extends BagTemplate = BagTemplate> = UseStreamCustomOptions & { subagentToolNames?: string[]; filterSubagentMessages?: boolean; toMessage?: (chunk: BaseMessage) => Message | BaseMessage; tools?: AnyHeadlessToolImplementation[]; onTool?: OnToolCallback; }; type CustomSubmitOptions = Record, ConfigurableType extends Record = Record> = Pick, "optimisticValues" | "context" | "command" | "config" | "onError" | "threadId" | "streamSubgraphs">; //#endregion export { AcceptBaseMessages, AgentMiddlewareLike, AgentTypeConfigLike, AnyStreamCustomOptions, AnyStreamOptions, BaseSubagentState, CompiledSubAgentLike, CustomSubmitOptions, DeepAgentTypeConfigLike, DefaultSubagentStates, ExtractAgentConfig, ExtractDeepAgentConfig, ExtractSubAgentMiddleware, ExtractToolCallsFromState, GetConfigurableType, GetCustomEventType, GetInterruptType, GetToolCallsType, GetUpdateType, InferAgentState, InferAgentToolCalls, InferDeepAgentSubagents, InferMiddlewareStatesFromArray, InferSubagentByName, InferSubagentNames, InferSubagentState, IsAgentLike, IsDeepAgentLike, MessageMetadata, RunCallbackMeta, StreamBase, SubAgentLike, SubagentApi, SubagentStateMap, SubagentStatus, SubagentStream, SubagentStreamInterface, SubagentToolCall, SubmitOptions, UseStreamCustomOptions, UseStreamOptions, UseStreamThread, UseStreamTransport, UseStreamTransportPayload }; //# sourceMappingURL=types.d.ts.map