"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__, {
    autoGLMPlanning: ()=>autoGLMPlanning
});
const logger_namespaceObject = require("@midscene/shared/logger");
const index_js_namespaceObject = require("../service-caller/index.js");
const external_actions_js_namespaceObject = require("./actions.js");
const external_parser_js_namespaceObject = require("./parser.js");
const external_prompt_js_namespaceObject = require("./prompt.js");
const debug = (0, logger_namespaceObject.getDebug)('auto-glm-planning');
async function autoGLMPlanning(userInstruction, options) {
    const { conversationHistory, context, modelConfig, actionContext } = options;
    const systemPrompt = (0, external_prompt_js_namespaceObject.getAutoGLMPlanPrompt)(modelConfig.modelFamily) + (actionContext ? `<high_priority_knowledge>${actionContext}</high_priority_knowledge>` : '');
    const imagePayloadBase64 = context.screenshot.base64;
    conversationHistory.append({
        role: 'user',
        content: [
            {
                type: 'text',
                text: userInstruction
            }
        ]
    });
    conversationHistory.append({
        role: 'user',
        content: [
            {
                type: 'image_url',
                image_url: {
                    url: imagePayloadBase64
                }
            }
        ]
    });
    const msgs = [
        {
            role: 'system',
            content: systemPrompt
        },
        ...conversationHistory.snapshot(1)
    ];
    const { content: rawResponse, usage } = await (0, index_js_namespaceObject.callAIWithStringResponse)(msgs, modelConfig);
    debug('autoGLMPlanning rawResponse:', rawResponse);
    let parsedResponse;
    let transformedActions;
    try {
        parsedResponse = (0, external_parser_js_namespaceObject.parseAutoGLMResponse)(rawResponse);
        debug('thinking in response:', parsedResponse.think);
        debug('action in response:', parsedResponse.content);
        const parsedAction = (0, external_parser_js_namespaceObject.parseAction)(parsedResponse);
        debug('Parsed action object:', parsedAction);
        transformedActions = (0, external_actions_js_namespaceObject.transformAutoGLMAction)(parsedAction, context.shotSize);
        debug('Transformed actions:', transformedActions);
    } catch (parseError) {
        const errorMessage = parseError instanceof Error ? parseError.message : String(parseError);
        throw new index_js_namespaceObject.AIResponseParseError(`Parse error: ${errorMessage}`, JSON.stringify(rawResponse, void 0, 2), usage);
    }
    conversationHistory.append({
        role: 'assistant',
        content: `<think>${parsedResponse.think}</think><answer>${parsedResponse.content}</answer>`
    });
    const shouldContinuePlanning = !parsedResponse.content.startsWith('finish(');
    return {
        actions: transformedActions,
        log: rawResponse,
        usage,
        shouldContinuePlanning,
        rawResponse: JSON.stringify(rawResponse, void 0, 2)
    };
}
exports.autoGLMPlanning = __webpack_exports__.autoGLMPlanning;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "autoGLMPlanning"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});

//# sourceMappingURL=planning.js.map