@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.
- package/dist/channels/any_value.cjs +57 -0
- package/dist/channels/any_value.d.ts +16 -0
- package/dist/channels/any_value.js +53 -0
- package/dist/channels/base.cjs +19 -28
- package/dist/channels/base.d.ts +13 -19
- package/dist/channels/base.js +17 -24
- package/dist/channels/binop.cjs +4 -3
- package/dist/channels/binop.d.ts +1 -1
- package/dist/channels/binop.js +3 -2
- package/dist/channels/dynamic_barrier_value.cjs +88 -0
- package/dist/channels/dynamic_barrier_value.d.ts +26 -0
- package/dist/channels/dynamic_barrier_value.js +84 -0
- package/dist/channels/ephemeral_value.cjs +64 -0
- package/dist/channels/ephemeral_value.d.ts +14 -0
- package/dist/channels/ephemeral_value.js +60 -0
- package/dist/channels/index.cjs +1 -3
- package/dist/channels/index.d.ts +1 -1
- package/dist/channels/index.js +1 -1
- package/dist/channels/last_value.cjs +11 -5
- package/dist/channels/last_value.d.ts +5 -1
- package/dist/channels/last_value.js +9 -3
- package/dist/channels/named_barrier_value.cjs +71 -0
- package/dist/channels/named_barrier_value.d.ts +18 -0
- package/dist/channels/named_barrier_value.js +66 -0
- package/dist/channels/topic.cjs +5 -3
- package/dist/channels/topic.d.ts +3 -3
- package/dist/channels/topic.js +5 -3
- package/dist/checkpoint/base.cjs +30 -12
- package/dist/checkpoint/base.d.ts +39 -22
- package/dist/checkpoint/base.js +28 -11
- package/dist/checkpoint/id.cjs +40 -0
- package/dist/checkpoint/id.d.ts +2 -0
- package/dist/checkpoint/id.js +35 -0
- package/dist/checkpoint/index.cjs +2 -2
- package/dist/checkpoint/index.d.ts +2 -2
- package/dist/checkpoint/index.js +2 -2
- package/dist/checkpoint/memory.cjs +63 -49
- package/dist/checkpoint/memory.d.ts +7 -10
- package/dist/checkpoint/memory.js +62 -47
- package/dist/checkpoint/sqlite.cjs +170 -0
- package/dist/checkpoint/sqlite.d.ts +14 -0
- package/dist/checkpoint/sqlite.js +163 -0
- package/dist/constants.cjs +3 -1
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +2 -0
- package/dist/errors.cjs +31 -0
- package/dist/errors.d.ts +12 -0
- package/dist/errors.js +24 -0
- package/dist/graph/graph.cjs +235 -95
- package/dist/graph/graph.d.ts +52 -23
- package/dist/graph/graph.js +234 -96
- package/dist/graph/index.cjs +2 -2
- package/dist/graph/index.d.ts +2 -2
- package/dist/graph/index.js +2 -2
- package/dist/graph/message.cjs +4 -3
- package/dist/graph/message.d.ts +4 -1
- package/dist/graph/message.js +4 -3
- package/dist/graph/state.cjs +237 -102
- package/dist/graph/state.d.ts +41 -18
- package/dist/graph/state.js +238 -104
- package/dist/index.cjs +6 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/prebuilt/agent_executor.cjs +22 -36
- package/dist/prebuilt/agent_executor.d.ts +7 -10
- package/dist/prebuilt/agent_executor.js +23 -37
- package/dist/prebuilt/chat_agent_executor.cjs +13 -13
- package/dist/prebuilt/chat_agent_executor.d.ts +3 -1
- package/dist/prebuilt/chat_agent_executor.js +15 -15
- package/dist/prebuilt/index.cjs +4 -1
- package/dist/prebuilt/index.d.ts +1 -0
- package/dist/prebuilt/index.js +1 -0
- package/dist/prebuilt/tool_node.cjs +59 -0
- package/dist/prebuilt/tool_node.d.ts +17 -0
- package/dist/prebuilt/tool_node.js +54 -0
- package/dist/pregel/debug.cjs +6 -8
- package/dist/pregel/debug.d.ts +2 -2
- package/dist/pregel/debug.js +5 -7
- package/dist/pregel/index.cjs +406 -236
- package/dist/pregel/index.d.ts +77 -41
- package/dist/pregel/index.js +408 -241
- package/dist/pregel/io.cjs +117 -30
- package/dist/pregel/io.d.ts +11 -3
- package/dist/pregel/io.js +111 -28
- package/dist/pregel/read.cjs +126 -46
- package/dist/pregel/read.d.ts +27 -18
- package/dist/pregel/read.js +125 -45
- package/dist/pregel/types.cjs +2 -0
- package/dist/pregel/types.d.ts +32 -0
- package/dist/pregel/types.js +1 -0
- package/dist/pregel/validate.cjs +58 -51
- package/dist/pregel/validate.d.ts +14 -13
- package/dist/pregel/validate.js +56 -50
- package/dist/pregel/write.cjs +46 -30
- package/dist/pregel/write.d.ts +18 -8
- package/dist/pregel/write.js +45 -29
- package/dist/serde/base.cjs +2 -0
- package/dist/serde/base.d.ts +4 -0
- package/dist/serde/base.js +1 -0
- package/dist/setup/async_local_storage.cjs +2 -2
- package/dist/setup/async_local_storage.js +1 -1
- package/dist/tests/channels.test.d.ts +1 -0
- package/dist/tests/channels.test.js +151 -0
- package/dist/tests/chatbot.int.test.d.ts +1 -0
- package/dist/tests/chatbot.int.test.js +61 -0
- package/dist/tests/checkpoints.test.d.ts +1 -0
- package/dist/tests/checkpoints.test.js +190 -0
- package/dist/tests/graph.test.d.ts +1 -0
- package/dist/tests/graph.test.js +15 -0
- package/dist/tests/prebuilt.int.test.d.ts +1 -0
- package/dist/tests/prebuilt.int.test.js +101 -0
- package/dist/tests/prebuilt.test.d.ts +1 -0
- package/dist/tests/prebuilt.test.js +195 -0
- package/dist/tests/pregel.io.test.d.ts +1 -0
- package/dist/tests/pregel.io.test.js +332 -0
- package/dist/tests/pregel.read.test.d.ts +1 -0
- package/dist/tests/pregel.read.test.js +109 -0
- package/dist/tests/pregel.test.d.ts +1 -0
- package/dist/tests/pregel.test.js +1879 -0
- package/dist/tests/pregel.validate.test.d.ts +1 -0
- package/dist/tests/pregel.validate.test.js +198 -0
- package/dist/tests/pregel.write.test.d.ts +1 -0
- package/dist/tests/pregel.write.test.js +44 -0
- package/dist/tests/tracing.int.test.d.ts +1 -0
- package/dist/tests/tracing.int.test.js +449 -0
- package/dist/tests/utils.d.ts +22 -0
- package/dist/tests/utils.js +76 -0
- package/dist/utils.cjs +74 -0
- package/dist/utils.d.ts +18 -0
- package/dist/utils.js +70 -0
- package/package.json +11 -7
- package/dist/pregel/reserved.cjs +0 -6
- package/dist/pregel/reserved.d.ts +0 -3
- package/dist/pregel/reserved.js +0 -3
package/dist/graph/state.cjs
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
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
|
-
|
|
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 (
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
|
85
|
+
const outputs = stateKeys.length === 1 && stateKeys[0] === ROOT
|
|
36
86
|
? stateKeys[0]
|
|
37
87
|
: stateKeys;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
.
|
|
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
|
-
|
|
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
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
|
|
116
|
-
return input;
|
|
136
|
+
return channels;
|
|
117
137
|
}
|
|
118
|
-
function
|
|
119
|
-
if (
|
|
120
|
-
|
|
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
|
-
|
|
123
|
-
__root__: input,
|
|
124
|
-
});
|
|
125
|
-
return input;
|
|
145
|
+
return new last_value_js_1.LastValue();
|
|
126
146
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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
|
-
|
|
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;
|
package/dist/graph/state.d.ts
CHANGED
|
@@ -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 {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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<
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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 {};
|