From 88e66d54ce5ccdfda485281a2392c1a5c4afa4b4 Mon Sep 17 00:00:00 2001 From: Ajax Davis Date: Sun, 25 Jan 2026 12:38:32 +1000 Subject: [PATCH] fix(omega): return error result instead of throwing for tool failures When a tool execution fails, return an error object instead of throwing. This allows the AI model to see the error and inform the user properly, rather than silently failing with no output. --- .../conversations/[id]/messages/route.ts | 56 ++++++++++++------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/apps/web/src/app/api/omega/conversations/[id]/messages/route.ts b/apps/web/src/app/api/omega/conversations/[id]/messages/route.ts index ce70aa8..9532f6e 100644 --- a/apps/web/src/app/api/omega/conversations/[id]/messages/route.ts +++ b/apps/web/src/app/api/omega/conversations/[id]/messages/route.ts @@ -12,7 +12,7 @@ import { Prisma, prisma } from '@tpmjs/db'; import { registryExecuteTool } from '@tpmjs/registry-execute'; import { registrySearchTool } from '@tpmjs/registry-search'; -import { jsonSchema, wrapLanguageModel, type ModelMessage } from 'ai'; +import { jsonSchema, type ModelMessage, wrapLanguageModel } from 'ai'; import { type NextRequest, NextResponse } from 'next/server'; import { z } from 'zod'; import { authenticateRequest } from '~/lib/api-keys/middleware'; @@ -223,29 +223,43 @@ async function createDynamicTool( execute: async (params: any) => { console.log(`🚀 Executing ${toolMeta.packageName}/${toolMeta.name} with params:`, params); - const response = await fetch(`${EXECUTOR_URL}/execute-tool`, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - packageName: toolMeta.packageName, - name: toolMeta.name, - version: toolMeta.version, - importUrl: toolMeta.importUrl, - params, - env: userEnvVars, - }), - }); + try { + const response = await fetch(`${EXECUTOR_URL}/execute-tool`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + packageName: toolMeta.packageName, + name: toolMeta.name, + version: toolMeta.version, + importUrl: toolMeta.importUrl, + params, + env: userEnvVars, + }), + }); - // biome-ignore lint/suspicious/noExplicitAny: API response types vary - const result = (await response.json()) as any; + // biome-ignore lint/suspicious/noExplicitAny: API response types vary + const result = (await response.json()) as any; - if (!result.success) { - console.error(`❌ Tool execution failed: ${result.error}`); - throw new Error(result.error || 'Tool execution failed'); + if (!result.success) { + console.error(`❌ Tool execution failed: ${result.error}`); + // Return error as result instead of throwing so AI can see it + return { + error: true, + message: result.error || 'Tool execution failed', + toolId: toolMeta.toolId, + }; + } + + console.log(`✅ Tool executed in ${result.executionTimeMs}ms`); + return result.output; + } catch (error) { + console.error(`❌ Tool execution error:`, error); + return { + error: true, + message: error instanceof Error ? error.message : 'Unknown error during tool execution', + toolId: toolMeta.toolId, + }; } - - console.log(`✅ Tool executed in ${result.executionTimeMs}ms`); - return result.output; }, }); }