From 8b5cf72356c772bcf8844b36c0ffb6c42e33752c Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 1 Oct 2025 16:29:02 +0700 Subject: [PATCH] define and surface an error for malformed tool responses --- src/lib/SessionManager.ts | 35 ++++++++++++++++++++++-- src/lib/errors/MalformedToolCallError.ts | 22 +++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 src/lib/errors/MalformedToolCallError.ts diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index 22f416c1..7f9431ee 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -8,6 +8,7 @@ import type { NUser } from '@nostrify/react/login'; import type { AIProvider } from '@/contexts/AISettingsContext'; import { makeSystemPrompt } from './system'; import { NostrMetadata } from '@nostrify/nostrify'; +import { MalformedToolCallError } from './errors/MalformedToolCallError'; export type AIMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam | { role: 'assistant'; @@ -283,7 +284,7 @@ export class SessionManager { // LiteLLM, Z.ai, etc. use 'reasoning_content' if (delta && 'reasoning_content' in delta && typeof delta.reasoning_content === 'string') { reasoningContent = delta.reasoning_content; - // ollama uses 'reasoning' + // ollama uses 'reasoning' } else if (delta && 'reasoning' in delta && typeof delta.reasoning === 'string') { reasoningContent = delta.reasoning; } @@ -336,10 +337,20 @@ export class SessionManager { } } - // Fix tool calls with empty arguments + // Fix tool calls with empty arguments or names for (const toolCall of accumulatedToolCalls) { if (toolCall.type === 'function') { toolCall.function.arguments = toolCall.function.arguments || '{}'; + + // Log and handle empty tool names (malformed AI response) + if (!toolCall.function.name || toolCall.function.name.trim() === '') { + console.error('Malformed tool call detected: missing function name', { + projectId, + providerModel, + toolCallId: toolCall.id, + toolCall: JSON.stringify(toolCall), + }); + } } } @@ -366,10 +377,28 @@ export class SessionManager { const functionToolCall = toolCall as OpenAI.Chat.Completions.ChatCompletionMessageFunctionToolCall; const toolName = functionToolCall.function.name; + + // Check for missing/empty tool name (malformed AI response) + if (!toolName || toolName.trim() === '') { + // Add detailed technical error to tool message for debugging + await this.addToolMessage( + projectId, + functionToolCall.id, + `Error: Malformed tool call received from AI provider (missing function name). This is likely a provider-specific issue. Tool call ID: ${functionToolCall.id}` + ); + + // Throw custom error to trigger Quilly UI with user-friendly message + throw new MalformedToolCallError( + 'The AI provider sent an incomplete response. This may be due to a network interruption or provider issue.', + functionToolCall.id, + providerModel + ); + } + const tool = session.customTools[toolName]; if (!tool) { - await this.addToolMessage(projectId, functionToolCall.id, `Tool ${toolName} not found`); + await this.addToolMessage(projectId, functionToolCall.id, `Tool "${toolName}" not found`); continue; } diff --git a/src/lib/errors/MalformedToolCallError.ts b/src/lib/errors/MalformedToolCallError.ts new file mode 100644 index 00000000..d2ef93b7 --- /dev/null +++ b/src/lib/errors/MalformedToolCallError.ts @@ -0,0 +1,22 @@ +/** + * Error thrown when an AI provider sends a malformed tool call response + * (e.g., missing function name in the streaming response) + */ +export class MalformedToolCallError extends Error { + public readonly code = 'malformed_tool_call' as const; + public readonly toolCallId: string; + public readonly providerModel: string; + + constructor(message: string, toolCallId: string, providerModel: string) { + super(message); + this.name = 'MalformedToolCallError'; + this.toolCallId = toolCallId; + this.providerModel = providerModel; + + // Maintains proper stack trace for where our error was thrown (only available on V8) + if (Error.captureStackTrace) { + Error.captureStackTrace(this, MalformedToolCallError); + } + } +} +