"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }



























var _chunkUXPZNWPHjs = require('./chunk-UXPZNWPH.js');



var _chunkLQ2VYIYDjs = require('./chunk-LQ2VYIYD.js');

// ../../node_modules/.pnpm/lex@1.7.9/node_modules/lex/lexer.js
var require_lexer = _chunkLQ2VYIYDjs.__commonJS.call(void 0, {
  "../../node_modules/.pnpm/lex@1.7.9/node_modules/lex/lexer.js"(exports, module) {
    "use strict";
    if (typeof module === "object" && typeof module.exports === "object")
      module.exports = Lexer;
    Lexer.defunct = function(chr) {
      throw new Error("Unexpected character at index " + (this.index - 1) + ": " + chr);
    };
    function Lexer(defunct) {
      if (typeof defunct !== "function")
        defunct = Lexer.defunct;
      var tokens = [];
      var rules = [];
      var remove = 0;
      this.state = 0;
      this.index = 0;
      this.input = "";
      this.addRule = function(pattern, action, start) {
        var global = pattern.global;
        if (!global) {
          var flags = "g";
          if (pattern.multiline)
            flags += "m";
          if (pattern.ignoreCase)
            flags += "i";
          pattern = new RegExp(pattern.source, flags);
        }
        if (Object.prototype.toString.call(start) !== "[object Array]")
          start = [0];
        rules.push({
          pattern,
          global,
          action,
          start
        });
        return this;
      };
      this.setInput = function(input) {
        remove = 0;
        this.state = 0;
        this.index = 0;
        tokens.length = 0;
        this.input = input;
        return this;
      };
      this.lex = function() {
        if (tokens.length)
          return tokens.shift();
        this.reject = true;
        while (this.index <= this.input.length) {
          var matches = scan.call(this).splice(remove);
          var index = this.index;
          while (matches.length) {
            if (this.reject) {
              var match = matches.shift();
              var result = match.result;
              var length = match.length;
              this.index += length;
              this.reject = false;
              remove++;
              var token = match.action.apply(this, result);
              if (this.reject)
                this.index = result.index;
              else if (typeof token !== "undefined") {
                switch (Object.prototype.toString.call(token)) {
                  case "[object Array]":
                    tokens = token.slice(1);
                    token = token[0];
                  default:
                    if (length)
                      remove = 0;
                    return token;
                }
              }
            } else
              break;
          }
          var input = this.input;
          if (index < input.length) {
            if (this.reject) {
              remove = 0;
              var token = defunct.call(this, input.charAt(this.index++));
              if (typeof token !== "undefined") {
                if (Object.prototype.toString.call(token) === "[object Array]") {
                  tokens = token.slice(1);
                  return token[0];
                } else
                  return token;
              }
            } else {
              if (this.index !== index)
                remove = 0;
              this.reject = true;
            }
          } else if (matches.length)
            this.reject = true;
          else
            break;
        }
      };
      function scan() {
        var matches = [];
        var index = 0;
        var state = this.state;
        var lastIndex = this.index;
        var input = this.input;
        for (var i = 0, length = rules.length; i < length; i++) {
          var rule = rules[i];
          var start = rule.start;
          var states = start.length;
          if (!states || start.indexOf(state) >= 0 || state % 2 && states === 1 && !start[0]) {
            var pattern = rule.pattern;
            pattern.lastIndex = lastIndex;
            var result = pattern.exec(input);
            if (result && result.index === lastIndex) {
              var j = matches.push({
                result,
                action: rule.action,
                length: result[0].length
              });
              if (rule.global)
                index = j;
              while (--j > index) {
                var k = j - 1;
                if (matches[j].length > matches[k].length) {
                  var temple = matches[j];
                  matches[j] = matches[k];
                  matches[k] = temple;
                }
              }
            }
          }
        }
        return matches;
      }
    }
  }
});

// ../../node_modules/.pnpm/string.fromcodepoint@0.2.1/node_modules/string.fromcodepoint/fromcodepoint.js
var require_fromcodepoint = _chunkLQ2VYIYDjs.__commonJS.call(void 0, {
  "../../node_modules/.pnpm/string.fromcodepoint@0.2.1/node_modules/string.fromcodepoint/fromcodepoint.js"() {
    "use strict";
    if (!String.fromCodePoint) {
      (function() {
        var defineProperty = function() {
          try {
            var object = {};
            var $defineProperty = Object.defineProperty;
            var result = $defineProperty(object, object, object) && $defineProperty;
          } catch (error) {
          }
          return result;
        }();
        var stringFromCharCode = String.fromCharCode;
        var floor = Math.floor;
        var fromCodePoint = function(_) {
          var MAX_SIZE = 16384;
          var codeUnits = [];
          var highSurrogate;
          var lowSurrogate;
          var index = -1;
          var length = arguments.length;
          if (!length) {
            return "";
          }
          var result = "";
          while (++index < length) {
            var codePoint = Number(arguments[index]);
            if (!isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
            codePoint < 0 || // not a valid Unicode code point
            codePoint > 1114111 || // not a valid Unicode code point
            floor(codePoint) != codePoint) {
              throw RangeError("Invalid code point: " + codePoint);
            }
            if (codePoint <= 65535) {
              codeUnits.push(codePoint);
            } else {
              codePoint -= 65536;
              highSurrogate = (codePoint >> 10) + 55296;
              lowSurrogate = codePoint % 1024 + 56320;
              codeUnits.push(highSurrogate, lowSurrogate);
            }
            if (index + 1 == length || codeUnits.length > MAX_SIZE) {
              result += stringFromCharCode.apply(null, codeUnits);
              codeUnits.length = 0;
            }
          }
          return result;
        };
        if (defineProperty) {
          defineProperty(String, "fromCodePoint", {
            "value": fromCodePoint,
            "configurable": true,
            "writable": true
          });
        } else {
          String.fromCodePoint = fromCodePoint;
        }
      })();
    }
  }
});

// ../../node_modules/.pnpm/unescape-js@1.1.4/node_modules/unescape-js/dist/index.js
var require_dist = _chunkLQ2VYIYDjs.__commonJS.call(void 0, {
  "../../node_modules/.pnpm/unescape-js@1.1.4/node_modules/unescape-js/dist/index.js"(exports, module) {
    "use strict";
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = void 0;
    require_fromcodepoint();
    var jsEscapeRegex = /\\(u\{([0-9A-Fa-f]+)\}|u([0-9A-Fa-f]{4})|x([0-9A-Fa-f]{2})|([1-7][0-7]{0,2}|[0-7]{2,3})|(['"tbrnfv0\\]))|\\U([0-9A-Fa-f]{8})/g;
    var usualEscapeSequences = {
      "0": "\0",
      "b": "\b",
      "f": "\f",
      "n": "\n",
      "r": "\r",
      "t": "	",
      "v": "\v",
      "'": "'",
      '"': '"',
      "\\": "\\"
    };
    var fromHex = function fromHex2(str) {
      return String.fromCodePoint(parseInt(str, 16));
    };
    var fromOct = function fromOct2(str) {
      return String.fromCodePoint(parseInt(str, 8));
    };
    var _default = function _default2(string) {
      return string.replace(jsEscapeRegex, function(_, __, varHex, longHex, shortHex, octal, specialCharacter, python) {
        if (varHex !== void 0) {
          return fromHex(varHex);
        } else if (longHex !== void 0) {
          return fromHex(longHex);
        } else if (shortHex !== void 0) {
          return fromHex(shortHex);
        } else if (octal !== void 0) {
          return fromOct(octal);
        } else if (python !== void 0) {
          return fromHex(python);
        } else {
          return usualEscapeSequences[specialCharacter];
        }
      });
    };
    exports.default = _default;
    module.exports = exports.default;
  }
});

// ../../node_modules/.pnpm/utf8@3.0.0/node_modules/utf8/utf8.js
var require_utf8 = _chunkLQ2VYIYDjs.__commonJS.call(void 0, {
  "../../node_modules/.pnpm/utf8@3.0.0/node_modules/utf8/utf8.js"(exports) {
    "use strict";
    (function(root) {
      var stringFromCharCode = String.fromCharCode;
      function ucs2decode(string) {
        var output = [];
        var counter = 0;
        var length = string.length;
        var value;
        var extra;
        while (counter < length) {
          value = string.charCodeAt(counter++);
          if (value >= 55296 && value <= 56319 && counter < length) {
            extra = string.charCodeAt(counter++);
            if ((extra & 64512) == 56320) {
              output.push(((value & 1023) << 10) + (extra & 1023) + 65536);
            } else {
              output.push(value);
              counter--;
            }
          } else {
            output.push(value);
          }
        }
        return output;
      }
      function ucs2encode(array) {
        var length = array.length;
        var index = -1;
        var value;
        var output = "";
        while (++index < length) {
          value = array[index];
          if (value > 65535) {
            value -= 65536;
            output += stringFromCharCode(value >>> 10 & 1023 | 55296);
            value = 56320 | value & 1023;
          }
          output += stringFromCharCode(value);
        }
        return output;
      }
      function checkScalarValue(codePoint) {
        if (codePoint >= 55296 && codePoint <= 57343) {
          throw Error(
            "Lone surrogate U+" + codePoint.toString(16).toUpperCase() + " is not a scalar value"
          );
        }
      }
      function createByte(codePoint, shift) {
        return stringFromCharCode(codePoint >> shift & 63 | 128);
      }
      function encodeCodePoint(codePoint) {
        if ((codePoint & 4294967168) == 0) {
          return stringFromCharCode(codePoint);
        }
        var symbol = "";
        if ((codePoint & 4294965248) == 0) {
          symbol = stringFromCharCode(codePoint >> 6 & 31 | 192);
        } else if ((codePoint & 4294901760) == 0) {
          checkScalarValue(codePoint);
          symbol = stringFromCharCode(codePoint >> 12 & 15 | 224);
          symbol += createByte(codePoint, 6);
        } else if ((codePoint & 4292870144) == 0) {
          symbol = stringFromCharCode(codePoint >> 18 & 7 | 240);
          symbol += createByte(codePoint, 12);
          symbol += createByte(codePoint, 6);
        }
        symbol += stringFromCharCode(codePoint & 63 | 128);
        return symbol;
      }
      function utf8encode(string) {
        var codePoints = ucs2decode(string);
        var length = codePoints.length;
        var index = -1;
        var codePoint;
        var byteString = "";
        while (++index < length) {
          codePoint = codePoints[index];
          byteString += encodeCodePoint(codePoint);
        }
        return byteString;
      }
      function readContinuationByte() {
        if (byteIndex >= byteCount) {
          throw Error("Invalid byte index");
        }
        var continuationByte = byteArray[byteIndex] & 255;
        byteIndex++;
        if ((continuationByte & 192) == 128) {
          return continuationByte & 63;
        }
        throw Error("Invalid continuation byte");
      }
      function decodeSymbol() {
        var byte1;
        var byte2;
        var byte3;
        var byte4;
        var codePoint;
        if (byteIndex > byteCount) {
          throw Error("Invalid byte index");
        }
        if (byteIndex == byteCount) {
          return false;
        }
        byte1 = byteArray[byteIndex] & 255;
        byteIndex++;
        if ((byte1 & 128) == 0) {
          return byte1;
        }
        if ((byte1 & 224) == 192) {
          byte2 = readContinuationByte();
          codePoint = (byte1 & 31) << 6 | byte2;
          if (codePoint >= 128) {
            return codePoint;
          } else {
            throw Error("Invalid continuation byte");
          }
        }
        if ((byte1 & 240) == 224) {
          byte2 = readContinuationByte();
          byte3 = readContinuationByte();
          codePoint = (byte1 & 15) << 12 | byte2 << 6 | byte3;
          if (codePoint >= 2048) {
            checkScalarValue(codePoint);
            return codePoint;
          } else {
            throw Error("Invalid continuation byte");
          }
        }
        if ((byte1 & 248) == 240) {
          byte2 = readContinuationByte();
          byte3 = readContinuationByte();
          byte4 = readContinuationByte();
          codePoint = (byte1 & 7) << 18 | byte2 << 12 | byte3 << 6 | byte4;
          if (codePoint >= 65536 && codePoint <= 1114111) {
            return codePoint;
          }
        }
        throw Error("Invalid UTF-8 detected");
      }
      var byteArray;
      var byteCount;
      var byteIndex;
      function utf8decode(byteString) {
        byteArray = ucs2decode(byteString);
        byteCount = byteArray.length;
        byteIndex = 0;
        var codePoints = [];
        var tmp;
        while ((tmp = decodeSymbol()) !== false) {
          codePoints.push(tmp);
        }
        return ucs2encode(codePoints);
      }
      root.version = "3.0.0";
      root.encode = utf8encode;
      root.decode = utf8decode;
    })(typeof exports === "undefined" ? exports.utf8 = {} : exports);
  }
});

// ../../node_modules/.pnpm/dirty-json@0.9.2/node_modules/dirty-json/lexer.js
var require_lexer2 = _chunkLQ2VYIYDjs.__commonJS.call(void 0, {
  "../../node_modules/.pnpm/dirty-json@0.9.2/node_modules/dirty-json/lexer.js"(exports, module) {
    "use strict";
    var Lexer = require_lexer();
    var unescapeJs = require_dist();
    var utf8 = require_utf8();
    var LEX_FLOAT = 6;
    var LEX_INT = 7;
    var LEX_QUOTE = 11;
    var LEX_RB = 12;
    var LEX_RCB = 13;
    var LEX_TOKEN = 14;
    var LEX_COLON = -1;
    var LEX_COMMA = -2;
    var LEX_LCB = -3;
    var LEX_LB = -4;
    var LEX_DOT = -5;
    var lexSpc = [
      [/\s*:\s*/, LEX_COLON],
      [/\s*,\s*/, LEX_COMMA],
      [/\s*{\s*/, LEX_LCB],
      [/\s*}\s*/, LEX_RCB],
      [/\s*\[\s*/, LEX_LB],
      [/\s*\]\s*/, LEX_RB],
      [/\s*\.\s*/, LEX_DOT]
      // TODO: remove?
    ];
    function parseString(str) {
      str = str.replace(/\\\//, "/");
      return unescapeJs(str);
    }
    function getLexer(string) {
      let lexer = new Lexer();
      let col = 0;
      let row = 0;
      lexer.addRule(/"((?:\\.|[^"])*?)($|")/, (lexeme, txt) => {
        col += lexeme.length;
        return { type: LEX_QUOTE, value: parseString(txt), row, col, single: false };
      });
      lexer.addRule(/'((?:\\.|[^'])*?)($|'|(",?[ \t]*\n))/, (lexeme, txt) => {
        col += lexeme.length;
        return { type: LEX_QUOTE, value: parseString(txt), row, col, single: true };
      });
      lexer.addRule(/[\-0-9]*\.[0-9]*([eE][\+\-]?)?[0-9]*(?:\s*)/, (lexeme) => {
        col += lexeme.length;
        return { type: LEX_FLOAT, value: parseFloat(lexeme), row, col };
      });
      lexer.addRule(/\-?[0-9]+([eE][\+\-]?)[0-9]*(?:\s*)/, (lexeme) => {
        col += lexeme.length;
        return { type: LEX_FLOAT, value: parseFloat(lexeme), row, col };
      });
      lexer.addRule(/\-?[0-9]+(?:\s*)/, (lexeme) => {
        col += lexeme.length;
        return { type: LEX_INT, value: parseInt(lexeme), row, col };
      });
      lexSpc.forEach((item) => {
        lexer.addRule(item[0], (lexeme) => {
          col += lexeme.length;
          return { type: item[1], value: lexeme, row, col };
        });
      });
      lexer.addRule(/\s/, (lexeme) => {
        if (lexeme == "\n") {
          col = 0;
          row++;
        } else {
          col += lexeme.length;
        }
      });
      lexer.addRule(/\S[ \t]*/, (lexeme) => {
        col += lexeme.length;
        let lt = LEX_TOKEN;
        let val = lexeme;
        return { type: lt, value: val, row, col };
      });
      lexer.setInput(string);
      return lexer;
    }
    module.exports.lexString = lexString;
    function lexString(str, emit) {
      let lex = getLexer(str);
      let token = "";
      while (token = lex.lex()) {
        emit(token);
      }
    }
    module.exports.getAllTokens = getAllTokens;
    function getAllTokens(str) {
      let arr = [];
      let emit = function(i) {
        arr.push(i);
      };
      lexString(str, emit);
      return arr;
    }
  }
});

// ../../node_modules/.pnpm/dirty-json@0.9.2/node_modules/dirty-json/parser.js
var require_parser = _chunkLQ2VYIYDjs.__commonJS.call(void 0, {
  "../../node_modules/.pnpm/dirty-json@0.9.2/node_modules/dirty-json/parser.js"(exports, module) {
    "use strict";
    var lexer = require_lexer2();
    var LEX_KV = 0;
    var LEX_KVLIST = 1;
    var LEX_VLIST = 2;
    var LEX_BOOLEAN = 3;
    var LEX_COVALUE = 4;
    var LEX_CVALUE = 5;
    var LEX_FLOAT = 6;
    var LEX_INT = 7;
    var LEX_KEY = 8;
    var LEX_LIST = 9;
    var LEX_OBJ = 10;
    var LEX_QUOTE = 11;
    var LEX_RB = 12;
    var LEX_RCB = 13;
    var LEX_TOKEN = 14;
    var LEX_VALUE = 15;
    var LEX_COLON = -1;
    var LEX_COMMA = -2;
    var LEX_LCB = -3;
    var LEX_LB = -4;
    function extendArray(arr) {
      if (arr.peek == null) {
        Object.defineProperty(arr, "peek", {
          enumerable: false,
          value: function() {
            return this[this.length - 1];
          }
        });
      }
      if (arr.last == null) {
        Object.defineProperty(arr, "last", {
          enumerable: false,
          value: function(i) {
            return this[this.length - (1 + i)];
          }
        });
      }
    }
    function is(obj, prop) {
      return obj && obj.hasOwnProperty("type") && obj.type == prop;
    }
    function log(str) {
    }
    module.exports.parse = parse;
    function parse(text, dupKeys) {
      let stack = [];
      let tokens = [];
      extendArray(stack);
      extendArray(tokens);
      let emit = function(t) {
        tokens.push(t);
      };
      lexer.lexString(text, emit);
      if (tokens[0].type == LEX_LB && tokens.last(0).type != LEX_RB) {
        tokens.push({ type: LEX_RB, value: "]", row: -1, col: -1 });
      }
      if (tokens[0].type == LEX_LCB && tokens.last(0).type != LEX_RCB) {
        tokens.push({ type: LEX_RCB, value: "}", row: -1, col: -1 });
      }
      for (let i = 0; i < tokens.length; i++) {
        log("Shifting " + tokens[i].type);
        stack.push(tokens[i]);
        log(stack);
        log("Reducing...");
        while (reduce(stack)) {
          log(stack);
          log("Reducing...");
        }
      }
      if (stack.length == 1 && stack[0].type == LEX_KVLIST) {
        log("Pre-compile error fix 1");
        stack = [{ type: LEX_OBJ, value: stack[0].value }];
      }
      return compileOST(stack[0], dupKeys);
    }
    function reduce(stack) {
      let next = stack.pop();
      switch (next.type) {
        case LEX_KEY:
          if (next.value.trim() == "true") {
            log("Rule 5");
            stack.push({ "type": LEX_BOOLEAN, "value": "true" });
            return true;
          }
          if (next.value.trim() == "false") {
            log("Rule 6");
            stack.push({ "type": LEX_BOOLEAN, "value": "false" });
            return true;
          }
          if (next.value.trim() == "null") {
            log("Rule 7");
            stack.push({ "type": LEX_VALUE, "value": null });
            return true;
          }
          break;
        case LEX_TOKEN:
          if (is(stack.peek(), LEX_KEY)) {
            log("Rule 11a");
            stack.peek().value += next.value;
            return true;
          }
          log("Rule 11c");
          stack.push({ type: LEX_KEY, value: next.value });
          return true;
        case LEX_INT:
          if (is(next, LEX_INT) && is(stack.peek(), LEX_KEY)) {
            log("Rule 11b");
            stack.peek().value += next.value;
            return true;
          }
          log("Rule 11f");
          next.type = LEX_VALUE;
          stack.push(next);
          return true;
        case LEX_QUOTE:
          log("Rule 11d");
          next.type = LEX_VALUE;
          next.value = next.value;
          stack.push(next);
          return true;
        case LEX_BOOLEAN:
          log("Rule 11e");
          next.type = LEX_VALUE;
          if (next.value == "true") {
            next.value = true;
          } else {
            next.value = false;
          }
          stack.push(next);
          return true;
        case LEX_FLOAT:
          log("Rule 11g");
          next.type = LEX_VALUE;
          stack.push(next);
          return true;
        case LEX_VALUE:
          if (is(stack.peek(), LEX_COMMA)) {
            log("Rule 12");
            next.type = LEX_CVALUE;
            stack.pop();
            stack.push(next);
            return true;
          }
          if (is(stack.peek(), LEX_COLON)) {
            log("Rule 13");
            next.type = LEX_COVALUE;
            stack.pop();
            stack.push(next);
            return true;
          }
          if (is(stack.peek(), LEX_KEY) && is(stack.last(1), LEX_VALUE)) {
            log("Error rule 1");
            let middleVal = stack.pop();
            stack.peek().value += '"' + middleVal.value + '"';
            stack.peek().value += next.value;
            return true;
          }
          if (is(stack.peek(), LEX_KEY) && is(stack.last(1), LEX_VLIST)) {
            log("Error rule 2");
            let middleVal = stack.pop();
            let oldLastVal = stack.peek().value.pop();
            oldLastVal += '"' + middleVal.value + '"';
            oldLastVal += next.value;
            stack.peek().value.push(oldLastVal);
            return true;
          }
          if (is(stack.peek(), LEX_KEY) && is(stack.last(1), LEX_KVLIST)) {
            log("Error rule 3");
            let middleVal = stack.pop();
            let oldLastVal = stack.peek().value.pop();
            const qChar = next.single ? "'" : '"';
            oldLastVal.value += qChar + middleVal.value + qChar;
            oldLastVal.value += next.value;
            stack.peek().value.push(oldLastVal);
            return true;
          }
          if (is(stack.peek(), LEX_KEY)) {
            log("Error rule 4");
            let keyValue = stack.pop().value;
            next.value = keyValue + next.value;
            stack.push(next);
            return true;
          }
          break;
        case LEX_LIST:
          if (is(next, LEX_LIST) && is(stack.peek(), LEX_COMMA)) {
            log("Rule 12a");
            next.type = LEX_CVALUE;
            stack.pop();
            stack.push(next);
            return true;
          }
          if (is(stack.peek(), LEX_COLON)) {
            log("Rule 13a");
            next.type = LEX_COVALUE;
            stack.pop();
            stack.push(next);
            return true;
          }
          break;
        case LEX_OBJ:
          if (is(stack.peek(), LEX_COMMA)) {
            log("Rule 12b");
            let toPush = { "type": LEX_CVALUE, "value": next };
            stack.pop();
            stack.push(toPush);
            return true;
          }
          if (is(stack.peek(), LEX_COLON)) {
            log("Rule 13b");
            let toPush = { "type": LEX_COVALUE, "value": next };
            stack.pop();
            stack.push(toPush);
            return true;
          }
          if (is(stack.peek(), LEX_KEY)) {
            log("Error rule 9");
            let key = stack.pop();
            stack.push({ "type": LEX_KV, "key": key.value.trim(), "value": next });
            return true;
          }
          break;
        case LEX_CVALUE:
          if (is(stack.peek(), LEX_VLIST)) {
            log("Rule 14");
            stack.peek().value.push(next.value);
            return true;
          }
          log("Rule 15");
          stack.push({ "type": LEX_VLIST, "value": [next.value] });
          return true;
        case LEX_VLIST:
          if (is(stack.peek(), LEX_VALUE)) {
            log("Rule 15a");
            next.value.unshift(stack.peek().value);
            stack.pop();
            stack.push(next);
            return true;
          }
          if (is(stack.peek(), LEX_LIST)) {
            log("Rule 15b");
            next.value.unshift(stack.peek().value);
            stack.pop();
            stack.push(next);
            return true;
          }
          if (is(stack.peek(), LEX_OBJ)) {
            log("Rule 15c");
            next.value.unshift(stack.peek());
            stack.pop();
            stack.push(next);
            return true;
          }
          if (is(stack.peek(), LEX_KEY) && (stack.last(1), LEX_COMMA)) {
            log("Error rule 7");
            let l = stack.pop();
            stack.push({ type: LEX_VALUE, "value": l.value });
            log("Start subreduce... (" + l.value + ")");
            while (reduce(stack))
              ;
            log("End subreduce");
            stack.push(next);
            return true;
          }
          if (is(stack.peek(), LEX_VLIST)) {
            log("Error rule 8");
            stack.peek().value.push(next.value[0]);
            return true;
          }
          break;
        case LEX_COVALUE:
          if (is(stack.peek(), LEX_KEY) || is(stack.peek(), LEX_VALUE) || is(stack.peek(), LEX_VLIST)) {
            log("Rule 16");
            let key = stack.pop();
            stack.push({ "type": LEX_KV, "key": key.value, "value": next.value });
            return true;
          }
          throw new Error("Got a :value that can't be handled at line " + next.row + ":" + next.col);
        case LEX_KV:
          if (is(stack.last(0), LEX_COMMA) && is(stack.last(1), LEX_KVLIST)) {
            log("Rule 17");
            stack.last(1).value.push(next);
            stack.pop();
            return true;
          }
          log("Rule 18");
          stack.push({ "type": LEX_KVLIST, "value": [next] });
          return true;
        case LEX_KVLIST:
          if (is(stack.peek(), LEX_KVLIST)) {
            log("Rule 17a");
            next.value.forEach(function(i) {
              stack.peek().value.push(i);
            });
            return true;
          }
          break;
        case LEX_RB:
          if (is(stack.peek(), LEX_VLIST) && is(stack.last(1), LEX_LB)) {
            log("Rule 19");
            let l = stack.pop();
            stack.pop();
            stack.push({ "type": LEX_LIST, "value": l.value });
            return true;
          }
          if (is(stack.peek(), LEX_LIST) && is(stack.last(1), LEX_LB)) {
            log("Rule 19b");
            let l = stack.pop();
            stack.pop();
            stack.push({ "type": LEX_LIST, "value": [l.value] });
            return true;
          }
          if (is(stack.peek(), LEX_LB)) {
            log("Rule 22");
            stack.pop();
            stack.push({ type: LEX_LIST, "value": [] });
            return true;
          }
          if (is(stack.peek(), LEX_VALUE) && is(stack.last(1), LEX_LB)) {
            log("Rule 23");
            let val = stack.pop().value;
            stack.pop();
            stack.push({ type: LEX_LIST, "value": [val] });
            return true;
          }
          if (is(stack.peek(), LEX_OBJ) && is(stack.last(1), LEX_LB)) {
            log("Rule 23b");
            let val = stack.pop();
            stack.pop();
            stack.push({ type: LEX_LIST, "value": [val] });
            return true;
          }
          if (is(stack.peek(), LEX_KEY) && is(stack.last(1), LEX_COMMA)) {
            log("Error rule 5");
            let l = stack.pop();
            stack.push({ type: LEX_VALUE, "value": l.value });
            log("Start subreduce... (" + l.value + ")");
            while (reduce(stack))
              ;
            log("End subreduce");
            stack.push({ type: LEX_RB });
            return true;
          }
          if (is(stack.peek(), LEX_COMMA) && (is(stack.last(1), LEX_KEY) || is(stack.last(1), LEX_OBJ) || is(stack.last(1), LEX_VALUE))) {
            log("Error rule 5a");
            stack.pop();
            stack.push({ type: LEX_RB, "value": "]" });
            log("Start subreduce...");
            log("Content: " + JSON.stringify(stack));
            while (reduce(stack))
              ;
            log("End subreduce");
            return true;
          }
          if (is(stack.peek(), LEX_KEY) && is(stack.last(1), LEX_LB)) {
            log("Error rule 5b");
            let v = stack.pop();
            stack.pop();
            stack.push({ type: LEX_LIST, value: [v.value] });
            return true;
          }
          if (is(stack.peek(), LEX_COMMA) && is(stack.last(1), LEX_VLIST)) {
            log("Error rule 5c");
            stack.pop();
            stack.push({ type: LEX_RB });
            log("Start subreduce...");
            log("Content: " + JSON.stringify(stack));
            while (reduce(stack))
              ;
            log("End subreduce");
            return true;
          }
          break;
        case LEX_RCB:
          if (is(stack.peek(), LEX_KVLIST) && is(stack.last(1), LEX_LCB)) {
            log("Rule 20");
            let l = stack.pop();
            stack.pop();
            stack.push({ "type": LEX_OBJ, "value": l.value });
            return true;
          }
          if (is(stack.peek(), LEX_LCB)) {
            log("Rule 21");
            stack.pop();
            stack.push({ type: LEX_OBJ, "value": null });
            return true;
          }
          if (is(stack.peek(), LEX_KEY) && is(stack.last(1), LEX_COLON)) {
            log("Error rule 4a");
            let l = stack.pop();
            stack.push({ type: LEX_VALUE, "value": l.value });
            log("Start subreduce... (" + l.value + ")");
            while (reduce(stack))
              ;
            log("End subreduce");
            stack.push({ type: LEX_RCB });
            return true;
          }
          if (is(stack.peek(), LEX_COLON)) {
            log("Error rule 4b");
            stack.push({ type: LEX_VALUE, value: null });
            log("Starting subreduce...");
            while (reduce(stack))
              ;
            log("End subreduce.");
            stack.push({ type: LEX_RCB });
            return true;
          }
          if (is(stack.peek(), LEX_COMMA)) {
            log("Error rule 10a");
            stack.pop();
            stack.push({ type: LEX_RCB });
            return true;
          }
          throw new Error("Found } that I can't handle at line " + next.row + ":" + next.col);
        case LEX_COMMA:
          if (is(stack.peek(), LEX_COMMA)) {
            log("Comma error rule 1");
            return true;
          }
          if (is(stack.peek(), LEX_KEY)) {
            log("Comma error rule 2");
            const key = stack.pop();
            stack.push({ type: LEX_VALUE, value: key.value });
            log("Starting subreduce...");
            while (reduce(stack))
              ;
            log("End subreduce.");
            stack.push(next);
            return true;
          }
          if (is(stack.peek(), LEX_COLON)) {
            log("Comma error rule 3");
            stack.push({ type: LEX_VALUE, value: null });
            log("Starting subreduce...");
            while (reduce(stack))
              ;
            log("End subreduce.");
            stack.push(next);
            return true;
          }
      }
      stack.push(next);
      return false;
    }
    function compileOST(tree, dupKeys) {
      let rawTypes = ["boolean", "number", "string"];
      if (rawTypes.indexOf(typeof tree) != -1)
        return tree;
      if (tree === null)
        return null;
      if (Array.isArray(tree)) {
        let toR = [];
        while (tree.length > 0)
          toR.unshift(compileOST(tree.pop()));
        return toR;
      }
      if (is(tree, LEX_OBJ)) {
        let toR = {};
        if (tree.value === null)
          return {};
        tree.value.forEach(function(i) {
          const key = i.key;
          const val = compileOST(i.value);
          if (dupKeys && key in toR) {
            toR[key] = {
              "value": toR[key],
              "next": val
            };
          } else {
            toR[key] = val;
          }
        });
        return toR;
      }
      if (is(tree, LEX_LIST)) {
        return compileOST(tree.value);
      }
      return tree.value;
    }
  }
});

// ../../node_modules/.pnpm/dirty-json@0.9.2/node_modules/dirty-json/dirty-json.js
var require_dirty_json = _chunkLQ2VYIYDjs.__commonJS.call(void 0, {
  "../../node_modules/.pnpm/dirty-json@0.9.2/node_modules/dirty-json/dirty-json.js"(exports, module) {
    "use strict";
    var parser = require_parser();
    module.exports.parse = parse;
    function parse(text, config) {
      let fallback = true;
      let duplicateKeys = false;
      if (config) {
        if ("fallback" in config && config[fallback] === false) {
          fallback = false;
        }
        duplicateKeys = "duplicateKeys" in config && config["duplicateKeys"] === true;
      }
      try {
        return parser.parse(text, duplicateKeys);
      } catch (e) {
        if (fallback === false) {
          throw e;
        }
        try {
          let json = JSON.parse(text);
          console.warn("dirty-json got valid JSON that failed with the custom parser. We're returning the valid JSON, but please file a bug report here: https://github.com/RyanMarcus/dirty-json/issues  -- the JSON that caused the failure was: " + text);
          return json;
        } catch (json_error) {
          throw e;
        }
      }
    }
  }
});

// src/ai-model/service-caller/index.ts
var _assert = require('assert'); var _assert2 = _interopRequireDefault(_assert);

// src/types.ts
var BaseElement = class {
};
var AIResponseFormat = /* @__PURE__ */ ((AIResponseFormat2) => {
  AIResponseFormat2["JSON"] = "json_object";
  AIResponseFormat2["TEXT"] = "text";
  return AIResponseFormat2;
})(AIResponseFormat || {});
var UIContext = class {
};

// src/ai-model/service-caller/index.ts
var import_dirty_json = _chunkLQ2VYIYDjs.__toESM.call(void 0, require_dirty_json());
var _utils = require('@aico-test/shared/utils');
var _sdk = require('@anthropic-ai/sdk');



var _identity = require('@azure/identity');
var _openai = require('openai'); var _openai2 = _interopRequireDefault(_openai);
var _socksproxyagent = require('socks-proxy-agent');
var _httpsproxyagent = require('https-proxy-agent');

// src/ai-model/common.ts

async function callAiFn(msgs, AIActionTypeValue) {
  _assert2.default.call(void 0, 
    checkAIConfig(),
    "Cannot find config for AI model service. If you are using a self-hosted model without validating the API key, please set `OPENAI_API_KEY` to any non-null value. https://midscenejs.com/model-provider.html"
  );
  const { content, usage } = await callToGetJSONObject(
    msgs,
    AIActionTypeValue
  );
  return { content, usage };
}
function fillLocateParam(locate) {
  if ((locate == null ? void 0 : locate.bbox_2d) && !(locate == null ? void 0 : locate.bbox)) {
    locate.bbox = locate.bbox_2d;
    delete locate.bbox_2d;
  }
  const defaultBboxSize = 10;
  if (locate == null ? void 0 : locate.bbox) {
    locate.bbox[0] = Math.round(locate.bbox[0]);
    locate.bbox[1] = Math.round(locate.bbox[1]);
    locate.bbox[2] = typeof locate.bbox[2] === "number" ? Math.round(locate.bbox[2]) : Math.round(locate.bbox[0] + defaultBboxSize);
    locate.bbox[3] = typeof locate.bbox[3] === "number" ? Math.round(locate.bbox[3]) : Math.round(locate.bbox[1] + defaultBboxSize);
  }
  return locate;
}
var warned = false;
function warnGPT4oSizeLimit(size) {
  var _a;
  if (warned)
    return;
  if ((_a = getModelName()) == null ? void 0 : _a.toLowerCase().includes("gpt-4o")) {
    const warningMsg = `GPT-4o has a maximum image input size of 2000x768 or 768x2000, but got ${size.width}x${size.height}. Please set your page to a smaller resolution. Otherwise, the result may be inaccurate.`;
    if (Math.max(size.width, size.height) > 2e3 || Math.min(size.width, size.height) > 768) {
      console.warn(warningMsg);
      warned = true;
    }
  }
}

// src/ai-model/prompt/ui-tars-planning.ts
function getTimeZoneInfo() {
  const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
  const offset = -(/* @__PURE__ */ new Date()).getTimezoneOffset() / 60;
  return {
    timezone: `UTC${offset >= 0 ? "+" : ""}${offset}`,
    isChina: timeZone === "Asia/Shanghai"
  };
}
var language = getTimeZoneInfo().isChina ? "Chinese" : "English";
var uiTarsPlanningPrompt = `
You are a GUI agent. You are given a task and your action history, with screenshots. You need to perform the next action to complete the task. 

## Output Format
\`\`\`
Thought: ...
Action: ...
\`\`\`

## Action Space
click(start_box='[x1, y1, x2, y2]')
left_double(start_box='[x1, y1, x2, y2]')
right_single(start_box='[x1, y1, x2, y2]')
drag(start_box='[x1, y1, x2, y2]', end_box='[x3, y3, x4, y4]')
hotkey(key='')
type(content='') #If you want to submit your input, use "\\n" at the end of \`content\`.
scroll(start_box='[x1, y1, x2, y2]', direction='down or up or right or left')
wait() #Sleep for 5s and take a screenshot to check for any changes.
finished()
call_user() # Submit the task and call the user when the task is unsolvable, or when you need the user's help.

## Note
- Use ${language} in \`Thought\` part.
- Write a small plan and finally summarize your next action (with its target element) in one sentence in \`Thought\` part.

## User Instruction
`;
var getSummary = (prediction) => prediction.replace(/Reflection:[\s\S]*?(?=Action_Summary:|Action:|$)/g, "").trim();

// src/ai-model/prompt/assertion.ts
var language2 = getTimeZoneInfo().isChina ? "Chinese" : "English";
var defaultAssertionPrompt = "You are a senior testing engineer. User will give an assertion and a screenshot of a page. By carefully viewing the screenshot, please tell whether the assertion is truthy.";
var defaultAssertionResponseJsonFormat = `Return in the following JSON format:
{
  pass: boolean, // whether the assertion is truthy
  thought: string | null, // string, if the result is falsy, give the reason why it is falsy. Otherwise, put null.
}`;
var uiTarsAssertionResponseJsonFormat = `## Output Json String Format
\`\`\`
"{
  "pass": <<is a boolean value from the enum [true, false], true means the assertion is truthy>>, 
  "thought": "<<is a string, give the reason why the assertion is falsy or truthy. Otherwise.>>"
}"
\`\`\`

## Rules **MUST** follow
- Make sure to return **only** the JSON, with **no additional** text or explanations.
- Use ${language2} in \`thought\` part.
- You **MUST** strictly follow up the **Output Json String Format**.`;
function systemPromptToAssert(model) {
  return `${defaultAssertionPrompt}

${model.isUITars ? uiTarsAssertionResponseJsonFormat : defaultAssertionResponseJsonFormat}`;
}
var assertSchema = {
  type: "json_schema",
  json_schema: {
    name: "assert",
    strict: true,
    schema: {
      type: "object",
      properties: {
        pass: {
          type: "boolean",
          description: "Whether the assertion passed or failed"
        },
        thought: {
          type: ["string", "null"],
          description: "The thought process behind the assertion"
        }
      },
      required: ["pass", "thought"],
      additionalProperties: false
    }
  }
};

// src/ai-model/prompt/llm-locator.ts
var _prompts = require('@langchain/core/prompts');
function systemPromptToLocateElement() {
  if (_chunkUXPZNWPHjs.getAIConfigInBoolean.call(void 0, _chunkUXPZNWPHjs.MATCH_BY_POSITION)) {
    return `
## Role:
You are an expert in software testing.

## Objective:
- Identify elements in screenshots and text that match the user's description.
- Give the coordinates of the element that matches the user's description best in the screenshot.

## Output Format:
\`\`\`json
{
  "bbox": [number, number, number, number], 
  "errors"?: string[]
}
\`\`\`

Fields:
* \`bbox\` is the bounding box of the element that matches the user's description best in the screenshot
* \`errors\` is an optional array of error messages (if any)
`;
  }
  return `
## Role:
You are an expert in software page image (2D) and page element text analysis.

## Objective:
- Identify elements in screenshots and text that match the user's description.
- Return JSON data containing the selection reason and element ID.

## Skills:
- Image analysis and recognition
- Multilingual text understanding
- Software UI design and testing

## Workflow:
1. Receive the user's element description, screenshot, and element description information. Note that the text may contain non-English characters (e.g., Chinese), indicating that the application may be non-English.
2. Based on the user's description, locate the target element ID in the list of element descriptions and the screenshot.
3. Found the required number of elements
4. Return JSON data containing the selection reason and element ID.

## Constraints:
- Strictly adhere to the specified location when describing the required element; do not select elements from other locations.
- Elements in the image with NodeType other than "TEXT Node" have been highlighted to identify the element among multiple non-text elements.
- Accurately identify element information based on the user's description and return the corresponding element ID from the element description information, not extracted from the image.
- If no elements are found, the "elements" array should be empty.
- The returned data must conform to the specified JSON format.
- The returned value id information must use the id from element info (important: **use id not indexId, id is hash content**)

## Output Format:

Please return the result in JSON format as follows:

\`\`\`json
{
  "elements": [
    // If no matching elements are found, return an empty array []
    {
      "reason": "PLACEHOLDER", // The thought process for finding the element, replace PLACEHOLDER with your thought process
      "text": "PLACEHOLDER", // Replace PLACEHOLDER with the text of elementInfo, if none, leave empty
      "id": "PLACEHOLDER" // Replace PLACEHOLDER with the ID (important: **use id not indexId, id is hash content**) of elementInfo
    }
    // More elements...
  ],
  "errors": [] // Array of strings containing any error messages
}
\`\`\`

## Example:
Example 1:
Input Example:
\`\`\`json
// Description: "Shopping cart icon in the upper right corner"
{
  "description": "PLACEHOLDER", // Description of the target element
  "screenshot": "path/screenshot.png",
  "text": '{
      "pageSize": {
        "width": 400, // Width of the page
        "height": 905 // Height of the page
      },
      "elementInfos": [
        {
          "id": "1231", // ID of the element
          "indexId": "0", // Index of the element，The image is labeled to the left of the element
          "attributes": { // Attributes of the element
            "nodeType": "IMG Node", // Type of element, types include: TEXT Node, IMG Node, BUTTON Node, INPUT Node
            "src": "https://ap-southeast-3.m",
            "class": ".img"
          },
          "content": "", // Text content of the element
          "rect": {
            "left": 280, // Distance from the left side of the page
            "top": 8, // Distance from the top of the page
            "width": 44, // Width of the element
            "height": 44 // Height of the element
          }
        },
        {
          "id": "66551", // ID of the element
          "indexId": "1", // Index of the element,The image is labeled to the left of the element
          "attributes": { // Attributes of the element
            "nodeType": "IMG Node", // Type of element, types include: TEXT Node, IMG Node, BUTTON Node, INPUT Node
            "src": "data:image/png;base64,iVBORw0KGgoAAAANSU...",
            "class": ".icon"
          },
          "content": "", // Text content of the element
          "rect": {
            "left": 350, // Distance from the left side of the page
            "top": 16, // Distance from the top of the page
            "width": 25, // Width of the element
            "height": 25 // Height of the element
          }
        },
        ...
        {
          "id": "12344",
          "indexId": "2", // Index of the element，The image is labeled to the left of the element
          "attributes": {
            "nodeType": "TEXT Node",
            "class": ".product-name"
          },
          "center": [
            288,
            834
          ],
          "content": "Mango Drink",
          "rect": {
            "left": 188,
            "top": 827,
            "width": 199,
            "height": 13
          }
        },
        ...
      ]
    }
  '
}
\`\`\`
Output Example:
\`\`\`json
{
  "elements": [
    {
      // Describe the reason for finding this element, replace with actual value in practice
      "reason": "Reason for finding element 4: It is located in the upper right corner, is an image type, and according to the screenshot, it is a shopping cart icon button",
      "text": "",
      // ID(**use id not indexId**) of this element, replace with actual value in practice, **use id not indexId**
      "id": "1231"
    }
  ],
  "errors": []
}
\`\`\`
  
  `;
}
var locatorSchema = {
  type: "json_schema",
  json_schema: {
    name: "find_elements",
    strict: true,
    schema: {
      type: "object",
      properties: {
        elements: {
          type: "array",
          items: {
            type: "object",
            properties: {
              reason: {
                type: "string",
                description: "Reason for finding this element"
              },
              text: {
                type: "string",
                description: "Text content of the element"
              },
              id: {
                type: "string",
                description: "ID of this element"
              }
            },
            required: ["reason", "text", "id"],
            additionalProperties: false
          },
          description: "List of found elements"
        },
        errors: {
          type: "array",
          items: {
            type: "string"
          },
          description: "List of error messages, if any"
        }
      },
      required: ["elements", "errors"],
      additionalProperties: false
    }
  }
};
var findElementPrompt = new (0, _prompts.PromptTemplate)({
  template: `
Here is the item user want to find:
=====================================
{targetElementDescription}
=====================================

{pageDescription}
  `,
  inputVariables: ["pageDescription", "targetElementDescription"]
});

// src/ai-model/prompt/llm-planning.ts


// src/ai-model/prompt/util.ts


// src/image/index.ts








var _img = require('@aico-test/shared/img');

// src/ai-model/prompt/util.ts
var _constants = require('@aico-test/shared/constants');
var _extractor = require('@aico-test/shared/extractor');

function describeSize(size) {
  return `${size.width} x ${size.height}`;
}
function elementByPositionWithElementInfo(treeRoot, position) {
  _assert2.default.call(void 0, typeof position !== "undefined", "position is required for query");
  const matchingElements = [];
  function dfs(node) {
    if (node == null ? void 0 : node.node) {
      const item = node.node;
      if (item.rect.left <= position.x && position.x <= item.rect.left + item.rect.width && item.rect.top <= position.y && position.y <= item.rect.top + item.rect.height) {
        matchingElements.push(item);
      }
    }
    for (const child of node.children) {
      dfs(child);
    }
  }
  dfs(treeRoot);
  if (matchingElements.length === 0) {
    return void 0;
  }
  const element = matchingElements.reduce((smallest, current) => {
    const smallestArea = smallest.rect.width * smallest.rect.height;
    const currentArea = current.rect.width * current.rect.height;
    return currentArea < smallestArea ? current : smallest;
  });
  const distanceToCenter = distance(
    { x: element.center[0], y: element.center[1] },
    position
  );
  return distanceToCenter <= distanceThreshold ? element : void 0;
}
var distanceThreshold = 16;
function distance(point1, point2) {
  return Math.sqrt((point1.x - point2.x) ** 2 + (point1.y - point2.y) ** 2);
}
var samplePageDescription = `
And the page is described as follows:
====================
The size of the page: 1280 x 720
Some of the elements are marked with a rectangle in the screenshot corresponding to the markerId, some are not.

Description of all the elements in screenshot:
<div id="969f1637" markerId="1" left="100" top="100" width="100" height="100"> // The markerId indicated by the rectangle label in the screenshot
  <h4 id="b211ecb2" markerId="5" left="150" top="150" width="90" height="60">
    The username is accepted
  </h4>
  ...many more
</div>
====================
`;
async function describeUserPage(context, opt) {
  const { screenshotBase64 } = context;
  let width;
  let height;
  if (context.size) {
    ({ width, height } = context.size);
  } else {
    const imgSize = await _img.imageInfoOfBase64.call(void 0, screenshotBase64);
    ({ width, height } = imgSize);
  }
  const treeRoot = context.tree;
  const idElementMap = {};
  const flatElements = _extractor.treeToList.call(void 0, treeRoot);
  flatElements.forEach((element) => {
    idElementMap[element.id] = element;
    if (typeof element.indexId !== "undefined") {
      idElementMap[`${element.indexId}`] = element;
    }
  });
  const contentTree = await _extractor.descriptionOfTree.call(void 0, 
    treeRoot,
    opt == null ? void 0 : opt.truncateTextLength,
    opt == null ? void 0 : opt.filterNonTextContent
  );
  const pageJSONDescription = _chunkUXPZNWPHjs.getAIConfigInBoolean.call(void 0, _chunkUXPZNWPHjs.MATCH_BY_POSITION) ? "" : `Some of the elements are marked with a rectangle in the screenshot, some are not. 
 The page elements tree:
${contentTree}`;
  const sizeDescription = describeSize({ width, height });
  return {
    description: `The size of the page: ${sizeDescription} 
 ${pageJSONDescription}`,
    elementById(id) {
      _assert2.default.call(void 0, typeof id !== "undefined", "id is required for query");
      const item = idElementMap[`${id}`];
      return item;
    },
    elementByPosition(position, size) {
      return elementByPositionWithElementInfo(treeRoot, position);
    },
    insertElementByPosition(position) {
      const rect = {
        left: Math.max(position.x - 4, 0),
        top: Math.max(position.y - 4, 0),
        width: 8,
        height: 8
      };
      const id = _utils.generateHashId.call(void 0, rect);
      const element = {
        id,
        attributes: { nodeType: _constants.NodeType.POSITION },
        rect,
        content: "",
        center: [position.x, position.y]
      };
      treeRoot.children.push({
        node: element,
        children: []
      });
      flatElements.push(element);
      idElementMap[id] = element;
      return element;
    },
    size: { width, height }
  };
}

// src/ai-model/prompt/llm-planning.ts
var commonOutputFields = `"log": string, // Log what this action(s) you just planned do. Use the same language as the user's instruction.
  "more_actions_needed_by_instruction": boolean, // If all the actions described in the instruction have been covered by this action and logs, set this field to true.
  "error"?: string // Error messages about unexpected situations, if any. Use the same language as the user's instruction.`;
var qwenLocateParam = "locate: {bbox_2d: [number, number, number, number], prompt: string }";
var systemTemplateOfQwen = `
Target: User will give you a screenshot, an instruction and some previous logs indicating what have been done. Please tell what the NEXT action is to do the tasks the instruction requires.
Restriction:
- Don't give extra actions or plans beyond the instruction. ONLY plan for what the instruction requires. For example, don't try to submit the form if the instruction is only to fill something.
- Don't repeat actions in the previous logs.

Supporting actions:
- Tap: { type: "Tap", ${qwenLocateParam} }
- Hover: { type: "Hover", ${qwenLocateParam} }
- Input: { type: "Input", ${qwenLocateParam}, param: { value: string } } // \`value\` is the final that should be filled in the input box. No matter what modifications are required, just provide the final value to replace the existing input value. 
- KeyboardPress: { type: "KeyboardPress", param: { value: string } }
- Scroll: { type: "Scroll", ${qwenLocateParam} | null, param: { direction: 'down'(default) | 'up' | 'right' | 'left', scrollType: 'once' (default) | 'untilBottom' | 'untilTop' | 'untilRight' | 'untilLeft', distance: null | number }} // locate is the element to scroll. If it's a page scroll, put \`null\` in the \`locate\` field.
- ExpectedFalsyCondition: { type: "ExpectedFalsyCondition", param: {reason: string} } // Use this action when the conditional statement talked about in the instruction is falsy.

Field description:
* The \`prompt\` field inside the \`locate\` field is a short description that could be used to locate the element.

Return in JSON format:
{
  "action": 
    {
      // one of the supporting actions
    },
  ,
  "sleep"?: number, // The sleep time after the action, in milliseconds.
  ${commonOutputFields}
}
`;
var llmLocateParam = `locate: {{"id": string, "prompt": string}} | null`;
var systemTemplateOfLLM = `
## Role

You are a versatile professional in software UI automation. Your outstanding contributions will impact the user experience of billions of users.

## Objective

- Decompose the instruction user asked into a series of actions
- Locate the target element if possible
- If the instruction cannot be accomplished, give a further plan.

## Workflow

1. Receive the screenshot, element description of screenshot(if any), user's instruction and previous logs.
2. Decompose the user's task into a sequence of actions, and place it in the \`actions\` field. There are different types of actions (Tap / Hover / Input / KeyboardPress / Scroll / FalsyConditionStatement / Sleep). The "About the action" section below will give you more details.
3. Precisely locate the target element if it's already shown in the screenshot, put the location info in the \`locate\` field of the action.
4. If some target elements is not shown in the screenshot, consider the user's instruction is not feasible on this page. Follow the next steps.
5. Consider whether the user's instruction will be accomplished after all the actions
 - If yes, set \`taskWillBeAccomplished\` to true
 - If no, don't plan more actions by closing the array. Get ready to reevaluate the task. Some talent people like you will handle this. Give him a clear description of what have been done and what to do next. Put your new plan in the \`furtherPlan\` field. The "How to compose the \`taskWillBeAccomplished\` and \`furtherPlan\` fields" section will give you more details.

## Constraints

- All the actions you composed MUST be based on the page context information you get.
- Trust the "What have been done" field about the task (if any), don't repeat actions in it.
- Respond only with valid JSON. Do not write an introduction or summary or markdown prefix like \`\`\`json\`\`\`.
- If the screenshot and the instruction are totally irrelevant, set reason in the \`error\` field.

## About the \`actions\` field

The \`locate\` param is commonly used in the \`param\` field of the action, means to locate the target element to perform the action, it conforms to the following scheme:

type LocateParam = {{
  "id": string, // the id of the element found. It should either be the id marked with a rectangle in the screenshot or the id described in the description.
  "prompt"?: string // the description of the element to find. It can only be omitted when locate is null.
}} | null // If it's not on the page, the LocateParam should be null

## Supported actions

Each action has a \`type\` and corresponding \`param\`. To be detailed:
- type: 'Tap'
  * {{ ${llmLocateParam} }}
- type: 'Hover'
  * {{ ${llmLocateParam} }}
- type: 'Input', replace the value in the input field
  * {{ ${llmLocateParam}, param: {{ value: string }} }}
  * \`value\` is the final required input value based on the existing input. No matter what modifications are required, just provide the final value to replace the existing input value. 
- type: 'KeyboardPress', press a key
  * {{ param: {{ value: string }} }}
- type: 'Scroll', scroll up or down.
  * {{ 
      ${llmLocateParam}, 
      param: {{ 
        direction: 'down'(default) | 'up' | 'right' | 'left', 
        scrollType: 'once' (default) | 'untilBottom' | 'untilTop' | 'untilRight' | 'untilLeft', 
        distance: null | number 
      }} 
    }}
    * To scroll some specific element, put the element at the center of the region in the \`locate\` field. If it's a page scroll, put \`null\` in the \`locate\` field. 
    * \`param\` is required in this action. If some fields are not specified, use direction \`down\`, \`once\` scroll type, and \`null\` distance.
- type: 'ExpectedFalsyCondition'
  * {{ param: {{ reason: string }} }}
  * use this action when the conditional statement talked about in the instruction is falsy.
- type: 'Sleep'
  * {{ param: {{ timeMs: number }} }}
`;
var outputTemplate = `
## Output JSON Format:

The JSON format is as follows:

{{
  "actions": [
    // ... some actions
  ],
  ${commonOutputFields}
}}

## Examples

### Example: Decompose a task

When the instruction is 'Click the language switch button, wait 1s, click "English"', and not log is provided

By viewing the page screenshot and description, you should consider this and output the JSON:

* The main steps should be: tap the switch button, sleep, and tap the 'English' option
* The language switch button is shown in the screenshot, but it's not marked with a rectangle. So we have to use the page description to find the element. By carefully checking the context information (coordinates, attributes, content, etc.), you can find the element.
* The "English" option button is not shown in the screenshot now, it means it may only show after the previous actions are finished. So don't plan any action to do this.
* Log what these action do: Click the language switch button to open the language options. Wait for 1 second.
* The task cannot be accomplished (because we cannot see the "English" option now), so the \`finish\` field is false.

{{
  "actions":[
    {{
      "type": "Tap", 
      "thought": "Click the language switch button to open the language options.",
      "param": null,
      "locate": {{ id: "c81c4e9a33", prompt: "The language switch button" }},
    }},
    {{
      "type": "Sleep",
      "thought": "Wait for 1 second to ensure the language options are displayed.",
      "param": {{ "timeMs": 1000 }},
    }}
  ],
  "error": null,
  "finish": false,
  "log": "Click the language switch button to open the language options. Wait for 1 second",
}}

### Example: What NOT to do
Wrong output:
{{
  "actions":[
    {{
      "type": "Tap",
      "thought": "Click the language switch button to open the language options.",
      "param": null,
      "locate": {{
        {{ "id": "c81c4e9a33" }}, // WRONG: prompt is missing
      }}
    }},
    {{
      "type": "Tap", 
      "thought": "Click the English option",
      "param": null,
      "locate": null, // This means the 'English' option is not shown in the screenshot, the task cannot be accomplished
    }}
  ],
  "finish": true, // WRONG: should be false
  "log": "Click the language switch button to open the language options",
}}

Reason:
* The \`prompt\` is missing in the first 'Locate' action
* Since the option button is not shown in the screenshot, there are still more actions to be done, so the \`finish\` field should be false
`;
async function systemPromptToTaskPlanning() {
  if (_chunkUXPZNWPHjs.getAIConfigInBoolean.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_USE_QWEN_VL)) {
    return systemTemplateOfQwen;
  }
  const promptTemplate = new (0, _prompts.PromptTemplate)({
    template: `${systemTemplateOfLLM}

${outputTemplate}`,
    inputVariables: ["pageDescription"]
  });
  return await promptTemplate.format({
    pageDescription: samplePageDescription
  });
}
var planSchema = {
  type: "json_schema",
  json_schema: {
    name: "action_items",
    strict: true,
    schema: {
      type: "object",
      strict: true,
      properties: {
        actions: {
          //  TODO
          type: "array",
          items: {
            type: "object",
            strict: true,
            properties: {
              thought: {
                type: "string",
                description: "Reasons for generating this task, and why this task is feasible on this page"
              },
              type: {
                type: "string",
                description: 'Type of action, one of "Tap", "Hover" , "Input", "KeyboardPress", "Scroll", "ExpectedFalsyCondition", "Sleep"'
              },
              param: {
                anyOf: [
                  { type: "null" },
                  {
                    type: "object",
                    properties: { value: { type: ["string", "number"] } },
                    required: ["value"],
                    additionalProperties: false
                  },
                  {
                    type: "object",
                    properties: { timeMs: { type: ["number", "string"] } },
                    required: ["timeMs"],
                    additionalProperties: false
                  },
                  {
                    type: "object",
                    properties: {
                      direction: { type: "string" },
                      scrollType: { type: "string" },
                      distance: { type: ["number", "string", "null"] }
                    },
                    required: ["direction", "scrollType", "distance"],
                    additionalProperties: false
                  },
                  {
                    type: "object",
                    properties: { reason: { type: "string" } },
                    required: ["reason"],
                    additionalProperties: false
                  }
                ],
                description: "Parameter of the action, can be null ONLY when the type field is Tap or Hover"
              },
              locate: {
                type: ["object", "null"],
                properties: {
                  id: { type: "string" },
                  prompt: { type: "string" }
                },
                required: ["id", "prompt"],
                additionalProperties: false,
                description: "Location information for the target element"
              }
            },
            required: ["thought", "type", "param", "locate"],
            additionalProperties: false
          },
          description: "List of actions to be performed"
        },
        finish: {
          type: "boolean",
          description: "If all the actions described in the instruction have been covered by this action and logs, set this field to true."
        },
        log: {
          type: "string",
          description: "Log what these planned actions do. Do not include further actions that have not been planned."
        },
        error: {
          type: ["string", "null"],
          description: "Error messages about unexpected situations"
        }
      },
      required: ["actions", "finish", "log", "error"],
      additionalProperties: false
    }
  }
};
var generateTaskBackgroundContext = (userInstruction, log) => {
  if (log) {
    return `
Here is the user's instruction:
=============
${userInstruction}
=============

These are the logs from previous executions, which indicate what was done in the previous actions.
Do NOT repeat these actions.
=============
${log}
=============
`;
  }
  return `
Here is the user's instruction:
=============
${userInstruction}
=============
`;
};
var automationUserPrompt = () => {
  if (_chunkUXPZNWPHjs.getAIConfigInBoolean.call(void 0, _chunkUXPZNWPHjs.MATCH_BY_POSITION)) {
    return new (0, _prompts.PromptTemplate)({
      template: "{taskBackgroundContext}",
      inputVariables: ["taskBackgroundContext"]
    });
  }
  return new (0, _prompts.PromptTemplate)({
    template: `
pageDescription:
=====================================
{pageDescription}
=====================================

{taskBackgroundContext}
    `,
    inputVariables: ["pageDescription", "taskBackgroundContext"]
  });
};

// src/ai-model/service-caller/index.ts
function checkAIConfig() {
  if (_chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.OPENAI_API_KEY))
    return true;
  if (_chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_USE_AZURE_OPENAI))
    return true;
  if (_chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.ANTHROPIC_API_KEY))
    return true;
  return Boolean(_chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_OPENAI_INIT_CONFIG_JSON));
}
var defaultModel = "gpt-4o";
function getModelName() {
  let modelName = defaultModel;
  const nameInConfig = _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_MODEL_NAME);
  if (nameInConfig) {
    modelName = nameInConfig;
  }
  return modelName;
}
async function createChatClient({
  AIActionTypeValue
}) {
  let openai;
  const extraConfig = _chunkUXPZNWPHjs.getAIConfigInJson.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_OPENAI_INIT_CONFIG_JSON);
  const socksProxy = _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_OPENAI_SOCKS_PROXY);
  const httpsProxy = _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_OPENAI_HTTPS_PROXY);
  let httpAgent;
  if (socksProxy) {
    httpAgent = new (0, _socksproxyagent.SocksProxyAgent)(socksProxy);
  } else if (httpsProxy) {
    httpAgent = new (0, _httpsproxyagent.HttpsProxyAgent)(httpsProxy);
  }
  if (_chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.OPENAI_USE_AZURE)) {
    openai = new (0, _openai.AzureOpenAI)({
      baseURL: _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.OPENAI_BASE_URL),
      apiKey: _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.OPENAI_API_KEY),
      httpAgent,
      ...extraConfig,
      dangerouslyAllowBrowser: true
    });
  } else if (_chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_USE_AZURE_OPENAI)) {
    const extraAzureConfig = _chunkUXPZNWPHjs.getAIConfigInJson.call(void 0, 
      _chunkUXPZNWPHjs.MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON
    );
    const scope = _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_AZURE_OPENAI_SCOPE);
    let tokenProvider = void 0;
    if (scope) {
      _assert2.default.call(void 0, 
        !_utils.ifInBrowser,
        "Azure OpenAI is not supported in browser with Midscene."
      );
      const credential = new (0, _identity.DefaultAzureCredential)();
      _assert2.default.call(void 0, scope, "MIDSCENE_AZURE_OPENAI_SCOPE is required");
      tokenProvider = _identity.getBearerTokenProvider.call(void 0, credential, scope);
      openai = new (0, _openai.AzureOpenAI)({
        azureADTokenProvider: tokenProvider,
        endpoint: _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.AZURE_OPENAI_ENDPOINT),
        apiVersion: _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.AZURE_OPENAI_API_VERSION),
        deployment: _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.AZURE_OPENAI_DEPLOYMENT),
        ...extraConfig,
        ...extraAzureConfig
      });
    } else {
      openai = new (0, _openai.AzureOpenAI)({
        apiKey: _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.AZURE_OPENAI_KEY),
        endpoint: _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.AZURE_OPENAI_ENDPOINT),
        apiVersion: _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.AZURE_OPENAI_API_VERSION),
        deployment: _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.AZURE_OPENAI_DEPLOYMENT),
        dangerouslyAllowBrowser: true,
        ...extraConfig,
        ...extraAzureConfig
      });
    }
  } else if (!_chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_USE_ANTHROPIC_SDK)) {
    const baseURL = _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.OPENAI_BASE_URL);
    if (typeof baseURL === "string") {
      if (!/^https?:\/\//.test(baseURL)) {
        throw new Error(
          `OPENAI_BASE_URL must be a valid URL starting with http:// or https://, but got: ${baseURL}
Please check your config.`
        );
      }
    }
    openai = new (0, _openai2.default)({
      baseURL: _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.OPENAI_BASE_URL),
      apiKey: _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.OPENAI_API_KEY),
      httpAgent,
      ...extraConfig,
      defaultHeaders: {
        ...(extraConfig == null ? void 0 : extraConfig.defaultHeaders) || {},
        [_chunkUXPZNWPHjs.MIDSCENE_API_TYPE]: AIActionTypeValue.toString()
      },
      dangerouslyAllowBrowser: true
    });
  }
  if (openai && _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_LANGSMITH_DEBUG)) {
    if (_utils.ifInBrowser) {
      throw new Error("langsmith is not supported in browser");
    }
    console.log("DEBUGGING MODE: langsmith wrapper enabled");
    const { wrapOpenAI } = await Promise.resolve().then(() => require("./wrappers-RF6ICPOK.js"));
    openai = wrapOpenAI(openai);
  }
  if (typeof openai !== "undefined") {
    return {
      completion: openai.chat.completions,
      style: "openai"
    };
  }
  if (_chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_USE_ANTHROPIC_SDK)) {
    const apiKey = _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.ANTHROPIC_API_KEY);
    _assert2.default.call(void 0, apiKey, "ANTHROPIC_API_KEY is required");
    openai = new (0, _sdk.Anthropic)({
      apiKey,
      dangerouslyAllowBrowser: true
    });
  }
  if (typeof openai !== "undefined" && openai.messages) {
    return {
      completion: openai.messages,
      style: "anthropic"
    };
  }
  throw new Error("Openai SDK or Anthropic SDK is not initialized");
}
async function call(messages, AIActionTypeValue, responseFormat) {
  const { completion, style } = await createChatClient({
    AIActionTypeValue
  });
  const shouldPrintTiming = _chunkUXPZNWPHjs.getAIConfigInBoolean.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_DEBUG_AI_PROFILE);
  const shouldPrintAIResponse = _chunkUXPZNWPHjs.getAIConfigInBoolean.call(void 0, 
    _chunkUXPZNWPHjs.MIDSCENE_DEBUG_AI_RESPONSE
  );
  const maxTokens = _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.OPENAI_MAX_TOKENS);
  const startTime = Date.now();
  const model = getModelName();
  let content;
  let usage;
  const commonConfig = {
    temperature: _chunkUXPZNWPHjs.getAIConfigInBoolean.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_USE_VLM_UI_TARS) ? 0 : 0.1,
    stream: false,
    max_tokens: typeof maxTokens === "number" ? maxTokens : Number.parseInt(maxTokens || "2048", 10),
    ..._chunkUXPZNWPHjs.getAIConfigInBoolean.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_USE_QWEN_VL) ? {
      vl_high_resolution_images: true
    } : {}
  };
  if (style === "openai") {
    const result = await completion.create({
      model,
      messages,
      response_format: responseFormat,
      ...commonConfig
    });
    shouldPrintTiming && console.log(
      "Midscene - AI call",
      _chunkUXPZNWPHjs.getAIConfig.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_USE_QWEN_VL) ? "MIDSCENE_USE_QWEN_VL" : "",
      model,
      result.usage,
      `${Date.now() - startTime}ms`,
      result._request_id || ""
    );
    _assert2.default.call(void 0, 
      result.choices,
      `invalid response from LLM service: ${JSON.stringify(result)}`
    );
    content = result.choices[0].message.content;
    shouldPrintAIResponse && console.log("Midscene - AI response", content);
    _assert2.default.call(void 0, content, "empty content");
    usage = result.usage;
  } else if (style === "anthropic") {
    const convertImageContent = (content2) => {
      if (content2.type === "image_url") {
        const imgBase64 = content2.image_url.url;
        _assert2.default.call(void 0, imgBase64, "image_url is required");
        return {
          source: {
            type: "base64",
            media_type: imgBase64.includes("data:image/png;base64,") ? "image/png" : "image/jpeg",
            data: imgBase64.split(",")[1]
          },
          type: "image"
        };
      }
      return content2;
    };
    const result = await completion.create({
      model,
      system: "You are a versatile professional in software UI automation",
      messages: messages.map((m) => ({
        role: "user",
        content: Array.isArray(m.content) ? m.content.map(convertImageContent) : m.content
      })),
      response_format: responseFormat,
      ...commonConfig
    });
    content = result.content[0].text;
    _assert2.default.call(void 0, content, "empty content");
    usage = result.usage;
  }
  return { content: content || "", usage };
}
async function callToGetJSONObject(messages, AIActionTypeValue) {
  let responseFormat;
  const model = getModelName();
  if (model.includes("gpt-4o")) {
    switch (AIActionTypeValue) {
      case 0 /* ASSERT */:
        responseFormat = assertSchema;
        break;
      case 1 /* INSPECT_ELEMENT */:
        responseFormat = locatorSchema;
        break;
      case 2 /* EXTRACT_DATA */:
        responseFormat = { type: "json_object" /* JSON */ };
        break;
      case 3 /* PLAN */:
        responseFormat = planSchema;
        break;
    }
  }
  if (model === "gpt-4o-2024-05-13") {
    responseFormat = { type: "json_object" /* JSON */ };
  }
  const response = await call(messages, AIActionTypeValue, responseFormat);
  _assert2.default.call(void 0, response, "empty response");
  const jsonContent = safeParseJson(response.content);
  return { content: jsonContent, usage: response.usage };
}
function extractJSONFromCodeBlock(response) {
  try {
    const jsonMatch = response.match(/^\s*(\{[\s\S]*\})\s*$/);
    if (jsonMatch) {
      return jsonMatch[1];
    }
    const codeBlockMatch = response.match(
      /```(?:json)?\s*(\{[\s\S]*?\})\s*```/
    );
    if (codeBlockMatch) {
      return codeBlockMatch[1];
    }
    const jsonLikeMatch = response.match(/\{[\s\S]*\}/);
    if (jsonLikeMatch) {
      return jsonLikeMatch[0];
    }
  } catch (e) {
  }
  return response;
}
function safeParseJson(input) {
  var _a;
  const cleanJsonString = extractJSONFromCodeBlock(input);
  if (cleanJsonString.match(/\((\d+),(\d+)\)/)) {
    return (_a = cleanJsonString.match(/\((\d+),(\d+)\)/)) == null ? void 0 : _a.slice(1).map(Number);
  }
  try {
    return JSON.parse(cleanJsonString);
  } catch (e) {
  }
  try {
    return import_dirty_json.default.parse(cleanJsonString);
  } catch (e) {
    console.log("e:", e);
  }
  throw Error(`failed to parse json response: ${input}`);
}

// src/ai-model/inspect.ts



// src/ai-model/prompt/extraction.ts

function systemPromptToExtract() {
  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 extract the data according to the DATA_DEMAND.

Return in the following JSON format:
{
  data: any, // the extracted data from extract_data_from_UI skill. 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.
  errors: [], // string[], error message if any
}
`;
}
var extractDataPrompt = new (0, _prompts.PromptTemplate)({
  template: `
pageDescription: {pageDescription}

Use your extract_data_from_UI skill to find the following data, placing it in the \`data\` field
DATA_DEMAND start:
=====================================
{dataKeys}

{dataQuery}
=====================================
DATA_DEMAND ends.
  `,
  inputVariables: ["pageDescription", "dataKeys", "dataQuery"]
});

// src/ai-model/inspect.ts
var liteContextConfig = {
  filterNonTextContent: true,
  truncateTextLength: 200
};
function transformToAbsoluteCoords(relativePosition, size) {
  return {
    x: Number((relativePosition.x / 1e3 * size.width).toFixed(3)),
    y: Number((relativePosition.y / 1e3 * size.height).toFixed(3))
  };
}
async function transformElementPositionToId(aiResult, treeRoot, size, insertElementByPosition) {
  const emptyResponse = {
    errors: [],
    elements: []
  };
  const elementAtPosition = (center) => {
    const element = elementByPositionWithElementInfo(treeRoot, center);
    const distanceToCenter = element ? distance({ x: element.center[0], y: element.center[1] }, center) : 0;
    return distanceToCenter <= distanceThreshold ? element : void 0;
  };
  if ("bbox" in aiResult) {
    if (!Array.isArray(aiResult.bbox) || aiResult.bbox.length !== 4) {
      return emptyResponse;
    }
    aiResult.bbox[0] = Math.ceil(aiResult.bbox[0]);
    aiResult.bbox[1] = Math.ceil(aiResult.bbox[1]);
    aiResult.bbox[2] = Math.ceil(aiResult.bbox[2]);
    aiResult.bbox[3] = Math.ceil(aiResult.bbox[3]);
    const centerX = (aiResult.bbox[0] + aiResult.bbox[2]) / 2;
    const centerY = (aiResult.bbox[1] + aiResult.bbox[3]) / 2;
    let element = elementAtPosition({ x: centerX, y: centerY });
    if (!element) {
      element = insertElementByPosition({
        x: centerX,
        y: centerY
      });
    }
    _assert2.default.call(void 0, 
      element,
      `inspect: no element found with coordinates: ${JSON.stringify(aiResult.bbox)}`
    );
    return {
      errors: [],
      elements: [
        {
          id: element.id
        }
      ]
    };
  }
  if (Array.isArray(aiResult)) {
    const relativePosition = aiResult;
    const absolutePosition = transformToAbsoluteCoords(
      {
        x: relativePosition[0],
        y: relativePosition[1]
      },
      size
    );
    let element = elementAtPosition(absolutePosition);
    if (!element) {
      element = insertElementByPosition(absolutePosition);
    }
    _assert2.default.call(void 0, 
      element,
      `inspect: no id found with position: ${JSON.stringify({ absolutePosition })}`
    );
    return {
      errors: [],
      elements: [
        {
          id: element.id
        }
      ]
    };
  }
  return {
    errors: aiResult.errors,
    elements: aiResult.elements
  };
}
function matchQuickAnswer(quickAnswer, tree, elementById, insertElementByPosition) {
  if (!quickAnswer) {
    return void 0;
  }
  if ("id" in quickAnswer && quickAnswer.id && elementById(quickAnswer.id)) {
    return {
      parseResult: {
        elements: [quickAnswer],
        errors: []
      },
      rawResponse: quickAnswer,
      elementById
    };
  }
  if ("position" in quickAnswer && quickAnswer.position) {
    let element = elementByPositionWithElementInfo(tree, quickAnswer.position);
    if (!element) {
      element = insertElementByPosition(quickAnswer.position);
    }
    return {
      parseResult: {
        elements: [element],
        errors: []
      },
      rawResponse: quickAnswer,
      elementById
    };
  }
  if ("bbox" in quickAnswer && quickAnswer.bbox) {
    const centerPosition = {
      x: Math.floor((quickAnswer.bbox[0] + quickAnswer.bbox[2]) / 2),
      y: Math.floor((quickAnswer.bbox[1] + quickAnswer.bbox[3]) / 2)
    };
    let element = elementByPositionWithElementInfo(tree, centerPosition);
    if (!element) {
      element = insertElementByPosition(centerPosition);
    }
    return {
      parseResult: {
        elements: [element],
        errors: []
      },
      rawResponse: quickAnswer,
      elementById
    };
  }
  return void 0;
}
async function AiInspectElement(options) {
  const { context, targetElementDescription, callAI } = options;
  const { screenshotBase64, screenshotBase64WithElementMarker } = context;
  const { description, elementById, insertElementByPosition, size } = await describeUserPage(context);
  const quickAnswer = matchQuickAnswer(
    options.quickAnswer,
    context.tree,
    elementById,
    insertElementByPosition
  );
  if (quickAnswer) {
    return quickAnswer;
  }
  _assert2.default.call(void 0, 
    targetElementDescription,
    "cannot find the target element description"
  );
  const userInstructionPrompt = await findElementPrompt.format({
    pageDescription: description,
    targetElementDescription
  });
  const systemPrompt = systemPromptToLocateElement();
  let imagePayload = screenshotBase64WithElementMarker || screenshotBase64;
  if (_chunkUXPZNWPHjs.getAIConfigInBoolean.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_USE_QWEN_VL)) {
    imagePayload = await _img.paddingToMatchBlock.call(void 0, imagePayload);
  }
  const msgs = [
    { role: "system", content: systemPrompt },
    {
      role: "user",
      content: [
        {
          type: "image_url",
          image_url: {
            url: imagePayload,
            detail: "high"
          }
        },
        {
          type: "text",
          text: userInstructionPrompt
        }
      ]
    }
  ];
  const callAIFn = callAI || callToGetJSONObject;
  const res = await callAIFn(msgs, 1 /* INSPECT_ELEMENT */);
  const parseResult = await transformElementPositionToId(
    res.content,
    context.tree,
    size,
    insertElementByPosition
  );
  return {
    parseResult,
    rawResponse: res.content,
    elementById,
    usage: res.usage
  };
}
async function AiExtractElementInfo(options) {
  const { dataQuery, context } = options;
  const systemPrompt = systemPromptToExtract();
  const { screenshotBase64 } = context;
  const { description, elementById } = await describeUserPage(
    context,
    liteContextConfig
  );
  let dataKeys = "";
  let dataQueryText = "";
  if (typeof dataQuery === "string") {
    dataKeys = "";
    dataQueryText = dataQuery;
  } else {
    dataKeys = `return in key-value style object, keys are ${Object.keys(dataQuery).join(",")}`;
    dataQueryText = JSON.stringify(dataQuery, null, 2);
  }
  const extractDataPromptText = await extractDataPrompt.format({
    pageDescription: description,
    dataKeys,
    dataQuery: dataQueryText
  });
  const msgs = [
    { role: "system", content: systemPrompt },
    {
      role: "user",
      content: [
        {
          type: "image_url",
          image_url: {
            url: screenshotBase64,
            detail: "high"
          }
        },
        {
          type: "text",
          text: extractDataPromptText
        }
      ]
    }
  ];
  const result = await callAiFn(
    msgs,
    2 /* EXTRACT_DATA */
  );
  return {
    parseResult: result.content,
    elementById,
    usage: result.usage
  };
}
async function AiAssert(options) {
  const { assertion, context } = options;
  _assert2.default.call(void 0, assertion, "assertion should be a string");
  const { screenshotBase64 } = context;
  const systemPrompt = systemPromptToAssert({
    isUITars: _chunkUXPZNWPHjs.getAIConfigInBoolean.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_USE_VLM_UI_TARS)
  });
  const msgs = [
    { role: "system", content: systemPrompt },
    {
      role: "user",
      content: [
        {
          type: "image_url",
          image_url: {
            url: screenshotBase64,
            detail: "high"
          }
        },
        {
          type: "text",
          text: `
Here is the assertion. Please tell whether it is truthy according to the screenshot.
=====================================
${assertion}
=====================================
  `
        }
      ]
    }
  ];
  const { content: assertResult, usage } = await callAiFn(
    msgs,
    0 /* ASSERT */
  );
  return {
    content: assertResult,
    usage
  };
}

// src/ai-model/llm-planning.ts


async function plan(userInstruction, opts) {
  var _a;
  const { callAI, context } = opts || {};
  const { screenshotBase64, screenshotBase64WithElementMarker, size } = context;
  const { description: pageDescription } = await describeUserPage(context);
  const systemPrompt = await systemPromptToTaskPlanning();
  const taskBackgroundContextText = generateTaskBackgroundContext(
    userInstruction,
    opts.log
  );
  const userInstructionPrompt = await automationUserPrompt().format({
    pageDescription,
    taskBackgroundContext: taskBackgroundContextText
  });
  let imagePayload = screenshotBase64WithElementMarker || screenshotBase64;
  if (_chunkUXPZNWPHjs.getAIConfigInBoolean.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_USE_QWEN_VL)) {
    imagePayload = await _img.paddingToMatchBlock.call(void 0, imagePayload);
  }
  warnGPT4oSizeLimit(size);
  const msgs = [
    { role: "system", content: systemPrompt },
    {
      role: "user",
      content: [
        {
          type: "image_url",
          image_url: {
            url: imagePayload,
            detail: "high"
          }
        },
        {
          type: "text",
          text: userInstructionPrompt
        }
      ]
    }
  ];
  const call2 = callAI || callAiFn;
  const { content, usage } = await call2(msgs, 3 /* PLAN */);
  const rawResponse = JSON.stringify(content, void 0, 2);
  const planFromAI = content;
  const actions = (((_a = planFromAI.action) == null ? void 0 : _a.type) ? [planFromAI.action] : planFromAI.actions) || [];
  const returnValue = {
    ...planFromAI,
    actions,
    rawResponse,
    usage
  };
  if (_chunkUXPZNWPHjs.getAIConfigInBoolean.call(void 0, _chunkUXPZNWPHjs.MIDSCENE_USE_QWEN_VL)) {
    actions.forEach((action) => {
      if (action.locate) {
        action.locate = fillLocateParam(action.locate);
      }
    });
  }
  _assert2.default.call(void 0, planFromAI, "can't get plans from AI");
  _assert2.default.call(void 0, 
    actions.length > 0 || !returnValue.more_actions_needed_by_instruction || returnValue.sleep,
    `Failed to plan actions: ${planFromAI.error || "(no error details)"}`
  );
  return returnValue;
}

// src/ai-model/ui-tars-planning.ts

var _keyboardlayout = require('@aico-test/shared/keyboard-layout');
var _actionparser = require('@ui-tars/action-parser');
async function vlmPlanning(options) {
  const { conversationHistory, userInstruction, size } = options;
  const systemPrompt = uiTarsPlanningPrompt + userInstruction;
  const res = await call(
    [
      {
        role: "user",
        content: systemPrompt
      },
      ...conversationHistory
    ],
    1 /* INSPECT_ELEMENT */
  );
  const { parsed } = _actionparser.actionParser.call(void 0, {
    prediction: res.content,
    factor: 1e3
  });
  const transformActions = [];
  parsed.forEach((action) => {
    if (action.action_type === "click") {
      _assert2.default.call(void 0, action.action_inputs.start_box, "start_box is required");
      const point = getPoint(action.action_inputs.start_box, size);
      transformActions.push({
        type: "Locate",
        locate: {
          prompt: action.thought || "",
          position: { x: point[0], y: point[1] }
        },
        param: {
          // action,
          // position: { x: point[0], y: point[1] },
        }
      });
      transformActions.push({
        type: "Tap",
        locate: {
          prompt: action.thought || "",
          position: { x: point[0], y: point[1] }
        },
        param: action.thought || ""
      });
    } else if (action.action_type === "drag") {
      _assert2.default.call(void 0, action.action_inputs.start_box, "start_box is required");
      _assert2.default.call(void 0, action.action_inputs.end_box, "end_box is required");
      const startPoint = getPoint(action.action_inputs.start_box, size);
      const endPoint = getPoint(action.action_inputs.end_box, size);
      transformActions.push({
        type: "Drag",
        param: {
          start_box: { x: startPoint[0], y: startPoint[1] },
          end_box: { x: endPoint[0], y: endPoint[1] }
        },
        locate: null,
        thought: action.thought || ""
      });
    } else if (action.action_type === "type") {
      transformActions.push({
        type: "Input",
        param: {
          value: action.action_inputs.content
        },
        locate: null,
        thought: action.thought || ""
      });
    } else if (action.action_type === "scroll") {
      transformActions.push({
        type: "Scroll",
        param: {
          direction: action.action_inputs.direction
        },
        locate: null,
        thought: action.thought || ""
      });
    } else if (action.action_type === "finished") {
      transformActions.push({
        type: "Finished",
        param: {},
        locate: null,
        thought: action.thought || ""
      });
    } else if (action.action_type === "hotkey") {
      _assert2.default.call(void 0, action.action_inputs.key, "key is required");
      const keys = _keyboardlayout.transformHotkeyInput.call(void 0, action.action_inputs.key);
      transformActions.push({
        type: "KeyboardPress",
        param: {
          value: keys
        },
        locate: null,
        thought: action.thought || ""
      });
    } else if (action.action_type === "wait") {
      transformActions.push({
        type: "Sleep",
        param: {
          timeMs: 1e3
        },
        locate: null,
        thought: action.thought || ""
      });
    }
  });
  if (transformActions.length === 0) {
    throw new Error(`No actions found, response: ${res.content}`, {
      cause: {
        prediction: res.content,
        parsed
      }
    });
  }
  return {
    actions: transformActions,
    realActions: parsed,
    action_summary: getSummary(res.content)
  };
}
function getPoint(startBox, size) {
  const [x, y] = JSON.parse(startBox);
  return [x * size.width, y * size.height];
}















exports.BaseElement = BaseElement; exports.AIResponseFormat = AIResponseFormat; exports.UIContext = UIContext; exports.systemPromptToLocateElement = systemPromptToLocateElement; exports.describeUserPage = describeUserPage; exports.callToGetJSONObject = callToGetJSONObject; exports.callAiFn = callAiFn; exports.transformElementPositionToId = transformElementPositionToId; exports.AiInspectElement = AiInspectElement; exports.AiExtractElementInfo = AiExtractElementInfo; exports.AiAssert = AiAssert; exports.plan = plan; exports.vlmPlanning = vlmPlanning;
/*! Bundled license information:

string.fromcodepoint/fromcodepoint.js:
  (*! http://mths.be/fromcodepoint v0.2.1 by @mathias *)

utf8/utf8.js:
  (*! https://mths.be/utf8js v3.0.0 by @mathias *)
*/

//# sourceMappingURL=chunk-QJSEL33O.js.map