From ec27965ab48c69bf056d7b91b579454ebc9ce44a Mon Sep 17 00:00:00 2001 From: Ajax Davis Date: Thu, 4 Dec 2025 09:42:53 +1000 Subject: [PATCH] feat: add fallback schema extraction with Zod v4 support - Try Zod v4 toJSONSchema() or jsonSchema() first - Fall back to AI SDK jsonSchema.schema property - Fail gracefully with detailed debug info - Supports both Zod-based and jsonSchema-based tools --- apps/railway-executor/server.ts | 62 ++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/apps/railway-executor/server.ts b/apps/railway-executor/server.ts index 9876a6d..4ef0521 100644 --- a/apps/railway-executor/server.ts +++ b/apps/railway-executor/server.ts @@ -75,20 +75,58 @@ async function loadAndDescribe(req: Request): Promise { console.log(`✅ Cached: ${cacheKey}`); } - // Extract tool definition - // AI SDK v6 tools use jsonSchema() which wraps a plain JSON Schema object - // Extract the raw JSON Schema from toolModule.inputSchema.schema + // Extract tool definition - try multiple schema formats + let rawJsonSchema = null; - // Debug: Log the inputSchema structure - console.log(`🔍 InputSchema structure for ${cacheKey}:`, { - hasInputSchema: !!toolModule.inputSchema, - inputSchemaType: typeof toolModule.inputSchema, - hasSchema: !!toolModule.inputSchema?.schema, - hasParameters: !!toolModule.inputSchema?.parameters, - keys: toolModule.inputSchema ? Object.keys(toolModule.inputSchema) : [], - }); + if (toolModule.inputSchema) { + // Strategy 1: Try Zod v4 native JSON Schema export + if (typeof toolModule.inputSchema.toJSONSchema === 'function') { + console.log(`📋 Using Zod v4 toJSONSchema() for ${cacheKey}`); + try { + rawJsonSchema = toolModule.inputSchema.toJSONSchema(); + } catch (error) { + console.warn(`⚠️ Zod toJSONSchema() failed for ${cacheKey}:`, error); + } + } else if (typeof toolModule.inputSchema.jsonSchema === 'function') { + console.log(`📋 Using Zod v4 jsonSchema() for ${cacheKey}`); + try { + rawJsonSchema = toolModule.inputSchema.jsonSchema(); + } catch (error) { + console.warn(`⚠️ Zod jsonSchema() failed for ${cacheKey}:`, error); + } + } - const rawJsonSchema = toolModule.inputSchema?.schema ?? null; + // Strategy 2: Try AI SDK v6 jsonSchema() wrapper (has .schema property) + if (!rawJsonSchema && toolModule.inputSchema.schema) { + console.log(`📋 Using AI SDK jsonSchema.schema for ${cacheKey}`); + rawJsonSchema = toolModule.inputSchema.schema; + } + } + + // If no schema found, fail with helpful error + if (!rawJsonSchema) { + console.error(`❌ No valid schema found for ${cacheKey}`, { + hasInputSchema: !!toolModule.inputSchema, + inputSchemaType: typeof toolModule.inputSchema, + hasToJSONSchema: typeof toolModule.inputSchema?.toJSONSchema === 'function', + hasJsonSchema: typeof toolModule.inputSchema?.jsonSchema === 'function', + hasSchema: !!toolModule.inputSchema?.schema, + keys: toolModule.inputSchema ? Object.keys(toolModule.inputSchema) : [], + }); + return Response.json( + { + success: false, + error: `Tool "${exportName}" has no valid inputSchema. Tools must use AI SDK jsonSchema() or Zod v4 with toJSONSchema().`, + debug: { + hasInputSchema: !!toolModule.inputSchema, + availableMethods: toolModule.inputSchema ? Object.keys(toolModule.inputSchema) : [], + }, + }, + { status: 400 } + ); + } + + console.log(`✅ Extracted schema for ${cacheKey}`); return Response.json({ success: true,