fix(core): merge consecutive user roles in chat history and update e2e tests

This commit is contained in:
Sehoon Shon
2026-03-31 13:34:56 -04:00
parent ee092dc172
commit 3658d0ebc7
3 changed files with 22 additions and 13 deletions
+1 -1
View File
@@ -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 () => {
+20 -7
View File
@@ -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* (
+1 -5
View File
@@ -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',