"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveProviderToolResultMaxChars = resolveProviderToolResultMaxChars;
exports.truncateProviderToolOutputText = truncateProviderToolOutputText;
const DEFAULT_PROVIDER_TOOL_OUTPUT_CHAR_LIMITS = {
    codex: 8 * 1024 * 1024,
    openai: 8 * 1024 * 1024,
    'openai-compatible': 4 * 1024 * 1024,
    anthropic: 4 * 1024 * 1024,
    'anthropic-compatible': 4 * 1024 * 1024,
    mock: 8 * 1024 * 1024,
};
function buildProviderToolOutputSuffix(originalChars, limitChars) {
    return `\n[tool_output_truncated_for_provider original_chars=${originalChars} limit_chars=${limitChars}]`;
}
function resolveProviderToolResultMaxChars(providerConfig) {
    const configured = providerConfig?.tool_result_max_chars;
    if (typeof configured === 'number' && Number.isInteger(configured) && configured > 0) {
        return configured;
    }
    if (!providerConfig) {
        return DEFAULT_PROVIDER_TOOL_OUTPUT_CHAR_LIMITS['openai'];
    }
    return DEFAULT_PROVIDER_TOOL_OUTPUT_CHAR_LIMITS[providerConfig.apiType];
}
function truncateProviderToolOutputText(text, limitChars = DEFAULT_PROVIDER_TOOL_OUTPUT_CHAR_LIMITS['openai']) {
    if (text.length <= limitChars) {
        return {
            text,
            truncated: false,
            originalChars: text.length,
            limitChars,
        };
    }
    const suffix = buildProviderToolOutputSuffix(text.length, limitChars);
    if (suffix.length >= limitChars) {
        return {
            text: suffix.slice(0, limitChars),
            truncated: true,
            originalChars: text.length,
            limitChars,
        };
    }
    const keepChars = limitChars - suffix.length;
    return {
        text: `${text.slice(0, keepChars)}${suffix}`,
        truncated: true,
        originalChars: text.length,
        limitChars,
    };
}
