import { T as TokenCount } from '../common.d-iR60eBef.js'; /** * Context truncation types and interfaces */ /** * Supported AI models for token counting */ type ModelType = 'gpt-4' | 'gpt-4-32k' | 'gpt-4-turbo' | 'gpt-4o' | 'gpt-3.5-turbo' | 'gpt-3.5-turbo-16k' | 'claude-3-opus' | 'claude-3-sonnet' | 'claude-3-haiku' | 'claude-3-5-sonnet' | 'claude-3-5-haiku'; /** * Message importance levels */ declare enum MessageImportance { SYSTEM = "system", CRITICAL = "critical", HIGH = "high", NORMAL = "normal", LOW = "low" } /** * Truncation strategy types */ type TruncationStrategyType = 'oldest-first' | 'least-relevant' | 'sliding-window' | 'importance-based' | 'custom'; /** * Message structure for context management */ interface ContextMessage { role: 'system' | 'user' | 'assistant' | 'function' | 'tool'; content: string | null; name?: string; function_call?: { name: string; arguments: string; }; tool_calls?: Array<{ id: string; type: 'function'; function: { name: string; arguments: string; }; }>; tool_call_id?: string; importance?: MessageImportance; metadata?: Record; embedding?: number[]; } /** * Token usage information */ interface TokenUsage { totalTokens: number; messageTokens: number; overheadTokens: number; remainingTokens: number; } /** * Context configuration */ interface ContextConfig { model: ModelType; maxTokens: number; reservedTokens?: number; truncationStrategy?: TruncationStrategyType; preserveSystemMessages?: boolean; preserveRecentCount?: number; slidingWindowConfig?: { keepFirst: number; keepLast: number; }; warningThreshold?: number; onWarning?: (usage: TokenUsage) => void; onTruncate?: (removed: ContextMessage[], remaining: ContextMessage[]) => void; customTruncationFn?: (messages: ContextMessage[], maxTokens: number) => ContextMessage[]; } /** * Truncation strategy interface */ interface TruncationStrategy { name: TruncationStrategyType; truncate(messages: ContextMessage[], maxTokens: number, currentTokens: number, config: ContextConfig): ContextMessage[]; } /** * Model token limits mapping */ declare const MODEL_TOKEN_LIMITS: Record; /** * Model encoding mapping for tiktoken */ declare const MODEL_ENCODING_MAP: Record; /** * Token counting utilities using tiktoken (Node.js) or fallback approximation (browser) */ /** * TokenCounter class for accurate token counting * Automatically uses tiktoken in Node.js or fallback in browsers */ declare class TokenCounter { private encoders; private tiktokenReady; private fallbackCounter; private initPromise; /** * Initialize tiktoken (lazy loading) */ private init; /** * Get or create encoder for a model (sync - uses fallback if tiktoken not ready) */ private getEncoderSync; /** * Count tokens in a string */ countStringTokens(text: string, model: ModelType): number; /** * Count tokens in a message with detailed breakdown */ countMessageTokens(message: ContextMessage, model: ModelType): TokenCount; /** * Count tokens for an array of messages */ countMessagesTokens(messages: ContextMessage[], model: ModelType): number; /** * Estimate tokens remaining after adding a message */ estimateRemainingTokens(messages: ContextMessage[], maxTokens: number, model: ModelType): number; /** * Check if adding a message would exceed token limit */ wouldExceedLimit(messages: ContextMessage[], newMessage: ContextMessage, maxTokens: number, model: ModelType): boolean; /** * Find the index where messages would exceed token limit */ findTokenLimitIndex(messages: ContextMessage[], maxTokens: number, model: ModelType, fromEnd?: boolean): number; /** * Free encoder resources */ dispose(): void; /** * Check if tiktoken is available */ isTiktokenAvailable(): boolean; /** * Preload tiktoken (optional, for better performance) * Call this early in Node.js environments to ensure tiktoken is ready */ preload(): Promise; } /** * Singleton instance for easy access */ declare const tokenCounter: TokenCounter; /** * Context Manager with automatic truncation */ /** * Context Manager for automatic context truncation */ declare class ContextManager { private config; private counter; private strategies; constructor(config: ContextConfig); /** * Get current token usage */ getTokenUsage(messages: ContextMessage[]): TokenUsage; /** * Check if messages need truncation */ needsTruncation(messages: ContextMessage[]): boolean; /** * Check if approaching token limit */ isApproachingLimit(messages: ContextMessage[]): boolean; /** * Truncate messages using configured strategy */ truncate(messages: ContextMessage[]): ContextMessage[]; /** * Add message with automatic truncation */ addMessage(messages: ContextMessage[], newMessage: ContextMessage): ContextMessage[]; /** * Add multiple messages with automatic truncation */ addMessages(messages: ContextMessage[], newMessages: ContextMessage[]): ContextMessage[]; /** * Get maximum token limit for current model */ getModelMaxTokens(): number; /** * Update configuration */ updateConfig(config: Partial): void; /** * Register a custom truncation strategy */ registerStrategy(strategy: TruncationStrategy): void; /** * Clean up resources */ dispose(): void; } export { type ContextConfig, ContextManager, type ContextMessage, MODEL_ENCODING_MAP, MODEL_TOKEN_LIMITS, MessageImportance, type ModelType, TokenCount, TokenCounter, type TokenUsage, type TruncationStrategy, type TruncationStrategyType, tokenCounter };