"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__, {
    promptsToChatParam: ()=>external_common_js_namespaceObject.multimodalPromptToChatMessages,
    AiJudgeOrderSensitive: ()=>AiJudgeOrderSensitive,
    AiLocateSection: ()=>AiLocateSection,
    genericLocate: ()=>genericLocate,
    AiLocateElement: ()=>AiLocateElement,
    AiExtractElementInfo: ()=>AiExtractElementInfo,
    buildSearchAreaConfig: ()=>buildSearchAreaConfig,
    extraTextFromUserPrompt: ()=>external_common_js_namespaceObject.userPromptToString
});
const extractor_namespaceObject = require("@midscene/shared/extractor");
const img_namespaceObject = require("@midscene/shared/img");
const logger_namespaceObject = require("@midscene/shared/logger");
const utils_namespaceObject = require("@midscene/shared/utils");
const external_common_js_namespaceObject = require("../common.js");
const extraction_js_namespaceObject = require("./prompt/extraction.js");
const llm_locator_js_namespaceObject = require("./prompt/llm-locator.js");
const llm_section_locator_js_namespaceObject = require("./prompt/llm-section-locator.js");
const order_sensitive_judge_js_namespaceObject = require("./prompt/order-sensitive-judge.js");
const index_js_namespaceObject = require("./service-caller/index.js");
const image_preprocess_js_namespaceObject = require("./workflows/image-preprocess.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 debugInspect = (0, logger_namespaceObject.getDebug)('ai:inspect');
const debugSection = (0, logger_namespaceObject.getDebug)('ai:section');
function hasLocateResult(input, resultKey) {
    if (!input || 'object' != typeof input) return false;
    const record = input;
    const locateResult = record[resultKey];
    return Array.isArray(locateResult) ? locateResult.length > 0 : void 0 !== locateResult;
}
async function buildSearchAreaConfig(options) {
    const { context, baseRect } = options;
    const scaleRatio = 2;
    const sectionRect = (0, external_common_js_namespaceObject.expandSearchArea)(baseRect, context.shotSize);
    const croppedResult = await (0, img_namespaceObject.cropByRect)(context.screenshot.base64, sectionRect);
    const scaledResult = await (0, img_namespaceObject.scaleImage)(croppedResult.imageBase64, scaleRatio);
    return {
        sourceRect: sectionRect,
        image: {
            imageBase64: scaledResult.imageBase64,
            width: scaledResult.width,
            height: scaledResult.height
        },
        mapping: {
            offset: {
                x: sectionRect.left,
                y: sectionRect.top
            },
            scale: scaleRatio
        }
    };
}
async function AiLocateElement(options) {
    const { targetElementDescription, ...locateOptions } = options;
    const locateAdapter = options.modelRuntime.adapter.locate;
    if ('custom' === locateAdapter.kind) return locateAdapter.locateFn(targetElementDescription, locateOptions);
    return genericLocate(targetElementDescription, locateOptions);
}
async function genericLocate(elementDescription, options) {
    const { context } = options;
    const modelRuntime = options.modelRuntime;
    const { adapter } = modelRuntime;
    (0, utils_namespaceObject.assert)('standard' === adapter.locate.kind, 'generic locate requires a standard locate adapter');
    const screenshotBase64 = context.screenshot.base64;
    (0, utils_namespaceObject.assert)(elementDescription, "cannot find the target element description");
    const elementDescriptionText = (0, external_common_js_namespaceObject.userPromptToString)(elementDescription);
    const userInstructionPrompt = (0, llm_locator_js_namespaceObject.findElementPrompt)(elementDescriptionText);
    const systemPrompt = (0, llm_locator_js_namespaceObject.systemPromptToLocateElement)(adapter.locate.resultAdapter.promptSpec);
    const modelImage = options.searchConfig?.image ?? {
        imageBase64: screenshotBase64,
        width: context.shotSize.width,
        height: context.shotSize.height
    };
    const preparedImage = await (0, image_preprocess_js_namespaceObject.prepareModelImage)({
        imageBase64: modelImage.imageBase64,
        width: modelImage.width,
        height: modelImage.height,
        policy: adapter.imagePreprocess
    });
    const imagePayload = preparedImage.imageBase64;
    const msgs = [
        {
            role: 'system',
            content: systemPrompt
        },
        {
            role: 'user',
            content: [
                {
                    type: 'image_url',
                    image_url: {
                        url: imagePayload,
                        detail: 'high'
                    }
                },
                {
                    type: 'text',
                    text: userInstructionPrompt
                }
            ]
        }
    ];
    if ('string' != typeof elementDescription) {
        const addOns = await (0, external_common_js_namespaceObject.multimodalPromptToChatMessages)((0, external_common_js_namespaceObject.userPromptToMultimodalPrompt)(elementDescription));
        msgs.push(...addOns);
    }
    let res;
    try {
        res = await (0, index_js_namespaceObject.callAIWithObjectResponse)(msgs, modelRuntime, {
            abortSignal: options.abortSignal,
            jsonParserSource: 'locate'
        });
    } catch (callError) {
        const errorMessage = callError instanceof Error ? callError.message : String(callError);
        const rawResponse = callError instanceof index_js_namespaceObject.AIResponseParseError ? callError.rawResponse : errorMessage;
        const usage = callError instanceof index_js_namespaceObject.AIResponseParseError ? callError.usage : void 0;
        const rawChoiceMessage = callError instanceof index_js_namespaceObject.AIResponseParseError ? callError.rawChoiceMessage : void 0;
        return {
            rect: void 0,
            parseResult: {
                element: void 0,
                errors: [
                    `AI call error: ${errorMessage}`
                ]
            },
            rawResponse,
            rawChoiceMessage,
            usage,
            reasoning_content: void 0
        };
    }
    const rawResponse = JSON.stringify(res.content);
    let resRect;
    let matchedElement;
    let errors = 'errors' in res.content ? res.content.errors : [];
    const resultAdapter = adapter.locate.resultAdapter;
    if (!hasLocateResult(res.content, resultAdapter.promptSpec.resultKey)) return {
        rect: void 0,
        parseResult: {
            element: void 0,
            errors: errors
        },
        rawResponse,
        rawChoiceMessage: res.rawChoiceMessage,
        usage: res.usage,
        reasoning_content: res.reasoning_content
    };
    try {
        const mapping = options.searchConfig?.mapping;
        const targetPixelBbox = resultAdapter.adaptElementLocateResultToPixelBbox(res.content, {
            preparedSize: preparedImage.preparedSize,
            contentSize: preparedImage.contentSize
        });
        resRect = (0, locate_result_rect_js_namespaceObject.pixelBboxToRect)((0, search_area_mapping_js_namespaceObject.mapSearchAreaPixelBboxToOriginalPixelBbox)(targetPixelBbox, mapping));
        debugInspect('resRect', resRect);
        const element = (0, extractor_namespaceObject.generateElementByRect)(resRect, elementDescriptionText);
        errors = [];
        if (element) matchedElement = element;
    } catch (e) {
        const msg = e instanceof Error ? `Failed to parse locate result: ${e.message}` : 'unknown error in locate';
        if (errors && errors?.length !== 0) errors.push(`(${msg})`);
        else errors = [
            msg
        ];
    }
    return {
        rect: resRect,
        parseResult: {
            element: matchedElement,
            errors: errors
        },
        rawResponse,
        rawChoiceMessage: res.rawChoiceMessage,
        usage: res.usage,
        reasoning_content: res.reasoning_content
    };
}
async function AiLocateSection(options) {
    const { context, sectionDescription } = options;
    const modelRuntime = options.modelRuntime;
    const { adapter } = modelRuntime;
    (0, utils_namespaceObject.assert)('standard' === adapter.locate.kind, 'section locate requires a standard locate adapter');
    const screenshotBase64 = context.screenshot.base64;
    const preparedImage = await (0, image_preprocess_js_namespaceObject.prepareModelImage)({
        imageBase64: screenshotBase64,
        width: context.shotSize.width,
        height: context.shotSize.height,
        policy: adapter.imagePreprocess
    });
    const systemPrompt = (0, llm_section_locator_js_namespaceObject.systemPromptToLocateSection)(adapter.locate.resultAdapter.promptSpec);
    const sectionLocatorInstructionText = (0, llm_section_locator_js_namespaceObject.sectionLocatorInstruction)((0, external_common_js_namespaceObject.userPromptToString)(sectionDescription));
    const msgs = [
        {
            role: 'system',
            content: systemPrompt
        },
        {
            role: 'user',
            content: [
                {
                    type: 'image_url',
                    image_url: {
                        url: preparedImage.imageBase64,
                        detail: 'high'
                    }
                },
                {
                    type: 'text',
                    text: sectionLocatorInstructionText
                }
            ]
        }
    ];
    if ('string' != typeof sectionDescription) {
        const addOns = await (0, external_common_js_namespaceObject.multimodalPromptToChatMessages)((0, external_common_js_namespaceObject.userPromptToMultimodalPrompt)(sectionDescription));
        msgs.push(...addOns);
    }
    let result;
    try {
        result = await (0, index_js_namespaceObject.callAIWithObjectResponse)(msgs, modelRuntime, {
            abortSignal: options.abortSignal,
            jsonParserSource: 'section-locator'
        });
    } catch (callError) {
        const errorMessage = callError instanceof Error ? callError.message : String(callError);
        const rawResponse = callError instanceof index_js_namespaceObject.AIResponseParseError ? callError.rawResponse : errorMessage;
        const usage = callError instanceof index_js_namespaceObject.AIResponseParseError ? callError.usage : void 0;
        const rawChoiceMessage = callError instanceof index_js_namespaceObject.AIResponseParseError ? callError.rawChoiceMessage : void 0;
        return {
            searchAreaConfig: void 0,
            error: `AI call error: ${errorMessage}`,
            rawResponse,
            rawChoiceMessage,
            usage
        };
    }
    let searchAreaConfig;
    let sectionError = result.content.error;
    const resultAdapter = adapter.locate.resultAdapter;
    if (!hasLocateResult(result.content, resultAdapter.promptSpec.resultKey)) return {
        searchAreaConfig: void 0,
        error: sectionError,
        rawResponse: JSON.stringify(result.content),
        rawChoiceMessage: result.rawChoiceMessage,
        usage: result.usage
    };
    try {
        const adaptedResult = resultAdapter.adaptSectionLocateResultToPixelBboxGroup(result.content, {
            preparedSize: preparedImage.preparedSize,
            contentSize: preparedImage.contentSize
        });
        const mergedRect = (0, locate_result_rect_js_namespaceObject.mergePixelBboxesToRect)([
            adaptedResult.target,
            ...adaptedResult.references ?? []
        ]);
        debugSection('mergedRect %j', mergedRect);
        const expandedRect = (0, external_common_js_namespaceObject.expandSearchArea)(mergedRect, context.shotSize);
        const originalWidth = expandedRect.width;
        const originalHeight = expandedRect.height;
        debugSection('expanded sectionRect %j', expandedRect);
        searchAreaConfig = await buildSearchAreaConfig({
            context,
            baseRect: mergedRect
        });
        debugSection('scaled section image from %dx%d to %dx%d (scale=%d)', originalWidth, originalHeight, searchAreaConfig.image.width, searchAreaConfig.image.height, searchAreaConfig.mapping.scale);
    } catch (error) {
        const parseErrorMessage = error instanceof Error ? `Failed to parse section locate result: ${error.message}` : 'unknown error in section locate';
        sectionError = sectionError ? `${sectionError} (${parseErrorMessage})` : parseErrorMessage;
    }
    return {
        searchAreaConfig,
        error: sectionError,
        rawResponse: JSON.stringify(result.content),
        rawChoiceMessage: result.rawChoiceMessage,
        usage: result.usage
    };
}
async function AiExtractElementInfo(options) {
    const { dataQuery, context, extractOption, multimodalPrompt, modelRuntime } = options;
    const systemPrompt = (0, extraction_js_namespaceObject.systemPromptToExtract)({
        screenshotIncluded: extractOption?.screenshotIncluded !== false,
        referenceImagesIncluded: !!multimodalPrompt?.images?.length
    });
    const screenshotBase64 = context.screenshot.base64;
    const extractDataPromptText = (0, extraction_js_namespaceObject.extractDataQueryPrompt)(options.pageDescription || '', dataQuery);
    const userContent = [];
    if (extractOption?.screenshotIncluded !== false) {
        userContent.push({
            type: 'text',
            text: 'This is the current screenshot to evaluate. Unless <DATA_DEMAND> explicitly asks for comparison or matching against reference images, base your answer on this screenshot and its contents when provided.'
        });
        userContent.push({
            type: 'image_url',
            image_url: {
                url: screenshotBase64,
                detail: 'high'
            }
        });
    }
    userContent.push({
        type: 'text',
        text: extractDataPromptText
    });
    const msgs = [
        {
            role: 'system',
            content: systemPrompt
        },
        {
            role: 'user',
            content: userContent
        }
    ];
    if (multimodalPrompt) {
        const addOns = await (0, external_common_js_namespaceObject.multimodalPromptToChatMessages)(multimodalPrompt);
        msgs.push(...addOns);
    }
    const { content: rawResponse, usage, reasoning_content, rawChoiceMessage } = await (0, index_js_namespaceObject.callAI)(msgs, modelRuntime);
    let parseResult;
    try {
        parseResult = (0, extraction_js_namespaceObject.parseXMLExtractionResponse)(rawResponse);
    } catch (parseError) {
        const errorMessage = parseError instanceof Error ? parseError.message : String(parseError);
        throw new index_js_namespaceObject.AIResponseParseError(`XML parse error: ${errorMessage}`, rawResponse, usage, rawChoiceMessage);
    }
    return {
        parseResult,
        rawResponse,
        rawChoiceMessage,
        usage,
        reasoning_content
    };
}
async function AiJudgeOrderSensitive(description, modelRuntime) {
    const systemPrompt = (0, order_sensitive_judge_js_namespaceObject.systemPromptToJudgeOrderSensitive)();
    const userPrompt = (0, order_sensitive_judge_js_namespaceObject.orderSensitiveJudgePrompt)(description);
    const msgs = [
        {
            role: 'system',
            content: systemPrompt
        },
        {
            role: 'user',
            content: userPrompt
        }
    ];
    debugInspect("AiJudgeOrderSensitive: description=%s", description);
    const result = await (0, index_js_namespaceObject.callAIWithObjectResponse)(msgs, modelRuntime, {
        jsonParserSource: 'generic-object'
    });
    return {
        isOrderSensitive: result.content.isOrderSensitive ?? false,
        usage: result.usage
    };
}
exports.AiExtractElementInfo = __webpack_exports__.AiExtractElementInfo;
exports.AiJudgeOrderSensitive = __webpack_exports__.AiJudgeOrderSensitive;
exports.AiLocateElement = __webpack_exports__.AiLocateElement;
exports.AiLocateSection = __webpack_exports__.AiLocateSection;
exports.buildSearchAreaConfig = __webpack_exports__.buildSearchAreaConfig;
exports.extraTextFromUserPrompt = __webpack_exports__.extraTextFromUserPrompt;
exports.genericLocate = __webpack_exports__.genericLocate;
exports.promptsToChatParam = __webpack_exports__.promptsToChatParam;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AiExtractElementInfo",
    "AiJudgeOrderSensitive",
    "AiLocateElement",
    "AiLocateSection",
    "buildSearchAreaConfig",
    "extraTextFromUserPrompt",
    "genericLocate",
    "promptsToChatParam"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});

//# sourceMappingURL=inspect.js.map