/** * Context Manager * * Manages context window tracking, auto-compaction, and truncation * for multi-provider agent orchestration. Tracks token usage across * different providers and models with varying context limits. * * Key features: * - Per-provider/model context limit tracking * - Automatic compaction when approaching limits * - Smart truncation that preserves important content * - Context visualization for debugging * - Event emission for monitoring */ import type { Logger } from '../observability/logger.js'; import type { Message, ContentBlock } from '../types.js'; import type { ContextManagerConfig, ContextState, ContextBreakdown, ContextMessage, ContextContentBlock, ContextEventListener, TruncationOptions, TruncationResult } from './context-types.js'; /** * Manages context window tracking and optimization for multi-provider * agent orchestration. * * @example * ```typescript * const contextManager = new ContextManager({ autoCompactThreshold: 0.85 }, logger); * * // Check context state before making a request * const state = contextManager.getState('openai', 'gpt-4o', messages); * if (state.isBlocked) { * // Context too full, need to compact * messages = contextManager.compact(messages); * } * * // Get detailed breakdown for debugging * const breakdown = contextManager.getBreakdown(messages); * console.log(contextManager.visualize(state, breakdown)); * ``` */ export declare class ContextManager { private readonly config; private readonly logger; private readonly listeners; /** * Create a new ContextManager instance. * * @param config - Partial configuration (merged with defaults) * @param logger - Logger instance for observability */ constructor(config: Partial, logger: Logger); /** * Get the current context state for a provider/model combination. * * @param provider - Provider name (e.g., 'openai', 'anthropic') * @param model - Model name (e.g., 'gpt-4o', 'claude-3-opus') * @param messages - Current conversation messages * @returns Current context state with usage metrics */ getState(provider: string, model: string, messages: Message[]): ContextState; /** * Check if the context should be auto-compacted. * * @param state - Current context state * @returns True if compaction should be triggered */ shouldCompact(state: ContextState): boolean; /** * Check if the context is blocked (too full for new messages). * * @param state - Current context state * @returns True if context is blocked */ isBlocked(state: ContextState): boolean; /** * Estimate the number of tokens in a string. * * Uses a simple character-based approximation. For more accurate counting, * consider integrating tiktoken or a provider-specific tokenizer. * * @param text - Text to estimate tokens for * @param provider - Provider name for provider-specific ratios * @returns Estimated token count */ estimateTokens(text: string, provider?: string): number; /** * Estimate tokens for a content block. * * @param block - Content block to analyze * @param provider - Provider name for estimation * @returns Estimated token count */ estimateContentBlockTokens(block: ContentBlock | ContextContentBlock, provider?: string): number; /** * Estimate tokens for a message. * * @param message - Message to analyze * @param provider - Provider name for estimation * @returns Estimated token count */ estimateMessageTokens(message: Message | ContextMessage, provider?: string): number; /** * Estimate total tokens for an array of messages. * * @param messages - Messages to analyze * @param provider - Provider name for estimation * @returns Total estimated token count */ estimateMessagesTokens(messages: (Message | ContextMessage)[], provider?: string): number; /** * Get the context limit for a specific model. * * Looks up the model in the configured limits, falling back to * DEFAULT_MODEL_LIMITS, then to the default context limit. * * @param provider - Provider name * @param model - Model name * @returns Context limit in tokens */ getModelLimit(provider: string, model: string): number; /** * Truncate content to fit within a token limit. * * @param content - Content to truncate * @param maxTokens - Maximum tokens allowed * @param options - Truncation options * @returns Truncated content */ truncate(content: string, maxTokens: number, options?: Partial): string; /** * Truncate content with detailed result information. * * @param content - Content to truncate * @param maxTokens - Maximum tokens allowed * @param options - Truncation options * @returns Truncation result with metadata */ truncateWithResult(content: string, maxTokens: number, options?: Partial): TruncationResult; /** * Compact messages to reduce context usage. * * Strategy: * 1. Truncate long tool results * 2. Truncate long individual messages * 3. Summarize/drop older messages if still over limit * * @param messages - Messages to compact * @param targetReduction - Target reduction percentage (0-1, default 0.3) * @returns Compacted messages array */ compact(messages: Message[], targetReduction?: number): Message[]; /** * Get a detailed breakdown of context usage by category. * * @param messages - Messages to analyze * @param provider - Provider name for estimation * @returns Context breakdown by category */ getBreakdown(messages: (Message | ContextMessage)[], provider?: string): ContextBreakdown; /** * Generate a human-readable visualization of context usage. * * @param state - Current context state * @param breakdown - Context breakdown by category * @returns Formatted visualization string */ visualize(state: ContextState, breakdown: ContextBreakdown): string; /** * Add an event listener for context events. * * @param listener - Event listener function */ addEventListener(listener: ContextEventListener): void; /** * Remove an event listener. * * @param listener - Event listener to remove */ removeEventListener(listener: ContextEventListener): void; /** * Emit a context event to all listeners. */ private emit; /** * Truncate tool results that exceed the configured limit. */ private truncateToolResults; /** * Truncate individual messages that exceed the configured limit. */ private truncateLongMessages; /** * Check if a message is a system prompt. */ private isSystemPromptMessage; /** * Emit a compaction result event. */ private emitCompactionResult; /** * Format a percentage for display. */ private formatPercentage; /** * Generate a visual progress bar for context usage. */ private generateUsageBar; } /** * Create a ContextManager instance. * * @param config - Partial configuration (merged with defaults) * @param logger - Logger instance * @returns Configured ContextManager instance */ export declare function createContextManager(config: Partial, logger: Logger): ContextManager; export type { ContextManagerConfig, ContextState, ContextBreakdown, ContextMessage, ContextContentBlock, CompactionResult, CompactionStrategy, ContextEvent, ContextEventListener, TruncationOptions, TruncationResult, } from './context-types.js'; //# sourceMappingURL=context-manager.d.ts.map