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:
Pyush Sinha
2026-01-21 13:24:33 -08:00
committed by GitHub
parent c266b529ae
commit addecf2c74
4 changed files with 46 additions and 0 deletions

View File

@@ -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', () => {
it('should update telemetry token count when a chat is resumed', async () => {
const history: Content[] = [

View File

@@ -246,6 +246,7 @@ export class GeminiClient {
setHistory(history: Content[]) {
this.getChat().setHistory(history);
this.updateTelemetryTokenCount();
this.forceFullIdeContext = true;
}

View File

@@ -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', () => {
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.

View File

@@ -676,6 +676,9 @@ export class GeminiChat {
setHistory(history: Content[]): void {
this.history = history;
this.lastPromptTokenCount = estimateTokenCountSync(
this.history.flatMap((c) => c.parts || []),
);
}
stripThoughtsFromHistory(): void {