feat(core): Add infinite loop protection to client (#2793)

This commit is contained in:
Allen Hutchison
2025-07-01 16:09:21 -07:00
committed by GitHub
parent 3a995305c0
commit e94decea39
2 changed files with 182 additions and 2 deletions

View File

@@ -219,7 +219,9 @@ export class GeminiClient {
signal: AbortSignal,
turns: number = this.MAX_TURNS,
): AsyncGenerator<ServerGeminiStreamEvent, Turn> {
if (!turns) {
// Ensure turns never exceeds MAX_TURNS to prevent infinite loops
const boundedTurns = Math.min(turns, this.MAX_TURNS);
if (!boundedTurns) {
return new Turn(this.getChat());
}
@@ -242,7 +244,7 @@ export class GeminiClient {
const nextRequest = [{ text: 'Please continue.' }];
// This recursive call's events will be yielded out, but the final
// turn object will be from the top-level call.
yield* this.sendMessageStream(nextRequest, signal, turns - 1);
yield* this.sendMessageStream(nextRequest, signal, boundedTurns - 1);
}
}
return turn;