@langchain/langgraph 0.0.26 → 0.0.27-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/channels/binop.d.ts +10 -10
- package/dist/graph/graph.cjs +75 -0
- package/dist/graph/graph.d.ts +4 -1
- package/dist/graph/graph.js +76 -1
- package/dist/graph/state.d.ts +7 -7
- package/dist/prebuilt/agent_executor.d.ts +1 -1
- package/dist/tests/diagrams.test.d.ts +1 -0
- package/dist/tests/diagrams.test.js +26 -0
- package/dist/tests/graph.test.js +18 -0
- package/dist/utils.cjs +36 -1
- package/dist/utils.d.ts +4 -0
- package/dist/utils.js +34 -0
- package/package.json +4 -4
package/dist/channels/binop.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { BaseChannel } from "./index.js";
|
|
2
|
-
export type BinaryOperator<
|
|
2
|
+
export type BinaryOperator<ValueType, UpdateType> = (a: ValueType, b: UpdateType) => ValueType;
|
|
3
3
|
/**
|
|
4
4
|
* Stores the result of applying a binary operator to the current value and each new value.
|
|
5
5
|
*/
|
|
6
|
-
export declare class BinaryOperatorAggregate<
|
|
6
|
+
export declare class BinaryOperatorAggregate<ValueType, UpdateType = ValueType> extends BaseChannel<ValueType, UpdateType, ValueType> {
|
|
7
7
|
lc_graph_name: string;
|
|
8
|
-
value:
|
|
9
|
-
operator: BinaryOperator<
|
|
10
|
-
initialValueFactory?: () =>
|
|
11
|
-
constructor(operator: BinaryOperator<
|
|
12
|
-
fromCheckpoint(checkpoint?:
|
|
13
|
-
update(values:
|
|
14
|
-
get():
|
|
15
|
-
checkpoint():
|
|
8
|
+
value: ValueType | undefined;
|
|
9
|
+
operator: BinaryOperator<ValueType, UpdateType>;
|
|
10
|
+
initialValueFactory?: () => ValueType;
|
|
11
|
+
constructor(operator: BinaryOperator<ValueType, UpdateType>, initialValueFactory?: () => ValueType);
|
|
12
|
+
fromCheckpoint(checkpoint?: ValueType): this;
|
|
13
|
+
update(values: UpdateType[]): void;
|
|
14
|
+
get(): ValueType;
|
|
15
|
+
checkpoint(): ValueType;
|
|
16
16
|
}
|
package/dist/graph/graph.cjs
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.CompiledGraph = exports.Graph = exports.Branch = exports.END = exports.START = void 0;
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
5
5
|
const runnables_1 = require("@langchain/core/runnables");
|
|
6
|
+
const zod_1 = require("zod");
|
|
6
7
|
const read_js_1 = require("../pregel/read.cjs");
|
|
7
8
|
const index_js_1 = require("../pregel/index.cjs");
|
|
8
9
|
const ephemeral_value_js_1 = require("../channels/ephemeral_value.cjs");
|
|
@@ -316,5 +317,79 @@ class CompiledGraph extends index_js_1.Pregel {
|
|
|
316
317
|
}
|
|
317
318
|
}
|
|
318
319
|
}
|
|
320
|
+
// Returns a drawable representation of the computation graph.
|
|
321
|
+
getGraph(config) {
|
|
322
|
+
const xray = config?.xray;
|
|
323
|
+
const graph = new utils_js_1.DrawableGraph();
|
|
324
|
+
const startNodes = {
|
|
325
|
+
[exports.START]: graph.addNode({
|
|
326
|
+
schema: zod_1.z.any(),
|
|
327
|
+
}, exports.START),
|
|
328
|
+
};
|
|
329
|
+
const endNodes = {
|
|
330
|
+
[exports.END]: graph.addNode({
|
|
331
|
+
schema: zod_1.z.any(),
|
|
332
|
+
}, exports.END),
|
|
333
|
+
};
|
|
334
|
+
for (const [key, node] of Object.entries(this.builder.nodes)) {
|
|
335
|
+
if (!!config?.xray) {
|
|
336
|
+
const subgraph = isCompiledGraph(node)
|
|
337
|
+
? node.getGraph({
|
|
338
|
+
...config,
|
|
339
|
+
xray: typeof xray === "number" && xray > 0 ? xray - 1 : xray,
|
|
340
|
+
})
|
|
341
|
+
: node.getGraph(config);
|
|
342
|
+
subgraph.trimFirstNode();
|
|
343
|
+
subgraph.trimLastNode();
|
|
344
|
+
if (Object.keys(subgraph.nodes).length > 1) {
|
|
345
|
+
const [newEndNode, newStartNode] = graph.extend(subgraph, key);
|
|
346
|
+
if (newEndNode !== undefined) {
|
|
347
|
+
endNodes[key] = newEndNode;
|
|
348
|
+
}
|
|
349
|
+
if (newStartNode !== undefined) {
|
|
350
|
+
startNodes[key] = newStartNode;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
const newNode = graph.addNode(node, key);
|
|
355
|
+
startNodes[key] = newNode;
|
|
356
|
+
endNodes[key] = newNode;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
const newNode = graph.addNode(node, key);
|
|
361
|
+
startNodes[key] = newNode;
|
|
362
|
+
endNodes[key] = newNode;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
for (const [start, end] of this.builder.allEdges) {
|
|
366
|
+
graph.addEdge(startNodes[start], endNodes[end]);
|
|
367
|
+
}
|
|
368
|
+
for (const [start, branches] of Object.entries(this.builder.branches)) {
|
|
369
|
+
const defaultEnds = {
|
|
370
|
+
...Object.fromEntries(Object.keys(this.builder.nodes)
|
|
371
|
+
.filter((k) => k !== start)
|
|
372
|
+
.map((k) => [k, k])),
|
|
373
|
+
END: exports.END,
|
|
374
|
+
};
|
|
375
|
+
for (const branch of Object.values(branches)) {
|
|
376
|
+
let ends;
|
|
377
|
+
if (branch.ends !== undefined) {
|
|
378
|
+
ends = branch.ends;
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
ends = defaultEnds;
|
|
382
|
+
}
|
|
383
|
+
for (const [label, end] of Object.entries(ends)) {
|
|
384
|
+
graph.addEdge(startNodes[start], endNodes[end], label !== end ? label : undefined, true);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return graph;
|
|
389
|
+
}
|
|
319
390
|
}
|
|
320
391
|
exports.CompiledGraph = CompiledGraph;
|
|
392
|
+
function isCompiledGraph(x) {
|
|
393
|
+
return (typeof x.attachNode === "function" &&
|
|
394
|
+
typeof x.attachEdge === "function");
|
|
395
|
+
}
|
package/dist/graph/graph.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { Pregel, PregelInterface } from "../pregel/index.js";
|
|
|
4
4
|
import { BaseCheckpointSaver } from "../checkpoint/base.js";
|
|
5
5
|
import { BaseChannel } from "../channels/base.js";
|
|
6
6
|
import { All } from "../pregel/types.js";
|
|
7
|
-
import { RunnableCallable } from "../utils.js";
|
|
7
|
+
import { DrawableGraph, RunnableCallable } from "../utils.js";
|
|
8
8
|
export declare const START = "__start__";
|
|
9
9
|
export declare const END = "__end__";
|
|
10
10
|
export interface BranchOptions<IO, N extends string> {
|
|
@@ -56,4 +56,7 @@ export declare class CompiledGraph<N extends string, RunInput = any, RunOutput =
|
|
|
56
56
|
attachNode(key: N, node: Runnable<RunInput, RunOutput>): void;
|
|
57
57
|
attachEdge(start: N | typeof START, end: N | typeof END): void;
|
|
58
58
|
attachBranch(start: N | typeof START, name: string, branch: Branch<RunInput, N>): void;
|
|
59
|
+
getGraph(config?: RunnableConfig & {
|
|
60
|
+
xray?: boolean | number;
|
|
61
|
+
}): DrawableGraph;
|
|
59
62
|
}
|
package/dist/graph/graph.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
2
2
|
import { _coerceToRunnable, } from "@langchain/core/runnables";
|
|
3
|
+
import { z } from "zod";
|
|
3
4
|
import { PregelNode } from "../pregel/read.js";
|
|
4
5
|
import { Channel, Pregel } from "../pregel/index.js";
|
|
5
6
|
import { EphemeralValue } from "../channels/ephemeral_value.js";
|
|
6
7
|
import { ChannelWrite, PASSTHROUGH } from "../pregel/write.js";
|
|
7
8
|
import { TAG_HIDDEN } from "../constants.js";
|
|
8
|
-
import { RunnableCallable } from "../utils.js";
|
|
9
|
+
import { DrawableGraph, RunnableCallable } from "../utils.js";
|
|
9
10
|
export const START = "__start__";
|
|
10
11
|
export const END = "__end__";
|
|
11
12
|
export class Branch {
|
|
@@ -311,4 +312,78 @@ export class CompiledGraph extends Pregel {
|
|
|
311
312
|
}
|
|
312
313
|
}
|
|
313
314
|
}
|
|
315
|
+
// Returns a drawable representation of the computation graph.
|
|
316
|
+
getGraph(config) {
|
|
317
|
+
const xray = config?.xray;
|
|
318
|
+
const graph = new DrawableGraph();
|
|
319
|
+
const startNodes = {
|
|
320
|
+
[START]: graph.addNode({
|
|
321
|
+
schema: z.any(),
|
|
322
|
+
}, START),
|
|
323
|
+
};
|
|
324
|
+
const endNodes = {
|
|
325
|
+
[END]: graph.addNode({
|
|
326
|
+
schema: z.any(),
|
|
327
|
+
}, END),
|
|
328
|
+
};
|
|
329
|
+
for (const [key, node] of Object.entries(this.builder.nodes)) {
|
|
330
|
+
if (!!config?.xray) {
|
|
331
|
+
const subgraph = isCompiledGraph(node)
|
|
332
|
+
? node.getGraph({
|
|
333
|
+
...config,
|
|
334
|
+
xray: typeof xray === "number" && xray > 0 ? xray - 1 : xray,
|
|
335
|
+
})
|
|
336
|
+
: node.getGraph(config);
|
|
337
|
+
subgraph.trimFirstNode();
|
|
338
|
+
subgraph.trimLastNode();
|
|
339
|
+
if (Object.keys(subgraph.nodes).length > 1) {
|
|
340
|
+
const [newEndNode, newStartNode] = graph.extend(subgraph, key);
|
|
341
|
+
if (newEndNode !== undefined) {
|
|
342
|
+
endNodes[key] = newEndNode;
|
|
343
|
+
}
|
|
344
|
+
if (newStartNode !== undefined) {
|
|
345
|
+
startNodes[key] = newStartNode;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
const newNode = graph.addNode(node, key);
|
|
350
|
+
startNodes[key] = newNode;
|
|
351
|
+
endNodes[key] = newNode;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
const newNode = graph.addNode(node, key);
|
|
356
|
+
startNodes[key] = newNode;
|
|
357
|
+
endNodes[key] = newNode;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
for (const [start, end] of this.builder.allEdges) {
|
|
361
|
+
graph.addEdge(startNodes[start], endNodes[end]);
|
|
362
|
+
}
|
|
363
|
+
for (const [start, branches] of Object.entries(this.builder.branches)) {
|
|
364
|
+
const defaultEnds = {
|
|
365
|
+
...Object.fromEntries(Object.keys(this.builder.nodes)
|
|
366
|
+
.filter((k) => k !== start)
|
|
367
|
+
.map((k) => [k, k])),
|
|
368
|
+
END: END,
|
|
369
|
+
};
|
|
370
|
+
for (const branch of Object.values(branches)) {
|
|
371
|
+
let ends;
|
|
372
|
+
if (branch.ends !== undefined) {
|
|
373
|
+
ends = branch.ends;
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
ends = defaultEnds;
|
|
377
|
+
}
|
|
378
|
+
for (const [label, end] of Object.entries(ends)) {
|
|
379
|
+
graph.addEdge(startNodes[start], endNodes[end], label !== end ? label : undefined, true);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return graph;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
function isCompiledGraph(x) {
|
|
387
|
+
return (typeof x.attachNode === "function" &&
|
|
388
|
+
typeof x.attachEdge === "function");
|
|
314
389
|
}
|
package/dist/graph/state.d.ts
CHANGED
|
@@ -4,18 +4,18 @@ import { BinaryOperator } from "../channels/binop.js";
|
|
|
4
4
|
import { END, CompiledGraph, Graph, START, Branch } from "./graph.js";
|
|
5
5
|
import { BaseCheckpointSaver } from "../checkpoint/base.js";
|
|
6
6
|
import { All } from "../pregel/types.js";
|
|
7
|
-
type SingleReducer<
|
|
8
|
-
reducer: BinaryOperator<
|
|
9
|
-
default?: () =>
|
|
7
|
+
type SingleReducer<ValueType, UpdateType = ValueType> = {
|
|
8
|
+
reducer: BinaryOperator<ValueType, UpdateType>;
|
|
9
|
+
default?: () => ValueType;
|
|
10
10
|
} | {
|
|
11
11
|
/**
|
|
12
12
|
* @deprecated Use `reducer` instead
|
|
13
13
|
*/
|
|
14
|
-
value: BinaryOperator<
|
|
15
|
-
default?: () =>
|
|
14
|
+
value: BinaryOperator<ValueType, UpdateType>;
|
|
15
|
+
default?: () => ValueType;
|
|
16
16
|
} | null;
|
|
17
17
|
export type ChannelReducers<Channels extends object> = {
|
|
18
|
-
[K in keyof Channels]: SingleReducer<Channels[K]>;
|
|
18
|
+
[K in keyof Channels]: SingleReducer<Channels[K], any>;
|
|
19
19
|
};
|
|
20
20
|
export interface StateGraphArgs<Channels extends object | unknown> {
|
|
21
21
|
channels: Channels extends object ? Channels extends unknown[] ? ChannelReducers<{
|
|
@@ -24,7 +24,7 @@ export interface StateGraphArgs<Channels extends object | unknown> {
|
|
|
24
24
|
__root__: Channels;
|
|
25
25
|
}>;
|
|
26
26
|
}
|
|
27
|
-
export declare class StateGraph<State extends object | unknown, Update extends object | unknown = Partial<State
|
|
27
|
+
export declare class StateGraph<State extends object | unknown, Update extends object | unknown = Partial<Record<keyof State, any>>, N extends string = typeof START> extends Graph<N, State, Update> {
|
|
28
28
|
channels: Record<string, BaseChannel>;
|
|
29
29
|
waitingEdges: Set<[N[], N]>;
|
|
30
30
|
constructor(fields: StateGraphArgs<State>);
|
|
@@ -16,5 +16,5 @@ export interface AgentExecutorState {
|
|
|
16
16
|
export declare function createAgentExecutor({ agentRunnable, tools, }: {
|
|
17
17
|
agentRunnable: Runnable;
|
|
18
18
|
tools: Array<Tool> | ToolExecutor;
|
|
19
|
-
}): import("../graph/state.js").CompiledStateGraph<AgentExecutorState, Partial<AgentExecutorState
|
|
19
|
+
}): import("../graph/state.js").CompiledStateGraph<AgentExecutorState, Partial<Record<keyof AgentExecutorState, any>>, "__start__" | "agent" | "action">;
|
|
20
20
|
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { test, expect } from "@jest/globals";
|
|
2
|
+
import { TavilySearchResults } from "@langchain/community/tools/tavily_search";
|
|
3
|
+
import { ChatOpenAI } from "@langchain/openai";
|
|
4
|
+
import { createReactAgent } from "../prebuilt/index.js";
|
|
5
|
+
test("langgraph", async () => {
|
|
6
|
+
// Define the tools for the agent to use
|
|
7
|
+
const tools = [new TavilySearchResults({ maxResults: 1 })];
|
|
8
|
+
const model = new ChatOpenAI({ temperature: 0 });
|
|
9
|
+
const app = createReactAgent({ llm: model, tools });
|
|
10
|
+
const graph = app.getGraph();
|
|
11
|
+
const mermaid = graph.drawMermaid();
|
|
12
|
+
expect(mermaid).toEqual(`%%{init: {'flowchart': {'curve': 'linear'}}}%%
|
|
13
|
+
graph TD;
|
|
14
|
+
\t__start__[__start__]:::startclass;
|
|
15
|
+
\t__end__[__end__]:::endclass;
|
|
16
|
+
\tagent([agent]):::otherclass;
|
|
17
|
+
\ttools([tools]):::otherclass;
|
|
18
|
+
\t__start__ --> agent;
|
|
19
|
+
\ttools --> agent;
|
|
20
|
+
\tagent -. continue .-> tools;
|
|
21
|
+
\tagent -.-> __end__;
|
|
22
|
+
\tclassDef startclass fill:#ffdfba;
|
|
23
|
+
\tclassDef endclass fill:#baffc9;
|
|
24
|
+
\tclassDef otherclass fill:#fad7de;
|
|
25
|
+
`);
|
|
26
|
+
});
|
package/dist/tests/graph.test.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { describe, it, expect } from "@jest/globals";
|
|
2
2
|
import { StateGraph } from "../graph/state.js";
|
|
3
|
+
import { END, START } from "../web.js";
|
|
3
4
|
describe("State", () => {
|
|
4
5
|
it("should validate a new node key correctly ", () => {
|
|
5
6
|
const stateGraph = new StateGraph({
|
|
@@ -12,4 +13,21 @@ describe("State", () => {
|
|
|
12
13
|
stateGraph.addNode("newNodeKey", (_) => ({}));
|
|
13
14
|
}).not.toThrow();
|
|
14
15
|
});
|
|
16
|
+
it("should allow reducers with different argument types", async () => {
|
|
17
|
+
const stateGraph = new StateGraph({
|
|
18
|
+
channels: {
|
|
19
|
+
testval: {
|
|
20
|
+
reducer: (left, right) => right ? left.concat([right.toString()]) : left,
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
const graph = stateGraph
|
|
25
|
+
.addNode("testnode", (_) => ({ testval: "hi!" }))
|
|
26
|
+
.addEdge(START, "testnode")
|
|
27
|
+
.addEdge("testnode", END)
|
|
28
|
+
.compile();
|
|
29
|
+
expect(await graph.invoke({ testval: ["hello"] })).toEqual({
|
|
30
|
+
testval: ["hello", "hi!"],
|
|
31
|
+
});
|
|
32
|
+
});
|
|
15
33
|
});
|
package/dist/utils.cjs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RunnableCallable = void 0;
|
|
3
|
+
exports.DrawableGraph = exports.RunnableCallable = void 0;
|
|
4
4
|
const runnables_1 = require("@langchain/core/runnables");
|
|
5
5
|
const singletons_1 = require("@langchain/core/singletons");
|
|
6
|
+
const graph_1 = require("@langchain/core/runnables/graph");
|
|
7
|
+
const uuid_1 = require("uuid");
|
|
6
8
|
class RunnableCallable extends runnables_1.Runnable {
|
|
7
9
|
constructor(fields) {
|
|
8
10
|
super();
|
|
@@ -85,3 +87,36 @@ class RunnableCallable extends runnables_1.Runnable {
|
|
|
85
87
|
}
|
|
86
88
|
}
|
|
87
89
|
exports.RunnableCallable = RunnableCallable;
|
|
90
|
+
class DrawableGraph extends graph_1.Graph {
|
|
91
|
+
extend(graph, prefix = "") {
|
|
92
|
+
const nodeIds = Object.values(graph.nodes).map((node) => node.id);
|
|
93
|
+
if (nodeIds.every(uuid_1.validate)) {
|
|
94
|
+
super.extend(graph);
|
|
95
|
+
return [graph.firstNode(), graph.lastNode()];
|
|
96
|
+
}
|
|
97
|
+
const newNodes = Object.entries(graph.nodes).reduce((nodes, [key, value]) => {
|
|
98
|
+
nodes[`${prefix}:${key}`] = {
|
|
99
|
+
id: `${prefix}:${key}`,
|
|
100
|
+
data: value.data,
|
|
101
|
+
};
|
|
102
|
+
return nodes;
|
|
103
|
+
}, {});
|
|
104
|
+
const newEdges = graph.edges.map((edge) => {
|
|
105
|
+
return {
|
|
106
|
+
source: `${prefix}:${edge.source}`,
|
|
107
|
+
target: `${prefix}:${edge.target}`,
|
|
108
|
+
data: edge.data,
|
|
109
|
+
conditional: edge.conditional,
|
|
110
|
+
};
|
|
111
|
+
});
|
|
112
|
+
this.nodes = { ...this.nodes, ...newNodes };
|
|
113
|
+
this.edges = this.edges.concat(newEdges);
|
|
114
|
+
const first = graph.firstNode();
|
|
115
|
+
const last = graph.lastNode();
|
|
116
|
+
return [
|
|
117
|
+
first ? { id: `${prefix}:${first.id}`, data: first.data } : undefined,
|
|
118
|
+
last ? { id: `${prefix}:${last.id}`, data: last.data } : undefined,
|
|
119
|
+
];
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
exports.DrawableGraph = DrawableGraph;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CallbackManagerForChainRun } from "@langchain/core/callbacks/manager";
|
|
2
2
|
import { Runnable, RunnableConfig } from "@langchain/core/runnables";
|
|
3
|
+
import { Graph, type Node as RunnableGraphNode } from "@langchain/core/runnables/graph";
|
|
3
4
|
export interface RunnableCallableArgs extends Partial<any> {
|
|
4
5
|
name?: string;
|
|
5
6
|
func: (...args: any[]) => any;
|
|
@@ -18,3 +19,6 @@ export declare class RunnableCallable<I = unknown, O = unknown> extends Runnable
|
|
|
18
19
|
protected _tracedInvoke(input: I, config?: Partial<RunnableConfig>, runManager?: CallbackManagerForChainRun): Promise<O>;
|
|
19
20
|
invoke(input: any, options?: Partial<RunnableConfig> | undefined): Promise<any>;
|
|
20
21
|
}
|
|
22
|
+
export declare class DrawableGraph extends Graph {
|
|
23
|
+
extend(graph: Graph, prefix?: string): (RunnableGraphNode | undefined)[];
|
|
24
|
+
}
|
package/dist/utils.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { mergeConfigs, patchConfig, Runnable, } from "@langchain/core/runnables";
|
|
2
2
|
import { AsyncLocalStorageProviderSingleton } from "@langchain/core/singletons";
|
|
3
|
+
import { Graph, } from "@langchain/core/runnables/graph";
|
|
4
|
+
import { validate as isUuid } from "uuid";
|
|
3
5
|
export class RunnableCallable extends Runnable {
|
|
4
6
|
constructor(fields) {
|
|
5
7
|
super();
|
|
@@ -81,3 +83,35 @@ export class RunnableCallable extends Runnable {
|
|
|
81
83
|
return returnValue;
|
|
82
84
|
}
|
|
83
85
|
}
|
|
86
|
+
export class DrawableGraph extends Graph {
|
|
87
|
+
extend(graph, prefix = "") {
|
|
88
|
+
const nodeIds = Object.values(graph.nodes).map((node) => node.id);
|
|
89
|
+
if (nodeIds.every(isUuid)) {
|
|
90
|
+
super.extend(graph);
|
|
91
|
+
return [graph.firstNode(), graph.lastNode()];
|
|
92
|
+
}
|
|
93
|
+
const newNodes = Object.entries(graph.nodes).reduce((nodes, [key, value]) => {
|
|
94
|
+
nodes[`${prefix}:${key}`] = {
|
|
95
|
+
id: `${prefix}:${key}`,
|
|
96
|
+
data: value.data,
|
|
97
|
+
};
|
|
98
|
+
return nodes;
|
|
99
|
+
}, {});
|
|
100
|
+
const newEdges = graph.edges.map((edge) => {
|
|
101
|
+
return {
|
|
102
|
+
source: `${prefix}:${edge.source}`,
|
|
103
|
+
target: `${prefix}:${edge.target}`,
|
|
104
|
+
data: edge.data,
|
|
105
|
+
conditional: edge.conditional,
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
this.nodes = { ...this.nodes, ...newNodes };
|
|
109
|
+
this.edges = this.edges.concat(newEdges);
|
|
110
|
+
const first = graph.firstNode();
|
|
111
|
+
const last = graph.lastNode();
|
|
112
|
+
return [
|
|
113
|
+
first ? { id: `${prefix}:${first.id}`, data: first.data } : undefined,
|
|
114
|
+
last ? { id: `${prefix}:${last.id}`, data: last.data } : undefined,
|
|
115
|
+
];
|
|
116
|
+
}
|
|
117
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/langgraph",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.27-rc.0",
|
|
4
4
|
"description": "LangGraph",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -37,8 +37,9 @@
|
|
|
37
37
|
"author": "LangChain",
|
|
38
38
|
"license": "MIT",
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@langchain/core": ">0.
|
|
41
|
-
"uuid": "^10.0.0"
|
|
40
|
+
"@langchain/core": ">0.2.14 <0.3.0",
|
|
41
|
+
"uuid": "^10.0.0",
|
|
42
|
+
"zod": "^3.23.8"
|
|
42
43
|
},
|
|
43
44
|
"devDependencies": {
|
|
44
45
|
"@jest/globals": "^29.5.0",
|
|
@@ -73,7 +74,6 @@
|
|
|
73
74
|
"ts-jest": "^29.1.0",
|
|
74
75
|
"tsx": "^4.7.0",
|
|
75
76
|
"typescript": "^4.9.5 || ^5.4.5",
|
|
76
|
-
"zod": "^3.22.4",
|
|
77
77
|
"zod-to-json-schema": "^3.22.4"
|
|
78
78
|
},
|
|
79
79
|
"peerDependencies": {
|