@langchain/langgraph 0.2.73 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/channels/any_value.cjs +3 -0
- package/dist/channels/any_value.d.ts +1 -0
- package/dist/channels/any_value.js +3 -0
- package/dist/channels/any_value.js.map +1 -1
- package/dist/channels/base.cjs +35 -6
- package/dist/channels/base.d.ts +17 -2
- package/dist/channels/base.js +31 -2
- package/dist/channels/base.js.map +1 -1
- package/dist/channels/binop.cjs +3 -0
- package/dist/channels/binop.d.ts +1 -0
- package/dist/channels/binop.js +3 -0
- package/dist/channels/binop.js.map +1 -1
- package/dist/channels/dynamic_barrier_value.cjs +119 -9
- package/dist/channels/dynamic_barrier_value.d.ts +29 -0
- package/dist/channels/dynamic_barrier_value.js +117 -8
- package/dist/channels/dynamic_barrier_value.js.map +1 -1
- package/dist/channels/ephemeral_value.cjs +3 -0
- package/dist/channels/ephemeral_value.d.ts +1 -0
- package/dist/channels/ephemeral_value.js +3 -0
- package/dist/channels/ephemeral_value.js.map +1 -1
- package/dist/channels/last_value.cjs +82 -4
- package/dist/channels/last_value.d.ts +21 -0
- package/dist/channels/last_value.js +80 -3
- package/dist/channels/last_value.js.map +1 -1
- package/dist/channels/named_barrier_value.cjs +94 -1
- package/dist/channels/named_barrier_value.d.ts +23 -0
- package/dist/channels/named_barrier_value.js +92 -0
- package/dist/channels/named_barrier_value.js.map +1 -1
- package/dist/channels/topic.cjs +3 -0
- package/dist/channels/topic.d.ts +1 -0
- package/dist/channels/topic.js +3 -0
- package/dist/channels/topic.js.map +1 -1
- package/dist/constants.cjs +33 -12
- package/dist/constants.d.ts +48 -27
- package/dist/constants.js +27 -7
- package/dist/constants.js.map +1 -1
- package/dist/errors.cjs +4 -4
- package/dist/errors.js.map +1 -1
- package/dist/func/index.cjs +15 -7
- package/dist/func/index.d.ts +12 -3
- package/dist/func/index.js +12 -4
- package/dist/func/index.js.map +1 -1
- package/dist/func/types.cjs +1 -2
- package/dist/func/types.d.ts +1 -1
- package/dist/graph/annotation.cjs +2 -2
- package/dist/graph/annotation.js.map +1 -1
- package/dist/graph/graph.cjs +46 -40
- package/dist/graph/graph.d.ts +14 -4
- package/dist/graph/graph.js +46 -40
- package/dist/graph/graph.js.map +1 -1
- package/dist/graph/index.cjs +3 -1
- package/dist/graph/index.d.ts +2 -2
- package/dist/graph/index.js +2 -2
- package/dist/graph/index.js.map +1 -1
- package/dist/graph/message.cjs +43 -5
- package/dist/graph/message.d.ts +5 -0
- package/dist/graph/message.js +40 -3
- package/dist/graph/message.js.map +1 -1
- package/dist/graph/message.test.cjs +196 -0
- package/dist/graph/message.test.d.ts +1 -0
- package/dist/graph/message.test.js +194 -0
- package/dist/graph/message.test.js.map +1 -0
- package/dist/graph/messages_annotation.cjs +54 -1
- package/dist/graph/messages_annotation.d.ts +47 -0
- package/dist/graph/messages_annotation.js +53 -0
- package/dist/graph/messages_annotation.js.map +1 -1
- package/dist/graph/state.cjs +162 -104
- package/dist/graph/state.d.ts +36 -11
- package/dist/graph/state.js +163 -106
- package/dist/graph/state.js.map +1 -1
- package/dist/graph/zod/plugin.js.map +1 -1
- package/dist/graph/zod/schema.cjs +17 -67
- package/dist/graph/zod/schema.js +12 -61
- package/dist/graph/zod/schema.js.map +1 -1
- package/dist/graph/zod/state.cjs +69 -7
- package/dist/graph/zod/state.d.ts +10 -1
- package/dist/graph/zod/state.js +61 -0
- package/dist/graph/zod/state.js.map +1 -1
- package/dist/interrupt.cjs +1 -2
- package/dist/interrupt.js.map +1 -1
- package/dist/managed/base.cjs +3 -3
- package/dist/managed/base.js.map +1 -1
- package/dist/managed/shared_value.js.map +1 -1
- package/dist/prebuilt/agentName.cjs +3 -4
- package/dist/prebuilt/agentName.js.map +1 -1
- package/dist/prebuilt/agent_executor.cjs +1 -2
- package/dist/prebuilt/agent_executor.d.ts +1 -1
- package/dist/prebuilt/agent_executor.js.map +1 -1
- package/dist/prebuilt/chat_agent_executor.cjs +1 -2
- package/dist/prebuilt/chat_agent_executor.js.map +1 -1
- package/dist/prebuilt/react_agent_executor.cjs +103 -49
- package/dist/prebuilt/react_agent_executor.d.ts +22 -6
- package/dist/prebuilt/react_agent_executor.js +99 -45
- package/dist/prebuilt/react_agent_executor.js.map +1 -1
- package/dist/prebuilt/tool_executor.js.map +1 -1
- package/dist/prebuilt/tool_node.cjs +2 -2
- package/dist/prebuilt/tool_node.js.map +1 -1
- package/dist/pregel/algo.cjs +68 -27
- package/dist/pregel/algo.d.ts +1 -1
- package/dist/pregel/algo.js +62 -21
- package/dist/pregel/algo.js.map +1 -1
- package/dist/pregel/call.cjs +5 -5
- package/dist/pregel/call.d.ts +3 -2
- package/dist/pregel/call.js +2 -1
- package/dist/pregel/call.js.map +1 -1
- package/dist/pregel/debug.cjs +10 -10
- package/dist/pregel/debug.d.ts +3 -3
- package/dist/pregel/debug.js.map +1 -1
- package/dist/pregel/debug.test.cjs +37 -31
- package/dist/pregel/debug.test.js +18 -12
- package/dist/pregel/debug.test.js.map +1 -1
- package/dist/pregel/index.cjs +99 -29
- package/dist/pregel/index.d.ts +19 -6
- package/dist/pregel/index.js +100 -30
- package/dist/pregel/index.js.map +1 -1
- package/dist/pregel/io.cjs +8 -9
- package/dist/pregel/io.js +2 -2
- package/dist/pregel/io.js.map +1 -1
- package/dist/pregel/io.mapCommand.test.cjs +29 -29
- package/dist/pregel/io.mapCommand.test.js +5 -5
- package/dist/pregel/io.mapCommand.test.js.map +1 -1
- package/dist/pregel/loop.cjs +126 -26
- package/dist/pregel/loop.d.ts +29 -2
- package/dist/pregel/loop.js +127 -27
- package/dist/pregel/loop.js.map +1 -1
- package/dist/pregel/messages.cjs +15 -13
- package/dist/pregel/messages.d.ts +1 -1
- package/dist/pregel/messages.js +15 -13
- package/dist/pregel/messages.js.map +1 -1
- package/dist/pregel/messages.test.cjs +105 -105
- package/dist/pregel/messages.test.js +31 -31
- package/dist/pregel/messages.test.js.map +1 -1
- package/dist/pregel/read.cjs +12 -1
- package/dist/pregel/read.d.ts +3 -1
- package/dist/pregel/read.js +12 -1
- package/dist/pregel/read.js.map +1 -1
- package/dist/pregel/read.test.cjs +35 -35
- package/dist/pregel/read.test.js +4 -4
- package/dist/pregel/read.test.js.map +1 -1
- package/dist/pregel/remote.js.map +1 -1
- package/dist/pregel/retry.cjs +12 -16
- package/dist/pregel/retry.js +10 -14
- package/dist/pregel/retry.js.map +1 -1
- package/dist/pregel/runner.cjs +92 -118
- package/dist/pregel/runner.js +93 -119
- package/dist/pregel/runner.js.map +1 -1
- package/dist/pregel/runner.test.cjs +14 -14
- package/dist/pregel/runner.test.js +4 -4
- package/dist/pregel/runner.test.js.map +1 -1
- package/dist/pregel/stream.js.map +1 -1
- package/dist/pregel/types.cjs +10 -3
- package/dist/pregel/types.d.ts +64 -8
- package/dist/pregel/types.js +8 -1
- package/dist/pregel/types.js.map +1 -1
- package/dist/pregel/utils/config.cjs +40 -22
- package/dist/pregel/utils/config.d.ts +8 -5
- package/dist/pregel/utils/config.js +33 -14
- package/dist/pregel/utils/config.js.map +1 -1
- package/dist/pregel/utils/config.test.cjs +58 -58
- package/dist/pregel/utils/config.test.js +12 -12
- package/dist/pregel/utils/config.test.js.map +1 -1
- package/dist/pregel/utils/index.cjs +12 -11
- package/dist/pregel/utils/index.d.ts +15 -0
- package/dist/pregel/utils/index.js +6 -4
- package/dist/pregel/utils/index.js.map +1 -1
- package/dist/pregel/utils/subgraph.cjs +2 -3
- package/dist/pregel/utils/subgraph.js.map +1 -1
- package/dist/pregel/utils/subgraph.test.cjs +18 -18
- package/dist/pregel/utils/subgraph.test.js +1 -1
- package/dist/pregel/utils/subgraph.test.js.map +1 -1
- package/dist/pregel/validate.cjs +3 -3
- package/dist/pregel/validate.js.map +1 -1
- package/dist/pregel/validate.test.cjs +43 -43
- package/dist/pregel/validate.test.js +3 -3
- package/dist/pregel/validate.test.js.map +1 -1
- package/dist/pregel/write.js.map +1 -1
- package/dist/pregel/write.test.cjs +30 -30
- package/dist/pregel/write.test.js +8 -8
- package/dist/pregel/write.test.js.map +1 -1
- package/dist/setup/async_local_storage.cjs +1 -2
- package/dist/utils.cjs +7 -7
- package/dist/utils.js.map +1 -1
- package/dist/web.cjs +6 -1
- package/dist/web.d.ts +3 -3
- package/dist/web.js +3 -3
- package/dist/web.js.map +1 -1
- package/package.json +15 -15
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */
|
|
2
|
+
import { z } from "zod";
|
|
2
3
|
import { Annotation } from "./annotation.js";
|
|
3
4
|
import { messagesStateReducer } from "./message.js";
|
|
5
|
+
import { withLangGraph } from "./zod/state.js";
|
|
4
6
|
/**
|
|
5
7
|
* Prebuilt state annotation that combines returned messages.
|
|
6
8
|
* Can handle standard messages and special modifiers like {@link RemoveMessage}
|
|
@@ -42,4 +44,55 @@ export const MessagesAnnotation = Annotation.Root({
|
|
|
42
44
|
default: () => [],
|
|
43
45
|
}),
|
|
44
46
|
});
|
|
47
|
+
/**
|
|
48
|
+
* Prebuilt state object that uses Zod to combine returned messages.
|
|
49
|
+
* This utility is synonymous with the `MessagesAnnotation` annotation,
|
|
50
|
+
* but uses Zod as the way to express messages state.
|
|
51
|
+
*
|
|
52
|
+
* You can use import and use this prebuilt schema like this:
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { MessagesZodState, StateGraph } from "@langchain/langgraph";
|
|
57
|
+
*
|
|
58
|
+
* const graph = new StateGraph(MessagesZodState)
|
|
59
|
+
* .addNode(...)
|
|
60
|
+
* ...
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* Which is equivalent to initializing the schema object manually like this:
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { z } from "zod";
|
|
68
|
+
* import type { BaseMessage, BaseMessageLike } from "@langchain/core/messages";
|
|
69
|
+
* import { StateGraph, messagesStateReducer } from "@langchain/langgraph";
|
|
70
|
+
* import "@langchain/langgraph/zod";
|
|
71
|
+
*
|
|
72
|
+
* const AgentState = z.object({
|
|
73
|
+
* messages: z
|
|
74
|
+
* .custom<BaseMessage[]>()
|
|
75
|
+
* .default(() => [])
|
|
76
|
+
* .langgraph.reducer(
|
|
77
|
+
* messagesStateReducer,
|
|
78
|
+
* z.custom<BaseMessageLike | BaseMessageLike[]>()
|
|
79
|
+
* ),
|
|
80
|
+
* });
|
|
81
|
+
* const graph = new StateGraph(AgentState)
|
|
82
|
+
* .addNode(...)
|
|
83
|
+
* ...
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export const MessagesZodState = z.object({
|
|
87
|
+
messages: withLangGraph(z.custom(), {
|
|
88
|
+
reducer: {
|
|
89
|
+
schema: z.custom(),
|
|
90
|
+
fn: messagesStateReducer,
|
|
91
|
+
},
|
|
92
|
+
jsonSchemaExtra: {
|
|
93
|
+
langgraph_type: "messages",
|
|
94
|
+
},
|
|
95
|
+
default: () => [],
|
|
96
|
+
}),
|
|
97
|
+
});
|
|
45
98
|
//# sourceMappingURL=messages_annotation.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"messages_annotation.js","sourceRoot":"","sources":["../../src/graph/messages_annotation.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAG1C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAY,oBAAoB,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"messages_annotation.js","sourceRoot":"","sources":["../../src/graph/messages_annotation.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAG1C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAY,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,UAAU,CAAC,IAAI,CAAC;IAChD,QAAQ,EAAE,UAAU,CAA0B;QAC5C,OAAO,EAAE,oBAAoB;QAC7B,OAAO,EAAE,GAAG,EAAE,CAAC,EAAE;KAClB,CAAC;CACH,CAAC,CAAC;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC,MAAM,CAAC;IACvC,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,MAAM,EAAiB,EAAE;QACjD,OAAO,EAAE;YACP,MAAM,EAAE,CAAC,CAAC,MAAM,EAAY;YAC5B,EAAE,EAAE,oBAAoB;SACzB;QACD,eAAe,EAAE;YACf,cAAc,EAAE,UAAU;SAC3B;QACD,OAAO,EAAE,GAAG,EAAE,CAAC,EAAE;KAClB,CAAC;CACH,CAAC,CAAC"}
|
package/dist/graph/state.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CompiledStateGraph = exports.StateGraph = void 0;
|
|
4
|
+
exports.typedNode = typedNode;
|
|
4
5
|
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
5
6
|
const runnables_1 = require("@langchain/core/runnables");
|
|
6
7
|
const base_js_1 = require("../channels/base.cjs");
|
|
@@ -16,7 +17,9 @@ const annotation_js_1 = require("./annotation.cjs");
|
|
|
16
17
|
const base_js_2 = require("../managed/base.cjs");
|
|
17
18
|
const subgraph_js_1 = require("../pregel/utils/subgraph.cjs");
|
|
18
19
|
const state_js_1 = require("./zod/state.cjs");
|
|
20
|
+
const last_value_js_1 = require("../channels/last_value.cjs");
|
|
19
21
|
const ROOT = "__root__";
|
|
22
|
+
const PartialStateSchema = Symbol.for("langgraph.state.partial");
|
|
20
23
|
/**
|
|
21
24
|
* A graph whose nodes communicate by reading and writing to a shared state.
|
|
22
25
|
* Each node takes a defined `State` as input and returns a `Partial<State>`.
|
|
@@ -168,7 +171,7 @@ class StateGraph extends graph_js_1.Graph {
|
|
|
168
171
|
this._schemaDefinition = stateDef;
|
|
169
172
|
this._schemaRuntimeDefinition = fields.state;
|
|
170
173
|
this._inputDefinition = inputDef;
|
|
171
|
-
this._inputRuntimeDefinition = fields.input ??
|
|
174
|
+
this._inputRuntimeDefinition = fields.input ?? PartialStateSchema;
|
|
172
175
|
this._outputDefinition = outputDef;
|
|
173
176
|
this._outputRuntimeDefinition = fields.output ?? fields.state;
|
|
174
177
|
}
|
|
@@ -177,7 +180,7 @@ class StateGraph extends graph_js_1.Graph {
|
|
|
177
180
|
this._schemaDefinition = stateDef;
|
|
178
181
|
this._schemaRuntimeDefinition = fields;
|
|
179
182
|
this._inputDefinition = stateDef;
|
|
180
|
-
this._inputRuntimeDefinition =
|
|
183
|
+
this._inputRuntimeDefinition = PartialStateSchema;
|
|
181
184
|
this._outputDefinition = stateDef;
|
|
182
185
|
this._outputRuntimeDefinition = fields;
|
|
183
186
|
}
|
|
@@ -202,7 +205,7 @@ class StateGraph extends graph_js_1.Graph {
|
|
|
202
205
|
this._schemaDefinition = spec;
|
|
203
206
|
}
|
|
204
207
|
else {
|
|
205
|
-
throw new Error("Invalid StateGraph input.");
|
|
208
|
+
throw new Error("Invalid StateGraph input. Make sure to pass a valid Annotation.Root or Zod schema.");
|
|
206
209
|
}
|
|
207
210
|
this._inputDefinition ??= this._schemaDefinition;
|
|
208
211
|
this._outputDefinition ??= this._schemaDefinition;
|
|
@@ -246,54 +249,87 @@ class StateGraph extends graph_js_1.Graph {
|
|
|
246
249
|
}
|
|
247
250
|
}
|
|
248
251
|
}
|
|
249
|
-
addNode(
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
252
|
+
addNode(...args) {
|
|
253
|
+
function isMultipleNodes(args) {
|
|
254
|
+
return args.length >= 1 && typeof args[0] !== "string";
|
|
255
|
+
}
|
|
256
|
+
const nodes = (isMultipleNodes(args) // eslint-disable-line no-nested-ternary
|
|
257
|
+
? Array.isArray(args[0])
|
|
258
|
+
? args[0]
|
|
259
|
+
: Object.entries(args[0]).map(([key, action]) => [
|
|
260
|
+
key,
|
|
261
|
+
action,
|
|
262
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
263
|
+
action[Symbol.for("langgraph.state.node")] ?? undefined,
|
|
264
|
+
])
|
|
265
|
+
: [[args[0], args[1], args[2]]]);
|
|
266
|
+
if (nodes.length === 0) {
|
|
267
|
+
throw new Error("No nodes provided in `addNode`");
|
|
268
|
+
}
|
|
269
|
+
for (const [key, action, options] of nodes) {
|
|
270
|
+
if (key in this.channels) {
|
|
271
|
+
throw new Error(`${key} is already being used as a state attribute (a.k.a. a channel), cannot also be used as a node name.`);
|
|
259
272
|
}
|
|
273
|
+
for (const reservedChar of [
|
|
274
|
+
constants_js_1.CHECKPOINT_NAMESPACE_SEPARATOR,
|
|
275
|
+
constants_js_1.CHECKPOINT_NAMESPACE_END,
|
|
276
|
+
]) {
|
|
277
|
+
if (key.includes(reservedChar)) {
|
|
278
|
+
throw new Error(`"${reservedChar}" is a reserved character and is not allowed in node names.`);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
this.warnIfCompiled(`Adding a node to a graph that has already been compiled. This will not be reflected in the compiled graph.`);
|
|
282
|
+
if (key in this.nodes) {
|
|
283
|
+
throw new Error(`Node \`${key}\` already present.`);
|
|
284
|
+
}
|
|
285
|
+
if (key === constants_js_1.END || key === constants_js_1.START) {
|
|
286
|
+
throw new Error(`Node \`${key}\` is reserved.`);
|
|
287
|
+
}
|
|
288
|
+
let inputSpec = this._schemaDefinition;
|
|
289
|
+
if (options?.input !== undefined) {
|
|
290
|
+
if ((0, state_js_1.isAnyZodObject)(options.input)) {
|
|
291
|
+
inputSpec = (0, state_js_1.getChannelsFromZod)(options.input);
|
|
292
|
+
}
|
|
293
|
+
else if (options.input.spec !== undefined) {
|
|
294
|
+
inputSpec = options.input.spec;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (inputSpec !== undefined) {
|
|
298
|
+
this._addSchema(inputSpec);
|
|
299
|
+
}
|
|
300
|
+
let runnable;
|
|
301
|
+
if (runnables_1.Runnable.isRunnable(action)) {
|
|
302
|
+
runnable = action;
|
|
303
|
+
}
|
|
304
|
+
else if (typeof action === "function") {
|
|
305
|
+
runnable = new utils_js_1.RunnableCallable({
|
|
306
|
+
func: action,
|
|
307
|
+
name: key,
|
|
308
|
+
trace: false,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
runnable = (0, runnables_1._coerceToRunnable)(action);
|
|
313
|
+
}
|
|
314
|
+
let cachePolicy = options?.cachePolicy;
|
|
315
|
+
if (typeof cachePolicy === "boolean") {
|
|
316
|
+
cachePolicy = cachePolicy ? {} : undefined;
|
|
317
|
+
}
|
|
318
|
+
const nodeSpec = {
|
|
319
|
+
runnable: runnable,
|
|
320
|
+
retryPolicy: options?.retryPolicy,
|
|
321
|
+
cachePolicy,
|
|
322
|
+
metadata: options?.metadata,
|
|
323
|
+
input: inputSpec ?? this._schemaDefinition,
|
|
324
|
+
subgraphs: (0, subgraph_js_1.isPregelLike)(runnable)
|
|
325
|
+
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
326
|
+
[runnable]
|
|
327
|
+
: options?.subgraphs,
|
|
328
|
+
ends: options?.ends,
|
|
329
|
+
defer: options?.defer,
|
|
330
|
+
};
|
|
331
|
+
this.nodes[key] = nodeSpec;
|
|
260
332
|
}
|
|
261
|
-
this.warnIfCompiled(`Adding a node to a graph that has already been compiled. This will not be reflected in the compiled graph.`);
|
|
262
|
-
if (key in this.nodes) {
|
|
263
|
-
throw new Error(`Node \`${key}\` already present.`);
|
|
264
|
-
}
|
|
265
|
-
if (key === constants_js_1.END || key === constants_js_1.START) {
|
|
266
|
-
throw new Error(`Node \`${key}\` is reserved.`);
|
|
267
|
-
}
|
|
268
|
-
if (options?.input !== undefined) {
|
|
269
|
-
this._addSchema(options.input.spec);
|
|
270
|
-
}
|
|
271
|
-
let runnable;
|
|
272
|
-
if (runnables_1.Runnable.isRunnable(action)) {
|
|
273
|
-
runnable = action;
|
|
274
|
-
}
|
|
275
|
-
else if (typeof action === "function") {
|
|
276
|
-
runnable = new utils_js_1.RunnableCallable({
|
|
277
|
-
func: action,
|
|
278
|
-
name: key,
|
|
279
|
-
trace: false,
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
else {
|
|
283
|
-
runnable = (0, runnables_1._coerceToRunnable)(action);
|
|
284
|
-
}
|
|
285
|
-
const nodeSpec = {
|
|
286
|
-
runnable: runnable,
|
|
287
|
-
retryPolicy: options?.retryPolicy,
|
|
288
|
-
metadata: options?.metadata,
|
|
289
|
-
input: options?.input?.spec ?? this._schemaDefinition,
|
|
290
|
-
subgraphs: (0, subgraph_js_1.isPregelLike)(runnable)
|
|
291
|
-
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
292
|
-
[runnable]
|
|
293
|
-
: options?.subgraphs,
|
|
294
|
-
ends: options?.ends,
|
|
295
|
-
};
|
|
296
|
-
this.nodes[key] = nodeSpec;
|
|
297
333
|
return this;
|
|
298
334
|
}
|
|
299
335
|
addEdge(startKey, endKey) {
|
|
@@ -321,7 +357,33 @@ class StateGraph extends graph_js_1.Graph {
|
|
|
321
357
|
this.waitingEdges.add([startKey, endKey]);
|
|
322
358
|
return this;
|
|
323
359
|
}
|
|
324
|
-
|
|
360
|
+
addSequence(nodes) {
|
|
361
|
+
const parsedNodes = Array.isArray(nodes)
|
|
362
|
+
? nodes
|
|
363
|
+
: Object.entries(nodes).map(([key, action]) => [
|
|
364
|
+
key,
|
|
365
|
+
action,
|
|
366
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
367
|
+
action[Symbol.for("langgraph.state.node")] ?? undefined,
|
|
368
|
+
]);
|
|
369
|
+
if (parsedNodes.length === 0) {
|
|
370
|
+
throw new Error("Sequence requires at least one node.");
|
|
371
|
+
}
|
|
372
|
+
let previousNode;
|
|
373
|
+
for (const [key, action, options] of parsedNodes) {
|
|
374
|
+
if (key in this.nodes) {
|
|
375
|
+
throw new Error(`Node names must be unique: node with the name "${key}" already exists.`);
|
|
376
|
+
}
|
|
377
|
+
const validKey = key;
|
|
378
|
+
this.addNode(validKey, action, options);
|
|
379
|
+
if (previousNode != null) {
|
|
380
|
+
this.addEdge(previousNode, validKey);
|
|
381
|
+
}
|
|
382
|
+
previousNode = validKey;
|
|
383
|
+
}
|
|
384
|
+
return this;
|
|
385
|
+
}
|
|
386
|
+
compile({ checkpointer, store, cache, interruptBefore, interruptAfter, name, } = {}) {
|
|
325
387
|
// validate the graph
|
|
326
388
|
this.validate([
|
|
327
389
|
...(Array.isArray(interruptBefore) ? interruptBefore : []),
|
|
@@ -349,6 +411,7 @@ class StateGraph extends graph_js_1.Graph {
|
|
|
349
411
|
streamChannels,
|
|
350
412
|
streamMode: "updates",
|
|
351
413
|
store,
|
|
414
|
+
cache,
|
|
352
415
|
name,
|
|
353
416
|
});
|
|
354
417
|
// attach nodes, edges and branches
|
|
@@ -507,15 +570,16 @@ class CompiledStateGraph extends graph_js_1.CompiledGraph {
|
|
|
507
570
|
const inputDefinition = node?.input ?? this.builder._schemaDefinition;
|
|
508
571
|
const inputValues = Object.fromEntries(Object.keys(this.builder._schemaDefinitions.get(inputDefinition)).map((k) => [k, k]));
|
|
509
572
|
const isSingleInput = Object.keys(inputValues).length === 1 && ROOT in inputValues;
|
|
510
|
-
|
|
573
|
+
const branchChannel = `branch:to:${key}`;
|
|
574
|
+
this.channels[branchChannel] = node?.defer
|
|
575
|
+
? new last_value_js_1.LastValueAfterFinish()
|
|
576
|
+
: new ephemeral_value_js_1.EphemeralValue(false);
|
|
511
577
|
this.nodes[key] = new read_js_1.PregelNode({
|
|
512
|
-
triggers: [],
|
|
578
|
+
triggers: [branchChannel],
|
|
513
579
|
// read state keys
|
|
514
580
|
channels: isSingleInput ? Object.keys(inputValues) : inputValues,
|
|
515
|
-
// publish to
|
|
516
|
-
writers: [
|
|
517
|
-
new write_js_1.ChannelWrite(stateWriteEntries.concat({ channel: key, value: key }), [constants_js_1.TAG_HIDDEN]),
|
|
518
|
-
],
|
|
581
|
+
// publish to state keys
|
|
582
|
+
writers: [new write_js_1.ChannelWrite(stateWriteEntries, [constants_js_1.TAG_HIDDEN])],
|
|
519
583
|
mapper: isSingleInput
|
|
520
584
|
? undefined
|
|
521
585
|
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -525,55 +589,41 @@ class CompiledStateGraph extends graph_js_1.CompiledGraph {
|
|
|
525
589
|
bound: node?.runnable,
|
|
526
590
|
metadata: node?.metadata,
|
|
527
591
|
retryPolicy: node?.retryPolicy,
|
|
592
|
+
cachePolicy: node?.cachePolicy,
|
|
528
593
|
subgraphs: node?.subgraphs,
|
|
529
594
|
ends: node?.ends,
|
|
530
595
|
});
|
|
531
596
|
}
|
|
532
597
|
}
|
|
533
|
-
attachEdge(
|
|
534
|
-
if (end === constants_js_1.END)
|
|
598
|
+
attachEdge(starts, end) {
|
|
599
|
+
if (end === constants_js_1.END)
|
|
535
600
|
return;
|
|
601
|
+
if (typeof starts === "string") {
|
|
602
|
+
this.nodes[starts].writers.push(new write_js_1.ChannelWrite([{ channel: `branch:to:${end}`, value: null }], [constants_js_1.TAG_HIDDEN]));
|
|
536
603
|
}
|
|
537
|
-
if (Array.isArray(
|
|
538
|
-
const channelName = `join:${
|
|
604
|
+
else if (Array.isArray(starts)) {
|
|
605
|
+
const channelName = `join:${starts.join("+")}:${end}`;
|
|
539
606
|
// register channel
|
|
540
|
-
this.channels[channelName] =
|
|
541
|
-
new named_barrier_value_js_1.
|
|
607
|
+
this.channels[channelName] = this.builder.nodes[end].defer
|
|
608
|
+
? new named_barrier_value_js_1.NamedBarrierValueAfterFinish(new Set(starts))
|
|
609
|
+
: new named_barrier_value_js_1.NamedBarrierValue(new Set(starts));
|
|
542
610
|
// subscribe to channel
|
|
543
611
|
this.nodes[end].triggers.push(channelName);
|
|
544
612
|
// publish to channel
|
|
545
|
-
for (const
|
|
546
|
-
this.nodes[
|
|
613
|
+
for (const start of starts) {
|
|
614
|
+
this.nodes[start].writers.push(new write_js_1.ChannelWrite([{ channel: channelName, value: start }], [constants_js_1.TAG_HIDDEN]));
|
|
547
615
|
}
|
|
548
616
|
}
|
|
549
|
-
else if (start === constants_js_1.START) {
|
|
550
|
-
const channelName = `${constants_js_1.START}:${end}`;
|
|
551
|
-
// register channel
|
|
552
|
-
this.channels[channelName] =
|
|
553
|
-
new ephemeral_value_js_1.EphemeralValue();
|
|
554
|
-
// subscribe to channel
|
|
555
|
-
this.nodes[end].triggers.push(channelName);
|
|
556
|
-
// publish to channel
|
|
557
|
-
this.nodes[constants_js_1.START].writers.push(new write_js_1.ChannelWrite([{ channel: channelName, value: constants_js_1.START }], [constants_js_1.TAG_HIDDEN]));
|
|
558
|
-
}
|
|
559
|
-
else {
|
|
560
|
-
this.nodes[end].triggers.push(start);
|
|
561
|
-
}
|
|
562
617
|
}
|
|
563
|
-
attachBranch(start,
|
|
618
|
+
attachBranch(start, _, branch, options = { withReader: true }) {
|
|
564
619
|
const branchWriter = async (packets, config) => {
|
|
565
620
|
const filteredPackets = packets.filter((p) => p !== constants_js_1.END);
|
|
566
|
-
if (!filteredPackets.length)
|
|
621
|
+
if (!filteredPackets.length)
|
|
567
622
|
return;
|
|
568
|
-
}
|
|
569
623
|
const writes = filteredPackets.map((p) => {
|
|
570
|
-
if ((0, constants_js_1._isSend)(p))
|
|
624
|
+
if ((0, constants_js_1._isSend)(p))
|
|
571
625
|
return p;
|
|
572
|
-
}
|
|
573
|
-
return {
|
|
574
|
-
channel: `branch:${start}:${name}:${p}`,
|
|
575
|
-
value: start,
|
|
576
|
-
};
|
|
626
|
+
return { channel: p === constants_js_1.END ? p : `branch:to:${p}`, value: start };
|
|
577
627
|
});
|
|
578
628
|
await write_js_1.ChannelWrite.doWrite({ ...config, tags: (config.tags ?? []).concat([constants_js_1.TAG_HIDDEN]) }, writes);
|
|
579
629
|
};
|
|
@@ -583,30 +633,32 @@ class CompiledStateGraph extends graph_js_1.CompiledGraph {
|
|
|
583
633
|
options.withReader
|
|
584
634
|
? (config) => read_js_1.ChannelRead.doRead(config, this.streamChannels ?? this.outputChannels, true)
|
|
585
635
|
: undefined));
|
|
586
|
-
// attach branch subscribers
|
|
587
|
-
const ends = branch.ends
|
|
588
|
-
? Object.values(branch.ends)
|
|
589
|
-
: Object.keys(this.builder.nodes);
|
|
590
|
-
for (const end of ends) {
|
|
591
|
-
if (end === constants_js_1.END) {
|
|
592
|
-
continue;
|
|
593
|
-
}
|
|
594
|
-
const channelName = `branch:${start}:${name}:${end}`;
|
|
595
|
-
this.channels[channelName] =
|
|
596
|
-
new ephemeral_value_js_1.EphemeralValue(false);
|
|
597
|
-
this.nodes[end].triggers.push(channelName);
|
|
598
|
-
}
|
|
599
636
|
}
|
|
600
637
|
async _validateInput(input) {
|
|
601
|
-
|
|
638
|
+
if (input == null)
|
|
639
|
+
return input;
|
|
640
|
+
const schema = (() => {
|
|
641
|
+
const input = this.builder._inputRuntimeDefinition;
|
|
642
|
+
const schema = this.builder._schemaRuntimeDefinition;
|
|
643
|
+
const apply = (schema) => {
|
|
644
|
+
if (schema == null)
|
|
645
|
+
return undefined;
|
|
646
|
+
return (0, state_js_1.applyZodPlugin)(schema, { reducer: true });
|
|
647
|
+
};
|
|
648
|
+
if ((0, state_js_1.isAnyZodObject)(input))
|
|
649
|
+
return apply(input);
|
|
650
|
+
if (input === PartialStateSchema)
|
|
651
|
+
return apply(schema)?.partial();
|
|
652
|
+
return undefined;
|
|
653
|
+
})();
|
|
602
654
|
if ((0, constants_js_1.isCommand)(input)) {
|
|
603
655
|
const parsedInput = input;
|
|
604
|
-
if (input.update &&
|
|
605
|
-
parsedInput.update =
|
|
656
|
+
if (input.update && schema != null)
|
|
657
|
+
parsedInput.update = schema.parse(input.update);
|
|
606
658
|
return parsedInput;
|
|
607
659
|
}
|
|
608
|
-
if (
|
|
609
|
-
return
|
|
660
|
+
if (schema != null)
|
|
661
|
+
return schema.parse(input);
|
|
610
662
|
return input;
|
|
611
663
|
}
|
|
612
664
|
async _validateConfigurable(config) {
|
|
@@ -707,4 +759,10 @@ function _getControlBranch() {
|
|
|
707
759
|
path: CONTROL_BRANCH_PATH,
|
|
708
760
|
});
|
|
709
761
|
}
|
|
762
|
+
function typedNode(_state, _options) {
|
|
763
|
+
return (func, options) => {
|
|
764
|
+
Object.assign(func, { [Symbol.for("langgraph.state.node")]: options });
|
|
765
|
+
return func;
|
|
766
|
+
};
|
|
767
|
+
}
|
|
710
768
|
//# sourceMappingURL=state.js.map
|
package/dist/graph/state.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { RunnableLike } from "@langchain/core/runnables";
|
|
2
|
-
import { All, BaseCheckpointSaver, BaseStore } from "@langchain/langgraph-checkpoint";
|
|
2
|
+
import { All, type BaseCache, BaseCheckpointSaver, BaseStore } from "@langchain/langgraph-checkpoint";
|
|
3
3
|
import { BaseChannel } from "../channels/base.js";
|
|
4
4
|
import { CompiledGraph, Graph, Branch, AddNodeOptions, NodeSpec } from "./graph.js";
|
|
5
|
-
import { END, START } from "../constants.js";
|
|
5
|
+
import { Command, END, START } from "../constants.js";
|
|
6
6
|
import { AnnotationRoot, SingleReducer, StateDefinition, StateType, UpdateType } from "./annotation.js";
|
|
7
|
-
import type { RetryPolicy } from "../pregel/utils/index.js";
|
|
7
|
+
import type { CachePolicy, RetryPolicy } from "../pregel/utils/index.js";
|
|
8
8
|
import { ManagedValueSpec } from "../managed/base.js";
|
|
9
9
|
import type { LangGraphRunnableConfig } from "../pregel/runnable_types.js";
|
|
10
10
|
import { AnyZodObject, ZodToStateDefinition } from "./zod/state.js";
|
|
@@ -21,11 +21,13 @@ export interface StateGraphArgs<Channels extends object | unknown> {
|
|
|
21
21
|
export type StateGraphNodeSpec<RunInput, RunOutput> = NodeSpec<RunInput, RunOutput> & {
|
|
22
22
|
input?: StateDefinition;
|
|
23
23
|
retryPolicy?: RetryPolicy;
|
|
24
|
+
cachePolicy?: CachePolicy;
|
|
24
25
|
};
|
|
25
|
-
export type StateGraphAddNodeOptions = {
|
|
26
|
+
export type StateGraphAddNodeOptions<Nodes extends string = string> = {
|
|
26
27
|
retryPolicy?: RetryPolicy;
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
cachePolicy?: CachePolicy | boolean;
|
|
29
|
+
input?: AnnotationRoot<any> | AnyZodObject;
|
|
30
|
+
} & AddNodeOptions<Nodes>;
|
|
29
31
|
export type StateGraphArgsWithStateSchema<SD extends StateDefinition, I extends StateDefinition, O extends StateDefinition> = {
|
|
30
32
|
stateSchema: AnnotationRoot<SD>;
|
|
31
33
|
input?: AnnotationRoot<I>;
|
|
@@ -42,6 +44,10 @@ type ZodStateGraphArgsWithStateSchema<SD extends AnyZodObject, I extends SDZod,
|
|
|
42
44
|
};
|
|
43
45
|
type SDZod = StateDefinition | AnyZodObject;
|
|
44
46
|
type ToStateDefinition<T> = T extends AnyZodObject ? ZodToStateDefinition<T> : T extends StateDefinition ? T : never;
|
|
47
|
+
type NodeAction<S, U, C extends SDZod> = RunnableLike<S, U extends object ? U & Record<string, any> : U, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
48
|
+
LangGraphRunnableConfig<StateType<ToStateDefinition<C>>>>;
|
|
49
|
+
declare const PartialStateSchema: unique symbol;
|
|
50
|
+
type PartialStateSchema = typeof PartialStateSchema;
|
|
45
51
|
/**
|
|
46
52
|
* A graph whose nodes communicate by reading and writing to a shared state.
|
|
47
53
|
* Each node takes a defined `State` as input and returns a `Partial<State>`.
|
|
@@ -114,7 +120,7 @@ export declare class StateGraph<SD extends SDZod | unknown, S = SD extends SDZod
|
|
|
114
120
|
/** @internal */
|
|
115
121
|
_inputDefinition: I;
|
|
116
122
|
/** @internal */
|
|
117
|
-
_inputRuntimeDefinition: AnyZodObject | undefined;
|
|
123
|
+
_inputRuntimeDefinition: AnyZodObject | PartialStateSchema | undefined;
|
|
118
124
|
/** @internal */
|
|
119
125
|
_outputDefinition: O;
|
|
120
126
|
/** @internal */
|
|
@@ -133,11 +139,23 @@ export declare class StateGraph<SD extends SDZod | unknown, S = SD extends SDZod
|
|
|
133
139
|
constructor(fields: SD extends AnyZodObject ? SD | ZodStateGraphArgsWithStateSchema<SD, I, O> : never, configSchema?: C | AnnotationRoot<ToStateDefinition<C>>);
|
|
134
140
|
get allEdges(): Set<[string, string]>;
|
|
135
141
|
_addSchema(stateDefinition: SDZod): void;
|
|
136
|
-
addNode<K extends string
|
|
142
|
+
addNode<K extends string>(nodes: Record<K, NodeAction<S, U, C>> | [
|
|
143
|
+
key: K,
|
|
144
|
+
action: NodeAction<S, U, C>,
|
|
145
|
+
options?: StateGraphAddNodeOptions
|
|
146
|
+
][]): StateGraph<SD, S, U, N | K, I, O, C>;
|
|
147
|
+
addNode<K extends string, NodeInput = S>(key: K, action: NodeAction<NodeInput, U, C>, options?: StateGraphAddNodeOptions): StateGraph<SD, S, U, N | K, I, O, C>;
|
|
137
148
|
addEdge(startKey: typeof START | N | N[], endKey: N | typeof END): this;
|
|
138
|
-
|
|
149
|
+
addSequence<K extends string>(nodes: [
|
|
150
|
+
key: K,
|
|
151
|
+
action: NodeAction<S, U, C>,
|
|
152
|
+
options?: StateGraphAddNodeOptions
|
|
153
|
+
][]): StateGraph<SD, S, U, N | K, I, O, C>;
|
|
154
|
+
addSequence<K extends string>(nodes: Record<K, NodeAction<S, U, C>>): StateGraph<SD, S, U, N | K, I, O, C>;
|
|
155
|
+
compile({ checkpointer, store, cache, interruptBefore, interruptAfter, name, }?: {
|
|
139
156
|
checkpointer?: BaseCheckpointSaver | false;
|
|
140
157
|
store?: BaseStore;
|
|
158
|
+
cache?: BaseCache;
|
|
141
159
|
interruptBefore?: N[] | All;
|
|
142
160
|
interruptAfter?: N[] | All;
|
|
143
161
|
name?: string;
|
|
@@ -152,11 +170,18 @@ export declare class CompiledStateGraph<S, U, N extends string = typeof START, I
|
|
|
152
170
|
builder: StateGraph<unknown, S, U, N, I, O, C>;
|
|
153
171
|
attachNode(key: typeof START, node?: never): void;
|
|
154
172
|
attachNode(key: N, node: StateGraphNodeSpec<S, U>): void;
|
|
155
|
-
attachEdge(
|
|
156
|
-
attachBranch(start: N | typeof START,
|
|
173
|
+
attachEdge(starts: N | N[] | "__start__", end: N | "__end__"): void;
|
|
174
|
+
attachBranch(start: N | typeof START, _: string, branch: Branch<S, N>, options?: {
|
|
157
175
|
withReader?: boolean;
|
|
158
176
|
}): void;
|
|
159
177
|
protected _validateInput(input: UpdateType<ToStateDefinition<I>>): Promise<UpdateType<ToStateDefinition<I>>>;
|
|
160
178
|
protected _validateConfigurable(config: Partial<LangGraphRunnableConfig["configurable"]>): Promise<LangGraphRunnableConfig["configurable"]>;
|
|
161
179
|
}
|
|
180
|
+
type TypedNodeAction<SD extends StateDefinition, Nodes extends string> = (state: StateType<SD>, config: LangGraphRunnableConfig) => UpdateType<SD> | Command<unknown, UpdateType<SD>, Nodes>;
|
|
181
|
+
export declare function typedNode<SD extends SDZod, Nodes extends string>(_state: SD extends StateDefinition ? AnnotationRoot<SD> : never, _options?: {
|
|
182
|
+
nodes?: Nodes[];
|
|
183
|
+
}): (func: TypedNodeAction<ToStateDefinition<SD>, Nodes>, options?: StateGraphAddNodeOptions<Nodes>) => TypedNodeAction<ToStateDefinition<SD>, Nodes>;
|
|
184
|
+
export declare function typedNode<SD extends SDZod, Nodes extends string>(_state: SD extends AnyZodObject ? SD : never, _options?: {
|
|
185
|
+
nodes?: Nodes[];
|
|
186
|
+
}): (func: TypedNodeAction<ToStateDefinition<SD>, Nodes>, options?: StateGraphAddNodeOptions<Nodes>) => TypedNodeAction<ToStateDefinition<SD>, Nodes>;
|
|
162
187
|
export {};
|