@copilotkit/shared 2.0.0-rc.8 → 2.0.0-rc.9

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 (100) hide show
  1. package/README.md +3 -14
  2. package/dist/index.cjs +0 -2
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +3 -4
  5. package/dist/index.d.cts.map +1 -1
  6. package/dist/index.d.mts +3 -4
  7. package/dist/index.d.mts.map +1 -1
  8. package/dist/index.mjs +2 -2
  9. package/dist/index.mjs.map +1 -1
  10. package/dist/index.umd.js +12 -95
  11. package/dist/index.umd.js.map +1 -1
  12. package/dist/package.cjs +1 -1
  13. package/dist/package.mjs +1 -1
  14. package/dist/standard-schema.cjs +7 -7
  15. package/dist/standard-schema.cjs.map +1 -1
  16. package/dist/standard-schema.d.cts +7 -16
  17. package/dist/standard-schema.d.cts.map +1 -1
  18. package/dist/standard-schema.d.mts +7 -16
  19. package/dist/standard-schema.d.mts.map +1 -1
  20. package/dist/standard-schema.mjs +7 -7
  21. package/dist/standard-schema.mjs.map +1 -1
  22. package/dist/telemetry/telemetry-disabled.cjs +0 -5
  23. package/dist/telemetry/telemetry-disabled.cjs.map +1 -1
  24. package/dist/telemetry/telemetry-disabled.d.cts +0 -5
  25. package/dist/telemetry/telemetry-disabled.d.cts.map +1 -1
  26. package/dist/telemetry/telemetry-disabled.d.mts +0 -5
  27. package/dist/telemetry/telemetry-disabled.d.mts.map +1 -1
  28. package/dist/telemetry/telemetry-disabled.mjs +0 -5
  29. package/dist/telemetry/telemetry-disabled.mjs.map +1 -1
  30. package/dist/utils/index.d.cts +1 -1
  31. package/dist/utils/index.d.mts +1 -1
  32. package/dist/utils/index.mjs +1 -1
  33. package/dist/utils/json-schema.cjs +0 -79
  34. package/dist/utils/json-schema.cjs.map +1 -1
  35. package/dist/utils/json-schema.d.cts +1 -4
  36. package/dist/utils/json-schema.d.cts.map +1 -1
  37. package/dist/utils/json-schema.d.mts +1 -4
  38. package/dist/utils/json-schema.d.mts.map +1 -1
  39. package/dist/utils/json-schema.mjs +1 -78
  40. package/dist/utils/json-schema.mjs.map +1 -1
  41. package/package.json +7 -28
  42. package/src/__tests__/root-entry-browser-safety.test.ts +5 -7
  43. package/src/__tests__/standard-schema.test.ts +38 -103
  44. package/src/index.ts +3 -16
  45. package/src/standard-schema.ts +9 -27
  46. package/src/telemetry/telemetry-disabled.ts +0 -5
  47. package/src/utils/__tests__/json-schema.test.ts +3 -479
  48. package/src/utils/json-schema.ts +1 -152
  49. package/tsdown.config.ts +2 -10
  50. package/dist/event-transforms/index.cjs +0 -15
  51. package/dist/event-transforms/index.d.cts +0 -4
  52. package/dist/event-transforms/index.d.mts +0 -4
  53. package/dist/event-transforms/index.mjs +0 -5
  54. package/dist/event-transforms/open-generative-ui-middleware.cjs +0 -286
  55. package/dist/event-transforms/open-generative-ui-middleware.cjs.map +0 -1
  56. package/dist/event-transforms/open-generative-ui-middleware.d.cts +0 -60
  57. package/dist/event-transforms/open-generative-ui-middleware.d.cts.map +0 -1
  58. package/dist/event-transforms/open-generative-ui-middleware.d.mts +0 -60
  59. package/dist/event-transforms/open-generative-ui-middleware.d.mts.map +0 -1
  60. package/dist/event-transforms/open-generative-ui-middleware.mjs +0 -283
  61. package/dist/event-transforms/open-generative-ui-middleware.mjs.map +0 -1
  62. package/dist/event-transforms/recorded-events.cjs +0 -59
  63. package/dist/event-transforms/recorded-events.cjs.map +0 -1
  64. package/dist/event-transforms/recorded-events.d.cts +0 -23
  65. package/dist/event-transforms/recorded-events.d.cts.map +0 -1
  66. package/dist/event-transforms/recorded-events.d.mts +0 -23
  67. package/dist/event-transforms/recorded-events.d.mts.map +0 -1
  68. package/dist/event-transforms/recorded-events.mjs +0 -58
  69. package/dist/event-transforms/recorded-events.mjs.map +0 -1
  70. package/dist/telemetry/events.d.cts +0 -72
  71. package/dist/telemetry/events.d.cts.map +0 -1
  72. package/dist/telemetry/events.d.mts +0 -72
  73. package/dist/telemetry/events.d.mts.map +0 -1
  74. package/dist/telemetry/index.cjs +0 -18
  75. package/dist/telemetry/index.d.cts +0 -5
  76. package/dist/telemetry/index.d.mts +0 -5
  77. package/dist/telemetry/index.mjs +0 -6
  78. package/dist/telemetry/telemetry-client.cjs +0 -165
  79. package/dist/telemetry/telemetry-client.cjs.map +0 -1
  80. package/dist/telemetry/telemetry-client.d.cts +0 -87
  81. package/dist/telemetry/telemetry-client.d.cts.map +0 -1
  82. package/dist/telemetry/telemetry-client.d.mts +0 -87
  83. package/dist/telemetry/telemetry-client.d.mts.map +0 -1
  84. package/dist/telemetry/telemetry-client.mjs +0 -164
  85. package/dist/telemetry/telemetry-client.mjs.map +0 -1
  86. package/dist/telemetry/utils.cjs +0 -14
  87. package/dist/telemetry/utils.cjs.map +0 -1
  88. package/dist/telemetry/utils.mjs +0 -13
  89. package/dist/telemetry/utils.mjs.map +0 -1
  90. package/src/__tests__/zod-regression.test.ts +0 -319
  91. package/src/event-transforms/index.ts +0 -5
  92. package/src/event-transforms/open-generative-ui-middleware.ts +0 -392
  93. package/src/event-transforms/recorded-events.test.ts +0 -166
  94. package/src/event-transforms/recorded-events.ts +0 -74
  95. package/src/telemetry/events.ts +0 -68
  96. package/src/telemetry/index.ts +0 -9
  97. package/src/telemetry/telemetry-blank-license-identity.test.ts +0 -128
  98. package/src/telemetry/telemetry-client.test.ts +0 -798
  99. package/src/telemetry/telemetry-client.ts +0 -328
  100. package/src/telemetry/utils.ts +0 -15
@@ -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", () => {
@@ -1,5 +1,4 @@
1
- import { z } from "zod";
2
- import { Parameter } from "../types";
1
+ import type { Parameter } from "../types";
3
2
 
4
3
  export type JSONSchemaString = {
5
4
  type: "string";
@@ -250,153 +249,3 @@ function convertAttribute(attribute: Parameter): JSONSchema {
250
249
  };
251
250
  }
252
251
  }
253
-
254
- export function convertJsonSchemaToZodSchema(
255
- jsonSchema: any,
256
- required: boolean,
257
- definitions?: Record<string, any>,
258
- visitedRefs?: Set<string>,
259
- ): z.ZodSchema {
260
- // Resolve $ref references
261
- if (jsonSchema.$ref && definitions) {
262
- const refPath = jsonSchema.$ref.replace(
263
- /^#\/\$defs\/|^#\/definitions\//,
264
- "",
265
- );
266
-
267
- // Detect circular $ref cycles
268
- const refs = visitedRefs ?? new Set<string>();
269
- if (refs.has(refPath)) {
270
- console.warn(
271
- `[CopilotKit] Circular $ref detected for "${refPath}" — falling back to z.any()`,
272
- );
273
- let schema = z.any();
274
- if (jsonSchema.description) {
275
- schema = schema.describe(jsonSchema.description);
276
- }
277
- return required ? schema : schema.optional();
278
- }
279
-
280
- const resolved = definitions[refPath];
281
- if (resolved) {
282
- // Clone the set so sibling branches don't see each other's visited refs
283
- const nextRefs = new Set(refs);
284
- nextRefs.add(refPath);
285
- return convertJsonSchemaToZodSchema(
286
- resolved,
287
- required,
288
- definitions,
289
- nextRefs,
290
- );
291
- }
292
- }
293
-
294
- // Collect top-level definitions for $ref resolution
295
- const defs = definitions ?? jsonSchema.$defs ?? jsonSchema.definitions;
296
-
297
- // Handle null-union types like ["string", "null"]
298
- if (Array.isArray(jsonSchema.type)) {
299
- const types = jsonSchema.type as string[];
300
- const hasNull = types.includes("null");
301
- const nonNullTypes = types.filter((t: string) => t !== "null");
302
- const resolvedType = nonNullTypes.length > 0 ? nonNullTypes[0] : "string";
303
- const innerSchema = convertJsonSchemaToZodSchema(
304
- { ...jsonSchema, type: resolvedType },
305
- true,
306
- defs,
307
- visitedRefs,
308
- );
309
- let schema = hasNull ? z.union([innerSchema, z.null()]) : innerSchema;
310
- if (jsonSchema.description) {
311
- schema = schema.describe(jsonSchema.description);
312
- }
313
- return required ? schema : schema.optional();
314
- }
315
-
316
- // Handle anyOf / oneOf as z.union
317
- const unionVariants = jsonSchema.anyOf ?? jsonSchema.oneOf;
318
- if (Array.isArray(unionVariants) && unionVariants.length > 0) {
319
- if (unionVariants.length === 1) {
320
- return convertJsonSchemaToZodSchema(
321
- unionVariants[0],
322
- required,
323
- defs,
324
- visitedRefs,
325
- );
326
- }
327
- const schemas = unionVariants.map((v: any) =>
328
- convertJsonSchemaToZodSchema(v, true, defs, visitedRefs),
329
- );
330
- let schema = z.union(
331
- schemas as [z.ZodSchema, z.ZodSchema, ...z.ZodSchema[]],
332
- );
333
- if (jsonSchema.description) {
334
- schema = schema.describe(jsonSchema.description);
335
- }
336
- return required ? schema : schema.optional();
337
- }
338
-
339
- if (jsonSchema.type === "object") {
340
- const spec: { [key: string]: z.ZodSchema } = {};
341
-
342
- if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
343
- return !required ? z.object(spec).optional() : z.object(spec);
344
- }
345
-
346
- for (const [key, value] of Object.entries(jsonSchema.properties)) {
347
- spec[key] = convertJsonSchemaToZodSchema(
348
- value,
349
- jsonSchema.required ? jsonSchema.required.includes(key) : false,
350
- defs,
351
- visitedRefs,
352
- );
353
- }
354
- let schema = z.object(spec).describe(jsonSchema.description);
355
- return required ? schema : schema.optional();
356
- } else if (jsonSchema.type === "string") {
357
- if (jsonSchema.enum && jsonSchema.enum.length > 0) {
358
- let schema = z
359
- .enum(jsonSchema.enum as [string, ...string[]])
360
- .describe(jsonSchema.description);
361
- return required ? schema : schema.optional();
362
- }
363
- let schema = z.string().describe(jsonSchema.description);
364
- return required ? schema : schema.optional();
365
- } else if (jsonSchema.type === "number" || jsonSchema.type === "integer") {
366
- let schema = z.number().describe(jsonSchema.description);
367
- return required ? schema : schema.optional();
368
- } else if (jsonSchema.type === "boolean") {
369
- let schema = z.boolean().describe(jsonSchema.description);
370
- return required ? schema : schema.optional();
371
- } else if (jsonSchema.type === "array") {
372
- let itemSchema = convertJsonSchemaToZodSchema(
373
- jsonSchema.items,
374
- true,
375
- defs,
376
- visitedRefs,
377
- );
378
- let schema = z.array(itemSchema).describe(jsonSchema.description);
379
- return required ? schema : schema.optional();
380
- } else if (jsonSchema.type === "null") {
381
- let schema = z.null().describe(jsonSchema.description);
382
- return required ? schema : schema.optional();
383
- }
384
-
385
- // Fallback: accept any value rather than throwing
386
- console.warn(
387
- `[CopilotKit] Unsupported JSON schema type "${jsonSchema.type ?? "unknown"}" — falling back to z.any()`,
388
- );
389
- let schema = z.any();
390
- if (jsonSchema.description) {
391
- schema = schema.describe(jsonSchema.description);
392
- }
393
- return required ? schema : schema.optional();
394
- }
395
-
396
- export function getZodParameters<T extends [] | Parameter[] | undefined>(
397
- parameters: T,
398
- ): any {
399
- if (!parameters) return z.object({});
400
- const jsonParams = actionParametersToJsonSchema(parameters);
401
- return convertJsonSchemaToZodSchema(jsonParams, true);
402
- }
package/tsdown.config.ts CHANGED
@@ -2,13 +2,7 @@ import { defineConfig } from "tsdown";
2
2
 
3
3
  export default defineConfig([
4
4
  {
5
- // Node-only telemetry and event transforms have separate entries.
6
- // The browser-facing root must not re-export either (#4151).
7
- entry: [
8
- "src/index.ts",
9
- "src/telemetry/index.ts",
10
- "src/event-transforms/index.ts",
11
- ],
5
+ entry: ["src/index.ts"],
12
6
  format: ["esm", "cjs"],
13
7
  dts: true,
14
8
  sourcemap: true,
@@ -24,14 +18,12 @@ export default defineConfig([
24
18
  sourcemap: true,
25
19
  target: "es2018",
26
20
  outDir: "dist",
27
- external: ["zod", "@ag-ui/core", "@ag-ui/client", "partial-json"],
21
+ external: ["@ag-ui/core", "@ag-ui/client", "partial-json"],
28
22
  outputOptions(options) {
29
23
  options.entryFileNames = "[name].umd.js";
30
24
  options.globals = {
31
- zod: "Zod",
32
25
  "@ag-ui/core": "AgUICore",
33
26
  "@ag-ui/client": "AgUIClient",
34
- "@segment/analytics-node": "SegmentAnalyticsNode",
35
27
  "partial-json": "PartialJSON",
36
28
  };
37
29
  return options;
@@ -1,15 +0,0 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
3
- const require_open_generative_ui_middleware = require('./open-generative-ui-middleware.cjs');
4
- const require_recorded_events = require('./recorded-events.cjs');
5
- let _ag_ui_a2ui_middleware = require("@ag-ui/a2ui-middleware");
6
-
7
- Object.defineProperty(exports, 'A2UIMiddleware', {
8
- enumerable: true,
9
- get: function () {
10
- return _ag_ui_a2ui_middleware.A2UIMiddleware;
11
- }
12
- });
13
- exports.ArgsParser = require_open_generative_ui_middleware.ArgsParser;
14
- exports.OpenGenerativeUIMiddleware = require_open_generative_ui_middleware.OpenGenerativeUIMiddleware;
15
- exports.transformRecordedEvents = require_recorded_events.transformRecordedEvents;
@@ -1,4 +0,0 @@
1
- import { ArgsParser, GenerateSandboxedUIParams, OnParamEvent, OpenGenerativeUIMiddleware } from "./open-generative-ui-middleware.cjs";
2
- import { RecordedEventTransformOptions, transformRecordedEvents } from "./recorded-events.cjs";
3
- import { A2UIMiddleware, A2UIMiddlewareConfig } from "@ag-ui/a2ui-middleware";
4
- export { A2UIMiddleware, type A2UIMiddlewareConfig, ArgsParser, GenerateSandboxedUIParams, OnParamEvent, OpenGenerativeUIMiddleware, RecordedEventTransformOptions, transformRecordedEvents };