import { type LanguageModel, type ModelMessage } from 'ai'; /** * Automatic conversation-history compaction. * * Long-running sessions (weeks-long WhatsApp threads) grow unbounded; budget * truncation silently loses the early relationship. Compaction summarizes the * older history into one system note and keeps the recent tail verbatim, so * the model retains facts/decisions without paying the full token cost. * * Runs post-turn (off the user's latency path) when the estimated history * tokens exceed `triggerTokens`, and force-runs as the recovery step after a * provider context-overflow error (see `contextOverflow.ts`). */ export interface CompactionConfig { /** Summarizer model. Defaults to the active agent's controlModel/model. */ model?: LanguageModel; /** Estimated history tokens that trigger compaction. Default: 8000. */ triggerTokens?: number; /** Number of recent messages kept verbatim. Default: 12. */ keepRecentMessages?: number; /** Override the summarizer system prompt. */ summaryPrompt?: string; } export declare const DEFAULT_COMPACTION_TRIGGER_TOKENS = 8000; export declare const DEFAULT_COMPACTION_KEEP_RECENT = 12; export type CompactionResult = { compacted: true; /** Verbatim retained tail — never includes a compaction summary message. */ messages: ModelMessage[]; /** Summary text for the system-note channel (`addSystemNote`, lifetime `run`). */ summary: string; beforeTokens: number; afterTokens: number; summarizedCount: number; } | { compacted: false; reason: 'under-threshold' | 'too-few-messages' | 'summarizer-error'; beforeTokens: number; }; export declare function estimateMessagesTokens(messages: ModelMessage[]): number; export interface CompactMessagesOptions { messages: ModelMessage[]; model: LanguageModel; config: CompactionConfig; /** Skip the threshold check (overflow recovery). */ force?: boolean; abortSignal?: AbortSignal; /** Real last-turn prompt tokens when available (replaces chars/4 for threshold). */ lastPromptTokens?: number; /** Prior compaction summary — folded into the summarizer input so facts chain across rounds. */ priorSummary?: string; } /** * Pure compaction step: returns a new message list when compaction applied. * The kept tail always starts at a `user` message so assistant/tool-call * pairs are never split across the summary boundary. */ export declare function compactMessages(options: CompactMessagesOptions): Promise;