From e705f45c5fd373c741626171a733b25bb9103436 Mon Sep 17 00:00:00 2001 From: Sandy Tao Date: Mon, 6 Oct 2025 16:32:35 -0700 Subject: [PATCH] fix(core): retain user message in history on stream failure (#10608) --- packages/core/src/core/geminiChat.test.ts | 8 ++++++-- packages/core/src/core/geminiChat.ts | 4 ---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/core/src/core/geminiChat.test.ts b/packages/core/src/core/geminiChat.test.ts index 3e31cb0365..32a6883689 100644 --- a/packages/core/src/core/geminiChat.test.ts +++ b/packages/core/src/core/geminiChat.test.ts @@ -899,9 +899,13 @@ describe('GeminiChat', () => { expect(mockLogContentRetry).toHaveBeenCalledTimes(1); expect(mockLogContentRetryFailure).toHaveBeenCalledTimes(1); - // History should be clean, as if the failed turn never happened. + // History should still contain the user message. const history = chat.getHistory(); - expect(history.length).toBe(0); + expect(history.length).toBe(1); + expect(history[0]).toEqual({ + role: 'user', + parts: [{ text: 'test' }], + }); }); describe('API error retry behavior', () => { diff --git a/packages/core/src/core/geminiChat.ts b/packages/core/src/core/geminiChat.ts index 0c70cd1d2f..01285fa526 100644 --- a/packages/core/src/core/geminiChat.ts +++ b/packages/core/src/core/geminiChat.ts @@ -327,10 +327,6 @@ export class GeminiChat { ), ); } - // If the stream fails, remove the user message that was added. - if (self.history[self.history.length - 1] === userContent) { - self.history.pop(); - } throw lastError; } } finally {