diff --git a/integration-tests/file-system.test.ts b/integration-tests/file-system.test.ts index 64481068c2..522f4e4606 100644 --- a/integration-tests/file-system.test.ts +++ b/integration-tests/file-system.test.ts @@ -133,7 +133,7 @@ describe('file-system', () => { ).toBeTruthy(); const newFileContent = rig.readFile(fileName); - expect(newFileContent).toBe('hello'); + expect(newFileContent.trim()).toBe('hello'); }); it('should perform a read-then-write sequence', async () => { diff --git a/packages/core/src/core/geminiChat.ts b/packages/core/src/core/geminiChat.ts index b96baa1c6a..1829035d33 100644 --- a/packages/core/src/core/geminiChat.ts +++ b/packages/core/src/core/geminiChat.ts @@ -167,8 +167,16 @@ function extractCuratedHistory(comprehensiveHistory: Content[]): Content[] { let i = 0; while (i < length) { if (comprehensiveHistory[i].role === 'user') { - curatedHistory.push(comprehensiveHistory[i]); + const userMessage: Content = { + role: 'user', + parts: [...(comprehensiveHistory[i].parts || [])], + }; i++; + while (i < length && comprehensiveHistory[i].role === 'user') { + userMessage.parts!.push(...(comprehensiveHistory[i].parts || [])); + i++; + } + curatedHistory.push(userMessage); } else { const modelOutput: Content[] = []; let isValid = true; @@ -186,11 +194,6 @@ function extractCuratedHistory(comprehensiveHistory: Content[]): Content[] { } return curatedHistory; } - -/** - * Custom error to signal that a stream completed with invalid content, - * which should trigger a retry. - */ export class InvalidStreamError extends Error { readonly type: | 'NO_FINISH_REASON' @@ -342,7 +345,17 @@ export class GeminiChat { } // Add user content to history ONCE before any attempts. - this.history.push(userContent); + if ( + this.history.length > 0 && + this.history[this.history.length - 1].role === 'user' + ) { + const lastUser = this.history[this.history.length - 1]; + const lastUserParts = lastUser.parts || []; + const currentUserParts = userContent.parts || []; + lastUser.parts = [...lastUserParts, ...currentUserParts]; + } else { + this.history.push(userContent); + } const requestContents = this.getHistory(true); const streamWithRetries = async function* ( diff --git a/packages/core/src/core/turn.ts b/packages/core/src/core/turn.ts index 9c0e536c48..23dc5f21ec 100644 --- a/packages/core/src/core/turn.ts +++ b/packages/core/src/core/turn.ts @@ -5,7 +5,6 @@ */ import { - createUserContent, type PartListUnion, type GenerateContentResponse, type FunctionCall, @@ -375,10 +374,7 @@ export class Turn { throw error; } - const contextForReport = [ - ...this.chat.getHistory(/*curated*/ true), - createUserContent(req), - ]; + const contextForReport = [...this.chat.getHistory(/*curated*/ true)]; await reportError( error, 'Error when talking to Gemini API',