@langchain/langgraph 0.1.1 → 0.1.3
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/README.md +2 -2
- package/dist/constants.cjs +1 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +1 -0
- package/dist/graph/annotation.cjs +78 -4
- package/dist/graph/annotation.d.ts +82 -5
- package/dist/graph/annotation.js +77 -2
- package/dist/graph/graph.cjs +14 -8
- package/dist/graph/graph.d.ts +14 -5
- package/dist/graph/graph.js +14 -8
- package/dist/graph/message.cjs +6 -0
- package/dist/graph/message.d.ts +6 -0
- package/dist/graph/message.js +6 -0
- package/dist/graph/messages_annotation.cjs +5 -0
- package/dist/graph/messages_annotation.d.ts +5 -0
- package/dist/graph/messages_annotation.js +5 -0
- package/dist/graph/state.cjs +84 -3
- package/dist/graph/state.d.ts +75 -5
- package/dist/graph/state.js +86 -5
- package/dist/prebuilt/agent_executor.cjs +1 -0
- package/dist/prebuilt/agent_executor.d.ts +2 -0
- package/dist/prebuilt/agent_executor.js +1 -0
- package/dist/prebuilt/chat_agent_executor.cjs +1 -0
- package/dist/prebuilt/chat_agent_executor.d.ts +2 -0
- package/dist/prebuilt/chat_agent_executor.js +1 -0
- package/dist/prebuilt/react_agent_executor.cjs +1 -1
- package/dist/prebuilt/react_agent_executor.js +2 -2
- package/dist/pregel/algo.cjs +3 -41
- package/dist/pregel/algo.d.ts +0 -5
- package/dist/pregel/algo.js +2 -39
- package/dist/pregel/debug.d.ts +1 -1
- package/dist/pregel/index.cjs +17 -30
- package/dist/pregel/index.d.ts +2 -0
- package/dist/pregel/index.js +18 -31
- package/dist/pregel/loop.cjs +5 -2
- package/dist/pregel/loop.js +5 -2
- package/dist/pregel/read.cjs +19 -1
- package/dist/pregel/read.d.ts +5 -0
- package/dist/pregel/read.js +19 -1
- package/dist/pregel/retry.cjs +147 -0
- package/dist/pregel/retry.d.ts +11 -0
- package/dist/pregel/retry.js +143 -0
- package/dist/pregel/types.d.ts +4 -2
- package/dist/pregel/utils.d.ts +26 -0
- package/dist/web.d.ts +1 -0
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
## Overview
|
|
11
11
|
|
|
12
|
-
[LangGraph.js](https://langchain-ai.github.io/langgraphjs/) is a library for building stateful, multi-actor applications with LLMs, used to create agent and multi-agent workflows.
|
|
12
|
+
[LangGraph.js](https://langchain-ai.github.io/langgraphjs/) is a library for building stateful, multi-actor applications with LLMs, used to create agent and multi-agent workflows. Compared to other LLM frameworks, it offers these core benefits: cycles, controllability, and persistence. LangGraph allows you to define flows that involve cycles, essential for most agentic architectures, differentiating it from DAG-based solutions. As a very low-level framework, it provides fine-grained control over both the flow and state of your application, crucial for creating reliable agents. Additionally, LangGraph includes built-in persistence, enabling advanced human-in-the-loop and memory features.
|
|
13
13
|
|
|
14
14
|
LangGraph is inspired by [Pregel](https://research.google/pubs/pub37252/) and [Apache Beam](https://beam.apache.org/). The public interface draws inspiration from [NetworkX](https://networkx.org/documentation/latest/). LangGraph is built by LangChain Inc, the creators of LangChain, but can be used without LangChain.
|
|
15
15
|
|
|
@@ -19,7 +19,7 @@ LangGraph is inspired by [Pregel](https://research.google/pubs/pub37252/) and [A
|
|
|
19
19
|
- **Persistence**: Automatically save state after each step in the graph. Pause and resume the graph execution at any point to support error recovery, human-in-the-loop workflows, time travel and more.
|
|
20
20
|
- **Human-in-the-Loop**: Interrupt graph execution to approve or edit next action planned by the agent.
|
|
21
21
|
- **Streaming Support**: Stream outputs as they are produced by each node (including token streaming).
|
|
22
|
-
- **Integration with LangChain**: LangGraph integrates seamlessly with [LangChain](https://github.com/langchain-ai/langchainjs/) and [LangSmith](https://docs.smith.langchain.com/) (but does not require them).
|
|
22
|
+
- **Integration with LangChain**: LangGraph integrates seamlessly with [LangChain.js](https://github.com/langchain-ai/langchainjs/) and [LangSmith](https://docs.smith.langchain.com/) (but does not require them).
|
|
23
23
|
|
|
24
24
|
## Installation
|
|
25
25
|
|
package/dist/constants.cjs
CHANGED
package/dist/constants.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ export interface SendInterface {
|
|
|
16
16
|
}
|
|
17
17
|
export declare function _isSendInterface(x: unknown): x is SendInterface;
|
|
18
18
|
/**
|
|
19
|
+
*
|
|
19
20
|
* A message or packet to send to a specific node in the graph.
|
|
20
21
|
*
|
|
21
22
|
* The `Send` class is used within a `StateGraph`'s conditional edges to
|
package/dist/constants.js
CHANGED
|
@@ -24,6 +24,7 @@ export function _isSendInterface(x) {
|
|
|
24
24
|
return typeof operation.node === "string" && operation.args !== undefined;
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
|
+
*
|
|
27
28
|
* A message or packet to send to a specific node in the graph.
|
|
28
29
|
*
|
|
29
30
|
* The `Send` class is used within a `StateGraph`'s conditional edges to
|
|
@@ -3,6 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getChannel = exports.Annotation = exports.AnnotationRoot = void 0;
|
|
4
4
|
const binop_js_1 = require("../channels/binop.cjs");
|
|
5
5
|
const last_value_js_1 = require("../channels/last_value.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Should not be instantiated directly. See {@link Annotation}.
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
6
10
|
class AnnotationRoot {
|
|
7
11
|
constructor(s) {
|
|
8
12
|
Object.defineProperty(this, "lc_graph_name", {
|
|
@@ -21,7 +25,78 @@ class AnnotationRoot {
|
|
|
21
25
|
}
|
|
22
26
|
}
|
|
23
27
|
exports.AnnotationRoot = AnnotationRoot;
|
|
24
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Helper that instantiates channels within a StateGraph state.
|
|
30
|
+
*
|
|
31
|
+
* Can be used as a field in an {@link Annotation.Root} wrapper in one of two ways:
|
|
32
|
+
* 1. **Directly**: Creates a channel that stores the most recent value returned from a node.
|
|
33
|
+
* 2. **With a reducer**: Creates a channel that applies the reducer on a node's return value.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { StateGraph, Annotation } from "@langchain/langgraph";
|
|
38
|
+
*
|
|
39
|
+
* // Define a state with a single string key named "currentOutput"
|
|
40
|
+
* const SimpleAnnotation = Annotation.Root({
|
|
41
|
+
* currentOutput: Annotation<string>,
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* const graphBuilder = new StateGraph(SimpleAnnotation);
|
|
45
|
+
*
|
|
46
|
+
* // A node in the graph that returns an object with a "currentOutput" key
|
|
47
|
+
* // replaces the value in the state. You can get the state type as shown below:
|
|
48
|
+
* const myNode = (state: typeof SimpleAnnotation.State) => {
|
|
49
|
+
* return {
|
|
50
|
+
* currentOutput: "some_new_value",
|
|
51
|
+
* };
|
|
52
|
+
* }
|
|
53
|
+
*
|
|
54
|
+
* const graph = graphBuilder
|
|
55
|
+
* .addNode("myNode", myNode)
|
|
56
|
+
* ...
|
|
57
|
+
* .compile();
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* import { type BaseMessage, AIMessage } from "@langchain/core/messages";
|
|
63
|
+
* import { StateGraph, Annotation } from "@langchain/langgraph";
|
|
64
|
+
*
|
|
65
|
+
* // Define a state with a single key named "messages" that will
|
|
66
|
+
* // combine a returned BaseMessage or arrays of BaseMessages
|
|
67
|
+
* const AnnotationWithReducer = Annotation.Root({
|
|
68
|
+
* messages: Annotation<BaseMessage[]>({
|
|
69
|
+
* // Different types are allowed for updates
|
|
70
|
+
* reducer: (left: BaseMessage[], right: BaseMessage | BaseMessage[]) => {
|
|
71
|
+
* if (Array.isArray(right)) {
|
|
72
|
+
* return left.concat(right);
|
|
73
|
+
* }
|
|
74
|
+
* return left.concat([right]);
|
|
75
|
+
* },
|
|
76
|
+
* default: () => [],
|
|
77
|
+
* }),
|
|
78
|
+
* });
|
|
79
|
+
*
|
|
80
|
+
* const graphBuilder = new StateGraph(AnnotationWithReducer);
|
|
81
|
+
*
|
|
82
|
+
* // A node in the graph that returns an object with a "messages" key
|
|
83
|
+
* // will update the state by combining the existing value with the returned one.
|
|
84
|
+
* const myNode = (state: typeof AnnotationWithReducer.State) => {
|
|
85
|
+
* return {
|
|
86
|
+
* messages: [new AIMessage("Some new response")],
|
|
87
|
+
* };
|
|
88
|
+
* };
|
|
89
|
+
*
|
|
90
|
+
* const graph = graphBuilder
|
|
91
|
+
* .addNode("myNode", myNode)
|
|
92
|
+
* ...
|
|
93
|
+
* .compile();
|
|
94
|
+
* ```
|
|
95
|
+
* @namespace
|
|
96
|
+
* @property Root
|
|
97
|
+
* Helper function that instantiates a StateGraph state. See {@link Annotation} for usage.
|
|
98
|
+
*/
|
|
99
|
+
exports.Annotation = function (annotation) {
|
|
25
100
|
if (annotation) {
|
|
26
101
|
return getChannel(annotation);
|
|
27
102
|
}
|
|
@@ -29,9 +104,8 @@ function Annotation(annotation) {
|
|
|
29
104
|
// @ts-expect-error - Annotation without reducer
|
|
30
105
|
return new last_value_js_1.LastValue();
|
|
31
106
|
}
|
|
32
|
-
}
|
|
33
|
-
exports.Annotation =
|
|
34
|
-
Annotation.Root = (sd) => new AnnotationRoot(sd);
|
|
107
|
+
};
|
|
108
|
+
exports.Annotation.Root = (sd) => new AnnotationRoot(sd);
|
|
35
109
|
function getChannel(reducer) {
|
|
36
110
|
if (typeof reducer === "object" &&
|
|
37
111
|
reducer &&
|
|
@@ -24,6 +24,16 @@ export type UpdateType<SD extends StateDefinition> = {
|
|
|
24
24
|
[key in keyof SD]?: ExtractUpdateType<SD[key]>;
|
|
25
25
|
};
|
|
26
26
|
export type NodeType<SD extends StateDefinition> = RunnableLike<StateType<SD>, UpdateType<SD>>;
|
|
27
|
+
/** @ignore */
|
|
28
|
+
export interface AnnotationFunction {
|
|
29
|
+
<ValueType>(): LastValue<ValueType>;
|
|
30
|
+
<ValueType, UpdateType = ValueType>(annotation: SingleReducer<ValueType, UpdateType>): BinaryOperatorAggregate<ValueType, UpdateType>;
|
|
31
|
+
Root: <S extends StateDefinition>(sd: S) => AnnotationRoot<S>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Should not be instantiated directly. See {@link Annotation}.
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
27
37
|
export declare class AnnotationRoot<SD extends StateDefinition> {
|
|
28
38
|
lc_graph_name: string;
|
|
29
39
|
State: StateType<SD>;
|
|
@@ -32,10 +42,77 @@ export declare class AnnotationRoot<SD extends StateDefinition> {
|
|
|
32
42
|
spec: SD;
|
|
33
43
|
constructor(s: SD);
|
|
34
44
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Helper that instantiates channels within a StateGraph state.
|
|
47
|
+
*
|
|
48
|
+
* Can be used as a field in an {@link Annotation.Root} wrapper in one of two ways:
|
|
49
|
+
* 1. **Directly**: Creates a channel that stores the most recent value returned from a node.
|
|
50
|
+
* 2. **With a reducer**: Creates a channel that applies the reducer on a node's return value.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* import { StateGraph, Annotation } from "@langchain/langgraph";
|
|
55
|
+
*
|
|
56
|
+
* // Define a state with a single string key named "currentOutput"
|
|
57
|
+
* const SimpleAnnotation = Annotation.Root({
|
|
58
|
+
* currentOutput: Annotation<string>,
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* const graphBuilder = new StateGraph(SimpleAnnotation);
|
|
62
|
+
*
|
|
63
|
+
* // A node in the graph that returns an object with a "currentOutput" key
|
|
64
|
+
* // replaces the value in the state. You can get the state type as shown below:
|
|
65
|
+
* const myNode = (state: typeof SimpleAnnotation.State) => {
|
|
66
|
+
* return {
|
|
67
|
+
* currentOutput: "some_new_value",
|
|
68
|
+
* };
|
|
69
|
+
* }
|
|
70
|
+
*
|
|
71
|
+
* const graph = graphBuilder
|
|
72
|
+
* .addNode("myNode", myNode)
|
|
73
|
+
* ...
|
|
74
|
+
* .compile();
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* import { type BaseMessage, AIMessage } from "@langchain/core/messages";
|
|
80
|
+
* import { StateGraph, Annotation } from "@langchain/langgraph";
|
|
81
|
+
*
|
|
82
|
+
* // Define a state with a single key named "messages" that will
|
|
83
|
+
* // combine a returned BaseMessage or arrays of BaseMessages
|
|
84
|
+
* const AnnotationWithReducer = Annotation.Root({
|
|
85
|
+
* messages: Annotation<BaseMessage[]>({
|
|
86
|
+
* // Different types are allowed for updates
|
|
87
|
+
* reducer: (left: BaseMessage[], right: BaseMessage | BaseMessage[]) => {
|
|
88
|
+
* if (Array.isArray(right)) {
|
|
89
|
+
* return left.concat(right);
|
|
90
|
+
* }
|
|
91
|
+
* return left.concat([right]);
|
|
92
|
+
* },
|
|
93
|
+
* default: () => [],
|
|
94
|
+
* }),
|
|
95
|
+
* });
|
|
96
|
+
*
|
|
97
|
+
* const graphBuilder = new StateGraph(AnnotationWithReducer);
|
|
98
|
+
*
|
|
99
|
+
* // A node in the graph that returns an object with a "messages" key
|
|
100
|
+
* // will update the state by combining the existing value with the returned one.
|
|
101
|
+
* const myNode = (state: typeof AnnotationWithReducer.State) => {
|
|
102
|
+
* return {
|
|
103
|
+
* messages: [new AIMessage("Some new response")],
|
|
104
|
+
* };
|
|
105
|
+
* };
|
|
106
|
+
*
|
|
107
|
+
* const graph = graphBuilder
|
|
108
|
+
* .addNode("myNode", myNode)
|
|
109
|
+
* ...
|
|
110
|
+
* .compile();
|
|
111
|
+
* ```
|
|
112
|
+
* @namespace
|
|
113
|
+
* @property Root
|
|
114
|
+
* Helper function that instantiates a StateGraph state. See {@link Annotation} for usage.
|
|
115
|
+
*/
|
|
116
|
+
export declare const Annotation: AnnotationFunction;
|
|
40
117
|
export declare function getChannel<V, U = V>(reducer: SingleReducer<V, U>): BaseChannel<V, U>;
|
|
41
118
|
export {};
|
package/dist/graph/annotation.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { BinaryOperatorAggregate } from "../channels/binop.js";
|
|
2
2
|
import { LastValue } from "../channels/last_value.js";
|
|
3
|
+
/**
|
|
4
|
+
* Should not be instantiated directly. See {@link Annotation}.
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
3
7
|
export class AnnotationRoot {
|
|
4
8
|
constructor(s) {
|
|
5
9
|
Object.defineProperty(this, "lc_graph_name", {
|
|
@@ -17,7 +21,78 @@ export class AnnotationRoot {
|
|
|
17
21
|
this.spec = s;
|
|
18
22
|
}
|
|
19
23
|
}
|
|
20
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Helper that instantiates channels within a StateGraph state.
|
|
26
|
+
*
|
|
27
|
+
* Can be used as a field in an {@link Annotation.Root} wrapper in one of two ways:
|
|
28
|
+
* 1. **Directly**: Creates a channel that stores the most recent value returned from a node.
|
|
29
|
+
* 2. **With a reducer**: Creates a channel that applies the reducer on a node's return value.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { StateGraph, Annotation } from "@langchain/langgraph";
|
|
34
|
+
*
|
|
35
|
+
* // Define a state with a single string key named "currentOutput"
|
|
36
|
+
* const SimpleAnnotation = Annotation.Root({
|
|
37
|
+
* currentOutput: Annotation<string>,
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* const graphBuilder = new StateGraph(SimpleAnnotation);
|
|
41
|
+
*
|
|
42
|
+
* // A node in the graph that returns an object with a "currentOutput" key
|
|
43
|
+
* // replaces the value in the state. You can get the state type as shown below:
|
|
44
|
+
* const myNode = (state: typeof SimpleAnnotation.State) => {
|
|
45
|
+
* return {
|
|
46
|
+
* currentOutput: "some_new_value",
|
|
47
|
+
* };
|
|
48
|
+
* }
|
|
49
|
+
*
|
|
50
|
+
* const graph = graphBuilder
|
|
51
|
+
* .addNode("myNode", myNode)
|
|
52
|
+
* ...
|
|
53
|
+
* .compile();
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* import { type BaseMessage, AIMessage } from "@langchain/core/messages";
|
|
59
|
+
* import { StateGraph, Annotation } from "@langchain/langgraph";
|
|
60
|
+
*
|
|
61
|
+
* // Define a state with a single key named "messages" that will
|
|
62
|
+
* // combine a returned BaseMessage or arrays of BaseMessages
|
|
63
|
+
* const AnnotationWithReducer = Annotation.Root({
|
|
64
|
+
* messages: Annotation<BaseMessage[]>({
|
|
65
|
+
* // Different types are allowed for updates
|
|
66
|
+
* reducer: (left: BaseMessage[], right: BaseMessage | BaseMessage[]) => {
|
|
67
|
+
* if (Array.isArray(right)) {
|
|
68
|
+
* return left.concat(right);
|
|
69
|
+
* }
|
|
70
|
+
* return left.concat([right]);
|
|
71
|
+
* },
|
|
72
|
+
* default: () => [],
|
|
73
|
+
* }),
|
|
74
|
+
* });
|
|
75
|
+
*
|
|
76
|
+
* const graphBuilder = new StateGraph(AnnotationWithReducer);
|
|
77
|
+
*
|
|
78
|
+
* // A node in the graph that returns an object with a "messages" key
|
|
79
|
+
* // will update the state by combining the existing value with the returned one.
|
|
80
|
+
* const myNode = (state: typeof AnnotationWithReducer.State) => {
|
|
81
|
+
* return {
|
|
82
|
+
* messages: [new AIMessage("Some new response")],
|
|
83
|
+
* };
|
|
84
|
+
* };
|
|
85
|
+
*
|
|
86
|
+
* const graph = graphBuilder
|
|
87
|
+
* .addNode("myNode", myNode)
|
|
88
|
+
* ...
|
|
89
|
+
* .compile();
|
|
90
|
+
* ```
|
|
91
|
+
* @namespace
|
|
92
|
+
* @property Root
|
|
93
|
+
* Helper function that instantiates a StateGraph state. See {@link Annotation} for usage.
|
|
94
|
+
*/
|
|
95
|
+
export const Annotation = function (annotation) {
|
|
21
96
|
if (annotation) {
|
|
22
97
|
return getChannel(annotation);
|
|
23
98
|
}
|
|
@@ -25,7 +100,7 @@ export function Annotation(annotation) {
|
|
|
25
100
|
// @ts-expect-error - Annotation without reducer
|
|
26
101
|
return new LastValue();
|
|
27
102
|
}
|
|
28
|
-
}
|
|
103
|
+
};
|
|
29
104
|
Annotation.Root = (sd) => new AnnotationRoot(sd);
|
|
30
105
|
export function getChannel(reducer) {
|
|
31
106
|
if (typeof reducer === "object" &&
|
package/dist/graph/graph.cjs
CHANGED
|
@@ -12,7 +12,9 @@ const write_js_1 = require("../pregel/write.cjs");
|
|
|
12
12
|
const constants_js_1 = require("../constants.cjs");
|
|
13
13
|
const utils_js_1 = require("../utils.cjs");
|
|
14
14
|
const errors_js_1 = require("../errors.cjs");
|
|
15
|
+
/** Special reserved node name denoting the start of a graph. */
|
|
15
16
|
exports.START = "__start__";
|
|
17
|
+
/** Special reserved node name denoting the end of a graph. */
|
|
16
18
|
exports.END = "__end__";
|
|
17
19
|
class Branch {
|
|
18
20
|
constructor(options) {
|
|
@@ -114,7 +116,7 @@ class Graph {
|
|
|
114
116
|
get allEdges() {
|
|
115
117
|
return this.edges;
|
|
116
118
|
}
|
|
117
|
-
addNode(key, action) {
|
|
119
|
+
addNode(key, action, options) {
|
|
118
120
|
if (key.includes(constants_js_1.CHECKPOINT_NAMESPACE_SEPARATOR)) {
|
|
119
121
|
throw new Error(`"${constants_js_1.CHECKPOINT_NAMESPACE_SEPARATOR}" is a reserved character and is not allowed in node names.`);
|
|
120
122
|
}
|
|
@@ -125,9 +127,12 @@ class Graph {
|
|
|
125
127
|
if (key === exports.END) {
|
|
126
128
|
throw new Error(`Node \`${key}\` is reserved.`);
|
|
127
129
|
}
|
|
128
|
-
this.nodes[key] =
|
|
129
|
-
|
|
130
|
-
|
|
130
|
+
this.nodes[key] = {
|
|
131
|
+
runnable: (0, runnables_1._coerceToRunnable)(
|
|
132
|
+
// Account for arbitrary state due to Send API
|
|
133
|
+
action),
|
|
134
|
+
metadata: options?.metadata,
|
|
135
|
+
};
|
|
131
136
|
return this;
|
|
132
137
|
}
|
|
133
138
|
addEdge(startKey, endKey) {
|
|
@@ -287,8 +292,9 @@ class CompiledGraph extends index_js_1.Pregel {
|
|
|
287
292
|
this.nodes[key] = new read_js_1.PregelNode({
|
|
288
293
|
channels: [],
|
|
289
294
|
triggers: [],
|
|
295
|
+
metadata: node.metadata,
|
|
290
296
|
})
|
|
291
|
-
.pipe(node)
|
|
297
|
+
.pipe(node.runnable)
|
|
292
298
|
.pipe(new write_js_1.ChannelWrite([{ channel: key, value: write_js_1.PASSTHROUGH }], [constants_js_1.TAG_HIDDEN]));
|
|
293
299
|
this.streamChannels.push(key);
|
|
294
300
|
}
|
|
@@ -359,7 +365,7 @@ class CompiledGraph extends index_js_1.Pregel {
|
|
|
359
365
|
...config,
|
|
360
366
|
xray: typeof xray === "number" && xray > 0 ? xray - 1 : xray,
|
|
361
367
|
})
|
|
362
|
-
: node.getGraph(config);
|
|
368
|
+
: node.runnable.getGraph(config);
|
|
363
369
|
subgraph.trimFirstNode();
|
|
364
370
|
subgraph.trimLastNode();
|
|
365
371
|
if (Object.keys(subgraph.nodes).length > 1) {
|
|
@@ -372,13 +378,13 @@ class CompiledGraph extends index_js_1.Pregel {
|
|
|
372
378
|
}
|
|
373
379
|
}
|
|
374
380
|
else {
|
|
375
|
-
const newNode = graph.addNode(node, key);
|
|
381
|
+
const newNode = graph.addNode(node.runnable, key);
|
|
376
382
|
startNodes[key] = newNode;
|
|
377
383
|
endNodes[key] = newNode;
|
|
378
384
|
}
|
|
379
385
|
}
|
|
380
386
|
else {
|
|
381
|
-
const newNode = graph.addNode(node, key);
|
|
387
|
+
const newNode = graph.addNode(node.runnable, key);
|
|
382
388
|
startNodes[key] = newNode;
|
|
383
389
|
endNodes[key] = newNode;
|
|
384
390
|
}
|
package/dist/graph/graph.d.ts
CHANGED
|
@@ -8,7 +8,9 @@ import { BaseChannel } from "../channels/base.js";
|
|
|
8
8
|
import { All } from "../pregel/types.js";
|
|
9
9
|
import { Send } from "../constants.js";
|
|
10
10
|
import { RunnableCallable } from "../utils.js";
|
|
11
|
+
/** Special reserved node name denoting the start of a graph. */
|
|
11
12
|
export declare const START = "__start__";
|
|
13
|
+
/** Special reserved node name denoting the end of a graph. */
|
|
12
14
|
export declare const END = "__end__";
|
|
13
15
|
export interface BranchOptions<IO, N extends string> {
|
|
14
16
|
source: N;
|
|
@@ -22,17 +24,24 @@ export declare class Branch<IO, N extends string> {
|
|
|
22
24
|
compile(writer: (dests: (string | Send)[]) => Runnable | undefined, reader?: (config: RunnableConfig) => IO): RunnableCallable<unknown, unknown>;
|
|
23
25
|
_route(input: IO, config: RunnableConfig, writer: (dests: (string | Send)[]) => Runnable | undefined, reader?: (config: RunnableConfig) => IO): Promise<Runnable | undefined>;
|
|
24
26
|
}
|
|
25
|
-
export
|
|
26
|
-
|
|
27
|
+
export type NodeSpec<RunInput, RunOutput> = {
|
|
28
|
+
runnable: Runnable<RunInput, RunOutput>;
|
|
29
|
+
metadata?: Record<string, unknown>;
|
|
30
|
+
};
|
|
31
|
+
export type AddNodeOptions = {
|
|
32
|
+
metadata?: Record<string, unknown>;
|
|
33
|
+
};
|
|
34
|
+
export declare class Graph<N extends string = typeof END, RunInput = any, RunOutput = any, NodeSpecType extends NodeSpec<RunInput, RunOutput> = NodeSpec<RunInput, RunOutput>> {
|
|
35
|
+
nodes: Record<N, NodeSpecType>;
|
|
27
36
|
edges: Set<[N | typeof START, N | typeof END]>;
|
|
28
37
|
branches: Record<string, Record<string, Branch<RunInput, N>>>;
|
|
29
38
|
entryPoint?: string;
|
|
30
39
|
compiled: boolean;
|
|
31
40
|
supportMultipleEdges: boolean;
|
|
32
41
|
constructor();
|
|
33
|
-
|
|
42
|
+
protected warnIfCompiled(message: string): void;
|
|
34
43
|
get allEdges(): Set<[string, string]>;
|
|
35
|
-
addNode<K extends string, NodeInput = RunInput>(key: K, action: RunnableLike<NodeInput, RunOutput
|
|
44
|
+
addNode<K extends string, NodeInput = RunInput>(key: K, action: RunnableLike<NodeInput, RunOutput>, options?: AddNodeOptions): Graph<N | K, RunInput, RunOutput>;
|
|
36
45
|
addEdge(startKey: N | typeof START, endKey: N | typeof END): this;
|
|
37
46
|
addConditionalEdges(source: BranchOptions<RunInput, N>): this;
|
|
38
47
|
addConditionalEdges(source: N, path: Branch<RunInput, N>["condition"], pathMap?: BranchOptions<RunInput, N>["pathMap"]): this;
|
|
@@ -59,7 +68,7 @@ export declare class CompiledGraph<N extends string, RunInput = any, RunOutput =
|
|
|
59
68
|
constructor({ builder, ...rest }: {
|
|
60
69
|
builder: Graph<N, RunInput, RunOutput>;
|
|
61
70
|
} & PregelParams<Record<N | typeof START, PregelNode<RunInput, RunOutput>>, Record<N | typeof START | typeof END | string, BaseChannel>>);
|
|
62
|
-
attachNode(key: N, node:
|
|
71
|
+
attachNode(key: N, node: NodeSpec<RunInput, RunOutput>): void;
|
|
63
72
|
attachEdge(start: N | typeof START, end: N | typeof END): void;
|
|
64
73
|
attachBranch(start: N | typeof START, name: string, branch: Branch<RunInput, N>): void;
|
|
65
74
|
/**
|
package/dist/graph/graph.js
CHANGED
|
@@ -9,7 +9,9 @@ import { ChannelWrite, PASSTHROUGH } from "../pregel/write.js";
|
|
|
9
9
|
import { _isSend, CHECKPOINT_NAMESPACE_SEPARATOR, TAG_HIDDEN, } from "../constants.js";
|
|
10
10
|
import { RunnableCallable } from "../utils.js";
|
|
11
11
|
import { InvalidUpdateError } from "../errors.js";
|
|
12
|
+
/** Special reserved node name denoting the start of a graph. */
|
|
12
13
|
export const START = "__start__";
|
|
14
|
+
/** Special reserved node name denoting the end of a graph. */
|
|
13
15
|
export const END = "__end__";
|
|
14
16
|
export class Branch {
|
|
15
17
|
constructor(options) {
|
|
@@ -110,7 +112,7 @@ export class Graph {
|
|
|
110
112
|
get allEdges() {
|
|
111
113
|
return this.edges;
|
|
112
114
|
}
|
|
113
|
-
addNode(key, action) {
|
|
115
|
+
addNode(key, action, options) {
|
|
114
116
|
if (key.includes(CHECKPOINT_NAMESPACE_SEPARATOR)) {
|
|
115
117
|
throw new Error(`"${CHECKPOINT_NAMESPACE_SEPARATOR}" is a reserved character and is not allowed in node names.`);
|
|
116
118
|
}
|
|
@@ -121,9 +123,12 @@ export class Graph {
|
|
|
121
123
|
if (key === END) {
|
|
122
124
|
throw new Error(`Node \`${key}\` is reserved.`);
|
|
123
125
|
}
|
|
124
|
-
this.nodes[key] =
|
|
125
|
-
|
|
126
|
-
|
|
126
|
+
this.nodes[key] = {
|
|
127
|
+
runnable: _coerceToRunnable(
|
|
128
|
+
// Account for arbitrary state due to Send API
|
|
129
|
+
action),
|
|
130
|
+
metadata: options?.metadata,
|
|
131
|
+
};
|
|
127
132
|
return this;
|
|
128
133
|
}
|
|
129
134
|
addEdge(startKey, endKey) {
|
|
@@ -282,8 +287,9 @@ export class CompiledGraph extends Pregel {
|
|
|
282
287
|
this.nodes[key] = new PregelNode({
|
|
283
288
|
channels: [],
|
|
284
289
|
triggers: [],
|
|
290
|
+
metadata: node.metadata,
|
|
285
291
|
})
|
|
286
|
-
.pipe(node)
|
|
292
|
+
.pipe(node.runnable)
|
|
287
293
|
.pipe(new ChannelWrite([{ channel: key, value: PASSTHROUGH }], [TAG_HIDDEN]));
|
|
288
294
|
this.streamChannels.push(key);
|
|
289
295
|
}
|
|
@@ -354,7 +360,7 @@ export class CompiledGraph extends Pregel {
|
|
|
354
360
|
...config,
|
|
355
361
|
xray: typeof xray === "number" && xray > 0 ? xray - 1 : xray,
|
|
356
362
|
})
|
|
357
|
-
: node.getGraph(config);
|
|
363
|
+
: node.runnable.getGraph(config);
|
|
358
364
|
subgraph.trimFirstNode();
|
|
359
365
|
subgraph.trimLastNode();
|
|
360
366
|
if (Object.keys(subgraph.nodes).length > 1) {
|
|
@@ -367,13 +373,13 @@ export class CompiledGraph extends Pregel {
|
|
|
367
373
|
}
|
|
368
374
|
}
|
|
369
375
|
else {
|
|
370
|
-
const newNode = graph.addNode(node, key);
|
|
376
|
+
const newNode = graph.addNode(node.runnable, key);
|
|
371
377
|
startNodes[key] = newNode;
|
|
372
378
|
endNodes[key] = newNode;
|
|
373
379
|
}
|
|
374
380
|
}
|
|
375
381
|
else {
|
|
376
|
-
const newNode = graph.addNode(node, key);
|
|
382
|
+
const newNode = graph.addNode(node.runnable, key);
|
|
377
383
|
startNodes[key] = newNode;
|
|
378
384
|
endNodes[key] = newNode;
|
|
379
385
|
}
|
package/dist/graph/message.cjs
CHANGED
|
@@ -4,6 +4,11 @@ exports.MessageGraph = exports.messagesStateReducer = void 0;
|
|
|
4
4
|
const messages_1 = require("@langchain/core/messages");
|
|
5
5
|
const uuid_1 = require("uuid");
|
|
6
6
|
const state_js_1 = require("./state.cjs");
|
|
7
|
+
/**
|
|
8
|
+
* Prebuilt reducer that combines returned messages.
|
|
9
|
+
* Can handle standard messages and special modifiers like {@link RemoveMessage}
|
|
10
|
+
* instances.
|
|
11
|
+
*/
|
|
7
12
|
function messagesStateReducer(left, right) {
|
|
8
13
|
const leftArray = Array.isArray(left) ? left : [left];
|
|
9
14
|
const rightArray = Array.isArray(right) ? right : [right];
|
|
@@ -45,6 +50,7 @@ function messagesStateReducer(left, right) {
|
|
|
45
50
|
return merged.filter((m) => !idsToRemove.has(m.id));
|
|
46
51
|
}
|
|
47
52
|
exports.messagesStateReducer = messagesStateReducer;
|
|
53
|
+
/** @ignore */
|
|
48
54
|
class MessageGraph extends state_js_1.StateGraph {
|
|
49
55
|
constructor() {
|
|
50
56
|
super({
|
package/dist/graph/message.d.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { BaseMessage, BaseMessageLike } from "@langchain/core/messages";
|
|
2
2
|
import { StateGraph } from "./state.js";
|
|
3
3
|
type Messages = Array<BaseMessage | BaseMessageLike> | BaseMessage | BaseMessageLike;
|
|
4
|
+
/**
|
|
5
|
+
* Prebuilt reducer that combines returned messages.
|
|
6
|
+
* Can handle standard messages and special modifiers like {@link RemoveMessage}
|
|
7
|
+
* instances.
|
|
8
|
+
*/
|
|
4
9
|
export declare function messagesStateReducer(left: BaseMessage[], right: Messages): BaseMessage[];
|
|
10
|
+
/** @ignore */
|
|
5
11
|
export declare class MessageGraph extends StateGraph<BaseMessage[], BaseMessage[], Messages> {
|
|
6
12
|
constructor();
|
|
7
13
|
}
|
package/dist/graph/message.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { coerceMessageLikeToMessage, } from "@langchain/core/messages";
|
|
2
2
|
import { v4 } from "uuid";
|
|
3
3
|
import { StateGraph } from "./state.js";
|
|
4
|
+
/**
|
|
5
|
+
* Prebuilt reducer that combines returned messages.
|
|
6
|
+
* Can handle standard messages and special modifiers like {@link RemoveMessage}
|
|
7
|
+
* instances.
|
|
8
|
+
*/
|
|
4
9
|
export function messagesStateReducer(left, right) {
|
|
5
10
|
const leftArray = Array.isArray(left) ? left : [left];
|
|
6
11
|
const rightArray = Array.isArray(right) ? right : [right];
|
|
@@ -41,6 +46,7 @@ export function messagesStateReducer(left, right) {
|
|
|
41
46
|
}
|
|
42
47
|
return merged.filter((m) => !idsToRemove.has(m.id));
|
|
43
48
|
}
|
|
49
|
+
/** @ignore */
|
|
44
50
|
export class MessageGraph extends StateGraph {
|
|
45
51
|
constructor() {
|
|
46
52
|
super({
|
|
@@ -4,6 +4,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.MessagesAnnotation = void 0;
|
|
5
5
|
const annotation_js_1 = require("./annotation.cjs");
|
|
6
6
|
const message_js_1 = require("./message.cjs");
|
|
7
|
+
/**
|
|
8
|
+
* Prebuilt state annotation that combines returned messages.
|
|
9
|
+
* Can handle standard messages and special modifiers like {@link RemoveMessage}
|
|
10
|
+
* instances.
|
|
11
|
+
*/
|
|
7
12
|
exports.MessagesAnnotation = annotation_js_1.Annotation.Root({
|
|
8
13
|
messages: (0, annotation_js_1.Annotation)({
|
|
9
14
|
reducer: message_js_1.messagesStateReducer,
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { BaseMessage } from "@langchain/core/messages";
|
|
2
|
+
/**
|
|
3
|
+
* Prebuilt state annotation that combines returned messages.
|
|
4
|
+
* Can handle standard messages and special modifiers like {@link RemoveMessage}
|
|
5
|
+
* instances.
|
|
6
|
+
*/
|
|
2
7
|
export declare const MessagesAnnotation: import("./annotation.js").AnnotationRoot<{
|
|
3
8
|
messages: import("../channels/binop.js").BinaryOperatorAggregate<BaseMessage[], BaseMessage[]>;
|
|
4
9
|
}>;
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */
|
|
2
2
|
import { Annotation } from "./annotation.js";
|
|
3
3
|
import { messagesStateReducer } from "./message.js";
|
|
4
|
+
/**
|
|
5
|
+
* Prebuilt state annotation that combines returned messages.
|
|
6
|
+
* Can handle standard messages and special modifiers like {@link RemoveMessage}
|
|
7
|
+
* instances.
|
|
8
|
+
*/
|
|
4
9
|
export const MessagesAnnotation = Annotation.Root({
|
|
5
10
|
messages: Annotation({
|
|
6
11
|
reducer: messagesStateReducer,
|