@copilotkit/shared 1.5.9 → 1.5.10-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/dist/chunk-6QGXWNS5.mjs +34 -0
  3. package/dist/chunk-6QGXWNS5.mjs.map +1 -0
  4. package/dist/chunk-BANDZXMP.mjs +1 -0
  5. package/dist/chunk-BANDZXMP.mjs.map +1 -0
  6. package/dist/chunk-CYDWEPFL.mjs +1 -0
  7. package/dist/chunk-CYDWEPFL.mjs.map +1 -0
  8. package/dist/chunk-FCCOSO5L.mjs +1 -0
  9. package/dist/chunk-FCCOSO5L.mjs.map +1 -0
  10. package/dist/chunk-GYZIHHE6.mjs +13 -0
  11. package/dist/chunk-GYZIHHE6.mjs.map +1 -0
  12. package/dist/chunk-IAFBVORQ.mjs +1 -0
  13. package/dist/chunk-IAFBVORQ.mjs.map +1 -0
  14. package/dist/chunk-JP2M4U4G.mjs +112 -0
  15. package/dist/chunk-JP2M4U4G.mjs.map +1 -0
  16. package/dist/chunk-MSUB6DGR.mjs +1 -0
  17. package/dist/chunk-MSUB6DGR.mjs.map +1 -0
  18. package/dist/chunk-NAFEBKSO.mjs +1 -0
  19. package/dist/chunk-NAFEBKSO.mjs.map +1 -0
  20. package/dist/chunk-P7STFMPO.mjs +1 -0
  21. package/dist/chunk-P7STFMPO.mjs.map +1 -0
  22. package/dist/chunk-QFITLWYZ.mjs +96 -0
  23. package/dist/chunk-QFITLWYZ.mjs.map +1 -0
  24. package/dist/chunk-RIPX6APP.mjs +10 -0
  25. package/dist/chunk-RIPX6APP.mjs.map +1 -0
  26. package/dist/constants/index.d.ts +6 -0
  27. package/dist/constants/index.js +40 -0
  28. package/dist/constants/index.js.map +1 -0
  29. package/dist/constants/index.mjs +13 -0
  30. package/dist/constants/index.mjs.map +1 -0
  31. package/dist/index.d.ts +15 -0
  32. package/dist/index.js +285 -0
  33. package/dist/index.js.map +1 -0
  34. package/dist/index.mjs +42 -0
  35. package/dist/index.mjs.map +1 -0
  36. package/dist/telemetry/events.d.ts +39 -0
  37. package/dist/telemetry/events.js +19 -0
  38. package/dist/telemetry/events.js.map +1 -0
  39. package/dist/telemetry/events.mjs +1 -0
  40. package/dist/telemetry/events.mjs.map +1 -0
  41. package/dist/telemetry/index.d.ts +3 -0
  42. package/dist/telemetry/index.js +144 -0
  43. package/dist/telemetry/index.js.map +1 -0
  44. package/dist/telemetry/index.mjs +9 -0
  45. package/dist/telemetry/index.mjs.map +1 -0
  46. package/dist/telemetry/security-check.d.ts +2 -0
  47. package/dist/telemetry/security-check.js +2 -0
  48. package/dist/telemetry/security-check.js.map +1 -0
  49. package/dist/telemetry/security-check.mjs +1 -0
  50. package/dist/telemetry/security-check.mjs.map +1 -0
  51. package/dist/telemetry/telemetry-client.d.ts +33 -0
  52. package/dist/telemetry/telemetry-client.js +142 -0
  53. package/dist/telemetry/telemetry-client.js.map +1 -0
  54. package/dist/telemetry/telemetry-client.mjs +8 -0
  55. package/dist/telemetry/telemetry-client.mjs.map +1 -0
  56. package/dist/telemetry/utils.d.ts +8 -0
  57. package/dist/telemetry/utils.js +69 -0
  58. package/dist/telemetry/utils.js.map +1 -0
  59. package/dist/telemetry/utils.mjs +9 -0
  60. package/dist/telemetry/utils.mjs.map +1 -0
  61. package/dist/types/action.d.ts +58 -0
  62. package/dist/types/action.js +19 -0
  63. package/dist/types/action.js.map +1 -0
  64. package/dist/types/action.mjs +2 -0
  65. package/dist/types/action.mjs.map +1 -0
  66. package/dist/types/copilot-cloud-config.d.ts +13 -0
  67. package/dist/types/copilot-cloud-config.js +19 -0
  68. package/dist/types/copilot-cloud-config.js.map +1 -0
  69. package/dist/types/copilot-cloud-config.mjs +2 -0
  70. package/dist/types/copilot-cloud-config.mjs.map +1 -0
  71. package/dist/types/index.d.ts +4 -0
  72. package/dist/types/index.js +19 -0
  73. package/dist/types/index.js.map +1 -0
  74. package/dist/types/index.mjs +6 -0
  75. package/dist/types/index.mjs.map +1 -0
  76. package/dist/types/openai-assistant.d.ts +53 -0
  77. package/dist/types/openai-assistant.js +19 -0
  78. package/dist/types/openai-assistant.js.map +1 -0
  79. package/dist/types/openai-assistant.mjs +2 -0
  80. package/dist/types/openai-assistant.mjs.map +1 -0
  81. package/dist/types/utility.d.ts +3 -0
  82. package/dist/types/utility.js +19 -0
  83. package/dist/types/utility.js.map +1 -0
  84. package/dist/types/utility.mjs +2 -0
  85. package/dist/types/utility.mjs.map +1 -0
  86. package/dist/utils/index.d.ts +4 -0
  87. package/dist/utils/index.js +147 -0
  88. package/dist/utils/index.js.map +1 -0
  89. package/dist/utils/index.mjs +14 -0
  90. package/dist/utils/index.mjs.map +1 -0
  91. package/dist/utils/json-schema.d.ts +32 -0
  92. package/dist/utils/json-schema.js +137 -0
  93. package/dist/utils/json-schema.js.map +1 -0
  94. package/dist/utils/json-schema.mjs +9 -0
  95. package/dist/utils/json-schema.mjs.map +1 -0
  96. package/dist/utils/json-schema.test.d.ts +2 -0
  97. package/dist/utils/json-schema.test.js +173 -0
  98. package/dist/utils/json-schema.test.js.map +1 -0
  99. package/dist/utils/json-schema.test.mjs +140 -0
  100. package/dist/utils/json-schema.test.mjs.map +1 -0
  101. package/dist/utils/random-id.d.ts +3 -0
  102. package/dist/utils/random-id.js +34 -0
  103. package/dist/utils/random-id.js.map +1 -0
  104. package/dist/utils/random-id.mjs +7 -0
  105. package/dist/utils/random-id.mjs.map +1 -0
  106. package/package.json +1 -1
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/utils/index.ts
21
+ var utils_exports = {};
22
+ __export(utils_exports, {
23
+ actionParametersToJsonSchema: () => actionParametersToJsonSchema,
24
+ convertJsonSchemaToZodSchema: () => convertJsonSchemaToZodSchema,
25
+ randomId: () => randomId
26
+ });
27
+ module.exports = __toCommonJS(utils_exports);
28
+
29
+ // src/utils/json-schema.ts
30
+ var import_zod = require("zod");
31
+ function actionParametersToJsonSchema(actionParameters) {
32
+ let parameters = {};
33
+ for (let parameter of actionParameters || []) {
34
+ parameters[parameter.name] = convertAttribute(parameter);
35
+ }
36
+ let requiredParameterNames = [];
37
+ for (let arg of actionParameters || []) {
38
+ if (arg.required !== false) {
39
+ requiredParameterNames.push(arg.name);
40
+ }
41
+ }
42
+ return {
43
+ type: "object",
44
+ properties: parameters,
45
+ required: requiredParameterNames
46
+ };
47
+ }
48
+ function convertAttribute(attribute) {
49
+ var _a, _b, _c;
50
+ switch (attribute.type) {
51
+ case "string":
52
+ return {
53
+ type: "string",
54
+ description: attribute.description,
55
+ ...attribute.enum && { enum: attribute.enum }
56
+ };
57
+ case "number":
58
+ case "boolean":
59
+ return {
60
+ type: attribute.type,
61
+ description: attribute.description
62
+ };
63
+ case "object":
64
+ case "object[]":
65
+ const properties = (_a = attribute.attributes) == null ? void 0 : _a.reduce(
66
+ (acc, attr) => {
67
+ acc[attr.name] = convertAttribute(attr);
68
+ return acc;
69
+ },
70
+ {}
71
+ );
72
+ const required = (_b = attribute.attributes) == null ? void 0 : _b.filter((attr) => attr.required !== false).map((attr) => attr.name);
73
+ if (attribute.type === "object[]") {
74
+ return {
75
+ type: "array",
76
+ items: {
77
+ type: "object",
78
+ ...properties && { properties },
79
+ ...required && required.length > 0 && { required }
80
+ },
81
+ description: attribute.description
82
+ };
83
+ }
84
+ return {
85
+ type: "object",
86
+ description: attribute.description,
87
+ ...properties && { properties },
88
+ ...required && required.length > 0 && { required }
89
+ };
90
+ default:
91
+ if ((_c = attribute.type) == null ? void 0 : _c.endsWith("[]")) {
92
+ const itemType = attribute.type.slice(0, -2);
93
+ return {
94
+ type: "array",
95
+ items: { type: itemType },
96
+ description: attribute.description
97
+ };
98
+ }
99
+ return {
100
+ type: "string",
101
+ description: attribute.description
102
+ };
103
+ }
104
+ }
105
+ function convertJsonSchemaToZodSchema(jsonSchema, required) {
106
+ if (jsonSchema.type === "object") {
107
+ const spec = {};
108
+ if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
109
+ return !required ? import_zod.z.object(spec).optional() : import_zod.z.object(spec);
110
+ }
111
+ for (const [key, value] of Object.entries(jsonSchema.properties)) {
112
+ spec[key] = convertJsonSchemaToZodSchema(
113
+ value,
114
+ jsonSchema.required ? jsonSchema.required.includes(key) : false
115
+ );
116
+ }
117
+ let schema = import_zod.z.object(spec).describe(jsonSchema.description);
118
+ return required ? schema : schema.optional();
119
+ } else if (jsonSchema.type === "string") {
120
+ let schema = import_zod.z.string().describe(jsonSchema.description);
121
+ return required ? schema : schema.optional();
122
+ } else if (jsonSchema.type === "number") {
123
+ let schema = import_zod.z.number().describe(jsonSchema.description);
124
+ return required ? schema : schema.optional();
125
+ } else if (jsonSchema.type === "boolean") {
126
+ let schema = import_zod.z.boolean().describe(jsonSchema.description);
127
+ return required ? schema : schema.optional();
128
+ } else if (jsonSchema.type === "array") {
129
+ let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
130
+ let schema = import_zod.z.array(itemSchema).describe(jsonSchema.description);
131
+ return required ? schema : schema.optional();
132
+ }
133
+ throw new Error("Invalid JSON schema");
134
+ }
135
+
136
+ // src/utils/random-id.ts
137
+ var import_uuid = require("uuid");
138
+ function randomId() {
139
+ return "ck-" + (0, import_uuid.v4)();
140
+ }
141
+ // Annotate the CommonJS export names for ESM import in node:
142
+ 0 && (module.exports = {
143
+ actionParametersToJsonSchema,
144
+ convertJsonSchemaToZodSchema,
145
+ randomId
146
+ });
147
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utils/index.ts","../../src/utils/json-schema.ts","../../src/utils/random-id.ts"],"sourcesContent":["export * from \"./json-schema\";\nexport * from \"./random-id\";\n","import { z } from \"zod\";\nimport { Parameter } from \"../types\";\n\nexport type JSONSchemaString = {\n type: \"string\";\n description?: string;\n enum?: string[];\n};\n\nexport type JSONSchemaNumber = {\n type: \"number\";\n description?: string;\n};\n\nexport type JSONSchemaBoolean = {\n type: \"boolean\";\n description?: string;\n};\n\nexport type JSONSchemaObject = {\n type: \"object\";\n properties?: Record<string, JSONSchema>;\n required?: string[];\n description?: string;\n};\n\nexport type JSONSchemaArray = {\n type: \"array\";\n items: JSONSchema;\n description?: string;\n};\n\nexport type JSONSchema =\n | JSONSchemaString\n | JSONSchemaNumber\n | JSONSchemaBoolean\n | JSONSchemaObject\n | JSONSchemaArray;\n\nexport function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of actionParameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of actionParameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n return {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n };\n}\n\nfunction convertAttribute(attribute: Parameter): JSONSchema {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce(\n (acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n },\n {} as Record<string, any>,\n );\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType as any },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n\nexport function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema {\n if (jsonSchema.type === \"object\") {\n const spec: { [key: string]: z.ZodSchema } = {};\n\n if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {\n return !required ? z.object(spec).optional() : z.object(spec);\n }\n\n for (const [key, value] of Object.entries(jsonSchema.properties)) {\n spec[key] = convertJsonSchemaToZodSchema(\n value,\n jsonSchema.required ? jsonSchema.required.includes(key) : false,\n );\n }\n let schema = z.object(spec).describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"string\") {\n let schema = z.string().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"number\") {\n let schema = z.number().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"boolean\") {\n let schema = z.boolean().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"array\") {\n let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);\n let schema = z.array(itemSchema).describe(jsonSchema.description);\n return required ? schema : schema.optional();\n }\n throw new Error(\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AW,WAAW;AACvD,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C,WAAW,WAAW,SAAS,UAAU;AACvC,QAAI,SAAS,aAAE,OAAO,EAAE,SAAS,WAAW,WAAW;AACvD,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C,WAAW,WAAW,SAAS,WAAW;AACxC,QAAI,SAAS,aAAE,QAAQ,EAAE,SAAS,WAAW,WAAW;AACxD,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C,WAAW,WAAW,SAAS,SAAS;AACtC,QAAI,aAAa,6BAA6B,WAAW,OAAO,IAAI;AACpE,QAAI,SAAS,aAAE,MAAM,UAAU,EAAE,SAAS,WAAW,WAAW;AAChE,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C;AACA,QAAM,IAAI,MAAM,qBAAqB;AACvC;;;ACzJA,kBAA6B;AAEtB,SAAS,WAAW;AACzB,SAAO,YAAQ,YAAAA,IAAO;AACxB;","names":["uuidv4"]}
@@ -0,0 +1,14 @@
1
+ import "../chunk-CYDWEPFL.mjs";
2
+ import {
3
+ actionParametersToJsonSchema,
4
+ convertJsonSchemaToZodSchema
5
+ } from "../chunk-JP2M4U4G.mjs";
6
+ import {
7
+ randomId
8
+ } from "../chunk-RIPX6APP.mjs";
9
+ export {
10
+ actionParametersToJsonSchema,
11
+ convertJsonSchemaToZodSchema,
12
+ randomId
13
+ };
14
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,32 @@
1
+ import { z } from 'zod';
2
+ import { Parameter } from '../types/action.js';
3
+
4
+ type JSONSchemaString = {
5
+ type: "string";
6
+ description?: string;
7
+ enum?: string[];
8
+ };
9
+ type JSONSchemaNumber = {
10
+ type: "number";
11
+ description?: string;
12
+ };
13
+ type JSONSchemaBoolean = {
14
+ type: "boolean";
15
+ description?: string;
16
+ };
17
+ type JSONSchemaObject = {
18
+ type: "object";
19
+ properties?: Record<string, JSONSchema>;
20
+ required?: string[];
21
+ description?: string;
22
+ };
23
+ type JSONSchemaArray = {
24
+ type: "array";
25
+ items: JSONSchema;
26
+ description?: string;
27
+ };
28
+ type JSONSchema = JSONSchemaString | JSONSchemaNumber | JSONSchemaBoolean | JSONSchemaObject | JSONSchemaArray;
29
+ declare function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema;
30
+ declare function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema;
31
+
32
+ export { JSONSchema, JSONSchemaArray, JSONSchemaBoolean, JSONSchemaNumber, JSONSchemaObject, JSONSchemaString, actionParametersToJsonSchema, convertJsonSchemaToZodSchema };
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/utils/json-schema.ts
21
+ var json_schema_exports = {};
22
+ __export(json_schema_exports, {
23
+ actionParametersToJsonSchema: () => actionParametersToJsonSchema,
24
+ convertJsonSchemaToZodSchema: () => convertJsonSchemaToZodSchema
25
+ });
26
+ module.exports = __toCommonJS(json_schema_exports);
27
+ var import_zod = require("zod");
28
+ function actionParametersToJsonSchema(actionParameters) {
29
+ let parameters = {};
30
+ for (let parameter of actionParameters || []) {
31
+ parameters[parameter.name] = convertAttribute(parameter);
32
+ }
33
+ let requiredParameterNames = [];
34
+ for (let arg of actionParameters || []) {
35
+ if (arg.required !== false) {
36
+ requiredParameterNames.push(arg.name);
37
+ }
38
+ }
39
+ return {
40
+ type: "object",
41
+ properties: parameters,
42
+ required: requiredParameterNames
43
+ };
44
+ }
45
+ function convertAttribute(attribute) {
46
+ var _a, _b, _c;
47
+ switch (attribute.type) {
48
+ case "string":
49
+ return {
50
+ type: "string",
51
+ description: attribute.description,
52
+ ...attribute.enum && { enum: attribute.enum }
53
+ };
54
+ case "number":
55
+ case "boolean":
56
+ return {
57
+ type: attribute.type,
58
+ description: attribute.description
59
+ };
60
+ case "object":
61
+ case "object[]":
62
+ const properties = (_a = attribute.attributes) == null ? void 0 : _a.reduce(
63
+ (acc, attr) => {
64
+ acc[attr.name] = convertAttribute(attr);
65
+ return acc;
66
+ },
67
+ {}
68
+ );
69
+ const required = (_b = attribute.attributes) == null ? void 0 : _b.filter((attr) => attr.required !== false).map((attr) => attr.name);
70
+ if (attribute.type === "object[]") {
71
+ return {
72
+ type: "array",
73
+ items: {
74
+ type: "object",
75
+ ...properties && { properties },
76
+ ...required && required.length > 0 && { required }
77
+ },
78
+ description: attribute.description
79
+ };
80
+ }
81
+ return {
82
+ type: "object",
83
+ description: attribute.description,
84
+ ...properties && { properties },
85
+ ...required && required.length > 0 && { required }
86
+ };
87
+ default:
88
+ if ((_c = attribute.type) == null ? void 0 : _c.endsWith("[]")) {
89
+ const itemType = attribute.type.slice(0, -2);
90
+ return {
91
+ type: "array",
92
+ items: { type: itemType },
93
+ description: attribute.description
94
+ };
95
+ }
96
+ return {
97
+ type: "string",
98
+ description: attribute.description
99
+ };
100
+ }
101
+ }
102
+ function convertJsonSchemaToZodSchema(jsonSchema, required) {
103
+ if (jsonSchema.type === "object") {
104
+ const spec = {};
105
+ if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
106
+ return !required ? import_zod.z.object(spec).optional() : import_zod.z.object(spec);
107
+ }
108
+ for (const [key, value] of Object.entries(jsonSchema.properties)) {
109
+ spec[key] = convertJsonSchemaToZodSchema(
110
+ value,
111
+ jsonSchema.required ? jsonSchema.required.includes(key) : false
112
+ );
113
+ }
114
+ let schema = import_zod.z.object(spec).describe(jsonSchema.description);
115
+ return required ? schema : schema.optional();
116
+ } else if (jsonSchema.type === "string") {
117
+ let schema = import_zod.z.string().describe(jsonSchema.description);
118
+ return required ? schema : schema.optional();
119
+ } else if (jsonSchema.type === "number") {
120
+ let schema = import_zod.z.number().describe(jsonSchema.description);
121
+ return required ? schema : schema.optional();
122
+ } else if (jsonSchema.type === "boolean") {
123
+ let schema = import_zod.z.boolean().describe(jsonSchema.description);
124
+ return required ? schema : schema.optional();
125
+ } else if (jsonSchema.type === "array") {
126
+ let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
127
+ let schema = import_zod.z.array(itemSchema).describe(jsonSchema.description);
128
+ return required ? schema : schema.optional();
129
+ }
130
+ throw new Error("Invalid JSON schema");
131
+ }
132
+ // Annotate the CommonJS export names for ESM import in node:
133
+ 0 && (module.exports = {
134
+ actionParametersToJsonSchema,
135
+ convertJsonSchemaToZodSchema
136
+ });
137
+ //# sourceMappingURL=json-schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utils/json-schema.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { Parameter } from \"../types\";\n\nexport type JSONSchemaString = {\n type: \"string\";\n description?: string;\n enum?: string[];\n};\n\nexport type JSONSchemaNumber = {\n type: \"number\";\n description?: string;\n};\n\nexport type JSONSchemaBoolean = {\n type: \"boolean\";\n description?: string;\n};\n\nexport type JSONSchemaObject = {\n type: \"object\";\n properties?: Record<string, JSONSchema>;\n required?: string[];\n description?: string;\n};\n\nexport type JSONSchemaArray = {\n type: \"array\";\n items: JSONSchema;\n description?: string;\n};\n\nexport type JSONSchema =\n | JSONSchemaString\n | JSONSchemaNumber\n | JSONSchemaBoolean\n | JSONSchemaObject\n | JSONSchemaArray;\n\nexport function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of actionParameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of actionParameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n return {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n };\n}\n\nfunction convertAttribute(attribute: Parameter): JSONSchema {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce(\n (acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n },\n {} as Record<string, any>,\n );\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType as any },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n\nexport function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema {\n if (jsonSchema.type === \"object\") {\n const spec: { [key: string]: z.ZodSchema } = {};\n\n if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {\n return !required ? z.object(spec).optional() : z.object(spec);\n }\n\n for (const [key, value] of Object.entries(jsonSchema.properties)) {\n spec[key] = convertJsonSchemaToZodSchema(\n value,\n jsonSchema.required ? jsonSchema.required.includes(key) : false,\n );\n }\n let schema = z.object(spec).describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"string\") {\n let schema = z.string().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"number\") {\n let schema = z.number().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"boolean\") {\n let schema = z.boolean().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"array\") {\n let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);\n let schema = z.array(itemSchema).describe(jsonSchema.description);\n return required ? schema : schema.optional();\n }\n throw new Error(\"Invalid JSON schema\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAAkB;AAuCX,SAAS,6BAA6B,kBAA2C;AAEtF,MAAI,aAAqC,CAAC;AAC1C,WAAS,aAAa,oBAAoB,CAAC,GAAG;AAC5C,eAAW,UAAU,IAAI,IAAI,iBAAiB,SAAS;AAAA,EACzD;AAEA,MAAI,yBAAmC,CAAC;AACxC,WAAS,OAAO,oBAAoB,CAAC,GAAG;AACtC,QAAI,IAAI,aAAa,OAAO;AAC1B,6BAAuB,KAAK,IAAI,IAAI;AAAA,IACtC;AAAA,EACF;AAGA,SAAO;AAAA,IACL,MAAM;AAAA,IACN,YAAY;AAAA,IACZ,UAAU;AAAA,EACZ;AACF;AAEA,SAAS,iBAAiB,WAAkC;AA7D5D;AA8DE,UAAQ,UAAU,MAAM;AAAA,IACtB,KAAK;AACH,aAAO;AAAA,QACL,MAAM;AAAA,QACN,aAAa,UAAU;AAAA,QACvB,GAAI,UAAU,QAAQ,EAAE,MAAM,UAAU,KAAK;AAAA,MAC/C;AAAA,IAC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E,QAAQ;AACxE,aAAO,CAAC,WAAW,aAAE,OAAO,IAAI,EAAE,SAAS,IAAI,aAAE,OAAO,IAAI;AAAA,IAC9D;AAEA,eAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,WAAW,UAAU,GAAG;AAChE,WAAK,GAAG,IAAI;AAAA,QACV;AAAA,QACA,WAAW,WAAW,WAAW,SAAS,SAAS,GAAG,IAAI;AAAA,MAC5D;AAAA,IACF;AACA,QAAI,SAAS,aAAE,OAAO,IAAI,EAAE,SAAS,WAAW,WAAW;AAC3D,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C,WAAW,WAAW,SAAS,UAAU;AACvC,QAAI,SAAS,aAAE,OAAO,EAAE,SAAS,WAAW,WAAW;AACvD,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C,WAAW,WAAW,SAAS,UAAU;AACvC,QAAI,SAAS,aAAE,OAAO,EAAE,SAAS,WAAW,WAAW;AACvD,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C,WAAW,WAAW,SAAS,WAAW;AACxC,QAAI,SAAS,aAAE,QAAQ,EAAE,SAAS,WAAW,WAAW;AACxD,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C,WAAW,WAAW,SAAS,SAAS;AACtC,QAAI,aAAa,6BAA6B,WAAW,OAAO,IAAI;AACpE,QAAI,SAAS,aAAE,MAAM,UAAU,EAAE,SAAS,WAAW,WAAW;AAChE,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C;AACA,QAAM,IAAI,MAAM,qBAAqB;AACvC;","names":[]}
@@ -0,0 +1,9 @@
1
+ import {
2
+ actionParametersToJsonSchema,
3
+ convertJsonSchemaToZodSchema
4
+ } from "../chunk-JP2M4U4G.mjs";
5
+ export {
6
+ actionParametersToJsonSchema,
7
+ convertJsonSchemaToZodSchema
8
+ };
9
+ //# sourceMappingURL=json-schema.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+
3
+ // src/utils/json-schema.test.ts
4
+ var import_zod2 = require("zod");
5
+
6
+ // src/utils/json-schema.ts
7
+ var import_zod = require("zod");
8
+ function convertJsonSchemaToZodSchema(jsonSchema, required) {
9
+ if (jsonSchema.type === "object") {
10
+ const spec = {};
11
+ if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
12
+ return !required ? import_zod.z.object(spec).optional() : import_zod.z.object(spec);
13
+ }
14
+ for (const [key, value] of Object.entries(jsonSchema.properties)) {
15
+ spec[key] = convertJsonSchemaToZodSchema(
16
+ value,
17
+ jsonSchema.required ? jsonSchema.required.includes(key) : false
18
+ );
19
+ }
20
+ let schema = import_zod.z.object(spec).describe(jsonSchema.description);
21
+ return required ? schema : schema.optional();
22
+ } else if (jsonSchema.type === "string") {
23
+ let schema = import_zod.z.string().describe(jsonSchema.description);
24
+ return required ? schema : schema.optional();
25
+ } else if (jsonSchema.type === "number") {
26
+ let schema = import_zod.z.number().describe(jsonSchema.description);
27
+ return required ? schema : schema.optional();
28
+ } else if (jsonSchema.type === "boolean") {
29
+ let schema = import_zod.z.boolean().describe(jsonSchema.description);
30
+ return required ? schema : schema.optional();
31
+ } else if (jsonSchema.type === "array") {
32
+ let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
33
+ let schema = import_zod.z.array(itemSchema).describe(jsonSchema.description);
34
+ return required ? schema : schema.optional();
35
+ }
36
+ throw new Error("Invalid JSON schema");
37
+ }
38
+
39
+ // src/utils/json-schema.test.ts
40
+ var import_zod_to_json_schema = require("zod-to-json-schema");
41
+ describe("convertJsonSchemaToZodSchema", () => {
42
+ it("should convert a simple JSON schema to a Zod schema", () => {
43
+ const jsonSchema = {
44
+ type: "object",
45
+ properties: {
46
+ name: { type: "string" },
47
+ age: { type: "number" }
48
+ },
49
+ required: ["name", "age"]
50
+ };
51
+ const expectedSchema = import_zod2.z.object({
52
+ name: import_zod2.z.string(),
53
+ age: import_zod2.z.number()
54
+ });
55
+ const result = convertJsonSchemaToZodSchema(jsonSchema, true);
56
+ const resultSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(result);
57
+ const expectedSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(expectedSchema);
58
+ expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
59
+ });
60
+ it("should convert a JSON schema with nested objects to a Zod schema", () => {
61
+ const jsonSchema = {
62
+ type: "object",
63
+ properties: {
64
+ name: { type: "string" },
65
+ address: {
66
+ type: "object",
67
+ properties: {
68
+ street: { type: "string" },
69
+ city: { type: "string" }
70
+ },
71
+ required: ["street", "city"]
72
+ }
73
+ },
74
+ required: ["name", "address"]
75
+ };
76
+ const expectedSchema = import_zod2.z.object({
77
+ name: import_zod2.z.string(),
78
+ address: import_zod2.z.object({
79
+ street: import_zod2.z.string(),
80
+ city: import_zod2.z.string()
81
+ })
82
+ });
83
+ const result = convertJsonSchemaToZodSchema(jsonSchema, true);
84
+ const resultSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(result);
85
+ const expectedSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(expectedSchema);
86
+ expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
87
+ });
88
+ it("should convert a JSON schema with arrays to a Zod schema", () => {
89
+ const jsonSchema = {
90
+ type: "object",
91
+ properties: {
92
+ names: {
93
+ type: "array",
94
+ items: { type: "string" }
95
+ }
96
+ },
97
+ required: ["names"]
98
+ };
99
+ const expectedSchema = import_zod2.z.object({
100
+ names: import_zod2.z.array(import_zod2.z.string())
101
+ });
102
+ const result = convertJsonSchemaToZodSchema(jsonSchema, true);
103
+ const resultSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(result);
104
+ const expectedSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(expectedSchema);
105
+ expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
106
+ });
107
+ it("should convert a JSON schema with optional properties to a Zod schema", () => {
108
+ const jsonSchema = {
109
+ type: "object",
110
+ properties: {
111
+ name: { type: "string" },
112
+ age: { type: "number", required: false }
113
+ }
114
+ };
115
+ const expectedSchema = import_zod2.z.object({
116
+ name: import_zod2.z.string().optional(),
117
+ age: import_zod2.z.number().optional()
118
+ }).optional();
119
+ const result = convertJsonSchemaToZodSchema(jsonSchema, false);
120
+ console.log(convertJsonSchemaToZodSchema(jsonSchema, false));
121
+ const resultSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(result);
122
+ const expectedSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(expectedSchema);
123
+ expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
124
+ });
125
+ it("should convert a JSON schema with different types to a Zod schema", () => {
126
+ const jsonSchema = {
127
+ type: "object",
128
+ properties: {
129
+ name: { type: "string" },
130
+ age: { type: "number" },
131
+ isAdmin: { type: "boolean" }
132
+ },
133
+ required: ["name", "age", "isAdmin"]
134
+ };
135
+ const expectedSchema = import_zod2.z.object({
136
+ name: import_zod2.z.string(),
137
+ age: import_zod2.z.number(),
138
+ isAdmin: import_zod2.z.boolean()
139
+ });
140
+ const result = convertJsonSchemaToZodSchema(jsonSchema, true);
141
+ const resultSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(result);
142
+ const expectedSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(expectedSchema);
143
+ expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
144
+ });
145
+ it("should handle edge case where JSON schema has no properties", () => {
146
+ const jsonSchema = {
147
+ type: "object"
148
+ };
149
+ const expectedSchema = import_zod2.z.object({});
150
+ const result = convertJsonSchemaToZodSchema(jsonSchema, true);
151
+ const resultSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(result);
152
+ const expectedSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(expectedSchema);
153
+ expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
154
+ });
155
+ it("should handle edge case where JSON schema has no required properties", () => {
156
+ const jsonSchema = {
157
+ type: "object",
158
+ properties: {
159
+ name: { type: "string" },
160
+ age: { type: "number" }
161
+ }
162
+ };
163
+ const expectedSchema = import_zod2.z.object({
164
+ name: import_zod2.z.string().optional(),
165
+ age: import_zod2.z.number().optional()
166
+ }).optional();
167
+ const result = convertJsonSchemaToZodSchema(jsonSchema, false);
168
+ const resultSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(result);
169
+ const expectedSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(expectedSchema);
170
+ expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
171
+ });
172
+ });
173
+ //# sourceMappingURL=json-schema.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utils/json-schema.test.ts","../../src/utils/json-schema.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { convertJsonSchemaToZodSchema } from \"../utils/json-schema\";\nimport { zodToJsonSchema } from \"zod-to-json-schema\";\n\ndescribe(\"convertJsonSchemaToZodSchema\", () => {\n it(\"should convert a simple JSON schema to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n age: { type: \"number\" },\n },\n required: [\"name\", \"age\"],\n };\n\n const expectedSchema = z.object({\n name: z.string(),\n age: z.number(),\n });\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should convert a JSON schema with nested objects to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n address: {\n type: \"object\",\n properties: {\n street: { type: \"string\" },\n city: { type: \"string\" },\n },\n required: [\"street\", \"city\"],\n },\n },\n required: [\"name\", \"address\"],\n };\n\n const expectedSchema = z.object({\n name: z.string(),\n address: z.object({\n street: z.string(),\n city: z.string(),\n }),\n });\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should convert a JSON schema with arrays to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n names: {\n type: \"array\",\n items: { type: \"string\" },\n },\n },\n required: [\"names\"],\n };\n\n const expectedSchema = z.object({\n names: z.array(z.string()),\n });\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should convert a JSON schema with optional properties to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n age: { type: \"number\", required: false },\n },\n };\n\n const expectedSchema = z\n .object({\n name: z.string().optional(),\n age: z.number().optional(),\n })\n .optional();\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, false);\n\n console.log(convertJsonSchemaToZodSchema(jsonSchema, false));\n\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should convert a JSON schema with different types to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n age: { type: \"number\" },\n isAdmin: { type: \"boolean\" },\n },\n required: [\"name\", \"age\", \"isAdmin\"],\n };\n\n const expectedSchema = z.object({\n name: z.string(),\n age: z.number(),\n isAdmin: z.boolean(),\n });\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should handle edge case where JSON schema has no properties\", () => {\n const jsonSchema = {\n type: \"object\",\n };\n\n const expectedSchema = z.object({});\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should handle edge case where JSON schema has no required properties\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n age: { type: \"number\" },\n },\n };\n\n const expectedSchema = z\n .object({\n name: z.string().optional(),\n age: z.number().optional(),\n })\n .optional();\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, false);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n});\n","import { z } from \"zod\";\nimport { Parameter } from \"../types\";\n\nexport type JSONSchemaString = {\n type: \"string\";\n description?: string;\n enum?: string[];\n};\n\nexport type JSONSchemaNumber = {\n type: \"number\";\n description?: string;\n};\n\nexport type JSONSchemaBoolean = {\n type: \"boolean\";\n description?: string;\n};\n\nexport type JSONSchemaObject = {\n type: \"object\";\n properties?: Record<string, JSONSchema>;\n required?: string[];\n description?: string;\n};\n\nexport type JSONSchemaArray = {\n type: \"array\";\n items: JSONSchema;\n description?: string;\n};\n\nexport type JSONSchema =\n | JSONSchemaString\n | JSONSchemaNumber\n | JSONSchemaBoolean\n | JSONSchemaObject\n | JSONSchemaArray;\n\nexport function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of actionParameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of actionParameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n return {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n };\n}\n\nfunction convertAttribute(attribute: Parameter): JSONSchema {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce(\n (acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n },\n {} as Record<string, any>,\n );\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType as any },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n\nexport function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema {\n if (jsonSchema.type === \"object\") {\n const spec: { [key: string]: z.ZodSchema } = {};\n\n if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {\n return !required ? z.object(spec).optional() : z.object(spec);\n }\n\n for (const [key, value] of Object.entries(jsonSchema.properties)) {\n spec[key] = convertJsonSchemaToZodSchema(\n value,\n jsonSchema.required ? jsonSchema.required.includes(key) : false,\n );\n }\n let schema = z.object(spec).describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"string\") {\n let schema = z.string().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"number\") {\n let schema = z.number().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"boolean\") {\n let schema = z.boolean().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"array\") {\n let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);\n let schema = z.array(itemSchema).describe(jsonSchema.description);\n return required ? schema : schema.optional();\n }\n throw new Error(\"Invalid JSON schema\");\n}\n"],"mappings":";;;AAAA,IAAAA,cAAkB;;;ACAlB,iBAAkB;AA0HX,SAAS,6BAA6B,YAAiB,UAAgC;AAC5F,MAAI,WAAW,SAAS,UAAU;AAChC,UAAM,OAAuC,CAAC;AAE9C,QAAI,CAAC,WAAW,cAAc,CAAC,OAAO,KAAK,WAAW,UAAU,EAAE,QAAQ;AACxE,aAAO,CAAC,WAAW,aAAE,OAAO,IAAI,EAAE,SAAS,IAAI,aAAE,OAAO,IAAI;AAAA,IAC9D;AAEA,eAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,WAAW,UAAU,GAAG;AAChE,WAAK,GAAG,IAAI;AAAA,QACV;AAAA,QACA,WAAW,WAAW,WAAW,SAAS,SAAS,GAAG,IAAI;AAAA,MAC5D;AAAA,IACF;AACA,QAAI,SAAS,aAAE,OAAO,IAAI,EAAE,SAAS,WAAW,WAAW;AAC3D,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C,WAAW,WAAW,SAAS,UAAU;AACvC,QAAI,SAAS,aAAE,OAAO,EAAE,SAAS,WAAW,WAAW;AACvD,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C,WAAW,WAAW,SAAS,UAAU;AACvC,QAAI,SAAS,aAAE,OAAO,EAAE,SAAS,WAAW,WAAW;AACvD,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C,WAAW,WAAW,SAAS,WAAW;AACxC,QAAI,SAAS,aAAE,QAAQ,EAAE,SAAS,WAAW,WAAW;AACxD,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC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