mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-15 22:33:05 -07:00
fix(core): merge consecutive user roles in chat history and update e2e tests
This commit is contained in:
@@ -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 () => {
|
||||
|
||||
@@ -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* (
|
||||
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user