@langchain/langgraph 0.1.8 → 0.1.10-rc.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/constants.d.ts +4 -0
- package/dist/errors.cjs +33 -3
- package/dist/errors.d.ts +9 -1
- package/dist/errors.js +30 -2
- package/dist/graph/graph.d.ts +1 -2
- package/dist/graph/message.d.ts +1 -2
- package/dist/graph/messages_annotation.d.ts +2 -1
- package/dist/graph/state.cjs +85 -18
- package/dist/graph/state.d.ts +20 -8
- package/dist/graph/state.js +85 -18
- package/dist/prebuilt/agent_executor.d.ts +1 -1
- package/dist/prebuilt/react_agent_executor.d.ts +1 -2
- package/dist/pregel/algo.cjs +2 -2
- package/dist/pregel/algo.d.ts +2 -2
- package/dist/pregel/algo.js +2 -2
- package/dist/pregel/debug.cjs +34 -7
- package/dist/pregel/debug.d.ts +2 -1
- package/dist/pregel/debug.js +35 -8
- package/dist/pregel/index.cjs +44 -25
- package/dist/pregel/index.d.ts +2 -2
- package/dist/pregel/index.js +44 -25
- package/dist/pregel/loop.cjs +42 -29
- package/dist/pregel/loop.d.ts +14 -6
- package/dist/pregel/loop.js +44 -31
- package/dist/pregel/retry.cjs +5 -11
- package/dist/pregel/retry.d.ts +5 -1
- package/dist/pregel/retry.js +6 -12
- package/dist/pregel/types.d.ts +3 -2
- package/dist/pregel/validate.d.ts +1 -1
- package/dist/utils.cjs +9 -1
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +7 -0
- package/dist/web.cjs +16 -7
- package/dist/web.d.ts +2 -2
- package/dist/web.js +2 -2
- package/package.json +1 -1
package/dist/constants.d.ts
CHANGED
package/dist/errors.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.InvalidUpdateError = exports.EmptyChannelError = exports.EmptyInputError = exports.GraphInterrupt = exports.GraphValueError = exports.GraphRecursionError = void 0;
|
|
3
|
+
exports.InvalidUpdateError = exports.EmptyChannelError = exports.EmptyInputError = exports.isGraphInterrupt = exports.NodeInterrupt = exports.GraphInterrupt = exports.GraphValueError = exports.GraphRecursionError = void 0;
|
|
4
4
|
class GraphRecursionError extends Error {
|
|
5
5
|
constructor(message) {
|
|
6
6
|
super(message);
|
|
@@ -22,15 +22,45 @@ class GraphValueError extends Error {
|
|
|
22
22
|
}
|
|
23
23
|
exports.GraphValueError = GraphValueError;
|
|
24
24
|
class GraphInterrupt extends Error {
|
|
25
|
-
constructor(
|
|
26
|
-
super(
|
|
25
|
+
constructor(interrupts = []) {
|
|
26
|
+
super(JSON.stringify(interrupts, null, 2));
|
|
27
|
+
Object.defineProperty(this, "interrupts", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: void 0
|
|
32
|
+
});
|
|
27
33
|
this.name = "GraphInterrupt";
|
|
34
|
+
this.interrupts = interrupts;
|
|
28
35
|
}
|
|
29
36
|
static get unminifiable_name() {
|
|
30
37
|
return "GraphInterrupt";
|
|
31
38
|
}
|
|
32
39
|
}
|
|
33
40
|
exports.GraphInterrupt = GraphInterrupt;
|
|
41
|
+
/** Raised by a node to interrupt execution. */
|
|
42
|
+
class NodeInterrupt extends GraphInterrupt {
|
|
43
|
+
constructor(message) {
|
|
44
|
+
super([
|
|
45
|
+
{
|
|
46
|
+
value: message,
|
|
47
|
+
when: "during",
|
|
48
|
+
},
|
|
49
|
+
]);
|
|
50
|
+
this.name = "NodeInterrupt";
|
|
51
|
+
}
|
|
52
|
+
static get unminifiable_name() {
|
|
53
|
+
return "NodeInterrupt";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.NodeInterrupt = NodeInterrupt;
|
|
57
|
+
function isGraphInterrupt(e) {
|
|
58
|
+
return [
|
|
59
|
+
GraphInterrupt.unminifiable_name,
|
|
60
|
+
NodeInterrupt.unminifiable_name,
|
|
61
|
+
].includes(e.name);
|
|
62
|
+
}
|
|
63
|
+
exports.isGraphInterrupt = isGraphInterrupt;
|
|
34
64
|
class EmptyInputError extends Error {
|
|
35
65
|
constructor(message) {
|
|
36
66
|
super(message);
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Interrupt } from "./constants.js";
|
|
1
2
|
export declare class GraphRecursionError extends Error {
|
|
2
3
|
constructor(message?: string);
|
|
3
4
|
static get unminifiable_name(): string;
|
|
@@ -7,9 +8,16 @@ export declare class GraphValueError extends Error {
|
|
|
7
8
|
static get unminifiable_name(): string;
|
|
8
9
|
}
|
|
9
10
|
export declare class GraphInterrupt extends Error {
|
|
10
|
-
|
|
11
|
+
interrupts: Interrupt[];
|
|
12
|
+
constructor(interrupts?: Interrupt[]);
|
|
13
|
+
static get unminifiable_name(): string;
|
|
14
|
+
}
|
|
15
|
+
/** Raised by a node to interrupt execution. */
|
|
16
|
+
export declare class NodeInterrupt extends GraphInterrupt {
|
|
17
|
+
constructor(message: string);
|
|
11
18
|
static get unminifiable_name(): string;
|
|
12
19
|
}
|
|
20
|
+
export declare function isGraphInterrupt(e: GraphInterrupt | Error): e is GraphInterrupt;
|
|
13
21
|
export declare class EmptyInputError extends Error {
|
|
14
22
|
constructor(message?: string);
|
|
15
23
|
static get unminifiable_name(): string;
|
package/dist/errors.js
CHANGED
|
@@ -17,14 +17,42 @@ export class GraphValueError extends Error {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
export class GraphInterrupt extends Error {
|
|
20
|
-
constructor(
|
|
21
|
-
super(
|
|
20
|
+
constructor(interrupts = []) {
|
|
21
|
+
super(JSON.stringify(interrupts, null, 2));
|
|
22
|
+
Object.defineProperty(this, "interrupts", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: void 0
|
|
27
|
+
});
|
|
22
28
|
this.name = "GraphInterrupt";
|
|
29
|
+
this.interrupts = interrupts;
|
|
23
30
|
}
|
|
24
31
|
static get unminifiable_name() {
|
|
25
32
|
return "GraphInterrupt";
|
|
26
33
|
}
|
|
27
34
|
}
|
|
35
|
+
/** Raised by a node to interrupt execution. */
|
|
36
|
+
export class NodeInterrupt extends GraphInterrupt {
|
|
37
|
+
constructor(message) {
|
|
38
|
+
super([
|
|
39
|
+
{
|
|
40
|
+
value: message,
|
|
41
|
+
when: "during",
|
|
42
|
+
},
|
|
43
|
+
]);
|
|
44
|
+
this.name = "NodeInterrupt";
|
|
45
|
+
}
|
|
46
|
+
static get unminifiable_name() {
|
|
47
|
+
return "NodeInterrupt";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export function isGraphInterrupt(e) {
|
|
51
|
+
return [
|
|
52
|
+
GraphInterrupt.unminifiable_name,
|
|
53
|
+
NodeInterrupt.unminifiable_name,
|
|
54
|
+
].includes(e.name);
|
|
55
|
+
}
|
|
28
56
|
export class EmptyInputError extends Error {
|
|
29
57
|
constructor(message) {
|
|
30
58
|
super(message);
|
package/dist/graph/graph.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { Runnable, RunnableConfig, RunnableLike } from "@langchain/core/runnables";
|
|
2
2
|
import { Graph as RunnableGraph } from "@langchain/core/runnables/graph";
|
|
3
|
-
import { BaseCheckpointSaver } from "@langchain/langgraph-checkpoint";
|
|
3
|
+
import { All, BaseCheckpointSaver } from "@langchain/langgraph-checkpoint";
|
|
4
4
|
import { PregelNode } from "../pregel/read.js";
|
|
5
5
|
import { Pregel } from "../pregel/index.js";
|
|
6
6
|
import type { PregelParams } from "../pregel/types.js";
|
|
7
7
|
import { BaseChannel } from "../channels/base.js";
|
|
8
|
-
import { All } from "../pregel/types.js";
|
|
9
8
|
import { Send } from "../constants.js";
|
|
10
9
|
import { RunnableCallable } from "../utils.js";
|
|
11
10
|
/** Special reserved node name denoting the start of a graph. */
|
package/dist/graph/message.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseMessage, BaseMessageLike } from "@langchain/core/messages";
|
|
2
2
|
import { StateGraph } from "./state.js";
|
|
3
|
-
type Messages = Array<BaseMessage | BaseMessageLike> | BaseMessage | BaseMessageLike;
|
|
3
|
+
export type Messages = Array<BaseMessage | BaseMessageLike> | BaseMessage | BaseMessageLike;
|
|
4
4
|
/**
|
|
5
5
|
* Prebuilt reducer that combines returned messages.
|
|
6
6
|
* Can handle standard messages and special modifiers like {@link RemoveMessage}
|
|
@@ -11,4 +11,3 @@ export declare function messagesStateReducer(left: BaseMessage[], right: Message
|
|
|
11
11
|
export declare class MessageGraph extends StateGraph<BaseMessage[], BaseMessage[], Messages> {
|
|
12
12
|
constructor();
|
|
13
13
|
}
|
|
14
|
-
export {};
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { BaseMessage } from "@langchain/core/messages";
|
|
2
|
+
import { Messages } from "./message.js";
|
|
2
3
|
/**
|
|
3
4
|
* Prebuilt state annotation that combines returned messages.
|
|
4
5
|
* Can handle standard messages and special modifiers like {@link RemoveMessage}
|
|
5
6
|
* instances.
|
|
6
7
|
*/
|
|
7
8
|
export declare const MessagesAnnotation: import("./annotation.js").AnnotationRoot<{
|
|
8
|
-
messages: import("../web.js").BinaryOperatorAggregate<BaseMessage[],
|
|
9
|
+
messages: import("../web.js").BinaryOperatorAggregate<BaseMessage[], Messages>;
|
|
9
10
|
}>;
|
package/dist/graph/state.cjs
CHANGED
|
@@ -82,7 +82,7 @@ class StateGraph extends graph_js_1.Graph {
|
|
|
82
82
|
enumerable: true,
|
|
83
83
|
configurable: true,
|
|
84
84
|
writable: true,
|
|
85
|
-
value:
|
|
85
|
+
value: {}
|
|
86
86
|
});
|
|
87
87
|
// TODO: this doesn't dedupe edges as in py, so worth fixing at some point
|
|
88
88
|
Object.defineProperty(this, "waitingEdges", {
|
|
@@ -91,21 +91,50 @@ class StateGraph extends graph_js_1.Graph {
|
|
|
91
91
|
writable: true,
|
|
92
92
|
value: new Set()
|
|
93
93
|
});
|
|
94
|
-
|
|
94
|
+
Object.defineProperty(this, "schema", {
|
|
95
|
+
enumerable: true,
|
|
96
|
+
configurable: true,
|
|
97
|
+
writable: true,
|
|
98
|
+
value: void 0
|
|
99
|
+
});
|
|
100
|
+
Object.defineProperty(this, "input", {
|
|
101
|
+
enumerable: true,
|
|
102
|
+
configurable: true,
|
|
103
|
+
writable: true,
|
|
104
|
+
value: void 0
|
|
105
|
+
});
|
|
106
|
+
Object.defineProperty(this, "output", {
|
|
107
|
+
enumerable: true,
|
|
108
|
+
configurable: true,
|
|
109
|
+
writable: true,
|
|
110
|
+
value: void 0
|
|
111
|
+
});
|
|
112
|
+
if (isStateGraphArgsWithInputOutputSchemas(fields)) {
|
|
113
|
+
this.schema = fields.input.spec;
|
|
114
|
+
this.input = fields.input.spec;
|
|
115
|
+
this.output = fields.output.spec;
|
|
116
|
+
}
|
|
117
|
+
else if (isStateGraphArgsWithStateSchema(fields)) {
|
|
118
|
+
this.schema = fields.stateSchema.spec;
|
|
119
|
+
this.input = (fields.input?.spec ?? this.schema);
|
|
120
|
+
this.output = (fields.output?.spec ?? this.schema);
|
|
121
|
+
}
|
|
122
|
+
else if (isStateDefinition(fields) || isAnnotationRoot(fields)) {
|
|
95
123
|
const spec = isAnnotationRoot(fields) ? fields.spec : fields;
|
|
96
|
-
this.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
else {
|
|
102
|
-
this.channels[key] = val;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
124
|
+
this.schema = spec;
|
|
125
|
+
}
|
|
126
|
+
else if (isStateGraphArgs(fields)) {
|
|
127
|
+
const spec = _getChannels(fields.channels);
|
|
128
|
+
this.schema = spec;
|
|
105
129
|
}
|
|
106
130
|
else {
|
|
107
|
-
|
|
131
|
+
throw new Error("Invalid StateGraph input.");
|
|
108
132
|
}
|
|
133
|
+
this.input = this.input ?? this.schema;
|
|
134
|
+
this.output = this.output ?? this.schema;
|
|
135
|
+
this._addSchema(this.schema);
|
|
136
|
+
this._addSchema(this.input);
|
|
137
|
+
this._addSchema(this.output);
|
|
109
138
|
for (const c of Object.values(this.channels)) {
|
|
110
139
|
if (c.lc_graph_name === "BinaryOperatorAggregate") {
|
|
111
140
|
this.supportMultipleEdges = true;
|
|
@@ -119,6 +148,27 @@ class StateGraph extends graph_js_1.Graph {
|
|
|
119
148
|
...Array.from(this.waitingEdges).flatMap(([starts, end]) => starts.map((start) => [start, end])),
|
|
120
149
|
]);
|
|
121
150
|
}
|
|
151
|
+
_addSchema(stateDefinition) {
|
|
152
|
+
for (const [key, val] of Object.entries(stateDefinition)) {
|
|
153
|
+
let channel;
|
|
154
|
+
if (typeof val === "function") {
|
|
155
|
+
channel = val();
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
channel = val;
|
|
159
|
+
}
|
|
160
|
+
if (this.channels[key] !== undefined) {
|
|
161
|
+
if (this.channels[key].lc_graph_name !== channel.lc_graph_name) {
|
|
162
|
+
if (channel.lc_graph_name !== "LastValue") {
|
|
163
|
+
throw new Error(`Channel "${key}" already exists with a different type.`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
this.channels[key] = channel;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
122
172
|
addNode(key, action, options) {
|
|
123
173
|
if (key in this.channels) {
|
|
124
174
|
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.`);
|
|
@@ -173,10 +223,10 @@ class StateGraph extends graph_js_1.Graph {
|
|
|
173
223
|
...(Array.isArray(interruptAfter) ? interruptAfter : []),
|
|
174
224
|
]);
|
|
175
225
|
// prepare output channels
|
|
176
|
-
const stateKeys = Object.keys(this.
|
|
177
|
-
const outputChannels = stateKeys.length === 1 && stateKeys[0] === ROOT
|
|
178
|
-
|
|
179
|
-
|
|
226
|
+
const stateKeys = Object.keys(this.schema);
|
|
227
|
+
const outputChannels = stateKeys.length === 1 && stateKeys[0] === ROOT ? ROOT : stateKeys;
|
|
228
|
+
const outputKeys = Object.keys(this.output);
|
|
229
|
+
const streamChannels = outputKeys.length === 1 && outputKeys[0] === ROOT ? ROOT : outputKeys;
|
|
180
230
|
// create empty compiled graph
|
|
181
231
|
const compiled = new CompiledStateGraph({
|
|
182
232
|
builder: this,
|
|
@@ -191,7 +241,7 @@ class StateGraph extends graph_js_1.Graph {
|
|
|
191
241
|
},
|
|
192
242
|
inputChannels: graph_js_1.START,
|
|
193
243
|
outputChannels,
|
|
194
|
-
streamChannels
|
|
244
|
+
streamChannels,
|
|
195
245
|
streamMode: "updates",
|
|
196
246
|
});
|
|
197
247
|
// attach nodes, edges and branches
|
|
@@ -229,7 +279,7 @@ function _getChannels(schema) {
|
|
|
229
279
|
}
|
|
230
280
|
class CompiledStateGraph extends graph_js_1.CompiledGraph {
|
|
231
281
|
attachNode(key, node) {
|
|
232
|
-
const stateKeys = Object.keys(this.builder.
|
|
282
|
+
const stateKeys = Object.keys(this.builder.input);
|
|
233
283
|
function getStateKey(key, input) {
|
|
234
284
|
if (!input) {
|
|
235
285
|
return write_js_1.SKIP_WRITE;
|
|
@@ -367,3 +417,20 @@ function isAnnotationRoot(obj) {
|
|
|
367
417
|
"lc_graph_name" in obj &&
|
|
368
418
|
obj.lc_graph_name === "AnnotationRoot");
|
|
369
419
|
}
|
|
420
|
+
function isStateGraphArgs(obj) {
|
|
421
|
+
return (typeof obj === "object" &&
|
|
422
|
+
obj !== null &&
|
|
423
|
+
obj.channels !== undefined);
|
|
424
|
+
}
|
|
425
|
+
function isStateGraphArgsWithStateSchema(obj) {
|
|
426
|
+
return (typeof obj === "object" &&
|
|
427
|
+
obj !== null &&
|
|
428
|
+
obj.stateSchema !== undefined);
|
|
429
|
+
}
|
|
430
|
+
function isStateGraphArgsWithInputOutputSchemas(obj) {
|
|
431
|
+
return (typeof obj === "object" &&
|
|
432
|
+
obj !== null &&
|
|
433
|
+
obj.stateSchema === undefined &&
|
|
434
|
+
obj.input !== undefined &&
|
|
435
|
+
obj.output !== undefined);
|
|
436
|
+
}
|
package/dist/graph/state.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { RunnableLike } from "@langchain/core/runnables";
|
|
2
|
-
import { BaseCheckpointSaver } from "@langchain/langgraph-checkpoint";
|
|
2
|
+
import { All, BaseCheckpointSaver } from "@langchain/langgraph-checkpoint";
|
|
3
3
|
import { BaseChannel } from "../channels/base.js";
|
|
4
4
|
import { END, CompiledGraph, Graph, START, Branch, AddNodeOptions, NodeSpec } from "./graph.js";
|
|
5
|
-
import { All } from "../pregel/types.js";
|
|
6
5
|
import { AnnotationRoot, SingleReducer, StateDefinition, StateType, UpdateType } from "./annotation.js";
|
|
7
6
|
import type { RetryPolicy } from "../pregel/utils.js";
|
|
8
7
|
export type ChannelReducers<Channels extends object> = {
|
|
@@ -22,6 +21,15 @@ export type StateGraphNodeSpec<RunInput, RunOutput> = NodeSpec<RunInput, RunOutp
|
|
|
22
21
|
export type StateGraphAddNodeOptions = {
|
|
23
22
|
retryPolicy?: RetryPolicy;
|
|
24
23
|
} & AddNodeOptions;
|
|
24
|
+
export type StateGraphArgsWithStateSchema<SD extends StateDefinition, I extends StateDefinition, O extends StateDefinition> = {
|
|
25
|
+
stateSchema: AnnotationRoot<SD>;
|
|
26
|
+
input?: AnnotationRoot<I>;
|
|
27
|
+
output?: AnnotationRoot<O>;
|
|
28
|
+
};
|
|
29
|
+
export type StateGraphArgsWithInputOutputSchemas<SD extends StateDefinition, O extends StateDefinition = SD> = {
|
|
30
|
+
input: AnnotationRoot<SD>;
|
|
31
|
+
output: AnnotationRoot<O>;
|
|
32
|
+
};
|
|
25
33
|
/**
|
|
26
34
|
* A graph whose nodes communicate by reading and writing to a shared state.
|
|
27
35
|
* Each node takes a defined `State` as input and returns a `Partial<State>`.
|
|
@@ -84,21 +92,25 @@ export type StateGraphAddNodeOptions = {
|
|
|
84
92
|
* // }
|
|
85
93
|
* ```
|
|
86
94
|
*/
|
|
87
|
-
export declare class StateGraph<SD extends StateDefinition | unknown, S = SD extends StateDefinition ? StateType<SD> : SD, U = SD extends StateDefinition ? UpdateType<SD> : Partial<S>, N extends string = typeof START> extends Graph<N, S, U, StateGraphNodeSpec<S, U>> {
|
|
95
|
+
export declare class StateGraph<SD extends StateDefinition | unknown, S = SD extends StateDefinition ? StateType<SD> : SD, U = SD extends StateDefinition ? UpdateType<SD> : Partial<S>, N extends string = typeof START, I extends StateDefinition = SD extends StateDefinition ? SD : StateDefinition, O extends StateDefinition = SD extends StateDefinition ? SD : StateDefinition> extends Graph<N, S, U, StateGraphNodeSpec<S, U>> {
|
|
88
96
|
channels: Record<string, BaseChannel>;
|
|
89
97
|
waitingEdges: Set<[N[], N]>;
|
|
90
|
-
|
|
98
|
+
schema: StateDefinition;
|
|
99
|
+
input: I;
|
|
100
|
+
output: O;
|
|
101
|
+
constructor(fields: SD extends StateDefinition ? SD | AnnotationRoot<SD> | StateGraphArgs<S> | StateGraphArgsWithStateSchema<SD, I, O> | StateGraphArgsWithInputOutputSchemas<SD, O> : StateGraphArgs<S>);
|
|
91
102
|
get allEdges(): Set<[string, string]>;
|
|
92
|
-
|
|
103
|
+
_addSchema(stateDefinition: StateDefinition): void;
|
|
104
|
+
addNode<K extends string, NodeInput = S>(key: K, action: RunnableLike<NodeInput, U>, options?: StateGraphAddNodeOptions): StateGraph<SD, S, U, N | K, I, O>;
|
|
93
105
|
addEdge(startKey: typeof START | N | N[], endKey: N | typeof END): this;
|
|
94
106
|
compile({ checkpointer, interruptBefore, interruptAfter, }?: {
|
|
95
107
|
checkpointer?: BaseCheckpointSaver;
|
|
96
108
|
interruptBefore?: N[] | All;
|
|
97
109
|
interruptAfter?: N[] | All;
|
|
98
|
-
}): CompiledStateGraph<S, U, N>;
|
|
110
|
+
}): CompiledStateGraph<S, U, N, I, O>;
|
|
99
111
|
}
|
|
100
|
-
export declare class CompiledStateGraph<S, U, N extends string = typeof START> extends CompiledGraph<N, S, U> {
|
|
101
|
-
builder: StateGraph<unknown, S, U, N>;
|
|
112
|
+
export declare class CompiledStateGraph<S, U, N extends string = typeof START, I extends StateDefinition = StateDefinition, O extends StateDefinition = StateDefinition> extends CompiledGraph<N, S, U> {
|
|
113
|
+
builder: StateGraph<unknown, S, U, N, I, O>;
|
|
102
114
|
attachNode(key: typeof START, node?: never): void;
|
|
103
115
|
attachNode(key: N, node: StateGraphNodeSpec<S, U>): void;
|
|
104
116
|
attachEdge(start: N | N[] | "__start__", end: N | "__end__"): void;
|
package/dist/graph/state.js
CHANGED
|
@@ -79,7 +79,7 @@ export class StateGraph extends Graph {
|
|
|
79
79
|
enumerable: true,
|
|
80
80
|
configurable: true,
|
|
81
81
|
writable: true,
|
|
82
|
-
value:
|
|
82
|
+
value: {}
|
|
83
83
|
});
|
|
84
84
|
// TODO: this doesn't dedupe edges as in py, so worth fixing at some point
|
|
85
85
|
Object.defineProperty(this, "waitingEdges", {
|
|
@@ -88,21 +88,50 @@ export class StateGraph extends Graph {
|
|
|
88
88
|
writable: true,
|
|
89
89
|
value: new Set()
|
|
90
90
|
});
|
|
91
|
-
|
|
91
|
+
Object.defineProperty(this, "schema", {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
configurable: true,
|
|
94
|
+
writable: true,
|
|
95
|
+
value: void 0
|
|
96
|
+
});
|
|
97
|
+
Object.defineProperty(this, "input", {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
configurable: true,
|
|
100
|
+
writable: true,
|
|
101
|
+
value: void 0
|
|
102
|
+
});
|
|
103
|
+
Object.defineProperty(this, "output", {
|
|
104
|
+
enumerable: true,
|
|
105
|
+
configurable: true,
|
|
106
|
+
writable: true,
|
|
107
|
+
value: void 0
|
|
108
|
+
});
|
|
109
|
+
if (isStateGraphArgsWithInputOutputSchemas(fields)) {
|
|
110
|
+
this.schema = fields.input.spec;
|
|
111
|
+
this.input = fields.input.spec;
|
|
112
|
+
this.output = fields.output.spec;
|
|
113
|
+
}
|
|
114
|
+
else if (isStateGraphArgsWithStateSchema(fields)) {
|
|
115
|
+
this.schema = fields.stateSchema.spec;
|
|
116
|
+
this.input = (fields.input?.spec ?? this.schema);
|
|
117
|
+
this.output = (fields.output?.spec ?? this.schema);
|
|
118
|
+
}
|
|
119
|
+
else if (isStateDefinition(fields) || isAnnotationRoot(fields)) {
|
|
92
120
|
const spec = isAnnotationRoot(fields) ? fields.spec : fields;
|
|
93
|
-
this.
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
else {
|
|
99
|
-
this.channels[key] = val;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
121
|
+
this.schema = spec;
|
|
122
|
+
}
|
|
123
|
+
else if (isStateGraphArgs(fields)) {
|
|
124
|
+
const spec = _getChannels(fields.channels);
|
|
125
|
+
this.schema = spec;
|
|
102
126
|
}
|
|
103
127
|
else {
|
|
104
|
-
|
|
128
|
+
throw new Error("Invalid StateGraph input.");
|
|
105
129
|
}
|
|
130
|
+
this.input = this.input ?? this.schema;
|
|
131
|
+
this.output = this.output ?? this.schema;
|
|
132
|
+
this._addSchema(this.schema);
|
|
133
|
+
this._addSchema(this.input);
|
|
134
|
+
this._addSchema(this.output);
|
|
106
135
|
for (const c of Object.values(this.channels)) {
|
|
107
136
|
if (c.lc_graph_name === "BinaryOperatorAggregate") {
|
|
108
137
|
this.supportMultipleEdges = true;
|
|
@@ -116,6 +145,27 @@ export class StateGraph extends Graph {
|
|
|
116
145
|
...Array.from(this.waitingEdges).flatMap(([starts, end]) => starts.map((start) => [start, end])),
|
|
117
146
|
]);
|
|
118
147
|
}
|
|
148
|
+
_addSchema(stateDefinition) {
|
|
149
|
+
for (const [key, val] of Object.entries(stateDefinition)) {
|
|
150
|
+
let channel;
|
|
151
|
+
if (typeof val === "function") {
|
|
152
|
+
channel = val();
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
channel = val;
|
|
156
|
+
}
|
|
157
|
+
if (this.channels[key] !== undefined) {
|
|
158
|
+
if (this.channels[key].lc_graph_name !== channel.lc_graph_name) {
|
|
159
|
+
if (channel.lc_graph_name !== "LastValue") {
|
|
160
|
+
throw new Error(`Channel "${key}" already exists with a different type.`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
this.channels[key] = channel;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
119
169
|
addNode(key, action, options) {
|
|
120
170
|
if (key in this.channels) {
|
|
121
171
|
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.`);
|
|
@@ -170,10 +220,10 @@ export class StateGraph extends Graph {
|
|
|
170
220
|
...(Array.isArray(interruptAfter) ? interruptAfter : []),
|
|
171
221
|
]);
|
|
172
222
|
// prepare output channels
|
|
173
|
-
const stateKeys = Object.keys(this.
|
|
174
|
-
const outputChannels = stateKeys.length === 1 && stateKeys[0] === ROOT
|
|
175
|
-
|
|
176
|
-
|
|
223
|
+
const stateKeys = Object.keys(this.schema);
|
|
224
|
+
const outputChannels = stateKeys.length === 1 && stateKeys[0] === ROOT ? ROOT : stateKeys;
|
|
225
|
+
const outputKeys = Object.keys(this.output);
|
|
226
|
+
const streamChannels = outputKeys.length === 1 && outputKeys[0] === ROOT ? ROOT : outputKeys;
|
|
177
227
|
// create empty compiled graph
|
|
178
228
|
const compiled = new CompiledStateGraph({
|
|
179
229
|
builder: this,
|
|
@@ -188,7 +238,7 @@ export class StateGraph extends Graph {
|
|
|
188
238
|
},
|
|
189
239
|
inputChannels: START,
|
|
190
240
|
outputChannels,
|
|
191
|
-
streamChannels
|
|
241
|
+
streamChannels,
|
|
192
242
|
streamMode: "updates",
|
|
193
243
|
});
|
|
194
244
|
// attach nodes, edges and branches
|
|
@@ -225,7 +275,7 @@ function _getChannels(schema) {
|
|
|
225
275
|
}
|
|
226
276
|
export class CompiledStateGraph extends CompiledGraph {
|
|
227
277
|
attachNode(key, node) {
|
|
228
|
-
const stateKeys = Object.keys(this.builder.
|
|
278
|
+
const stateKeys = Object.keys(this.builder.input);
|
|
229
279
|
function getStateKey(key, input) {
|
|
230
280
|
if (!input) {
|
|
231
281
|
return SKIP_WRITE;
|
|
@@ -362,3 +412,20 @@ function isAnnotationRoot(obj) {
|
|
|
362
412
|
"lc_graph_name" in obj &&
|
|
363
413
|
obj.lc_graph_name === "AnnotationRoot");
|
|
364
414
|
}
|
|
415
|
+
function isStateGraphArgs(obj) {
|
|
416
|
+
return (typeof obj === "object" &&
|
|
417
|
+
obj !== null &&
|
|
418
|
+
obj.channels !== undefined);
|
|
419
|
+
}
|
|
420
|
+
function isStateGraphArgsWithStateSchema(obj) {
|
|
421
|
+
return (typeof obj === "object" &&
|
|
422
|
+
obj !== null &&
|
|
423
|
+
obj.stateSchema !== undefined);
|
|
424
|
+
}
|
|
425
|
+
function isStateGraphArgsWithInputOutputSchemas(obj) {
|
|
426
|
+
return (typeof obj === "object" &&
|
|
427
|
+
obj !== null &&
|
|
428
|
+
obj.stateSchema === undefined &&
|
|
429
|
+
obj.input !== undefined &&
|
|
430
|
+
obj.output !== undefined);
|
|
431
|
+
}
|
|
@@ -18,5 +18,5 @@ export interface AgentExecutorState {
|
|
|
18
18
|
export declare function createAgentExecutor({ agentRunnable, tools, }: {
|
|
19
19
|
agentRunnable: Runnable;
|
|
20
20
|
tools: Array<Tool> | ToolExecutor;
|
|
21
|
-
}): import("../graph/state.js").CompiledStateGraph<AgentExecutorState, Partial<AgentExecutorState>, "__start__" | "agent" | "action">;
|
|
21
|
+
}): import("../graph/state.js").CompiledStateGraph<AgentExecutorState, Partial<AgentExecutorState>, "__start__" | "agent" | "action", import("../graph/annotation.js").StateDefinition, import("../graph/annotation.js").StateDefinition>;
|
|
22
22
|
export {};
|
|
@@ -2,11 +2,10 @@ import { BaseChatModel } from "@langchain/core/language_models/chat_models";
|
|
|
2
2
|
import { BaseMessage, SystemMessage } from "@langchain/core/messages";
|
|
3
3
|
import { Runnable, RunnableToolLike } from "@langchain/core/runnables";
|
|
4
4
|
import { StructuredToolInterface } from "@langchain/core/tools";
|
|
5
|
-
import { BaseCheckpointSaver } from "@langchain/langgraph-checkpoint";
|
|
5
|
+
import { All, BaseCheckpointSaver } from "@langchain/langgraph-checkpoint";
|
|
6
6
|
import { START } from "../graph/index.js";
|
|
7
7
|
import { MessagesAnnotation } from "../graph/messages_annotation.js";
|
|
8
8
|
import { CompiledStateGraph } from "../graph/state.js";
|
|
9
|
-
import { All } from "../pregel/types.js";
|
|
10
9
|
import { ToolNode } from "./tool_node.js";
|
|
11
10
|
export interface AgentState {
|
|
12
11
|
messages: BaseMessage[];
|
package/dist/pregel/algo.cjs
CHANGED
|
@@ -218,7 +218,7 @@ function _prepareNextTasks(checkpoint, processes, channels, config, forExecution
|
|
|
218
218
|
}
|
|
219
219
|
}
|
|
220
220
|
else {
|
|
221
|
-
taskDescriptions.push({ id: taskId, name: packet.node });
|
|
221
|
+
taskDescriptions.push({ id: taskId, name: packet.node, interrupts: [] });
|
|
222
222
|
}
|
|
223
223
|
}
|
|
224
224
|
// Check if any processes should be run in next step
|
|
@@ -291,7 +291,7 @@ function _prepareNextTasks(checkpoint, processes, channels, config, forExecution
|
|
|
291
291
|
}
|
|
292
292
|
}
|
|
293
293
|
else {
|
|
294
|
-
taskDescriptions.push({ id: taskId, name });
|
|
294
|
+
taskDescriptions.push({ id: taskId, name, interrupts: [] });
|
|
295
295
|
}
|
|
296
296
|
}
|
|
297
297
|
}
|
package/dist/pregel/algo.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { RunnableConfig } from "@langchain/core/runnables";
|
|
2
2
|
import { CallbackManagerForChainRun } from "@langchain/core/callbacks/manager";
|
|
3
|
-
import { BaseCheckpointSaver, Checkpoint, ReadonlyCheckpoint, type PendingWrite } from "@langchain/langgraph-checkpoint";
|
|
3
|
+
import { All, BaseCheckpointSaver, Checkpoint, ReadonlyCheckpoint, type PendingWrite } from "@langchain/langgraph-checkpoint";
|
|
4
4
|
import { BaseChannel } from "../channels/base.js";
|
|
5
5
|
import { PregelNode } from "./read.js";
|
|
6
|
-
import {
|
|
6
|
+
import { PregelExecutableTask, PregelTaskDescription } from "./types.js";
|
|
7
7
|
/**
|
|
8
8
|
* Construct a type with a set of properties K of type T
|
|
9
9
|
*/
|
package/dist/pregel/algo.js
CHANGED
|
@@ -210,7 +210,7 @@ export function _prepareNextTasks(checkpoint, processes, channels, config, forEx
|
|
|
210
210
|
}
|
|
211
211
|
}
|
|
212
212
|
else {
|
|
213
|
-
taskDescriptions.push({ id: taskId, name: packet.node });
|
|
213
|
+
taskDescriptions.push({ id: taskId, name: packet.node, interrupts: [] });
|
|
214
214
|
}
|
|
215
215
|
}
|
|
216
216
|
// Check if any processes should be run in next step
|
|
@@ -283,7 +283,7 @@ export function _prepareNextTasks(checkpoint, processes, channels, config, forEx
|
|
|
283
283
|
}
|
|
284
284
|
}
|
|
285
285
|
else {
|
|
286
|
-
taskDescriptions.push({ id: taskId, name });
|
|
286
|
+
taskDescriptions.push({ id: taskId, name, interrupts: [] });
|
|
287
287
|
}
|
|
288
288
|
}
|
|
289
289
|
}
|