define and surface an error for malformed tool responses

This commit is contained in:
sam committed 2025-10-03 16:18:32 +07:00
1 parent c48288f9e6
commit 8b5cf72356
2 files changed
+54 -3

No files matched your search

+32 -3
View File
@@ -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;
}
+22
View File
@@ -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);
}
}
}