"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__, {
    parseXMLExtractionResponse: ()=>parseXMLExtractionResponse,
    systemPromptToExtract: ()=>systemPromptToExtract,
    extractDataQueryPrompt: ()=>extractDataQueryPrompt
});
const env_namespaceObject = require("@midscene/shared/env");
const index_js_namespaceObject = require("../service-caller/index.js");
const external_util_js_namespaceObject = require("./util.js");
function parseXMLExtractionResponse(xmlString) {
    const thought = (0, external_util_js_namespaceObject.extractXMLTag)(xmlString, 'thought');
    const dataJsonStr = (0, external_util_js_namespaceObject.extractXMLTag)(xmlString, 'data-json');
    const errorsStr = (0, external_util_js_namespaceObject.extractXMLTag)(xmlString, 'errors');
    if (!dataJsonStr) throw new Error('Missing required field: data-json');
    let data;
    try {
        data = (0, index_js_namespaceObject.safeParseJson)(dataJsonStr, void 0);
    } catch (e) {
        throw new Error(`Failed to parse data-json: ${e}`);
    }
    let errors;
    if (errorsStr) try {
        const parsedErrors = (0, index_js_namespaceObject.safeParseJson)(errorsStr, void 0);
        if (Array.isArray(parsedErrors)) errors = parsedErrors;
    } catch (e) {}
    return {
        ...thought ? {
            thought
        } : {},
        data,
        ...errors && errors.length > 0 ? {
            errors
        } : {}
    };
}
function systemPromptToExtract() {
    const preferredLanguage = (0, env_namespaceObject.getPreferredLanguage)();
    return `
You are a versatile professional in software UI design and testing. Your outstanding contributions will impact the user experience of billions of users.

The user will give you a screenshot, the contents of it (optional), and some data requirements in <DATA_DEMAND>. You need to understand the user's requirements and extract the data satisfying the <DATA_DEMAND>.

If a key specifies a JSON data type (such as Number, String, Boolean, Object, Array), ensure the returned value strictly matches that data type.

If the user provides multiple reference images, please carefully review the reference images with the screenshot and provide the correct answer for <DATA_DEMAND>.


Return in the following XML format:
<thought>the thinking process of the extraction, less than 300 words. Use ${preferredLanguage} in this field.</thought>
<data-json>the extracted data as JSON. Make sure both the value and scheme meet the DATA_DEMAND. If you want to write some description in this field, use the same language as the DATA_DEMAND.</data-json>
<errors>optional error messages as JSON array, e.g., ["error1", "error2"]</errors>

# Example 1
For example, if the DATA_DEMAND is:

<DATA_DEMAND>
{
  "name": "name shows on the left panel, string",
  "age": "age shows on the right panel, number",
  "isAdmin": "if the user is admin, boolean"
}
</DATA_DEMAND>

By viewing the screenshot and page contents, you can extract the following data:

<thought>According to the screenshot, i can see ...</thought>
<data-json>
{
  "name": "John",
  "age": 30,
  "isAdmin": true
}
</data-json>

# Example 2
If the DATA_DEMAND is:

<DATA_DEMAND>
the todo items list, string[]
</DATA_DEMAND>

By viewing the screenshot and page contents, you can extract the following data:

<thought>According to the screenshot, i can see ...</thought>
<data-json>
["todo 1", "todo 2", "todo 3"]
</data-json>

# Example 3
If the DATA_DEMAND is:

<DATA_DEMAND>
the page title, string
</DATA_DEMAND>

By viewing the screenshot and page contents, you can extract the following data:

<thought>According to the screenshot, i can see ...</thought>
<data-json>
"todo list"
</data-json>

# Example 4
If the DATA_DEMAND is:

<DATA_DEMAND>
{
  "result": "Boolean, is it currently the SMS page?"
}
</DATA_DEMAND>

By viewing the screenshot and page contents, you can extract the following data:

<thought>According to the screenshot, i can see ...</thought>
<data-json>
{ "result": true }
</data-json>
`;
}
const extractDataQueryPrompt = (pageDescription, dataQuery)=>{
    let dataQueryText = '';
    dataQueryText = 'string' == typeof dataQuery ? dataQuery : JSON.stringify(dataQuery, null, 2);
    return `
<PageDescription>
${pageDescription}
</PageDescription>

<DATA_DEMAND>
${dataQueryText}
</DATA_DEMAND>
  `;
};
exports.extractDataQueryPrompt = __webpack_exports__.extractDataQueryPrompt;
exports.parseXMLExtractionResponse = __webpack_exports__.parseXMLExtractionResponse;
exports.systemPromptToExtract = __webpack_exports__.systemPromptToExtract;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "extractDataQueryPrompt",
    "parseXMLExtractionResponse",
    "systemPromptToExtract"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});

//# sourceMappingURL=extraction.js.map