"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__, {
    dumpMidsceneLocatorField: ()=>dumpMidsceneLocatorField,
    TUserPromptSchema: ()=>TUserPromptSchema,
    buildYamlFlowFromPlans: ()=>buildYamlFlowFromPlans,
    markupImageForLLM: ()=>markupImageForLLM,
    pointToBbox: ()=>pointToBbox,
    adaptGeminiBbox: ()=>adaptGeminiBbox,
    parseActionParam: ()=>parseActionParam,
    adaptBboxToRect: ()=>adaptBboxToRect,
    getReadableTimeString: ()=>getReadableTimeString,
    expandSearchArea: ()=>expandSearchArea,
    mergeRects: ()=>mergeRects,
    normalized01000: ()=>normalized01000,
    SizeSchema: ()=>SizeSchema,
    findAllMidsceneLocatorField: ()=>findAllMidsceneLocatorField,
    adaptQwen2_5Bbox: ()=>adaptQwen2_5Bbox,
    getMidsceneLocationSchema: ()=>getMidsceneLocationSchema,
    adaptDoubaoBbox: ()=>adaptDoubaoBbox,
    fillBboxParam: ()=>fillBboxParam,
    finalizeActionName: ()=>finalizeActionName,
    TMultimodalPromptSchema: ()=>TMultimodalPromptSchema,
    PointSchema: ()=>PointSchema,
    RectSchema: ()=>RectSchema,
    dumpActionParam: ()=>dumpActionParam,
    ifMidsceneLocatorField: ()=>ifMidsceneLocatorField,
    adaptBbox: ()=>adaptBbox
});
const utils_namespaceObject = require("@midscene/shared/utils");
const util_js_namespaceObject = require("./ai-model/auto-glm/util.js");
const constants_namespaceObject = require("@midscene/shared/constants");
const extractor_namespaceObject = require("@midscene/shared/extractor");
const img_namespaceObject = require("@midscene/shared/img");
const logger_namespaceObject = require("@midscene/shared/logger");
const external_zod_namespaceObject = require("zod");
const defaultBboxSize = 20;
const debugInspectUtils = (0, logger_namespaceObject.getDebug)('ai:common');
function pointToBbox(x, y, bboxSize = defaultBboxSize) {
    const halfSize = bboxSize / 2;
    const x1 = Math.max(x - halfSize, 0);
    const y1 = Math.max(y - halfSize, 0);
    const x2 = Math.min(x + halfSize, 1000);
    const y2 = Math.min(y + halfSize, 1000);
    return [
        x1,
        y1,
        x2,
        y2
    ];
}
function fillBboxParam(locate, width, height, modelFamily) {
    if (locate.bbox_2d && !locate?.bbox) {
        locate.bbox = locate.bbox_2d;
        delete locate.bbox_2d;
    }
    if (locate?.bbox) locate.bbox = adaptBbox(locate.bbox, width, height, modelFamily);
    return locate;
}
function adaptQwen2_5Bbox(bbox) {
    if (bbox.length < 2) {
        const msg = `invalid bbox data for qwen-vl mode: ${JSON.stringify(bbox)} `;
        throw new Error(msg);
    }
    const result = [
        Math.round(bbox[0]),
        Math.round(bbox[1]),
        'number' == typeof bbox[2] ? Math.round(bbox[2]) : Math.round(bbox[0] + defaultBboxSize),
        'number' == typeof bbox[3] ? Math.round(bbox[3]) : Math.round(bbox[1] + defaultBboxSize)
    ];
    return result;
}
function adaptDoubaoBbox(bbox, width, height) {
    (0, utils_namespaceObject.assert)(width > 0 && height > 0, 'width and height must be greater than 0 in doubao mode');
    if ('string' == typeof bbox) {
        (0, utils_namespaceObject.assert)(/^(\d+)\s(\d+)\s(\d+)\s(\d+)$/.test(bbox.trim()), `invalid bbox data string for doubao-vision mode: ${bbox}`);
        const splitted = bbox.split(' ');
        if (4 === splitted.length) return [
            Math.round(Number(splitted[0]) * width / 1000),
            Math.round(Number(splitted[1]) * height / 1000),
            Math.round(Number(splitted[2]) * width / 1000),
            Math.round(Number(splitted[3]) * height / 1000)
        ];
        throw new Error(`invalid bbox data string for doubao-vision mode: ${bbox}`);
    }
    let bboxList = [];
    if (Array.isArray(bbox) && 'string' == typeof bbox[0]) bbox.forEach((item)=>{
        if ('string' == typeof item && item.includes(',')) {
            const [x, y] = item.split(',');
            bboxList.push(Number(x.trim()), Number(y.trim()));
        } else if ('string' == typeof item && item.includes(' ')) {
            const [x, y] = item.split(' ');
            bboxList.push(Number(x.trim()), Number(y.trim()));
        } else bboxList.push(Number(item));
    });
    else bboxList = bbox;
    if (4 === bboxList.length || 5 === bboxList.length) return [
        Math.round(bboxList[0] * width / 1000),
        Math.round(bboxList[1] * height / 1000),
        Math.round(bboxList[2] * width / 1000),
        Math.round(bboxList[3] * height / 1000)
    ];
    if (6 === bboxList.length || 2 === bboxList.length || 3 === bboxList.length || 7 === bboxList.length) return [
        Math.max(0, Math.round(bboxList[0] * width / 1000) - defaultBboxSize / 2),
        Math.max(0, Math.round(bboxList[1] * height / 1000) - defaultBboxSize / 2),
        Math.min(width, Math.round(bboxList[0] * width / 1000) + defaultBboxSize / 2),
        Math.min(height, Math.round(bboxList[1] * height / 1000) + defaultBboxSize / 2)
    ];
    if (8 === bbox.length) return [
        Math.round(bboxList[0] * width / 1000),
        Math.round(bboxList[1] * height / 1000),
        Math.round(bboxList[4] * width / 1000),
        Math.round(bboxList[5] * height / 1000)
    ];
    const msg = `invalid bbox data for doubao-vision mode: ${JSON.stringify(bbox)} `;
    throw new Error(msg);
}
function normalizeBboxInput(bbox) {
    if (Array.isArray(bbox)) {
        if (Array.isArray(bbox[0])) return bbox[0];
    }
    return bbox;
}
function adaptBbox(bbox, width, height, modelFamily) {
    const normalizedBbox = normalizeBboxInput(bbox);
    let result = [
        0,
        0,
        0,
        0
    ];
    result = 'doubao-vision' === modelFamily || 'doubao-seed' === modelFamily || (0, util_js_namespaceObject.isUITars)(modelFamily) ? adaptDoubaoBbox(normalizedBbox, width, height) : 'gemini' === modelFamily ? adaptGeminiBbox(normalizedBbox, width, height) : 'qwen2.5-vl' === modelFamily ? adaptQwen2_5Bbox(normalizedBbox) : normalized01000(normalizedBbox, width, height);
    return result;
}
function normalized01000(bbox, width, height) {
    return [
        Math.round(bbox[0] * width / 1000),
        Math.round(bbox[1] * height / 1000),
        Math.round(bbox[2] * width / 1000),
        Math.round(bbox[3] * height / 1000)
    ];
}
function adaptGeminiBbox(bbox, width, height) {
    const left = Math.round(bbox[1] * width / 1000);
    const top = Math.round(bbox[0] * height / 1000);
    const right = Math.round(bbox[3] * width / 1000);
    const bottom = Math.round(bbox[2] * height / 1000);
    return [
        left,
        top,
        right,
        bottom
    ];
}
function adaptBboxToRect(bbox, width, height, offsetX = 0, offsetY = 0, rightLimit = width, bottomLimit = height, modelFamily, scale = 1) {
    debugInspectUtils('adaptBboxToRect', bbox, width, height, 'offset', offsetX, offsetY, 'limit', rightLimit, bottomLimit, 'modelFamily', modelFamily, 'scale', scale);
    const [left, top, right, bottom] = adaptBbox(bbox, width, height, modelFamily);
    const rectLeft = Math.max(0, left);
    const rectTop = Math.max(0, top);
    const boundedRight = Math.min(right, rightLimit);
    const boundedBottom = Math.min(bottom, bottomLimit);
    const rectWidth = boundedRight - rectLeft + 1;
    const rectHeight = boundedBottom - rectTop + 1;
    const finalLeft = 1 !== scale ? Math.round(rectLeft / scale) : rectLeft;
    const finalTop = 1 !== scale ? Math.round(rectTop / scale) : rectTop;
    const finalWidth = 1 !== scale ? Math.round(rectWidth / scale) : rectWidth;
    const finalHeight = 1 !== scale ? Math.round(rectHeight / scale) : rectHeight;
    const rect = {
        left: finalLeft + offsetX,
        top: finalTop + offsetY,
        width: finalWidth,
        height: finalHeight
    };
    debugInspectUtils('adaptBboxToRect, result=', rect);
    return rect;
}
function mergeRects(rects) {
    const minLeft = Math.min(...rects.map((r)=>r.left));
    const minTop = Math.min(...rects.map((r)=>r.top));
    const maxRight = Math.max(...rects.map((r)=>r.left + r.width));
    const maxBottom = Math.max(...rects.map((r)=>r.top + r.height));
    return {
        left: minLeft,
        top: minTop,
        width: maxRight - minLeft,
        height: maxBottom - minTop
    };
}
function expandSearchArea(rect, screenSize) {
    const minArea = 160000;
    const expandSize = 100;
    const expandedLeft = Math.max(rect.left - expandSize, 0);
    const expandedTop = Math.max(rect.top - expandSize, 0);
    const expandRect = {
        left: expandedLeft,
        top: expandedTop,
        width: Math.min(rect.left - expandedLeft + rect.width + expandSize, screenSize.width - expandedLeft),
        height: Math.min(rect.top - expandedTop + rect.height + expandSize, screenSize.height - expandedTop)
    };
    const currentArea = expandRect.width * expandRect.height;
    if (currentArea >= minArea) return expandRect;
    const centerX = expandRect.left + expandRect.width / 2;
    const centerY = expandRect.top + expandRect.height / 2;
    const scaleFactor = Math.sqrt(minArea / currentArea);
    const newWidth = Math.round(expandRect.width * scaleFactor);
    const newHeight = Math.round(expandRect.height * scaleFactor);
    const newLeft = Math.round(centerX - newWidth / 2);
    const newTop = Math.round(centerY - newHeight / 2);
    const left = Math.max(newLeft, 0);
    const top = Math.max(newTop, 0);
    return {
        left,
        top,
        width: Math.min(newWidth, screenSize.width - left),
        height: Math.min(newHeight, screenSize.height - top)
    };
}
async function markupImageForLLM(screenshotBase64, tree, size) {
    const elementsInfo = (0, extractor_namespaceObject.treeToList)(tree);
    const elementsPositionInfoWithoutText = elementsInfo.filter((elementInfo)=>{
        if (elementInfo.attributes.nodeType === constants_namespaceObject.NodeType.TEXT) return false;
        return true;
    });
    const imagePayload = await (0, img_namespaceObject.compositeElementInfoImg)({
        inputImgBase64: screenshotBase64,
        elementsPositionInfo: elementsPositionInfoWithoutText,
        size
    });
    return imagePayload;
}
function buildYamlFlowFromPlans(plans, actionSpace) {
    const flow = [];
    for (const plan of plans){
        const verb = plan.type;
        const action = actionSpace.find((action)=>action.name === verb);
        if (!action) {
            console.warn(`Cannot convert action ${verb} to yaml flow. Will ignore it.`);
            continue;
        }
        const flowKey = action.interfaceAlias || verb;
        const flowParam = action.paramSchema ? dumpActionParam(plan.param || {}, action.paramSchema) : {};
        const flowItem = {
            [flowKey]: '',
            ...flowParam
        };
        flow.push(flowItem);
    }
    return flow;
}
const PointSchema = external_zod_namespaceObject.z.object({
    left: external_zod_namespaceObject.z.number(),
    top: external_zod_namespaceObject.z.number()
});
const SizeSchema = external_zod_namespaceObject.z.object({
    width: external_zod_namespaceObject.z.number(),
    height: external_zod_namespaceObject.z.number()
});
const RectSchema = PointSchema.and(SizeSchema).and(external_zod_namespaceObject.z.object({
    zoom: external_zod_namespaceObject.z.number().optional()
}));
const TMultimodalPromptSchema = external_zod_namespaceObject.z.object({
    images: external_zod_namespaceObject.z.array(external_zod_namespaceObject.z.object({
        name: external_zod_namespaceObject.z.string(),
        url: external_zod_namespaceObject.z.string()
    })).optional(),
    convertHttpImage2Base64: external_zod_namespaceObject.z.boolean().optional()
});
const TUserPromptSchema = external_zod_namespaceObject.z.union([
    external_zod_namespaceObject.z.string(),
    external_zod_namespaceObject.z.object({
        prompt: external_zod_namespaceObject.z.string()
    }).and(TMultimodalPromptSchema.partial())
]);
const locateFieldFlagName = 'midscene_location_field_flag';
const MidsceneLocationInput = external_zod_namespaceObject.z.object({
    prompt: TUserPromptSchema,
    deepThink: external_zod_namespaceObject.z.boolean().optional(),
    cacheable: external_zod_namespaceObject.z.boolean().optional(),
    xpath: external_zod_namespaceObject.z.union([
        external_zod_namespaceObject.z.string(),
        external_zod_namespaceObject.z.boolean()
    ]).optional()
}).passthrough();
const getMidsceneLocationSchema = ()=>MidsceneLocationInput;
const ifMidsceneLocatorField = (field)=>{
    let actualField = field;
    if (actualField._def?.typeName === 'ZodOptional') actualField = actualField._def.innerType;
    if (actualField._def?.typeName === 'ZodObject') {
        const shape = actualField._def.shape();
        if (locateFieldFlagName in shape) return true;
        if ('prompt' in shape && shape.prompt) return true;
    }
    return false;
};
const dumpMidsceneLocatorField = (field)=>{
    (0, utils_namespaceObject.assert)(ifMidsceneLocatorField(field), 'field is not a midscene locator field');
    if ('string' == typeof field) return field;
    if (field && 'object' == typeof field && field.prompt) {
        if ('string' == typeof field.prompt) return field.prompt;
        if ('object' == typeof field.prompt && field.prompt.prompt) return field.prompt.prompt;
    }
    return String(field);
};
const findAllMidsceneLocatorField = (zodType, requiredOnly)=>{
    if (!zodType) return [];
    const zodObject = zodType;
    if (zodObject._def?.typeName === 'ZodObject' && zodObject.shape) {
        const keys = Object.keys(zodObject.shape);
        return keys.filter((key)=>{
            const field = zodObject.shape[key];
            if (!ifMidsceneLocatorField(field)) return false;
            if (requiredOnly) return field._def?.typeName !== 'ZodOptional';
            return true;
        });
    }
    return [];
};
const dumpActionParam = (jsonObject, zodSchema)=>{
    if (!(0, utils_namespaceObject.isPlainObject)(jsonObject)) return {};
    const locatorFields = findAllMidsceneLocatorField(zodSchema);
    const result = {
        ...jsonObject
    };
    for (const fieldName of locatorFields){
        const fieldValue = result[fieldName];
        if (fieldValue) {
            if ('string' == typeof fieldValue) result[fieldName] = fieldValue;
            else if ('object' == typeof fieldValue) {
                if (fieldValue.prompt) {
                    if ('string' == typeof fieldValue.prompt) result[fieldName] = fieldValue.prompt;
                    else if ('object' == typeof fieldValue.prompt && fieldValue.prompt.prompt) result[fieldName] = fieldValue.prompt.prompt;
                }
            }
        }
    }
    return result;
};
const parseActionParam = (rawParam, zodSchema)=>{
    if (!zodSchema) return;
    const param = rawParam ?? {};
    const locateFields = findAllMidsceneLocatorField(zodSchema);
    if (0 === locateFields.length) return zodSchema.parse(param);
    const locateFieldValues = {};
    for (const fieldName of locateFields)if (fieldName in param) locateFieldValues[fieldName] = param[fieldName];
    const paramsForValidation = {};
    for(const key in param)if (locateFields.includes(key)) paramsForValidation[key] = {
        prompt: '_dummy_'
    };
    else paramsForValidation[key] = param[key];
    const validated = zodSchema.parse(paramsForValidation);
    for(const fieldName in locateFieldValues)validated[fieldName] = locateFieldValues[fieldName];
    return validated;
};
const finalizeActionName = 'Finalize';
const getReadableTimeString = (format = 'YYYY-MM-DD HH:mm:ss', timestamp)=>{
    const now = void 0 !== timestamp ? new Date(timestamp) : new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
    const timeString = format.replace('YYYY', String(year)).replace('MM', month).replace('DD', day).replace('HH', hours).replace('mm', minutes).replace('ss', seconds);
    return `${timeString} (${format})`;
};
exports.PointSchema = __webpack_exports__.PointSchema;
exports.RectSchema = __webpack_exports__.RectSchema;
exports.SizeSchema = __webpack_exports__.SizeSchema;
exports.TMultimodalPromptSchema = __webpack_exports__.TMultimodalPromptSchema;
exports.TUserPromptSchema = __webpack_exports__.TUserPromptSchema;
exports.adaptBbox = __webpack_exports__.adaptBbox;
exports.adaptBboxToRect = __webpack_exports__.adaptBboxToRect;
exports.adaptDoubaoBbox = __webpack_exports__.adaptDoubaoBbox;
exports.adaptGeminiBbox = __webpack_exports__.adaptGeminiBbox;
exports.adaptQwen2_5Bbox = __webpack_exports__.adaptQwen2_5Bbox;
exports.buildYamlFlowFromPlans = __webpack_exports__.buildYamlFlowFromPlans;
exports.dumpActionParam = __webpack_exports__.dumpActionParam;
exports.dumpMidsceneLocatorField = __webpack_exports__.dumpMidsceneLocatorField;
exports.expandSearchArea = __webpack_exports__.expandSearchArea;
exports.fillBboxParam = __webpack_exports__.fillBboxParam;
exports.finalizeActionName = __webpack_exports__.finalizeActionName;
exports.findAllMidsceneLocatorField = __webpack_exports__.findAllMidsceneLocatorField;
exports.getMidsceneLocationSchema = __webpack_exports__.getMidsceneLocationSchema;
exports.getReadableTimeString = __webpack_exports__.getReadableTimeString;
exports.ifMidsceneLocatorField = __webpack_exports__.ifMidsceneLocatorField;
exports.markupImageForLLM = __webpack_exports__.markupImageForLLM;
exports.mergeRects = __webpack_exports__.mergeRects;
exports.normalized01000 = __webpack_exports__.normalized01000;
exports.parseActionParam = __webpack_exports__.parseActionParam;
exports.pointToBbox = __webpack_exports__.pointToBbox;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "PointSchema",
    "RectSchema",
    "SizeSchema",
    "TMultimodalPromptSchema",
    "TUserPromptSchema",
    "adaptBbox",
    "adaptBboxToRect",
    "adaptDoubaoBbox",
    "adaptGeminiBbox",
    "adaptQwen2_5Bbox",
    "buildYamlFlowFromPlans",
    "dumpActionParam",
    "dumpMidsceneLocatorField",
    "expandSearchArea",
    "fillBboxParam",
    "finalizeActionName",
    "findAllMidsceneLocatorField",
    "getMidsceneLocationSchema",
    "getReadableTimeString",
    "ifMidsceneLocatorField",
    "markupImageForLLM",
    "mergeRects",
    "normalized01000",
    "parseActionParam",
    "pointToBbox"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});

//# sourceMappingURL=common.js.map