@copilotkit/sdk-js 2.0.0-rc.5 → 2.0.0-rc.51
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/langgraph/json-schema-to-zod.cjs +87 -0
- package/dist/langgraph/json-schema-to-zod.cjs.map +1 -0
- package/dist/langgraph/json-schema-to-zod.mjs +86 -0
- package/dist/langgraph/json-schema-to-zod.mjs.map +1 -0
- package/dist/langgraph/middleware.cjs +11 -9
- package/dist/langgraph/middleware.cjs.map +1 -1
- package/dist/langgraph/middleware.d.cts +7 -5
- package/dist/langgraph/middleware.d.cts.map +1 -1
- package/dist/langgraph/middleware.d.mts +7 -5
- package/dist/langgraph/middleware.d.mts.map +1 -1
- package/dist/langgraph/middleware.mjs +19 -17
- package/dist/langgraph/middleware.mjs.map +1 -1
- package/dist/langgraph/utils.cjs +62 -40
- package/dist/langgraph/utils.cjs.map +1 -1
- package/dist/langgraph/utils.d.cts +64 -43
- package/dist/langgraph/utils.d.cts.map +1 -1
- package/dist/langgraph/utils.d.mts +64 -43
- package/dist/langgraph/utils.d.mts.map +1 -1
- package/dist/langgraph/utils.mjs +62 -40
- package/dist/langgraph/utils.mjs.map +1 -1
- package/package.json +23 -8
- package/src/langgraph/__tests__/json-schema-to-zod.test.ts +124 -0
- package/src/langgraph/__tests__/middleware.test.ts +41 -24
- package/src/langgraph/json-schema-to-zod.ts +151 -0
- package/src/langgraph/middleware.ts +10 -8
- package/src/langgraph/utils.ts +61 -40
- package/tsconfig.json +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@copilotkit/sdk-js",
|
|
3
|
-
"version": "2.0.0-rc.
|
|
3
|
+
"version": "2.0.0-rc.51",
|
|
4
4
|
"private": false,
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -16,6 +16,9 @@
|
|
|
16
16
|
],
|
|
17
17
|
"homepage": "https://github.com/CopilotKit/CopilotKit",
|
|
18
18
|
"license": "MIT",
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=20"
|
|
21
|
+
},
|
|
19
22
|
"repository": {
|
|
20
23
|
"type": "git",
|
|
21
24
|
"url": "https://github.com/CopilotKit/CopilotKit.git"
|
|
@@ -25,6 +28,19 @@
|
|
|
25
28
|
"main": "./dist/index.cjs",
|
|
26
29
|
"module": "./dist/index.mjs",
|
|
27
30
|
"types": "./dist/index.d.cts",
|
|
31
|
+
"typesVersions": {
|
|
32
|
+
"*": {
|
|
33
|
+
"langchain": [
|
|
34
|
+
"./dist/langchain.d.cts"
|
|
35
|
+
],
|
|
36
|
+
"langgraph": [
|
|
37
|
+
"./dist/langgraph.d.cts"
|
|
38
|
+
],
|
|
39
|
+
"langgraph-middlewares": [
|
|
40
|
+
"./dist/langgraph-middlewares.d.cts"
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
},
|
|
28
44
|
"exports": {
|
|
29
45
|
".": {
|
|
30
46
|
"import": "./dist/index.mjs",
|
|
@@ -49,7 +65,7 @@
|
|
|
49
65
|
},
|
|
50
66
|
"dependencies": {
|
|
51
67
|
"@ag-ui/langgraph": "0.0.44",
|
|
52
|
-
"@copilotkit/shared": "2.0.0-rc.
|
|
68
|
+
"@copilotkit/shared": "2.0.0-rc.51",
|
|
53
69
|
"@standard-schema/spec": "^1.0.0"
|
|
54
70
|
},
|
|
55
71
|
"devDependencies": {
|
|
@@ -66,14 +82,13 @@
|
|
|
66
82
|
"tsdown": "^0.20.3",
|
|
67
83
|
"typescript": "^5.2.3",
|
|
68
84
|
"vitest": "^3.2.4",
|
|
69
|
-
"
|
|
85
|
+
"@copilotkit/typescript-config": "1.55.0-next.8"
|
|
70
86
|
},
|
|
71
87
|
"peerDependencies": {
|
|
72
|
-
"@langchain/core": ">=
|
|
73
|
-
"@langchain/langgraph": ">=
|
|
74
|
-
"langchain": ">=1.
|
|
75
|
-
"
|
|
76
|
-
"zod": "^3.23.3 || ^3.24.0 || ^3.25.0"
|
|
88
|
+
"@langchain/core": ">=1.1.41 <2",
|
|
89
|
+
"@langchain/langgraph": ">=1.2.9 <2",
|
|
90
|
+
"langchain": ">=1.3.4 <2",
|
|
91
|
+
"zod": "^4.2.0"
|
|
77
92
|
},
|
|
78
93
|
"scripts": {
|
|
79
94
|
"build": "tsdown && node ../../scripts/deprecations/v1-dist-notices.mjs --entrypoint sdk-js,sdk-js-langchain,sdk-js-langgraph-middlewares",
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { convertJsonSchemaToZodSchema } from "../json-schema-to-zod";
|
|
3
|
+
|
|
4
|
+
afterEach(() => {
|
|
5
|
+
vi.restoreAllMocks();
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
describe("convertJsonSchemaToZodSchema", () => {
|
|
9
|
+
it("converts objects with required, optional, nested, and array properties", () => {
|
|
10
|
+
const schema = convertJsonSchemaToZodSchema(
|
|
11
|
+
{
|
|
12
|
+
type: "object",
|
|
13
|
+
properties: {
|
|
14
|
+
name: { type: "string", description: "User name" },
|
|
15
|
+
age: { type: "integer" },
|
|
16
|
+
address: {
|
|
17
|
+
type: "object",
|
|
18
|
+
properties: { city: { type: "string" } },
|
|
19
|
+
required: ["city"],
|
|
20
|
+
},
|
|
21
|
+
tags: { type: "array", items: { type: "string" } },
|
|
22
|
+
},
|
|
23
|
+
required: ["name", "address"],
|
|
24
|
+
},
|
|
25
|
+
true,
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
expect(
|
|
29
|
+
schema.safeParse({ name: "Ada", address: { city: "London" } }).success,
|
|
30
|
+
).toBe(true);
|
|
31
|
+
expect(
|
|
32
|
+
schema.safeParse({
|
|
33
|
+
name: "Ada",
|
|
34
|
+
age: 36,
|
|
35
|
+
address: { city: "London" },
|
|
36
|
+
tags: ["a", "b"],
|
|
37
|
+
}).success,
|
|
38
|
+
).toBe(true);
|
|
39
|
+
expect(schema.safeParse({ name: "Ada" }).success).toBe(false);
|
|
40
|
+
expect(
|
|
41
|
+
schema.safeParse({ name: "Ada", address: {}, tags: [1] }).success,
|
|
42
|
+
).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("makes the schema optional when not required", () => {
|
|
46
|
+
const schema = convertJsonSchemaToZodSchema({ type: "string" }, false);
|
|
47
|
+
expect(schema.safeParse(undefined).success).toBe(true);
|
|
48
|
+
expect(schema.safeParse(1).success).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("keeps string enums, null unions, and anyOf unions", () => {
|
|
52
|
+
const schema = convertJsonSchemaToZodSchema(
|
|
53
|
+
{
|
|
54
|
+
type: "object",
|
|
55
|
+
properties: {
|
|
56
|
+
role: { type: "string", enum: ["admin", "user"] },
|
|
57
|
+
note: { type: ["string", "null"] },
|
|
58
|
+
id: { anyOf: [{ type: "string" }, { type: "number" }] },
|
|
59
|
+
},
|
|
60
|
+
required: ["role", "note", "id"],
|
|
61
|
+
},
|
|
62
|
+
true,
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
expect(schema.safeParse({ role: "admin", note: null, id: 1 }).success).toBe(
|
|
66
|
+
true,
|
|
67
|
+
);
|
|
68
|
+
expect(schema.safeParse({ role: "user", note: "x", id: "a" }).success).toBe(
|
|
69
|
+
true,
|
|
70
|
+
);
|
|
71
|
+
expect(schema.safeParse({ role: "owner", note: null, id: 1 }).success).toBe(
|
|
72
|
+
false,
|
|
73
|
+
);
|
|
74
|
+
expect(
|
|
75
|
+
schema.safeParse({ role: "user", note: null, id: true }).success,
|
|
76
|
+
).toBe(false);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("resolves $ref definitions", () => {
|
|
80
|
+
const schema = convertJsonSchemaToZodSchema(
|
|
81
|
+
{
|
|
82
|
+
type: "object",
|
|
83
|
+
properties: { home: { $ref: "#/$defs/Address" } },
|
|
84
|
+
required: ["home"],
|
|
85
|
+
$defs: {
|
|
86
|
+
Address: {
|
|
87
|
+
type: "object",
|
|
88
|
+
properties: { city: { type: "string" } },
|
|
89
|
+
required: ["city"],
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
true,
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
expect(schema.safeParse({ home: { city: "Paris" } }).success).toBe(true);
|
|
97
|
+
expect(schema.safeParse({ home: {} }).success).toBe(false);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("falls back to z.any() for circular $ref and unsupported types", () => {
|
|
101
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
102
|
+
const circular = convertJsonSchemaToZodSchema(
|
|
103
|
+
{
|
|
104
|
+
type: "object",
|
|
105
|
+
properties: { root: { $ref: "#/$defs/Node" } },
|
|
106
|
+
required: ["root"],
|
|
107
|
+
$defs: {
|
|
108
|
+
Node: {
|
|
109
|
+
type: "object",
|
|
110
|
+
properties: { child: { $ref: "#/$defs/Node" } },
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
true,
|
|
115
|
+
);
|
|
116
|
+
expect(
|
|
117
|
+
circular.safeParse({ root: { child: { anything: 1 } } }).success,
|
|
118
|
+
).toBe(true);
|
|
119
|
+
|
|
120
|
+
const unsupported = convertJsonSchemaToZodSchema({ type: "tuple" }, true);
|
|
121
|
+
expect(unsupported.safeParse(["x", 1]).success).toBe(true);
|
|
122
|
+
expect(warn).toHaveBeenCalledTimes(2);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
@@ -1032,11 +1032,12 @@ describe("auto-A2UI injection", () => {
|
|
|
1032
1032
|
// zodState — Standard-Schema JSON-schema augmentation
|
|
1033
1033
|
// ---------------------------------------------------------------------------
|
|
1034
1034
|
//
|
|
1035
|
-
// Contract: a
|
|
1036
|
-
//
|
|
1037
|
-
//
|
|
1038
|
-
//
|
|
1039
|
-
//
|
|
1035
|
+
// Contract: a Standard Schema without `~standard.jsonSchema` makes LangGraph's
|
|
1036
|
+
// `isStandardJSONSchema()` return false, and the field is dropped from
|
|
1037
|
+
// `output_schema`. `zodState` patches `~standard.jsonSchema.input` onto such a
|
|
1038
|
+
// schema so the field survives serialization and reaches the frontend via
|
|
1039
|
+
// AG-UI `STATE_SNAPSHOT` events. Zod 4.2+ implements the hook natively, and
|
|
1040
|
+
// `zodState` leaves it alone.
|
|
1040
1041
|
|
|
1041
1042
|
type JsonSchema = {
|
|
1042
1043
|
type?: string;
|
|
@@ -1045,27 +1046,41 @@ type JsonSchema = {
|
|
|
1045
1046
|
type StdJson = { jsonSchema?: { input: () => JsonSchema } };
|
|
1046
1047
|
const standardOf = (s: object): StdJson | undefined =>
|
|
1047
1048
|
(s as { "~standard"?: StdJson })["~standard"];
|
|
1048
|
-
const hasZodV4ToJsonSchema = (): boolean =>
|
|
1049
|
-
typeof (z as { toJSONSchema?: unknown }).toJSONSchema === "function";
|
|
1050
1049
|
|
|
1051
1050
|
describe("zodState", () => {
|
|
1052
|
-
it("
|
|
1051
|
+
it("keeps the native ~standard.jsonSchema hook of a Zod 4.2+ schema", () => {
|
|
1053
1052
|
const schema = z.object({ name: z.string() });
|
|
1054
|
-
|
|
1053
|
+
const native = standardOf(schema)?.jsonSchema;
|
|
1054
|
+
expect(typeof native?.input).toBe("function");
|
|
1055
|
+
|
|
1056
|
+
const wrapped = zodState(schema);
|
|
1057
|
+
|
|
1058
|
+
expect(standardOf(wrapped)?.jsonSchema).toBe(native);
|
|
1059
|
+
expect(standardOf(wrapped)?.jsonSchema?.input()).toMatchObject({
|
|
1060
|
+
type: "object",
|
|
1061
|
+
properties: { name: { type: "string" } },
|
|
1062
|
+
});
|
|
1063
|
+
});
|
|
1064
|
+
|
|
1065
|
+
it("attaches a ~standard.jsonSchema.input hook to a Standard Schema without one", () => {
|
|
1066
|
+
const schema = {
|
|
1067
|
+
"~standard": {
|
|
1068
|
+
version: 1,
|
|
1069
|
+
vendor: "other",
|
|
1070
|
+
validate: (value: unknown) => ({ value }),
|
|
1071
|
+
},
|
|
1072
|
+
};
|
|
1055
1073
|
|
|
1056
1074
|
const wrapped = zodState(schema);
|
|
1057
1075
|
|
|
1058
1076
|
const std = standardOf(wrapped);
|
|
1059
|
-
expect(std?.jsonSchema).toBeDefined();
|
|
1060
1077
|
expect(typeof std?.jsonSchema?.input).toBe("function");
|
|
1078
|
+
// Not a Zod schema, so `z.toJSONSchema` throws and the hook falls back
|
|
1079
|
+
// to an opaque `{}`.
|
|
1080
|
+
expect(std?.jsonSchema?.input()).toEqual({});
|
|
1061
1081
|
});
|
|
1062
1082
|
|
|
1063
|
-
it("returns
|
|
1064
|
-
// The contract langgraph cares about is just "input() returns an
|
|
1065
|
-
// object" — `isStandardJSONSchema()` doesn't introspect the shape.
|
|
1066
|
-
// When zod v4's `toJSONSchema` is present we get the real schema;
|
|
1067
|
-
// otherwise we get `{}`, which is still enough for the field to
|
|
1068
|
-
// appear in `output_schema` (langgraph-api treats it as opaque).
|
|
1083
|
+
it("returns the real JSON Schema from input() for a Zod schema", () => {
|
|
1069
1084
|
const schema = z.object({ todos: z.array(z.string()) });
|
|
1070
1085
|
const wrapped = zodState(schema);
|
|
1071
1086
|
|
|
@@ -1073,14 +1088,18 @@ describe("zodState", () => {
|
|
|
1073
1088
|
|
|
1074
1089
|
expect(json).toBeTypeOf("object");
|
|
1075
1090
|
expect(json).not.toBeNull();
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
expect(json?.properties?.todos).toBeDefined();
|
|
1079
|
-
}
|
|
1091
|
+
expect(json?.type).toBe("object");
|
|
1092
|
+
expect(json?.properties?.todos).toBeDefined();
|
|
1080
1093
|
});
|
|
1081
1094
|
|
|
1082
1095
|
it("caches the JSON Schema across calls (input() returns the same object)", () => {
|
|
1083
|
-
const schema =
|
|
1096
|
+
const schema = {
|
|
1097
|
+
"~standard": {
|
|
1098
|
+
version: 1,
|
|
1099
|
+
vendor: "other",
|
|
1100
|
+
validate: (value: unknown) => ({ value }),
|
|
1101
|
+
},
|
|
1102
|
+
};
|
|
1084
1103
|
const wrapped = zodState(schema);
|
|
1085
1104
|
const std = standardOf(wrapped);
|
|
1086
1105
|
|
|
@@ -1123,9 +1142,7 @@ describe("zodState", () => {
|
|
|
1123
1142
|
expect(std?.jsonSchema).toBeDefined();
|
|
1124
1143
|
const json = std?.jsonSchema?.input();
|
|
1125
1144
|
expect(json).toBeTypeOf("object");
|
|
1126
|
-
|
|
1127
|
-
expect(json?.type).toBe("array");
|
|
1128
|
-
}
|
|
1145
|
+
expect(json?.type).toBe("array");
|
|
1129
1146
|
});
|
|
1130
1147
|
|
|
1131
1148
|
it("makes the wrapped field pass a StandardJSONSchemaV1-style probe", () => {
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Convert a JSON Schema to a Zod schema. Resolves `$ref` against `$defs` /
|
|
5
|
+
* `definitions`, maps `anyOf` / `oneOf` to unions, and falls back to
|
|
6
|
+
* `z.any()` for circular refs and unsupported types.
|
|
7
|
+
*
|
|
8
|
+
* Moved here from `@copilotkit/shared` in 2.0 so that shared does not depend
|
|
9
|
+
* on zod.
|
|
10
|
+
*/
|
|
11
|
+
export function convertJsonSchemaToZodSchema(
|
|
12
|
+
jsonSchema: any,
|
|
13
|
+
required: boolean,
|
|
14
|
+
definitions?: Record<string, any>,
|
|
15
|
+
visitedRefs?: Set<string>,
|
|
16
|
+
): z.ZodSchema {
|
|
17
|
+
// Resolve $ref references
|
|
18
|
+
if (jsonSchema.$ref && definitions) {
|
|
19
|
+
const refPath = jsonSchema.$ref.replace(
|
|
20
|
+
/^#\/\$defs\/|^#\/definitions\//,
|
|
21
|
+
"",
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
// Detect circular $ref cycles
|
|
25
|
+
const refs = visitedRefs ?? new Set<string>();
|
|
26
|
+
if (refs.has(refPath)) {
|
|
27
|
+
console.warn(
|
|
28
|
+
`[CopilotKit] Circular $ref detected for "${refPath}" — falling back to z.any()`,
|
|
29
|
+
);
|
|
30
|
+
let schema = z.any();
|
|
31
|
+
if (jsonSchema.description) {
|
|
32
|
+
schema = schema.describe(jsonSchema.description);
|
|
33
|
+
}
|
|
34
|
+
return required ? schema : schema.optional();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const resolved = definitions[refPath];
|
|
38
|
+
if (resolved) {
|
|
39
|
+
// Clone the set so sibling branches don't see each other's visited refs
|
|
40
|
+
const nextRefs = new Set(refs);
|
|
41
|
+
nextRefs.add(refPath);
|
|
42
|
+
return convertJsonSchemaToZodSchema(
|
|
43
|
+
resolved,
|
|
44
|
+
required,
|
|
45
|
+
definitions,
|
|
46
|
+
nextRefs,
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Collect top-level definitions for $ref resolution
|
|
52
|
+
const defs = definitions ?? jsonSchema.$defs ?? jsonSchema.definitions;
|
|
53
|
+
|
|
54
|
+
// Handle null-union types like ["string", "null"]
|
|
55
|
+
if (Array.isArray(jsonSchema.type)) {
|
|
56
|
+
const types = jsonSchema.type as string[];
|
|
57
|
+
const hasNull = types.includes("null");
|
|
58
|
+
const nonNullTypes = types.filter((t: string) => t !== "null");
|
|
59
|
+
const resolvedType = nonNullTypes.length > 0 ? nonNullTypes[0] : "string";
|
|
60
|
+
const innerSchema = convertJsonSchemaToZodSchema(
|
|
61
|
+
{ ...jsonSchema, type: resolvedType },
|
|
62
|
+
true,
|
|
63
|
+
defs,
|
|
64
|
+
visitedRefs,
|
|
65
|
+
);
|
|
66
|
+
let schema = hasNull ? z.union([innerSchema, z.null()]) : innerSchema;
|
|
67
|
+
if (jsonSchema.description) {
|
|
68
|
+
schema = schema.describe(jsonSchema.description);
|
|
69
|
+
}
|
|
70
|
+
return required ? schema : schema.optional();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Handle anyOf / oneOf as z.union
|
|
74
|
+
const unionVariants = jsonSchema.anyOf ?? jsonSchema.oneOf;
|
|
75
|
+
if (Array.isArray(unionVariants) && unionVariants.length > 0) {
|
|
76
|
+
if (unionVariants.length === 1) {
|
|
77
|
+
return convertJsonSchemaToZodSchema(
|
|
78
|
+
unionVariants[0],
|
|
79
|
+
required,
|
|
80
|
+
defs,
|
|
81
|
+
visitedRefs,
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
const schemas = unionVariants.map((v: any) =>
|
|
85
|
+
convertJsonSchemaToZodSchema(v, true, defs, visitedRefs),
|
|
86
|
+
);
|
|
87
|
+
let schema = z.union(
|
|
88
|
+
schemas as [z.ZodSchema, z.ZodSchema, ...z.ZodSchema[]],
|
|
89
|
+
);
|
|
90
|
+
if (jsonSchema.description) {
|
|
91
|
+
schema = schema.describe(jsonSchema.description);
|
|
92
|
+
}
|
|
93
|
+
return required ? schema : schema.optional();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (jsonSchema.type === "object") {
|
|
97
|
+
const spec: { [key: string]: z.ZodSchema } = {};
|
|
98
|
+
|
|
99
|
+
if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
|
|
100
|
+
return !required ? z.object(spec).optional() : z.object(spec);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
104
|
+
spec[key] = convertJsonSchemaToZodSchema(
|
|
105
|
+
value,
|
|
106
|
+
jsonSchema.required ? jsonSchema.required.includes(key) : false,
|
|
107
|
+
defs,
|
|
108
|
+
visitedRefs,
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
let schema = z.object(spec).describe(jsonSchema.description);
|
|
112
|
+
return required ? schema : schema.optional();
|
|
113
|
+
} else if (jsonSchema.type === "string") {
|
|
114
|
+
if (jsonSchema.enum && jsonSchema.enum.length > 0) {
|
|
115
|
+
let schema = z
|
|
116
|
+
.enum(jsonSchema.enum as [string, ...string[]])
|
|
117
|
+
.describe(jsonSchema.description);
|
|
118
|
+
return required ? schema : schema.optional();
|
|
119
|
+
}
|
|
120
|
+
let schema = z.string().describe(jsonSchema.description);
|
|
121
|
+
return required ? schema : schema.optional();
|
|
122
|
+
} else if (jsonSchema.type === "number" || jsonSchema.type === "integer") {
|
|
123
|
+
let schema = z.number().describe(jsonSchema.description);
|
|
124
|
+
return required ? schema : schema.optional();
|
|
125
|
+
} else if (jsonSchema.type === "boolean") {
|
|
126
|
+
let schema = z.boolean().describe(jsonSchema.description);
|
|
127
|
+
return required ? schema : schema.optional();
|
|
128
|
+
} else if (jsonSchema.type === "array") {
|
|
129
|
+
let itemSchema = convertJsonSchemaToZodSchema(
|
|
130
|
+
jsonSchema.items,
|
|
131
|
+
true,
|
|
132
|
+
defs,
|
|
133
|
+
visitedRefs,
|
|
134
|
+
);
|
|
135
|
+
let schema = z.array(itemSchema).describe(jsonSchema.description);
|
|
136
|
+
return required ? schema : schema.optional();
|
|
137
|
+
} else if (jsonSchema.type === "null") {
|
|
138
|
+
let schema = z.null().describe(jsonSchema.description);
|
|
139
|
+
return required ? schema : schema.optional();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Fallback: accept any value rather than throwing
|
|
143
|
+
console.warn(
|
|
144
|
+
`[CopilotKit] Unsupported JSON schema type "${jsonSchema.type ?? "unknown"}" — falling back to z.any()`,
|
|
145
|
+
);
|
|
146
|
+
let schema = z.any();
|
|
147
|
+
if (jsonSchema.description) {
|
|
148
|
+
schema = schema.describe(jsonSchema.description);
|
|
149
|
+
}
|
|
150
|
+
return required ? schema : schema.optional();
|
|
151
|
+
}
|
|
@@ -134,11 +134,13 @@ type WithJsonSchema<T> = T extends { "~standard": infer S }
|
|
|
134
134
|
* `getJsonSchemaFromSchema` (called from `StateSchema.getJsonSchema`)
|
|
135
135
|
* can serialize the field.
|
|
136
136
|
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
137
|
+
* Zod 4.2+ schemas already implement `~standard.jsonSchema` and are
|
|
138
|
+
* returned unchanged. A Standard Schema without that hook carries
|
|
139
|
+
* `~standard.validate` + `vendor` only, so `isStandardJSONSchema()` returns
|
|
140
|
+
* false and the field is silently dropped from the graph's `output_schema`.
|
|
141
|
+
* That makes AG-UI `STATE_SNAPSHOT` events filter the field out of the
|
|
142
|
+
* payload sent to the frontend even though the underlying thread state has
|
|
143
|
+
* the value.
|
|
142
144
|
*
|
|
143
145
|
* Use this on any custom state field you want visible to the frontend
|
|
144
146
|
* via `useAgent().state.*`.
|
|
@@ -410,7 +412,7 @@ const applyAppContextNote = (request: any): any => {
|
|
|
410
412
|
* import { copilotkitMiddleware } from "@copilotkit/sdk-js/langgraph";
|
|
411
413
|
*
|
|
412
414
|
* const agent = createAgent({
|
|
413
|
-
* model: "gpt-
|
|
415
|
+
* model: "gpt-6-luna",
|
|
414
416
|
* tools: [backendTool],
|
|
415
417
|
* middleware: [copilotkitMiddleware],
|
|
416
418
|
* });
|
|
@@ -430,11 +432,11 @@ const copilotKitStateSchema = z
|
|
|
430
432
|
interceptedToolCalls: z.array(z.any()).optional(),
|
|
431
433
|
originalAIMessageId: z.string().optional(),
|
|
432
434
|
})
|
|
433
|
-
.
|
|
435
|
+
.loose()
|
|
434
436
|
.optional(),
|
|
435
437
|
),
|
|
436
438
|
})
|
|
437
|
-
.
|
|
439
|
+
.loose();
|
|
438
440
|
|
|
439
441
|
const isToolCallContentBlock = (block: unknown) =>
|
|
440
442
|
typeof block === "object" &&
|
package/src/langgraph/utils.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { RunnableConfig } from "@langchain/core/runnables";
|
|
2
2
|
import { dispatchCustomEvent } from "@langchain/core/callbacks/dispatch";
|
|
3
3
|
import {
|
|
4
|
-
convertJsonSchemaToZodSchema,
|
|
5
4
|
randomId,
|
|
6
5
|
randomUUID,
|
|
7
6
|
CopilotKitMisuseError,
|
|
@@ -10,11 +9,12 @@ import { interrupt } from "@langchain/langgraph";
|
|
|
10
9
|
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
11
10
|
import { AIMessage } from "@langchain/core/messages";
|
|
12
11
|
import type { OptionsConfig } from "./types";
|
|
12
|
+
import { convertJsonSchemaToZodSchema } from "./json-schema-to-zod";
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Customize the LangGraph configuration for use in CopilotKit.
|
|
16
16
|
*
|
|
17
|
-
* To the CopilotKit SDK, run:
|
|
17
|
+
* To install the CopilotKit SDK, run:
|
|
18
18
|
*
|
|
19
19
|
* ```bash
|
|
20
20
|
* npm install @copilotkit/sdk-js
|
|
@@ -25,13 +25,15 @@ import type { OptionsConfig } from "./types";
|
|
|
25
25
|
* Disable emitting messages and tool calls:
|
|
26
26
|
*
|
|
27
27
|
* ```typescript
|
|
28
|
-
* import {
|
|
28
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
29
|
+
* import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js/langgraph";
|
|
29
30
|
*
|
|
30
|
-
* config
|
|
31
|
-
* config,
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
31
|
+
* function customize(config: RunnableConfig) {
|
|
32
|
+
* return copilotkitCustomizeConfig(config, {
|
|
33
|
+
* emitMessages: false,
|
|
34
|
+
* emitToolCalls: false,
|
|
35
|
+
* });
|
|
36
|
+
* }
|
|
35
37
|
* ```
|
|
36
38
|
*
|
|
37
39
|
* To emit a tool call as streaming LangGraph state, pass the destination key in state,
|
|
@@ -39,18 +41,20 @@ import type { OptionsConfig } from "./types";
|
|
|
39
41
|
* all arguments are emitted under the state key.)
|
|
40
42
|
*
|
|
41
43
|
* ```typescript
|
|
42
|
-
* import {
|
|
44
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
45
|
+
* import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js/langgraph";
|
|
43
46
|
*
|
|
44
|
-
* config
|
|
45
|
-
* config,
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
47
|
+
* function customize(config: RunnableConfig) {
|
|
48
|
+
* return copilotkitCustomizeConfig(config, {
|
|
49
|
+
* emitIntermediateState: [
|
|
50
|
+
* {
|
|
51
|
+
* stateKey: "steps",
|
|
52
|
+
* tool: "SearchTool",
|
|
53
|
+
* toolArgument: "steps",
|
|
54
|
+
* },
|
|
55
|
+
* ],
|
|
56
|
+
* });
|
|
57
|
+
* }
|
|
54
58
|
* ```
|
|
55
59
|
*/
|
|
56
60
|
export function copilotkitCustomizeConfig(
|
|
@@ -159,18 +163,23 @@ export function copilotkitCustomizeConfig(
|
|
|
159
163
|
}
|
|
160
164
|
}
|
|
161
165
|
/**
|
|
162
|
-
* Exits the current agent after the run completes. Calling
|
|
166
|
+
* Exits the current agent after the run completes. Calling copilotkitExit() will
|
|
163
167
|
* not immediately stop the agent. Instead, it signals to CopilotKit to stop the agent after
|
|
164
168
|
* the run completes.
|
|
165
169
|
*
|
|
166
170
|
* ### Examples
|
|
167
171
|
*
|
|
168
172
|
* ```typescript
|
|
169
|
-
* import {
|
|
173
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
174
|
+
* import type { MessagesAnnotation } from "@langchain/langgraph";
|
|
175
|
+
* import { copilotkitExit } from "@copilotkit/sdk-js/langgraph";
|
|
176
|
+
*
|
|
177
|
+
* type AgentState = typeof MessagesAnnotation.State;
|
|
170
178
|
*
|
|
171
|
-
* async function myNode(state:
|
|
172
|
-
* await copilotkitExit(config)
|
|
173
|
-
* return state
|
|
179
|
+
* async function myNode(state: AgentState, config: RunnableConfig) {
|
|
180
|
+
* await copilotkitExit(config);
|
|
181
|
+
* return state;
|
|
182
|
+
* }
|
|
174
183
|
* ```
|
|
175
184
|
*/
|
|
176
185
|
export async function copilotkitExit(
|
|
@@ -200,11 +209,16 @@ export async function copilotkitExit(
|
|
|
200
209
|
* ### Examples
|
|
201
210
|
*
|
|
202
211
|
* ```typescript
|
|
203
|
-
* import {
|
|
212
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
213
|
+
* import { copilotkitEmitState } from "@copilotkit/sdk-js/langgraph";
|
|
214
|
+
*
|
|
215
|
+
* declare function someLongRunningOperation(step: number): Promise<void>;
|
|
204
216
|
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
217
|
+
* async function myNode(config: RunnableConfig) {
|
|
218
|
+
* for (let i = 0; i < 10; i++) {
|
|
219
|
+
* await someLongRunningOperation(i);
|
|
220
|
+
* await copilotkitEmitState(config, { progress: i });
|
|
221
|
+
* }
|
|
208
222
|
* }
|
|
209
223
|
* ```
|
|
210
224
|
*/
|
|
@@ -249,14 +263,18 @@ export async function copilotkitEmitState(
|
|
|
249
263
|
* ### Examples
|
|
250
264
|
*
|
|
251
265
|
* ```typescript
|
|
252
|
-
* import {
|
|
266
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
267
|
+
* import { AIMessage } from "@langchain/core/messages";
|
|
268
|
+
* import { copilotkitEmitMessage } from "@copilotkit/sdk-js/langgraph";
|
|
253
269
|
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
270
|
+
* async function myNode(config: RunnableConfig) {
|
|
271
|
+
* const message = "Step 1 of 10 complete";
|
|
272
|
+
* await copilotkitEmitMessage(config, message);
|
|
256
273
|
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
274
|
+
* // Return the message from the node
|
|
275
|
+
* return {
|
|
276
|
+
* messages: [new AIMessage({ content: message })],
|
|
277
|
+
* };
|
|
260
278
|
* }
|
|
261
279
|
* ```
|
|
262
280
|
*/
|
|
@@ -300,15 +318,18 @@ export async function copilotkitEmitMessage(
|
|
|
300
318
|
* ### Examples
|
|
301
319
|
*
|
|
302
320
|
* ```typescript
|
|
303
|
-
* import {
|
|
321
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
322
|
+
* import { copilotkitEmitToolCall } from "@copilotkit/sdk-js/langgraph";
|
|
304
323
|
*
|
|
305
|
-
*
|
|
324
|
+
* async function myNode(config: RunnableConfig) {
|
|
325
|
+
* const autoId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 });
|
|
306
326
|
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
327
|
+
* // With a custom ID for correlation/idempotency:
|
|
328
|
+
* const customId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 }, { toolCallId: "my-custom-id" });
|
|
329
|
+
* }
|
|
309
330
|
* ```
|
|
310
331
|
*
|
|
311
|
-
* @returns The tool call ID used for the emitted call
|
|
332
|
+
* @returns The tool call ID used for the emitted call. It equals `options.toolCallId`
|
|
312
333
|
* when provided, otherwise a randomly generated ID.
|
|
313
334
|
*/
|
|
314
335
|
export async function copilotkitEmitToolCall(
|
|
@@ -446,7 +467,7 @@ export function convertActionToDynamicStructuredTool(
|
|
|
446
467
|
* ### Examples
|
|
447
468
|
*
|
|
448
469
|
* ```typescript
|
|
449
|
-
* import { convertActionsToDynamicStructuredTools } from "@copilotkit/sdk-js";
|
|
470
|
+
* import { convertActionsToDynamicStructuredTools } from "@copilotkit/sdk-js/langgraph";
|
|
450
471
|
*
|
|
451
472
|
* const tools = convertActionsToDynamicStructuredTools(state.copilotkit.actions);
|
|
452
473
|
* ```
|
package/tsconfig.json
CHANGED