Throw an error if the AI service return an empty assistant message

This commit is contained in:
Alex Gleason committed 2025-10-07 21:13:04 -05:00
1 parent ca45f62585
commit 496ab8722a
7 files changed
+249 -3

No files matched your search

+16
View File
@@ -9,6 +9,8 @@ import type { AIProvider } from '@/contexts/AISettingsContext';
import { makeSystemPrompt } from './system';
import { NostrMetadata } from '@nostrify/nostrify';
import { MalformedToolCallError } from './errors/MalformedToolCallError';
import { EmptyMessageError } from './errors/EmptyMessageError';
import { isEmptyMessage } from './isEmptyMessage';
export type AIMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam | {
role: 'assistant';
@@ -362,6 +364,20 @@ export class SessionManager {
...(accumulatedToolCalls.length > 0 && { tool_calls: accumulatedToolCalls })
};
// Check if the assistant message is empty (no content, reasoning, or tool calls)
if (isEmptyMessage(assistantMessage)) {
console.error('Empty assistant message detected', {
projectId,
providerModel,
finishReason,
});
throw new EmptyMessageError(
'The AI generated an empty response. This may be due to a provider issue or model configuration problem.',
providerModel,
);
}
// Add final message but keep streaming message until finally block
await this.addMessage(projectId, assistantMessage);