import { BaseChannel } from "../channels/base.js"; import { Command, CommandInstance, END, INTERRUPT, Interrupt, START } from "../constants.js"; import { SchemaMetaRegistry } from "./zod/meta.js"; import { RunnableLike, Runtime } from "../pregel/runnable_types.js"; import { AnnotationRoot, SingleReducer, StateDefinition, StateType } from "./annotation.js"; import { CachePolicy, RetryPolicy } from "../pregel/utils/index.js"; import { AddNodeOptions, Branch, CompiledGraph, Graph, NodeSpec } from "./graph.js"; import { InferInterruptInputType, InferInterruptResumeType } from "../interrupt.js"; import { InferWriterType } from "../writer.js"; import { AnyStateSchema } from "../state/schema.js"; import { ContextSchemaInit, ExtractStateType, ExtractUpdateType, StateDefinitionInit, StateGraphInit, StateGraphOptions, ToStateDefinition } from "./types.js"; import { All, BaseCache, BaseCheckpointSaver, BaseStore } from "@langchain/langgraph-checkpoint"; import { InteropZodObject } from "@langchain/core/utils/types"; //#region src/graph/state.d.ts type ChannelReducers = { [K in keyof Channels]: SingleReducer }; interface StateGraphArgs { channels: Channels extends object ? Channels extends unknown[] ? ChannelReducers<{ __root__: Channels; }> : ChannelReducers : ChannelReducers<{ __root__: Channels; }>; } type StateGraphNodeSpec = NodeSpec & { input?: StateDefinition; retryPolicy?: RetryPolicy; cachePolicy?: CachePolicy; }; /** * Options for StateGraph.addNode() method. * * @template Nodes - Node name constraints * @template InputSchema - Per-node input schema type (inferred from options.input) */ type StateGraphAddNodeOptions = { retryPolicy?: RetryPolicy; cachePolicy?: CachePolicy | boolean; input?: InputSchema; } & AddNodeOptions; type StateGraphArgsWithStateSchema = { stateSchema: AnnotationRoot; input?: AnnotationRoot; output?: AnnotationRoot; }; type StateGraphArgsWithInputOutputSchemas = { input: AnnotationRoot; output: AnnotationRoot; }; type ExtractStateDefinition = T extends AnyStateSchema ? T : T extends StateDefinitionInit ? ToStateDefinition : StateDefinition; type NodeAction = RunnableLike : U, Runtime>, InterruptType, WriterType>>; type StrictNodeAction = RunnableLike, U | Command, U & Record, Nodes>, Runtime>, InterruptType, WriterType>>; declare const PartialStateSchema: unique symbol; type PartialStateSchema = typeof PartialStateSchema; type MergeReturnType = Prev & Curr extends infer T ? { [K in keyof T]: T[K] } & unknown : never; type Prettify = { [K in keyof T]: T[K] } & {}; /** * A graph whose nodes communicate by reading and writing to a shared state. * Each node takes a defined `State` as input and returns a `Partial`. * * Each state key can optionally be annotated with a reducer function that * will be used to aggregate the values of that key received from multiple nodes. * The signature of a reducer function is (left: Value, right: UpdateValue) => Value. * * See {@link Annotation} for more on defining state. * * After adding nodes and edges to your graph, you must call `.compile()` on it before * you can use it. * * @typeParam SD - The state definition used to construct the graph. Can be an * {@link AnnotationRoot}, {@link StateSchema}, or Zod object schema. This is the * primary generic from which `S` and `U` are derived. * * @typeParam S - The full state type representing the complete shape of your graph's * state after all reducers have been applied. Automatically inferred from `SD`. * * @typeParam U - The update type representing what nodes can return to modify state. * Typically a partial of the state type. Automatically inferred from `SD`. * * @typeParam N - Union of all node names in the graph (e.g., `"agent" | "tool"`). * Accumulated as you call `.addNode()`. Used for type-safe routing. * * @typeParam I - The input schema definition. Set via the `input` option in the * constructor to restrict what data the graph accepts when invoked. * * @typeParam O - The output schema definition. Set via the `output` option in the * constructor to restrict what data the graph returns after execution. * * @typeParam C - The config/context schema definition. Set via the `context` option * to define additional configuration passed at runtime. * * @typeParam NodeReturnType - Constrains what types nodes in this graph can return. * * @typeParam InterruptType - The type for {@link interrupt} resume values. Set via * the `interrupt` option for typed human-in-the-loop patterns. * * @typeParam WriterType - The type for custom stream writers. Set via the `writer` * option to enable typed custom streaming from within nodes. * * @example * ```ts * import { * type BaseMessage, * AIMessage, * HumanMessage, * } from "@langchain/core/messages"; * import { StateGraph, Annotation } from "@langchain/langgraph"; * * // Define a state with a single key named "messages" that will * // combine a returned BaseMessage or arrays of BaseMessages * const StateAnnotation = Annotation.Root({ * sentiment: Annotation, * messages: Annotation({ * reducer: (left: BaseMessage[], right: BaseMessage | BaseMessage[]) => { * if (Array.isArray(right)) { * return left.concat(right); * } * return left.concat([right]); * }, * default: () => [], * }), * }); * * const graphBuilder = new StateGraph(StateAnnotation); * * // A node in the graph that returns an object with a "messages" key * // will update the state by combining the existing value with the returned one. * const myNode = (state: typeof StateAnnotation.State) => { * return { * messages: [new AIMessage("Some new response")], * sentiment: "positive", * }; * }; * * const graph = graphBuilder * .addNode("myNode", myNode) * .addEdge("__start__", "myNode") * .addEdge("myNode", "__end__") * .compile(); * * await graph.invoke({ messages: [new HumanMessage("how are you?")] }); * * // { * // messages: [HumanMessage("how are you?"), AIMessage("Some new response")], * // sentiment: "positive", * // } * ``` */ declare class StateGraph, U = ExtractUpdateType, N extends string = typeof START, I extends StateDefinitionInit = ExtractStateDefinition, O extends StateDefinitionInit = ExtractStateDefinition, C extends StateDefinitionInit = StateDefinition, NodeReturnType = unknown, InterruptType = unknown, WriterType = unknown> extends Graph, ToStateDefinition> { channels: Record; waitingEdges: Set<[N[], N]>; /** @internal */ _schemaDefinition: StateDefinition; /** @internal */ _schemaRuntimeDefinition: InteropZodObject | AnyStateSchema | undefined; /** @internal */ _inputDefinition: I; /** @internal */ _inputRuntimeDefinition: InteropZodObject | AnyStateSchema | PartialStateSchema | undefined; /** @internal */ _outputDefinition: O; /** @internal */ _outputRuntimeDefinition: InteropZodObject | AnyStateSchema | undefined; /** * Map schemas to managed values * @internal */ _schemaDefinitions: Map; /** @internal */ _metaRegistry: SchemaMetaRegistry; /** @internal Used only for typing. */ _configSchema: ToStateDefinition | undefined; /** @internal */ _configRuntimeSchema: InteropZodObject | undefined; /** @internal */ _interrupt: InterruptType; /** @internal */ _writer: WriterType; Node: StrictNodeAction; /** * Create a new StateGraph for building stateful, multi-step workflows. * * Accepts state definitions via `Annotation.Root`, `StateSchema`, or Zod schemas. * * @example Direct schema * ```ts * const StateAnnotation = Annotation.Root({ * messages: Annotation({ reducer: (a, b) => [...a, ...b] }), * }); * const graph = new StateGraph(StateAnnotation); * ``` * * @example Direct schema with input/output filtering * ```ts * const graph = new StateGraph(StateAnnotation, { * input: InputSchema, * output: OutputSchema, * }); * ``` * * @example Object pattern with state, input, output * ```ts * const graph = new StateGraph({ * state: FullStateSchema, * input: InputSchema, * output: OutputSchema, * }); * ``` * * @example Input/output only (state inferred from input) * ```ts * const graph = new StateGraph({ * input: InputAnnotation, * output: OutputAnnotation, * }); * ``` */ constructor(state: SD extends StateDefinitionInit ? SD : never, options?: C | AnnotationRoot> | StateGraphOptions); constructor(fields: SD extends StateDefinition ? StateGraphArgsWithInputOutputSchemas> : never, contextSchema?: C | AnnotationRoot>); constructor(fields: SD extends StateDefinition ? AnnotationRoot | StateGraphArgsWithStateSchema, ToStateDefinition> : never, contextSchema?: C | AnnotationRoot>); constructor(init: Omit, "state" | "stateSchema" | "input"> & { input: SD extends StateDefinitionInit ? SD : never; state?: never; stateSchema?: never; }, contextSchema?: C | AnnotationRoot>); constructor(init: StateGraphInit); /** @deprecated Use `Annotation.Root`, `StateSchema`, or Zod schemas instead. */ constructor(fields: StateGraphArgs, contextSchema?: C | AnnotationRoot>); /** * Normalize all constructor input patterns to a unified StateGraphInit object. * @internal */ private _normalizeToStateGraphInit; /** * Convert any supported schema type to a StateDefinition (channel map). * @internal */ private _getChannelsFromSchema; get allEdges(): Set<[string, string]>; _addSchema(stateDefinition: StateDefinitionInit): void; addNode>>(nodes: NodeMap): StateGraph ? U : never }>>; addNode(nodes: [key: K, action: NodeAction, options?: StateGraphAddNodeOptions][]): StateGraph>; addNode(key: K, action: NodeAction, NodeOutput, C, InterruptType, WriterType>, options: StateGraphAddNodeOptions): StateGraph>; addNode(key: K, action: NodeAction, NodeOutput, C, InterruptType, WriterType>, options: StateGraphAddNodeOptions): StateGraph>; addNode(key: K, action: NodeAction, options?: StateGraphAddNodeOptions): StateGraph>; addNode(key: K, action: NodeAction, options?: StateGraphAddNodeOptions): StateGraph; addEdge(startKey: typeof START | N | N[], endKey: N | typeof END): this; addSequence(nodes: [key: K, action: NodeAction, options?: StateGraphAddNodeOptions][]): StateGraph>; addSequence>>(nodes: NodeMap): StateGraph ? U : never }>>; compile({ checkpointer, store, cache, interruptBefore, interruptAfter, name, description }?: { checkpointer?: BaseCheckpointSaver | boolean; store?: BaseStore; cache?: BaseCache; interruptBefore?: N[] | All; interruptAfter?: N[] | All; name?: string; description?: string; }): CompiledStateGraph, Prettify, N, I, O, C, NodeReturnType, InterruptType, WriterType>; } /** * Final result from building and compiling a {@link StateGraph}. * Should not be instantiated directly, only using the StateGraph `.compile()` * instance method. * * @typeParam S - The full state type representing the complete shape of your graph's * state after all reducers have been applied. This is the type you receive when * reading state in nodes or after invoking the graph. * * @typeParam U - The update type representing what nodes can return to modify state. * Typically a partial of the state type, allowing nodes to update only specific fields. * Can also include {@link Command} objects for advanced control flow. * * @typeParam N - Union of all node names in the graph (e.g., `"agent" | "tool"`). * Used for type-safe routing with {@link Command.goto} and edge definitions. * * @typeParam I - The input schema definition. Determines what shape of data the graph * accepts when invoked. Defaults to the main state schema if not explicitly set. * * @typeParam O - The output schema definition. Determines what shape of data the graph * returns after execution. Defaults to the main state schema if not explicitly set. * * @typeParam C - The config/context schema definition. Defines additional configuration * that can be passed to the graph at runtime via {@link LangGraphRunnableConfig}. * * @typeParam NodeReturnType - Constrains what types nodes in this graph can return. * Useful for enforcing consistent return patterns across all nodes. * * @typeParam InterruptType - The type of values that can be passed when resuming from * an {@link interrupt}. Used with human-in-the-loop patterns. * * @typeParam WriterType - The type for custom stream writers. Used with the `writer` * option to enable typed custom streaming from within nodes. */ declare class CompiledStateGraph extends CompiledGraph, ExtractUpdateType>, ExtractStateType, NodeReturnType, CommandInstance, Prettify, N>, InferWriterType> { builder: StateGraph; /** * The description of the compiled graph. * This is used by the supervisor agent to describe the handoff to the agent. */ description?: string; /** @internal */ _metaRegistry: SchemaMetaRegistry; constructor({ description, ...rest }: { description?: string; } & ConstructorParameters, ExtractUpdateType>, ExtractStateType, NodeReturnType, CommandInstance, Prettify, N>, InferWriterType>>[0]); attachNode(key: typeof START, node?: never): void; attachNode(key: N, node: StateGraphNodeSpec): void; attachEdge(starts: N | N[] | "__start__", end: N | "__end__"): void; attachBranch(start: N | typeof START, _: string, branch: Branch, options?: { withReader?: boolean; }): void; protected _validateInput(input: ExtractUpdateType>): Promise>>; isInterrupted(input: unknown): input is { [INTERRUPT]: Interrupt>[]; }; protected _validateContext(config: Partial>): Promise>>; } //#endregion export { CompiledStateGraph, StateGraph, StateGraphAddNodeOptions, StateGraphArgs, StateGraphArgsWithInputOutputSchemas, StateGraphArgsWithStateSchema, StateGraphNodeSpec }; //# sourceMappingURL=state.d.ts.map