diff --git a/src/components/Quilly.test.tsx b/src/components/Quilly.test.tsx
new file mode 100644
index 00000000..59d388e1
--- /dev/null
+++ b/src/components/Quilly.test.tsx
@@ -0,0 +1,80 @@
+import { render, screen } from '@testing-library/react';
+import { describe, it, expect, vi } from 'vitest';
+import { Quilly } from './Quilly';
+import { MalformedToolCallError } from '@/lib/errors/MalformedToolCallError';
+import { ProjectPreviewConsoleError } from '@/lib/consoleMessages';
+
+// Mock all required hooks and components
+vi.mock('@/hooks/useAISettings', () => ({
+ useAISettings: () => ({
+ settings: {
+ providers: []
+ }
+ })
+}));
+
+vi.mock('@/hooks/useAICredits', () => ({
+ useAICredits: () => ({
+ data: null
+ })
+}));
+
+vi.mock('react-router-dom', () => ({
+ useNavigate: () => vi.fn()
+}));
+
+// Mock CreditsDialog to avoid NostrProvider dependency
+vi.mock('./CreditsDialog', () => ({
+ CreditsDialog: () => null
+}));
+
+describe('Quilly', () => {
+ const defaultProps = {
+ error: new Error('Generic error'),
+ onDismiss: vi.fn(),
+ onNewChat: vi.fn(),
+ onOpenModelSelector: vi.fn(),
+ providerModel: 'test/model'
+ };
+
+ it('renders generic error message', () => {
+ render();
+
+ expect(screen.getByText(/AI service error: Generic error/)).toBeInTheDocument();
+ });
+
+ it('renders correctly for malformed tool call errors', () => {
+ const error = new MalformedToolCallError(
+ 'The AI provider sent an incomplete response.',
+ 'call_abc123',
+ 'test/model'
+ );
+
+ render(
+
+ );
+
+ expect(screen.getByText(/The AI sent an incomplete response/)).toBeInTheDocument();
+ expect(screen.getByText(/Change model/)).toBeInTheDocument();
+ });
+
+ it('renders correctly for console errors', () => {
+ const error = new ProjectPreviewConsoleError(
+ 'Console error',
+ []
+ );
+
+ render(
+
+ );
+
+ expect(screen.getByText(/I noticed some console errors/)).toBeInTheDocument();
+ expect(screen.getByText(/Help fix errors/)).toBeInTheDocument();
+ });
+});
diff --git a/src/components/Quilly.tsx b/src/components/Quilly.tsx
index adb087b7..a951d6ef 100644
--- a/src/components/Quilly.tsx
+++ b/src/components/Quilly.tsx
@@ -9,6 +9,7 @@ import { useAICredits } from '@/hooks/useAICredits';
import { CreditsDialog } from './CreditsDialog';
import { ProjectPreviewConsoleError } from '@/lib/consoleMessages';
import { useState } from 'react';
+import { MalformedToolCallError } from '@/lib/errors/MalformedToolCallError';
export interface QuillyProps {
error: Error;
@@ -58,6 +59,16 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq
};
}
+ if (error instanceof MalformedToolCallError) {
+ return {
+ message: 'The AI sent an incomplete response, possibly due to a network issue or provider problem. Try sending your message again, or switch to a different model if this persists.',
+ action: {
+ label: 'Change model',
+ onClick: onOpenModelSelector,
+ },
+ };
+ }
+
// Handle OpenAI API errors with specific error codes
if (error instanceof OpenAI.APIError) {
switch (error.code) {
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..1bd79d89
--- /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);
+ }
+ }
+}
+