/** * Sliding window conversation history management. * * This module provides a sliding window strategy for managing conversation history * that preserves tool usage pairs and avoids invalid window states. */ import type { LocalAgent } from '../types/agent.js'; import { ConversationManager, type ProactiveCompressionConfig, type ConversationManagerReduceOptions } from './conversation-manager.js'; /** * Configuration for the sliding window conversation manager. */ export type SlidingWindowConversationManagerConfig = { /** * Maximum number of messages to keep in the conversation history. * Defaults to 40 messages. */ windowSize?: number; /** * Whether to truncate tool results when a message is too large for the model's context window. * Defaults to true. */ shouldTruncateResults?: boolean; /** * Enable proactive context compression before the model call. * * - `true`: compress when 70% of the context window is used (default threshold). * - `{ compressionThreshold: number }`: compress at the specified ratio (0, 1]. * - `false` or omitted: disabled, only reactive overflow recovery is used. */ proactiveCompression?: boolean | ProactiveCompressionConfig; /** * Number of messages at the start of the conversation to permanently pin. * Pinned messages are protected from eviction during context reduction. */ pinFirst?: number; }; /** * Implements a sliding window strategy for managing conversation history. * * This class handles the logic of maintaining a conversation window that preserves * tool usage pairs and avoids invalid window states. When the message count exceeds * the window size, it will either truncate large tool results or remove the oldest * messages while ensuring tool use/result pairs remain valid. * * Registers hooks for: * - AfterInvocationEvent: Applies sliding window management after each invocation * - AfterModelCallEvent: Reduces context on overflow errors and requests retry (via super) * - BeforeModelCallEvent: Proactive compression when threshold is exceeded (via super) */ export declare class SlidingWindowConversationManager extends ConversationManager { private readonly _windowSize; private readonly _shouldTruncateResults; private readonly _pinFirst; private _pinFirstApplied; /** * Unique identifier for this conversation manager. */ readonly name = "strands:sliding-window-conversation-manager"; /** * Initialize the sliding window conversation manager. * * @param config - Configuration options for the sliding window manager. */ constructor(config?: SlidingWindowConversationManagerConfig); /** * Initialize the plugin by registering hooks with the agent. * * Registers: * - AfterInvocationEvent callback to apply sliding window management * - AfterModelCallEvent callback to handle context overflow and request retry (via super) * - BeforeModelCallEvent callback for proactive compression (via super) * * @param agent - The agent to register hooks with */ initAgent(agent: LocalAgent): void; /** * Reduce the conversation history. * * When `error` is set (reactive overflow recovery), attempts to truncate large tool results * first before falling back to message trimming. * * When `error` is undefined (proactive compression), only trims messages without attempting * tool result truncation. * * @param options - The reduction options * @returns `true` if the history was reduced, `false` otherwise */ reduce({ agent, error }: ConversationManagerReduceOptions): boolean; /** * Apply the sliding window to the messages array to maintain a manageable history size. * * Called after every agent invocation. No-op if within the window size. * * @param messages - The message array to manage. Modified in-place. */ private _applyManagement; /** * Trim the oldest messages to reduce the conversation context size. * * The method handles special cases where trimming the messages leads to: * - toolResult with no corresponding toolUse * - toolUse with no corresponding toolResult * * The strategy is: * 1. First, attempt to truncate large tool results if shouldTruncateResults is true * 2. If truncation is not possible or doesn't help, trim oldest messages * 3. When trimming, skip invalid trim points (toolResult at start, or toolUse without following toolResult) * * @param messages - The message array to reduce. Modified in-place. * @param _error - The error that triggered the context reduction, if any. * @returns `true` if any reduction occurred, `false` otherwise. */ private _reduceContext; /** * Apply head/tail truncation to a string if it exceeds the size threshold. * * Returns the truncated form (first {@link PRESERVE_CHARS} + marker + last * {@link PRESERVE_CHARS}) when the input exceeds {@link TRUNCATION_THRESHOLD}, * otherwise `undefined`. */ private _truncateLongText; /** * Truncate tool result content in a message to reduce context size. * * Rule: preserve head/tail when the payload is plain-text-shaped; replace * wholesale when it's binary or remote. Specifically: * - Text blocks: partial head/tail truncation if over threshold. * - Image, Video blocks: wholesale replacement with a textual placeholder. * - Document blocks with bytes/s3 source: wholesale replacement. * - Document blocks with text source: partial truncation of the inner text. * - Document blocks with content source (TextBlock[]): partial truncation of * each nested block. * - JSON blocks: wholesale replacement if serialized length is over threshold; * mid-structure truncation would produce invalid JSON. * * The tool result `status` and `error` fields are preserved. * * @param messages - The conversation message history. * @param msgIdx - Index of the message containing tool results to truncate. * @returns True if any changes were made to the message, false otherwise. */ private _truncateToolResults; /** * Find the index of the oldest message containing tool results. * * Truncation targets the least-recent tool result first so the most relevant * recent context is preserved as long as possible. * * @param messages - The conversation message history. * @returns Index of the oldest message with tool results, or undefined if no such message exists. */ private _findOldestMessageWithToolResults; } //# sourceMappingURL=sliding-window-conversation-manager.d.ts.map