1818import type { WorkspaceUsageState } from "@/browser/stores/WorkspaceStore" ;
1919import { getModelStats } from "@/common/utils/tokens/modelStats" ;
2020import { supports1MContext } from "@/common/utils/ai/models" ;
21+ import { DEFAULT_AUTO_COMPACTION_THRESHOLD } from "@/common/constants/ui" ;
2122
2223export interface AutoCompactionCheckResult {
2324 shouldShowWarning : boolean ;
2425 usagePercentage : number ;
2526 thresholdPercentage : number ;
27+ enabled : boolean ;
2628}
2729
28- // Auto-compaction threshold (0.7 = 70%)
29- // TODO: Make this configurable via settings
30- const AUTO_COMPACTION_THRESHOLD = 0.7 ;
31-
3230// Show warning this many percentage points before threshold
3331const WARNING_ADVANCE_PERCENT = 10 ;
3432
3533/**
3634 * Check if auto-compaction should trigger based on token usage
3735 *
3836 * @param usage - Current workspace usage state (from useWorkspaceUsage)
39- * @param model - Current model string
37+ * @param model - Current model string (optional - returns safe default if not provided)
4038 * @param use1M - Whether 1M context is enabled
39+ * @param enabled - Whether auto-compaction is enabled for this workspace
4140 * @param threshold - Usage percentage threshold (0.0-1.0, default 0.7 = 70%)
4241 * @param warningAdvancePercent - Show warning this many percentage points before threshold (default 10)
4342 * @returns Check result with warning flag and usage percentage
4443 */
4544export function shouldAutoCompact (
4645 usage : WorkspaceUsageState | undefined ,
47- model : string ,
46+ model : string | null | undefined ,
4847 use1M : boolean ,
49- threshold : number = AUTO_COMPACTION_THRESHOLD ,
48+ enabled = true ,
49+ threshold : number = DEFAULT_AUTO_COMPACTION_THRESHOLD ,
5050 warningAdvancePercent : number = WARNING_ADVANCE_PERCENT
5151) : AutoCompactionCheckResult {
5252 const thresholdPercentage = threshold * 100 ;
5353
54+ // Short-circuit if auto-compaction is disabled
55+ if ( ! enabled || ! model ) {
56+ return {
57+ shouldShowWarning : false ,
58+ usagePercentage : 0 ,
59+ thresholdPercentage,
60+ enabled : false ,
61+ } ;
62+ }
63+
5464 // No usage data yet - safe default (don't trigger on first message)
5565 if ( ! usage || usage . usageHistory . length === 0 ) {
5666 return {
5767 shouldShowWarning : false ,
5868 usagePercentage : 0 ,
5969 thresholdPercentage,
70+ enabled : true ,
6071 } ;
6172 }
6273
@@ -70,6 +81,7 @@ export function shouldAutoCompact(
7081 shouldShowWarning : false ,
7182 usagePercentage : 0 ,
7283 thresholdPercentage,
84+ enabled : true ,
7385 } ;
7486 }
7587
@@ -83,5 +95,6 @@ export function shouldAutoCompact(
8395 shouldShowWarning,
8496 usagePercentage,
8597 thresholdPercentage,
98+ enabled : true ,
8699 } ;
87100}
0 commit comments