diff options
| author | Jacob MacDonald <[email protected]> | 2025-08-07 07:34:40 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-07 14:34:40 +0000 |
| commit | 6ae75c9f32a968efa50857a8f24b958a58a84fd6 (patch) | |
| tree | 6daccaf6fba4ac462a19ed75e198c02840f6a1a3 /packages/core/src | |
| parent | 36750ca49b1b2fa43a3d7904416b876203a1850f (diff) | |
Add a context percentage threshold setting for auto compression (#5721)
Diffstat (limited to 'packages/core/src')
| -rw-r--r-- | packages/core/src/config/config.ts | 11 | ||||
| -rw-r--r-- | packages/core/src/core/client.test.ts | 10 | ||||
| -rw-r--r-- | packages/core/src/core/client.ts | 14 |
3 files changed, 28 insertions, 7 deletions
diff --git a/packages/core/src/config/config.ts b/packages/core/src/config/config.ts index 005573da..4848bfb6 100644 --- a/packages/core/src/config/config.ts +++ b/packages/core/src/config/config.ts @@ -69,6 +69,10 @@ export interface BugCommandSettings { urlTemplate: string; } +export interface ChatCompressionSettings { + contextPercentageThreshold?: number; +} + export interface SummarizeToolOutputSettings { tokenBudget?: number; } @@ -191,6 +195,7 @@ export interface ConfigParameters { folderTrustFeature?: boolean; ideMode?: boolean; loadMemoryFromIncludeDirectories?: boolean; + chatCompression?: ChatCompressionSettings; } export class Config { @@ -252,6 +257,7 @@ export class Config { | undefined; private readonly experimentalAcp: boolean = false; private readonly loadMemoryFromIncludeDirectories: boolean = false; + private readonly chatCompression: ChatCompressionSettings | undefined; constructor(params: ConfigParameters) { this.sessionId = params.sessionId; @@ -316,6 +322,7 @@ export class Config { } this.loadMemoryFromIncludeDirectories = params.loadMemoryFromIncludeDirectories ?? false; + this.chatCompression = params.chatCompression; if (params.contextFileName) { setGeminiMdFilename(params.contextFileName); @@ -667,6 +674,10 @@ export class Config { return this.ideClient; } + getChatCompression(): ChatCompressionSettings | undefined { + return this.chatCompression; + } + async getGitService(): Promise<GitService> { if (!this.gitService) { this.gitService = new GitService(this.targetDir); diff --git a/packages/core/src/core/client.test.ts b/packages/core/src/core/client.test.ts index 1e39758a..ff901a8b 100644 --- a/packages/core/src/core/client.test.ts +++ b/packages/core/src/core/client.test.ts @@ -206,6 +206,7 @@ describe('Gemini Client (client.ts)', () => { }), getGeminiClient: vi.fn(), setFallbackMode: vi.fn(), + getChatCompression: vi.fn().mockReturnValue(undefined), }; const MockedConfig = vi.mocked(Config, true); MockedConfig.mockImplementation( @@ -531,14 +532,19 @@ describe('Gemini Client (client.ts)', () => { expect(newChat).toBe(initialChat); }); - it('should trigger summarization if token count is at threshold', async () => { + it('should trigger summarization if token count is at threshold with contextPercentageThreshold setting', async () => { const MOCKED_TOKEN_LIMIT = 1000; + const MOCKED_CONTEXT_PERCENTAGE_THRESHOLD = 0.5; vi.mocked(tokenLimit).mockReturnValue(MOCKED_TOKEN_LIMIT); + vi.spyOn(client['config'], 'getChatCompression').mockReturnValue({ + contextPercentageThreshold: MOCKED_CONTEXT_PERCENTAGE_THRESHOLD, + }); mockGetHistory.mockReturnValue([ { role: 'user', parts: [{ text: '...history...' }] }, ]); - const originalTokenCount = 1000 * 0.7; + const originalTokenCount = + MOCKED_TOKEN_LIMIT * MOCKED_CONTEXT_PERCENTAGE_THRESHOLD; const newTokenCount = 100; mockCountTokens diff --git a/packages/core/src/core/client.ts b/packages/core/src/core/client.ts index a16a72cc..13e60039 100644 --- a/packages/core/src/core/client.ts +++ b/packages/core/src/core/client.ts @@ -596,12 +596,16 @@ export class GeminiClient { return null; } + const contextPercentageThreshold = + this.config.getChatCompression()?.contextPercentageThreshold; + // Don't compress if not forced and we are under the limit. - if ( - !force && - originalTokenCount < this.COMPRESSION_TOKEN_THRESHOLD * tokenLimit(model) - ) { - return null; + if (!force) { + const threshold = + contextPercentageThreshold ?? this.COMPRESSION_TOKEN_THRESHOLD; + if (originalTokenCount < threshold * tokenLimit(model)) { + return null; + } } let compressBeforeIndex = findIndexAfterFraction( |
