import { findSafeTrimIndex } from '../dangling.js';
/**
 * Simple window-based conversation manager.
 * Trims old messages to maintain a fixed-size window of recent context.
 * Fastest strategy, least context preservation.
 *
 * Algorithm:
 * 1. If message count exceeds window size, trim excess from the start
 * 2. Use findSafeTrimIndex to ensure tool call/result pairs remain atomic
 * 3. applyManagement runs proactively each iteration; reduceContext on overflow
 */
export class SlidingWindowManager {
    name = 'sliding-window';
    keepRecentMessages;
    constructor(config = {}) {
        this.keepRecentMessages = config.keepRecentMessages ?? 4;
    }
    applyManagement(messages) {
        // Trim to window size if needed
        if (messages.length <= this.keepRecentMessages) {
            return messages;
        }
        const desiredTrimPoint = messages.length - this.keepRecentMessages;
        const safeTrimIdx = findSafeTrimIndex(messages, desiredTrimPoint);
        return messages.slice(safeTrimIdx);
    }
    reduceContext(messages, _overflowTokens) {
        // Try to reduce by trimming more aggressively
        // Target: keep even fewer messages than normal window
        const targetWindow = Math.max(1, Math.floor(this.keepRecentMessages * 0.5));
        const desiredTrimPoint = messages.length - targetWindow;
        const safeTrimIdx = findSafeTrimIndex(messages, desiredTrimPoint);
        if (safeTrimIdx >= messages.length) {
            // Cannot trim further
            return false;
        }
        // Successfully trimmed if we removed at least one message
        return safeTrimIdx > 0;
    }
}
//# sourceMappingURL=slidingWindow.js.map