@copilotkit/shared 0.0.0-0.0.0-max-changeset-10101010101010-20260109191632

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 (185) hide show
  1. package/CHANGELOG.md +1800 -0
  2. package/LICENSE +21 -0
  3. package/README.md +141 -0
  4. package/dist/chunk-2OJ5OJ2D.mjs +1 -0
  5. package/dist/chunk-2OJ5OJ2D.mjs.map +1 -0
  6. package/dist/chunk-5YXKLIUO.mjs +38 -0
  7. package/dist/chunk-5YXKLIUO.mjs.map +1 -0
  8. package/dist/chunk-64DYJ27R.mjs +201 -0
  9. package/dist/chunk-64DYJ27R.mjs.map +1 -0
  10. package/dist/chunk-6QGXWNS5.mjs +34 -0
  11. package/dist/chunk-6QGXWNS5.mjs.map +1 -0
  12. package/dist/chunk-6W2UEO2M.mjs +94 -0
  13. package/dist/chunk-6W2UEO2M.mjs.map +1 -0
  14. package/dist/chunk-BOKE4OKY.mjs +41 -0
  15. package/dist/chunk-BOKE4OKY.mjs.map +1 -0
  16. package/dist/chunk-FCCOSO5L.mjs +1 -0
  17. package/dist/chunk-FCCOSO5L.mjs.map +1 -0
  18. package/dist/chunk-GYZIHHE6.mjs +13 -0
  19. package/dist/chunk-GYZIHHE6.mjs.map +1 -0
  20. package/dist/chunk-IAFBVORQ.mjs +1 -0
  21. package/dist/chunk-IAFBVORQ.mjs.map +1 -0
  22. package/dist/chunk-MSUB6DGR.mjs +1 -0
  23. package/dist/chunk-MSUB6DGR.mjs.map +1 -0
  24. package/dist/chunk-N5EP5OD5.mjs +1 -0
  25. package/dist/chunk-N5EP5OD5.mjs.map +1 -0
  26. package/dist/chunk-NAFEBKSO.mjs +1 -0
  27. package/dist/chunk-NAFEBKSO.mjs.map +1 -0
  28. package/dist/chunk-NBLG4PC5.mjs +426 -0
  29. package/dist/chunk-NBLG4PC5.mjs.map +1 -0
  30. package/dist/chunk-P7STFMPO.mjs +1 -0
  31. package/dist/chunk-P7STFMPO.mjs.map +1 -0
  32. package/dist/chunk-PL5WNHFZ.mjs +50 -0
  33. package/dist/chunk-PL5WNHFZ.mjs.map +1 -0
  34. package/dist/chunk-UYARLQNA.mjs +1 -0
  35. package/dist/chunk-UYARLQNA.mjs.map +1 -0
  36. package/dist/chunk-WUPFNPXO.mjs +107 -0
  37. package/dist/chunk-WUPFNPXO.mjs.map +1 -0
  38. package/dist/chunk-XEMZTHQZ.mjs +67 -0
  39. package/dist/chunk-XEMZTHQZ.mjs.map +1 -0
  40. package/dist/chunk-XTHC46M2.mjs +1 -0
  41. package/dist/chunk-XTHC46M2.mjs.map +1 -0
  42. package/dist/constants/index.d.ts +6 -0
  43. package/dist/constants/index.js +40 -0
  44. package/dist/constants/index.js.map +1 -0
  45. package/dist/constants/index.mjs +13 -0
  46. package/dist/constants/index.mjs.map +1 -0
  47. package/dist/index.d.ts +25 -0
  48. package/dist/index.js +1098 -0
  49. package/dist/index.js.map +1 -0
  50. package/dist/index.mjs +124 -0
  51. package/dist/index.mjs.map +1 -0
  52. package/dist/telemetry/events.d.ts +45 -0
  53. package/dist/telemetry/events.js +19 -0
  54. package/dist/telemetry/events.js.map +1 -0
  55. package/dist/telemetry/events.mjs +1 -0
  56. package/dist/telemetry/events.mjs.map +1 -0
  57. package/dist/telemetry/index.d.ts +3 -0
  58. package/dist/telemetry/index.js +188 -0
  59. package/dist/telemetry/index.js.map +1 -0
  60. package/dist/telemetry/index.mjs +12 -0
  61. package/dist/telemetry/index.mjs.map +1 -0
  62. package/dist/telemetry/scarf-client.d.ts +7 -0
  63. package/dist/telemetry/scarf-client.js +59 -0
  64. package/dist/telemetry/scarf-client.js.map +1 -0
  65. package/dist/telemetry/scarf-client.mjs +7 -0
  66. package/dist/telemetry/scarf-client.mjs.map +1 -0
  67. package/dist/telemetry/security-check.d.ts +2 -0
  68. package/dist/telemetry/security-check.js +2 -0
  69. package/dist/telemetry/security-check.js.map +1 -0
  70. package/dist/telemetry/security-check.mjs +1 -0
  71. package/dist/telemetry/security-check.mjs.map +1 -0
  72. package/dist/telemetry/telemetry-client.d.ts +40 -0
  73. package/dist/telemetry/telemetry-client.js +186 -0
  74. package/dist/telemetry/telemetry-client.js.map +1 -0
  75. package/dist/telemetry/telemetry-client.mjs +11 -0
  76. package/dist/telemetry/telemetry-client.mjs.map +1 -0
  77. package/dist/telemetry/utils.d.ts +8 -0
  78. package/dist/telemetry/utils.js +69 -0
  79. package/dist/telemetry/utils.js.map +1 -0
  80. package/dist/telemetry/utils.mjs +9 -0
  81. package/dist/telemetry/utils.mjs.map +1 -0
  82. package/dist/types/action.d.ts +59 -0
  83. package/dist/types/action.js +19 -0
  84. package/dist/types/action.js.map +1 -0
  85. package/dist/types/action.mjs +2 -0
  86. package/dist/types/action.mjs.map +1 -0
  87. package/dist/types/copilot-cloud-config.d.ts +13 -0
  88. package/dist/types/copilot-cloud-config.js +19 -0
  89. package/dist/types/copilot-cloud-config.js.map +1 -0
  90. package/dist/types/copilot-cloud-config.mjs +2 -0
  91. package/dist/types/copilot-cloud-config.mjs.map +1 -0
  92. package/dist/types/error.d.ts +55 -0
  93. package/dist/types/error.js +19 -0
  94. package/dist/types/error.js.map +1 -0
  95. package/dist/types/error.mjs +2 -0
  96. package/dist/types/error.mjs.map +1 -0
  97. package/dist/types/index.d.ts +7 -0
  98. package/dist/types/index.js +19 -0
  99. package/dist/types/index.js.map +1 -0
  100. package/dist/types/index.mjs +8 -0
  101. package/dist/types/index.mjs.map +1 -0
  102. package/dist/types/message.d.ts +26 -0
  103. package/dist/types/message.js +19 -0
  104. package/dist/types/message.js.map +1 -0
  105. package/dist/types/message.mjs +2 -0
  106. package/dist/types/message.mjs.map +1 -0
  107. package/dist/types/openai-assistant.d.ts +53 -0
  108. package/dist/types/openai-assistant.js +19 -0
  109. package/dist/types/openai-assistant.js.map +1 -0
  110. package/dist/types/openai-assistant.mjs +2 -0
  111. package/dist/types/openai-assistant.mjs.map +1 -0
  112. package/dist/types/utility.d.ts +6 -0
  113. package/dist/types/utility.js +19 -0
  114. package/dist/types/utility.js.map +1 -0
  115. package/dist/types/utility.mjs +2 -0
  116. package/dist/types/utility.mjs.map +1 -0
  117. package/dist/utils/conditions.d.ts +26 -0
  118. package/dist/utils/conditions.js +74 -0
  119. package/dist/utils/conditions.js.map +1 -0
  120. package/dist/utils/conditions.mjs +7 -0
  121. package/dist/utils/conditions.mjs.map +1 -0
  122. package/dist/utils/console-styling.d.ts +84 -0
  123. package/dist/utils/console-styling.js +123 -0
  124. package/dist/utils/console-styling.js.map +1 -0
  125. package/dist/utils/console-styling.mjs +17 -0
  126. package/dist/utils/console-styling.mjs.map +1 -0
  127. package/dist/utils/errors.d.ts +280 -0
  128. package/dist/utils/errors.js +443 -0
  129. package/dist/utils/errors.js.map +1 -0
  130. package/dist/utils/errors.mjs +65 -0
  131. package/dist/utils/errors.mjs.map +1 -0
  132. package/dist/utils/index.d.ts +32 -0
  133. package/dist/utils/index.js +927 -0
  134. package/dist/utils/index.js.map +1 -0
  135. package/dist/utils/index.mjs +108 -0
  136. package/dist/utils/index.mjs.map +1 -0
  137. package/dist/utils/json-schema.d.ts +34 -0
  138. package/dist/utils/json-schema.js +228 -0
  139. package/dist/utils/json-schema.js.map +1 -0
  140. package/dist/utils/json-schema.mjs +13 -0
  141. package/dist/utils/json-schema.mjs.map +1 -0
  142. package/dist/utils/random-id.d.ts +6 -0
  143. package/dist/utils/random-id.js +68 -0
  144. package/dist/utils/random-id.js.map +1 -0
  145. package/dist/utils/random-id.mjs +13 -0
  146. package/dist/utils/random-id.mjs.map +1 -0
  147. package/dist/utils/requests.d.ts +10 -0
  148. package/dist/utils/requests.js +91 -0
  149. package/dist/utils/requests.js.map +1 -0
  150. package/dist/utils/requests.mjs +7 -0
  151. package/dist/utils/requests.mjs.map +1 -0
  152. package/dist/utils/types.d.ts +20 -0
  153. package/dist/utils/types.js +19 -0
  154. package/dist/utils/types.js.map +1 -0
  155. package/dist/utils/types.mjs +2 -0
  156. package/dist/utils/types.mjs.map +1 -0
  157. package/jest.config.js +5 -0
  158. package/package.json +68 -0
  159. package/src/constants/index.ts +4 -0
  160. package/src/index.ts +7 -0
  161. package/src/telemetry/events.ts +41 -0
  162. package/src/telemetry/index.ts +1 -0
  163. package/src/telemetry/scarf-client.ts +39 -0
  164. package/src/telemetry/security-check.ts +0 -0
  165. package/src/telemetry/telemetry-client.ts +142 -0
  166. package/src/telemetry/utils.ts +39 -0
  167. package/src/types/action.ts +129 -0
  168. package/src/types/copilot-cloud-config.ts +11 -0
  169. package/src/types/error.ts +70 -0
  170. package/src/types/index.ts +6 -0
  171. package/src/types/message.ts +37 -0
  172. package/src/types/openai-assistant.ts +62 -0
  173. package/src/types/utility.ts +2 -0
  174. package/src/utils/conditions.ts +98 -0
  175. package/src/utils/console-styling.ts +124 -0
  176. package/src/utils/errors.ts +525 -0
  177. package/src/utils/index.ts +49 -0
  178. package/src/utils/json-schema.test.ts +383 -0
  179. package/src/utils/json-schema.ts +258 -0
  180. package/src/utils/random-id.ts +42 -0
  181. package/src/utils/requests.ts +77 -0
  182. package/src/utils/types.ts +22 -0
  183. package/tsconfig.json +9 -0
  184. package/tsup.config.ts +11 -0
  185. package/typedoc.json +4 -0
@@ -0,0 +1,383 @@
1
+ import { z } from "zod";
2
+ import {
3
+ convertJsonSchemaToZodSchema,
4
+ actionParametersToJsonSchema,
5
+ jsonSchemaToActionParameters,
6
+ JSONSchema,
7
+ } from "../utils/json-schema";
8
+ import { zodToJsonSchema } from "zod-to-json-schema";
9
+ import { Parameter } from "../types";
10
+
11
+ describe("convertJsonSchemaToZodSchema", () => {
12
+ it("should convert a simple JSON schema to a Zod schema", () => {
13
+ const jsonSchema = {
14
+ type: "object",
15
+ properties: {
16
+ name: { type: "string" },
17
+ age: { type: "number" },
18
+ },
19
+ required: ["name", "age"],
20
+ };
21
+
22
+ const expectedSchema = z.object({
23
+ name: z.string(),
24
+ age: z.number(),
25
+ });
26
+
27
+ const result = convertJsonSchemaToZodSchema(jsonSchema, true);
28
+ const resultSchemaJson = zodToJsonSchema(result);
29
+ const expectedSchemaJson = zodToJsonSchema(expectedSchema);
30
+
31
+ expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
32
+ });
33
+
34
+ it("should convert a JSON schema with nested objects to a Zod schema", () => {
35
+ const jsonSchema = {
36
+ type: "object",
37
+ properties: {
38
+ name: { type: "string" },
39
+ address: {
40
+ type: "object",
41
+ properties: {
42
+ street: { type: "string" },
43
+ city: { type: "string" },
44
+ },
45
+ required: ["street", "city"],
46
+ },
47
+ },
48
+ required: ["name", "address"],
49
+ };
50
+
51
+ const expectedSchema = z.object({
52
+ name: z.string(),
53
+ address: z.object({
54
+ street: z.string(),
55
+ city: z.string(),
56
+ }),
57
+ });
58
+
59
+ const result = convertJsonSchemaToZodSchema(jsonSchema, true);
60
+ const resultSchemaJson = zodToJsonSchema(result);
61
+ const expectedSchemaJson = zodToJsonSchema(expectedSchema);
62
+
63
+ expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
64
+ });
65
+
66
+ it("should convert a JSON schema with arrays to a Zod schema", () => {
67
+ const jsonSchema = {
68
+ type: "object",
69
+ properties: {
70
+ names: {
71
+ type: "array",
72
+ items: { type: "string" },
73
+ },
74
+ },
75
+ required: ["names"],
76
+ };
77
+
78
+ const expectedSchema = z.object({
79
+ names: z.array(z.string()),
80
+ });
81
+
82
+ const result = convertJsonSchemaToZodSchema(jsonSchema, true);
83
+ const resultSchemaJson = zodToJsonSchema(result);
84
+ const expectedSchemaJson = zodToJsonSchema(expectedSchema);
85
+
86
+ expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
87
+ });
88
+
89
+ it("should convert a JSON schema with optional properties to a Zod schema", () => {
90
+ const jsonSchema = {
91
+ type: "object",
92
+ properties: {
93
+ name: { type: "string" },
94
+ age: { type: "number", required: false },
95
+ },
96
+ };
97
+
98
+ const expectedSchema = z
99
+ .object({
100
+ name: z.string().optional(),
101
+ age: z.number().optional(),
102
+ })
103
+ .optional();
104
+
105
+ const result = convertJsonSchemaToZodSchema(jsonSchema, false);
106
+
107
+ console.log(convertJsonSchemaToZodSchema(jsonSchema, false));
108
+
109
+ const resultSchemaJson = zodToJsonSchema(result);
110
+ const expectedSchemaJson = zodToJsonSchema(expectedSchema);
111
+
112
+ expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
113
+ });
114
+
115
+ it("should convert a JSON schema with different types to a Zod schema", () => {
116
+ const jsonSchema = {
117
+ type: "object",
118
+ properties: {
119
+ name: { type: "string" },
120
+ age: { type: "number" },
121
+ isAdmin: { type: "boolean" },
122
+ },
123
+ required: ["name", "age", "isAdmin"],
124
+ };
125
+
126
+ const expectedSchema = z.object({
127
+ name: z.string(),
128
+ age: z.number(),
129
+ isAdmin: z.boolean(),
130
+ });
131
+
132
+ const result = convertJsonSchemaToZodSchema(jsonSchema, true);
133
+ const resultSchemaJson = zodToJsonSchema(result);
134
+ const expectedSchemaJson = zodToJsonSchema(expectedSchema);
135
+
136
+ expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
137
+ });
138
+
139
+ it("should handle edge case where JSON schema has no properties", () => {
140
+ const jsonSchema = {
141
+ type: "object",
142
+ };
143
+
144
+ const expectedSchema = z.object({});
145
+
146
+ const result = convertJsonSchemaToZodSchema(jsonSchema, true);
147
+ const resultSchemaJson = zodToJsonSchema(result);
148
+ const expectedSchemaJson = zodToJsonSchema(expectedSchema);
149
+
150
+ expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
151
+ });
152
+
153
+ it("should handle edge case where JSON schema has no required properties", () => {
154
+ const jsonSchema = {
155
+ type: "object",
156
+ properties: {
157
+ name: { type: "string" },
158
+ age: { type: "number" },
159
+ },
160
+ };
161
+
162
+ const expectedSchema = z
163
+ .object({
164
+ name: z.string().optional(),
165
+ age: z.number().optional(),
166
+ })
167
+ .optional();
168
+
169
+ const result = convertJsonSchemaToZodSchema(jsonSchema, false);
170
+ const resultSchemaJson = zodToJsonSchema(result);
171
+ const expectedSchemaJson = zodToJsonSchema(expectedSchema);
172
+
173
+ expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
174
+ });
175
+ });
176
+
177
+ describe("jsonSchemaToActionParameters", () => {
178
+ it("should convert a simple JSONSchema to Parameter array", () => {
179
+ const jsonSchema: JSONSchema = {
180
+ type: "object",
181
+ properties: {
182
+ name: { type: "string", description: "User name" },
183
+ age: { type: "number", description: "User age" },
184
+ },
185
+ required: ["name"],
186
+ };
187
+
188
+ const expectedParameters: Parameter[] = [
189
+ { name: "name", type: "string", description: "User name" },
190
+ { name: "age", type: "number", description: "User age", required: false },
191
+ ];
192
+
193
+ const result = jsonSchemaToActionParameters(jsonSchema);
194
+ expect(result).toEqual(expectedParameters);
195
+ });
196
+
197
+ it("should convert JSONSchema with enum to Parameter array", () => {
198
+ const jsonSchema: JSONSchema = {
199
+ type: "object",
200
+ properties: {
201
+ role: { type: "string", enum: ["admin", "user", "guest"], description: "User role" },
202
+ },
203
+ required: ["role"],
204
+ };
205
+
206
+ const expectedParameters: Parameter[] = [
207
+ { name: "role", type: "string", enum: ["admin", "user", "guest"], description: "User role" },
208
+ ];
209
+
210
+ const result = jsonSchemaToActionParameters(jsonSchema);
211
+ expect(result).toEqual(expectedParameters);
212
+ });
213
+
214
+ it("should convert nested object JSONSchema to Parameter array", () => {
215
+ const jsonSchema: JSONSchema = {
216
+ type: "object",
217
+ properties: {
218
+ user: {
219
+ type: "object",
220
+ properties: {
221
+ name: { type: "string", description: "User name" },
222
+ age: { type: "number", description: "User age" },
223
+ },
224
+ required: ["name"],
225
+ description: "User information",
226
+ },
227
+ },
228
+ required: ["user"],
229
+ };
230
+
231
+ const expectedParameters: Parameter[] = [
232
+ {
233
+ name: "user",
234
+ type: "object",
235
+ description: "User information",
236
+ attributes: [
237
+ { name: "name", type: "string", description: "User name" },
238
+ { name: "age", type: "number", description: "User age", required: false },
239
+ ],
240
+ },
241
+ ];
242
+
243
+ const result = jsonSchemaToActionParameters(jsonSchema);
244
+ expect(result).toEqual(expectedParameters);
245
+ });
246
+
247
+ it("should convert array JSONSchema to Parameter array", () => {
248
+ const jsonSchema: JSONSchema = {
249
+ type: "object",
250
+ properties: {
251
+ tags: {
252
+ type: "array",
253
+ items: { type: "string" },
254
+ description: "User tags",
255
+ },
256
+ },
257
+ required: ["tags"],
258
+ };
259
+
260
+ const expectedParameters: Parameter[] = [
261
+ { name: "tags", type: "string[]", description: "User tags" },
262
+ ];
263
+
264
+ const result = jsonSchemaToActionParameters(jsonSchema);
265
+ expect(result).toEqual(expectedParameters);
266
+ });
267
+
268
+ it("should convert object array JSONSchema to Parameter array", () => {
269
+ const jsonSchema: JSONSchema = {
270
+ type: "object",
271
+ properties: {
272
+ addresses: {
273
+ type: "array",
274
+ items: {
275
+ type: "object",
276
+ properties: {
277
+ street: { type: "string", description: "Street name" },
278
+ city: { type: "string", description: "City name" },
279
+ },
280
+ required: ["street"],
281
+ },
282
+ description: "User addresses",
283
+ },
284
+ },
285
+ required: ["addresses"],
286
+ };
287
+
288
+ const expectedParameters: Parameter[] = [
289
+ {
290
+ name: "addresses",
291
+ type: "object[]",
292
+ description: "User addresses",
293
+ attributes: [
294
+ { name: "street", type: "string", description: "Street name" },
295
+ { name: "city", type: "string", description: "City name", required: false },
296
+ ],
297
+ },
298
+ ];
299
+
300
+ const result = jsonSchemaToActionParameters(jsonSchema);
301
+ expect(result).toEqual(expectedParameters);
302
+ });
303
+
304
+ it("should handle boolean types", () => {
305
+ const jsonSchema: JSONSchema = {
306
+ type: "object",
307
+ properties: {
308
+ isAdmin: { type: "boolean", description: "Is user an admin" },
309
+ },
310
+ required: ["isAdmin"],
311
+ };
312
+
313
+ const expectedParameters: Parameter[] = [
314
+ { name: "isAdmin", type: "boolean", description: "Is user an admin" },
315
+ ];
316
+
317
+ const result = jsonSchemaToActionParameters(jsonSchema);
318
+ expect(result).toEqual(expectedParameters);
319
+ });
320
+
321
+ it("should handle empty object schema", () => {
322
+ const jsonSchema: JSONSchema = {
323
+ type: "object",
324
+ };
325
+
326
+ const expectedParameters: Parameter[] = [];
327
+
328
+ const result = jsonSchemaToActionParameters(jsonSchema);
329
+ expect(result).toEqual(expectedParameters);
330
+ });
331
+
332
+ it("should throw error for nested arrays", () => {
333
+ const jsonSchema: JSONSchema = {
334
+ type: "object",
335
+ properties: {
336
+ nestedArray: {
337
+ type: "array",
338
+ items: {
339
+ type: "array",
340
+ items: { type: "string" },
341
+ },
342
+ description: "Matrix of strings",
343
+ },
344
+ },
345
+ required: ["nestedArray"],
346
+ };
347
+
348
+ expect(() => jsonSchemaToActionParameters(jsonSchema)).toThrow(
349
+ "Nested arrays are not supported",
350
+ );
351
+ });
352
+
353
+ it("should ensure round-trip conversion works", () => {
354
+ const originalParameters: Parameter[] = [
355
+ { name: "name", type: "string", description: "User name" },
356
+ { name: "age", type: "number", description: "User age", required: false },
357
+ { name: "role", type: "string", enum: ["admin", "user"], description: "User role" },
358
+ {
359
+ name: "address",
360
+ type: "object",
361
+ description: "User address",
362
+ attributes: [
363
+ { name: "street", type: "string", description: "Street name" },
364
+ { name: "city", type: "string", description: "City name" },
365
+ ],
366
+ },
367
+ {
368
+ name: "contacts",
369
+ type: "object[]",
370
+ description: "User contacts",
371
+ attributes: [
372
+ { name: "type", type: "string", description: "Contact type" },
373
+ { name: "value", type: "string", description: "Contact value" },
374
+ ],
375
+ },
376
+ ];
377
+
378
+ const jsonSchema = actionParametersToJsonSchema(originalParameters);
379
+ const roundTripParameters = jsonSchemaToActionParameters(jsonSchema);
380
+
381
+ expect(roundTripParameters).toEqual(originalParameters);
382
+ });
383
+ });
@@ -0,0 +1,258 @@
1
+ import { z } from "zod";
2
+ import { Parameter } from "../types";
3
+
4
+ export type JSONSchemaString = {
5
+ type: "string";
6
+ description?: string;
7
+ enum?: string[];
8
+ };
9
+
10
+ export type JSONSchemaNumber = {
11
+ type: "number";
12
+ description?: string;
13
+ };
14
+
15
+ export type JSONSchemaBoolean = {
16
+ type: "boolean";
17
+ description?: string;
18
+ };
19
+
20
+ export type JSONSchemaObject = {
21
+ type: "object";
22
+ properties?: Record<string, JSONSchema>;
23
+ required?: string[];
24
+ description?: string;
25
+ };
26
+
27
+ export type JSONSchemaArray = {
28
+ type: "array";
29
+ items: JSONSchema;
30
+ description?: string;
31
+ };
32
+
33
+ export type JSONSchema =
34
+ | JSONSchemaString
35
+ | JSONSchemaNumber
36
+ | JSONSchemaBoolean
37
+ | JSONSchemaObject
38
+ | JSONSchemaArray;
39
+
40
+ export function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema {
41
+ // Create the parameters object based on the argumentAnnotations
42
+ let parameters: { [key: string]: any } = {};
43
+ for (let parameter of actionParameters || []) {
44
+ parameters[parameter.name] = convertAttribute(parameter);
45
+ }
46
+
47
+ let requiredParameterNames: string[] = [];
48
+ for (let arg of actionParameters || []) {
49
+ if (arg.required !== false) {
50
+ requiredParameterNames.push(arg.name);
51
+ }
52
+ }
53
+
54
+ // Create the ChatCompletionFunctions object
55
+ return {
56
+ type: "object",
57
+ properties: parameters,
58
+ required: requiredParameterNames,
59
+ };
60
+ }
61
+
62
+ // Convert JSONSchema to Parameter[]
63
+ export function jsonSchemaToActionParameters(jsonSchema: JSONSchema): Parameter[] {
64
+ if (jsonSchema.type !== "object" || !jsonSchema.properties) {
65
+ return [];
66
+ }
67
+
68
+ const parameters: Parameter[] = [];
69
+ const requiredFields = jsonSchema.required || [];
70
+
71
+ for (const [name, schema] of Object.entries(jsonSchema.properties)) {
72
+ const parameter = convertJsonSchemaToParameter(name, schema, requiredFields.includes(name));
73
+ parameters.push(parameter);
74
+ }
75
+
76
+ return parameters;
77
+ }
78
+
79
+ // Convert JSONSchema property to Parameter
80
+ function convertJsonSchemaToParameter(
81
+ name: string,
82
+ schema: JSONSchema,
83
+ isRequired: boolean,
84
+ ): Parameter {
85
+ const baseParameter: Parameter = {
86
+ name,
87
+ description: schema.description,
88
+ };
89
+
90
+ if (!isRequired) {
91
+ baseParameter.required = false;
92
+ }
93
+
94
+ switch (schema.type) {
95
+ case "string":
96
+ return {
97
+ ...baseParameter,
98
+ type: "string",
99
+ ...(schema.enum && { enum: schema.enum }),
100
+ };
101
+ case "number":
102
+ case "boolean":
103
+ return {
104
+ ...baseParameter,
105
+ type: schema.type,
106
+ };
107
+ case "object":
108
+ if (schema.properties) {
109
+ const attributes: Parameter[] = [];
110
+ const requiredFields = schema.required || [];
111
+
112
+ for (const [propName, propSchema] of Object.entries(schema.properties)) {
113
+ attributes.push(
114
+ convertJsonSchemaToParameter(propName, propSchema, requiredFields.includes(propName)),
115
+ );
116
+ }
117
+
118
+ return {
119
+ ...baseParameter,
120
+ type: "object",
121
+ attributes,
122
+ };
123
+ }
124
+ return {
125
+ ...baseParameter,
126
+ type: "object",
127
+ };
128
+ case "array":
129
+ if (schema.items.type === "object" && "properties" in schema.items) {
130
+ const attributes: Parameter[] = [];
131
+ const requiredFields = schema.items.required || [];
132
+
133
+ for (const [propName, propSchema] of Object.entries(schema.items.properties || {})) {
134
+ attributes.push(
135
+ convertJsonSchemaToParameter(propName, propSchema, requiredFields.includes(propName)),
136
+ );
137
+ }
138
+
139
+ return {
140
+ ...baseParameter,
141
+ type: "object[]",
142
+ attributes,
143
+ };
144
+ } else if (schema.items.type === "array") {
145
+ throw new Error("Nested arrays are not supported");
146
+ } else {
147
+ return {
148
+ ...baseParameter,
149
+ type: `${schema.items.type}[]`,
150
+ };
151
+ }
152
+ default:
153
+ return {
154
+ ...baseParameter,
155
+ type: "string",
156
+ };
157
+ }
158
+ }
159
+
160
+ function convertAttribute(attribute: Parameter): JSONSchema {
161
+ switch (attribute.type) {
162
+ case "string":
163
+ return {
164
+ type: "string",
165
+ description: attribute.description,
166
+ ...(attribute.enum && { enum: attribute.enum }),
167
+ };
168
+ case "number":
169
+ case "boolean":
170
+ return {
171
+ type: attribute.type,
172
+ description: attribute.description,
173
+ };
174
+ case "object":
175
+ case "object[]":
176
+ const properties = attribute.attributes?.reduce(
177
+ (acc, attr) => {
178
+ acc[attr.name] = convertAttribute(attr);
179
+ return acc;
180
+ },
181
+ {} as Record<string, any>,
182
+ );
183
+ const required = attribute.attributes
184
+ ?.filter((attr) => attr.required !== false)
185
+ .map((attr) => attr.name);
186
+ if (attribute.type === "object[]") {
187
+ return {
188
+ type: "array",
189
+ items: {
190
+ type: "object",
191
+ ...(properties && { properties }),
192
+ ...(required && required.length > 0 && { required }),
193
+ },
194
+ description: attribute.description,
195
+ };
196
+ }
197
+ return {
198
+ type: "object",
199
+ description: attribute.description,
200
+ ...(properties && { properties }),
201
+ ...(required && required.length > 0 && { required }),
202
+ };
203
+ default:
204
+ // Handle arrays of primitive types and undefined attribute.type
205
+ if (attribute.type?.endsWith("[]")) {
206
+ const itemType = attribute.type.slice(0, -2);
207
+ return {
208
+ type: "array",
209
+ items: { type: itemType as any },
210
+ description: attribute.description,
211
+ };
212
+ }
213
+ // Fallback for undefined type or any other unexpected type
214
+ return {
215
+ type: "string",
216
+ description: attribute.description,
217
+ };
218
+ }
219
+ }
220
+
221
+ export function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema {
222
+ if (jsonSchema.type === "object") {
223
+ const spec: { [key: string]: z.ZodSchema } = {};
224
+
225
+ if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
226
+ return !required ? z.object(spec).optional() : z.object(spec);
227
+ }
228
+
229
+ for (const [key, value] of Object.entries(jsonSchema.properties)) {
230
+ spec[key] = convertJsonSchemaToZodSchema(
231
+ value,
232
+ jsonSchema.required ? jsonSchema.required.includes(key) : false,
233
+ );
234
+ }
235
+ let schema = z.object(spec).describe(jsonSchema.description);
236
+ return required ? schema : schema.optional();
237
+ } else if (jsonSchema.type === "string") {
238
+ let schema = z.string().describe(jsonSchema.description);
239
+ return required ? schema : schema.optional();
240
+ } else if (jsonSchema.type === "number") {
241
+ let schema = z.number().describe(jsonSchema.description);
242
+ return required ? schema : schema.optional();
243
+ } else if (jsonSchema.type === "boolean") {
244
+ let schema = z.boolean().describe(jsonSchema.description);
245
+ return required ? schema : schema.optional();
246
+ } else if (jsonSchema.type === "array") {
247
+ let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
248
+ let schema = z.array(itemSchema).describe(jsonSchema.description);
249
+ return required ? schema : schema.optional();
250
+ }
251
+ throw new Error("Invalid JSON schema");
252
+ }
253
+
254
+ export function getZodParameters<T extends [] | Parameter[] | undefined>(parameters: T): any {
255
+ if (!parameters) return z.object({});
256
+ const jsonParams = actionParametersToJsonSchema(parameters);
257
+ return convertJsonSchemaToZodSchema(jsonParams, true);
258
+ }
@@ -0,0 +1,42 @@
1
+ import { v4 as uuidv4, validate, v5 as uuidv5 } from "uuid";
2
+
3
+ export function randomId() {
4
+ return "ck-" + uuidv4();
5
+ }
6
+
7
+ export function randomUUID() {
8
+ return uuidv4();
9
+ }
10
+
11
+ /**
12
+ * Recursively converts an object to a serializable form by converting functions to their string representation.
13
+ */
14
+ function toSerializable(value: unknown): unknown {
15
+ if (typeof value === "function") {
16
+ return value.toString();
17
+ }
18
+ if (Array.isArray(value)) {
19
+ return value.map(toSerializable);
20
+ }
21
+ if (value !== null && typeof value === "object") {
22
+ const result: Record<string, unknown> = {};
23
+ for (const key of Object.keys(value)) {
24
+ result[key] = toSerializable((value as Record<string, unknown>)[key]);
25
+ }
26
+ return result;
27
+ }
28
+ return value;
29
+ }
30
+
31
+ export function dataToUUID(input: string | object, namespace?: string): string {
32
+ const BASE_NAMESPACE = "e4b01160-ff74-4c6e-9b27-d53cd930fe8e";
33
+ // Since namespace needs to be a uuid, we are creating a uuid for it.
34
+ const boundNamespace = namespace ? uuidv5(namespace, BASE_NAMESPACE) : BASE_NAMESPACE;
35
+
36
+ const stringInput = typeof input === "string" ? input : JSON.stringify(toSerializable(input));
37
+ return uuidv5(stringInput, boundNamespace);
38
+ }
39
+
40
+ export function isValidUUID(uuid: string) {
41
+ return validate(uuid);
42
+ }