// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Determine the effective verbosity level, auto-degrading when context
 * pressure exceeds the configured threshold.
 */
export function getEffectiveVerbosity(requested, percentUsed, autoCompressThreshold) {
    const fraction = percentUsed / 100;
    if (fraction < autoCompressThreshold) {
        return { verbosity: requested, autoCompressed: false };
    }
    const degraded = degradeVerbosity(requested);
    return {
        verbosity: degraded,
        autoCompressed: degraded !== requested,
    };
}
/**
 * Check whether the current context usage warrants auto-compression.
 */
export function shouldAutoCompress(percentUsed, threshold) {
    return percentUsed / 100 >= threshold;
}
/**
 * Compress a tool response text based on the target verbosity level.
 * - compact: strip code blocks, collapse bullet lists, shorten sections
 * - minimal: return only first paragraph + structured summary
 */
export function compressResponse(text, targetVerbosity) {
    if (targetVerbosity === 'full') {
        return text;
    }
    if (targetVerbosity === 'compact') {
        return compactCompress(text);
    }
    return minimalCompress(text);
}
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
function degradeVerbosity(current) {
    if (current === 'full') {
        return 'compact';
    }
    if (current === 'compact') {
        return 'minimal';
    }
    return 'minimal';
}
/** Compact: remove code blocks, trim long lists, keep structure. */
function compactCompress(text) {
    let result = text;
    // Remove fenced code blocks
    result = result.replace(/```[\s\S]*?```/g, '[code block omitted for brevity]');
    // Collapse long bullet lists (>5 items) to first 3 + count
    result = collapseListItems(result);
    return result;
}
/** Minimal: extract first meaningful paragraph only. */
function minimalCompress(text) {
    const lines = text.split('\n');
    const meaningful = [];
    for (const line of lines) {
        const trimmed = line.trim();
        // Skip empty lines and code blocks
        if (trimmed === '' && meaningful.length > 0) {
            break;
        }
        if (trimmed.startsWith('```')) {
            break;
        }
        if (trimmed !== '') {
            meaningful.push(trimmed);
        }
    }
    return meaningful.length > 0 ? meaningful.join('\n') : text.slice(0, 200);
}
/** Collapse bullet lists with more than 5 items. */
function collapseListItems(text) {
    const lines = text.split('\n');
    const result = [];
    let bulletCount = 0;
    let inBulletList = false;
    let totalBullets = 0;
    for (const line of lines) {
        const isBullet = /^\s*[-*]\s/.test(line);
        if (isBullet) {
            if (!inBulletList) {
                inBulletList = true;
                bulletCount = 0;
                totalBullets = countConsecutiveBullets(lines, lines.indexOf(line));
            }
            bulletCount++;
            if (bulletCount <= 3) {
                result.push(line);
            }
            else if (bulletCount === 4 && totalBullets > 5) {
                result.push(`  ... and ${String(totalBullets - 3)} more items`);
            }
        }
        else {
            inBulletList = false;
            bulletCount = 0;
            result.push(line);
        }
    }
    return result.join('\n');
}
function countConsecutiveBullets(lines, startIndex) {
    let count = 0;
    for (let i = startIndex; i < lines.length; i++) {
        if (/^\s*[-*]\s/.test(lines[i] ?? '')) {
            count++;
        }
        else {
            break;
        }
    }
    return count;
}
//# sourceMappingURL=compressor.js.map