When an error occurs stop processing.

This commit is contained in:
Taylor Mullen 2025-05-12 00:04:21 -07:00 committed by N. Taylor Mullen
parent 8537aabba4
commit df74594b8f
2 changed files with 9 additions and 3 deletions

View File

@ -333,8 +333,6 @@ export const useGeminiStream = (
{ type: 'error', text: `[API Error: ${event.value.message}]` }, { type: 'error', text: `[API Error: ${event.value.message}]` },
userMessageTimestamp, userMessageTimestamp,
); );
setStreamingState(StreamingState.Idle);
// Allow stream to end naturally
} }
} // End stream loop } // End stream loop

View File

@ -16,7 +16,7 @@ import {
} from '@google/genai'; } from '@google/genai';
import process from 'node:process'; import process from 'node:process';
import { getFolderStructure } from '../utils/getFolderStructure.js'; import { getFolderStructure } from '../utils/getFolderStructure.js';
import { Turn, ServerGeminiStreamEvent } from './turn.js'; import { Turn, ServerGeminiStreamEvent, GeminiEventType } from './turn.js';
import { Config } from '../config/config.js'; import { Config } from '../config/config.js';
import { getCoreSystemPrompt } from './prompts.js'; import { getCoreSystemPrompt } from './prompts.js';
import { ReadManyFilesTool } from '../tools/read-many-files.js'; import { ReadManyFilesTool } from '../tools/read-many-files.js';
@ -156,7 +156,10 @@ export class GeminiClient {
turns++; turns++;
const turn = new Turn(chat, availableTools); const turn = new Turn(chat, availableTools);
const resultStream = turn.run(request, signal); const resultStream = turn.run(request, signal);
let seenError = false;
for await (const event of resultStream) { for await (const event of resultStream) {
seenError =
seenError === false ? false : event.type === GeminiEventType.Error;
yield event; yield event;
} }
@ -176,6 +179,11 @@ export class GeminiClient {
} }
} }
request = fnResponses; request = fnResponses;
if (seenError) {
// We saw an error, lets stop processing to prevent unexpected consequences.
break;
}
} }
if (turns >= this.MAX_TURNS) { if (turns >= this.MAX_TURNS) {
console.warn('sendMessageStream: Reached maximum tool call turns limit.'); console.warn('sendMessageStream: Reached maximum tool call turns limit.');