@langchain/langgraph 0.2.74 → 0.3.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.
- package/dist/channels/any_value.cjs +3 -0
- package/dist/channels/any_value.d.ts +1 -0
- package/dist/channels/any_value.js +3 -0
- package/dist/channels/any_value.js.map +1 -1
- package/dist/channels/base.cjs +31 -2
- package/dist/channels/base.d.ts +17 -2
- package/dist/channels/base.js +31 -2
- package/dist/channels/base.js.map +1 -1
- package/dist/channels/binop.cjs +3 -0
- package/dist/channels/binop.d.ts +1 -0
- package/dist/channels/binop.js +3 -0
- package/dist/channels/binop.js.map +1 -1
- package/dist/channels/dynamic_barrier_value.cjs +119 -9
- package/dist/channels/dynamic_barrier_value.d.ts +29 -0
- package/dist/channels/dynamic_barrier_value.js +117 -8
- package/dist/channels/dynamic_barrier_value.js.map +1 -1
- package/dist/channels/ephemeral_value.cjs +3 -0
- package/dist/channels/ephemeral_value.d.ts +1 -0
- package/dist/channels/ephemeral_value.js +3 -0
- package/dist/channels/ephemeral_value.js.map +1 -1
- package/dist/channels/last_value.cjs +82 -4
- package/dist/channels/last_value.d.ts +21 -0
- package/dist/channels/last_value.js +80 -3
- package/dist/channels/last_value.js.map +1 -1
- package/dist/channels/named_barrier_value.cjs +94 -1
- package/dist/channels/named_barrier_value.d.ts +23 -0
- package/dist/channels/named_barrier_value.js +92 -0
- package/dist/channels/named_barrier_value.js.map +1 -1
- package/dist/channels/topic.cjs +3 -0
- package/dist/channels/topic.d.ts +1 -0
- package/dist/channels/topic.js +3 -0
- package/dist/channels/topic.js.map +1 -1
- package/dist/constants.cjs +29 -8
- package/dist/constants.d.ts +47 -26
- package/dist/constants.js +27 -7
- package/dist/constants.js.map +1 -1
- package/dist/func/index.cjs +17 -4
- package/dist/func/index.d.ts +14 -5
- package/dist/func/index.js +17 -4
- package/dist/func/index.js.map +1 -1
- package/dist/func/types.d.ts +1 -1
- package/dist/graph/graph.d.ts +4 -2
- package/dist/graph/graph.js.map +1 -1
- package/dist/graph/index.cjs +2 -1
- package/dist/graph/index.d.ts +1 -1
- package/dist/graph/index.js +1 -1
- package/dist/graph/index.js.map +1 -1
- package/dist/graph/messages_annotation.cjs +3 -0
- package/dist/graph/messages_annotation.js +3 -0
- package/dist/graph/messages_annotation.js.map +1 -1
- package/dist/graph/state.cjs +67 -22
- package/dist/graph/state.d.ts +22 -8
- package/dist/graph/state.js +68 -24
- package/dist/graph/state.js.map +1 -1
- package/dist/graph/zod/schema.cjs +12 -61
- package/dist/graph/zod/schema.js +12 -61
- package/dist/graph/zod/schema.js.map +1 -1
- package/dist/graph/zod/state.cjs +63 -0
- package/dist/graph/zod/state.d.ts +10 -1
- package/dist/graph/zod/state.js +61 -0
- package/dist/graph/zod/state.js.map +1 -1
- package/dist/prebuilt/react_agent_executor.cjs +99 -45
- package/dist/prebuilt/react_agent_executor.d.ts +20 -4
- package/dist/prebuilt/react_agent_executor.js +99 -45
- package/dist/prebuilt/react_agent_executor.js.map +1 -1
- package/dist/pregel/algo.cjs +60 -20
- package/dist/pregel/algo.d.ts +1 -1
- package/dist/pregel/algo.js +61 -21
- package/dist/pregel/algo.js.map +1 -1
- package/dist/pregel/call.cjs +2 -1
- package/dist/pregel/call.d.ts +3 -2
- package/dist/pregel/call.js +2 -1
- package/dist/pregel/call.js.map +1 -1
- package/dist/pregel/debug.test.cjs +6 -0
- package/dist/pregel/debug.test.js +6 -0
- package/dist/pregel/debug.test.js.map +1 -1
- package/dist/pregel/index.cjs +99 -29
- package/dist/pregel/index.d.ts +19 -6
- package/dist/pregel/index.js +100 -30
- package/dist/pregel/index.js.map +1 -1
- package/dist/pregel/loop.cjs +126 -26
- package/dist/pregel/loop.d.ts +29 -2
- package/dist/pregel/loop.js +127 -27
- package/dist/pregel/loop.js.map +1 -1
- package/dist/pregel/read.cjs +12 -1
- package/dist/pregel/read.d.ts +3 -1
- package/dist/pregel/read.js +12 -1
- package/dist/pregel/read.js.map +1 -1
- package/dist/pregel/retry.cjs +2 -6
- package/dist/pregel/retry.js +2 -6
- package/dist/pregel/retry.js.map +1 -1
- package/dist/pregel/runner.cjs +1 -0
- package/dist/pregel/runner.js +1 -0
- package/dist/pregel/runner.js.map +1 -1
- package/dist/pregel/types.cjs +8 -1
- package/dist/pregel/types.d.ts +64 -8
- package/dist/pregel/types.js +8 -1
- package/dist/pregel/types.js.map +1 -1
- package/dist/pregel/utils/index.d.ts +15 -0
- package/dist/pregel/utils/index.js.map +1 -1
- package/dist/web.cjs +4 -1
- package/dist/web.d.ts +3 -3
- package/dist/web.js +2 -2
- package/dist/web.js.map +1 -1
- package/package.json +5 -5
|
@@ -5,42 +5,9 @@ exports.getUpdateTypeSchema = getUpdateTypeSchema;
|
|
|
5
5
|
exports.getInputTypeSchema = getInputTypeSchema;
|
|
6
6
|
exports.getOutputTypeSchema = getOutputTypeSchema;
|
|
7
7
|
exports.getConfigTypeSchema = getConfigTypeSchema;
|
|
8
|
-
const zod_1 = require("zod");
|
|
9
8
|
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
10
9
|
const state_js_1 = require("./state.cjs");
|
|
11
|
-
const
|
|
12
|
-
const DESCRIPTION_PREFIX = "lg:";
|
|
13
|
-
function applyPlugin(schema, actions) {
|
|
14
|
-
const cacheKey = [
|
|
15
|
-
`reducer:${actions.reducer ?? false}`,
|
|
16
|
-
`jsonSchemaExtra:${actions.jsonSchemaExtra ?? false}`,
|
|
17
|
-
`partial:${actions.partial ?? false}`,
|
|
18
|
-
].join("|");
|
|
19
|
-
TYPE_CACHE[cacheKey] ??= new WeakMap();
|
|
20
|
-
const cache = TYPE_CACHE[cacheKey];
|
|
21
|
-
if (cache.has(schema))
|
|
22
|
-
return cache.get(schema);
|
|
23
|
-
let shape = zod_1.z.object({
|
|
24
|
-
...Object.fromEntries(Object.entries(schema.shape).map(([key, input]) => {
|
|
25
|
-
const meta = (0, state_js_1.getMeta)(input);
|
|
26
|
-
let output = actions.reducer ? meta?.reducer?.schema ?? input : input;
|
|
27
|
-
if (actions.jsonSchemaExtra) {
|
|
28
|
-
const strMeta = JSON.stringify({
|
|
29
|
-
...meta?.jsonSchemaExtra,
|
|
30
|
-
description: output.description ?? input.description,
|
|
31
|
-
});
|
|
32
|
-
if (strMeta !== "{}") {
|
|
33
|
-
output = output.describe(`${DESCRIPTION_PREFIX}${strMeta}`);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return [key, output];
|
|
37
|
-
})),
|
|
38
|
-
});
|
|
39
|
-
if (actions.partial)
|
|
40
|
-
shape = shape.partial();
|
|
41
|
-
cache.set(schema, shape);
|
|
42
|
-
return shape;
|
|
43
|
-
}
|
|
10
|
+
const PartialStateSchema = Symbol.for("langgraph.state.partial");
|
|
44
11
|
function isGraphWithZodLike(graph) {
|
|
45
12
|
if (!graph || typeof graph !== "object")
|
|
46
13
|
return false;
|
|
@@ -51,28 +18,8 @@ function isGraphWithZodLike(graph) {
|
|
|
51
18
|
}
|
|
52
19
|
return true;
|
|
53
20
|
}
|
|
54
|
-
function applyExtraFromDescription(schema) {
|
|
55
|
-
if (Array.isArray(schema)) {
|
|
56
|
-
return schema.map(applyExtraFromDescription);
|
|
57
|
-
}
|
|
58
|
-
if (typeof schema === "object" && schema != null) {
|
|
59
|
-
const output = Object.fromEntries(Object.entries(schema).map(([key, value]) => [
|
|
60
|
-
key,
|
|
61
|
-
applyExtraFromDescription(value),
|
|
62
|
-
]));
|
|
63
|
-
if ("description" in output &&
|
|
64
|
-
typeof output.description === "string" &&
|
|
65
|
-
output.description.startsWith(DESCRIPTION_PREFIX)) {
|
|
66
|
-
const strMeta = output.description.slice(DESCRIPTION_PREFIX.length);
|
|
67
|
-
delete output.description;
|
|
68
|
-
Object.assign(output, JSON.parse(strMeta));
|
|
69
|
-
}
|
|
70
|
-
return output;
|
|
71
|
-
}
|
|
72
|
-
return schema;
|
|
73
|
-
}
|
|
74
21
|
function toJsonSchema(schema) {
|
|
75
|
-
return applyExtraFromDescription((0, zod_to_json_schema_1.zodToJsonSchema)(schema));
|
|
22
|
+
return (0, state_js_1.applyExtraFromDescription)((0, zod_to_json_schema_1.zodToJsonSchema)(schema));
|
|
76
23
|
}
|
|
77
24
|
/**
|
|
78
25
|
* Get the state schema for a graph.
|
|
@@ -85,7 +32,7 @@ function getStateTypeSchema(graph) {
|
|
|
85
32
|
const schemaDef = graph.builder._schemaRuntimeDefinition;
|
|
86
33
|
if (!schemaDef)
|
|
87
34
|
return undefined;
|
|
88
|
-
return toJsonSchema(schemaDef);
|
|
35
|
+
return toJsonSchema((0, state_js_1.applyZodPlugin)(schemaDef, { jsonSchemaExtra: true }));
|
|
89
36
|
}
|
|
90
37
|
/**
|
|
91
38
|
* Get the update schema for a graph.
|
|
@@ -98,7 +45,7 @@ function getUpdateTypeSchema(graph) {
|
|
|
98
45
|
const schemaDef = graph.builder._schemaRuntimeDefinition;
|
|
99
46
|
if (!schemaDef)
|
|
100
47
|
return undefined;
|
|
101
|
-
return toJsonSchema(
|
|
48
|
+
return toJsonSchema((0, state_js_1.applyZodPlugin)(schemaDef, {
|
|
102
49
|
reducer: true,
|
|
103
50
|
jsonSchemaExtra: true,
|
|
104
51
|
partial: true,
|
|
@@ -112,10 +59,14 @@ function getUpdateTypeSchema(graph) {
|
|
|
112
59
|
function getInputTypeSchema(graph) {
|
|
113
60
|
if (!isGraphWithZodLike(graph))
|
|
114
61
|
return undefined;
|
|
115
|
-
|
|
62
|
+
let schemaDef = graph.builder._inputRuntimeDefinition;
|
|
63
|
+
if (schemaDef === PartialStateSchema) {
|
|
64
|
+
// No need to pass `.partial()` here, that's being done by `applyPlugin`
|
|
65
|
+
schemaDef = graph.builder._schemaRuntimeDefinition;
|
|
66
|
+
}
|
|
116
67
|
if (!schemaDef)
|
|
117
68
|
return undefined;
|
|
118
|
-
return toJsonSchema(
|
|
69
|
+
return toJsonSchema((0, state_js_1.applyZodPlugin)(schemaDef, {
|
|
119
70
|
reducer: true,
|
|
120
71
|
jsonSchemaExtra: true,
|
|
121
72
|
partial: true,
|
|
@@ -132,7 +83,7 @@ function getOutputTypeSchema(graph) {
|
|
|
132
83
|
const schemaDef = graph.builder._outputRuntimeDefinition;
|
|
133
84
|
if (!schemaDef)
|
|
134
85
|
return undefined;
|
|
135
|
-
return toJsonSchema(
|
|
86
|
+
return toJsonSchema((0, state_js_1.applyZodPlugin)(schemaDef, { jsonSchemaExtra: true }));
|
|
136
87
|
}
|
|
137
88
|
/**
|
|
138
89
|
* Get the config schema for a graph.
|
|
@@ -145,6 +96,6 @@ function getConfigTypeSchema(graph) {
|
|
|
145
96
|
const configDef = graph.builder._configRuntimeSchema;
|
|
146
97
|
if (!configDef)
|
|
147
98
|
return undefined;
|
|
148
|
-
return toJsonSchema(
|
|
99
|
+
return toJsonSchema((0, state_js_1.applyZodPlugin)(configDef, { jsonSchemaExtra: true }));
|
|
149
100
|
}
|
|
150
101
|
//# sourceMappingURL=schema.js.map
|
package/dist/graph/zod/schema.js
CHANGED
|
@@ -1,39 +1,6 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
1
|
import { zodToJsonSchema as _zodToJsonSchema } from "zod-to-json-schema";
|
|
3
|
-
import {
|
|
4
|
-
const
|
|
5
|
-
const DESCRIPTION_PREFIX = "lg:";
|
|
6
|
-
function applyPlugin(schema, actions) {
|
|
7
|
-
const cacheKey = [
|
|
8
|
-
`reducer:${actions.reducer ?? false}`,
|
|
9
|
-
`jsonSchemaExtra:${actions.jsonSchemaExtra ?? false}`,
|
|
10
|
-
`partial:${actions.partial ?? false}`,
|
|
11
|
-
].join("|");
|
|
12
|
-
TYPE_CACHE[cacheKey] ??= new WeakMap();
|
|
13
|
-
const cache = TYPE_CACHE[cacheKey];
|
|
14
|
-
if (cache.has(schema))
|
|
15
|
-
return cache.get(schema);
|
|
16
|
-
let shape = z.object({
|
|
17
|
-
...Object.fromEntries(Object.entries(schema.shape).map(([key, input]) => {
|
|
18
|
-
const meta = getMeta(input);
|
|
19
|
-
let output = actions.reducer ? meta?.reducer?.schema ?? input : input;
|
|
20
|
-
if (actions.jsonSchemaExtra) {
|
|
21
|
-
const strMeta = JSON.stringify({
|
|
22
|
-
...meta?.jsonSchemaExtra,
|
|
23
|
-
description: output.description ?? input.description,
|
|
24
|
-
});
|
|
25
|
-
if (strMeta !== "{}") {
|
|
26
|
-
output = output.describe(`${DESCRIPTION_PREFIX}${strMeta}`);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return [key, output];
|
|
30
|
-
})),
|
|
31
|
-
});
|
|
32
|
-
if (actions.partial)
|
|
33
|
-
shape = shape.partial();
|
|
34
|
-
cache.set(schema, shape);
|
|
35
|
-
return shape;
|
|
36
|
-
}
|
|
2
|
+
import { applyZodPlugin, applyExtraFromDescription } from "./state.js";
|
|
3
|
+
const PartialStateSchema = Symbol.for("langgraph.state.partial");
|
|
37
4
|
function isGraphWithZodLike(graph) {
|
|
38
5
|
if (!graph || typeof graph !== "object")
|
|
39
6
|
return false;
|
|
@@ -44,26 +11,6 @@ function isGraphWithZodLike(graph) {
|
|
|
44
11
|
}
|
|
45
12
|
return true;
|
|
46
13
|
}
|
|
47
|
-
function applyExtraFromDescription(schema) {
|
|
48
|
-
if (Array.isArray(schema)) {
|
|
49
|
-
return schema.map(applyExtraFromDescription);
|
|
50
|
-
}
|
|
51
|
-
if (typeof schema === "object" && schema != null) {
|
|
52
|
-
const output = Object.fromEntries(Object.entries(schema).map(([key, value]) => [
|
|
53
|
-
key,
|
|
54
|
-
applyExtraFromDescription(value),
|
|
55
|
-
]));
|
|
56
|
-
if ("description" in output &&
|
|
57
|
-
typeof output.description === "string" &&
|
|
58
|
-
output.description.startsWith(DESCRIPTION_PREFIX)) {
|
|
59
|
-
const strMeta = output.description.slice(DESCRIPTION_PREFIX.length);
|
|
60
|
-
delete output.description;
|
|
61
|
-
Object.assign(output, JSON.parse(strMeta));
|
|
62
|
-
}
|
|
63
|
-
return output;
|
|
64
|
-
}
|
|
65
|
-
return schema;
|
|
66
|
-
}
|
|
67
14
|
function toJsonSchema(schema) {
|
|
68
15
|
return applyExtraFromDescription(_zodToJsonSchema(schema));
|
|
69
16
|
}
|
|
@@ -78,7 +25,7 @@ export function getStateTypeSchema(graph) {
|
|
|
78
25
|
const schemaDef = graph.builder._schemaRuntimeDefinition;
|
|
79
26
|
if (!schemaDef)
|
|
80
27
|
return undefined;
|
|
81
|
-
return toJsonSchema(schemaDef);
|
|
28
|
+
return toJsonSchema(applyZodPlugin(schemaDef, { jsonSchemaExtra: true }));
|
|
82
29
|
}
|
|
83
30
|
/**
|
|
84
31
|
* Get the update schema for a graph.
|
|
@@ -91,7 +38,7 @@ export function getUpdateTypeSchema(graph) {
|
|
|
91
38
|
const schemaDef = graph.builder._schemaRuntimeDefinition;
|
|
92
39
|
if (!schemaDef)
|
|
93
40
|
return undefined;
|
|
94
|
-
return toJsonSchema(
|
|
41
|
+
return toJsonSchema(applyZodPlugin(schemaDef, {
|
|
95
42
|
reducer: true,
|
|
96
43
|
jsonSchemaExtra: true,
|
|
97
44
|
partial: true,
|
|
@@ -105,10 +52,14 @@ export function getUpdateTypeSchema(graph) {
|
|
|
105
52
|
export function getInputTypeSchema(graph) {
|
|
106
53
|
if (!isGraphWithZodLike(graph))
|
|
107
54
|
return undefined;
|
|
108
|
-
|
|
55
|
+
let schemaDef = graph.builder._inputRuntimeDefinition;
|
|
56
|
+
if (schemaDef === PartialStateSchema) {
|
|
57
|
+
// No need to pass `.partial()` here, that's being done by `applyPlugin`
|
|
58
|
+
schemaDef = graph.builder._schemaRuntimeDefinition;
|
|
59
|
+
}
|
|
109
60
|
if (!schemaDef)
|
|
110
61
|
return undefined;
|
|
111
|
-
return toJsonSchema(
|
|
62
|
+
return toJsonSchema(applyZodPlugin(schemaDef, {
|
|
112
63
|
reducer: true,
|
|
113
64
|
jsonSchemaExtra: true,
|
|
114
65
|
partial: true,
|
|
@@ -125,7 +76,7 @@ export function getOutputTypeSchema(graph) {
|
|
|
125
76
|
const schemaDef = graph.builder._outputRuntimeDefinition;
|
|
126
77
|
if (!schemaDef)
|
|
127
78
|
return undefined;
|
|
128
|
-
return toJsonSchema(
|
|
79
|
+
return toJsonSchema(applyZodPlugin(schemaDef, { jsonSchemaExtra: true }));
|
|
129
80
|
}
|
|
130
81
|
/**
|
|
131
82
|
* Get the config schema for a graph.
|
|
@@ -138,6 +89,6 @@ export function getConfigTypeSchema(graph) {
|
|
|
138
89
|
const configDef = graph.builder._configRuntimeSchema;
|
|
139
90
|
if (!configDef)
|
|
140
91
|
return undefined;
|
|
141
|
-
return toJsonSchema(
|
|
92
|
+
return toJsonSchema(applyZodPlugin(configDef, { jsonSchemaExtra: true }));
|
|
142
93
|
}
|
|
143
94
|
//# sourceMappingURL=schema.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/graph/zod/schema.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/graph/zod/schema.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,cAAc,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAEvE,MAAM,kBAAkB,GAAG,MAAM,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;AAcjE,SAAS,kBAAkB,CAAC,KAAc;IACxC,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IACtD,IACE,CAAC,CAAC,SAAS,IAAI,KAAK,CAAC;QACrB,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ;QACjC,KAAK,CAAC,OAAO,IAAI,IAAI,EACrB,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,YAAY,CAAC,MAAiB;IACrC,OAAO,yBAAyB,CAAC,gBAAgB,CAAC,MAAM,CA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;KACd,CAAC,CACH,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAc;IAChD,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IACjD,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,wBAAwB,CAAC;IACzD,IAAI,CAAC,SAAS;QAAE,OAAO,SAAS,CAAC;IACjC,OAAO,YAAY,CAAC,cAAc,CAAC,SAAS,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;AAC5E,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAc;IAChD,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IACjD,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,oBAAoB,CAAC;IACrD,IAAI,CAAC,SAAS;QAAE,OAAO,SAAS,CAAC;IACjC,OAAO,YAAY,CAAC,cAAc,CAAC,SAAS,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;AAC5E,CAAC"}
|
package/dist/graph/zod/state.cjs
CHANGED
|
@@ -6,6 +6,8 @@ exports.withLangGraph = withLangGraph;
|
|
|
6
6
|
exports.getMeta = getMeta;
|
|
7
7
|
exports.extendMeta = extendMeta;
|
|
8
8
|
exports.getChannelsFromZod = getChannelsFromZod;
|
|
9
|
+
exports.applyZodPlugin = applyZodPlugin;
|
|
10
|
+
exports.applyExtraFromDescription = applyExtraFromDescription;
|
|
9
11
|
const binop_js_1 = require("../../channels/binop.cjs");
|
|
10
12
|
const last_value_js_1 = require("../../channels/last_value.cjs");
|
|
11
13
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -69,4 +71,65 @@ function getChannelsFromZod(schema) {
|
|
|
69
71
|
}
|
|
70
72
|
return channels;
|
|
71
73
|
}
|
|
74
|
+
const ZOD_TYPE_CACHE = {};
|
|
75
|
+
const ZOD_DESCRIPTION_PREFIX = "lg:";
|
|
76
|
+
function applyZodPlugin(schema, actions) {
|
|
77
|
+
const cacheKey = [
|
|
78
|
+
`reducer:${actions.reducer ?? false}`,
|
|
79
|
+
`jsonSchemaExtra:${actions.jsonSchemaExtra ?? false}`,
|
|
80
|
+
`partial:${actions.partial ?? false}`,
|
|
81
|
+
].join("|");
|
|
82
|
+
ZOD_TYPE_CACHE[cacheKey] ??= new WeakMap();
|
|
83
|
+
const cache = ZOD_TYPE_CACHE[cacheKey];
|
|
84
|
+
if (cache.has(schema))
|
|
85
|
+
return cache.get(schema);
|
|
86
|
+
let shape = schema.extend({
|
|
87
|
+
...Object.fromEntries(Object.entries(schema.shape).map(([key, input]) => {
|
|
88
|
+
const meta = getMeta(input);
|
|
89
|
+
let output = actions.reducer ? meta?.reducer?.schema ?? input : input;
|
|
90
|
+
if (actions.jsonSchemaExtra) {
|
|
91
|
+
const strMeta = JSON.stringify({
|
|
92
|
+
...meta?.jsonSchemaExtra,
|
|
93
|
+
description: output.description ?? input.description,
|
|
94
|
+
});
|
|
95
|
+
if (strMeta !== "{}") {
|
|
96
|
+
output = output.describe(`${ZOD_DESCRIPTION_PREFIX}${strMeta}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return [key, output];
|
|
100
|
+
})),
|
|
101
|
+
});
|
|
102
|
+
// using zObject.extend() will set `unknownKeys` to `passthrough`
|
|
103
|
+
// which trips up `zod-to-json-schema`
|
|
104
|
+
if ("_def" in shape &&
|
|
105
|
+
shape._def != null &&
|
|
106
|
+
typeof shape._def === "object" &&
|
|
107
|
+
"unknownKeys" in shape._def) {
|
|
108
|
+
shape._def.unknownKeys = "strip";
|
|
109
|
+
}
|
|
110
|
+
if (actions.partial)
|
|
111
|
+
shape = shape.partial();
|
|
112
|
+
cache.set(schema, shape);
|
|
113
|
+
return shape;
|
|
114
|
+
}
|
|
115
|
+
function applyExtraFromDescription(schema) {
|
|
116
|
+
if (Array.isArray(schema)) {
|
|
117
|
+
return schema.map(applyExtraFromDescription);
|
|
118
|
+
}
|
|
119
|
+
if (typeof schema === "object" && schema != null) {
|
|
120
|
+
const output = Object.fromEntries(Object.entries(schema).map(([key, value]) => [
|
|
121
|
+
key,
|
|
122
|
+
applyExtraFromDescription(value),
|
|
123
|
+
]));
|
|
124
|
+
if ("description" in output &&
|
|
125
|
+
typeof output.description === "string" &&
|
|
126
|
+
output.description.startsWith(ZOD_DESCRIPTION_PREFIX)) {
|
|
127
|
+
const strMeta = output.description.slice(ZOD_DESCRIPTION_PREFIX.length);
|
|
128
|
+
delete output.description;
|
|
129
|
+
Object.assign(output, JSON.parse(strMeta));
|
|
130
|
+
}
|
|
131
|
+
return output;
|
|
132
|
+
}
|
|
133
|
+
return schema;
|
|
134
|
+
}
|
|
72
135
|
//# sourceMappingURL=state.js.map
|
|
@@ -3,7 +3,7 @@ import { BaseChannel } from "../../channels/base.js";
|
|
|
3
3
|
export interface Meta<ValueType, UpdateType = ValueType> {
|
|
4
4
|
jsonSchemaExtra?: {
|
|
5
5
|
langgraph_nodes?: string[];
|
|
6
|
-
langgraph_type?: "prompt";
|
|
6
|
+
langgraph_type?: "prompt" | "messages";
|
|
7
7
|
[key: string]: unknown;
|
|
8
8
|
};
|
|
9
9
|
reducer?: {
|
|
@@ -28,3 +28,12 @@ export type ZodToStateDefinition<T extends AnyZodObject> = {
|
|
|
28
28
|
[key in keyof T["shape"]]: T["shape"][key] extends z.ZodType<infer V, z.ZodTypeDef, infer U> ? BaseChannel<V, U> : never;
|
|
29
29
|
};
|
|
30
30
|
export declare function getChannelsFromZod<T extends z.ZodRawShape>(schema: z.ZodObject<T>): ZodToStateDefinition<z.ZodObject<T>>;
|
|
31
|
+
export declare function applyZodPlugin(schema: z.AnyZodObject, actions: {
|
|
32
|
+
/** Apply .langgraph.reducer calls */
|
|
33
|
+
reducer?: boolean;
|
|
34
|
+
/** Apply .langgraph.metadata() calls */
|
|
35
|
+
jsonSchemaExtra?: boolean;
|
|
36
|
+
/** Apply .partial() */
|
|
37
|
+
partial?: boolean;
|
|
38
|
+
}): z.AnyZodObject;
|
|
39
|
+
export declare function applyExtraFromDescription(schema: unknown): unknown;
|
package/dist/graph/zod/state.js
CHANGED
|
@@ -61,4 +61,65 @@ export function getChannelsFromZod(schema) {
|
|
|
61
61
|
}
|
|
62
62
|
return channels;
|
|
63
63
|
}
|
|
64
|
+
const ZOD_TYPE_CACHE = {};
|
|
65
|
+
const ZOD_DESCRIPTION_PREFIX = "lg:";
|
|
66
|
+
export function applyZodPlugin(schema, actions) {
|
|
67
|
+
const cacheKey = [
|
|
68
|
+
`reducer:${actions.reducer ?? false}`,
|
|
69
|
+
`jsonSchemaExtra:${actions.jsonSchemaExtra ?? false}`,
|
|
70
|
+
`partial:${actions.partial ?? false}`,
|
|
71
|
+
].join("|");
|
|
72
|
+
ZOD_TYPE_CACHE[cacheKey] ??= new WeakMap();
|
|
73
|
+
const cache = ZOD_TYPE_CACHE[cacheKey];
|
|
74
|
+
if (cache.has(schema))
|
|
75
|
+
return cache.get(schema);
|
|
76
|
+
let shape = schema.extend({
|
|
77
|
+
...Object.fromEntries(Object.entries(schema.shape).map(([key, input]) => {
|
|
78
|
+
const meta = getMeta(input);
|
|
79
|
+
let output = actions.reducer ? meta?.reducer?.schema ?? input : input;
|
|
80
|
+
if (actions.jsonSchemaExtra) {
|
|
81
|
+
const strMeta = JSON.stringify({
|
|
82
|
+
...meta?.jsonSchemaExtra,
|
|
83
|
+
description: output.description ?? input.description,
|
|
84
|
+
});
|
|
85
|
+
if (strMeta !== "{}") {
|
|
86
|
+
output = output.describe(`${ZOD_DESCRIPTION_PREFIX}${strMeta}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return [key, output];
|
|
90
|
+
})),
|
|
91
|
+
});
|
|
92
|
+
// using zObject.extend() will set `unknownKeys` to `passthrough`
|
|
93
|
+
// which trips up `zod-to-json-schema`
|
|
94
|
+
if ("_def" in shape &&
|
|
95
|
+
shape._def != null &&
|
|
96
|
+
typeof shape._def === "object" &&
|
|
97
|
+
"unknownKeys" in shape._def) {
|
|
98
|
+
shape._def.unknownKeys = "strip";
|
|
99
|
+
}
|
|
100
|
+
if (actions.partial)
|
|
101
|
+
shape = shape.partial();
|
|
102
|
+
cache.set(schema, shape);
|
|
103
|
+
return shape;
|
|
104
|
+
}
|
|
105
|
+
export function applyExtraFromDescription(schema) {
|
|
106
|
+
if (Array.isArray(schema)) {
|
|
107
|
+
return schema.map(applyExtraFromDescription);
|
|
108
|
+
}
|
|
109
|
+
if (typeof schema === "object" && schema != null) {
|
|
110
|
+
const output = Object.fromEntries(Object.entries(schema).map(([key, value]) => [
|
|
111
|
+
key,
|
|
112
|
+
applyExtraFromDescription(value),
|
|
113
|
+
]));
|
|
114
|
+
if ("description" in output &&
|
|
115
|
+
typeof output.description === "string" &&
|
|
116
|
+
output.description.startsWith(ZOD_DESCRIPTION_PREFIX)) {
|
|
117
|
+
const strMeta = output.description.slice(ZOD_DESCRIPTION_PREFIX.length);
|
|
118
|
+
delete output.description;
|
|
119
|
+
Object.assign(output, JSON.parse(strMeta));
|
|
120
|
+
}
|
|
121
|
+
return output;
|
|
122
|
+
}
|
|
123
|
+
return schema;
|
|
124
|
+
}
|
|
64
125
|
//# sourceMappingURL=state.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"state.js","sourceRoot":"","sources":["../../../src/graph/zod/state.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAEzD,8DAA8D;AAC9D,MAAM,QAAQ,GAAG,IAAI,OAAO,EAA6B,CAAC;AAkB1D,SAAS,SAAS,CAAC,KAAc;IAC/B,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,IAAI,IAAI;QACb,QAAQ,IAAI,KAAK;QACjB,OAAO,KAAK,C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CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;IAC/C,CAAC;IAED,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;QACjD,MAAM,MAAM,GAAG,MAAM,CAAC,WAAW,CAC/B,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC;YAC3C,GAAG;YACH,yBAAyB,CAAC,KAAK,CAAC;SACjC,CAAC,CACH,CAAC;QAEF,IACE,aAAa,IAAI,MAAM;YACvB,OAAO,MAAM,CAAC,WAAW,KAAK,QAAQ;YACtC,MAAM,CAAC,WAAW,CAAC,UAAU,CAAC,sBAAsB,CAAC,EACrD,CAAC;YACD,MAAM,OAAO,GAAG,MAAM,CAAC,WAAW,CAAC,KAAK,CAAC,sBAAsB,CAAC,MAAM,CAAC,CAAC;YACxE,OAAO,MAAM,CAAC,WAAW,CAAC;YAC1B,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;QAC7C,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -56,6 +56,9 @@ function _getPromptRunnable(prompt) {
|
|
|
56
56
|
}
|
|
57
57
|
return promptRunnable;
|
|
58
58
|
}
|
|
59
|
+
function isClientTool(tool) {
|
|
60
|
+
return runnables_1.Runnable.isRunnable(tool);
|
|
61
|
+
}
|
|
59
62
|
function _getPrompt(prompt, stateModifier, messageModifier) {
|
|
60
63
|
// Check if multiple modifiers exist
|
|
61
64
|
const definedCount = [prompt, stateModifier, messageModifier].filter((x) => x != null).length;
|
|
@@ -113,7 +116,7 @@ async function _shouldBindTools(llm, tools) {
|
|
|
113
116
|
if (tools.length !== boundTools.length) {
|
|
114
117
|
throw new Error("Number of tools in the model.bindTools() and tools passed to createReactAgent must match");
|
|
115
118
|
}
|
|
116
|
-
const toolNames = new Set(tools.
|
|
119
|
+
const toolNames = new Set(tools.flatMap((tool) => (isClientTool(tool) ? tool.name : [])));
|
|
117
120
|
const boundToolNames = new Set();
|
|
118
121
|
for (const boundTool of boundTools) {
|
|
119
122
|
let boundToolName;
|
|
@@ -121,7 +124,7 @@ async function _shouldBindTools(llm, tools) {
|
|
|
121
124
|
if ("type" in boundTool && boundTool.type === "function") {
|
|
122
125
|
boundToolName = boundTool.function.name;
|
|
123
126
|
}
|
|
124
|
-
// Anthropic
|
|
127
|
+
// Anthropic or Google-style tool
|
|
125
128
|
else if ("name" in boundTool) {
|
|
126
129
|
boundToolName = boundTool.name;
|
|
127
130
|
}
|
|
@@ -173,6 +176,12 @@ const createReactAgentAnnotation = () => annotation_js_1.Annotation.Root({
|
|
|
173
176
|
structuredResponse: (annotation_js_1.Annotation),
|
|
174
177
|
});
|
|
175
178
|
exports.createReactAgentAnnotation = createReactAgentAnnotation;
|
|
179
|
+
const PreHookAnnotation = annotation_js_1.Annotation.Root({
|
|
180
|
+
llmInputMessages: (0, annotation_js_1.Annotation)({
|
|
181
|
+
reducer: message_js_1.messagesStateReducer,
|
|
182
|
+
default: () => [],
|
|
183
|
+
}),
|
|
184
|
+
});
|
|
176
185
|
/**
|
|
177
186
|
* Creates a StateGraph agent that relies on a chat model utilizing tool calling.
|
|
178
187
|
*
|
|
@@ -216,7 +225,7 @@ exports.createReactAgentAnnotation = createReactAgentAnnotation;
|
|
|
216
225
|
* ```
|
|
217
226
|
*/
|
|
218
227
|
function createReactAgent(params) {
|
|
219
|
-
const { llm, tools, messageModifier, stateModifier, prompt, stateSchema, checkpointSaver, checkpointer, interruptBefore, interruptAfter, store, responseFormat, name, includeAgentName, } = params;
|
|
228
|
+
const { llm, tools, messageModifier, stateModifier, prompt, stateSchema, checkpointSaver, checkpointer, interruptBefore, interruptAfter, store, responseFormat, preModelHook, postModelHook, name, includeAgentName, } = params;
|
|
220
229
|
let toolClasses;
|
|
221
230
|
let toolNode;
|
|
222
231
|
if (!Array.isArray(tools)) {
|
|
@@ -225,7 +234,7 @@ function createReactAgent(params) {
|
|
|
225
234
|
}
|
|
226
235
|
else {
|
|
227
236
|
toolClasses = tools;
|
|
228
|
-
toolNode = new tool_node_js_1.ToolNode(
|
|
237
|
+
toolNode = new tool_node_js_1.ToolNode(toolClasses.filter(isClientTool));
|
|
229
238
|
}
|
|
230
239
|
let cachedModelRunnable = null;
|
|
231
240
|
const getModelRunnable = async (llm) => {
|
|
@@ -252,19 +261,16 @@ function createReactAgent(params) {
|
|
|
252
261
|
// If any of the tools are configured to return_directly after running,
|
|
253
262
|
// our graph needs to check if these were called
|
|
254
263
|
const shouldReturnDirect = new Set(toolClasses
|
|
264
|
+
.filter(isClientTool)
|
|
255
265
|
.filter((tool) => "returnDirect" in tool && tool.returnDirect)
|
|
256
266
|
.map((tool) => tool.name));
|
|
257
|
-
|
|
258
|
-
const { messages } = state;
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
(!lastMessage.tool_calls || lastMessage.tool_calls.length === 0)) {
|
|
262
|
-
return responseFormat != null ? "generate_structured_response" : constants_js_1.END;
|
|
263
|
-
}
|
|
264
|
-
else {
|
|
265
|
-
return "continue";
|
|
267
|
+
function getModelInputState(state) {
|
|
268
|
+
const { messages, llmInputMessages, ...rest } = state;
|
|
269
|
+
if (llmInputMessages != null && llmInputMessages.length > 0) {
|
|
270
|
+
return { messages: llmInputMessages, ...rest };
|
|
266
271
|
}
|
|
267
|
-
|
|
272
|
+
return { messages, ...rest };
|
|
273
|
+
}
|
|
268
274
|
const generateStructuredResponse = async (state, config) => {
|
|
269
275
|
if (responseFormat == null) {
|
|
270
276
|
throw new Error("Attempted to generate structured output with no passed response schema. Please contact us for help.");
|
|
@@ -289,54 +295,102 @@ function createReactAgent(params) {
|
|
|
289
295
|
// to ensure that we can validate ConfigurableModel properly
|
|
290
296
|
const modelRunnable = await getModelRunnable(llm);
|
|
291
297
|
// TODO: Auto-promote streaming.
|
|
292
|
-
const response = (await modelRunnable.invoke(state, config));
|
|
298
|
+
const response = (await modelRunnable.invoke(getModelInputState(state), config));
|
|
293
299
|
// add agent name to the AIMessage
|
|
294
300
|
// TODO: figure out if we can avoid mutating the message directly
|
|
295
301
|
response.name = name;
|
|
296
302
|
response.lc_kwargs.name = name;
|
|
297
303
|
return { messages: [response] };
|
|
298
304
|
};
|
|
299
|
-
const
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
305
|
+
const schema = stateSchema ?? (0, exports.createReactAgentAnnotation)();
|
|
306
|
+
const workflow = new index_js_1.StateGraph(schema).addNode("tools", toolNode);
|
|
307
|
+
const allNodeWorkflows = workflow;
|
|
308
|
+
const conditionalMap = (map) => {
|
|
309
|
+
return Object.fromEntries(Object.entries(map).filter(([_, v]) => v != null));
|
|
310
|
+
};
|
|
311
|
+
let entrypoint = "agent";
|
|
312
|
+
let inputSchema;
|
|
313
|
+
if (preModelHook != null) {
|
|
314
|
+
allNodeWorkflows
|
|
315
|
+
.addNode("pre_model_hook", preModelHook)
|
|
316
|
+
.addEdge("pre_model_hook", "agent");
|
|
317
|
+
entrypoint = "pre_model_hook";
|
|
318
|
+
inputSchema = annotation_js_1.Annotation.Root({
|
|
319
|
+
...schema.spec,
|
|
320
|
+
...PreHookAnnotation.spec,
|
|
311
321
|
});
|
|
312
322
|
}
|
|
313
323
|
else {
|
|
314
|
-
|
|
315
|
-
continue: "tools",
|
|
316
|
-
[constants_js_1.END]: constants_js_1.END,
|
|
317
|
-
});
|
|
324
|
+
entrypoint = "agent";
|
|
318
325
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
326
|
+
allNodeWorkflows
|
|
327
|
+
.addNode("agent", callModel, { input: inputSchema })
|
|
328
|
+
.addEdge(constants_js_1.START, entrypoint);
|
|
329
|
+
if (postModelHook != null) {
|
|
330
|
+
allNodeWorkflows
|
|
331
|
+
.addNode("post_model_hook", postModelHook)
|
|
332
|
+
.addEdge("agent", "post_model_hook")
|
|
333
|
+
.addConditionalEdges("post_model_hook", (state) => {
|
|
334
|
+
const { messages } = state;
|
|
335
|
+
const lastMessage = messages[messages.length - 1];
|
|
336
|
+
if ((0, messages_1.isAIMessage)(lastMessage) && lastMessage.tool_calls?.length) {
|
|
337
|
+
return "tools";
|
|
325
338
|
}
|
|
326
|
-
|
|
327
|
-
|
|
339
|
+
if ((0, messages_1.isToolMessage)(lastMessage))
|
|
340
|
+
return entrypoint;
|
|
341
|
+
if (responseFormat != null)
|
|
342
|
+
return "generate_structured_response";
|
|
343
|
+
return constants_js_1.END;
|
|
344
|
+
}, conditionalMap({
|
|
345
|
+
tools: "tools",
|
|
346
|
+
[entrypoint]: entrypoint,
|
|
347
|
+
generate_structured_response: responseFormat != null ? "generate_structured_response" : null,
|
|
348
|
+
[constants_js_1.END]: responseFormat != null ? null : constants_js_1.END,
|
|
349
|
+
}));
|
|
350
|
+
}
|
|
351
|
+
if (responseFormat !== undefined) {
|
|
352
|
+
workflow
|
|
353
|
+
.addNode("generate_structured_response", generateStructuredResponse)
|
|
354
|
+
.addEdge("generate_structured_response", constants_js_1.END);
|
|
355
|
+
}
|
|
356
|
+
if (postModelHook == null) {
|
|
357
|
+
allNodeWorkflows.addConditionalEdges("agent", (state) => {
|
|
358
|
+
const { messages } = state;
|
|
359
|
+
const lastMessage = messages[messages.length - 1];
|
|
360
|
+
// if there's no function call, we finish
|
|
361
|
+
if (!(0, messages_1.isAIMessage)(lastMessage) || !lastMessage.tool_calls?.length) {
|
|
362
|
+
if (responseFormat != null)
|
|
363
|
+
return "generate_structured_response";
|
|
328
364
|
return constants_js_1.END;
|
|
329
365
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
366
|
+
// there are function calls, we continue
|
|
367
|
+
return "tools";
|
|
368
|
+
}, conditionalMap({
|
|
369
|
+
tools: "tools",
|
|
370
|
+
generate_structured_response: responseFormat != null ? "generate_structured_response" : null,
|
|
371
|
+
[constants_js_1.END]: responseFormat != null ? null : constants_js_1.END,
|
|
372
|
+
}));
|
|
373
|
+
}
|
|
333
374
|
if (shouldReturnDirect.size > 0) {
|
|
334
|
-
|
|
375
|
+
allNodeWorkflows.addConditionalEdges("tools", (state) => {
|
|
376
|
+
// Check the last consecutive tool calls
|
|
377
|
+
for (let i = state.messages.length - 1; i >= 0; i -= 1) {
|
|
378
|
+
const message = state.messages[i];
|
|
379
|
+
if (!(0, messages_1.isToolMessage)(message))
|
|
380
|
+
break;
|
|
381
|
+
// Check if this tool is configured to return directly
|
|
382
|
+
if (message.name !== undefined &&
|
|
383
|
+
shouldReturnDirect.has(message.name)) {
|
|
384
|
+
return constants_js_1.END;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return entrypoint;
|
|
388
|
+
}, conditionalMap({ [entrypoint]: entrypoint, [constants_js_1.END]: constants_js_1.END }));
|
|
335
389
|
}
|
|
336
390
|
else {
|
|
337
|
-
|
|
391
|
+
allNodeWorkflows.addEdge("tools", entrypoint);
|
|
338
392
|
}
|
|
339
|
-
return
|
|
393
|
+
return allNodeWorkflows.compile({
|
|
340
394
|
checkpointer: checkpointer ?? checkpointSaver,
|
|
341
395
|
interruptBefore,
|
|
342
396
|
interruptAfter,
|