@langchain/langgraph
Version:
181 lines (180 loc) • 9.82 kB
text/typescript
import { BaseChannel } from "../channels/base.cjs";
import { END, START, Send } from "../constants.cjs";
import { LangGraphRunnableConfig, RunnableLike as RunnableLike$1 } from "../pregel/runnable_types.cjs";
import { StateDefinition, StateType } from "./annotation.cjs";
import { RunnableCallable } from "../utils.cjs";
import { PregelNode } from "../pregel/read.cjs";
import { StreamTransformer } from "../stream/types.cjs";
import { PregelOptions, PregelParams } from "../pregel/types.cjs";
import { Pregel } from "../pregel/index.cjs";
import { NodeError } from "../errors.cjs";
import { GraphNodeReturnValue } from "./types.cjs";
import { All, BaseCheckpointSaver } from "@langchain/langgraph-checkpoint";
import { Runnable, RunnableConfig } from "@langchain/core/runnables";
import { Graph } from "@langchain/core/runnables/graph";
//#region src/graph/graph.d.ts
interface BranchOptions<IO, N extends string, CallOptions extends LangGraphRunnableConfig = LangGraphRunnableConfig> {
source: N;
path: RunnableLike$1<IO, BranchPathReturnValue, CallOptions>;
pathMap?: Record<string, N | typeof END> | (N | typeof END)[];
}
type BranchPathReturnValue = string | Send | (string | Send)[] | Promise<string | Send | (string | Send)[]>;
type CompiledGraphTypeNode<Spec> = Spec extends {
node: infer N extends string;
} ? N : any;
type CompiledGraphTypeContext<Spec> = Spec extends {
context: infer Context extends Record<string, any>;
} ? Context : Record<string, any>;
type CompiledGraphTypeStreamTransformers<Spec> = Spec extends {
streamTransformers: infer Transformers;
} ? Transformers extends ReadonlyArray<() => StreamTransformer<any>> ? Transformers : Transformers extends ReadonlyArray<StreamTransformer<any>> ? { readonly [K in keyof Transformers]: () => Transformers[K] } : Transformers extends StreamTransformer<any> ? readonly [() => Transformers] : [] : [];
/**
* Convenience type for referencing a compiled graph by named type slots.
*
* @example
* ```ts
* type MyCompiledGraph = CompiledGraphType<{
* state: State;
* update: Update;
* streamTransformers: [
* StreamTransformer<Extensions>,
* StreamTransformer<MoreExtensions>,
* ];
* }>;
* ```
*/
type CompiledGraphType<Spec extends object = object> = CompiledGraph<CompiledGraphTypeNode<Spec>, Spec extends {
state: infer State;
} ? State : any, Spec extends {
update: infer Update;
} ? Update : any, CompiledGraphTypeContext<Spec>, Spec extends {
input: infer Input;
} ? Input : any, Spec extends {
output: infer Output;
} ? Output : any, Spec extends {
nodeReturn: infer NodeReturn;
} ? NodeReturn : unknown, Spec extends {
command: infer Command;
} ? Command : unknown, Spec extends {
streamCustom: infer StreamCustom;
} ? StreamCustom : any, CompiledGraphTypeStreamTransformers<Spec>>;
type NodeAction<S, U, C extends StateDefinition> = RunnableLike$1<S, U extends object ? U & Record<string, any> : U, LangGraphRunnableConfig<StateType<C>>>;
declare class Branch<IO, N extends string, CallOptions extends LangGraphRunnableConfig = LangGraphRunnableConfig> {
path: Runnable<IO, BranchPathReturnValue>;
ends?: Record<string, N | typeof END>;
constructor(options: Omit<BranchOptions<IO, N, CallOptions>, "source">);
run(writer: (dests: (string | Send)[], config: LangGraphRunnableConfig) => Runnable | void | Promise<void>, reader?: (config: CallOptions) => IO): RunnableCallable<unknown, unknown>;
_route(input: IO, config: CallOptions, writer: (dests: (string | Send)[], config: LangGraphRunnableConfig) => Runnable | void | Promise<void>, reader?: (config: CallOptions) => IO): Promise<Runnable | any>;
}
type NodeSpec<RunInput, RunOutput> = {
runnable: Runnable<RunInput, RunOutput>;
metadata?: Record<string, unknown>;
subgraphs?: Pregel<any, any>[];
ends?: string[];
defer?: boolean; /** Whether this node is an auto-generated node-level error handler. */
isErrorHandler?: boolean; /** Name of the auto-generated error handler node to run on failure. */
errorHandlerNode?: string;
};
/**
* Return value type for node-level error handlers.
*
* Handlers may return a partial state update, a `Command`, or a Promise of either.
*
* @template Update - The update type (what fields can be returned)
* @template Nodes - Union of valid node names for Command.goto
*/
type NodeErrorHandlerReturnValue<Update, Nodes extends string = string> = GraphNodeReturnValue<Update, Nodes>;
/**
* A node-level error handler callable.
*
* Invoked with the node input state, a {@link NodeError} describing the failed
* node and thrown error, and the runnable config. The handler runs ONLY after
* the failing node's {@link RetryPolicy} is exhausted. It may return a state
* update or a `Command` (to route via `goto`).
*/
type NodeErrorHandler<TState = unknown, TUpdate = Partial<TState>, Nodes extends string = string> = (state: TState, error: NodeError, config?: LangGraphRunnableConfig) => NodeErrorHandlerReturnValue<TUpdate, Nodes>;
type AddNodeOptions<Nodes extends string = string> = {
metadata?: Record<string, unknown>;
subgraphs?: Pregel<any, any>[];
ends?: Nodes[];
defer?: boolean;
};
declare class Graph$1<N extends string = typeof START | typeof END, RunInput = any, RunOutput = any, NodeSpecType extends NodeSpec<RunInput, RunOutput> = NodeSpec<RunInput, RunOutput>, C extends StateDefinition = StateDefinition> {
nodes: Record<N, NodeSpecType>;
edges: Set<[N | typeof START, N | typeof END]>;
branches: Record<string, Record<string, Branch<RunInput, N, any>>>;
entryPoint?: string;
compiled: boolean;
constructor();
protected warnIfCompiled(message: string): void;
get allEdges(): Set<[string, string]>;
addNode<K extends string, NodeInput = RunInput, NodeOutput = RunOutput>(nodes: Record<K, NodeAction<NodeInput, NodeOutput, C>> | [key: K, action: NodeAction<NodeInput, NodeOutput, C>, options?: AddNodeOptions][]): Graph$1<N | K, RunInput, RunOutput>;
addNode<K extends string, NodeInput = RunInput, NodeOutput = RunOutput>(key: K, action: NodeAction<NodeInput, NodeOutput, C>, options?: AddNodeOptions): Graph$1<N | K, RunInput, RunOutput>;
addEdge(startKey: N | typeof START, endKey: N | typeof END): this;
addConditionalEdges(source: BranchOptions<RunInput, N, LangGraphRunnableConfig<StateType<C>>>): this;
addConditionalEdges(source: N, path: RunnableLike$1<RunInput, BranchPathReturnValue, LangGraphRunnableConfig<StateType<C>>>, pathMap?: BranchOptions<RunInput, N, LangGraphRunnableConfig<StateType<C>>>["pathMap"]): this;
/**
* @deprecated use `addEdge(START, key)` instead
*/
setEntryPoint(key: N): this;
/**
* @deprecated use `addEdge(key, END)` instead
*/
setFinishPoint(key: N): this;
compile<const TTransformers extends ReadonlyArray<() => StreamTransformer<any>> = []>({
checkpointer,
interruptBefore,
interruptAfter,
name,
transformers
}?: {
checkpointer?: BaseCheckpointSaver | false;
interruptBefore?: N[] | All;
interruptAfter?: N[] | All;
name?: string;
/**
* Stream transformer factories baked into the compiled graph. These run
* automatically for every `streamEvents(..., { version: "v3" })` call,
* before any call-site transformers.
*/
transformers?: TTransformers;
}): CompiledGraph<N, RunInput, RunOutput, Record<string, any>, any, any, unknown, unknown, any, TTransformers>;
validate(interrupt?: string[]): void;
}
declare class CompiledGraph<N extends string, State = any, Update = any, ContextType extends Record<string, any> = Record<string, any>, InputType = any, OutputType = any, NodeReturnType = unknown, CommandType = unknown, StreamCustomType = any, TStreamTransformers extends ReadonlyArray<() => StreamTransformer<any>> = []> extends Pregel<Record<N | typeof START, PregelNode<State, Update>>, Record<N | typeof START | typeof END | string, BaseChannel>, ContextType & Record<string, any>, InputType, OutputType, InputType, OutputType, NodeReturnType, CommandType, StreamCustomType, TStreamTransformers> {
"~NodeType": N;
"~NodeReturnType": NodeReturnType;
"~RunInput": Update;
"~RunOutput": State;
builder: Graph$1<N, State, Update>;
constructor({
builder,
...rest
}: {
builder: Graph$1<N, State, Update>;
} & PregelParams<Record<N | typeof START, PregelNode<State, Update>>, Record<N | typeof START | typeof END | string, BaseChannel>, TStreamTransformers>);
withConfig<const TTransformers extends ReadonlyArray<() => StreamTransformer<any>> = []>(config: Omit<LangGraphRunnableConfig, "store" | "writer" | "interrupt"> & {
streamTransformers: TTransformers;
}): CompiledGraph<N, State, Update, ContextType, InputType, OutputType, NodeReturnType, CommandType, StreamCustomType, readonly [...TStreamTransformers, ...TTransformers]>;
withConfig(config: PregelOptions<Record<N | typeof START, PregelNode<State, Update>>, Record<N | typeof START | typeof END | string, BaseChannel>, ContextType & Record<string, any>>): this;
attachNode(key: N, node: NodeSpec<State, Update>): void;
attachEdge(start: N | typeof START, end: N | typeof END): void;
attachBranch(start: N | typeof START, name: string, branch: Branch<State, N>): void;
/**
* Returns a drawable representation of the computation graph.
*/
getGraphAsync(config?: RunnableConfig & {
xray?: boolean | number;
}): Promise<Graph>;
/**
* Returns a drawable representation of the computation graph.
*
* @deprecated Use getGraphAsync instead. The async method will be the default in the next minor core release.
*/
getGraph(config?: RunnableConfig & {
xray?: boolean | number;
}): Graph;
}
//#endregion
export { AddNodeOptions, Branch, CompiledGraph, CompiledGraphType, Graph$1 as Graph, NodeErrorHandler, NodeSpec };
//# sourceMappingURL=graph.d.cts.map