@langchain/langgraph 0.2.18 → 0.2.20-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/errors.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSubgraphsSeenSet = exports.MultipleSubgraphsError = exports.InvalidUpdateError = exports.EmptyChannelError = exports.EmptyInputError = exports.isGraphInterrupt = exports.NodeInterrupt = exports.GraphInterrupt = exports.GraphValueError = exports.GraphRecursionError = exports.BaseLangGraphError = void 0;
3
+ exports.getSubgraphsSeenSet = exports.RemoteException = exports.MultipleSubgraphsError = exports.InvalidUpdateError = exports.EmptyChannelError = exports.EmptyInputError = exports.isGraphInterrupt = exports.NodeInterrupt = exports.GraphInterrupt = exports.GraphValueError = exports.GraphRecursionError = exports.BaseLangGraphError = void 0;
4
4
  // TODO: Merge with base LangChain error class when we drop support for core@0.2.0
5
5
  class BaseLangGraphError extends Error {
6
6
  constructor(message, fields) {
@@ -120,6 +120,19 @@ class MultipleSubgraphsError extends BaseLangGraphError {
120
120
  }
121
121
  }
122
122
  exports.MultipleSubgraphsError = MultipleSubgraphsError;
123
+ /**
124
+ * Exception raised when an error occurs in the remote graph.
125
+ */
126
+ class RemoteException extends BaseLangGraphError {
127
+ constructor(message, fields) {
128
+ super(message, fields);
129
+ this.name = "RemoteException";
130
+ }
131
+ static get unminifiable_name() {
132
+ return "RemoteException";
133
+ }
134
+ }
135
+ exports.RemoteException = RemoteException;
123
136
  /**
124
137
  * Used for subgraph detection.
125
138
  */
package/dist/errors.d.ts CHANGED
@@ -41,6 +41,13 @@ export declare class MultipleSubgraphsError extends BaseLangGraphError {
41
41
  constructor(message?: string, fields?: BaseLangGraphErrorFields);
42
42
  static get unminifiable_name(): string;
43
43
  }
44
+ /**
45
+ * Exception raised when an error occurs in the remote graph.
46
+ */
47
+ export declare class RemoteException extends BaseLangGraphError {
48
+ constructor(message?: string, fields?: BaseLangGraphErrorFields);
49
+ static get unminifiable_name(): string;
50
+ }
44
51
  /**
45
52
  * Used for subgraph detection.
46
53
  */
package/dist/errors.js CHANGED
@@ -107,6 +107,18 @@ export class MultipleSubgraphsError extends BaseLangGraphError {
107
107
  return "MultipleSubgraphError";
108
108
  }
109
109
  }
110
+ /**
111
+ * Exception raised when an error occurs in the remote graph.
112
+ */
113
+ export class RemoteException extends BaseLangGraphError {
114
+ constructor(message, fields) {
115
+ super(message, fields);
116
+ this.name = "RemoteException";
117
+ }
118
+ static get unminifiable_name() {
119
+ return "RemoteException";
120
+ }
121
+ }
110
122
  /**
111
123
  * Used for subgraph detection.
112
124
  */
@@ -357,6 +357,156 @@ class CompiledGraph extends index_js_1.Pregel {
357
357
  /**
358
358
  * Returns a drawable representation of the computation graph.
359
359
  */
360
+ async getGraphAsync(config) {
361
+ const xray = config?.xray;
362
+ const graph = new graph_1.Graph();
363
+ const startNodes = {
364
+ [exports.START]: graph.addNode({
365
+ schema: zod_1.z.any(),
366
+ }, exports.START),
367
+ };
368
+ const endNodes = {};
369
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
370
+ let subgraphs = {};
371
+ if (xray) {
372
+ subgraphs = Object.fromEntries((await (0, utils_js_1.gatherIterator)(this.getSubgraphsAsync())).filter(
373
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
374
+ (x) => isCompiledGraph(x[1])));
375
+ }
376
+ function addEdge(start, end, label, conditional = false) {
377
+ if (end === exports.END && endNodes[exports.END] === undefined) {
378
+ endNodes[exports.END] = graph.addNode({ schema: zod_1.z.any() }, exports.END);
379
+ }
380
+ return graph.addEdge(startNodes[start], endNodes[end], label !== end ? label : undefined, conditional);
381
+ }
382
+ for (const [key, nodeSpec] of Object.entries(this.builder.nodes)) {
383
+ const displayKey = _escapeMermaidKeywords(key);
384
+ const node = nodeSpec.runnable;
385
+ const metadata = nodeSpec.metadata ?? {};
386
+ if (this.interruptBefore?.includes(key) &&
387
+ this.interruptAfter?.includes(key)) {
388
+ metadata.__interrupt = "before,after";
389
+ }
390
+ else if (this.interruptBefore?.includes(key)) {
391
+ metadata.__interrupt = "before";
392
+ }
393
+ else if (this.interruptAfter?.includes(key)) {
394
+ metadata.__interrupt = "after";
395
+ }
396
+ if (xray) {
397
+ const newXrayValue = typeof xray === "number" ? xray - 1 : xray;
398
+ const drawableSubgraph = subgraphs[key] !== undefined
399
+ ? await subgraphs[key].getGraphAsync({
400
+ ...config,
401
+ xray: newXrayValue,
402
+ })
403
+ : node.getGraph(config);
404
+ drawableSubgraph.trimFirstNode();
405
+ drawableSubgraph.trimLastNode();
406
+ if (Object.keys(drawableSubgraph.nodes).length > 1) {
407
+ const [e, s] = graph.extend(drawableSubgraph, displayKey);
408
+ if (e === undefined) {
409
+ throw new Error(`Could not extend subgraph "${key}" due to missing entrypoint.`);
410
+ }
411
+ // TODO: Remove default name once we stop supporting core 0.2.0
412
+ // eslint-disable-next-line no-inner-declarations
413
+ function _isRunnableInterface(
414
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
415
+ thing) {
416
+ return thing ? thing.lc_runnable : false;
417
+ }
418
+ // eslint-disable-next-line no-inner-declarations
419
+ function _nodeDataStr(id, data) {
420
+ if (id !== undefined && !(0, uuid_1.validate)(id)) {
421
+ return id;
422
+ }
423
+ else if (_isRunnableInterface(data)) {
424
+ try {
425
+ let dataStr = data.getName();
426
+ dataStr = dataStr.startsWith("Runnable")
427
+ ? dataStr.slice("Runnable".length)
428
+ : dataStr;
429
+ return dataStr;
430
+ }
431
+ catch (error) {
432
+ return data.getName();
433
+ }
434
+ }
435
+ else {
436
+ return data.name ?? "UnknownSchema";
437
+ }
438
+ }
439
+ // TODO: Remove casts when we stop supporting core 0.2.0
440
+ if (s !== undefined) {
441
+ startNodes[displayKey] = {
442
+ name: _nodeDataStr(s.id, s.data),
443
+ ...s,
444
+ };
445
+ }
446
+ endNodes[displayKey] = {
447
+ name: _nodeDataStr(e.id, e.data),
448
+ ...e,
449
+ };
450
+ }
451
+ else {
452
+ // TODO: Remove when we stop supporting core 0.2.0
453
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
454
+ // @ts-ignore
455
+ const newNode = graph.addNode(node, displayKey, metadata);
456
+ startNodes[displayKey] = newNode;
457
+ endNodes[displayKey] = newNode;
458
+ }
459
+ }
460
+ else {
461
+ // TODO: Remove when we stop supporting core 0.2.0
462
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
463
+ // @ts-ignore
464
+ const newNode = graph.addNode(node, displayKey, metadata);
465
+ startNodes[displayKey] = newNode;
466
+ endNodes[displayKey] = newNode;
467
+ }
468
+ }
469
+ const sortedEdges = [...this.builder.allEdges].sort(([a], [b]) => {
470
+ if (a < b) {
471
+ return -1;
472
+ }
473
+ else if (b > a) {
474
+ return 1;
475
+ }
476
+ else {
477
+ return 0;
478
+ }
479
+ });
480
+ for (const [start, end] of sortedEdges) {
481
+ addEdge(_escapeMermaidKeywords(start), _escapeMermaidKeywords(end));
482
+ }
483
+ for (const [start, branches] of Object.entries(this.builder.branches)) {
484
+ const defaultEnds = {
485
+ ...Object.fromEntries(Object.keys(this.builder.nodes)
486
+ .filter((k) => k !== start)
487
+ .map((k) => [_escapeMermaidKeywords(k), _escapeMermaidKeywords(k)])),
488
+ [exports.END]: exports.END,
489
+ };
490
+ for (const branch of Object.values(branches)) {
491
+ let ends;
492
+ if (branch.ends !== undefined) {
493
+ ends = branch.ends;
494
+ }
495
+ else {
496
+ ends = defaultEnds;
497
+ }
498
+ for (const [label, end] of Object.entries(ends)) {
499
+ addEdge(_escapeMermaidKeywords(start), _escapeMermaidKeywords(end), label, true);
500
+ }
501
+ }
502
+ }
503
+ return graph;
504
+ }
505
+ /**
506
+ * Returns a drawable representation of the computation graph.
507
+ *
508
+ * @deprecated Use getGraphAsync instead. The async method will be the default in the next minor core release.
509
+ */
360
510
  getGraph(config) {
361
511
  const xray = config?.xray;
362
512
  const graph = new graph_1.Graph();
@@ -76,6 +76,14 @@ export declare class CompiledGraph<N extends string, RunInput = any, RunOutput =
76
76
  /**
77
77
  * Returns a drawable representation of the computation graph.
78
78
  */
79
+ getGraphAsync(config?: RunnableConfig & {
80
+ xray?: boolean | number;
81
+ }): Promise<DrawableGraph>;
82
+ /**
83
+ * Returns a drawable representation of the computation graph.
84
+ *
85
+ * @deprecated Use getGraphAsync instead. The async method will be the default in the next minor core release.
86
+ */
79
87
  getGraph(config?: RunnableConfig & {
80
88
  xray?: boolean | number;
81
89
  }): DrawableGraph;
@@ -8,7 +8,7 @@ import { Channel, Pregel } from "../pregel/index.js";
8
8
  import { EphemeralValue } from "../channels/ephemeral_value.js";
9
9
  import { ChannelWrite, PASSTHROUGH } from "../pregel/write.js";
10
10
  import { _isSend, CHECKPOINT_NAMESPACE_END, CHECKPOINT_NAMESPACE_SEPARATOR, TAG_HIDDEN, } from "../constants.js";
11
- import { gatherIteratorSync, RunnableCallable } from "../utils.js";
11
+ import { gatherIterator, gatherIteratorSync, RunnableCallable, } from "../utils.js";
12
12
  import { InvalidUpdateError, NodeInterrupt } from "../errors.js";
13
13
  import { isPregelLike } from "../pregel/utils/subgraph.js";
14
14
  /** Special reserved node name denoting the start of a graph. */
@@ -352,6 +352,156 @@ export class CompiledGraph extends Pregel {
352
352
  /**
353
353
  * Returns a drawable representation of the computation graph.
354
354
  */
355
+ async getGraphAsync(config) {
356
+ const xray = config?.xray;
357
+ const graph = new DrawableGraph();
358
+ const startNodes = {
359
+ [START]: graph.addNode({
360
+ schema: z.any(),
361
+ }, START),
362
+ };
363
+ const endNodes = {};
364
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
365
+ let subgraphs = {};
366
+ if (xray) {
367
+ subgraphs = Object.fromEntries((await gatherIterator(this.getSubgraphsAsync())).filter(
368
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
369
+ (x) => isCompiledGraph(x[1])));
370
+ }
371
+ function addEdge(start, end, label, conditional = false) {
372
+ if (end === END && endNodes[END] === undefined) {
373
+ endNodes[END] = graph.addNode({ schema: z.any() }, END);
374
+ }
375
+ return graph.addEdge(startNodes[start], endNodes[end], label !== end ? label : undefined, conditional);
376
+ }
377
+ for (const [key, nodeSpec] of Object.entries(this.builder.nodes)) {
378
+ const displayKey = _escapeMermaidKeywords(key);
379
+ const node = nodeSpec.runnable;
380
+ const metadata = nodeSpec.metadata ?? {};
381
+ if (this.interruptBefore?.includes(key) &&
382
+ this.interruptAfter?.includes(key)) {
383
+ metadata.__interrupt = "before,after";
384
+ }
385
+ else if (this.interruptBefore?.includes(key)) {
386
+ metadata.__interrupt = "before";
387
+ }
388
+ else if (this.interruptAfter?.includes(key)) {
389
+ metadata.__interrupt = "after";
390
+ }
391
+ if (xray) {
392
+ const newXrayValue = typeof xray === "number" ? xray - 1 : xray;
393
+ const drawableSubgraph = subgraphs[key] !== undefined
394
+ ? await subgraphs[key].getGraphAsync({
395
+ ...config,
396
+ xray: newXrayValue,
397
+ })
398
+ : node.getGraph(config);
399
+ drawableSubgraph.trimFirstNode();
400
+ drawableSubgraph.trimLastNode();
401
+ if (Object.keys(drawableSubgraph.nodes).length > 1) {
402
+ const [e, s] = graph.extend(drawableSubgraph, displayKey);
403
+ if (e === undefined) {
404
+ throw new Error(`Could not extend subgraph "${key}" due to missing entrypoint.`);
405
+ }
406
+ // TODO: Remove default name once we stop supporting core 0.2.0
407
+ // eslint-disable-next-line no-inner-declarations
408
+ function _isRunnableInterface(
409
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
410
+ thing) {
411
+ return thing ? thing.lc_runnable : false;
412
+ }
413
+ // eslint-disable-next-line no-inner-declarations
414
+ function _nodeDataStr(id, data) {
415
+ if (id !== undefined && !isUuid(id)) {
416
+ return id;
417
+ }
418
+ else if (_isRunnableInterface(data)) {
419
+ try {
420
+ let dataStr = data.getName();
421
+ dataStr = dataStr.startsWith("Runnable")
422
+ ? dataStr.slice("Runnable".length)
423
+ : dataStr;
424
+ return dataStr;
425
+ }
426
+ catch (error) {
427
+ return data.getName();
428
+ }
429
+ }
430
+ else {
431
+ return data.name ?? "UnknownSchema";
432
+ }
433
+ }
434
+ // TODO: Remove casts when we stop supporting core 0.2.0
435
+ if (s !== undefined) {
436
+ startNodes[displayKey] = {
437
+ name: _nodeDataStr(s.id, s.data),
438
+ ...s,
439
+ };
440
+ }
441
+ endNodes[displayKey] = {
442
+ name: _nodeDataStr(e.id, e.data),
443
+ ...e,
444
+ };
445
+ }
446
+ else {
447
+ // TODO: Remove when we stop supporting core 0.2.0
448
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
449
+ // @ts-ignore
450
+ const newNode = graph.addNode(node, displayKey, metadata);
451
+ startNodes[displayKey] = newNode;
452
+ endNodes[displayKey] = newNode;
453
+ }
454
+ }
455
+ else {
456
+ // TODO: Remove when we stop supporting core 0.2.0
457
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
458
+ // @ts-ignore
459
+ const newNode = graph.addNode(node, displayKey, metadata);
460
+ startNodes[displayKey] = newNode;
461
+ endNodes[displayKey] = newNode;
462
+ }
463
+ }
464
+ const sortedEdges = [...this.builder.allEdges].sort(([a], [b]) => {
465
+ if (a < b) {
466
+ return -1;
467
+ }
468
+ else if (b > a) {
469
+ return 1;
470
+ }
471
+ else {
472
+ return 0;
473
+ }
474
+ });
475
+ for (const [start, end] of sortedEdges) {
476
+ addEdge(_escapeMermaidKeywords(start), _escapeMermaidKeywords(end));
477
+ }
478
+ for (const [start, branches] of Object.entries(this.builder.branches)) {
479
+ const defaultEnds = {
480
+ ...Object.fromEntries(Object.keys(this.builder.nodes)
481
+ .filter((k) => k !== start)
482
+ .map((k) => [_escapeMermaidKeywords(k), _escapeMermaidKeywords(k)])),
483
+ [END]: END,
484
+ };
485
+ for (const branch of Object.values(branches)) {
486
+ let ends;
487
+ if (branch.ends !== undefined) {
488
+ ends = branch.ends;
489
+ }
490
+ else {
491
+ ends = defaultEnds;
492
+ }
493
+ for (const [label, end] of Object.entries(ends)) {
494
+ addEdge(_escapeMermaidKeywords(start), _escapeMermaidKeywords(end), label, true);
495
+ }
496
+ }
497
+ }
498
+ return graph;
499
+ }
500
+ /**
501
+ * Returns a drawable representation of the computation graph.
502
+ *
503
+ * @deprecated Use getGraphAsync instead. The async method will be the default in the next minor core release.
504
+ */
355
505
  getGraph(config) {
356
506
  const xray = config?.xray;
357
507
  const graph = new DrawableGraph();