import type { ONIModel, ONIModelMessage } from "../models/types.js"; export interface CompactionBudgetSnapshot { estimatedTokens: number; percentUsed: number; threshold: number; maxTokens: number; } export interface CompactionCheck extends CompactionBudgetSnapshot { needed: boolean; } export interface CompactorConfig { /** Model used to generate the summary (typically a fast/cheap model) */ summaryModel: ONIModel; /** * Usage fraction at which compaction triggers. * Research: performance degrades non-linearly above 65%. * @default 0.68 */ threshold?: number; /** * Maximum token budget for the context window. * @default 200_000 */ maxTokens?: number; /** * Approximate characters per token for estimation. * @default 4 */ charsPerToken?: number; /** * Additional instructions included in the summarization prompt. */ compactInstructions?: string; } export declare class ContextCompactor { private readonly summaryModel; private readonly threshold; private readonly maxTokens; private readonly charsPerToken; private readonly compactInstructions?; private _lastSummary; /** Promise-chain lock: serializes concurrent calls to compact(). */ private _lockChain; constructor(config: CompactorConfig); /** * Estimate token count from total character length of all messages. * Uses ceiling to avoid underestimation. */ estimateTokens(messages: ONIModelMessage[]): number; /** * Returns true when estimated usage exceeds the compaction threshold. */ shouldCompact(messages: ONIModelMessage[]): boolean; /** * Check if compaction is needed and return details. */ checkCompaction(messages: ONIModelMessage[]): CompactionCheck; /** * Current usage as a fraction of maxTokens (0–1+). */ usageFraction(messages: ONIModelMessage[]): number; getThreshold(): number; getMaxTokens(): number; getBudgetSnapshot(messages: ONIModelMessage[]): CompactionBudgetSnapshot; /** * Returns the text of the last compaction summary, or null if compaction * has not been run in this session. */ getLastSummary(): string | null; /** * Extracts open threads / unresolved tasks from the last summary. * Looks for lines containing open-ended markers (TODO, WIP, pending, unresolved, * open, in progress) and returns them as a deduplicated string array. * Returns [] if no summary has been produced. */ getOpenThreads(): string[]; /** * Two-stage compaction: * 1. Clear old tool results (less lossy) * 2. Full summarization if still over threshold * * Returns the compacted message array. */ compact(messages: ONIModelMessage[], opts?: { skipInitialCheck?: boolean; }): Promise; private _runCompaction; /** * Remove tool-role messages from the older portion of the conversation, * keeping the most recent `keepRecent` messages intact. */ private clearOldToolResults; /** * Build a history text from all messages and ask the summary model * to produce a compact summary wrapped in tags. */ private summarize; /** * Fallback when the summary model fails — keep the most recent messages * that fit within the token budget and prepend a truncation notice. */ private fallbackTruncation; /** * Get the character length of a message's content. */ private contentLength; } //# sourceMappingURL=context-compactor.d.ts.map