@copilotkit/shared 2.0.0-rc.1 → 2.0.0-rc.10

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 (220) hide show
  1. package/README.md +3 -14
  2. package/dist/a2ui-prompts.cjs +21 -62
  3. package/dist/a2ui-prompts.cjs.map +1 -1
  4. package/dist/a2ui-prompts.d.cts +8 -1
  5. package/dist/a2ui-prompts.d.cts.map +1 -1
  6. package/dist/a2ui-prompts.d.mts +8 -1
  7. package/dist/a2ui-prompts.d.mts.map +1 -1
  8. package/dist/a2ui-prompts.mjs +22 -62
  9. package/dist/a2ui-prompts.mjs.map +1 -1
  10. package/dist/generative-ui/basic-functions.cjs +1 -1
  11. package/dist/generative-ui/basic-functions.cjs.map +1 -1
  12. package/dist/generative-ui/basic-functions.mjs +1 -1
  13. package/dist/generative-ui/basic-functions.mjs.map +1 -1
  14. package/dist/generative-ui/compact-schema.cjs +187 -0
  15. package/dist/generative-ui/compact-schema.cjs.map +1 -0
  16. package/dist/generative-ui/compact-schema.d.cts +55 -0
  17. package/dist/generative-ui/compact-schema.d.cts.map +1 -0
  18. package/dist/generative-ui/compact-schema.d.mts +55 -0
  19. package/dist/generative-ui/compact-schema.d.mts.map +1 -0
  20. package/dist/generative-ui/compact-schema.mjs +185 -0
  21. package/dist/generative-ui/compact-schema.mjs.map +1 -0
  22. package/dist/generative-ui/prompt-spec.cjs +225 -0
  23. package/dist/generative-ui/prompt-spec.cjs.map +1 -0
  24. package/dist/generative-ui/prompt-spec.d.cts +94 -0
  25. package/dist/generative-ui/prompt-spec.d.cts.map +1 -0
  26. package/dist/generative-ui/prompt-spec.d.mts +94 -0
  27. package/dist/generative-ui/prompt-spec.d.mts.map +1 -0
  28. package/dist/generative-ui/prompt-spec.mjs +220 -0
  29. package/dist/generative-ui/prompt-spec.mjs.map +1 -0
  30. package/dist/generative-ui/prompt.cjs +10 -36
  31. package/dist/generative-ui/prompt.cjs.map +1 -1
  32. package/dist/generative-ui/prompt.d.cts +2 -1
  33. package/dist/generative-ui/prompt.d.cts.map +1 -1
  34. package/dist/generative-ui/prompt.d.mts +2 -1
  35. package/dist/generative-ui/prompt.d.mts.map +1 -1
  36. package/dist/generative-ui/prompt.mjs +12 -38
  37. package/dist/generative-ui/prompt.mjs.map +1 -1
  38. package/dist/generative-ui/schema-render.cjs +242 -0
  39. package/dist/generative-ui/schema-render.cjs.map +1 -0
  40. package/dist/generative-ui/schema-render.d.cts +44 -0
  41. package/dist/generative-ui/schema-render.d.cts.map +1 -0
  42. package/dist/generative-ui/schema-render.d.mts +44 -0
  43. package/dist/generative-ui/schema-render.d.mts.map +1 -0
  44. package/dist/generative-ui/schema-render.mjs +239 -0
  45. package/dist/generative-ui/schema-render.mjs.map +1 -0
  46. package/dist/index.cjs +17 -15
  47. package/dist/index.cjs.map +1 -1
  48. package/dist/index.d.cts +10 -9
  49. package/dist/index.d.cts.map +1 -1
  50. package/dist/index.d.mts +10 -9
  51. package/dist/index.d.mts.map +1 -1
  52. package/dist/index.mjs +9 -7
  53. package/dist/index.mjs.map +1 -1
  54. package/dist/index.umd.js +808 -438
  55. package/dist/index.umd.js.map +1 -1
  56. package/dist/package.cjs +1 -1
  57. package/dist/package.mjs +1 -1
  58. package/dist/standard-schema.cjs +20 -7
  59. package/dist/standard-schema.cjs.map +1 -1
  60. package/dist/standard-schema.d.cts +15 -16
  61. package/dist/standard-schema.d.cts.map +1 -1
  62. package/dist/standard-schema.d.mts +15 -16
  63. package/dist/standard-schema.d.mts.map +1 -1
  64. package/dist/standard-schema.mjs +20 -8
  65. package/dist/standard-schema.mjs.map +1 -1
  66. package/dist/telemetry/lambda-client.cjs.map +1 -1
  67. package/dist/telemetry/lambda-client.d.cts.map +1 -1
  68. package/dist/telemetry/lambda-client.d.mts.map +1 -1
  69. package/dist/telemetry/lambda-client.mjs.map +1 -1
  70. package/dist/telemetry/telemetry-disabled.cjs +3 -6
  71. package/dist/telemetry/telemetry-disabled.cjs.map +1 -1
  72. package/dist/telemetry/telemetry-disabled.d.cts +0 -5
  73. package/dist/telemetry/telemetry-disabled.d.cts.map +1 -1
  74. package/dist/telemetry/telemetry-disabled.d.mts +0 -5
  75. package/dist/telemetry/telemetry-disabled.d.mts.map +1 -1
  76. package/dist/telemetry/telemetry-disabled.mjs +4 -6
  77. package/dist/telemetry/telemetry-disabled.mjs.map +1 -1
  78. package/dist/types/message.d.cts +1 -18
  79. package/dist/types/message.d.cts.map +1 -1
  80. package/dist/types/message.d.mts +1 -18
  81. package/dist/types/message.d.mts.map +1 -1
  82. package/dist/utils/errors.cjs +4 -42
  83. package/dist/utils/errors.cjs.map +1 -1
  84. package/dist/utils/errors.d.cts +16 -34
  85. package/dist/utils/errors.d.cts.map +1 -1
  86. package/dist/utils/errors.d.mts +16 -34
  87. package/dist/utils/errors.d.mts.map +1 -1
  88. package/dist/utils/errors.mjs +5 -41
  89. package/dist/utils/errors.mjs.map +1 -1
  90. package/dist/utils/index.cjs +1 -2
  91. package/dist/utils/index.cjs.map +1 -1
  92. package/dist/utils/index.d.cts +4 -5
  93. package/dist/utils/index.d.cts.map +1 -1
  94. package/dist/utils/index.d.mts +4 -5
  95. package/dist/utils/index.d.mts.map +1 -1
  96. package/dist/utils/index.mjs +4 -5
  97. package/dist/utils/index.mjs.map +1 -1
  98. package/dist/utils/json-schema.cjs +0 -79
  99. package/dist/utils/json-schema.cjs.map +1 -1
  100. package/dist/utils/json-schema.d.cts +1 -4
  101. package/dist/utils/json-schema.d.cts.map +1 -1
  102. package/dist/utils/json-schema.d.mts +1 -4
  103. package/dist/utils/json-schema.d.mts.map +1 -1
  104. package/dist/utils/json-schema.mjs +1 -78
  105. package/dist/utils/json-schema.mjs.map +1 -1
  106. package/dist/utils/process-env.cjs +14 -0
  107. package/dist/utils/process-env.cjs.map +1 -0
  108. package/dist/utils/process-env.d.cts +10 -0
  109. package/dist/utils/process-env.d.cts.map +1 -0
  110. package/dist/utils/process-env.d.mts +10 -0
  111. package/dist/utils/process-env.d.mts.map +1 -0
  112. package/dist/utils/process-env.mjs +13 -0
  113. package/dist/utils/process-env.mjs.map +1 -0
  114. package/dist/utils/random-id.cjs +13 -26
  115. package/dist/utils/random-id.cjs.map +1 -1
  116. package/dist/utils/random-id.d.cts +6 -3
  117. package/dist/utils/random-id.d.cts.map +1 -1
  118. package/dist/utils/random-id.d.mts +6 -3
  119. package/dist/utils/random-id.d.mts.map +1 -1
  120. package/dist/utils/random-id.mjs +14 -25
  121. package/dist/utils/random-id.mjs.map +1 -1
  122. package/package.json +7 -31
  123. package/src/__tests__/a2ui-prompts.test.ts +42 -9
  124. package/src/__tests__/fixtures/prompt-goldens.json +354 -0
  125. package/src/__tests__/prompt-spec.test.ts +191 -0
  126. package/src/__tests__/root-entry-browser-safety.test.ts +5 -7
  127. package/src/__tests__/schema-render.test.ts +245 -0
  128. package/src/__tests__/standard-schema.test.ts +61 -101
  129. package/src/a2ui-prompts.ts +47 -96
  130. package/src/generative-ui/basic-functions.ts +1 -1
  131. package/src/generative-ui/compact-schema.ts +277 -0
  132. package/src/generative-ui/index.ts +3 -0
  133. package/src/generative-ui/prompt-spec.ts +335 -0
  134. package/src/generative-ui/prompt.ts +25 -75
  135. package/src/generative-ui/schema-render.ts +374 -0
  136. package/src/index.ts +3 -16
  137. package/src/standard-schema.ts +24 -27
  138. package/src/telemetry/lambda-client.ts +3 -2
  139. package/src/telemetry/telemetry-disabled.ts +7 -12
  140. package/src/types/message.ts +1 -19
  141. package/src/utils/__tests__/errors.test.ts +44 -0
  142. package/src/utils/__tests__/json-schema.test.ts +3 -479
  143. package/src/utils/__tests__/process-env.test.ts +22 -0
  144. package/src/utils/__tests__/random-id.test.ts +37 -0
  145. package/src/utils/errors.ts +27 -85
  146. package/src/utils/index.ts +1 -2
  147. package/src/utils/json-schema.ts +1 -152
  148. package/src/utils/process-env.ts +14 -0
  149. package/src/utils/random-id.ts +17 -37
  150. package/tsdown.config.ts +2 -19
  151. package/dist/event-transforms/index.cjs +0 -15
  152. package/dist/event-transforms/index.d.cts +0 -4
  153. package/dist/event-transforms/index.d.mts +0 -4
  154. package/dist/event-transforms/index.mjs +0 -5
  155. package/dist/event-transforms/open-generative-ui-middleware.cjs +0 -286
  156. package/dist/event-transforms/open-generative-ui-middleware.cjs.map +0 -1
  157. package/dist/event-transforms/open-generative-ui-middleware.d.cts +0 -60
  158. package/dist/event-transforms/open-generative-ui-middleware.d.cts.map +0 -1
  159. package/dist/event-transforms/open-generative-ui-middleware.d.mts +0 -60
  160. package/dist/event-transforms/open-generative-ui-middleware.d.mts.map +0 -1
  161. package/dist/event-transforms/open-generative-ui-middleware.mjs +0 -283
  162. package/dist/event-transforms/open-generative-ui-middleware.mjs.map +0 -1
  163. package/dist/event-transforms/recorded-events.cjs +0 -59
  164. package/dist/event-transforms/recorded-events.cjs.map +0 -1
  165. package/dist/event-transforms/recorded-events.d.cts +0 -23
  166. package/dist/event-transforms/recorded-events.d.cts.map +0 -1
  167. package/dist/event-transforms/recorded-events.d.mts +0 -23
  168. package/dist/event-transforms/recorded-events.d.mts.map +0 -1
  169. package/dist/event-transforms/recorded-events.mjs +0 -58
  170. package/dist/event-transforms/recorded-events.mjs.map +0 -1
  171. package/dist/telemetry/events.d.cts +0 -72
  172. package/dist/telemetry/events.d.cts.map +0 -1
  173. package/dist/telemetry/events.d.mts +0 -72
  174. package/dist/telemetry/events.d.mts.map +0 -1
  175. package/dist/telemetry/index.cjs +0 -18
  176. package/dist/telemetry/index.d.cts +0 -5
  177. package/dist/telemetry/index.d.mts +0 -5
  178. package/dist/telemetry/index.mjs +0 -6
  179. package/dist/telemetry/telemetry-client.cjs +0 -165
  180. package/dist/telemetry/telemetry-client.cjs.map +0 -1
  181. package/dist/telemetry/telemetry-client.d.cts +0 -87
  182. package/dist/telemetry/telemetry-client.d.cts.map +0 -1
  183. package/dist/telemetry/telemetry-client.d.mts +0 -87
  184. package/dist/telemetry/telemetry-client.d.mts.map +0 -1
  185. package/dist/telemetry/telemetry-client.mjs +0 -164
  186. package/dist/telemetry/telemetry-client.mjs.map +0 -1
  187. package/dist/telemetry/utils.cjs +0 -14
  188. package/dist/telemetry/utils.cjs.map +0 -1
  189. package/dist/telemetry/utils.mjs +0 -13
  190. package/dist/telemetry/utils.mjs.map +0 -1
  191. package/dist/utils/conditions.cjs +0 -32
  192. package/dist/utils/conditions.cjs.map +0 -1
  193. package/dist/utils/conditions.d.cts +0 -31
  194. package/dist/utils/conditions.d.cts.map +0 -1
  195. package/dist/utils/conditions.d.mts +0 -31
  196. package/dist/utils/conditions.d.mts.map +0 -1
  197. package/dist/utils/conditions.mjs +0 -31
  198. package/dist/utils/conditions.mjs.map +0 -1
  199. package/dist/utils/console-styling.cjs +0 -91
  200. package/dist/utils/console-styling.cjs.map +0 -1
  201. package/dist/utils/console-styling.d.cts +0 -65
  202. package/dist/utils/console-styling.d.cts.map +0 -1
  203. package/dist/utils/console-styling.d.mts +0 -65
  204. package/dist/utils/console-styling.d.mts.map +0 -1
  205. package/dist/utils/console-styling.mjs +0 -85
  206. package/dist/utils/console-styling.mjs.map +0 -1
  207. package/src/__tests__/zod-regression.test.ts +0 -319
  208. package/src/event-transforms/index.ts +0 -5
  209. package/src/event-transforms/open-generative-ui-middleware.ts +0 -392
  210. package/src/event-transforms/recorded-events.test.ts +0 -166
  211. package/src/event-transforms/recorded-events.ts +0 -74
  212. package/src/telemetry/events.ts +0 -68
  213. package/src/telemetry/index.ts +0 -9
  214. package/src/telemetry/telemetry-blank-license-identity.test.ts +0 -128
  215. package/src/telemetry/telemetry-client.test.ts +0 -798
  216. package/src/telemetry/telemetry-client.ts +0 -328
  217. package/src/telemetry/utils.ts +0 -15
  218. package/src/utils/__tests__/conditions.test.ts +0 -161
  219. package/src/utils/conditions.ts +0 -116
  220. package/src/utils/console-styling.ts +0 -126
@@ -1,486 +1,10 @@
1
- import { describe, it, expect, vi } from "vitest";
2
- import { z } from "zod";
1
+ import { describe, it, expect } from "vitest";
2
+ import type { JSONSchema } from "../json-schema";
3
3
  import {
4
- convertJsonSchemaToZodSchema,
5
4
  actionParametersToJsonSchema,
6
5
  jsonSchemaToActionParameters,
7
- JSONSchema,
8
6
  } from "../json-schema";
9
- import { zodToJsonSchema } from "zod-to-json-schema";
10
- import { Parameter } from "../../types";
11
-
12
- describe("convertJsonSchemaToZodSchema", () => {
13
- it("should convert a simple JSON schema to a Zod schema", () => {
14
- const jsonSchema = {
15
- type: "object",
16
- properties: {
17
- name: { type: "string" },
18
- age: { type: "number" },
19
- },
20
- required: ["name", "age"],
21
- };
22
-
23
- const expectedSchema = z.object({
24
- name: z.string(),
25
- age: z.number(),
26
- });
27
-
28
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
29
- const resultSchemaJson = zodToJsonSchema(result);
30
- const expectedSchemaJson = zodToJsonSchema(expectedSchema);
31
-
32
- expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
33
- });
34
-
35
- it("should convert a JSON schema with nested objects to a Zod schema", () => {
36
- const jsonSchema = {
37
- type: "object",
38
- properties: {
39
- name: { type: "string" },
40
- address: {
41
- type: "object",
42
- properties: {
43
- street: { type: "string" },
44
- city: { type: "string" },
45
- },
46
- required: ["street", "city"],
47
- },
48
- },
49
- required: ["name", "address"],
50
- };
51
-
52
- const expectedSchema = z.object({
53
- name: z.string(),
54
- address: z.object({
55
- street: z.string(),
56
- city: z.string(),
57
- }),
58
- });
59
-
60
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
61
- const resultSchemaJson = zodToJsonSchema(result);
62
- const expectedSchemaJson = zodToJsonSchema(expectedSchema);
63
-
64
- expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
65
- });
66
-
67
- it("should convert a JSON schema with arrays to a Zod schema", () => {
68
- const jsonSchema = {
69
- type: "object",
70
- properties: {
71
- names: {
72
- type: "array",
73
- items: { type: "string" },
74
- },
75
- },
76
- required: ["names"],
77
- };
78
-
79
- const expectedSchema = z.object({
80
- names: z.array(z.string()),
81
- });
82
-
83
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
84
- const resultSchemaJson = zodToJsonSchema(result);
85
- const expectedSchemaJson = zodToJsonSchema(expectedSchema);
86
-
87
- expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
88
- });
89
-
90
- it("should convert a JSON schema with optional properties to a Zod schema", () => {
91
- const jsonSchema = {
92
- type: "object",
93
- properties: {
94
- name: { type: "string" },
95
- age: { type: "number", required: false },
96
- },
97
- };
98
-
99
- const expectedSchema = z
100
- .object({
101
- name: z.string().optional(),
102
- age: z.number().optional(),
103
- })
104
- .optional();
105
-
106
- const result = convertJsonSchemaToZodSchema(jsonSchema, false);
107
-
108
- console.log(convertJsonSchemaToZodSchema(jsonSchema, false));
109
-
110
- const resultSchemaJson = zodToJsonSchema(result);
111
- const expectedSchemaJson = zodToJsonSchema(expectedSchema);
112
-
113
- expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
114
- });
115
-
116
- it("should convert a JSON schema with different types to a Zod schema", () => {
117
- const jsonSchema = {
118
- type: "object",
119
- properties: {
120
- name: { type: "string" },
121
- age: { type: "number" },
122
- isAdmin: { type: "boolean" },
123
- },
124
- required: ["name", "age", "isAdmin"],
125
- };
126
-
127
- const expectedSchema = z.object({
128
- name: z.string(),
129
- age: z.number(),
130
- isAdmin: z.boolean(),
131
- });
132
-
133
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
134
- const resultSchemaJson = zodToJsonSchema(result);
135
- const expectedSchemaJson = zodToJsonSchema(expectedSchema);
136
-
137
- expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
138
- });
139
-
140
- it("should handle edge case where JSON schema has no properties", () => {
141
- const jsonSchema = {
142
- type: "object",
143
- };
144
-
145
- const expectedSchema = z.object({});
146
-
147
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
148
- const resultSchemaJson = zodToJsonSchema(result);
149
- const expectedSchemaJson = zodToJsonSchema(expectedSchema);
150
-
151
- expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
152
- });
153
-
154
- it("should preserve string enum constraints", () => {
155
- const jsonSchema = {
156
- type: "object",
157
- properties: {
158
- status: {
159
- type: "string",
160
- description: "The status",
161
- enum: ["todo", "done"],
162
- },
163
- },
164
- required: ["status"],
165
- };
166
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
167
- const statusSchema = (result as z.ZodObject<any>).shape.status;
168
- expect(statusSchema._def.typeName).toBe("ZodEnum");
169
- expect(statusSchema._def.values).toEqual(["todo", "done"]);
170
- });
171
-
172
- it("should handle null-union type ['string', 'null'] as nullable", () => {
173
- const jsonSchema = {
174
- type: "object",
175
- properties: {
176
- nickname: {
177
- type: ["string", "null"],
178
- description: "Optional nickname",
179
- },
180
- },
181
- required: ["nickname"],
182
- };
183
-
184
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
185
- const shape = (result as z.ZodObject<any>).shape;
186
-
187
- // The nickname field should accept both string and null
188
- expect(shape.nickname.safeParse("hello").success).toBe(true);
189
- expect(shape.nickname.safeParse(null).success).toBe(true);
190
- expect(shape.nickname.safeParse(42).success).toBe(false);
191
- });
192
-
193
- it("should handle null-union type ['number', 'null'] as nullable number", () => {
194
- const jsonSchema = {
195
- type: "object",
196
- properties: {
197
- score: {
198
- type: ["number", "null"],
199
- },
200
- },
201
- required: ["score"],
202
- };
203
-
204
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
205
- const shape = (result as z.ZodObject<any>).shape;
206
-
207
- expect(shape.score.safeParse(42).success).toBe(true);
208
- expect(shape.score.safeParse(null).success).toBe(true);
209
- expect(shape.score.safeParse("hello").success).toBe(false);
210
- });
211
-
212
- it("should handle edge case where JSON schema has no required properties", () => {
213
- const jsonSchema = {
214
- type: "object",
215
- properties: {
216
- name: { type: "string" },
217
- age: { type: "number" },
218
- },
219
- };
220
-
221
- const expectedSchema = z
222
- .object({
223
- name: z.string().optional(),
224
- age: z.number().optional(),
225
- })
226
- .optional();
227
-
228
- const result = convertJsonSchemaToZodSchema(jsonSchema, false);
229
- const resultSchemaJson = zodToJsonSchema(result);
230
- const expectedSchemaJson = zodToJsonSchema(expectedSchema);
231
-
232
- expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
233
- });
234
-
235
- it("should resolve non-circular $ref definitions correctly", () => {
236
- const jsonSchema = {
237
- type: "object",
238
- properties: {
239
- address: { $ref: "#/$defs/Address" },
240
- },
241
- required: ["address"],
242
- $defs: {
243
- Address: {
244
- type: "object",
245
- properties: {
246
- street: { type: "string", description: "Street name" },
247
- city: { type: "string", description: "City name" },
248
- },
249
- required: ["street", "city"],
250
- description: "A postal address",
251
- },
252
- },
253
- };
254
-
255
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
256
- const resultJson = zodToJsonSchema(result);
257
-
258
- const expectedSchema = z.object({
259
- address: z
260
- .object({
261
- street: z.string().describe("Street name"),
262
- city: z.string().describe("City name"),
263
- })
264
- .describe("A postal address"),
265
- });
266
- const expectedJson = zodToJsonSchema(expectedSchema);
267
-
268
- expect(resultJson).toStrictEqual(expectedJson);
269
- });
270
-
271
- it("should handle circular $ref without crashing and return z.any()", () => {
272
- // A schema where Node references itself — this would cause infinite
273
- // recursion without cycle detection.
274
- const jsonSchema = {
275
- type: "object",
276
- properties: {
277
- root: { $ref: "#/$defs/Node" },
278
- },
279
- required: ["root"],
280
- $defs: {
281
- Node: {
282
- type: "object",
283
- properties: {
284
- value: { type: "string", description: "Node value" },
285
- child: { $ref: "#/$defs/Node" },
286
- },
287
- required: ["value"],
288
- description: "A tree node",
289
- },
290
- },
291
- };
292
-
293
- const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
294
-
295
- // Must not throw or hang
296
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
297
- expect(result).toBeDefined();
298
-
299
- // The circular ref should have produced a console.warn
300
- expect(warnSpy).toHaveBeenCalledWith(
301
- expect.stringContaining("Circular $ref detected"),
302
- );
303
-
304
- // The top-level shape should still have a "root" key that is an object
305
- const shape = (result as z.ZodObject<any>).shape;
306
- expect(shape.root).toBeDefined();
307
-
308
- // Inside root, "value" should be a string and "child" should be z.any()
309
- // (child is optional since it's not in required[], so unwrap ZodOptional)
310
- const rootShape = (shape.root as z.ZodObject<any>).shape;
311
- expect(rootShape.value._def.typeName).toBe("ZodString");
312
- const childDef = rootShape.child._def;
313
- if (childDef.typeName === "ZodOptional") {
314
- expect(childDef.innerType._def.typeName).toBe("ZodAny");
315
- } else {
316
- expect(childDef.typeName).toBe("ZodAny");
317
- }
318
-
319
- warnSpy.mockRestore();
320
- });
321
-
322
- it("should resolve the same $ref used by multiple sibling properties", () => {
323
- // Two properties reference the same $def — the visited set must NOT
324
- // mark the second usage as circular.
325
- const jsonSchema = {
326
- type: "object",
327
- properties: {
328
- billing: { $ref: "#/$defs/Address" },
329
- shipping: { $ref: "#/$defs/Address" },
330
- },
331
- required: ["billing", "shipping"],
332
- $defs: {
333
- Address: {
334
- type: "object",
335
- properties: {
336
- street: { type: "string", description: "Street" },
337
- city: { type: "string", description: "City" },
338
- },
339
- required: ["street", "city"],
340
- description: "An address",
341
- },
342
- },
343
- };
344
-
345
- const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
346
-
347
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
348
- const shape = (result as z.ZodObject<any>).shape;
349
-
350
- // Both should be fully resolved objects, NOT z.any()
351
- expect(shape.billing._def.typeName).toBe("ZodObject");
352
- expect(shape.shipping._def.typeName).toBe("ZodObject");
353
-
354
- // No circular-ref warning should have been emitted
355
- expect(warnSpy).not.toHaveBeenCalled();
356
-
357
- warnSpy.mockRestore();
358
- });
359
-
360
- it("should resolve a shared $ref used in different branches of a $ref chain", () => {
361
- // Wrapper -> Container (via $ref) which has two children both using $ref to Leaf.
362
- // Without set cloning, the second Leaf ref in Container would be wrongly flagged
363
- // as circular because the first Leaf resolution already added it to the set.
364
- const jsonSchema = {
365
- type: "object",
366
- properties: {
367
- wrapper: { $ref: "#/$defs/Container" },
368
- },
369
- required: ["wrapper"],
370
- $defs: {
371
- Container: {
372
- type: "object",
373
- properties: {
374
- first: { $ref: "#/$defs/Leaf" },
375
- second: { $ref: "#/$defs/Leaf" },
376
- },
377
- required: ["first", "second"],
378
- description: "A container with two leaves",
379
- },
380
- Leaf: {
381
- type: "object",
382
- properties: {
383
- label: { type: "string", description: "Leaf label" },
384
- },
385
- required: ["label"],
386
- description: "A leaf node",
387
- },
388
- },
389
- };
390
-
391
- const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
392
-
393
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
394
- const wrapperShape = (
395
- (result as z.ZodObject<any>).shape.wrapper as z.ZodObject<any>
396
- ).shape;
397
-
398
- // Both first and second should be fully resolved Leaf objects
399
- expect(wrapperShape.first._def.typeName).toBe("ZodObject");
400
- expect(wrapperShape.second._def.typeName).toBe("ZodObject");
401
-
402
- // No circular-ref warning should have been emitted
403
- expect(warnSpy).not.toHaveBeenCalled();
404
-
405
- warnSpy.mockRestore();
406
- });
407
-
408
- it("should handle anyOf with $ref variants", () => {
409
- const jsonSchema = {
410
- type: "object",
411
- properties: {
412
- pet: {
413
- anyOf: [{ $ref: "#/$defs/Cat" }, { $ref: "#/$defs/Dog" }],
414
- description: "A pet",
415
- },
416
- },
417
- required: ["pet"],
418
- $defs: {
419
- Cat: {
420
- type: "object",
421
- properties: { meow: { type: "boolean" } },
422
- required: ["meow"],
423
- },
424
- Dog: {
425
- type: "object",
426
- properties: { bark: { type: "boolean" } },
427
- required: ["bark"],
428
- },
429
- },
430
- };
431
-
432
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
433
- expect(result).toBeDefined();
434
-
435
- // Should produce a union inside the "pet" property
436
- const petSchema = (result as z.ZodObject<any>).shape.pet;
437
- expect(petSchema._def.typeName).toBe("ZodUnion");
438
- });
439
-
440
- it("should handle integer type as z.number()", () => {
441
- const jsonSchema = {
442
- type: "object",
443
- properties: {
444
- count: { type: "integer", description: "A count" },
445
- },
446
- required: ["count"],
447
- };
448
-
449
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
450
- const shape = (result as z.ZodObject<any>).shape;
451
- expect(shape.count._def.typeName).toBe("ZodNumber");
452
- });
453
-
454
- it("should handle null type", () => {
455
- const jsonSchema = {
456
- type: "object",
457
- properties: {
458
- empty: { type: "null", description: "Always null" },
459
- },
460
- required: ["empty"],
461
- };
462
-
463
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
464
- const shape = (result as z.ZodObject<any>).shape;
465
- expect(shape.empty._def.typeName).toBe("ZodNull");
466
- });
467
-
468
- it("should warn and return z.any() for unsupported schema types", () => {
469
- const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
470
-
471
- const jsonSchema = { type: "custom_unsupported" };
472
- const result = convertJsonSchemaToZodSchema(jsonSchema, true);
473
-
474
- expect(result._def.typeName).toBe("ZodAny");
475
- expect(warnSpy).toHaveBeenCalledWith(
476
- expect.stringContaining(
477
- 'Unsupported JSON schema type "custom_unsupported"',
478
- ),
479
- );
480
-
481
- warnSpy.mockRestore();
482
- });
483
- });
7
+ import type { Parameter } from "../../types";
484
8
 
485
9
  describe("jsonSchemaToActionParameters", () => {
486
10
  it("should convert a simple JSONSchema to Parameter array", () => {
@@ -0,0 +1,22 @@
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+ import { processEnv } from "../process-env";
3
+
4
+ describe("processEnv", () => {
5
+ afterEach(() => {
6
+ vi.unstubAllGlobals();
7
+ vi.unstubAllEnvs();
8
+ });
9
+
10
+ it("reads the live process environment", () => {
11
+ vi.stubEnv("CPK_PROCESS_ENV_TEST", "first");
12
+ expect(processEnv().CPK_PROCESS_ENV_TEST).toBe("first");
13
+ vi.stubEnv("CPK_PROCESS_ENV_TEST", "second");
14
+ expect(processEnv().CPK_PROCESS_ENV_TEST).toBe("second");
15
+ });
16
+
17
+ // Cloudflare Workers without nodejs_compat define no `process`.
18
+ it("returns an empty environment where there is no process", () => {
19
+ vi.stubGlobal("process", undefined);
20
+ expect(processEnv()).toEqual({});
21
+ });
22
+ });
@@ -0,0 +1,37 @@
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+ import { randomId, randomUUID } from "../random-id";
3
+
4
+ const UUID_V4 =
5
+ /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
6
+
7
+ describe("randomUUID", () => {
8
+ afterEach(() => {
9
+ vi.unstubAllGlobals();
10
+ });
11
+
12
+ it("returns crypto.randomUUID() when it exists", () => {
13
+ vi.stubGlobal("crypto", {
14
+ randomUUID: () => "11111111-2222-4333-8444-555555555555",
15
+ });
16
+ expect(randomUUID()).toBe("11111111-2222-4333-8444-555555555555");
17
+ });
18
+
19
+ it("builds a v4 UUID from getRandomValues when randomUUID is missing", () => {
20
+ vi.stubGlobal("crypto", {
21
+ getRandomValues: (bytes: Uint8Array) => bytes.fill(0xff),
22
+ });
23
+ const id = randomUUID();
24
+ expect(id).toBe("ffffffff-ffff-4fff-bfff-ffffffffffff");
25
+ expect(id).toMatch(UUID_V4);
26
+ });
27
+
28
+ it("throws a clear error when the runtime has no crypto", () => {
29
+ vi.stubGlobal("crypto", undefined);
30
+ expect(() => randomUUID()).toThrow(/getRandomValues/);
31
+ });
32
+
33
+ it("prefixes randomId with ck-", () => {
34
+ expect(randomId()).toMatch(/^ck-/);
35
+ expect(randomId().slice(3)).toMatch(UUID_V4);
36
+ });
37
+ });
@@ -1,6 +1,3 @@
1
- import { GraphQLError } from "graphql";
2
- import { COPILOTKIT_VERSION } from "../index";
3
-
4
1
  export enum Severity {
5
2
  CRITICAL = "critical", // Critical errors that block core functionality
6
3
  WARNING = "warning", // Configuration/setup issues that need attention
@@ -21,7 +18,6 @@ export const ERROR_NAMES = {
21
18
  "CopilotKitRemoteEndpointDiscoveryError",
22
19
  COPILOT_KIT_AGENT_DISCOVERY_ERROR: "CopilotKitAgentDiscoveryError",
23
20
  COPILOT_KIT_LOW_LEVEL_ERROR: "CopilotKitLowLevelError",
24
- COPILOT_KIT_VERSION_MISMATCH_ERROR: "CopilotKitVersionMismatchError",
25
21
  RESOLVED_COPILOT_KIT_ERROR: "ResolvedCopilotKitError",
26
22
  CONFIGURATION_ERROR: "ConfigurationError",
27
23
  MISSING_PUBLIC_API_KEY_ERROR: "MissingPublicApiKeyError",
@@ -134,7 +130,19 @@ export const ERROR_CONFIG = {
134
130
  },
135
131
  };
136
132
 
137
- export class CopilotKitError extends GraphQLError {
133
+ export interface CopilotKitErrorExtensions {
134
+ name: string;
135
+ statusCode: number;
136
+ code: CopilotKitErrorCode;
137
+ visibility: ErrorVisibility;
138
+ severity?: Severity;
139
+ troubleshootingUrl: string | null;
140
+ originalError: { message: string; stack?: string };
141
+ }
142
+
143
+ export class CopilotKitError extends Error {
144
+ /** Structured error details, kept in the shape GraphQL errors used in 1.x. */
145
+ extensions: CopilotKitErrorExtensions;
138
146
  code: CopilotKitErrorCode;
139
147
  statusCode: number;
140
148
  severity?: Severity;
@@ -159,22 +167,21 @@ export class CopilotKitError extends GraphQLError {
159
167
  const resolvedSeverity =
160
168
  severity ?? ("severity" in config ? config.severity : undefined);
161
169
 
162
- super(message, {
163
- extensions: {
164
- name,
165
- statusCode,
166
- code,
167
- visibility: resolvedVisibility,
168
- severity: resolvedSeverity,
169
- troubleshootingUrl:
170
- "troubleshootingUrl" in config ? config.troubleshootingUrl : null,
171
- originalError: {
172
- message,
173
- stack: new Error().stack,
174
- },
175
- },
176
- });
170
+ super(message);
177
171
 
172
+ this.extensions = {
173
+ name,
174
+ statusCode,
175
+ code,
176
+ visibility: resolvedVisibility,
177
+ severity: resolvedSeverity,
178
+ troubleshootingUrl:
179
+ "troubleshootingUrl" in config ? config.troubleshootingUrl : null,
180
+ originalError: {
181
+ message,
182
+ stack: new Error().stack,
183
+ },
184
+ };
178
185
  this.code = code;
179
186
  this.name = name;
180
187
  this.statusCode = statusCode;
@@ -208,36 +215,6 @@ export class CopilotKitMisuseError extends CopilotKitError {
208
215
  }
209
216
  }
210
217
 
211
- const getVersionMismatchErrorMessage = ({
212
- reactCoreVersion,
213
- runtimeVersion,
214
- runtimeClientGqlVersion,
215
- }: VersionMismatchResponse) =>
216
- `Version mismatch detected: @copilotkit/runtime@${runtimeVersion ?? ""} is not compatible with @copilotkit/react-core@${reactCoreVersion} and @copilotkit/runtime-client-gql@${runtimeClientGqlVersion}. Please ensure all installed copilotkit packages are on the same version.`;
217
- /**
218
- * Error thrown when CPK versions does not match
219
- *
220
- * @extends CopilotKitError
221
- */
222
- export class CopilotKitVersionMismatchError extends CopilotKitError {
223
- constructor({
224
- reactCoreVersion,
225
- runtimeVersion,
226
- runtimeClientGqlVersion,
227
- }: VersionMismatchResponse) {
228
- const code = CopilotKitErrorCode.VERSION_MISMATCH;
229
- super({
230
- message: getVersionMismatchErrorMessage({
231
- reactCoreVersion,
232
- runtimeVersion,
233
- runtimeClientGqlVersion,
234
- }),
235
- code,
236
- });
237
- this.name = ERROR_NAMES.COPILOT_KIT_VERSION_MISMATCH_ERROR;
238
- }
239
- }
240
-
241
218
  /**
242
219
  * Error thrown when the CopilotKit API endpoint cannot be discovered or accessed.
243
220
  * This typically occurs when:
@@ -477,41 +454,6 @@ export function ensureStructuredError<T extends CopilotKitError>(
477
454
  return isStructuredCopilotKitError(error) ? error : converter(error);
478
455
  }
479
456
 
480
- interface VersionMismatchResponse {
481
- runtimeVersion?: string;
482
- runtimeClientGqlVersion: string;
483
- reactCoreVersion: string;
484
- }
485
-
486
- export async function getPossibleVersionMismatch({
487
- runtimeVersion,
488
- runtimeClientGqlVersion,
489
- }: {
490
- runtimeVersion?: string;
491
- runtimeClientGqlVersion: string;
492
- }) {
493
- if (!runtimeVersion || runtimeVersion === "" || !runtimeClientGqlVersion)
494
- return;
495
- if (
496
- COPILOTKIT_VERSION !== runtimeVersion ||
497
- COPILOTKIT_VERSION !== runtimeClientGqlVersion ||
498
- runtimeVersion !== runtimeClientGqlVersion
499
- ) {
500
- return {
501
- runtimeVersion,
502
- runtimeClientGqlVersion,
503
- reactCoreVersion: COPILOTKIT_VERSION,
504
- message: getVersionMismatchErrorMessage({
505
- runtimeVersion,
506
- runtimeClientGqlVersion,
507
- reactCoreVersion: COPILOTKIT_VERSION,
508
- }),
509
- };
510
- }
511
-
512
- return;
513
- }
514
-
515
457
  const resolveLowLevelErrorMessage = ({
516
458
  errorCode,
517
459
  url,