From 078d843b78c04e4015a3f0cd1144db3563e04133 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 23 Jan 2026 23:56:10 -0600 Subject: [PATCH] Upgrade to zod 4 --- package-lock.json | 84 +++++++++++------- package.json | 12 +-- src/lib/configUtils.ts | 4 +- src/lib/tools/openai-adapter.test.ts | 10 +-- src/lib/tools/openai-adapter.ts | 124 +-------------------------- 5 files changed, 65 insertions(+), 169 deletions(-) diff --git a/package-lock.json b/package-lock.json index b548c126..98fa62ef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,12 +14,12 @@ "@dnd-kit/utilities": "^3.2.2", "@fontsource-variable/inter": "^5.2.6", "@getalby/sdk": "^6.0.1", - "@hookform/resolvers": "^3.9.0", + "@hookform/resolvers": "^5.2.2", "@isomorphic-git/lightning-fs": "^4.6.2", "@modelcontextprotocol/sdk": "^1.22.0", "@mozilla/readability": "^0.6.0", - "@nostrify/nostrify": "^0.48.2", - "@nostrify/react": "^0.2.20", + "@nostrify/nostrify": "^0.49.2", + "@nostrify/react": "^0.2.24", "@opensecret/react": "^1.5.4", "@radix-ui/react-accordion": "^1.2.0", "@radix-ui/react-alert-dialog": "^1.1.1", @@ -71,7 +71,7 @@ "mime": "^4.1.0", "nostr-tools": "^2.15.2", "nunjucks": "^3.2.4", - "openai": "^5.15.0", + "openai": "^6.16.0", "path-browserify": "^1.0.1", "prismjs": "^1.30.0", "qrcode": "^1.5.4", @@ -79,7 +79,7 @@ "react-day-picker": "^8.10.1", "react-dom": "^18.3.1", "react-error-boundary": "^6.0.0", - "react-hook-form": "^7.53.0", + "react-hook-form": "^7.71.1", "react-i18next": "^15.7.3", "react-markdown": "^10.1.0", "react-resizable-panels": "^3.0.3", @@ -96,7 +96,7 @@ "vaul": "^0.9.3", "webln": "^0.3.2", "workbox-window": "^7.3.0", - "zod": "^3.25.76" + "zod": "^4.3.6" }, "devDependencies": { "@eslint/js": "^9.9.0", @@ -2591,12 +2591,15 @@ } }, "node_modules/@hookform/resolvers": { - "version": "3.10.0", - "resolved": "https://registry.npmjs.org/@hookform/resolvers/-/resolvers-3.10.0.tgz", - "integrity": "sha512-79Dv+3mDF7i+2ajj7SkypSKHhl1cbln1OGavqrsF7p6mbUv11xpqpacPsGDCTRvCSjEEIez2ef1NveSVL3b0Ag==", + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/@hookform/resolvers/-/resolvers-5.2.2.tgz", + "integrity": "sha512-A/IxlMLShx3KjV/HeTcTfaMxdwy690+L/ZADoeaTltLx+CVuzkeVIPuybK3jrRfw7YZnmdKsVVHAlEPIAEUNlA==", "license": "MIT", + "dependencies": { + "@standard-schema/utils": "^0.3.0" + }, "peerDependencies": { - "react-hook-form": "^7.0.0" + "react-hook-form": "^7.55.0" } }, "node_modules/@html-eslint/eslint-plugin": { @@ -2964,11 +2967,11 @@ } }, "node_modules/@nostrify/nostrify": { - "version": "0.48.2", - "resolved": "https://registry.npmjs.org/@nostrify/nostrify/-/nostrify-0.48.2.tgz", - "integrity": "sha512-Zze3RASsgn4MLQCuC5I4XR0WJi9G3J8qZpVjVIPvkw6vbZRct++JgavcZSxwKjJH1hHq+8d90IrHMOrskNQ8NQ==", + "version": "0.49.2", + "resolved": "https://registry.npmjs.org/@nostrify/nostrify/-/nostrify-0.49.2.tgz", + "integrity": "sha512-JMiGifc/XX4QhzFnmJVw00UjcOS295w/NYth9mJH0cbS0EqPQ/4HBn33P0DGW3iE5EAkk+n98cnwzYeIPtn9pQ==", "dependencies": { - "@nostrify/types": "0.36.8", + "@nostrify/types": "0.36.9", "@scure/base": "^2.0.0", "@smithy/util-base64": "^4.3.0", "@smithy/util-hex-encoding": "^4.2.0", @@ -2977,7 +2980,7 @@ "nostr-tools": "^2.13.0", "websocket-ts": "^2.2.1", "ws": "^8.18.3", - "zod": "^3.23.8" + "zod": "^4.3.6" } }, "node_modules/@nostrify/nostrify/node_modules/@scure/base": { @@ -3005,12 +3008,12 @@ "license": "MIT" }, "node_modules/@nostrify/react": { - "version": "0.2.20", - "resolved": "https://registry.npmjs.org/@nostrify/react/-/react-0.2.20.tgz", - "integrity": "sha512-Tvkdr5X+MQxh6CKKthk9Idch3vFp5nPcm57uJZJWrIqBtuj9jxMamM6uEo2lQAr0ugK52xDlyD50lRDRe0RMbQ==", + "version": "0.2.24", + "resolved": "https://registry.npmjs.org/@nostrify/react/-/react-0.2.24.tgz", + "integrity": "sha512-LdB6UjNKZ/Xhz+jj8CRCcudHt4WNNVZMgJsiO+XAloqWE8oPRf1G2i1k6I/eF6buCJokKE1PFUTMlyieOpIXNQ==", "dependencies": { - "@nostrify/nostrify": "0.48.2", - "@nostrify/types": "0.36.8", + "@nostrify/nostrify": "0.49.2", + "@nostrify/types": "0.36.9", "@tanstack/react-query": "^5.69.0", "nostr-tools": "^2.13.0", "react": "^18.0.0", @@ -3018,9 +3021,9 @@ } }, "node_modules/@nostrify/types": { - "version": "0.36.8", - "resolved": "https://registry.npmjs.org/@nostrify/types/-/types-0.36.8.tgz", - "integrity": "sha512-tmpz5pkU8oXu7+V1qxvAzuoo8s5x4Gvo7h+JRm+JAW6x/2VZ8aQoA54yhn89qZ9agciMJhVdhqCm1PlK55w17A==" + "version": "0.36.9", + "resolved": "https://registry.npmjs.org/@nostrify/types/-/types-0.36.9.tgz", + "integrity": "sha512-tMx/r0W+QoVRRgs8d6ltaSgrftasOXuFsi33kW8WirswCy2b3UR1tqRgc0iBU9zRa9XR0nlej/wJZW+6wUFi+Q==" }, "node_modules/@opensecret/react": { "version": "1.5.4", @@ -3041,6 +3044,15 @@ "react-dom": "^18.0.0 || ^19.0.0" } }, + "node_modules/@opensecret/react/node_modules/zod": { + "version": "3.25.76", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.25.76.tgz", + "integrity": "sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } + }, "node_modules/@peculiar/asn1-cms": { "version": "2.6.0", "resolved": "https://registry.npmjs.org/@peculiar/asn1-cms/-/asn1-cms-2.6.0.tgz", @@ -5267,6 +5279,12 @@ "integrity": "sha512-1eU2K9EgOcV4qc9jcP6G72xxZxEm5PfeI5H55l08W95b4oRJaqhmlWRc4xZAm6IVSKhVNxMi66V67hCzzuMTAg==", "license": "MIT" }, + "node_modules/@standard-schema/utils": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@standard-schema/utils/-/utils-0.3.0.tgz", + "integrity": "sha512-e7Mew686owMaPJVNNLs55PUvgz371nKgwsc4vxE49zsODpJEnxgxRo2y/OKrqueavXgZNMDVj3DdHFlaSAeU8g==", + "license": "MIT" + }, "node_modules/@std/encoding": { "name": "@jsr/std__encoding", "version": "1.0.10", @@ -12211,16 +12229,16 @@ } }, "node_modules/openai": { - "version": "5.15.0", - "resolved": "https://registry.npmjs.org/openai/-/openai-5.15.0.tgz", - "integrity": "sha512-kcUdws8K/A8m02I+IqFBwO51gS+87GP89yWEufGbzEi8anBz4FB/bti2QxaJdGwwY4mwJGzx85XO7TuL/Tpu1w==", + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/openai/-/openai-6.16.0.tgz", + "integrity": "sha512-fZ1uBqjFUjXzbGc35fFtYKEOxd20kd9fDpFeqWtsOZWiubY8CZ1NAlXHW3iathaFvqmNtCWMIsosCuyeI7Joxg==", "license": "Apache-2.0", "bin": { "openai": "bin/cli" }, "peerDependencies": { "ws": "^8.18.0", - "zod": "^3.23.8" + "zod": "^3.25 || ^4.0" }, "peerDependenciesMeta": { "ws": { @@ -12976,9 +12994,9 @@ } }, "node_modules/react-hook-form": { - "version": "7.56.1", - "resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-7.56.1.tgz", - "integrity": "sha512-qWAVokhSpshhcEuQDSANHx3jiAEFzu2HAaaQIzi/r9FNPm1ioAvuJSD4EuZzWd7Al7nTRKcKPnBKO7sRn+zavQ==", + "version": "7.71.1", + "resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-7.71.1.tgz", + "integrity": "sha512-9SUJKCGKo8HUSsCO+y0CtqkqI5nNuaDqTxyqPsZPqIwudpj4rCrAz/jZV+jn57bx5gtZKOh3neQu94DXMc+w5w==", "license": "MIT", "engines": { "node": ">=18.0.0" @@ -16814,9 +16832,9 @@ } }, "node_modules/zod": { - "version": "3.25.76", - "resolved": "https://registry.npmjs.org/zod/-/zod-3.25.76.tgz", - "integrity": "sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==", + "version": "4.3.6", + "resolved": "https://registry.npmjs.org/zod/-/zod-4.3.6.tgz", + "integrity": "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==", "license": "MIT", "funding": { "url": "https://github.com/sponsors/colinhacks" diff --git a/package.json b/package.json index b7742d3a..ae48af24 100644 --- a/package.json +++ b/package.json @@ -25,12 +25,12 @@ "@dnd-kit/utilities": "^3.2.2", "@fontsource-variable/inter": "^5.2.6", "@getalby/sdk": "^6.0.1", - "@hookform/resolvers": "^3.9.0", + "@hookform/resolvers": "^5.2.2", "@isomorphic-git/lightning-fs": "^4.6.2", "@modelcontextprotocol/sdk": "^1.22.0", "@mozilla/readability": "^0.6.0", - "@nostrify/nostrify": "^0.48.2", - "@nostrify/react": "^0.2.20", + "@nostrify/nostrify": "^0.49.2", + "@nostrify/react": "^0.2.24", "@opensecret/react": "^1.5.4", "@radix-ui/react-accordion": "^1.2.0", "@radix-ui/react-alert-dialog": "^1.1.1", @@ -82,7 +82,7 @@ "mime": "^4.1.0", "nostr-tools": "^2.15.2", "nunjucks": "^3.2.4", - "openai": "^5.15.0", + "openai": "^6.16.0", "path-browserify": "^1.0.1", "prismjs": "^1.30.0", "qrcode": "^1.5.4", @@ -90,7 +90,7 @@ "react-day-picker": "^8.10.1", "react-dom": "^18.3.1", "react-error-boundary": "^6.0.0", - "react-hook-form": "^7.53.0", + "react-hook-form": "^7.71.1", "react-i18next": "^15.7.3", "react-markdown": "^10.1.0", "react-resizable-panels": "^3.0.3", @@ -107,7 +107,7 @@ "vaul": "^0.9.3", "webln": "^0.3.2", "workbox-window": "^7.3.0", - "zod": "^3.25.76" + "zod": "^4.3.6" }, "devDependencies": { "@eslint/js": "^9.9.0", diff --git a/src/lib/configUtils.ts b/src/lib/configUtils.ts index 8349f3dc..3a3ee548 100644 --- a/src/lib/configUtils.ts +++ b/src/lib/configUtils.ts @@ -208,7 +208,7 @@ export async function readAISettings(fs: JSRuntimeFS, configPath = '/config'): P }; } catch (error) { if (error instanceof z.ZodError) { - console.error('AI settings parsing error:', error.errors); + console.error('AI settings parsing error:', error.issues); } return defaultSettings; } @@ -329,7 +329,7 @@ export async function readDeploySettings(fs: JSRuntimeFS, configPath = '/config' return deploySettingsSchema.parse(data); } catch (error) { if (error instanceof z.ZodError) { - console.error('Deploy settings parsing error:', error.errors); + console.error('Deploy settings parsing error:', error.issues); } return defaultSettings; } diff --git a/src/lib/tools/openai-adapter.test.ts b/src/lib/tools/openai-adapter.test.ts index 66465539..520ff03f 100644 --- a/src/lib/tools/openai-adapter.test.ts +++ b/src/lib/tools/openai-adapter.test.ts @@ -22,7 +22,7 @@ describe('openai-adapter', () => { expect(result.type).toBe('function'); expect(result.function.name).toBe('test_tool'); expect(result.function.description).toBe('Test tool'); - expect(result.function.parameters).toEqual({ + expect(result.function.parameters).toMatchObject({ type: 'object', properties: { message: { @@ -50,7 +50,7 @@ describe('openai-adapter', () => { const result = toolToOpenAI('user_tool', tool) as OpenAI.Chat.Completions.ChatCompletionTool & { type: 'function' }; - expect(result.function.parameters).toEqual({ + expect(result.function.parameters).toMatchObject({ type: 'object', properties: { user: { @@ -92,7 +92,7 @@ describe('openai-adapter', () => { const result = toolToOpenAI('todo_tool', tool) as OpenAI.Chat.Completions.ChatCompletionTool & { type: 'function' }; - expect(result.function.parameters).toEqual({ + expect(result.function.parameters).toMatchObject({ type: 'object', properties: { todos: { @@ -137,7 +137,7 @@ describe('openai-adapter', () => { const result = toolToOpenAI('test_tool', tool) as OpenAI.Chat.Completions.ChatCompletionTool & { type: 'function' }; - expect(result.function.parameters).toEqual({ + expect(result.function.parameters).toMatchObject({ type: 'object', properties: { required: { @@ -182,7 +182,7 @@ describe('openai-adapter', () => { const result = toolToOpenAI('priority_tool', tool) as OpenAI.Chat.Completions.ChatCompletionTool & { type: 'function' }; - expect(result.function.parameters).toEqual({ + expect(result.function.parameters).toMatchObject({ type: 'object', properties: { priority: { diff --git a/src/lib/tools/openai-adapter.ts b/src/lib/tools/openai-adapter.ts index acfd84e0..c3710782 100644 --- a/src/lib/tools/openai-adapter.ts +++ b/src/lib/tools/openai-adapter.ts @@ -15,7 +15,7 @@ export function toolToOpenAI(name: string, tool: Tool): OpenAI }; if (tool.inputSchema) { - functionDef.parameters = zodSchemaToJsonSchema(tool.inputSchema); + functionDef.parameters = tool.inputSchema.toJSONSchema(); } else { functionDef.parameters = { type: "object", properties: {} }; } @@ -25,125 +25,3 @@ export function toolToOpenAI(name: string, tool: Tool): OpenAI function: functionDef }; } - -/** - * Convert a Zod schema to JSON Schema format for OpenAI - */ -function zodSchemaToJsonSchema(schema: unknown): Record { - // This is a simplified conversion. For a full implementation, - // you might want to use a library like zod-to-json-schema - const schemaObj = schema as { _def?: { typeName?: string; shape?: () => Record } }; - - if (schemaObj._def?.typeName === 'ZodObject') { - const properties: Record = {}; - const required: string[] = []; - - const shape = schemaObj._def.shape?.(); - if (shape) { - for (const [key, value] of Object.entries(shape)) { - const fieldSchema = value as { _def?: { typeName?: string } }; - properties[key] = convertZodField(fieldSchema); - - if (!fieldSchema._def?.typeName?.includes('Optional')) { - required.push(key); - } - } - } - - return { - type: 'object', - properties, - required: required.length > 0 ? required : undefined - }; - } - - return { type: 'object' }; -} - -/** - * Convert individual Zod field to JSON Schema - */ -function convertZodField(field: unknown): Record { - const fieldObj = field as { - _def?: { - typeName?: string; - description?: string; - checks?: Array<{ kind: string; value: unknown }>; - innerType?: unknown; - type?: unknown; - shape?: () => Record; - values?: unknown[]; - } - }; - - const typeName = fieldObj._def?.typeName; - - switch (typeName) { - case 'ZodString': - return { - type: 'string', - description: fieldObj._def?.description - }; - case 'ZodNumber': - return { - type: 'number', - description: fieldObj._def?.description, - minimum: fieldObj._def?.checks?.find((c) => c.kind === 'min')?.value - }; - case 'ZodBoolean': - return { - type: 'boolean', - description: fieldObj._def?.description - }; - case 'ZodOptional': { - const innerSchema = convertZodField(fieldObj._def?.innerType); - // Preserve description from ZodOptional if it exists - if (fieldObj._def?.description && !innerSchema.description) { - innerSchema.description = fieldObj._def.description; - } - return innerSchema; - } - case 'ZodArray': - return { - type: 'array', - items: convertZodField(fieldObj._def?.type), - description: fieldObj._def?.description - }; - case 'ZodObject': { - const properties: Record = {}; - const required: string[] = []; - - const shape = fieldObj._def?.shape?.(); - if (shape) { - for (const [key, value] of Object.entries(shape)) { - const fieldSchema = value as { _def?: { typeName?: string } }; - properties[key] = convertZodField(fieldSchema); - - if (!fieldSchema._def?.typeName?.includes('Optional')) { - required.push(key); - } - } - } - - return { - type: 'object', - properties, - required: required.length > 0 ? required : undefined, - description: fieldObj._def?.description - }; - } - case 'ZodEnum': { - const values = fieldObj._def?.values as string[] | undefined; - return { - type: 'string', - enum: values || [], - description: fieldObj._def?.description - }; - } - default: - return { - type: 'string', - description: fieldObj._def?.description || '' - }; - } -} \ No newline at end of file