@langchain/langgraph 0.0.12 → 0.0.13

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.
Files changed (134) hide show
  1. package/dist/channels/any_value.cjs +57 -0
  2. package/dist/channels/any_value.d.ts +16 -0
  3. package/dist/channels/any_value.js +53 -0
  4. package/dist/channels/base.cjs +19 -28
  5. package/dist/channels/base.d.ts +13 -19
  6. package/dist/channels/base.js +17 -24
  7. package/dist/channels/binop.cjs +4 -3
  8. package/dist/channels/binop.d.ts +1 -1
  9. package/dist/channels/binop.js +3 -2
  10. package/dist/channels/dynamic_barrier_value.cjs +88 -0
  11. package/dist/channels/dynamic_barrier_value.d.ts +26 -0
  12. package/dist/channels/dynamic_barrier_value.js +84 -0
  13. package/dist/channels/ephemeral_value.cjs +64 -0
  14. package/dist/channels/ephemeral_value.d.ts +14 -0
  15. package/dist/channels/ephemeral_value.js +60 -0
  16. package/dist/channels/index.cjs +1 -3
  17. package/dist/channels/index.d.ts +1 -1
  18. package/dist/channels/index.js +1 -1
  19. package/dist/channels/last_value.cjs +11 -5
  20. package/dist/channels/last_value.d.ts +5 -1
  21. package/dist/channels/last_value.js +9 -3
  22. package/dist/channels/named_barrier_value.cjs +71 -0
  23. package/dist/channels/named_barrier_value.d.ts +18 -0
  24. package/dist/channels/named_barrier_value.js +66 -0
  25. package/dist/channels/topic.cjs +5 -3
  26. package/dist/channels/topic.d.ts +3 -3
  27. package/dist/channels/topic.js +5 -3
  28. package/dist/checkpoint/base.cjs +30 -12
  29. package/dist/checkpoint/base.d.ts +39 -22
  30. package/dist/checkpoint/base.js +28 -11
  31. package/dist/checkpoint/id.cjs +40 -0
  32. package/dist/checkpoint/id.d.ts +2 -0
  33. package/dist/checkpoint/id.js +35 -0
  34. package/dist/checkpoint/index.cjs +2 -2
  35. package/dist/checkpoint/index.d.ts +2 -2
  36. package/dist/checkpoint/index.js +2 -2
  37. package/dist/checkpoint/memory.cjs +63 -49
  38. package/dist/checkpoint/memory.d.ts +7 -10
  39. package/dist/checkpoint/memory.js +62 -47
  40. package/dist/checkpoint/sqlite.cjs +170 -0
  41. package/dist/checkpoint/sqlite.d.ts +14 -0
  42. package/dist/checkpoint/sqlite.js +163 -0
  43. package/dist/constants.cjs +3 -1
  44. package/dist/constants.d.ts +2 -0
  45. package/dist/constants.js +2 -0
  46. package/dist/errors.cjs +31 -0
  47. package/dist/errors.d.ts +12 -0
  48. package/dist/errors.js +24 -0
  49. package/dist/graph/graph.cjs +235 -95
  50. package/dist/graph/graph.d.ts +52 -23
  51. package/dist/graph/graph.js +234 -96
  52. package/dist/graph/index.cjs +2 -2
  53. package/dist/graph/index.d.ts +2 -2
  54. package/dist/graph/index.js +2 -2
  55. package/dist/graph/message.cjs +4 -3
  56. package/dist/graph/message.d.ts +4 -1
  57. package/dist/graph/message.js +4 -3
  58. package/dist/graph/state.cjs +237 -102
  59. package/dist/graph/state.d.ts +41 -18
  60. package/dist/graph/state.js +238 -104
  61. package/dist/index.cjs +6 -2
  62. package/dist/index.d.ts +3 -2
  63. package/dist/index.js +2 -1
  64. package/dist/prebuilt/agent_executor.cjs +22 -36
  65. package/dist/prebuilt/agent_executor.d.ts +7 -10
  66. package/dist/prebuilt/agent_executor.js +23 -37
  67. package/dist/prebuilt/chat_agent_executor.cjs +13 -13
  68. package/dist/prebuilt/chat_agent_executor.d.ts +3 -1
  69. package/dist/prebuilt/chat_agent_executor.js +15 -15
  70. package/dist/prebuilt/index.cjs +4 -1
  71. package/dist/prebuilt/index.d.ts +1 -0
  72. package/dist/prebuilt/index.js +1 -0
  73. package/dist/prebuilt/tool_node.cjs +59 -0
  74. package/dist/prebuilt/tool_node.d.ts +17 -0
  75. package/dist/prebuilt/tool_node.js +54 -0
  76. package/dist/pregel/debug.cjs +6 -8
  77. package/dist/pregel/debug.d.ts +2 -2
  78. package/dist/pregel/debug.js +5 -7
  79. package/dist/pregel/index.cjs +406 -236
  80. package/dist/pregel/index.d.ts +77 -41
  81. package/dist/pregel/index.js +408 -241
  82. package/dist/pregel/io.cjs +117 -30
  83. package/dist/pregel/io.d.ts +11 -3
  84. package/dist/pregel/io.js +111 -28
  85. package/dist/pregel/read.cjs +126 -46
  86. package/dist/pregel/read.d.ts +27 -18
  87. package/dist/pregel/read.js +125 -45
  88. package/dist/pregel/types.cjs +2 -0
  89. package/dist/pregel/types.d.ts +32 -0
  90. package/dist/pregel/types.js +1 -0
  91. package/dist/pregel/validate.cjs +58 -51
  92. package/dist/pregel/validate.d.ts +14 -13
  93. package/dist/pregel/validate.js +56 -50
  94. package/dist/pregel/write.cjs +46 -30
  95. package/dist/pregel/write.d.ts +18 -8
  96. package/dist/pregel/write.js +45 -29
  97. package/dist/serde/base.cjs +2 -0
  98. package/dist/serde/base.d.ts +4 -0
  99. package/dist/serde/base.js +1 -0
  100. package/dist/setup/async_local_storage.cjs +2 -2
  101. package/dist/setup/async_local_storage.js +1 -1
  102. package/dist/tests/channels.test.d.ts +1 -0
  103. package/dist/tests/channels.test.js +151 -0
  104. package/dist/tests/chatbot.int.test.d.ts +1 -0
  105. package/dist/tests/chatbot.int.test.js +61 -0
  106. package/dist/tests/checkpoints.test.d.ts +1 -0
  107. package/dist/tests/checkpoints.test.js +190 -0
  108. package/dist/tests/graph.test.d.ts +1 -0
  109. package/dist/tests/graph.test.js +15 -0
  110. package/dist/tests/prebuilt.int.test.d.ts +1 -0
  111. package/dist/tests/prebuilt.int.test.js +101 -0
  112. package/dist/tests/prebuilt.test.d.ts +1 -0
  113. package/dist/tests/prebuilt.test.js +195 -0
  114. package/dist/tests/pregel.io.test.d.ts +1 -0
  115. package/dist/tests/pregel.io.test.js +332 -0
  116. package/dist/tests/pregel.read.test.d.ts +1 -0
  117. package/dist/tests/pregel.read.test.js +109 -0
  118. package/dist/tests/pregel.test.d.ts +1 -0
  119. package/dist/tests/pregel.test.js +1879 -0
  120. package/dist/tests/pregel.validate.test.d.ts +1 -0
  121. package/dist/tests/pregel.validate.test.js +198 -0
  122. package/dist/tests/pregel.write.test.d.ts +1 -0
  123. package/dist/tests/pregel.write.test.js +44 -0
  124. package/dist/tests/tracing.int.test.d.ts +1 -0
  125. package/dist/tests/tracing.int.test.js +449 -0
  126. package/dist/tests/utils.d.ts +22 -0
  127. package/dist/tests/utils.js +76 -0
  128. package/dist/utils.cjs +74 -0
  129. package/dist/utils.d.ts +18 -0
  130. package/dist/utils.js +70 -0
  131. package/package.json +11 -7
  132. package/dist/pregel/reserved.cjs +0 -6
  133. package/dist/pregel/reserved.d.ts +0 -3
  134. package/dist/pregel/reserved.js +0 -3
@@ -1,14 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StateGraph = exports.START = void 0;
4
- const runnables_1 = require("@langchain/core/runnables");
3
+ exports.CompiledStateGraph = exports.StateGraph = void 0;
5
4
  const binop_js_1 = require("../channels/binop.cjs");
6
5
  const graph_js_1 = require("./graph.cjs");
7
6
  const last_value_js_1 = require("../channels/last_value.cjs");
8
7
  const write_js_1 = require("../pregel/write.cjs");
9
- const index_js_1 = require("../pregel/index.cjs");
10
8
  const read_js_1 = require("../pregel/read.cjs");
11
- exports.START = "__start__";
9
+ const named_barrier_value_js_1 = require("../channels/named_barrier_value.cjs");
10
+ const ephemeral_value_js_1 = require("../channels/ephemeral_value.cjs");
11
+ const utils_js_1 = require("../utils.cjs");
12
+ const constants_js_1 = require("../constants.cjs");
13
+ const errors_js_1 = require("../errors.cjs");
14
+ const dynamic_barrier_value_js_1 = require("../channels/dynamic_barrier_value.cjs");
15
+ const ROOT = "__root__";
12
16
  class StateGraph extends graph_js_1.Graph {
13
17
  constructor(fields) {
14
18
  super();
@@ -18,129 +22,260 @@ class StateGraph extends graph_js_1.Graph {
18
22
  writable: true,
19
23
  value: void 0
20
24
  });
25
+ // TODO: this doesn't dedupe edges as in py, so worth fixing at some point
26
+ Object.defineProperty(this, "waitingEdges", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: new Set()
31
+ });
21
32
  this.channels = _getChannels(fields.channels);
33
+ for (const c of Object.values(this.channels)) {
34
+ if (c.lc_graph_name === "BinaryOperatorAggregate") {
35
+ this.supportMultipleEdges = true;
36
+ break;
37
+ }
38
+ }
39
+ }
40
+ get allEdges() {
41
+ return new Set([
42
+ ...this.edges,
43
+ ...Array.from(this.waitingEdges).flatMap(([starts, end]) => starts.map((start) => [start, end])),
44
+ ]);
22
45
  }
23
46
  addNode(key, action) {
24
- if (Object.keys(this.nodes).some((key) => key in this.channels)) {
47
+ if (key in this.channels) {
25
48
  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.`);
26
49
  }
27
- super.addNode(key, action);
50
+ return super.addNode(key, action);
28
51
  }
29
- compile(checkpointer) {
30
- this.validate();
31
- if (Object.keys(this.nodes).some((key) => key in this.channels)) {
32
- throw new Error("Cannot use channel names as node names");
52
+ addEdge(startKey, endKey) {
53
+ if (typeof startKey === "string") {
54
+ return super.addEdge(startKey, endKey);
33
55
  }
56
+ if (this.compiled) {
57
+ console.warn("Adding an edge to a graph that has already been compiled. This will " +
58
+ "not be reflected in the compiled graph.");
59
+ }
60
+ for (const start of startKey) {
61
+ if (start === graph_js_1.END) {
62
+ throw new Error("END cannot be a start node");
63
+ }
64
+ if (!Object.keys(this.nodes).some((node) => node === start)) {
65
+ throw new Error(`Need to add_node ${start} first`);
66
+ }
67
+ }
68
+ if (endKey === graph_js_1.END) {
69
+ throw new Error("END cannot be an end node");
70
+ }
71
+ if (!Object.keys(this.nodes).some((node) => node === endKey)) {
72
+ throw new Error(`Need to add_node ${endKey} first`);
73
+ }
74
+ this.waitingEdges.add([startKey, endKey]);
75
+ return this;
76
+ }
77
+ compile({ checkpointer, interruptBefore, interruptAfter, } = {}) {
78
+ // validate the graph
79
+ this.validate([
80
+ ...(Array.isArray(interruptBefore) ? interruptBefore : []),
81
+ ...(Array.isArray(interruptAfter) ? interruptAfter : []),
82
+ ]);
83
+ // prepare output channels
34
84
  const stateKeys = Object.keys(this.channels);
35
- const stateKeysRead = stateKeys.length === 1 && stateKeys[0] === "__root__"
85
+ const outputs = stateKeys.length === 1 && stateKeys[0] === ROOT
36
86
  ? stateKeys[0]
37
87
  : stateKeys;
38
- const updateState = Array.isArray(stateKeysRead)
39
- ? (nodeName,
40
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
- input, options) => _updateStateObject(stateKeys, nodeName, input, options)
42
- : _updateStateRoot;
43
- const outgoingEdges = {};
88
+ // create empty compiled graph
89
+ const compiled = new CompiledStateGraph({
90
+ builder: this,
91
+ checkpointer,
92
+ interruptAfter,
93
+ interruptBefore,
94
+ autoValidate: false,
95
+ nodes: {},
96
+ channels: {
97
+ ...this.channels,
98
+ [graph_js_1.START]: new ephemeral_value_js_1.EphemeralValue(),
99
+ },
100
+ inputs: graph_js_1.START,
101
+ outputs,
102
+ streamChannels: outputs,
103
+ streamMode: "updates",
104
+ });
105
+ // attach nodes, edges and branches
106
+ compiled.attachNode(graph_js_1.START);
107
+ for (const [key, node] of Object.entries(this.nodes)) {
108
+ compiled.attachNode(key, node);
109
+ }
44
110
  for (const [start, end] of this.edges) {
45
- if (!outgoingEdges[start]) {
46
- outgoingEdges[start] = [];
47
- }
48
- outgoingEdges[start].push(end !== graph_js_1.END ? `${end}:inbox` : graph_js_1.END);
111
+ compiled.attachEdge(start, end);
49
112
  }
50
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
51
- const nodes = {};
52
- for (const [key, node] of Object.entries(this.nodes)) {
53
- nodes[key] = index_js_1.Channel.subscribeTo(`${key}:inbox`)
54
- .pipe(node)
55
- .pipe((
56
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
57
- input, options) => updateState(key, input, options))
58
- .pipe(index_js_1.Channel.writeTo(key));
59
- }
60
- for (const key of Object.keys(this.nodes)) {
61
- const outgoing = outgoingEdges[key];
62
- const edgesKey = `${key}:edges`;
63
- if (outgoing || this.branches[key]) {
64
- nodes[edgesKey] = index_js_1.Channel.subscribeTo(key, {
65
- tags: ["langsmith:hidden"],
66
- }).pipe(new read_js_1.ChannelRead(stateKeysRead));
67
- }
68
- if (outgoing) {
69
- nodes[edgesKey] = nodes[edgesKey].pipe(index_js_1.Channel.writeTo(...outgoing));
70
- }
71
- if (this.branches[key]) {
72
- for (const branch of this.branches[key]) {
73
- nodes[edgesKey] = nodes[edgesKey].pipe(new runnables_1.RunnableLambda({
74
- func: (i, c) => branch.runnable(i, c),
75
- }));
76
- }
113
+ for (const [starts, end] of this.waitingEdges) {
114
+ compiled.attachEdge(starts, end);
115
+ }
116
+ for (const [start, branches] of Object.entries(this.branches)) {
117
+ for (const [name, branch] of Object.entries(branches)) {
118
+ compiled.attachBranch(start, name, branch);
77
119
  }
78
120
  }
79
- nodes[exports.START] = index_js_1.Channel.subscribeTo(`${exports.START}:inbox`, {
80
- tags: ["langsmith:hidden"],
81
- })
82
- .pipe((
83
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
84
- input, options) => updateState(exports.START, input, options))
85
- .pipe(index_js_1.Channel.writeTo(exports.START));
86
- nodes[`${exports.START}:edges`] = index_js_1.Channel.subscribeTo(exports.START, {
87
- tags: ["langsmith:hidden"],
88
- })
89
- .pipe(new read_js_1.ChannelRead(stateKeysRead))
90
- .pipe(index_js_1.Channel.writeTo(`${this.entryPoint}:inbox`));
91
- return new index_js_1.Pregel({
92
- nodes,
93
- channels: this.channels,
94
- input: `${exports.START}:inbox`,
95
- output: graph_js_1.END,
96
- hidden: Object.keys(this.nodes)
97
- .map((node) => `${node}:inbox`)
98
- .concat(exports.START, stateKeys),
99
- checkpointer,
100
- });
121
+ return compiled.validate();
101
122
  }
102
123
  }
103
124
  exports.StateGraph = StateGraph;
104
- function _updateStateObject(stateKeys, nodeName,
105
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
106
- input, options
107
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
108
- ) {
109
- if (!options?.config) {
110
- throw new Error("Config not found when updating state.");
111
- }
112
- if (Object.keys(input).some((key) => !stateKeys.some((sk) => sk === key))) {
113
- throw new Error(`Invalid state update from node ${nodeName}, expected object with one or more of ${stateKeys.join(", ")}, got ${Object.keys(input).join(",")}`);
125
+ function _getChannels(schema) {
126
+ const channels = {};
127
+ for (const [name, val] of Object.entries(schema)) {
128
+ if (name === ROOT) {
129
+ channels[name] = getChannel(val);
130
+ }
131
+ else {
132
+ const key = name;
133
+ channels[name] = getChannel(val);
134
+ }
114
135
  }
115
- write_js_1.ChannelWrite.doWrite(options.config, input);
116
- return input;
136
+ return channels;
117
137
  }
118
- function _updateStateRoot(_nodeName, input, options) {
119
- if (!options?.config) {
120
- throw new Error("Config not found when updating state.");
138
+ function getChannel(reducer) {
139
+ if (reducer && "reducer" in reducer && reducer.reducer) {
140
+ return new binop_js_1.BinaryOperatorAggregate(reducer.reducer, reducer.default);
141
+ }
142
+ if (reducer && "value" in reducer && reducer.value) {
143
+ return new binop_js_1.BinaryOperatorAggregate(reducer.value, reducer.default);
121
144
  }
122
- write_js_1.ChannelWrite.doWrite(options.config, {
123
- __root__: input,
124
- });
125
- return input;
145
+ return new last_value_js_1.LastValue();
126
146
  }
127
- function _getChannels(schema) {
128
- if ("value" in schema && "default" in schema) {
129
- if (!schema.value) {
130
- throw new Error("Value is required for channels");
147
+ class CompiledStateGraph extends graph_js_1.CompiledGraph {
148
+ attachNode(key, node) {
149
+ const stateKeys = Object.keys(this.builder.channels);
150
+ function getStateKey(key, input) {
151
+ if (!input) {
152
+ return write_js_1.SKIP_WRITE;
153
+ }
154
+ else if (typeof input !== "object" || Array.isArray(input)) {
155
+ throw new errors_js_1.InvalidUpdateError(`Expected dict, got ${typeof input}`);
156
+ }
157
+ else {
158
+ return key in input ? input[key] : write_js_1.SKIP_WRITE;
159
+ }
160
+ }
161
+ // state updaters
162
+ const stateWriteEntries = stateKeys.map((key) => key === ROOT
163
+ ? { channel: key, value: write_js_1.PASSTHROUGH, skipNone: true }
164
+ : {
165
+ channel: key,
166
+ value: write_js_1.PASSTHROUGH,
167
+ mapper: new utils_js_1.RunnableCallable({
168
+ func: getStateKey.bind(null, key),
169
+ trace: false,
170
+ recurse: false,
171
+ }),
172
+ });
173
+ // add node and output channel
174
+ if (key === graph_js_1.START) {
175
+ this.nodes[key] = new read_js_1.PregelNode({
176
+ tags: [constants_js_1.TAG_HIDDEN],
177
+ triggers: [graph_js_1.START],
178
+ channels: [graph_js_1.START],
179
+ writers: [new write_js_1.ChannelWrite(stateWriteEntries, [constants_js_1.TAG_HIDDEN])],
180
+ });
181
+ }
182
+ else {
183
+ this.channels[key] = new ephemeral_value_js_1.EphemeralValue();
184
+ this.nodes[key] = new read_js_1.PregelNode({
185
+ triggers: [],
186
+ // read state keys
187
+ channels: stateKeys.length === 1 && stateKeys[0] === ROOT
188
+ ? stateKeys
189
+ : stateKeys.reduce((acc, k) => {
190
+ acc[k] = k;
191
+ return acc;
192
+ }, {}),
193
+ // publish to this channel and state keys
194
+ writers: [
195
+ new write_js_1.ChannelWrite(stateWriteEntries.concat({ channel: key, value: key }), [constants_js_1.TAG_HIDDEN]),
196
+ ],
197
+ bound: node,
198
+ });
131
199
  }
132
- return {
133
- __root__: new binop_js_1.BinaryOperatorAggregate(schema.value, schema.default),
134
- };
135
200
  }
136
- const channels = {};
137
- for (const [name, values] of Object.entries(schema)) {
138
- if (values.value) {
139
- channels[name] = new binop_js_1.BinaryOperatorAggregate(values.value, values.default);
201
+ attachEdge(start, end) {
202
+ if (end === graph_js_1.END) {
203
+ return;
204
+ }
205
+ if (Array.isArray(start)) {
206
+ const channelName = `join:${start.join("+")}:${end}`;
207
+ // register channel
208
+ this.channels[channelName] =
209
+ new named_barrier_value_js_1.NamedBarrierValue(new Set(start));
210
+ // subscribe to channel
211
+ this.nodes[end].triggers.push(channelName);
212
+ // publish to channel
213
+ for (const s of start) {
214
+ this.nodes[s].writers.push(new write_js_1.ChannelWrite([{ channel: channelName, value: s }], [constants_js_1.TAG_HIDDEN]));
215
+ }
216
+ }
217
+ else if (start === graph_js_1.START) {
218
+ const channelName = `start:${end}`;
219
+ // register channel
220
+ this.channels[channelName] =
221
+ new ephemeral_value_js_1.EphemeralValue();
222
+ // subscribe to channel
223
+ this.nodes[end].triggers.push(channelName);
224
+ // publish to channel
225
+ this.nodes[graph_js_1.START].writers.push(new write_js_1.ChannelWrite([{ channel: channelName, value: graph_js_1.START }], [constants_js_1.TAG_HIDDEN]));
140
226
  }
141
227
  else {
142
- channels[name] = new last_value_js_1.LastValue();
228
+ this.nodes[end].triggers.push(start);
229
+ }
230
+ }
231
+ attachBranch(start, name, branch) {
232
+ // attach branch publisher
233
+ this.nodes[start].writers.push(branch.compile(
234
+ // writer
235
+ (dests) => {
236
+ const filteredDests = dests.filter((dest) => dest !== graph_js_1.END);
237
+ if (!filteredDests.length) {
238
+ return;
239
+ }
240
+ const writes = filteredDests.map((dest) => ({
241
+ channel: `branch:${start}:${name}:${dest}`,
242
+ value: start,
243
+ }));
244
+ if (branch.then && branch.then !== graph_js_1.END) {
245
+ writes.push({
246
+ channel: `branch:${start}:${name}:then`,
247
+ value: { __names: filteredDests },
248
+ });
249
+ }
250
+ return new write_js_1.ChannelWrite(writes, [constants_js_1.TAG_HIDDEN]);
251
+ },
252
+ // reader
253
+ (config) => read_js_1.ChannelRead.doRead(config, this.outputs, true)));
254
+ // attach branch subscribers
255
+ const ends = branch.ends
256
+ ? Object.values(branch.ends)
257
+ : Object.keys(this.builder.nodes).filter((n) => n !== branch.then);
258
+ for (const end of ends) {
259
+ if (end === graph_js_1.END) {
260
+ continue;
261
+ }
262
+ const channelName = `branch:${start}:${name}:${end}`;
263
+ this.channels[channelName] =
264
+ new ephemeral_value_js_1.EphemeralValue();
265
+ this.nodes[end].triggers.push(channelName);
266
+ }
267
+ if (branch.then && branch.then !== graph_js_1.END) {
268
+ const channelName = `branch:${start}:${name}:then`;
269
+ this.channels[channelName] =
270
+ new dynamic_barrier_value_js_1.DynamicBarrierValue();
271
+ this.nodes[branch.then].triggers.push(channelName);
272
+ for (const end of ends) {
273
+ if (end === graph_js_1.END) {
274
+ continue;
275
+ }
276
+ this.nodes[end].writers.push(new write_js_1.ChannelWrite([{ channel: channelName, value: end }], [constants_js_1.TAG_HIDDEN]));
277
+ }
143
278
  }
144
279
  }
145
- return channels;
146
280
  }
281
+ exports.CompiledStateGraph = CompiledStateGraph;
@@ -1,24 +1,47 @@
1
- import { RunnableLike } from "@langchain/core/runnables";
1
+ import { Runnable, RunnableConfig, RunnableLike } from "@langchain/core/runnables";
2
2
  import { BaseChannel } from "../channels/base.js";
3
3
  import { BinaryOperator } from "../channels/binop.js";
4
- import { Graph } from "./graph.js";
4
+ import { END, CompiledGraph, Graph, START, Branch } from "./graph.js";
5
5
  import { BaseCheckpointSaver } from "../checkpoint/base.js";
6
- import { Pregel } from "../pregel/index.js";
7
- export declare const START = "__start__";
8
- export interface StateGraphArgs<Channels extends Record<string, any>> {
9
- channels: {
10
- [K in keyof Channels]: {
11
- value: BinaryOperator<Channels[K]> | null;
12
- default?: () => Channels[K];
13
- } | string;
14
- } | {
15
- value: BinaryOperator<unknown> | null;
16
- default?: () => unknown;
17
- };
6
+ import { All } from "../pregel/types.js";
7
+ type SingleReducer<T> = {
8
+ reducer: BinaryOperator<T>;
9
+ default?: () => T;
10
+ } | {
11
+ /**
12
+ * @deprecated Use `reducer` instead
13
+ */
14
+ value: BinaryOperator<T>;
15
+ default?: () => T;
16
+ } | null;
17
+ export type ChannelReducers<Channels extends object> = {
18
+ [K in keyof Channels]: SingleReducer<Channels[K]>;
19
+ };
20
+ export interface StateGraphArgs<Channels extends object | unknown> {
21
+ channels: Channels extends object ? Channels extends unknown[] ? ChannelReducers<{
22
+ __root__: Channels;
23
+ }> : ChannelReducers<Channels> : ChannelReducers<{
24
+ __root__: Channels;
25
+ }>;
18
26
  }
19
- export declare class StateGraph<Channels extends Record<string, any>> extends Graph<Channels> {
27
+ export declare class StateGraph<const State extends object | unknown, const Update extends object | unknown = Partial<State>, const N extends string = typeof START> extends Graph<N, State, Update> {
20
28
  channels: Record<string, BaseChannel>;
21
- constructor(fields: StateGraphArgs<Channels>);
22
- addNode(key: string, action: RunnableLike): void;
23
- compile(checkpointer?: BaseCheckpointSaver): Pregel;
29
+ waitingEdges: Set<[N[], N]>;
30
+ constructor(fields: StateGraphArgs<State>);
31
+ get allEdges(): Set<[string, string]>;
32
+ addNode<K extends string>(key: K, action: RunnableLike<State, Update>): StateGraph<State, Update, N | K>;
33
+ addEdge(startKey: typeof START | N | N[], endKey: N | typeof END): this;
34
+ compile({ checkpointer, interruptBefore, interruptAfter, }?: {
35
+ checkpointer?: BaseCheckpointSaver;
36
+ interruptBefore?: N[] | All;
37
+ interruptAfter?: N[] | All;
38
+ }): CompiledStateGraph<State, Update, N>;
24
39
  }
40
+ export declare class CompiledStateGraph<const State extends object | unknown, const Update extends object | unknown = Partial<State>, const N extends string = typeof START> extends CompiledGraph<N, State, Update> {
41
+ builder: StateGraph<State, Update, N>;
42
+ attachNode(key: typeof START, node?: never): void;
43
+ attachNode(key: N, node: Runnable<State, Update, RunnableConfig>): void;
44
+ attachEdge(start: N | N[] | "__start__", end: N | "__end__"): void;
45
+ attachBranch(start: N | typeof START, name: string, branch: Branch<State, N>): void;
46
+ }
47
+ export {};