@copilotkit/sdk-js 2.0.0-next.1 → 2.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +0 -1
  3. package/dist/_virtual/_rolldown/runtime.cjs +29 -0
  4. package/dist/header-propagation.cjs +37 -0
  5. package/dist/header-propagation.cjs.map +1 -0
  6. package/dist/header-propagation.d.cts +16 -0
  7. package/dist/header-propagation.d.cts.map +1 -0
  8. package/dist/header-propagation.d.mts +16 -0
  9. package/dist/header-propagation.d.mts.map +1 -0
  10. package/dist/header-propagation.mjs +35 -0
  11. package/dist/header-propagation.mjs.map +1 -0
  12. package/dist/index.cjs +5 -0
  13. package/dist/index.d.cts +18 -0
  14. package/dist/index.d.mts +18 -0
  15. package/dist/index.mjs +3 -1
  16. package/dist/langchain.cjs +21 -0
  17. package/dist/langchain.cjs.map +1 -0
  18. package/dist/langchain.d.cts +139 -0
  19. package/dist/langchain.d.mts +140 -0
  20. package/dist/langchain.mjs +7 -23
  21. package/dist/langchain.mjs.map +1 -1
  22. package/dist/langgraph/middleware.cjs +433 -0
  23. package/dist/langgraph/middleware.cjs.map +1 -0
  24. package/dist/langgraph/middleware.d.cts +86 -0
  25. package/dist/langgraph/middleware.d.cts.map +1 -0
  26. package/dist/langgraph/middleware.d.mts +86 -0
  27. package/dist/langgraph/middleware.d.mts.map +1 -0
  28. package/dist/langgraph/middleware.mjs +429 -0
  29. package/dist/langgraph/middleware.mjs.map +1 -0
  30. package/dist/langgraph/state-schema.cjs +37 -0
  31. package/dist/langgraph/state-schema.cjs.map +1 -0
  32. package/dist/langgraph/state-schema.d.cts +118 -0
  33. package/dist/langgraph/state-schema.d.cts.map +1 -0
  34. package/dist/langgraph/state-schema.d.mts +118 -0
  35. package/dist/langgraph/state-schema.d.mts.map +1 -0
  36. package/dist/langgraph/state-schema.mjs +36 -0
  37. package/dist/langgraph/state-schema.mjs.map +1 -0
  38. package/dist/langgraph/types.cjs +60 -0
  39. package/dist/langgraph/types.cjs.map +1 -0
  40. package/dist/langgraph/types.d.cts +335 -0
  41. package/dist/langgraph/types.d.cts.map +1 -0
  42. package/dist/langgraph/types.d.mts +335 -0
  43. package/dist/langgraph/types.d.mts.map +1 -0
  44. package/dist/langgraph/types.mjs +57 -0
  45. package/dist/langgraph/types.mjs.map +1 -0
  46. package/dist/langgraph/utils.cjs +297 -0
  47. package/dist/langgraph/utils.cjs.map +1 -0
  48. package/dist/langgraph/utils.d.cts +209 -0
  49. package/dist/langgraph/utils.d.cts.map +1 -0
  50. package/dist/langgraph/utils.d.mts +209 -0
  51. package/dist/langgraph/utils.d.mts.map +1 -0
  52. package/dist/langgraph/utils.mjs +289 -0
  53. package/dist/langgraph/utils.mjs.map +1 -0
  54. package/dist/langgraph-middlewares.cjs +22 -0
  55. package/dist/langgraph-middlewares.d.cts +30 -0
  56. package/dist/langgraph-middlewares.d.mts +30 -0
  57. package/dist/langgraph-middlewares.mjs +5 -0
  58. package/dist/langgraph.cjs +21 -0
  59. package/dist/langgraph.d.cts +5 -0
  60. package/dist/langgraph.d.mts +5 -0
  61. package/dist/langgraph.mjs +6 -27
  62. package/package.json +50 -47
  63. package/src/__tests__/error-handling.test.ts +54 -20
  64. package/src/__tests__/header-propagation.test.ts +131 -0
  65. package/src/header-propagation.ts +61 -0
  66. package/src/index.ts +41 -0
  67. package/src/langchain.ts +168 -8
  68. package/src/langgraph/__tests__/middleware.a2ui-params.test.ts +139 -0
  69. package/src/langgraph/__tests__/middleware.native-a2ui.test.ts +118 -0
  70. package/src/langgraph/__tests__/middleware.properties.test.ts +357 -0
  71. package/src/langgraph/__tests__/middleware.test.ts +1141 -0
  72. package/src/langgraph/__tests__/state-schema.test.ts +85 -0
  73. package/src/langgraph/__tests__/utils.test.ts +363 -0
  74. package/src/langgraph/index.ts +1 -0
  75. package/src/langgraph/middleware.ts +698 -95
  76. package/src/langgraph/state-schema.ts +34 -0
  77. package/src/langgraph/types.ts +57 -0
  78. package/src/langgraph/utils.ts +76 -19
  79. package/src/langgraph-middlewares.ts +55 -0
  80. package/tsconfig.json +11 -2
  81. package/tsdown.config.ts +18 -0
  82. package/vitest.config.mjs +11 -0
  83. package/CHANGELOG.md +0 -2169
  84. package/dist/chunk-JLGOQLGS.mjs +0 -433
  85. package/dist/chunk-JLGOQLGS.mjs.map +0 -1
  86. package/dist/index.d.ts +0 -2
  87. package/dist/index.js +0 -1
  88. package/dist/index.js.map +0 -1
  89. package/dist/index.mjs.map +0 -1
  90. package/dist/langchain-2645763d.d.ts +0 -427
  91. package/dist/langchain.d.ts +0 -6
  92. package/dist/langchain.js +0 -284
  93. package/dist/langchain.js.map +0 -1
  94. package/dist/langgraph/index.d.ts +0 -11
  95. package/dist/langgraph.js +0 -476
  96. package/dist/langgraph.js.map +0 -1
  97. package/dist/langgraph.mjs.map +0 -1
  98. package/jest.config.js +0 -5
  99. package/tsup.config.ts +0 -23
@@ -0,0 +1,85 @@
1
+ import { StateGraph, END, START, StateSchema } from "@langchain/langgraph";
2
+ import { HumanMessage } from "@langchain/core/messages";
3
+ import * as z from "zod";
4
+
5
+ import { CopilotKitStateSchema } from "../state-schema";
6
+ import { CopilotKitPropertiesSchema } from "../types";
7
+
8
+ describe("CopilotKitStateSchema", () => {
9
+ it("exposes `copilotkit` and `messages` fields for composition", () => {
10
+ expect(Object.keys(CopilotKitStateSchema.fields).sort()).toEqual([
11
+ "copilotkit",
12
+ "messages",
13
+ ]);
14
+ });
15
+
16
+ it("can be used directly as a StateGraph schema", async () => {
17
+ const graph = new StateGraph(CopilotKitStateSchema)
18
+ .addNode("echo", (state) => ({ messages: state.messages }))
19
+ .addEdge(START, "echo")
20
+ .addEdge("echo", END)
21
+ .compile();
22
+
23
+ const result = await graph.invoke({
24
+ messages: [new HumanMessage("hello")],
25
+ });
26
+
27
+ expect(result.messages).toHaveLength(1);
28
+ expect(result.messages[0].content).toBe("hello");
29
+ });
30
+
31
+ it("can be extended with user fields via spread", async () => {
32
+ const AgentStateSchema = new StateSchema({
33
+ language: z.enum(["english", "spanish"]).default("english"),
34
+ ...CopilotKitStateSchema.fields,
35
+ });
36
+
37
+ const graph = new StateGraph(AgentStateSchema)
38
+ .addNode("chat", (state) => ({ language: state.language }))
39
+ .addEdge(START, "chat")
40
+ .addEdge("chat", END)
41
+ .compile();
42
+
43
+ const result = await graph.invoke({
44
+ language: "spanish",
45
+ messages: [new HumanMessage("hola")],
46
+ });
47
+
48
+ expect(result.language).toBe("spanish");
49
+ expect(result.messages).toHaveLength(1);
50
+ });
51
+
52
+ it("preserves copilotkit runtime state across nodes", async () => {
53
+ const graph = new StateGraph(CopilotKitStateSchema)
54
+ .addNode("noop", (state) => ({ copilotkit: state.copilotkit }))
55
+ .addEdge(START, "noop")
56
+ .addEdge("noop", END)
57
+ .compile();
58
+
59
+ const result = await graph.invoke({
60
+ copilotkit: {
61
+ actions: [{ name: "doThing" }],
62
+ context: [{ description: "user", value: "alice" }],
63
+ interceptedToolCalls: [],
64
+ originalAIMessageId: "msg-1",
65
+ },
66
+ messages: [],
67
+ });
68
+
69
+ expect(result.copilotkit.actions).toEqual([{ name: "doThing" }]);
70
+ expect(result.copilotkit.context).toEqual([
71
+ { description: "user", value: "alice" },
72
+ ]);
73
+ expect(result.copilotkit.originalAIMessageId).toBe("msg-1");
74
+ });
75
+
76
+ it("exposes a standard schema shape for copilotkit properties", () => {
77
+ expect(CopilotKitPropertiesSchema["~standard"].version).toBe(1);
78
+ expect(CopilotKitPropertiesSchema["~standard"].vendor).toBe(
79
+ "@copilotkit/sdk-js",
80
+ );
81
+ expect(
82
+ typeof CopilotKitPropertiesSchema["~standard"].jsonSchema.input,
83
+ ).toBe("function");
84
+ });
85
+ });
@@ -0,0 +1,363 @@
1
+ import { vi } from "vitest";
2
+ import { dispatchCustomEvent } from "@langchain/core/callbacks/dispatch";
3
+ import {
4
+ copilotkitCustomizeConfig,
5
+ copilotkitEmitToolCall,
6
+ convertActionsToDynamicStructuredTools,
7
+ convertActionToDynamicStructuredTool,
8
+ } from "../utils";
9
+
10
+ vi.mock("@langchain/core/callbacks/dispatch", () => ({
11
+ dispatchCustomEvent: vi.fn().mockResolvedValue(undefined),
12
+ }));
13
+
14
+ describe("copilotkitCustomizeConfig", () => {
15
+ it("returns config unchanged when no options provided", () => {
16
+ const baseConfig = { metadata: { existing: true } };
17
+ const result = copilotkitCustomizeConfig(baseConfig);
18
+ expect(result.metadata).toEqual({ existing: true });
19
+ });
20
+
21
+ it("returns config unchanged when options is empty object", () => {
22
+ const baseConfig = { metadata: {} };
23
+ const result = copilotkitCustomizeConfig(baseConfig, {});
24
+ expect(result.metadata).toEqual({});
25
+ });
26
+
27
+ it("sets emit-messages metadata flag to false", () => {
28
+ const result = copilotkitCustomizeConfig({}, { emitMessages: false });
29
+ expect(result.metadata!["copilotkit:emit-messages"]).toBe(false);
30
+ });
31
+
32
+ it("sets emit-messages metadata flag to true", () => {
33
+ const result = copilotkitCustomizeConfig({}, { emitMessages: true });
34
+ expect(result.metadata!["copilotkit:emit-messages"]).toBe(true);
35
+ });
36
+
37
+ it("sets emit-tool-calls metadata flag to false", () => {
38
+ const result = copilotkitCustomizeConfig({}, { emitToolCalls: false });
39
+ expect(result.metadata!["copilotkit:emit-tool-calls"]).toBe(false);
40
+ });
41
+
42
+ it("sets emit-tool-calls to a specific tool name string", () => {
43
+ const result = copilotkitCustomizeConfig(
44
+ {},
45
+ { emitToolCalls: "SearchTool" },
46
+ );
47
+ expect(result.metadata!["copilotkit:emit-tool-calls"]).toBe("SearchTool");
48
+ });
49
+
50
+ it("sets emit-tool-calls to an array of tool names", () => {
51
+ const result = copilotkitCustomizeConfig(
52
+ {},
53
+ { emitToolCalls: ["SearchTool", "FetchTool"] },
54
+ );
55
+ expect(result.metadata!["copilotkit:emit-tool-calls"]).toEqual([
56
+ "SearchTool",
57
+ "FetchTool",
58
+ ]);
59
+ });
60
+
61
+ it("sets both emit flags together", () => {
62
+ const result = copilotkitCustomizeConfig(
63
+ {},
64
+ {
65
+ emitMessages: false,
66
+ emitToolCalls: false,
67
+ },
68
+ );
69
+ expect(result.metadata!["copilotkit:emit-messages"]).toBe(false);
70
+ expect(result.metadata!["copilotkit:emit-tool-calls"]).toBe(false);
71
+ });
72
+
73
+ it("emitAll sets both messages and tool-calls to true", () => {
74
+ const result = copilotkitCustomizeConfig({}, { emitAll: true });
75
+ expect(result.metadata!["copilotkit:emit-messages"]).toBe(true);
76
+ expect(result.metadata!["copilotkit:emit-tool-calls"]).toBe(true);
77
+ });
78
+
79
+ it("converts emitIntermediateState to snake_case in metadata", () => {
80
+ const result = copilotkitCustomizeConfig(
81
+ {},
82
+ {
83
+ emitIntermediateState: [
84
+ { stateKey: "steps", tool: "SearchTool", toolArgument: "steps" },
85
+ ],
86
+ },
87
+ );
88
+ const intermediateState =
89
+ result.metadata!["copilotkit:emit-intermediate-state"];
90
+ expect(intermediateState).toHaveLength(1);
91
+ expect(intermediateState[0]).toEqual({
92
+ state_key: "steps",
93
+ tool: "SearchTool",
94
+ tool_argument: "steps",
95
+ });
96
+ });
97
+
98
+ it("handles emitIntermediateState without toolArgument", () => {
99
+ const result = copilotkitCustomizeConfig(
100
+ {},
101
+ {
102
+ emitIntermediateState: [{ stateKey: "output", tool: "WriteTool" }],
103
+ },
104
+ );
105
+ const intermediateState =
106
+ result.metadata!["copilotkit:emit-intermediate-state"];
107
+ expect(intermediateState[0]).toEqual({
108
+ state_key: "output",
109
+ tool: "WriteTool",
110
+ tool_argument: undefined,
111
+ });
112
+ });
113
+
114
+ it("throws when emitIntermediateState item is missing stateKey", () => {
115
+ expect(() => {
116
+ copilotkitCustomizeConfig(
117
+ {},
118
+ {
119
+ emitIntermediateState: [{ tool: "SearchTool" } as any],
120
+ },
121
+ );
122
+ }).toThrow("stateKey");
123
+ });
124
+
125
+ it("throws when emitIntermediateState item is missing tool", () => {
126
+ expect(() => {
127
+ copilotkitCustomizeConfig(
128
+ {},
129
+ {
130
+ emitIntermediateState: [{ stateKey: "steps" } as any],
131
+ },
132
+ );
133
+ }).toThrow("tool");
134
+ });
135
+
136
+ it("preserves existing metadata from baseConfig", () => {
137
+ const result = copilotkitCustomizeConfig(
138
+ { metadata: { "custom-key": "custom-value" } },
139
+ { emitMessages: false },
140
+ );
141
+ expect(result.metadata!["custom-key"]).toBe("custom-value");
142
+ expect(result.metadata!["copilotkit:emit-messages"]).toBe(false);
143
+ });
144
+
145
+ it("handles null/undefined baseConfig gracefully", () => {
146
+ const result = copilotkitCustomizeConfig(null as any, {
147
+ emitMessages: false,
148
+ });
149
+ expect(result.metadata!["copilotkit:emit-messages"]).toBe(false);
150
+ });
151
+ });
152
+
153
+ describe("convertActionToDynamicStructuredTool", () => {
154
+ it("converts a valid action to DynamicStructuredTool", () => {
155
+ const action = {
156
+ name: "myTool",
157
+ description: "A test tool",
158
+ parameters: {
159
+ type: "object",
160
+ properties: {
161
+ query: { type: "string" },
162
+ },
163
+ },
164
+ };
165
+ const tool = convertActionToDynamicStructuredTool(action);
166
+ expect(tool.name).toBe("myTool");
167
+ expect(tool.description).toBe("A test tool");
168
+ });
169
+
170
+ it("throws when actionInput is null", () => {
171
+ expect(() => convertActionToDynamicStructuredTool(null)).toThrow(
172
+ "Action input is required",
173
+ );
174
+ });
175
+
176
+ it("throws when name is missing", () => {
177
+ expect(() =>
178
+ convertActionToDynamicStructuredTool({
179
+ description: "test",
180
+ parameters: {},
181
+ }),
182
+ ).toThrow("name");
183
+ });
184
+
185
+ it("throws when description is missing", () => {
186
+ expect(() =>
187
+ convertActionToDynamicStructuredTool({ name: "test", parameters: {} }),
188
+ ).toThrow("description");
189
+ });
190
+
191
+ it("throws when parameters is missing", () => {
192
+ expect(() =>
193
+ convertActionToDynamicStructuredTool({
194
+ name: "test",
195
+ description: "test",
196
+ }),
197
+ ).toThrow("parameters");
198
+ });
199
+ });
200
+
201
+ describe("convertActionsToDynamicStructuredTools", () => {
202
+ it("converts multiple actions", () => {
203
+ const actions = [
204
+ {
205
+ name: "tool1",
206
+ description: "First tool",
207
+ parameters: { type: "object", properties: {} },
208
+ },
209
+ {
210
+ name: "tool2",
211
+ description: "Second tool",
212
+ parameters: { type: "object", properties: {} },
213
+ },
214
+ ];
215
+ const tools = convertActionsToDynamicStructuredTools(actions);
216
+ expect(tools).toHaveLength(2);
217
+ expect(tools[0].name).toBe("tool1");
218
+ expect(tools[1].name).toBe("tool2");
219
+ });
220
+
221
+ it("returns empty array for empty input", () => {
222
+ const tools = convertActionsToDynamicStructuredTools([]);
223
+ expect(tools).toEqual([]);
224
+ });
225
+
226
+ it("handles { type: 'function', function: {...} } format", () => {
227
+ const actions = [
228
+ {
229
+ type: "function",
230
+ function: {
231
+ name: "wrappedTool",
232
+ description: "A wrapped tool",
233
+ parameters: { type: "object", properties: {} },
234
+ },
235
+ },
236
+ ];
237
+ const tools = convertActionsToDynamicStructuredTools(actions);
238
+ expect(tools).toHaveLength(1);
239
+ expect(tools[0].name).toBe("wrappedTool");
240
+ });
241
+
242
+ it("throws when input is not an array", () => {
243
+ expect(() =>
244
+ convertActionsToDynamicStructuredTools("not-array" as any),
245
+ ).toThrow("Actions must be an array");
246
+ });
247
+
248
+ it("wraps individual action errors with index info", () => {
249
+ const actions = [
250
+ {
251
+ name: "goodTool",
252
+ description: "works",
253
+ parameters: { type: "object", properties: {} },
254
+ },
255
+ { name: "badTool" }, // missing description and parameters
256
+ ];
257
+ expect(() => convertActionsToDynamicStructuredTools(actions)).toThrow(
258
+ "index 1",
259
+ );
260
+ });
261
+ });
262
+
263
+ describe("copilotkitEmitToolCall", () => {
264
+ const mockConfig = { metadata: {} } as any;
265
+ const mockedDispatch = vi.mocked(dispatchCustomEvent);
266
+
267
+ beforeEach(() => {
268
+ mockedDispatch.mockClear();
269
+ });
270
+
271
+ it("returns a generated id when no id is provided", async () => {
272
+ const result = await copilotkitEmitToolCall(mockConfig, "SearchTool", {
273
+ steps: 10,
274
+ });
275
+
276
+ expect(typeof result).toBe("string");
277
+ expect(result.length).toBeGreaterThan(0);
278
+ expect(mockedDispatch).toHaveBeenCalledWith(
279
+ "copilotkit_manually_emit_tool_call",
280
+ { name: "SearchTool", args: { steps: 10 }, id: result },
281
+ mockConfig,
282
+ );
283
+ });
284
+
285
+ it("uses caller-provided toolCallId verbatim", async () => {
286
+ const result = await copilotkitEmitToolCall(
287
+ mockConfig,
288
+ "SearchTool",
289
+ { steps: 10 },
290
+ { toolCallId: "custom-abc" },
291
+ );
292
+
293
+ expect(result).toBe("custom-abc");
294
+ expect(mockedDispatch).toHaveBeenCalledWith(
295
+ "copilotkit_manually_emit_tool_call",
296
+ { name: "SearchTool", args: { steps: 10 }, id: "custom-abc" },
297
+ mockConfig,
298
+ );
299
+ });
300
+
301
+ it("treats undefined options the same as omitted", async () => {
302
+ const result = await copilotkitEmitToolCall(
303
+ mockConfig,
304
+ "SearchTool",
305
+ {},
306
+ undefined,
307
+ );
308
+
309
+ expect(typeof result).toBe("string");
310
+ expect(result.length).toBeGreaterThan(0);
311
+ expect(mockedDispatch).toHaveBeenCalledWith(
312
+ "copilotkit_manually_emit_tool_call",
313
+ { name: "SearchTool", args: {}, id: result },
314
+ mockConfig,
315
+ );
316
+ });
317
+
318
+ it("throws on empty string toolCallId", async () => {
319
+ await expect(
320
+ copilotkitEmitToolCall(mockConfig, "SearchTool", {}, { toolCallId: "" }),
321
+ ).rejects.toThrow("non-empty string");
322
+ });
323
+
324
+ it("throws on whitespace-only toolCallId", async () => {
325
+ await expect(
326
+ copilotkitEmitToolCall(
327
+ mockConfig,
328
+ "SearchTool",
329
+ {},
330
+ { toolCallId: " " },
331
+ ),
332
+ ).rejects.toThrow("non-empty string");
333
+ });
334
+
335
+ it("throws on whitespace-only name", async () => {
336
+ await expect(copilotkitEmitToolCall(mockConfig, " ", {})).rejects.toThrow(
337
+ "non-empty string",
338
+ );
339
+ });
340
+
341
+ it("throws on undefined args", async () => {
342
+ await expect(
343
+ copilotkitEmitToolCall(mockConfig, "SearchTool", undefined),
344
+ ).rejects.toThrow("required");
345
+ });
346
+
347
+ it("throws on non-JSON-serializable args", async () => {
348
+ const circular: any = {};
349
+ circular.self = circular;
350
+ await expect(
351
+ copilotkitEmitToolCall(mockConfig, "SearchTool", circular),
352
+ ).rejects.toThrow("not JSON-serializable");
353
+ });
354
+
355
+ it("propagates dispatch errors with tool-call context", async () => {
356
+ mockedDispatch.mockRejectedValueOnce(new Error("transport closed"));
357
+ await expect(
358
+ copilotkitEmitToolCall(mockConfig, "SearchTool", {}),
359
+ ).rejects.toThrow(
360
+ /copilotkitEmitToolCall dispatch failed.*SearchTool.*transport closed/,
361
+ );
362
+ });
363
+ });
@@ -1,3 +1,4 @@
1
1
  export * from "./types";
2
2
  export * from "./utils";
3
3
  export * from "./middleware";
4
+ export * from "./state-schema";