"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    autoGlmLocate: ()=>autoGlmLocate
});
const extractor_namespaceObject = require("@midscene/shared/extractor");
const logger_namespaceObject = require("@midscene/shared/logger");
const utils_namespaceObject = require("@midscene/shared/utils");
const external_inspect_js_namespaceObject = require("../../inspect.js");
const llm_locator_js_namespaceObject = require("../../prompt/llm-locator.js");
const index_js_namespaceObject = require("../../service-caller/index.js");
const bbox_js_namespaceObject = require("../../shared/model-locate-result/bbox.js");
const pixel_bbox_mapper_js_namespaceObject = require("../../shared/model-locate-result/pixel-bbox-mapper.js");
const locate_result_rect_js_namespaceObject = require("../../workflows/inspect/locate-result-rect.js");
const search_area_mapping_js_namespaceObject = require("../../workflows/inspect/search-area-mapping.js");
const external_parser_js_namespaceObject = require("./parser.js");
const debugInspect = (0, logger_namespaceObject.getDebug)('ai:inspect');
async function autoGlmLocate(elementDescription, options, getSystemPrompt) {
    const { context, modelRuntime } = options;
    const screenshotBase64 = context.screenshot.base64;
    (0, utils_namespaceObject.assert)(elementDescription, "cannot find the target element description");
    const elementDescriptionText = (0, external_inspect_js_namespaceObject.extraTextFromUserPrompt)(elementDescription);
    const userInstructionPrompt = (0, llm_locator_js_namespaceObject.findElementPrompt)(elementDescriptionText);
    const locateImage = options.searchConfig?.image ?? {
        imageBase64: screenshotBase64,
        width: context.shotSize.width,
        height: context.shotSize.height
    };
    const imagePayload = locateImage.imageBase64;
    const imageWidth = locateImage.width;
    const imageHeight = locateImage.height;
    const msgs = [
        {
            role: 'system',
            content: getSystemPrompt()
        },
        {
            role: 'user',
            content: [
                {
                    type: 'image_url',
                    image_url: {
                        url: imagePayload,
                        detail: 'high'
                    }
                },
                {
                    type: 'text',
                    text: `Tap: ${userInstructionPrompt}`
                }
            ]
        }
    ];
    if ('string' != typeof elementDescription) {
        const addOns = await (0, external_inspect_js_namespaceObject.promptsToChatParam)({
            images: elementDescription.images,
            convertHttpImage2Base64: elementDescription.convertHttpImage2Base64
        });
        msgs.push(...addOns);
    }
    const { content: rawResponseContent, usage, rawChoiceMessage } = await (0, index_js_namespaceObject.callAIWithStringResponse)(msgs, modelRuntime, {
        abortSignal: options.abortSignal
    });
    debugInspect('auto-glm rawResponse:', rawResponseContent);
    const parsed = (0, external_parser_js_namespaceObject.parseAutoGLMLocateResponse)(rawResponseContent);
    debugInspect('auto-glm thinking:', parsed.think);
    debugInspect('auto-glm coordinates:', parsed.coordinates);
    let resRect;
    let matchedElement;
    let errors = [];
    if (parsed.error || !parsed.coordinates) {
        errors = [
            parsed.error || 'Failed to parse auto-glm response'
        ];
        debugInspect('auto-glm parse error:', errors[0]);
    } else {
        const { x, y } = parsed.coordinates;
        debugInspect('auto-glm coordinates [0-999]:', {
            x,
            y
        });
        const ctx = {
            preparedSize: {
                width: imageWidth,
                height: imageHeight
            }
        };
        const targetPixelBbox = (0, bbox_js_namespaceObject.finalizePixelBbox)((0, pixel_bbox_mapper_js_namespaceObject.mapLocateResultToPixelBboxByCoordinates)({
            type: 'point',
            coordinates: [
                x,
                y
            ]
        }, ctx, {
            shape: 'point',
            order: 'xy',
            normalizedBy: 1000
        }), parsed.coordinates, ctx);
        resRect = (0, locate_result_rect_js_namespaceObject.pixelBboxToRect)((0, search_area_mapping_js_namespaceObject.mapSearchAreaPixelBboxToOriginalPixelBbox)(targetPixelBbox, options.searchConfig?.mapping));
        debugInspect('auto-glm resRect:', resRect);
        const element = (0, extractor_namespaceObject.generateElementByRect)(resRect, elementDescriptionText);
        if (element) matchedElement = element;
    }
    return {
        rect: resRect,
        parseResult: {
            element: matchedElement,
            errors
        },
        rawResponse: rawResponseContent,
        rawChoiceMessage,
        usage,
        reasoning_content: parsed.think
    };
}
exports.autoGlmLocate = __webpack_exports__.autoGlmLocate;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "autoGlmLocate"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});

//# sourceMappingURL=locate.js.map