"use strict";
/**
 * Shared vision analysis helper for playwright tools.
 *
 * Reads a screenshot from the storage backend and analyzes it
 * via the secondary vision LLM. Used by both the standalone
 * screenshot tool and run_script's screenshot steps.
 */
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.analyzeScreenshot = analyzeScreenshot;
const path = __importStar(require("path"));
const storage_1 = require("../../../lib/storage");
const MIME_MAP = {
    '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg',
    '.gif': 'image/gif', '.webp': 'image/webp',
};
/**
 * Read a screenshot from storage and analyze it via the vision LLM.
 * Returns the raw LLM text response (typically JSON with description, objects, etc.).
 */
async function analyzeScreenshot(storagePath) {
    const { callLLM, hasLLMApiKey } = await Promise.resolve().then(() => __importStar(require('../../../services/llm')));
    const { LLM_MODEL_SECONDARY, LLM_MAX_TOKENS_VISION } = await Promise.resolve().then(() => __importStar(require('../../../modules/defaults')));
    const { ANALYZE_IMAGE_PROMPT } = await Promise.resolve().then(() => __importStar(require('../vision-prompts')));
    if (!hasLLMApiKey(LLM_MODEL_SECONDARY)) {
        return 'Vision LLM not configured — set an API key to enable screenshot analysis.';
    }
    const backend = (0, storage_1.getStorageBackend)();
    const { data } = await backend.read(storagePath);
    const base64 = data.toString('base64');
    const ext = path.extname(storagePath).toLowerCase();
    const mime = MIME_MAP[ext] || 'image/png';
    const response = await callLLM({
        model: LLM_MODEL_SECONDARY,
        messages: [
            { role: 'system', content: ANALYZE_IMAGE_PROMPT },
            {
                role: 'user',
                content: [
                    { type: 'image_url', image_url: { url: `data:${mime};base64,${base64}` } },
                ],
            },
        ],
        max_tokens: LLM_MAX_TOKENS_VISION,
        temperature: 0,
    });
    return response.content || 'No description generated.';
}
