feat(tui): use configured compaction threshold for /context output

This commit is contained in:
William Valentin
2026-02-16 18:10:54 -08:00
parent 409ab04ca1
commit fc6a79ed90
7 changed files with 53 additions and 4 deletions
+3 -1
View File
@@ -50,6 +50,7 @@ export interface AppProps {
agent?: NativeAgent;
hookEngine?: HookEngine;
modelProviderConfigs?: Partial<Record<ModelProvider, ModelConfig>>;
contextThresholdPct?: number;
onExit?: () => void;
}
@@ -62,6 +63,7 @@ export function App({
agent,
hookEngine,
modelProviderConfigs,
contextThresholdPct,
onExit,
}: AppProps): React.ReactElement {
const { exit } = useApp();
@@ -247,7 +249,7 @@ export function App({
const modelName = modelRouter?.getLabel(tier) ?? model;
const window = getContextWindow(modelName);
const usagePct = window > 0 ? (estimated / window) * 100 : 0;
const thresholdPct = 80;
const thresholdPct = contextThresholdPct ?? 80;
const thresholdTokens = Math.floor((thresholdPct / 100) * window);
const remaining = Math.max(0, window - estimated);
const text = [
+2
View File
@@ -17,6 +17,7 @@ export interface FullscreenTuiConfig {
agent?: NativeAgent;
hookEngine?: HookEngine;
modelProviderConfigs?: Partial<Record<ModelProvider, ModelConfig>>;
contextThresholdPct?: number;
onExit?: () => void;
}
@@ -40,6 +41,7 @@ export async function startFullscreenTui(config: FullscreenTuiConfig): Promise<v
agent: config.agent,
hookEngine: config.hookEngine,
modelProviderConfigs: config.modelProviderConfigs,
contextThresholdPct: config.contextThresholdPct,
onExit: config.onExit,
}),
);
+39
View File
@@ -29,6 +29,7 @@ function asAgent(value: unknown): NativeAgent {
function minimalTuiPrivates(value: MinimalTui): {
handleBackendCommand: (provider: string) => Promise<void>;
handleModelCommand: (tier: string, providerModel?: string) => void;
handleContextCommand: () => void;
handleEscapeAction: () => boolean;
prompt: (text: string) => Promise<string>;
rl: {
@@ -43,6 +44,7 @@ function minimalTuiPrivates(value: MinimalTui): {
return value as unknown as {
handleBackendCommand: (provider: string) => Promise<void>;
handleModelCommand: (tier: string, providerModel?: string) => void;
handleContextCommand: () => void;
handleEscapeAction: () => boolean;
prompt: (text: string) => Promise<string>;
rl: {
@@ -174,6 +176,43 @@ describe('MinimalTui backend command', () => {
expect(mockAgent.setModelTier).toHaveBeenCalledWith('local');
});
it('uses configured compaction threshold in /context output', () => {
const mockSession = {
id: 'test',
getHistory: () => [{ role: 'user', content: 'x'.repeat(400) }],
addMessage: vi.fn(),
clear: vi.fn(),
replaceHistory: vi.fn(),
};
const mockRouter: TuiRouterStub = {
getTier: () => 'default' as const,
getAvailableTiers: () => ['default'],
setTier: vi.fn(() => true),
getLabel: () => 'gpt-4o',
getLocalProviderName: () => 'ollama',
setLocalClient: vi.fn(),
chat: vi.fn(),
getClient: vi.fn(),
};
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
try {
const tui = new MinimalTui({
session: asSession(mockSession),
modelClient: asRouter(mockRouter),
modelRouter: asRouter(mockRouter),
systemPrompt: 'test',
contextThresholdPct: 67,
});
minimalTuiPrivates(tui).handleContextCommand();
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('compaction threshold: 67%'));
} finally {
logSpy.mockRestore();
}
});
it('reuses configured provider credentials for /model <tier> <provider/model>', () => {
const prevOpenRouterKey = process.env.OPENROUTER_API_KEY;
delete process.env.OPENROUTER_API_KEY;
+2 -1
View File
@@ -68,6 +68,7 @@ export interface MinimalTuiConfig {
currentLocalProvider?: string;
pairingManager?: PairingManager;
hookEngine?: HookEngine;
contextThresholdPct?: number;
}
export class MinimalTui {
@@ -394,7 +395,7 @@ export class MinimalTui {
const modelName = this.config.modelRouter?.getLabel(tier) ?? 'unknown';
const window = getContextWindow(modelName);
const usagePct = window > 0 ? (estimated / window) * 100 : 0;
const thresholdPct = 80;
const thresholdPct = this.config.contextThresholdPct ?? 80;
const thresholdTokens = Math.floor((thresholdPct / 100) * window);
const remaining = Math.max(0, window - estimated);