@langchain/langgraph 0.2.18 → 0.2.19
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/errors.cjs +14 -1
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +12 -0
- package/dist/graph/graph.cjs +150 -0
- package/dist/graph/graph.d.ts +8 -0
- package/dist/graph/graph.js +151 -1
- package/dist/pregel/index.cjs +21 -5
- package/dist/pregel/index.d.ts +7 -25
- package/dist/pregel/index.js +21 -5
- package/dist/pregel/remote.cjs +447 -0
- package/dist/pregel/remote.d.ts +113 -0
- package/dist/pregel/remote.js +443 -0
- package/dist/pregel/types.d.ts +44 -5
- package/dist/pregel/utils/subgraph.cjs +1 -4
- package/dist/pregel/utils/subgraph.js +1 -4
- package/dist/remote.cjs +5 -0
- package/dist/remote.d.ts +1 -0
- package/dist/remote.js +1 -0
- package/package.json +21 -7
- package/remote.cjs +1 -0
- package/remote.d.cts +1 -0
- package/remote.d.ts +1 -0
- package/remote.js +1 -0
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
import { Client, } from "@langchain/langgraph-sdk";
|
|
2
|
+
import { Graph as DrawableGraph, } from "@langchain/core/runnables/graph";
|
|
3
|
+
import { mergeConfigs, Runnable, } from "@langchain/core/runnables";
|
|
4
|
+
import { isBaseMessage } from "@langchain/core/messages";
|
|
5
|
+
import { GraphInterrupt, RemoteException, } from "../web.js";
|
|
6
|
+
import { CHECKPOINT_NAMESPACE_SEPARATOR, CONFIG_KEY_STREAM, INTERRUPT, } from "../constants.js";
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
|
+
const _serializeInputs = (obj) => {
|
|
9
|
+
if (obj === null || typeof obj !== "object") {
|
|
10
|
+
return obj;
|
|
11
|
+
}
|
|
12
|
+
if (Array.isArray(obj)) {
|
|
13
|
+
return obj.map(_serializeInputs);
|
|
14
|
+
}
|
|
15
|
+
// Handle BaseMessage instances by converting them to a serializable format
|
|
16
|
+
if (isBaseMessage(obj)) {
|
|
17
|
+
const dict = obj.toDict();
|
|
18
|
+
return {
|
|
19
|
+
...dict.data,
|
|
20
|
+
role: obj.getType(),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, _serializeInputs(value)]));
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Return a tuple of the final list of stream modes sent to the
|
|
27
|
+
* remote graph and a boolean flag indicating if only one stream mode was
|
|
28
|
+
* originally requested and whether stream mode 'updates'
|
|
29
|
+
* was present in the original list of stream modes.
|
|
30
|
+
*
|
|
31
|
+
* 'updates' mode is always added to the list of stream modes so that interrupts
|
|
32
|
+
* can be detected in the remote graph.
|
|
33
|
+
*/
|
|
34
|
+
const getStreamModes = (streamMode, defaultStreamMode = "updates") => {
|
|
35
|
+
const updatedStreamModes = [];
|
|
36
|
+
let reqUpdates = false;
|
|
37
|
+
let reqSingle = true;
|
|
38
|
+
if (streamMode !== undefined &&
|
|
39
|
+
(typeof streamMode === "string" ||
|
|
40
|
+
(Array.isArray(streamMode) && streamMode.length > 0))) {
|
|
41
|
+
if (typeof streamMode === "string") {
|
|
42
|
+
updatedStreamModes.push(streamMode);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
reqSingle = false;
|
|
46
|
+
updatedStreamModes.push(...streamMode);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
updatedStreamModes.push(defaultStreamMode);
|
|
51
|
+
}
|
|
52
|
+
// TODO: Map messages to messages-tuple
|
|
53
|
+
if (updatedStreamModes.includes("updates")) {
|
|
54
|
+
reqUpdates = true;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
updatedStreamModes.push("updates");
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
updatedStreamModes,
|
|
61
|
+
reqUpdates,
|
|
62
|
+
reqSingle,
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* The `RemoteGraph` class is a client implementation for calling remote
|
|
67
|
+
* APIs that implement the LangGraph Server API specification.
|
|
68
|
+
*
|
|
69
|
+
* For example, the `RemoteGraph` class can be used to call APIs from deployments
|
|
70
|
+
* on LangGraph Cloud.
|
|
71
|
+
*
|
|
72
|
+
* `RemoteGraph` behaves the same way as a `StateGraph` and can be used directly as
|
|
73
|
+
* a node in another `StateGraph`.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { RemoteGraph } from "@langchain/langgraph/remote";
|
|
78
|
+
*
|
|
79
|
+
* // Can also pass a LangGraph SDK client instance directly
|
|
80
|
+
* const remoteGraph = new RemoteGraph({
|
|
81
|
+
* graphId: process.env.LANGGRAPH_REMOTE_GRAPH_ID!,
|
|
82
|
+
* apiKey: process.env.LANGGRAPH_REMOTE_GRAPH_API_KEY,
|
|
83
|
+
* url: process.env.LANGGRAPH_REMOTE_GRAPH_API_URL,
|
|
84
|
+
* });
|
|
85
|
+
*
|
|
86
|
+
* const input = {
|
|
87
|
+
* messages: [
|
|
88
|
+
* {
|
|
89
|
+
* role: "human",
|
|
90
|
+
* content: "Hello world!",
|
|
91
|
+
* },
|
|
92
|
+
* ],
|
|
93
|
+
* };
|
|
94
|
+
*
|
|
95
|
+
* const config = {
|
|
96
|
+
* configurable: { thread_id: "threadId1" },
|
|
97
|
+
* };
|
|
98
|
+
*
|
|
99
|
+
* await remoteGraph.invoke(input, config);
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
export class RemoteGraph extends Runnable {
|
|
103
|
+
static lc_name() {
|
|
104
|
+
return "RemoteGraph";
|
|
105
|
+
}
|
|
106
|
+
constructor(params) {
|
|
107
|
+
super(params);
|
|
108
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
109
|
+
enumerable: true,
|
|
110
|
+
configurable: true,
|
|
111
|
+
writable: true,
|
|
112
|
+
value: ["langgraph", "pregel"]
|
|
113
|
+
});
|
|
114
|
+
Object.defineProperty(this, "lg_is_pregel", {
|
|
115
|
+
enumerable: true,
|
|
116
|
+
configurable: true,
|
|
117
|
+
writable: true,
|
|
118
|
+
value: true
|
|
119
|
+
});
|
|
120
|
+
Object.defineProperty(this, "config", {
|
|
121
|
+
enumerable: true,
|
|
122
|
+
configurable: true,
|
|
123
|
+
writable: true,
|
|
124
|
+
value: void 0
|
|
125
|
+
});
|
|
126
|
+
Object.defineProperty(this, "graphId", {
|
|
127
|
+
enumerable: true,
|
|
128
|
+
configurable: true,
|
|
129
|
+
writable: true,
|
|
130
|
+
value: void 0
|
|
131
|
+
});
|
|
132
|
+
Object.defineProperty(this, "client", {
|
|
133
|
+
enumerable: true,
|
|
134
|
+
configurable: true,
|
|
135
|
+
writable: true,
|
|
136
|
+
value: void 0
|
|
137
|
+
});
|
|
138
|
+
Object.defineProperty(this, "interruptBefore", {
|
|
139
|
+
enumerable: true,
|
|
140
|
+
configurable: true,
|
|
141
|
+
writable: true,
|
|
142
|
+
value: void 0
|
|
143
|
+
});
|
|
144
|
+
Object.defineProperty(this, "interruptAfter", {
|
|
145
|
+
enumerable: true,
|
|
146
|
+
configurable: true,
|
|
147
|
+
writable: true,
|
|
148
|
+
value: void 0
|
|
149
|
+
});
|
|
150
|
+
this.graphId = params.graphId;
|
|
151
|
+
this.client =
|
|
152
|
+
params.client ??
|
|
153
|
+
new Client({
|
|
154
|
+
apiUrl: params.url,
|
|
155
|
+
apiKey: params.apiKey,
|
|
156
|
+
defaultHeaders: params.headers,
|
|
157
|
+
});
|
|
158
|
+
this.config = params.config;
|
|
159
|
+
this.interruptBefore = params.interruptBefore;
|
|
160
|
+
this.interruptAfter = params.interruptAfter;
|
|
161
|
+
}
|
|
162
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
163
|
+
// @ts-ignore Remove ignore when we remove support for 0.2 versions of core
|
|
164
|
+
withConfig(config) {
|
|
165
|
+
const mergedConfig = mergeConfigs(this.config, config);
|
|
166
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
167
|
+
return new this.constructor({ ...this, config: mergedConfig });
|
|
168
|
+
}
|
|
169
|
+
_sanitizeConfig(config) {
|
|
170
|
+
const reservedConfigurableKeys = new Set([
|
|
171
|
+
"callbacks",
|
|
172
|
+
"checkpoint_map",
|
|
173
|
+
"checkpoint_id",
|
|
174
|
+
"checkpoint_ns",
|
|
175
|
+
]);
|
|
176
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
177
|
+
const sanitizeObj = (obj) => {
|
|
178
|
+
// Remove non-JSON serializable fields from the given object
|
|
179
|
+
if (obj && typeof obj === "object") {
|
|
180
|
+
if (Array.isArray(obj)) {
|
|
181
|
+
return obj.map((v) => sanitizeObj(v));
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, sanitizeObj(v)]));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
JSON.stringify(obj);
|
|
189
|
+
return obj;
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
// Remove non-JSON serializable fields from the config
|
|
196
|
+
const sanitizedConfig = sanitizeObj(config);
|
|
197
|
+
// Only include configurable keys that are not reserved and
|
|
198
|
+
// not starting with "__pregel_" prefix
|
|
199
|
+
const newConfigurable = Object.fromEntries(Object.entries(sanitizedConfig.configurable ?? {}).filter(([k]) => !reservedConfigurableKeys.has(k) && !k.startsWith("__pregel_")));
|
|
200
|
+
return {
|
|
201
|
+
tags: sanitizedConfig.tags ?? [],
|
|
202
|
+
metadata: sanitizedConfig.metadata ?? {},
|
|
203
|
+
configurable: newConfigurable,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
_getConfig(checkpoint) {
|
|
207
|
+
return {
|
|
208
|
+
configurable: {
|
|
209
|
+
thread_id: checkpoint.thread_id,
|
|
210
|
+
checkpoint_ns: checkpoint.checkpoint_ns,
|
|
211
|
+
checkpoint_id: checkpoint.checkpoint_id,
|
|
212
|
+
checkpoint_map: checkpoint.checkpoint_map ?? {},
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
_getCheckpoint(config) {
|
|
217
|
+
if (config?.configurable === undefined) {
|
|
218
|
+
return undefined;
|
|
219
|
+
}
|
|
220
|
+
const checkpointKeys = [
|
|
221
|
+
"thread_id",
|
|
222
|
+
"checkpoint_ns",
|
|
223
|
+
"checkpoint_id",
|
|
224
|
+
"checkpoint_map",
|
|
225
|
+
];
|
|
226
|
+
const checkpoint = Object.fromEntries(checkpointKeys
|
|
227
|
+
.map((key) => [key, config.configurable[key]])
|
|
228
|
+
.filter(([_, value]) => value !== undefined));
|
|
229
|
+
return Object.keys(checkpoint).length > 0 ? checkpoint : undefined;
|
|
230
|
+
}
|
|
231
|
+
_createStateSnapshot(state) {
|
|
232
|
+
const tasks = state.tasks.map((task) => {
|
|
233
|
+
return {
|
|
234
|
+
id: task.id,
|
|
235
|
+
name: task.name,
|
|
236
|
+
error: task.error ? { message: task.error } : undefined,
|
|
237
|
+
interrupts: task.interrupts,
|
|
238
|
+
// eslint-disable-next-line no-nested-ternary
|
|
239
|
+
state: task.state
|
|
240
|
+
? this._createStateSnapshot(task.state)
|
|
241
|
+
: task.checkpoint
|
|
242
|
+
? { configurable: task.checkpoint }
|
|
243
|
+
: undefined,
|
|
244
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
245
|
+
result: task.result,
|
|
246
|
+
};
|
|
247
|
+
});
|
|
248
|
+
return {
|
|
249
|
+
values: state.values,
|
|
250
|
+
next: state.next ? [...state.next] : [],
|
|
251
|
+
config: {
|
|
252
|
+
configurable: {
|
|
253
|
+
thread_id: state.checkpoint.thread_id,
|
|
254
|
+
checkpoint_ns: state.checkpoint.checkpoint_ns,
|
|
255
|
+
checkpoint_id: state.checkpoint.checkpoint_id,
|
|
256
|
+
checkpoint_map: state.checkpoint.checkpoint_map ?? {},
|
|
257
|
+
},
|
|
258
|
+
},
|
|
259
|
+
metadata: state.metadata
|
|
260
|
+
? state.metadata
|
|
261
|
+
: undefined,
|
|
262
|
+
createdAt: state.created_at ?? undefined,
|
|
263
|
+
parentConfig: state.parent_checkpoint
|
|
264
|
+
? {
|
|
265
|
+
configurable: {
|
|
266
|
+
thread_id: state.parent_checkpoint.thread_id,
|
|
267
|
+
checkpoint_ns: state.parent_checkpoint.checkpoint_ns,
|
|
268
|
+
checkpoint_id: state.parent_checkpoint.checkpoint_id,
|
|
269
|
+
checkpoint_map: state.parent_checkpoint.checkpoint_map ?? {},
|
|
270
|
+
},
|
|
271
|
+
}
|
|
272
|
+
: undefined,
|
|
273
|
+
tasks,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
async invoke(input, options) {
|
|
277
|
+
let lastValue;
|
|
278
|
+
const stream = await this.stream(input, {
|
|
279
|
+
...options,
|
|
280
|
+
streamMode: "values",
|
|
281
|
+
});
|
|
282
|
+
for await (const chunk of stream) {
|
|
283
|
+
lastValue = chunk;
|
|
284
|
+
}
|
|
285
|
+
return lastValue;
|
|
286
|
+
}
|
|
287
|
+
streamEvents(_input, _options) {
|
|
288
|
+
throw new Error("Not implemented.");
|
|
289
|
+
}
|
|
290
|
+
async *_streamIterator(input, options) {
|
|
291
|
+
const mergedConfig = mergeConfigs(this.config, options);
|
|
292
|
+
const sanitizedConfig = this._sanitizeConfig(mergedConfig);
|
|
293
|
+
const streamProtocolInstance = options?.configurable?.[CONFIG_KEY_STREAM];
|
|
294
|
+
const streamSubgraphs = options?.subgraphs ?? streamProtocolInstance !== undefined;
|
|
295
|
+
const interruptBefore = this.interruptBefore ?? options?.interruptBefore;
|
|
296
|
+
const interruptAfter = this.interruptAfter ?? options?.interruptAfter;
|
|
297
|
+
const { updatedStreamModes, reqSingle, reqUpdates } = getStreamModes(options?.streamMode);
|
|
298
|
+
const extendedStreamModes = [
|
|
299
|
+
...new Set([
|
|
300
|
+
...updatedStreamModes,
|
|
301
|
+
...(streamProtocolInstance?.modes ?? new Set()),
|
|
302
|
+
]),
|
|
303
|
+
];
|
|
304
|
+
for await (const chunk of this.client.runs.stream(sanitizedConfig.configurable.thread_id, this.graphId, {
|
|
305
|
+
input: _serializeInputs(input),
|
|
306
|
+
config: sanitizedConfig,
|
|
307
|
+
streamMode: extendedStreamModes,
|
|
308
|
+
interruptBefore: interruptBefore,
|
|
309
|
+
interruptAfter: interruptAfter,
|
|
310
|
+
streamSubgraphs,
|
|
311
|
+
ifNotExists: "create",
|
|
312
|
+
})) {
|
|
313
|
+
let mode;
|
|
314
|
+
let namespace;
|
|
315
|
+
if (chunk.event.includes(CHECKPOINT_NAMESPACE_SEPARATOR)) {
|
|
316
|
+
const eventComponents = chunk.event.split(CHECKPOINT_NAMESPACE_SEPARATOR);
|
|
317
|
+
// eslint-disable-next-line prefer-destructuring
|
|
318
|
+
mode = eventComponents[0];
|
|
319
|
+
namespace = eventComponents.slice(1);
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
mode = chunk.event;
|
|
323
|
+
namespace = [];
|
|
324
|
+
}
|
|
325
|
+
const callerNamespace = options?.configurable?.checkpoint_ns;
|
|
326
|
+
if (typeof callerNamespace === "string") {
|
|
327
|
+
namespace = callerNamespace
|
|
328
|
+
.split(CHECKPOINT_NAMESPACE_SEPARATOR)
|
|
329
|
+
.concat(namespace);
|
|
330
|
+
}
|
|
331
|
+
if (streamProtocolInstance !== undefined &&
|
|
332
|
+
streamProtocolInstance.modes?.has(chunk.event)) {
|
|
333
|
+
streamProtocolInstance.push([namespace, mode, chunk.data]);
|
|
334
|
+
}
|
|
335
|
+
if (chunk.event.startsWith("updates")) {
|
|
336
|
+
if (typeof chunk.data === "object" &&
|
|
337
|
+
chunk.data?.[INTERRUPT] !== undefined) {
|
|
338
|
+
throw new GraphInterrupt(chunk.data[INTERRUPT]);
|
|
339
|
+
}
|
|
340
|
+
if (!reqUpdates) {
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
else if (chunk.event?.startsWith("error")) {
|
|
345
|
+
throw new RemoteException(typeof chunk.data === "string"
|
|
346
|
+
? chunk.data
|
|
347
|
+
: JSON.stringify(chunk.data));
|
|
348
|
+
}
|
|
349
|
+
if (!updatedStreamModes.includes(chunk.event.split(CHECKPOINT_NAMESPACE_SEPARATOR)[0])) {
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
if (options?.subgraphs) {
|
|
353
|
+
if (reqSingle) {
|
|
354
|
+
yield [namespace, chunk.data];
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
yield [namespace, mode, chunk.data];
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
else if (reqSingle) {
|
|
361
|
+
yield chunk.data;
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
yield [mode, chunk.data];
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
async updateState(inputConfig, values, asNode) {
|
|
369
|
+
const mergedConfig = mergeConfigs(this.config, inputConfig);
|
|
370
|
+
const response = await this.client.threads.updateState(mergedConfig.configurable?.thread_id, { values, asNode, checkpoint: this._getCheckpoint(mergedConfig) });
|
|
371
|
+
// TODO: Fix SDK typing
|
|
372
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
373
|
+
return this._getConfig(response.checkpoint);
|
|
374
|
+
}
|
|
375
|
+
async *getStateHistory(config, options) {
|
|
376
|
+
const mergedConfig = mergeConfigs(this.config, config);
|
|
377
|
+
const states = await this.client.threads.getHistory(mergedConfig.configurable?.thread_id, {
|
|
378
|
+
limit: options?.limit ?? 10,
|
|
379
|
+
// TODO: Fix type
|
|
380
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
381
|
+
before: this._getCheckpoint(options?.before),
|
|
382
|
+
metadata: options?.filter,
|
|
383
|
+
checkpoint: this._getCheckpoint(mergedConfig),
|
|
384
|
+
});
|
|
385
|
+
for (const state of states) {
|
|
386
|
+
yield this._createStateSnapshot(state);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
_getDrawableNodes(nodes) {
|
|
390
|
+
const nodesMap = {};
|
|
391
|
+
for (const node of nodes) {
|
|
392
|
+
const nodeId = node.id;
|
|
393
|
+
nodesMap[nodeId] = {
|
|
394
|
+
id: nodeId.toString(),
|
|
395
|
+
name: typeof node.data === "string" ? node.data : node.data?.name ?? "",
|
|
396
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
397
|
+
data: node.data ?? {},
|
|
398
|
+
metadata: typeof node.data !== "string" ? node.data?.metadata ?? {} : {},
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
return nodesMap;
|
|
402
|
+
}
|
|
403
|
+
async getState(config, options) {
|
|
404
|
+
const mergedConfig = mergeConfigs(this.config, config);
|
|
405
|
+
const state = await this.client.threads.getState(mergedConfig.configurable?.thread_id, this._getCheckpoint(mergedConfig), options);
|
|
406
|
+
return this._createStateSnapshot(state);
|
|
407
|
+
}
|
|
408
|
+
/** @deprecated Use getGraphAsync instead. The async method will become the default in the next minor release. */
|
|
409
|
+
getGraph(_) {
|
|
410
|
+
throw new Error(`The synchronous "getGraph" is not supported for this graph. Call "getGraphAsync" instead.`);
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Returns a drawable representation of the computation graph.
|
|
414
|
+
*/
|
|
415
|
+
async getGraphAsync(config) {
|
|
416
|
+
const graph = await this.client.assistants.getGraph(this.graphId, {
|
|
417
|
+
xray: config?.xray,
|
|
418
|
+
});
|
|
419
|
+
return new DrawableGraph({
|
|
420
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
421
|
+
nodes: this._getDrawableNodes(graph.nodes),
|
|
422
|
+
edges: graph.edges,
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
/** @deprecated Use getSubgraphsAsync instead. The async method will become the default in the next minor release. */
|
|
426
|
+
getSubgraphs() {
|
|
427
|
+
throw new Error(`The synchronous "getSubgraphs" method is not supported for this graph. Call "getSubgraphsAsync" instead.`);
|
|
428
|
+
}
|
|
429
|
+
async *getSubgraphsAsync(namespace, recurse = false) {
|
|
430
|
+
const subgraphs = await this.client.assistants.getSubgraphs(this.graphId, {
|
|
431
|
+
namespace,
|
|
432
|
+
recurse,
|
|
433
|
+
});
|
|
434
|
+
for (const [ns, graphSchema] of Object.entries(subgraphs)) {
|
|
435
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
436
|
+
const remoteSubgraph = new this.constructor({
|
|
437
|
+
...this,
|
|
438
|
+
graphId: graphSchema.graph_id,
|
|
439
|
+
});
|
|
440
|
+
yield [ns, remoteSubgraph];
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
package/dist/pregel/types.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { Runnable, RunnableConfig } from "@langchain/core/runnables";
|
|
2
|
-
import type { All, PendingWrite, CheckpointMetadata, BaseCheckpointSaver, BaseStore } from "@langchain/langgraph-checkpoint";
|
|
2
|
+
import type { All, PendingWrite, CheckpointMetadata, BaseCheckpointSaver, BaseStore, CheckpointListOptions } from "@langchain/langgraph-checkpoint";
|
|
3
|
+
import { Graph as DrawableGraph } from "@langchain/core/runnables/graph";
|
|
4
|
+
import { IterableReadableStream } from "@langchain/core/utils/stream";
|
|
3
5
|
import type { BaseChannel } from "../channels/base.js";
|
|
4
6
|
import type { PregelNode } from "./read.js";
|
|
5
7
|
import { RetryPolicy } from "./utils/index.js";
|
|
@@ -7,13 +9,52 @@ import { Interrupt } from "../constants.js";
|
|
|
7
9
|
import { type ManagedValueSpec } from "../managed/base.js";
|
|
8
10
|
import { LangGraphRunnableConfig } from "./runnable_types.js";
|
|
9
11
|
export type StreamMode = "values" | "updates" | "debug";
|
|
12
|
+
export type PregelInputType = any;
|
|
13
|
+
export type PregelOutputType = any;
|
|
14
|
+
/**
|
|
15
|
+
* Config for executing the graph.
|
|
16
|
+
*/
|
|
17
|
+
export interface PregelOptions<Nn extends StrRecord<string, PregelNode>, Cc extends StrRecord<string, BaseChannel | ManagedValueSpec>, ConfigurableFieldType extends Record<string, any> = Record<string, any>> extends RunnableConfig<ConfigurableFieldType> {
|
|
18
|
+
/** The stream mode for the graph run. Default is ["values"]. */
|
|
19
|
+
streamMode?: StreamMode | StreamMode[];
|
|
20
|
+
inputKeys?: keyof Cc | Array<keyof Cc>;
|
|
21
|
+
/** The output keys to retrieve from the graph run. */
|
|
22
|
+
outputKeys?: keyof Cc | Array<keyof Cc>;
|
|
23
|
+
/** The nodes to interrupt the graph run before. */
|
|
24
|
+
interruptBefore?: All | Array<keyof Nn>;
|
|
25
|
+
/** The nodes to interrupt the graph run after. */
|
|
26
|
+
interruptAfter?: All | Array<keyof Nn>;
|
|
27
|
+
/** Enable debug mode for the graph run. */
|
|
28
|
+
debug?: boolean;
|
|
29
|
+
/** Whether to stream subgraphs. */
|
|
30
|
+
subgraphs?: boolean;
|
|
31
|
+
/** The shared value store */
|
|
32
|
+
store?: BaseStore;
|
|
33
|
+
}
|
|
10
34
|
/**
|
|
11
35
|
* Construct a type with a set of properties K of type T
|
|
12
36
|
*/
|
|
13
37
|
type StrRecord<K extends string, T> = {
|
|
14
38
|
[P in K]: T;
|
|
15
39
|
};
|
|
16
|
-
export interface PregelInterface<Nn extends StrRecord<string, PregelNode>, Cc extends StrRecord<string, BaseChannel | ManagedValueSpec>> {
|
|
40
|
+
export interface PregelInterface<Nn extends StrRecord<string, PregelNode>, Cc extends StrRecord<string, BaseChannel | ManagedValueSpec>, ConfigurableFieldType extends Record<string, any> = StrRecord<string, any>> {
|
|
41
|
+
lg_is_pregel: boolean;
|
|
42
|
+
withConfig(config: RunnableConfig): PregelInterface<Nn, Cc>;
|
|
43
|
+
getGraphAsync(config: RunnableConfig & {
|
|
44
|
+
xray?: boolean | number;
|
|
45
|
+
}): Promise<DrawableGraph>;
|
|
46
|
+
/** @deprecated Use getSubgraphsAsync instead. The async method will become the default in the next minor release. */
|
|
47
|
+
getSubgraphs(namespace?: string, recurse?: boolean): Generator<[string, PregelInterface<any, any>]>;
|
|
48
|
+
getSubgraphsAsync(namespace?: string, recurse?: boolean): AsyncGenerator<[string, PregelInterface<any, any>]>;
|
|
49
|
+
getState(config: RunnableConfig, options?: {
|
|
50
|
+
subgraphs?: boolean;
|
|
51
|
+
}): Promise<StateSnapshot>;
|
|
52
|
+
getStateHistory(config: RunnableConfig, options?: CheckpointListOptions): AsyncIterableIterator<StateSnapshot>;
|
|
53
|
+
updateState(inputConfig: LangGraphRunnableConfig, values: Record<string, unknown> | unknown, asNode?: keyof Nn | string): Promise<RunnableConfig>;
|
|
54
|
+
stream(input: PregelInputType, options?: Partial<PregelOptions<Nn, Cc, ConfigurableFieldType>>): Promise<IterableReadableStream<PregelOutputType>>;
|
|
55
|
+
invoke(input: PregelInputType, options?: Partial<PregelOptions<Nn, Cc, ConfigurableFieldType>>): Promise<PregelOutputType>;
|
|
56
|
+
}
|
|
57
|
+
export type PregelParams<Nn extends StrRecord<string, PregelNode>, Cc extends StrRecord<string, BaseChannel | ManagedValueSpec>> = {
|
|
17
58
|
nodes: Nn;
|
|
18
59
|
channels: Cc;
|
|
19
60
|
/**
|
|
@@ -35,7 +76,6 @@ export interface PregelInterface<Nn extends StrRecord<string, PregelNode>, Cc ex
|
|
|
35
76
|
*/
|
|
36
77
|
interruptBefore?: Array<keyof Nn> | All;
|
|
37
78
|
streamChannels?: keyof Cc | Array<keyof Cc>;
|
|
38
|
-
get streamChannelsAsIs(): keyof Cc | Array<keyof Cc>;
|
|
39
79
|
/**
|
|
40
80
|
* @default undefined
|
|
41
81
|
*/
|
|
@@ -51,8 +91,7 @@ export interface PregelInterface<Nn extends StrRecord<string, PregelNode>, Cc ex
|
|
|
51
91
|
* Memory store to use for SharedValues.
|
|
52
92
|
*/
|
|
53
93
|
store?: BaseStore;
|
|
54
|
-
}
|
|
55
|
-
export type PregelParams<Nn extends StrRecord<string, PregelNode>, Cc extends StrRecord<string, BaseChannel | ManagedValueSpec>> = Omit<PregelInterface<Nn, Cc>, "streamChannelsAsIs">;
|
|
94
|
+
};
|
|
56
95
|
export interface PregelTaskDescription {
|
|
57
96
|
readonly id: string;
|
|
58
97
|
readonly name: string;
|
|
@@ -10,10 +10,7 @@ function isPregelLike(
|
|
|
10
10
|
x
|
|
11
11
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
12
|
) {
|
|
13
|
-
return
|
|
14
|
-
x.inputChannels !== undefined &&
|
|
15
|
-
"outputChannels" &&
|
|
16
|
-
x.outputChannels !== undefined);
|
|
13
|
+
return "lg_is_pregel" in x && x.lg_is_pregel === true;
|
|
17
14
|
}
|
|
18
15
|
exports.isPregelLike = isPregelLike;
|
|
19
16
|
function findSubgraphPregel(candidate
|
|
@@ -7,10 +7,7 @@ export function isPregelLike(
|
|
|
7
7
|
x
|
|
8
8
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
9
|
) {
|
|
10
|
-
return
|
|
11
|
-
x.inputChannels !== undefined &&
|
|
12
|
-
"outputChannels" &&
|
|
13
|
-
x.outputChannels !== undefined);
|
|
10
|
+
return "lg_is_pregel" in x && x.lg_is_pregel === true;
|
|
14
11
|
}
|
|
15
12
|
export function findSubgraphPregel(candidate
|
|
16
13
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
package/dist/remote.cjs
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RemoteGraph = void 0;
|
|
4
|
+
var remote_js_1 = require("./pregel/remote.cjs");
|
|
5
|
+
Object.defineProperty(exports, "RemoteGraph", { enumerable: true, get: function () { return remote_js_1.RemoteGraph; } });
|
package/dist/remote.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { RemoteGraph, type RemoteGraphParams } from "./pregel/remote.js";
|
package/dist/remote.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { RemoteGraph } from "./pregel/remote.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/langgraph",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.19",
|
|
4
4
|
"description": "LangGraph",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"license": "MIT",
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@langchain/langgraph-checkpoint": "~0.0.10",
|
|
35
|
+
"@langchain/langgraph-sdk": "~0.0.20",
|
|
35
36
|
"double-ended-queue": "^2.1.0-0",
|
|
36
37
|
"uuid": "^10.0.0",
|
|
37
38
|
"zod": "^3.23.8"
|
|
@@ -41,12 +42,12 @@
|
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
44
|
"@jest/globals": "^29.5.0",
|
|
44
|
-
"@langchain/anthropic": "^0.3.
|
|
45
|
-
"@langchain/community": "^0.3.
|
|
46
|
-
"@langchain/core": "^0.3.
|
|
45
|
+
"@langchain/anthropic": "^0.3.5",
|
|
46
|
+
"@langchain/community": "^0.3.9",
|
|
47
|
+
"@langchain/core": "^0.3.15",
|
|
47
48
|
"@langchain/langgraph-checkpoint-postgres": "workspace:*",
|
|
48
49
|
"@langchain/langgraph-checkpoint-sqlite": "workspace:*",
|
|
49
|
-
"@langchain/openai": "^0.3.
|
|
50
|
+
"@langchain/openai": "^0.3.11",
|
|
50
51
|
"@langchain/scripts": ">=0.1.3 <0.2.0",
|
|
51
52
|
"@swc/core": "^1.3.90",
|
|
52
53
|
"@swc/jest": "^0.2.29",
|
|
@@ -69,7 +70,7 @@
|
|
|
69
70
|
"eslint-plugin-prettier": "^4.2.1",
|
|
70
71
|
"jest": "^29.5.0",
|
|
71
72
|
"jest-environment-node": "^29.6.4",
|
|
72
|
-
"langchain": "^0.3.
|
|
73
|
+
"langchain": "^0.3.4",
|
|
73
74
|
"pg": "^8.13.0",
|
|
74
75
|
"prettier": "^2.8.3",
|
|
75
76
|
"release-it": "^17.6.0",
|
|
@@ -120,6 +121,15 @@
|
|
|
120
121
|
"import": "./prebuilt.js",
|
|
121
122
|
"require": "./prebuilt.cjs"
|
|
122
123
|
},
|
|
124
|
+
"./remote": {
|
|
125
|
+
"types": {
|
|
126
|
+
"import": "./remote.d.ts",
|
|
127
|
+
"require": "./remote.d.cts",
|
|
128
|
+
"default": "./remote.d.ts"
|
|
129
|
+
},
|
|
130
|
+
"import": "./remote.js",
|
|
131
|
+
"require": "./remote.cjs"
|
|
132
|
+
},
|
|
123
133
|
"./package.json": "./package.json"
|
|
124
134
|
},
|
|
125
135
|
"files": [
|
|
@@ -139,6 +149,10 @@
|
|
|
139
149
|
"prebuilt.cjs",
|
|
140
150
|
"prebuilt.js",
|
|
141
151
|
"prebuilt.d.ts",
|
|
142
|
-
"prebuilt.d.cts"
|
|
152
|
+
"prebuilt.d.cts",
|
|
153
|
+
"remote.cjs",
|
|
154
|
+
"remote.js",
|
|
155
|
+
"remote.d.ts",
|
|
156
|
+
"remote.d.cts"
|
|
143
157
|
]
|
|
144
158
|
}
|
package/remote.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/remote.cjs');
|
package/remote.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/remote.js'
|
package/remote.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/remote.js'
|
package/remote.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/remote.js'
|