"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WORKER_PROMPT_ARTIFACT_LIMITS = void 0;
exports.truncatePromptTextArtifact = truncatePromptTextArtifact;
exports.truncatePromptDiffArtifact = truncatePromptDiffArtifact;
exports.WORKER_PROMPT_ARTIFACT_LIMITS = {
    stdoutCharacters: 2_000,
    stderrCharacters: 4_000,
    gitStatusCharacters: 4_000,
    diffTotalCharacters: 24_000,
    diffFileCharacters: 4_000,
    diffFileCount: 8,
};
function truncatePromptTextArtifact(value, maxCharacters) {
    if (value.length <= maxCharacters) {
        return value;
    }
    const marker = `[truncated: original=${String(value.length)} chars]`;
    const separatorCharacters = marker.length + 2;
    const remainingCharacters = Math.max(0, maxCharacters - separatorCharacters);
    const headCharacters = Math.ceil(remainingCharacters / 2);
    const tailCharacters = Math.floor(remainingCharacters / 2);
    if (tailCharacters === 0) {
        return `${value.slice(0, headCharacters)}\n${marker}`;
    }
    return [
        value.slice(0, headCharacters),
        marker,
        value.slice(value.length - tailCharacters),
    ].join("\n");
}
function truncatePromptDiffArtifact(value) {
    if (value.length === 0) {
        return value;
    }
    const sections = splitDiffSections(value);
    const selectedSections = sections.slice(0, exports.WORKER_PROMPT_ARTIFACT_LIMITS.diffFileCount);
    const omittedSectionCount = Math.max(0, sections.length - selectedSections.length);
    const omittedMarker = omittedSectionCount === 0
        ? ""
        : `\n[truncated: original=${String(value.length)} chars, omittedDiffFiles=${String(omittedSectionCount)}]`;
    const joinCharacters = Math.max(0, selectedSections.length - 1) * 2;
    const availableForSections = Math.max(selectedSections.length, exports.WORKER_PROMPT_ARTIFACT_LIMITS.diffTotalCharacters -
        omittedMarker.length -
        joinCharacters);
    const sectionBudget = Math.max(1, Math.min(exports.WORKER_PROMPT_ARTIFACT_LIMITS.diffFileCharacters, Math.floor(availableForSections / Math.max(1, selectedSections.length))));
    const diffText = selectedSections
        .map((section) => truncatePromptTextArtifact(section, sectionBudget))
        .join("\n\n");
    const combined = `${diffText}${omittedMarker}`;
    return truncatePromptTextArtifact(combined, exports.WORKER_PROMPT_ARTIFACT_LIMITS.diffTotalCharacters);
}
function splitDiffSections(value) {
    const sections = value
        .split(/(?=^diff --git )/mu)
        .map((section) => section.trim())
        .filter((section) => section.length > 0);
    return sections.length === 0 ? [value] : sections;
}
