"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.filterHistoryImages = filterHistoryImages;
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
const l10n = __importStar(require("@vscode/l10n"));
const prompt_tsx_1 = require("@vscode/prompt-tsx");
/**
 * Model-facing placeholder substituted for dropped history images.
 * Intentionally not localized — this text is sent to the model, not the user.
 */
const IMAGE_PLACEHOLDER_TEXT = '[Image omitted from conversation history due to model limit.]';
/**
 * Silently drops the oldest images from history when the total number of images
 * in the conversation exceeds `maxImages`. Images belonging to the current turn
 * (the last user message and anything after it, e.g. recent tool results) are
 * always preserved.
 *
 * If the current turn alone exceeds the limit, throws a localized error rather
 * than sending a request we know will be rejected with an opaque server error.
 *
 * @returns A (possibly filtered) copy of messages. The original array is never mutated.
 */
function filterHistoryImages(messages, maxImages) {
    // Anchor the current turn at the last user message; anything at or after this
    // index is treated as "current turn" and its images are never filtered.
    let lastUserIdx = -1;
    for (let i = messages.length - 1; i >= 0; i--) {
        if (messages[i].role === prompt_tsx_1.Raw.ChatRole.User) {
            lastUserIdx = i;
            break;
        }
    }
    // Corner case: no user message at all (e.g. system-only history). Treat the
    // last message as the current turn so we still filter earlier images.
    if (lastUserIdx === -1 && messages.length > 0) {
        lastUserIdx = messages.length - 1;
    }
    // Count images in the current turn (the last user message and anything after it).
    let currentTurnImages = 0;
    for (let i = Math.max(lastUserIdx, 0); i < messages.length; i++) {
        const content = messages[i].content;
        if (!Array.isArray(content)) {
            continue;
        }
        for (const part of content) {
            if (part.type === prompt_tsx_1.Raw.ChatCompletionContentPartKind.Image) {
                currentTurnImages++;
            }
        }
    }
    // Count total images across all messages
    let totalImages = 0;
    for (const message of messages) {
        if (Array.isArray(message.content)) {
            for (const part of message.content) {
                if (part.type === prompt_tsx_1.Raw.ChatCompletionContentPartKind.Image) {
                    totalImages++;
                }
            }
        }
    }
    // No filtering needed if total is within the limit
    if (totalImages <= maxImages) {
        return messages;
    }
    // Fail fast with a clear, localized error when the current turn alone exceeds
    // the limit — otherwise we'd send a request the server will reject with an
    // opaque error. Silent history filtering is only safe when dropping history
    // images can bring the total down to the limit.
    if (currentTurnImages > maxImages) {
        throw new Error(l10n.t('Too many images in request: {0} images provided, but the model supports a maximum of {1} images.', currentTurnImages, maxImages));
    }
    // Walk backward through history (before the current turn), keeping the
    // most recent images and replacing the oldest with placeholders.
    let historyBudget = maxImages - currentTurnImages;
    // Collect keep/drop decisions by walking backward through history
    const historyImageDecisions = new Map(); // "msgIdx:partIdx" -> keep
    for (let i = lastUserIdx - 1; i >= 0; i--) {
        if (!Array.isArray(messages[i].content)) {
            continue;
        }
        for (let j = messages[i].content.length - 1; j >= 0; j--) {
            if (messages[i].content[j].type === prompt_tsx_1.Raw.ChatCompletionContentPartKind.Image) {
                const key = `${i}:${j}`;
                if (historyBudget > 0) {
                    historyImageDecisions.set(key, true);
                    historyBudget--;
                }
                else {
                    historyImageDecisions.set(key, false);
                }
            }
        }
    }
    // Build filtered messages, replacing dropped images with text placeholders
    return messages.map((message, msgIdx) => {
        if (msgIdx >= lastUserIdx) {
            return message;
        }
        if (!Array.isArray(message.content)) {
            return message;
        }
        if (!message.content.some(p => p.type === prompt_tsx_1.Raw.ChatCompletionContentPartKind.Image)) {
            return message;
        }
        return {
            ...message,
            content: message.content.map((part, partIdx) => {
                if (part.type !== prompt_tsx_1.Raw.ChatCompletionContentPartKind.Image) {
                    return part;
                }
                if (historyImageDecisions.get(`${msgIdx}:${partIdx}`)) {
                    return part;
                }
                return { type: prompt_tsx_1.Raw.ChatCompletionContentPartKind.Text, text: IMAGE_PLACEHOLDER_TEXT };
            })
        };
    });
}
//# sourceMappingURL=imageLimits.js.map