From 4a1678e3fe086a84e6adc6d4999217138d9d41fa Mon Sep 17 00:00:00 2001 From: luojiyin Date: Wed, 31 Dec 2025 11:19:39 +0800 Subject: [PATCH 1/9] feat: compactThreshold command Signed-off-by: luojiyin --- src/commands/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/commands/index.ts b/src/commands/index.ts index d9f9c94..4b33995 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -2,6 +2,7 @@ import React from 'react' import bug from './bug' import clear from './clear' import compact from './compact' +import compactThreshold from './compact-threshold' import config from './config' import cost from './cost' import ctxViz from './ctx-viz' @@ -92,6 +93,7 @@ const COMMANDS = memoize((): Command[] => [ agents, clear, compact, + compactThreshold, config, cost, doctor, From e207a19ceda5c01a021506f920f9aa14a4d3804e Mon Sep 17 00:00:00 2001 From: luojiyin Date: Wed, 31 Dec 2025 11:21:50 +0800 Subject: [PATCH 2/9] feat: compactThreshold to schema Signed-off-by: luojiyin --- src/core/config/schema.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/config/schema.ts b/src/core/config/schema.ts index ba0b28b..c593ec4 100644 --- a/src/core/config/schema.ts +++ b/src/core/config/schema.ts @@ -165,6 +165,7 @@ export type GlobalConfig = { } primaryProvider?: ProviderType maxTokens?: number + autoCompactThreshold?: number hasAcknowledgedCostThreshold?: boolean oauthAccount?: AccountInfo proxy?: string @@ -187,6 +188,7 @@ export const GLOBAL_CONFIG_KEYS = [ 'primaryProvider', 'preferredNotifChannel', 'maxTokens', + 'autoCompactThreshold', ] as const export type GlobalConfigKey = (typeof GLOBAL_CONFIG_KEYS)[number] @@ -214,4 +216,3 @@ export type ProjectMcpServerDefinitions = { mcpJsonPath: string mcprcPath: string } - From 30c16797bfef97495054aa93d8de939e5abad426 Mon Sep 17 00:00:00 2001 From: luojiyin Date: Wed, 31 Dec 2025 11:25:02 +0800 Subject: [PATCH 3/9] feat: return calculateAutoCompactThresholds Signed-off-by: luojiyin --- src/utils/session/autoCompactCore.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/utils/session/autoCompactCore.ts b/src/utils/session/autoCompactCore.ts index 8cfb77f..bc6c311 100644 --- a/src/utils/session/autoCompactCore.ts +++ b/src/utils/session/autoCompactCore.ts @@ -13,7 +13,6 @@ import { getModelManager } from '@utils/model' import { debug as debugLogger } from '@utils/log/debugLogger' import { logError } from '@utils/log' import { - AUTO_COMPACT_THRESHOLD_RATIO, calculateAutoCompactThresholds, } from './autoCompactThreshold' @@ -63,11 +62,7 @@ Focus on information essential for continuing the conversation effectively, incl async function calculateThresholds(tokenCount: number) { const contextLimit = await getMainConversationContextLimit() - return calculateAutoCompactThresholds( - tokenCount, - contextLimit, - AUTO_COMPACT_THRESHOLD_RATIO, - ) + return calculateAutoCompactThresholds(tokenCount, contextLimit) } async function shouldAutoCompact(messages: Message[]): Promise { From 2db29357892764b6e7d8568d0db758d710d19036 Mon Sep 17 00:00:00 2001 From: luojiyin Date: Wed, 31 Dec 2025 11:26:29 +0800 Subject: [PATCH 4/9] feat: getAutoCompactThresholdRatio function Signed-off-by: luojiyin --- src/utils/session/autoCompactThreshold.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/utils/session/autoCompactThreshold.ts b/src/utils/session/autoCompactThreshold.ts index 1107fda..4271f1a 100644 --- a/src/utils/session/autoCompactThreshold.ts +++ b/src/utils/session/autoCompactThreshold.ts @@ -1,9 +1,30 @@ +import { getGlobalConfig } from '@utils/config' + export const AUTO_COMPACT_THRESHOLD_RATIO = 0.9 +export function isValidAutoCompactThresholdRatio( + value: unknown, +): value is number { + return ( + typeof value === 'number' && + Number.isFinite(value) && + value > 0 && + value < 1 + ) +} + +export function getAutoCompactThresholdRatio(): number { + const config = getGlobalConfig() + if (isValidAutoCompactThresholdRatio(config.autoCompactThreshold)) { + return config.autoCompactThreshold + } + return AUTO_COMPACT_THRESHOLD_RATIO +} + export function calculateAutoCompactThresholds( tokenCount: number, contextLimit: number, - ratio: number = AUTO_COMPACT_THRESHOLD_RATIO, + ratio: number = getAutoCompactThresholdRatio(), ): { isAboveAutoCompactThreshold: boolean percentUsed: number From 5adeba6fe466189362a187c16c929c133f7c6712 Mon Sep 17 00:00:00 2001 From: luojiyin Date: Wed, 31 Dec 2025 11:27:37 +0800 Subject: [PATCH 5/9] feat: compact-threshould command Signed-off-by: luojiyin --- src/commands/compact-threshold.ts | 91 +++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/commands/compact-threshold.ts diff --git a/src/commands/compact-threshold.ts b/src/commands/compact-threshold.ts new file mode 100644 index 0000000..8904ec3 --- /dev/null +++ b/src/commands/compact-threshold.ts @@ -0,0 +1,91 @@ +import chalk from 'chalk' +import type { Command } from '@commands' +import { getGlobalConfig, saveGlobalConfig } from '@utils/config' +import { + AUTO_COMPACT_THRESHOLD_RATIO, + getAutoCompactThresholdRatio, + isValidAutoCompactThresholdRatio, +} from '@utils/session/autoCompactThreshold' + +const HELP_ARGS = new Set(['help', '-h', '--help', '?']) +const RESET_ARGS = new Set(['reset', 'default']) + +function parseThresholdInput(raw: string): number | null { + const trimmed = raw.trim() + if (!trimmed) return null + + let valueText = trimmed + let isPercent = false + + if (valueText.endsWith('%')) { + isPercent = true + valueText = valueText.slice(0, -1).trim() + } + + if (!valueText) return null + + const value = Number(valueText) + if (!Number.isFinite(value)) return null + + let ratio = value + // Treat bare values >1 as percentages (85 => 0.85) while still allowing ratios like 0.85. + if (isPercent || (value > 1 && value <= 100)) { + ratio = value / 100 + } + + return isValidAutoCompactThresholdRatio(ratio) ? ratio : null +} + +function formatRatio(ratio: number): string { + const percent = Math.round(ratio * 100) + return `${ratio} (${percent}%)` +} + +const compactThreshold = { + type: 'local', + name: 'compact-threshold', + description: 'View or set the auto-compact threshold ratio', + isEnabled: true, + isHidden: false, + argumentHint: '[ratio]', + userFacingName() { + return 'compact-threshold' + }, + async call(args) { + const raw = args.trim() + + if (!raw || HELP_ARGS.has(raw)) { + const configured = getGlobalConfig().autoCompactThreshold + const isCustom = isValidAutoCompactThresholdRatio(configured) + const ratio = getAutoCompactThresholdRatio() + const defaultNote = isCustom ? '' : ' (default)' + + return [ + `Auto-compact threshold: ${formatRatio(ratio)}${defaultNote}`, + 'Usage: /compact-threshold 0.85', + 'Tip: You can also use percentages, e.g. /compact-threshold 85%', + ].join('\n') + } + + if (RESET_ARGS.has(raw)) { + const nextConfig = { ...getGlobalConfig() } + delete nextConfig.autoCompactThreshold + saveGlobalConfig(nextConfig) + return `Auto-compact threshold reset to default (${AUTO_COMPACT_THRESHOLD_RATIO}).` + } + + const parsed = parseThresholdInput(raw) + if (!parsed) { + return [ + `Invalid threshold: ${chalk.bold(raw)}`, + 'Provide a ratio greater than 0 and less than 1 (e.g. 0.85 or 85%).', + ].join('\n') + } + + const config = getGlobalConfig() + saveGlobalConfig({ ...config, autoCompactThreshold: parsed }) + return `Auto-compact threshold set to ${formatRatio(parsed)}.` + }, +} satisfies Command + +export default compactThreshold From a1e4576f7692303920359e908e81d22f41cf2ffa Mon Sep 17 00:00:00 2001 From: luojiyin Date: Wed, 31 Dec 2025 11:39:48 +0800 Subject: [PATCH 6/9] docs: add /compact-threshold command to README MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- README.md | 1 + README.zh-CN.md | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index a2a9ace..b851946 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,7 @@ As long as you have an openai-like endpoint, it should work. - `/config` - Open configuration panel - `/cost` - Show token usage and costs - `/clear` - Clear conversation history +- `/compact-threshold` - View or set auto-compact threshold ratio (e.g. `/compact-threshold 0.85` or `/compact-threshold 85%`) - `/init` - Initialize project context ## Multi-Model Intelligent Collaboration diff --git a/README.zh-CN.md b/README.zh-CN.md index 45a7a27..dc12244 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -141,6 +141,7 @@ Kode 同时使用 `~/.kode` 目录(存放额外数据,如内存文件)和 - `/config` - 打开配置面板 - `/cost` - 显示 token 使用量和成本 - `/clear` - 清除对话历史 +- `/compact-threshold` - 查看或设置自动压缩阈值比例(如 `/compact-threshold 0.85` 或 `/compact-threshold 85%`) - `/init` - 初始化项目上下文 ## 多模型智能协同 From 5e651c191464df5794d18a09f270ca9d25776ea5 Mon Sep 17 00:00:00 2001 From: luojiyin Date: Wed, 31 Dec 2025 11:43:26 +0800 Subject: [PATCH 7/9] fix: change default auto-compact threshold to 0.92 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- src/utils/session/autoCompactThreshold.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/session/autoCompactThreshold.ts b/src/utils/session/autoCompactThreshold.ts index 4271f1a..b8282e5 100644 --- a/src/utils/session/autoCompactThreshold.ts +++ b/src/utils/session/autoCompactThreshold.ts @@ -1,6 +1,6 @@ import { getGlobalConfig } from '@utils/config' -export const AUTO_COMPACT_THRESHOLD_RATIO = 0.9 +export const AUTO_COMPACT_THRESHOLD_RATIO = 0.92 export function isValidAutoCompactThresholdRatio( value: unknown, From 62bb5c41dbbdde958ae72e2be78caaffc428acba Mon Sep 17 00:00:00 2001 From: luojiyin Date: Wed, 31 Dec 2025 11:46:15 +0800 Subject: [PATCH 8/9] fix: revert default auto-compact threshold to 0.9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Keep consistent with upstream. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- src/utils/session/autoCompactThreshold.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/session/autoCompactThreshold.ts b/src/utils/session/autoCompactThreshold.ts index b8282e5..4271f1a 100644 --- a/src/utils/session/autoCompactThreshold.ts +++ b/src/utils/session/autoCompactThreshold.ts @@ -1,6 +1,6 @@ import { getGlobalConfig } from '@utils/config' -export const AUTO_COMPACT_THRESHOLD_RATIO = 0.92 +export const AUTO_COMPACT_THRESHOLD_RATIO = 0.9 export function isValidAutoCompactThresholdRatio( value: unknown, From 8e989ff1a8686592f6e310d147842ae33420295b Mon Sep 17 00:00:00 2001 From: luojiyin Date: Wed, 31 Dec 2025 11:51:32 +0800 Subject: [PATCH 9/9] docs: add threshold reference table to README MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Help users choose appropriate threshold based on model context size. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- README.md | 7 +++++++ README.zh-CN.md | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/README.md b/README.md index b851946..dbdfaac 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,13 @@ As long as you have an openai-like endpoint, it should work. - `/cost` - Show token usage and costs - `/clear` - Clear conversation history - `/compact-threshold` - View or set auto-compact threshold ratio (e.g. `/compact-threshold 0.85` or `/compact-threshold 85%`) + + | Value | Effect | Use Case | + |-------|--------|----------| + | 0.80 | Compress earlier, more conservative | Small context models (e.g. deepseek 131k) | + | 0.85 | Balanced | Medium context models | + | 0.90 | Default | Large context models (e.g. Claude 200k) | + - `/init` - Initialize project context ## Multi-Model Intelligent Collaboration diff --git a/README.zh-CN.md b/README.zh-CN.md index dc12244..33111d4 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -142,6 +142,13 @@ Kode 同时使用 `~/.kode` 目录(存放额外数据,如内存文件)和 - `/cost` - 显示 token 使用量和成本 - `/clear` - 清除对话历史 - `/compact-threshold` - 查看或设置自动压缩阈值比例(如 `/compact-threshold 0.85` 或 `/compact-threshold 85%`) + + | 值 | 效果 | 适用场景 | + |----|------|----------| + | 0.80 | 更早压缩,更保守 | 小上下文模型(如 deepseek 131k) | + | 0.85 | 平衡 | 中等上下文模型 | + | 0.90 | 默认 | 大上下文模型(如 Claude 200k) | + - `/init` - 初始化项目上下文 ## 多模型智能协同