@copilotkit/shared 1.4.1-pre.0 → 1.4.1-pre.1
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.
- package/CHANGELOG.md +6 -0
- package/dist/{chunk-CIPF7PMC.mjs → chunk-JP2M4U4G.mjs} +34 -2
- package/dist/chunk-JP2M4U4G.mjs.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +34 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -3
- package/dist/index.mjs.map +1 -1
- package/dist/utils/index.d.ts +2 -1
- package/dist/utils/index.js +33 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +4 -2
- package/dist/utils/json-schema.d.ts +3 -1
- package/dist/utils/json-schema.js +35 -2
- package/dist/utils/json-schema.js.map +1 -1
- package/dist/utils/json-schema.mjs +5 -3
- package/dist/utils/json-schema.test.d.ts +2 -0
- package/dist/utils/json-schema.test.js +173 -0
- package/dist/utils/json-schema.test.js.map +1 -0
- package/dist/utils/json-schema.test.mjs +140 -0
- package/dist/utils/json-schema.test.mjs.map +1 -0
- package/package.json +6 -4
- package/src/utils/json-schema.test.ts +169 -0
- package/src/utils/json-schema.ts +34 -0
- package/dist/chunk-CIPF7PMC.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/utils/json-schema.ts
|
|
2
|
+
import { z } from "zod";
|
|
2
3
|
function actionParametersToJsonSchema(actionParameters) {
|
|
3
4
|
let parameters = {};
|
|
4
5
|
for (let parameter of actionParameters || []) {
|
|
@@ -73,8 +74,39 @@ function convertAttribute(attribute) {
|
|
|
73
74
|
};
|
|
74
75
|
}
|
|
75
76
|
}
|
|
77
|
+
function convertJsonSchemaToZodSchema(jsonSchema, required) {
|
|
78
|
+
if (jsonSchema.type === "object") {
|
|
79
|
+
const spec = {};
|
|
80
|
+
if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
|
|
81
|
+
return !required ? z.object(spec).optional() : z.object(spec);
|
|
82
|
+
}
|
|
83
|
+
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
84
|
+
spec[key] = convertJsonSchemaToZodSchema(
|
|
85
|
+
value,
|
|
86
|
+
jsonSchema.required ? jsonSchema.required.includes(key) : false
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
let schema = z.object(spec).describe(jsonSchema.description);
|
|
90
|
+
return required ? schema : schema.optional();
|
|
91
|
+
} else if (jsonSchema.type === "string") {
|
|
92
|
+
let schema = z.string().describe(jsonSchema.description);
|
|
93
|
+
return required ? schema : schema.optional();
|
|
94
|
+
} else if (jsonSchema.type === "number") {
|
|
95
|
+
let schema = z.number().describe(jsonSchema.description);
|
|
96
|
+
return required ? schema : schema.optional();
|
|
97
|
+
} else if (jsonSchema.type === "boolean") {
|
|
98
|
+
let schema = z.boolean().describe(jsonSchema.description);
|
|
99
|
+
return required ? schema : schema.optional();
|
|
100
|
+
} else if (jsonSchema.type === "array") {
|
|
101
|
+
let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
|
|
102
|
+
let schema = z.array(itemSchema).describe(jsonSchema.description);
|
|
103
|
+
return required ? schema : schema.optional();
|
|
104
|
+
}
|
|
105
|
+
throw new Error("Invalid JSON schema");
|
|
106
|
+
}
|
|
76
107
|
|
|
77
108
|
export {
|
|
78
|
-
actionParametersToJsonSchema
|
|
109
|
+
actionParametersToJsonSchema,
|
|
110
|
+
convertJsonSchemaToZodSchema
|
|
79
111
|
};
|
|
80
|
-
//# sourceMappingURL=chunk-
|
|
112
|
+
//# sourceMappingURL=chunk-JP2M4U4G.mjs.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,SAAS,SAAS;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;A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@ -1,10 +1,11 @@
|
|
|
1
1
|
export { AssistantMessage, CoAgentStateRenderHandler, CoAgentStateRenderHandlerArguments, FunctionCallHandler, FunctionCallHandlerArguments, FunctionDefinition, JSONValue, ToolDefinition } from './types/openai-assistant.js';
|
|
2
2
|
export { Action, MappedParameterTypes, Parameter } from './types/action.js';
|
|
3
3
|
export { CopilotCloudConfig } from './types/copilot-cloud-config.js';
|
|
4
|
-
export { JSONSchema, JSONSchemaArray, JSONSchemaBoolean, JSONSchemaNumber, JSONSchemaObject, JSONSchemaString, actionParametersToJsonSchema } from './utils/json-schema.js';
|
|
4
|
+
export { JSONSchema, JSONSchemaArray, JSONSchemaBoolean, JSONSchemaNumber, JSONSchemaObject, JSONSchemaString, actionParametersToJsonSchema, convertJsonSchemaToZodSchema } from './utils/json-schema.js';
|
|
5
5
|
export { randomId } from './utils/random-id.js';
|
|
6
6
|
export { COPILOT_CLOUD_API_URL, COPILOT_CLOUD_CHAT_URL, COPILOT_CLOUD_PUBLIC_API_KEY_HEADER, COPILOT_CLOUD_VERSION } from './constants/index.js';
|
|
7
7
|
export { TelemetryClient } from './telemetry/telemetry-client.js';
|
|
8
|
+
import 'zod';
|
|
8
9
|
import '@segment/analytics-node';
|
|
9
10
|
import './telemetry/events.js';
|
|
10
11
|
|
package/dist/index.js
CHANGED
|
@@ -37,11 +37,13 @@ __export(src_exports, {
|
|
|
37
37
|
COPILOT_CLOUD_VERSION: () => COPILOT_CLOUD_VERSION,
|
|
38
38
|
TelemetryClient: () => TelemetryClient,
|
|
39
39
|
actionParametersToJsonSchema: () => actionParametersToJsonSchema,
|
|
40
|
+
convertJsonSchemaToZodSchema: () => convertJsonSchemaToZodSchema,
|
|
40
41
|
randomId: () => randomId
|
|
41
42
|
});
|
|
42
43
|
module.exports = __toCommonJS(src_exports);
|
|
43
44
|
|
|
44
45
|
// src/utils/json-schema.ts
|
|
46
|
+
var import_zod = require("zod");
|
|
45
47
|
function actionParametersToJsonSchema(actionParameters) {
|
|
46
48
|
let parameters = {};
|
|
47
49
|
for (let parameter of actionParameters || []) {
|
|
@@ -116,6 +118,36 @@ function convertAttribute(attribute) {
|
|
|
116
118
|
};
|
|
117
119
|
}
|
|
118
120
|
}
|
|
121
|
+
function convertJsonSchemaToZodSchema(jsonSchema, required) {
|
|
122
|
+
if (jsonSchema.type === "object") {
|
|
123
|
+
const spec = {};
|
|
124
|
+
if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
|
|
125
|
+
return !required ? import_zod.z.object(spec).optional() : import_zod.z.object(spec);
|
|
126
|
+
}
|
|
127
|
+
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
128
|
+
spec[key] = convertJsonSchemaToZodSchema(
|
|
129
|
+
value,
|
|
130
|
+
jsonSchema.required ? jsonSchema.required.includes(key) : false
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
let schema = import_zod.z.object(spec).describe(jsonSchema.description);
|
|
134
|
+
return required ? schema : schema.optional();
|
|
135
|
+
} else if (jsonSchema.type === "string") {
|
|
136
|
+
let schema = import_zod.z.string().describe(jsonSchema.description);
|
|
137
|
+
return required ? schema : schema.optional();
|
|
138
|
+
} else if (jsonSchema.type === "number") {
|
|
139
|
+
let schema = import_zod.z.number().describe(jsonSchema.description);
|
|
140
|
+
return required ? schema : schema.optional();
|
|
141
|
+
} else if (jsonSchema.type === "boolean") {
|
|
142
|
+
let schema = import_zod.z.boolean().describe(jsonSchema.description);
|
|
143
|
+
return required ? schema : schema.optional();
|
|
144
|
+
} else if (jsonSchema.type === "array") {
|
|
145
|
+
let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
|
|
146
|
+
let schema = import_zod.z.array(itemSchema).describe(jsonSchema.description);
|
|
147
|
+
return required ? schema : schema.optional();
|
|
148
|
+
}
|
|
149
|
+
throw new Error("Invalid JSON schema");
|
|
150
|
+
}
|
|
119
151
|
|
|
120
152
|
// src/utils/random-id.ts
|
|
121
153
|
var import_uuid = require("uuid");
|
|
@@ -234,7 +266,7 @@ var TelemetryClient = class {
|
|
|
234
266
|
};
|
|
235
267
|
|
|
236
268
|
// package.json
|
|
237
|
-
var version = "1.4.1-pre.
|
|
269
|
+
var version = "1.4.1-pre.1";
|
|
238
270
|
|
|
239
271
|
// src/index.ts
|
|
240
272
|
var COPILOTKIT_VERSION = version;
|
|
@@ -247,6 +279,7 @@ var COPILOTKIT_VERSION = version;
|
|
|
247
279
|
COPILOT_CLOUD_VERSION,
|
|
248
280
|
TelemetryClient,
|
|
249
281
|
actionParametersToJsonSchema,
|
|
282
|
+
convertJsonSchemaToZodSchema,
|
|
250
283
|
randomId
|
|
251
284
|
});
|
|
252
285
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/utils/json-schema.ts","../src/utils/random-id.ts","../src/constants/index.ts","../src/telemetry/telemetry-client.ts","../src/telemetry/utils.ts","../package.json"],"sourcesContent":["export * from \"./types\";\nexport * from \"./utils\";\nexport * from \"./constants\";\nexport * from \"./telemetry\";\n\nimport * as packageJson from \"../package.json\";\nexport const COPILOTKIT_VERSION = packageJson.version;\n","import { 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","import { v4 as uuidv4 } from \"uuid\";\n\nexport function randomId() {\n return \"ck-\" + uuidv4();\n}\n","export const COPILOT_CLOUD_API_URL = \"https://api.copilotkit.ai\";\nexport const COPILOT_CLOUD_VERSION = \"v1\";\nexport const COPILOT_CLOUD_CHAT_URL = `${COPILOT_CLOUD_API_URL}/copilotkit/${COPILOT_CLOUD_VERSION}`;\nexport const COPILOT_CLOUD_PUBLIC_API_KEY_HEADER = \"X-CopilotCloud-Public-Api-Key\";\n","import { Analytics } from \"@segment/analytics-node\";\nimport { AnalyticsEvents } from \"./events\";\nimport { flattenObject, printSecurityNotice } from \"./utils\";\nimport { v4 as uuidv4 } from \"uuid\";\n\nexport class TelemetryClient {\n segment: Analytics | undefined;\n globalProperties: Record<string, any> = {};\n cloudConfiguration: { publicApiKey: string; baseUrl: string } | null = null;\n packageName: string;\n packageVersion: string;\n private telemetryDisabled: boolean = false;\n private sampleRate: number = 0.05;\n private anonymousId = `anon_${uuidv4()}`;\n\n constructor({\n packageName,\n packageVersion,\n telemetryDisabled,\n telemetryBaseUrl,\n sampleRate,\n }: {\n packageName: string;\n packageVersion: string;\n telemetryDisabled?: boolean;\n telemetryBaseUrl?: string;\n sampleRate?: number;\n }) {\n this.packageName = packageName;\n this.packageVersion = packageVersion;\n this.telemetryDisabled =\n telemetryDisabled ||\n (process.env as any).COPILOTKIT_TELEMETRY_DISABLED === \"true\" ||\n (process.env as any).COPILOTKIT_TELEMETRY_DISABLED === \"1\" ||\n (process.env as any).DO_NOT_TRACK === \"true\" ||\n (process.env as any).DO_NOT_TRACK === \"1\";\n\n if (this.telemetryDisabled) {\n return;\n }\n\n this.setSampleRate(sampleRate);\n\n // eslint-disable-next-line\n const writeKey = process.env.COPILOTKIT_SEGMENT_WRITE_KEY || \"n7XAZtQCGS2v1vvBy3LgBCv2h3Y8whja\";\n\n this.segment = new Analytics({\n writeKey,\n });\n\n this.setGlobalProperties({\n \"copilotkit.package.name\": packageName,\n \"copilotkit.package.version\": packageVersion,\n });\n }\n\n private shouldSendEvent() {\n const randomNumber = Math.random();\n return randomNumber < this.sampleRate;\n }\n\n async capture<K extends keyof AnalyticsEvents>(event: K, properties: AnalyticsEvents[K]) {\n if (!this.shouldSendEvent() || !this.segment) {\n return;\n }\n\n const flattenedProperties = flattenObject(properties);\n const propertiesWithGlobal = {\n ...this.globalProperties,\n ...flattenedProperties,\n };\n const orderedPropertiesWithGlobal = Object.keys(propertiesWithGlobal)\n .sort()\n .reduce(\n (obj, key) => {\n obj[key] = propertiesWithGlobal[key];\n return obj;\n },\n {} as Record<string, any>,\n );\n\n this.segment.track({\n anonymousId: this.anonymousId,\n event,\n properties: { ...orderedPropertiesWithGlobal },\n });\n }\n\n setGlobalProperties(properties: Record<string, any>) {\n const flattenedProperties = flattenObject(properties);\n this.globalProperties = { ...this.globalProperties, ...flattenedProperties };\n }\n\n setCloudConfiguration(properties: { publicApiKey: string; baseUrl: string }) {\n this.cloudConfiguration = properties;\n\n this.setGlobalProperties({\n cloud: {\n publicApiKey: properties.publicApiKey,\n baseUrl: properties.baseUrl,\n },\n });\n }\n\n private setSampleRate(sampleRate: number | undefined) {\n let _sampleRate: number;\n\n _sampleRate = sampleRate ?? 0.05;\n\n // eslint-disable-next-line\n if (process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE) {\n // eslint-disable-next-line\n _sampleRate = parseFloat(process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE);\n }\n\n if (_sampleRate < 0 || _sampleRate > 1) {\n throw new Error(\"Sample rate must be between 0 and 1\");\n }\n\n this.sampleRate = _sampleRate;\n this.setGlobalProperties({\n sampleRate: this.sampleRate,\n sampleRateAdjustmentFactor: 1 - this.sampleRate,\n });\n }\n}\n","import chalk from \"chalk\";\n\nexport function flattenObject(\n obj: Record<string, any>,\n parentKey = \"\",\n res: Record<string, any> = {},\n): Record<string, any> {\n for (let key in obj) {\n const propName = parentKey ? `${parentKey}.${key}` : key;\n if (typeof obj[key] === \"object\" && obj[key] !== null) {\n flattenObject(obj[key], propName, res);\n } else {\n res[propName] = obj[key];\n }\n }\n return res;\n}\n\nexport function printSecurityNotice(advisory: {\n advisory: string | null;\n message: string;\n severity: \"low\" | \"medium\" | \"high\" | \"none\";\n}) {\n const severityColor =\n {\n low: chalk.blue,\n medium: chalk.yellow,\n high: chalk.red,\n }[advisory.severity.toLowerCase()] || chalk.white;\n\n console.log();\n console.log(`━━━━━━━━━━━━━━━━━━ ${chalk.bold(`CopilotKit`)} ━━━━━━━━━━━━━━━━━━`);\n console.log();\n console.log(`${chalk.bold(`Severity: ${severityColor(advisory.severity.toUpperCase())}`)}`);\n console.log();\n console.log(`${chalk.bold(advisory.message)}`);\n console.log();\n console.log(`━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`);\n}\n","{\n \"name\": \"@copilotkit/shared\",\n \"private\": false,\n \"homepage\": \"https://github.com/CopilotKit/CopilotKit\",\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"https://github.com/CopilotKit/CopilotKit.git\"\n },\n \"publishConfig\": {\n \"access\": \"public\"\n },\n \"version\": \"1.4.1-pre.0\",\n \"sideEffects\": false,\n \"main\": \"./dist/index.js\",\n \"module\": \"./dist/index.mjs\",\n \"exports\": {\n \".\": {\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\"\n }\n },\n \"types\": \"./dist/index.d.ts\",\n \"license\": \"MIT\",\n \"scripts\": {\n \"build\": \"tsup --clean\",\n \"dev\": \"tsup --watch --no-splitting\",\n \"test\": \"jest --passWithNoTests\",\n \"check-types\": \"tsc --noEmit\",\n \"clean\": \"rm -rf .turbo && rm -rf node_modules && rm -rf dist && rm -rf .next\",\n \"link:global\": \"pnpm link --global\",\n \"unlink:global\": \"pnpm unlink --global\"\n },\n \"devDependencies\": {\n \"@types/jest\": \"^29.5.4\",\n \"@types/uuid\": \"^10.0.0\",\n \"eslint\": \"^8.56.0\",\n \"eslint-config-custom\": \"workspace:*\",\n \"jest\": \"^29.6.4\",\n \"ts-jest\": \"^29.1.1\",\n \"tsconfig\": \"workspace:*\",\n \"tsup\": \"^6.7.0\",\n \"typescript\": \"^5.2.3\"\n },\n \"dependencies\": {\n \"@segment/analytics-node\": \"^2.1.2\",\n \"chalk\": \"4.1.2\",\n \"uuid\": \"^10.0.0\"\n },\n \"keywords\": [\n \"copilotkit\",\n \"copilot\",\n \"react\",\n \"nextjs\",\n \"nodejs\",\n \"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AAoB,YAAiC;AACnD,UAAM,sBAAsB,cAAc,UAAU;AACpD,SAAK,mBAAmB,EAAE,GAAG,KAAK,kBAAkB,GAAG,oBAAoB;AAAA,EAC7E;AAAA,EAEA,sBAAsB,YAAuD;AAC3E,SAAK,qBAAqB;AAE1B,SAAK,oBAAoB;AAAA,MACvB,OAAO;AAAA,QACL,cAAc,WAAW;AAAA,QACzB,SAAS,WAAW;AAAA,MACtB;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEQ,cAAc,YAAgC;AACpD,QAAI;AAEJ,kBAAc,cAAc;AAG5B,QAAI,QAAQ,IAAI,kCAAkC;AAEhD,oBAAc,WAAW,QAAQ,IAAI,gCAAgC;AAAA,IACvE;AAEA,QAAI,cAAc,KAAK,cAAc,GAAG;AACtC,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AAEA,SAAK,aAAa;AAClB,SAAK,oBAAoB;AAAA,MACvB,YAAY,KAAK;AAAA,MACjB,4BAA4B,IAAI,KAAK;AAAA,IACvC,CAAC;AAAA,EACH;AACF;;;AElHE,cAAW;;;ANLN,IAAM,qBAAiC;","names":["uuidv4","import_uuid","uuidv4"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/utils/json-schema.ts","../src/utils/random-id.ts","../src/constants/index.ts","../src/telemetry/telemetry-client.ts","../src/telemetry/utils.ts","../package.json"],"sourcesContent":["export * from \"./types\";\nexport * from \"./utils\";\nexport * from \"./constants\";\nexport * from \"./telemetry\";\n\nimport * as packageJson from \"../package.json\";\nexport const COPILOTKIT_VERSION = packageJson.version;\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","import { v4 as uuidv4 } from \"uuid\";\n\nexport function randomId() {\n return \"ck-\" + uuidv4();\n}\n","export const COPILOT_CLOUD_API_URL = \"https://api.copilotkit.ai\";\nexport const COPILOT_CLOUD_VERSION = \"v1\";\nexport const COPILOT_CLOUD_CHAT_URL = `${COPILOT_CLOUD_API_URL}/copilotkit/${COPILOT_CLOUD_VERSION}`;\nexport const COPILOT_CLOUD_PUBLIC_API_KEY_HEADER = \"X-CopilotCloud-Public-Api-Key\";\n","import { Analytics } from \"@segment/analytics-node\";\nimport { AnalyticsEvents } from \"./events\";\nimport { flattenObject, printSecurityNotice } from \"./utils\";\nimport { v4 as uuidv4 } from \"uuid\";\n\nexport class TelemetryClient {\n segment: Analytics | undefined;\n globalProperties: Record<string, any> = {};\n cloudConfiguration: { publicApiKey: string; baseUrl: string } | null = null;\n packageName: string;\n packageVersion: string;\n private telemetryDisabled: boolean = false;\n private sampleRate: number = 0.05;\n private anonymousId = `anon_${uuidv4()}`;\n\n constructor({\n packageName,\n packageVersion,\n telemetryDisabled,\n telemetryBaseUrl,\n sampleRate,\n }: {\n packageName: string;\n packageVersion: string;\n telemetryDisabled?: boolean;\n telemetryBaseUrl?: string;\n sampleRate?: number;\n }) {\n this.packageName = packageName;\n this.packageVersion = packageVersion;\n this.telemetryDisabled =\n telemetryDisabled ||\n (process.env as any).COPILOTKIT_TELEMETRY_DISABLED === \"true\" ||\n (process.env as any).COPILOTKIT_TELEMETRY_DISABLED === \"1\" ||\n (process.env as any).DO_NOT_TRACK === \"true\" ||\n (process.env as any).DO_NOT_TRACK === \"1\";\n\n if (this.telemetryDisabled) {\n return;\n }\n\n this.setSampleRate(sampleRate);\n\n // eslint-disable-next-line\n const writeKey = process.env.COPILOTKIT_SEGMENT_WRITE_KEY || \"n7XAZtQCGS2v1vvBy3LgBCv2h3Y8whja\";\n\n this.segment = new Analytics({\n writeKey,\n });\n\n this.setGlobalProperties({\n \"copilotkit.package.name\": packageName,\n \"copilotkit.package.version\": packageVersion,\n });\n }\n\n private shouldSendEvent() {\n const randomNumber = Math.random();\n return randomNumber < this.sampleRate;\n }\n\n async capture<K extends keyof AnalyticsEvents>(event: K, properties: AnalyticsEvents[K]) {\n if (!this.shouldSendEvent() || !this.segment) {\n return;\n }\n\n const flattenedProperties = flattenObject(properties);\n const propertiesWithGlobal = {\n ...this.globalProperties,\n ...flattenedProperties,\n };\n const orderedPropertiesWithGlobal = Object.keys(propertiesWithGlobal)\n .sort()\n .reduce(\n (obj, key) => {\n obj[key] = propertiesWithGlobal[key];\n return obj;\n },\n {} as Record<string, any>,\n );\n\n this.segment.track({\n anonymousId: this.anonymousId,\n event,\n properties: { ...orderedPropertiesWithGlobal },\n });\n }\n\n setGlobalProperties(properties: Record<string, any>) {\n const flattenedProperties = flattenObject(properties);\n this.globalProperties = { ...this.globalProperties, ...flattenedProperties };\n }\n\n setCloudConfiguration(properties: { publicApiKey: string; baseUrl: string }) {\n this.cloudConfiguration = properties;\n\n this.setGlobalProperties({\n cloud: {\n publicApiKey: properties.publicApiKey,\n baseUrl: properties.baseUrl,\n },\n });\n }\n\n private setSampleRate(sampleRate: number | undefined) {\n let _sampleRate: number;\n\n _sampleRate = sampleRate ?? 0.05;\n\n // eslint-disable-next-line\n if (process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE) {\n // eslint-disable-next-line\n _sampleRate = parseFloat(process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE);\n }\n\n if (_sampleRate < 0 || _sampleRate > 1) {\n throw new Error(\"Sample rate must be between 0 and 1\");\n }\n\n this.sampleRate = _sampleRate;\n this.setGlobalProperties({\n sampleRate: this.sampleRate,\n sampleRateAdjustmentFactor: 1 - this.sampleRate,\n });\n }\n}\n","import chalk from \"chalk\";\n\nexport function flattenObject(\n obj: Record<string, any>,\n parentKey = \"\",\n res: Record<string, any> = {},\n): Record<string, any> {\n for (let key in obj) {\n const propName = parentKey ? `${parentKey}.${key}` : key;\n if (typeof obj[key] === \"object\" && obj[key] !== null) {\n flattenObject(obj[key], propName, res);\n } else {\n res[propName] = obj[key];\n }\n }\n return res;\n}\n\nexport function printSecurityNotice(advisory: {\n advisory: string | null;\n message: string;\n severity: \"low\" | \"medium\" | \"high\" | \"none\";\n}) {\n const severityColor =\n {\n low: chalk.blue,\n medium: chalk.yellow,\n high: chalk.red,\n }[advisory.severity.toLowerCase()] || chalk.white;\n\n console.log();\n console.log(`━━━━━━━━━━━━━━━━━━ ${chalk.bold(`CopilotKit`)} ━━━━━━━━━━━━━━━━━━`);\n console.log();\n console.log(`${chalk.bold(`Severity: ${severityColor(advisory.severity.toUpperCase())}`)}`);\n console.log();\n console.log(`${chalk.bold(advisory.message)}`);\n console.log();\n console.log(`━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`);\n}\n","{\n \"name\": \"@copilotkit/shared\",\n \"private\": false,\n \"homepage\": \"https://github.com/CopilotKit/CopilotKit\",\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"https://github.com/CopilotKit/CopilotKit.git\"\n },\n \"publishConfig\": {\n \"access\": \"public\"\n },\n \"version\": \"1.4.1-pre.1\",\n \"sideEffects\": false,\n \"main\": \"./dist/index.js\",\n \"module\": \"./dist/index.mjs\",\n \"exports\": {\n \".\": {\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\"\n }\n },\n \"types\": \"./dist/index.d.ts\",\n \"license\": \"MIT\",\n \"scripts\": {\n \"build\": \"tsup --clean\",\n \"dev\": \"tsup --watch --no-splitting\",\n \"test\": \"jest --passWithNoTests\",\n \"check-types\": \"tsc --noEmit\",\n \"clean\": \"rm -rf .turbo && rm -rf node_modules && rm -rf dist && rm -rf .next\",\n \"link:global\": \"pnpm link --global\",\n \"unlink:global\": \"pnpm unlink --global\"\n },\n \"devDependencies\": {\n \"@types/jest\": \"^29.5.4\",\n \"@types/uuid\": \"^10.0.0\",\n \"eslint\": \"^8.56.0\",\n \"eslint-config-custom\": \"workspace:*\",\n \"jest\": \"^29.6.4\",\n \"ts-jest\": \"^29.1.1\",\n \"tsconfig\": \"workspace:*\",\n \"tsup\": \"^6.7.0\",\n \"typescript\": \"^5.2.3\"\n },\n \"dependencies\": {\n \"@segment/analytics-node\": \"^2.1.2\",\n \"chalk\": \"4.1.2\",\n \"uuid\": \"^10.0.0\",\n \"zod\": \"^3.23.3\",\n \"zod-to-json-schema\": \"^3.23.5\"\n },\n \"keywords\": [\n \"copilotkit\",\n \"copilot\",\n \"react\",\n \"nextjs\",\n \"nodejs\",\n \"ai\",\n \"assistant\",\n \"javascript\",\n \"automation\",\n \"textarea\"\n ]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAAkB;AAuC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;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEQ,cAAc,YAAgC;AACpD,QAAI;AAEJ,kBAAc,cAAc;AAG5B,QAAI,QAAQ,IAAI,kCAAkC;AAEhD,oBAAc,WAAW,QAAQ,IAAI,gCAAgC;AAAA,IACvE;AAEA,QAAI,cAAc,KAAK,cAAc,GAAG;AACtC,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AAEA,SAAK,aAAa;AAClB,SAAK,oBAAoB;AAAA,MACvB,YAAY,KAAK;AAAA,MACjB,4BAA4B,IAAI,KAAK;AAAA,IACvC,CAAC;AAAA,EACH;AACF;;;AElHE,cAAW;;;ANLN,IAAM,qBAAiC;","names":["uuidv4","import_uuid","uuidv4"]}
|
package/dist/index.mjs
CHANGED
|
@@ -3,8 +3,9 @@ import "./chunk-MSUB6DGR.mjs";
|
|
|
3
3
|
import "./chunk-IAFBVORQ.mjs";
|
|
4
4
|
import "./chunk-CYDWEPFL.mjs";
|
|
5
5
|
import {
|
|
6
|
-
actionParametersToJsonSchema
|
|
7
|
-
|
|
6
|
+
actionParametersToJsonSchema,
|
|
7
|
+
convertJsonSchemaToZodSchema
|
|
8
|
+
} from "./chunk-JP2M4U4G.mjs";
|
|
8
9
|
import {
|
|
9
10
|
randomId
|
|
10
11
|
} from "./chunk-RIPX6APP.mjs";
|
|
@@ -22,7 +23,7 @@ import "./chunk-6QGXWNS5.mjs";
|
|
|
22
23
|
import "./chunk-NAFEBKSO.mjs";
|
|
23
24
|
|
|
24
25
|
// package.json
|
|
25
|
-
var version = "1.4.1-pre.
|
|
26
|
+
var version = "1.4.1-pre.1";
|
|
26
27
|
|
|
27
28
|
// src/index.ts
|
|
28
29
|
var COPILOTKIT_VERSION = version;
|
|
@@ -34,6 +35,7 @@ export {
|
|
|
34
35
|
COPILOT_CLOUD_VERSION,
|
|
35
36
|
TelemetryClient,
|
|
36
37
|
actionParametersToJsonSchema,
|
|
38
|
+
convertJsonSchemaToZodSchema,
|
|
37
39
|
randomId
|
|
38
40
|
};
|
|
39
41
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@copilotkit/shared\",\n \"private\": false,\n \"homepage\": \"https://github.com/CopilotKit/CopilotKit\",\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"https://github.com/CopilotKit/CopilotKit.git\"\n },\n \"publishConfig\": {\n \"access\": \"public\"\n },\n \"version\": \"1.4.1-pre.
|
|
1
|
+
{"version":3,"sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@copilotkit/shared\",\n \"private\": false,\n \"homepage\": \"https://github.com/CopilotKit/CopilotKit\",\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"https://github.com/CopilotKit/CopilotKit.git\"\n },\n \"publishConfig\": {\n \"access\": \"public\"\n },\n \"version\": \"1.4.1-pre.1\",\n \"sideEffects\": false,\n \"main\": \"./dist/index.js\",\n \"module\": \"./dist/index.mjs\",\n \"exports\": {\n \".\": {\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\"\n }\n },\n \"types\": \"./dist/index.d.ts\",\n \"license\": \"MIT\",\n \"scripts\": {\n \"build\": \"tsup --clean\",\n \"dev\": \"tsup --watch --no-splitting\",\n \"test\": \"jest --passWithNoTests\",\n \"check-types\": \"tsc --noEmit\",\n \"clean\": \"rm -rf .turbo && rm -rf node_modules && rm -rf dist && rm -rf .next\",\n \"link:global\": \"pnpm link --global\",\n \"unlink:global\": \"pnpm unlink --global\"\n },\n \"devDependencies\": {\n \"@types/jest\": \"^29.5.4\",\n \"@types/uuid\": \"^10.0.0\",\n \"eslint\": \"^8.56.0\",\n \"eslint-config-custom\": \"workspace:*\",\n \"jest\": \"^29.6.4\",\n \"ts-jest\": \"^29.1.1\",\n \"tsconfig\": \"workspace:*\",\n \"tsup\": \"^6.7.0\",\n \"typescript\": \"^5.2.3\"\n },\n \"dependencies\": {\n \"@segment/analytics-node\": \"^2.1.2\",\n \"chalk\": \"4.1.2\",\n \"uuid\": \"^10.0.0\",\n \"zod\": \"^3.23.3\",\n \"zod-to-json-schema\": \"^3.23.5\"\n },\n \"keywords\": [\n \"copilotkit\",\n \"copilot\",\n \"react\",\n \"nextjs\",\n \"nodejs\",\n \"ai\",\n \"assistant\",\n \"javascript\",\n \"automation\",\n \"textarea\"\n ]\n}\n","export * from \"./types\";\nexport * from \"./utils\";\nexport * from \"./constants\";\nexport * from \"./telemetry\";\n\nimport * as packageJson from \"../package.json\";\nexport const COPILOTKIT_VERSION = packageJson.version;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAWE,cAAW;;;ACLN,IAAM,qBAAiC;","names":[]}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export { JSONSchema, JSONSchemaArray, JSONSchemaBoolean, JSONSchemaNumber, JSONSchemaObject, JSONSchemaString, actionParametersToJsonSchema } from './json-schema.js';
|
|
1
|
+
export { JSONSchema, JSONSchemaArray, JSONSchemaBoolean, JSONSchemaNumber, JSONSchemaObject, JSONSchemaString, actionParametersToJsonSchema, convertJsonSchemaToZodSchema } from './json-schema.js';
|
|
2
2
|
export { randomId } from './random-id.js';
|
|
3
|
+
import 'zod';
|
|
3
4
|
import '../types/action.js';
|
package/dist/utils/index.js
CHANGED
|
@@ -21,11 +21,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var utils_exports = {};
|
|
22
22
|
__export(utils_exports, {
|
|
23
23
|
actionParametersToJsonSchema: () => actionParametersToJsonSchema,
|
|
24
|
+
convertJsonSchemaToZodSchema: () => convertJsonSchemaToZodSchema,
|
|
24
25
|
randomId: () => randomId
|
|
25
26
|
});
|
|
26
27
|
module.exports = __toCommonJS(utils_exports);
|
|
27
28
|
|
|
28
29
|
// src/utils/json-schema.ts
|
|
30
|
+
var import_zod = require("zod");
|
|
29
31
|
function actionParametersToJsonSchema(actionParameters) {
|
|
30
32
|
let parameters = {};
|
|
31
33
|
for (let parameter of actionParameters || []) {
|
|
@@ -100,6 +102,36 @@ function convertAttribute(attribute) {
|
|
|
100
102
|
};
|
|
101
103
|
}
|
|
102
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
|
+
}
|
|
103
135
|
|
|
104
136
|
// src/utils/random-id.ts
|
|
105
137
|
var import_uuid = require("uuid");
|
|
@@ -109,6 +141,7 @@ function randomId() {
|
|
|
109
141
|
// Annotate the CommonJS export names for ESM import in node:
|
|
110
142
|
0 && (module.exports = {
|
|
111
143
|
actionParametersToJsonSchema,
|
|
144
|
+
convertJsonSchemaToZodSchema,
|
|
112
145
|
randomId
|
|
113
146
|
});
|
|
114
147
|
//# sourceMappingURL=index.js.map
|
package/dist/utils/index.js.map
CHANGED
|
@@ -1 +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 { 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","import { v4 as uuidv4 } from \"uuid\";\n\nexport function randomId() {\n return \"ck-\" + uuidv4();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;
|
|
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(\"Invalid JSON schema\");\n}\n","import { v4 as uuidv4 } from \"uuid\";\n\nexport function randomId() {\n return \"ck-\" + uuidv4();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,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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
|
package/dist/utils/index.mjs
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import "../chunk-CYDWEPFL.mjs";
|
|
2
2
|
import {
|
|
3
|
-
actionParametersToJsonSchema
|
|
4
|
-
|
|
3
|
+
actionParametersToJsonSchema,
|
|
4
|
+
convertJsonSchemaToZodSchema
|
|
5
|
+
} from "../chunk-JP2M4U4G.mjs";
|
|
5
6
|
import {
|
|
6
7
|
randomId
|
|
7
8
|
} from "../chunk-RIPX6APP.mjs";
|
|
8
9
|
export {
|
|
9
10
|
actionParametersToJsonSchema,
|
|
11
|
+
convertJsonSchemaToZodSchema,
|
|
10
12
|
randomId
|
|
11
13
|
};
|
|
12
14
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
1
2
|
import { Parameter } from '../types/action.js';
|
|
2
3
|
|
|
3
4
|
type JSONSchemaString = {
|
|
@@ -26,5 +27,6 @@ type JSONSchemaArray = {
|
|
|
26
27
|
};
|
|
27
28
|
type JSONSchema = JSONSchemaString | JSONSchemaNumber | JSONSchemaBoolean | JSONSchemaObject | JSONSchemaArray;
|
|
28
29
|
declare function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema;
|
|
30
|
+
declare function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema;
|
|
29
31
|
|
|
30
|
-
export { JSONSchema, JSONSchemaArray, JSONSchemaBoolean, JSONSchemaNumber, JSONSchemaObject, JSONSchemaString, actionParametersToJsonSchema };
|
|
32
|
+
export { JSONSchema, JSONSchemaArray, JSONSchemaBoolean, JSONSchemaNumber, JSONSchemaObject, JSONSchemaString, actionParametersToJsonSchema, convertJsonSchemaToZodSchema };
|
|
@@ -20,9 +20,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/utils/json-schema.ts
|
|
21
21
|
var json_schema_exports = {};
|
|
22
22
|
__export(json_schema_exports, {
|
|
23
|
-
actionParametersToJsonSchema: () => actionParametersToJsonSchema
|
|
23
|
+
actionParametersToJsonSchema: () => actionParametersToJsonSchema,
|
|
24
|
+
convertJsonSchemaToZodSchema: () => convertJsonSchemaToZodSchema
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(json_schema_exports);
|
|
27
|
+
var import_zod = require("zod");
|
|
26
28
|
function actionParametersToJsonSchema(actionParameters) {
|
|
27
29
|
let parameters = {};
|
|
28
30
|
for (let parameter of actionParameters || []) {
|
|
@@ -97,8 +99,39 @@ function convertAttribute(attribute) {
|
|
|
97
99
|
};
|
|
98
100
|
}
|
|
99
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
|
+
}
|
|
100
132
|
// Annotate the CommonJS export names for ESM import in node:
|
|
101
133
|
0 && (module.exports = {
|
|
102
|
-
actionParametersToJsonSchema
|
|
134
|
+
actionParametersToJsonSchema,
|
|
135
|
+
convertJsonSchemaToZodSchema
|
|
103
136
|
});
|
|
104
137
|
//# sourceMappingURL=json-schema.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/json-schema.ts"],"sourcesContent":["import { 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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
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,IACF,KAAK;AAAA,IACL,KAAK;AACH,aAAO;AAAA,QACL,MAAM,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AAE,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":[]}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
-
actionParametersToJsonSchema
|
|
3
|
-
|
|
2
|
+
actionParametersToJsonSchema,
|
|
3
|
+
convertJsonSchemaToZodSchema
|
|
4
|
+
} from "../chunk-JP2M4U4G.mjs";
|
|
4
5
|
export {
|
|
5
|
-
actionParametersToJsonSchema
|
|
6
|
+
actionParametersToJsonSchema,
|
|
7
|
+
convertJsonSchemaToZodSchema
|
|
6
8
|
};
|
|
7
9
|
//# sourceMappingURL=json-schema.mjs.map
|
|
@@ -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,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B,CAAC,EACA,SAAS;AAEZ,UAAM,SAAS,6BAA6B,YAAY,KAAK;AAC7D,UAAM,uBAAmB,2CAAgB,MAAM;AAC/C,UAAM,yBAAqB,2CAAgB,cAAc;AAEzD,WAAO,gBAAgB,EAAE,cAAc,kBAAkB;AAAA,EAC3D,CAAC;AACH,CAAC;","names":["import_zod"]}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import {
|
|
2
|
+
convertJsonSchemaToZodSchema
|
|
3
|
+
} from "../chunk-JP2M4U4G.mjs";
|
|
4
|
+
|
|
5
|
+
// src/utils/json-schema.test.ts
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
8
|
+
describe("convertJsonSchemaToZodSchema", () => {
|
|
9
|
+
it("should convert a simple JSON schema to a Zod schema", () => {
|
|
10
|
+
const jsonSchema = {
|
|
11
|
+
type: "object",
|
|
12
|
+
properties: {
|
|
13
|
+
name: { type: "string" },
|
|
14
|
+
age: { type: "number" }
|
|
15
|
+
},
|
|
16
|
+
required: ["name", "age"]
|
|
17
|
+
};
|
|
18
|
+
const expectedSchema = z.object({
|
|
19
|
+
name: z.string(),
|
|
20
|
+
age: z.number()
|
|
21
|
+
});
|
|
22
|
+
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
23
|
+
const resultSchemaJson = zodToJsonSchema(result);
|
|
24
|
+
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
25
|
+
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
26
|
+
});
|
|
27
|
+
it("should convert a JSON schema with nested objects to a Zod schema", () => {
|
|
28
|
+
const jsonSchema = {
|
|
29
|
+
type: "object",
|
|
30
|
+
properties: {
|
|
31
|
+
name: { type: "string" },
|
|
32
|
+
address: {
|
|
33
|
+
type: "object",
|
|
34
|
+
properties: {
|
|
35
|
+
street: { type: "string" },
|
|
36
|
+
city: { type: "string" }
|
|
37
|
+
},
|
|
38
|
+
required: ["street", "city"]
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
required: ["name", "address"]
|
|
42
|
+
};
|
|
43
|
+
const expectedSchema = z.object({
|
|
44
|
+
name: z.string(),
|
|
45
|
+
address: z.object({
|
|
46
|
+
street: z.string(),
|
|
47
|
+
city: z.string()
|
|
48
|
+
})
|
|
49
|
+
});
|
|
50
|
+
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
51
|
+
const resultSchemaJson = zodToJsonSchema(result);
|
|
52
|
+
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
53
|
+
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
54
|
+
});
|
|
55
|
+
it("should convert a JSON schema with arrays to a Zod schema", () => {
|
|
56
|
+
const jsonSchema = {
|
|
57
|
+
type: "object",
|
|
58
|
+
properties: {
|
|
59
|
+
names: {
|
|
60
|
+
type: "array",
|
|
61
|
+
items: { type: "string" }
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
required: ["names"]
|
|
65
|
+
};
|
|
66
|
+
const expectedSchema = z.object({
|
|
67
|
+
names: z.array(z.string())
|
|
68
|
+
});
|
|
69
|
+
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
70
|
+
const resultSchemaJson = zodToJsonSchema(result);
|
|
71
|
+
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
72
|
+
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
73
|
+
});
|
|
74
|
+
it("should convert a JSON schema with optional properties to a Zod schema", () => {
|
|
75
|
+
const jsonSchema = {
|
|
76
|
+
type: "object",
|
|
77
|
+
properties: {
|
|
78
|
+
name: { type: "string" },
|
|
79
|
+
age: { type: "number", required: false }
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
const expectedSchema = z.object({
|
|
83
|
+
name: z.string().optional(),
|
|
84
|
+
age: z.number().optional()
|
|
85
|
+
}).optional();
|
|
86
|
+
const result = convertJsonSchemaToZodSchema(jsonSchema, false);
|
|
87
|
+
console.log(convertJsonSchemaToZodSchema(jsonSchema, false));
|
|
88
|
+
const resultSchemaJson = zodToJsonSchema(result);
|
|
89
|
+
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
90
|
+
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
91
|
+
});
|
|
92
|
+
it("should convert a JSON schema with different types to a Zod schema", () => {
|
|
93
|
+
const jsonSchema = {
|
|
94
|
+
type: "object",
|
|
95
|
+
properties: {
|
|
96
|
+
name: { type: "string" },
|
|
97
|
+
age: { type: "number" },
|
|
98
|
+
isAdmin: { type: "boolean" }
|
|
99
|
+
},
|
|
100
|
+
required: ["name", "age", "isAdmin"]
|
|
101
|
+
};
|
|
102
|
+
const expectedSchema = z.object({
|
|
103
|
+
name: z.string(),
|
|
104
|
+
age: z.number(),
|
|
105
|
+
isAdmin: z.boolean()
|
|
106
|
+
});
|
|
107
|
+
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
108
|
+
const resultSchemaJson = zodToJsonSchema(result);
|
|
109
|
+
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
110
|
+
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
111
|
+
});
|
|
112
|
+
it("should handle edge case where JSON schema has no properties", () => {
|
|
113
|
+
const jsonSchema = {
|
|
114
|
+
type: "object"
|
|
115
|
+
};
|
|
116
|
+
const expectedSchema = z.object({});
|
|
117
|
+
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
118
|
+
const resultSchemaJson = zodToJsonSchema(result);
|
|
119
|
+
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
120
|
+
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
121
|
+
});
|
|
122
|
+
it("should handle edge case where JSON schema has no required properties", () => {
|
|
123
|
+
const jsonSchema = {
|
|
124
|
+
type: "object",
|
|
125
|
+
properties: {
|
|
126
|
+
name: { type: "string" },
|
|
127
|
+
age: { type: "number" }
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
const expectedSchema = z.object({
|
|
131
|
+
name: z.string().optional(),
|
|
132
|
+
age: z.number().optional()
|
|
133
|
+
}).optional();
|
|
134
|
+
const result = convertJsonSchemaToZodSchema(jsonSchema, false);
|
|
135
|
+
const resultSchemaJson = zodToJsonSchema(result);
|
|
136
|
+
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
137
|
+
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
//# sourceMappingURL=json-schema.test.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/json-schema.test.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"],"mappings":";;;;;AAAA,SAAS,SAAS;AAElB,SAAS,uBAAuB;AAEhC,SAAS,gCAAgC,MAAM;AAC7C,KAAG,uDAAuD,MAAM;AAC9D,UAAM,aAAa;AAAA,MACjB,MAAM;AAAA,MACN,YAAY;AAAA,QACV,MAAM,EAAE,MAAM,SAAS;AAAA,QACvB,KAAK,EAAE,MAAM,SAAS;AAAA,MACxB;AAAA,MACA,UAAU,CAAC,QAAQ,KAAK;AAAA,IAC1B;AAEA,UAAM,iBAAiB,EAAE,OAAO;AAAA,MAC9B,MAAM,EAAE,OAAO;AAAA,MACf,KAAK,EAAE,OAAO;AAAA,IAChB,CAAC;AAED,UAAM,SAAS,6BAA6B,YAAY,IAAI;AAC5D,UAAM,mBAAmB,gBAAgB,MAAM;AAC/C,UAAM,qBAAqB,gBAAgB,cAAc;AAEzD,WAAO,gBAAgB,EAAE,cAAc,kBAAkB;AAAA,EAC3D,CAAC;AAED,KAAG,oEAAoE,MAAM;AAC3E,UAAM,aAAa;AAAA,MACjB,MAAM;AAAA,MACN,YAAY;AAAA,QACV,MAAM,EAAE,MAAM,SAAS;AAAA,QACvB,SAAS;AAAA,UACP,MAAM;AAAA,UACN,YAAY;AAAA,YACV,QAAQ,EAAE,MAA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MACjB,MAAM;AAAA,MACN,YAAY;AAAA,QACV,MAAM,EAAE,MAAM,SAAS;AAAA,QACvB,KAAK,EAAE,MAAM,SAAS;AAAA,MACxB;AAAA,IACF;AAEA,UAAM,iBAAiB,EACpB,OAAO;AAAA,MACN,MAAM,EAAE,OAAO,EAAE,SAAS;AAAA,MAC1B,KAAK,EAAE,OAAO,EAAE,SAAS;AAAA,IAC3B,CAAC,EACA,SAAS;AAEZ,UAAM,SAAS,6BAA6B,YAAY,KAAK;AAC7D,UAAM,mBAAmB,gBAAgB,MAAM;AAC/C,UAAM,qBAAqB,gBAAgB,cAAc;AAEzD,WAAO,gBAAgB,EAAE,cAAc,kBAAkB;AAAA,EAC3D,CAAC;AACH,CAAC;","names":[]}
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"publishConfig": {
|
|
10
10
|
"access": "public"
|
|
11
11
|
},
|
|
12
|
-
"version": "1.4.1-pre.
|
|
12
|
+
"version": "1.4.1-pre.1",
|
|
13
13
|
"sideEffects": false,
|
|
14
14
|
"main": "./dist/index.js",
|
|
15
15
|
"module": "./dist/index.mjs",
|
|
@@ -30,13 +30,15 @@
|
|
|
30
30
|
"ts-jest": "^29.1.1",
|
|
31
31
|
"tsup": "^6.7.0",
|
|
32
32
|
"typescript": "^5.2.3",
|
|
33
|
-
"eslint-config-custom": "1.4.1-pre.
|
|
34
|
-
"tsconfig": "1.4.1-pre.
|
|
33
|
+
"eslint-config-custom": "1.4.1-pre.1",
|
|
34
|
+
"tsconfig": "1.4.1-pre.1"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@segment/analytics-node": "^2.1.2",
|
|
38
38
|
"chalk": "4.1.2",
|
|
39
|
-
"uuid": "^10.0.0"
|
|
39
|
+
"uuid": "^10.0.0",
|
|
40
|
+
"zod": "^3.23.3",
|
|
41
|
+
"zod-to-json-schema": "^3.23.5"
|
|
40
42
|
},
|
|
41
43
|
"keywords": [
|
|
42
44
|
"copilotkit",
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { convertJsonSchemaToZodSchema } from "../utils/json-schema";
|
|
3
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
4
|
+
|
|
5
|
+
describe("convertJsonSchemaToZodSchema", () => {
|
|
6
|
+
it("should convert a simple JSON schema to a Zod schema", () => {
|
|
7
|
+
const jsonSchema = {
|
|
8
|
+
type: "object",
|
|
9
|
+
properties: {
|
|
10
|
+
name: { type: "string" },
|
|
11
|
+
age: { type: "number" },
|
|
12
|
+
},
|
|
13
|
+
required: ["name", "age"],
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const expectedSchema = z.object({
|
|
17
|
+
name: z.string(),
|
|
18
|
+
age: z.number(),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
22
|
+
const resultSchemaJson = zodToJsonSchema(result);
|
|
23
|
+
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
24
|
+
|
|
25
|
+
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should convert a JSON schema with nested objects to a Zod schema", () => {
|
|
29
|
+
const jsonSchema = {
|
|
30
|
+
type: "object",
|
|
31
|
+
properties: {
|
|
32
|
+
name: { type: "string" },
|
|
33
|
+
address: {
|
|
34
|
+
type: "object",
|
|
35
|
+
properties: {
|
|
36
|
+
street: { type: "string" },
|
|
37
|
+
city: { type: "string" },
|
|
38
|
+
},
|
|
39
|
+
required: ["street", "city"],
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
required: ["name", "address"],
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const expectedSchema = z.object({
|
|
46
|
+
name: z.string(),
|
|
47
|
+
address: z.object({
|
|
48
|
+
street: z.string(),
|
|
49
|
+
city: z.string(),
|
|
50
|
+
}),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
54
|
+
const resultSchemaJson = zodToJsonSchema(result);
|
|
55
|
+
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
56
|
+
|
|
57
|
+
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should convert a JSON schema with arrays to a Zod schema", () => {
|
|
61
|
+
const jsonSchema = {
|
|
62
|
+
type: "object",
|
|
63
|
+
properties: {
|
|
64
|
+
names: {
|
|
65
|
+
type: "array",
|
|
66
|
+
items: { type: "string" },
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
required: ["names"],
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const expectedSchema = z.object({
|
|
73
|
+
names: z.array(z.string()),
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
77
|
+
const resultSchemaJson = zodToJsonSchema(result);
|
|
78
|
+
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
79
|
+
|
|
80
|
+
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should convert a JSON schema with optional properties to a Zod schema", () => {
|
|
84
|
+
const jsonSchema = {
|
|
85
|
+
type: "object",
|
|
86
|
+
properties: {
|
|
87
|
+
name: { type: "string" },
|
|
88
|
+
age: { type: "number", required: false },
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const expectedSchema = z
|
|
93
|
+
.object({
|
|
94
|
+
name: z.string().optional(),
|
|
95
|
+
age: z.number().optional(),
|
|
96
|
+
})
|
|
97
|
+
.optional();
|
|
98
|
+
|
|
99
|
+
const result = convertJsonSchemaToZodSchema(jsonSchema, false);
|
|
100
|
+
|
|
101
|
+
console.log(convertJsonSchemaToZodSchema(jsonSchema, false));
|
|
102
|
+
|
|
103
|
+
const resultSchemaJson = zodToJsonSchema(result);
|
|
104
|
+
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
105
|
+
|
|
106
|
+
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("should convert a JSON schema with different types to a Zod schema", () => {
|
|
110
|
+
const jsonSchema = {
|
|
111
|
+
type: "object",
|
|
112
|
+
properties: {
|
|
113
|
+
name: { type: "string" },
|
|
114
|
+
age: { type: "number" },
|
|
115
|
+
isAdmin: { type: "boolean" },
|
|
116
|
+
},
|
|
117
|
+
required: ["name", "age", "isAdmin"],
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const expectedSchema = z.object({
|
|
121
|
+
name: z.string(),
|
|
122
|
+
age: z.number(),
|
|
123
|
+
isAdmin: z.boolean(),
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
127
|
+
const resultSchemaJson = zodToJsonSchema(result);
|
|
128
|
+
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
129
|
+
|
|
130
|
+
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("should handle edge case where JSON schema has no properties", () => {
|
|
134
|
+
const jsonSchema = {
|
|
135
|
+
type: "object",
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const expectedSchema = z.object({});
|
|
139
|
+
|
|
140
|
+
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
141
|
+
const resultSchemaJson = zodToJsonSchema(result);
|
|
142
|
+
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
143
|
+
|
|
144
|
+
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("should handle edge case where JSON schema has no required properties", () => {
|
|
148
|
+
const jsonSchema = {
|
|
149
|
+
type: "object",
|
|
150
|
+
properties: {
|
|
151
|
+
name: { type: "string" },
|
|
152
|
+
age: { type: "number" },
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const expectedSchema = z
|
|
157
|
+
.object({
|
|
158
|
+
name: z.string().optional(),
|
|
159
|
+
age: z.number().optional(),
|
|
160
|
+
})
|
|
161
|
+
.optional();
|
|
162
|
+
|
|
163
|
+
const result = convertJsonSchemaToZodSchema(jsonSchema, false);
|
|
164
|
+
const resultSchemaJson = zodToJsonSchema(result);
|
|
165
|
+
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
166
|
+
|
|
167
|
+
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
168
|
+
});
|
|
169
|
+
});
|
package/src/utils/json-schema.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
import { Parameter } from "../types";
|
|
2
3
|
|
|
3
4
|
export type JSONSchemaString = {
|
|
@@ -118,3 +119,36 @@ function convertAttribute(attribute: Parameter): JSONSchema {
|
|
|
118
119
|
};
|
|
119
120
|
}
|
|
120
121
|
}
|
|
122
|
+
|
|
123
|
+
export function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema {
|
|
124
|
+
if (jsonSchema.type === "object") {
|
|
125
|
+
const spec: { [key: string]: z.ZodSchema } = {};
|
|
126
|
+
|
|
127
|
+
if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
|
|
128
|
+
return !required ? z.object(spec).optional() : z.object(spec);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
132
|
+
spec[key] = convertJsonSchemaToZodSchema(
|
|
133
|
+
value,
|
|
134
|
+
jsonSchema.required ? jsonSchema.required.includes(key) : false,
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
let schema = z.object(spec).describe(jsonSchema.description);
|
|
138
|
+
return required ? schema : schema.optional();
|
|
139
|
+
} else if (jsonSchema.type === "string") {
|
|
140
|
+
let schema = z.string().describe(jsonSchema.description);
|
|
141
|
+
return required ? schema : schema.optional();
|
|
142
|
+
} else if (jsonSchema.type === "number") {
|
|
143
|
+
let schema = z.number().describe(jsonSchema.description);
|
|
144
|
+
return required ? schema : schema.optional();
|
|
145
|
+
} else if (jsonSchema.type === "boolean") {
|
|
146
|
+
let schema = z.boolean().describe(jsonSchema.description);
|
|
147
|
+
return required ? schema : schema.optional();
|
|
148
|
+
} else if (jsonSchema.type === "array") {
|
|
149
|
+
let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
|
|
150
|
+
let schema = z.array(itemSchema).describe(jsonSchema.description);
|
|
151
|
+
return required ? schema : schema.optional();
|
|
152
|
+
}
|
|
153
|
+
throw new Error("Invalid JSON schema");
|
|
154
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils/json-schema.ts"],"sourcesContent":["import { 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"],"mappings":";AAsCO,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,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