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