/** * Leaper Agent - Context Compressor * * ContextEngine implementation that uses an LLM to summarise the middle * portion of a conversation when the context window fills up. * Translated from Python agent/context_compressor.py. */ import type { ChatMessage, LLMProvider } from '../types.js'; import { ContextEngine } from './engine.js'; export interface CompressorConfig { /** Hard token limit of the target context window. */ maxTokens: number; /** Fraction of maxTokens at which compression triggers (default 0.75). */ thresholdPercent?: number; /** Number of leading messages shielded from compression (default 3). */ protectFirstN?: number; /** Number of trailing messages shielded from compression (default 6). */ protectLastN?: number; /** * Optional model override for summary generation. * Falls back to the model used by the provider at call time. */ summaryModel?: string; } export declare class ContextCompressor extends ContextEngine { readonly name = "context-compressor"; private readonly summaryModel; constructor(config: CompressorConfig); updateFromResponse(usage: { prompt_tokens: number; completion_tokens: number; total_tokens: number; }): void; /** * Returns true when the observed (or estimated) prompt token count exceeds * the configured threshold. */ shouldCompress(promptTokens?: number): boolean; /** * Pre-flight check: estimate tokens from the raw messages array and decide * whether to compress before the API call rather than after. */ shouldCompressPreflight(messages: ChatMessage[]): boolean; /** * Returns false when the middle slice (messages between the protected head * and tail) contains fewer than two messages — nothing useful to summarise. */ hasContentToCompress(messages: ChatMessage[]): boolean; /** * Compress the message history. * * Steps: * 1. Prune stale tool-result messages from the middle slice. * 2. Call the LLM to produce a prose summary of the pruned middle. * 3. Anti-thrash guard: reject the result if savings < MIN_SAVING_RATIO. * 4. Splice the summary back in between head and tail. * * The `provider` is required at call time (not stored in the constructor) so * callers can use whatever provider is active for the current session. */ compress(messages: ChatMessage[], currentTokens?: number, focusTopic?: string, provider?: LLMProvider): ChatMessage[]; /** * Async variant of compress() that calls the LLM to produce a real summary. * Callers that have access to a provider should prefer this method. */ compressAsync(messages: ChatMessage[], provider: LLMProvider, currentTokens?: number, focusTopic?: string): Promise; /** Extract the slice between the protected head and tail. */ private sliceMiddle; /** * Remove tool-result messages whose preceding tool-call message has already * been scrolled out of the context window (i.e. is not present in the slice). * Also removes dangling tool-call messages with no corresponding result. */ private pruneToolResults; /** Rough token estimate: characters / 4. */ private estimateTokens; /** * Call the LLM to summarise a slice of conversation messages. * * The prompt asks the model to produce a dense, factual summary of what * was discussed so the context can be truncated without losing important * information. */ private generateSummary; } //# sourceMappingURL=compressor.d.ts.map