mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-28 22:14:52 -07:00
fix(core): update token count and telemetry on /chat resume history load (#16279)
Co-authored-by: Tommaso Sciortino <sciortino@gmail.com>
This commit is contained in:
@@ -303,6 +303,17 @@ describe('Gemini Client (client.ts)', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('setHistory', () => {
|
||||||
|
it('should update telemetry token count when history is set', () => {
|
||||||
|
const history: Content[] = [
|
||||||
|
{ role: 'user', parts: [{ text: 'some message' }] },
|
||||||
|
];
|
||||||
|
client.setHistory(history);
|
||||||
|
|
||||||
|
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('resumeChat', () => {
|
describe('resumeChat', () => {
|
||||||
it('should update telemetry token count when a chat is resumed', async () => {
|
it('should update telemetry token count when a chat is resumed', async () => {
|
||||||
const history: Content[] = [
|
const history: Content[] = [
|
||||||
|
|||||||
@@ -246,6 +246,7 @@ export class GeminiClient {
|
|||||||
|
|
||||||
setHistory(history: Content[]) {
|
setHistory(history: Content[]) {
|
||||||
this.getChat().setHistory(history);
|
this.getChat().setHistory(history);
|
||||||
|
this.updateTelemetryTokenCount();
|
||||||
this.forceFullIdeContext = true;
|
this.forceFullIdeContext = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -219,6 +219,37 @@ describe('GeminiChat', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('setHistory', () => {
|
||||||
|
it('should recalculate lastPromptTokenCount when history is updated', () => {
|
||||||
|
const initialHistory: Content[] = [
|
||||||
|
{ role: 'user', parts: [{ text: 'Hello' }] },
|
||||||
|
];
|
||||||
|
const chatWithHistory = new GeminiChat(
|
||||||
|
mockConfig,
|
||||||
|
'',
|
||||||
|
[],
|
||||||
|
initialHistory,
|
||||||
|
);
|
||||||
|
const initialCount = chatWithHistory.getLastPromptTokenCount();
|
||||||
|
|
||||||
|
const newHistory: Content[] = [
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
parts: [
|
||||||
|
{
|
||||||
|
text: 'This is a much longer history item that should result in more tokens than just hello.',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
chatWithHistory.setHistory(newHistory);
|
||||||
|
|
||||||
|
expect(chatWithHistory.getLastPromptTokenCount()).toBeGreaterThan(
|
||||||
|
initialCount,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('sendMessageStream', () => {
|
describe('sendMessageStream', () => {
|
||||||
it('should succeed if a tool call is followed by an empty part', async () => {
|
it('should succeed if a tool call is followed by an empty part', async () => {
|
||||||
// 1. Mock a stream that contains a tool call, then an invalid (empty) part.
|
// 1. Mock a stream that contains a tool call, then an invalid (empty) part.
|
||||||
|
|||||||
@@ -676,6 +676,9 @@ export class GeminiChat {
|
|||||||
|
|
||||||
setHistory(history: Content[]): void {
|
setHistory(history: Content[]): void {
|
||||||
this.history = history;
|
this.history = history;
|
||||||
|
this.lastPromptTokenCount = estimateTokenCountSync(
|
||||||
|
this.history.flatMap((c) => c.parts || []),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
stripThoughtsFromHistory(): void {
|
stripThoughtsFromHistory(): void {
|
||||||
|
|||||||
Reference in New Issue
Block a user