"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__, {
    parseXMLPlanningResponse: ()=>parseXMLPlanningResponse,
    plan: ()=>plan
});
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 llm_planning_js_namespaceObject = require("./prompt/llm-planning.js");
const util_js_namespaceObject = require("./prompt/util.js");
const index_js_namespaceObject = require("./service-caller/index.js");
const debug = (0, logger_namespaceObject.getDebug)('planning');
const warnLog = (0, logger_namespaceObject.getDebug)('planning', {
    console: true
});
function parseXMLPlanningResponse(xmlString, modelFamily) {
    const thought = (0, util_js_namespaceObject.extractXMLTag)(xmlString, 'thought');
    const memory = (0, util_js_namespaceObject.extractXMLTag)(xmlString, 'memory');
    const log = (0, util_js_namespaceObject.extractXMLTag)(xmlString, 'log') || '';
    const error = (0, util_js_namespaceObject.extractXMLTag)(xmlString, 'error');
    const actionType = (0, util_js_namespaceObject.extractXMLTag)(xmlString, 'action-type');
    const actionParamStr = (0, util_js_namespaceObject.extractXMLTag)(xmlString, 'action-param-json');
    const completeGoalRegex = /<complete\s+success="(true|false)">([\s\S]*?)<\/complete>/i;
    const completeGoalMatch = xmlString.match(completeGoalRegex);
    let finalizeMessage;
    let finalizeSuccess;
    if (completeGoalMatch) {
        finalizeSuccess = 'true' === completeGoalMatch[1];
        finalizeMessage = completeGoalMatch[2]?.trim() || void 0;
    }
    let action = null;
    if (actionType && 'null' !== actionType.toLowerCase()) {
        const type = actionType.trim();
        let param;
        if (actionParamStr) try {
            param = (0, index_js_namespaceObject.safeParseJson)(actionParamStr, modelFamily);
        } catch (e) {
            throw new Error(`Failed to parse action-param-json: ${e}`);
        }
        action = {
            type,
            ...void 0 !== param ? {
                param
            } : {}
        };
    }
    return {
        ...thought ? {
            thought
        } : {},
        ...memory ? {
            memory
        } : {},
        log,
        ...error ? {
            error
        } : {},
        action,
        ...void 0 !== finalizeMessage ? {
            finalizeMessage
        } : {},
        ...void 0 !== finalizeSuccess ? {
            finalizeSuccess
        } : {}
    };
}
async function plan(userInstruction, opts) {
    const { context, modelConfig, conversationHistory } = opts;
    const { shotSize } = context;
    const screenshotBase64 = context.screenshot.base64;
    const { modelFamily } = modelConfig;
    const systemPrompt = await (0, llm_planning_js_namespaceObject.systemPromptToTaskPlanning)({
        actionSpace: opts.actionSpace,
        modelFamily,
        includeBbox: opts.includeBbox,
        includeThought: true,
        deepThink: true === opts.deepThink
    });
    let imagePayload = screenshotBase64;
    let imageWidth = shotSize.width;
    let imageHeight = shotSize.height;
    if ('qwen2.5-vl' === modelFamily) {
        const paddedResult = await (0, img_namespaceObject.paddingToMatchBlockByBase64)(imagePayload);
        imageWidth = paddedResult.width;
        imageHeight = paddedResult.height;
        imagePayload = paddedResult.imageBase64;
    }
    const actionContext = opts.actionContext ? `<high_priority_knowledge>${opts.actionContext}</high_priority_knowledge>\n` : '';
    const instruction = [
        {
            role: 'user',
            content: [
                {
                    type: 'text',
                    text: `${actionContext}<user_instruction>${userInstruction}</user_instruction>`
                }
            ]
        }
    ];
    let latestFeedbackMessage;
    const historicalLogsText = conversationHistory.historicalLogsToText();
    const historicalLogsSection = historicalLogsText ? `\n\n${historicalLogsText}` : '';
    const memoriesText = conversationHistory.memoriesToText();
    const memoriesSection = memoriesText ? `\n\n${memoriesText}` : '';
    if (conversationHistory.pendingFeedbackMessage) {
        latestFeedbackMessage = {
            role: 'user',
            content: [
                {
                    type: 'text',
                    text: `${conversationHistory.pendingFeedbackMessage}. The previous action has been executed, here is the latest screenshot. Please continue according to the instruction.${memoriesSection}${historicalLogsSection}`
                },
                {
                    type: 'image_url',
                    image_url: {
                        url: imagePayload,
                        detail: 'high'
                    }
                }
            ]
        };
        conversationHistory.resetPendingFeedbackMessageIfExists();
    } else latestFeedbackMessage = {
        role: 'user',
        content: [
            {
                type: 'text',
                text: `this is the latest screenshot${memoriesSection}${historicalLogsSection}`
            },
            {
                type: 'image_url',
                image_url: {
                    url: imagePayload,
                    detail: 'high'
                }
            }
        ]
    };
    conversationHistory.append(latestFeedbackMessage);
    conversationHistory.compressHistory(50, 20);
    const historyLog = conversationHistory.snapshot(opts.imagesIncludeCount);
    const msgs = [
        {
            role: 'system',
            content: systemPrompt
        },
        ...instruction,
        ...historyLog
    ];
    const { content: rawResponse, usage, reasoning_content } = await (0, index_js_namespaceObject.callAI)(msgs, modelConfig, {
        deepThink: 'unset' === opts.deepThink ? void 0 : opts.deepThink
    });
    let planFromAI;
    try {
        planFromAI = parseXMLPlanningResponse(rawResponse, modelFamily);
        if (planFromAI.action && void 0 !== planFromAI.finalizeSuccess) {
            warnLog('Planning response included both an action and <complete>; ignoring <complete> output.');
            planFromAI.finalizeMessage = void 0;
            planFromAI.finalizeSuccess = void 0;
        }
        const actions = planFromAI.action ? [
            planFromAI.action
        ] : [];
        let shouldContinuePlanning = true;
        if (void 0 !== planFromAI.finalizeSuccess) {
            debug('task completed via <complete> tag, stop planning');
            shouldContinuePlanning = false;
        }
        const returnValue = {
            ...planFromAI,
            actions,
            rawResponse,
            usage,
            reasoning_content,
            yamlFlow: (0, external_common_js_namespaceObject.buildYamlFlowFromPlans)(actions, opts.actionSpace),
            shouldContinuePlanning
        };
        (0, utils_namespaceObject.assert)(planFromAI, "can't get plans from AI");
        actions.forEach((action)=>{
            const type = action.type;
            const actionInActionSpace = opts.actionSpace.find((action)=>action.name === type);
            debug('actionInActionSpace matched', actionInActionSpace);
            const locateFields = actionInActionSpace ? (0, external_common_js_namespaceObject.findAllMidsceneLocatorField)(actionInActionSpace.paramSchema) : [];
            debug('locateFields', locateFields);
            locateFields.forEach((field)=>{
                const locateResult = action.param[field];
                if (locateResult && void 0 !== modelFamily) action.param[field] = (0, external_common_js_namespaceObject.fillBboxParam)(locateResult, imageWidth, imageHeight, modelFamily);
            });
        });
        if (planFromAI.log) conversationHistory.appendHistoricalLog(planFromAI.log);
        if (planFromAI.memory) conversationHistory.appendMemory(planFromAI.memory);
        conversationHistory.append({
            role: 'assistant',
            content: [
                {
                    type: 'text',
                    text: rawResponse
                }
            ]
        });
        return returnValue;
    } catch (parseError) {
        const errorMessage = parseError instanceof Error ? parseError.message : String(parseError);
        throw new index_js_namespaceObject.AIResponseParseError(`XML parse error: ${errorMessage}`, rawResponse, usage);
    }
}
exports.parseXMLPlanningResponse = __webpack_exports__.parseXMLPlanningResponse;
exports.plan = __webpack_exports__.plan;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "parseXMLPlanningResponse",
    "plan"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});

//# sourceMappingURL=llm-planning.js.map