@langchain/langgraph 0.0.1

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.
Files changed (84) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +588 -0
  3. package/dist/channels/base.cjs +58 -0
  4. package/dist/channels/base.d.ts +46 -0
  5. package/dist/channels/base.js +50 -0
  6. package/dist/channels/binop.cjs +70 -0
  7. package/dist/channels/binop.d.ts +16 -0
  8. package/dist/channels/binop.js +66 -0
  9. package/dist/channels/index.cjs +9 -0
  10. package/dist/channels/index.d.ts +1 -0
  11. package/dist/channels/index.js +1 -0
  12. package/dist/channels/last_value.cjs +53 -0
  13. package/dist/channels/last_value.d.ts +12 -0
  14. package/dist/channels/last_value.js +49 -0
  15. package/dist/channels/topic.cjs +90 -0
  16. package/dist/channels/topic.d.ts +19 -0
  17. package/dist/channels/topic.js +86 -0
  18. package/dist/checkpoint/base.cjs +32 -0
  19. package/dist/checkpoint/base.d.ts +47 -0
  20. package/dist/checkpoint/base.js +27 -0
  21. package/dist/checkpoint/index.cjs +8 -0
  22. package/dist/checkpoint/index.d.ts +2 -0
  23. package/dist/checkpoint/index.js +2 -0
  24. package/dist/checkpoint/memory.cjs +35 -0
  25. package/dist/checkpoint/memory.d.ts +8 -0
  26. package/dist/checkpoint/memory.js +31 -0
  27. package/dist/constants.cjs +5 -0
  28. package/dist/constants.d.ts +2 -0
  29. package/dist/constants.js +2 -0
  30. package/dist/graph/graph.cjs +175 -0
  31. package/dist/graph/graph.d.ts +30 -0
  32. package/dist/graph/graph.js +171 -0
  33. package/dist/graph/index.cjs +9 -0
  34. package/dist/graph/index.d.ts +2 -0
  35. package/dist/graph/index.js +2 -0
  36. package/dist/graph/state.cjs +108 -0
  37. package/dist/graph/state.d.ts +17 -0
  38. package/dist/graph/state.js +104 -0
  39. package/dist/index.cjs +8 -0
  40. package/dist/index.d.ts +1 -0
  41. package/dist/index.js +1 -0
  42. package/dist/prebuilt/agent_executor.cjs +96 -0
  43. package/dist/prebuilt/agent_executor.d.ts +12 -0
  44. package/dist/prebuilt/agent_executor.js +92 -0
  45. package/dist/prebuilt/chat_agent_executor.cjs +130 -0
  46. package/dist/prebuilt/chat_agent_executor.d.ts +6 -0
  47. package/dist/prebuilt/chat_agent_executor.js +126 -0
  48. package/dist/prebuilt/index.cjs +9 -0
  49. package/dist/prebuilt/index.d.ts +3 -0
  50. package/dist/prebuilt/index.js +3 -0
  51. package/dist/prebuilt/tool_executor.cjs +63 -0
  52. package/dist/prebuilt/tool_executor.d.ts +27 -0
  53. package/dist/prebuilt/tool_executor.js +59 -0
  54. package/dist/pregel/debug.cjs +46 -0
  55. package/dist/pregel/debug.d.ts +4 -0
  56. package/dist/pregel/debug.js +41 -0
  57. package/dist/pregel/index.cjs +475 -0
  58. package/dist/pregel/index.d.ts +75 -0
  59. package/dist/pregel/index.js +469 -0
  60. package/dist/pregel/io.cjs +57 -0
  61. package/dist/pregel/io.d.ts +9 -0
  62. package/dist/pregel/io.js +52 -0
  63. package/dist/pregel/read.cjs +217 -0
  64. package/dist/pregel/read.d.ts +43 -0
  65. package/dist/pregel/read.js +211 -0
  66. package/dist/pregel/reserved.cjs +7 -0
  67. package/dist/pregel/reserved.d.ts +3 -0
  68. package/dist/pregel/reserved.js +4 -0
  69. package/dist/pregel/validate.cjs +90 -0
  70. package/dist/pregel/validate.d.ts +15 -0
  71. package/dist/pregel/validate.js +85 -0
  72. package/dist/pregel/write.cjs +54 -0
  73. package/dist/pregel/write.d.ts +13 -0
  74. package/dist/pregel/write.js +50 -0
  75. package/index.cjs +1 -0
  76. package/index.d.ts +1 -0
  77. package/index.js +1 -0
  78. package/package.json +100 -0
  79. package/prebuilt.cjs +1 -0
  80. package/prebuilt.d.ts +1 -0
  81. package/prebuilt.js +1 -0
  82. package/pregel.cjs +1 -0
  83. package/pregel.d.ts +1 -0
  84. package/pregel.js +1 -0
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseCheckpointSaver = exports.emptyCheckpoint = exports.MemorySaver = void 0;
4
+ var memory_js_1 = require("./memory.cjs");
5
+ Object.defineProperty(exports, "MemorySaver", { enumerable: true, get: function () { return memory_js_1.MemorySaver; } });
6
+ var base_js_1 = require("./base.cjs");
7
+ Object.defineProperty(exports, "emptyCheckpoint", { enumerable: true, get: function () { return base_js_1.emptyCheckpoint; } });
8
+ Object.defineProperty(exports, "BaseCheckpointSaver", { enumerable: true, get: function () { return base_js_1.BaseCheckpointSaver; } });
@@ -0,0 +1,2 @@
1
+ export { MemorySaver } from "./memory.js";
2
+ export { type ConfigurableFieldSpec, type Checkpoint, type CheckpointAt, emptyCheckpoint, BaseCheckpointSaver, } from "./base.js";
@@ -0,0 +1,2 @@
1
+ export { MemorySaver } from "./memory.js";
2
+ export { emptyCheckpoint, BaseCheckpointSaver, } from "./base.js";
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MemorySaver = void 0;
4
+ const base_js_1 = require("./base.cjs");
5
+ class MemorySaver extends base_js_1.BaseCheckpointSaver {
6
+ constructor() {
7
+ super(...arguments);
8
+ Object.defineProperty(this, "storage", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: {}
13
+ });
14
+ }
15
+ get configSpecs() {
16
+ return [
17
+ {
18
+ id: "threadId",
19
+ name: "Thread ID",
20
+ annotation: null,
21
+ description: null,
22
+ default: null,
23
+ isShared: true,
24
+ dependencies: null,
25
+ },
26
+ ];
27
+ }
28
+ get(config) {
29
+ return this.storage[config.configurable?.threadId];
30
+ }
31
+ put(config, checkpoint) {
32
+ this.storage[config.configurable?.threadId] = checkpoint;
33
+ }
34
+ }
35
+ exports.MemorySaver = MemorySaver;
@@ -0,0 +1,8 @@
1
+ import { RunnableConfig } from "@langchain/core/runnables";
2
+ import { BaseCheckpointSaver, Checkpoint, ConfigurableFieldSpec } from "./base.js";
3
+ export declare class MemorySaver extends BaseCheckpointSaver {
4
+ storage: Record<string, Checkpoint>;
5
+ get configSpecs(): ConfigurableFieldSpec[];
6
+ get(config: RunnableConfig): Checkpoint | undefined;
7
+ put(config: RunnableConfig, checkpoint: Checkpoint): void;
8
+ }
@@ -0,0 +1,31 @@
1
+ import { BaseCheckpointSaver, } from "./base.js";
2
+ export class MemorySaver extends BaseCheckpointSaver {
3
+ constructor() {
4
+ super(...arguments);
5
+ Object.defineProperty(this, "storage", {
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true,
9
+ value: {}
10
+ });
11
+ }
12
+ get configSpecs() {
13
+ return [
14
+ {
15
+ id: "threadId",
16
+ name: "Thread ID",
17
+ annotation: null,
18
+ description: null,
19
+ default: null,
20
+ isShared: true,
21
+ dependencies: null,
22
+ },
23
+ ];
24
+ }
25
+ get(config) {
26
+ return this.storage[config.configurable?.threadId];
27
+ }
28
+ put(config, checkpoint) {
29
+ this.storage[config.configurable?.threadId] = checkpoint;
30
+ }
31
+ }
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CONFIG_KEY_READ = exports.CONFIG_KEY_SEND = void 0;
4
+ exports.CONFIG_KEY_SEND = "__pregel_send";
5
+ exports.CONFIG_KEY_READ = "__pregel_read";
@@ -0,0 +1,2 @@
1
+ export declare const CONFIG_KEY_SEND = "__pregel_send";
2
+ export declare const CONFIG_KEY_READ = "__pregel_read";
@@ -0,0 +1,2 @@
1
+ export const CONFIG_KEY_SEND = "__pregel_send";
2
+ export const CONFIG_KEY_READ = "__pregel_read";
@@ -0,0 +1,175 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Graph = exports.END = void 0;
4
+ const runnables_1 = require("@langchain/core/runnables");
5
+ const index_js_1 = require("../pregel/index.cjs");
6
+ exports.END = "__end__";
7
+ class Branch {
8
+ constructor(condition, ends) {
9
+ Object.defineProperty(this, "condition", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: void 0
14
+ });
15
+ Object.defineProperty(this, "ends", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
21
+ this.condition = condition;
22
+ this.ends = ends;
23
+ }
24
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
+ runnable(input, options) {
26
+ const result = this.condition(input, options?.config);
27
+ const destination = this.ends[result];
28
+ return index_js_1.Channel.writeTo(destination !== exports.END ? `${destination}:inbox` : exports.END);
29
+ }
30
+ }
31
+ class Graph {
32
+ constructor() {
33
+ Object.defineProperty(this, "nodes", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ Object.defineProperty(this, "edges", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ Object.defineProperty(this, "branches", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ Object.defineProperty(this, "entryPoint", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ this.nodes = {};
58
+ this.edges = new Set();
59
+ this.branches = {};
60
+ }
61
+ addNode(key, action) {
62
+ if (this.nodes[key]) {
63
+ throw new Error(`Node \`${key}\` already present.`);
64
+ }
65
+ if (key === exports.END) {
66
+ throw new Error(`Node \`${key}\` is reserved.`);
67
+ }
68
+ this.nodes[key] = (0, runnables_1._coerceToRunnable)(action);
69
+ }
70
+ addEdge(startKey, endKey) {
71
+ if (startKey === exports.END) {
72
+ throw new Error("END cannot be a start node");
73
+ }
74
+ if (!this.nodes[startKey]) {
75
+ throw new Error(`Need to addNode \`${startKey}\` first`);
76
+ }
77
+ if (!this.nodes[endKey] && endKey !== exports.END) {
78
+ throw new Error(`Need to addNode \`${endKey}\` first`);
79
+ }
80
+ // TODO: support multiple message passing
81
+ if (Array.from(this.edges).some(([start]) => start === startKey)) {
82
+ throw new Error(`Already found path for ${startKey}`);
83
+ }
84
+ this.edges.add([startKey, endKey]);
85
+ }
86
+ addConditionalEdges(startKey, condition, conditionalEdgeMapping) {
87
+ if (!this.nodes[startKey]) {
88
+ throw new Error(`Need to addNode \`${startKey}\` first`);
89
+ }
90
+ if (condition.constructor.name === "AsyncFunction") {
91
+ throw new Error("Condition cannot be an async function");
92
+ }
93
+ for (const destination of Object.values(conditionalEdgeMapping)) {
94
+ if (!this.nodes[destination] && destination !== exports.END) {
95
+ throw new Error(`Need to addNode \`${destination}\` first`);
96
+ }
97
+ }
98
+ if (!this.branches[startKey]) {
99
+ this.branches[startKey] = [];
100
+ }
101
+ this.branches[startKey].push(new Branch(condition, conditionalEdgeMapping));
102
+ }
103
+ setEntryPoint(key) {
104
+ if (!this.nodes[key]) {
105
+ throw new Error(`Need to addNode \`${key}\` first`);
106
+ }
107
+ this.entryPoint = key;
108
+ }
109
+ setFinishPoint(key) {
110
+ this.addEdge(key, exports.END);
111
+ }
112
+ compile(checkpointer) {
113
+ this.validate();
114
+ const outgoingEdges = {};
115
+ this.edges.forEach(([start, end]) => {
116
+ if (!outgoingEdges[start]) {
117
+ outgoingEdges[start] = [];
118
+ }
119
+ outgoingEdges[start].push(end !== exports.END ? `${end}:inbox` : exports.END);
120
+ });
121
+ const nodes = {};
122
+ for (const [key, node] of Object.entries(this.nodes)) {
123
+ nodes[key] = index_js_1.Channel.subscribeTo(`${key}:inbox`)
124
+ .pipe(node)
125
+ .pipe(index_js_1.Channel.writeTo(key));
126
+ }
127
+ for (const key of Object.keys(this.nodes)) {
128
+ const outgoing = outgoingEdges[key];
129
+ const edgesKey = `${key}:edges`;
130
+ if (outgoing || this.branches[key]) {
131
+ nodes[edgesKey] = index_js_1.Channel.subscribeTo(key, {
132
+ tags: ["langsmith:hidden"],
133
+ });
134
+ }
135
+ if (outgoing) {
136
+ nodes[edgesKey] = nodes[edgesKey].pipe(index_js_1.Channel.writeTo(...outgoing));
137
+ }
138
+ if (this.branches[key]) {
139
+ this.branches[key].forEach((branch) => {
140
+ const runnableLambda = new runnables_1.RunnableLambda({
141
+ func: (input) => branch.runnable(input),
142
+ });
143
+ nodes[edgesKey] = nodes[edgesKey].pipe(runnableLambda);
144
+ });
145
+ }
146
+ }
147
+ const hidden = Object.keys(this.nodes).map((node) => `${node}:inbox`);
148
+ if (!this.entryPoint) {
149
+ throw new Error("Entry point not set");
150
+ }
151
+ return new index_js_1.Pregel({
152
+ nodes,
153
+ input: `${this.entryPoint}:inbox`,
154
+ output: exports.END,
155
+ hidden,
156
+ checkpointer,
157
+ });
158
+ }
159
+ validate() {
160
+ const allStarts = new Set([...this.edges].map(([src, _]) => src).concat(Object.keys(this.branches)));
161
+ const allEnds = new Set([...this.edges]
162
+ .map(([_, end]) => end)
163
+ .concat(...Object.values(this.branches).flatMap((branchList) => branchList.flatMap((branch) => Object.values(branch.ends))))
164
+ .concat(this.entryPoint ? [this.entryPoint] : []));
165
+ for (const node of Object.keys(this.nodes)) {
166
+ if (!allEnds.has(node)) {
167
+ throw new Error(`Node \`${node}\` is not reachable`);
168
+ }
169
+ if (!allStarts.has(node)) {
170
+ throw new Error(`Node \`${node}\` is a dead-end`);
171
+ }
172
+ }
173
+ }
174
+ }
175
+ exports.Graph = Graph;
@@ -0,0 +1,30 @@
1
+ import { Runnable, RunnableConfig, RunnableLike } from "@langchain/core/runnables";
2
+ import { Pregel } from "../pregel/index.js";
3
+ import { BaseCheckpointSaver } from "../checkpoint/base.js";
4
+ export declare const END = "__end__";
5
+ type EndsMap = {
6
+ [result: string]: string;
7
+ };
8
+ declare class Branch {
9
+ condition: CallableFunction;
10
+ ends: EndsMap;
11
+ constructor(condition: CallableFunction, ends: EndsMap);
12
+ runnable(input: any, options?: {
13
+ config?: RunnableConfig;
14
+ }): Runnable;
15
+ }
16
+ export declare class Graph<RunInput = any, RunOutput = any> {
17
+ nodes: Record<string, Runnable<RunInput, RunOutput>>;
18
+ edges: Set<[string, string]>;
19
+ branches: Record<string, Branch[]>;
20
+ entryPoint?: string;
21
+ constructor();
22
+ addNode(key: string, action: RunnableLike<RunInput, RunOutput>): void;
23
+ addEdge(startKey: string, endKey: string): void;
24
+ addConditionalEdges(startKey: string, condition: CallableFunction, conditionalEdgeMapping: Record<string, string>): void;
25
+ setEntryPoint(key: string): void;
26
+ setFinishPoint(key: string): void;
27
+ compile(checkpointer?: BaseCheckpointSaver): Pregel;
28
+ validate(): void;
29
+ }
30
+ export {};
@@ -0,0 +1,171 @@
1
+ import { RunnableLambda, _coerceToRunnable, } from "@langchain/core/runnables";
2
+ import { Channel, Pregel } from "../pregel/index.js";
3
+ export const END = "__end__";
4
+ class Branch {
5
+ constructor(condition, ends) {
6
+ Object.defineProperty(this, "condition", {
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ value: void 0
11
+ });
12
+ Object.defineProperty(this, "ends", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: void 0
17
+ });
18
+ this.condition = condition;
19
+ this.ends = ends;
20
+ }
21
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
+ runnable(input, options) {
23
+ const result = this.condition(input, options?.config);
24
+ const destination = this.ends[result];
25
+ return Channel.writeTo(destination !== END ? `${destination}:inbox` : END);
26
+ }
27
+ }
28
+ export class Graph {
29
+ constructor() {
30
+ Object.defineProperty(this, "nodes", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ Object.defineProperty(this, "edges", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
42
+ Object.defineProperty(this, "branches", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: void 0
47
+ });
48
+ Object.defineProperty(this, "entryPoint", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: void 0
53
+ });
54
+ this.nodes = {};
55
+ this.edges = new Set();
56
+ this.branches = {};
57
+ }
58
+ addNode(key, action) {
59
+ if (this.nodes[key]) {
60
+ throw new Error(`Node \`${key}\` already present.`);
61
+ }
62
+ if (key === END) {
63
+ throw new Error(`Node \`${key}\` is reserved.`);
64
+ }
65
+ this.nodes[key] = _coerceToRunnable(action);
66
+ }
67
+ addEdge(startKey, endKey) {
68
+ if (startKey === END) {
69
+ throw new Error("END cannot be a start node");
70
+ }
71
+ if (!this.nodes[startKey]) {
72
+ throw new Error(`Need to addNode \`${startKey}\` first`);
73
+ }
74
+ if (!this.nodes[endKey] && endKey !== END) {
75
+ throw new Error(`Need to addNode \`${endKey}\` first`);
76
+ }
77
+ // TODO: support multiple message passing
78
+ if (Array.from(this.edges).some(([start]) => start === startKey)) {
79
+ throw new Error(`Already found path for ${startKey}`);
80
+ }
81
+ this.edges.add([startKey, endKey]);
82
+ }
83
+ addConditionalEdges(startKey, condition, conditionalEdgeMapping) {
84
+ if (!this.nodes[startKey]) {
85
+ throw new Error(`Need to addNode \`${startKey}\` first`);
86
+ }
87
+ if (condition.constructor.name === "AsyncFunction") {
88
+ throw new Error("Condition cannot be an async function");
89
+ }
90
+ for (const destination of Object.values(conditionalEdgeMapping)) {
91
+ if (!this.nodes[destination] && destination !== END) {
92
+ throw new Error(`Need to addNode \`${destination}\` first`);
93
+ }
94
+ }
95
+ if (!this.branches[startKey]) {
96
+ this.branches[startKey] = [];
97
+ }
98
+ this.branches[startKey].push(new Branch(condition, conditionalEdgeMapping));
99
+ }
100
+ setEntryPoint(key) {
101
+ if (!this.nodes[key]) {
102
+ throw new Error(`Need to addNode \`${key}\` first`);
103
+ }
104
+ this.entryPoint = key;
105
+ }
106
+ setFinishPoint(key) {
107
+ this.addEdge(key, END);
108
+ }
109
+ compile(checkpointer) {
110
+ this.validate();
111
+ const outgoingEdges = {};
112
+ this.edges.forEach(([start, end]) => {
113
+ if (!outgoingEdges[start]) {
114
+ outgoingEdges[start] = [];
115
+ }
116
+ outgoingEdges[start].push(end !== END ? `${end}:inbox` : END);
117
+ });
118
+ const nodes = {};
119
+ for (const [key, node] of Object.entries(this.nodes)) {
120
+ nodes[key] = Channel.subscribeTo(`${key}:inbox`)
121
+ .pipe(node)
122
+ .pipe(Channel.writeTo(key));
123
+ }
124
+ for (const key of Object.keys(this.nodes)) {
125
+ const outgoing = outgoingEdges[key];
126
+ const edgesKey = `${key}:edges`;
127
+ if (outgoing || this.branches[key]) {
128
+ nodes[edgesKey] = Channel.subscribeTo(key, {
129
+ tags: ["langsmith:hidden"],
130
+ });
131
+ }
132
+ if (outgoing) {
133
+ nodes[edgesKey] = nodes[edgesKey].pipe(Channel.writeTo(...outgoing));
134
+ }
135
+ if (this.branches[key]) {
136
+ this.branches[key].forEach((branch) => {
137
+ const runnableLambda = new RunnableLambda({
138
+ func: (input) => branch.runnable(input),
139
+ });
140
+ nodes[edgesKey] = nodes[edgesKey].pipe(runnableLambda);
141
+ });
142
+ }
143
+ }
144
+ const hidden = Object.keys(this.nodes).map((node) => `${node}:inbox`);
145
+ if (!this.entryPoint) {
146
+ throw new Error("Entry point not set");
147
+ }
148
+ return new Pregel({
149
+ nodes,
150
+ input: `${this.entryPoint}:inbox`,
151
+ output: END,
152
+ hidden,
153
+ checkpointer,
154
+ });
155
+ }
156
+ validate() {
157
+ const allStarts = new Set([...this.edges].map(([src, _]) => src).concat(Object.keys(this.branches)));
158
+ const allEnds = new Set([...this.edges]
159
+ .map(([_, end]) => end)
160
+ .concat(...Object.values(this.branches).flatMap((branchList) => branchList.flatMap((branch) => Object.values(branch.ends))))
161
+ .concat(this.entryPoint ? [this.entryPoint] : []));
162
+ for (const node of Object.keys(this.nodes)) {
163
+ if (!allEnds.has(node)) {
164
+ throw new Error(`Node \`${node}\` is not reachable`);
165
+ }
166
+ if (!allStarts.has(node)) {
167
+ throw new Error(`Node \`${node}\` is a dead-end`);
168
+ }
169
+ }
170
+ }
171
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StateGraph = exports.START = exports.Graph = exports.END = void 0;
4
+ var graph_js_1 = require("./graph.cjs");
5
+ Object.defineProperty(exports, "END", { enumerable: true, get: function () { return graph_js_1.END; } });
6
+ Object.defineProperty(exports, "Graph", { enumerable: true, get: function () { return graph_js_1.Graph; } });
7
+ var state_js_1 = require("./state.cjs");
8
+ Object.defineProperty(exports, "START", { enumerable: true, get: function () { return state_js_1.START; } });
9
+ Object.defineProperty(exports, "StateGraph", { enumerable: true, get: function () { return state_js_1.StateGraph; } });
@@ -0,0 +1,2 @@
1
+ export { END, Graph } from "./graph.js";
2
+ export { type StateGraphArgs, START, StateGraph } from "./state.js";
@@ -0,0 +1,2 @@
1
+ export { END, Graph } from "./graph.js";
2
+ export { START, StateGraph } from "./state.js";
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StateGraph = exports.START = void 0;
4
+ const runnables_1 = require("@langchain/core/runnables");
5
+ const binop_js_1 = require("../channels/binop.cjs");
6
+ const graph_js_1 = require("./graph.cjs");
7
+ const last_value_js_1 = require("../channels/last_value.cjs");
8
+ const write_js_1 = require("../pregel/write.cjs");
9
+ const index_js_1 = require("../pregel/index.cjs");
10
+ const read_js_1 = require("../pregel/read.cjs");
11
+ exports.START = "__start__";
12
+ class StateGraph extends graph_js_1.Graph {
13
+ constructor(fields) {
14
+ super();
15
+ Object.defineProperty(this, "channels", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
21
+ this.channels = _getChannels(fields.channels);
22
+ }
23
+ compile(checkpointer) {
24
+ this.validate();
25
+ if (Object.keys(this.nodes).some((key) => key in this.channels)) {
26
+ throw new Error("Cannot use channel names as node names");
27
+ }
28
+ const stateKeys = Object.keys(this.channels);
29
+ const outgoingEdges = {};
30
+ for (const [start, end] of this.edges) {
31
+ if (!outgoingEdges[start]) {
32
+ outgoingEdges[start] = [];
33
+ }
34
+ outgoingEdges[start].push(end !== graph_js_1.END ? `${end}:inbox` : graph_js_1.END);
35
+ }
36
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
+ const nodes = {};
38
+ for (const [key, node] of Object.entries(this.nodes)) {
39
+ nodes[key] = index_js_1.Channel.subscribeTo(`${key}:inbox`)
40
+ .pipe(node)
41
+ .pipe(_updateState)
42
+ .pipe(index_js_1.Channel.writeTo(key));
43
+ }
44
+ for (const key of Object.keys(this.nodes)) {
45
+ const outgoing = outgoingEdges[key];
46
+ const edgesKey = `${key}:edges`;
47
+ if (outgoing || this.branches[key]) {
48
+ nodes[edgesKey] = index_js_1.Channel.subscribeTo(key, {
49
+ tags: ["langsmith:hidden"],
50
+ }).pipe(new read_js_1.ChannelRead(stateKeys));
51
+ }
52
+ if (outgoing) {
53
+ nodes[edgesKey] = nodes[edgesKey].pipe(index_js_1.Channel.writeTo(...outgoing));
54
+ }
55
+ if (this.branches[key]) {
56
+ for (const branch of this.branches[key]) {
57
+ nodes[edgesKey] = nodes[edgesKey].pipe(new runnables_1.RunnableLambda({
58
+ func: (i, c) => branch.runnable(i, c),
59
+ }));
60
+ }
61
+ }
62
+ }
63
+ nodes[exports.START] = index_js_1.Channel.subscribeTo(`${exports.START}:inbox`, {
64
+ tags: ["langsmith:hidden"],
65
+ })
66
+ .pipe(_updateState)
67
+ .pipe(index_js_1.Channel.writeTo(exports.START));
68
+ nodes[`${exports.START}:edges`] = index_js_1.Channel.subscribeTo(exports.START, {
69
+ tags: ["langsmith:hidden"],
70
+ })
71
+ .pipe(new read_js_1.ChannelRead(stateKeys))
72
+ .pipe(index_js_1.Channel.writeTo(`${this.entryPoint}:inbox`));
73
+ return new index_js_1.Pregel({
74
+ nodes,
75
+ channels: this.channels,
76
+ input: `${exports.START}:inbox`,
77
+ output: graph_js_1.END,
78
+ hidden: Object.keys(this.nodes)
79
+ .map((node) => `${node}:inbox`)
80
+ .concat(exports.START, stateKeys),
81
+ checkpointer,
82
+ });
83
+ }
84
+ }
85
+ exports.StateGraph = StateGraph;
86
+ function _updateState(
87
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
88
+ input, options
89
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
90
+ ) {
91
+ if (!options?.config) {
92
+ throw new Error("Config not found when updating state.");
93
+ }
94
+ write_js_1.ChannelWrite.doWrite(options.config, input);
95
+ return input;
96
+ }
97
+ function _getChannels(schema) {
98
+ const channels = {};
99
+ for (const [name, values] of Object.entries(schema)) {
100
+ if (values.value) {
101
+ channels[name] = new binop_js_1.BinaryOperatorAggregate(values.value, values.default);
102
+ }
103
+ else {
104
+ channels[name] = new last_value_js_1.LastValue();
105
+ }
106
+ }
107
+ return channels;
108
+ }
@@ -0,0 +1,17 @@
1
+ import { BaseChannel } from "../channels/base.js";
2
+ import { BinaryOperator } from "../channels/binop.js";
3
+ import { Graph } from "./graph.js";
4
+ import { BaseCheckpointSaver } from "../checkpoint/base.js";
5
+ import { Pregel } from "../pregel/index.js";
6
+ export declare const START = "__start__";
7
+ export interface StateGraphArgs<T = any> {
8
+ channels: Record<string, {
9
+ value: BinaryOperator<T> | null;
10
+ default?: () => T;
11
+ }>;
12
+ }
13
+ export declare class StateGraph<T> extends Graph {
14
+ channels: Record<string, BaseChannel>;
15
+ constructor(fields: StateGraphArgs<T>);
16
+ compile(checkpointer?: BaseCheckpointSaver): Pregel;
17
+ }