/** * Layer 2: Conversation Summarization. * * Replaces older conversation history with an LLM-generated summary * while preserving a tail of recent turns. Uses the primary model * for summarization quality (the conversation history is structurally * complex with interleaved tool calls and multi-turn reasoning). * * Fires at 70% of context window (configurable). Emits lifecycle * events (onBeforeCompaction, onPostCompaction) for consumer * coordination (e.g., observational memory flush). * * References: * - compaction-strategy.md (Layer 2: Conversation Summarization) * - phase-5-compaction.md (5.3) */ import type { AgentMessage } from '../context-manager.js'; import type { CompactionConfig, CompactionResult, CompactionTarget } from '../types.js'; export declare const COMPACTION_DEFAULTS: CompactionConfig; /** * Extract the content from the LLM's compaction output. * The prompt asks for (scratchpad) then (the actual summary). * We strip the analysis and keep only the summary content. * If no tags are found, return the full output (the model may * have skipped the tags but still produced useful content). */ export declare function extractSummaryContent(raw: string): string; /** * Partition conversation history into compaction target and preserved tail. * * @param history - The full conversation history (post-slot region) * @param preserveRecentTurns - Number of recent turns to preserve * @returns [target, preserved] where target is summarized and preserved is kept verbatim */ export declare function partitionHistory(history: AgentMessage[], preserveRecentTurns: number): [AgentMessage[], AgentMessage[]]; /** * Build the compaction summary message wrapping it in XML tags. * * @param summary - The LLM-generated summary text * @param turnsCompacted - Number of turns that were summarized * @returns A user-role message containing the tagged summary */ export declare function buildSummaryMessage(summary: string, turnsCompacted: number): AgentMessage; /** * Format conversation turns for the summarization prompt. * Extracts text content and labels each turn with role. */ export declare function formatTurnsForSummarization(turns: AgentMessage[]): string; /** * Type for the LLM completion function. * Matches the signature of CortexAgent.directComplete(). */ export type CompleteFn = (context: { systemPrompt: string; messages: unknown[]; }) => Promise; /** * Type for the consumer's onBeforeCompaction handler. */ export type BeforeCompactionHandler = (target: CompactionTarget) => Promise; /** * Type for the consumer's onPostCompaction handler. */ export type PostCompactionHandler = (result: CompactionResult) => void; /** * Type for the consumer's onCompactionError handler. */ export type CompactionErrorHandler = (error: Error) => void; /** * Run Layer 2 conversation summarization. * * Steps: * 1. Partition history into target and preserved tail * 2. Emit onBeforeCompaction (awaited) * 3. Generate summary via LLM * 4. Build new history: [summary message] + [preserved tail] * 5. Emit onPostCompaction * * @param history - Current conversation history (post-slot region) * @param config - Compaction configuration * @param complete - LLM completion function * @param handlers - Consumer lifecycle handlers * @returns The new conversation history and compaction result */ export declare function runCompaction(history: AgentMessage[], config: CompactionConfig, complete: CompleteFn, handlers?: { onBeforeCompaction?: BeforeCompactionHandler[]; onPostCompaction?: PostCompactionHandler[]; onCompactionError?: CompactionErrorHandler[]; }, /** Actual full-context token count (includes system prompt, slots, tools). When provided, used as tokensBefore instead of text-only heuristic. */ actualContextTokens?: number): Promise<{ newHistory: AgentMessage[]; result: CompactionResult; }>; /** * Check if compaction should trigger based on token count and threshold. */ export declare function shouldCompact(currentTokens: number, contextWindow: number, threshold: number): boolean; //# sourceMappingURL=compaction.d.ts.map