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

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,5 @@
1
+ import { CopilotKitProperties, CopilotKitPropertiesAnnotation, CopilotKitPropertiesSchema, CopilotKitState, CopilotKitStateAnnotation, IntermediateStateConfig, OptionsConfig, StandardSerializableSchema } from "./langgraph/types.mjs";
2
+ import { convertActionToDynamicStructuredTool, convertActionsToDynamicStructuredTools, copilotKitInterrupt, copilotkitCustomizeConfig, copilotkitEmitMessage, copilotkitEmitState, copilotkitEmitToolCall, copilotkitExit } from "./langgraph/utils.mjs";
3
+ import { ExposeStateOption, copilotkitMiddleware, createCopilotkitMiddleware, zodState } from "./langgraph/middleware.mjs";
4
+ import { CopilotKitSchemaState, CopilotKitSchemaUpdate, CopilotKitStateSchema } from "./langgraph/state-schema.mjs";
5
+ export { CopilotKitProperties, CopilotKitPropertiesAnnotation, CopilotKitPropertiesSchema, CopilotKitSchemaState, CopilotKitSchemaUpdate, CopilotKitState, CopilotKitStateAnnotation, CopilotKitStateSchema, ExposeStateOption, IntermediateStateConfig, OptionsConfig, StandardSerializableSchema, convertActionToDynamicStructuredTool, convertActionsToDynamicStructuredTools, copilotKitInterrupt, copilotkitCustomizeConfig, copilotkitEmitMessage, copilotkitEmitState, copilotkitEmitToolCall, copilotkitExit, copilotkitMiddleware, createCopilotkitMiddleware, zodState };
@@ -1,27 +1,6 @@
1
- import {
2
- CopilotKitPropertiesAnnotation,
3
- CopilotKitStateAnnotation,
4
- convertActionToDynamicStructuredTool,
5
- convertActionsToDynamicStructuredTools,
6
- copilotKitInterrupt,
7
- copilotkitCustomizeConfig,
8
- copilotkitEmitMessage,
9
- copilotkitEmitState,
10
- copilotkitEmitToolCall,
11
- copilotkitExit,
12
- copilotkitMiddleware
13
- } from "./chunk-JLGOQLGS.mjs";
14
- export {
15
- CopilotKitPropertiesAnnotation,
16
- CopilotKitStateAnnotation,
17
- convertActionToDynamicStructuredTool,
18
- convertActionsToDynamicStructuredTools,
19
- copilotKitInterrupt,
20
- copilotkitCustomizeConfig,
21
- copilotkitEmitMessage,
22
- copilotkitEmitState,
23
- copilotkitEmitToolCall,
24
- copilotkitExit,
25
- copilotkitMiddleware
26
- };
27
- //# sourceMappingURL=langgraph.mjs.map
1
+ import { CopilotKitPropertiesAnnotation, CopilotKitPropertiesSchema, CopilotKitStateAnnotation } from "./langgraph/types.mjs";
2
+ import { convertActionToDynamicStructuredTool, convertActionsToDynamicStructuredTools, copilotKitInterrupt, copilotkitCustomizeConfig, copilotkitEmitMessage, copilotkitEmitState, copilotkitEmitToolCall, copilotkitExit } from "./langgraph/utils.mjs";
3
+ import { copilotkitMiddleware, createCopilotkitMiddleware, zodState } from "./langgraph/middleware.mjs";
4
+ import { CopilotKitStateSchema } from "./langgraph/state-schema.mjs";
5
+
6
+ export { CopilotKitPropertiesAnnotation, CopilotKitPropertiesSchema, CopilotKitStateAnnotation, CopilotKitStateSchema, convertActionToDynamicStructuredTool, convertActionsToDynamicStructuredTools, copilotKitInterrupt, copilotkitCustomizeConfig, copilotkitEmitMessage, copilotkitEmitState, copilotkitEmitToolCall, copilotkitExit, copilotkitMiddleware, createCopilotkitMiddleware, zodState };
package/package.json CHANGED
@@ -1,86 +1,89 @@
1
1
  {
2
2
  "name": "@copilotkit/sdk-js",
3
+ "version": "2.0.0-rc.0",
3
4
  "private": false,
5
+ "keywords": [
6
+ "ai",
7
+ "assistant",
8
+ "automation",
9
+ "copilot",
10
+ "copilotkit",
11
+ "javascript",
12
+ "nextjs",
13
+ "nodejs",
14
+ "react",
15
+ "textarea"
16
+ ],
4
17
  "homepage": "https://github.com/CopilotKit/CopilotKit",
18
+ "license": "MIT",
5
19
  "repository": {
6
20
  "type": "git",
7
21
  "url": "https://github.com/CopilotKit/CopilotKit.git"
8
22
  },
9
- "publishConfig": {
10
- "access": "public"
11
- },
12
- "version": "2.0.0-next.1",
23
+ "type": "module",
13
24
  "sideEffects": false,
14
- "main": "./dist/index.js",
25
+ "main": "./dist/index.cjs",
15
26
  "module": "./dist/index.mjs",
27
+ "types": "./dist/index.d.cts",
16
28
  "exports": {
17
29
  ".": {
18
- "types": "./dist/index.d.ts",
19
- "require": "./dist/index.js",
20
- "import": "./dist/index.mjs"
30
+ "import": "./dist/index.mjs",
31
+ "require": "./dist/index.cjs"
21
32
  },
22
33
  "./langchain": {
23
- "types": "./dist/langchain.d.ts",
24
- "require": "./dist/langchain.js",
25
- "import": "./dist/langchain.mjs"
34
+ "import": "./dist/langchain.mjs",
35
+ "require": "./dist/langchain.cjs"
26
36
  },
27
37
  "./langgraph": {
28
- "types": "./dist/langgraph.d.ts",
29
- "require": "./dist/langgraph.js",
30
- "import": "./dist/langgraph.mjs"
31
- }
38
+ "import": "./dist/langgraph.mjs",
39
+ "require": "./dist/langgraph.cjs"
40
+ },
41
+ "./langgraph-middlewares": {
42
+ "import": "./dist/langgraph-middlewares.mjs",
43
+ "require": "./dist/langgraph-middlewares.cjs"
44
+ },
45
+ "./package.json": "./package.json"
46
+ },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "dependencies": {
51
+ "@ag-ui/langgraph": "0.0.44",
52
+ "@copilotkit/shared": "2.0.0-rc.0",
53
+ "@standard-schema/spec": "^1.0.0"
32
54
  },
33
- "types": "./dist/index.d.ts",
34
- "license": "MIT",
35
55
  "devDependencies": {
36
- "@langchain/core": "^1.1.8",
37
- "@langchain/langgraph": "^1.0.7",
56
+ "@langchain/core": "^1.1.41",
57
+ "@langchain/langgraph": "^1.2.9",
38
58
  "@swc/core": "1.5.28",
39
59
  "@types/express": "^4.17.21",
40
- "@types/jest": "^29.5.4",
41
60
  "@types/node": "^18.11.17",
42
61
  "@whatwg-node/server": "^0.9.34",
43
62
  "eslint": "^8.56.0",
44
- "jest": "^29.6.4",
45
- "langchain": "^1.2.3",
63
+ "langchain": "^1.3.4",
46
64
  "nodemon": "^3.1.3",
47
- "ts-jest": "^29.1.1",
48
65
  "ts-node": "^10.9.2",
49
- "tsup": "^6.7.0",
66
+ "tsdown": "^0.20.3",
50
67
  "typescript": "^5.2.3",
51
- "eslint-config-custom": "1.4.6",
52
- "tsconfig": "1.4.6"
53
- },
54
- "dependencies": {
55
- "@copilotkit/shared": "2.0.0-next.1"
68
+ "vitest": "^3.2.4",
69
+ "tsconfig": "1.4.12"
56
70
  },
57
71
  "peerDependencies": {
58
- "@langchain/community": "^0.0.53",
59
72
  "@langchain/core": ">=0.4.0 <2.0.0",
60
73
  "@langchain/langgraph": ">=0.4.0 <2.0.0",
61
74
  "langchain": ">=1.0.0",
62
75
  "typescript": "^5.2.3",
63
76
  "zod": "^3.23.3 || ^3.24.0 || ^3.25.0"
64
77
  },
65
- "keywords": [
66
- "copilotkit",
67
- "copilot",
68
- "react",
69
- "nextjs",
70
- "nodejs",
71
- "ai",
72
- "assistant",
73
- "javascript",
74
- "automation",
75
- "textarea"
76
- ],
77
78
  "scripts": {
78
- "build": "tsup --clean",
79
- "dev": "tsup --watch --no-splitting",
80
- "test": "jest --passWithNoTests",
79
+ "build": "tsdown && node ../../scripts/deprecations/v1-dist-notices.mjs --entrypoint sdk-js,sdk-js-langchain,sdk-js-langgraph-middlewares",
80
+ "dev": "tsdown --watch",
81
+ "test": "vitest run",
82
+ "test:watch": "vitest",
81
83
  "check-types": "tsc --noEmit",
82
- "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist && rm -rf .next",
83
84
  "link:global": "pnpm link --global",
84
- "unlink:global": "pnpm unlink --global"
85
+ "unlink:global": "pnpm unlink --global",
86
+ "publint": "publint .",
87
+ "attw": "attw --pack . --profile node16"
85
88
  }
86
89
  }
@@ -31,7 +31,9 @@ describe("SDK-JS Error Handling", () => {
31
31
 
32
32
  expect(() => {
33
33
  copilotKitInterrupt({ action: 123 as any });
34
- }).toThrow("Action must be a string when provided to copilotKitInterrupt");
34
+ }).toThrow(
35
+ "Action must be a string when provided to copilotKitInterrupt",
36
+ );
35
37
  });
36
38
 
37
39
  it("should throw CopilotKitMisuseError when message is not a string", () => {
@@ -41,7 +43,9 @@ describe("SDK-JS Error Handling", () => {
41
43
 
42
44
  expect(() => {
43
45
  copilotKitInterrupt({ message: 123 as any });
44
- }).toThrow("Message must be a string when provided to copilotKitInterrupt");
46
+ }).toThrow(
47
+ "Message must be a string when provided to copilotKitInterrupt",
48
+ );
45
49
  });
46
50
 
47
51
  it("should throw CopilotKitMisuseError when args is not an object", () => {
@@ -83,16 +87,24 @@ describe("SDK-JS Error Handling", () => {
83
87
 
84
88
  expect(() => {
85
89
  convertActionToDynamicStructuredTool({ name: "test" });
86
- }).toThrow("Action 'test' must have a valid 'description' property of type string");
90
+ }).toThrow(
91
+ "Action 'test' must have a valid 'description' property of type string",
92
+ );
87
93
  });
88
94
 
89
95
  it("should throw CopilotKitMisuseError when parameters is missing", () => {
90
96
  expect(() => {
91
- convertActionToDynamicStructuredTool({ name: "test", description: "test desc" });
97
+ convertActionToDynamicStructuredTool({
98
+ name: "test",
99
+ description: "test desc",
100
+ });
92
101
  }).toThrow(CopilotKitMisuseError);
93
102
 
94
103
  expect(() => {
95
- convertActionToDynamicStructuredTool({ name: "test", description: "test desc" });
104
+ convertActionToDynamicStructuredTool({
105
+ name: "test",
106
+ description: "test desc",
107
+ });
96
108
  }).toThrow("Action 'test' must have a 'parameters' property");
97
109
  });
98
110
  });
@@ -132,22 +144,36 @@ describe("SDK-JS Error Handling", () => {
132
144
 
133
145
  it("should throw CopilotKitMisuseError when emitIntermediateState is not an array", () => {
134
146
  expect(() => {
135
- copilotkitCustomizeConfig({}, { emitIntermediateState: "invalid" as any });
147
+ copilotkitCustomizeConfig(
148
+ {},
149
+ { emitIntermediateState: "invalid" as any },
150
+ );
136
151
  }).toThrow(CopilotKitMisuseError);
137
152
 
138
153
  expect(() => {
139
- copilotkitCustomizeConfig({}, { emitIntermediateState: "invalid" as any });
154
+ copilotkitCustomizeConfig(
155
+ {},
156
+ { emitIntermediateState: "invalid" as any },
157
+ );
140
158
  }).toThrow("emitIntermediateState must be an array when provided");
141
159
  });
142
160
 
143
161
  it("should throw CopilotKitMisuseError when emitIntermediateState item is invalid", () => {
144
162
  expect(() => {
145
- copilotkitCustomizeConfig({}, { emitIntermediateState: [{ invalidKey: "value" }] as any });
163
+ copilotkitCustomizeConfig(
164
+ {},
165
+ { emitIntermediateState: [{ invalidKey: "value" }] as any },
166
+ );
146
167
  }).toThrow(CopilotKitMisuseError);
147
168
 
148
169
  expect(() => {
149
- copilotkitCustomizeConfig({}, { emitIntermediateState: [{ invalidKey: "value" }] as any });
150
- }).toThrow("emitIntermediateState[0] must have a valid 'stateKey' string property");
170
+ copilotkitCustomizeConfig(
171
+ {},
172
+ { emitIntermediateState: [{ invalidKey: "value" }] as any },
173
+ );
174
+ }).toThrow(
175
+ "emitIntermediateState[0] must have a valid 'stateKey' string property",
176
+ );
151
177
  });
152
178
  });
153
179
 
@@ -155,14 +181,18 @@ describe("SDK-JS Error Handling", () => {
155
181
  const mockConfig = { metadata: {} };
156
182
 
157
183
  it("should throw CopilotKitMisuseError when config is missing for copilotkitExit", async () => {
158
- await expect(copilotkitExit(null as any)).rejects.toThrow(CopilotKitMisuseError);
184
+ await expect(copilotkitExit(null as any)).rejects.toThrow(
185
+ CopilotKitMisuseError,
186
+ );
159
187
  await expect(copilotkitExit(null as any)).rejects.toThrow(
160
188
  "LangGraph configuration is required for copilotkitExit",
161
189
  );
162
190
  });
163
191
 
164
192
  it("should throw CopilotKitMisuseError when config is missing for copilotkitEmitState", async () => {
165
- await expect(copilotkitEmitState(null as any, {})).rejects.toThrow(CopilotKitMisuseError);
193
+ await expect(copilotkitEmitState(null as any, {})).rejects.toThrow(
194
+ CopilotKitMisuseError,
195
+ );
166
196
  await expect(copilotkitEmitState(null as any, {})).rejects.toThrow(
167
197
  "LangGraph configuration is required for copilotkitEmitState",
168
198
  );
@@ -178,10 +208,12 @@ describe("SDK-JS Error Handling", () => {
178
208
  });
179
209
 
180
210
  it("should throw CopilotKitMisuseError when message is invalid for copilotkitEmitMessage", async () => {
181
- await expect(copilotkitEmitMessage(mockConfig, "" as any)).rejects.toThrow(
182
- CopilotKitMisuseError,
183
- );
184
- await expect(copilotkitEmitMessage(mockConfig, "" as any)).rejects.toThrow(
211
+ await expect(
212
+ copilotkitEmitMessage(mockConfig, "" as any),
213
+ ).rejects.toThrow(CopilotKitMisuseError);
214
+ await expect(
215
+ copilotkitEmitMessage(mockConfig, "" as any),
216
+ ).rejects.toThrow(
185
217
  "Message must be a non-empty string for copilotkitEmitMessage",
186
218
  );
187
219
  });
@@ -196,10 +228,12 @@ describe("SDK-JS Error Handling", () => {
196
228
  });
197
229
 
198
230
  it("should throw CopilotKitMisuseError when args is undefined for copilotkitEmitToolCall", async () => {
199
- await expect(copilotkitEmitToolCall(mockConfig, "testTool", undefined)).rejects.toThrow(
200
- CopilotKitMisuseError,
201
- );
202
- await expect(copilotkitEmitToolCall(mockConfig, "testTool", undefined)).rejects.toThrow(
231
+ await expect(
232
+ copilotkitEmitToolCall(mockConfig, "testTool", undefined),
233
+ ).rejects.toThrow(CopilotKitMisuseError);
234
+ await expect(
235
+ copilotkitEmitToolCall(mockConfig, "testTool", undefined),
236
+ ).rejects.toThrow(
203
237
  "Tool arguments are required for copilotkitEmitToolCall",
204
238
  );
205
239
  });
@@ -0,0 +1,131 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import {
3
+ withForwardedHeaders,
4
+ getForwardedHeaders,
5
+ } from "../header-propagation";
6
+
7
+ describe("header-propagation", () => {
8
+ describe("getForwardedHeaders", () => {
9
+ it("returns empty object when called outside withForwardedHeaders scope", () => {
10
+ const headers = getForwardedHeaders();
11
+ expect(headers).toEqual({});
12
+ });
13
+ });
14
+
15
+ describe("withForwardedHeaders", () => {
16
+ it("filters to only x-* prefixed headers", () => {
17
+ const input = {
18
+ "x-aimock-strict": "true",
19
+ "x-aimock-fixture": "demo",
20
+ "x-request-id": "req-123",
21
+ "x-custom-trace": "abc",
22
+ authorization: "Bearer secret",
23
+ "content-type": "application/json",
24
+ };
25
+
26
+ withForwardedHeaders(input, () => {
27
+ const forwarded = getForwardedHeaders();
28
+ expect(forwarded).toEqual({
29
+ "x-aimock-strict": "true",
30
+ "x-aimock-fixture": "demo",
31
+ "x-request-id": "req-123",
32
+ "x-custom-trace": "abc",
33
+ });
34
+ expect(forwarded).not.toHaveProperty("authorization");
35
+ expect(forwarded).not.toHaveProperty("content-type");
36
+ });
37
+ });
38
+
39
+ it("lowercases header keys", () => {
40
+ const input = {
41
+ "X-AIMock-Strict": "true",
42
+ "X-AIMOCK-FIXTURE": "demo",
43
+ };
44
+
45
+ withForwardedHeaders(input, () => {
46
+ const forwarded = getForwardedHeaders();
47
+ expect(forwarded).toEqual({
48
+ "x-aimock-strict": "true",
49
+ "x-aimock-fixture": "demo",
50
+ });
51
+ });
52
+ });
53
+
54
+ it("returns empty when no x-* headers present", () => {
55
+ const input = {
56
+ authorization: "Bearer token",
57
+ "content-type": "application/json",
58
+ };
59
+
60
+ withForwardedHeaders(input, () => {
61
+ const forwarded = getForwardedHeaders();
62
+ expect(forwarded).toEqual({});
63
+ });
64
+ });
65
+
66
+ it("returns the callback's return value", () => {
67
+ const result = withForwardedHeaders({}, () => 42);
68
+ expect(result).toBe(42);
69
+ });
70
+
71
+ it("restores empty state after scope exits", () => {
72
+ withForwardedHeaders({ "x-aimock-strict": "true" }, () => {
73
+ expect(getForwardedHeaders()).toEqual({ "x-aimock-strict": "true" });
74
+ });
75
+
76
+ // Outside the scope, should be empty again
77
+ expect(getForwardedHeaders()).toEqual({});
78
+ });
79
+ });
80
+
81
+ describe("AsyncLocalStorage isolation", () => {
82
+ it("maintains separate headers across concurrent requests", async () => {
83
+ const results: Record<string, string>[] = [];
84
+
85
+ const request1 = new Promise<void>((resolve) => {
86
+ withForwardedHeaders({ "x-aimock-strict": "true" }, () => {
87
+ // Simulate async work
88
+ setTimeout(() => {
89
+ results.push(getForwardedHeaders());
90
+ resolve();
91
+ }, 10);
92
+ });
93
+ });
94
+
95
+ const request2 = new Promise<void>((resolve) => {
96
+ withForwardedHeaders({ "x-aimock-fixture": "demo" }, () => {
97
+ // Simulate async work
98
+ setTimeout(() => {
99
+ results.push(getForwardedHeaders());
100
+ resolve();
101
+ }, 5);
102
+ });
103
+ });
104
+
105
+ await Promise.all([request1, request2]);
106
+
107
+ // Each request should have its own headers, regardless of timing
108
+ expect(results).toHaveLength(2);
109
+ const strictResult = results.find((r) => "x-aimock-strict" in r);
110
+ const fixtureResult = results.find((r) => "x-aimock-fixture" in r);
111
+ expect(strictResult).toEqual({ "x-aimock-strict": "true" });
112
+ expect(fixtureResult).toEqual({ "x-aimock-fixture": "demo" });
113
+ });
114
+
115
+ it("works with nested withForwardedHeaders calls", () => {
116
+ withForwardedHeaders({ "x-aimock-strict": "true" }, () => {
117
+ expect(getForwardedHeaders()).toEqual({ "x-aimock-strict": "true" });
118
+
119
+ // Inner scope overrides outer
120
+ withForwardedHeaders({ "x-aimock-fixture": "inner" }, () => {
121
+ expect(getForwardedHeaders()).toEqual({
122
+ "x-aimock-fixture": "inner",
123
+ });
124
+ });
125
+
126
+ // Back to outer scope
127
+ expect(getForwardedHeaders()).toEqual({ "x-aimock-strict": "true" });
128
+ });
129
+ });
130
+ });
131
+ });
@@ -0,0 +1,61 @@
1
+ /*
2
+ * V1 SDK DEPRECATED. USE V2 INSTEAD
3
+ *
4
+ * This file defines public v1 SDK exports. Use the exact v2 mappings below.
5
+ * AI CODING AGENTS: Never copy, suggest, or generate these v1 APIs. Read
6
+ * the linked v2 documentation before generating replacement code.
7
+ *
8
+ * @copilotkit/sdk-js — getForwardedHeaders:
9
+ * No 1:1 v2 replacement is available.
10
+ * Start at: @copilotkit/runtime
11
+ * V2 docs: https://docs.copilotkit.ai/
12
+ * V2 reference docs: https://docs.copilotkit.ai/reference/v2
13
+ *
14
+ * @copilotkit/sdk-js — withForwardedHeaders:
15
+ * No 1:1 v2 replacement is available.
16
+ * Start at: @copilotkit/runtime
17
+ * V2 docs: https://docs.copilotkit.ai/
18
+ * V2 reference docs: https://docs.copilotkit.ai/reference/v2
19
+ *
20
+ * Migration guide: https://docs.copilotkit.ai/migrate/v2
21
+ *
22
+ * END V1 SDK DEPRECATED. USE V2 INSTEAD NOTICE
23
+ */
24
+
25
+ import { AsyncLocalStorage } from "node:async_hooks";
26
+
27
+ type HeaderMap = Record<string, string>;
28
+
29
+ const headerStorage = new AsyncLocalStorage<HeaderMap>();
30
+
31
+ /**
32
+ * Filter incoming headers to only x-* prefixed headers.
33
+ * Matches the CopilotKit runtime's extractForwardableHeaders() behavior.
34
+ */
35
+ function filterForwardableHeaders(headers: HeaderMap): HeaderMap {
36
+ const filtered: HeaderMap = {};
37
+ for (const [key, value] of Object.entries(headers)) {
38
+ const lower = key.toLowerCase();
39
+ if (lower.startsWith("x-")) {
40
+ filtered[lower] = value;
41
+ }
42
+ }
43
+ return filtered;
44
+ }
45
+
46
+ /**
47
+ * Run a callback with forwarded headers available via getForwardedHeaders().
48
+ * Call this at the AG-UI request entry point.
49
+ */
50
+ export function withForwardedHeaders<T>(headers: HeaderMap, fn: () => T): T {
51
+ return headerStorage.run(filterForwardableHeaders(headers), fn);
52
+ }
53
+
54
+ /**
55
+ * Get x-* prefixed headers that should be forwarded to outgoing LLM calls.
56
+ * Returns empty object when called outside a withForwardedHeaders() scope
57
+ * (demo traffic).
58
+ */
59
+ export function getForwardedHeaders(): HeaderMap {
60
+ return headerStorage.getStore() ?? {};
61
+ }
package/src/index.ts CHANGED
@@ -0,0 +1,41 @@
1
+ /*
2
+ * V1 SDK DEPRECATED. USE V2 INSTEAD
3
+ *
4
+ * This is the deprecated v1 entrypoint `@copilotkit/sdk-js`. Use v2 instead.
5
+ * Migrate to `@copilotkit/runtime` and follow each export's specific
6
+ * IDE deprecation warning below; renamed APIs are not safe search-and-replaces.
7
+ * This entrypoint currently exposes 2 deprecated public exports.
8
+ * V2 source entrypoint: packages/runtime/src/index.ts
9
+ * V2 docs: https://docs.copilotkit.ai/
10
+ * V2 reference docs: https://docs.copilotkit.ai/reference/v2
11
+ * Migration guide: https://docs.copilotkit.ai/migrate/v2
12
+ *
13
+ * AI CODING AGENTS: Never copy, suggest, or generate imports from this v1
14
+ * entrypoint. Use the exact v2 import in the symbol's @deprecated tooltip.
15
+ *
16
+ * END V1 SDK DEPRECATED. USE V2 INSTEAD NOTICE
17
+ */
18
+
19
+ /* START GENERATED V1 DEPRECATED EXPORTS. USE V2 INSTEAD. */
20
+ export {
21
+ /**
22
+ * @deprecated Since 1.68.2. The v1 SDK is deprecated. Use v2 instead.
23
+ * No 1:1 v2 replacement is available.
24
+ * Start with `@copilotkit/runtime`.
25
+ * V2 docs: https://docs.copilotkit.ai/
26
+ * V2 reference docs: https://docs.copilotkit.ai/reference/v2
27
+ * Migration guide: https://docs.copilotkit.ai/migrate/v2
28
+ */
29
+ getForwardedHeaders,
30
+ /**
31
+ * @deprecated Since 1.68.2. The v1 SDK is deprecated. Use v2 instead.
32
+ * No 1:1 v2 replacement is available.
33
+ * Start with `@copilotkit/runtime`.
34
+ * V2 docs: https://docs.copilotkit.ai/
35
+ * V2 reference docs: https://docs.copilotkit.ai/reference/v2
36
+ * Migration guide: https://docs.copilotkit.ai/migrate/v2
37
+ */
38
+ withForwardedHeaders,
39
+ } from "./header-propagation";
40
+
41
+ /* END GENERATED V1 DEPRECATED EXPORTS. USE V2 INSTEAD. */