@langchain/langgraph 0.2.40 → 0.2.42
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 +237 -154
- package/dist/channels/any_value.cjs +10 -10
- package/dist/channels/any_value.d.ts +1 -1
- package/dist/channels/any_value.js +10 -10
- package/dist/channels/ephemeral_value.cjs +10 -9
- package/dist/channels/ephemeral_value.d.ts +1 -1
- package/dist/channels/ephemeral_value.js +10 -9
- package/dist/channels/last_value.cjs +8 -7
- package/dist/channels/last_value.d.ts +1 -1
- package/dist/channels/last_value.js +8 -7
- package/dist/constants.cjs +33 -6
- package/dist/constants.d.ts +17 -2
- package/dist/constants.js +32 -5
- package/dist/errors.d.ts +3 -3
- package/dist/func/index.cjs +272 -0
- package/dist/func/index.d.ts +310 -0
- package/dist/func/index.js +267 -0
- package/dist/func/types.cjs +15 -0
- package/dist/func/types.d.ts +59 -0
- package/dist/func/types.js +11 -0
- package/dist/graph/graph.cjs +31 -35
- package/dist/graph/graph.d.ts +1 -5
- package/dist/graph/graph.js +1 -5
- package/dist/graph/index.cjs +1 -3
- package/dist/graph/index.d.ts +1 -1
- package/dist/graph/index.js +1 -1
- package/dist/graph/message.d.ts +1 -1
- package/dist/graph/state.cjs +17 -17
- package/dist/graph/state.d.ts +2 -1
- package/dist/graph/state.js +2 -2
- package/dist/index.cjs +8 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interrupt.cjs +21 -34
- package/dist/interrupt.d.ts +1 -1
- package/dist/interrupt.js +22 -35
- package/dist/prebuilt/agent_executor.cjs +3 -3
- package/dist/prebuilt/agent_executor.d.ts +1 -1
- package/dist/prebuilt/agent_executor.js +1 -1
- package/dist/prebuilt/chat_agent_executor.cjs +3 -3
- package/dist/prebuilt/chat_agent_executor.d.ts +1 -1
- package/dist/prebuilt/chat_agent_executor.js +1 -1
- package/dist/prebuilt/react_agent_executor.cjs +79 -12
- package/dist/prebuilt/react_agent_executor.d.ts +35 -4
- package/dist/prebuilt/react_agent_executor.js +79 -13
- package/dist/prebuilt/tool_node.cjs +1 -2
- package/dist/prebuilt/tool_node.d.ts +1 -1
- package/dist/prebuilt/tool_node.js +1 -2
- package/dist/pregel/algo.cjs +121 -12
- package/dist/pregel/algo.d.ts +8 -6
- package/dist/pregel/algo.js +122 -13
- package/dist/pregel/call.cjs +77 -0
- package/dist/pregel/call.d.ts +15 -0
- package/dist/pregel/call.js +71 -0
- package/dist/pregel/index.cjs +59 -96
- package/dist/pregel/index.d.ts +1 -10
- package/dist/pregel/index.js +61 -98
- package/dist/pregel/io.cjs +6 -1
- package/dist/pregel/io.js +7 -2
- package/dist/pregel/loop.cjs +109 -75
- package/dist/pregel/loop.d.ts +17 -23
- package/dist/pregel/loop.js +110 -75
- package/dist/pregel/messages.d.ts +1 -1
- package/dist/pregel/retry.cjs +22 -50
- package/dist/pregel/retry.d.ts +6 -6
- package/dist/pregel/retry.js +22 -50
- package/dist/pregel/runner.cjs +275 -0
- package/dist/pregel/runner.d.ts +64 -0
- package/dist/pregel/runner.js +271 -0
- package/dist/pregel/stream.cjs +71 -0
- package/dist/pregel/stream.d.ts +17 -0
- package/dist/pregel/stream.js +67 -0
- package/dist/pregel/types.cjs +54 -0
- package/dist/pregel/types.d.ts +78 -6
- package/dist/pregel/types.js +51 -1
- package/dist/pregel/utils/config.cjs +26 -1
- package/dist/pregel/utils/config.d.ts +14 -0
- package/dist/pregel/utils/config.js +22 -0
- package/dist/pregel/write.d.ts +1 -1
- package/dist/utils.cjs +15 -1
- package/dist/utils.d.ts +3 -1
- package/dist/utils.js +12 -0
- package/dist/web.cjs +7 -5
- package/dist/web.d.ts +4 -4
- package/dist/web.js +3 -3
- package/package.json +8 -8
|
@@ -19,45 +19,46 @@ export class EphemeralValue extends BaseChannel {
|
|
|
19
19
|
writable: true,
|
|
20
20
|
value: void 0
|
|
21
21
|
});
|
|
22
|
+
// value is an array so we don't misinterpret an update to undefined as no write
|
|
22
23
|
Object.defineProperty(this, "value", {
|
|
23
24
|
enumerable: true,
|
|
24
25
|
configurable: true,
|
|
25
26
|
writable: true,
|
|
26
|
-
value:
|
|
27
|
+
value: []
|
|
27
28
|
});
|
|
28
29
|
this.guard = guard;
|
|
29
30
|
}
|
|
30
31
|
fromCheckpoint(checkpoint) {
|
|
31
32
|
const empty = new EphemeralValue(this.guard);
|
|
32
33
|
if (checkpoint) {
|
|
33
|
-
empty.value = checkpoint;
|
|
34
|
+
empty.value = [checkpoint];
|
|
34
35
|
}
|
|
35
36
|
return empty;
|
|
36
37
|
}
|
|
37
38
|
update(values) {
|
|
38
39
|
if (values.length === 0) {
|
|
39
|
-
const updated = this.value
|
|
40
|
+
const updated = this.value.length > 0;
|
|
40
41
|
// If there are no updates for this specific channel at the end of the step, wipe it.
|
|
41
|
-
this.value =
|
|
42
|
+
this.value = [];
|
|
42
43
|
return updated;
|
|
43
44
|
}
|
|
44
45
|
if (values.length !== 1 && this.guard) {
|
|
45
46
|
throw new InvalidUpdateError("EphemeralValue can only receive one value per step.");
|
|
46
47
|
}
|
|
47
48
|
// eslint-disable-next-line prefer-destructuring
|
|
48
|
-
this.value = values[values.length - 1];
|
|
49
|
+
this.value = [values[values.length - 1]];
|
|
49
50
|
return true;
|
|
50
51
|
}
|
|
51
52
|
get() {
|
|
52
|
-
if (this.value ===
|
|
53
|
+
if (this.value.length === 0) {
|
|
53
54
|
throw new EmptyChannelError();
|
|
54
55
|
}
|
|
55
|
-
return this.value;
|
|
56
|
+
return this.value[0];
|
|
56
57
|
}
|
|
57
58
|
checkpoint() {
|
|
58
|
-
if (this.value ===
|
|
59
|
+
if (this.value.length === 0) {
|
|
59
60
|
throw new EmptyChannelError();
|
|
60
61
|
}
|
|
61
|
-
return this.value;
|
|
62
|
+
return this.value[0];
|
|
62
63
|
}
|
|
63
64
|
}
|
|
@@ -20,17 +20,18 @@ class LastValue extends base_js_1.BaseChannel {
|
|
|
20
20
|
writable: true,
|
|
21
21
|
value: "LastValue"
|
|
22
22
|
});
|
|
23
|
+
// value is an array so we don't misinterpret an update to undefined as no write
|
|
23
24
|
Object.defineProperty(this, "value", {
|
|
24
25
|
enumerable: true,
|
|
25
26
|
configurable: true,
|
|
26
27
|
writable: true,
|
|
27
|
-
value:
|
|
28
|
+
value: []
|
|
28
29
|
});
|
|
29
30
|
}
|
|
30
31
|
fromCheckpoint(checkpoint) {
|
|
31
32
|
const empty = new LastValue();
|
|
32
33
|
if (checkpoint) {
|
|
33
|
-
empty.value = checkpoint;
|
|
34
|
+
empty.value = [checkpoint];
|
|
34
35
|
}
|
|
35
36
|
return empty;
|
|
36
37
|
}
|
|
@@ -44,20 +45,20 @@ class LastValue extends base_js_1.BaseChannel {
|
|
|
44
45
|
});
|
|
45
46
|
}
|
|
46
47
|
// eslint-disable-next-line prefer-destructuring
|
|
47
|
-
this.value = values[values.length - 1];
|
|
48
|
+
this.value = [values[values.length - 1]];
|
|
48
49
|
return true;
|
|
49
50
|
}
|
|
50
51
|
get() {
|
|
51
|
-
if (this.value ===
|
|
52
|
+
if (this.value.length === 0) {
|
|
52
53
|
throw new errors_js_1.EmptyChannelError();
|
|
53
54
|
}
|
|
54
|
-
return this.value;
|
|
55
|
+
return this.value[0];
|
|
55
56
|
}
|
|
56
57
|
checkpoint() {
|
|
57
|
-
if (this.value ===
|
|
58
|
+
if (this.value.length === 0) {
|
|
58
59
|
throw new errors_js_1.EmptyChannelError();
|
|
59
60
|
}
|
|
60
|
-
return this.value;
|
|
61
|
+
return this.value[0];
|
|
61
62
|
}
|
|
62
63
|
}
|
|
63
64
|
exports.LastValue = LastValue;
|
|
@@ -9,7 +9,7 @@ import { BaseChannel } from "./base.js";
|
|
|
9
9
|
*/
|
|
10
10
|
export declare class LastValue<Value> extends BaseChannel<Value, Value, Value> {
|
|
11
11
|
lc_graph_name: string;
|
|
12
|
-
value
|
|
12
|
+
value: [Value] | [];
|
|
13
13
|
fromCheckpoint(checkpoint?: Value): this;
|
|
14
14
|
update(values: Value[]): boolean;
|
|
15
15
|
get(): Value;
|
|
@@ -17,17 +17,18 @@ export class LastValue extends BaseChannel {
|
|
|
17
17
|
writable: true,
|
|
18
18
|
value: "LastValue"
|
|
19
19
|
});
|
|
20
|
+
// value is an array so we don't misinterpret an update to undefined as no write
|
|
20
21
|
Object.defineProperty(this, "value", {
|
|
21
22
|
enumerable: true,
|
|
22
23
|
configurable: true,
|
|
23
24
|
writable: true,
|
|
24
|
-
value:
|
|
25
|
+
value: []
|
|
25
26
|
});
|
|
26
27
|
}
|
|
27
28
|
fromCheckpoint(checkpoint) {
|
|
28
29
|
const empty = new LastValue();
|
|
29
30
|
if (checkpoint) {
|
|
30
|
-
empty.value = checkpoint;
|
|
31
|
+
empty.value = [checkpoint];
|
|
31
32
|
}
|
|
32
33
|
return empty;
|
|
33
34
|
}
|
|
@@ -41,19 +42,19 @@ export class LastValue extends BaseChannel {
|
|
|
41
42
|
});
|
|
42
43
|
}
|
|
43
44
|
// eslint-disable-next-line prefer-destructuring
|
|
44
|
-
this.value = values[values.length - 1];
|
|
45
|
+
this.value = [values[values.length - 1]];
|
|
45
46
|
return true;
|
|
46
47
|
}
|
|
47
48
|
get() {
|
|
48
|
-
if (this.value ===
|
|
49
|
+
if (this.value.length === 0) {
|
|
49
50
|
throw new EmptyChannelError();
|
|
50
51
|
}
|
|
51
|
-
return this.value;
|
|
52
|
+
return this.value[0];
|
|
52
53
|
}
|
|
53
54
|
checkpoint() {
|
|
54
|
-
if (this.value ===
|
|
55
|
+
if (this.value.length === 0) {
|
|
55
56
|
throw new EmptyChannelError();
|
|
56
57
|
}
|
|
57
|
-
return this.value;
|
|
58
|
+
return this.value[0];
|
|
58
59
|
}
|
|
59
60
|
}
|
package/dist/constants.cjs
CHANGED
|
@@ -1,23 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isCommand = exports.Command = exports._isSend = exports.Send = exports._isSendInterface = exports.CHECKPOINT_NAMESPACE_END = exports.CHECKPOINT_NAMESPACE_SEPARATOR = exports.RESERVED = exports.NULL_TASK_ID = exports.TASK_NAMESPACE = exports.PULL = exports.PUSH = exports.TASKS = exports.SELF = exports.TAG_NOSTREAM = exports.TAG_HIDDEN = exports.RECURSION_LIMIT_DEFAULT = exports.RUNTIME_PLACEHOLDER = exports.RESUME = exports.INTERRUPT = exports.CONFIG_KEY_CHECKPOINT_MAP = exports.CONFIG_KEY_CHECKPOINT_NS = exports.
|
|
4
|
-
|
|
3
|
+
exports.isCommand = exports.Command = exports._isSend = exports.Send = exports._isSendInterface = exports.CHECKPOINT_NAMESPACE_END = exports.CHECKPOINT_NAMESPACE_SEPARATOR = exports.RESERVED = exports.NULL_TASK_ID = exports.TASK_NAMESPACE = exports.PULL = exports.PUSH = exports.TASKS = exports.SELF = exports.TAG_NOSTREAM = exports.TAG_HIDDEN = exports.RECURSION_LIMIT_DEFAULT = exports.RUNTIME_PLACEHOLDER = exports.PREVIOUS = exports.RETURN = exports.NO_WRITES = exports.RESUME = exports.INTERRUPT = exports.CONFIG_KEY_CHECKPOINT_MAP = exports.CONFIG_KEY_CHECKPOINT_NS = exports.CONFIG_KEY_CHECKPOINT_ID = exports.CONFIG_KEY_PREVIOUS_STATE = exports.CONFIG_KEY_SCRATCHPAD = exports.CONFIG_KEY_RESUME_VALUE = exports.CONFIG_KEY_STREAM = exports.CONFIG_KEY_TASK_ID = exports.CONFIG_KEY_RESUMING = exports.CONFIG_KEY_CHECKPOINTER = exports.CONFIG_KEY_READ = exports.CONFIG_KEY_CALL = exports.CONFIG_KEY_SEND = exports.ERROR = exports.INPUT = exports.END = exports.START = void 0;
|
|
4
|
+
/** Special reserved node name denoting the start of a graph. */
|
|
5
|
+
exports.START = "__start__";
|
|
6
|
+
/** Special reserved node name denoting the end of a graph. */
|
|
7
|
+
exports.END = "__end__";
|
|
5
8
|
exports.INPUT = "__input__";
|
|
6
9
|
exports.ERROR = "__error__";
|
|
7
10
|
exports.CONFIG_KEY_SEND = "__pregel_send";
|
|
11
|
+
/** config key containing function used to call a node (push task) */
|
|
12
|
+
exports.CONFIG_KEY_CALL = "__pregel_call";
|
|
8
13
|
exports.CONFIG_KEY_READ = "__pregel_read";
|
|
9
14
|
exports.CONFIG_KEY_CHECKPOINTER = "__pregel_checkpointer";
|
|
10
15
|
exports.CONFIG_KEY_RESUMING = "__pregel_resuming";
|
|
11
16
|
exports.CONFIG_KEY_TASK_ID = "__pregel_task_id";
|
|
12
17
|
exports.CONFIG_KEY_STREAM = "__pregel_stream";
|
|
13
18
|
exports.CONFIG_KEY_RESUME_VALUE = "__pregel_resume_value";
|
|
14
|
-
exports.CONFIG_KEY_WRITES = "__pregel_writes";
|
|
15
19
|
exports.CONFIG_KEY_SCRATCHPAD = "__pregel_scratchpad";
|
|
20
|
+
/** config key containing state from previous invocation of graph for the given thread */
|
|
21
|
+
exports.CONFIG_KEY_PREVIOUS_STATE = "__pregel_previous";
|
|
22
|
+
exports.CONFIG_KEY_CHECKPOINT_ID = "checkpoint_id";
|
|
16
23
|
exports.CONFIG_KEY_CHECKPOINT_NS = "checkpoint_ns";
|
|
17
24
|
// this one is part of public API
|
|
18
25
|
exports.CONFIG_KEY_CHECKPOINT_MAP = "checkpoint_map";
|
|
26
|
+
/** Special channel reserved for graph interrupts */
|
|
19
27
|
exports.INTERRUPT = "__interrupt__";
|
|
28
|
+
/** Special channel reserved for graph resume */
|
|
20
29
|
exports.RESUME = "__resume__";
|
|
30
|
+
/** Special channel reserved for cases when a task exits without any writes */
|
|
31
|
+
exports.NO_WRITES = "__no_writes__";
|
|
32
|
+
/** Special channel reserved for graph return */
|
|
33
|
+
exports.RETURN = "__return__";
|
|
34
|
+
/** Special channel reserved for graph previous state */
|
|
35
|
+
exports.PREVIOUS = "__previous__";
|
|
21
36
|
exports.RUNTIME_PLACEHOLDER = "__pregel_runtime_placeholder__";
|
|
22
37
|
exports.RECURSION_LIMIT_DEFAULT = 25;
|
|
23
38
|
exports.TAG_HIDDEN = "langsmith:hidden";
|
|
@@ -29,24 +44,36 @@ exports.PULL = "__pregel_pull";
|
|
|
29
44
|
exports.TASK_NAMESPACE = "6ba7b831-9dad-11d1-80b4-00c04fd430c8";
|
|
30
45
|
exports.NULL_TASK_ID = "00000000-0000-0000-0000-000000000000";
|
|
31
46
|
exports.RESERVED = [
|
|
47
|
+
exports.TAG_HIDDEN,
|
|
48
|
+
exports.INPUT,
|
|
32
49
|
exports.INTERRUPT,
|
|
33
50
|
exports.RESUME,
|
|
34
51
|
exports.ERROR,
|
|
52
|
+
exports.NO_WRITES,
|
|
35
53
|
exports.TASKS,
|
|
54
|
+
// reserved config.configurable keys
|
|
36
55
|
exports.CONFIG_KEY_SEND,
|
|
37
56
|
exports.CONFIG_KEY_READ,
|
|
38
57
|
exports.CONFIG_KEY_CHECKPOINTER,
|
|
58
|
+
exports.CONFIG_KEY_STREAM,
|
|
39
59
|
exports.CONFIG_KEY_RESUMING,
|
|
40
60
|
exports.CONFIG_KEY_TASK_ID,
|
|
41
|
-
exports.
|
|
61
|
+
exports.CONFIG_KEY_CALL,
|
|
62
|
+
exports.CONFIG_KEY_RESUME_VALUE,
|
|
63
|
+
exports.CONFIG_KEY_SCRATCHPAD,
|
|
64
|
+
exports.CONFIG_KEY_PREVIOUS_STATE,
|
|
42
65
|
exports.CONFIG_KEY_CHECKPOINT_MAP,
|
|
43
|
-
exports.
|
|
66
|
+
exports.CONFIG_KEY_CHECKPOINT_NS,
|
|
67
|
+
exports.CONFIG_KEY_CHECKPOINT_ID,
|
|
44
68
|
];
|
|
45
69
|
exports.CHECKPOINT_NAMESPACE_SEPARATOR = "|";
|
|
46
70
|
exports.CHECKPOINT_NAMESPACE_END = ":";
|
|
47
71
|
function _isSendInterface(x) {
|
|
48
72
|
const operation = x;
|
|
49
|
-
return
|
|
73
|
+
return (operation !== null &&
|
|
74
|
+
operation !== undefined &&
|
|
75
|
+
typeof operation.node === "string" &&
|
|
76
|
+
operation.args !== undefined);
|
|
50
77
|
}
|
|
51
78
|
exports._isSendInterface = _isSendInterface;
|
|
52
79
|
/**
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,19 +1,34 @@
|
|
|
1
|
-
|
|
1
|
+
/** Special reserved node name denoting the start of a graph. */
|
|
2
|
+
export declare const START = "__start__";
|
|
3
|
+
/** Special reserved node name denoting the end of a graph. */
|
|
4
|
+
export declare const END = "__end__";
|
|
2
5
|
export declare const INPUT = "__input__";
|
|
3
6
|
export declare const ERROR = "__error__";
|
|
4
7
|
export declare const CONFIG_KEY_SEND = "__pregel_send";
|
|
8
|
+
/** config key containing function used to call a node (push task) */
|
|
9
|
+
export declare const CONFIG_KEY_CALL = "__pregel_call";
|
|
5
10
|
export declare const CONFIG_KEY_READ = "__pregel_read";
|
|
6
11
|
export declare const CONFIG_KEY_CHECKPOINTER = "__pregel_checkpointer";
|
|
7
12
|
export declare const CONFIG_KEY_RESUMING = "__pregel_resuming";
|
|
8
13
|
export declare const CONFIG_KEY_TASK_ID = "__pregel_task_id";
|
|
9
14
|
export declare const CONFIG_KEY_STREAM = "__pregel_stream";
|
|
10
15
|
export declare const CONFIG_KEY_RESUME_VALUE = "__pregel_resume_value";
|
|
11
|
-
export declare const CONFIG_KEY_WRITES = "__pregel_writes";
|
|
12
16
|
export declare const CONFIG_KEY_SCRATCHPAD = "__pregel_scratchpad";
|
|
17
|
+
/** config key containing state from previous invocation of graph for the given thread */
|
|
18
|
+
export declare const CONFIG_KEY_PREVIOUS_STATE = "__pregel_previous";
|
|
19
|
+
export declare const CONFIG_KEY_CHECKPOINT_ID = "checkpoint_id";
|
|
13
20
|
export declare const CONFIG_KEY_CHECKPOINT_NS = "checkpoint_ns";
|
|
14
21
|
export declare const CONFIG_KEY_CHECKPOINT_MAP = "checkpoint_map";
|
|
22
|
+
/** Special channel reserved for graph interrupts */
|
|
15
23
|
export declare const INTERRUPT = "__interrupt__";
|
|
24
|
+
/** Special channel reserved for graph resume */
|
|
16
25
|
export declare const RESUME = "__resume__";
|
|
26
|
+
/** Special channel reserved for cases when a task exits without any writes */
|
|
27
|
+
export declare const NO_WRITES = "__no_writes__";
|
|
28
|
+
/** Special channel reserved for graph return */
|
|
29
|
+
export declare const RETURN = "__return__";
|
|
30
|
+
/** Special channel reserved for graph previous state */
|
|
31
|
+
export declare const PREVIOUS = "__previous__";
|
|
17
32
|
export declare const RUNTIME_PLACEHOLDER = "__pregel_runtime_placeholder__";
|
|
18
33
|
export declare const RECURSION_LIMIT_DEFAULT = 25;
|
|
19
34
|
export declare const TAG_HIDDEN = "langsmith:hidden";
|
package/dist/constants.js
CHANGED
|
@@ -1,20 +1,35 @@
|
|
|
1
|
-
|
|
1
|
+
/** Special reserved node name denoting the start of a graph. */
|
|
2
|
+
export const START = "__start__";
|
|
3
|
+
/** Special reserved node name denoting the end of a graph. */
|
|
4
|
+
export const END = "__end__";
|
|
2
5
|
export const INPUT = "__input__";
|
|
3
6
|
export const ERROR = "__error__";
|
|
4
7
|
export const CONFIG_KEY_SEND = "__pregel_send";
|
|
8
|
+
/** config key containing function used to call a node (push task) */
|
|
9
|
+
export const CONFIG_KEY_CALL = "__pregel_call";
|
|
5
10
|
export const CONFIG_KEY_READ = "__pregel_read";
|
|
6
11
|
export const CONFIG_KEY_CHECKPOINTER = "__pregel_checkpointer";
|
|
7
12
|
export const CONFIG_KEY_RESUMING = "__pregel_resuming";
|
|
8
13
|
export const CONFIG_KEY_TASK_ID = "__pregel_task_id";
|
|
9
14
|
export const CONFIG_KEY_STREAM = "__pregel_stream";
|
|
10
15
|
export const CONFIG_KEY_RESUME_VALUE = "__pregel_resume_value";
|
|
11
|
-
export const CONFIG_KEY_WRITES = "__pregel_writes";
|
|
12
16
|
export const CONFIG_KEY_SCRATCHPAD = "__pregel_scratchpad";
|
|
17
|
+
/** config key containing state from previous invocation of graph for the given thread */
|
|
18
|
+
export const CONFIG_KEY_PREVIOUS_STATE = "__pregel_previous";
|
|
19
|
+
export const CONFIG_KEY_CHECKPOINT_ID = "checkpoint_id";
|
|
13
20
|
export const CONFIG_KEY_CHECKPOINT_NS = "checkpoint_ns";
|
|
14
21
|
// this one is part of public API
|
|
15
22
|
export const CONFIG_KEY_CHECKPOINT_MAP = "checkpoint_map";
|
|
23
|
+
/** Special channel reserved for graph interrupts */
|
|
16
24
|
export const INTERRUPT = "__interrupt__";
|
|
25
|
+
/** Special channel reserved for graph resume */
|
|
17
26
|
export const RESUME = "__resume__";
|
|
27
|
+
/** Special channel reserved for cases when a task exits without any writes */
|
|
28
|
+
export const NO_WRITES = "__no_writes__";
|
|
29
|
+
/** Special channel reserved for graph return */
|
|
30
|
+
export const RETURN = "__return__";
|
|
31
|
+
/** Special channel reserved for graph previous state */
|
|
32
|
+
export const PREVIOUS = "__previous__";
|
|
18
33
|
export const RUNTIME_PLACEHOLDER = "__pregel_runtime_placeholder__";
|
|
19
34
|
export const RECURSION_LIMIT_DEFAULT = 25;
|
|
20
35
|
export const TAG_HIDDEN = "langsmith:hidden";
|
|
@@ -26,24 +41,36 @@ export const PULL = "__pregel_pull";
|
|
|
26
41
|
export const TASK_NAMESPACE = "6ba7b831-9dad-11d1-80b4-00c04fd430c8";
|
|
27
42
|
export const NULL_TASK_ID = "00000000-0000-0000-0000-000000000000";
|
|
28
43
|
export const RESERVED = [
|
|
44
|
+
TAG_HIDDEN,
|
|
45
|
+
INPUT,
|
|
29
46
|
INTERRUPT,
|
|
30
47
|
RESUME,
|
|
31
48
|
ERROR,
|
|
49
|
+
NO_WRITES,
|
|
32
50
|
TASKS,
|
|
51
|
+
// reserved config.configurable keys
|
|
33
52
|
CONFIG_KEY_SEND,
|
|
34
53
|
CONFIG_KEY_READ,
|
|
35
54
|
CONFIG_KEY_CHECKPOINTER,
|
|
55
|
+
CONFIG_KEY_STREAM,
|
|
36
56
|
CONFIG_KEY_RESUMING,
|
|
37
57
|
CONFIG_KEY_TASK_ID,
|
|
38
|
-
|
|
58
|
+
CONFIG_KEY_CALL,
|
|
59
|
+
CONFIG_KEY_RESUME_VALUE,
|
|
60
|
+
CONFIG_KEY_SCRATCHPAD,
|
|
61
|
+
CONFIG_KEY_PREVIOUS_STATE,
|
|
39
62
|
CONFIG_KEY_CHECKPOINT_MAP,
|
|
40
|
-
|
|
63
|
+
CONFIG_KEY_CHECKPOINT_NS,
|
|
64
|
+
CONFIG_KEY_CHECKPOINT_ID,
|
|
41
65
|
];
|
|
42
66
|
export const CHECKPOINT_NAMESPACE_SEPARATOR = "|";
|
|
43
67
|
export const CHECKPOINT_NAMESPACE_END = ":";
|
|
44
68
|
export function _isSendInterface(x) {
|
|
45
69
|
const operation = x;
|
|
46
|
-
return
|
|
70
|
+
return (operation !== null &&
|
|
71
|
+
operation !== undefined &&
|
|
72
|
+
typeof operation.node === "string" &&
|
|
73
|
+
operation.args !== undefined);
|
|
47
74
|
}
|
|
48
75
|
/**
|
|
49
76
|
*
|
package/dist/errors.d.ts
CHANGED
|
@@ -32,9 +32,9 @@ export declare class ParentCommand extends GraphBubbleUp {
|
|
|
32
32
|
constructor(command: Command);
|
|
33
33
|
static get unminifiable_name(): string;
|
|
34
34
|
}
|
|
35
|
-
export declare function isParentCommand(e?:
|
|
36
|
-
export declare function isGraphBubbleUp(e?:
|
|
37
|
-
export declare function isGraphInterrupt(e?:
|
|
35
|
+
export declare function isParentCommand(e?: unknown): e is ParentCommand;
|
|
36
|
+
export declare function isGraphBubbleUp(e?: unknown): e is GraphBubbleUp;
|
|
37
|
+
export declare function isGraphInterrupt(e?: unknown): e is GraphInterrupt;
|
|
38
38
|
export declare class EmptyInputError extends BaseLangGraphError {
|
|
39
39
|
constructor(message?: string, fields?: BaseLangGraphErrorFields);
|
|
40
40
|
static get unminifiable_name(): string;
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPreviousState = exports.entrypoint = exports.task = void 0;
|
|
4
|
+
const singletons_1 = require("@langchain/core/singletons");
|
|
5
|
+
const index_js_1 = require("../pregel/index.cjs");
|
|
6
|
+
const read_js_1 = require("../pregel/read.cjs");
|
|
7
|
+
const constants_js_1 = require("../constants.cjs");
|
|
8
|
+
const ephemeral_value_js_1 = require("../channels/ephemeral_value.cjs");
|
|
9
|
+
const call_js_1 = require("../pregel/call.cjs");
|
|
10
|
+
const last_value_js_1 = require("../channels/last_value.cjs");
|
|
11
|
+
const utils_js_1 = require("../utils.cjs");
|
|
12
|
+
/**
|
|
13
|
+
* Define a LangGraph task using the `task` function.
|
|
14
|
+
*
|
|
15
|
+
* !!! warning "Beta"
|
|
16
|
+
* The Functional API is currently in beta and is subject to change.
|
|
17
|
+
*
|
|
18
|
+
* @beta
|
|
19
|
+
*
|
|
20
|
+
* Tasks can only be called from within an {@link entrypoint} or from within a StateGraph.
|
|
21
|
+
* A task can be called like a regular function with the following differences:
|
|
22
|
+
*
|
|
23
|
+
* - When a checkpointer is enabled, the function inputs and outputs must be serializable.
|
|
24
|
+
* - The wrapped function can only be called from within an entrypoint or StateGraph.
|
|
25
|
+
* - Calling the function produces a promise. This makes it easy to parallelize tasks.
|
|
26
|
+
*
|
|
27
|
+
* @typeParam ArgsT - The type of arguments the task function accepts
|
|
28
|
+
* @typeParam OutputT - The type of value the task function returns
|
|
29
|
+
* @param optionsOrName - Either an {@link TaskOptions} object, or a string for the name of the task
|
|
30
|
+
* @param func - The function that executes this task
|
|
31
|
+
* @returns A proxy function that accepts the same arguments as the original and always returns the result as a Promise
|
|
32
|
+
*
|
|
33
|
+
* @example basic example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const addOne = task("add", async (a: number) => a + 1);
|
|
36
|
+
*
|
|
37
|
+
* const workflow = entrypoint("example", async (numbers: number[]) => {
|
|
38
|
+
* const promises = numbers.map(n => addOne(n));
|
|
39
|
+
* const results = await Promise.all(promises);
|
|
40
|
+
* return results;
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // Call the entrypoint
|
|
44
|
+
* await workflow.invoke([1, 2, 3]); // Returns [2, 3, 4]
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @example using a retry policy
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const addOne = task({
|
|
50
|
+
* name: "add",
|
|
51
|
+
* retry: { maxAttempts: 3 }
|
|
52
|
+
* },
|
|
53
|
+
* async (a: number) => a + 1
|
|
54
|
+
* );
|
|
55
|
+
*
|
|
56
|
+
* const workflow = entrypoint("example", async (numbers: number[]) => {
|
|
57
|
+
* const promises = numbers.map(n => addOne(n));
|
|
58
|
+
* const results = await Promise.all(promises);
|
|
59
|
+
* return results;
|
|
60
|
+
* });
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
function task(optionsOrName, func) {
|
|
64
|
+
const { name, retry } = typeof optionsOrName === "string"
|
|
65
|
+
? { name: optionsOrName, retry: undefined }
|
|
66
|
+
: optionsOrName;
|
|
67
|
+
if ((0, utils_js_1.isAsyncGeneratorFunction)(func) || (0, utils_js_1.isGeneratorFunction)(func)) {
|
|
68
|
+
throw new Error("Generators are disallowed as tasks. For streaming responses, use config.write.");
|
|
69
|
+
}
|
|
70
|
+
return (...args) => {
|
|
71
|
+
return (0, call_js_1.call)({ func, name, retry }, ...args);
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
exports.task = task;
|
|
75
|
+
/**
|
|
76
|
+
* Define a LangGraph workflow using the `entrypoint` function.
|
|
77
|
+
*
|
|
78
|
+
* !!! warning "Beta"
|
|
79
|
+
* The Functional API is currently in beta and is subject to change.
|
|
80
|
+
*
|
|
81
|
+
* @beta
|
|
82
|
+
*
|
|
83
|
+
* ### Function signature
|
|
84
|
+
*
|
|
85
|
+
* The wrapped function must accept at most **two parameters**. The first parameter
|
|
86
|
+
* is the input to the function. The second (optional) parameter is a
|
|
87
|
+
* {@link LangGraphRunnableConfig} object. If you wish to pass multiple parameters to
|
|
88
|
+
* the function, you can pass them as an object.
|
|
89
|
+
*
|
|
90
|
+
* ### Helper functions
|
|
91
|
+
*
|
|
92
|
+
* #### Streaming
|
|
93
|
+
* To write data to the "custom" stream, use the {@link getWriter} function, or the
|
|
94
|
+
* {@link LangGraphRunnableConfig.writer} property.
|
|
95
|
+
*
|
|
96
|
+
* #### State management
|
|
97
|
+
* The {@link getPreviousState} function can be used to access the previous state
|
|
98
|
+
* that was returned from the last invocation of the entrypoint on the same thread id.
|
|
99
|
+
*
|
|
100
|
+
* If you wish to save state other than the return value, you can use the
|
|
101
|
+
* {@link entrypoint.final} function.
|
|
102
|
+
*
|
|
103
|
+
* @typeParam InputT - The type of input the entrypoint accepts
|
|
104
|
+
* @typeParam OutputT - The type of output the entrypoint produces
|
|
105
|
+
* @param optionsOrName - Either an {@link EntrypointOptions} object, or a string for the name of the entrypoint
|
|
106
|
+
* @param func - The function that executes this entrypoint
|
|
107
|
+
* @returns A {@link Pregel} instance that can be run to execute the workflow
|
|
108
|
+
*
|
|
109
|
+
* @example Using entrypoint and tasks
|
|
110
|
+
* ```typescript
|
|
111
|
+
* import { task, entrypoint } from "@langchain/langgraph";
|
|
112
|
+
* import { MemorySaver } from "@langchain/langgraph-checkpoint";
|
|
113
|
+
* import { interrupt, Command } from "@langchain/langgraph";
|
|
114
|
+
*
|
|
115
|
+
* const composeEssay = task("compose", async (topic: string) => {
|
|
116
|
+
* await new Promise(r => setTimeout(r, 1000)); // Simulate slow operation
|
|
117
|
+
* return `An essay about ${topic}`;
|
|
118
|
+
* });
|
|
119
|
+
*
|
|
120
|
+
* const reviewWorkflow = entrypoint({
|
|
121
|
+
* name: "review",
|
|
122
|
+
* checkpointer: new MemorySaver()
|
|
123
|
+
* }, async (topic: string) => {
|
|
124
|
+
* const essay = await composeEssay(topic);
|
|
125
|
+
* const humanReview = await interrupt({
|
|
126
|
+
* question: "Please provide a review",
|
|
127
|
+
* essay
|
|
128
|
+
* });
|
|
129
|
+
* return {
|
|
130
|
+
* essay,
|
|
131
|
+
* review: humanReview
|
|
132
|
+
* };
|
|
133
|
+
* });
|
|
134
|
+
*
|
|
135
|
+
* // Example configuration for the workflow
|
|
136
|
+
* const config = {
|
|
137
|
+
* configurable: {
|
|
138
|
+
* thread_id: "some_thread"
|
|
139
|
+
* }
|
|
140
|
+
* };
|
|
141
|
+
*
|
|
142
|
+
* // Topic for the essay
|
|
143
|
+
* const topic = "cats";
|
|
144
|
+
*
|
|
145
|
+
* // Stream the workflow to generate the essay and await human review
|
|
146
|
+
* for await (const result of reviewWorkflow.stream(topic, config)) {
|
|
147
|
+
* console.log(result);
|
|
148
|
+
* }
|
|
149
|
+
*
|
|
150
|
+
* // Example human review provided after the interrupt
|
|
151
|
+
* const humanReview = "This essay is great.";
|
|
152
|
+
*
|
|
153
|
+
* // Resume the workflow with the provided human review
|
|
154
|
+
* for await (const result of reviewWorkflow.stream(new Command({ resume: humanReview }), config)) {
|
|
155
|
+
* console.log(result);
|
|
156
|
+
* }
|
|
157
|
+
* ```
|
|
158
|
+
*
|
|
159
|
+
* @example Accessing the previous return value
|
|
160
|
+
* ```typescript
|
|
161
|
+
* import { entrypoint, getPreviousState } from "@langchain/langgraph";
|
|
162
|
+
* import { MemorySaver } from "@langchain/langgraph-checkpoint";
|
|
163
|
+
*
|
|
164
|
+
* const accumulator = entrypoint({
|
|
165
|
+
* name: "accumulator",
|
|
166
|
+
* checkpointer: new MemorySaver()
|
|
167
|
+
* }, async (input: string) => {
|
|
168
|
+
* const previous = getPreviousState<number>();
|
|
169
|
+
* return previous !== undefined ? `${previous } ${input}` : input;
|
|
170
|
+
* });
|
|
171
|
+
*
|
|
172
|
+
* const config = {
|
|
173
|
+
* configurable: {
|
|
174
|
+
* thread_id: "some_thread"
|
|
175
|
+
* }
|
|
176
|
+
* };
|
|
177
|
+
* await accumulator.invoke("hello", config); // returns "hello"
|
|
178
|
+
* await accumulator.invoke("world", config); // returns "hello world"
|
|
179
|
+
* ```
|
|
180
|
+
*
|
|
181
|
+
* @example Using entrypoint.final to save a value
|
|
182
|
+
* ```typescript
|
|
183
|
+
* import { entrypoint, getPreviousState } from "@langchain/langgraph";
|
|
184
|
+
* import { MemorySaver } from "@langchain/langgraph-checkpoint";
|
|
185
|
+
*
|
|
186
|
+
* const myWorkflow = entrypoint({
|
|
187
|
+
* name: "accumulator",
|
|
188
|
+
* checkpointer: new MemorySaver()
|
|
189
|
+
* }, async (num: number) => {
|
|
190
|
+
* const previous = getPreviousState<number>();
|
|
191
|
+
*
|
|
192
|
+
* // This will return the previous value to the caller, saving
|
|
193
|
+
* // 2 * num to the checkpoint, which will be used in the next invocation
|
|
194
|
+
* // for the `previous` parameter.
|
|
195
|
+
* return entrypoint.final({
|
|
196
|
+
* value: previous ?? 0,
|
|
197
|
+
* save: 2 * num
|
|
198
|
+
* });
|
|
199
|
+
* });
|
|
200
|
+
*
|
|
201
|
+
* const config = {
|
|
202
|
+
* configurable: {
|
|
203
|
+
* thread_id: "some_thread"
|
|
204
|
+
* }
|
|
205
|
+
* };
|
|
206
|
+
*
|
|
207
|
+
* await myWorkflow.invoke(3, config); // 0 (previous was undefined)
|
|
208
|
+
* await myWorkflow.invoke(1, config); // 6 (previous was 3 * 2 from the previous invocation)
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
exports.entrypoint = function entrypoint(optionsOrName, func) {
|
|
212
|
+
const { name, checkpointer, store } = typeof optionsOrName === "string"
|
|
213
|
+
? { name: optionsOrName, checkpointer: undefined, store: undefined }
|
|
214
|
+
: optionsOrName;
|
|
215
|
+
if ((0, utils_js_1.isAsyncGeneratorFunction)(func) || (0, utils_js_1.isGeneratorFunction)(func)) {
|
|
216
|
+
throw new Error("Generators are disallowed as entrypoints. For streaming responses, use config.write.");
|
|
217
|
+
}
|
|
218
|
+
const streamMode = "updates";
|
|
219
|
+
const bound = (0, call_js_1.getRunnableForEntrypoint)(name, func);
|
|
220
|
+
return new index_js_1.Pregel({
|
|
221
|
+
name,
|
|
222
|
+
checkpointer,
|
|
223
|
+
nodes: {
|
|
224
|
+
[name]: new read_js_1.PregelNode({
|
|
225
|
+
bound,
|
|
226
|
+
triggers: [constants_js_1.START],
|
|
227
|
+
channels: [constants_js_1.START],
|
|
228
|
+
writers: [],
|
|
229
|
+
}),
|
|
230
|
+
},
|
|
231
|
+
channels: {
|
|
232
|
+
[constants_js_1.START]: new ephemeral_value_js_1.EphemeralValue(),
|
|
233
|
+
[constants_js_1.END]: new last_value_js_1.LastValue(),
|
|
234
|
+
[constants_js_1.PREVIOUS]: new last_value_js_1.LastValue(),
|
|
235
|
+
},
|
|
236
|
+
inputChannels: constants_js_1.START,
|
|
237
|
+
outputChannels: constants_js_1.END,
|
|
238
|
+
streamChannels: constants_js_1.END,
|
|
239
|
+
streamMode,
|
|
240
|
+
store,
|
|
241
|
+
});
|
|
242
|
+
};
|
|
243
|
+
// documented by the EntrypointFunction interface
|
|
244
|
+
exports.entrypoint.final = function final({ value, save, }) {
|
|
245
|
+
return { value, save, __lg_type: "__pregel_final" };
|
|
246
|
+
};
|
|
247
|
+
/**
|
|
248
|
+
* A helper utility function for use with the functional API that returns the previous
|
|
249
|
+
* state from the checkpoint from the last invocation of the current thread.
|
|
250
|
+
*
|
|
251
|
+
* This function allows workflows to access state that was saved in previous runs
|
|
252
|
+
* using {@link entrypoint.final}.
|
|
253
|
+
*
|
|
254
|
+
* !!! warning "Beta"
|
|
255
|
+
* The Functional API is currently in beta and is subject to change.
|
|
256
|
+
*
|
|
257
|
+
* @beta
|
|
258
|
+
*
|
|
259
|
+
* @typeParam StateT - The type of the state that was previously saved
|
|
260
|
+
* @returns The previous saved state from the last invocation of the current thread
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```typescript
|
|
264
|
+
* const previousState = getPreviousState<{ counter: number }>();
|
|
265
|
+
* const newCount = (previousState?.counter ?? 0) + 1;
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
function getPreviousState() {
|
|
269
|
+
const config = singletons_1.AsyncLocalStorageProviderSingleton.getRunnableConfig();
|
|
270
|
+
return config.configurable?.[constants_js_1.CONFIG_KEY_PREVIOUS_STATE];
|
|
271
|
+
}
|
|
272
|
+
exports.getPreviousState = getPreviousState;
|