#!/usr/bin/env node
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __commonJS = (cb, mod) => function __require() {
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));

// ../../node_modules/isexe/windows.js
var require_windows = __commonJS({
  "../../node_modules/isexe/windows.js"(exports2, module2) {
    module2.exports = isexe;
    isexe.sync = sync;
    var fs3 = require("fs");
    function checkPathExt(path5, options) {
      var pathext = options.pathExt !== void 0 ? options.pathExt : process.env.PATHEXT;
      if (!pathext) {
        return true;
      }
      pathext = pathext.split(";");
      if (pathext.indexOf("") !== -1) {
        return true;
      }
      for (var i = 0; i < pathext.length; i++) {
        var p = pathext[i].toLowerCase();
        if (p && path5.substr(-p.length).toLowerCase() === p) {
          return true;
        }
      }
      return false;
    }
    function checkStat(stat, path5, options) {
      if (!stat.isSymbolicLink() && !stat.isFile()) {
        return false;
      }
      return checkPathExt(path5, options);
    }
    function isexe(path5, options, cb) {
      fs3.stat(path5, function(er, stat) {
        cb(er, er ? false : checkStat(stat, path5, options));
      });
    }
    function sync(path5, options) {
      return checkStat(fs3.statSync(path5), path5, options);
    }
  }
});

// ../../node_modules/isexe/mode.js
var require_mode = __commonJS({
  "../../node_modules/isexe/mode.js"(exports2, module2) {
    module2.exports = isexe;
    isexe.sync = sync;
    var fs3 = require("fs");
    function isexe(path5, options, cb) {
      fs3.stat(path5, function(er, stat) {
        cb(er, er ? false : checkStat(stat, options));
      });
    }
    function sync(path5, options) {
      return checkStat(fs3.statSync(path5), options);
    }
    function checkStat(stat, options) {
      return stat.isFile() && checkMode(stat, options);
    }
    function checkMode(stat, options) {
      var mod = stat.mode;
      var uid = stat.uid;
      var gid = stat.gid;
      var myUid = options.uid !== void 0 ? options.uid : process.getuid && process.getuid();
      var myGid = options.gid !== void 0 ? options.gid : process.getgid && process.getgid();
      var u = parseInt("100", 8);
      var g = parseInt("010", 8);
      var o = parseInt("001", 8);
      var ug = u | g;
      var ret = mod & o || mod & g && gid === myGid || mod & u && uid === myUid || mod & ug && myUid === 0;
      return ret;
    }
  }
});

// ../../node_modules/isexe/index.js
var require_isexe = __commonJS({
  "../../node_modules/isexe/index.js"(exports2, module2) {
    var fs3 = require("fs");
    var core;
    if (process.platform === "win32" || global.TESTING_WINDOWS) {
      core = require_windows();
    } else {
      core = require_mode();
    }
    module2.exports = isexe;
    isexe.sync = sync;
    function isexe(path5, options, cb) {
      if (typeof options === "function") {
        cb = options;
        options = {};
      }
      if (!cb) {
        if (typeof Promise !== "function") {
          throw new TypeError("callback not provided");
        }
        return new Promise(function(resolve, reject) {
          isexe(path5, options || {}, function(er, is) {
            if (er) {
              reject(er);
            } else {
              resolve(is);
            }
          });
        });
      }
      core(path5, options || {}, function(er, is) {
        if (er) {
          if (er.code === "EACCES" || options && options.ignoreErrors) {
            er = null;
            is = false;
          }
        }
        cb(er, is);
      });
    }
    function sync(path5, options) {
      try {
        return core.sync(path5, options || {});
      } catch (er) {
        if (options && options.ignoreErrors || er.code === "EACCES") {
          return false;
        } else {
          throw er;
        }
      }
    }
  }
});

// ../../node_modules/which/which.js
var require_which = __commonJS({
  "../../node_modules/which/which.js"(exports2, module2) {
    var isWindows = process.platform === "win32" || process.env.OSTYPE === "cygwin" || process.env.OSTYPE === "msys";
    var path5 = require("path");
    var COLON = isWindows ? ";" : ":";
    var isexe = require_isexe();
    var getNotFoundError = (cmd) => Object.assign(new Error(`not found: ${cmd}`), { code: "ENOENT" });
    var getPathInfo = (cmd, opt) => {
      const colon = opt.colon || COLON;
      const pathEnv = cmd.match(/\//) || isWindows && cmd.match(/\\/) ? [""] : [
        // windows always checks the cwd first
        ...isWindows ? [process.cwd()] : [],
        ...(opt.path || process.env.PATH || /* istanbul ignore next: very unusual */
        "").split(colon)
      ];
      const pathExtExe = isWindows ? opt.pathExt || process.env.PATHEXT || ".EXE;.CMD;.BAT;.COM" : "";
      const pathExt = isWindows ? pathExtExe.split(colon) : [""];
      if (isWindows) {
        if (cmd.indexOf(".") !== -1 && pathExt[0] !== "")
          pathExt.unshift("");
      }
      return {
        pathEnv,
        pathExt,
        pathExtExe
      };
    };
    var which = (cmd, opt, cb) => {
      if (typeof opt === "function") {
        cb = opt;
        opt = {};
      }
      if (!opt)
        opt = {};
      const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
      const found = [];
      const step = (i) => new Promise((resolve, reject) => {
        if (i === pathEnv.length)
          return opt.all && found.length ? resolve(found) : reject(getNotFoundError(cmd));
        const ppRaw = pathEnv[i];
        const pathPart = /^".*"$/.test(ppRaw) ? ppRaw.slice(1, -1) : ppRaw;
        const pCmd = path5.join(pathPart, cmd);
        const p = !pathPart && /^\.[\\\/]/.test(cmd) ? cmd.slice(0, 2) + pCmd : pCmd;
        resolve(subStep(p, i, 0));
      });
      const subStep = (p, i, ii) => new Promise((resolve, reject) => {
        if (ii === pathExt.length)
          return resolve(step(i + 1));
        const ext = pathExt[ii];
        isexe(p + ext, { pathExt: pathExtExe }, (er, is) => {
          if (!er && is) {
            if (opt.all)
              found.push(p + ext);
            else
              return resolve(p + ext);
          }
          return resolve(subStep(p, i, ii + 1));
        });
      });
      return cb ? step(0).then((res) => cb(null, res), cb) : step(0);
    };
    var whichSync = (cmd, opt) => {
      opt = opt || {};
      const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
      const found = [];
      for (let i = 0; i < pathEnv.length; i++) {
        const ppRaw = pathEnv[i];
        const pathPart = /^".*"$/.test(ppRaw) ? ppRaw.slice(1, -1) : ppRaw;
        const pCmd = path5.join(pathPart, cmd);
        const p = !pathPart && /^\.[\\\/]/.test(cmd) ? cmd.slice(0, 2) + pCmd : pCmd;
        for (let j = 0; j < pathExt.length; j++) {
          const cur = p + pathExt[j];
          try {
            const is = isexe.sync(cur, { pathExt: pathExtExe });
            if (is) {
              if (opt.all)
                found.push(cur);
              else
                return cur;
            }
          } catch (ex) {
          }
        }
      }
      if (opt.all && found.length)
        return found;
      if (opt.nothrow)
        return null;
      throw getNotFoundError(cmd);
    };
    module2.exports = which;
    which.sync = whichSync;
  }
});

// ../../node_modules/path-key/index.js
var require_path_key = __commonJS({
  "../../node_modules/path-key/index.js"(exports2, module2) {
    "use strict";
    var pathKey = (options = {}) => {
      const environment = options.env || process.env;
      const platform = options.platform || process.platform;
      if (platform !== "win32") {
        return "PATH";
      }
      return Object.keys(environment).reverse().find((key) => key.toUpperCase() === "PATH") || "Path";
    };
    module2.exports = pathKey;
    module2.exports.default = pathKey;
  }
});

// ../../node_modules/cross-spawn/lib/util/resolveCommand.js
var require_resolveCommand = __commonJS({
  "../../node_modules/cross-spawn/lib/util/resolveCommand.js"(exports2, module2) {
    "use strict";
    var path5 = require("path");
    var which = require_which();
    var getPathKey = require_path_key();
    function resolveCommandAttempt(parsed, withoutPathExt) {
      const env = parsed.options.env || process.env;
      const cwd = process.cwd();
      const hasCustomCwd = parsed.options.cwd != null;
      const shouldSwitchCwd = hasCustomCwd && process.chdir !== void 0 && !process.chdir.disabled;
      if (shouldSwitchCwd) {
        try {
          process.chdir(parsed.options.cwd);
        } catch (err) {
        }
      }
      let resolved;
      try {
        resolved = which.sync(parsed.command, {
          path: env[getPathKey({ env })],
          pathExt: withoutPathExt ? path5.delimiter : void 0
        });
      } catch (e) {
      } finally {
        if (shouldSwitchCwd) {
          process.chdir(cwd);
        }
      }
      if (resolved) {
        resolved = path5.resolve(hasCustomCwd ? parsed.options.cwd : "", resolved);
      }
      return resolved;
    }
    function resolveCommand(parsed) {
      return resolveCommandAttempt(parsed) || resolveCommandAttempt(parsed, true);
    }
    module2.exports = resolveCommand;
  }
});

// ../../node_modules/cross-spawn/lib/util/escape.js
var require_escape = __commonJS({
  "../../node_modules/cross-spawn/lib/util/escape.js"(exports2, module2) {
    "use strict";
    var metaCharsRegExp = /([()\][%!^"`<>&|;, *?])/g;
    function escapeCommand(arg) {
      arg = arg.replace(metaCharsRegExp, "^$1");
      return arg;
    }
    function escapeArgument(arg, doubleEscapeMetaChars) {
      arg = `${arg}`;
      arg = arg.replace(/(?=(\\+?)?)\1"/g, '$1$1\\"');
      arg = arg.replace(/(?=(\\+?)?)\1$/, "$1$1");
      arg = `"${arg}"`;
      arg = arg.replace(metaCharsRegExp, "^$1");
      if (doubleEscapeMetaChars) {
        arg = arg.replace(metaCharsRegExp, "^$1");
      }
      return arg;
    }
    module2.exports.command = escapeCommand;
    module2.exports.argument = escapeArgument;
  }
});

// ../../node_modules/shebang-regex/index.js
var require_shebang_regex = __commonJS({
  "../../node_modules/shebang-regex/index.js"(exports2, module2) {
    "use strict";
    module2.exports = /^#!(.*)/;
  }
});

// ../../node_modules/shebang-command/index.js
var require_shebang_command = __commonJS({
  "../../node_modules/shebang-command/index.js"(exports2, module2) {
    "use strict";
    var shebangRegex = require_shebang_regex();
    module2.exports = (string4 = "") => {
      const match = string4.match(shebangRegex);
      if (!match) {
        return null;
      }
      const [path5, argument] = match[0].replace(/#! ?/, "").split(" ");
      const binary = path5.split("/").pop();
      if (binary === "env") {
        return argument;
      }
      return argument ? `${binary} ${argument}` : binary;
    };
  }
});

// ../../node_modules/cross-spawn/lib/util/readShebang.js
var require_readShebang = __commonJS({
  "../../node_modules/cross-spawn/lib/util/readShebang.js"(exports2, module2) {
    "use strict";
    var fs3 = require("fs");
    var shebangCommand = require_shebang_command();
    function readShebang(command) {
      const size = 150;
      const buffer = Buffer.alloc(size);
      let fd;
      try {
        fd = fs3.openSync(command, "r");
        fs3.readSync(fd, buffer, 0, size, 0);
        fs3.closeSync(fd);
      } catch (e) {
      }
      return shebangCommand(buffer.toString());
    }
    module2.exports = readShebang;
  }
});

// ../../node_modules/cross-spawn/lib/parse.js
var require_parse = __commonJS({
  "../../node_modules/cross-spawn/lib/parse.js"(exports2, module2) {
    "use strict";
    var path5 = require("path");
    var resolveCommand = require_resolveCommand();
    var escape2 = require_escape();
    var readShebang = require_readShebang();
    var isWin = process.platform === "win32";
    var isExecutableRegExp = /\.(?:com|exe)$/i;
    var isCmdShimRegExp = /node_modules[\\/].bin[\\/][^\\/]+\.cmd$/i;
    function detectShebang(parsed) {
      parsed.file = resolveCommand(parsed);
      const shebang = parsed.file && readShebang(parsed.file);
      if (shebang) {
        parsed.args.unshift(parsed.file);
        parsed.command = shebang;
        return resolveCommand(parsed);
      }
      return parsed.file;
    }
    function parseNonShell(parsed) {
      if (!isWin) {
        return parsed;
      }
      const commandFile = detectShebang(parsed);
      const needsShell = !isExecutableRegExp.test(commandFile);
      if (parsed.options.forceShell || needsShell) {
        const needsDoubleEscapeMetaChars = isCmdShimRegExp.test(commandFile);
        parsed.command = path5.normalize(parsed.command);
        parsed.command = escape2.command(parsed.command);
        parsed.args = parsed.args.map((arg) => escape2.argument(arg, needsDoubleEscapeMetaChars));
        const shellCommand = [parsed.command].concat(parsed.args).join(" ");
        parsed.args = ["/d", "/s", "/c", `"${shellCommand}"`];
        parsed.command = process.env.comspec || "cmd.exe";
        parsed.options.windowsVerbatimArguments = true;
      }
      return parsed;
    }
    function parse3(command, args, options) {
      if (args && !Array.isArray(args)) {
        options = args;
        args = null;
      }
      args = args ? args.slice(0) : [];
      options = Object.assign({}, options);
      const parsed = {
        command,
        args,
        options,
        file: void 0,
        original: {
          command,
          args
        }
      };
      return options.shell ? parsed : parseNonShell(parsed);
    }
    module2.exports = parse3;
  }
});

// ../../node_modules/cross-spawn/lib/enoent.js
var require_enoent = __commonJS({
  "../../node_modules/cross-spawn/lib/enoent.js"(exports2, module2) {
    "use strict";
    var isWin = process.platform === "win32";
    function notFoundError(original, syscall) {
      return Object.assign(new Error(`${syscall} ${original.command} ENOENT`), {
        code: "ENOENT",
        errno: "ENOENT",
        syscall: `${syscall} ${original.command}`,
        path: original.command,
        spawnargs: original.args
      });
    }
    function hookChildProcess(cp, parsed) {
      if (!isWin) {
        return;
      }
      const originalEmit = cp.emit;
      cp.emit = function(name, arg1) {
        if (name === "exit") {
          const err = verifyENOENT(arg1, parsed);
          if (err) {
            return originalEmit.call(cp, "error", err);
          }
        }
        return originalEmit.apply(cp, arguments);
      };
    }
    function verifyENOENT(status, parsed) {
      if (isWin && status === 1 && !parsed.file) {
        return notFoundError(parsed.original, "spawn");
      }
      return null;
    }
    function verifyENOENTSync(status, parsed) {
      if (isWin && status === 1 && !parsed.file) {
        return notFoundError(parsed.original, "spawnSync");
      }
      return null;
    }
    module2.exports = {
      hookChildProcess,
      verifyENOENT,
      verifyENOENTSync,
      notFoundError
    };
  }
});

// ../../node_modules/cross-spawn/index.js
var require_cross_spawn = __commonJS({
  "../../node_modules/cross-spawn/index.js"(exports2, module2) {
    "use strict";
    var cp = require("child_process");
    var parse3 = require_parse();
    var enoent = require_enoent();
    function spawn3(command, args, options) {
      const parsed = parse3(command, args, options);
      const spawned = cp.spawn(parsed.command, parsed.args, parsed.options);
      enoent.hookChildProcess(spawned, parsed);
      return spawned;
    }
    function spawnSync(command, args, options) {
      const parsed = parse3(command, args, options);
      const result = cp.spawnSync(parsed.command, parsed.args, parsed.options);
      result.error = result.error || enoent.verifyENOENTSync(result.status, parsed);
      return result;
    }
    module2.exports = spawn3;
    module2.exports.spawn = spawn3;
    module2.exports.sync = spawnSync;
    module2.exports._parse = parse3;
    module2.exports._enoent = enoent;
  }
});

// bin/picktime-agentd.mjs
var import_node_crypto2 = __toESM(require("node:crypto"), 1);
var import_node_fs2 = __toESM(require("node:fs"), 1);
var import_node_http = __toESM(require("node:http"), 1);
var import_node_os2 = __toESM(require("node:os"), 1);
var import_node_path4 = __toESM(require("node:path"), 1);
var import_node_child_process = require("node:child_process");
var import_node_fs3 = require("node:fs");

// bin/picktime-command-policy.mjs
var import_node_path = __toESM(require("node:path"), 1);
var LEGACY_SHELL_BLOCKED_EXECUTABLES = Object.freeze([
  "cmd",
  "powershell",
  "pwsh",
  "sh",
  "bash",
  "zsh"
]);
var DEFAULT_BLOCKED_EXECUTABLES = Object.freeze(["rm -rf"]);
var DEFAULT_COMMAND_APPROVAL_PATTERNS = Object.freeze([
  "destructive-delete"
]);
var POWERSHELL_DELETE_COMMANDS = /* @__PURE__ */ new Set([
  "remove-item",
  "ri",
  "del",
  "erase",
  "rm"
]);
var CMD_DELETE_COMMANDS = /* @__PURE__ */ new Set(["rd", "rmdir", "del", "erase"]);
function normalizeWhitespace(value) {
  return String(value || "").replace(/\s+/g, " ").trim();
}
function normalizePattern(value) {
  return normalizeWhitespace(value).toLowerCase();
}
function hasUnixShortFlag(args, letter) {
  return args.some((arg) => {
    const normalized = String(arg || "").trim().toLowerCase();
    return /^-[a-z]+$/.test(normalized) && normalized.includes(letter);
  });
}
function hasAnyArgument(args, candidates) {
  const normalizedCandidates = candidates.map((item) => item.toLowerCase());
  return args.some((arg) => {
    const normalized = String(arg || "").trim().toLowerCase();
    return normalizedCandidates.some(
      (candidate) => normalized === candidate || normalized.startsWith(`${candidate}=`) || normalized.startsWith(`${candidate}:`)
    );
  });
}
function matchesUnixRecursiveForceDelete(context) {
  if (context.commandName !== "rm") {
    return false;
  }
  return hasUnixShortFlag(context.args, "r") && hasUnixShortFlag(context.args, "f") || hasAnyArgument(context.args, ["-r", "--recursive"]) && hasAnyArgument(context.args, ["-f", "--force"]);
}
function matchesPowerShellRecursiveForceDelete(context) {
  if (!POWERSHELL_DELETE_COMMANDS.has(context.commandName)) {
    return false;
  }
  return hasAnyArgument(context.args, ["-recurse", "-r"]) && hasAnyArgument(context.args, ["-force", "-fo", "-f"]);
}
function matchesCmdDelete(context) {
  if (!CMD_DELETE_COMMANDS.has(context.commandName)) {
    return false;
  }
  if (context.commandName === "rd" || context.commandName === "rmdir") {
    return hasAnyArgument(context.args, ["/s"]) && hasAnyArgument(context.args, ["/q"]);
  }
  return hasAnyArgument(context.args, ["/f"]) && hasAnyArgument(context.args, ["/q"]);
}
function matchesUnixRecursiveForceDeleteText(text) {
  return /\brm\b(?:(?=[^|;&\n]*\s-[a-z]*r[a-z]*f\b)|(?=[^|;&\n]*\s-[a-z]*f[a-z]*r\b)|(?=[^|;&\n]*\s-r\b)(?=[^|;&\n]*\s-f\b)|(?=[^|;&\n]*\s--recursive\b)(?=[^|;&\n]*\s--force\b))/i.test(
    text
  );
}
function matchesPowerShellRecursiveForceDeleteText(text) {
  return /\b(?:remove-item|ri|del|erase|rm)\b(?=[^|;&\n]*\s-(?:recurse|r)\b)(?=[^|;&\n]*\s-(?:force|fo|f)\b)/i.test(
    text
  );
}
function matchesCmdDeleteText(text) {
  return /\b(?:rd|rmdir)\b(?=[^|;&\n]*\s\/s\b)(?=[^|;&\n]*\s\/q\b)/i.test(text) || /\b(?:del|erase)\b(?=[^|;&\n]*\s\/f\b)(?=[^|;&\n]*\s\/q\b)/i.test(text);
}
function isRmRfPattern(pattern) {
  return normalizePattern(pattern) === "rm -rf";
}
function isDestructiveDeletePattern(pattern) {
  return normalizePattern(pattern) === "destructive-delete";
}
function matchesPattern(pattern, context) {
  const normalizedPattern = normalizePattern(pattern);
  if (!normalizedPattern) {
    return false;
  }
  if (isRmRfPattern(normalizedPattern)) {
    return matchesUnixRecursiveForceDelete(context) || matchesUnixRecursiveForceDeleteText(context.commandText);
  }
  if (isDestructiveDeletePattern(normalizedPattern)) {
    return matchesUnixRecursiveForceDelete(context) || matchesPowerShellRecursiveForceDelete(context) || matchesCmdDelete(context) || matchesUnixRecursiveForceDeleteText(context.commandText) || matchesPowerShellRecursiveForceDeleteText(context.commandText) || matchesCmdDeleteText(context.commandText);
  }
  if (/\s/.test(normalizedPattern)) {
    return context.commandText.includes(normalizedPattern);
  }
  return context.commandName === normalizedPattern;
}
function normalizeCommandPatternList(value) {
  const source = Array.isArray(value) ? value : [];
  const normalized = [];
  const seen = /* @__PURE__ */ new Set();
  for (const item of source) {
    const pattern = normalizePattern(item);
    if (!pattern || seen.has(pattern)) {
      continue;
    }
    seen.add(pattern);
    normalized.push(pattern);
  }
  return normalized;
}
function isLegacyShellBlockedList(value) {
  const normalized = normalizeCommandPatternList(value);
  return normalized.length === LEGACY_SHELL_BLOCKED_EXECUTABLES.length && LEGACY_SHELL_BLOCKED_EXECUTABLES.every((item) => normalized.includes(item));
}
function buildCommandPolicyContext({
  command = "",
  args = [],
  rawCommand = "",
  shell = "auto"
} = {}) {
  const normalizedCommand = String(command || "").trim();
  const normalizedArgs = Array.isArray(args) ? args.map((item) => String(item)) : [];
  const normalizedRawCommand = String(rawCommand || "").trim();
  const commandTextSource = normalizedRawCommand || [normalizedCommand, ...normalizedArgs].join(" ");
  return {
    command: normalizedCommand,
    args: normalizedArgs,
    rawCommand: normalizedRawCommand,
    shell: normalizePattern(shell || "auto") || "auto",
    commandName: import_node_path.default.basename(normalizedCommand).toLowerCase(),
    commandText: normalizePattern(commandTextSource)
  };
}
function commandMatchesAnyPattern(context, patterns = []) {
  return normalizeCommandPatternList(patterns).some(
    (pattern) => matchesPattern(pattern, context)
  );
}
function normalizeCommandPolicyConfig(commandPolicy = {}) {
  const hadBlockedExecutables = Array.isArray(
    commandPolicy?.blockedExecutables
  );
  const usedLegacyBlockedDefaults = !hadBlockedExecutables || isLegacyShellBlockedList(commandPolicy?.blockedExecutables || []);
  const blockedExecutables = usedLegacyBlockedDefaults ? [...DEFAULT_BLOCKED_EXECUTABLES] : normalizeCommandPatternList(commandPolicy?.blockedExecutables);
  const approvalPatterns = Array.isArray(commandPolicy?.approvalPatterns) ? normalizeCommandPatternList(commandPolicy.approvalPatterns) : [...DEFAULT_COMMAND_APPROVAL_PATTERNS];
  return {
    blockedExecutables,
    approvalPatterns,
    usedLegacyBlockedDefaults
  };
}

// bin/picktime-shared.mjs
var import_node_fs = __toESM(require("node:fs"), 1);
var import_node_os = __toESM(require("node:os"), 1);
var import_node_path2 = __toESM(require("node:path"), 1);
var stateDir = process.env.PICKTIME_HOME || import_node_path2.default.join(import_node_os.default.homedir(), ".picktime");
var skillsDir = import_node_path2.default.join(stateDir, "skills");
var configPath = import_node_path2.default.join(stateDir, "config.json");
var mcpConfigPath = import_node_path2.default.join(stateDir, "mcp.json");
var runtimePath = import_node_path2.default.join(stateDir, "runtime.json");
var logsDir = import_node_path2.default.join(stateDir, "logs");
var auditLogPath = import_node_path2.default.join(logsDir, "agentd-audit.jsonl");
var daemonLogPath = import_node_path2.default.join(logsDir, "agentd.log");
var venvsDir = import_node_path2.default.join(stateDir, "venvs");
var defaultDaemonPort = Number(
  process.env.PICKTIME_AGENTD_PORT || "43110"
);
var DEFAULT_CLIENT_VERSION = "0.0.21";
var FILESYSTEM_ACCESS_MODES = Object.freeze({
  standard: "standard",
  allowlist: "allowlist",
  unrestricted: "unrestricted"
});
var capabilityProfileCatalog = {
  white_collar: {
    label: "\u767D\u9886\u9ED8\u8BA4\u6863",
    description: "\u4F18\u5148\u8986\u76D6\u529E\u516C\u8D44\u6599\u6574\u7406\u548C\u7814\u7A76\u6C47\u603B\u3002",
    enabledPacks: ["office_bridge", "research_assistant"]
  },
  student: {
    label: "\u5B66\u751F\u9ED8\u8BA4\u6863",
    description: "\u4F18\u5148\u8986\u76D6\u8BFE\u7A0B\u8D44\u6599\u6574\u7406\u548C\u5B66\u4E60\u7814\u7A76\u3002",
    enabledPacks: ["learning_capture", "research_assistant"]
  },
  custom: {
    label: "\u81EA\u5B9A\u4E49",
    description: "\u4FDD\u7559\u624B\u52A8\u914D\u7F6E\u7684\u80FD\u529B\u5305\u7EC4\u5408\u3002",
    enabledPacks: ["office_bridge", "research_assistant"]
  }
};
var capabilityPackCatalog = {
  office_bridge: {
    label: "\u529E\u516C\u6587\u6863\u589E\u5F3A\u5305",
    description: "\u8865\u9F50 Word / Excel / PDF \u7B49\u9AD8\u683C\u5F0F\u8981\u6C42\u6587\u6863\u5904\u7406\u80FD\u529B\u3002",
    level: "P1",
    recommendedSkills: [
      { name: "Word / DOCX", slug: "word-docx" },
      { name: "Excel / XLSX", slug: "excel-xlsx" },
      { name: "Nano Pdf", slug: "nano-pdf" }
    ]
  },
  research_assistant: {
    label: "\u7814\u7A76\u4E0E\u603B\u7ED3\u5305",
    description: "\u8986\u76D6\u641C\u7D22\u3001\u603B\u7ED3\u548C\u97F3\u9891\u8F6C\u5199\u7B49\u9AD8\u9891\u7814\u7A76\u80FD\u529B\u3002",
    level: "P1",
    recommendedSkills: [
      { name: "Summarize" },
      { name: "Multi Search Engine" },
      { name: "Openai Whisper" }
    ]
  },
  learning_capture: {
    label: "\u5B66\u4E60\u6574\u7406\u5305",
    description: "\u9762\u5411\u8BFE\u7A0B\u8D44\u6599\u3001\u8BFE\u5802\u8BB0\u5F55\u548C\u4E2A\u4EBA\u77E5\u8BC6\u5E93\u6574\u7406\u3002",
    level: "P1",
    recommendedSkills: [
      { name: "Obsidian" },
      { name: "Summarize" },
      { name: "Openai Whisper" }
    ]
  },
  advanced_automation: {
    label: "\u9AD8\u7EA7\u81EA\u52A8\u5316\u6269\u5C55\u5305",
    description: "\u6D4F\u89C8\u5668\u81EA\u52A8\u5316\u548C\u5927\u6743\u9650\u8FDE\u63A5\u5668\uFF0C\u4EC5\u9002\u5408\u9AD8\u7EA7\u7528\u6237\u3002",
    level: "P2",
    adminOnly: true,
    recommendedSkills: [
      { name: "Agent Browser" },
      { name: "API Gateway" },
      { name: "mcporter" },
      { name: "Skill Vetter" },
      { name: "Find Skills" }
    ]
  }
};
function isPlainObject(value) {
  return Boolean(value) && typeof value === "object" && !Array.isArray(value);
}
function normalizeStringList(value) {
  if (!Array.isArray(value)) {
    return [];
  }
  const seen = /* @__PURE__ */ new Set();
  const out = [];
  for (const item of value) {
    const normalized = String(item || "").trim().toLowerCase();
    if (!normalized || seen.has(normalized)) {
      continue;
    }
    seen.add(normalized);
    out.push(normalized);
  }
  return out;
}
function normalizeFilesystemRootList(value) {
  if (!Array.isArray(value)) {
    return [];
  }
  const seen = /* @__PURE__ */ new Set();
  const roots = [];
  for (const item of value) {
    const rawValue = String(item || "").trim();
    if (!rawValue) {
      continue;
    }
    const normalizedRoot = import_node_path2.default.resolve(rawValue);
    const dedupeKey = normalizedRoot.toLowerCase();
    if (seen.has(dedupeKey)) {
      continue;
    }
    seen.add(dedupeKey);
    roots.push(normalizedRoot);
  }
  return roots;
}
function defaultFilesystemAllowedRoots(homeDir = import_node_os.default.homedir()) {
  return normalizeFilesystemRootList([
    import_node_path2.default.join(homeDir, "Desktop"),
    import_node_path2.default.join(homeDir, "Documents"),
    import_node_path2.default.join(homeDir, "Downloads"),
    stateDir
  ]);
}
function defaultProtectedWriteRoots({
  platform = process.platform,
  homeDir = import_node_os.default.homedir(),
  env = process.env
} = {}) {
  const roots = [];
  const addRoot = (value) => {
    const rawValue = String(value || "").trim();
    if (!rawValue) {
      return;
    }
    roots.push(rawValue);
  };
  if (platform === "win32") {
    const systemDriveRoot = import_node_path2.default.parse(homeDir).root || "C:\\";
    addRoot(
      env.SystemRoot || env.WINDIR || import_node_path2.default.join(systemDriveRoot, "Windows")
    );
    addRoot(env.ProgramFiles);
    addRoot(env["ProgramFiles(x86)"]);
    addRoot(env.ProgramW6432);
  } else if (platform === "darwin") {
    addRoot("/System");
    addRoot("/Library");
    addRoot("/Applications");
    addRoot("/bin");
    addRoot("/sbin");
    addRoot("/usr");
  } else {
    addRoot("/bin");
    addRoot("/sbin");
    addRoot("/usr");
    addRoot("/etc");
    addRoot("/lib");
    addRoot("/opt");
  }
  return normalizeFilesystemRootList(roots);
}
function normalizeFilesystemAccessMode(value) {
  const candidate = String(value || "").trim().toLowerCase();
  if (candidate === FILESYSTEM_ACCESS_MODES.allowlist || candidate === FILESYSTEM_ACCESS_MODES.unrestricted) {
    return candidate;
  }
  return FILESYSTEM_ACCESS_MODES.standard;
}
function rootsMatchExactly(leftRoots, rightRoots) {
  if (leftRoots.length !== rightRoots.length) {
    return false;
  }
  const rightSet = new Set(rightRoots.map((item) => item.toLowerCase()));
  return leftRoots.every((item) => rightSet.has(item.toLowerCase()));
}
function inferFilesystemAccessMode({
  rawMode,
  allowedRoots,
  legacyDefaultRoots
}) {
  if (typeof rawMode === "string" && rawMode.trim()) {
    return normalizeFilesystemAccessMode(rawMode);
  }
  if (allowedRoots.length === 0 || rootsMatchExactly(allowedRoots, legacyDefaultRoots)) {
    return FILESYSTEM_ACCESS_MODES.standard;
  }
  return FILESYSTEM_ACCESS_MODES.allowlist;
}
function normalizeFilesystemPolicyConfig(filesystemPolicy = {}) {
  const legacyDefaultRoots = defaultFilesystemAllowedRoots();
  const allowedRoots = normalizeFilesystemRootList(
    filesystemPolicy.allowedRoots
  );
  const mode = inferFilesystemAccessMode({
    rawMode: filesystemPolicy.mode,
    allowedRoots,
    legacyDefaultRoots
  });
  const allowedRootsProvided = Array.isArray(filesystemPolicy.allowedRoots);
  const protectedWriteRootsProvided = Array.isArray(
    filesystemPolicy.protectedWriteRoots
  );
  const normalizedProtectedWriteRoots = normalizeFilesystemRootList(
    filesystemPolicy.protectedWriteRoots
  );
  return {
    ...filesystemPolicy,
    mode,
    allowedRoots: allowedRoots.length > 0 || allowedRootsProvided && mode === FILESYSTEM_ACCESS_MODES.allowlist ? allowedRoots : legacyDefaultRoots,
    protectedWriteRoots: normalizedProtectedWriteRoots.length > 0 || protectedWriteRootsProvided ? normalizedProtectedWriteRoots : mode === FILESYSTEM_ACCESS_MODES.unrestricted ? [] : defaultProtectedWriteRoots(),
    requireApprovalForOverwrite: typeof filesystemPolicy.requireApprovalForOverwrite === "boolean" ? filesystemPolicy.requireApprovalForOverwrite : false,
    maxWriteBytes: Number(filesystemPolicy.maxWriteBytes || 1024 * 1024),
    maxReadBytes: Number(filesystemPolicy.maxReadBytes || 1024 * 1024),
    allowWatch: filesystemPolicy.allowWatch !== false
  };
}
function getFilesystemPathAccessDecision(targetPath, filesystemPolicy = {}, accessKind = "read") {
  const resolvedPath = import_node_path2.default.resolve(String(targetPath || ""));
  const normalizedPolicy = normalizeFilesystemPolicyConfig(filesystemPolicy);
  if (normalizedPolicy.mode !== FILESYSTEM_ACCESS_MODES.allowlist) {
    return {
      allowed: true,
      accessKind,
      mode: normalizedPolicy.mode,
      path: resolvedPath
    };
  }
  const matchedRoot = normalizedPolicy.allowedRoots.find(
    (rootPath) => isPathInsideRoot(resolvedPath, rootPath)
  );
  if (matchedRoot) {
    return {
      allowed: true,
      accessKind,
      mode: normalizedPolicy.mode,
      path: resolvedPath,
      matchedRoot
    };
  }
  return {
    allowed: false,
    accessKind,
    mode: normalizedPolicy.mode,
    path: resolvedPath,
    reason: "path_outside_allowed_roots"
  };
}
function isProtectedWritePath(targetPath, filesystemPolicy = {}) {
  const resolvedPath = import_node_path2.default.resolve(String(targetPath || ""));
  const normalizedPolicy = normalizeFilesystemPolicyConfig(filesystemPolicy);
  if (normalizedPolicy.mode === FILESYSTEM_ACCESS_MODES.unrestricted) {
    return false;
  }
  return normalizedPolicy.protectedWriteRoots.some(
    (rootPath) => isPathInsideRoot(resolvedPath, rootPath)
  );
}
function normalizeSkillCapabilityManifest(value) {
  if (!isPlainObject(value)) {
    return null;
  }
  const topics = normalizeStringList(value.topics);
  const defaultFormats = normalizeStringList(value.formats);
  const defaultEntrypoint = String(value.entrypoint || value.action || "").trim() || null;
  const rawActions = Array.isArray(value.actions) ? value.actions : normalizeStringList(value.operations).map((name) => ({ name }));
  const actions = [];
  const seen = /* @__PURE__ */ new Set();
  for (const rawAction of rawActions) {
    const normalizedAction = typeof rawAction === "string" ? { name: rawAction } : isPlainObject(rawAction) ? rawAction : null;
    if (!normalizedAction) {
      continue;
    }
    const name = String(
      normalizedAction.name || normalizedAction.id || normalizedAction.operation || ""
    ).trim().toLowerCase();
    if (!name || seen.has(name)) {
      continue;
    }
    seen.add(name);
    actions.push({
      name,
      description: typeof normalizedAction.description === "string" && normalizedAction.description.trim() ? normalizedAction.description.trim() : null,
      formats: normalizeStringList(normalizedAction.formats).length > 0 ? normalizeStringList(normalizedAction.formats) : defaultFormats,
      entrypoint: String(normalizedAction.entrypoint || defaultEntrypoint || "").trim() || null
    });
  }
  if (topics.length === 0 && actions.length === 0) {
    return null;
  }
  return {
    topics,
    actions
  };
}
function readSkillCapabilityManifest(skillRoot) {
  const skillJson = readJson(import_node_path2.default.join(skillRoot, "skill.json"), null);
  if (!isPlainObject(skillJson)) {
    return null;
  }
  return normalizeSkillCapabilityManifest(
    skillJson.picktime?.capabilities || skillJson.capabilities || null
  );
}
function normalizeCapabilityProfile(value) {
  const candidate = String(value || "white_collar").trim();
  return capabilityProfileCatalog[candidate] ? candidate : "white_collar";
}
function defaultCapabilityPacksForProfile(profile) {
  const normalizedProfile = normalizeCapabilityProfile(profile);
  return [
    ...capabilityProfileCatalog[normalizedProfile]?.enabledPacks || capabilityProfileCatalog.white_collar.enabledPacks
  ];
}
function normalizeCapabilityPacks(value, profile = "white_collar") {
  const fallback = defaultCapabilityPacksForProfile(profile);
  if (!Array.isArray(value)) {
    return fallback;
  }
  const normalized = [];
  const seen = /* @__PURE__ */ new Set();
  for (const item of value) {
    const packId = String(item || "").trim();
    if (!packId || !capabilityPackCatalog[packId] || seen.has(packId)) {
      continue;
    }
    normalized.push(packId);
    seen.add(packId);
  }
  return normalized.length > 0 ? normalized : fallback;
}
function listCapabilityProfiles() {
  return Object.entries(capabilityProfileCatalog).map(([id, profile]) => ({
    id,
    label: profile.label,
    description: profile.description,
    enabledPacks: [...profile.enabledPacks]
  }));
}
function listCapabilityPacks(enabledPacks = []) {
  const enabledSet = new Set(enabledPacks);
  return Object.entries(capabilityPackCatalog).map(([id, pack]) => ({
    id,
    label: pack.label,
    description: pack.description,
    level: pack.level,
    adminOnly: pack.adminOnly === true,
    enabled: enabledSet.has(id),
    recommendedSkills: (pack.recommendedSkills || []).map((item) => ({
      name: item.name,
      slug: item.slug || null
    }))
  }));
}
function listRecommendedSkills(enabledPacks = []) {
  const seen = /* @__PURE__ */ new Set();
  const skills = [];
  for (const packId of enabledPacks) {
    for (const skill of capabilityPackCatalog[packId]?.recommendedSkills || []) {
      const name = String(skill.name || "").trim();
      if (!name || seen.has(name)) {
        continue;
      }
      seen.add(name);
      skills.push({ name, slug: skill.slug || null, packId });
    }
  }
  return skills;
}
function getCapabilityPreferences(config2 = loadConfig()) {
  const capabilities = config2.capabilities || {};
  const profile = normalizeCapabilityProfile(capabilities.profile);
  const enabledPacks = normalizeCapabilityPacks(
    capabilities.enabledPacks,
    profile
  );
  return {
    profile,
    enabledPacks,
    profiles: listCapabilityProfiles(),
    packs: listCapabilityPacks(enabledPacks),
    recommendedSkills: listRecommendedSkills(enabledPacks)
  };
}
function setCapabilityPreferences(nextPreferences = {}) {
  const config2 = loadConfig();
  config2.capabilities ||= {};
  const profile = normalizeCapabilityProfile(
    nextPreferences.profile ?? config2.capabilities.profile
  );
  const enabledPacks = normalizeCapabilityPacks(
    nextPreferences.enabledPacks ?? config2.capabilities.enabledPacks,
    profile
  );
  config2.capabilities.profile = profile;
  delete config2.capabilities.fileMode;
  config2.capabilities.enabledPacks = enabledPacks;
  saveConfig(config2);
  return getCapabilityPreferences(config2);
}
function defaultPolicies() {
  return {
    commands: {
      blockedExecutables: [...DEFAULT_BLOCKED_EXECUTABLES],
      approvalPatterns: [...DEFAULT_COMMAND_APPROVAL_PATTERNS],
      allowedExecutables: [],
      maxOutputBytes: 65536,
      maxStoredBytes: 4 * 1024 * 1024,
      defaultTimeoutMs: 12e4,
      defaultNoOutputTimeoutMs: 6e4,
      forceKillGraceMs: 2e3,
      maxEnvVars: 64
    },
    filesystem: normalizeFilesystemPolicyConfig(),
    processes: {
      maxOutputBytes: 65536,
      maxStoredBytes: 16 * 1024 * 1024,
      defaultTimeoutMs: 24 * 60 * 60 * 1e3,
      defaultNoOutputTimeoutMs: 6e4,
      finishedEntryTtlMs: 30 * 60 * 1e3,
      forceKillGraceMs: 2e3
    },
    system: {
      clipboardEnabled: true,
      notificationsEnabled: true
    }
  };
}
function ensureDir(dir) {
  import_node_fs.default.mkdirSync(dir, { recursive: true });
}
function readJson(file2, fallback) {
  try {
    return JSON.parse(import_node_fs.default.readFileSync(file2, "utf8"));
  } catch {
    return fallback;
  }
}
function writeJson(file2, value) {
  ensureDir(import_node_path2.default.dirname(file2));
  import_node_fs.default.writeFileSync(file2, JSON.stringify(value, null, 2));
}
function normalizeLoadedConfig(config2) {
  const normalized = config2 && typeof config2 === "object" ? { ...config2 } : {};
  const defaults = defaultPolicies();
  normalized.device ||= {};
  normalized.daemon ||= {};
  normalized.aiProviders ||= [];
  normalized.approvals ||= { autoApprove: false };
  normalized.capabilities ||= {};
  normalized.policies ||= {};
  normalized.policies.commands ||= {};
  normalized.policies.filesystem ||= {};
  normalized.policies.processes ||= {};
  normalized.policies.system ||= {};
  normalized.skills ||= { entries: {} };
  normalized.skills.entries ||= {};
  const normalizedCommandPolicy = normalizeCommandPolicyConfig(
    normalized.policies.commands
  );
  normalized.policies.commands = {
    ...defaults.commands,
    ...normalized.policies.commands,
    blockedExecutables: normalizedCommandPolicy.blockedExecutables,
    approvalPatterns: normalizedCommandPolicy.approvalPatterns
  };
  normalized.policies.filesystem = normalizeFilesystemPolicyConfig(
    normalized.policies.filesystem
  );
  if (normalizedCommandPolicy.usedLegacyBlockedDefaults) {
    normalized.policies.filesystem.requireApprovalForOverwrite = false;
  }
  normalized.policies.processes = {
    ...defaults.processes,
    ...normalized.policies.processes
  };
  normalized.policies.system = {
    ...defaults.system,
    ...normalized.policies.system
  };
  return normalized;
}
function loadConfig() {
  return normalizeLoadedConfig(
    readJson(configPath, {
      debug: false,
      device: {
        serverUrl: "",
        deviceId: "",
        deviceToken: "",
        name: import_node_os.default.hostname(),
        platform: process.platform,
        deviceFamily: process.platform === "darwin" ? "mac" : process.platform,
        clientVersion: DEFAULT_CLIENT_VERSION,
        autoConnect: true,
        heartbeatIntervalMs: 3e4
      },
      daemon: {
        port: defaultDaemonPort
      },
      aiProviders: [],
      approvals: {
        autoApprove: false
      },
      capabilities: {
        profile: "white_collar",
        enabledPacks: defaultCapabilityPacksForProfile("white_collar")
      },
      policies: defaultPolicies(),
      skills: {
        entries: {}
      }
    })
  );
}
function saveConfig(config2) {
  writeJson(configPath, config2);
}
function loadMcpConfig() {
  return readJson(mcpConfigPath, { mcpServers: {} });
}
function saveMcpConfig(config2) {
  writeJson(mcpConfigPath, config2);
}
function saveRuntime(runtime) {
  writeJson(runtimePath, runtime);
}
function clearRuntime() {
  try {
    import_node_fs.default.unlinkSync(runtimePath);
  } catch {
  }
}
function listSkillDirs() {
  ensureDir(skillsDir);
  return import_node_fs.default.readdirSync(skillsDir, { withFileTypes: true }).filter((entry) => entry.isDirectory()).filter(
    (entry) => import_node_fs.default.existsSync(import_node_path2.default.join(skillsDir, entry.name, "SKILL.md"))
  ).map((entry) => entry.name).sort();
}
function copyDirRecursive(source, target) {
  ensureDir(target);
  for (const entry of import_node_fs.default.readdirSync(source, { withFileTypes: true })) {
    const sourcePath = import_node_path2.default.join(source, entry.name);
    const targetPath = import_node_path2.default.join(target, entry.name);
    if (entry.isDirectory()) {
      copyDirRecursive(sourcePath, targetPath);
    } else {
      import_node_fs.default.copyFileSync(sourcePath, targetPath);
    }
  }
}
function getSkillEntry(name, config2 = loadConfig()) {
  const entry = config2.skills?.entries?.[name] || {};
  const skillPath = import_node_path2.default.join(skillsDir, name);
  const configuredCapabilities = normalizeSkillCapabilityManifest(
    entry.capabilities || null
  );
  const manifestCapabilities = readSkillCapabilityManifest(skillPath);
  return {
    name,
    label: entry.label || null,
    description: entry.description || null,
    enabled: entry.enabled !== false,
    source: entry.source || "managed",
    path: skillPath,
    slug: entry.slug || null,
    owner: entry.owner || null,
    version: entry.version || null,
    moderation: entry.moderation || null,
    installed_at: entry.installed_at || null,
    runtime: entry.runtime || null,
    runner: entry.runner || null,
    capabilities: configuredCapabilities || manifestCapabilities
  };
}
function updateSkillEntry(name, patch) {
  const config2 = loadConfig();
  config2.skills ||= {};
  config2.skills.entries ||= {};
  config2.skills.entries[name] ||= {};
  Object.assign(config2.skills.entries[name], patch || {});
  saveConfig(config2);
  return getSkillEntry(name, config2);
}
function removeSkillEntry(name) {
  const config2 = loadConfig();
  const entries = config2.skills?.entries;
  if (!entries || !Object.prototype.hasOwnProperty.call(entries, name)) {
    return false;
  }
  delete entries[name];
  if (Object.keys(entries).length === 0) {
    delete config2.skills.entries;
  }
  if (config2.skills && Object.keys(config2.skills).length === 0) {
    delete config2.skills;
  }
  saveConfig(config2);
  return true;
}
function setSkillEnabled(name, enabled) {
  return updateSkillEntry(name, {
    enabled
  });
}
function setSkillRunner(name, runner) {
  return updateSkillEntry(name, {
    runner
  });
}
function readSkillRunner(name) {
  const entry = getSkillEntry(name);
  if (entry.runner?.command) {
    return entry.runner;
  }
  const skillJsonPath = import_node_path2.default.join(entry.path, "skill.json");
  const skillJson = readJson(skillJsonPath, null);
  if (skillJson?.runner?.command) {
    return skillJson.runner;
  }
  return null;
}
function listManagedMcpServers() {
  const config2 = loadMcpConfig();
  return Object.entries(config2.mcpServers || {}).map(([name, value]) => ({
    name,
    ...value || {},
    enabled: value?.enabled !== false,
    source: value?.source || "managed"
  })).sort((a, b) => a.name.localeCompare(b.name));
}
function toCapabilityItem(name, value = {}) {
  return {
    name,
    label: value.label || name,
    description: value.description || null,
    enabled: value.enabled !== false,
    metadata: value.metadata || null
  };
}
function truncateOutput(text, maxBytes) {
  const buffer = Buffer.from(String(text || ""), "utf8");
  if (buffer.length <= maxBytes) {
    return buffer.toString("utf8");
  }
  return buffer.subarray(0, maxBytes).toString("utf8");
}
function normalizeServerUrl(serverUrl) {
  return String(serverUrl || "").replace(/\/+$/, "");
}
function websocketUrlForServer(serverUrl) {
  const base = normalizeServerUrl(serverUrl);
  if (!base) {
    return "";
  }
  if (base.startsWith("https://")) {
    return `wss://${base.slice("https://".length)}/api/v1/devices/ws/control`;
  }
  if (base.startsWith("http://")) {
    return `ws://${base.slice("http://".length)}/api/v1/devices/ws/control`;
  }
  return `${base}/api/v1/devices/ws/control`;
}
function appendLine(filePath, value) {
  ensureDir(import_node_path2.default.dirname(filePath));
  import_node_fs.default.appendFileSync(filePath, `${value}
`);
}
function isDebugMode() {
  try {
    const config2 = loadConfig();
    return config2.debug === true;
  } catch {
    return false;
  }
}
function appendAudit(event, payload = {}, debugPayload = null) {
  const debug = isDebugMode();
  const entry = {
    ts: (/* @__PURE__ */ new Date()).toISOString(),
    event,
    ...payload
  };
  if (debug && debugPayload) {
    Object.assign(entry, debugPayload);
  }
  appendLine(auditLogPath, JSON.stringify(entry));
}
function appendDaemonLog(level, message, payload = {}) {
  appendLine(
    daemonLogPath,
    JSON.stringify({
      ts: (/* @__PURE__ */ new Date()).toISOString(),
      level,
      message,
      ...payload
    })
  );
}
function isPathInsideRoot(targetPath, rootPath) {
  const relative = import_node_path2.default.relative(
    import_node_path2.default.resolve(rootPath),
    import_node_path2.default.resolve(targetPath)
  );
  return relative === "" || !relative.startsWith("..") && !import_node_path2.default.isAbsolute(relative);
}

// bin/picktime-mcp-runtime.mjs
var import_node_crypto = __toESM(require("node:crypto"), 1);
var import_node_path3 = __toESM(require("node:path"), 1);
var import_node_url = require("node:url");

// ../../node_modules/zod/v4/core/core.js
var NEVER = Object.freeze({
  status: "aborted"
});
// @__NO_SIDE_EFFECTS__
function $constructor(name, initializer3, params) {
  function init(inst, def) {
    if (!inst._zod) {
      Object.defineProperty(inst, "_zod", {
        value: {
          def,
          constr: _,
          traits: /* @__PURE__ */ new Set()
        },
        enumerable: false
      });
    }
    if (inst._zod.traits.has(name)) {
      return;
    }
    inst._zod.traits.add(name);
    initializer3(inst, def);
    const proto = _.prototype;
    const keys = Object.keys(proto);
    for (let i = 0; i < keys.length; i++) {
      const k = keys[i];
      if (!(k in inst)) {
        inst[k] = proto[k].bind(inst);
      }
    }
  }
  const Parent = params?.Parent ?? Object;
  class Definition extends Parent {
  }
  Object.defineProperty(Definition, "name", { value: name });
  function _(def) {
    var _a2;
    const inst = params?.Parent ? new Definition() : this;
    init(inst, def);
    (_a2 = inst._zod).deferred ?? (_a2.deferred = []);
    for (const fn of inst._zod.deferred) {
      fn();
    }
    return inst;
  }
  Object.defineProperty(_, "init", { value: init });
  Object.defineProperty(_, Symbol.hasInstance, {
    value: (inst) => {
      if (params?.Parent && inst instanceof params.Parent)
        return true;
      return inst?._zod?.traits?.has(name);
    }
  });
  Object.defineProperty(_, "name", { value: name });
  return _;
}
var $ZodAsyncError = class extends Error {
  constructor() {
    super(`Encountered Promise during synchronous parse. Use .parseAsync() instead.`);
  }
};
var $ZodEncodeError = class extends Error {
  constructor(name) {
    super(`Encountered unidirectional transform during encode: ${name}`);
    this.name = "ZodEncodeError";
  }
};
var globalConfig = {};
function config(newConfig) {
  if (newConfig)
    Object.assign(globalConfig, newConfig);
  return globalConfig;
}

// ../../node_modules/zod/v4/core/util.js
var util_exports = {};
__export(util_exports, {
  BIGINT_FORMAT_RANGES: () => BIGINT_FORMAT_RANGES,
  Class: () => Class,
  NUMBER_FORMAT_RANGES: () => NUMBER_FORMAT_RANGES,
  aborted: () => aborted,
  allowsEval: () => allowsEval,
  assert: () => assert,
  assertEqual: () => assertEqual,
  assertIs: () => assertIs,
  assertNever: () => assertNever,
  assertNotEqual: () => assertNotEqual,
  assignProp: () => assignProp,
  base64ToUint8Array: () => base64ToUint8Array,
  base64urlToUint8Array: () => base64urlToUint8Array,
  cached: () => cached,
  captureStackTrace: () => captureStackTrace,
  cleanEnum: () => cleanEnum,
  cleanRegex: () => cleanRegex,
  clone: () => clone,
  cloneDef: () => cloneDef,
  createTransparentProxy: () => createTransparentProxy,
  defineLazy: () => defineLazy,
  esc: () => esc,
  escapeRegex: () => escapeRegex,
  extend: () => extend,
  finalizeIssue: () => finalizeIssue,
  floatSafeRemainder: () => floatSafeRemainder,
  getElementAtPath: () => getElementAtPath,
  getEnumValues: () => getEnumValues,
  getLengthableOrigin: () => getLengthableOrigin,
  getParsedType: () => getParsedType,
  getSizableOrigin: () => getSizableOrigin,
  hexToUint8Array: () => hexToUint8Array,
  isObject: () => isObject,
  isPlainObject: () => isPlainObject2,
  issue: () => issue,
  joinValues: () => joinValues,
  jsonStringifyReplacer: () => jsonStringifyReplacer,
  merge: () => merge,
  mergeDefs: () => mergeDefs,
  normalizeParams: () => normalizeParams,
  nullish: () => nullish,
  numKeys: () => numKeys,
  objectClone: () => objectClone,
  omit: () => omit,
  optionalKeys: () => optionalKeys,
  parsedType: () => parsedType,
  partial: () => partial,
  pick: () => pick,
  prefixIssues: () => prefixIssues,
  primitiveTypes: () => primitiveTypes,
  promiseAllObject: () => promiseAllObject,
  propertyKeyTypes: () => propertyKeyTypes,
  randomString: () => randomString,
  required: () => required,
  safeExtend: () => safeExtend,
  shallowClone: () => shallowClone,
  slugify: () => slugify,
  stringifyPrimitive: () => stringifyPrimitive,
  uint8ArrayToBase64: () => uint8ArrayToBase64,
  uint8ArrayToBase64url: () => uint8ArrayToBase64url,
  uint8ArrayToHex: () => uint8ArrayToHex,
  unwrapMessage: () => unwrapMessage
});
function assertEqual(val) {
  return val;
}
function assertNotEqual(val) {
  return val;
}
function assertIs(_arg) {
}
function assertNever(_x) {
  throw new Error("Unexpected value in exhaustive check");
}
function assert(_) {
}
function getEnumValues(entries) {
  const numericValues = Object.values(entries).filter((v) => typeof v === "number");
  const values = Object.entries(entries).filter(([k, _]) => numericValues.indexOf(+k) === -1).map(([_, v]) => v);
  return values;
}
function joinValues(array2, separator = "|") {
  return array2.map((val) => stringifyPrimitive(val)).join(separator);
}
function jsonStringifyReplacer(_, value) {
  if (typeof value === "bigint")
    return value.toString();
  return value;
}
function cached(getter) {
  const set2 = false;
  return {
    get value() {
      if (!set2) {
        const value = getter();
        Object.defineProperty(this, "value", { value });
        return value;
      }
      throw new Error("cached value already set");
    }
  };
}
function nullish(input) {
  return input === null || input === void 0;
}
function cleanRegex(source) {
  const start = source.startsWith("^") ? 1 : 0;
  const end = source.endsWith("$") ? source.length - 1 : source.length;
  return source.slice(start, end);
}
function floatSafeRemainder(val, step) {
  const valDecCount = (val.toString().split(".")[1] || "").length;
  const stepString = step.toString();
  let stepDecCount = (stepString.split(".")[1] || "").length;
  if (stepDecCount === 0 && /\d?e-\d?/.test(stepString)) {
    const match = stepString.match(/\d?e-(\d?)/);
    if (match?.[1]) {
      stepDecCount = Number.parseInt(match[1]);
    }
  }
  const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
  const valInt = Number.parseInt(val.toFixed(decCount).replace(".", ""));
  const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", ""));
  return valInt % stepInt / 10 ** decCount;
}
var EVALUATING = /* @__PURE__ */ Symbol("evaluating");
function defineLazy(object2, key, getter) {
  let value = void 0;
  Object.defineProperty(object2, key, {
    get() {
      if (value === EVALUATING) {
        return void 0;
      }
      if (value === void 0) {
        value = EVALUATING;
        value = getter();
      }
      return value;
    },
    set(v) {
      Object.defineProperty(object2, key, {
        value: v
        // configurable: true,
      });
    },
    configurable: true
  });
}
function objectClone(obj) {
  return Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
}
function assignProp(target, prop, value) {
  Object.defineProperty(target, prop, {
    value,
    writable: true,
    enumerable: true,
    configurable: true
  });
}
function mergeDefs(...defs) {
  const mergedDescriptors = {};
  for (const def of defs) {
    const descriptors = Object.getOwnPropertyDescriptors(def);
    Object.assign(mergedDescriptors, descriptors);
  }
  return Object.defineProperties({}, mergedDescriptors);
}
function cloneDef(schema) {
  return mergeDefs(schema._zod.def);
}
function getElementAtPath(obj, path5) {
  if (!path5)
    return obj;
  return path5.reduce((acc, key) => acc?.[key], obj);
}
function promiseAllObject(promisesObj) {
  const keys = Object.keys(promisesObj);
  const promises = keys.map((key) => promisesObj[key]);
  return Promise.all(promises).then((results) => {
    const resolvedObj = {};
    for (let i = 0; i < keys.length; i++) {
      resolvedObj[keys[i]] = results[i];
    }
    return resolvedObj;
  });
}
function randomString(length = 10) {
  const chars = "abcdefghijklmnopqrstuvwxyz";
  let str = "";
  for (let i = 0; i < length; i++) {
    str += chars[Math.floor(Math.random() * chars.length)];
  }
  return str;
}
function esc(str) {
  return JSON.stringify(str);
}
function slugify(input) {
  return input.toLowerCase().trim().replace(/[^\w\s-]/g, "").replace(/[\s_-]+/g, "-").replace(/^-+|-+$/g, "");
}
var captureStackTrace = "captureStackTrace" in Error ? Error.captureStackTrace : (..._args) => {
};
function isObject(data) {
  return typeof data === "object" && data !== null && !Array.isArray(data);
}
var allowsEval = cached(() => {
  if (typeof navigator !== "undefined" && navigator?.userAgent?.includes("Cloudflare")) {
    return false;
  }
  try {
    const F = Function;
    new F("");
    return true;
  } catch (_) {
    return false;
  }
});
function isPlainObject2(o) {
  if (isObject(o) === false)
    return false;
  const ctor = o.constructor;
  if (ctor === void 0)
    return true;
  if (typeof ctor !== "function")
    return true;
  const prot = ctor.prototype;
  if (isObject(prot) === false)
    return false;
  if (Object.prototype.hasOwnProperty.call(prot, "isPrototypeOf") === false) {
    return false;
  }
  return true;
}
function shallowClone(o) {
  if (isPlainObject2(o))
    return { ...o };
  if (Array.isArray(o))
    return [...o];
  return o;
}
function numKeys(data) {
  let keyCount = 0;
  for (const key in data) {
    if (Object.prototype.hasOwnProperty.call(data, key)) {
      keyCount++;
    }
  }
  return keyCount;
}
var getParsedType = (data) => {
  const t = typeof data;
  switch (t) {
    case "undefined":
      return "undefined";
    case "string":
      return "string";
    case "number":
      return Number.isNaN(data) ? "nan" : "number";
    case "boolean":
      return "boolean";
    case "function":
      return "function";
    case "bigint":
      return "bigint";
    case "symbol":
      return "symbol";
    case "object":
      if (Array.isArray(data)) {
        return "array";
      }
      if (data === null) {
        return "null";
      }
      if (data.then && typeof data.then === "function" && data.catch && typeof data.catch === "function") {
        return "promise";
      }
      if (typeof Map !== "undefined" && data instanceof Map) {
        return "map";
      }
      if (typeof Set !== "undefined" && data instanceof Set) {
        return "set";
      }
      if (typeof Date !== "undefined" && data instanceof Date) {
        return "date";
      }
      if (typeof File !== "undefined" && data instanceof File) {
        return "file";
      }
      return "object";
    default:
      throw new Error(`Unknown data type: ${t}`);
  }
};
var propertyKeyTypes = /* @__PURE__ */ new Set(["string", "number", "symbol"]);
var primitiveTypes = /* @__PURE__ */ new Set(["string", "number", "bigint", "boolean", "symbol", "undefined"]);
function escapeRegex(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function clone(inst, def, params) {
  const cl = new inst._zod.constr(def ?? inst._zod.def);
  if (!def || params?.parent)
    cl._zod.parent = inst;
  return cl;
}
function normalizeParams(_params) {
  const params = _params;
  if (!params)
    return {};
  if (typeof params === "string")
    return { error: () => params };
  if (params?.message !== void 0) {
    if (params?.error !== void 0)
      throw new Error("Cannot specify both `message` and `error` params");
    params.error = params.message;
  }
  delete params.message;
  if (typeof params.error === "string")
    return { ...params, error: () => params.error };
  return params;
}
function createTransparentProxy(getter) {
  let target;
  return new Proxy({}, {
    get(_, prop, receiver) {
      target ?? (target = getter());
      return Reflect.get(target, prop, receiver);
    },
    set(_, prop, value, receiver) {
      target ?? (target = getter());
      return Reflect.set(target, prop, value, receiver);
    },
    has(_, prop) {
      target ?? (target = getter());
      return Reflect.has(target, prop);
    },
    deleteProperty(_, prop) {
      target ?? (target = getter());
      return Reflect.deleteProperty(target, prop);
    },
    ownKeys(_) {
      target ?? (target = getter());
      return Reflect.ownKeys(target);
    },
    getOwnPropertyDescriptor(_, prop) {
      target ?? (target = getter());
      return Reflect.getOwnPropertyDescriptor(target, prop);
    },
    defineProperty(_, prop, descriptor) {
      target ?? (target = getter());
      return Reflect.defineProperty(target, prop, descriptor);
    }
  });
}
function stringifyPrimitive(value) {
  if (typeof value === "bigint")
    return value.toString() + "n";
  if (typeof value === "string")
    return `"${value}"`;
  return `${value}`;
}
function optionalKeys(shape) {
  return Object.keys(shape).filter((k) => {
    return shape[k]._zod.optin === "optional" && shape[k]._zod.optout === "optional";
  });
}
var NUMBER_FORMAT_RANGES = {
  safeint: [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER],
  int32: [-2147483648, 2147483647],
  uint32: [0, 4294967295],
  float32: [-34028234663852886e22, 34028234663852886e22],
  float64: [-Number.MAX_VALUE, Number.MAX_VALUE]
};
var BIGINT_FORMAT_RANGES = {
  int64: [/* @__PURE__ */ BigInt("-9223372036854775808"), /* @__PURE__ */ BigInt("9223372036854775807")],
  uint64: [/* @__PURE__ */ BigInt(0), /* @__PURE__ */ BigInt("18446744073709551615")]
};
function pick(schema, mask) {
  const currDef = schema._zod.def;
  const checks = currDef.checks;
  const hasChecks = checks && checks.length > 0;
  if (hasChecks) {
    throw new Error(".pick() cannot be used on object schemas containing refinements");
  }
  const def = mergeDefs(schema._zod.def, {
    get shape() {
      const newShape = {};
      for (const key in mask) {
        if (!(key in currDef.shape)) {
          throw new Error(`Unrecognized key: "${key}"`);
        }
        if (!mask[key])
          continue;
        newShape[key] = currDef.shape[key];
      }
      assignProp(this, "shape", newShape);
      return newShape;
    },
    checks: []
  });
  return clone(schema, def);
}
function omit(schema, mask) {
  const currDef = schema._zod.def;
  const checks = currDef.checks;
  const hasChecks = checks && checks.length > 0;
  if (hasChecks) {
    throw new Error(".omit() cannot be used on object schemas containing refinements");
  }
  const def = mergeDefs(schema._zod.def, {
    get shape() {
      const newShape = { ...schema._zod.def.shape };
      for (const key in mask) {
        if (!(key in currDef.shape)) {
          throw new Error(`Unrecognized key: "${key}"`);
        }
        if (!mask[key])
          continue;
        delete newShape[key];
      }
      assignProp(this, "shape", newShape);
      return newShape;
    },
    checks: []
  });
  return clone(schema, def);
}
function extend(schema, shape) {
  if (!isPlainObject2(shape)) {
    throw new Error("Invalid input to extend: expected a plain object");
  }
  const checks = schema._zod.def.checks;
  const hasChecks = checks && checks.length > 0;
  if (hasChecks) {
    const existingShape = schema._zod.def.shape;
    for (const key in shape) {
      if (Object.getOwnPropertyDescriptor(existingShape, key) !== void 0) {
        throw new Error("Cannot overwrite keys on object schemas containing refinements. Use `.safeExtend()` instead.");
      }
    }
  }
  const def = mergeDefs(schema._zod.def, {
    get shape() {
      const _shape = { ...schema._zod.def.shape, ...shape };
      assignProp(this, "shape", _shape);
      return _shape;
    }
  });
  return clone(schema, def);
}
function safeExtend(schema, shape) {
  if (!isPlainObject2(shape)) {
    throw new Error("Invalid input to safeExtend: expected a plain object");
  }
  const def = mergeDefs(schema._zod.def, {
    get shape() {
      const _shape = { ...schema._zod.def.shape, ...shape };
      assignProp(this, "shape", _shape);
      return _shape;
    }
  });
  return clone(schema, def);
}
function merge(a, b) {
  const def = mergeDefs(a._zod.def, {
    get shape() {
      const _shape = { ...a._zod.def.shape, ...b._zod.def.shape };
      assignProp(this, "shape", _shape);
      return _shape;
    },
    get catchall() {
      return b._zod.def.catchall;
    },
    checks: []
    // delete existing checks
  });
  return clone(a, def);
}
function partial(Class2, schema, mask) {
  const currDef = schema._zod.def;
  const checks = currDef.checks;
  const hasChecks = checks && checks.length > 0;
  if (hasChecks) {
    throw new Error(".partial() cannot be used on object schemas containing refinements");
  }
  const def = mergeDefs(schema._zod.def, {
    get shape() {
      const oldShape = schema._zod.def.shape;
      const shape = { ...oldShape };
      if (mask) {
        for (const key in mask) {
          if (!(key in oldShape)) {
            throw new Error(`Unrecognized key: "${key}"`);
          }
          if (!mask[key])
            continue;
          shape[key] = Class2 ? new Class2({
            type: "optional",
            innerType: oldShape[key]
          }) : oldShape[key];
        }
      } else {
        for (const key in oldShape) {
          shape[key] = Class2 ? new Class2({
            type: "optional",
            innerType: oldShape[key]
          }) : oldShape[key];
        }
      }
      assignProp(this, "shape", shape);
      return shape;
    },
    checks: []
  });
  return clone(schema, def);
}
function required(Class2, schema, mask) {
  const def = mergeDefs(schema._zod.def, {
    get shape() {
      const oldShape = schema._zod.def.shape;
      const shape = { ...oldShape };
      if (mask) {
        for (const key in mask) {
          if (!(key in shape)) {
            throw new Error(`Unrecognized key: "${key}"`);
          }
          if (!mask[key])
            continue;
          shape[key] = new Class2({
            type: "nonoptional",
            innerType: oldShape[key]
          });
        }
      } else {
        for (const key in oldShape) {
          shape[key] = new Class2({
            type: "nonoptional",
            innerType: oldShape[key]
          });
        }
      }
      assignProp(this, "shape", shape);
      return shape;
    }
  });
  return clone(schema, def);
}
function aborted(x, startIndex = 0) {
  if (x.aborted === true)
    return true;
  for (let i = startIndex; i < x.issues.length; i++) {
    if (x.issues[i]?.continue !== true) {
      return true;
    }
  }
  return false;
}
function prefixIssues(path5, issues) {
  return issues.map((iss) => {
    var _a2;
    (_a2 = iss).path ?? (_a2.path = []);
    iss.path.unshift(path5);
    return iss;
  });
}
function unwrapMessage(message) {
  return typeof message === "string" ? message : message?.message;
}
function finalizeIssue(iss, ctx, config2) {
  const full = { ...iss, path: iss.path ?? [] };
  if (!iss.message) {
    const message = unwrapMessage(iss.inst?._zod.def?.error?.(iss)) ?? unwrapMessage(ctx?.error?.(iss)) ?? unwrapMessage(config2.customError?.(iss)) ?? unwrapMessage(config2.localeError?.(iss)) ?? "Invalid input";
    full.message = message;
  }
  delete full.inst;
  delete full.continue;
  if (!ctx?.reportInput) {
    delete full.input;
  }
  return full;
}
function getSizableOrigin(input) {
  if (input instanceof Set)
    return "set";
  if (input instanceof Map)
    return "map";
  if (input instanceof File)
    return "file";
  return "unknown";
}
function getLengthableOrigin(input) {
  if (Array.isArray(input))
    return "array";
  if (typeof input === "string")
    return "string";
  return "unknown";
}
function parsedType(data) {
  const t = typeof data;
  switch (t) {
    case "number": {
      return Number.isNaN(data) ? "nan" : "number";
    }
    case "object": {
      if (data === null) {
        return "null";
      }
      if (Array.isArray(data)) {
        return "array";
      }
      const obj = data;
      if (obj && Object.getPrototypeOf(obj) !== Object.prototype && "constructor" in obj && obj.constructor) {
        return obj.constructor.name;
      }
    }
  }
  return t;
}
function issue(...args) {
  const [iss, input, inst] = args;
  if (typeof iss === "string") {
    return {
      message: iss,
      code: "custom",
      input,
      inst
    };
  }
  return { ...iss };
}
function cleanEnum(obj) {
  return Object.entries(obj).filter(([k, _]) => {
    return Number.isNaN(Number.parseInt(k, 10));
  }).map((el) => el[1]);
}
function base64ToUint8Array(base643) {
  const binaryString = atob(base643);
  const bytes = new Uint8Array(binaryString.length);
  for (let i = 0; i < binaryString.length; i++) {
    bytes[i] = binaryString.charCodeAt(i);
  }
  return bytes;
}
function uint8ArrayToBase64(bytes) {
  let binaryString = "";
  for (let i = 0; i < bytes.length; i++) {
    binaryString += String.fromCharCode(bytes[i]);
  }
  return btoa(binaryString);
}
function base64urlToUint8Array(base64url3) {
  const base643 = base64url3.replace(/-/g, "+").replace(/_/g, "/");
  const padding = "=".repeat((4 - base643.length % 4) % 4);
  return base64ToUint8Array(base643 + padding);
}
function uint8ArrayToBase64url(bytes) {
  return uint8ArrayToBase64(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
}
function hexToUint8Array(hex3) {
  const cleanHex = hex3.replace(/^0x/, "");
  if (cleanHex.length % 2 !== 0) {
    throw new Error("Invalid hex string length");
  }
  const bytes = new Uint8Array(cleanHex.length / 2);
  for (let i = 0; i < cleanHex.length; i += 2) {
    bytes[i / 2] = Number.parseInt(cleanHex.slice(i, i + 2), 16);
  }
  return bytes;
}
function uint8ArrayToHex(bytes) {
  return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
}
var Class = class {
  constructor(..._args) {
  }
};

// ../../node_modules/zod/v4/core/errors.js
var initializer = (inst, def) => {
  inst.name = "$ZodError";
  Object.defineProperty(inst, "_zod", {
    value: inst._zod,
    enumerable: false
  });
  Object.defineProperty(inst, "issues", {
    value: def,
    enumerable: false
  });
  inst.message = JSON.stringify(def, jsonStringifyReplacer, 2);
  Object.defineProperty(inst, "toString", {
    value: () => inst.message,
    enumerable: false
  });
};
var $ZodError = $constructor("$ZodError", initializer);
var $ZodRealError = $constructor("$ZodError", initializer, { Parent: Error });
function flattenError(error2, mapper = (issue2) => issue2.message) {
  const fieldErrors = {};
  const formErrors = [];
  for (const sub of error2.issues) {
    if (sub.path.length > 0) {
      fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
      fieldErrors[sub.path[0]].push(mapper(sub));
    } else {
      formErrors.push(mapper(sub));
    }
  }
  return { formErrors, fieldErrors };
}
function formatError(error2, mapper = (issue2) => issue2.message) {
  const fieldErrors = { _errors: [] };
  const processError = (error3) => {
    for (const issue2 of error3.issues) {
      if (issue2.code === "invalid_union" && issue2.errors.length) {
        issue2.errors.map((issues) => processError({ issues }));
      } else if (issue2.code === "invalid_key") {
        processError({ issues: issue2.issues });
      } else if (issue2.code === "invalid_element") {
        processError({ issues: issue2.issues });
      } else if (issue2.path.length === 0) {
        fieldErrors._errors.push(mapper(issue2));
      } else {
        let curr = fieldErrors;
        let i = 0;
        while (i < issue2.path.length) {
          const el = issue2.path[i];
          const terminal = i === issue2.path.length - 1;
          if (!terminal) {
            curr[el] = curr[el] || { _errors: [] };
          } else {
            curr[el] = curr[el] || { _errors: [] };
            curr[el]._errors.push(mapper(issue2));
          }
          curr = curr[el];
          i++;
        }
      }
    }
  };
  processError(error2);
  return fieldErrors;
}

// ../../node_modules/zod/v4/core/parse.js
var _parse = (_Err) => (schema, value, _ctx, _params) => {
  const ctx = _ctx ? Object.assign(_ctx, { async: false }) : { async: false };
  const result = schema._zod.run({ value, issues: [] }, ctx);
  if (result instanceof Promise) {
    throw new $ZodAsyncError();
  }
  if (result.issues.length) {
    const e = new (_params?.Err ?? _Err)(result.issues.map((iss) => finalizeIssue(iss, ctx, config())));
    captureStackTrace(e, _params?.callee);
    throw e;
  }
  return result.value;
};
var parse = /* @__PURE__ */ _parse($ZodRealError);
var _parseAsync = (_Err) => async (schema, value, _ctx, params) => {
  const ctx = _ctx ? Object.assign(_ctx, { async: true }) : { async: true };
  let result = schema._zod.run({ value, issues: [] }, ctx);
  if (result instanceof Promise)
    result = await result;
  if (result.issues.length) {
    const e = new (params?.Err ?? _Err)(result.issues.map((iss) => finalizeIssue(iss, ctx, config())));
    captureStackTrace(e, params?.callee);
    throw e;
  }
  return result.value;
};
var parseAsync = /* @__PURE__ */ _parseAsync($ZodRealError);
var _safeParse = (_Err) => (schema, value, _ctx) => {
  const ctx = _ctx ? { ..._ctx, async: false } : { async: false };
  const result = schema._zod.run({ value, issues: [] }, ctx);
  if (result instanceof Promise) {
    throw new $ZodAsyncError();
  }
  return result.issues.length ? {
    success: false,
    error: new (_Err ?? $ZodError)(result.issues.map((iss) => finalizeIssue(iss, ctx, config())))
  } : { success: true, data: result.value };
};
var safeParse = /* @__PURE__ */ _safeParse($ZodRealError);
var _safeParseAsync = (_Err) => async (schema, value, _ctx) => {
  const ctx = _ctx ? Object.assign(_ctx, { async: true }) : { async: true };
  let result = schema._zod.run({ value, issues: [] }, ctx);
  if (result instanceof Promise)
    result = await result;
  return result.issues.length ? {
    success: false,
    error: new _Err(result.issues.map((iss) => finalizeIssue(iss, ctx, config())))
  } : { success: true, data: result.value };
};
var safeParseAsync = /* @__PURE__ */ _safeParseAsync($ZodRealError);
var _encode = (_Err) => (schema, value, _ctx) => {
  const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
  return _parse(_Err)(schema, value, ctx);
};
var _decode = (_Err) => (schema, value, _ctx) => {
  return _parse(_Err)(schema, value, _ctx);
};
var _encodeAsync = (_Err) => async (schema, value, _ctx) => {
  const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
  return _parseAsync(_Err)(schema, value, ctx);
};
var _decodeAsync = (_Err) => async (schema, value, _ctx) => {
  return _parseAsync(_Err)(schema, value, _ctx);
};
var _safeEncode = (_Err) => (schema, value, _ctx) => {
  const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
  return _safeParse(_Err)(schema, value, ctx);
};
var _safeDecode = (_Err) => (schema, value, _ctx) => {
  return _safeParse(_Err)(schema, value, _ctx);
};
var _safeEncodeAsync = (_Err) => async (schema, value, _ctx) => {
  const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
  return _safeParseAsync(_Err)(schema, value, ctx);
};
var _safeDecodeAsync = (_Err) => async (schema, value, _ctx) => {
  return _safeParseAsync(_Err)(schema, value, _ctx);
};

// ../../node_modules/zod/v4/core/regexes.js
var regexes_exports = {};
__export(regexes_exports, {
  base64: () => base64,
  base64url: () => base64url,
  bigint: () => bigint,
  boolean: () => boolean,
  browserEmail: () => browserEmail,
  cidrv4: () => cidrv4,
  cidrv6: () => cidrv6,
  cuid: () => cuid,
  cuid2: () => cuid2,
  date: () => date,
  datetime: () => datetime,
  domain: () => domain,
  duration: () => duration,
  e164: () => e164,
  email: () => email,
  emoji: () => emoji,
  extendedDuration: () => extendedDuration,
  guid: () => guid,
  hex: () => hex,
  hostname: () => hostname,
  html5Email: () => html5Email,
  idnEmail: () => idnEmail,
  integer: () => integer,
  ipv4: () => ipv4,
  ipv6: () => ipv6,
  ksuid: () => ksuid,
  lowercase: () => lowercase,
  mac: () => mac,
  md5_base64: () => md5_base64,
  md5_base64url: () => md5_base64url,
  md5_hex: () => md5_hex,
  nanoid: () => nanoid,
  null: () => _null,
  number: () => number,
  rfc5322Email: () => rfc5322Email,
  sha1_base64: () => sha1_base64,
  sha1_base64url: () => sha1_base64url,
  sha1_hex: () => sha1_hex,
  sha256_base64: () => sha256_base64,
  sha256_base64url: () => sha256_base64url,
  sha256_hex: () => sha256_hex,
  sha384_base64: () => sha384_base64,
  sha384_base64url: () => sha384_base64url,
  sha384_hex: () => sha384_hex,
  sha512_base64: () => sha512_base64,
  sha512_base64url: () => sha512_base64url,
  sha512_hex: () => sha512_hex,
  string: () => string,
  time: () => time,
  ulid: () => ulid,
  undefined: () => _undefined,
  unicodeEmail: () => unicodeEmail,
  uppercase: () => uppercase,
  uuid: () => uuid,
  uuid4: () => uuid4,
  uuid6: () => uuid6,
  uuid7: () => uuid7,
  xid: () => xid
});
var cuid = /^[cC][^\s-]{8,}$/;
var cuid2 = /^[0-9a-z]+$/;
var ulid = /^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$/;
var xid = /^[0-9a-vA-V]{20}$/;
var ksuid = /^[A-Za-z0-9]{27}$/;
var nanoid = /^[a-zA-Z0-9_-]{21}$/;
var duration = /^P(?:(\d+W)|(?!.*W)(?=\d|T\d)(\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+([.,]\d+)?S)?)?)$/;
var extendedDuration = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
var guid = /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/;
var uuid = (version2) => {
  if (!version2)
    return /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/;
  return new RegExp(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${version2}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`);
};
var uuid4 = /* @__PURE__ */ uuid(4);
var uuid6 = /* @__PURE__ */ uuid(6);
var uuid7 = /* @__PURE__ */ uuid(7);
var email = /^(?!\.)(?!.*\.\.)([A-Za-z0-9_'+\-\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,}$/;
var html5Email = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
var rfc5322Email = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var unicodeEmail = /^[^\s@"]{1,64}@[^\s@]{1,255}$/u;
var idnEmail = unicodeEmail;
var browserEmail = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
var _emoji = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
function emoji() {
  return new RegExp(_emoji, "u");
}
var ipv4 = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
var ipv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$/;
var mac = (delimiter) => {
  const escapedDelim = escapeRegex(delimiter ?? ":");
  return new RegExp(`^(?:[0-9A-F]{2}${escapedDelim}){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}${escapedDelim}){5}[0-9a-f]{2}$`);
};
var cidrv4 = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/;
var cidrv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:?){0,6})\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
var base64 = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/;
var base64url = /^[A-Za-z0-9_-]*$/;
var hostname = /^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/;
var domain = /^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/;
var e164 = /^\+[1-9]\d{6,14}$/;
var dateSource = `(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))`;
var date = /* @__PURE__ */ new RegExp(`^${dateSource}$`);
function timeSource(args) {
  const hhmm = `(?:[01]\\d|2[0-3]):[0-5]\\d`;
  const regex2 = typeof args.precision === "number" ? args.precision === -1 ? `${hhmm}` : args.precision === 0 ? `${hhmm}:[0-5]\\d` : `${hhmm}:[0-5]\\d\\.\\d{${args.precision}}` : `${hhmm}(?::[0-5]\\d(?:\\.\\d+)?)?`;
  return regex2;
}
function time(args) {
  return new RegExp(`^${timeSource(args)}$`);
}
function datetime(args) {
  const time4 = timeSource({ precision: args.precision });
  const opts = ["Z"];
  if (args.local)
    opts.push("");
  if (args.offset)
    opts.push(`([+-](?:[01]\\d|2[0-3]):[0-5]\\d)`);
  const timeRegex = `${time4}(?:${opts.join("|")})`;
  return new RegExp(`^${dateSource}T(?:${timeRegex})$`);
}
var string = (params) => {
  const regex2 = params ? `[\\s\\S]{${params?.minimum ?? 0},${params?.maximum ?? ""}}` : `[\\s\\S]*`;
  return new RegExp(`^${regex2}$`);
};
var bigint = /^-?\d+n?$/;
var integer = /^-?\d+$/;
var number = /^-?\d+(?:\.\d+)?$/;
var boolean = /^(?:true|false)$/i;
var _null = /^null$/i;
var _undefined = /^undefined$/i;
var lowercase = /^[^A-Z]*$/;
var uppercase = /^[^a-z]*$/;
var hex = /^[0-9a-fA-F]*$/;
function fixedBase64(bodyLength, padding) {
  return new RegExp(`^[A-Za-z0-9+/]{${bodyLength}}${padding}$`);
}
function fixedBase64url(length) {
  return new RegExp(`^[A-Za-z0-9_-]{${length}}$`);
}
var md5_hex = /^[0-9a-fA-F]{32}$/;
var md5_base64 = /* @__PURE__ */ fixedBase64(22, "==");
var md5_base64url = /* @__PURE__ */ fixedBase64url(22);
var sha1_hex = /^[0-9a-fA-F]{40}$/;
var sha1_base64 = /* @__PURE__ */ fixedBase64(27, "=");
var sha1_base64url = /* @__PURE__ */ fixedBase64url(27);
var sha256_hex = /^[0-9a-fA-F]{64}$/;
var sha256_base64 = /* @__PURE__ */ fixedBase64(43, "=");
var sha256_base64url = /* @__PURE__ */ fixedBase64url(43);
var sha384_hex = /^[0-9a-fA-F]{96}$/;
var sha384_base64 = /* @__PURE__ */ fixedBase64(64, "");
var sha384_base64url = /* @__PURE__ */ fixedBase64url(64);
var sha512_hex = /^[0-9a-fA-F]{128}$/;
var sha512_base64 = /* @__PURE__ */ fixedBase64(86, "==");
var sha512_base64url = /* @__PURE__ */ fixedBase64url(86);

// ../../node_modules/zod/v4/core/checks.js
var $ZodCheck = /* @__PURE__ */ $constructor("$ZodCheck", (inst, def) => {
  var _a2;
  inst._zod ?? (inst._zod = {});
  inst._zod.def = def;
  (_a2 = inst._zod).onattach ?? (_a2.onattach = []);
});
var numericOriginMap = {
  number: "number",
  bigint: "bigint",
  object: "date"
};
var $ZodCheckLessThan = /* @__PURE__ */ $constructor("$ZodCheckLessThan", (inst, def) => {
  $ZodCheck.init(inst, def);
  const origin = numericOriginMap[typeof def.value];
  inst._zod.onattach.push((inst2) => {
    const bag = inst2._zod.bag;
    const curr = (def.inclusive ? bag.maximum : bag.exclusiveMaximum) ?? Number.POSITIVE_INFINITY;
    if (def.value < curr) {
      if (def.inclusive)
        bag.maximum = def.value;
      else
        bag.exclusiveMaximum = def.value;
    }
  });
  inst._zod.check = (payload) => {
    if (def.inclusive ? payload.value <= def.value : payload.value < def.value) {
      return;
    }
    payload.issues.push({
      origin,
      code: "too_big",
      maximum: typeof def.value === "object" ? def.value.getTime() : def.value,
      input: payload.value,
      inclusive: def.inclusive,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodCheckGreaterThan = /* @__PURE__ */ $constructor("$ZodCheckGreaterThan", (inst, def) => {
  $ZodCheck.init(inst, def);
  const origin = numericOriginMap[typeof def.value];
  inst._zod.onattach.push((inst2) => {
    const bag = inst2._zod.bag;
    const curr = (def.inclusive ? bag.minimum : bag.exclusiveMinimum) ?? Number.NEGATIVE_INFINITY;
    if (def.value > curr) {
      if (def.inclusive)
        bag.minimum = def.value;
      else
        bag.exclusiveMinimum = def.value;
    }
  });
  inst._zod.check = (payload) => {
    if (def.inclusive ? payload.value >= def.value : payload.value > def.value) {
      return;
    }
    payload.issues.push({
      origin,
      code: "too_small",
      minimum: typeof def.value === "object" ? def.value.getTime() : def.value,
      input: payload.value,
      inclusive: def.inclusive,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodCheckMultipleOf = /* @__PURE__ */ $constructor("$ZodCheckMultipleOf", (inst, def) => {
  $ZodCheck.init(inst, def);
  inst._zod.onattach.push((inst2) => {
    var _a2;
    (_a2 = inst2._zod.bag).multipleOf ?? (_a2.multipleOf = def.value);
  });
  inst._zod.check = (payload) => {
    if (typeof payload.value !== typeof def.value)
      throw new Error("Cannot mix number and bigint in multiple_of check.");
    const isMultiple = typeof payload.value === "bigint" ? payload.value % def.value === BigInt(0) : floatSafeRemainder(payload.value, def.value) === 0;
    if (isMultiple)
      return;
    payload.issues.push({
      origin: typeof payload.value,
      code: "not_multiple_of",
      divisor: def.value,
      input: payload.value,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodCheckNumberFormat = /* @__PURE__ */ $constructor("$ZodCheckNumberFormat", (inst, def) => {
  $ZodCheck.init(inst, def);
  def.format = def.format || "float64";
  const isInt = def.format?.includes("int");
  const origin = isInt ? "int" : "number";
  const [minimum, maximum] = NUMBER_FORMAT_RANGES[def.format];
  inst._zod.onattach.push((inst2) => {
    const bag = inst2._zod.bag;
    bag.format = def.format;
    bag.minimum = minimum;
    bag.maximum = maximum;
    if (isInt)
      bag.pattern = integer;
  });
  inst._zod.check = (payload) => {
    const input = payload.value;
    if (isInt) {
      if (!Number.isInteger(input)) {
        payload.issues.push({
          expected: origin,
          format: def.format,
          code: "invalid_type",
          continue: false,
          input,
          inst
        });
        return;
      }
      if (!Number.isSafeInteger(input)) {
        if (input > 0) {
          payload.issues.push({
            input,
            code: "too_big",
            maximum: Number.MAX_SAFE_INTEGER,
            note: "Integers must be within the safe integer range.",
            inst,
            origin,
            inclusive: true,
            continue: !def.abort
          });
        } else {
          payload.issues.push({
            input,
            code: "too_small",
            minimum: Number.MIN_SAFE_INTEGER,
            note: "Integers must be within the safe integer range.",
            inst,
            origin,
            inclusive: true,
            continue: !def.abort
          });
        }
        return;
      }
    }
    if (input < minimum) {
      payload.issues.push({
        origin: "number",
        input,
        code: "too_small",
        minimum,
        inclusive: true,
        inst,
        continue: !def.abort
      });
    }
    if (input > maximum) {
      payload.issues.push({
        origin: "number",
        input,
        code: "too_big",
        maximum,
        inclusive: true,
        inst,
        continue: !def.abort
      });
    }
  };
});
var $ZodCheckBigIntFormat = /* @__PURE__ */ $constructor("$ZodCheckBigIntFormat", (inst, def) => {
  $ZodCheck.init(inst, def);
  const [minimum, maximum] = BIGINT_FORMAT_RANGES[def.format];
  inst._zod.onattach.push((inst2) => {
    const bag = inst2._zod.bag;
    bag.format = def.format;
    bag.minimum = minimum;
    bag.maximum = maximum;
  });
  inst._zod.check = (payload) => {
    const input = payload.value;
    if (input < minimum) {
      payload.issues.push({
        origin: "bigint",
        input,
        code: "too_small",
        minimum,
        inclusive: true,
        inst,
        continue: !def.abort
      });
    }
    if (input > maximum) {
      payload.issues.push({
        origin: "bigint",
        input,
        code: "too_big",
        maximum,
        inclusive: true,
        inst,
        continue: !def.abort
      });
    }
  };
});
var $ZodCheckMaxSize = /* @__PURE__ */ $constructor("$ZodCheckMaxSize", (inst, def) => {
  var _a2;
  $ZodCheck.init(inst, def);
  (_a2 = inst._zod.def).when ?? (_a2.when = (payload) => {
    const val = payload.value;
    return !nullish(val) && val.size !== void 0;
  });
  inst._zod.onattach.push((inst2) => {
    const curr = inst2._zod.bag.maximum ?? Number.POSITIVE_INFINITY;
    if (def.maximum < curr)
      inst2._zod.bag.maximum = def.maximum;
  });
  inst._zod.check = (payload) => {
    const input = payload.value;
    const size = input.size;
    if (size <= def.maximum)
      return;
    payload.issues.push({
      origin: getSizableOrigin(input),
      code: "too_big",
      maximum: def.maximum,
      inclusive: true,
      input,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodCheckMinSize = /* @__PURE__ */ $constructor("$ZodCheckMinSize", (inst, def) => {
  var _a2;
  $ZodCheck.init(inst, def);
  (_a2 = inst._zod.def).when ?? (_a2.when = (payload) => {
    const val = payload.value;
    return !nullish(val) && val.size !== void 0;
  });
  inst._zod.onattach.push((inst2) => {
    const curr = inst2._zod.bag.minimum ?? Number.NEGATIVE_INFINITY;
    if (def.minimum > curr)
      inst2._zod.bag.minimum = def.minimum;
  });
  inst._zod.check = (payload) => {
    const input = payload.value;
    const size = input.size;
    if (size >= def.minimum)
      return;
    payload.issues.push({
      origin: getSizableOrigin(input),
      code: "too_small",
      minimum: def.minimum,
      inclusive: true,
      input,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodCheckSizeEquals = /* @__PURE__ */ $constructor("$ZodCheckSizeEquals", (inst, def) => {
  var _a2;
  $ZodCheck.init(inst, def);
  (_a2 = inst._zod.def).when ?? (_a2.when = (payload) => {
    const val = payload.value;
    return !nullish(val) && val.size !== void 0;
  });
  inst._zod.onattach.push((inst2) => {
    const bag = inst2._zod.bag;
    bag.minimum = def.size;
    bag.maximum = def.size;
    bag.size = def.size;
  });
  inst._zod.check = (payload) => {
    const input = payload.value;
    const size = input.size;
    if (size === def.size)
      return;
    const tooBig = size > def.size;
    payload.issues.push({
      origin: getSizableOrigin(input),
      ...tooBig ? { code: "too_big", maximum: def.size } : { code: "too_small", minimum: def.size },
      inclusive: true,
      exact: true,
      input: payload.value,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodCheckMaxLength = /* @__PURE__ */ $constructor("$ZodCheckMaxLength", (inst, def) => {
  var _a2;
  $ZodCheck.init(inst, def);
  (_a2 = inst._zod.def).when ?? (_a2.when = (payload) => {
    const val = payload.value;
    return !nullish(val) && val.length !== void 0;
  });
  inst._zod.onattach.push((inst2) => {
    const curr = inst2._zod.bag.maximum ?? Number.POSITIVE_INFINITY;
    if (def.maximum < curr)
      inst2._zod.bag.maximum = def.maximum;
  });
  inst._zod.check = (payload) => {
    const input = payload.value;
    const length = input.length;
    if (length <= def.maximum)
      return;
    const origin = getLengthableOrigin(input);
    payload.issues.push({
      origin,
      code: "too_big",
      maximum: def.maximum,
      inclusive: true,
      input,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodCheckMinLength = /* @__PURE__ */ $constructor("$ZodCheckMinLength", (inst, def) => {
  var _a2;
  $ZodCheck.init(inst, def);
  (_a2 = inst._zod.def).when ?? (_a2.when = (payload) => {
    const val = payload.value;
    return !nullish(val) && val.length !== void 0;
  });
  inst._zod.onattach.push((inst2) => {
    const curr = inst2._zod.bag.minimum ?? Number.NEGATIVE_INFINITY;
    if (def.minimum > curr)
      inst2._zod.bag.minimum = def.minimum;
  });
  inst._zod.check = (payload) => {
    const input = payload.value;
    const length = input.length;
    if (length >= def.minimum)
      return;
    const origin = getLengthableOrigin(input);
    payload.issues.push({
      origin,
      code: "too_small",
      minimum: def.minimum,
      inclusive: true,
      input,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodCheckLengthEquals = /* @__PURE__ */ $constructor("$ZodCheckLengthEquals", (inst, def) => {
  var _a2;
  $ZodCheck.init(inst, def);
  (_a2 = inst._zod.def).when ?? (_a2.when = (payload) => {
    const val = payload.value;
    return !nullish(val) && val.length !== void 0;
  });
  inst._zod.onattach.push((inst2) => {
    const bag = inst2._zod.bag;
    bag.minimum = def.length;
    bag.maximum = def.length;
    bag.length = def.length;
  });
  inst._zod.check = (payload) => {
    const input = payload.value;
    const length = input.length;
    if (length === def.length)
      return;
    const origin = getLengthableOrigin(input);
    const tooBig = length > def.length;
    payload.issues.push({
      origin,
      ...tooBig ? { code: "too_big", maximum: def.length } : { code: "too_small", minimum: def.length },
      inclusive: true,
      exact: true,
      input: payload.value,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodCheckStringFormat = /* @__PURE__ */ $constructor("$ZodCheckStringFormat", (inst, def) => {
  var _a2, _b;
  $ZodCheck.init(inst, def);
  inst._zod.onattach.push((inst2) => {
    const bag = inst2._zod.bag;
    bag.format = def.format;
    if (def.pattern) {
      bag.patterns ?? (bag.patterns = /* @__PURE__ */ new Set());
      bag.patterns.add(def.pattern);
    }
  });
  if (def.pattern)
    (_a2 = inst._zod).check ?? (_a2.check = (payload) => {
      def.pattern.lastIndex = 0;
      if (def.pattern.test(payload.value))
        return;
      payload.issues.push({
        origin: "string",
        code: "invalid_format",
        format: def.format,
        input: payload.value,
        ...def.pattern ? { pattern: def.pattern.toString() } : {},
        inst,
        continue: !def.abort
      });
    });
  else
    (_b = inst._zod).check ?? (_b.check = () => {
    });
});
var $ZodCheckRegex = /* @__PURE__ */ $constructor("$ZodCheckRegex", (inst, def) => {
  $ZodCheckStringFormat.init(inst, def);
  inst._zod.check = (payload) => {
    def.pattern.lastIndex = 0;
    if (def.pattern.test(payload.value))
      return;
    payload.issues.push({
      origin: "string",
      code: "invalid_format",
      format: "regex",
      input: payload.value,
      pattern: def.pattern.toString(),
      inst,
      continue: !def.abort
    });
  };
});
var $ZodCheckLowerCase = /* @__PURE__ */ $constructor("$ZodCheckLowerCase", (inst, def) => {
  def.pattern ?? (def.pattern = lowercase);
  $ZodCheckStringFormat.init(inst, def);
});
var $ZodCheckUpperCase = /* @__PURE__ */ $constructor("$ZodCheckUpperCase", (inst, def) => {
  def.pattern ?? (def.pattern = uppercase);
  $ZodCheckStringFormat.init(inst, def);
});
var $ZodCheckIncludes = /* @__PURE__ */ $constructor("$ZodCheckIncludes", (inst, def) => {
  $ZodCheck.init(inst, def);
  const escapedRegex = escapeRegex(def.includes);
  const pattern = new RegExp(typeof def.position === "number" ? `^.{${def.position}}${escapedRegex}` : escapedRegex);
  def.pattern = pattern;
  inst._zod.onattach.push((inst2) => {
    const bag = inst2._zod.bag;
    bag.patterns ?? (bag.patterns = /* @__PURE__ */ new Set());
    bag.patterns.add(pattern);
  });
  inst._zod.check = (payload) => {
    if (payload.value.includes(def.includes, def.position))
      return;
    payload.issues.push({
      origin: "string",
      code: "invalid_format",
      format: "includes",
      includes: def.includes,
      input: payload.value,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodCheckStartsWith = /* @__PURE__ */ $constructor("$ZodCheckStartsWith", (inst, def) => {
  $ZodCheck.init(inst, def);
  const pattern = new RegExp(`^${escapeRegex(def.prefix)}.*`);
  def.pattern ?? (def.pattern = pattern);
  inst._zod.onattach.push((inst2) => {
    const bag = inst2._zod.bag;
    bag.patterns ?? (bag.patterns = /* @__PURE__ */ new Set());
    bag.patterns.add(pattern);
  });
  inst._zod.check = (payload) => {
    if (payload.value.startsWith(def.prefix))
      return;
    payload.issues.push({
      origin: "string",
      code: "invalid_format",
      format: "starts_with",
      prefix: def.prefix,
      input: payload.value,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodCheckEndsWith = /* @__PURE__ */ $constructor("$ZodCheckEndsWith", (inst, def) => {
  $ZodCheck.init(inst, def);
  const pattern = new RegExp(`.*${escapeRegex(def.suffix)}$`);
  def.pattern ?? (def.pattern = pattern);
  inst._zod.onattach.push((inst2) => {
    const bag = inst2._zod.bag;
    bag.patterns ?? (bag.patterns = /* @__PURE__ */ new Set());
    bag.patterns.add(pattern);
  });
  inst._zod.check = (payload) => {
    if (payload.value.endsWith(def.suffix))
      return;
    payload.issues.push({
      origin: "string",
      code: "invalid_format",
      format: "ends_with",
      suffix: def.suffix,
      input: payload.value,
      inst,
      continue: !def.abort
    });
  };
});
function handleCheckPropertyResult(result, payload, property) {
  if (result.issues.length) {
    payload.issues.push(...prefixIssues(property, result.issues));
  }
}
var $ZodCheckProperty = /* @__PURE__ */ $constructor("$ZodCheckProperty", (inst, def) => {
  $ZodCheck.init(inst, def);
  inst._zod.check = (payload) => {
    const result = def.schema._zod.run({
      value: payload.value[def.property],
      issues: []
    }, {});
    if (result instanceof Promise) {
      return result.then((result2) => handleCheckPropertyResult(result2, payload, def.property));
    }
    handleCheckPropertyResult(result, payload, def.property);
    return;
  };
});
var $ZodCheckMimeType = /* @__PURE__ */ $constructor("$ZodCheckMimeType", (inst, def) => {
  $ZodCheck.init(inst, def);
  const mimeSet = new Set(def.mime);
  inst._zod.onattach.push((inst2) => {
    inst2._zod.bag.mime = def.mime;
  });
  inst._zod.check = (payload) => {
    if (mimeSet.has(payload.value.type))
      return;
    payload.issues.push({
      code: "invalid_value",
      values: def.mime,
      input: payload.value.type,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodCheckOverwrite = /* @__PURE__ */ $constructor("$ZodCheckOverwrite", (inst, def) => {
  $ZodCheck.init(inst, def);
  inst._zod.check = (payload) => {
    payload.value = def.tx(payload.value);
  };
});

// ../../node_modules/zod/v4/core/doc.js
var Doc = class {
  constructor(args = []) {
    this.content = [];
    this.indent = 0;
    if (this)
      this.args = args;
  }
  indented(fn) {
    this.indent += 1;
    fn(this);
    this.indent -= 1;
  }
  write(arg) {
    if (typeof arg === "function") {
      arg(this, { execution: "sync" });
      arg(this, { execution: "async" });
      return;
    }
    const content = arg;
    const lines = content.split("\n").filter((x) => x);
    const minIndent = Math.min(...lines.map((x) => x.length - x.trimStart().length));
    const dedented = lines.map((x) => x.slice(minIndent)).map((x) => " ".repeat(this.indent * 2) + x);
    for (const line of dedented) {
      this.content.push(line);
    }
  }
  compile() {
    const F = Function;
    const args = this?.args;
    const content = this?.content ?? [``];
    const lines = [...content.map((x) => `  ${x}`)];
    return new F(...args, lines.join("\n"));
  }
};

// ../../node_modules/zod/v4/core/versions.js
var version = {
  major: 4,
  minor: 3,
  patch: 6
};

// ../../node_modules/zod/v4/core/schemas.js
var $ZodType = /* @__PURE__ */ $constructor("$ZodType", (inst, def) => {
  var _a2;
  inst ?? (inst = {});
  inst._zod.def = def;
  inst._zod.bag = inst._zod.bag || {};
  inst._zod.version = version;
  const checks = [...inst._zod.def.checks ?? []];
  if (inst._zod.traits.has("$ZodCheck")) {
    checks.unshift(inst);
  }
  for (const ch of checks) {
    for (const fn of ch._zod.onattach) {
      fn(inst);
    }
  }
  if (checks.length === 0) {
    (_a2 = inst._zod).deferred ?? (_a2.deferred = []);
    inst._zod.deferred?.push(() => {
      inst._zod.run = inst._zod.parse;
    });
  } else {
    const runChecks = (payload, checks2, ctx) => {
      let isAborted = aborted(payload);
      let asyncResult;
      for (const ch of checks2) {
        if (ch._zod.def.when) {
          const shouldRun = ch._zod.def.when(payload);
          if (!shouldRun)
            continue;
        } else if (isAborted) {
          continue;
        }
        const currLen = payload.issues.length;
        const _ = ch._zod.check(payload);
        if (_ instanceof Promise && ctx?.async === false) {
          throw new $ZodAsyncError();
        }
        if (asyncResult || _ instanceof Promise) {
          asyncResult = (asyncResult ?? Promise.resolve()).then(async () => {
            await _;
            const nextLen = payload.issues.length;
            if (nextLen === currLen)
              return;
            if (!isAborted)
              isAborted = aborted(payload, currLen);
          });
        } else {
          const nextLen = payload.issues.length;
          if (nextLen === currLen)
            continue;
          if (!isAborted)
            isAborted = aborted(payload, currLen);
        }
      }
      if (asyncResult) {
        return asyncResult.then(() => {
          return payload;
        });
      }
      return payload;
    };
    const handleCanaryResult = (canary, payload, ctx) => {
      if (aborted(canary)) {
        canary.aborted = true;
        return canary;
      }
      const checkResult = runChecks(payload, checks, ctx);
      if (checkResult instanceof Promise) {
        if (ctx.async === false)
          throw new $ZodAsyncError();
        return checkResult.then((checkResult2) => inst._zod.parse(checkResult2, ctx));
      }
      return inst._zod.parse(checkResult, ctx);
    };
    inst._zod.run = (payload, ctx) => {
      if (ctx.skipChecks) {
        return inst._zod.parse(payload, ctx);
      }
      if (ctx.direction === "backward") {
        const canary = inst._zod.parse({ value: payload.value, issues: [] }, { ...ctx, skipChecks: true });
        if (canary instanceof Promise) {
          return canary.then((canary2) => {
            return handleCanaryResult(canary2, payload, ctx);
          });
        }
        return handleCanaryResult(canary, payload, ctx);
      }
      const result = inst._zod.parse(payload, ctx);
      if (result instanceof Promise) {
        if (ctx.async === false)
          throw new $ZodAsyncError();
        return result.then((result2) => runChecks(result2, checks, ctx));
      }
      return runChecks(result, checks, ctx);
    };
  }
  defineLazy(inst, "~standard", () => ({
    validate: (value) => {
      try {
        const r = safeParse(inst, value);
        return r.success ? { value: r.data } : { issues: r.error?.issues };
      } catch (_) {
        return safeParseAsync(inst, value).then((r) => r.success ? { value: r.data } : { issues: r.error?.issues });
      }
    },
    vendor: "zod",
    version: 1
  }));
});
var $ZodString = /* @__PURE__ */ $constructor("$ZodString", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.pattern = [...inst?._zod.bag?.patterns ?? []].pop() ?? string(inst._zod.bag);
  inst._zod.parse = (payload, _) => {
    if (def.coerce)
      try {
        payload.value = String(payload.value);
      } catch (_2) {
      }
    if (typeof payload.value === "string")
      return payload;
    payload.issues.push({
      expected: "string",
      code: "invalid_type",
      input: payload.value,
      inst
    });
    return payload;
  };
});
var $ZodStringFormat = /* @__PURE__ */ $constructor("$ZodStringFormat", (inst, def) => {
  $ZodCheckStringFormat.init(inst, def);
  $ZodString.init(inst, def);
});
var $ZodGUID = /* @__PURE__ */ $constructor("$ZodGUID", (inst, def) => {
  def.pattern ?? (def.pattern = guid);
  $ZodStringFormat.init(inst, def);
});
var $ZodUUID = /* @__PURE__ */ $constructor("$ZodUUID", (inst, def) => {
  if (def.version) {
    const versionMap = {
      v1: 1,
      v2: 2,
      v3: 3,
      v4: 4,
      v5: 5,
      v6: 6,
      v7: 7,
      v8: 8
    };
    const v = versionMap[def.version];
    if (v === void 0)
      throw new Error(`Invalid UUID version: "${def.version}"`);
    def.pattern ?? (def.pattern = uuid(v));
  } else
    def.pattern ?? (def.pattern = uuid());
  $ZodStringFormat.init(inst, def);
});
var $ZodEmail = /* @__PURE__ */ $constructor("$ZodEmail", (inst, def) => {
  def.pattern ?? (def.pattern = email);
  $ZodStringFormat.init(inst, def);
});
var $ZodURL = /* @__PURE__ */ $constructor("$ZodURL", (inst, def) => {
  $ZodStringFormat.init(inst, def);
  inst._zod.check = (payload) => {
    try {
      const trimmed = payload.value.trim();
      const url2 = new URL(trimmed);
      if (def.hostname) {
        def.hostname.lastIndex = 0;
        if (!def.hostname.test(url2.hostname)) {
          payload.issues.push({
            code: "invalid_format",
            format: "url",
            note: "Invalid hostname",
            pattern: def.hostname.source,
            input: payload.value,
            inst,
            continue: !def.abort
          });
        }
      }
      if (def.protocol) {
        def.protocol.lastIndex = 0;
        if (!def.protocol.test(url2.protocol.endsWith(":") ? url2.protocol.slice(0, -1) : url2.protocol)) {
          payload.issues.push({
            code: "invalid_format",
            format: "url",
            note: "Invalid protocol",
            pattern: def.protocol.source,
            input: payload.value,
            inst,
            continue: !def.abort
          });
        }
      }
      if (def.normalize) {
        payload.value = url2.href;
      } else {
        payload.value = trimmed;
      }
      return;
    } catch (_) {
      payload.issues.push({
        code: "invalid_format",
        format: "url",
        input: payload.value,
        inst,
        continue: !def.abort
      });
    }
  };
});
var $ZodEmoji = /* @__PURE__ */ $constructor("$ZodEmoji", (inst, def) => {
  def.pattern ?? (def.pattern = emoji());
  $ZodStringFormat.init(inst, def);
});
var $ZodNanoID = /* @__PURE__ */ $constructor("$ZodNanoID", (inst, def) => {
  def.pattern ?? (def.pattern = nanoid);
  $ZodStringFormat.init(inst, def);
});
var $ZodCUID = /* @__PURE__ */ $constructor("$ZodCUID", (inst, def) => {
  def.pattern ?? (def.pattern = cuid);
  $ZodStringFormat.init(inst, def);
});
var $ZodCUID2 = /* @__PURE__ */ $constructor("$ZodCUID2", (inst, def) => {
  def.pattern ?? (def.pattern = cuid2);
  $ZodStringFormat.init(inst, def);
});
var $ZodULID = /* @__PURE__ */ $constructor("$ZodULID", (inst, def) => {
  def.pattern ?? (def.pattern = ulid);
  $ZodStringFormat.init(inst, def);
});
var $ZodXID = /* @__PURE__ */ $constructor("$ZodXID", (inst, def) => {
  def.pattern ?? (def.pattern = xid);
  $ZodStringFormat.init(inst, def);
});
var $ZodKSUID = /* @__PURE__ */ $constructor("$ZodKSUID", (inst, def) => {
  def.pattern ?? (def.pattern = ksuid);
  $ZodStringFormat.init(inst, def);
});
var $ZodISODateTime = /* @__PURE__ */ $constructor("$ZodISODateTime", (inst, def) => {
  def.pattern ?? (def.pattern = datetime(def));
  $ZodStringFormat.init(inst, def);
});
var $ZodISODate = /* @__PURE__ */ $constructor("$ZodISODate", (inst, def) => {
  def.pattern ?? (def.pattern = date);
  $ZodStringFormat.init(inst, def);
});
var $ZodISOTime = /* @__PURE__ */ $constructor("$ZodISOTime", (inst, def) => {
  def.pattern ?? (def.pattern = time(def));
  $ZodStringFormat.init(inst, def);
});
var $ZodISODuration = /* @__PURE__ */ $constructor("$ZodISODuration", (inst, def) => {
  def.pattern ?? (def.pattern = duration);
  $ZodStringFormat.init(inst, def);
});
var $ZodIPv4 = /* @__PURE__ */ $constructor("$ZodIPv4", (inst, def) => {
  def.pattern ?? (def.pattern = ipv4);
  $ZodStringFormat.init(inst, def);
  inst._zod.bag.format = `ipv4`;
});
var $ZodIPv6 = /* @__PURE__ */ $constructor("$ZodIPv6", (inst, def) => {
  def.pattern ?? (def.pattern = ipv6);
  $ZodStringFormat.init(inst, def);
  inst._zod.bag.format = `ipv6`;
  inst._zod.check = (payload) => {
    try {
      new URL(`http://[${payload.value}]`);
    } catch {
      payload.issues.push({
        code: "invalid_format",
        format: "ipv6",
        input: payload.value,
        inst,
        continue: !def.abort
      });
    }
  };
});
var $ZodMAC = /* @__PURE__ */ $constructor("$ZodMAC", (inst, def) => {
  def.pattern ?? (def.pattern = mac(def.delimiter));
  $ZodStringFormat.init(inst, def);
  inst._zod.bag.format = `mac`;
});
var $ZodCIDRv4 = /* @__PURE__ */ $constructor("$ZodCIDRv4", (inst, def) => {
  def.pattern ?? (def.pattern = cidrv4);
  $ZodStringFormat.init(inst, def);
});
var $ZodCIDRv6 = /* @__PURE__ */ $constructor("$ZodCIDRv6", (inst, def) => {
  def.pattern ?? (def.pattern = cidrv6);
  $ZodStringFormat.init(inst, def);
  inst._zod.check = (payload) => {
    const parts = payload.value.split("/");
    try {
      if (parts.length !== 2)
        throw new Error();
      const [address, prefix] = parts;
      if (!prefix)
        throw new Error();
      const prefixNum = Number(prefix);
      if (`${prefixNum}` !== prefix)
        throw new Error();
      if (prefixNum < 0 || prefixNum > 128)
        throw new Error();
      new URL(`http://[${address}]`);
    } catch {
      payload.issues.push({
        code: "invalid_format",
        format: "cidrv6",
        input: payload.value,
        inst,
        continue: !def.abort
      });
    }
  };
});
function isValidBase64(data) {
  if (data === "")
    return true;
  if (data.length % 4 !== 0)
    return false;
  try {
    atob(data);
    return true;
  } catch {
    return false;
  }
}
var $ZodBase64 = /* @__PURE__ */ $constructor("$ZodBase64", (inst, def) => {
  def.pattern ?? (def.pattern = base64);
  $ZodStringFormat.init(inst, def);
  inst._zod.bag.contentEncoding = "base64";
  inst._zod.check = (payload) => {
    if (isValidBase64(payload.value))
      return;
    payload.issues.push({
      code: "invalid_format",
      format: "base64",
      input: payload.value,
      inst,
      continue: !def.abort
    });
  };
});
function isValidBase64URL(data) {
  if (!base64url.test(data))
    return false;
  const base643 = data.replace(/[-_]/g, (c) => c === "-" ? "+" : "/");
  const padded = base643.padEnd(Math.ceil(base643.length / 4) * 4, "=");
  return isValidBase64(padded);
}
var $ZodBase64URL = /* @__PURE__ */ $constructor("$ZodBase64URL", (inst, def) => {
  def.pattern ?? (def.pattern = base64url);
  $ZodStringFormat.init(inst, def);
  inst._zod.bag.contentEncoding = "base64url";
  inst._zod.check = (payload) => {
    if (isValidBase64URL(payload.value))
      return;
    payload.issues.push({
      code: "invalid_format",
      format: "base64url",
      input: payload.value,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodE164 = /* @__PURE__ */ $constructor("$ZodE164", (inst, def) => {
  def.pattern ?? (def.pattern = e164);
  $ZodStringFormat.init(inst, def);
});
function isValidJWT(token, algorithm = null) {
  try {
    const tokensParts = token.split(".");
    if (tokensParts.length !== 3)
      return false;
    const [header] = tokensParts;
    if (!header)
      return false;
    const parsedHeader = JSON.parse(atob(header));
    if ("typ" in parsedHeader && parsedHeader?.typ !== "JWT")
      return false;
    if (!parsedHeader.alg)
      return false;
    if (algorithm && (!("alg" in parsedHeader) || parsedHeader.alg !== algorithm))
      return false;
    return true;
  } catch {
    return false;
  }
}
var $ZodJWT = /* @__PURE__ */ $constructor("$ZodJWT", (inst, def) => {
  $ZodStringFormat.init(inst, def);
  inst._zod.check = (payload) => {
    if (isValidJWT(payload.value, def.alg))
      return;
    payload.issues.push({
      code: "invalid_format",
      format: "jwt",
      input: payload.value,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodCustomStringFormat = /* @__PURE__ */ $constructor("$ZodCustomStringFormat", (inst, def) => {
  $ZodStringFormat.init(inst, def);
  inst._zod.check = (payload) => {
    if (def.fn(payload.value))
      return;
    payload.issues.push({
      code: "invalid_format",
      format: def.format,
      input: payload.value,
      inst,
      continue: !def.abort
    });
  };
});
var $ZodNumber = /* @__PURE__ */ $constructor("$ZodNumber", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.pattern = inst._zod.bag.pattern ?? number;
  inst._zod.parse = (payload, _ctx) => {
    if (def.coerce)
      try {
        payload.value = Number(payload.value);
      } catch (_) {
      }
    const input = payload.value;
    if (typeof input === "number" && !Number.isNaN(input) && Number.isFinite(input)) {
      return payload;
    }
    const received = typeof input === "number" ? Number.isNaN(input) ? "NaN" : !Number.isFinite(input) ? "Infinity" : void 0 : void 0;
    payload.issues.push({
      expected: "number",
      code: "invalid_type",
      input,
      inst,
      ...received ? { received } : {}
    });
    return payload;
  };
});
var $ZodNumberFormat = /* @__PURE__ */ $constructor("$ZodNumberFormat", (inst, def) => {
  $ZodCheckNumberFormat.init(inst, def);
  $ZodNumber.init(inst, def);
});
var $ZodBoolean = /* @__PURE__ */ $constructor("$ZodBoolean", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.pattern = boolean;
  inst._zod.parse = (payload, _ctx) => {
    if (def.coerce)
      try {
        payload.value = Boolean(payload.value);
      } catch (_) {
      }
    const input = payload.value;
    if (typeof input === "boolean")
      return payload;
    payload.issues.push({
      expected: "boolean",
      code: "invalid_type",
      input,
      inst
    });
    return payload;
  };
});
var $ZodBigInt = /* @__PURE__ */ $constructor("$ZodBigInt", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.pattern = bigint;
  inst._zod.parse = (payload, _ctx) => {
    if (def.coerce)
      try {
        payload.value = BigInt(payload.value);
      } catch (_) {
      }
    if (typeof payload.value === "bigint")
      return payload;
    payload.issues.push({
      expected: "bigint",
      code: "invalid_type",
      input: payload.value,
      inst
    });
    return payload;
  };
});
var $ZodBigIntFormat = /* @__PURE__ */ $constructor("$ZodBigIntFormat", (inst, def) => {
  $ZodCheckBigIntFormat.init(inst, def);
  $ZodBigInt.init(inst, def);
});
var $ZodSymbol = /* @__PURE__ */ $constructor("$ZodSymbol", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, _ctx) => {
    const input = payload.value;
    if (typeof input === "symbol")
      return payload;
    payload.issues.push({
      expected: "symbol",
      code: "invalid_type",
      input,
      inst
    });
    return payload;
  };
});
var $ZodUndefined = /* @__PURE__ */ $constructor("$ZodUndefined", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.pattern = _undefined;
  inst._zod.values = /* @__PURE__ */ new Set([void 0]);
  inst._zod.optin = "optional";
  inst._zod.optout = "optional";
  inst._zod.parse = (payload, _ctx) => {
    const input = payload.value;
    if (typeof input === "undefined")
      return payload;
    payload.issues.push({
      expected: "undefined",
      code: "invalid_type",
      input,
      inst
    });
    return payload;
  };
});
var $ZodNull = /* @__PURE__ */ $constructor("$ZodNull", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.pattern = _null;
  inst._zod.values = /* @__PURE__ */ new Set([null]);
  inst._zod.parse = (payload, _ctx) => {
    const input = payload.value;
    if (input === null)
      return payload;
    payload.issues.push({
      expected: "null",
      code: "invalid_type",
      input,
      inst
    });
    return payload;
  };
});
var $ZodAny = /* @__PURE__ */ $constructor("$ZodAny", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload) => payload;
});
var $ZodUnknown = /* @__PURE__ */ $constructor("$ZodUnknown", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload) => payload;
});
var $ZodNever = /* @__PURE__ */ $constructor("$ZodNever", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, _ctx) => {
    payload.issues.push({
      expected: "never",
      code: "invalid_type",
      input: payload.value,
      inst
    });
    return payload;
  };
});
var $ZodVoid = /* @__PURE__ */ $constructor("$ZodVoid", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, _ctx) => {
    const input = payload.value;
    if (typeof input === "undefined")
      return payload;
    payload.issues.push({
      expected: "void",
      code: "invalid_type",
      input,
      inst
    });
    return payload;
  };
});
var $ZodDate = /* @__PURE__ */ $constructor("$ZodDate", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, _ctx) => {
    if (def.coerce) {
      try {
        payload.value = new Date(payload.value);
      } catch (_err) {
      }
    }
    const input = payload.value;
    const isDate = input instanceof Date;
    const isValidDate = isDate && !Number.isNaN(input.getTime());
    if (isValidDate)
      return payload;
    payload.issues.push({
      expected: "date",
      code: "invalid_type",
      input,
      ...isDate ? { received: "Invalid Date" } : {},
      inst
    });
    return payload;
  };
});
function handleArrayResult(result, final, index) {
  if (result.issues.length) {
    final.issues.push(...prefixIssues(index, result.issues));
  }
  final.value[index] = result.value;
}
var $ZodArray = /* @__PURE__ */ $constructor("$ZodArray", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, ctx) => {
    const input = payload.value;
    if (!Array.isArray(input)) {
      payload.issues.push({
        expected: "array",
        code: "invalid_type",
        input,
        inst
      });
      return payload;
    }
    payload.value = Array(input.length);
    const proms = [];
    for (let i = 0; i < input.length; i++) {
      const item = input[i];
      const result = def.element._zod.run({
        value: item,
        issues: []
      }, ctx);
      if (result instanceof Promise) {
        proms.push(result.then((result2) => handleArrayResult(result2, payload, i)));
      } else {
        handleArrayResult(result, payload, i);
      }
    }
    if (proms.length) {
      return Promise.all(proms).then(() => payload);
    }
    return payload;
  };
});
function handlePropertyResult(result, final, key, input, isOptionalOut) {
  if (result.issues.length) {
    if (isOptionalOut && !(key in input)) {
      return;
    }
    final.issues.push(...prefixIssues(key, result.issues));
  }
  if (result.value === void 0) {
    if (key in input) {
      final.value[key] = void 0;
    }
  } else {
    final.value[key] = result.value;
  }
}
function normalizeDef(def) {
  const keys = Object.keys(def.shape);
  for (const k of keys) {
    if (!def.shape?.[k]?._zod?.traits?.has("$ZodType")) {
      throw new Error(`Invalid element at key "${k}": expected a Zod schema`);
    }
  }
  const okeys = optionalKeys(def.shape);
  return {
    ...def,
    keys,
    keySet: new Set(keys),
    numKeys: keys.length,
    optionalKeys: new Set(okeys)
  };
}
function handleCatchall(proms, input, payload, ctx, def, inst) {
  const unrecognized = [];
  const keySet = def.keySet;
  const _catchall = def.catchall._zod;
  const t = _catchall.def.type;
  const isOptionalOut = _catchall.optout === "optional";
  for (const key in input) {
    if (keySet.has(key))
      continue;
    if (t === "never") {
      unrecognized.push(key);
      continue;
    }
    const r = _catchall.run({ value: input[key], issues: [] }, ctx);
    if (r instanceof Promise) {
      proms.push(r.then((r2) => handlePropertyResult(r2, payload, key, input, isOptionalOut)));
    } else {
      handlePropertyResult(r, payload, key, input, isOptionalOut);
    }
  }
  if (unrecognized.length) {
    payload.issues.push({
      code: "unrecognized_keys",
      keys: unrecognized,
      input,
      inst
    });
  }
  if (!proms.length)
    return payload;
  return Promise.all(proms).then(() => {
    return payload;
  });
}
var $ZodObject = /* @__PURE__ */ $constructor("$ZodObject", (inst, def) => {
  $ZodType.init(inst, def);
  const desc = Object.getOwnPropertyDescriptor(def, "shape");
  if (!desc?.get) {
    const sh = def.shape;
    Object.defineProperty(def, "shape", {
      get: () => {
        const newSh = { ...sh };
        Object.defineProperty(def, "shape", {
          value: newSh
        });
        return newSh;
      }
    });
  }
  const _normalized = cached(() => normalizeDef(def));
  defineLazy(inst._zod, "propValues", () => {
    const shape = def.shape;
    const propValues = {};
    for (const key in shape) {
      const field = shape[key]._zod;
      if (field.values) {
        propValues[key] ?? (propValues[key] = /* @__PURE__ */ new Set());
        for (const v of field.values)
          propValues[key].add(v);
      }
    }
    return propValues;
  });
  const isObject2 = isObject;
  const catchall = def.catchall;
  let value;
  inst._zod.parse = (payload, ctx) => {
    value ?? (value = _normalized.value);
    const input = payload.value;
    if (!isObject2(input)) {
      payload.issues.push({
        expected: "object",
        code: "invalid_type",
        input,
        inst
      });
      return payload;
    }
    payload.value = {};
    const proms = [];
    const shape = value.shape;
    for (const key of value.keys) {
      const el = shape[key];
      const isOptionalOut = el._zod.optout === "optional";
      const r = el._zod.run({ value: input[key], issues: [] }, ctx);
      if (r instanceof Promise) {
        proms.push(r.then((r2) => handlePropertyResult(r2, payload, key, input, isOptionalOut)));
      } else {
        handlePropertyResult(r, payload, key, input, isOptionalOut);
      }
    }
    if (!catchall) {
      return proms.length ? Promise.all(proms).then(() => payload) : payload;
    }
    return handleCatchall(proms, input, payload, ctx, _normalized.value, inst);
  };
});
var $ZodObjectJIT = /* @__PURE__ */ $constructor("$ZodObjectJIT", (inst, def) => {
  $ZodObject.init(inst, def);
  const superParse = inst._zod.parse;
  const _normalized = cached(() => normalizeDef(def));
  const generateFastpass = (shape) => {
    const doc = new Doc(["shape", "payload", "ctx"]);
    const normalized = _normalized.value;
    const parseStr = (key) => {
      const k = esc(key);
      return `shape[${k}]._zod.run({ value: input[${k}], issues: [] }, ctx)`;
    };
    doc.write(`const input = payload.value;`);
    const ids = /* @__PURE__ */ Object.create(null);
    let counter = 0;
    for (const key of normalized.keys) {
      ids[key] = `key_${counter++}`;
    }
    doc.write(`const newResult = {};`);
    for (const key of normalized.keys) {
      const id = ids[key];
      const k = esc(key);
      const schema = shape[key];
      const isOptionalOut = schema?._zod?.optout === "optional";
      doc.write(`const ${id} = ${parseStr(key)};`);
      if (isOptionalOut) {
        doc.write(`
        if (${id}.issues.length) {
          if (${k} in input) {
            payload.issues = payload.issues.concat(${id}.issues.map(iss => ({
              ...iss,
              path: iss.path ? [${k}, ...iss.path] : [${k}]
            })));
          }
        }
        
        if (${id}.value === undefined) {
          if (${k} in input) {
            newResult[${k}] = undefined;
          }
        } else {
          newResult[${k}] = ${id}.value;
        }
        
      `);
      } else {
        doc.write(`
        if (${id}.issues.length) {
          payload.issues = payload.issues.concat(${id}.issues.map(iss => ({
            ...iss,
            path: iss.path ? [${k}, ...iss.path] : [${k}]
          })));
        }
        
        if (${id}.value === undefined) {
          if (${k} in input) {
            newResult[${k}] = undefined;
          }
        } else {
          newResult[${k}] = ${id}.value;
        }
        
      `);
      }
    }
    doc.write(`payload.value = newResult;`);
    doc.write(`return payload;`);
    const fn = doc.compile();
    return (payload, ctx) => fn(shape, payload, ctx);
  };
  let fastpass;
  const isObject2 = isObject;
  const jit = !globalConfig.jitless;
  const allowsEval2 = allowsEval;
  const fastEnabled = jit && allowsEval2.value;
  const catchall = def.catchall;
  let value;
  inst._zod.parse = (payload, ctx) => {
    value ?? (value = _normalized.value);
    const input = payload.value;
    if (!isObject2(input)) {
      payload.issues.push({
        expected: "object",
        code: "invalid_type",
        input,
        inst
      });
      return payload;
    }
    if (jit && fastEnabled && ctx?.async === false && ctx.jitless !== true) {
      if (!fastpass)
        fastpass = generateFastpass(def.shape);
      payload = fastpass(payload, ctx);
      if (!catchall)
        return payload;
      return handleCatchall([], input, payload, ctx, value, inst);
    }
    return superParse(payload, ctx);
  };
});
function handleUnionResults(results, final, inst, ctx) {
  for (const result of results) {
    if (result.issues.length === 0) {
      final.value = result.value;
      return final;
    }
  }
  const nonaborted = results.filter((r) => !aborted(r));
  if (nonaborted.length === 1) {
    final.value = nonaborted[0].value;
    return nonaborted[0];
  }
  final.issues.push({
    code: "invalid_union",
    input: final.value,
    inst,
    errors: results.map((result) => result.issues.map((iss) => finalizeIssue(iss, ctx, config())))
  });
  return final;
}
var $ZodUnion = /* @__PURE__ */ $constructor("$ZodUnion", (inst, def) => {
  $ZodType.init(inst, def);
  defineLazy(inst._zod, "optin", () => def.options.some((o) => o._zod.optin === "optional") ? "optional" : void 0);
  defineLazy(inst._zod, "optout", () => def.options.some((o) => o._zod.optout === "optional") ? "optional" : void 0);
  defineLazy(inst._zod, "values", () => {
    if (def.options.every((o) => o._zod.values)) {
      return new Set(def.options.flatMap((option) => Array.from(option._zod.values)));
    }
    return void 0;
  });
  defineLazy(inst._zod, "pattern", () => {
    if (def.options.every((o) => o._zod.pattern)) {
      const patterns = def.options.map((o) => o._zod.pattern);
      return new RegExp(`^(${patterns.map((p) => cleanRegex(p.source)).join("|")})$`);
    }
    return void 0;
  });
  const single = def.options.length === 1;
  const first = def.options[0]._zod.run;
  inst._zod.parse = (payload, ctx) => {
    if (single) {
      return first(payload, ctx);
    }
    let async = false;
    const results = [];
    for (const option of def.options) {
      const result = option._zod.run({
        value: payload.value,
        issues: []
      }, ctx);
      if (result instanceof Promise) {
        results.push(result);
        async = true;
      } else {
        if (result.issues.length === 0)
          return result;
        results.push(result);
      }
    }
    if (!async)
      return handleUnionResults(results, payload, inst, ctx);
    return Promise.all(results).then((results2) => {
      return handleUnionResults(results2, payload, inst, ctx);
    });
  };
});
function handleExclusiveUnionResults(results, final, inst, ctx) {
  const successes = results.filter((r) => r.issues.length === 0);
  if (successes.length === 1) {
    final.value = successes[0].value;
    return final;
  }
  if (successes.length === 0) {
    final.issues.push({
      code: "invalid_union",
      input: final.value,
      inst,
      errors: results.map((result) => result.issues.map((iss) => finalizeIssue(iss, ctx, config())))
    });
  } else {
    final.issues.push({
      code: "invalid_union",
      input: final.value,
      inst,
      errors: [],
      inclusive: false
    });
  }
  return final;
}
var $ZodXor = /* @__PURE__ */ $constructor("$ZodXor", (inst, def) => {
  $ZodUnion.init(inst, def);
  def.inclusive = false;
  const single = def.options.length === 1;
  const first = def.options[0]._zod.run;
  inst._zod.parse = (payload, ctx) => {
    if (single) {
      return first(payload, ctx);
    }
    let async = false;
    const results = [];
    for (const option of def.options) {
      const result = option._zod.run({
        value: payload.value,
        issues: []
      }, ctx);
      if (result instanceof Promise) {
        results.push(result);
        async = true;
      } else {
        results.push(result);
      }
    }
    if (!async)
      return handleExclusiveUnionResults(results, payload, inst, ctx);
    return Promise.all(results).then((results2) => {
      return handleExclusiveUnionResults(results2, payload, inst, ctx);
    });
  };
});
var $ZodDiscriminatedUnion = /* @__PURE__ */ $constructor("$ZodDiscriminatedUnion", (inst, def) => {
  def.inclusive = false;
  $ZodUnion.init(inst, def);
  const _super = inst._zod.parse;
  defineLazy(inst._zod, "propValues", () => {
    const propValues = {};
    for (const option of def.options) {
      const pv = option._zod.propValues;
      if (!pv || Object.keys(pv).length === 0)
        throw new Error(`Invalid discriminated union option at index "${def.options.indexOf(option)}"`);
      for (const [k, v] of Object.entries(pv)) {
        if (!propValues[k])
          propValues[k] = /* @__PURE__ */ new Set();
        for (const val of v) {
          propValues[k].add(val);
        }
      }
    }
    return propValues;
  });
  const disc = cached(() => {
    const opts = def.options;
    const map2 = /* @__PURE__ */ new Map();
    for (const o of opts) {
      const values = o._zod.propValues?.[def.discriminator];
      if (!values || values.size === 0)
        throw new Error(`Invalid discriminated union option at index "${def.options.indexOf(o)}"`);
      for (const v of values) {
        if (map2.has(v)) {
          throw new Error(`Duplicate discriminator value "${String(v)}"`);
        }
        map2.set(v, o);
      }
    }
    return map2;
  });
  inst._zod.parse = (payload, ctx) => {
    const input = payload.value;
    if (!isObject(input)) {
      payload.issues.push({
        code: "invalid_type",
        expected: "object",
        input,
        inst
      });
      return payload;
    }
    const opt = disc.value.get(input?.[def.discriminator]);
    if (opt) {
      return opt._zod.run(payload, ctx);
    }
    if (def.unionFallback) {
      return _super(payload, ctx);
    }
    payload.issues.push({
      code: "invalid_union",
      errors: [],
      note: "No matching discriminator",
      discriminator: def.discriminator,
      input,
      path: [def.discriminator],
      inst
    });
    return payload;
  };
});
var $ZodIntersection = /* @__PURE__ */ $constructor("$ZodIntersection", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, ctx) => {
    const input = payload.value;
    const left = def.left._zod.run({ value: input, issues: [] }, ctx);
    const right = def.right._zod.run({ value: input, issues: [] }, ctx);
    const async = left instanceof Promise || right instanceof Promise;
    if (async) {
      return Promise.all([left, right]).then(([left2, right2]) => {
        return handleIntersectionResults(payload, left2, right2);
      });
    }
    return handleIntersectionResults(payload, left, right);
  };
});
function mergeValues(a, b) {
  if (a === b) {
    return { valid: true, data: a };
  }
  if (a instanceof Date && b instanceof Date && +a === +b) {
    return { valid: true, data: a };
  }
  if (isPlainObject2(a) && isPlainObject2(b)) {
    const bKeys = Object.keys(b);
    const sharedKeys = Object.keys(a).filter((key) => bKeys.indexOf(key) !== -1);
    const newObj = { ...a, ...b };
    for (const key of sharedKeys) {
      const sharedValue = mergeValues(a[key], b[key]);
      if (!sharedValue.valid) {
        return {
          valid: false,
          mergeErrorPath: [key, ...sharedValue.mergeErrorPath]
        };
      }
      newObj[key] = sharedValue.data;
    }
    return { valid: true, data: newObj };
  }
  if (Array.isArray(a) && Array.isArray(b)) {
    if (a.length !== b.length) {
      return { valid: false, mergeErrorPath: [] };
    }
    const newArray = [];
    for (let index = 0; index < a.length; index++) {
      const itemA = a[index];
      const itemB = b[index];
      const sharedValue = mergeValues(itemA, itemB);
      if (!sharedValue.valid) {
        return {
          valid: false,
          mergeErrorPath: [index, ...sharedValue.mergeErrorPath]
        };
      }
      newArray.push(sharedValue.data);
    }
    return { valid: true, data: newArray };
  }
  return { valid: false, mergeErrorPath: [] };
}
function handleIntersectionResults(result, left, right) {
  const unrecKeys = /* @__PURE__ */ new Map();
  let unrecIssue;
  for (const iss of left.issues) {
    if (iss.code === "unrecognized_keys") {
      unrecIssue ?? (unrecIssue = iss);
      for (const k of iss.keys) {
        if (!unrecKeys.has(k))
          unrecKeys.set(k, {});
        unrecKeys.get(k).l = true;
      }
    } else {
      result.issues.push(iss);
    }
  }
  for (const iss of right.issues) {
    if (iss.code === "unrecognized_keys") {
      for (const k of iss.keys) {
        if (!unrecKeys.has(k))
          unrecKeys.set(k, {});
        unrecKeys.get(k).r = true;
      }
    } else {
      result.issues.push(iss);
    }
  }
  const bothKeys = [...unrecKeys].filter(([, f]) => f.l && f.r).map(([k]) => k);
  if (bothKeys.length && unrecIssue) {
    result.issues.push({ ...unrecIssue, keys: bothKeys });
  }
  if (aborted(result))
    return result;
  const merged = mergeValues(left.value, right.value);
  if (!merged.valid) {
    throw new Error(`Unmergable intersection. Error path: ${JSON.stringify(merged.mergeErrorPath)}`);
  }
  result.value = merged.data;
  return result;
}
var $ZodTuple = /* @__PURE__ */ $constructor("$ZodTuple", (inst, def) => {
  $ZodType.init(inst, def);
  const items = def.items;
  inst._zod.parse = (payload, ctx) => {
    const input = payload.value;
    if (!Array.isArray(input)) {
      payload.issues.push({
        input,
        inst,
        expected: "tuple",
        code: "invalid_type"
      });
      return payload;
    }
    payload.value = [];
    const proms = [];
    const reversedIndex = [...items].reverse().findIndex((item) => item._zod.optin !== "optional");
    const optStart = reversedIndex === -1 ? 0 : items.length - reversedIndex;
    if (!def.rest) {
      const tooBig = input.length > items.length;
      const tooSmall = input.length < optStart - 1;
      if (tooBig || tooSmall) {
        payload.issues.push({
          ...tooBig ? { code: "too_big", maximum: items.length, inclusive: true } : { code: "too_small", minimum: items.length },
          input,
          inst,
          origin: "array"
        });
        return payload;
      }
    }
    let i = -1;
    for (const item of items) {
      i++;
      if (i >= input.length) {
        if (i >= optStart)
          continue;
      }
      const result = item._zod.run({
        value: input[i],
        issues: []
      }, ctx);
      if (result instanceof Promise) {
        proms.push(result.then((result2) => handleTupleResult(result2, payload, i)));
      } else {
        handleTupleResult(result, payload, i);
      }
    }
    if (def.rest) {
      const rest = input.slice(items.length);
      for (const el of rest) {
        i++;
        const result = def.rest._zod.run({
          value: el,
          issues: []
        }, ctx);
        if (result instanceof Promise) {
          proms.push(result.then((result2) => handleTupleResult(result2, payload, i)));
        } else {
          handleTupleResult(result, payload, i);
        }
      }
    }
    if (proms.length)
      return Promise.all(proms).then(() => payload);
    return payload;
  };
});
function handleTupleResult(result, final, index) {
  if (result.issues.length) {
    final.issues.push(...prefixIssues(index, result.issues));
  }
  final.value[index] = result.value;
}
var $ZodRecord = /* @__PURE__ */ $constructor("$ZodRecord", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, ctx) => {
    const input = payload.value;
    if (!isPlainObject2(input)) {
      payload.issues.push({
        expected: "record",
        code: "invalid_type",
        input,
        inst
      });
      return payload;
    }
    const proms = [];
    const values = def.keyType._zod.values;
    if (values) {
      payload.value = {};
      const recordKeys = /* @__PURE__ */ new Set();
      for (const key of values) {
        if (typeof key === "string" || typeof key === "number" || typeof key === "symbol") {
          recordKeys.add(typeof key === "number" ? key.toString() : key);
          const result = def.valueType._zod.run({ value: input[key], issues: [] }, ctx);
          if (result instanceof Promise) {
            proms.push(result.then((result2) => {
              if (result2.issues.length) {
                payload.issues.push(...prefixIssues(key, result2.issues));
              }
              payload.value[key] = result2.value;
            }));
          } else {
            if (result.issues.length) {
              payload.issues.push(...prefixIssues(key, result.issues));
            }
            payload.value[key] = result.value;
          }
        }
      }
      let unrecognized;
      for (const key in input) {
        if (!recordKeys.has(key)) {
          unrecognized = unrecognized ?? [];
          unrecognized.push(key);
        }
      }
      if (unrecognized && unrecognized.length > 0) {
        payload.issues.push({
          code: "unrecognized_keys",
          input,
          inst,
          keys: unrecognized
        });
      }
    } else {
      payload.value = {};
      for (const key of Reflect.ownKeys(input)) {
        if (key === "__proto__")
          continue;
        let keyResult = def.keyType._zod.run({ value: key, issues: [] }, ctx);
        if (keyResult instanceof Promise) {
          throw new Error("Async schemas not supported in object keys currently");
        }
        const checkNumericKey = typeof key === "string" && number.test(key) && keyResult.issues.length;
        if (checkNumericKey) {
          const retryResult = def.keyType._zod.run({ value: Number(key), issues: [] }, ctx);
          if (retryResult instanceof Promise) {
            throw new Error("Async schemas not supported in object keys currently");
          }
          if (retryResult.issues.length === 0) {
            keyResult = retryResult;
          }
        }
        if (keyResult.issues.length) {
          if (def.mode === "loose") {
            payload.value[key] = input[key];
          } else {
            payload.issues.push({
              code: "invalid_key",
              origin: "record",
              issues: keyResult.issues.map((iss) => finalizeIssue(iss, ctx, config())),
              input: key,
              path: [key],
              inst
            });
          }
          continue;
        }
        const result = def.valueType._zod.run({ value: input[key], issues: [] }, ctx);
        if (result instanceof Promise) {
          proms.push(result.then((result2) => {
            if (result2.issues.length) {
              payload.issues.push(...prefixIssues(key, result2.issues));
            }
            payload.value[keyResult.value] = result2.value;
          }));
        } else {
          if (result.issues.length) {
            payload.issues.push(...prefixIssues(key, result.issues));
          }
          payload.value[keyResult.value] = result.value;
        }
      }
    }
    if (proms.length) {
      return Promise.all(proms).then(() => payload);
    }
    return payload;
  };
});
var $ZodMap = /* @__PURE__ */ $constructor("$ZodMap", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, ctx) => {
    const input = payload.value;
    if (!(input instanceof Map)) {
      payload.issues.push({
        expected: "map",
        code: "invalid_type",
        input,
        inst
      });
      return payload;
    }
    const proms = [];
    payload.value = /* @__PURE__ */ new Map();
    for (const [key, value] of input) {
      const keyResult = def.keyType._zod.run({ value: key, issues: [] }, ctx);
      const valueResult = def.valueType._zod.run({ value, issues: [] }, ctx);
      if (keyResult instanceof Promise || valueResult instanceof Promise) {
        proms.push(Promise.all([keyResult, valueResult]).then(([keyResult2, valueResult2]) => {
          handleMapResult(keyResult2, valueResult2, payload, key, input, inst, ctx);
        }));
      } else {
        handleMapResult(keyResult, valueResult, payload, key, input, inst, ctx);
      }
    }
    if (proms.length)
      return Promise.all(proms).then(() => payload);
    return payload;
  };
});
function handleMapResult(keyResult, valueResult, final, key, input, inst, ctx) {
  if (keyResult.issues.length) {
    if (propertyKeyTypes.has(typeof key)) {
      final.issues.push(...prefixIssues(key, keyResult.issues));
    } else {
      final.issues.push({
        code: "invalid_key",
        origin: "map",
        input,
        inst,
        issues: keyResult.issues.map((iss) => finalizeIssue(iss, ctx, config()))
      });
    }
  }
  if (valueResult.issues.length) {
    if (propertyKeyTypes.has(typeof key)) {
      final.issues.push(...prefixIssues(key, valueResult.issues));
    } else {
      final.issues.push({
        origin: "map",
        code: "invalid_element",
        input,
        inst,
        key,
        issues: valueResult.issues.map((iss) => finalizeIssue(iss, ctx, config()))
      });
    }
  }
  final.value.set(keyResult.value, valueResult.value);
}
var $ZodSet = /* @__PURE__ */ $constructor("$ZodSet", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, ctx) => {
    const input = payload.value;
    if (!(input instanceof Set)) {
      payload.issues.push({
        input,
        inst,
        expected: "set",
        code: "invalid_type"
      });
      return payload;
    }
    const proms = [];
    payload.value = /* @__PURE__ */ new Set();
    for (const item of input) {
      const result = def.valueType._zod.run({ value: item, issues: [] }, ctx);
      if (result instanceof Promise) {
        proms.push(result.then((result2) => handleSetResult(result2, payload)));
      } else
        handleSetResult(result, payload);
    }
    if (proms.length)
      return Promise.all(proms).then(() => payload);
    return payload;
  };
});
function handleSetResult(result, final) {
  if (result.issues.length) {
    final.issues.push(...result.issues);
  }
  final.value.add(result.value);
}
var $ZodEnum = /* @__PURE__ */ $constructor("$ZodEnum", (inst, def) => {
  $ZodType.init(inst, def);
  const values = getEnumValues(def.entries);
  const valuesSet = new Set(values);
  inst._zod.values = valuesSet;
  inst._zod.pattern = new RegExp(`^(${values.filter((k) => propertyKeyTypes.has(typeof k)).map((o) => typeof o === "string" ? escapeRegex(o) : o.toString()).join("|")})$`);
  inst._zod.parse = (payload, _ctx) => {
    const input = payload.value;
    if (valuesSet.has(input)) {
      return payload;
    }
    payload.issues.push({
      code: "invalid_value",
      values,
      input,
      inst
    });
    return payload;
  };
});
var $ZodLiteral = /* @__PURE__ */ $constructor("$ZodLiteral", (inst, def) => {
  $ZodType.init(inst, def);
  if (def.values.length === 0) {
    throw new Error("Cannot create literal schema with no valid values");
  }
  const values = new Set(def.values);
  inst._zod.values = values;
  inst._zod.pattern = new RegExp(`^(${def.values.map((o) => typeof o === "string" ? escapeRegex(o) : o ? escapeRegex(o.toString()) : String(o)).join("|")})$`);
  inst._zod.parse = (payload, _ctx) => {
    const input = payload.value;
    if (values.has(input)) {
      return payload;
    }
    payload.issues.push({
      code: "invalid_value",
      values: def.values,
      input,
      inst
    });
    return payload;
  };
});
var $ZodFile = /* @__PURE__ */ $constructor("$ZodFile", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, _ctx) => {
    const input = payload.value;
    if (input instanceof File)
      return payload;
    payload.issues.push({
      expected: "file",
      code: "invalid_type",
      input,
      inst
    });
    return payload;
  };
});
var $ZodTransform = /* @__PURE__ */ $constructor("$ZodTransform", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, ctx) => {
    if (ctx.direction === "backward") {
      throw new $ZodEncodeError(inst.constructor.name);
    }
    const _out = def.transform(payload.value, payload);
    if (ctx.async) {
      const output = _out instanceof Promise ? _out : Promise.resolve(_out);
      return output.then((output2) => {
        payload.value = output2;
        return payload;
      });
    }
    if (_out instanceof Promise) {
      throw new $ZodAsyncError();
    }
    payload.value = _out;
    return payload;
  };
});
function handleOptionalResult(result, input) {
  if (result.issues.length && input === void 0) {
    return { issues: [], value: void 0 };
  }
  return result;
}
var $ZodOptional = /* @__PURE__ */ $constructor("$ZodOptional", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.optin = "optional";
  inst._zod.optout = "optional";
  defineLazy(inst._zod, "values", () => {
    return def.innerType._zod.values ? /* @__PURE__ */ new Set([...def.innerType._zod.values, void 0]) : void 0;
  });
  defineLazy(inst._zod, "pattern", () => {
    const pattern = def.innerType._zod.pattern;
    return pattern ? new RegExp(`^(${cleanRegex(pattern.source)})?$`) : void 0;
  });
  inst._zod.parse = (payload, ctx) => {
    if (def.innerType._zod.optin === "optional") {
      const result = def.innerType._zod.run(payload, ctx);
      if (result instanceof Promise)
        return result.then((r) => handleOptionalResult(r, payload.value));
      return handleOptionalResult(result, payload.value);
    }
    if (payload.value === void 0) {
      return payload;
    }
    return def.innerType._zod.run(payload, ctx);
  };
});
var $ZodExactOptional = /* @__PURE__ */ $constructor("$ZodExactOptional", (inst, def) => {
  $ZodOptional.init(inst, def);
  defineLazy(inst._zod, "values", () => def.innerType._zod.values);
  defineLazy(inst._zod, "pattern", () => def.innerType._zod.pattern);
  inst._zod.parse = (payload, ctx) => {
    return def.innerType._zod.run(payload, ctx);
  };
});
var $ZodNullable = /* @__PURE__ */ $constructor("$ZodNullable", (inst, def) => {
  $ZodType.init(inst, def);
  defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
  defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
  defineLazy(inst._zod, "pattern", () => {
    const pattern = def.innerType._zod.pattern;
    return pattern ? new RegExp(`^(${cleanRegex(pattern.source)}|null)$`) : void 0;
  });
  defineLazy(inst._zod, "values", () => {
    return def.innerType._zod.values ? /* @__PURE__ */ new Set([...def.innerType._zod.values, null]) : void 0;
  });
  inst._zod.parse = (payload, ctx) => {
    if (payload.value === null)
      return payload;
    return def.innerType._zod.run(payload, ctx);
  };
});
var $ZodDefault = /* @__PURE__ */ $constructor("$ZodDefault", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.optin = "optional";
  defineLazy(inst._zod, "values", () => def.innerType._zod.values);
  inst._zod.parse = (payload, ctx) => {
    if (ctx.direction === "backward") {
      return def.innerType._zod.run(payload, ctx);
    }
    if (payload.value === void 0) {
      payload.value = def.defaultValue;
      return payload;
    }
    const result = def.innerType._zod.run(payload, ctx);
    if (result instanceof Promise) {
      return result.then((result2) => handleDefaultResult(result2, def));
    }
    return handleDefaultResult(result, def);
  };
});
function handleDefaultResult(payload, def) {
  if (payload.value === void 0) {
    payload.value = def.defaultValue;
  }
  return payload;
}
var $ZodPrefault = /* @__PURE__ */ $constructor("$ZodPrefault", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.optin = "optional";
  defineLazy(inst._zod, "values", () => def.innerType._zod.values);
  inst._zod.parse = (payload, ctx) => {
    if (ctx.direction === "backward") {
      return def.innerType._zod.run(payload, ctx);
    }
    if (payload.value === void 0) {
      payload.value = def.defaultValue;
    }
    return def.innerType._zod.run(payload, ctx);
  };
});
var $ZodNonOptional = /* @__PURE__ */ $constructor("$ZodNonOptional", (inst, def) => {
  $ZodType.init(inst, def);
  defineLazy(inst._zod, "values", () => {
    const v = def.innerType._zod.values;
    return v ? new Set([...v].filter((x) => x !== void 0)) : void 0;
  });
  inst._zod.parse = (payload, ctx) => {
    const result = def.innerType._zod.run(payload, ctx);
    if (result instanceof Promise) {
      return result.then((result2) => handleNonOptionalResult(result2, inst));
    }
    return handleNonOptionalResult(result, inst);
  };
});
function handleNonOptionalResult(payload, inst) {
  if (!payload.issues.length && payload.value === void 0) {
    payload.issues.push({
      code: "invalid_type",
      expected: "nonoptional",
      input: payload.value,
      inst
    });
  }
  return payload;
}
var $ZodSuccess = /* @__PURE__ */ $constructor("$ZodSuccess", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, ctx) => {
    if (ctx.direction === "backward") {
      throw new $ZodEncodeError("ZodSuccess");
    }
    const result = def.innerType._zod.run(payload, ctx);
    if (result instanceof Promise) {
      return result.then((result2) => {
        payload.value = result2.issues.length === 0;
        return payload;
      });
    }
    payload.value = result.issues.length === 0;
    return payload;
  };
});
var $ZodCatch = /* @__PURE__ */ $constructor("$ZodCatch", (inst, def) => {
  $ZodType.init(inst, def);
  defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
  defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
  defineLazy(inst._zod, "values", () => def.innerType._zod.values);
  inst._zod.parse = (payload, ctx) => {
    if (ctx.direction === "backward") {
      return def.innerType._zod.run(payload, ctx);
    }
    const result = def.innerType._zod.run(payload, ctx);
    if (result instanceof Promise) {
      return result.then((result2) => {
        payload.value = result2.value;
        if (result2.issues.length) {
          payload.value = def.catchValue({
            ...payload,
            error: {
              issues: result2.issues.map((iss) => finalizeIssue(iss, ctx, config()))
            },
            input: payload.value
          });
          payload.issues = [];
        }
        return payload;
      });
    }
    payload.value = result.value;
    if (result.issues.length) {
      payload.value = def.catchValue({
        ...payload,
        error: {
          issues: result.issues.map((iss) => finalizeIssue(iss, ctx, config()))
        },
        input: payload.value
      });
      payload.issues = [];
    }
    return payload;
  };
});
var $ZodNaN = /* @__PURE__ */ $constructor("$ZodNaN", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, _ctx) => {
    if (typeof payload.value !== "number" || !Number.isNaN(payload.value)) {
      payload.issues.push({
        input: payload.value,
        inst,
        expected: "nan",
        code: "invalid_type"
      });
      return payload;
    }
    return payload;
  };
});
var $ZodPipe = /* @__PURE__ */ $constructor("$ZodPipe", (inst, def) => {
  $ZodType.init(inst, def);
  defineLazy(inst._zod, "values", () => def.in._zod.values);
  defineLazy(inst._zod, "optin", () => def.in._zod.optin);
  defineLazy(inst._zod, "optout", () => def.out._zod.optout);
  defineLazy(inst._zod, "propValues", () => def.in._zod.propValues);
  inst._zod.parse = (payload, ctx) => {
    if (ctx.direction === "backward") {
      const right = def.out._zod.run(payload, ctx);
      if (right instanceof Promise) {
        return right.then((right2) => handlePipeResult(right2, def.in, ctx));
      }
      return handlePipeResult(right, def.in, ctx);
    }
    const left = def.in._zod.run(payload, ctx);
    if (left instanceof Promise) {
      return left.then((left2) => handlePipeResult(left2, def.out, ctx));
    }
    return handlePipeResult(left, def.out, ctx);
  };
});
function handlePipeResult(left, next, ctx) {
  if (left.issues.length) {
    left.aborted = true;
    return left;
  }
  return next._zod.run({ value: left.value, issues: left.issues }, ctx);
}
var $ZodCodec = /* @__PURE__ */ $constructor("$ZodCodec", (inst, def) => {
  $ZodType.init(inst, def);
  defineLazy(inst._zod, "values", () => def.in._zod.values);
  defineLazy(inst._zod, "optin", () => def.in._zod.optin);
  defineLazy(inst._zod, "optout", () => def.out._zod.optout);
  defineLazy(inst._zod, "propValues", () => def.in._zod.propValues);
  inst._zod.parse = (payload, ctx) => {
    const direction = ctx.direction || "forward";
    if (direction === "forward") {
      const left = def.in._zod.run(payload, ctx);
      if (left instanceof Promise) {
        return left.then((left2) => handleCodecAResult(left2, def, ctx));
      }
      return handleCodecAResult(left, def, ctx);
    } else {
      const right = def.out._zod.run(payload, ctx);
      if (right instanceof Promise) {
        return right.then((right2) => handleCodecAResult(right2, def, ctx));
      }
      return handleCodecAResult(right, def, ctx);
    }
  };
});
function handleCodecAResult(result, def, ctx) {
  if (result.issues.length) {
    result.aborted = true;
    return result;
  }
  const direction = ctx.direction || "forward";
  if (direction === "forward") {
    const transformed = def.transform(result.value, result);
    if (transformed instanceof Promise) {
      return transformed.then((value) => handleCodecTxResult(result, value, def.out, ctx));
    }
    return handleCodecTxResult(result, transformed, def.out, ctx);
  } else {
    const transformed = def.reverseTransform(result.value, result);
    if (transformed instanceof Promise) {
      return transformed.then((value) => handleCodecTxResult(result, value, def.in, ctx));
    }
    return handleCodecTxResult(result, transformed, def.in, ctx);
  }
}
function handleCodecTxResult(left, value, nextSchema, ctx) {
  if (left.issues.length) {
    left.aborted = true;
    return left;
  }
  return nextSchema._zod.run({ value, issues: left.issues }, ctx);
}
var $ZodReadonly = /* @__PURE__ */ $constructor("$ZodReadonly", (inst, def) => {
  $ZodType.init(inst, def);
  defineLazy(inst._zod, "propValues", () => def.innerType._zod.propValues);
  defineLazy(inst._zod, "values", () => def.innerType._zod.values);
  defineLazy(inst._zod, "optin", () => def.innerType?._zod?.optin);
  defineLazy(inst._zod, "optout", () => def.innerType?._zod?.optout);
  inst._zod.parse = (payload, ctx) => {
    if (ctx.direction === "backward") {
      return def.innerType._zod.run(payload, ctx);
    }
    const result = def.innerType._zod.run(payload, ctx);
    if (result instanceof Promise) {
      return result.then(handleReadonlyResult);
    }
    return handleReadonlyResult(result);
  };
});
function handleReadonlyResult(payload) {
  payload.value = Object.freeze(payload.value);
  return payload;
}
var $ZodTemplateLiteral = /* @__PURE__ */ $constructor("$ZodTemplateLiteral", (inst, def) => {
  $ZodType.init(inst, def);
  const regexParts = [];
  for (const part of def.parts) {
    if (typeof part === "object" && part !== null) {
      if (!part._zod.pattern) {
        throw new Error(`Invalid template literal part, no pattern found: ${[...part._zod.traits].shift()}`);
      }
      const source = part._zod.pattern instanceof RegExp ? part._zod.pattern.source : part._zod.pattern;
      if (!source)
        throw new Error(`Invalid template literal part: ${part._zod.traits}`);
      const start = source.startsWith("^") ? 1 : 0;
      const end = source.endsWith("$") ? source.length - 1 : source.length;
      regexParts.push(source.slice(start, end));
    } else if (part === null || primitiveTypes.has(typeof part)) {
      regexParts.push(escapeRegex(`${part}`));
    } else {
      throw new Error(`Invalid template literal part: ${part}`);
    }
  }
  inst._zod.pattern = new RegExp(`^${regexParts.join("")}$`);
  inst._zod.parse = (payload, _ctx) => {
    if (typeof payload.value !== "string") {
      payload.issues.push({
        input: payload.value,
        inst,
        expected: "string",
        code: "invalid_type"
      });
      return payload;
    }
    inst._zod.pattern.lastIndex = 0;
    if (!inst._zod.pattern.test(payload.value)) {
      payload.issues.push({
        input: payload.value,
        inst,
        code: "invalid_format",
        format: def.format ?? "template_literal",
        pattern: inst._zod.pattern.source
      });
      return payload;
    }
    return payload;
  };
});
var $ZodFunction = /* @__PURE__ */ $constructor("$ZodFunction", (inst, def) => {
  $ZodType.init(inst, def);
  inst._def = def;
  inst._zod.def = def;
  inst.implement = (func) => {
    if (typeof func !== "function") {
      throw new Error("implement() must be called with a function");
    }
    return function(...args) {
      const parsedArgs = inst._def.input ? parse(inst._def.input, args) : args;
      const result = Reflect.apply(func, this, parsedArgs);
      if (inst._def.output) {
        return parse(inst._def.output, result);
      }
      return result;
    };
  };
  inst.implementAsync = (func) => {
    if (typeof func !== "function") {
      throw new Error("implementAsync() must be called with a function");
    }
    return async function(...args) {
      const parsedArgs = inst._def.input ? await parseAsync(inst._def.input, args) : args;
      const result = await Reflect.apply(func, this, parsedArgs);
      if (inst._def.output) {
        return await parseAsync(inst._def.output, result);
      }
      return result;
    };
  };
  inst._zod.parse = (payload, _ctx) => {
    if (typeof payload.value !== "function") {
      payload.issues.push({
        code: "invalid_type",
        expected: "function",
        input: payload.value,
        inst
      });
      return payload;
    }
    const hasPromiseOutput = inst._def.output && inst._def.output._zod.def.type === "promise";
    if (hasPromiseOutput) {
      payload.value = inst.implementAsync(payload.value);
    } else {
      payload.value = inst.implement(payload.value);
    }
    return payload;
  };
  inst.input = (...args) => {
    const F = inst.constructor;
    if (Array.isArray(args[0])) {
      return new F({
        type: "function",
        input: new $ZodTuple({
          type: "tuple",
          items: args[0],
          rest: args[1]
        }),
        output: inst._def.output
      });
    }
    return new F({
      type: "function",
      input: args[0],
      output: inst._def.output
    });
  };
  inst.output = (output) => {
    const F = inst.constructor;
    return new F({
      type: "function",
      input: inst._def.input,
      output
    });
  };
  return inst;
});
var $ZodPromise = /* @__PURE__ */ $constructor("$ZodPromise", (inst, def) => {
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, ctx) => {
    return Promise.resolve(payload.value).then((inner) => def.innerType._zod.run({ value: inner, issues: [] }, ctx));
  };
});
var $ZodLazy = /* @__PURE__ */ $constructor("$ZodLazy", (inst, def) => {
  $ZodType.init(inst, def);
  defineLazy(inst._zod, "innerType", () => def.getter());
  defineLazy(inst._zod, "pattern", () => inst._zod.innerType?._zod?.pattern);
  defineLazy(inst._zod, "propValues", () => inst._zod.innerType?._zod?.propValues);
  defineLazy(inst._zod, "optin", () => inst._zod.innerType?._zod?.optin ?? void 0);
  defineLazy(inst._zod, "optout", () => inst._zod.innerType?._zod?.optout ?? void 0);
  inst._zod.parse = (payload, ctx) => {
    const inner = inst._zod.innerType;
    return inner._zod.run(payload, ctx);
  };
});
var $ZodCustom = /* @__PURE__ */ $constructor("$ZodCustom", (inst, def) => {
  $ZodCheck.init(inst, def);
  $ZodType.init(inst, def);
  inst._zod.parse = (payload, _) => {
    return payload;
  };
  inst._zod.check = (payload) => {
    const input = payload.value;
    const r = def.fn(input);
    if (r instanceof Promise) {
      return r.then((r2) => handleRefineResult(r2, payload, input, inst));
    }
    handleRefineResult(r, payload, input, inst);
    return;
  };
});
function handleRefineResult(result, payload, input, inst) {
  if (!result) {
    const _iss = {
      code: "custom",
      input,
      inst,
      // incorporates params.error into issue reporting
      path: [...inst._zod.def.path ?? []],
      // incorporates params.error into issue reporting
      continue: !inst._zod.def.abort
      // params: inst._zod.def.params,
    };
    if (inst._zod.def.params)
      _iss.params = inst._zod.def.params;
    payload.issues.push(issue(_iss));
  }
}

// ../../node_modules/zod/v4/locales/en.js
var error = () => {
  const Sizable = {
    string: { unit: "characters", verb: "to have" },
    file: { unit: "bytes", verb: "to have" },
    array: { unit: "items", verb: "to have" },
    set: { unit: "items", verb: "to have" },
    map: { unit: "entries", verb: "to have" }
  };
  function getSizing(origin) {
    return Sizable[origin] ?? null;
  }
  const FormatDictionary = {
    regex: "input",
    email: "email address",
    url: "URL",
    emoji: "emoji",
    uuid: "UUID",
    uuidv4: "UUIDv4",
    uuidv6: "UUIDv6",
    nanoid: "nanoid",
    guid: "GUID",
    cuid: "cuid",
    cuid2: "cuid2",
    ulid: "ULID",
    xid: "XID",
    ksuid: "KSUID",
    datetime: "ISO datetime",
    date: "ISO date",
    time: "ISO time",
    duration: "ISO duration",
    ipv4: "IPv4 address",
    ipv6: "IPv6 address",
    mac: "MAC address",
    cidrv4: "IPv4 range",
    cidrv6: "IPv6 range",
    base64: "base64-encoded string",
    base64url: "base64url-encoded string",
    json_string: "JSON string",
    e164: "E.164 number",
    jwt: "JWT",
    template_literal: "input"
  };
  const TypeDictionary = {
    // Compatibility: "nan" -> "NaN" for display
    nan: "NaN"
    // All other type names omitted - they fall back to raw values via ?? operator
  };
  return (issue2) => {
    switch (issue2.code) {
      case "invalid_type": {
        const expected = TypeDictionary[issue2.expected] ?? issue2.expected;
        const receivedType = parsedType(issue2.input);
        const received = TypeDictionary[receivedType] ?? receivedType;
        return `Invalid input: expected ${expected}, received ${received}`;
      }
      case "invalid_value":
        if (issue2.values.length === 1)
          return `Invalid input: expected ${stringifyPrimitive(issue2.values[0])}`;
        return `Invalid option: expected one of ${joinValues(issue2.values, "|")}`;
      case "too_big": {
        const adj = issue2.inclusive ? "<=" : "<";
        const sizing = getSizing(issue2.origin);
        if (sizing)
          return `Too big: expected ${issue2.origin ?? "value"} to have ${adj}${issue2.maximum.toString()} ${sizing.unit ?? "elements"}`;
        return `Too big: expected ${issue2.origin ?? "value"} to be ${adj}${issue2.maximum.toString()}`;
      }
      case "too_small": {
        const adj = issue2.inclusive ? ">=" : ">";
        const sizing = getSizing(issue2.origin);
        if (sizing) {
          return `Too small: expected ${issue2.origin} to have ${adj}${issue2.minimum.toString()} ${sizing.unit}`;
        }
        return `Too small: expected ${issue2.origin} to be ${adj}${issue2.minimum.toString()}`;
      }
      case "invalid_format": {
        const _issue = issue2;
        if (_issue.format === "starts_with") {
          return `Invalid string: must start with "${_issue.prefix}"`;
        }
        if (_issue.format === "ends_with")
          return `Invalid string: must end with "${_issue.suffix}"`;
        if (_issue.format === "includes")
          return `Invalid string: must include "${_issue.includes}"`;
        if (_issue.format === "regex")
          return `Invalid string: must match pattern ${_issue.pattern}`;
        return `Invalid ${FormatDictionary[_issue.format] ?? issue2.format}`;
      }
      case "not_multiple_of":
        return `Invalid number: must be a multiple of ${issue2.divisor}`;
      case "unrecognized_keys":
        return `Unrecognized key${issue2.keys.length > 1 ? "s" : ""}: ${joinValues(issue2.keys, ", ")}`;
      case "invalid_key":
        return `Invalid key in ${issue2.origin}`;
      case "invalid_union":
        return "Invalid input";
      case "invalid_element":
        return `Invalid value in ${issue2.origin}`;
      default:
        return `Invalid input`;
    }
  };
};
function en_default() {
  return {
    localeError: error()
  };
}

// ../../node_modules/zod/v4/core/registries.js
var _a;
var $ZodRegistry = class {
  constructor() {
    this._map = /* @__PURE__ */ new WeakMap();
    this._idmap = /* @__PURE__ */ new Map();
  }
  add(schema, ..._meta) {
    const meta3 = _meta[0];
    this._map.set(schema, meta3);
    if (meta3 && typeof meta3 === "object" && "id" in meta3) {
      this._idmap.set(meta3.id, schema);
    }
    return this;
  }
  clear() {
    this._map = /* @__PURE__ */ new WeakMap();
    this._idmap = /* @__PURE__ */ new Map();
    return this;
  }
  remove(schema) {
    const meta3 = this._map.get(schema);
    if (meta3 && typeof meta3 === "object" && "id" in meta3) {
      this._idmap.delete(meta3.id);
    }
    this._map.delete(schema);
    return this;
  }
  get(schema) {
    const p = schema._zod.parent;
    if (p) {
      const pm = { ...this.get(p) ?? {} };
      delete pm.id;
      const f = { ...pm, ...this._map.get(schema) };
      return Object.keys(f).length ? f : void 0;
    }
    return this._map.get(schema);
  }
  has(schema) {
    return this._map.has(schema);
  }
};
function registry() {
  return new $ZodRegistry();
}
(_a = globalThis).__zod_globalRegistry ?? (_a.__zod_globalRegistry = registry());
var globalRegistry = globalThis.__zod_globalRegistry;

// ../../node_modules/zod/v4/core/api.js
// @__NO_SIDE_EFFECTS__
function _string(Class2, params) {
  return new Class2({
    type: "string",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _coercedString(Class2, params) {
  return new Class2({
    type: "string",
    coerce: true,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _email(Class2, params) {
  return new Class2({
    type: "string",
    format: "email",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _guid(Class2, params) {
  return new Class2({
    type: "string",
    format: "guid",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _uuid(Class2, params) {
  return new Class2({
    type: "string",
    format: "uuid",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _uuidv4(Class2, params) {
  return new Class2({
    type: "string",
    format: "uuid",
    check: "string_format",
    abort: false,
    version: "v4",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _uuidv6(Class2, params) {
  return new Class2({
    type: "string",
    format: "uuid",
    check: "string_format",
    abort: false,
    version: "v6",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _uuidv7(Class2, params) {
  return new Class2({
    type: "string",
    format: "uuid",
    check: "string_format",
    abort: false,
    version: "v7",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _url(Class2, params) {
  return new Class2({
    type: "string",
    format: "url",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _emoji2(Class2, params) {
  return new Class2({
    type: "string",
    format: "emoji",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _nanoid(Class2, params) {
  return new Class2({
    type: "string",
    format: "nanoid",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _cuid(Class2, params) {
  return new Class2({
    type: "string",
    format: "cuid",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _cuid2(Class2, params) {
  return new Class2({
    type: "string",
    format: "cuid2",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _ulid(Class2, params) {
  return new Class2({
    type: "string",
    format: "ulid",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _xid(Class2, params) {
  return new Class2({
    type: "string",
    format: "xid",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _ksuid(Class2, params) {
  return new Class2({
    type: "string",
    format: "ksuid",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _ipv4(Class2, params) {
  return new Class2({
    type: "string",
    format: "ipv4",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _ipv6(Class2, params) {
  return new Class2({
    type: "string",
    format: "ipv6",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _mac(Class2, params) {
  return new Class2({
    type: "string",
    format: "mac",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _cidrv4(Class2, params) {
  return new Class2({
    type: "string",
    format: "cidrv4",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _cidrv6(Class2, params) {
  return new Class2({
    type: "string",
    format: "cidrv6",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _base64(Class2, params) {
  return new Class2({
    type: "string",
    format: "base64",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _base64url(Class2, params) {
  return new Class2({
    type: "string",
    format: "base64url",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _e164(Class2, params) {
  return new Class2({
    type: "string",
    format: "e164",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _jwt(Class2, params) {
  return new Class2({
    type: "string",
    format: "jwt",
    check: "string_format",
    abort: false,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _isoDateTime(Class2, params) {
  return new Class2({
    type: "string",
    format: "datetime",
    check: "string_format",
    offset: false,
    local: false,
    precision: null,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _isoDate(Class2, params) {
  return new Class2({
    type: "string",
    format: "date",
    check: "string_format",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _isoTime(Class2, params) {
  return new Class2({
    type: "string",
    format: "time",
    check: "string_format",
    precision: null,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _isoDuration(Class2, params) {
  return new Class2({
    type: "string",
    format: "duration",
    check: "string_format",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _number(Class2, params) {
  return new Class2({
    type: "number",
    checks: [],
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _coercedNumber(Class2, params) {
  return new Class2({
    type: "number",
    coerce: true,
    checks: [],
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _int(Class2, params) {
  return new Class2({
    type: "number",
    check: "number_format",
    abort: false,
    format: "safeint",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _float32(Class2, params) {
  return new Class2({
    type: "number",
    check: "number_format",
    abort: false,
    format: "float32",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _float64(Class2, params) {
  return new Class2({
    type: "number",
    check: "number_format",
    abort: false,
    format: "float64",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _int32(Class2, params) {
  return new Class2({
    type: "number",
    check: "number_format",
    abort: false,
    format: "int32",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _uint32(Class2, params) {
  return new Class2({
    type: "number",
    check: "number_format",
    abort: false,
    format: "uint32",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _boolean(Class2, params) {
  return new Class2({
    type: "boolean",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _coercedBoolean(Class2, params) {
  return new Class2({
    type: "boolean",
    coerce: true,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _bigint(Class2, params) {
  return new Class2({
    type: "bigint",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _coercedBigint(Class2, params) {
  return new Class2({
    type: "bigint",
    coerce: true,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _int64(Class2, params) {
  return new Class2({
    type: "bigint",
    check: "bigint_format",
    abort: false,
    format: "int64",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _uint64(Class2, params) {
  return new Class2({
    type: "bigint",
    check: "bigint_format",
    abort: false,
    format: "uint64",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _symbol(Class2, params) {
  return new Class2({
    type: "symbol",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _undefined2(Class2, params) {
  return new Class2({
    type: "undefined",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _null2(Class2, params) {
  return new Class2({
    type: "null",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _any(Class2) {
  return new Class2({
    type: "any"
  });
}
// @__NO_SIDE_EFFECTS__
function _unknown(Class2) {
  return new Class2({
    type: "unknown"
  });
}
// @__NO_SIDE_EFFECTS__
function _never(Class2, params) {
  return new Class2({
    type: "never",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _void(Class2, params) {
  return new Class2({
    type: "void",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _date(Class2, params) {
  return new Class2({
    type: "date",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _coercedDate(Class2, params) {
  return new Class2({
    type: "date",
    coerce: true,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _nan(Class2, params) {
  return new Class2({
    type: "nan",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _lt(value, params) {
  return new $ZodCheckLessThan({
    check: "less_than",
    ...normalizeParams(params),
    value,
    inclusive: false
  });
}
// @__NO_SIDE_EFFECTS__
function _lte(value, params) {
  return new $ZodCheckLessThan({
    check: "less_than",
    ...normalizeParams(params),
    value,
    inclusive: true
  });
}
// @__NO_SIDE_EFFECTS__
function _gt(value, params) {
  return new $ZodCheckGreaterThan({
    check: "greater_than",
    ...normalizeParams(params),
    value,
    inclusive: false
  });
}
// @__NO_SIDE_EFFECTS__
function _gte(value, params) {
  return new $ZodCheckGreaterThan({
    check: "greater_than",
    ...normalizeParams(params),
    value,
    inclusive: true
  });
}
// @__NO_SIDE_EFFECTS__
function _positive(params) {
  return /* @__PURE__ */ _gt(0, params);
}
// @__NO_SIDE_EFFECTS__
function _negative(params) {
  return /* @__PURE__ */ _lt(0, params);
}
// @__NO_SIDE_EFFECTS__
function _nonpositive(params) {
  return /* @__PURE__ */ _lte(0, params);
}
// @__NO_SIDE_EFFECTS__
function _nonnegative(params) {
  return /* @__PURE__ */ _gte(0, params);
}
// @__NO_SIDE_EFFECTS__
function _multipleOf(value, params) {
  return new $ZodCheckMultipleOf({
    check: "multiple_of",
    ...normalizeParams(params),
    value
  });
}
// @__NO_SIDE_EFFECTS__
function _maxSize(maximum, params) {
  return new $ZodCheckMaxSize({
    check: "max_size",
    ...normalizeParams(params),
    maximum
  });
}
// @__NO_SIDE_EFFECTS__
function _minSize(minimum, params) {
  return new $ZodCheckMinSize({
    check: "min_size",
    ...normalizeParams(params),
    minimum
  });
}
// @__NO_SIDE_EFFECTS__
function _size(size, params) {
  return new $ZodCheckSizeEquals({
    check: "size_equals",
    ...normalizeParams(params),
    size
  });
}
// @__NO_SIDE_EFFECTS__
function _maxLength(maximum, params) {
  const ch = new $ZodCheckMaxLength({
    check: "max_length",
    ...normalizeParams(params),
    maximum
  });
  return ch;
}
// @__NO_SIDE_EFFECTS__
function _minLength(minimum, params) {
  return new $ZodCheckMinLength({
    check: "min_length",
    ...normalizeParams(params),
    minimum
  });
}
// @__NO_SIDE_EFFECTS__
function _length(length, params) {
  return new $ZodCheckLengthEquals({
    check: "length_equals",
    ...normalizeParams(params),
    length
  });
}
// @__NO_SIDE_EFFECTS__
function _regex(pattern, params) {
  return new $ZodCheckRegex({
    check: "string_format",
    format: "regex",
    ...normalizeParams(params),
    pattern
  });
}
// @__NO_SIDE_EFFECTS__
function _lowercase(params) {
  return new $ZodCheckLowerCase({
    check: "string_format",
    format: "lowercase",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _uppercase(params) {
  return new $ZodCheckUpperCase({
    check: "string_format",
    format: "uppercase",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _includes(includes, params) {
  return new $ZodCheckIncludes({
    check: "string_format",
    format: "includes",
    ...normalizeParams(params),
    includes
  });
}
// @__NO_SIDE_EFFECTS__
function _startsWith(prefix, params) {
  return new $ZodCheckStartsWith({
    check: "string_format",
    format: "starts_with",
    ...normalizeParams(params),
    prefix
  });
}
// @__NO_SIDE_EFFECTS__
function _endsWith(suffix, params) {
  return new $ZodCheckEndsWith({
    check: "string_format",
    format: "ends_with",
    ...normalizeParams(params),
    suffix
  });
}
// @__NO_SIDE_EFFECTS__
function _property(property, schema, params) {
  return new $ZodCheckProperty({
    check: "property",
    property,
    schema,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _mime(types, params) {
  return new $ZodCheckMimeType({
    check: "mime_type",
    mime: types,
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _overwrite(tx) {
  return new $ZodCheckOverwrite({
    check: "overwrite",
    tx
  });
}
// @__NO_SIDE_EFFECTS__
function _normalize(form) {
  return /* @__PURE__ */ _overwrite((input) => input.normalize(form));
}
// @__NO_SIDE_EFFECTS__
function _trim() {
  return /* @__PURE__ */ _overwrite((input) => input.trim());
}
// @__NO_SIDE_EFFECTS__
function _toLowerCase() {
  return /* @__PURE__ */ _overwrite((input) => input.toLowerCase());
}
// @__NO_SIDE_EFFECTS__
function _toUpperCase() {
  return /* @__PURE__ */ _overwrite((input) => input.toUpperCase());
}
// @__NO_SIDE_EFFECTS__
function _slugify() {
  return /* @__PURE__ */ _overwrite((input) => slugify(input));
}
// @__NO_SIDE_EFFECTS__
function _array(Class2, element, params) {
  return new Class2({
    type: "array",
    element,
    // get element() {
    //   return element;
    // },
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _file(Class2, params) {
  return new Class2({
    type: "file",
    ...normalizeParams(params)
  });
}
// @__NO_SIDE_EFFECTS__
function _custom(Class2, fn, _params) {
  const norm = normalizeParams(_params);
  norm.abort ?? (norm.abort = true);
  const schema = new Class2({
    type: "custom",
    check: "custom",
    fn,
    ...norm
  });
  return schema;
}
// @__NO_SIDE_EFFECTS__
function _refine(Class2, fn, _params) {
  const schema = new Class2({
    type: "custom",
    check: "custom",
    fn,
    ...normalizeParams(_params)
  });
  return schema;
}
// @__NO_SIDE_EFFECTS__
function _superRefine(fn) {
  const ch = /* @__PURE__ */ _check((payload) => {
    payload.addIssue = (issue2) => {
      if (typeof issue2 === "string") {
        payload.issues.push(issue(issue2, payload.value, ch._zod.def));
      } else {
        const _issue = issue2;
        if (_issue.fatal)
          _issue.continue = false;
        _issue.code ?? (_issue.code = "custom");
        _issue.input ?? (_issue.input = payload.value);
        _issue.inst ?? (_issue.inst = ch);
        _issue.continue ?? (_issue.continue = !ch._zod.def.abort);
        payload.issues.push(issue(_issue));
      }
    };
    return fn(payload.value, payload);
  });
  return ch;
}
// @__NO_SIDE_EFFECTS__
function _check(fn, params) {
  const ch = new $ZodCheck({
    check: "custom",
    ...normalizeParams(params)
  });
  ch._zod.check = fn;
  return ch;
}
// @__NO_SIDE_EFFECTS__
function describe(description) {
  const ch = new $ZodCheck({ check: "describe" });
  ch._zod.onattach = [
    (inst) => {
      const existing = globalRegistry.get(inst) ?? {};
      globalRegistry.add(inst, { ...existing, description });
    }
  ];
  ch._zod.check = () => {
  };
  return ch;
}
// @__NO_SIDE_EFFECTS__
function meta(metadata) {
  const ch = new $ZodCheck({ check: "meta" });
  ch._zod.onattach = [
    (inst) => {
      const existing = globalRegistry.get(inst) ?? {};
      globalRegistry.add(inst, { ...existing, ...metadata });
    }
  ];
  ch._zod.check = () => {
  };
  return ch;
}
// @__NO_SIDE_EFFECTS__
function _stringbool(Classes, _params) {
  const params = normalizeParams(_params);
  let truthyArray = params.truthy ?? ["true", "1", "yes", "on", "y", "enabled"];
  let falsyArray = params.falsy ?? ["false", "0", "no", "off", "n", "disabled"];
  if (params.case !== "sensitive") {
    truthyArray = truthyArray.map((v) => typeof v === "string" ? v.toLowerCase() : v);
    falsyArray = falsyArray.map((v) => typeof v === "string" ? v.toLowerCase() : v);
  }
  const truthySet = new Set(truthyArray);
  const falsySet = new Set(falsyArray);
  const _Codec = Classes.Codec ?? $ZodCodec;
  const _Boolean = Classes.Boolean ?? $ZodBoolean;
  const _String = Classes.String ?? $ZodString;
  const stringSchema = new _String({ type: "string", error: params.error });
  const booleanSchema = new _Boolean({ type: "boolean", error: params.error });
  const codec2 = new _Codec({
    type: "pipe",
    in: stringSchema,
    out: booleanSchema,
    transform: ((input, payload) => {
      let data = input;
      if (params.case !== "sensitive")
        data = data.toLowerCase();
      if (truthySet.has(data)) {
        return true;
      } else if (falsySet.has(data)) {
        return false;
      } else {
        payload.issues.push({
          code: "invalid_value",
          expected: "stringbool",
          values: [...truthySet, ...falsySet],
          input: payload.value,
          inst: codec2,
          continue: false
        });
        return {};
      }
    }),
    reverseTransform: ((input, _payload) => {
      if (input === true) {
        return truthyArray[0] || "true";
      } else {
        return falsyArray[0] || "false";
      }
    }),
    error: params.error
  });
  return codec2;
}
// @__NO_SIDE_EFFECTS__
function _stringFormat(Class2, format2, fnOrRegex, _params = {}) {
  const params = normalizeParams(_params);
  const def = {
    ...normalizeParams(_params),
    check: "string_format",
    type: "string",
    format: format2,
    fn: typeof fnOrRegex === "function" ? fnOrRegex : (val) => fnOrRegex.test(val),
    ...params
  };
  if (fnOrRegex instanceof RegExp) {
    def.pattern = fnOrRegex;
  }
  const inst = new Class2(def);
  return inst;
}

// ../../node_modules/zod/v4/core/to-json-schema.js
function initializeContext(params) {
  let target = params?.target ?? "draft-2020-12";
  if (target === "draft-4")
    target = "draft-04";
  if (target === "draft-7")
    target = "draft-07";
  return {
    processors: params.processors ?? {},
    metadataRegistry: params?.metadata ?? globalRegistry,
    target,
    unrepresentable: params?.unrepresentable ?? "throw",
    override: params?.override ?? (() => {
    }),
    io: params?.io ?? "output",
    counter: 0,
    seen: /* @__PURE__ */ new Map(),
    cycles: params?.cycles ?? "ref",
    reused: params?.reused ?? "inline",
    external: params?.external ?? void 0
  };
}
function process2(schema, ctx, _params = { path: [], schemaPath: [] }) {
  var _a2;
  const def = schema._zod.def;
  const seen = ctx.seen.get(schema);
  if (seen) {
    seen.count++;
    const isCycle = _params.schemaPath.includes(schema);
    if (isCycle) {
      seen.cycle = _params.path;
    }
    return seen.schema;
  }
  const result = { schema: {}, count: 1, cycle: void 0, path: _params.path };
  ctx.seen.set(schema, result);
  const overrideSchema = schema._zod.toJSONSchema?.();
  if (overrideSchema) {
    result.schema = overrideSchema;
  } else {
    const params = {
      ..._params,
      schemaPath: [..._params.schemaPath, schema],
      path: _params.path
    };
    if (schema._zod.processJSONSchema) {
      schema._zod.processJSONSchema(ctx, result.schema, params);
    } else {
      const _json = result.schema;
      const processor = ctx.processors[def.type];
      if (!processor) {
        throw new Error(`[toJSONSchema]: Non-representable type encountered: ${def.type}`);
      }
      processor(schema, ctx, _json, params);
    }
    const parent = schema._zod.parent;
    if (parent) {
      if (!result.ref)
        result.ref = parent;
      process2(parent, ctx, params);
      ctx.seen.get(parent).isParent = true;
    }
  }
  const meta3 = ctx.metadataRegistry.get(schema);
  if (meta3)
    Object.assign(result.schema, meta3);
  if (ctx.io === "input" && isTransforming(schema)) {
    delete result.schema.examples;
    delete result.schema.default;
  }
  if (ctx.io === "input" && result.schema._prefault)
    (_a2 = result.schema).default ?? (_a2.default = result.schema._prefault);
  delete result.schema._prefault;
  const _result = ctx.seen.get(schema);
  return _result.schema;
}
function extractDefs(ctx, schema) {
  const root = ctx.seen.get(schema);
  if (!root)
    throw new Error("Unprocessed schema. This is a bug in Zod.");
  const idToSchema = /* @__PURE__ */ new Map();
  for (const entry of ctx.seen.entries()) {
    const id = ctx.metadataRegistry.get(entry[0])?.id;
    if (id) {
      const existing = idToSchema.get(id);
      if (existing && existing !== entry[0]) {
        throw new Error(`Duplicate schema id "${id}" detected during JSON Schema conversion. Two different schemas cannot share the same id when converted together.`);
      }
      idToSchema.set(id, entry[0]);
    }
  }
  const makeURI = (entry) => {
    const defsSegment = ctx.target === "draft-2020-12" ? "$defs" : "definitions";
    if (ctx.external) {
      const externalId = ctx.external.registry.get(entry[0])?.id;
      const uriGenerator = ctx.external.uri ?? ((id2) => id2);
      if (externalId) {
        return { ref: uriGenerator(externalId) };
      }
      const id = entry[1].defId ?? entry[1].schema.id ?? `schema${ctx.counter++}`;
      entry[1].defId = id;
      return { defId: id, ref: `${uriGenerator("__shared")}#/${defsSegment}/${id}` };
    }
    if (entry[1] === root) {
      return { ref: "#" };
    }
    const uriPrefix = `#`;
    const defUriPrefix = `${uriPrefix}/${defsSegment}/`;
    const defId = entry[1].schema.id ?? `__schema${ctx.counter++}`;
    return { defId, ref: defUriPrefix + defId };
  };
  const extractToDef = (entry) => {
    if (entry[1].schema.$ref) {
      return;
    }
    const seen = entry[1];
    const { ref, defId } = makeURI(entry);
    seen.def = { ...seen.schema };
    if (defId)
      seen.defId = defId;
    const schema2 = seen.schema;
    for (const key in schema2) {
      delete schema2[key];
    }
    schema2.$ref = ref;
  };
  if (ctx.cycles === "throw") {
    for (const entry of ctx.seen.entries()) {
      const seen = entry[1];
      if (seen.cycle) {
        throw new Error(`Cycle detected: #/${seen.cycle?.join("/")}/<root>

Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.`);
      }
    }
  }
  for (const entry of ctx.seen.entries()) {
    const seen = entry[1];
    if (schema === entry[0]) {
      extractToDef(entry);
      continue;
    }
    if (ctx.external) {
      const ext = ctx.external.registry.get(entry[0])?.id;
      if (schema !== entry[0] && ext) {
        extractToDef(entry);
        continue;
      }
    }
    const id = ctx.metadataRegistry.get(entry[0])?.id;
    if (id) {
      extractToDef(entry);
      continue;
    }
    if (seen.cycle) {
      extractToDef(entry);
      continue;
    }
    if (seen.count > 1) {
      if (ctx.reused === "ref") {
        extractToDef(entry);
        continue;
      }
    }
  }
}
function finalize(ctx, schema) {
  const root = ctx.seen.get(schema);
  if (!root)
    throw new Error("Unprocessed schema. This is a bug in Zod.");
  const flattenRef = (zodSchema) => {
    const seen = ctx.seen.get(zodSchema);
    if (seen.ref === null)
      return;
    const schema2 = seen.def ?? seen.schema;
    const _cached = { ...schema2 };
    const ref = seen.ref;
    seen.ref = null;
    if (ref) {
      flattenRef(ref);
      const refSeen = ctx.seen.get(ref);
      const refSchema = refSeen.schema;
      if (refSchema.$ref && (ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0")) {
        schema2.allOf = schema2.allOf ?? [];
        schema2.allOf.push(refSchema);
      } else {
        Object.assign(schema2, refSchema);
      }
      Object.assign(schema2, _cached);
      const isParentRef = zodSchema._zod.parent === ref;
      if (isParentRef) {
        for (const key in schema2) {
          if (key === "$ref" || key === "allOf")
            continue;
          if (!(key in _cached)) {
            delete schema2[key];
          }
        }
      }
      if (refSchema.$ref && refSeen.def) {
        for (const key in schema2) {
          if (key === "$ref" || key === "allOf")
            continue;
          if (key in refSeen.def && JSON.stringify(schema2[key]) === JSON.stringify(refSeen.def[key])) {
            delete schema2[key];
          }
        }
      }
    }
    const parent = zodSchema._zod.parent;
    if (parent && parent !== ref) {
      flattenRef(parent);
      const parentSeen = ctx.seen.get(parent);
      if (parentSeen?.schema.$ref) {
        schema2.$ref = parentSeen.schema.$ref;
        if (parentSeen.def) {
          for (const key in schema2) {
            if (key === "$ref" || key === "allOf")
              continue;
            if (key in parentSeen.def && JSON.stringify(schema2[key]) === JSON.stringify(parentSeen.def[key])) {
              delete schema2[key];
            }
          }
        }
      }
    }
    ctx.override({
      zodSchema,
      jsonSchema: schema2,
      path: seen.path ?? []
    });
  };
  for (const entry of [...ctx.seen.entries()].reverse()) {
    flattenRef(entry[0]);
  }
  const result = {};
  if (ctx.target === "draft-2020-12") {
    result.$schema = "https://json-schema.org/draft/2020-12/schema";
  } else if (ctx.target === "draft-07") {
    result.$schema = "http://json-schema.org/draft-07/schema#";
  } else if (ctx.target === "draft-04") {
    result.$schema = "http://json-schema.org/draft-04/schema#";
  } else if (ctx.target === "openapi-3.0") {
  } else {
  }
  if (ctx.external?.uri) {
    const id = ctx.external.registry.get(schema)?.id;
    if (!id)
      throw new Error("Schema is missing an `id` property");
    result.$id = ctx.external.uri(id);
  }
  Object.assign(result, root.def ?? root.schema);
  const defs = ctx.external?.defs ?? {};
  for (const entry of ctx.seen.entries()) {
    const seen = entry[1];
    if (seen.def && seen.defId) {
      defs[seen.defId] = seen.def;
    }
  }
  if (ctx.external) {
  } else {
    if (Object.keys(defs).length > 0) {
      if (ctx.target === "draft-2020-12") {
        result.$defs = defs;
      } else {
        result.definitions = defs;
      }
    }
  }
  try {
    const finalized = JSON.parse(JSON.stringify(result));
    Object.defineProperty(finalized, "~standard", {
      value: {
        ...schema["~standard"],
        jsonSchema: {
          input: createStandardJSONSchemaMethod(schema, "input", ctx.processors),
          output: createStandardJSONSchemaMethod(schema, "output", ctx.processors)
        }
      },
      enumerable: false,
      writable: false
    });
    return finalized;
  } catch (_err) {
    throw new Error("Error converting schema to JSON.");
  }
}
function isTransforming(_schema, _ctx) {
  const ctx = _ctx ?? { seen: /* @__PURE__ */ new Set() };
  if (ctx.seen.has(_schema))
    return false;
  ctx.seen.add(_schema);
  const def = _schema._zod.def;
  if (def.type === "transform")
    return true;
  if (def.type === "array")
    return isTransforming(def.element, ctx);
  if (def.type === "set")
    return isTransforming(def.valueType, ctx);
  if (def.type === "lazy")
    return isTransforming(def.getter(), ctx);
  if (def.type === "promise" || def.type === "optional" || def.type === "nonoptional" || def.type === "nullable" || def.type === "readonly" || def.type === "default" || def.type === "prefault") {
    return isTransforming(def.innerType, ctx);
  }
  if (def.type === "intersection") {
    return isTransforming(def.left, ctx) || isTransforming(def.right, ctx);
  }
  if (def.type === "record" || def.type === "map") {
    return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
  }
  if (def.type === "pipe") {
    return isTransforming(def.in, ctx) || isTransforming(def.out, ctx);
  }
  if (def.type === "object") {
    for (const key in def.shape) {
      if (isTransforming(def.shape[key], ctx))
        return true;
    }
    return false;
  }
  if (def.type === "union") {
    for (const option of def.options) {
      if (isTransforming(option, ctx))
        return true;
    }
    return false;
  }
  if (def.type === "tuple") {
    for (const item of def.items) {
      if (isTransforming(item, ctx))
        return true;
    }
    if (def.rest && isTransforming(def.rest, ctx))
      return true;
    return false;
  }
  return false;
}
var createToJSONSchemaMethod = (schema, processors = {}) => (params) => {
  const ctx = initializeContext({ ...params, processors });
  process2(schema, ctx);
  extractDefs(ctx, schema);
  return finalize(ctx, schema);
};
var createStandardJSONSchemaMethod = (schema, io, processors = {}) => (params) => {
  const { libraryOptions, target } = params ?? {};
  const ctx = initializeContext({ ...libraryOptions ?? {}, target, io, processors });
  process2(schema, ctx);
  extractDefs(ctx, schema);
  return finalize(ctx, schema);
};

// ../../node_modules/zod/v4/core/json-schema-processors.js
var formatMap = {
  guid: "uuid",
  url: "uri",
  datetime: "date-time",
  json_string: "json-string",
  regex: ""
  // do not set
};
var stringProcessor = (schema, ctx, _json, _params) => {
  const json2 = _json;
  json2.type = "string";
  const { minimum, maximum, format: format2, patterns, contentEncoding } = schema._zod.bag;
  if (typeof minimum === "number")
    json2.minLength = minimum;
  if (typeof maximum === "number")
    json2.maxLength = maximum;
  if (format2) {
    json2.format = formatMap[format2] ?? format2;
    if (json2.format === "")
      delete json2.format;
    if (format2 === "time") {
      delete json2.format;
    }
  }
  if (contentEncoding)
    json2.contentEncoding = contentEncoding;
  if (patterns && patterns.size > 0) {
    const regexes = [...patterns];
    if (regexes.length === 1)
      json2.pattern = regexes[0].source;
    else if (regexes.length > 1) {
      json2.allOf = [
        ...regexes.map((regex2) => ({
          ...ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0" ? { type: "string" } : {},
          pattern: regex2.source
        }))
      ];
    }
  }
};
var numberProcessor = (schema, ctx, _json, _params) => {
  const json2 = _json;
  const { minimum, maximum, format: format2, multipleOf, exclusiveMaximum, exclusiveMinimum } = schema._zod.bag;
  if (typeof format2 === "string" && format2.includes("int"))
    json2.type = "integer";
  else
    json2.type = "number";
  if (typeof exclusiveMinimum === "number") {
    if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
      json2.minimum = exclusiveMinimum;
      json2.exclusiveMinimum = true;
    } else {
      json2.exclusiveMinimum = exclusiveMinimum;
    }
  }
  if (typeof minimum === "number") {
    json2.minimum = minimum;
    if (typeof exclusiveMinimum === "number" && ctx.target !== "draft-04") {
      if (exclusiveMinimum >= minimum)
        delete json2.minimum;
      else
        delete json2.exclusiveMinimum;
    }
  }
  if (typeof exclusiveMaximum === "number") {
    if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
      json2.maximum = exclusiveMaximum;
      json2.exclusiveMaximum = true;
    } else {
      json2.exclusiveMaximum = exclusiveMaximum;
    }
  }
  if (typeof maximum === "number") {
    json2.maximum = maximum;
    if (typeof exclusiveMaximum === "number" && ctx.target !== "draft-04") {
      if (exclusiveMaximum <= maximum)
        delete json2.maximum;
      else
        delete json2.exclusiveMaximum;
    }
  }
  if (typeof multipleOf === "number")
    json2.multipleOf = multipleOf;
};
var booleanProcessor = (_schema, _ctx, json2, _params) => {
  json2.type = "boolean";
};
var bigintProcessor = (_schema, ctx, _json, _params) => {
  if (ctx.unrepresentable === "throw") {
    throw new Error("BigInt cannot be represented in JSON Schema");
  }
};
var symbolProcessor = (_schema, ctx, _json, _params) => {
  if (ctx.unrepresentable === "throw") {
    throw new Error("Symbols cannot be represented in JSON Schema");
  }
};
var nullProcessor = (_schema, ctx, json2, _params) => {
  if (ctx.target === "openapi-3.0") {
    json2.type = "string";
    json2.nullable = true;
    json2.enum = [null];
  } else {
    json2.type = "null";
  }
};
var undefinedProcessor = (_schema, ctx, _json, _params) => {
  if (ctx.unrepresentable === "throw") {
    throw new Error("Undefined cannot be represented in JSON Schema");
  }
};
var voidProcessor = (_schema, ctx, _json, _params) => {
  if (ctx.unrepresentable === "throw") {
    throw new Error("Void cannot be represented in JSON Schema");
  }
};
var neverProcessor = (_schema, _ctx, json2, _params) => {
  json2.not = {};
};
var anyProcessor = (_schema, _ctx, _json, _params) => {
};
var unknownProcessor = (_schema, _ctx, _json, _params) => {
};
var dateProcessor = (_schema, ctx, _json, _params) => {
  if (ctx.unrepresentable === "throw") {
    throw new Error("Date cannot be represented in JSON Schema");
  }
};
var enumProcessor = (schema, _ctx, json2, _params) => {
  const def = schema._zod.def;
  const values = getEnumValues(def.entries);
  if (values.every((v) => typeof v === "number"))
    json2.type = "number";
  if (values.every((v) => typeof v === "string"))
    json2.type = "string";
  json2.enum = values;
};
var literalProcessor = (schema, ctx, json2, _params) => {
  const def = schema._zod.def;
  const vals = [];
  for (const val of def.values) {
    if (val === void 0) {
      if (ctx.unrepresentable === "throw") {
        throw new Error("Literal `undefined` cannot be represented in JSON Schema");
      } else {
      }
    } else if (typeof val === "bigint") {
      if (ctx.unrepresentable === "throw") {
        throw new Error("BigInt literals cannot be represented in JSON Schema");
      } else {
        vals.push(Number(val));
      }
    } else {
      vals.push(val);
    }
  }
  if (vals.length === 0) {
  } else if (vals.length === 1) {
    const val = vals[0];
    json2.type = val === null ? "null" : typeof val;
    if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
      json2.enum = [val];
    } else {
      json2.const = val;
    }
  } else {
    if (vals.every((v) => typeof v === "number"))
      json2.type = "number";
    if (vals.every((v) => typeof v === "string"))
      json2.type = "string";
    if (vals.every((v) => typeof v === "boolean"))
      json2.type = "boolean";
    if (vals.every((v) => v === null))
      json2.type = "null";
    json2.enum = vals;
  }
};
var nanProcessor = (_schema, ctx, _json, _params) => {
  if (ctx.unrepresentable === "throw") {
    throw new Error("NaN cannot be represented in JSON Schema");
  }
};
var templateLiteralProcessor = (schema, _ctx, json2, _params) => {
  const _json = json2;
  const pattern = schema._zod.pattern;
  if (!pattern)
    throw new Error("Pattern not found in template literal");
  _json.type = "string";
  _json.pattern = pattern.source;
};
var fileProcessor = (schema, _ctx, json2, _params) => {
  const _json = json2;
  const file2 = {
    type: "string",
    format: "binary",
    contentEncoding: "binary"
  };
  const { minimum, maximum, mime } = schema._zod.bag;
  if (minimum !== void 0)
    file2.minLength = minimum;
  if (maximum !== void 0)
    file2.maxLength = maximum;
  if (mime) {
    if (mime.length === 1) {
      file2.contentMediaType = mime[0];
      Object.assign(_json, file2);
    } else {
      Object.assign(_json, file2);
      _json.anyOf = mime.map((m) => ({ contentMediaType: m }));
    }
  } else {
    Object.assign(_json, file2);
  }
};
var successProcessor = (_schema, _ctx, json2, _params) => {
  json2.type = "boolean";
};
var customProcessor = (_schema, ctx, _json, _params) => {
  if (ctx.unrepresentable === "throw") {
    throw new Error("Custom types cannot be represented in JSON Schema");
  }
};
var functionProcessor = (_schema, ctx, _json, _params) => {
  if (ctx.unrepresentable === "throw") {
    throw new Error("Function types cannot be represented in JSON Schema");
  }
};
var transformProcessor = (_schema, ctx, _json, _params) => {
  if (ctx.unrepresentable === "throw") {
    throw new Error("Transforms cannot be represented in JSON Schema");
  }
};
var mapProcessor = (_schema, ctx, _json, _params) => {
  if (ctx.unrepresentable === "throw") {
    throw new Error("Map cannot be represented in JSON Schema");
  }
};
var setProcessor = (_schema, ctx, _json, _params) => {
  if (ctx.unrepresentable === "throw") {
    throw new Error("Set cannot be represented in JSON Schema");
  }
};
var arrayProcessor = (schema, ctx, _json, params) => {
  const json2 = _json;
  const def = schema._zod.def;
  const { minimum, maximum } = schema._zod.bag;
  if (typeof minimum === "number")
    json2.minItems = minimum;
  if (typeof maximum === "number")
    json2.maxItems = maximum;
  json2.type = "array";
  json2.items = process2(def.element, ctx, { ...params, path: [...params.path, "items"] });
};
var objectProcessor = (schema, ctx, _json, params) => {
  const json2 = _json;
  const def = schema._zod.def;
  json2.type = "object";
  json2.properties = {};
  const shape = def.shape;
  for (const key in shape) {
    json2.properties[key] = process2(shape[key], ctx, {
      ...params,
      path: [...params.path, "properties", key]
    });
  }
  const allKeys = new Set(Object.keys(shape));
  const requiredKeys = new Set([...allKeys].filter((key) => {
    const v = def.shape[key]._zod;
    if (ctx.io === "input") {
      return v.optin === void 0;
    } else {
      return v.optout === void 0;
    }
  }));
  if (requiredKeys.size > 0) {
    json2.required = Array.from(requiredKeys);
  }
  if (def.catchall?._zod.def.type === "never") {
    json2.additionalProperties = false;
  } else if (!def.catchall) {
    if (ctx.io === "output")
      json2.additionalProperties = false;
  } else if (def.catchall) {
    json2.additionalProperties = process2(def.catchall, ctx, {
      ...params,
      path: [...params.path, "additionalProperties"]
    });
  }
};
var unionProcessor = (schema, ctx, json2, params) => {
  const def = schema._zod.def;
  const isExclusive = def.inclusive === false;
  const options = def.options.map((x, i) => process2(x, ctx, {
    ...params,
    path: [...params.path, isExclusive ? "oneOf" : "anyOf", i]
  }));
  if (isExclusive) {
    json2.oneOf = options;
  } else {
    json2.anyOf = options;
  }
};
var intersectionProcessor = (schema, ctx, json2, params) => {
  const def = schema._zod.def;
  const a = process2(def.left, ctx, {
    ...params,
    path: [...params.path, "allOf", 0]
  });
  const b = process2(def.right, ctx, {
    ...params,
    path: [...params.path, "allOf", 1]
  });
  const isSimpleIntersection = (val) => "allOf" in val && Object.keys(val).length === 1;
  const allOf = [
    ...isSimpleIntersection(a) ? a.allOf : [a],
    ...isSimpleIntersection(b) ? b.allOf : [b]
  ];
  json2.allOf = allOf;
};
var tupleProcessor = (schema, ctx, _json, params) => {
  const json2 = _json;
  const def = schema._zod.def;
  json2.type = "array";
  const prefixPath = ctx.target === "draft-2020-12" ? "prefixItems" : "items";
  const restPath = ctx.target === "draft-2020-12" ? "items" : ctx.target === "openapi-3.0" ? "items" : "additionalItems";
  const prefixItems = def.items.map((x, i) => process2(x, ctx, {
    ...params,
    path: [...params.path, prefixPath, i]
  }));
  const rest = def.rest ? process2(def.rest, ctx, {
    ...params,
    path: [...params.path, restPath, ...ctx.target === "openapi-3.0" ? [def.items.length] : []]
  }) : null;
  if (ctx.target === "draft-2020-12") {
    json2.prefixItems = prefixItems;
    if (rest) {
      json2.items = rest;
    }
  } else if (ctx.target === "openapi-3.0") {
    json2.items = {
      anyOf: prefixItems
    };
    if (rest) {
      json2.items.anyOf.push(rest);
    }
    json2.minItems = prefixItems.length;
    if (!rest) {
      json2.maxItems = prefixItems.length;
    }
  } else {
    json2.items = prefixItems;
    if (rest) {
      json2.additionalItems = rest;
    }
  }
  const { minimum, maximum } = schema._zod.bag;
  if (typeof minimum === "number")
    json2.minItems = minimum;
  if (typeof maximum === "number")
    json2.maxItems = maximum;
};
var recordProcessor = (schema, ctx, _json, params) => {
  const json2 = _json;
  const def = schema._zod.def;
  json2.type = "object";
  const keyType = def.keyType;
  const keyBag = keyType._zod.bag;
  const patterns = keyBag?.patterns;
  if (def.mode === "loose" && patterns && patterns.size > 0) {
    const valueSchema = process2(def.valueType, ctx, {
      ...params,
      path: [...params.path, "patternProperties", "*"]
    });
    json2.patternProperties = {};
    for (const pattern of patterns) {
      json2.patternProperties[pattern.source] = valueSchema;
    }
  } else {
    if (ctx.target === "draft-07" || ctx.target === "draft-2020-12") {
      json2.propertyNames = process2(def.keyType, ctx, {
        ...params,
        path: [...params.path, "propertyNames"]
      });
    }
    json2.additionalProperties = process2(def.valueType, ctx, {
      ...params,
      path: [...params.path, "additionalProperties"]
    });
  }
  const keyValues = keyType._zod.values;
  if (keyValues) {
    const validKeyValues = [...keyValues].filter((v) => typeof v === "string" || typeof v === "number");
    if (validKeyValues.length > 0) {
      json2.required = validKeyValues;
    }
  }
};
var nullableProcessor = (schema, ctx, json2, params) => {
  const def = schema._zod.def;
  const inner = process2(def.innerType, ctx, params);
  const seen = ctx.seen.get(schema);
  if (ctx.target === "openapi-3.0") {
    seen.ref = def.innerType;
    json2.nullable = true;
  } else {
    json2.anyOf = [inner, { type: "null" }];
  }
};
var nonoptionalProcessor = (schema, ctx, _json, params) => {
  const def = schema._zod.def;
  process2(def.innerType, ctx, params);
  const seen = ctx.seen.get(schema);
  seen.ref = def.innerType;
};
var defaultProcessor = (schema, ctx, json2, params) => {
  const def = schema._zod.def;
  process2(def.innerType, ctx, params);
  const seen = ctx.seen.get(schema);
  seen.ref = def.innerType;
  json2.default = JSON.parse(JSON.stringify(def.defaultValue));
};
var prefaultProcessor = (schema, ctx, json2, params) => {
  const def = schema._zod.def;
  process2(def.innerType, ctx, params);
  const seen = ctx.seen.get(schema);
  seen.ref = def.innerType;
  if (ctx.io === "input")
    json2._prefault = JSON.parse(JSON.stringify(def.defaultValue));
};
var catchProcessor = (schema, ctx, json2, params) => {
  const def = schema._zod.def;
  process2(def.innerType, ctx, params);
  const seen = ctx.seen.get(schema);
  seen.ref = def.innerType;
  let catchValue;
  try {
    catchValue = def.catchValue(void 0);
  } catch {
    throw new Error("Dynamic catch values are not supported in JSON Schema");
  }
  json2.default = catchValue;
};
var pipeProcessor = (schema, ctx, _json, params) => {
  const def = schema._zod.def;
  const innerType = ctx.io === "input" ? def.in._zod.def.type === "transform" ? def.out : def.in : def.out;
  process2(innerType, ctx, params);
  const seen = ctx.seen.get(schema);
  seen.ref = innerType;
};
var readonlyProcessor = (schema, ctx, json2, params) => {
  const def = schema._zod.def;
  process2(def.innerType, ctx, params);
  const seen = ctx.seen.get(schema);
  seen.ref = def.innerType;
  json2.readOnly = true;
};
var promiseProcessor = (schema, ctx, _json, params) => {
  const def = schema._zod.def;
  process2(def.innerType, ctx, params);
  const seen = ctx.seen.get(schema);
  seen.ref = def.innerType;
};
var optionalProcessor = (schema, ctx, _json, params) => {
  const def = schema._zod.def;
  process2(def.innerType, ctx, params);
  const seen = ctx.seen.get(schema);
  seen.ref = def.innerType;
};
var lazyProcessor = (schema, ctx, _json, params) => {
  const innerType = schema._zod.innerType;
  process2(innerType, ctx, params);
  const seen = ctx.seen.get(schema);
  seen.ref = innerType;
};

// ../../node_modules/zod/v4/classic/schemas.js
var schemas_exports2 = {};
__export(schemas_exports2, {
  ZodAny: () => ZodAny,
  ZodArray: () => ZodArray,
  ZodBase64: () => ZodBase64,
  ZodBase64URL: () => ZodBase64URL,
  ZodBigInt: () => ZodBigInt,
  ZodBigIntFormat: () => ZodBigIntFormat,
  ZodBoolean: () => ZodBoolean,
  ZodCIDRv4: () => ZodCIDRv4,
  ZodCIDRv6: () => ZodCIDRv6,
  ZodCUID: () => ZodCUID,
  ZodCUID2: () => ZodCUID2,
  ZodCatch: () => ZodCatch,
  ZodCodec: () => ZodCodec,
  ZodCustom: () => ZodCustom,
  ZodCustomStringFormat: () => ZodCustomStringFormat,
  ZodDate: () => ZodDate,
  ZodDefault: () => ZodDefault,
  ZodDiscriminatedUnion: () => ZodDiscriminatedUnion,
  ZodE164: () => ZodE164,
  ZodEmail: () => ZodEmail,
  ZodEmoji: () => ZodEmoji,
  ZodEnum: () => ZodEnum,
  ZodExactOptional: () => ZodExactOptional,
  ZodFile: () => ZodFile,
  ZodFunction: () => ZodFunction,
  ZodGUID: () => ZodGUID,
  ZodIPv4: () => ZodIPv4,
  ZodIPv6: () => ZodIPv6,
  ZodIntersection: () => ZodIntersection,
  ZodJWT: () => ZodJWT,
  ZodKSUID: () => ZodKSUID,
  ZodLazy: () => ZodLazy,
  ZodLiteral: () => ZodLiteral,
  ZodMAC: () => ZodMAC,
  ZodMap: () => ZodMap,
  ZodNaN: () => ZodNaN,
  ZodNanoID: () => ZodNanoID,
  ZodNever: () => ZodNever,
  ZodNonOptional: () => ZodNonOptional,
  ZodNull: () => ZodNull,
  ZodNullable: () => ZodNullable,
  ZodNumber: () => ZodNumber,
  ZodNumberFormat: () => ZodNumberFormat,
  ZodObject: () => ZodObject,
  ZodOptional: () => ZodOptional,
  ZodPipe: () => ZodPipe,
  ZodPrefault: () => ZodPrefault,
  ZodPromise: () => ZodPromise,
  ZodReadonly: () => ZodReadonly,
  ZodRecord: () => ZodRecord,
  ZodSet: () => ZodSet,
  ZodString: () => ZodString,
  ZodStringFormat: () => ZodStringFormat,
  ZodSuccess: () => ZodSuccess,
  ZodSymbol: () => ZodSymbol,
  ZodTemplateLiteral: () => ZodTemplateLiteral,
  ZodTransform: () => ZodTransform,
  ZodTuple: () => ZodTuple,
  ZodType: () => ZodType,
  ZodULID: () => ZodULID,
  ZodURL: () => ZodURL,
  ZodUUID: () => ZodUUID,
  ZodUndefined: () => ZodUndefined,
  ZodUnion: () => ZodUnion,
  ZodUnknown: () => ZodUnknown,
  ZodVoid: () => ZodVoid,
  ZodXID: () => ZodXID,
  ZodXor: () => ZodXor,
  _ZodString: () => _ZodString,
  _default: () => _default,
  _function: () => _function,
  any: () => any,
  array: () => array,
  base64: () => base642,
  base64url: () => base64url2,
  bigint: () => bigint2,
  boolean: () => boolean2,
  catch: () => _catch,
  check: () => check,
  cidrv4: () => cidrv42,
  cidrv6: () => cidrv62,
  codec: () => codec,
  cuid: () => cuid3,
  cuid2: () => cuid22,
  custom: () => custom,
  date: () => date3,
  describe: () => describe2,
  discriminatedUnion: () => discriminatedUnion,
  e164: () => e1642,
  email: () => email2,
  emoji: () => emoji2,
  enum: () => _enum,
  exactOptional: () => exactOptional,
  file: () => file,
  float32: () => float32,
  float64: () => float64,
  function: () => _function,
  guid: () => guid2,
  hash: () => hash,
  hex: () => hex2,
  hostname: () => hostname2,
  httpUrl: () => httpUrl,
  instanceof: () => _instanceof,
  int: () => int,
  int32: () => int32,
  int64: () => int64,
  intersection: () => intersection,
  ipv4: () => ipv42,
  ipv6: () => ipv62,
  json: () => json,
  jwt: () => jwt,
  keyof: () => keyof,
  ksuid: () => ksuid2,
  lazy: () => lazy,
  literal: () => literal,
  looseObject: () => looseObject,
  looseRecord: () => looseRecord,
  mac: () => mac2,
  map: () => map,
  meta: () => meta2,
  nan: () => nan,
  nanoid: () => nanoid2,
  nativeEnum: () => nativeEnum,
  never: () => never,
  nonoptional: () => nonoptional,
  null: () => _null3,
  nullable: () => nullable,
  nullish: () => nullish2,
  number: () => number2,
  object: () => object,
  optional: () => optional,
  partialRecord: () => partialRecord,
  pipe: () => pipe,
  prefault: () => prefault,
  preprocess: () => preprocess,
  promise: () => promise,
  readonly: () => readonly,
  record: () => record,
  refine: () => refine,
  set: () => set,
  strictObject: () => strictObject,
  string: () => string2,
  stringFormat: () => stringFormat,
  stringbool: () => stringbool,
  success: () => success,
  superRefine: () => superRefine,
  symbol: () => symbol,
  templateLiteral: () => templateLiteral,
  transform: () => transform,
  tuple: () => tuple,
  uint32: () => uint32,
  uint64: () => uint64,
  ulid: () => ulid2,
  undefined: () => _undefined3,
  union: () => union,
  unknown: () => unknown,
  url: () => url,
  uuid: () => uuid2,
  uuidv4: () => uuidv4,
  uuidv6: () => uuidv6,
  uuidv7: () => uuidv7,
  void: () => _void2,
  xid: () => xid2,
  xor: () => xor
});

// ../../node_modules/zod/v4/classic/checks.js
var checks_exports2 = {};
__export(checks_exports2, {
  endsWith: () => _endsWith,
  gt: () => _gt,
  gte: () => _gte,
  includes: () => _includes,
  length: () => _length,
  lowercase: () => _lowercase,
  lt: () => _lt,
  lte: () => _lte,
  maxLength: () => _maxLength,
  maxSize: () => _maxSize,
  mime: () => _mime,
  minLength: () => _minLength,
  minSize: () => _minSize,
  multipleOf: () => _multipleOf,
  negative: () => _negative,
  nonnegative: () => _nonnegative,
  nonpositive: () => _nonpositive,
  normalize: () => _normalize,
  overwrite: () => _overwrite,
  positive: () => _positive,
  property: () => _property,
  regex: () => _regex,
  size: () => _size,
  slugify: () => _slugify,
  startsWith: () => _startsWith,
  toLowerCase: () => _toLowerCase,
  toUpperCase: () => _toUpperCase,
  trim: () => _trim,
  uppercase: () => _uppercase
});

// ../../node_modules/zod/v4/classic/iso.js
var iso_exports = {};
__export(iso_exports, {
  ZodISODate: () => ZodISODate,
  ZodISODateTime: () => ZodISODateTime,
  ZodISODuration: () => ZodISODuration,
  ZodISOTime: () => ZodISOTime,
  date: () => date2,
  datetime: () => datetime2,
  duration: () => duration2,
  time: () => time2
});
var ZodISODateTime = /* @__PURE__ */ $constructor("ZodISODateTime", (inst, def) => {
  $ZodISODateTime.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function datetime2(params) {
  return _isoDateTime(ZodISODateTime, params);
}
var ZodISODate = /* @__PURE__ */ $constructor("ZodISODate", (inst, def) => {
  $ZodISODate.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function date2(params) {
  return _isoDate(ZodISODate, params);
}
var ZodISOTime = /* @__PURE__ */ $constructor("ZodISOTime", (inst, def) => {
  $ZodISOTime.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function time2(params) {
  return _isoTime(ZodISOTime, params);
}
var ZodISODuration = /* @__PURE__ */ $constructor("ZodISODuration", (inst, def) => {
  $ZodISODuration.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function duration2(params) {
  return _isoDuration(ZodISODuration, params);
}

// ../../node_modules/zod/v4/classic/errors.js
var initializer2 = (inst, issues) => {
  $ZodError.init(inst, issues);
  inst.name = "ZodError";
  Object.defineProperties(inst, {
    format: {
      value: (mapper) => formatError(inst, mapper)
      // enumerable: false,
    },
    flatten: {
      value: (mapper) => flattenError(inst, mapper)
      // enumerable: false,
    },
    addIssue: {
      value: (issue2) => {
        inst.issues.push(issue2);
        inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
      }
      // enumerable: false,
    },
    addIssues: {
      value: (issues2) => {
        inst.issues.push(...issues2);
        inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
      }
      // enumerable: false,
    },
    isEmpty: {
      get() {
        return inst.issues.length === 0;
      }
      // enumerable: false,
    }
  });
};
var ZodError = $constructor("ZodError", initializer2);
var ZodRealError = $constructor("ZodError", initializer2, {
  Parent: Error
});

// ../../node_modules/zod/v4/classic/parse.js
var parse2 = /* @__PURE__ */ _parse(ZodRealError);
var parseAsync2 = /* @__PURE__ */ _parseAsync(ZodRealError);
var safeParse2 = /* @__PURE__ */ _safeParse(ZodRealError);
var safeParseAsync2 = /* @__PURE__ */ _safeParseAsync(ZodRealError);
var encode = /* @__PURE__ */ _encode(ZodRealError);
var decode = /* @__PURE__ */ _decode(ZodRealError);
var encodeAsync = /* @__PURE__ */ _encodeAsync(ZodRealError);
var decodeAsync = /* @__PURE__ */ _decodeAsync(ZodRealError);
var safeEncode = /* @__PURE__ */ _safeEncode(ZodRealError);
var safeDecode = /* @__PURE__ */ _safeDecode(ZodRealError);
var safeEncodeAsync = /* @__PURE__ */ _safeEncodeAsync(ZodRealError);
var safeDecodeAsync = /* @__PURE__ */ _safeDecodeAsync(ZodRealError);

// ../../node_modules/zod/v4/classic/schemas.js
var ZodType = /* @__PURE__ */ $constructor("ZodType", (inst, def) => {
  $ZodType.init(inst, def);
  Object.assign(inst["~standard"], {
    jsonSchema: {
      input: createStandardJSONSchemaMethod(inst, "input"),
      output: createStandardJSONSchemaMethod(inst, "output")
    }
  });
  inst.toJSONSchema = createToJSONSchemaMethod(inst, {});
  inst.def = def;
  inst.type = def.type;
  Object.defineProperty(inst, "_def", { value: def });
  inst.check = (...checks) => {
    return inst.clone(util_exports.mergeDefs(def, {
      checks: [
        ...def.checks ?? [],
        ...checks.map((ch) => typeof ch === "function" ? { _zod: { check: ch, def: { check: "custom" }, onattach: [] } } : ch)
      ]
    }), {
      parent: true
    });
  };
  inst.with = inst.check;
  inst.clone = (def2, params) => clone(inst, def2, params);
  inst.brand = () => inst;
  inst.register = ((reg, meta3) => {
    reg.add(inst, meta3);
    return inst;
  });
  inst.parse = (data, params) => parse2(inst, data, params, { callee: inst.parse });
  inst.safeParse = (data, params) => safeParse2(inst, data, params);
  inst.parseAsync = async (data, params) => parseAsync2(inst, data, params, { callee: inst.parseAsync });
  inst.safeParseAsync = async (data, params) => safeParseAsync2(inst, data, params);
  inst.spa = inst.safeParseAsync;
  inst.encode = (data, params) => encode(inst, data, params);
  inst.decode = (data, params) => decode(inst, data, params);
  inst.encodeAsync = async (data, params) => encodeAsync(inst, data, params);
  inst.decodeAsync = async (data, params) => decodeAsync(inst, data, params);
  inst.safeEncode = (data, params) => safeEncode(inst, data, params);
  inst.safeDecode = (data, params) => safeDecode(inst, data, params);
  inst.safeEncodeAsync = async (data, params) => safeEncodeAsync(inst, data, params);
  inst.safeDecodeAsync = async (data, params) => safeDecodeAsync(inst, data, params);
  inst.refine = (check2, params) => inst.check(refine(check2, params));
  inst.superRefine = (refinement) => inst.check(superRefine(refinement));
  inst.overwrite = (fn) => inst.check(_overwrite(fn));
  inst.optional = () => optional(inst);
  inst.exactOptional = () => exactOptional(inst);
  inst.nullable = () => nullable(inst);
  inst.nullish = () => optional(nullable(inst));
  inst.nonoptional = (params) => nonoptional(inst, params);
  inst.array = () => array(inst);
  inst.or = (arg) => union([inst, arg]);
  inst.and = (arg) => intersection(inst, arg);
  inst.transform = (tx) => pipe(inst, transform(tx));
  inst.default = (def2) => _default(inst, def2);
  inst.prefault = (def2) => prefault(inst, def2);
  inst.catch = (params) => _catch(inst, params);
  inst.pipe = (target) => pipe(inst, target);
  inst.readonly = () => readonly(inst);
  inst.describe = (description) => {
    const cl = inst.clone();
    globalRegistry.add(cl, { description });
    return cl;
  };
  Object.defineProperty(inst, "description", {
    get() {
      return globalRegistry.get(inst)?.description;
    },
    configurable: true
  });
  inst.meta = (...args) => {
    if (args.length === 0) {
      return globalRegistry.get(inst);
    }
    const cl = inst.clone();
    globalRegistry.add(cl, args[0]);
    return cl;
  };
  inst.isOptional = () => inst.safeParse(void 0).success;
  inst.isNullable = () => inst.safeParse(null).success;
  inst.apply = (fn) => fn(inst);
  return inst;
});
var _ZodString = /* @__PURE__ */ $constructor("_ZodString", (inst, def) => {
  $ZodString.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => stringProcessor(inst, ctx, json2, params);
  const bag = inst._zod.bag;
  inst.format = bag.format ?? null;
  inst.minLength = bag.minimum ?? null;
  inst.maxLength = bag.maximum ?? null;
  inst.regex = (...args) => inst.check(_regex(...args));
  inst.includes = (...args) => inst.check(_includes(...args));
  inst.startsWith = (...args) => inst.check(_startsWith(...args));
  inst.endsWith = (...args) => inst.check(_endsWith(...args));
  inst.min = (...args) => inst.check(_minLength(...args));
  inst.max = (...args) => inst.check(_maxLength(...args));
  inst.length = (...args) => inst.check(_length(...args));
  inst.nonempty = (...args) => inst.check(_minLength(1, ...args));
  inst.lowercase = (params) => inst.check(_lowercase(params));
  inst.uppercase = (params) => inst.check(_uppercase(params));
  inst.trim = () => inst.check(_trim());
  inst.normalize = (...args) => inst.check(_normalize(...args));
  inst.toLowerCase = () => inst.check(_toLowerCase());
  inst.toUpperCase = () => inst.check(_toUpperCase());
  inst.slugify = () => inst.check(_slugify());
});
var ZodString = /* @__PURE__ */ $constructor("ZodString", (inst, def) => {
  $ZodString.init(inst, def);
  _ZodString.init(inst, def);
  inst.email = (params) => inst.check(_email(ZodEmail, params));
  inst.url = (params) => inst.check(_url(ZodURL, params));
  inst.jwt = (params) => inst.check(_jwt(ZodJWT, params));
  inst.emoji = (params) => inst.check(_emoji2(ZodEmoji, params));
  inst.guid = (params) => inst.check(_guid(ZodGUID, params));
  inst.uuid = (params) => inst.check(_uuid(ZodUUID, params));
  inst.uuidv4 = (params) => inst.check(_uuidv4(ZodUUID, params));
  inst.uuidv6 = (params) => inst.check(_uuidv6(ZodUUID, params));
  inst.uuidv7 = (params) => inst.check(_uuidv7(ZodUUID, params));
  inst.nanoid = (params) => inst.check(_nanoid(ZodNanoID, params));
  inst.guid = (params) => inst.check(_guid(ZodGUID, params));
  inst.cuid = (params) => inst.check(_cuid(ZodCUID, params));
  inst.cuid2 = (params) => inst.check(_cuid2(ZodCUID2, params));
  inst.ulid = (params) => inst.check(_ulid(ZodULID, params));
  inst.base64 = (params) => inst.check(_base64(ZodBase64, params));
  inst.base64url = (params) => inst.check(_base64url(ZodBase64URL, params));
  inst.xid = (params) => inst.check(_xid(ZodXID, params));
  inst.ksuid = (params) => inst.check(_ksuid(ZodKSUID, params));
  inst.ipv4 = (params) => inst.check(_ipv4(ZodIPv4, params));
  inst.ipv6 = (params) => inst.check(_ipv6(ZodIPv6, params));
  inst.cidrv4 = (params) => inst.check(_cidrv4(ZodCIDRv4, params));
  inst.cidrv6 = (params) => inst.check(_cidrv6(ZodCIDRv6, params));
  inst.e164 = (params) => inst.check(_e164(ZodE164, params));
  inst.datetime = (params) => inst.check(datetime2(params));
  inst.date = (params) => inst.check(date2(params));
  inst.time = (params) => inst.check(time2(params));
  inst.duration = (params) => inst.check(duration2(params));
});
function string2(params) {
  return _string(ZodString, params);
}
var ZodStringFormat = /* @__PURE__ */ $constructor("ZodStringFormat", (inst, def) => {
  $ZodStringFormat.init(inst, def);
  _ZodString.init(inst, def);
});
var ZodEmail = /* @__PURE__ */ $constructor("ZodEmail", (inst, def) => {
  $ZodEmail.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function email2(params) {
  return _email(ZodEmail, params);
}
var ZodGUID = /* @__PURE__ */ $constructor("ZodGUID", (inst, def) => {
  $ZodGUID.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function guid2(params) {
  return _guid(ZodGUID, params);
}
var ZodUUID = /* @__PURE__ */ $constructor("ZodUUID", (inst, def) => {
  $ZodUUID.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function uuid2(params) {
  return _uuid(ZodUUID, params);
}
function uuidv4(params) {
  return _uuidv4(ZodUUID, params);
}
function uuidv6(params) {
  return _uuidv6(ZodUUID, params);
}
function uuidv7(params) {
  return _uuidv7(ZodUUID, params);
}
var ZodURL = /* @__PURE__ */ $constructor("ZodURL", (inst, def) => {
  $ZodURL.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function url(params) {
  return _url(ZodURL, params);
}
function httpUrl(params) {
  return _url(ZodURL, {
    protocol: /^https?$/,
    hostname: regexes_exports.domain,
    ...util_exports.normalizeParams(params)
  });
}
var ZodEmoji = /* @__PURE__ */ $constructor("ZodEmoji", (inst, def) => {
  $ZodEmoji.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function emoji2(params) {
  return _emoji2(ZodEmoji, params);
}
var ZodNanoID = /* @__PURE__ */ $constructor("ZodNanoID", (inst, def) => {
  $ZodNanoID.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function nanoid2(params) {
  return _nanoid(ZodNanoID, params);
}
var ZodCUID = /* @__PURE__ */ $constructor("ZodCUID", (inst, def) => {
  $ZodCUID.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function cuid3(params) {
  return _cuid(ZodCUID, params);
}
var ZodCUID2 = /* @__PURE__ */ $constructor("ZodCUID2", (inst, def) => {
  $ZodCUID2.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function cuid22(params) {
  return _cuid2(ZodCUID2, params);
}
var ZodULID = /* @__PURE__ */ $constructor("ZodULID", (inst, def) => {
  $ZodULID.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function ulid2(params) {
  return _ulid(ZodULID, params);
}
var ZodXID = /* @__PURE__ */ $constructor("ZodXID", (inst, def) => {
  $ZodXID.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function xid2(params) {
  return _xid(ZodXID, params);
}
var ZodKSUID = /* @__PURE__ */ $constructor("ZodKSUID", (inst, def) => {
  $ZodKSUID.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function ksuid2(params) {
  return _ksuid(ZodKSUID, params);
}
var ZodIPv4 = /* @__PURE__ */ $constructor("ZodIPv4", (inst, def) => {
  $ZodIPv4.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function ipv42(params) {
  return _ipv4(ZodIPv4, params);
}
var ZodMAC = /* @__PURE__ */ $constructor("ZodMAC", (inst, def) => {
  $ZodMAC.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function mac2(params) {
  return _mac(ZodMAC, params);
}
var ZodIPv6 = /* @__PURE__ */ $constructor("ZodIPv6", (inst, def) => {
  $ZodIPv6.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function ipv62(params) {
  return _ipv6(ZodIPv6, params);
}
var ZodCIDRv4 = /* @__PURE__ */ $constructor("ZodCIDRv4", (inst, def) => {
  $ZodCIDRv4.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function cidrv42(params) {
  return _cidrv4(ZodCIDRv4, params);
}
var ZodCIDRv6 = /* @__PURE__ */ $constructor("ZodCIDRv6", (inst, def) => {
  $ZodCIDRv6.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function cidrv62(params) {
  return _cidrv6(ZodCIDRv6, params);
}
var ZodBase64 = /* @__PURE__ */ $constructor("ZodBase64", (inst, def) => {
  $ZodBase64.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function base642(params) {
  return _base64(ZodBase64, params);
}
var ZodBase64URL = /* @__PURE__ */ $constructor("ZodBase64URL", (inst, def) => {
  $ZodBase64URL.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function base64url2(params) {
  return _base64url(ZodBase64URL, params);
}
var ZodE164 = /* @__PURE__ */ $constructor("ZodE164", (inst, def) => {
  $ZodE164.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function e1642(params) {
  return _e164(ZodE164, params);
}
var ZodJWT = /* @__PURE__ */ $constructor("ZodJWT", (inst, def) => {
  $ZodJWT.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function jwt(params) {
  return _jwt(ZodJWT, params);
}
var ZodCustomStringFormat = /* @__PURE__ */ $constructor("ZodCustomStringFormat", (inst, def) => {
  $ZodCustomStringFormat.init(inst, def);
  ZodStringFormat.init(inst, def);
});
function stringFormat(format2, fnOrRegex, _params = {}) {
  return _stringFormat(ZodCustomStringFormat, format2, fnOrRegex, _params);
}
function hostname2(_params) {
  return _stringFormat(ZodCustomStringFormat, "hostname", regexes_exports.hostname, _params);
}
function hex2(_params) {
  return _stringFormat(ZodCustomStringFormat, "hex", regexes_exports.hex, _params);
}
function hash(alg, params) {
  const enc = params?.enc ?? "hex";
  const format2 = `${alg}_${enc}`;
  const regex2 = regexes_exports[format2];
  if (!regex2)
    throw new Error(`Unrecognized hash format: ${format2}`);
  return _stringFormat(ZodCustomStringFormat, format2, regex2, params);
}
var ZodNumber = /* @__PURE__ */ $constructor("ZodNumber", (inst, def) => {
  $ZodNumber.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => numberProcessor(inst, ctx, json2, params);
  inst.gt = (value, params) => inst.check(_gt(value, params));
  inst.gte = (value, params) => inst.check(_gte(value, params));
  inst.min = (value, params) => inst.check(_gte(value, params));
  inst.lt = (value, params) => inst.check(_lt(value, params));
  inst.lte = (value, params) => inst.check(_lte(value, params));
  inst.max = (value, params) => inst.check(_lte(value, params));
  inst.int = (params) => inst.check(int(params));
  inst.safe = (params) => inst.check(int(params));
  inst.positive = (params) => inst.check(_gt(0, params));
  inst.nonnegative = (params) => inst.check(_gte(0, params));
  inst.negative = (params) => inst.check(_lt(0, params));
  inst.nonpositive = (params) => inst.check(_lte(0, params));
  inst.multipleOf = (value, params) => inst.check(_multipleOf(value, params));
  inst.step = (value, params) => inst.check(_multipleOf(value, params));
  inst.finite = () => inst;
  const bag = inst._zod.bag;
  inst.minValue = Math.max(bag.minimum ?? Number.NEGATIVE_INFINITY, bag.exclusiveMinimum ?? Number.NEGATIVE_INFINITY) ?? null;
  inst.maxValue = Math.min(bag.maximum ?? Number.POSITIVE_INFINITY, bag.exclusiveMaximum ?? Number.POSITIVE_INFINITY) ?? null;
  inst.isInt = (bag.format ?? "").includes("int") || Number.isSafeInteger(bag.multipleOf ?? 0.5);
  inst.isFinite = true;
  inst.format = bag.format ?? null;
});
function number2(params) {
  return _number(ZodNumber, params);
}
var ZodNumberFormat = /* @__PURE__ */ $constructor("ZodNumberFormat", (inst, def) => {
  $ZodNumberFormat.init(inst, def);
  ZodNumber.init(inst, def);
});
function int(params) {
  return _int(ZodNumberFormat, params);
}
function float32(params) {
  return _float32(ZodNumberFormat, params);
}
function float64(params) {
  return _float64(ZodNumberFormat, params);
}
function int32(params) {
  return _int32(ZodNumberFormat, params);
}
function uint32(params) {
  return _uint32(ZodNumberFormat, params);
}
var ZodBoolean = /* @__PURE__ */ $constructor("ZodBoolean", (inst, def) => {
  $ZodBoolean.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => booleanProcessor(inst, ctx, json2, params);
});
function boolean2(params) {
  return _boolean(ZodBoolean, params);
}
var ZodBigInt = /* @__PURE__ */ $constructor("ZodBigInt", (inst, def) => {
  $ZodBigInt.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => bigintProcessor(inst, ctx, json2, params);
  inst.gte = (value, params) => inst.check(_gte(value, params));
  inst.min = (value, params) => inst.check(_gte(value, params));
  inst.gt = (value, params) => inst.check(_gt(value, params));
  inst.gte = (value, params) => inst.check(_gte(value, params));
  inst.min = (value, params) => inst.check(_gte(value, params));
  inst.lt = (value, params) => inst.check(_lt(value, params));
  inst.lte = (value, params) => inst.check(_lte(value, params));
  inst.max = (value, params) => inst.check(_lte(value, params));
  inst.positive = (params) => inst.check(_gt(BigInt(0), params));
  inst.negative = (params) => inst.check(_lt(BigInt(0), params));
  inst.nonpositive = (params) => inst.check(_lte(BigInt(0), params));
  inst.nonnegative = (params) => inst.check(_gte(BigInt(0), params));
  inst.multipleOf = (value, params) => inst.check(_multipleOf(value, params));
  const bag = inst._zod.bag;
  inst.minValue = bag.minimum ?? null;
  inst.maxValue = bag.maximum ?? null;
  inst.format = bag.format ?? null;
});
function bigint2(params) {
  return _bigint(ZodBigInt, params);
}
var ZodBigIntFormat = /* @__PURE__ */ $constructor("ZodBigIntFormat", (inst, def) => {
  $ZodBigIntFormat.init(inst, def);
  ZodBigInt.init(inst, def);
});
function int64(params) {
  return _int64(ZodBigIntFormat, params);
}
function uint64(params) {
  return _uint64(ZodBigIntFormat, params);
}
var ZodSymbol = /* @__PURE__ */ $constructor("ZodSymbol", (inst, def) => {
  $ZodSymbol.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => symbolProcessor(inst, ctx, json2, params);
});
function symbol(params) {
  return _symbol(ZodSymbol, params);
}
var ZodUndefined = /* @__PURE__ */ $constructor("ZodUndefined", (inst, def) => {
  $ZodUndefined.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => undefinedProcessor(inst, ctx, json2, params);
});
function _undefined3(params) {
  return _undefined2(ZodUndefined, params);
}
var ZodNull = /* @__PURE__ */ $constructor("ZodNull", (inst, def) => {
  $ZodNull.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => nullProcessor(inst, ctx, json2, params);
});
function _null3(params) {
  return _null2(ZodNull, params);
}
var ZodAny = /* @__PURE__ */ $constructor("ZodAny", (inst, def) => {
  $ZodAny.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => anyProcessor(inst, ctx, json2, params);
});
function any() {
  return _any(ZodAny);
}
var ZodUnknown = /* @__PURE__ */ $constructor("ZodUnknown", (inst, def) => {
  $ZodUnknown.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => unknownProcessor(inst, ctx, json2, params);
});
function unknown() {
  return _unknown(ZodUnknown);
}
var ZodNever = /* @__PURE__ */ $constructor("ZodNever", (inst, def) => {
  $ZodNever.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => neverProcessor(inst, ctx, json2, params);
});
function never(params) {
  return _never(ZodNever, params);
}
var ZodVoid = /* @__PURE__ */ $constructor("ZodVoid", (inst, def) => {
  $ZodVoid.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => voidProcessor(inst, ctx, json2, params);
});
function _void2(params) {
  return _void(ZodVoid, params);
}
var ZodDate = /* @__PURE__ */ $constructor("ZodDate", (inst, def) => {
  $ZodDate.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => dateProcessor(inst, ctx, json2, params);
  inst.min = (value, params) => inst.check(_gte(value, params));
  inst.max = (value, params) => inst.check(_lte(value, params));
  const c = inst._zod.bag;
  inst.minDate = c.minimum ? new Date(c.minimum) : null;
  inst.maxDate = c.maximum ? new Date(c.maximum) : null;
});
function date3(params) {
  return _date(ZodDate, params);
}
var ZodArray = /* @__PURE__ */ $constructor("ZodArray", (inst, def) => {
  $ZodArray.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => arrayProcessor(inst, ctx, json2, params);
  inst.element = def.element;
  inst.min = (minLength, params) => inst.check(_minLength(minLength, params));
  inst.nonempty = (params) => inst.check(_minLength(1, params));
  inst.max = (maxLength, params) => inst.check(_maxLength(maxLength, params));
  inst.length = (len, params) => inst.check(_length(len, params));
  inst.unwrap = () => inst.element;
});
function array(element, params) {
  return _array(ZodArray, element, params);
}
function keyof(schema) {
  const shape = schema._zod.def.shape;
  return _enum(Object.keys(shape));
}
var ZodObject = /* @__PURE__ */ $constructor("ZodObject", (inst, def) => {
  $ZodObjectJIT.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => objectProcessor(inst, ctx, json2, params);
  util_exports.defineLazy(inst, "shape", () => {
    return def.shape;
  });
  inst.keyof = () => _enum(Object.keys(inst._zod.def.shape));
  inst.catchall = (catchall) => inst.clone({ ...inst._zod.def, catchall });
  inst.passthrough = () => inst.clone({ ...inst._zod.def, catchall: unknown() });
  inst.loose = () => inst.clone({ ...inst._zod.def, catchall: unknown() });
  inst.strict = () => inst.clone({ ...inst._zod.def, catchall: never() });
  inst.strip = () => inst.clone({ ...inst._zod.def, catchall: void 0 });
  inst.extend = (incoming) => {
    return util_exports.extend(inst, incoming);
  };
  inst.safeExtend = (incoming) => {
    return util_exports.safeExtend(inst, incoming);
  };
  inst.merge = (other) => util_exports.merge(inst, other);
  inst.pick = (mask) => util_exports.pick(inst, mask);
  inst.omit = (mask) => util_exports.omit(inst, mask);
  inst.partial = (...args) => util_exports.partial(ZodOptional, inst, args[0]);
  inst.required = (...args) => util_exports.required(ZodNonOptional, inst, args[0]);
});
function object(shape, params) {
  const def = {
    type: "object",
    shape: shape ?? {},
    ...util_exports.normalizeParams(params)
  };
  return new ZodObject(def);
}
function strictObject(shape, params) {
  return new ZodObject({
    type: "object",
    shape,
    catchall: never(),
    ...util_exports.normalizeParams(params)
  });
}
function looseObject(shape, params) {
  return new ZodObject({
    type: "object",
    shape,
    catchall: unknown(),
    ...util_exports.normalizeParams(params)
  });
}
var ZodUnion = /* @__PURE__ */ $constructor("ZodUnion", (inst, def) => {
  $ZodUnion.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => unionProcessor(inst, ctx, json2, params);
  inst.options = def.options;
});
function union(options, params) {
  return new ZodUnion({
    type: "union",
    options,
    ...util_exports.normalizeParams(params)
  });
}
var ZodXor = /* @__PURE__ */ $constructor("ZodXor", (inst, def) => {
  ZodUnion.init(inst, def);
  $ZodXor.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => unionProcessor(inst, ctx, json2, params);
  inst.options = def.options;
});
function xor(options, params) {
  return new ZodXor({
    type: "union",
    options,
    inclusive: false,
    ...util_exports.normalizeParams(params)
  });
}
var ZodDiscriminatedUnion = /* @__PURE__ */ $constructor("ZodDiscriminatedUnion", (inst, def) => {
  ZodUnion.init(inst, def);
  $ZodDiscriminatedUnion.init(inst, def);
});
function discriminatedUnion(discriminator, options, params) {
  return new ZodDiscriminatedUnion({
    type: "union",
    options,
    discriminator,
    ...util_exports.normalizeParams(params)
  });
}
var ZodIntersection = /* @__PURE__ */ $constructor("ZodIntersection", (inst, def) => {
  $ZodIntersection.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => intersectionProcessor(inst, ctx, json2, params);
});
function intersection(left, right) {
  return new ZodIntersection({
    type: "intersection",
    left,
    right
  });
}
var ZodTuple = /* @__PURE__ */ $constructor("ZodTuple", (inst, def) => {
  $ZodTuple.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => tupleProcessor(inst, ctx, json2, params);
  inst.rest = (rest) => inst.clone({
    ...inst._zod.def,
    rest
  });
});
function tuple(items, _paramsOrRest, _params) {
  const hasRest = _paramsOrRest instanceof $ZodType;
  const params = hasRest ? _params : _paramsOrRest;
  const rest = hasRest ? _paramsOrRest : null;
  return new ZodTuple({
    type: "tuple",
    items,
    rest,
    ...util_exports.normalizeParams(params)
  });
}
var ZodRecord = /* @__PURE__ */ $constructor("ZodRecord", (inst, def) => {
  $ZodRecord.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => recordProcessor(inst, ctx, json2, params);
  inst.keyType = def.keyType;
  inst.valueType = def.valueType;
});
function record(keyType, valueType, params) {
  return new ZodRecord({
    type: "record",
    keyType,
    valueType,
    ...util_exports.normalizeParams(params)
  });
}
function partialRecord(keyType, valueType, params) {
  const k = clone(keyType);
  k._zod.values = void 0;
  return new ZodRecord({
    type: "record",
    keyType: k,
    valueType,
    ...util_exports.normalizeParams(params)
  });
}
function looseRecord(keyType, valueType, params) {
  return new ZodRecord({
    type: "record",
    keyType,
    valueType,
    mode: "loose",
    ...util_exports.normalizeParams(params)
  });
}
var ZodMap = /* @__PURE__ */ $constructor("ZodMap", (inst, def) => {
  $ZodMap.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => mapProcessor(inst, ctx, json2, params);
  inst.keyType = def.keyType;
  inst.valueType = def.valueType;
  inst.min = (...args) => inst.check(_minSize(...args));
  inst.nonempty = (params) => inst.check(_minSize(1, params));
  inst.max = (...args) => inst.check(_maxSize(...args));
  inst.size = (...args) => inst.check(_size(...args));
});
function map(keyType, valueType, params) {
  return new ZodMap({
    type: "map",
    keyType,
    valueType,
    ...util_exports.normalizeParams(params)
  });
}
var ZodSet = /* @__PURE__ */ $constructor("ZodSet", (inst, def) => {
  $ZodSet.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => setProcessor(inst, ctx, json2, params);
  inst.min = (...args) => inst.check(_minSize(...args));
  inst.nonempty = (params) => inst.check(_minSize(1, params));
  inst.max = (...args) => inst.check(_maxSize(...args));
  inst.size = (...args) => inst.check(_size(...args));
});
function set(valueType, params) {
  return new ZodSet({
    type: "set",
    valueType,
    ...util_exports.normalizeParams(params)
  });
}
var ZodEnum = /* @__PURE__ */ $constructor("ZodEnum", (inst, def) => {
  $ZodEnum.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => enumProcessor(inst, ctx, json2, params);
  inst.enum = def.entries;
  inst.options = Object.values(def.entries);
  const keys = new Set(Object.keys(def.entries));
  inst.extract = (values, params) => {
    const newEntries = {};
    for (const value of values) {
      if (keys.has(value)) {
        newEntries[value] = def.entries[value];
      } else
        throw new Error(`Key ${value} not found in enum`);
    }
    return new ZodEnum({
      ...def,
      checks: [],
      ...util_exports.normalizeParams(params),
      entries: newEntries
    });
  };
  inst.exclude = (values, params) => {
    const newEntries = { ...def.entries };
    for (const value of values) {
      if (keys.has(value)) {
        delete newEntries[value];
      } else
        throw new Error(`Key ${value} not found in enum`);
    }
    return new ZodEnum({
      ...def,
      checks: [],
      ...util_exports.normalizeParams(params),
      entries: newEntries
    });
  };
});
function _enum(values, params) {
  const entries = Array.isArray(values) ? Object.fromEntries(values.map((v) => [v, v])) : values;
  return new ZodEnum({
    type: "enum",
    entries,
    ...util_exports.normalizeParams(params)
  });
}
function nativeEnum(entries, params) {
  return new ZodEnum({
    type: "enum",
    entries,
    ...util_exports.normalizeParams(params)
  });
}
var ZodLiteral = /* @__PURE__ */ $constructor("ZodLiteral", (inst, def) => {
  $ZodLiteral.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => literalProcessor(inst, ctx, json2, params);
  inst.values = new Set(def.values);
  Object.defineProperty(inst, "value", {
    get() {
      if (def.values.length > 1) {
        throw new Error("This schema contains multiple valid literal values. Use `.values` instead.");
      }
      return def.values[0];
    }
  });
});
function literal(value, params) {
  return new ZodLiteral({
    type: "literal",
    values: Array.isArray(value) ? value : [value],
    ...util_exports.normalizeParams(params)
  });
}
var ZodFile = /* @__PURE__ */ $constructor("ZodFile", (inst, def) => {
  $ZodFile.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => fileProcessor(inst, ctx, json2, params);
  inst.min = (size, params) => inst.check(_minSize(size, params));
  inst.max = (size, params) => inst.check(_maxSize(size, params));
  inst.mime = (types, params) => inst.check(_mime(Array.isArray(types) ? types : [types], params));
});
function file(params) {
  return _file(ZodFile, params);
}
var ZodTransform = /* @__PURE__ */ $constructor("ZodTransform", (inst, def) => {
  $ZodTransform.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => transformProcessor(inst, ctx, json2, params);
  inst._zod.parse = (payload, _ctx) => {
    if (_ctx.direction === "backward") {
      throw new $ZodEncodeError(inst.constructor.name);
    }
    payload.addIssue = (issue2) => {
      if (typeof issue2 === "string") {
        payload.issues.push(util_exports.issue(issue2, payload.value, def));
      } else {
        const _issue = issue2;
        if (_issue.fatal)
          _issue.continue = false;
        _issue.code ?? (_issue.code = "custom");
        _issue.input ?? (_issue.input = payload.value);
        _issue.inst ?? (_issue.inst = inst);
        payload.issues.push(util_exports.issue(_issue));
      }
    };
    const output = def.transform(payload.value, payload);
    if (output instanceof Promise) {
      return output.then((output2) => {
        payload.value = output2;
        return payload;
      });
    }
    payload.value = output;
    return payload;
  };
});
function transform(fn) {
  return new ZodTransform({
    type: "transform",
    transform: fn
  });
}
var ZodOptional = /* @__PURE__ */ $constructor("ZodOptional", (inst, def) => {
  $ZodOptional.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => optionalProcessor(inst, ctx, json2, params);
  inst.unwrap = () => inst._zod.def.innerType;
});
function optional(innerType) {
  return new ZodOptional({
    type: "optional",
    innerType
  });
}
var ZodExactOptional = /* @__PURE__ */ $constructor("ZodExactOptional", (inst, def) => {
  $ZodExactOptional.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => optionalProcessor(inst, ctx, json2, params);
  inst.unwrap = () => inst._zod.def.innerType;
});
function exactOptional(innerType) {
  return new ZodExactOptional({
    type: "optional",
    innerType
  });
}
var ZodNullable = /* @__PURE__ */ $constructor("ZodNullable", (inst, def) => {
  $ZodNullable.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => nullableProcessor(inst, ctx, json2, params);
  inst.unwrap = () => inst._zod.def.innerType;
});
function nullable(innerType) {
  return new ZodNullable({
    type: "nullable",
    innerType
  });
}
function nullish2(innerType) {
  return optional(nullable(innerType));
}
var ZodDefault = /* @__PURE__ */ $constructor("ZodDefault", (inst, def) => {
  $ZodDefault.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => defaultProcessor(inst, ctx, json2, params);
  inst.unwrap = () => inst._zod.def.innerType;
  inst.removeDefault = inst.unwrap;
});
function _default(innerType, defaultValue) {
  return new ZodDefault({
    type: "default",
    innerType,
    get defaultValue() {
      return typeof defaultValue === "function" ? defaultValue() : util_exports.shallowClone(defaultValue);
    }
  });
}
var ZodPrefault = /* @__PURE__ */ $constructor("ZodPrefault", (inst, def) => {
  $ZodPrefault.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => prefaultProcessor(inst, ctx, json2, params);
  inst.unwrap = () => inst._zod.def.innerType;
});
function prefault(innerType, defaultValue) {
  return new ZodPrefault({
    type: "prefault",
    innerType,
    get defaultValue() {
      return typeof defaultValue === "function" ? defaultValue() : util_exports.shallowClone(defaultValue);
    }
  });
}
var ZodNonOptional = /* @__PURE__ */ $constructor("ZodNonOptional", (inst, def) => {
  $ZodNonOptional.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => nonoptionalProcessor(inst, ctx, json2, params);
  inst.unwrap = () => inst._zod.def.innerType;
});
function nonoptional(innerType, params) {
  return new ZodNonOptional({
    type: "nonoptional",
    innerType,
    ...util_exports.normalizeParams(params)
  });
}
var ZodSuccess = /* @__PURE__ */ $constructor("ZodSuccess", (inst, def) => {
  $ZodSuccess.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => successProcessor(inst, ctx, json2, params);
  inst.unwrap = () => inst._zod.def.innerType;
});
function success(innerType) {
  return new ZodSuccess({
    type: "success",
    innerType
  });
}
var ZodCatch = /* @__PURE__ */ $constructor("ZodCatch", (inst, def) => {
  $ZodCatch.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => catchProcessor(inst, ctx, json2, params);
  inst.unwrap = () => inst._zod.def.innerType;
  inst.removeCatch = inst.unwrap;
});
function _catch(innerType, catchValue) {
  return new ZodCatch({
    type: "catch",
    innerType,
    catchValue: typeof catchValue === "function" ? catchValue : () => catchValue
  });
}
var ZodNaN = /* @__PURE__ */ $constructor("ZodNaN", (inst, def) => {
  $ZodNaN.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => nanProcessor(inst, ctx, json2, params);
});
function nan(params) {
  return _nan(ZodNaN, params);
}
var ZodPipe = /* @__PURE__ */ $constructor("ZodPipe", (inst, def) => {
  $ZodPipe.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => pipeProcessor(inst, ctx, json2, params);
  inst.in = def.in;
  inst.out = def.out;
});
function pipe(in_, out) {
  return new ZodPipe({
    type: "pipe",
    in: in_,
    out
    // ...util.normalizeParams(params),
  });
}
var ZodCodec = /* @__PURE__ */ $constructor("ZodCodec", (inst, def) => {
  ZodPipe.init(inst, def);
  $ZodCodec.init(inst, def);
});
function codec(in_, out, params) {
  return new ZodCodec({
    type: "pipe",
    in: in_,
    out,
    transform: params.decode,
    reverseTransform: params.encode
  });
}
var ZodReadonly = /* @__PURE__ */ $constructor("ZodReadonly", (inst, def) => {
  $ZodReadonly.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => readonlyProcessor(inst, ctx, json2, params);
  inst.unwrap = () => inst._zod.def.innerType;
});
function readonly(innerType) {
  return new ZodReadonly({
    type: "readonly",
    innerType
  });
}
var ZodTemplateLiteral = /* @__PURE__ */ $constructor("ZodTemplateLiteral", (inst, def) => {
  $ZodTemplateLiteral.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => templateLiteralProcessor(inst, ctx, json2, params);
});
function templateLiteral(parts, params) {
  return new ZodTemplateLiteral({
    type: "template_literal",
    parts,
    ...util_exports.normalizeParams(params)
  });
}
var ZodLazy = /* @__PURE__ */ $constructor("ZodLazy", (inst, def) => {
  $ZodLazy.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => lazyProcessor(inst, ctx, json2, params);
  inst.unwrap = () => inst._zod.def.getter();
});
function lazy(getter) {
  return new ZodLazy({
    type: "lazy",
    getter
  });
}
var ZodPromise = /* @__PURE__ */ $constructor("ZodPromise", (inst, def) => {
  $ZodPromise.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => promiseProcessor(inst, ctx, json2, params);
  inst.unwrap = () => inst._zod.def.innerType;
});
function promise(innerType) {
  return new ZodPromise({
    type: "promise",
    innerType
  });
}
var ZodFunction = /* @__PURE__ */ $constructor("ZodFunction", (inst, def) => {
  $ZodFunction.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => functionProcessor(inst, ctx, json2, params);
});
function _function(params) {
  return new ZodFunction({
    type: "function",
    input: Array.isArray(params?.input) ? tuple(params?.input) : params?.input ?? array(unknown()),
    output: params?.output ?? unknown()
  });
}
var ZodCustom = /* @__PURE__ */ $constructor("ZodCustom", (inst, def) => {
  $ZodCustom.init(inst, def);
  ZodType.init(inst, def);
  inst._zod.processJSONSchema = (ctx, json2, params) => customProcessor(inst, ctx, json2, params);
});
function check(fn) {
  const ch = new $ZodCheck({
    check: "custom"
    // ...util.normalizeParams(params),
  });
  ch._zod.check = fn;
  return ch;
}
function custom(fn, _params) {
  return _custom(ZodCustom, fn ?? (() => true), _params);
}
function refine(fn, _params = {}) {
  return _refine(ZodCustom, fn, _params);
}
function superRefine(fn) {
  return _superRefine(fn);
}
var describe2 = describe;
var meta2 = meta;
function _instanceof(cls, params = {}) {
  const inst = new ZodCustom({
    type: "custom",
    check: "custom",
    fn: (data) => data instanceof cls,
    abort: true,
    ...util_exports.normalizeParams(params)
  });
  inst._zod.bag.Class = cls;
  inst._zod.check = (payload) => {
    if (!(payload.value instanceof cls)) {
      payload.issues.push({
        code: "invalid_type",
        expected: cls.name,
        input: payload.value,
        inst,
        path: [...inst._zod.def.path ?? []]
      });
    }
  };
  return inst;
}
var stringbool = (...args) => _stringbool({
  Codec: ZodCodec,
  Boolean: ZodBoolean,
  String: ZodString
}, ...args);
function json(params) {
  const jsonSchema = lazy(() => {
    return union([string2(params), number2(), boolean2(), _null3(), array(jsonSchema), record(string2(), jsonSchema)]);
  });
  return jsonSchema;
}
function preprocess(fn, schema) {
  return pipe(transform(fn), schema);
}

// ../../node_modules/zod/v4/classic/compat.js
var ZodIssueCode = {
  invalid_type: "invalid_type",
  too_big: "too_big",
  too_small: "too_small",
  invalid_format: "invalid_format",
  not_multiple_of: "not_multiple_of",
  unrecognized_keys: "unrecognized_keys",
  invalid_union: "invalid_union",
  invalid_key: "invalid_key",
  invalid_element: "invalid_element",
  invalid_value: "invalid_value",
  custom: "custom"
};
var ZodFirstPartyTypeKind;
/* @__PURE__ */ (function(ZodFirstPartyTypeKind2) {
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));

// ../../node_modules/zod/v4/classic/from-json-schema.js
var z = {
  ...schemas_exports2,
  ...checks_exports2,
  iso: iso_exports
};

// ../../node_modules/zod/v4/classic/coerce.js
var coerce_exports = {};
__export(coerce_exports, {
  bigint: () => bigint3,
  boolean: () => boolean3,
  date: () => date4,
  number: () => number3,
  string: () => string3
});
function string3(params) {
  return _coercedString(ZodString, params);
}
function number3(params) {
  return _coercedNumber(ZodNumber, params);
}
function boolean3(params) {
  return _coercedBoolean(ZodBoolean, params);
}
function bigint3(params) {
  return _coercedBigint(ZodBigInt, params);
}
function date4(params) {
  return _coercedDate(ZodDate, params);
}

// ../../node_modules/zod/v4/classic/external.js
config(en_default());

// ../../node_modules/@cfworker/json-schema/dist/esm/dereference.js
var initialBaseURI = typeof self !== "undefined" && self.location && self.location.origin !== "null" ? new URL(self.location.origin + self.location.pathname + location.search) : new URL("https://github.com/cfworker");

// ../../node_modules/@cfworker/json-schema/dist/esm/format.js
var DATE = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
var DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var TIME = /^(\d\d):(\d\d):(\d\d)(\.\d+)?(z|[+-]\d\d(?::?\d\d)?)?$/i;
var HOSTNAME = /^(?=.{1,253}\.?$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*\.?$/i;
var URIREF = /^(?:[a-z][a-z0-9+\-.]*:)?(?:\/?\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){3}|(?:(?:[0-9a-f]{1,4}:){0,2}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){2}|(?:(?:[0-9a-f]{1,4}:){0,3}[0-9a-f]{1,4})?::[0-9a-f]{1,4}:|(?:(?:[0-9a-f]{1,4}:){0,4}[0-9a-f]{1,4})?::)(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'"()*+,;=]|%[0-9a-f]{2})*)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})*)*)?(?:\?(?:[a-z0-9\-._~!$&'"()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'"()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i;
var URITEMPLATE = /^(?:(?:[^\x00-\x20"'<>%\\^`{|}]|%[0-9a-f]{2})|\{[+#./;?&=,!@|]?(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?(?:,(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?)*\})*$/i;
var URL_ = /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u{00a1}-\u{ffff}0-9]+-?)*[a-z\u{00a1}-\u{ffff}0-9]+)(?:\.(?:[a-z\u{00a1}-\u{ffff}0-9]+-?)*[a-z\u{00a1}-\u{ffff}0-9]+)*(?:\.(?:[a-z\u{00a1}-\u{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/iu;
var UUID = /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i;
var JSON_POINTER = /^(?:\/(?:[^~/]|~0|~1)*)*$/;
var JSON_POINTER_URI_FRAGMENT = /^#(?:\/(?:[a-z0-9_\-.!$&'()*+,;:=@]|%[0-9a-f]{2}|~0|~1)*)*$/i;
var RELATIVE_JSON_POINTER = /^(?:0|[1-9][0-9]*)(?:#|(?:\/(?:[^~/]|~0|~1)*)*)$/;
var EMAIL = (input) => {
  if (input[0] === '"')
    return false;
  const [name, host, ...rest] = input.split("@");
  if (!name || !host || rest.length !== 0 || name.length > 64 || host.length > 253)
    return false;
  if (name[0] === "." || name.endsWith(".") || name.includes(".."))
    return false;
  if (!/^[a-z0-9.-]+$/i.test(host) || !/^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+$/i.test(name))
    return false;
  return host.split(".").every((part) => /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/i.test(part));
};
var IPV4 = /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
var IPV6 = /^((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))$/i;
var DURATION = (input) => input.length > 1 && input.length < 80 && (/^P\d+([.,]\d+)?W$/.test(input) || /^P[\dYMDTHS]*(\d[.,]\d+)?[YMDHS]$/.test(input) && /^P([.,\d]+Y)?([.,\d]+M)?([.,\d]+D)?(T([.,\d]+H)?([.,\d]+M)?([.,\d]+S)?)?$/.test(input));
function bind(r) {
  return r.test.bind(r);
}
var format = {
  date: date5,
  time: time3.bind(void 0, false),
  "date-time": date_time,
  duration: DURATION,
  uri,
  "uri-reference": bind(URIREF),
  "uri-template": bind(URITEMPLATE),
  url: bind(URL_),
  email: EMAIL,
  hostname: bind(HOSTNAME),
  ipv4: bind(IPV4),
  ipv6: bind(IPV6),
  regex,
  uuid: bind(UUID),
  "json-pointer": bind(JSON_POINTER),
  "json-pointer-uri-fragment": bind(JSON_POINTER_URI_FRAGMENT),
  "relative-json-pointer": bind(RELATIVE_JSON_POINTER)
};
function isLeapYear(year) {
  return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
}
function date5(str) {
  const matches = str.match(DATE);
  if (!matches)
    return false;
  const year = +matches[1];
  const month = +matches[2];
  const day = +matches[3];
  return month >= 1 && month <= 12 && day >= 1 && day <= (month == 2 && isLeapYear(year) ? 29 : DAYS[month]);
}
function time3(full, str) {
  const matches = str.match(TIME);
  if (!matches)
    return false;
  const hour = +matches[1];
  const minute = +matches[2];
  const second = +matches[3];
  const timeZone = !!matches[5];
  return (hour <= 23 && minute <= 59 && second <= 59 || hour == 23 && minute == 59 && second == 60) && (!full || timeZone);
}
var DATE_TIME_SEPARATOR = /t|\s/i;
function date_time(str) {
  const dateTime = str.split(DATE_TIME_SEPARATOR);
  return dateTime.length == 2 && date5(dateTime[0]) && time3(true, dateTime[1]);
}
var NOT_URI_FRAGMENT = /\/|:/;
var URI_PATTERN = /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){3}|(?:(?:[0-9a-f]{1,4}:){0,2}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){2}|(?:(?:[0-9a-f]{1,4}:){0,3}[0-9a-f]{1,4})?::[0-9a-f]{1,4}:|(?:(?:[0-9a-f]{1,4}:){0,4}[0-9a-f]{1,4})?::)(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'()*+,;=]|%[0-9a-f]{2})*)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)(?:\?(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i;
function uri(str) {
  return NOT_URI_FRAGMENT.test(str) && URI_PATTERN.test(str);
}
var Z_ANCHOR = /[^\\]\\Z/;
function regex(str) {
  if (Z_ANCHOR.test(str))
    return false;
  try {
    new RegExp(str, "u");
    return true;
  } catch (e) {
    return false;
  }
}

// ../../node_modules/@cfworker/json-schema/dist/esm/types.js
var OutputFormat;
(function(OutputFormat2) {
  OutputFormat2[OutputFormat2["Flag"] = 1] = "Flag";
  OutputFormat2[OutputFormat2["Basic"] = 2] = "Basic";
  OutputFormat2[OutputFormat2["Detailed"] = 4] = "Detailed";
})(OutputFormat || (OutputFormat = {}));

// ../../node_modules/@modelcontextprotocol/client/dist/src-RIHfgPRm.mjs
var __create2 = Object.create;
var __defProp2 = Object.defineProperty;
var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
var __getOwnPropNames2 = Object.getOwnPropertyNames;
var __getProtoOf2 = Object.getPrototypeOf;
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
var __copyProps2 = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (var keys = __getOwnPropNames2(from), i = 0, n = keys.length, key; i < n; i++) {
      key = keys[i];
      if (!__hasOwnProp2.call(to, key) && key !== except) {
        __defProp2(to, key, {
          get: ((k) => from[k]).bind(null, key),
          enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable
        });
      }
    }
  }
  return to;
};
var __toESM2 = (mod, isNodeMode, target) => (target = mod != null ? __create2(__getProtoOf2(mod)) : {}, __copyProps2(isNodeMode || !mod || !mod.__esModule ? __defProp2(target, "default", {
  value: mod,
  enumerable: true
}) : target, mod));
var OAuthErrorCode = /* @__PURE__ */ (function(OAuthErrorCode$1) {
  OAuthErrorCode$1["InvalidRequest"] = "invalid_request";
  OAuthErrorCode$1["InvalidClient"] = "invalid_client";
  OAuthErrorCode$1["InvalidGrant"] = "invalid_grant";
  OAuthErrorCode$1["UnauthorizedClient"] = "unauthorized_client";
  OAuthErrorCode$1["UnsupportedGrantType"] = "unsupported_grant_type";
  OAuthErrorCode$1["InvalidScope"] = "invalid_scope";
  OAuthErrorCode$1["AccessDenied"] = "access_denied";
  OAuthErrorCode$1["ServerError"] = "server_error";
  OAuthErrorCode$1["TemporarilyUnavailable"] = "temporarily_unavailable";
  OAuthErrorCode$1["UnsupportedResponseType"] = "unsupported_response_type";
  OAuthErrorCode$1["UnsupportedTokenType"] = "unsupported_token_type";
  OAuthErrorCode$1["InvalidToken"] = "invalid_token";
  OAuthErrorCode$1["MethodNotAllowed"] = "method_not_allowed";
  OAuthErrorCode$1["TooManyRequests"] = "too_many_requests";
  OAuthErrorCode$1["InvalidClientMetadata"] = "invalid_client_metadata";
  OAuthErrorCode$1["InsufficientScope"] = "insufficient_scope";
  OAuthErrorCode$1["InvalidTarget"] = "invalid_target";
  return OAuthErrorCode$1;
})({});
var OAuthError = class OAuthError2 extends Error {
  constructor(code, message, errorUri) {
    super(message);
    this.code = code;
    this.errorUri = errorUri;
    this.name = "OAuthError";
  }
  /**
  * Converts the error to a standard OAuth error response object.
  */
  toResponseObject() {
    const response = {
      error: this.code,
      error_description: this.message
    };
    if (this.errorUri) response.error_uri = this.errorUri;
    return response;
  }
  /**
  * Creates an {@linkcode OAuthError} from an OAuth error response.
  */
  static fromResponse(response) {
    return new OAuthError2(response.error, response.error_description ?? response.error, response.error_uri);
  }
};
var SdkErrorCode = /* @__PURE__ */ (function(SdkErrorCode$1) {
  SdkErrorCode$1["NotConnected"] = "NOT_CONNECTED";
  SdkErrorCode$1["AlreadyConnected"] = "ALREADY_CONNECTED";
  SdkErrorCode$1["NotInitialized"] = "NOT_INITIALIZED";
  SdkErrorCode$1["CapabilityNotSupported"] = "CAPABILITY_NOT_SUPPORTED";
  SdkErrorCode$1["RequestTimeout"] = "REQUEST_TIMEOUT";
  SdkErrorCode$1["ConnectionClosed"] = "CONNECTION_CLOSED";
  SdkErrorCode$1["SendFailed"] = "SEND_FAILED";
  SdkErrorCode$1["ClientHttpNotImplemented"] = "CLIENT_HTTP_NOT_IMPLEMENTED";
  SdkErrorCode$1["ClientHttpAuthentication"] = "CLIENT_HTTP_AUTHENTICATION";
  SdkErrorCode$1["ClientHttpForbidden"] = "CLIENT_HTTP_FORBIDDEN";
  SdkErrorCode$1["ClientHttpUnexpectedContent"] = "CLIENT_HTTP_UNEXPECTED_CONTENT";
  SdkErrorCode$1["ClientHttpFailedToOpenStream"] = "CLIENT_HTTP_FAILED_TO_OPEN_STREAM";
  SdkErrorCode$1["ClientHttpFailedToTerminateSession"] = "CLIENT_HTTP_FAILED_TO_TERMINATE_SESSION";
  return SdkErrorCode$1;
})({});
var SdkError = class extends Error {
  constructor(code, message, data) {
    super(message);
    this.code = code;
    this.data = data;
    this.name = "SdkError";
  }
};
var SafeUrlSchema = url().superRefine((val, ctx) => {
  if (!URL.canParse(val)) {
    ctx.addIssue({
      code: ZodIssueCode.custom,
      message: "URL must be parseable",
      fatal: true
    });
    return NEVER;
  }
}).refine((url2) => {
  const u = new URL(url2);
  return u.protocol !== "javascript:" && u.protocol !== "data:" && u.protocol !== "vbscript:";
}, { message: "URL cannot use javascript:, data:, or vbscript: scheme" });
var OAuthProtectedResourceMetadataSchema = looseObject({
  resource: string2().url(),
  authorization_servers: array(SafeUrlSchema).optional(),
  jwks_uri: string2().url().optional(),
  scopes_supported: array(string2()).optional(),
  bearer_methods_supported: array(string2()).optional(),
  resource_signing_alg_values_supported: array(string2()).optional(),
  resource_name: string2().optional(),
  resource_documentation: string2().optional(),
  resource_policy_uri: string2().url().optional(),
  resource_tos_uri: string2().url().optional(),
  tls_client_certificate_bound_access_tokens: boolean2().optional(),
  authorization_details_types_supported: array(string2()).optional(),
  dpop_signing_alg_values_supported: array(string2()).optional(),
  dpop_bound_access_tokens_required: boolean2().optional()
});
var OAuthMetadataSchema = looseObject({
  issuer: string2(),
  authorization_endpoint: SafeUrlSchema,
  token_endpoint: SafeUrlSchema,
  registration_endpoint: SafeUrlSchema.optional(),
  scopes_supported: array(string2()).optional(),
  response_types_supported: array(string2()),
  response_modes_supported: array(string2()).optional(),
  grant_types_supported: array(string2()).optional(),
  token_endpoint_auth_methods_supported: array(string2()).optional(),
  token_endpoint_auth_signing_alg_values_supported: array(string2()).optional(),
  service_documentation: SafeUrlSchema.optional(),
  revocation_endpoint: SafeUrlSchema.optional(),
  revocation_endpoint_auth_methods_supported: array(string2()).optional(),
  revocation_endpoint_auth_signing_alg_values_supported: array(string2()).optional(),
  introspection_endpoint: string2().optional(),
  introspection_endpoint_auth_methods_supported: array(string2()).optional(),
  introspection_endpoint_auth_signing_alg_values_supported: array(string2()).optional(),
  code_challenge_methods_supported: array(string2()).optional(),
  client_id_metadata_document_supported: boolean2().optional()
});
var OpenIdProviderMetadataSchema = looseObject({
  issuer: string2(),
  authorization_endpoint: SafeUrlSchema,
  token_endpoint: SafeUrlSchema,
  userinfo_endpoint: SafeUrlSchema.optional(),
  jwks_uri: SafeUrlSchema,
  registration_endpoint: SafeUrlSchema.optional(),
  scopes_supported: array(string2()).optional(),
  response_types_supported: array(string2()),
  response_modes_supported: array(string2()).optional(),
  grant_types_supported: array(string2()).optional(),
  acr_values_supported: array(string2()).optional(),
  subject_types_supported: array(string2()),
  id_token_signing_alg_values_supported: array(string2()),
  id_token_encryption_alg_values_supported: array(string2()).optional(),
  id_token_encryption_enc_values_supported: array(string2()).optional(),
  userinfo_signing_alg_values_supported: array(string2()).optional(),
  userinfo_encryption_alg_values_supported: array(string2()).optional(),
  userinfo_encryption_enc_values_supported: array(string2()).optional(),
  request_object_signing_alg_values_supported: array(string2()).optional(),
  request_object_encryption_alg_values_supported: array(string2()).optional(),
  request_object_encryption_enc_values_supported: array(string2()).optional(),
  token_endpoint_auth_methods_supported: array(string2()).optional(),
  token_endpoint_auth_signing_alg_values_supported: array(string2()).optional(),
  display_values_supported: array(string2()).optional(),
  claim_types_supported: array(string2()).optional(),
  claims_supported: array(string2()).optional(),
  service_documentation: string2().optional(),
  claims_locales_supported: array(string2()).optional(),
  ui_locales_supported: array(string2()).optional(),
  claims_parameter_supported: boolean2().optional(),
  request_parameter_supported: boolean2().optional(),
  request_uri_parameter_supported: boolean2().optional(),
  require_request_uri_registration: boolean2().optional(),
  op_policy_uri: SafeUrlSchema.optional(),
  op_tos_uri: SafeUrlSchema.optional(),
  client_id_metadata_document_supported: boolean2().optional()
});
var OpenIdProviderDiscoveryMetadataSchema = object({
  ...OpenIdProviderMetadataSchema.shape,
  ...OAuthMetadataSchema.pick({ code_challenge_methods_supported: true }).shape
});
var OAuthTokensSchema = object({
  access_token: string2(),
  id_token: string2().optional(),
  token_type: string2(),
  expires_in: coerce_exports.number().optional(),
  scope: string2().optional(),
  refresh_token: string2().optional()
}).strip();
var IdJagTokenExchangeResponseSchema = object({
  issued_token_type: literal("urn:ietf:params:oauth:token-type:id-jag"),
  access_token: string2(),
  token_type: string2().optional(),
  expires_in: number2().optional(),
  scope: string2().optional()
}).strip();
var OAuthErrorResponseSchema = object({
  error: string2(),
  error_description: string2().optional(),
  error_uri: string2().optional()
});
var OptionalSafeUrlSchema = SafeUrlSchema.optional().or(literal("").transform(() => void 0));
var OAuthClientMetadataSchema = object({
  redirect_uris: array(SafeUrlSchema),
  token_endpoint_auth_method: string2().optional(),
  grant_types: array(string2()).optional(),
  response_types: array(string2()).optional(),
  client_name: string2().optional(),
  client_uri: SafeUrlSchema.optional(),
  logo_uri: OptionalSafeUrlSchema,
  scope: string2().optional(),
  contacts: array(string2()).optional(),
  tos_uri: OptionalSafeUrlSchema,
  policy_uri: string2().optional(),
  jwks_uri: SafeUrlSchema.optional(),
  jwks: any().optional(),
  software_id: string2().optional(),
  software_version: string2().optional(),
  software_statement: string2().optional()
}).strip();
var OAuthClientInformationSchema = object({
  client_id: string2(),
  client_secret: string2().optional(),
  client_id_issued_at: number2().optional(),
  client_secret_expires_at: number2().optional()
}).strip();
var OAuthClientInformationFullSchema = OAuthClientMetadataSchema.merge(OAuthClientInformationSchema);
var OAuthClientRegistrationErrorSchema = object({
  error: string2(),
  error_description: string2().optional()
}).strip();
var OAuthTokenRevocationRequestSchema = object({
  token: string2(),
  token_type_hint: string2().optional()
}).strip();
function resourceUrlFromServerUrl(url2) {
  const resourceURL = typeof url2 === "string" ? new URL(url2) : new URL(url2.href);
  resourceURL.hash = "";
  return resourceURL;
}
function checkResourceAllowed({ requestedResource, configuredResource }) {
  const requested = typeof requestedResource === "string" ? new URL(requestedResource) : new URL(requestedResource.href);
  const configured = typeof configuredResource === "string" ? new URL(configuredResource) : new URL(configuredResource.href);
  if (requested.origin !== configured.origin) return false;
  if (requested.pathname.length < configured.pathname.length) return false;
  const requestedPath = requested.pathname.endsWith("/") ? requested.pathname : requested.pathname + "/";
  const configuredPath = configured.pathname.endsWith("/") ? configured.pathname : configured.pathname + "/";
  return requestedPath.startsWith(configuredPath);
}
var LATEST_PROTOCOL_VERSION = "2025-11-25";
var SUPPORTED_PROTOCOL_VERSIONS = [
  LATEST_PROTOCOL_VERSION,
  "2025-06-18",
  "2025-03-26",
  "2024-11-05",
  "2024-10-07"
];
var RELATED_TASK_META_KEY = "io.modelcontextprotocol/related-task";
var JSONRPC_VERSION = "2.0";
var ProtocolErrorCode = /* @__PURE__ */ (function(ProtocolErrorCode$1) {
  ProtocolErrorCode$1[ProtocolErrorCode$1["ParseError"] = -32700] = "ParseError";
  ProtocolErrorCode$1[ProtocolErrorCode$1["InvalidRequest"] = -32600] = "InvalidRequest";
  ProtocolErrorCode$1[ProtocolErrorCode$1["MethodNotFound"] = -32601] = "MethodNotFound";
  ProtocolErrorCode$1[ProtocolErrorCode$1["InvalidParams"] = -32602] = "InvalidParams";
  ProtocolErrorCode$1[ProtocolErrorCode$1["InternalError"] = -32603] = "InternalError";
  ProtocolErrorCode$1[ProtocolErrorCode$1["ResourceNotFound"] = -32002] = "ResourceNotFound";
  ProtocolErrorCode$1[ProtocolErrorCode$1["UrlElicitationRequired"] = -32042] = "UrlElicitationRequired";
  return ProtocolErrorCode$1;
})({});
var ProtocolError = class ProtocolError2 extends Error {
  constructor(code, message, data) {
    super(message);
    this.code = code;
    this.data = data;
    this.name = "ProtocolError";
  }
  /**
  * Factory method to create the appropriate error type based on the error code and data
  */
  static fromError(code, message, data) {
    if (code === ProtocolErrorCode.UrlElicitationRequired && data) {
      const errorData = data;
      if (errorData.elicitations) return new UrlElicitationRequiredError(errorData.elicitations, message);
    }
    return new ProtocolError2(code, message, data);
  }
};
var UrlElicitationRequiredError = class extends ProtocolError {
  constructor(elicitations, message = `URL elicitation${elicitations.length > 1 ? "s" : ""} required`) {
    super(ProtocolErrorCode.UrlElicitationRequired, message, { elicitations });
  }
  get elicitations() {
    return this.data?.elicitations ?? [];
  }
};
var JSONValueSchema = lazy(() => union([
  string2(),
  number2(),
  boolean2(),
  _null3(),
  record(string2(), JSONValueSchema),
  array(JSONValueSchema)
]));
var JSONObjectSchema = record(string2(), JSONValueSchema);
var JSONArraySchema = array(JSONValueSchema);
var ProgressTokenSchema = union([string2(), number2().int()]);
var CursorSchema = string2();
var TaskCreationParamsSchema = looseObject({
  ttl: number2().optional(),
  pollInterval: number2().optional()
});
var TaskMetadataSchema = object({ ttl: number2().optional() });
var RelatedTaskMetadataSchema = object({ taskId: string2() });
var RequestMetaSchema = looseObject({
  progressToken: ProgressTokenSchema.optional(),
  [RELATED_TASK_META_KEY]: RelatedTaskMetadataSchema.optional()
});
var BaseRequestParamsSchema = object({ _meta: RequestMetaSchema.optional() });
var TaskAugmentedRequestParamsSchema = BaseRequestParamsSchema.extend({ task: TaskMetadataSchema.optional() });
var RequestSchema = object({
  method: string2(),
  params: BaseRequestParamsSchema.loose().optional()
});
var NotificationsParamsSchema = object({ _meta: RequestMetaSchema.optional() });
var NotificationSchema = object({
  method: string2(),
  params: NotificationsParamsSchema.loose().optional()
});
var ResultSchema = looseObject({ _meta: RequestMetaSchema.optional() });
var RequestIdSchema = union([string2(), number2().int()]);
var JSONRPCRequestSchema = object({
  jsonrpc: literal(JSONRPC_VERSION),
  id: RequestIdSchema,
  ...RequestSchema.shape
}).strict();
var JSONRPCNotificationSchema = object({
  jsonrpc: literal(JSONRPC_VERSION),
  ...NotificationSchema.shape
}).strict();
var JSONRPCResultResponseSchema = object({
  jsonrpc: literal(JSONRPC_VERSION),
  id: RequestIdSchema,
  result: ResultSchema
}).strict();
var JSONRPCErrorResponseSchema = object({
  jsonrpc: literal(JSONRPC_VERSION),
  id: RequestIdSchema.optional(),
  error: object({
    code: number2().int(),
    message: string2(),
    data: unknown().optional()
  })
}).strict();
var JSONRPCMessageSchema = union([
  JSONRPCRequestSchema,
  JSONRPCNotificationSchema,
  JSONRPCResultResponseSchema,
  JSONRPCErrorResponseSchema
]);
var JSONRPCResponseSchema = union([JSONRPCResultResponseSchema, JSONRPCErrorResponseSchema]);
var EmptyResultSchema = ResultSchema.strict();
var CancelledNotificationParamsSchema = NotificationsParamsSchema.extend({
  requestId: RequestIdSchema.optional(),
  reason: string2().optional()
});
var CancelledNotificationSchema = NotificationSchema.extend({
  method: literal("notifications/cancelled"),
  params: CancelledNotificationParamsSchema
});
var IconSchema = object({
  src: string2(),
  mimeType: string2().optional(),
  sizes: array(string2()).optional(),
  theme: _enum(["light", "dark"]).optional()
});
var IconsSchema = object({ icons: array(IconSchema).optional() });
var BaseMetadataSchema = object({
  name: string2(),
  title: string2().optional()
});
var ImplementationSchema = BaseMetadataSchema.extend({
  ...BaseMetadataSchema.shape,
  ...IconsSchema.shape,
  version: string2(),
  websiteUrl: string2().optional(),
  description: string2().optional()
});
var FormElicitationCapabilitySchema = intersection(object({ applyDefaults: boolean2().optional() }), JSONObjectSchema);
var ElicitationCapabilitySchema = preprocess((value) => {
  if (value && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0) return { form: {} };
  return value;
}, intersection(object({
  form: FormElicitationCapabilitySchema.optional(),
  url: JSONObjectSchema.optional()
}), JSONObjectSchema.optional()));
var ClientTasksCapabilitySchema = looseObject({
  list: JSONObjectSchema.optional(),
  cancel: JSONObjectSchema.optional(),
  requests: looseObject({
    sampling: looseObject({ createMessage: JSONObjectSchema.optional() }).optional(),
    elicitation: looseObject({ create: JSONObjectSchema.optional() }).optional()
  }).optional()
});
var ServerTasksCapabilitySchema = looseObject({
  list: JSONObjectSchema.optional(),
  cancel: JSONObjectSchema.optional(),
  requests: looseObject({ tools: looseObject({ call: JSONObjectSchema.optional() }).optional() }).optional()
});
var ClientCapabilitiesSchema = object({
  experimental: record(string2(), JSONObjectSchema).optional(),
  sampling: object({
    context: JSONObjectSchema.optional(),
    tools: JSONObjectSchema.optional()
  }).optional(),
  elicitation: ElicitationCapabilitySchema.optional(),
  roots: object({ listChanged: boolean2().optional() }).optional(),
  tasks: ClientTasksCapabilitySchema.optional(),
  extensions: record(string2(), JSONObjectSchema).optional()
});
var InitializeRequestParamsSchema = BaseRequestParamsSchema.extend({
  protocolVersion: string2(),
  capabilities: ClientCapabilitiesSchema,
  clientInfo: ImplementationSchema
});
var InitializeRequestSchema = RequestSchema.extend({
  method: literal("initialize"),
  params: InitializeRequestParamsSchema
});
var ServerCapabilitiesSchema = object({
  experimental: record(string2(), JSONObjectSchema).optional(),
  logging: JSONObjectSchema.optional(),
  completions: JSONObjectSchema.optional(),
  prompts: object({ listChanged: boolean2().optional() }).optional(),
  resources: object({
    subscribe: boolean2().optional(),
    listChanged: boolean2().optional()
  }).optional(),
  tools: object({ listChanged: boolean2().optional() }).optional(),
  tasks: ServerTasksCapabilitySchema.optional(),
  extensions: record(string2(), JSONObjectSchema).optional()
});
var InitializeResultSchema = ResultSchema.extend({
  protocolVersion: string2(),
  capabilities: ServerCapabilitiesSchema,
  serverInfo: ImplementationSchema,
  instructions: string2().optional()
});
var InitializedNotificationSchema = NotificationSchema.extend({
  method: literal("notifications/initialized"),
  params: NotificationsParamsSchema.optional()
});
var PingRequestSchema = RequestSchema.extend({
  method: literal("ping"),
  params: BaseRequestParamsSchema.optional()
});
var ProgressSchema = object({
  progress: number2(),
  total: optional(number2()),
  message: optional(string2())
});
var ProgressNotificationParamsSchema = object({
  ...NotificationsParamsSchema.shape,
  ...ProgressSchema.shape,
  progressToken: ProgressTokenSchema
});
var ProgressNotificationSchema = NotificationSchema.extend({
  method: literal("notifications/progress"),
  params: ProgressNotificationParamsSchema
});
var PaginatedRequestParamsSchema = BaseRequestParamsSchema.extend({ cursor: CursorSchema.optional() });
var PaginatedRequestSchema = RequestSchema.extend({ params: PaginatedRequestParamsSchema.optional() });
var PaginatedResultSchema = ResultSchema.extend({ nextCursor: CursorSchema.optional() });
var TaskStatusSchema = _enum([
  "working",
  "input_required",
  "completed",
  "failed",
  "cancelled"
]);
var TaskSchema = object({
  taskId: string2(),
  status: TaskStatusSchema,
  ttl: union([number2(), _null3()]),
  createdAt: string2(),
  lastUpdatedAt: string2(),
  pollInterval: optional(number2()),
  statusMessage: optional(string2())
});
var CreateTaskResultSchema = ResultSchema.extend({ task: TaskSchema });
var TaskStatusNotificationParamsSchema = NotificationsParamsSchema.merge(TaskSchema);
var TaskStatusNotificationSchema = NotificationSchema.extend({
  method: literal("notifications/tasks/status"),
  params: TaskStatusNotificationParamsSchema
});
var GetTaskRequestSchema = RequestSchema.extend({
  method: literal("tasks/get"),
  params: BaseRequestParamsSchema.extend({ taskId: string2() })
});
var GetTaskResultSchema = ResultSchema.merge(TaskSchema);
var GetTaskPayloadRequestSchema = RequestSchema.extend({
  method: literal("tasks/result"),
  params: BaseRequestParamsSchema.extend({ taskId: string2() })
});
var GetTaskPayloadResultSchema = ResultSchema.loose();
var ListTasksRequestSchema = PaginatedRequestSchema.extend({ method: literal("tasks/list") });
var ListTasksResultSchema = PaginatedResultSchema.extend({ tasks: array(TaskSchema) });
var CancelTaskRequestSchema = RequestSchema.extend({
  method: literal("tasks/cancel"),
  params: BaseRequestParamsSchema.extend({ taskId: string2() })
});
var CancelTaskResultSchema = ResultSchema.merge(TaskSchema);
var ResourceContentsSchema = object({
  uri: string2(),
  mimeType: optional(string2()),
  _meta: record(string2(), unknown()).optional()
});
var TextResourceContentsSchema = ResourceContentsSchema.extend({ text: string2() });
var Base64Schema = string2().refine((val) => {
  try {
    atob(val);
    return true;
  } catch {
    return false;
  }
}, { message: "Invalid Base64 string" });
var BlobResourceContentsSchema = ResourceContentsSchema.extend({ blob: Base64Schema });
var RoleSchema = _enum(["user", "assistant"]);
var AnnotationsSchema = object({
  audience: array(RoleSchema).optional(),
  priority: number2().min(0).max(1).optional(),
  lastModified: iso_exports.datetime({ offset: true }).optional()
});
var ResourceSchema = object({
  ...BaseMetadataSchema.shape,
  ...IconsSchema.shape,
  uri: string2(),
  description: optional(string2()),
  mimeType: optional(string2()),
  size: optional(number2()),
  annotations: AnnotationsSchema.optional(),
  _meta: optional(looseObject({}))
});
var ResourceTemplateSchema = object({
  ...BaseMetadataSchema.shape,
  ...IconsSchema.shape,
  uriTemplate: string2(),
  description: optional(string2()),
  mimeType: optional(string2()),
  annotations: AnnotationsSchema.optional(),
  _meta: optional(looseObject({}))
});
var ListResourcesRequestSchema = PaginatedRequestSchema.extend({ method: literal("resources/list") });
var ListResourcesResultSchema = PaginatedResultSchema.extend({ resources: array(ResourceSchema) });
var ListResourceTemplatesRequestSchema = PaginatedRequestSchema.extend({ method: literal("resources/templates/list") });
var ListResourceTemplatesResultSchema = PaginatedResultSchema.extend({ resourceTemplates: array(ResourceTemplateSchema) });
var ResourceRequestParamsSchema = BaseRequestParamsSchema.extend({ uri: string2() });
var ReadResourceRequestParamsSchema = ResourceRequestParamsSchema;
var ReadResourceRequestSchema = RequestSchema.extend({
  method: literal("resources/read"),
  params: ReadResourceRequestParamsSchema
});
var ReadResourceResultSchema = ResultSchema.extend({ contents: array(union([TextResourceContentsSchema, BlobResourceContentsSchema])) });
var ResourceListChangedNotificationSchema = NotificationSchema.extend({
  method: literal("notifications/resources/list_changed"),
  params: NotificationsParamsSchema.optional()
});
var SubscribeRequestParamsSchema = ResourceRequestParamsSchema;
var SubscribeRequestSchema = RequestSchema.extend({
  method: literal("resources/subscribe"),
  params: SubscribeRequestParamsSchema
});
var UnsubscribeRequestParamsSchema = ResourceRequestParamsSchema;
var UnsubscribeRequestSchema = RequestSchema.extend({
  method: literal("resources/unsubscribe"),
  params: UnsubscribeRequestParamsSchema
});
var ResourceUpdatedNotificationParamsSchema = NotificationsParamsSchema.extend({ uri: string2() });
var ResourceUpdatedNotificationSchema = NotificationSchema.extend({
  method: literal("notifications/resources/updated"),
  params: ResourceUpdatedNotificationParamsSchema
});
var PromptArgumentSchema = object({
  name: string2(),
  description: optional(string2()),
  required: optional(boolean2())
});
var PromptSchema = object({
  ...BaseMetadataSchema.shape,
  ...IconsSchema.shape,
  description: optional(string2()),
  arguments: optional(array(PromptArgumentSchema)),
  _meta: optional(looseObject({}))
});
var ListPromptsRequestSchema = PaginatedRequestSchema.extend({ method: literal("prompts/list") });
var ListPromptsResultSchema = PaginatedResultSchema.extend({ prompts: array(PromptSchema) });
var GetPromptRequestParamsSchema = BaseRequestParamsSchema.extend({
  name: string2(),
  arguments: record(string2(), string2()).optional()
});
var GetPromptRequestSchema = RequestSchema.extend({
  method: literal("prompts/get"),
  params: GetPromptRequestParamsSchema
});
var TextContentSchema = object({
  type: literal("text"),
  text: string2(),
  annotations: AnnotationsSchema.optional(),
  _meta: record(string2(), unknown()).optional()
});
var ImageContentSchema = object({
  type: literal("image"),
  data: Base64Schema,
  mimeType: string2(),
  annotations: AnnotationsSchema.optional(),
  _meta: record(string2(), unknown()).optional()
});
var AudioContentSchema = object({
  type: literal("audio"),
  data: Base64Schema,
  mimeType: string2(),
  annotations: AnnotationsSchema.optional(),
  _meta: record(string2(), unknown()).optional()
});
var ToolUseContentSchema = object({
  type: literal("tool_use"),
  name: string2(),
  id: string2(),
  input: record(string2(), unknown()),
  _meta: record(string2(), unknown()).optional()
});
var EmbeddedResourceSchema = object({
  type: literal("resource"),
  resource: union([TextResourceContentsSchema, BlobResourceContentsSchema]),
  annotations: AnnotationsSchema.optional(),
  _meta: record(string2(), unknown()).optional()
});
var ResourceLinkSchema = ResourceSchema.extend({ type: literal("resource_link") });
var ContentBlockSchema = union([
  TextContentSchema,
  ImageContentSchema,
  AudioContentSchema,
  ResourceLinkSchema,
  EmbeddedResourceSchema
]);
var PromptMessageSchema = object({
  role: RoleSchema,
  content: ContentBlockSchema
});
var GetPromptResultSchema = ResultSchema.extend({
  description: string2().optional(),
  messages: array(PromptMessageSchema)
});
var PromptListChangedNotificationSchema = NotificationSchema.extend({
  method: literal("notifications/prompts/list_changed"),
  params: NotificationsParamsSchema.optional()
});
var ToolAnnotationsSchema = object({
  title: string2().optional(),
  readOnlyHint: boolean2().optional(),
  destructiveHint: boolean2().optional(),
  idempotentHint: boolean2().optional(),
  openWorldHint: boolean2().optional()
});
var ToolExecutionSchema = object({ taskSupport: _enum([
  "required",
  "optional",
  "forbidden"
]).optional() });
var ToolSchema = object({
  ...BaseMetadataSchema.shape,
  ...IconsSchema.shape,
  description: string2().optional(),
  inputSchema: object({
    type: literal("object"),
    properties: record(string2(), JSONValueSchema).optional(),
    required: array(string2()).optional()
  }).catchall(unknown()),
  outputSchema: object({
    type: literal("object"),
    properties: record(string2(), JSONValueSchema).optional(),
    required: array(string2()).optional()
  }).catchall(unknown()).optional(),
  annotations: ToolAnnotationsSchema.optional(),
  execution: ToolExecutionSchema.optional(),
  _meta: record(string2(), unknown()).optional()
});
var ListToolsRequestSchema = PaginatedRequestSchema.extend({ method: literal("tools/list") });
var ListToolsResultSchema = PaginatedResultSchema.extend({ tools: array(ToolSchema) });
var CallToolResultSchema = ResultSchema.extend({
  content: array(ContentBlockSchema).default([]),
  structuredContent: record(string2(), unknown()).optional(),
  isError: boolean2().optional()
});
var CompatibilityCallToolResultSchema = CallToolResultSchema.or(ResultSchema.extend({ toolResult: unknown() }));
var CallToolRequestParamsSchema = TaskAugmentedRequestParamsSchema.extend({
  name: string2(),
  arguments: record(string2(), unknown()).optional()
});
var CallToolRequestSchema = RequestSchema.extend({
  method: literal("tools/call"),
  params: CallToolRequestParamsSchema
});
var ToolListChangedNotificationSchema = NotificationSchema.extend({
  method: literal("notifications/tools/list_changed"),
  params: NotificationsParamsSchema.optional()
});
var ListChangedOptionsBaseSchema = object({
  autoRefresh: boolean2().default(true),
  debounceMs: number2().int().nonnegative().default(300)
});
var LoggingLevelSchema = _enum([
  "debug",
  "info",
  "notice",
  "warning",
  "error",
  "critical",
  "alert",
  "emergency"
]);
var SetLevelRequestParamsSchema = BaseRequestParamsSchema.extend({ level: LoggingLevelSchema });
var SetLevelRequestSchema = RequestSchema.extend({
  method: literal("logging/setLevel"),
  params: SetLevelRequestParamsSchema
});
var LoggingMessageNotificationParamsSchema = NotificationsParamsSchema.extend({
  level: LoggingLevelSchema,
  logger: string2().optional(),
  data: unknown()
});
var LoggingMessageNotificationSchema = NotificationSchema.extend({
  method: literal("notifications/message"),
  params: LoggingMessageNotificationParamsSchema
});
var ModelHintSchema = object({ name: string2().optional() });
var ModelPreferencesSchema = object({
  hints: array(ModelHintSchema).optional(),
  costPriority: number2().min(0).max(1).optional(),
  speedPriority: number2().min(0).max(1).optional(),
  intelligencePriority: number2().min(0).max(1).optional()
});
var ToolChoiceSchema = object({ mode: _enum([
  "auto",
  "required",
  "none"
]).optional() });
var ToolResultContentSchema = object({
  type: literal("tool_result"),
  toolUseId: string2().describe("The unique identifier for the corresponding tool call."),
  content: array(ContentBlockSchema).default([]),
  structuredContent: object({}).loose().optional(),
  isError: boolean2().optional(),
  _meta: record(string2(), unknown()).optional()
});
var SamplingContentSchema = discriminatedUnion("type", [
  TextContentSchema,
  ImageContentSchema,
  AudioContentSchema
]);
var SamplingMessageContentBlockSchema = discriminatedUnion("type", [
  TextContentSchema,
  ImageContentSchema,
  AudioContentSchema,
  ToolUseContentSchema,
  ToolResultContentSchema
]);
var SamplingMessageSchema = object({
  role: RoleSchema,
  content: union([SamplingMessageContentBlockSchema, array(SamplingMessageContentBlockSchema)]),
  _meta: record(string2(), unknown()).optional()
});
var CreateMessageRequestParamsSchema = TaskAugmentedRequestParamsSchema.extend({
  messages: array(SamplingMessageSchema),
  modelPreferences: ModelPreferencesSchema.optional(),
  systemPrompt: string2().optional(),
  includeContext: _enum([
    "none",
    "thisServer",
    "allServers"
  ]).optional(),
  temperature: number2().optional(),
  maxTokens: number2().int(),
  stopSequences: array(string2()).optional(),
  metadata: JSONObjectSchema.optional(),
  tools: array(ToolSchema).optional(),
  toolChoice: ToolChoiceSchema.optional()
});
var CreateMessageRequestSchema = RequestSchema.extend({
  method: literal("sampling/createMessage"),
  params: CreateMessageRequestParamsSchema
});
var CreateMessageResultSchema = ResultSchema.extend({
  model: string2(),
  stopReason: optional(_enum([
    "endTurn",
    "stopSequence",
    "maxTokens"
  ]).or(string2())),
  role: RoleSchema,
  content: SamplingContentSchema
});
var CreateMessageResultWithToolsSchema = ResultSchema.extend({
  model: string2(),
  stopReason: optional(_enum([
    "endTurn",
    "stopSequence",
    "maxTokens",
    "toolUse"
  ]).or(string2())),
  role: RoleSchema,
  content: union([SamplingMessageContentBlockSchema, array(SamplingMessageContentBlockSchema)])
});
var BooleanSchemaSchema = object({
  type: literal("boolean"),
  title: string2().optional(),
  description: string2().optional(),
  default: boolean2().optional()
});
var StringSchemaSchema = object({
  type: literal("string"),
  title: string2().optional(),
  description: string2().optional(),
  minLength: number2().optional(),
  maxLength: number2().optional(),
  format: _enum([
    "email",
    "uri",
    "date",
    "date-time"
  ]).optional(),
  default: string2().optional()
});
var NumberSchemaSchema = object({
  type: _enum(["number", "integer"]),
  title: string2().optional(),
  description: string2().optional(),
  minimum: number2().optional(),
  maximum: number2().optional(),
  default: number2().optional()
});
var UntitledSingleSelectEnumSchemaSchema = object({
  type: literal("string"),
  title: string2().optional(),
  description: string2().optional(),
  enum: array(string2()),
  default: string2().optional()
});
var TitledSingleSelectEnumSchemaSchema = object({
  type: literal("string"),
  title: string2().optional(),
  description: string2().optional(),
  oneOf: array(object({
    const: string2(),
    title: string2()
  })),
  default: string2().optional()
});
var LegacyTitledEnumSchemaSchema = object({
  type: literal("string"),
  title: string2().optional(),
  description: string2().optional(),
  enum: array(string2()),
  enumNames: array(string2()).optional(),
  default: string2().optional()
});
var SingleSelectEnumSchemaSchema = union([UntitledSingleSelectEnumSchemaSchema, TitledSingleSelectEnumSchemaSchema]);
var UntitledMultiSelectEnumSchemaSchema = object({
  type: literal("array"),
  title: string2().optional(),
  description: string2().optional(),
  minItems: number2().optional(),
  maxItems: number2().optional(),
  items: object({
    type: literal("string"),
    enum: array(string2())
  }),
  default: array(string2()).optional()
});
var TitledMultiSelectEnumSchemaSchema = object({
  type: literal("array"),
  title: string2().optional(),
  description: string2().optional(),
  minItems: number2().optional(),
  maxItems: number2().optional(),
  items: object({ anyOf: array(object({
    const: string2(),
    title: string2()
  })) }),
  default: array(string2()).optional()
});
var MultiSelectEnumSchemaSchema = union([UntitledMultiSelectEnumSchemaSchema, TitledMultiSelectEnumSchemaSchema]);
var EnumSchemaSchema = union([
  LegacyTitledEnumSchemaSchema,
  SingleSelectEnumSchemaSchema,
  MultiSelectEnumSchemaSchema
]);
var PrimitiveSchemaDefinitionSchema = union([
  EnumSchemaSchema,
  BooleanSchemaSchema,
  StringSchemaSchema,
  NumberSchemaSchema
]);
var ElicitRequestFormParamsSchema = TaskAugmentedRequestParamsSchema.extend({
  mode: literal("form").optional(),
  message: string2(),
  requestedSchema: object({
    type: literal("object"),
    properties: record(string2(), PrimitiveSchemaDefinitionSchema),
    required: array(string2()).optional()
  })
});
var ElicitRequestURLParamsSchema = TaskAugmentedRequestParamsSchema.extend({
  mode: literal("url"),
  message: string2(),
  elicitationId: string2(),
  url: string2().url()
});
var ElicitRequestParamsSchema = union([ElicitRequestFormParamsSchema, ElicitRequestURLParamsSchema]);
var ElicitRequestSchema = RequestSchema.extend({
  method: literal("elicitation/create"),
  params: ElicitRequestParamsSchema
});
var ElicitationCompleteNotificationParamsSchema = NotificationsParamsSchema.extend({ elicitationId: string2() });
var ElicitationCompleteNotificationSchema = NotificationSchema.extend({
  method: literal("notifications/elicitation/complete"),
  params: ElicitationCompleteNotificationParamsSchema
});
var ElicitResultSchema = ResultSchema.extend({
  action: _enum([
    "accept",
    "decline",
    "cancel"
  ]),
  content: preprocess((val) => val === null ? void 0 : val, record(string2(), union([
    string2(),
    number2(),
    boolean2(),
    array(string2())
  ])).optional())
});
var ResourceTemplateReferenceSchema = object({
  type: literal("ref/resource"),
  uri: string2()
});
var PromptReferenceSchema = object({
  type: literal("ref/prompt"),
  name: string2()
});
var CompleteRequestParamsSchema = BaseRequestParamsSchema.extend({
  ref: union([PromptReferenceSchema, ResourceTemplateReferenceSchema]),
  argument: object({
    name: string2(),
    value: string2()
  }),
  context: object({ arguments: record(string2(), string2()).optional() }).optional()
});
var CompleteRequestSchema = RequestSchema.extend({
  method: literal("completion/complete"),
  params: CompleteRequestParamsSchema
});
var CompleteResultSchema = ResultSchema.extend({ completion: looseObject({
  values: array(string2()).max(100),
  total: optional(number2().int()),
  hasMore: optional(boolean2())
}) });
var RootSchema = object({
  uri: string2().startsWith("file://"),
  name: string2().optional(),
  _meta: record(string2(), unknown()).optional()
});
var ListRootsRequestSchema = RequestSchema.extend({
  method: literal("roots/list"),
  params: BaseRequestParamsSchema.optional()
});
var ListRootsResultSchema = ResultSchema.extend({ roots: array(RootSchema) });
var RootsListChangedNotificationSchema = NotificationSchema.extend({
  method: literal("notifications/roots/list_changed"),
  params: NotificationsParamsSchema.optional()
});
var ClientRequestSchema = union([
  PingRequestSchema,
  InitializeRequestSchema,
  CompleteRequestSchema,
  SetLevelRequestSchema,
  GetPromptRequestSchema,
  ListPromptsRequestSchema,
  ListResourcesRequestSchema,
  ListResourceTemplatesRequestSchema,
  ReadResourceRequestSchema,
  SubscribeRequestSchema,
  UnsubscribeRequestSchema,
  CallToolRequestSchema,
  ListToolsRequestSchema,
  GetTaskRequestSchema,
  GetTaskPayloadRequestSchema,
  ListTasksRequestSchema,
  CancelTaskRequestSchema
]);
var ClientNotificationSchema = union([
  CancelledNotificationSchema,
  ProgressNotificationSchema,
  InitializedNotificationSchema,
  RootsListChangedNotificationSchema,
  TaskStatusNotificationSchema
]);
var ClientResultSchema = union([
  EmptyResultSchema,
  CreateMessageResultSchema,
  CreateMessageResultWithToolsSchema,
  ElicitResultSchema,
  ListRootsResultSchema,
  GetTaskResultSchema,
  ListTasksResultSchema,
  CreateTaskResultSchema
]);
var ServerRequestSchema = union([
  PingRequestSchema,
  CreateMessageRequestSchema,
  ElicitRequestSchema,
  ListRootsRequestSchema,
  GetTaskRequestSchema,
  GetTaskPayloadRequestSchema,
  ListTasksRequestSchema,
  CancelTaskRequestSchema
]);
var ServerNotificationSchema = union([
  CancelledNotificationSchema,
  ProgressNotificationSchema,
  LoggingMessageNotificationSchema,
  ResourceUpdatedNotificationSchema,
  ResourceListChangedNotificationSchema,
  ToolListChangedNotificationSchema,
  PromptListChangedNotificationSchema,
  TaskStatusNotificationSchema,
  ElicitationCompleteNotificationSchema
]);
var ServerResultSchema = union([
  EmptyResultSchema,
  InitializeResultSchema,
  CompleteResultSchema,
  GetPromptResultSchema,
  ListPromptsResultSchema,
  ListResourcesResultSchema,
  ListResourceTemplatesResultSchema,
  ReadResourceResultSchema,
  CallToolResultSchema,
  ListToolsResultSchema,
  GetTaskResultSchema,
  ListTasksResultSchema,
  CreateTaskResultSchema
]);
var resultSchemas = {
  ping: EmptyResultSchema,
  initialize: InitializeResultSchema,
  "completion/complete": CompleteResultSchema,
  "logging/setLevel": EmptyResultSchema,
  "prompts/get": GetPromptResultSchema,
  "prompts/list": ListPromptsResultSchema,
  "resources/list": ListResourcesResultSchema,
  "resources/templates/list": ListResourceTemplatesResultSchema,
  "resources/read": ReadResourceResultSchema,
  "resources/subscribe": EmptyResultSchema,
  "resources/unsubscribe": EmptyResultSchema,
  "tools/call": union([CallToolResultSchema, CreateTaskResultSchema]),
  "tools/list": ListToolsResultSchema,
  "sampling/createMessage": union([CreateMessageResultWithToolsSchema, CreateTaskResultSchema]),
  "elicitation/create": union([ElicitResultSchema, CreateTaskResultSchema]),
  "roots/list": ListRootsResultSchema,
  "tasks/get": GetTaskResultSchema,
  "tasks/result": ResultSchema,
  "tasks/list": ListTasksResultSchema,
  "tasks/cancel": CancelTaskResultSchema
};
function getResultSchema(method) {
  return resultSchemas[method];
}
function buildSchemaMap(schemas) {
  const map2 = {};
  for (const schema of schemas) {
    const method = schema.shape.method.value;
    map2[method] = schema;
  }
  return map2;
}
var requestSchemas = buildSchemaMap([...ClientRequestSchema.options, ...ServerRequestSchema.options]);
var notificationSchemas = buildSchemaMap([...ClientNotificationSchema.options, ...ServerNotificationSchema.options]);
function getRequestSchema(method) {
  return requestSchemas[method];
}
function getNotificationSchema(method) {
  return notificationSchemas[method];
}
var isJSONRPCRequest = (value) => JSONRPCRequestSchema.safeParse(value).success;
var isJSONRPCNotification = (value) => JSONRPCNotificationSchema.safeParse(value).success;
var isJSONRPCResultResponse = (value) => JSONRPCResultResponseSchema.safeParse(value).success;
var isJSONRPCErrorResponse = (value) => JSONRPCErrorResponseSchema.safeParse(value).success;
var isTaskAugmentedRequestParams = (value) => TaskAugmentedRequestParamsSchema.safeParse(value).success;
var isInitializedNotification = (value) => InitializedNotificationSchema.safeParse(value).success;
function parseSchema(schema, data) {
  return safeParse2(schema, data);
}
function isTerminal(status) {
  return status === "completed" || status === "failed" || status === "cancelled";
}
function extractTaskManagerOptions(tasksCapability) {
  if (!tasksCapability) return void 0;
  const { taskStore, taskMessageQueue, defaultTaskPollInterval, maxTaskQueueSize } = tasksCapability;
  return {
    taskStore,
    taskMessageQueue,
    defaultTaskPollInterval,
    maxTaskQueueSize
  };
}
var TaskManager = class {
  _taskStore;
  _taskMessageQueue;
  _taskProgressTokens = /* @__PURE__ */ new Map();
  _requestResolvers = /* @__PURE__ */ new Map();
  _options;
  _host;
  constructor(options) {
    this._options = options;
    this._taskStore = options.taskStore;
    this._taskMessageQueue = options.taskMessageQueue;
  }
  bind(host) {
    this._host = host;
    if (this._taskStore) {
      host.registerHandler("tasks/get", async (request, ctx) => {
        const params = request.params;
        return { ...await this.handleGetTask(params.taskId, ctx.sessionId) };
      });
      host.registerHandler("tasks/result", async (request, ctx) => {
        const params = request.params;
        return await this.handleGetTaskPayload(params.taskId, ctx.sessionId, ctx.mcpReq.signal, async (message) => {
          await host.sendOnResponseStream(message, ctx.mcpReq.id);
        });
      });
      host.registerHandler("tasks/list", async (request, ctx) => {
        const params = request.params;
        return await this.handleListTasks(params?.cursor, ctx.sessionId);
      });
      host.registerHandler("tasks/cancel", async (request, ctx) => {
        const params = request.params;
        return await this.handleCancelTask(params.taskId, ctx.sessionId);
      });
    }
  }
  get _requireHost() {
    if (!this._host) throw new ProtocolError(ProtocolErrorCode.InternalError, "TaskManager is not bound to a Protocol host \u2014 call bind() first");
    return this._host;
  }
  get taskStore() {
    return this._taskStore;
  }
  get _requireTaskStore() {
    if (!this._taskStore) throw new ProtocolError(ProtocolErrorCode.InternalError, "TaskStore is not configured");
    return this._taskStore;
  }
  get taskMessageQueue() {
    return this._taskMessageQueue;
  }
  async *requestStream(request, resultSchema, options) {
    const host = this._requireHost;
    const { task } = options ?? {};
    if (!task) {
      try {
        yield {
          type: "result",
          result: await host.request(request, resultSchema, options)
        };
      } catch (error2) {
        yield {
          type: "error",
          error: error2 instanceof Error ? error2 : new Error(String(error2))
        };
      }
      return;
    }
    let taskId;
    try {
      const createResult = await host.request(request, CreateTaskResultSchema, options);
      if (createResult.task) {
        taskId = createResult.task.taskId;
        yield {
          type: "taskCreated",
          task: createResult.task
        };
      } else throw new ProtocolError(ProtocolErrorCode.InternalError, "Task creation did not return a task");
      while (true) {
        const task$1 = await this.getTask({ taskId }, options);
        yield {
          type: "taskStatus",
          task: task$1
        };
        if (isTerminal(task$1.status)) {
          switch (task$1.status) {
            case "completed":
              yield {
                type: "result",
                result: await this.getTaskResult({ taskId }, resultSchema, options)
              };
              break;
            case "failed":
              yield {
                type: "error",
                error: new ProtocolError(ProtocolErrorCode.InternalError, `Task ${taskId} failed`)
              };
              break;
            case "cancelled":
              yield {
                type: "error",
                error: new ProtocolError(ProtocolErrorCode.InternalError, `Task ${taskId} was cancelled`)
              };
              break;
          }
          return;
        }
        if (task$1.status === "input_required") {
          yield {
            type: "result",
            result: await this.getTaskResult({ taskId }, resultSchema, options)
          };
          return;
        }
        const pollInterval = task$1.pollInterval ?? this._options.defaultTaskPollInterval ?? 1e3;
        await new Promise((resolve) => setTimeout(resolve, pollInterval));
        options?.signal?.throwIfAborted();
      }
    } catch (error2) {
      yield {
        type: "error",
        error: error2 instanceof Error ? error2 : new Error(String(error2))
      };
    }
  }
  async getTask(params, options) {
    return this._requireHost.request({
      method: "tasks/get",
      params
    }, GetTaskResultSchema, options);
  }
  async getTaskResult(params, resultSchema, options) {
    return this._requireHost.request({
      method: "tasks/result",
      params
    }, resultSchema, options);
  }
  async listTasks(params, options) {
    return this._requireHost.request({
      method: "tasks/list",
      params
    }, ListTasksResultSchema, options);
  }
  async cancelTask(params, options) {
    return this._requireHost.request({
      method: "tasks/cancel",
      params
    }, CancelTaskResultSchema, options);
  }
  async handleGetTask(taskId, sessionId) {
    const task = await this._requireTaskStore.getTask(taskId, sessionId);
    if (!task) throw new ProtocolError(ProtocolErrorCode.InvalidParams, "Failed to retrieve task: Task not found");
    return task;
  }
  async handleGetTaskPayload(taskId, sessionId, signal, sendOnResponseStream) {
    const handleTaskResult = async () => {
      if (this._taskMessageQueue) {
        let queuedMessage;
        while (queuedMessage = await this._taskMessageQueue.dequeue(taskId, sessionId)) {
          if (queuedMessage.type === "response" || queuedMessage.type === "error") {
            const message = queuedMessage.message;
            const requestId = message.id;
            const resolver = this._requestResolvers.get(requestId);
            if (resolver) {
              this._requestResolvers.delete(requestId);
              if (queuedMessage.type === "response") resolver(message);
              else {
                const errorMessage = message;
                resolver(new ProtocolError(errorMessage.error.code, errorMessage.error.message, errorMessage.error.data));
              }
            } else {
              const messageType = queuedMessage.type === "response" ? "Response" : "Error";
              this._host?.reportError(/* @__PURE__ */ new Error(`${messageType} handler missing for request ${requestId}`));
            }
            continue;
          }
          await sendOnResponseStream(queuedMessage.message);
        }
      }
      const task = await this._requireTaskStore.getTask(taskId, sessionId);
      if (!task) throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Task not found: ${taskId}`);
      if (!isTerminal(task.status)) {
        await this._waitForTaskUpdate(task.pollInterval, signal);
        return await handleTaskResult();
      }
      const result = await this._requireTaskStore.getTaskResult(taskId, sessionId);
      await this._clearTaskQueue(taskId);
      return {
        ...result,
        _meta: {
          ...result._meta,
          [RELATED_TASK_META_KEY]: { taskId }
        }
      };
    };
    return await handleTaskResult();
  }
  async handleListTasks(cursor, sessionId) {
    try {
      const { tasks, nextCursor } = await this._requireTaskStore.listTasks(cursor, sessionId);
      return {
        tasks,
        nextCursor,
        _meta: {}
      };
    } catch (error2) {
      throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Failed to list tasks: ${error2 instanceof Error ? error2.message : String(error2)}`);
    }
  }
  async handleCancelTask(taskId, sessionId) {
    try {
      const task = await this._requireTaskStore.getTask(taskId, sessionId);
      if (!task) throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Task not found: ${taskId}`);
      if (isTerminal(task.status)) throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Cannot cancel task in terminal status: ${task.status}`);
      await this._requireTaskStore.updateTaskStatus(taskId, "cancelled", "Client cancelled task execution.", sessionId);
      await this._clearTaskQueue(taskId);
      const cancelledTask = await this._requireTaskStore.getTask(taskId, sessionId);
      if (!cancelledTask) throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Task not found after cancellation: ${taskId}`);
      return {
        _meta: {},
        ...cancelledTask
      };
    } catch (error2) {
      if (error2 instanceof ProtocolError) throw error2;
      throw new ProtocolError(ProtocolErrorCode.InvalidRequest, `Failed to cancel task: ${error2 instanceof Error ? error2.message : String(error2)}`);
    }
  }
  prepareOutboundRequest(jsonrpcRequest, options, messageId, responseHandler, onError) {
    const { task, relatedTask } = options ?? {};
    if (task) jsonrpcRequest.params = {
      ...jsonrpcRequest.params,
      task
    };
    if (relatedTask) jsonrpcRequest.params = {
      ...jsonrpcRequest.params,
      _meta: {
        ...jsonrpcRequest.params?._meta,
        [RELATED_TASK_META_KEY]: relatedTask
      }
    };
    const relatedTaskId = relatedTask?.taskId;
    if (relatedTaskId) {
      this._requestResolvers.set(messageId, responseHandler);
      this._enqueueTaskMessage(relatedTaskId, {
        type: "request",
        message: jsonrpcRequest,
        timestamp: Date.now()
      }).catch((error2) => {
        onError(error2);
      });
      return true;
    }
    return false;
  }
  extractInboundTaskContext(request, sessionId) {
    const relatedTaskId = request.params?._meta?.[RELATED_TASK_META_KEY]?.taskId;
    const taskCreationParams = isTaskAugmentedRequestParams(request.params) ? request.params.task : void 0;
    let taskContext;
    if (this._taskStore) taskContext = {
      id: relatedTaskId,
      store: this.createRequestTaskStore(request, sessionId),
      requestedTtl: taskCreationParams?.ttl
    };
    if (!relatedTaskId && !taskCreationParams && !taskContext) return {};
    return {
      relatedTaskId,
      taskCreationParams,
      taskContext
    };
  }
  wrapSendNotification(relatedTaskId, originalSendNotification) {
    return async (notification) => {
      await originalSendNotification(notification, { relatedTask: { taskId: relatedTaskId } });
    };
  }
  wrapSendRequest(relatedTaskId, taskStore, originalSendRequest) {
    return async (request, resultSchema, options) => {
      const requestOptions = { ...options };
      if (relatedTaskId && !requestOptions.relatedTask) requestOptions.relatedTask = { taskId: relatedTaskId };
      const effectiveTaskId = requestOptions.relatedTask?.taskId ?? relatedTaskId;
      if (effectiveTaskId && taskStore) await taskStore.updateTaskStatus(effectiveTaskId, "input_required");
      return await originalSendRequest(request, resultSchema, requestOptions);
    };
  }
  handleResponse(response) {
    const messageId = Number(response.id);
    const resolver = this._requestResolvers.get(messageId);
    if (resolver) {
      this._requestResolvers.delete(messageId);
      if (isJSONRPCResultResponse(response)) resolver(response);
      else resolver(new ProtocolError(response.error.code, response.error.message, response.error.data));
      return true;
    }
    return false;
  }
  shouldPreserveProgressHandler(response, messageId) {
    if (isJSONRPCResultResponse(response) && response.result && typeof response.result === "object") {
      const result = response.result;
      if (result.task && typeof result.task === "object") {
        const task = result.task;
        if (typeof task.taskId === "string") {
          this._taskProgressTokens.set(task.taskId, messageId);
          return true;
        }
      }
    }
    return false;
  }
  async routeNotification(notification, options) {
    const relatedTaskId = options?.relatedTask?.taskId;
    if (!relatedTaskId) return false;
    const jsonrpcNotification = {
      ...notification,
      jsonrpc: "2.0",
      params: {
        ...notification.params,
        _meta: {
          ...notification.params?._meta,
          [RELATED_TASK_META_KEY]: options.relatedTask
        }
      }
    };
    await this._enqueueTaskMessage(relatedTaskId, {
      type: "notification",
      message: jsonrpcNotification,
      timestamp: Date.now()
    });
    return true;
  }
  async routeResponse(relatedTaskId, message, sessionId) {
    if (!relatedTaskId || !this._taskMessageQueue) return false;
    await (isJSONRPCErrorResponse(message) ? this._enqueueTaskMessage(relatedTaskId, {
      type: "error",
      message,
      timestamp: Date.now()
    }, sessionId) : this._enqueueTaskMessage(relatedTaskId, {
      type: "response",
      message,
      timestamp: Date.now()
    }, sessionId));
    return true;
  }
  createRequestTaskStore(request, sessionId) {
    const taskStore = this._requireTaskStore;
    const host = this._host;
    return {
      createTask: async (taskParams) => {
        if (!request) throw new Error("No request provided");
        return await taskStore.createTask(taskParams, request.id, {
          method: request.method,
          params: request.params
        }, sessionId);
      },
      getTask: async (taskId) => {
        const task = await taskStore.getTask(taskId, sessionId);
        if (!task) throw new ProtocolError(ProtocolErrorCode.InvalidParams, "Failed to retrieve task: Task not found");
        return task;
      },
      storeTaskResult: async (taskId, status, result) => {
        await taskStore.storeTaskResult(taskId, status, result, sessionId);
        const task = await taskStore.getTask(taskId, sessionId);
        if (task) {
          const notification = TaskStatusNotificationSchema.parse({
            method: "notifications/tasks/status",
            params: task
          });
          await host?.notification(notification);
          if (isTerminal(task.status)) this._cleanupTaskProgressHandler(taskId);
        }
      },
      getTaskResult: (taskId) => taskStore.getTaskResult(taskId, sessionId),
      updateTaskStatus: async (taskId, status, statusMessage) => {
        const task = await taskStore.getTask(taskId, sessionId);
        if (!task) throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Task "${taskId}" not found - it may have been cleaned up`);
        if (isTerminal(task.status)) throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Cannot update task "${taskId}" from terminal status "${task.status}" to "${status}". Terminal states (completed, failed, cancelled) cannot transition to other states.`);
        await taskStore.updateTaskStatus(taskId, status, statusMessage, sessionId);
        const updatedTask = await taskStore.getTask(taskId, sessionId);
        if (updatedTask) {
          const notification = TaskStatusNotificationSchema.parse({
            method: "notifications/tasks/status",
            params: updatedTask
          });
          await host?.notification(notification);
          if (isTerminal(updatedTask.status)) this._cleanupTaskProgressHandler(taskId);
        }
      },
      listTasks: (cursor) => taskStore.listTasks(cursor, sessionId)
    };
  }
  processInboundRequest(request, ctx) {
    const taskInfo = this.extractInboundTaskContext(request, ctx.sessionId);
    const relatedTaskId = taskInfo?.relatedTaskId;
    const sendNotification = relatedTaskId ? this.wrapSendNotification(relatedTaskId, ctx.sendNotification) : (notification) => ctx.sendNotification(notification);
    const sendRequest = relatedTaskId ? this.wrapSendRequest(relatedTaskId, taskInfo?.taskContext?.store, ctx.sendRequest) : taskInfo?.taskContext ? this.wrapSendRequest("", taskInfo.taskContext.store, ctx.sendRequest) : ctx.sendRequest;
    const hasTaskCreationParams = !!taskInfo?.taskCreationParams;
    return {
      taskContext: taskInfo?.taskContext,
      sendNotification,
      sendRequest,
      routeResponse: async (message) => {
        if (relatedTaskId) return this.routeResponse(relatedTaskId, message, ctx.sessionId);
        return false;
      },
      hasTaskCreationParams,
      validateInbound: hasTaskCreationParams ? () => this._requireHost.assertTaskHandlerCapability(request.method) : void 0
    };
  }
  processOutboundRequest(jsonrpcRequest, options, messageId, responseHandler, onError) {
    if (this._requireHost.enforceStrictCapabilities && options?.task) this._requireHost.assertTaskCapability(jsonrpcRequest.method);
    return { queued: this.prepareOutboundRequest(jsonrpcRequest, options, messageId, responseHandler, onError) };
  }
  processInboundResponse(response, messageId) {
    if (this.handleResponse(response)) return {
      consumed: true,
      preserveProgress: false
    };
    return {
      consumed: false,
      preserveProgress: this.shouldPreserveProgressHandler(response, messageId)
    };
  }
  async processOutboundNotification(notification, options) {
    if (await this.routeNotification(notification, options)) return { queued: true };
    let jsonrpcNotification = {
      ...notification,
      jsonrpc: "2.0"
    };
    if (options?.relatedTask) jsonrpcNotification = {
      ...jsonrpcNotification,
      params: {
        ...jsonrpcNotification.params,
        _meta: {
          ...jsonrpcNotification.params?._meta,
          [RELATED_TASK_META_KEY]: options.relatedTask
        }
      }
    };
    return {
      queued: false,
      jsonrpcNotification
    };
  }
  onClose() {
    this._taskProgressTokens.clear();
    this._requestResolvers.clear();
  }
  async _enqueueTaskMessage(taskId, message, sessionId) {
    if (!this._taskStore || !this._taskMessageQueue) throw new Error("Cannot enqueue task message: taskStore and taskMessageQueue are not configured");
    await this._taskMessageQueue.enqueue(taskId, message, sessionId, this._options.maxTaskQueueSize);
  }
  async _clearTaskQueue(taskId, sessionId) {
    if (this._taskMessageQueue) {
      const messages = await this._taskMessageQueue.dequeueAll(taskId, sessionId);
      for (const message of messages) if (message.type === "request" && isJSONRPCRequest(message.message)) {
        const requestId = message.message.id;
        const resolver = this._requestResolvers.get(requestId);
        if (resolver) {
          resolver(new ProtocolError(ProtocolErrorCode.InternalError, "Task cancelled or completed"));
          this._requestResolvers.delete(requestId);
        } else this._host?.reportError(/* @__PURE__ */ new Error(`Resolver missing for request ${requestId} during task ${taskId} cleanup`));
      }
    }
  }
  async _waitForTaskUpdate(pollInterval, signal) {
    const interval = pollInterval ?? this._options.defaultTaskPollInterval ?? 1e3;
    return new Promise((resolve, reject) => {
      if (signal.aborted) {
        reject(new ProtocolError(ProtocolErrorCode.InvalidRequest, "Request cancelled"));
        return;
      }
      const timeoutId = setTimeout(resolve, interval);
      signal.addEventListener("abort", () => {
        clearTimeout(timeoutId);
        reject(new ProtocolError(ProtocolErrorCode.InvalidRequest, "Request cancelled"));
      }, { once: true });
    });
  }
  _cleanupTaskProgressHandler(taskId) {
    const progressToken = this._taskProgressTokens.get(taskId);
    if (progressToken !== void 0) {
      this._host?.removeProgressHandler(progressToken);
      this._taskProgressTokens.delete(taskId);
    }
  }
};
var NullTaskManager = class extends TaskManager {
  constructor() {
    super({});
  }
  processInboundRequest(request, ctx) {
    const hasTaskCreationParams = isTaskAugmentedRequestParams(request.params) && !!request.params.task;
    return {
      taskContext: void 0,
      sendNotification: (notification) => ctx.sendNotification(notification),
      sendRequest: ctx.sendRequest,
      routeResponse: async () => false,
      hasTaskCreationParams,
      validateInbound: hasTaskCreationParams ? () => this._requireHost.assertTaskHandlerCapability(request.method) : void 0
    };
  }
  async processOutboundNotification(notification, _options) {
    return {
      queued: false,
      jsonrpcNotification: {
        ...notification,
        jsonrpc: "2.0"
      }
    };
  }
};
var DEFAULT_REQUEST_TIMEOUT_MSEC = 6e4;
var Protocol = class {
  _transport;
  _requestMessageId = 0;
  _requestHandlers = /* @__PURE__ */ new Map();
  _requestHandlerAbortControllers = /* @__PURE__ */ new Map();
  _notificationHandlers = /* @__PURE__ */ new Map();
  _responseHandlers = /* @__PURE__ */ new Map();
  _progressHandlers = /* @__PURE__ */ new Map();
  _timeoutInfo = /* @__PURE__ */ new Map();
  _pendingDebouncedNotifications = /* @__PURE__ */ new Set();
  _taskManager;
  _supportedProtocolVersions;
  /**
  * Callback for when the connection is closed for any reason.
  *
  * This is invoked when {@linkcode Protocol.close | close()} is called as well.
  */
  onclose;
  /**
  * Callback for when an error occurs.
  *
  * Note that errors are not necessarily fatal; they are used for reporting any kind of exceptional condition out of band.
  */
  onerror;
  /**
  * A handler to invoke for any request types that do not have their own handler installed.
  */
  fallbackRequestHandler;
  /**
  * A handler to invoke for any notification types that do not have their own handler installed.
  */
  fallbackNotificationHandler;
  constructor(_options) {
    this._options = _options;
    this._supportedProtocolVersions = _options?.supportedProtocolVersions ?? SUPPORTED_PROTOCOL_VERSIONS;
    this._taskManager = _options?.tasks ? new TaskManager(_options.tasks) : new NullTaskManager();
    this._bindTaskManager();
    this.setNotificationHandler("notifications/cancelled", (notification) => {
      this._oncancel(notification);
    });
    this.setNotificationHandler("notifications/progress", (notification) => {
      this._onprogress(notification);
    });
    this.setRequestHandler("ping", (_request) => ({}));
  }
  /**
  * Access the TaskManager for task orchestration.
  * Always available; returns a NullTaskManager when no task store is configured.
  */
  get taskManager() {
    return this._taskManager;
  }
  _bindTaskManager() {
    const taskManager = this._taskManager;
    const host = {
      request: (request, resultSchema, options) => this._requestWithSchema(request, resultSchema, options),
      notification: (notification, options) => this.notification(notification, options),
      reportError: (error2) => this._onerror(error2),
      removeProgressHandler: (token) => this._progressHandlers.delete(token),
      registerHandler: (method, handler) => {
        const schema = getRequestSchema(method);
        this._requestHandlers.set(method, (request, ctx) => {
          schema.parse(request);
          return handler(request, ctx);
        });
      },
      sendOnResponseStream: async (message, relatedRequestId) => {
        await this._transport?.send(message, { relatedRequestId });
      },
      enforceStrictCapabilities: this._options?.enforceStrictCapabilities === true,
      assertTaskCapability: (method) => this.assertTaskCapability(method),
      assertTaskHandlerCapability: (method) => this.assertTaskHandlerCapability(method)
    };
    taskManager.bind(host);
  }
  async _oncancel(notification) {
    if (!notification.params.requestId) return;
    this._requestHandlerAbortControllers.get(notification.params.requestId)?.abort(notification.params.reason);
  }
  _setupTimeout(messageId, timeout, maxTotalTimeout, onTimeout, resetTimeoutOnProgress = false) {
    this._timeoutInfo.set(messageId, {
      timeoutId: setTimeout(onTimeout, timeout),
      startTime: Date.now(),
      timeout,
      maxTotalTimeout,
      resetTimeoutOnProgress,
      onTimeout
    });
  }
  _resetTimeout(messageId) {
    const info = this._timeoutInfo.get(messageId);
    if (!info) return false;
    const totalElapsed = Date.now() - info.startTime;
    if (info.maxTotalTimeout && totalElapsed >= info.maxTotalTimeout) {
      this._timeoutInfo.delete(messageId);
      throw new SdkError(SdkErrorCode.RequestTimeout, "Maximum total timeout exceeded", {
        maxTotalTimeout: info.maxTotalTimeout,
        totalElapsed
      });
    }
    clearTimeout(info.timeoutId);
    info.timeoutId = setTimeout(info.onTimeout, info.timeout);
    return true;
  }
  _cleanupTimeout(messageId) {
    const info = this._timeoutInfo.get(messageId);
    if (info) {
      clearTimeout(info.timeoutId);
      this._timeoutInfo.delete(messageId);
    }
  }
  /**
  * Attaches to the given transport, starts it, and starts listening for messages.
  *
  * The caller assumes ownership of the {@linkcode Transport}, replacing any callbacks that have already been set, and expects that it is the only user of the {@linkcode Transport} instance going forward.
  */
  async connect(transport) {
    this._transport = transport;
    const _onclose = this.transport?.onclose;
    this._transport.onclose = () => {
      try {
        _onclose?.();
      } finally {
        this._onclose();
      }
    };
    const _onerror = this.transport?.onerror;
    this._transport.onerror = (error2) => {
      _onerror?.(error2);
      this._onerror(error2);
    };
    const _onmessage = this._transport?.onmessage;
    this._transport.onmessage = (message, extra) => {
      _onmessage?.(message, extra);
      if (isJSONRPCResultResponse(message) || isJSONRPCErrorResponse(message)) this._onresponse(message);
      else if (isJSONRPCRequest(message)) this._onrequest(message, extra);
      else if (isJSONRPCNotification(message)) this._onnotification(message);
      else this._onerror(/* @__PURE__ */ new Error(`Unknown message type: ${JSON.stringify(message)}`));
    };
    transport.setSupportedProtocolVersions?.(this._supportedProtocolVersions);
    await this._transport.start();
  }
  _onclose() {
    const responseHandlers = this._responseHandlers;
    this._responseHandlers = /* @__PURE__ */ new Map();
    this._progressHandlers.clear();
    this._taskManager.onClose();
    this._pendingDebouncedNotifications.clear();
    for (const info of this._timeoutInfo.values()) clearTimeout(info.timeoutId);
    this._timeoutInfo.clear();
    const requestHandlerAbortControllers = this._requestHandlerAbortControllers;
    this._requestHandlerAbortControllers = /* @__PURE__ */ new Map();
    const error2 = new SdkError(SdkErrorCode.ConnectionClosed, "Connection closed");
    this._transport = void 0;
    try {
      this.onclose?.();
    } finally {
      for (const handler of responseHandlers.values()) handler(error2);
      for (const controller of requestHandlerAbortControllers.values()) controller.abort(error2);
    }
  }
  _onerror(error2) {
    this.onerror?.(error2);
  }
  _onnotification(notification) {
    const handler = this._notificationHandlers.get(notification.method) ?? this.fallbackNotificationHandler;
    if (handler === void 0) return;
    Promise.resolve().then(() => handler(notification)).catch((error2) => this._onerror(/* @__PURE__ */ new Error(`Uncaught error in notification handler: ${error2}`)));
  }
  _onrequest(request, extra) {
    const handler = this._requestHandlers.get(request.method) ?? this.fallbackRequestHandler;
    const capturedTransport = this._transport;
    const inboundCtx = {
      sessionId: capturedTransport?.sessionId,
      sendNotification: (notification, options) => this.notification(notification, {
        ...options,
        relatedRequestId: request.id
      }),
      sendRequest: (r, resultSchema, options) => this._requestWithSchema(r, resultSchema, {
        ...options,
        relatedRequestId: request.id
      })
    };
    const taskResult = this._taskManager.processInboundRequest(request, inboundCtx);
    const sendNotification = taskResult.sendNotification;
    const sendRequest = taskResult.sendRequest;
    const taskContext = taskResult.taskContext;
    const routeResponse = taskResult.routeResponse;
    const validators = [];
    if (taskResult.validateInbound) validators.push(taskResult.validateInbound);
    if (handler === void 0) {
      const errorResponse = {
        jsonrpc: "2.0",
        id: request.id,
        error: {
          code: ProtocolErrorCode.MethodNotFound,
          message: "Method not found"
        }
      };
      routeResponse(errorResponse).then((routed) => {
        if (!routed) capturedTransport?.send(errorResponse).catch((error2) => this._onerror(/* @__PURE__ */ new Error(`Failed to send an error response: ${error2}`)));
      }).catch((error2) => this._onerror(/* @__PURE__ */ new Error(`Failed to enqueue error response: ${error2}`)));
      return;
    }
    const abortController = new AbortController();
    this._requestHandlerAbortControllers.set(request.id, abortController);
    const baseCtx = {
      sessionId: capturedTransport?.sessionId,
      mcpReq: {
        id: request.id,
        method: request.method,
        _meta: request.params?._meta,
        signal: abortController.signal,
        send: (r, options) => {
          return sendRequest(r, getResultSchema(r.method), options);
        },
        notify: sendNotification
      },
      http: extra?.authInfo ? { authInfo: extra.authInfo } : void 0,
      task: taskContext
    };
    const ctx = this.buildContext(baseCtx, extra);
    Promise.resolve().then(() => {
      for (const validate2 of validators) validate2();
    }).then(() => handler(request, ctx)).then(async (result) => {
      if (abortController.signal.aborted) return;
      const response = {
        result,
        jsonrpc: "2.0",
        id: request.id
      };
      if (!await routeResponse(response)) await capturedTransport?.send(response);
    }, async (error2) => {
      if (abortController.signal.aborted) return;
      const errorResponse = {
        jsonrpc: "2.0",
        id: request.id,
        error: {
          code: Number.isSafeInteger(error2["code"]) ? error2["code"] : ProtocolErrorCode.InternalError,
          message: error2.message ?? "Internal error",
          ...error2["data"] !== void 0 && { data: error2["data"] }
        }
      };
      if (!await routeResponse(errorResponse)) await capturedTransport?.send(errorResponse);
    }).catch((error2) => this._onerror(/* @__PURE__ */ new Error(`Failed to send response: ${error2}`))).finally(() => {
      if (this._requestHandlerAbortControllers.get(request.id) === abortController) this._requestHandlerAbortControllers.delete(request.id);
    });
  }
  _onprogress(notification) {
    const { progressToken, ...params } = notification.params;
    const messageId = Number(progressToken);
    const handler = this._progressHandlers.get(messageId);
    if (!handler) {
      this._onerror(/* @__PURE__ */ new Error(`Received a progress notification for an unknown token: ${JSON.stringify(notification)}`));
      return;
    }
    const responseHandler = this._responseHandlers.get(messageId);
    const timeoutInfo = this._timeoutInfo.get(messageId);
    if (timeoutInfo && responseHandler && timeoutInfo.resetTimeoutOnProgress) try {
      this._resetTimeout(messageId);
    } catch (error2) {
      this._responseHandlers.delete(messageId);
      this._progressHandlers.delete(messageId);
      this._cleanupTimeout(messageId);
      responseHandler(error2);
      return;
    }
    handler(params);
  }
  _onresponse(response) {
    const messageId = Number(response.id);
    const taskResult = this._taskManager.processInboundResponse(response, messageId);
    if (taskResult.consumed) return;
    const preserveProgress = taskResult.preserveProgress;
    const handler = this._responseHandlers.get(messageId);
    if (handler === void 0) {
      this._onerror(/* @__PURE__ */ new Error(`Received a response for an unknown message ID: ${JSON.stringify(response)}`));
      return;
    }
    this._responseHandlers.delete(messageId);
    this._cleanupTimeout(messageId);
    if (!preserveProgress) this._progressHandlers.delete(messageId);
    if (isJSONRPCResultResponse(response)) handler(response);
    else handler(ProtocolError.fromError(response.error.code, response.error.message, response.error.data));
  }
  get transport() {
    return this._transport;
  }
  /**
  * Closes the connection.
  */
  async close() {
    await this._transport?.close();
  }
  /**
  * Sends a request and waits for a response, resolving the result schema
  * automatically from the method name.
  *
  * Do not use this method to emit notifications! Use {@linkcode Protocol.notification | notification()} instead.
  */
  request(request, options) {
    const resultSchema = getResultSchema(request.method);
    return this._requestWithSchema(request, resultSchema, options);
  }
  /**
  * Sends a request and waits for a response, using the provided schema for validation.
  *
  * This is the internal implementation used by SDK methods that need to specify
  * a particular result schema (e.g., for compatibility or task-specific schemas).
  */
  _requestWithSchema(request, resultSchema, options) {
    const { relatedRequestId, resumptionToken, onresumptiontoken } = options ?? {};
    let onAbort;
    let cleanupMessageId;
    return new Promise((resolve, reject) => {
      const earlyReject = (error2) => {
        reject(error2);
      };
      if (!this._transport) {
        earlyReject(/* @__PURE__ */ new Error("Not connected"));
        return;
      }
      if (this._options?.enforceStrictCapabilities === true) try {
        this.assertCapabilityForMethod(request.method);
      } catch (error2) {
        earlyReject(error2);
        return;
      }
      options?.signal?.throwIfAborted();
      const messageId = this._requestMessageId++;
      cleanupMessageId = messageId;
      const jsonrpcRequest = {
        ...request,
        jsonrpc: "2.0",
        id: messageId
      };
      if (options?.onprogress) {
        this._progressHandlers.set(messageId, options.onprogress);
        jsonrpcRequest.params = {
          ...request.params,
          _meta: {
            ...request.params?._meta,
            progressToken: messageId
          }
        };
      }
      const cancel = (reason) => {
        this._progressHandlers.delete(messageId);
        this._transport?.send({
          jsonrpc: "2.0",
          method: "notifications/cancelled",
          params: {
            requestId: messageId,
            reason: String(reason)
          }
        }, {
          relatedRequestId,
          resumptionToken,
          onresumptiontoken
        }).catch((error2) => this._onerror(/* @__PURE__ */ new Error(`Failed to send cancellation: ${error2}`)));
        reject(reason instanceof SdkError ? reason : new SdkError(SdkErrorCode.RequestTimeout, String(reason)));
      };
      this._responseHandlers.set(messageId, (response) => {
        if (options?.signal?.aborted) return;
        if (response instanceof Error) return reject(response);
        try {
          const parseResult = parseSchema(resultSchema, response.result);
          if (parseResult.success) resolve(parseResult.data);
          else reject(parseResult.error);
        } catch (error2) {
          reject(error2);
        }
      });
      onAbort = () => cancel(options?.signal?.reason);
      options?.signal?.addEventListener("abort", onAbort, { once: true });
      const timeout = options?.timeout ?? DEFAULT_REQUEST_TIMEOUT_MSEC;
      const timeoutHandler = () => cancel(new SdkError(SdkErrorCode.RequestTimeout, "Request timed out", { timeout }));
      this._setupTimeout(messageId, timeout, options?.maxTotalTimeout, timeoutHandler, options?.resetTimeoutOnProgress ?? false);
      const responseHandler = (response) => {
        const handler = this._responseHandlers.get(messageId);
        if (handler) handler(response);
        else this._onerror(/* @__PURE__ */ new Error(`Response handler missing for side-channeled request ${messageId}`));
      };
      let outboundQueued = false;
      try {
        if (this._taskManager.processOutboundRequest(jsonrpcRequest, options, messageId, responseHandler, (error2) => {
          this._progressHandlers.delete(messageId);
          reject(error2);
        }).queued) outboundQueued = true;
      } catch (error2) {
        this._progressHandlers.delete(messageId);
        reject(error2);
        return;
      }
      if (!outboundQueued) this._transport.send(jsonrpcRequest, {
        relatedRequestId,
        resumptionToken,
        onresumptiontoken
      }).catch((error2) => {
        this._progressHandlers.delete(messageId);
        reject(error2);
      });
    }).finally(() => {
      if (onAbort) options?.signal?.removeEventListener("abort", onAbort);
      if (cleanupMessageId !== void 0) {
        this._responseHandlers.delete(cleanupMessageId);
        this._cleanupTimeout(cleanupMessageId);
      }
    });
  }
  /**
  * Emits a notification, which is a one-way message that does not expect a response.
  */
  async notification(notification, options) {
    if (!this._transport) throw new SdkError(SdkErrorCode.NotConnected, "Not connected");
    this.assertNotificationCapability(notification.method);
    const taskResult = await this._taskManager.processOutboundNotification(notification, options);
    const queued = taskResult.queued;
    const jsonrpcNotification = taskResult.queued ? void 0 : taskResult.jsonrpcNotification;
    if (queued) return;
    if ((this._options?.debouncedNotificationMethods ?? []).includes(notification.method) && !notification.params && !options?.relatedRequestId && !options?.relatedTask) {
      if (this._pendingDebouncedNotifications.has(notification.method)) return;
      this._pendingDebouncedNotifications.add(notification.method);
      Promise.resolve().then(() => {
        this._pendingDebouncedNotifications.delete(notification.method);
        if (!this._transport) return;
        this._transport?.send(jsonrpcNotification, options).catch((error2) => this._onerror(error2));
      });
      return;
    }
    await this._transport.send(jsonrpcNotification, options);
  }
  /**
  * Registers a handler to invoke when this protocol object receives a request with the given method.
  *
  * Note that this will replace any previous request handler for the same method.
  */
  setRequestHandler(method, handler) {
    this.assertRequestHandlerCapability(method);
    const schema = getRequestSchema(method);
    this._requestHandlers.set(method, (request, ctx) => {
      const parsed = schema.parse(request);
      return Promise.resolve(handler(parsed, ctx));
    });
  }
  /**
  * Removes the request handler for the given method.
  */
  removeRequestHandler(method) {
    this._requestHandlers.delete(method);
  }
  /**
  * Asserts that a request handler has not already been set for the given method, in preparation for a new one being automatically installed.
  */
  assertCanSetRequestHandler(method) {
    if (this._requestHandlers.has(method)) throw new Error(`A request handler for ${method} already exists, which would be overridden`);
  }
  /**
  * Registers a handler to invoke when this protocol object receives a notification with the given method.
  *
  * Note that this will replace any previous notification handler for the same method.
  */
  setNotificationHandler(method, handler) {
    const schema = getNotificationSchema(method);
    this._notificationHandlers.set(method, (notification) => {
      const parsed = schema.parse(notification);
      return Promise.resolve(handler(parsed));
    });
  }
  /**
  * Removes the notification handler for the given method.
  */
  removeNotificationHandler(method) {
    this._notificationHandlers.delete(method);
  }
};
function isPlainObject3(value) {
  return value !== null && typeof value === "object" && !Array.isArray(value);
}
function mergeCapabilities(base, additional) {
  const result = { ...base };
  for (const key in additional) {
    const k = key;
    const addValue = additional[k];
    if (addValue === void 0) continue;
    const baseValue = result[k];
    result[k] = isPlainObject3(baseValue) && isPlainObject3(addValue) ? {
      ...baseValue,
      ...addValue
    } : addValue;
  }
  return result;
}
var ReadBuffer = class {
  _buffer;
  append(chunk) {
    this._buffer = this._buffer ? Buffer.concat([this._buffer, chunk]) : chunk;
  }
  readMessage() {
    while (this._buffer) {
      const index = this._buffer.indexOf("\n");
      if (index === -1) return null;
      const line = this._buffer.toString("utf8", 0, index).replace(/\r$/, "");
      this._buffer = this._buffer.subarray(index + 1);
      try {
        return deserializeMessage(line);
      } catch (error2) {
        if (error2 instanceof SyntaxError) continue;
        throw error2;
      }
    }
    return null;
  }
  clear() {
    this._buffer = void 0;
  }
};
function deserializeMessage(line) {
  return JSONRPCMessageSchema.parse(JSON.parse(line));
}
function serializeMessage(message) {
  return JSON.stringify(message) + "\n";
}
function normalizeHeaders(headers) {
  if (!headers) return {};
  if (headers instanceof Headers) return Object.fromEntries(headers.entries());
  if (Array.isArray(headers)) return Object.fromEntries(headers);
  return { ...headers };
}
function createFetchWithInit(baseFetch = fetch, baseInit) {
  if (!baseInit) return baseFetch;
  return async (url2, init) => {
    return baseFetch(url2, {
      ...baseInit,
      ...init,
      headers: init?.headers ? {
        ...normalizeHeaders(baseInit.headers),
        ...normalizeHeaders(init.headers)
      } : baseInit.headers
    });
  };
}
var require_code$1 = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.regexpCode = exports2.getEsmExportName = exports2.getProperty = exports2.safeStringify = exports2.stringify = exports2.strConcat = exports2.addCodeArg = exports2.str = exports2._ = exports2.nil = exports2._Code = exports2.Name = exports2.IDENTIFIER = exports2._CodeOrName = void 0;
  var _CodeOrName = class {
  };
  exports2._CodeOrName = _CodeOrName;
  exports2.IDENTIFIER = /^[a-z$_][a-z$_0-9]*$/i;
  var Name = class extends _CodeOrName {
    constructor(s) {
      super();
      if (!exports2.IDENTIFIER.test(s)) throw new Error("CodeGen: name must be a valid identifier");
      this.str = s;
    }
    toString() {
      return this.str;
    }
    emptyStr() {
      return false;
    }
    get names() {
      return { [this.str]: 1 };
    }
  };
  exports2.Name = Name;
  var _Code = class extends _CodeOrName {
    constructor(code) {
      super();
      this._items = typeof code === "string" ? [code] : code;
    }
    toString() {
      return this.str;
    }
    emptyStr() {
      if (this._items.length > 1) return false;
      const item = this._items[0];
      return item === "" || item === '""';
    }
    get str() {
      var _a2;
      return (_a2 = this._str) !== null && _a2 !== void 0 ? _a2 : this._str = this._items.reduce((s, c) => `${s}${c}`, "");
    }
    get names() {
      var _a2;
      return (_a2 = this._names) !== null && _a2 !== void 0 ? _a2 : this._names = this._items.reduce((names, c) => {
        if (c instanceof Name) names[c.str] = (names[c.str] || 0) + 1;
        return names;
      }, {});
    }
  };
  exports2._Code = _Code;
  exports2.nil = new _Code("");
  function _(strs, ...args) {
    const code = [strs[0]];
    let i = 0;
    while (i < args.length) {
      addCodeArg(code, args[i]);
      code.push(strs[++i]);
    }
    return new _Code(code);
  }
  exports2._ = _;
  const plus = new _Code("+");
  function str(strs, ...args) {
    const expr = [safeStringify(strs[0])];
    let i = 0;
    while (i < args.length) {
      expr.push(plus);
      addCodeArg(expr, args[i]);
      expr.push(plus, safeStringify(strs[++i]));
    }
    optimize(expr);
    return new _Code(expr);
  }
  exports2.str = str;
  function addCodeArg(code, arg) {
    if (arg instanceof _Code) code.push(...arg._items);
    else if (arg instanceof Name) code.push(arg);
    else code.push(interpolate(arg));
  }
  exports2.addCodeArg = addCodeArg;
  function optimize(expr) {
    let i = 1;
    while (i < expr.length - 1) {
      if (expr[i] === plus) {
        const res = mergeExprItems(expr[i - 1], expr[i + 1]);
        if (res !== void 0) {
          expr.splice(i - 1, 3, res);
          continue;
        }
        expr[i++] = "+";
      }
      i++;
    }
  }
  function mergeExprItems(a, b) {
    if (b === '""') return a;
    if (a === '""') return b;
    if (typeof a == "string") {
      if (b instanceof Name || a[a.length - 1] !== '"') return;
      if (typeof b != "string") return `${a.slice(0, -1)}${b}"`;
      if (b[0] === '"') return a.slice(0, -1) + b.slice(1);
      return;
    }
    if (typeof b == "string" && b[0] === '"' && !(a instanceof Name)) return `"${a}${b.slice(1)}`;
  }
  function strConcat(c1, c2) {
    return c2.emptyStr() ? c1 : c1.emptyStr() ? c2 : str`${c1}${c2}`;
  }
  exports2.strConcat = strConcat;
  function interpolate(x) {
    return typeof x == "number" || typeof x == "boolean" || x === null ? x : safeStringify(Array.isArray(x) ? x.join(",") : x);
  }
  function stringify(x) {
    return new _Code(safeStringify(x));
  }
  exports2.stringify = stringify;
  function safeStringify(x) {
    return JSON.stringify(x).replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
  }
  exports2.safeStringify = safeStringify;
  function getProperty(key) {
    return typeof key == "string" && exports2.IDENTIFIER.test(key) ? new _Code(`.${key}`) : _`[${key}]`;
  }
  exports2.getProperty = getProperty;
  function getEsmExportName(key) {
    if (typeof key == "string" && exports2.IDENTIFIER.test(key)) return new _Code(`${key}`);
    throw new Error(`CodeGen: invalid export name: ${key}, use explicit $id name mapping`);
  }
  exports2.getEsmExportName = getEsmExportName;
  function regexpCode(rx) {
    return new _Code(rx.toString());
  }
  exports2.regexpCode = regexpCode;
}));
var require_scope = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.ValueScope = exports2.ValueScopeName = exports2.Scope = exports2.varKinds = exports2.UsedValueState = void 0;
  const code_1 = require_code$1();
  var ValueError = class extends Error {
    constructor(name) {
      super(`CodeGen: "code" for ${name} not defined`);
      this.value = name.value;
    }
  };
  var UsedValueState;
  (function(UsedValueState2) {
    UsedValueState2[UsedValueState2["Started"] = 0] = "Started";
    UsedValueState2[UsedValueState2["Completed"] = 1] = "Completed";
  })(UsedValueState || (exports2.UsedValueState = UsedValueState = {}));
  exports2.varKinds = {
    const: new code_1.Name("const"),
    let: new code_1.Name("let"),
    var: new code_1.Name("var")
  };
  var Scope = class {
    constructor({ prefixes, parent } = {}) {
      this._names = {};
      this._prefixes = prefixes;
      this._parent = parent;
    }
    toName(nameOrPrefix) {
      return nameOrPrefix instanceof code_1.Name ? nameOrPrefix : this.name(nameOrPrefix);
    }
    name(prefix) {
      return new code_1.Name(this._newName(prefix));
    }
    _newName(prefix) {
      const ng = this._names[prefix] || this._nameGroup(prefix);
      return `${prefix}${ng.index++}`;
    }
    _nameGroup(prefix) {
      var _a2, _b;
      if (((_b = (_a2 = this._parent) === null || _a2 === void 0 ? void 0 : _a2._prefixes) === null || _b === void 0 ? void 0 : _b.has(prefix)) || this._prefixes && !this._prefixes.has(prefix)) throw new Error(`CodeGen: prefix "${prefix}" is not allowed in this scope`);
      return this._names[prefix] = {
        prefix,
        index: 0
      };
    }
  };
  exports2.Scope = Scope;
  var ValueScopeName = class extends code_1.Name {
    constructor(prefix, nameStr) {
      super(nameStr);
      this.prefix = prefix;
    }
    setValue(value, { property, itemIndex }) {
      this.value = value;
      this.scopePath = (0, code_1._)`.${new code_1.Name(property)}[${itemIndex}]`;
    }
  };
  exports2.ValueScopeName = ValueScopeName;
  const line = (0, code_1._)`\n`;
  var ValueScope = class extends Scope {
    constructor(opts) {
      super(opts);
      this._values = {};
      this._scope = opts.scope;
      this.opts = {
        ...opts,
        _n: opts.lines ? line : code_1.nil
      };
    }
    get() {
      return this._scope;
    }
    name(prefix) {
      return new ValueScopeName(prefix, this._newName(prefix));
    }
    value(nameOrPrefix, value) {
      var _a2;
      if (value.ref === void 0) throw new Error("CodeGen: ref must be passed in value");
      const name = this.toName(nameOrPrefix);
      const { prefix } = name;
      const valueKey = (_a2 = value.key) !== null && _a2 !== void 0 ? _a2 : value.ref;
      let vs = this._values[prefix];
      if (vs) {
        const _name = vs.get(valueKey);
        if (_name) return _name;
      } else vs = this._values[prefix] = /* @__PURE__ */ new Map();
      vs.set(valueKey, name);
      const s = this._scope[prefix] || (this._scope[prefix] = []);
      const itemIndex = s.length;
      s[itemIndex] = value.ref;
      name.setValue(value, {
        property: prefix,
        itemIndex
      });
      return name;
    }
    getValue(prefix, keyOrRef) {
      const vs = this._values[prefix];
      if (!vs) return;
      return vs.get(keyOrRef);
    }
    scopeRefs(scopeName, values = this._values) {
      return this._reduceValues(values, (name) => {
        if (name.scopePath === void 0) throw new Error(`CodeGen: name "${name}" has no value`);
        return (0, code_1._)`${scopeName}${name.scopePath}`;
      });
    }
    scopeCode(values = this._values, usedValues, getCode) {
      return this._reduceValues(values, (name) => {
        if (name.value === void 0) throw new Error(`CodeGen: name "${name}" has no value`);
        return name.value.code;
      }, usedValues, getCode);
    }
    _reduceValues(values, valueCode, usedValues = {}, getCode) {
      let code = code_1.nil;
      for (const prefix in values) {
        const vs = values[prefix];
        if (!vs) continue;
        const nameSet = usedValues[prefix] = usedValues[prefix] || /* @__PURE__ */ new Map();
        vs.forEach((name) => {
          if (nameSet.has(name)) return;
          nameSet.set(name, UsedValueState.Started);
          let c = valueCode(name);
          if (c) {
            const def = this.opts.es5 ? exports2.varKinds.var : exports2.varKinds.const;
            code = (0, code_1._)`${code}${def} ${name} = ${c};${this.opts._n}`;
          } else if (c = getCode === null || getCode === void 0 ? void 0 : getCode(name)) code = (0, code_1._)`${code}${c}${this.opts._n}`;
          else throw new ValueError(name);
          nameSet.set(name, UsedValueState.Completed);
        });
      }
      return code;
    }
  };
  exports2.ValueScope = ValueScope;
}));
var require_codegen = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.or = exports2.and = exports2.not = exports2.CodeGen = exports2.operators = exports2.varKinds = exports2.ValueScopeName = exports2.ValueScope = exports2.Scope = exports2.Name = exports2.regexpCode = exports2.stringify = exports2.getProperty = exports2.nil = exports2.strConcat = exports2.str = exports2._ = void 0;
  const code_1 = require_code$1();
  const scope_1 = require_scope();
  var code_2 = require_code$1();
  Object.defineProperty(exports2, "_", {
    enumerable: true,
    get: function() {
      return code_2._;
    }
  });
  Object.defineProperty(exports2, "str", {
    enumerable: true,
    get: function() {
      return code_2.str;
    }
  });
  Object.defineProperty(exports2, "strConcat", {
    enumerable: true,
    get: function() {
      return code_2.strConcat;
    }
  });
  Object.defineProperty(exports2, "nil", {
    enumerable: true,
    get: function() {
      return code_2.nil;
    }
  });
  Object.defineProperty(exports2, "getProperty", {
    enumerable: true,
    get: function() {
      return code_2.getProperty;
    }
  });
  Object.defineProperty(exports2, "stringify", {
    enumerable: true,
    get: function() {
      return code_2.stringify;
    }
  });
  Object.defineProperty(exports2, "regexpCode", {
    enumerable: true,
    get: function() {
      return code_2.regexpCode;
    }
  });
  Object.defineProperty(exports2, "Name", {
    enumerable: true,
    get: function() {
      return code_2.Name;
    }
  });
  var scope_2 = require_scope();
  Object.defineProperty(exports2, "Scope", {
    enumerable: true,
    get: function() {
      return scope_2.Scope;
    }
  });
  Object.defineProperty(exports2, "ValueScope", {
    enumerable: true,
    get: function() {
      return scope_2.ValueScope;
    }
  });
  Object.defineProperty(exports2, "ValueScopeName", {
    enumerable: true,
    get: function() {
      return scope_2.ValueScopeName;
    }
  });
  Object.defineProperty(exports2, "varKinds", {
    enumerable: true,
    get: function() {
      return scope_2.varKinds;
    }
  });
  exports2.operators = {
    GT: new code_1._Code(">"),
    GTE: new code_1._Code(">="),
    LT: new code_1._Code("<"),
    LTE: new code_1._Code("<="),
    EQ: new code_1._Code("==="),
    NEQ: new code_1._Code("!=="),
    NOT: new code_1._Code("!"),
    OR: new code_1._Code("||"),
    AND: new code_1._Code("&&"),
    ADD: new code_1._Code("+")
  };
  var Node = class {
    optimizeNodes() {
      return this;
    }
    optimizeNames(_names, _constants) {
      return this;
    }
  };
  var Def = class extends Node {
    constructor(varKind, name, rhs) {
      super();
      this.varKind = varKind;
      this.name = name;
      this.rhs = rhs;
    }
    render({ es5, _n }) {
      const varKind = es5 ? scope_1.varKinds.var : this.varKind;
      const rhs = this.rhs === void 0 ? "" : ` = ${this.rhs}`;
      return `${varKind} ${this.name}${rhs};` + _n;
    }
    optimizeNames(names, constants) {
      if (!names[this.name.str]) return;
      if (this.rhs) this.rhs = optimizeExpr(this.rhs, names, constants);
      return this;
    }
    get names() {
      return this.rhs instanceof code_1._CodeOrName ? this.rhs.names : {};
    }
  };
  var Assign = class extends Node {
    constructor(lhs, rhs, sideEffects) {
      super();
      this.lhs = lhs;
      this.rhs = rhs;
      this.sideEffects = sideEffects;
    }
    render({ _n }) {
      return `${this.lhs} = ${this.rhs};` + _n;
    }
    optimizeNames(names, constants) {
      if (this.lhs instanceof code_1.Name && !names[this.lhs.str] && !this.sideEffects) return;
      this.rhs = optimizeExpr(this.rhs, names, constants);
      return this;
    }
    get names() {
      return addExprNames(this.lhs instanceof code_1.Name ? {} : { ...this.lhs.names }, this.rhs);
    }
  };
  var AssignOp = class extends Assign {
    constructor(lhs, op, rhs, sideEffects) {
      super(lhs, rhs, sideEffects);
      this.op = op;
    }
    render({ _n }) {
      return `${this.lhs} ${this.op}= ${this.rhs};` + _n;
    }
  };
  var Label = class extends Node {
    constructor(label) {
      super();
      this.label = label;
      this.names = {};
    }
    render({ _n }) {
      return `${this.label}:` + _n;
    }
  };
  var Break = class extends Node {
    constructor(label) {
      super();
      this.label = label;
      this.names = {};
    }
    render({ _n }) {
      return `break${this.label ? ` ${this.label}` : ""};` + _n;
    }
  };
  var Throw = class extends Node {
    constructor(error2) {
      super();
      this.error = error2;
    }
    render({ _n }) {
      return `throw ${this.error};` + _n;
    }
    get names() {
      return this.error.names;
    }
  };
  var AnyCode = class extends Node {
    constructor(code) {
      super();
      this.code = code;
    }
    render({ _n }) {
      return `${this.code};` + _n;
    }
    optimizeNodes() {
      return `${this.code}` ? this : void 0;
    }
    optimizeNames(names, constants) {
      this.code = optimizeExpr(this.code, names, constants);
      return this;
    }
    get names() {
      return this.code instanceof code_1._CodeOrName ? this.code.names : {};
    }
  };
  var ParentNode = class extends Node {
    constructor(nodes = []) {
      super();
      this.nodes = nodes;
    }
    render(opts) {
      return this.nodes.reduce((code, n) => code + n.render(opts), "");
    }
    optimizeNodes() {
      const { nodes } = this;
      let i = nodes.length;
      while (i--) {
        const n = nodes[i].optimizeNodes();
        if (Array.isArray(n)) nodes.splice(i, 1, ...n);
        else if (n) nodes[i] = n;
        else nodes.splice(i, 1);
      }
      return nodes.length > 0 ? this : void 0;
    }
    optimizeNames(names, constants) {
      const { nodes } = this;
      let i = nodes.length;
      while (i--) {
        const n = nodes[i];
        if (n.optimizeNames(names, constants)) continue;
        subtractNames(names, n.names);
        nodes.splice(i, 1);
      }
      return nodes.length > 0 ? this : void 0;
    }
    get names() {
      return this.nodes.reduce((names, n) => addNames(names, n.names), {});
    }
  };
  var BlockNode = class extends ParentNode {
    render(opts) {
      return "{" + opts._n + super.render(opts) + "}" + opts._n;
    }
  };
  var Root = class extends ParentNode {
  };
  var Else = class extends BlockNode {
  };
  Else.kind = "else";
  var If = class If2 extends BlockNode {
    constructor(condition, nodes) {
      super(nodes);
      this.condition = condition;
    }
    render(opts) {
      let code = `if(${this.condition})` + super.render(opts);
      if (this.else) code += "else " + this.else.render(opts);
      return code;
    }
    optimizeNodes() {
      super.optimizeNodes();
      const cond = this.condition;
      if (cond === true) return this.nodes;
      let e = this.else;
      if (e) {
        const ns = e.optimizeNodes();
        e = this.else = Array.isArray(ns) ? new Else(ns) : ns;
      }
      if (e) {
        if (cond === false) return e instanceof If2 ? e : e.nodes;
        if (this.nodes.length) return this;
        return new If2(not(cond), e instanceof If2 ? [e] : e.nodes);
      }
      if (cond === false || !this.nodes.length) return void 0;
      return this;
    }
    optimizeNames(names, constants) {
      var _a2;
      this.else = (_a2 = this.else) === null || _a2 === void 0 ? void 0 : _a2.optimizeNames(names, constants);
      if (!(super.optimizeNames(names, constants) || this.else)) return;
      this.condition = optimizeExpr(this.condition, names, constants);
      return this;
    }
    get names() {
      const names = super.names;
      addExprNames(names, this.condition);
      if (this.else) addNames(names, this.else.names);
      return names;
    }
  };
  If.kind = "if";
  var For = class extends BlockNode {
  };
  For.kind = "for";
  var ForLoop = class extends For {
    constructor(iteration) {
      super();
      this.iteration = iteration;
    }
    render(opts) {
      return `for(${this.iteration})` + super.render(opts);
    }
    optimizeNames(names, constants) {
      if (!super.optimizeNames(names, constants)) return;
      this.iteration = optimizeExpr(this.iteration, names, constants);
      return this;
    }
    get names() {
      return addNames(super.names, this.iteration.names);
    }
  };
  var ForRange = class extends For {
    constructor(varKind, name, from, to) {
      super();
      this.varKind = varKind;
      this.name = name;
      this.from = from;
      this.to = to;
    }
    render(opts) {
      const varKind = opts.es5 ? scope_1.varKinds.var : this.varKind;
      const { name, from, to } = this;
      return `for(${varKind} ${name}=${from}; ${name}<${to}; ${name}++)` + super.render(opts);
    }
    get names() {
      return addExprNames(addExprNames(super.names, this.from), this.to);
    }
  };
  var ForIter = class extends For {
    constructor(loop, varKind, name, iterable) {
      super();
      this.loop = loop;
      this.varKind = varKind;
      this.name = name;
      this.iterable = iterable;
    }
    render(opts) {
      return `for(${this.varKind} ${this.name} ${this.loop} ${this.iterable})` + super.render(opts);
    }
    optimizeNames(names, constants) {
      if (!super.optimizeNames(names, constants)) return;
      this.iterable = optimizeExpr(this.iterable, names, constants);
      return this;
    }
    get names() {
      return addNames(super.names, this.iterable.names);
    }
  };
  var Func = class extends BlockNode {
    constructor(name, args, async) {
      super();
      this.name = name;
      this.args = args;
      this.async = async;
    }
    render(opts) {
      return `${this.async ? "async " : ""}function ${this.name}(${this.args})` + super.render(opts);
    }
  };
  Func.kind = "func";
  var Return = class extends ParentNode {
    render(opts) {
      return "return " + super.render(opts);
    }
  };
  Return.kind = "return";
  var Try = class extends BlockNode {
    render(opts) {
      let code = "try" + super.render(opts);
      if (this.catch) code += this.catch.render(opts);
      if (this.finally) code += this.finally.render(opts);
      return code;
    }
    optimizeNodes() {
      var _a2, _b;
      super.optimizeNodes();
      (_a2 = this.catch) === null || _a2 === void 0 || _a2.optimizeNodes();
      (_b = this.finally) === null || _b === void 0 || _b.optimizeNodes();
      return this;
    }
    optimizeNames(names, constants) {
      var _a2, _b;
      super.optimizeNames(names, constants);
      (_a2 = this.catch) === null || _a2 === void 0 || _a2.optimizeNames(names, constants);
      (_b = this.finally) === null || _b === void 0 || _b.optimizeNames(names, constants);
      return this;
    }
    get names() {
      const names = super.names;
      if (this.catch) addNames(names, this.catch.names);
      if (this.finally) addNames(names, this.finally.names);
      return names;
    }
  };
  var Catch = class extends BlockNode {
    constructor(error2) {
      super();
      this.error = error2;
    }
    render(opts) {
      return `catch(${this.error})` + super.render(opts);
    }
  };
  Catch.kind = "catch";
  var Finally = class extends BlockNode {
    render(opts) {
      return "finally" + super.render(opts);
    }
  };
  Finally.kind = "finally";
  var CodeGen = class {
    constructor(extScope, opts = {}) {
      this._values = {};
      this._blockStarts = [];
      this._constants = {};
      this.opts = {
        ...opts,
        _n: opts.lines ? "\n" : ""
      };
      this._extScope = extScope;
      this._scope = new scope_1.Scope({ parent: extScope });
      this._nodes = [new Root()];
    }
    toString() {
      return this._root.render(this.opts);
    }
    name(prefix) {
      return this._scope.name(prefix);
    }
    scopeName(prefix) {
      return this._extScope.name(prefix);
    }
    scopeValue(prefixOrName, value) {
      const name = this._extScope.value(prefixOrName, value);
      (this._values[name.prefix] || (this._values[name.prefix] = /* @__PURE__ */ new Set())).add(name);
      return name;
    }
    getScopeValue(prefix, keyOrRef) {
      return this._extScope.getValue(prefix, keyOrRef);
    }
    scopeRefs(scopeName) {
      return this._extScope.scopeRefs(scopeName, this._values);
    }
    scopeCode() {
      return this._extScope.scopeCode(this._values);
    }
    _def(varKind, nameOrPrefix, rhs, constant) {
      const name = this._scope.toName(nameOrPrefix);
      if (rhs !== void 0 && constant) this._constants[name.str] = rhs;
      this._leafNode(new Def(varKind, name, rhs));
      return name;
    }
    const(nameOrPrefix, rhs, _constant) {
      return this._def(scope_1.varKinds.const, nameOrPrefix, rhs, _constant);
    }
    let(nameOrPrefix, rhs, _constant) {
      return this._def(scope_1.varKinds.let, nameOrPrefix, rhs, _constant);
    }
    var(nameOrPrefix, rhs, _constant) {
      return this._def(scope_1.varKinds.var, nameOrPrefix, rhs, _constant);
    }
    assign(lhs, rhs, sideEffects) {
      return this._leafNode(new Assign(lhs, rhs, sideEffects));
    }
    add(lhs, rhs) {
      return this._leafNode(new AssignOp(lhs, exports2.operators.ADD, rhs));
    }
    code(c) {
      if (typeof c == "function") c();
      else if (c !== code_1.nil) this._leafNode(new AnyCode(c));
      return this;
    }
    object(...keyValues) {
      const code = ["{"];
      for (const [key, value] of keyValues) {
        if (code.length > 1) code.push(",");
        code.push(key);
        if (key !== value || this.opts.es5) {
          code.push(":");
          (0, code_1.addCodeArg)(code, value);
        }
      }
      code.push("}");
      return new code_1._Code(code);
    }
    if(condition, thenBody, elseBody) {
      this._blockNode(new If(condition));
      if (thenBody && elseBody) this.code(thenBody).else().code(elseBody).endIf();
      else if (thenBody) this.code(thenBody).endIf();
      else if (elseBody) throw new Error('CodeGen: "else" body without "then" body');
      return this;
    }
    elseIf(condition) {
      return this._elseNode(new If(condition));
    }
    else() {
      return this._elseNode(new Else());
    }
    endIf() {
      return this._endBlockNode(If, Else);
    }
    _for(node, forBody) {
      this._blockNode(node);
      if (forBody) this.code(forBody).endFor();
      return this;
    }
    for(iteration, forBody) {
      return this._for(new ForLoop(iteration), forBody);
    }
    forRange(nameOrPrefix, from, to, forBody, varKind = this.opts.es5 ? scope_1.varKinds.var : scope_1.varKinds.let) {
      const name = this._scope.toName(nameOrPrefix);
      return this._for(new ForRange(varKind, name, from, to), () => forBody(name));
    }
    forOf(nameOrPrefix, iterable, forBody, varKind = scope_1.varKinds.const) {
      const name = this._scope.toName(nameOrPrefix);
      if (this.opts.es5) {
        const arr = iterable instanceof code_1.Name ? iterable : this.var("_arr", iterable);
        return this.forRange("_i", 0, (0, code_1._)`${arr}.length`, (i) => {
          this.var(name, (0, code_1._)`${arr}[${i}]`);
          forBody(name);
        });
      }
      return this._for(new ForIter("of", varKind, name, iterable), () => forBody(name));
    }
    forIn(nameOrPrefix, obj, forBody, varKind = this.opts.es5 ? scope_1.varKinds.var : scope_1.varKinds.const) {
      if (this.opts.ownProperties) return this.forOf(nameOrPrefix, (0, code_1._)`Object.keys(${obj})`, forBody);
      const name = this._scope.toName(nameOrPrefix);
      return this._for(new ForIter("in", varKind, name, obj), () => forBody(name));
    }
    endFor() {
      return this._endBlockNode(For);
    }
    label(label) {
      return this._leafNode(new Label(label));
    }
    break(label) {
      return this._leafNode(new Break(label));
    }
    return(value) {
      const node = new Return();
      this._blockNode(node);
      this.code(value);
      if (node.nodes.length !== 1) throw new Error('CodeGen: "return" should have one node');
      return this._endBlockNode(Return);
    }
    try(tryBody, catchCode, finallyCode) {
      if (!catchCode && !finallyCode) throw new Error('CodeGen: "try" without "catch" and "finally"');
      const node = new Try();
      this._blockNode(node);
      this.code(tryBody);
      if (catchCode) {
        const error2 = this.name("e");
        this._currNode = node.catch = new Catch(error2);
        catchCode(error2);
      }
      if (finallyCode) {
        this._currNode = node.finally = new Finally();
        this.code(finallyCode);
      }
      return this._endBlockNode(Catch, Finally);
    }
    throw(error2) {
      return this._leafNode(new Throw(error2));
    }
    block(body, nodeCount) {
      this._blockStarts.push(this._nodes.length);
      if (body) this.code(body).endBlock(nodeCount);
      return this;
    }
    endBlock(nodeCount) {
      const len = this._blockStarts.pop();
      if (len === void 0) throw new Error("CodeGen: not in self-balancing block");
      const toClose = this._nodes.length - len;
      if (toClose < 0 || nodeCount !== void 0 && toClose !== nodeCount) throw new Error(`CodeGen: wrong number of nodes: ${toClose} vs ${nodeCount} expected`);
      this._nodes.length = len;
      return this;
    }
    func(name, args = code_1.nil, async, funcBody) {
      this._blockNode(new Func(name, args, async));
      if (funcBody) this.code(funcBody).endFunc();
      return this;
    }
    endFunc() {
      return this._endBlockNode(Func);
    }
    optimize(n = 1) {
      while (n-- > 0) {
        this._root.optimizeNodes();
        this._root.optimizeNames(this._root.names, this._constants);
      }
    }
    _leafNode(node) {
      this._currNode.nodes.push(node);
      return this;
    }
    _blockNode(node) {
      this._currNode.nodes.push(node);
      this._nodes.push(node);
    }
    _endBlockNode(N1, N2) {
      const n = this._currNode;
      if (n instanceof N1 || N2 && n instanceof N2) {
        this._nodes.pop();
        return this;
      }
      throw new Error(`CodeGen: not in block "${N2 ? `${N1.kind}/${N2.kind}` : N1.kind}"`);
    }
    _elseNode(node) {
      const n = this._currNode;
      if (!(n instanceof If)) throw new Error('CodeGen: "else" without "if"');
      this._currNode = n.else = node;
      return this;
    }
    get _root() {
      return this._nodes[0];
    }
    get _currNode() {
      const ns = this._nodes;
      return ns[ns.length - 1];
    }
    set _currNode(node) {
      const ns = this._nodes;
      ns[ns.length - 1] = node;
    }
  };
  exports2.CodeGen = CodeGen;
  function addNames(names, from) {
    for (const n in from) names[n] = (names[n] || 0) + (from[n] || 0);
    return names;
  }
  function addExprNames(names, from) {
    return from instanceof code_1._CodeOrName ? addNames(names, from.names) : names;
  }
  function optimizeExpr(expr, names, constants) {
    if (expr instanceof code_1.Name) return replaceName(expr);
    if (!canOptimize(expr)) return expr;
    return new code_1._Code(expr._items.reduce((items, c) => {
      if (c instanceof code_1.Name) c = replaceName(c);
      if (c instanceof code_1._Code) items.push(...c._items);
      else items.push(c);
      return items;
    }, []));
    function replaceName(n) {
      const c = constants[n.str];
      if (c === void 0 || names[n.str] !== 1) return n;
      delete names[n.str];
      return c;
    }
    function canOptimize(e) {
      return e instanceof code_1._Code && e._items.some((c) => c instanceof code_1.Name && names[c.str] === 1 && constants[c.str] !== void 0);
    }
  }
  function subtractNames(names, from) {
    for (const n in from) names[n] = (names[n] || 0) - (from[n] || 0);
  }
  function not(x) {
    return typeof x == "boolean" || typeof x == "number" || x === null ? !x : (0, code_1._)`!${par(x)}`;
  }
  exports2.not = not;
  const andCode = mappend(exports2.operators.AND);
  function and(...args) {
    return args.reduce(andCode);
  }
  exports2.and = and;
  const orCode = mappend(exports2.operators.OR);
  function or(...args) {
    return args.reduce(orCode);
  }
  exports2.or = or;
  function mappend(op) {
    return (x, y) => x === code_1.nil ? y : y === code_1.nil ? x : (0, code_1._)`${par(x)} ${op} ${par(y)}`;
  }
  function par(x) {
    return x instanceof code_1.Name ? x : (0, code_1._)`(${x})`;
  }
}));
var require_util = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.checkStrictMode = exports2.getErrorPath = exports2.Type = exports2.useFunc = exports2.setEvaluated = exports2.evaluatedPropsToName = exports2.mergeEvaluated = exports2.eachItem = exports2.unescapeJsonPointer = exports2.escapeJsonPointer = exports2.escapeFragment = exports2.unescapeFragment = exports2.schemaRefOrVal = exports2.schemaHasRulesButRef = exports2.schemaHasRules = exports2.checkUnknownRules = exports2.alwaysValidSchema = exports2.toHash = void 0;
  const codegen_1 = require_codegen();
  const code_1 = require_code$1();
  function toHash(arr) {
    const hash2 = {};
    for (const item of arr) hash2[item] = true;
    return hash2;
  }
  exports2.toHash = toHash;
  function alwaysValidSchema(it, schema) {
    if (typeof schema == "boolean") return schema;
    if (Object.keys(schema).length === 0) return true;
    checkUnknownRules(it, schema);
    return !schemaHasRules(schema, it.self.RULES.all);
  }
  exports2.alwaysValidSchema = alwaysValidSchema;
  function checkUnknownRules(it, schema = it.schema) {
    const { opts, self: self2 } = it;
    if (!opts.strictSchema) return;
    if (typeof schema === "boolean") return;
    const rules = self2.RULES.keywords;
    for (const key in schema) if (!rules[key]) checkStrictMode(it, `unknown keyword: "${key}"`);
  }
  exports2.checkUnknownRules = checkUnknownRules;
  function schemaHasRules(schema, rules) {
    if (typeof schema == "boolean") return !schema;
    for (const key in schema) if (rules[key]) return true;
    return false;
  }
  exports2.schemaHasRules = schemaHasRules;
  function schemaHasRulesButRef(schema, RULES) {
    if (typeof schema == "boolean") return !schema;
    for (const key in schema) if (key !== "$ref" && RULES.all[key]) return true;
    return false;
  }
  exports2.schemaHasRulesButRef = schemaHasRulesButRef;
  function schemaRefOrVal({ topSchemaRef, schemaPath }, schema, keyword, $data) {
    if (!$data) {
      if (typeof schema == "number" || typeof schema == "boolean") return schema;
      if (typeof schema == "string") return (0, codegen_1._)`${schema}`;
    }
    return (0, codegen_1._)`${topSchemaRef}${schemaPath}${(0, codegen_1.getProperty)(keyword)}`;
  }
  exports2.schemaRefOrVal = schemaRefOrVal;
  function unescapeFragment(str) {
    return unescapeJsonPointer(decodeURIComponent(str));
  }
  exports2.unescapeFragment = unescapeFragment;
  function escapeFragment(str) {
    return encodeURIComponent(escapeJsonPointer(str));
  }
  exports2.escapeFragment = escapeFragment;
  function escapeJsonPointer(str) {
    if (typeof str == "number") return `${str}`;
    return str.replace(/~/g, "~0").replace(/\//g, "~1");
  }
  exports2.escapeJsonPointer = escapeJsonPointer;
  function unescapeJsonPointer(str) {
    return str.replace(/~1/g, "/").replace(/~0/g, "~");
  }
  exports2.unescapeJsonPointer = unescapeJsonPointer;
  function eachItem(xs, f) {
    if (Array.isArray(xs)) for (const x of xs) f(x);
    else f(xs);
  }
  exports2.eachItem = eachItem;
  function makeMergeEvaluated({ mergeNames, mergeToName, mergeValues: mergeValues2, resultToName }) {
    return (gen, from, to, toName) => {
      const res = to === void 0 ? from : to instanceof codegen_1.Name ? (from instanceof codegen_1.Name ? mergeNames(gen, from, to) : mergeToName(gen, from, to), to) : from instanceof codegen_1.Name ? (mergeToName(gen, to, from), from) : mergeValues2(from, to);
      return toName === codegen_1.Name && !(res instanceof codegen_1.Name) ? resultToName(gen, res) : res;
    };
  }
  exports2.mergeEvaluated = {
    props: makeMergeEvaluated({
      mergeNames: (gen, from, to) => gen.if((0, codegen_1._)`${to} !== true && ${from} !== undefined`, () => {
        gen.if((0, codegen_1._)`${from} === true`, () => gen.assign(to, true), () => gen.assign(to, (0, codegen_1._)`${to} || {}`).code((0, codegen_1._)`Object.assign(${to}, ${from})`));
      }),
      mergeToName: (gen, from, to) => gen.if((0, codegen_1._)`${to} !== true`, () => {
        if (from === true) gen.assign(to, true);
        else {
          gen.assign(to, (0, codegen_1._)`${to} || {}`);
          setEvaluated(gen, to, from);
        }
      }),
      mergeValues: (from, to) => from === true ? true : {
        ...from,
        ...to
      },
      resultToName: evaluatedPropsToName
    }),
    items: makeMergeEvaluated({
      mergeNames: (gen, from, to) => gen.if((0, codegen_1._)`${to} !== true && ${from} !== undefined`, () => gen.assign(to, (0, codegen_1._)`${from} === true ? true : ${to} > ${from} ? ${to} : ${from}`)),
      mergeToName: (gen, from, to) => gen.if((0, codegen_1._)`${to} !== true`, () => gen.assign(to, from === true ? true : (0, codegen_1._)`${to} > ${from} ? ${to} : ${from}`)),
      mergeValues: (from, to) => from === true ? true : Math.max(from, to),
      resultToName: (gen, items) => gen.var("items", items)
    })
  };
  function evaluatedPropsToName(gen, ps) {
    if (ps === true) return gen.var("props", true);
    const props = gen.var("props", (0, codegen_1._)`{}`);
    if (ps !== void 0) setEvaluated(gen, props, ps);
    return props;
  }
  exports2.evaluatedPropsToName = evaluatedPropsToName;
  function setEvaluated(gen, props, ps) {
    Object.keys(ps).forEach((p) => gen.assign((0, codegen_1._)`${props}${(0, codegen_1.getProperty)(p)}`, true));
  }
  exports2.setEvaluated = setEvaluated;
  const snippets = {};
  function useFunc(gen, f) {
    return gen.scopeValue("func", {
      ref: f,
      code: snippets[f.code] || (snippets[f.code] = new code_1._Code(f.code))
    });
  }
  exports2.useFunc = useFunc;
  var Type;
  (function(Type2) {
    Type2[Type2["Num"] = 0] = "Num";
    Type2[Type2["Str"] = 1] = "Str";
  })(Type || (exports2.Type = Type = {}));
  function getErrorPath(dataProp, dataPropType, jsPropertySyntax) {
    if (dataProp instanceof codegen_1.Name) {
      const isNumber = dataPropType === Type.Num;
      return jsPropertySyntax ? isNumber ? (0, codegen_1._)`"[" + ${dataProp} + "]"` : (0, codegen_1._)`"['" + ${dataProp} + "']"` : isNumber ? (0, codegen_1._)`"/" + ${dataProp}` : (0, codegen_1._)`"/" + ${dataProp}.replace(/~/g, "~0").replace(/\\//g, "~1")`;
    }
    return jsPropertySyntax ? (0, codegen_1.getProperty)(dataProp).toString() : "/" + escapeJsonPointer(dataProp);
  }
  exports2.getErrorPath = getErrorPath;
  function checkStrictMode(it, msg, mode = it.opts.strictSchema) {
    if (!mode) return;
    msg = `strict mode: ${msg}`;
    if (mode === true) throw new Error(msg);
    it.self.logger.warn(msg);
  }
  exports2.checkStrictMode = checkStrictMode;
}));
var require_names = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const names = {
    data: new codegen_1.Name("data"),
    valCxt: new codegen_1.Name("valCxt"),
    instancePath: new codegen_1.Name("instancePath"),
    parentData: new codegen_1.Name("parentData"),
    parentDataProperty: new codegen_1.Name("parentDataProperty"),
    rootData: new codegen_1.Name("rootData"),
    dynamicAnchors: new codegen_1.Name("dynamicAnchors"),
    vErrors: new codegen_1.Name("vErrors"),
    errors: new codegen_1.Name("errors"),
    this: new codegen_1.Name("this"),
    self: new codegen_1.Name("self"),
    scope: new codegen_1.Name("scope"),
    json: new codegen_1.Name("json"),
    jsonPos: new codegen_1.Name("jsonPos"),
    jsonLen: new codegen_1.Name("jsonLen"),
    jsonPart: new codegen_1.Name("jsonPart")
  };
  exports2.default = names;
}));
var require_errors = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.extendErrors = exports2.resetErrorsCount = exports2.reportExtraError = exports2.reportError = exports2.keyword$DataError = exports2.keywordError = void 0;
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const names_1 = require_names();
  exports2.keywordError = { message: ({ keyword }) => (0, codegen_1.str)`must pass "${keyword}" keyword validation` };
  exports2.keyword$DataError = { message: ({ keyword, schemaType }) => schemaType ? (0, codegen_1.str)`"${keyword}" keyword must be ${schemaType} ($data)` : (0, codegen_1.str)`"${keyword}" keyword is invalid ($data)` };
  function reportError(cxt, error2 = exports2.keywordError, errorPaths, overrideAllErrors) {
    const { it } = cxt;
    const { gen, compositeRule, allErrors } = it;
    const errObj = errorObjectCode(cxt, error2, errorPaths);
    if (overrideAllErrors !== null && overrideAllErrors !== void 0 ? overrideAllErrors : compositeRule || allErrors) addError(gen, errObj);
    else returnErrors(it, (0, codegen_1._)`[${errObj}]`);
  }
  exports2.reportError = reportError;
  function reportExtraError(cxt, error2 = exports2.keywordError, errorPaths) {
    const { it } = cxt;
    const { gen, compositeRule, allErrors } = it;
    addError(gen, errorObjectCode(cxt, error2, errorPaths));
    if (!(compositeRule || allErrors)) returnErrors(it, names_1.default.vErrors);
  }
  exports2.reportExtraError = reportExtraError;
  function resetErrorsCount(gen, errsCount) {
    gen.assign(names_1.default.errors, errsCount);
    gen.if((0, codegen_1._)`${names_1.default.vErrors} !== null`, () => gen.if(errsCount, () => gen.assign((0, codegen_1._)`${names_1.default.vErrors}.length`, errsCount), () => gen.assign(names_1.default.vErrors, null)));
  }
  exports2.resetErrorsCount = resetErrorsCount;
  function extendErrors({ gen, keyword, schemaValue, data, errsCount, it }) {
    if (errsCount === void 0) throw new Error("ajv implementation error");
    const err = gen.name("err");
    gen.forRange("i", errsCount, names_1.default.errors, (i) => {
      gen.const(err, (0, codegen_1._)`${names_1.default.vErrors}[${i}]`);
      gen.if((0, codegen_1._)`${err}.instancePath === undefined`, () => gen.assign((0, codegen_1._)`${err}.instancePath`, (0, codegen_1.strConcat)(names_1.default.instancePath, it.errorPath)));
      gen.assign((0, codegen_1._)`${err}.schemaPath`, (0, codegen_1.str)`${it.errSchemaPath}/${keyword}`);
      if (it.opts.verbose) {
        gen.assign((0, codegen_1._)`${err}.schema`, schemaValue);
        gen.assign((0, codegen_1._)`${err}.data`, data);
      }
    });
  }
  exports2.extendErrors = extendErrors;
  function addError(gen, errObj) {
    const err = gen.const("err", errObj);
    gen.if((0, codegen_1._)`${names_1.default.vErrors} === null`, () => gen.assign(names_1.default.vErrors, (0, codegen_1._)`[${err}]`), (0, codegen_1._)`${names_1.default.vErrors}.push(${err})`);
    gen.code((0, codegen_1._)`${names_1.default.errors}++`);
  }
  function returnErrors(it, errs) {
    const { gen, validateName, schemaEnv } = it;
    if (schemaEnv.$async) gen.throw((0, codegen_1._)`new ${it.ValidationError}(${errs})`);
    else {
      gen.assign((0, codegen_1._)`${validateName}.errors`, errs);
      gen.return(false);
    }
  }
  const E = {
    keyword: new codegen_1.Name("keyword"),
    schemaPath: new codegen_1.Name("schemaPath"),
    params: new codegen_1.Name("params"),
    propertyName: new codegen_1.Name("propertyName"),
    message: new codegen_1.Name("message"),
    schema: new codegen_1.Name("schema"),
    parentSchema: new codegen_1.Name("parentSchema")
  };
  function errorObjectCode(cxt, error2, errorPaths) {
    const { createErrors } = cxt.it;
    if (createErrors === false) return (0, codegen_1._)`{}`;
    return errorObject(cxt, error2, errorPaths);
  }
  function errorObject(cxt, error2, errorPaths = {}) {
    const { gen, it } = cxt;
    const keyValues = [errorInstancePath(it, errorPaths), errorSchemaPath(cxt, errorPaths)];
    extraErrorProps(cxt, error2, keyValues);
    return gen.object(...keyValues);
  }
  function errorInstancePath({ errorPath }, { instancePath }) {
    const instPath = instancePath ? (0, codegen_1.str)`${errorPath}${(0, util_1.getErrorPath)(instancePath, util_1.Type.Str)}` : errorPath;
    return [names_1.default.instancePath, (0, codegen_1.strConcat)(names_1.default.instancePath, instPath)];
  }
  function errorSchemaPath({ keyword, it: { errSchemaPath } }, { schemaPath, parentSchema }) {
    let schPath = parentSchema ? errSchemaPath : (0, codegen_1.str)`${errSchemaPath}/${keyword}`;
    if (schemaPath) schPath = (0, codegen_1.str)`${schPath}${(0, util_1.getErrorPath)(schemaPath, util_1.Type.Str)}`;
    return [E.schemaPath, schPath];
  }
  function extraErrorProps(cxt, { params, message }, keyValues) {
    const { keyword, data, schemaValue, it } = cxt;
    const { opts, propertyName, topSchemaRef, schemaPath } = it;
    keyValues.push([E.keyword, keyword], [E.params, typeof params == "function" ? params(cxt) : params || (0, codegen_1._)`{}`]);
    if (opts.messages) keyValues.push([E.message, typeof message == "function" ? message(cxt) : message]);
    if (opts.verbose) keyValues.push([E.schema, schemaValue], [E.parentSchema, (0, codegen_1._)`${topSchemaRef}${schemaPath}`], [names_1.default.data, data]);
    if (propertyName) keyValues.push([E.propertyName, propertyName]);
  }
}));
var require_boolSchema = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.boolOrEmptySchema = exports2.topBoolOrEmptySchema = void 0;
  const errors_1 = require_errors();
  const codegen_1 = require_codegen();
  const names_1 = require_names();
  const boolError = { message: "boolean schema is false" };
  function topBoolOrEmptySchema(it) {
    const { gen, schema, validateName } = it;
    if (schema === false) falseSchemaError(it, false);
    else if (typeof schema == "object" && schema.$async === true) gen.return(names_1.default.data);
    else {
      gen.assign((0, codegen_1._)`${validateName}.errors`, null);
      gen.return(true);
    }
  }
  exports2.topBoolOrEmptySchema = topBoolOrEmptySchema;
  function boolOrEmptySchema(it, valid) {
    const { gen, schema } = it;
    if (schema === false) {
      gen.var(valid, false);
      falseSchemaError(it);
    } else gen.var(valid, true);
  }
  exports2.boolOrEmptySchema = boolOrEmptySchema;
  function falseSchemaError(it, overrideAllErrors) {
    const { gen, data } = it;
    const cxt = {
      gen,
      keyword: "false schema",
      data,
      schema: false,
      schemaCode: false,
      schemaValue: false,
      params: {},
      it
    };
    (0, errors_1.reportError)(cxt, boolError, void 0, overrideAllErrors);
  }
}));
var require_rules = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.getRules = exports2.isJSONType = void 0;
  const jsonTypes = /* @__PURE__ */ new Set([
    "string",
    "number",
    "integer",
    "boolean",
    "null",
    "object",
    "array"
  ]);
  function isJSONType(x) {
    return typeof x == "string" && jsonTypes.has(x);
  }
  exports2.isJSONType = isJSONType;
  function getRules() {
    const groups = {
      number: {
        type: "number",
        rules: []
      },
      string: {
        type: "string",
        rules: []
      },
      array: {
        type: "array",
        rules: []
      },
      object: {
        type: "object",
        rules: []
      }
    };
    return {
      types: {
        ...groups,
        integer: true,
        boolean: true,
        null: true
      },
      rules: [
        { rules: [] },
        groups.number,
        groups.string,
        groups.array,
        groups.object
      ],
      post: { rules: [] },
      all: {},
      keywords: {}
    };
  }
  exports2.getRules = getRules;
}));
var require_applicability = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.shouldUseRule = exports2.shouldUseGroup = exports2.schemaHasRulesForType = void 0;
  function schemaHasRulesForType({ schema, self: self2 }, type) {
    const group = self2.RULES.types[type];
    return group && group !== true && shouldUseGroup(schema, group);
  }
  exports2.schemaHasRulesForType = schemaHasRulesForType;
  function shouldUseGroup(schema, group) {
    return group.rules.some((rule) => shouldUseRule(schema, rule));
  }
  exports2.shouldUseGroup = shouldUseGroup;
  function shouldUseRule(schema, rule) {
    var _a2;
    return schema[rule.keyword] !== void 0 || ((_a2 = rule.definition.implements) === null || _a2 === void 0 ? void 0 : _a2.some((kwd) => schema[kwd] !== void 0));
  }
  exports2.shouldUseRule = shouldUseRule;
}));
var require_dataType = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.reportTypeError = exports2.checkDataTypes = exports2.checkDataType = exports2.coerceAndCheckDataType = exports2.getJSONTypes = exports2.getSchemaTypes = exports2.DataType = void 0;
  const rules_1 = require_rules();
  const applicability_1 = require_applicability();
  const errors_1 = require_errors();
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  var DataType;
  (function(DataType2) {
    DataType2[DataType2["Correct"] = 0] = "Correct";
    DataType2[DataType2["Wrong"] = 1] = "Wrong";
  })(DataType || (exports2.DataType = DataType = {}));
  function getSchemaTypes(schema) {
    const types = getJSONTypes(schema.type);
    if (types.includes("null")) {
      if (schema.nullable === false) throw new Error("type: null contradicts nullable: false");
    } else {
      if (!types.length && schema.nullable !== void 0) throw new Error('"nullable" cannot be used without "type"');
      if (schema.nullable === true) types.push("null");
    }
    return types;
  }
  exports2.getSchemaTypes = getSchemaTypes;
  function getJSONTypes(ts) {
    const types = Array.isArray(ts) ? ts : ts ? [ts] : [];
    if (types.every(rules_1.isJSONType)) return types;
    throw new Error("type must be JSONType or JSONType[]: " + types.join(","));
  }
  exports2.getJSONTypes = getJSONTypes;
  function coerceAndCheckDataType(it, types) {
    const { gen, data, opts } = it;
    const coerceTo = coerceToTypes(types, opts.coerceTypes);
    const checkTypes = types.length > 0 && !(coerceTo.length === 0 && types.length === 1 && (0, applicability_1.schemaHasRulesForType)(it, types[0]));
    if (checkTypes) {
      const wrongType = checkDataTypes(types, data, opts.strictNumbers, DataType.Wrong);
      gen.if(wrongType, () => {
        if (coerceTo.length) coerceData(it, types, coerceTo);
        else reportTypeError(it);
      });
    }
    return checkTypes;
  }
  exports2.coerceAndCheckDataType = coerceAndCheckDataType;
  const COERCIBLE = /* @__PURE__ */ new Set([
    "string",
    "number",
    "integer",
    "boolean",
    "null"
  ]);
  function coerceToTypes(types, coerceTypes) {
    return coerceTypes ? types.filter((t) => COERCIBLE.has(t) || coerceTypes === "array" && t === "array") : [];
  }
  function coerceData(it, types, coerceTo) {
    const { gen, data, opts } = it;
    const dataType = gen.let("dataType", (0, codegen_1._)`typeof ${data}`);
    const coerced = gen.let("coerced", (0, codegen_1._)`undefined`);
    if (opts.coerceTypes === "array") gen.if((0, codegen_1._)`${dataType} == 'object' && Array.isArray(${data}) && ${data}.length == 1`, () => gen.assign(data, (0, codegen_1._)`${data}[0]`).assign(dataType, (0, codegen_1._)`typeof ${data}`).if(checkDataTypes(types, data, opts.strictNumbers), () => gen.assign(coerced, data)));
    gen.if((0, codegen_1._)`${coerced} !== undefined`);
    for (const t of coerceTo) if (COERCIBLE.has(t) || t === "array" && opts.coerceTypes === "array") coerceSpecificType(t);
    gen.else();
    reportTypeError(it);
    gen.endIf();
    gen.if((0, codegen_1._)`${coerced} !== undefined`, () => {
      gen.assign(data, coerced);
      assignParentData(it, coerced);
    });
    function coerceSpecificType(t) {
      switch (t) {
        case "string":
          gen.elseIf((0, codegen_1._)`${dataType} == "number" || ${dataType} == "boolean"`).assign(coerced, (0, codegen_1._)`"" + ${data}`).elseIf((0, codegen_1._)`${data} === null`).assign(coerced, (0, codegen_1._)`""`);
          return;
        case "number":
          gen.elseIf((0, codegen_1._)`${dataType} == "boolean" || ${data} === null
              || (${dataType} == "string" && ${data} && ${data} == +${data})`).assign(coerced, (0, codegen_1._)`+${data}`);
          return;
        case "integer":
          gen.elseIf((0, codegen_1._)`${dataType} === "boolean" || ${data} === null
              || (${dataType} === "string" && ${data} && ${data} == +${data} && !(${data} % 1))`).assign(coerced, (0, codegen_1._)`+${data}`);
          return;
        case "boolean":
          gen.elseIf((0, codegen_1._)`${data} === "false" || ${data} === 0 || ${data} === null`).assign(coerced, false).elseIf((0, codegen_1._)`${data} === "true" || ${data} === 1`).assign(coerced, true);
          return;
        case "null":
          gen.elseIf((0, codegen_1._)`${data} === "" || ${data} === 0 || ${data} === false`);
          gen.assign(coerced, null);
          return;
        case "array":
          gen.elseIf((0, codegen_1._)`${dataType} === "string" || ${dataType} === "number"
              || ${dataType} === "boolean" || ${data} === null`).assign(coerced, (0, codegen_1._)`[${data}]`);
      }
    }
  }
  function assignParentData({ gen, parentData, parentDataProperty }, expr) {
    gen.if((0, codegen_1._)`${parentData} !== undefined`, () => gen.assign((0, codegen_1._)`${parentData}[${parentDataProperty}]`, expr));
  }
  function checkDataType(dataType, data, strictNums, correct = DataType.Correct) {
    const EQ = correct === DataType.Correct ? codegen_1.operators.EQ : codegen_1.operators.NEQ;
    let cond;
    switch (dataType) {
      case "null":
        return (0, codegen_1._)`${data} ${EQ} null`;
      case "array":
        cond = (0, codegen_1._)`Array.isArray(${data})`;
        break;
      case "object":
        cond = (0, codegen_1._)`${data} && typeof ${data} == "object" && !Array.isArray(${data})`;
        break;
      case "integer":
        cond = numCond((0, codegen_1._)`!(${data} % 1) && !isNaN(${data})`);
        break;
      case "number":
        cond = numCond();
        break;
      default:
        return (0, codegen_1._)`typeof ${data} ${EQ} ${dataType}`;
    }
    return correct === DataType.Correct ? cond : (0, codegen_1.not)(cond);
    function numCond(_cond = codegen_1.nil) {
      return (0, codegen_1.and)((0, codegen_1._)`typeof ${data} == "number"`, _cond, strictNums ? (0, codegen_1._)`isFinite(${data})` : codegen_1.nil);
    }
  }
  exports2.checkDataType = checkDataType;
  function checkDataTypes(dataTypes, data, strictNums, correct) {
    if (dataTypes.length === 1) return checkDataType(dataTypes[0], data, strictNums, correct);
    let cond;
    const types = (0, util_1.toHash)(dataTypes);
    if (types.array && types.object) {
      const notObj = (0, codegen_1._)`typeof ${data} != "object"`;
      cond = types.null ? notObj : (0, codegen_1._)`!${data} || ${notObj}`;
      delete types.null;
      delete types.array;
      delete types.object;
    } else cond = codegen_1.nil;
    if (types.number) delete types.integer;
    for (const t in types) cond = (0, codegen_1.and)(cond, checkDataType(t, data, strictNums, correct));
    return cond;
  }
  exports2.checkDataTypes = checkDataTypes;
  const typeError = {
    message: ({ schema }) => `must be ${schema}`,
    params: ({ schema, schemaValue }) => typeof schema == "string" ? (0, codegen_1._)`{type: ${schema}}` : (0, codegen_1._)`{type: ${schemaValue}}`
  };
  function reportTypeError(it) {
    const cxt = getTypeErrorContext(it);
    (0, errors_1.reportError)(cxt, typeError);
  }
  exports2.reportTypeError = reportTypeError;
  function getTypeErrorContext(it) {
    const { gen, data, schema } = it;
    const schemaCode = (0, util_1.schemaRefOrVal)(it, schema, "type");
    return {
      gen,
      keyword: "type",
      data,
      schema: schema.type,
      schemaCode,
      schemaValue: schemaCode,
      parentSchema: schema,
      params: {},
      it
    };
  }
}));
var require_defaults = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.assignDefaults = void 0;
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  function assignDefaults(it, ty) {
    const { properties, items } = it.schema;
    if (ty === "object" && properties) for (const key in properties) assignDefault(it, key, properties[key].default);
    else if (ty === "array" && Array.isArray(items)) items.forEach((sch, i) => assignDefault(it, i, sch.default));
  }
  exports2.assignDefaults = assignDefaults;
  function assignDefault(it, prop, defaultValue) {
    const { gen, compositeRule, data, opts } = it;
    if (defaultValue === void 0) return;
    const childData = (0, codegen_1._)`${data}${(0, codegen_1.getProperty)(prop)}`;
    if (compositeRule) {
      (0, util_1.checkStrictMode)(it, `default is ignored for: ${childData}`);
      return;
    }
    let condition = (0, codegen_1._)`${childData} === undefined`;
    if (opts.useDefaults === "empty") condition = (0, codegen_1._)`${condition} || ${childData} === null || ${childData} === ""`;
    gen.if(condition, (0, codegen_1._)`${childData} = ${(0, codegen_1.stringify)(defaultValue)}`);
  }
}));
var require_code = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.validateUnion = exports2.validateArray = exports2.usePattern = exports2.callValidateCode = exports2.schemaProperties = exports2.allSchemaProperties = exports2.noPropertyInData = exports2.propertyInData = exports2.isOwnProperty = exports2.hasPropFunc = exports2.reportMissingProp = exports2.checkMissingProp = exports2.checkReportMissingProp = void 0;
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const names_1 = require_names();
  const util_2 = require_util();
  function checkReportMissingProp(cxt, prop) {
    const { gen, data, it } = cxt;
    gen.if(noPropertyInData(gen, data, prop, it.opts.ownProperties), () => {
      cxt.setParams({ missingProperty: (0, codegen_1._)`${prop}` }, true);
      cxt.error();
    });
  }
  exports2.checkReportMissingProp = checkReportMissingProp;
  function checkMissingProp({ gen, data, it: { opts } }, properties, missing) {
    return (0, codegen_1.or)(...properties.map((prop) => (0, codegen_1.and)(noPropertyInData(gen, data, prop, opts.ownProperties), (0, codegen_1._)`${missing} = ${prop}`)));
  }
  exports2.checkMissingProp = checkMissingProp;
  function reportMissingProp(cxt, missing) {
    cxt.setParams({ missingProperty: missing }, true);
    cxt.error();
  }
  exports2.reportMissingProp = reportMissingProp;
  function hasPropFunc(gen) {
    return gen.scopeValue("func", {
      ref: Object.prototype.hasOwnProperty,
      code: (0, codegen_1._)`Object.prototype.hasOwnProperty`
    });
  }
  exports2.hasPropFunc = hasPropFunc;
  function isOwnProperty(gen, data, property) {
    return (0, codegen_1._)`${hasPropFunc(gen)}.call(${data}, ${property})`;
  }
  exports2.isOwnProperty = isOwnProperty;
  function propertyInData(gen, data, property, ownProperties) {
    const cond = (0, codegen_1._)`${data}${(0, codegen_1.getProperty)(property)} !== undefined`;
    return ownProperties ? (0, codegen_1._)`${cond} && ${isOwnProperty(gen, data, property)}` : cond;
  }
  exports2.propertyInData = propertyInData;
  function noPropertyInData(gen, data, property, ownProperties) {
    const cond = (0, codegen_1._)`${data}${(0, codegen_1.getProperty)(property)} === undefined`;
    return ownProperties ? (0, codegen_1.or)(cond, (0, codegen_1.not)(isOwnProperty(gen, data, property))) : cond;
  }
  exports2.noPropertyInData = noPropertyInData;
  function allSchemaProperties(schemaMap) {
    return schemaMap ? Object.keys(schemaMap).filter((p) => p !== "__proto__") : [];
  }
  exports2.allSchemaProperties = allSchemaProperties;
  function schemaProperties(it, schemaMap) {
    return allSchemaProperties(schemaMap).filter((p) => !(0, util_1.alwaysValidSchema)(it, schemaMap[p]));
  }
  exports2.schemaProperties = schemaProperties;
  function callValidateCode({ schemaCode, data, it: { gen, topSchemaRef, schemaPath, errorPath }, it }, func, context, passSchema) {
    const dataAndSchema = passSchema ? (0, codegen_1._)`${schemaCode}, ${data}, ${topSchemaRef}${schemaPath}` : data;
    const valCxt = [
      [names_1.default.instancePath, (0, codegen_1.strConcat)(names_1.default.instancePath, errorPath)],
      [names_1.default.parentData, it.parentData],
      [names_1.default.parentDataProperty, it.parentDataProperty],
      [names_1.default.rootData, names_1.default.rootData]
    ];
    if (it.opts.dynamicRef) valCxt.push([names_1.default.dynamicAnchors, names_1.default.dynamicAnchors]);
    const args = (0, codegen_1._)`${dataAndSchema}, ${gen.object(...valCxt)}`;
    return context !== codegen_1.nil ? (0, codegen_1._)`${func}.call(${context}, ${args})` : (0, codegen_1._)`${func}(${args})`;
  }
  exports2.callValidateCode = callValidateCode;
  const newRegExp = (0, codegen_1._)`new RegExp`;
  function usePattern({ gen, it: { opts } }, pattern) {
    const u = opts.unicodeRegExp ? "u" : "";
    const { regExp } = opts.code;
    const rx = regExp(pattern, u);
    return gen.scopeValue("pattern", {
      key: rx.toString(),
      ref: rx,
      code: (0, codegen_1._)`${regExp.code === "new RegExp" ? newRegExp : (0, util_2.useFunc)(gen, regExp)}(${pattern}, ${u})`
    });
  }
  exports2.usePattern = usePattern;
  function validateArray(cxt) {
    const { gen, data, keyword, it } = cxt;
    const valid = gen.name("valid");
    if (it.allErrors) {
      const validArr = gen.let("valid", true);
      validateItems(() => gen.assign(validArr, false));
      return validArr;
    }
    gen.var(valid, true);
    validateItems(() => gen.break());
    return valid;
    function validateItems(notValid) {
      const len = gen.const("len", (0, codegen_1._)`${data}.length`);
      gen.forRange("i", 0, len, (i) => {
        cxt.subschema({
          keyword,
          dataProp: i,
          dataPropType: util_1.Type.Num
        }, valid);
        gen.if((0, codegen_1.not)(valid), notValid);
      });
    }
  }
  exports2.validateArray = validateArray;
  function validateUnion(cxt) {
    const { gen, schema, keyword, it } = cxt;
    if (!Array.isArray(schema)) throw new Error("ajv implementation error");
    if (schema.some((sch) => (0, util_1.alwaysValidSchema)(it, sch)) && !it.opts.unevaluated) return;
    const valid = gen.let("valid", false);
    const schValid = gen.name("_valid");
    gen.block(() => schema.forEach((_sch, i) => {
      const schCxt = cxt.subschema({
        keyword,
        schemaProp: i,
        compositeRule: true
      }, schValid);
      gen.assign(valid, (0, codegen_1._)`${valid} || ${schValid}`);
      if (!cxt.mergeValidEvaluated(schCxt, schValid)) gen.if((0, codegen_1.not)(valid));
    }));
    cxt.result(valid, () => cxt.reset(), () => cxt.error(true));
  }
  exports2.validateUnion = validateUnion;
}));
var require_keyword = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.validateKeywordUsage = exports2.validSchemaType = exports2.funcKeywordCode = exports2.macroKeywordCode = void 0;
  const codegen_1 = require_codegen();
  const names_1 = require_names();
  const code_1 = require_code();
  const errors_1 = require_errors();
  function macroKeywordCode(cxt, def) {
    const { gen, keyword, schema, parentSchema, it } = cxt;
    const macroSchema = def.macro.call(it.self, schema, parentSchema, it);
    const schemaRef = useKeyword(gen, keyword, macroSchema);
    if (it.opts.validateSchema !== false) it.self.validateSchema(macroSchema, true);
    const valid = gen.name("valid");
    cxt.subschema({
      schema: macroSchema,
      schemaPath: codegen_1.nil,
      errSchemaPath: `${it.errSchemaPath}/${keyword}`,
      topSchemaRef: schemaRef,
      compositeRule: true
    }, valid);
    cxt.pass(valid, () => cxt.error(true));
  }
  exports2.macroKeywordCode = macroKeywordCode;
  function funcKeywordCode(cxt, def) {
    var _a2;
    const { gen, keyword, schema, parentSchema, $data, it } = cxt;
    checkAsyncKeyword(it, def);
    const validateRef = useKeyword(gen, keyword, !$data && def.compile ? def.compile.call(it.self, schema, parentSchema, it) : def.validate);
    const valid = gen.let("valid");
    cxt.block$data(valid, validateKeyword);
    cxt.ok((_a2 = def.valid) !== null && _a2 !== void 0 ? _a2 : valid);
    function validateKeyword() {
      if (def.errors === false) {
        assignValid();
        if (def.modifying) modifyData(cxt);
        reportErrs(() => cxt.error());
      } else {
        const ruleErrs = def.async ? validateAsync() : validateSync();
        if (def.modifying) modifyData(cxt);
        reportErrs(() => addErrs(cxt, ruleErrs));
      }
    }
    function validateAsync() {
      const ruleErrs = gen.let("ruleErrs", null);
      gen.try(() => assignValid((0, codegen_1._)`await `), (e) => gen.assign(valid, false).if((0, codegen_1._)`${e} instanceof ${it.ValidationError}`, () => gen.assign(ruleErrs, (0, codegen_1._)`${e}.errors`), () => gen.throw(e)));
      return ruleErrs;
    }
    function validateSync() {
      const validateErrs = (0, codegen_1._)`${validateRef}.errors`;
      gen.assign(validateErrs, null);
      assignValid(codegen_1.nil);
      return validateErrs;
    }
    function assignValid(_await = def.async ? (0, codegen_1._)`await ` : codegen_1.nil) {
      const passCxt = it.opts.passContext ? names_1.default.this : names_1.default.self;
      const passSchema = !("compile" in def && !$data || def.schema === false);
      gen.assign(valid, (0, codegen_1._)`${_await}${(0, code_1.callValidateCode)(cxt, validateRef, passCxt, passSchema)}`, def.modifying);
    }
    function reportErrs(errors) {
      var _a$1;
      gen.if((0, codegen_1.not)((_a$1 = def.valid) !== null && _a$1 !== void 0 ? _a$1 : valid), errors);
    }
  }
  exports2.funcKeywordCode = funcKeywordCode;
  function modifyData(cxt) {
    const { gen, data, it } = cxt;
    gen.if(it.parentData, () => gen.assign(data, (0, codegen_1._)`${it.parentData}[${it.parentDataProperty}]`));
  }
  function addErrs(cxt, errs) {
    const { gen } = cxt;
    gen.if((0, codegen_1._)`Array.isArray(${errs})`, () => {
      gen.assign(names_1.default.vErrors, (0, codegen_1._)`${names_1.default.vErrors} === null ? ${errs} : ${names_1.default.vErrors}.concat(${errs})`).assign(names_1.default.errors, (0, codegen_1._)`${names_1.default.vErrors}.length`);
      (0, errors_1.extendErrors)(cxt);
    }, () => cxt.error());
  }
  function checkAsyncKeyword({ schemaEnv }, def) {
    if (def.async && !schemaEnv.$async) throw new Error("async keyword in sync schema");
  }
  function useKeyword(gen, keyword, result) {
    if (result === void 0) throw new Error(`keyword "${keyword}" failed to compile`);
    return gen.scopeValue("keyword", typeof result == "function" ? { ref: result } : {
      ref: result,
      code: (0, codegen_1.stringify)(result)
    });
  }
  function validSchemaType(schema, schemaType, allowUndefined = false) {
    return !schemaType.length || schemaType.some((st) => st === "array" ? Array.isArray(schema) : st === "object" ? schema && typeof schema == "object" && !Array.isArray(schema) : typeof schema == st || allowUndefined && typeof schema == "undefined");
  }
  exports2.validSchemaType = validSchemaType;
  function validateKeywordUsage({ schema, opts, self: self2, errSchemaPath }, def, keyword) {
    if (Array.isArray(def.keyword) ? !def.keyword.includes(keyword) : def.keyword !== keyword) throw new Error("ajv implementation error");
    const deps = def.dependencies;
    if (deps === null || deps === void 0 ? void 0 : deps.some((kwd) => !Object.prototype.hasOwnProperty.call(schema, kwd))) throw new Error(`parent schema must have dependencies of ${keyword}: ${deps.join(",")}`);
    if (def.validateSchema) {
      if (!def.validateSchema(schema[keyword])) {
        const msg = `keyword "${keyword}" value is invalid at path "${errSchemaPath}": ` + self2.errorsText(def.validateSchema.errors);
        if (opts.validateSchema === "log") self2.logger.error(msg);
        else throw new Error(msg);
      }
    }
  }
  exports2.validateKeywordUsage = validateKeywordUsage;
}));
var require_subschema = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.extendSubschemaMode = exports2.extendSubschemaData = exports2.getSubschema = void 0;
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  function getSubschema(it, { keyword, schemaProp, schema, schemaPath, errSchemaPath, topSchemaRef }) {
    if (keyword !== void 0 && schema !== void 0) throw new Error('both "keyword" and "schema" passed, only one allowed');
    if (keyword !== void 0) {
      const sch = it.schema[keyword];
      return schemaProp === void 0 ? {
        schema: sch,
        schemaPath: (0, codegen_1._)`${it.schemaPath}${(0, codegen_1.getProperty)(keyword)}`,
        errSchemaPath: `${it.errSchemaPath}/${keyword}`
      } : {
        schema: sch[schemaProp],
        schemaPath: (0, codegen_1._)`${it.schemaPath}${(0, codegen_1.getProperty)(keyword)}${(0, codegen_1.getProperty)(schemaProp)}`,
        errSchemaPath: `${it.errSchemaPath}/${keyword}/${(0, util_1.escapeFragment)(schemaProp)}`
      };
    }
    if (schema !== void 0) {
      if (schemaPath === void 0 || errSchemaPath === void 0 || topSchemaRef === void 0) throw new Error('"schemaPath", "errSchemaPath" and "topSchemaRef" are required with "schema"');
      return {
        schema,
        schemaPath,
        topSchemaRef,
        errSchemaPath
      };
    }
    throw new Error('either "keyword" or "schema" must be passed');
  }
  exports2.getSubschema = getSubschema;
  function extendSubschemaData(subschema, it, { dataProp, dataPropType: dpType, data, dataTypes, propertyName }) {
    if (data !== void 0 && dataProp !== void 0) throw new Error('both "data" and "dataProp" passed, only one allowed');
    const { gen } = it;
    if (dataProp !== void 0) {
      const { errorPath, dataPathArr, opts } = it;
      dataContextProps(gen.let("data", (0, codegen_1._)`${it.data}${(0, codegen_1.getProperty)(dataProp)}`, true));
      subschema.errorPath = (0, codegen_1.str)`${errorPath}${(0, util_1.getErrorPath)(dataProp, dpType, opts.jsPropertySyntax)}`;
      subschema.parentDataProperty = (0, codegen_1._)`${dataProp}`;
      subschema.dataPathArr = [...dataPathArr, subschema.parentDataProperty];
    }
    if (data !== void 0) {
      dataContextProps(data instanceof codegen_1.Name ? data : gen.let("data", data, true));
      if (propertyName !== void 0) subschema.propertyName = propertyName;
    }
    if (dataTypes) subschema.dataTypes = dataTypes;
    function dataContextProps(_nextData) {
      subschema.data = _nextData;
      subschema.dataLevel = it.dataLevel + 1;
      subschema.dataTypes = [];
      it.definedProperties = /* @__PURE__ */ new Set();
      subschema.parentData = it.data;
      subschema.dataNames = [...it.dataNames, _nextData];
    }
  }
  exports2.extendSubschemaData = extendSubschemaData;
  function extendSubschemaMode(subschema, { jtdDiscriminator, jtdMetadata, compositeRule, createErrors, allErrors }) {
    if (compositeRule !== void 0) subschema.compositeRule = compositeRule;
    if (createErrors !== void 0) subschema.createErrors = createErrors;
    if (allErrors !== void 0) subschema.allErrors = allErrors;
    subschema.jtdDiscriminator = jtdDiscriminator;
    subschema.jtdMetadata = jtdMetadata;
  }
  exports2.extendSubschemaMode = extendSubschemaMode;
}));
var require_fast_deep_equal = /* @__PURE__ */ __commonJSMin(((exports2, module2) => {
  module2.exports = function equal(a, b) {
    if (a === b) return true;
    if (a && b && typeof a == "object" && typeof b == "object") {
      if (a.constructor !== b.constructor) return false;
      var length, i, keys;
      if (Array.isArray(a)) {
        length = a.length;
        if (length != b.length) return false;
        for (i = length; i-- !== 0; ) if (!equal(a[i], b[i])) return false;
        return true;
      }
      if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
      if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
      if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();
      keys = Object.keys(a);
      length = keys.length;
      if (length !== Object.keys(b).length) return false;
      for (i = length; i-- !== 0; ) if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
      for (i = length; i-- !== 0; ) {
        var key = keys[i];
        if (!equal(a[key], b[key])) return false;
      }
      return true;
    }
    return a !== a && b !== b;
  };
}));
var require_json_schema_traverse = /* @__PURE__ */ __commonJSMin(((exports2, module2) => {
  var traverse = module2.exports = function(schema, opts, cb) {
    if (typeof opts == "function") {
      cb = opts;
      opts = {};
    }
    cb = opts.cb || cb;
    var pre = typeof cb == "function" ? cb : cb.pre || function() {
    };
    var post = cb.post || function() {
    };
    _traverse(opts, pre, post, schema, "", schema);
  };
  traverse.keywords = {
    additionalItems: true,
    items: true,
    contains: true,
    additionalProperties: true,
    propertyNames: true,
    not: true,
    if: true,
    then: true,
    else: true
  };
  traverse.arrayKeywords = {
    items: true,
    allOf: true,
    anyOf: true,
    oneOf: true
  };
  traverse.propsKeywords = {
    $defs: true,
    definitions: true,
    properties: true,
    patternProperties: true,
    dependencies: true
  };
  traverse.skipKeywords = {
    default: true,
    enum: true,
    const: true,
    required: true,
    maximum: true,
    minimum: true,
    exclusiveMaximum: true,
    exclusiveMinimum: true,
    multipleOf: true,
    maxLength: true,
    minLength: true,
    pattern: true,
    format: true,
    maxItems: true,
    minItems: true,
    uniqueItems: true,
    maxProperties: true,
    minProperties: true
  };
  function _traverse(opts, pre, post, schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex) {
    if (schema && typeof schema == "object" && !Array.isArray(schema)) {
      pre(schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex);
      for (var key in schema) {
        var sch = schema[key];
        if (Array.isArray(sch)) {
          if (key in traverse.arrayKeywords) for (var i = 0; i < sch.length; i++) _traverse(opts, pre, post, sch[i], jsonPtr + "/" + key + "/" + i, rootSchema, jsonPtr, key, schema, i);
        } else if (key in traverse.propsKeywords) {
          if (sch && typeof sch == "object") for (var prop in sch) _traverse(opts, pre, post, sch[prop], jsonPtr + "/" + key + "/" + escapeJsonPtr(prop), rootSchema, jsonPtr, key, schema, prop);
        } else if (key in traverse.keywords || opts.allKeys && !(key in traverse.skipKeywords)) _traverse(opts, pre, post, sch, jsonPtr + "/" + key, rootSchema, jsonPtr, key, schema);
      }
      post(schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex);
    }
  }
  function escapeJsonPtr(str) {
    return str.replace(/~/g, "~0").replace(/\//g, "~1");
  }
}));
var require_resolve = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.getSchemaRefs = exports2.resolveUrl = exports2.normalizeId = exports2._getFullPath = exports2.getFullPath = exports2.inlineRef = void 0;
  const util_1 = require_util();
  const equal = require_fast_deep_equal();
  const traverse = require_json_schema_traverse();
  const SIMPLE_INLINED = /* @__PURE__ */ new Set([
    "type",
    "format",
    "pattern",
    "maxLength",
    "minLength",
    "maxProperties",
    "minProperties",
    "maxItems",
    "minItems",
    "maximum",
    "minimum",
    "uniqueItems",
    "multipleOf",
    "required",
    "enum",
    "const"
  ]);
  function inlineRef(schema, limit = true) {
    if (typeof schema == "boolean") return true;
    if (limit === true) return !hasRef(schema);
    if (!limit) return false;
    return countKeys(schema) <= limit;
  }
  exports2.inlineRef = inlineRef;
  const REF_KEYWORDS = /* @__PURE__ */ new Set([
    "$ref",
    "$recursiveRef",
    "$recursiveAnchor",
    "$dynamicRef",
    "$dynamicAnchor"
  ]);
  function hasRef(schema) {
    for (const key in schema) {
      if (REF_KEYWORDS.has(key)) return true;
      const sch = schema[key];
      if (Array.isArray(sch) && sch.some(hasRef)) return true;
      if (typeof sch == "object" && hasRef(sch)) return true;
    }
    return false;
  }
  function countKeys(schema) {
    let count = 0;
    for (const key in schema) {
      if (key === "$ref") return Infinity;
      count++;
      if (SIMPLE_INLINED.has(key)) continue;
      if (typeof schema[key] == "object") (0, util_1.eachItem)(schema[key], (sch) => count += countKeys(sch));
      if (count === Infinity) return Infinity;
    }
    return count;
  }
  function getFullPath(resolver, id = "", normalize) {
    if (normalize !== false) id = normalizeId(id);
    return _getFullPath(resolver, resolver.parse(id));
  }
  exports2.getFullPath = getFullPath;
  function _getFullPath(resolver, p) {
    return resolver.serialize(p).split("#")[0] + "#";
  }
  exports2._getFullPath = _getFullPath;
  const TRAILING_SLASH_HASH = /#\/?$/;
  function normalizeId(id) {
    return id ? id.replace(TRAILING_SLASH_HASH, "") : "";
  }
  exports2.normalizeId = normalizeId;
  function resolveUrl(resolver, baseId, id) {
    id = normalizeId(id);
    return resolver.resolve(baseId, id);
  }
  exports2.resolveUrl = resolveUrl;
  const ANCHOR = /^[a-z_][-a-z0-9._]*$/i;
  function getSchemaRefs(schema, baseId) {
    if (typeof schema == "boolean") return {};
    const { schemaId, uriResolver } = this.opts;
    const schId = normalizeId(schema[schemaId] || baseId);
    const baseIds = { "": schId };
    const pathPrefix = getFullPath(uriResolver, schId, false);
    const localRefs = {};
    const schemaRefs = /* @__PURE__ */ new Set();
    traverse(schema, { allKeys: true }, (sch, jsonPtr, _, parentJsonPtr) => {
      if (parentJsonPtr === void 0) return;
      const fullPath = pathPrefix + jsonPtr;
      let innerBaseId = baseIds[parentJsonPtr];
      if (typeof sch[schemaId] == "string") innerBaseId = addRef.call(this, sch[schemaId]);
      addAnchor.call(this, sch.$anchor);
      addAnchor.call(this, sch.$dynamicAnchor);
      baseIds[jsonPtr] = innerBaseId;
      function addRef(ref) {
        const _resolve = this.opts.uriResolver.resolve;
        ref = normalizeId(innerBaseId ? _resolve(innerBaseId, ref) : ref);
        if (schemaRefs.has(ref)) throw ambiguos(ref);
        schemaRefs.add(ref);
        let schOrRef = this.refs[ref];
        if (typeof schOrRef == "string") schOrRef = this.refs[schOrRef];
        if (typeof schOrRef == "object") checkAmbiguosRef(sch, schOrRef.schema, ref);
        else if (ref !== normalizeId(fullPath)) if (ref[0] === "#") {
          checkAmbiguosRef(sch, localRefs[ref], ref);
          localRefs[ref] = sch;
        } else this.refs[ref] = fullPath;
        return ref;
      }
      function addAnchor(anchor) {
        if (typeof anchor == "string") {
          if (!ANCHOR.test(anchor)) throw new Error(`invalid anchor "${anchor}"`);
          addRef.call(this, `#${anchor}`);
        }
      }
    });
    return localRefs;
    function checkAmbiguosRef(sch1, sch2, ref) {
      if (sch2 !== void 0 && !equal(sch1, sch2)) throw ambiguos(ref);
    }
    function ambiguos(ref) {
      return /* @__PURE__ */ new Error(`reference "${ref}" resolves to more than one schema`);
    }
  }
  exports2.getSchemaRefs = getSchemaRefs;
}));
var require_validate = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.getData = exports2.KeywordCxt = exports2.validateFunctionCode = void 0;
  const boolSchema_1 = require_boolSchema();
  const dataType_1 = require_dataType();
  const applicability_1 = require_applicability();
  const dataType_2 = require_dataType();
  const defaults_1 = require_defaults();
  const keyword_1 = require_keyword();
  const subschema_1 = require_subschema();
  const codegen_1 = require_codegen();
  const names_1 = require_names();
  const resolve_1 = require_resolve();
  const util_1 = require_util();
  const errors_1 = require_errors();
  function validateFunctionCode(it) {
    if (isSchemaObj(it)) {
      checkKeywords(it);
      if (schemaCxtHasRules(it)) {
        topSchemaObjCode(it);
        return;
      }
    }
    validateFunction(it, () => (0, boolSchema_1.topBoolOrEmptySchema)(it));
  }
  exports2.validateFunctionCode = validateFunctionCode;
  function validateFunction({ gen, validateName, schema, schemaEnv, opts }, body) {
    if (opts.code.es5) gen.func(validateName, (0, codegen_1._)`${names_1.default.data}, ${names_1.default.valCxt}`, schemaEnv.$async, () => {
      gen.code((0, codegen_1._)`"use strict"; ${funcSourceUrl(schema, opts)}`);
      destructureValCxtES5(gen, opts);
      gen.code(body);
    });
    else gen.func(validateName, (0, codegen_1._)`${names_1.default.data}, ${destructureValCxt(opts)}`, schemaEnv.$async, () => gen.code(funcSourceUrl(schema, opts)).code(body));
  }
  function destructureValCxt(opts) {
    return (0, codegen_1._)`{${names_1.default.instancePath}="", ${names_1.default.parentData}, ${names_1.default.parentDataProperty}, ${names_1.default.rootData}=${names_1.default.data}${opts.dynamicRef ? (0, codegen_1._)`, ${names_1.default.dynamicAnchors}={}` : codegen_1.nil}}={}`;
  }
  function destructureValCxtES5(gen, opts) {
    gen.if(names_1.default.valCxt, () => {
      gen.var(names_1.default.instancePath, (0, codegen_1._)`${names_1.default.valCxt}.${names_1.default.instancePath}`);
      gen.var(names_1.default.parentData, (0, codegen_1._)`${names_1.default.valCxt}.${names_1.default.parentData}`);
      gen.var(names_1.default.parentDataProperty, (0, codegen_1._)`${names_1.default.valCxt}.${names_1.default.parentDataProperty}`);
      gen.var(names_1.default.rootData, (0, codegen_1._)`${names_1.default.valCxt}.${names_1.default.rootData}`);
      if (opts.dynamicRef) gen.var(names_1.default.dynamicAnchors, (0, codegen_1._)`${names_1.default.valCxt}.${names_1.default.dynamicAnchors}`);
    }, () => {
      gen.var(names_1.default.instancePath, (0, codegen_1._)`""`);
      gen.var(names_1.default.parentData, (0, codegen_1._)`undefined`);
      gen.var(names_1.default.parentDataProperty, (0, codegen_1._)`undefined`);
      gen.var(names_1.default.rootData, names_1.default.data);
      if (opts.dynamicRef) gen.var(names_1.default.dynamicAnchors, (0, codegen_1._)`{}`);
    });
  }
  function topSchemaObjCode(it) {
    const { schema, opts, gen } = it;
    validateFunction(it, () => {
      if (opts.$comment && schema.$comment) commentKeyword(it);
      checkNoDefault(it);
      gen.let(names_1.default.vErrors, null);
      gen.let(names_1.default.errors, 0);
      if (opts.unevaluated) resetEvaluated(it);
      typeAndKeywords(it);
      returnResults(it);
    });
  }
  function resetEvaluated(it) {
    const { gen, validateName } = it;
    it.evaluated = gen.const("evaluated", (0, codegen_1._)`${validateName}.evaluated`);
    gen.if((0, codegen_1._)`${it.evaluated}.dynamicProps`, () => gen.assign((0, codegen_1._)`${it.evaluated}.props`, (0, codegen_1._)`undefined`));
    gen.if((0, codegen_1._)`${it.evaluated}.dynamicItems`, () => gen.assign((0, codegen_1._)`${it.evaluated}.items`, (0, codegen_1._)`undefined`));
  }
  function funcSourceUrl(schema, opts) {
    const schId = typeof schema == "object" && schema[opts.schemaId];
    return schId && (opts.code.source || opts.code.process) ? (0, codegen_1._)`/*# sourceURL=${schId} */` : codegen_1.nil;
  }
  function subschemaCode(it, valid) {
    if (isSchemaObj(it)) {
      checkKeywords(it);
      if (schemaCxtHasRules(it)) {
        subSchemaObjCode(it, valid);
        return;
      }
    }
    (0, boolSchema_1.boolOrEmptySchema)(it, valid);
  }
  function schemaCxtHasRules({ schema, self: self2 }) {
    if (typeof schema == "boolean") return !schema;
    for (const key in schema) if (self2.RULES.all[key]) return true;
    return false;
  }
  function isSchemaObj(it) {
    return typeof it.schema != "boolean";
  }
  function subSchemaObjCode(it, valid) {
    const { schema, gen, opts } = it;
    if (opts.$comment && schema.$comment) commentKeyword(it);
    updateContext(it);
    checkAsyncSchema(it);
    const errsCount = gen.const("_errs", names_1.default.errors);
    typeAndKeywords(it, errsCount);
    gen.var(valid, (0, codegen_1._)`${errsCount} === ${names_1.default.errors}`);
  }
  function checkKeywords(it) {
    (0, util_1.checkUnknownRules)(it);
    checkRefsAndKeywords(it);
  }
  function typeAndKeywords(it, errsCount) {
    if (it.opts.jtd) return schemaKeywords(it, [], false, errsCount);
    const types = (0, dataType_1.getSchemaTypes)(it.schema);
    schemaKeywords(it, types, !(0, dataType_1.coerceAndCheckDataType)(it, types), errsCount);
  }
  function checkRefsAndKeywords(it) {
    const { schema, errSchemaPath, opts, self: self2 } = it;
    if (schema.$ref && opts.ignoreKeywordsWithRef && (0, util_1.schemaHasRulesButRef)(schema, self2.RULES)) self2.logger.warn(`$ref: keywords ignored in schema at path "${errSchemaPath}"`);
  }
  function checkNoDefault(it) {
    const { schema, opts } = it;
    if (schema.default !== void 0 && opts.useDefaults && opts.strictSchema) (0, util_1.checkStrictMode)(it, "default is ignored in the schema root");
  }
  function updateContext(it) {
    const schId = it.schema[it.opts.schemaId];
    if (schId) it.baseId = (0, resolve_1.resolveUrl)(it.opts.uriResolver, it.baseId, schId);
  }
  function checkAsyncSchema(it) {
    if (it.schema.$async && !it.schemaEnv.$async) throw new Error("async schema in sync schema");
  }
  function commentKeyword({ gen, schemaEnv, schema, errSchemaPath, opts }) {
    const msg = schema.$comment;
    if (opts.$comment === true) gen.code((0, codegen_1._)`${names_1.default.self}.logger.log(${msg})`);
    else if (typeof opts.$comment == "function") {
      const schemaPath = (0, codegen_1.str)`${errSchemaPath}/$comment`;
      const rootName = gen.scopeValue("root", { ref: schemaEnv.root });
      gen.code((0, codegen_1._)`${names_1.default.self}.opts.$comment(${msg}, ${schemaPath}, ${rootName}.schema)`);
    }
  }
  function returnResults(it) {
    const { gen, schemaEnv, validateName, ValidationError, opts } = it;
    if (schemaEnv.$async) gen.if((0, codegen_1._)`${names_1.default.errors} === 0`, () => gen.return(names_1.default.data), () => gen.throw((0, codegen_1._)`new ${ValidationError}(${names_1.default.vErrors})`));
    else {
      gen.assign((0, codegen_1._)`${validateName}.errors`, names_1.default.vErrors);
      if (opts.unevaluated) assignEvaluated(it);
      gen.return((0, codegen_1._)`${names_1.default.errors} === 0`);
    }
  }
  function assignEvaluated({ gen, evaluated, props, items }) {
    if (props instanceof codegen_1.Name) gen.assign((0, codegen_1._)`${evaluated}.props`, props);
    if (items instanceof codegen_1.Name) gen.assign((0, codegen_1._)`${evaluated}.items`, items);
  }
  function schemaKeywords(it, types, typeErrors, errsCount) {
    const { gen, schema, data, allErrors, opts, self: self2 } = it;
    const { RULES } = self2;
    if (schema.$ref && (opts.ignoreKeywordsWithRef || !(0, util_1.schemaHasRulesButRef)(schema, RULES))) {
      gen.block(() => keywordCode(it, "$ref", RULES.all.$ref.definition));
      return;
    }
    if (!opts.jtd) checkStrictTypes(it, types);
    gen.block(() => {
      for (const group of RULES.rules) groupKeywords(group);
      groupKeywords(RULES.post);
    });
    function groupKeywords(group) {
      if (!(0, applicability_1.shouldUseGroup)(schema, group)) return;
      if (group.type) {
        gen.if((0, dataType_2.checkDataType)(group.type, data, opts.strictNumbers));
        iterateKeywords(it, group);
        if (types.length === 1 && types[0] === group.type && typeErrors) {
          gen.else();
          (0, dataType_2.reportTypeError)(it);
        }
        gen.endIf();
      } else iterateKeywords(it, group);
      if (!allErrors) gen.if((0, codegen_1._)`${names_1.default.errors} === ${errsCount || 0}`);
    }
  }
  function iterateKeywords(it, group) {
    const { gen, schema, opts: { useDefaults } } = it;
    if (useDefaults) (0, defaults_1.assignDefaults)(it, group.type);
    gen.block(() => {
      for (const rule of group.rules) if ((0, applicability_1.shouldUseRule)(schema, rule)) keywordCode(it, rule.keyword, rule.definition, group.type);
    });
  }
  function checkStrictTypes(it, types) {
    if (it.schemaEnv.meta || !it.opts.strictTypes) return;
    checkContextTypes(it, types);
    if (!it.opts.allowUnionTypes) checkMultipleTypes(it, types);
    checkKeywordTypes(it, it.dataTypes);
  }
  function checkContextTypes(it, types) {
    if (!types.length) return;
    if (!it.dataTypes.length) {
      it.dataTypes = types;
      return;
    }
    types.forEach((t) => {
      if (!includesType(it.dataTypes, t)) strictTypesError(it, `type "${t}" not allowed by context "${it.dataTypes.join(",")}"`);
    });
    narrowSchemaTypes(it, types);
  }
  function checkMultipleTypes(it, ts) {
    if (ts.length > 1 && !(ts.length === 2 && ts.includes("null"))) strictTypesError(it, "use allowUnionTypes to allow union type keyword");
  }
  function checkKeywordTypes(it, ts) {
    const rules = it.self.RULES.all;
    for (const keyword in rules) {
      const rule = rules[keyword];
      if (typeof rule == "object" && (0, applicability_1.shouldUseRule)(it.schema, rule)) {
        const { type } = rule.definition;
        if (type.length && !type.some((t) => hasApplicableType(ts, t))) strictTypesError(it, `missing type "${type.join(",")}" for keyword "${keyword}"`);
      }
    }
  }
  function hasApplicableType(schTs, kwdT) {
    return schTs.includes(kwdT) || kwdT === "number" && schTs.includes("integer");
  }
  function includesType(ts, t) {
    return ts.includes(t) || t === "integer" && ts.includes("number");
  }
  function narrowSchemaTypes(it, withTypes) {
    const ts = [];
    for (const t of it.dataTypes) if (includesType(withTypes, t)) ts.push(t);
    else if (withTypes.includes("integer") && t === "number") ts.push("integer");
    it.dataTypes = ts;
  }
  function strictTypesError(it, msg) {
    const schemaPath = it.schemaEnv.baseId + it.errSchemaPath;
    msg += ` at "${schemaPath}" (strictTypes)`;
    (0, util_1.checkStrictMode)(it, msg, it.opts.strictTypes);
  }
  var KeywordCxt = class {
    constructor(it, def, keyword) {
      (0, keyword_1.validateKeywordUsage)(it, def, keyword);
      this.gen = it.gen;
      this.allErrors = it.allErrors;
      this.keyword = keyword;
      this.data = it.data;
      this.schema = it.schema[keyword];
      this.$data = def.$data && it.opts.$data && this.schema && this.schema.$data;
      this.schemaValue = (0, util_1.schemaRefOrVal)(it, this.schema, keyword, this.$data);
      this.schemaType = def.schemaType;
      this.parentSchema = it.schema;
      this.params = {};
      this.it = it;
      this.def = def;
      if (this.$data) this.schemaCode = it.gen.const("vSchema", getData(this.$data, it));
      else {
        this.schemaCode = this.schemaValue;
        if (!(0, keyword_1.validSchemaType)(this.schema, def.schemaType, def.allowUndefined)) throw new Error(`${keyword} value must be ${JSON.stringify(def.schemaType)}`);
      }
      if ("code" in def ? def.trackErrors : def.errors !== false) this.errsCount = it.gen.const("_errs", names_1.default.errors);
    }
    result(condition, successAction, failAction) {
      this.failResult((0, codegen_1.not)(condition), successAction, failAction);
    }
    failResult(condition, successAction, failAction) {
      this.gen.if(condition);
      if (failAction) failAction();
      else this.error();
      if (successAction) {
        this.gen.else();
        successAction();
        if (this.allErrors) this.gen.endIf();
      } else if (this.allErrors) this.gen.endIf();
      else this.gen.else();
    }
    pass(condition, failAction) {
      this.failResult((0, codegen_1.not)(condition), void 0, failAction);
    }
    fail(condition) {
      if (condition === void 0) {
        this.error();
        if (!this.allErrors) this.gen.if(false);
        return;
      }
      this.gen.if(condition);
      this.error();
      if (this.allErrors) this.gen.endIf();
      else this.gen.else();
    }
    fail$data(condition) {
      if (!this.$data) return this.fail(condition);
      const { schemaCode } = this;
      this.fail((0, codegen_1._)`${schemaCode} !== undefined && (${(0, codegen_1.or)(this.invalid$data(), condition)})`);
    }
    error(append, errorParams, errorPaths) {
      if (errorParams) {
        this.setParams(errorParams);
        this._error(append, errorPaths);
        this.setParams({});
        return;
      }
      this._error(append, errorPaths);
    }
    _error(append, errorPaths) {
      (append ? errors_1.reportExtraError : errors_1.reportError)(this, this.def.error, errorPaths);
    }
    $dataError() {
      (0, errors_1.reportError)(this, this.def.$dataError || errors_1.keyword$DataError);
    }
    reset() {
      if (this.errsCount === void 0) throw new Error('add "trackErrors" to keyword definition');
      (0, errors_1.resetErrorsCount)(this.gen, this.errsCount);
    }
    ok(cond) {
      if (!this.allErrors) this.gen.if(cond);
    }
    setParams(obj, assign) {
      if (assign) Object.assign(this.params, obj);
      else this.params = obj;
    }
    block$data(valid, codeBlock, $dataValid = codegen_1.nil) {
      this.gen.block(() => {
        this.check$data(valid, $dataValid);
        codeBlock();
      });
    }
    check$data(valid = codegen_1.nil, $dataValid = codegen_1.nil) {
      if (!this.$data) return;
      const { gen, schemaCode, schemaType, def } = this;
      gen.if((0, codegen_1.or)((0, codegen_1._)`${schemaCode} === undefined`, $dataValid));
      if (valid !== codegen_1.nil) gen.assign(valid, true);
      if (schemaType.length || def.validateSchema) {
        gen.elseIf(this.invalid$data());
        this.$dataError();
        if (valid !== codegen_1.nil) gen.assign(valid, false);
      }
      gen.else();
    }
    invalid$data() {
      const { gen, schemaCode, schemaType, def, it } = this;
      return (0, codegen_1.or)(wrong$DataType(), invalid$DataSchema());
      function wrong$DataType() {
        if (schemaType.length) {
          if (!(schemaCode instanceof codegen_1.Name)) throw new Error("ajv implementation error");
          const st = Array.isArray(schemaType) ? schemaType : [schemaType];
          return (0, codegen_1._)`${(0, dataType_2.checkDataTypes)(st, schemaCode, it.opts.strictNumbers, dataType_2.DataType.Wrong)}`;
        }
        return codegen_1.nil;
      }
      function invalid$DataSchema() {
        if (def.validateSchema) {
          const validateSchemaRef = gen.scopeValue("validate$data", { ref: def.validateSchema });
          return (0, codegen_1._)`!${validateSchemaRef}(${schemaCode})`;
        }
        return codegen_1.nil;
      }
    }
    subschema(appl, valid) {
      const subschema = (0, subschema_1.getSubschema)(this.it, appl);
      (0, subschema_1.extendSubschemaData)(subschema, this.it, appl);
      (0, subschema_1.extendSubschemaMode)(subschema, appl);
      const nextContext = {
        ...this.it,
        ...subschema,
        items: void 0,
        props: void 0
      };
      subschemaCode(nextContext, valid);
      return nextContext;
    }
    mergeEvaluated(schemaCxt, toName) {
      const { it, gen } = this;
      if (!it.opts.unevaluated) return;
      if (it.props !== true && schemaCxt.props !== void 0) it.props = util_1.mergeEvaluated.props(gen, schemaCxt.props, it.props, toName);
      if (it.items !== true && schemaCxt.items !== void 0) it.items = util_1.mergeEvaluated.items(gen, schemaCxt.items, it.items, toName);
    }
    mergeValidEvaluated(schemaCxt, valid) {
      const { it, gen } = this;
      if (it.opts.unevaluated && (it.props !== true || it.items !== true)) {
        gen.if(valid, () => this.mergeEvaluated(schemaCxt, codegen_1.Name));
        return true;
      }
    }
  };
  exports2.KeywordCxt = KeywordCxt;
  function keywordCode(it, keyword, def, ruleType) {
    const cxt = new KeywordCxt(it, def, keyword);
    if ("code" in def) def.code(cxt, ruleType);
    else if (cxt.$data && def.validate) (0, keyword_1.funcKeywordCode)(cxt, def);
    else if ("macro" in def) (0, keyword_1.macroKeywordCode)(cxt, def);
    else if (def.compile || def.validate) (0, keyword_1.funcKeywordCode)(cxt, def);
  }
  const JSON_POINTER2 = /^\/(?:[^~]|~0|~1)*$/;
  const RELATIVE_JSON_POINTER2 = /^([0-9]+)(#|\/(?:[^~]|~0|~1)*)?$/;
  function getData($data, { dataLevel, dataNames, dataPathArr }) {
    let jsonPointer;
    let data;
    if ($data === "") return names_1.default.rootData;
    if ($data[0] === "/") {
      if (!JSON_POINTER2.test($data)) throw new Error(`Invalid JSON-pointer: ${$data}`);
      jsonPointer = $data;
      data = names_1.default.rootData;
    } else {
      const matches = RELATIVE_JSON_POINTER2.exec($data);
      if (!matches) throw new Error(`Invalid JSON-pointer: ${$data}`);
      const up = +matches[1];
      jsonPointer = matches[2];
      if (jsonPointer === "#") {
        if (up >= dataLevel) throw new Error(errorMsg("property/index", up));
        return dataPathArr[dataLevel - up];
      }
      if (up > dataLevel) throw new Error(errorMsg("data", up));
      data = dataNames[dataLevel - up];
      if (!jsonPointer) return data;
    }
    let expr = data;
    const segments = jsonPointer.split("/");
    for (const segment of segments) if (segment) {
      data = (0, codegen_1._)`${data}${(0, codegen_1.getProperty)((0, util_1.unescapeJsonPointer)(segment))}`;
      expr = (0, codegen_1._)`${expr} && ${data}`;
    }
    return expr;
    function errorMsg(pointerType, up) {
      return `Cannot access ${pointerType} ${up} levels up, current level is ${dataLevel}`;
    }
  }
  exports2.getData = getData;
}));
var require_validation_error = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  var ValidationError = class extends Error {
    constructor(errors) {
      super("validation failed");
      this.errors = errors;
      this.ajv = this.validation = true;
    }
  };
  exports2.default = ValidationError;
}));
var require_ref_error = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const resolve_1 = require_resolve();
  var MissingRefError = class extends Error {
    constructor(resolver, baseId, ref, msg) {
      super(msg || `can't resolve reference ${ref} from id ${baseId}`);
      this.missingRef = (0, resolve_1.resolveUrl)(resolver, baseId, ref);
      this.missingSchema = (0, resolve_1.normalizeId)((0, resolve_1.getFullPath)(resolver, this.missingRef));
    }
  };
  exports2.default = MissingRefError;
}));
var require_compile = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.resolveSchema = exports2.getCompilingSchema = exports2.resolveRef = exports2.compileSchema = exports2.SchemaEnv = void 0;
  const codegen_1 = require_codegen();
  const validation_error_1 = require_validation_error();
  const names_1 = require_names();
  const resolve_1 = require_resolve();
  const util_1 = require_util();
  const validate_1 = require_validate();
  var SchemaEnv = class {
    constructor(env) {
      var _a2;
      this.refs = {};
      this.dynamicAnchors = {};
      let schema;
      if (typeof env.schema == "object") schema = env.schema;
      this.schema = env.schema;
      this.schemaId = env.schemaId;
      this.root = env.root || this;
      this.baseId = (_a2 = env.baseId) !== null && _a2 !== void 0 ? _a2 : (0, resolve_1.normalizeId)(schema === null || schema === void 0 ? void 0 : schema[env.schemaId || "$id"]);
      this.schemaPath = env.schemaPath;
      this.localRefs = env.localRefs;
      this.meta = env.meta;
      this.$async = schema === null || schema === void 0 ? void 0 : schema.$async;
      this.refs = {};
    }
  };
  exports2.SchemaEnv = SchemaEnv;
  function compileSchema(sch) {
    const _sch = getCompilingSchema.call(this, sch);
    if (_sch) return _sch;
    const rootId = (0, resolve_1.getFullPath)(this.opts.uriResolver, sch.root.baseId);
    const { es5, lines } = this.opts.code;
    const { ownProperties } = this.opts;
    const gen = new codegen_1.CodeGen(this.scope, {
      es5,
      lines,
      ownProperties
    });
    let _ValidationError;
    if (sch.$async) _ValidationError = gen.scopeValue("Error", {
      ref: validation_error_1.default,
      code: (0, codegen_1._)`require("ajv/dist/runtime/validation_error").default`
    });
    const validateName = gen.scopeName("validate");
    sch.validateName = validateName;
    const schemaCxt = {
      gen,
      allErrors: this.opts.allErrors,
      data: names_1.default.data,
      parentData: names_1.default.parentData,
      parentDataProperty: names_1.default.parentDataProperty,
      dataNames: [names_1.default.data],
      dataPathArr: [codegen_1.nil],
      dataLevel: 0,
      dataTypes: [],
      definedProperties: /* @__PURE__ */ new Set(),
      topSchemaRef: gen.scopeValue("schema", this.opts.code.source === true ? {
        ref: sch.schema,
        code: (0, codegen_1.stringify)(sch.schema)
      } : { ref: sch.schema }),
      validateName,
      ValidationError: _ValidationError,
      schema: sch.schema,
      schemaEnv: sch,
      rootId,
      baseId: sch.baseId || rootId,
      schemaPath: codegen_1.nil,
      errSchemaPath: sch.schemaPath || (this.opts.jtd ? "" : "#"),
      errorPath: (0, codegen_1._)`""`,
      opts: this.opts,
      self: this
    };
    let sourceCode;
    try {
      this._compilations.add(sch);
      (0, validate_1.validateFunctionCode)(schemaCxt);
      gen.optimize(this.opts.code.optimize);
      const validateCode = gen.toString();
      sourceCode = `${gen.scopeRefs(names_1.default.scope)}return ${validateCode}`;
      if (this.opts.code.process) sourceCode = this.opts.code.process(sourceCode, sch);
      const validate2 = new Function(`${names_1.default.self}`, `${names_1.default.scope}`, sourceCode)(this, this.scope.get());
      this.scope.value(validateName, { ref: validate2 });
      validate2.errors = null;
      validate2.schema = sch.schema;
      validate2.schemaEnv = sch;
      if (sch.$async) validate2.$async = true;
      if (this.opts.code.source === true) validate2.source = {
        validateName,
        validateCode,
        scopeValues: gen._values
      };
      if (this.opts.unevaluated) {
        const { props, items } = schemaCxt;
        validate2.evaluated = {
          props: props instanceof codegen_1.Name ? void 0 : props,
          items: items instanceof codegen_1.Name ? void 0 : items,
          dynamicProps: props instanceof codegen_1.Name,
          dynamicItems: items instanceof codegen_1.Name
        };
        if (validate2.source) validate2.source.evaluated = (0, codegen_1.stringify)(validate2.evaluated);
      }
      sch.validate = validate2;
      return sch;
    } catch (e) {
      delete sch.validate;
      delete sch.validateName;
      if (sourceCode) this.logger.error("Error compiling schema, function code:", sourceCode);
      throw e;
    } finally {
      this._compilations.delete(sch);
    }
  }
  exports2.compileSchema = compileSchema;
  function resolveRef(root, baseId, ref) {
    var _a2;
    ref = (0, resolve_1.resolveUrl)(this.opts.uriResolver, baseId, ref);
    const schOrFunc = root.refs[ref];
    if (schOrFunc) return schOrFunc;
    let _sch = resolve.call(this, root, ref);
    if (_sch === void 0) {
      const schema = (_a2 = root.localRefs) === null || _a2 === void 0 ? void 0 : _a2[ref];
      const { schemaId } = this.opts;
      if (schema) _sch = new SchemaEnv({
        schema,
        schemaId,
        root,
        baseId
      });
    }
    if (_sch === void 0) return;
    return root.refs[ref] = inlineOrCompile.call(this, _sch);
  }
  exports2.resolveRef = resolveRef;
  function inlineOrCompile(sch) {
    if ((0, resolve_1.inlineRef)(sch.schema, this.opts.inlineRefs)) return sch.schema;
    return sch.validate ? sch : compileSchema.call(this, sch);
  }
  function getCompilingSchema(schEnv) {
    for (const sch of this._compilations) if (sameSchemaEnv(sch, schEnv)) return sch;
  }
  exports2.getCompilingSchema = getCompilingSchema;
  function sameSchemaEnv(s1, s2) {
    return s1.schema === s2.schema && s1.root === s2.root && s1.baseId === s2.baseId;
  }
  function resolve(root, ref) {
    let sch;
    while (typeof (sch = this.refs[ref]) == "string") ref = sch;
    return sch || this.schemas[ref] || resolveSchema.call(this, root, ref);
  }
  function resolveSchema(root, ref) {
    const p = this.opts.uriResolver.parse(ref);
    const refPath = (0, resolve_1._getFullPath)(this.opts.uriResolver, p);
    let baseId = (0, resolve_1.getFullPath)(this.opts.uriResolver, root.baseId, void 0);
    if (Object.keys(root.schema).length > 0 && refPath === baseId) return getJsonPointer.call(this, p, root);
    const id = (0, resolve_1.normalizeId)(refPath);
    const schOrRef = this.refs[id] || this.schemas[id];
    if (typeof schOrRef == "string") {
      const sch = resolveSchema.call(this, root, schOrRef);
      if (typeof (sch === null || sch === void 0 ? void 0 : sch.schema) !== "object") return;
      return getJsonPointer.call(this, p, sch);
    }
    if (typeof (schOrRef === null || schOrRef === void 0 ? void 0 : schOrRef.schema) !== "object") return;
    if (!schOrRef.validate) compileSchema.call(this, schOrRef);
    if (id === (0, resolve_1.normalizeId)(ref)) {
      const { schema } = schOrRef;
      const { schemaId } = this.opts;
      const schId = schema[schemaId];
      if (schId) baseId = (0, resolve_1.resolveUrl)(this.opts.uriResolver, baseId, schId);
      return new SchemaEnv({
        schema,
        schemaId,
        root,
        baseId
      });
    }
    return getJsonPointer.call(this, p, schOrRef);
  }
  exports2.resolveSchema = resolveSchema;
  const PREVENT_SCOPE_CHANGE = /* @__PURE__ */ new Set([
    "properties",
    "patternProperties",
    "enum",
    "dependencies",
    "definitions"
  ]);
  function getJsonPointer(parsedRef, { baseId, schema, root }) {
    var _a2;
    if (((_a2 = parsedRef.fragment) === null || _a2 === void 0 ? void 0 : _a2[0]) !== "/") return;
    for (const part of parsedRef.fragment.slice(1).split("/")) {
      if (typeof schema === "boolean") return;
      const partSchema = schema[(0, util_1.unescapeFragment)(part)];
      if (partSchema === void 0) return;
      schema = partSchema;
      const schId = typeof schema === "object" && schema[this.opts.schemaId];
      if (!PREVENT_SCOPE_CHANGE.has(part) && schId) baseId = (0, resolve_1.resolveUrl)(this.opts.uriResolver, baseId, schId);
    }
    let env;
    if (typeof schema != "boolean" && schema.$ref && !(0, util_1.schemaHasRulesButRef)(schema, this.RULES)) {
      const $ref = (0, resolve_1.resolveUrl)(this.opts.uriResolver, baseId, schema.$ref);
      env = resolveSchema.call(this, root, $ref);
    }
    const { schemaId } = this.opts;
    env = env || new SchemaEnv({
      schema,
      schemaId,
      root,
      baseId
    });
    if (env.schema !== env.root.schema) return env;
  }
}));
var require_data = /* @__PURE__ */ __commonJSMin(((exports2, module2) => {
  module2.exports = {
    "$id": "https://raw.githubusercontent.com/ajv-validator/ajv/master/lib/refs/data.json#",
    "description": "Meta-schema for $data reference (JSON AnySchema extension proposal)",
    "type": "object",
    "required": ["$data"],
    "properties": { "$data": {
      "type": "string",
      "anyOf": [{ "format": "relative-json-pointer" }, { "format": "json-pointer" }]
    } },
    "additionalProperties": false
  };
}));
var require_utils = /* @__PURE__ */ __commonJSMin(((exports2, module2) => {
  const isUUID = RegExp.prototype.test.bind(/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iu);
  const isIPv4 = RegExp.prototype.test.bind(/^(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$/u);
  function stringArrayToHexStripped(input) {
    let acc = "";
    let code = 0;
    let i = 0;
    for (i = 0; i < input.length; i++) {
      code = input[i].charCodeAt(0);
      if (code === 48) continue;
      if (!(code >= 48 && code <= 57 || code >= 65 && code <= 70 || code >= 97 && code <= 102)) return "";
      acc += input[i];
      break;
    }
    for (i += 1; i < input.length; i++) {
      code = input[i].charCodeAt(0);
      if (!(code >= 48 && code <= 57 || code >= 65 && code <= 70 || code >= 97 && code <= 102)) return "";
      acc += input[i];
    }
    return acc;
  }
  const nonSimpleDomain = RegExp.prototype.test.bind(/[^!"$&'()*+,\-.;=_`a-z{}~]/u);
  function consumeIsZone(buffer) {
    buffer.length = 0;
    return true;
  }
  function consumeHextets(buffer, address, output) {
    if (buffer.length) {
      const hex3 = stringArrayToHexStripped(buffer);
      if (hex3 !== "") address.push(hex3);
      else {
        output.error = true;
        return false;
      }
      buffer.length = 0;
    }
    return true;
  }
  function getIPV6(input) {
    let tokenCount = 0;
    const output = {
      error: false,
      address: "",
      zone: ""
    };
    const address = [];
    const buffer = [];
    let endipv6Encountered = false;
    let endIpv6 = false;
    let consume = consumeHextets;
    for (let i = 0; i < input.length; i++) {
      const cursor = input[i];
      if (cursor === "[" || cursor === "]") continue;
      if (cursor === ":") {
        if (endipv6Encountered === true) endIpv6 = true;
        if (!consume(buffer, address, output)) break;
        if (++tokenCount > 7) {
          output.error = true;
          break;
        }
        if (i > 0 && input[i - 1] === ":") endipv6Encountered = true;
        address.push(":");
        continue;
      } else if (cursor === "%") {
        if (!consume(buffer, address, output)) break;
        consume = consumeIsZone;
      } else {
        buffer.push(cursor);
        continue;
      }
    }
    if (buffer.length) if (consume === consumeIsZone) output.zone = buffer.join("");
    else if (endIpv6) address.push(buffer.join(""));
    else address.push(stringArrayToHexStripped(buffer));
    output.address = address.join("");
    return output;
  }
  function normalizeIPv6(host) {
    if (findToken(host, ":") < 2) return {
      host,
      isIPV6: false
    };
    const ipv63 = getIPV6(host);
    if (!ipv63.error) {
      let newHost = ipv63.address;
      let escapedHost = ipv63.address;
      if (ipv63.zone) {
        newHost += "%" + ipv63.zone;
        escapedHost += "%25" + ipv63.zone;
      }
      return {
        host: newHost,
        isIPV6: true,
        escapedHost
      };
    } else return {
      host,
      isIPV6: false
    };
  }
  function findToken(str, token) {
    let ind = 0;
    for (let i = 0; i < str.length; i++) if (str[i] === token) ind++;
    return ind;
  }
  function removeDotSegments(path5) {
    let input = path5;
    const output = [];
    let nextSlash = -1;
    let len = 0;
    while (len = input.length) {
      if (len === 1) if (input === ".") break;
      else if (input === "/") {
        output.push("/");
        break;
      } else {
        output.push(input);
        break;
      }
      else if (len === 2) {
        if (input[0] === ".") {
          if (input[1] === ".") break;
          else if (input[1] === "/") {
            input = input.slice(2);
            continue;
          }
        } else if (input[0] === "/") {
          if (input[1] === "." || input[1] === "/") {
            output.push("/");
            break;
          }
        }
      } else if (len === 3) {
        if (input === "/..") {
          if (output.length !== 0) output.pop();
          output.push("/");
          break;
        }
      }
      if (input[0] === ".") {
        if (input[1] === ".") {
          if (input[2] === "/") {
            input = input.slice(3);
            continue;
          }
        } else if (input[1] === "/") {
          input = input.slice(2);
          continue;
        }
      } else if (input[0] === "/") {
        if (input[1] === ".") {
          if (input[2] === "/") {
            input = input.slice(2);
            continue;
          } else if (input[2] === ".") {
            if (input[3] === "/") {
              input = input.slice(3);
              if (output.length !== 0) output.pop();
              continue;
            }
          }
        }
      }
      if ((nextSlash = input.indexOf("/", 1)) === -1) {
        output.push(input);
        break;
      } else {
        output.push(input.slice(0, nextSlash));
        input = input.slice(nextSlash);
      }
    }
    return output.join("");
  }
  function normalizeComponentEncoding(component, esc2) {
    const func = esc2 !== true ? escape : unescape;
    if (component.scheme !== void 0) component.scheme = func(component.scheme);
    if (component.userinfo !== void 0) component.userinfo = func(component.userinfo);
    if (component.host !== void 0) component.host = func(component.host);
    if (component.path !== void 0) component.path = func(component.path);
    if (component.query !== void 0) component.query = func(component.query);
    if (component.fragment !== void 0) component.fragment = func(component.fragment);
    return component;
  }
  function recomposeAuthority(component) {
    const uriTokens = [];
    if (component.userinfo !== void 0) {
      uriTokens.push(component.userinfo);
      uriTokens.push("@");
    }
    if (component.host !== void 0) {
      let host = unescape(component.host);
      if (!isIPv4(host)) {
        const ipV6res = normalizeIPv6(host);
        if (ipV6res.isIPV6 === true) host = `[${ipV6res.escapedHost}]`;
        else host = component.host;
      }
      uriTokens.push(host);
    }
    if (typeof component.port === "number" || typeof component.port === "string") {
      uriTokens.push(":");
      uriTokens.push(String(component.port));
    }
    return uriTokens.length ? uriTokens.join("") : void 0;
  }
  module2.exports = {
    nonSimpleDomain,
    recomposeAuthority,
    normalizeComponentEncoding,
    removeDotSegments,
    isIPv4,
    isUUID,
    normalizeIPv6,
    stringArrayToHexStripped
  };
}));
var require_schemes = /* @__PURE__ */ __commonJSMin(((exports2, module2) => {
  const { isUUID } = require_utils();
  const URN_REG = /([\da-z][\d\-a-z]{0,31}):((?:[\w!$'()*+,\-.:;=@]|%[\da-f]{2})+)/iu;
  const supportedSchemeNames = [
    "http",
    "https",
    "ws",
    "wss",
    "urn",
    "urn:uuid"
  ];
  function isValidSchemeName(name) {
    return supportedSchemeNames.indexOf(name) !== -1;
  }
  function wsIsSecure(wsComponent) {
    if (wsComponent.secure === true) return true;
    else if (wsComponent.secure === false) return false;
    else if (wsComponent.scheme) return wsComponent.scheme.length === 3 && (wsComponent.scheme[0] === "w" || wsComponent.scheme[0] === "W") && (wsComponent.scheme[1] === "s" || wsComponent.scheme[1] === "S") && (wsComponent.scheme[2] === "s" || wsComponent.scheme[2] === "S");
    else return false;
  }
  function httpParse(component) {
    if (!component.host) component.error = component.error || "HTTP URIs must have a host.";
    return component;
  }
  function httpSerialize(component) {
    const secure = String(component.scheme).toLowerCase() === "https";
    if (component.port === (secure ? 443 : 80) || component.port === "") component.port = void 0;
    if (!component.path) component.path = "/";
    return component;
  }
  function wsParse(wsComponent) {
    wsComponent.secure = wsIsSecure(wsComponent);
    wsComponent.resourceName = (wsComponent.path || "/") + (wsComponent.query ? "?" + wsComponent.query : "");
    wsComponent.path = void 0;
    wsComponent.query = void 0;
    return wsComponent;
  }
  function wsSerialize(wsComponent) {
    if (wsComponent.port === (wsIsSecure(wsComponent) ? 443 : 80) || wsComponent.port === "") wsComponent.port = void 0;
    if (typeof wsComponent.secure === "boolean") {
      wsComponent.scheme = wsComponent.secure ? "wss" : "ws";
      wsComponent.secure = void 0;
    }
    if (wsComponent.resourceName) {
      const [path5, query] = wsComponent.resourceName.split("?");
      wsComponent.path = path5 && path5 !== "/" ? path5 : void 0;
      wsComponent.query = query;
      wsComponent.resourceName = void 0;
    }
    wsComponent.fragment = void 0;
    return wsComponent;
  }
  function urnParse(urnComponent, options) {
    if (!urnComponent.path) {
      urnComponent.error = "URN can not be parsed";
      return urnComponent;
    }
    const matches = urnComponent.path.match(URN_REG);
    if (matches) {
      const scheme = options.scheme || urnComponent.scheme || "urn";
      urnComponent.nid = matches[1].toLowerCase();
      urnComponent.nss = matches[2];
      const schemeHandler = getSchemeHandler(`${scheme}:${options.nid || urnComponent.nid}`);
      urnComponent.path = void 0;
      if (schemeHandler) urnComponent = schemeHandler.parse(urnComponent, options);
    } else urnComponent.error = urnComponent.error || "URN can not be parsed.";
    return urnComponent;
  }
  function urnSerialize(urnComponent, options) {
    if (urnComponent.nid === void 0) throw new Error("URN without nid cannot be serialized");
    const scheme = options.scheme || urnComponent.scheme || "urn";
    const nid = urnComponent.nid.toLowerCase();
    const schemeHandler = getSchemeHandler(`${scheme}:${options.nid || nid}`);
    if (schemeHandler) urnComponent = schemeHandler.serialize(urnComponent, options);
    const uriComponent = urnComponent;
    const nss = urnComponent.nss;
    uriComponent.path = `${nid || options.nid}:${nss}`;
    options.skipEscape = true;
    return uriComponent;
  }
  function urnuuidParse(urnComponent, options) {
    const uuidComponent = urnComponent;
    uuidComponent.uuid = uuidComponent.nss;
    uuidComponent.nss = void 0;
    if (!options.tolerant && (!uuidComponent.uuid || !isUUID(uuidComponent.uuid))) uuidComponent.error = uuidComponent.error || "UUID is not valid.";
    return uuidComponent;
  }
  function urnuuidSerialize(uuidComponent) {
    const urnComponent = uuidComponent;
    urnComponent.nss = (uuidComponent.uuid || "").toLowerCase();
    return urnComponent;
  }
  const http2 = {
    scheme: "http",
    domainHost: true,
    parse: httpParse,
    serialize: httpSerialize
  };
  const https = {
    scheme: "https",
    domainHost: http2.domainHost,
    parse: httpParse,
    serialize: httpSerialize
  };
  const ws = {
    scheme: "ws",
    domainHost: true,
    parse: wsParse,
    serialize: wsSerialize
  };
  const wss = {
    scheme: "wss",
    domainHost: ws.domainHost,
    parse: ws.parse,
    serialize: ws.serialize
  };
  const urn = {
    scheme: "urn",
    parse: urnParse,
    serialize: urnSerialize,
    skipNormalize: true
  };
  const urnuuid = {
    scheme: "urn:uuid",
    parse: urnuuidParse,
    serialize: urnuuidSerialize,
    skipNormalize: true
  };
  const SCHEMES = {
    http: http2,
    https,
    ws,
    wss,
    urn,
    "urn:uuid": urnuuid
  };
  Object.setPrototypeOf(SCHEMES, null);
  function getSchemeHandler(scheme) {
    return scheme && (SCHEMES[scheme] || SCHEMES[scheme.toLowerCase()]) || void 0;
  }
  module2.exports = {
    wsIsSecure,
    SCHEMES,
    isValidSchemeName,
    getSchemeHandler
  };
}));
var require_fast_uri = /* @__PURE__ */ __commonJSMin(((exports2, module2) => {
  const { normalizeIPv6, removeDotSegments, recomposeAuthority, normalizeComponentEncoding, isIPv4, nonSimpleDomain } = require_utils();
  const { SCHEMES, getSchemeHandler } = require_schemes();
  function normalize(uri2, options) {
    if (typeof uri2 === "string") uri2 = serialize(parse3(uri2, options), options);
    else if (typeof uri2 === "object") uri2 = parse3(serialize(uri2, options), options);
    return uri2;
  }
  function resolve(baseURI, relativeURI, options) {
    const schemelessOptions = options ? Object.assign({ scheme: "null" }, options) : { scheme: "null" };
    const resolved = resolveComponent(parse3(baseURI, schemelessOptions), parse3(relativeURI, schemelessOptions), schemelessOptions, true);
    schemelessOptions.skipEscape = true;
    return serialize(resolved, schemelessOptions);
  }
  function resolveComponent(base, relative, options, skipNormalization) {
    const target = {};
    if (!skipNormalization) {
      base = parse3(serialize(base, options), options);
      relative = parse3(serialize(relative, options), options);
    }
    options = options || {};
    if (!options.tolerant && relative.scheme) {
      target.scheme = relative.scheme;
      target.userinfo = relative.userinfo;
      target.host = relative.host;
      target.port = relative.port;
      target.path = removeDotSegments(relative.path || "");
      target.query = relative.query;
    } else {
      if (relative.userinfo !== void 0 || relative.host !== void 0 || relative.port !== void 0) {
        target.userinfo = relative.userinfo;
        target.host = relative.host;
        target.port = relative.port;
        target.path = removeDotSegments(relative.path || "");
        target.query = relative.query;
      } else {
        if (!relative.path) {
          target.path = base.path;
          if (relative.query !== void 0) target.query = relative.query;
          else target.query = base.query;
        } else {
          if (relative.path[0] === "/") target.path = removeDotSegments(relative.path);
          else {
            if ((base.userinfo !== void 0 || base.host !== void 0 || base.port !== void 0) && !base.path) target.path = "/" + relative.path;
            else if (!base.path) target.path = relative.path;
            else target.path = base.path.slice(0, base.path.lastIndexOf("/") + 1) + relative.path;
            target.path = removeDotSegments(target.path);
          }
          target.query = relative.query;
        }
        target.userinfo = base.userinfo;
        target.host = base.host;
        target.port = base.port;
      }
      target.scheme = base.scheme;
    }
    target.fragment = relative.fragment;
    return target;
  }
  function equal(uriA, uriB, options) {
    if (typeof uriA === "string") {
      uriA = unescape(uriA);
      uriA = serialize(normalizeComponentEncoding(parse3(uriA, options), true), {
        ...options,
        skipEscape: true
      });
    } else if (typeof uriA === "object") uriA = serialize(normalizeComponentEncoding(uriA, true), {
      ...options,
      skipEscape: true
    });
    if (typeof uriB === "string") {
      uriB = unescape(uriB);
      uriB = serialize(normalizeComponentEncoding(parse3(uriB, options), true), {
        ...options,
        skipEscape: true
      });
    } else if (typeof uriB === "object") uriB = serialize(normalizeComponentEncoding(uriB, true), {
      ...options,
      skipEscape: true
    });
    return uriA.toLowerCase() === uriB.toLowerCase();
  }
  function serialize(cmpts, opts) {
    const component = {
      host: cmpts.host,
      scheme: cmpts.scheme,
      userinfo: cmpts.userinfo,
      port: cmpts.port,
      path: cmpts.path,
      query: cmpts.query,
      nid: cmpts.nid,
      nss: cmpts.nss,
      uuid: cmpts.uuid,
      fragment: cmpts.fragment,
      reference: cmpts.reference,
      resourceName: cmpts.resourceName,
      secure: cmpts.secure,
      error: ""
    };
    const options = Object.assign({}, opts);
    const uriTokens = [];
    const schemeHandler = getSchemeHandler(options.scheme || component.scheme);
    if (schemeHandler && schemeHandler.serialize) schemeHandler.serialize(component, options);
    if (component.path !== void 0) if (!options.skipEscape) {
      component.path = escape(component.path);
      if (component.scheme !== void 0) component.path = component.path.split("%3A").join(":");
    } else component.path = unescape(component.path);
    if (options.reference !== "suffix" && component.scheme) uriTokens.push(component.scheme, ":");
    const authority = recomposeAuthority(component);
    if (authority !== void 0) {
      if (options.reference !== "suffix") uriTokens.push("//");
      uriTokens.push(authority);
      if (component.path && component.path[0] !== "/") uriTokens.push("/");
    }
    if (component.path !== void 0) {
      let s = component.path;
      if (!options.absolutePath && (!schemeHandler || !schemeHandler.absolutePath)) s = removeDotSegments(s);
      if (authority === void 0 && s[0] === "/" && s[1] === "/") s = "/%2F" + s.slice(2);
      uriTokens.push(s);
    }
    if (component.query !== void 0) uriTokens.push("?", component.query);
    if (component.fragment !== void 0) uriTokens.push("#", component.fragment);
    return uriTokens.join("");
  }
  const URI_PARSE = /^(?:([^#/:?]+):)?(?:\/\/((?:([^#/?@]*)@)?(\[[^#/?\]]+\]|[^#/:?]*)(?::(\d*))?))?([^#?]*)(?:\?([^#]*))?(?:#((?:.|[\n\r])*))?/u;
  function parse3(uri2, opts) {
    const options = Object.assign({}, opts);
    const parsed = {
      scheme: void 0,
      userinfo: void 0,
      host: "",
      port: void 0,
      path: "",
      query: void 0,
      fragment: void 0
    };
    let isIP = false;
    if (options.reference === "suffix") if (options.scheme) uri2 = options.scheme + ":" + uri2;
    else uri2 = "//" + uri2;
    const matches = uri2.match(URI_PARSE);
    if (matches) {
      parsed.scheme = matches[1];
      parsed.userinfo = matches[3];
      parsed.host = matches[4];
      parsed.port = parseInt(matches[5], 10);
      parsed.path = matches[6] || "";
      parsed.query = matches[7];
      parsed.fragment = matches[8];
      if (isNaN(parsed.port)) parsed.port = matches[5];
      if (parsed.host) if (isIPv4(parsed.host) === false) {
        const ipv6result = normalizeIPv6(parsed.host);
        parsed.host = ipv6result.host.toLowerCase();
        isIP = ipv6result.isIPV6;
      } else isIP = true;
      if (parsed.scheme === void 0 && parsed.userinfo === void 0 && parsed.host === void 0 && parsed.port === void 0 && parsed.query === void 0 && !parsed.path) parsed.reference = "same-document";
      else if (parsed.scheme === void 0) parsed.reference = "relative";
      else if (parsed.fragment === void 0) parsed.reference = "absolute";
      else parsed.reference = "uri";
      if (options.reference && options.reference !== "suffix" && options.reference !== parsed.reference) parsed.error = parsed.error || "URI is not a " + options.reference + " reference.";
      const schemeHandler = getSchemeHandler(options.scheme || parsed.scheme);
      if (!options.unicodeSupport && (!schemeHandler || !schemeHandler.unicodeSupport)) {
        if (parsed.host && (options.domainHost || schemeHandler && schemeHandler.domainHost) && isIP === false && nonSimpleDomain(parsed.host)) try {
          parsed.host = URL.domainToASCII(parsed.host.toLowerCase());
        } catch (e) {
          parsed.error = parsed.error || "Host's domain name can not be converted to ASCII: " + e;
        }
      }
      if (!schemeHandler || schemeHandler && !schemeHandler.skipNormalize) {
        if (uri2.indexOf("%") !== -1) {
          if (parsed.scheme !== void 0) parsed.scheme = unescape(parsed.scheme);
          if (parsed.host !== void 0) parsed.host = unescape(parsed.host);
        }
        if (parsed.path) parsed.path = escape(unescape(parsed.path));
        if (parsed.fragment) parsed.fragment = encodeURI(decodeURIComponent(parsed.fragment));
      }
      if (schemeHandler && schemeHandler.parse) schemeHandler.parse(parsed, options);
    } else parsed.error = parsed.error || "URI can not be parsed.";
    return parsed;
  }
  const fastUri = {
    SCHEMES,
    normalize,
    resolve,
    resolveComponent,
    equal,
    serialize,
    parse: parse3
  };
  module2.exports = fastUri;
  module2.exports.default = fastUri;
  module2.exports.fastUri = fastUri;
}));
var require_uri = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const uri2 = require_fast_uri();
  uri2.code = 'require("ajv/dist/runtime/uri").default';
  exports2.default = uri2;
}));
var require_core$1 = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.CodeGen = exports2.Name = exports2.nil = exports2.stringify = exports2.str = exports2._ = exports2.KeywordCxt = void 0;
  var validate_1 = require_validate();
  Object.defineProperty(exports2, "KeywordCxt", {
    enumerable: true,
    get: function() {
      return validate_1.KeywordCxt;
    }
  });
  var codegen_1 = require_codegen();
  Object.defineProperty(exports2, "_", {
    enumerable: true,
    get: function() {
      return codegen_1._;
    }
  });
  Object.defineProperty(exports2, "str", {
    enumerable: true,
    get: function() {
      return codegen_1.str;
    }
  });
  Object.defineProperty(exports2, "stringify", {
    enumerable: true,
    get: function() {
      return codegen_1.stringify;
    }
  });
  Object.defineProperty(exports2, "nil", {
    enumerable: true,
    get: function() {
      return codegen_1.nil;
    }
  });
  Object.defineProperty(exports2, "Name", {
    enumerable: true,
    get: function() {
      return codegen_1.Name;
    }
  });
  Object.defineProperty(exports2, "CodeGen", {
    enumerable: true,
    get: function() {
      return codegen_1.CodeGen;
    }
  });
  const validation_error_1 = require_validation_error();
  const ref_error_1 = require_ref_error();
  const rules_1 = require_rules();
  const compile_1 = require_compile();
  const codegen_2 = require_codegen();
  const resolve_1 = require_resolve();
  const dataType_1 = require_dataType();
  const util_1 = require_util();
  const $dataRefSchema = require_data();
  const uri_1 = require_uri();
  const defaultRegExp = (str, flags) => new RegExp(str, flags);
  defaultRegExp.code = "new RegExp";
  const META_IGNORE_OPTIONS = [
    "removeAdditional",
    "useDefaults",
    "coerceTypes"
  ];
  const EXT_SCOPE_NAMES = /* @__PURE__ */ new Set([
    "validate",
    "serialize",
    "parse",
    "wrapper",
    "root",
    "schema",
    "keyword",
    "pattern",
    "formats",
    "validate$data",
    "func",
    "obj",
    "Error"
  ]);
  const removedOptions = {
    errorDataPath: "",
    format: "`validateFormats: false` can be used instead.",
    nullable: '"nullable" keyword is supported by default.',
    jsonPointers: "Deprecated jsPropertySyntax can be used instead.",
    extendRefs: "Deprecated ignoreKeywordsWithRef can be used instead.",
    missingRefs: "Pass empty schema with $id that should be ignored to ajv.addSchema.",
    processCode: "Use option `code: {process: (code, schemaEnv: object) => string}`",
    sourceCode: "Use option `code: {source: true}`",
    strictDefaults: "It is default now, see option `strict`.",
    strictKeywords: "It is default now, see option `strict`.",
    uniqueItems: '"uniqueItems" keyword is always validated.',
    unknownFormats: "Disable strict mode or pass `true` to `ajv.addFormat` (or `formats` option).",
    cache: "Map is used as cache, schema object as key.",
    serialize: "Map is used as cache, schema object as key.",
    ajvErrors: "It is default now."
  };
  const deprecatedOptions = {
    ignoreKeywordsWithRef: "",
    jsPropertySyntax: "",
    unicode: '"minLength"/"maxLength" account for unicode characters by default.'
  };
  const MAX_EXPRESSION = 200;
  function requiredOptions(o) {
    var _a2, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
    const s = o.strict;
    const _optz = (_a2 = o.code) === null || _a2 === void 0 ? void 0 : _a2.optimize;
    const optimize = _optz === true || _optz === void 0 ? 1 : _optz || 0;
    const regExp = (_c = (_b = o.code) === null || _b === void 0 ? void 0 : _b.regExp) !== null && _c !== void 0 ? _c : defaultRegExp;
    const uriResolver = (_d = o.uriResolver) !== null && _d !== void 0 ? _d : uri_1.default;
    return {
      strictSchema: (_f = (_e = o.strictSchema) !== null && _e !== void 0 ? _e : s) !== null && _f !== void 0 ? _f : true,
      strictNumbers: (_h = (_g = o.strictNumbers) !== null && _g !== void 0 ? _g : s) !== null && _h !== void 0 ? _h : true,
      strictTypes: (_k = (_j = o.strictTypes) !== null && _j !== void 0 ? _j : s) !== null && _k !== void 0 ? _k : "log",
      strictTuples: (_m = (_l = o.strictTuples) !== null && _l !== void 0 ? _l : s) !== null && _m !== void 0 ? _m : "log",
      strictRequired: (_p = (_o = o.strictRequired) !== null && _o !== void 0 ? _o : s) !== null && _p !== void 0 ? _p : false,
      code: o.code ? {
        ...o.code,
        optimize,
        regExp
      } : {
        optimize,
        regExp
      },
      loopRequired: (_q = o.loopRequired) !== null && _q !== void 0 ? _q : MAX_EXPRESSION,
      loopEnum: (_r = o.loopEnum) !== null && _r !== void 0 ? _r : MAX_EXPRESSION,
      meta: (_s = o.meta) !== null && _s !== void 0 ? _s : true,
      messages: (_t = o.messages) !== null && _t !== void 0 ? _t : true,
      inlineRefs: (_u = o.inlineRefs) !== null && _u !== void 0 ? _u : true,
      schemaId: (_v = o.schemaId) !== null && _v !== void 0 ? _v : "$id",
      addUsedSchema: (_w = o.addUsedSchema) !== null && _w !== void 0 ? _w : true,
      validateSchema: (_x = o.validateSchema) !== null && _x !== void 0 ? _x : true,
      validateFormats: (_y = o.validateFormats) !== null && _y !== void 0 ? _y : true,
      unicodeRegExp: (_z = o.unicodeRegExp) !== null && _z !== void 0 ? _z : true,
      int32range: (_0 = o.int32range) !== null && _0 !== void 0 ? _0 : true,
      uriResolver
    };
  }
  var Ajv = class {
    constructor(opts = {}) {
      this.schemas = {};
      this.refs = {};
      this.formats = {};
      this._compilations = /* @__PURE__ */ new Set();
      this._loading = {};
      this._cache = /* @__PURE__ */ new Map();
      opts = this.opts = {
        ...opts,
        ...requiredOptions(opts)
      };
      const { es5, lines } = this.opts.code;
      this.scope = new codegen_2.ValueScope({
        scope: {},
        prefixes: EXT_SCOPE_NAMES,
        es5,
        lines
      });
      this.logger = getLogger(opts.logger);
      const formatOpt = opts.validateFormats;
      opts.validateFormats = false;
      this.RULES = (0, rules_1.getRules)();
      checkOptions.call(this, removedOptions, opts, "NOT SUPPORTED");
      checkOptions.call(this, deprecatedOptions, opts, "DEPRECATED", "warn");
      this._metaOpts = getMetaSchemaOptions.call(this);
      if (opts.formats) addInitialFormats.call(this);
      this._addVocabularies();
      this._addDefaultMetaSchema();
      if (opts.keywords) addInitialKeywords.call(this, opts.keywords);
      if (typeof opts.meta == "object") this.addMetaSchema(opts.meta);
      addInitialSchemas.call(this);
      opts.validateFormats = formatOpt;
    }
    _addVocabularies() {
      this.addKeyword("$async");
    }
    _addDefaultMetaSchema() {
      const { $data, meta: meta3, schemaId } = this.opts;
      let _dataRefSchema = $dataRefSchema;
      if (schemaId === "id") {
        _dataRefSchema = { ...$dataRefSchema };
        _dataRefSchema.id = _dataRefSchema.$id;
        delete _dataRefSchema.$id;
      }
      if (meta3 && $data) this.addMetaSchema(_dataRefSchema, _dataRefSchema[schemaId], false);
    }
    defaultMeta() {
      const { meta: meta3, schemaId } = this.opts;
      return this.opts.defaultMeta = typeof meta3 == "object" ? meta3[schemaId] || meta3 : void 0;
    }
    validate(schemaKeyRef, data) {
      let v;
      if (typeof schemaKeyRef == "string") {
        v = this.getSchema(schemaKeyRef);
        if (!v) throw new Error(`no schema with key or ref "${schemaKeyRef}"`);
      } else v = this.compile(schemaKeyRef);
      const valid = v(data);
      if (!("$async" in v)) this.errors = v.errors;
      return valid;
    }
    compile(schema, _meta) {
      const sch = this._addSchema(schema, _meta);
      return sch.validate || this._compileSchemaEnv(sch);
    }
    compileAsync(schema, meta3) {
      if (typeof this.opts.loadSchema != "function") throw new Error("options.loadSchema should be a function");
      const { loadSchema } = this.opts;
      return runCompileAsync.call(this, schema, meta3);
      async function runCompileAsync(_schema, _meta) {
        await loadMetaSchema.call(this, _schema.$schema);
        const sch = this._addSchema(_schema, _meta);
        return sch.validate || _compileAsync.call(this, sch);
      }
      async function loadMetaSchema($ref) {
        if ($ref && !this.getSchema($ref)) await runCompileAsync.call(this, { $ref }, true);
      }
      async function _compileAsync(sch) {
        try {
          return this._compileSchemaEnv(sch);
        } catch (e) {
          if (!(e instanceof ref_error_1.default)) throw e;
          checkLoaded.call(this, e);
          await loadMissingSchema.call(this, e.missingSchema);
          return _compileAsync.call(this, sch);
        }
      }
      function checkLoaded({ missingSchema: ref, missingRef }) {
        if (this.refs[ref]) throw new Error(`AnySchema ${ref} is loaded but ${missingRef} cannot be resolved`);
      }
      async function loadMissingSchema(ref) {
        const _schema = await _loadSchema.call(this, ref);
        if (!this.refs[ref]) await loadMetaSchema.call(this, _schema.$schema);
        if (!this.refs[ref]) this.addSchema(_schema, ref, meta3);
      }
      async function _loadSchema(ref) {
        const p = this._loading[ref];
        if (p) return p;
        try {
          return await (this._loading[ref] = loadSchema(ref));
        } finally {
          delete this._loading[ref];
        }
      }
    }
    addSchema(schema, key, _meta, _validateSchema = this.opts.validateSchema) {
      if (Array.isArray(schema)) {
        for (const sch of schema) this.addSchema(sch, void 0, _meta, _validateSchema);
        return this;
      }
      let id;
      if (typeof schema === "object") {
        const { schemaId } = this.opts;
        id = schema[schemaId];
        if (id !== void 0 && typeof id != "string") throw new Error(`schema ${schemaId} must be string`);
      }
      key = (0, resolve_1.normalizeId)(key || id);
      this._checkUnique(key);
      this.schemas[key] = this._addSchema(schema, _meta, key, _validateSchema, true);
      return this;
    }
    addMetaSchema(schema, key, _validateSchema = this.opts.validateSchema) {
      this.addSchema(schema, key, true, _validateSchema);
      return this;
    }
    validateSchema(schema, throwOrLogError) {
      if (typeof schema == "boolean") return true;
      let $schema;
      $schema = schema.$schema;
      if ($schema !== void 0 && typeof $schema != "string") throw new Error("$schema must be a string");
      $schema = $schema || this.opts.defaultMeta || this.defaultMeta();
      if (!$schema) {
        this.logger.warn("meta-schema not available");
        this.errors = null;
        return true;
      }
      const valid = this.validate($schema, schema);
      if (!valid && throwOrLogError) {
        const message = "schema is invalid: " + this.errorsText();
        if (this.opts.validateSchema === "log") this.logger.error(message);
        else throw new Error(message);
      }
      return valid;
    }
    getSchema(keyRef) {
      let sch;
      while (typeof (sch = getSchEnv.call(this, keyRef)) == "string") keyRef = sch;
      if (sch === void 0) {
        const { schemaId } = this.opts;
        const root = new compile_1.SchemaEnv({
          schema: {},
          schemaId
        });
        sch = compile_1.resolveSchema.call(this, root, keyRef);
        if (!sch) return;
        this.refs[keyRef] = sch;
      }
      return sch.validate || this._compileSchemaEnv(sch);
    }
    removeSchema(schemaKeyRef) {
      if (schemaKeyRef instanceof RegExp) {
        this._removeAllSchemas(this.schemas, schemaKeyRef);
        this._removeAllSchemas(this.refs, schemaKeyRef);
        return this;
      }
      switch (typeof schemaKeyRef) {
        case "undefined":
          this._removeAllSchemas(this.schemas);
          this._removeAllSchemas(this.refs);
          this._cache.clear();
          return this;
        case "string": {
          const sch = getSchEnv.call(this, schemaKeyRef);
          if (typeof sch == "object") this._cache.delete(sch.schema);
          delete this.schemas[schemaKeyRef];
          delete this.refs[schemaKeyRef];
          return this;
        }
        case "object": {
          const cacheKey = schemaKeyRef;
          this._cache.delete(cacheKey);
          let id = schemaKeyRef[this.opts.schemaId];
          if (id) {
            id = (0, resolve_1.normalizeId)(id);
            delete this.schemas[id];
            delete this.refs[id];
          }
          return this;
        }
        default:
          throw new Error("ajv.removeSchema: invalid parameter");
      }
    }
    addVocabulary(definitions) {
      for (const def of definitions) this.addKeyword(def);
      return this;
    }
    addKeyword(kwdOrDef, def) {
      let keyword;
      if (typeof kwdOrDef == "string") {
        keyword = kwdOrDef;
        if (typeof def == "object") {
          this.logger.warn("these parameters are deprecated, see docs for addKeyword");
          def.keyword = keyword;
        }
      } else if (typeof kwdOrDef == "object" && def === void 0) {
        def = kwdOrDef;
        keyword = def.keyword;
        if (Array.isArray(keyword) && !keyword.length) throw new Error("addKeywords: keyword must be string or non-empty array");
      } else throw new Error("invalid addKeywords parameters");
      checkKeyword.call(this, keyword, def);
      if (!def) {
        (0, util_1.eachItem)(keyword, (kwd) => addRule.call(this, kwd));
        return this;
      }
      keywordMetaschema.call(this, def);
      const definition = {
        ...def,
        type: (0, dataType_1.getJSONTypes)(def.type),
        schemaType: (0, dataType_1.getJSONTypes)(def.schemaType)
      };
      (0, util_1.eachItem)(keyword, definition.type.length === 0 ? (k) => addRule.call(this, k, definition) : (k) => definition.type.forEach((t) => addRule.call(this, k, definition, t)));
      return this;
    }
    getKeyword(keyword) {
      const rule = this.RULES.all[keyword];
      return typeof rule == "object" ? rule.definition : !!rule;
    }
    removeKeyword(keyword) {
      const { RULES } = this;
      delete RULES.keywords[keyword];
      delete RULES.all[keyword];
      for (const group of RULES.rules) {
        const i = group.rules.findIndex((rule) => rule.keyword === keyword);
        if (i >= 0) group.rules.splice(i, 1);
      }
      return this;
    }
    addFormat(name, format2) {
      if (typeof format2 == "string") format2 = new RegExp(format2);
      this.formats[name] = format2;
      return this;
    }
    errorsText(errors = this.errors, { separator = ", ", dataVar = "data" } = {}) {
      if (!errors || errors.length === 0) return "No errors";
      return errors.map((e) => `${dataVar}${e.instancePath} ${e.message}`).reduce((text, msg) => text + separator + msg);
    }
    $dataMetaSchema(metaSchema, keywordsJsonPointers) {
      const rules = this.RULES.all;
      metaSchema = JSON.parse(JSON.stringify(metaSchema));
      for (const jsonPointer of keywordsJsonPointers) {
        const segments = jsonPointer.split("/").slice(1);
        let keywords = metaSchema;
        for (const seg of segments) keywords = keywords[seg];
        for (const key in rules) {
          const rule = rules[key];
          if (typeof rule != "object") continue;
          const { $data } = rule.definition;
          const schema = keywords[key];
          if ($data && schema) keywords[key] = schemaOrData(schema);
        }
      }
      return metaSchema;
    }
    _removeAllSchemas(schemas, regex2) {
      for (const keyRef in schemas) {
        const sch = schemas[keyRef];
        if (!regex2 || regex2.test(keyRef)) {
          if (typeof sch == "string") delete schemas[keyRef];
          else if (sch && !sch.meta) {
            this._cache.delete(sch.schema);
            delete schemas[keyRef];
          }
        }
      }
    }
    _addSchema(schema, meta3, baseId, validateSchema = this.opts.validateSchema, addSchema = this.opts.addUsedSchema) {
      let id;
      const { schemaId } = this.opts;
      if (typeof schema == "object") id = schema[schemaId];
      else if (this.opts.jtd) throw new Error("schema must be object");
      else if (typeof schema != "boolean") throw new Error("schema must be object or boolean");
      let sch = this._cache.get(schema);
      if (sch !== void 0) return sch;
      baseId = (0, resolve_1.normalizeId)(id || baseId);
      const localRefs = resolve_1.getSchemaRefs.call(this, schema, baseId);
      sch = new compile_1.SchemaEnv({
        schema,
        schemaId,
        meta: meta3,
        baseId,
        localRefs
      });
      this._cache.set(sch.schema, sch);
      if (addSchema && !baseId.startsWith("#")) {
        if (baseId) this._checkUnique(baseId);
        this.refs[baseId] = sch;
      }
      if (validateSchema) this.validateSchema(schema, true);
      return sch;
    }
    _checkUnique(id) {
      if (this.schemas[id] || this.refs[id]) throw new Error(`schema with key or id "${id}" already exists`);
    }
    _compileSchemaEnv(sch) {
      if (sch.meta) this._compileMetaSchema(sch);
      else compile_1.compileSchema.call(this, sch);
      if (!sch.validate) throw new Error("ajv implementation error");
      return sch.validate;
    }
    _compileMetaSchema(sch) {
      const currentOpts = this.opts;
      this.opts = this._metaOpts;
      try {
        compile_1.compileSchema.call(this, sch);
      } finally {
        this.opts = currentOpts;
      }
    }
  };
  Ajv.ValidationError = validation_error_1.default;
  Ajv.MissingRefError = ref_error_1.default;
  exports2.default = Ajv;
  function checkOptions(checkOpts, options, msg, log = "error") {
    for (const key in checkOpts) {
      const opt = key;
      if (opt in options) this.logger[log](`${msg}: option ${key}. ${checkOpts[opt]}`);
    }
  }
  function getSchEnv(keyRef) {
    keyRef = (0, resolve_1.normalizeId)(keyRef);
    return this.schemas[keyRef] || this.refs[keyRef];
  }
  function addInitialSchemas() {
    const optsSchemas = this.opts.schemas;
    if (!optsSchemas) return;
    if (Array.isArray(optsSchemas)) this.addSchema(optsSchemas);
    else for (const key in optsSchemas) this.addSchema(optsSchemas[key], key);
  }
  function addInitialFormats() {
    for (const name in this.opts.formats) {
      const format2 = this.opts.formats[name];
      if (format2) this.addFormat(name, format2);
    }
  }
  function addInitialKeywords(defs) {
    if (Array.isArray(defs)) {
      this.addVocabulary(defs);
      return;
    }
    this.logger.warn("keywords option as map is deprecated, pass array");
    for (const keyword in defs) {
      const def = defs[keyword];
      if (!def.keyword) def.keyword = keyword;
      this.addKeyword(def);
    }
  }
  function getMetaSchemaOptions() {
    const metaOpts = { ...this.opts };
    for (const opt of META_IGNORE_OPTIONS) delete metaOpts[opt];
    return metaOpts;
  }
  const noLogs = {
    log() {
    },
    warn() {
    },
    error() {
    }
  };
  function getLogger(logger) {
    if (logger === false) return noLogs;
    if (logger === void 0) return console;
    if (logger.log && logger.warn && logger.error) return logger;
    throw new Error("logger must implement log, warn and error methods");
  }
  const KEYWORD_NAME = /^[a-z_$][a-z0-9_$:-]*$/i;
  function checkKeyword(keyword, def) {
    const { RULES } = this;
    (0, util_1.eachItem)(keyword, (kwd) => {
      if (RULES.keywords[kwd]) throw new Error(`Keyword ${kwd} is already defined`);
      if (!KEYWORD_NAME.test(kwd)) throw new Error(`Keyword ${kwd} has invalid name`);
    });
    if (!def) return;
    if (def.$data && !("code" in def || "validate" in def)) throw new Error('$data keyword must have "code" or "validate" function');
  }
  function addRule(keyword, definition, dataType) {
    var _a2;
    const post = definition === null || definition === void 0 ? void 0 : definition.post;
    if (dataType && post) throw new Error('keyword with "post" flag cannot have "type"');
    const { RULES } = this;
    let ruleGroup = post ? RULES.post : RULES.rules.find(({ type: t }) => t === dataType);
    if (!ruleGroup) {
      ruleGroup = {
        type: dataType,
        rules: []
      };
      RULES.rules.push(ruleGroup);
    }
    RULES.keywords[keyword] = true;
    if (!definition) return;
    const rule = {
      keyword,
      definition: {
        ...definition,
        type: (0, dataType_1.getJSONTypes)(definition.type),
        schemaType: (0, dataType_1.getJSONTypes)(definition.schemaType)
      }
    };
    if (definition.before) addBeforeRule.call(this, ruleGroup, rule, definition.before);
    else ruleGroup.rules.push(rule);
    RULES.all[keyword] = rule;
    (_a2 = definition.implements) === null || _a2 === void 0 || _a2.forEach((kwd) => this.addKeyword(kwd));
  }
  function addBeforeRule(ruleGroup, rule, before) {
    const i = ruleGroup.rules.findIndex((_rule) => _rule.keyword === before);
    if (i >= 0) ruleGroup.rules.splice(i, 0, rule);
    else {
      ruleGroup.rules.push(rule);
      this.logger.warn(`rule ${before} is not defined`);
    }
  }
  function keywordMetaschema(def) {
    let { metaSchema } = def;
    if (metaSchema === void 0) return;
    if (def.$data && this.opts.$data) metaSchema = schemaOrData(metaSchema);
    def.validateSchema = this.compile(metaSchema, true);
  }
  const $dataRef = { $ref: "https://raw.githubusercontent.com/ajv-validator/ajv/master/lib/refs/data.json#" };
  function schemaOrData(schema) {
    return { anyOf: [schema, $dataRef] };
  }
}));
var require_id = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const def = {
    keyword: "id",
    code() {
      throw new Error('NOT SUPPORTED: keyword "id", use "$id" for schema ID');
    }
  };
  exports2.default = def;
}));
var require_ref = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.callRef = exports2.getValidate = void 0;
  const ref_error_1 = require_ref_error();
  const code_1 = require_code();
  const codegen_1 = require_codegen();
  const names_1 = require_names();
  const compile_1 = require_compile();
  const util_1 = require_util();
  const def = {
    keyword: "$ref",
    schemaType: "string",
    code(cxt) {
      const { gen, schema: $ref, it } = cxt;
      const { baseId, schemaEnv: env, validateName, opts, self: self2 } = it;
      const { root } = env;
      if (($ref === "#" || $ref === "#/") && baseId === root.baseId) return callRootRef();
      const schOrEnv = compile_1.resolveRef.call(self2, root, baseId, $ref);
      if (schOrEnv === void 0) throw new ref_error_1.default(it.opts.uriResolver, baseId, $ref);
      if (schOrEnv instanceof compile_1.SchemaEnv) return callValidate(schOrEnv);
      return inlineRefSchema(schOrEnv);
      function callRootRef() {
        if (env === root) return callRef(cxt, validateName, env, env.$async);
        const rootName = gen.scopeValue("root", { ref: root });
        return callRef(cxt, (0, codegen_1._)`${rootName}.validate`, root, root.$async);
      }
      function callValidate(sch) {
        callRef(cxt, getValidate(cxt, sch), sch, sch.$async);
      }
      function inlineRefSchema(sch) {
        const schName = gen.scopeValue("schema", opts.code.source === true ? {
          ref: sch,
          code: (0, codegen_1.stringify)(sch)
        } : { ref: sch });
        const valid = gen.name("valid");
        const schCxt = cxt.subschema({
          schema: sch,
          dataTypes: [],
          schemaPath: codegen_1.nil,
          topSchemaRef: schName,
          errSchemaPath: $ref
        }, valid);
        cxt.mergeEvaluated(schCxt);
        cxt.ok(valid);
      }
    }
  };
  function getValidate(cxt, sch) {
    const { gen } = cxt;
    return sch.validate ? gen.scopeValue("validate", { ref: sch.validate }) : (0, codegen_1._)`${gen.scopeValue("wrapper", { ref: sch })}.validate`;
  }
  exports2.getValidate = getValidate;
  function callRef(cxt, v, sch, $async) {
    const { gen, it } = cxt;
    const { allErrors, schemaEnv: env, opts } = it;
    const passCxt = opts.passContext ? names_1.default.this : codegen_1.nil;
    if ($async) callAsyncRef();
    else callSyncRef();
    function callAsyncRef() {
      if (!env.$async) throw new Error("async schema referenced by sync schema");
      const valid = gen.let("valid");
      gen.try(() => {
        gen.code((0, codegen_1._)`await ${(0, code_1.callValidateCode)(cxt, v, passCxt)}`);
        addEvaluatedFrom(v);
        if (!allErrors) gen.assign(valid, true);
      }, (e) => {
        gen.if((0, codegen_1._)`!(${e} instanceof ${it.ValidationError})`, () => gen.throw(e));
        addErrorsFrom(e);
        if (!allErrors) gen.assign(valid, false);
      });
      cxt.ok(valid);
    }
    function callSyncRef() {
      cxt.result((0, code_1.callValidateCode)(cxt, v, passCxt), () => addEvaluatedFrom(v), () => addErrorsFrom(v));
    }
    function addErrorsFrom(source) {
      const errs = (0, codegen_1._)`${source}.errors`;
      gen.assign(names_1.default.vErrors, (0, codegen_1._)`${names_1.default.vErrors} === null ? ${errs} : ${names_1.default.vErrors}.concat(${errs})`);
      gen.assign(names_1.default.errors, (0, codegen_1._)`${names_1.default.vErrors}.length`);
    }
    function addEvaluatedFrom(source) {
      var _a2;
      if (!it.opts.unevaluated) return;
      const schEvaluated = (_a2 = sch === null || sch === void 0 ? void 0 : sch.validate) === null || _a2 === void 0 ? void 0 : _a2.evaluated;
      if (it.props !== true) if (schEvaluated && !schEvaluated.dynamicProps) {
        if (schEvaluated.props !== void 0) it.props = util_1.mergeEvaluated.props(gen, schEvaluated.props, it.props);
      } else {
        const props = gen.var("props", (0, codegen_1._)`${source}.evaluated.props`);
        it.props = util_1.mergeEvaluated.props(gen, props, it.props, codegen_1.Name);
      }
      if (it.items !== true) if (schEvaluated && !schEvaluated.dynamicItems) {
        if (schEvaluated.items !== void 0) it.items = util_1.mergeEvaluated.items(gen, schEvaluated.items, it.items);
      } else {
        const items = gen.var("items", (0, codegen_1._)`${source}.evaluated.items`);
        it.items = util_1.mergeEvaluated.items(gen, items, it.items, codegen_1.Name);
      }
    }
  }
  exports2.callRef = callRef;
  exports2.default = def;
}));
var require_core = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const id_1 = require_id();
  const ref_1 = require_ref();
  const core = [
    "$schema",
    "$id",
    "$defs",
    "$vocabulary",
    { keyword: "$comment" },
    "definitions",
    id_1.default,
    ref_1.default
  ];
  exports2.default = core;
}));
var require_limitNumber = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const ops = codegen_1.operators;
  const KWDs = {
    maximum: {
      okStr: "<=",
      ok: ops.LTE,
      fail: ops.GT
    },
    minimum: {
      okStr: ">=",
      ok: ops.GTE,
      fail: ops.LT
    },
    exclusiveMaximum: {
      okStr: "<",
      ok: ops.LT,
      fail: ops.GTE
    },
    exclusiveMinimum: {
      okStr: ">",
      ok: ops.GT,
      fail: ops.LTE
    }
  };
  const def = {
    keyword: Object.keys(KWDs),
    type: "number",
    schemaType: "number",
    $data: true,
    error: {
      message: ({ keyword, schemaCode }) => (0, codegen_1.str)`must be ${KWDs[keyword].okStr} ${schemaCode}`,
      params: ({ keyword, schemaCode }) => (0, codegen_1._)`{comparison: ${KWDs[keyword].okStr}, limit: ${schemaCode}}`
    },
    code(cxt) {
      const { keyword, data, schemaCode } = cxt;
      cxt.fail$data((0, codegen_1._)`${data} ${KWDs[keyword].fail} ${schemaCode} || isNaN(${data})`);
    }
  };
  exports2.default = def;
}));
var require_multipleOf = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const def = {
    keyword: "multipleOf",
    type: "number",
    schemaType: "number",
    $data: true,
    error: {
      message: ({ schemaCode }) => (0, codegen_1.str)`must be multiple of ${schemaCode}`,
      params: ({ schemaCode }) => (0, codegen_1._)`{multipleOf: ${schemaCode}}`
    },
    code(cxt) {
      const { gen, data, schemaCode, it } = cxt;
      const prec = it.opts.multipleOfPrecision;
      const res = gen.let("res");
      const invalid = prec ? (0, codegen_1._)`Math.abs(Math.round(${res}) - ${res}) > 1e-${prec}` : (0, codegen_1._)`${res} !== parseInt(${res})`;
      cxt.fail$data((0, codegen_1._)`(${schemaCode} === 0 || (${res} = ${data}/${schemaCode}, ${invalid}))`);
    }
  };
  exports2.default = def;
}));
var require_ucs2length = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  function ucs2length2(str) {
    const len = str.length;
    let length = 0;
    let pos = 0;
    let value;
    while (pos < len) {
      length++;
      value = str.charCodeAt(pos++);
      if (value >= 55296 && value <= 56319 && pos < len) {
        value = str.charCodeAt(pos);
        if ((value & 64512) === 56320) pos++;
      }
    }
    return length;
  }
  exports2.default = ucs2length2;
  ucs2length2.code = 'require("ajv/dist/runtime/ucs2length").default';
}));
var require_limitLength = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const ucs2length_1 = require_ucs2length();
  const def = {
    keyword: ["maxLength", "minLength"],
    type: "string",
    schemaType: "number",
    $data: true,
    error: {
      message({ keyword, schemaCode }) {
        const comp = keyword === "maxLength" ? "more" : "fewer";
        return (0, codegen_1.str)`must NOT have ${comp} than ${schemaCode} characters`;
      },
      params: ({ schemaCode }) => (0, codegen_1._)`{limit: ${schemaCode}}`
    },
    code(cxt) {
      const { keyword, data, schemaCode, it } = cxt;
      const op = keyword === "maxLength" ? codegen_1.operators.GT : codegen_1.operators.LT;
      const len = it.opts.unicode === false ? (0, codegen_1._)`${data}.length` : (0, codegen_1._)`${(0, util_1.useFunc)(cxt.gen, ucs2length_1.default)}(${data})`;
      cxt.fail$data((0, codegen_1._)`${len} ${op} ${schemaCode}`);
    }
  };
  exports2.default = def;
}));
var require_pattern = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const code_1 = require_code();
  const util_1 = require_util();
  const codegen_1 = require_codegen();
  const def = {
    keyword: "pattern",
    type: "string",
    schemaType: "string",
    $data: true,
    error: {
      message: ({ schemaCode }) => (0, codegen_1.str)`must match pattern "${schemaCode}"`,
      params: ({ schemaCode }) => (0, codegen_1._)`{pattern: ${schemaCode}}`
    },
    code(cxt) {
      const { gen, data, $data, schema, schemaCode, it } = cxt;
      const u = it.opts.unicodeRegExp ? "u" : "";
      if ($data) {
        const { regExp } = it.opts.code;
        const regExpCode = regExp.code === "new RegExp" ? (0, codegen_1._)`new RegExp` : (0, util_1.useFunc)(gen, regExp);
        const valid = gen.let("valid");
        gen.try(() => gen.assign(valid, (0, codegen_1._)`${regExpCode}(${schemaCode}, ${u}).test(${data})`), () => gen.assign(valid, false));
        cxt.fail$data((0, codegen_1._)`!${valid}`);
      } else {
        const regExp = (0, code_1.usePattern)(cxt, schema);
        cxt.fail$data((0, codegen_1._)`!${regExp}.test(${data})`);
      }
    }
  };
  exports2.default = def;
}));
var require_limitProperties = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const def = {
    keyword: ["maxProperties", "minProperties"],
    type: "object",
    schemaType: "number",
    $data: true,
    error: {
      message({ keyword, schemaCode }) {
        const comp = keyword === "maxProperties" ? "more" : "fewer";
        return (0, codegen_1.str)`must NOT have ${comp} than ${schemaCode} properties`;
      },
      params: ({ schemaCode }) => (0, codegen_1._)`{limit: ${schemaCode}}`
    },
    code(cxt) {
      const { keyword, data, schemaCode } = cxt;
      const op = keyword === "maxProperties" ? codegen_1.operators.GT : codegen_1.operators.LT;
      cxt.fail$data((0, codegen_1._)`Object.keys(${data}).length ${op} ${schemaCode}`);
    }
  };
  exports2.default = def;
}));
var require_required = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const code_1 = require_code();
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const def = {
    keyword: "required",
    type: "object",
    schemaType: "array",
    $data: true,
    error: {
      message: ({ params: { missingProperty } }) => (0, codegen_1.str)`must have required property '${missingProperty}'`,
      params: ({ params: { missingProperty } }) => (0, codegen_1._)`{missingProperty: ${missingProperty}}`
    },
    code(cxt) {
      const { gen, schema, schemaCode, data, $data, it } = cxt;
      const { opts } = it;
      if (!$data && schema.length === 0) return;
      const useLoop = schema.length >= opts.loopRequired;
      if (it.allErrors) allErrorsMode();
      else exitOnErrorMode();
      if (opts.strictRequired) {
        const props = cxt.parentSchema.properties;
        const { definedProperties } = cxt.it;
        for (const requiredKey of schema) if ((props === null || props === void 0 ? void 0 : props[requiredKey]) === void 0 && !definedProperties.has(requiredKey)) {
          const msg = `required property "${requiredKey}" is not defined at "${it.schemaEnv.baseId + it.errSchemaPath}" (strictRequired)`;
          (0, util_1.checkStrictMode)(it, msg, it.opts.strictRequired);
        }
      }
      function allErrorsMode() {
        if (useLoop || $data) cxt.block$data(codegen_1.nil, loopAllRequired);
        else for (const prop of schema) (0, code_1.checkReportMissingProp)(cxt, prop);
      }
      function exitOnErrorMode() {
        const missing = gen.let("missing");
        if (useLoop || $data) {
          const valid = gen.let("valid", true);
          cxt.block$data(valid, () => loopUntilMissing(missing, valid));
          cxt.ok(valid);
        } else {
          gen.if((0, code_1.checkMissingProp)(cxt, schema, missing));
          (0, code_1.reportMissingProp)(cxt, missing);
          gen.else();
        }
      }
      function loopAllRequired() {
        gen.forOf("prop", schemaCode, (prop) => {
          cxt.setParams({ missingProperty: prop });
          gen.if((0, code_1.noPropertyInData)(gen, data, prop, opts.ownProperties), () => cxt.error());
        });
      }
      function loopUntilMissing(missing, valid) {
        cxt.setParams({ missingProperty: missing });
        gen.forOf(missing, schemaCode, () => {
          gen.assign(valid, (0, code_1.propertyInData)(gen, data, missing, opts.ownProperties));
          gen.if((0, codegen_1.not)(valid), () => {
            cxt.error();
            gen.break();
          });
        }, codegen_1.nil);
      }
    }
  };
  exports2.default = def;
}));
var require_limitItems = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const def = {
    keyword: ["maxItems", "minItems"],
    type: "array",
    schemaType: "number",
    $data: true,
    error: {
      message({ keyword, schemaCode }) {
        const comp = keyword === "maxItems" ? "more" : "fewer";
        return (0, codegen_1.str)`must NOT have ${comp} than ${schemaCode} items`;
      },
      params: ({ schemaCode }) => (0, codegen_1._)`{limit: ${schemaCode}}`
    },
    code(cxt) {
      const { keyword, data, schemaCode } = cxt;
      const op = keyword === "maxItems" ? codegen_1.operators.GT : codegen_1.operators.LT;
      cxt.fail$data((0, codegen_1._)`${data}.length ${op} ${schemaCode}`);
    }
  };
  exports2.default = def;
}));
var require_equal = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const equal = require_fast_deep_equal();
  equal.code = 'require("ajv/dist/runtime/equal").default';
  exports2.default = equal;
}));
var require_uniqueItems = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const dataType_1 = require_dataType();
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const equal_1 = require_equal();
  const def = {
    keyword: "uniqueItems",
    type: "array",
    schemaType: "boolean",
    $data: true,
    error: {
      message: ({ params: { i, j } }) => (0, codegen_1.str)`must NOT have duplicate items (items ## ${j} and ${i} are identical)`,
      params: ({ params: { i, j } }) => (0, codegen_1._)`{i: ${i}, j: ${j}}`
    },
    code(cxt) {
      const { gen, data, $data, schema, parentSchema, schemaCode, it } = cxt;
      if (!$data && !schema) return;
      const valid = gen.let("valid");
      const itemTypes = parentSchema.items ? (0, dataType_1.getSchemaTypes)(parentSchema.items) : [];
      cxt.block$data(valid, validateUniqueItems, (0, codegen_1._)`${schemaCode} === false`);
      cxt.ok(valid);
      function validateUniqueItems() {
        const i = gen.let("i", (0, codegen_1._)`${data}.length`);
        const j = gen.let("j");
        cxt.setParams({
          i,
          j
        });
        gen.assign(valid, true);
        gen.if((0, codegen_1._)`${i} > 1`, () => (canOptimize() ? loopN : loopN2)(i, j));
      }
      function canOptimize() {
        return itemTypes.length > 0 && !itemTypes.some((t) => t === "object" || t === "array");
      }
      function loopN(i, j) {
        const item = gen.name("item");
        const wrongType = (0, dataType_1.checkDataTypes)(itemTypes, item, it.opts.strictNumbers, dataType_1.DataType.Wrong);
        const indices = gen.const("indices", (0, codegen_1._)`{}`);
        gen.for((0, codegen_1._)`;${i}--;`, () => {
          gen.let(item, (0, codegen_1._)`${data}[${i}]`);
          gen.if(wrongType, (0, codegen_1._)`continue`);
          if (itemTypes.length > 1) gen.if((0, codegen_1._)`typeof ${item} == "string"`, (0, codegen_1._)`${item} += "_"`);
          gen.if((0, codegen_1._)`typeof ${indices}[${item}] == "number"`, () => {
            gen.assign(j, (0, codegen_1._)`${indices}[${item}]`);
            cxt.error();
            gen.assign(valid, false).break();
          }).code((0, codegen_1._)`${indices}[${item}] = ${i}`);
        });
      }
      function loopN2(i, j) {
        const eql = (0, util_1.useFunc)(gen, equal_1.default);
        const outer = gen.name("outer");
        gen.label(outer).for((0, codegen_1._)`;${i}--;`, () => gen.for((0, codegen_1._)`${j} = ${i}; ${j}--;`, () => gen.if((0, codegen_1._)`${eql}(${data}[${i}], ${data}[${j}])`, () => {
          cxt.error();
          gen.assign(valid, false).break(outer);
        })));
      }
    }
  };
  exports2.default = def;
}));
var require_const = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const equal_1 = require_equal();
  const def = {
    keyword: "const",
    $data: true,
    error: {
      message: "must be equal to constant",
      params: ({ schemaCode }) => (0, codegen_1._)`{allowedValue: ${schemaCode}}`
    },
    code(cxt) {
      const { gen, data, $data, schemaCode, schema } = cxt;
      if ($data || schema && typeof schema == "object") cxt.fail$data((0, codegen_1._)`!${(0, util_1.useFunc)(gen, equal_1.default)}(${data}, ${schemaCode})`);
      else cxt.fail((0, codegen_1._)`${schema} !== ${data}`);
    }
  };
  exports2.default = def;
}));
var require_enum = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const equal_1 = require_equal();
  const def = {
    keyword: "enum",
    schemaType: "array",
    $data: true,
    error: {
      message: "must be equal to one of the allowed values",
      params: ({ schemaCode }) => (0, codegen_1._)`{allowedValues: ${schemaCode}}`
    },
    code(cxt) {
      const { gen, data, $data, schema, schemaCode, it } = cxt;
      if (!$data && schema.length === 0) throw new Error("enum must have non-empty array");
      const useLoop = schema.length >= it.opts.loopEnum;
      let eql;
      const getEql = () => eql !== null && eql !== void 0 ? eql : eql = (0, util_1.useFunc)(gen, equal_1.default);
      let valid;
      if (useLoop || $data) {
        valid = gen.let("valid");
        cxt.block$data(valid, loopEnum);
      } else {
        if (!Array.isArray(schema)) throw new Error("ajv implementation error");
        const vSchema = gen.const("vSchema", schemaCode);
        valid = (0, codegen_1.or)(...schema.map((_x, i) => equalCode(vSchema, i)));
      }
      cxt.pass(valid);
      function loopEnum() {
        gen.assign(valid, false);
        gen.forOf("v", schemaCode, (v) => gen.if((0, codegen_1._)`${getEql()}(${data}, ${v})`, () => gen.assign(valid, true).break()));
      }
      function equalCode(vSchema, i) {
        const sch = schema[i];
        return typeof sch === "object" && sch !== null ? (0, codegen_1._)`${getEql()}(${data}, ${vSchema}[${i}])` : (0, codegen_1._)`${data} === ${sch}`;
      }
    }
  };
  exports2.default = def;
}));
var require_validation = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const limitNumber_1 = require_limitNumber();
  const multipleOf_1 = require_multipleOf();
  const limitLength_1 = require_limitLength();
  const pattern_1 = require_pattern();
  const limitProperties_1 = require_limitProperties();
  const required_1 = require_required();
  const limitItems_1 = require_limitItems();
  const uniqueItems_1 = require_uniqueItems();
  const const_1 = require_const();
  const enum_1 = require_enum();
  const validation = [
    limitNumber_1.default,
    multipleOf_1.default,
    limitLength_1.default,
    pattern_1.default,
    limitProperties_1.default,
    required_1.default,
    limitItems_1.default,
    uniqueItems_1.default,
    {
      keyword: "type",
      schemaType: ["string", "array"]
    },
    {
      keyword: "nullable",
      schemaType: "boolean"
    },
    const_1.default,
    enum_1.default
  ];
  exports2.default = validation;
}));
var require_additionalItems = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.validateAdditionalItems = void 0;
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const def = {
    keyword: "additionalItems",
    type: "array",
    schemaType: ["boolean", "object"],
    before: "uniqueItems",
    error: {
      message: ({ params: { len } }) => (0, codegen_1.str)`must NOT have more than ${len} items`,
      params: ({ params: { len } }) => (0, codegen_1._)`{limit: ${len}}`
    },
    code(cxt) {
      const { parentSchema, it } = cxt;
      const { items } = parentSchema;
      if (!Array.isArray(items)) {
        (0, util_1.checkStrictMode)(it, '"additionalItems" is ignored when "items" is not an array of schemas');
        return;
      }
      validateAdditionalItems(cxt, items);
    }
  };
  function validateAdditionalItems(cxt, items) {
    const { gen, schema, data, keyword, it } = cxt;
    it.items = true;
    const len = gen.const("len", (0, codegen_1._)`${data}.length`);
    if (schema === false) {
      cxt.setParams({ len: items.length });
      cxt.pass((0, codegen_1._)`${len} <= ${items.length}`);
    } else if (typeof schema == "object" && !(0, util_1.alwaysValidSchema)(it, schema)) {
      const valid = gen.var("valid", (0, codegen_1._)`${len} <= ${items.length}`);
      gen.if((0, codegen_1.not)(valid), () => validateItems(valid));
      cxt.ok(valid);
    }
    function validateItems(valid) {
      gen.forRange("i", items.length, len, (i) => {
        cxt.subschema({
          keyword,
          dataProp: i,
          dataPropType: util_1.Type.Num
        }, valid);
        if (!it.allErrors) gen.if((0, codegen_1.not)(valid), () => gen.break());
      });
    }
  }
  exports2.validateAdditionalItems = validateAdditionalItems;
  exports2.default = def;
}));
var require_items = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.validateTuple = void 0;
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const code_1 = require_code();
  const def = {
    keyword: "items",
    type: "array",
    schemaType: [
      "object",
      "array",
      "boolean"
    ],
    before: "uniqueItems",
    code(cxt) {
      const { schema, it } = cxt;
      if (Array.isArray(schema)) return validateTuple(cxt, "additionalItems", schema);
      it.items = true;
      if ((0, util_1.alwaysValidSchema)(it, schema)) return;
      cxt.ok((0, code_1.validateArray)(cxt));
    }
  };
  function validateTuple(cxt, extraItems, schArr = cxt.schema) {
    const { gen, parentSchema, data, keyword, it } = cxt;
    checkStrictTuple(parentSchema);
    if (it.opts.unevaluated && schArr.length && it.items !== true) it.items = util_1.mergeEvaluated.items(gen, schArr.length, it.items);
    const valid = gen.name("valid");
    const len = gen.const("len", (0, codegen_1._)`${data}.length`);
    schArr.forEach((sch, i) => {
      if ((0, util_1.alwaysValidSchema)(it, sch)) return;
      gen.if((0, codegen_1._)`${len} > ${i}`, () => cxt.subschema({
        keyword,
        schemaProp: i,
        dataProp: i
      }, valid));
      cxt.ok(valid);
    });
    function checkStrictTuple(sch) {
      const { opts, errSchemaPath } = it;
      const l = schArr.length;
      const fullTuple = l === sch.minItems && (l === sch.maxItems || sch[extraItems] === false);
      if (opts.strictTuples && !fullTuple) {
        const msg = `"${keyword}" is ${l}-tuple, but minItems or maxItems/${extraItems} are not specified or different at path "${errSchemaPath}"`;
        (0, util_1.checkStrictMode)(it, msg, opts.strictTuples);
      }
    }
  }
  exports2.validateTuple = validateTuple;
  exports2.default = def;
}));
var require_prefixItems = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const items_1 = require_items();
  const def = {
    keyword: "prefixItems",
    type: "array",
    schemaType: ["array"],
    before: "uniqueItems",
    code: (cxt) => (0, items_1.validateTuple)(cxt, "items")
  };
  exports2.default = def;
}));
var require_items2020 = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const code_1 = require_code();
  const additionalItems_1 = require_additionalItems();
  const def = {
    keyword: "items",
    type: "array",
    schemaType: ["object", "boolean"],
    before: "uniqueItems",
    error: {
      message: ({ params: { len } }) => (0, codegen_1.str)`must NOT have more than ${len} items`,
      params: ({ params: { len } }) => (0, codegen_1._)`{limit: ${len}}`
    },
    code(cxt) {
      const { schema, parentSchema, it } = cxt;
      const { prefixItems } = parentSchema;
      it.items = true;
      if ((0, util_1.alwaysValidSchema)(it, schema)) return;
      if (prefixItems) (0, additionalItems_1.validateAdditionalItems)(cxt, prefixItems);
      else cxt.ok((0, code_1.validateArray)(cxt));
    }
  };
  exports2.default = def;
}));
var require_contains = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const def = {
    keyword: "contains",
    type: "array",
    schemaType: ["object", "boolean"],
    before: "uniqueItems",
    trackErrors: true,
    error: {
      message: ({ params: { min, max } }) => max === void 0 ? (0, codegen_1.str)`must contain at least ${min} valid item(s)` : (0, codegen_1.str)`must contain at least ${min} and no more than ${max} valid item(s)`,
      params: ({ params: { min, max } }) => max === void 0 ? (0, codegen_1._)`{minContains: ${min}}` : (0, codegen_1._)`{minContains: ${min}, maxContains: ${max}}`
    },
    code(cxt) {
      const { gen, schema, parentSchema, data, it } = cxt;
      let min;
      let max;
      const { minContains, maxContains } = parentSchema;
      if (it.opts.next) {
        min = minContains === void 0 ? 1 : minContains;
        max = maxContains;
      } else min = 1;
      const len = gen.const("len", (0, codegen_1._)`${data}.length`);
      cxt.setParams({
        min,
        max
      });
      if (max === void 0 && min === 0) {
        (0, util_1.checkStrictMode)(it, `"minContains" == 0 without "maxContains": "contains" keyword ignored`);
        return;
      }
      if (max !== void 0 && min > max) {
        (0, util_1.checkStrictMode)(it, `"minContains" > "maxContains" is always invalid`);
        cxt.fail();
        return;
      }
      if ((0, util_1.alwaysValidSchema)(it, schema)) {
        let cond = (0, codegen_1._)`${len} >= ${min}`;
        if (max !== void 0) cond = (0, codegen_1._)`${cond} && ${len} <= ${max}`;
        cxt.pass(cond);
        return;
      }
      it.items = true;
      const valid = gen.name("valid");
      if (max === void 0 && min === 1) validateItems(valid, () => gen.if(valid, () => gen.break()));
      else if (min === 0) {
        gen.let(valid, true);
        if (max !== void 0) gen.if((0, codegen_1._)`${data}.length > 0`, validateItemsWithCount);
      } else {
        gen.let(valid, false);
        validateItemsWithCount();
      }
      cxt.result(valid, () => cxt.reset());
      function validateItemsWithCount() {
        const schValid = gen.name("_valid");
        const count = gen.let("count", 0);
        validateItems(schValid, () => gen.if(schValid, () => checkLimits(count)));
      }
      function validateItems(_valid, block) {
        gen.forRange("i", 0, len, (i) => {
          cxt.subschema({
            keyword: "contains",
            dataProp: i,
            dataPropType: util_1.Type.Num,
            compositeRule: true
          }, _valid);
          block();
        });
      }
      function checkLimits(count) {
        gen.code((0, codegen_1._)`${count}++`);
        if (max === void 0) gen.if((0, codegen_1._)`${count} >= ${min}`, () => gen.assign(valid, true).break());
        else {
          gen.if((0, codegen_1._)`${count} > ${max}`, () => gen.assign(valid, false).break());
          if (min === 1) gen.assign(valid, true);
          else gen.if((0, codegen_1._)`${count} >= ${min}`, () => gen.assign(valid, true));
        }
      }
    }
  };
  exports2.default = def;
}));
var require_dependencies = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.validateSchemaDeps = exports2.validatePropertyDeps = exports2.error = void 0;
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const code_1 = require_code();
  exports2.error = {
    message: ({ params: { property, depsCount, deps } }) => {
      const property_ies = depsCount === 1 ? "property" : "properties";
      return (0, codegen_1.str)`must have ${property_ies} ${deps} when property ${property} is present`;
    },
    params: ({ params: { property, depsCount, deps, missingProperty } }) => (0, codegen_1._)`{property: ${property},
    missingProperty: ${missingProperty},
    depsCount: ${depsCount},
    deps: ${deps}}`
  };
  const def = {
    keyword: "dependencies",
    type: "object",
    schemaType: "object",
    error: exports2.error,
    code(cxt) {
      const [propDeps, schDeps] = splitDependencies(cxt);
      validatePropertyDeps(cxt, propDeps);
      validateSchemaDeps(cxt, schDeps);
    }
  };
  function splitDependencies({ schema }) {
    const propertyDeps = {};
    const schemaDeps = {};
    for (const key in schema) {
      if (key === "__proto__") continue;
      const deps = Array.isArray(schema[key]) ? propertyDeps : schemaDeps;
      deps[key] = schema[key];
    }
    return [propertyDeps, schemaDeps];
  }
  function validatePropertyDeps(cxt, propertyDeps = cxt.schema) {
    const { gen, data, it } = cxt;
    if (Object.keys(propertyDeps).length === 0) return;
    const missing = gen.let("missing");
    for (const prop in propertyDeps) {
      const deps = propertyDeps[prop];
      if (deps.length === 0) continue;
      const hasProperty = (0, code_1.propertyInData)(gen, data, prop, it.opts.ownProperties);
      cxt.setParams({
        property: prop,
        depsCount: deps.length,
        deps: deps.join(", ")
      });
      if (it.allErrors) gen.if(hasProperty, () => {
        for (const depProp of deps) (0, code_1.checkReportMissingProp)(cxt, depProp);
      });
      else {
        gen.if((0, codegen_1._)`${hasProperty} && (${(0, code_1.checkMissingProp)(cxt, deps, missing)})`);
        (0, code_1.reportMissingProp)(cxt, missing);
        gen.else();
      }
    }
  }
  exports2.validatePropertyDeps = validatePropertyDeps;
  function validateSchemaDeps(cxt, schemaDeps = cxt.schema) {
    const { gen, data, keyword, it } = cxt;
    const valid = gen.name("valid");
    for (const prop in schemaDeps) {
      if ((0, util_1.alwaysValidSchema)(it, schemaDeps[prop])) continue;
      gen.if((0, code_1.propertyInData)(gen, data, prop, it.opts.ownProperties), () => {
        const schCxt = cxt.subschema({
          keyword,
          schemaProp: prop
        }, valid);
        cxt.mergeValidEvaluated(schCxt, valid);
      }, () => gen.var(valid, true));
      cxt.ok(valid);
    }
  }
  exports2.validateSchemaDeps = validateSchemaDeps;
  exports2.default = def;
}));
var require_propertyNames = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const def = {
    keyword: "propertyNames",
    type: "object",
    schemaType: ["object", "boolean"],
    error: {
      message: "property name must be valid",
      params: ({ params }) => (0, codegen_1._)`{propertyName: ${params.propertyName}}`
    },
    code(cxt) {
      const { gen, schema, data, it } = cxt;
      if ((0, util_1.alwaysValidSchema)(it, schema)) return;
      const valid = gen.name("valid");
      gen.forIn("key", data, (key) => {
        cxt.setParams({ propertyName: key });
        cxt.subschema({
          keyword: "propertyNames",
          data: key,
          dataTypes: ["string"],
          propertyName: key,
          compositeRule: true
        }, valid);
        gen.if((0, codegen_1.not)(valid), () => {
          cxt.error(true);
          if (!it.allErrors) gen.break();
        });
      });
      cxt.ok(valid);
    }
  };
  exports2.default = def;
}));
var require_additionalProperties = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const code_1 = require_code();
  const codegen_1 = require_codegen();
  const names_1 = require_names();
  const util_1 = require_util();
  const def = {
    keyword: "additionalProperties",
    type: ["object"],
    schemaType: ["boolean", "object"],
    allowUndefined: true,
    trackErrors: true,
    error: {
      message: "must NOT have additional properties",
      params: ({ params }) => (0, codegen_1._)`{additionalProperty: ${params.additionalProperty}}`
    },
    code(cxt) {
      const { gen, schema, parentSchema, data, errsCount, it } = cxt;
      if (!errsCount) throw new Error("ajv implementation error");
      const { allErrors, opts } = it;
      it.props = true;
      if (opts.removeAdditional !== "all" && (0, util_1.alwaysValidSchema)(it, schema)) return;
      const props = (0, code_1.allSchemaProperties)(parentSchema.properties);
      const patProps = (0, code_1.allSchemaProperties)(parentSchema.patternProperties);
      checkAdditionalProperties();
      cxt.ok((0, codegen_1._)`${errsCount} === ${names_1.default.errors}`);
      function checkAdditionalProperties() {
        gen.forIn("key", data, (key) => {
          if (!props.length && !patProps.length) additionalPropertyCode(key);
          else gen.if(isAdditional(key), () => additionalPropertyCode(key));
        });
      }
      function isAdditional(key) {
        let definedProp;
        if (props.length > 8) {
          const propsSchema = (0, util_1.schemaRefOrVal)(it, parentSchema.properties, "properties");
          definedProp = (0, code_1.isOwnProperty)(gen, propsSchema, key);
        } else if (props.length) definedProp = (0, codegen_1.or)(...props.map((p) => (0, codegen_1._)`${key} === ${p}`));
        else definedProp = codegen_1.nil;
        if (patProps.length) definedProp = (0, codegen_1.or)(definedProp, ...patProps.map((p) => (0, codegen_1._)`${(0, code_1.usePattern)(cxt, p)}.test(${key})`));
        return (0, codegen_1.not)(definedProp);
      }
      function deleteAdditional(key) {
        gen.code((0, codegen_1._)`delete ${data}[${key}]`);
      }
      function additionalPropertyCode(key) {
        if (opts.removeAdditional === "all" || opts.removeAdditional && schema === false) {
          deleteAdditional(key);
          return;
        }
        if (schema === false) {
          cxt.setParams({ additionalProperty: key });
          cxt.error();
          if (!allErrors) gen.break();
          return;
        }
        if (typeof schema == "object" && !(0, util_1.alwaysValidSchema)(it, schema)) {
          const valid = gen.name("valid");
          if (opts.removeAdditional === "failing") {
            applyAdditionalSchema(key, valid, false);
            gen.if((0, codegen_1.not)(valid), () => {
              cxt.reset();
              deleteAdditional(key);
            });
          } else {
            applyAdditionalSchema(key, valid);
            if (!allErrors) gen.if((0, codegen_1.not)(valid), () => gen.break());
          }
        }
      }
      function applyAdditionalSchema(key, valid, errors) {
        const subschema = {
          keyword: "additionalProperties",
          dataProp: key,
          dataPropType: util_1.Type.Str
        };
        if (errors === false) Object.assign(subschema, {
          compositeRule: true,
          createErrors: false,
          allErrors: false
        });
        cxt.subschema(subschema, valid);
      }
    }
  };
  exports2.default = def;
}));
var require_properties = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const validate_1 = require_validate();
  const code_1 = require_code();
  const util_1 = require_util();
  const additionalProperties_1 = require_additionalProperties();
  const def = {
    keyword: "properties",
    type: "object",
    schemaType: "object",
    code(cxt) {
      const { gen, schema, parentSchema, data, it } = cxt;
      if (it.opts.removeAdditional === "all" && parentSchema.additionalProperties === void 0) additionalProperties_1.default.code(new validate_1.KeywordCxt(it, additionalProperties_1.default, "additionalProperties"));
      const allProps = (0, code_1.allSchemaProperties)(schema);
      for (const prop of allProps) it.definedProperties.add(prop);
      if (it.opts.unevaluated && allProps.length && it.props !== true) it.props = util_1.mergeEvaluated.props(gen, (0, util_1.toHash)(allProps), it.props);
      const properties = allProps.filter((p) => !(0, util_1.alwaysValidSchema)(it, schema[p]));
      if (properties.length === 0) return;
      const valid = gen.name("valid");
      for (const prop of properties) {
        if (hasDefault(prop)) applyPropertySchema(prop);
        else {
          gen.if((0, code_1.propertyInData)(gen, data, prop, it.opts.ownProperties));
          applyPropertySchema(prop);
          if (!it.allErrors) gen.else().var(valid, true);
          gen.endIf();
        }
        cxt.it.definedProperties.add(prop);
        cxt.ok(valid);
      }
      function hasDefault(prop) {
        return it.opts.useDefaults && !it.compositeRule && schema[prop].default !== void 0;
      }
      function applyPropertySchema(prop) {
        cxt.subschema({
          keyword: "properties",
          schemaProp: prop,
          dataProp: prop
        }, valid);
      }
    }
  };
  exports2.default = def;
}));
var require_patternProperties = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const code_1 = require_code();
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const util_2 = require_util();
  const def = {
    keyword: "patternProperties",
    type: "object",
    schemaType: "object",
    code(cxt) {
      const { gen, schema, data, parentSchema, it } = cxt;
      const { opts } = it;
      const patterns = (0, code_1.allSchemaProperties)(schema);
      const alwaysValidPatterns = patterns.filter((p) => (0, util_1.alwaysValidSchema)(it, schema[p]));
      if (patterns.length === 0 || alwaysValidPatterns.length === patterns.length && (!it.opts.unevaluated || it.props === true)) return;
      const checkProperties = opts.strictSchema && !opts.allowMatchingProperties && parentSchema.properties;
      const valid = gen.name("valid");
      if (it.props !== true && !(it.props instanceof codegen_1.Name)) it.props = (0, util_2.evaluatedPropsToName)(gen, it.props);
      const { props } = it;
      validatePatternProperties();
      function validatePatternProperties() {
        for (const pat of patterns) {
          if (checkProperties) checkMatchingProperties(pat);
          if (it.allErrors) validateProperties(pat);
          else {
            gen.var(valid, true);
            validateProperties(pat);
            gen.if(valid);
          }
        }
      }
      function checkMatchingProperties(pat) {
        for (const prop in checkProperties) if (new RegExp(pat).test(prop)) (0, util_1.checkStrictMode)(it, `property ${prop} matches pattern ${pat} (use allowMatchingProperties)`);
      }
      function validateProperties(pat) {
        gen.forIn("key", data, (key) => {
          gen.if((0, codegen_1._)`${(0, code_1.usePattern)(cxt, pat)}.test(${key})`, () => {
            const alwaysValid = alwaysValidPatterns.includes(pat);
            if (!alwaysValid) cxt.subschema({
              keyword: "patternProperties",
              schemaProp: pat,
              dataProp: key,
              dataPropType: util_2.Type.Str
            }, valid);
            if (it.opts.unevaluated && props !== true) gen.assign((0, codegen_1._)`${props}[${key}]`, true);
            else if (!alwaysValid && !it.allErrors) gen.if((0, codegen_1.not)(valid), () => gen.break());
          });
        });
      }
    }
  };
  exports2.default = def;
}));
var require_not = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const util_1 = require_util();
  const def = {
    keyword: "not",
    schemaType: ["object", "boolean"],
    trackErrors: true,
    code(cxt) {
      const { gen, schema, it } = cxt;
      if ((0, util_1.alwaysValidSchema)(it, schema)) {
        cxt.fail();
        return;
      }
      const valid = gen.name("valid");
      cxt.subschema({
        keyword: "not",
        compositeRule: true,
        createErrors: false,
        allErrors: false
      }, valid);
      cxt.failResult(valid, () => cxt.reset(), () => cxt.error());
    },
    error: { message: "must NOT be valid" }
  };
  exports2.default = def;
}));
var require_anyOf = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const def = {
    keyword: "anyOf",
    schemaType: "array",
    trackErrors: true,
    code: require_code().validateUnion,
    error: { message: "must match a schema in anyOf" }
  };
  exports2.default = def;
}));
var require_oneOf = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const def = {
    keyword: "oneOf",
    schemaType: "array",
    trackErrors: true,
    error: {
      message: "must match exactly one schema in oneOf",
      params: ({ params }) => (0, codegen_1._)`{passingSchemas: ${params.passing}}`
    },
    code(cxt) {
      const { gen, schema, parentSchema, it } = cxt;
      if (!Array.isArray(schema)) throw new Error("ajv implementation error");
      if (it.opts.discriminator && parentSchema.discriminator) return;
      const schArr = schema;
      const valid = gen.let("valid", false);
      const passing = gen.let("passing", null);
      const schValid = gen.name("_valid");
      cxt.setParams({ passing });
      gen.block(validateOneOf);
      cxt.result(valid, () => cxt.reset(), () => cxt.error(true));
      function validateOneOf() {
        schArr.forEach((sch, i) => {
          let schCxt;
          if ((0, util_1.alwaysValidSchema)(it, sch)) gen.var(schValid, true);
          else schCxt = cxt.subschema({
            keyword: "oneOf",
            schemaProp: i,
            compositeRule: true
          }, schValid);
          if (i > 0) gen.if((0, codegen_1._)`${schValid} && ${valid}`).assign(valid, false).assign(passing, (0, codegen_1._)`[${passing}, ${i}]`).else();
          gen.if(schValid, () => {
            gen.assign(valid, true);
            gen.assign(passing, i);
            if (schCxt) cxt.mergeEvaluated(schCxt, codegen_1.Name);
          });
        });
      }
    }
  };
  exports2.default = def;
}));
var require_allOf = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const util_1 = require_util();
  const def = {
    keyword: "allOf",
    schemaType: "array",
    code(cxt) {
      const { gen, schema, it } = cxt;
      if (!Array.isArray(schema)) throw new Error("ajv implementation error");
      const valid = gen.name("valid");
      schema.forEach((sch, i) => {
        if ((0, util_1.alwaysValidSchema)(it, sch)) return;
        const schCxt = cxt.subschema({
          keyword: "allOf",
          schemaProp: i
        }, valid);
        cxt.ok(valid);
        cxt.mergeEvaluated(schCxt);
      });
    }
  };
  exports2.default = def;
}));
var require_if = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const util_1 = require_util();
  const def = {
    keyword: "if",
    schemaType: ["object", "boolean"],
    trackErrors: true,
    error: {
      message: ({ params }) => (0, codegen_1.str)`must match "${params.ifClause}" schema`,
      params: ({ params }) => (0, codegen_1._)`{failingKeyword: ${params.ifClause}}`
    },
    code(cxt) {
      const { gen, parentSchema, it } = cxt;
      if (parentSchema.then === void 0 && parentSchema.else === void 0) (0, util_1.checkStrictMode)(it, '"if" without "then" and "else" is ignored');
      const hasThen = hasSchema(it, "then");
      const hasElse = hasSchema(it, "else");
      if (!hasThen && !hasElse) return;
      const valid = gen.let("valid", true);
      const schValid = gen.name("_valid");
      validateIf();
      cxt.reset();
      if (hasThen && hasElse) {
        const ifClause = gen.let("ifClause");
        cxt.setParams({ ifClause });
        gen.if(schValid, validateClause("then", ifClause), validateClause("else", ifClause));
      } else if (hasThen) gen.if(schValid, validateClause("then"));
      else gen.if((0, codegen_1.not)(schValid), validateClause("else"));
      cxt.pass(valid, () => cxt.error(true));
      function validateIf() {
        const schCxt = cxt.subschema({
          keyword: "if",
          compositeRule: true,
          createErrors: false,
          allErrors: false
        }, schValid);
        cxt.mergeEvaluated(schCxt);
      }
      function validateClause(keyword, ifClause) {
        return () => {
          const schCxt = cxt.subschema({ keyword }, schValid);
          gen.assign(valid, schValid);
          cxt.mergeValidEvaluated(schCxt, valid);
          if (ifClause) gen.assign(ifClause, (0, codegen_1._)`${keyword}`);
          else cxt.setParams({ ifClause: keyword });
        };
      }
    }
  };
  function hasSchema(it, keyword) {
    const schema = it.schema[keyword];
    return schema !== void 0 && !(0, util_1.alwaysValidSchema)(it, schema);
  }
  exports2.default = def;
}));
var require_thenElse = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const util_1 = require_util();
  const def = {
    keyword: ["then", "else"],
    schemaType: ["object", "boolean"],
    code({ keyword, parentSchema, it }) {
      if (parentSchema.if === void 0) (0, util_1.checkStrictMode)(it, `"${keyword}" without "if" is ignored`);
    }
  };
  exports2.default = def;
}));
var require_applicator = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const additionalItems_1 = require_additionalItems();
  const prefixItems_1 = require_prefixItems();
  const items_1 = require_items();
  const items2020_1 = require_items2020();
  const contains_1 = require_contains();
  const dependencies_1 = require_dependencies();
  const propertyNames_1 = require_propertyNames();
  const additionalProperties_1 = require_additionalProperties();
  const properties_1 = require_properties();
  const patternProperties_1 = require_patternProperties();
  const not_1 = require_not();
  const anyOf_1 = require_anyOf();
  const oneOf_1 = require_oneOf();
  const allOf_1 = require_allOf();
  const if_1 = require_if();
  const thenElse_1 = require_thenElse();
  function getApplicator(draft2020 = false) {
    const applicator = [
      not_1.default,
      anyOf_1.default,
      oneOf_1.default,
      allOf_1.default,
      if_1.default,
      thenElse_1.default,
      propertyNames_1.default,
      additionalProperties_1.default,
      dependencies_1.default,
      properties_1.default,
      patternProperties_1.default
    ];
    if (draft2020) applicator.push(prefixItems_1.default, items2020_1.default);
    else applicator.push(additionalItems_1.default, items_1.default);
    applicator.push(contains_1.default);
    return applicator;
  }
  exports2.default = getApplicator;
}));
var require_format$1 = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const def = {
    keyword: "format",
    type: ["number", "string"],
    schemaType: "string",
    $data: true,
    error: {
      message: ({ schemaCode }) => (0, codegen_1.str)`must match format "${schemaCode}"`,
      params: ({ schemaCode }) => (0, codegen_1._)`{format: ${schemaCode}}`
    },
    code(cxt, ruleType) {
      const { gen, data, $data, schema, schemaCode, it } = cxt;
      const { opts, errSchemaPath, schemaEnv, self: self2 } = it;
      if (!opts.validateFormats) return;
      if ($data) validate$DataFormat();
      else validateFormat();
      function validate$DataFormat() {
        const fmts = gen.scopeValue("formats", {
          ref: self2.formats,
          code: opts.code.formats
        });
        const fDef = gen.const("fDef", (0, codegen_1._)`${fmts}[${schemaCode}]`);
        const fType = gen.let("fType");
        const format2 = gen.let("format");
        gen.if((0, codegen_1._)`typeof ${fDef} == "object" && !(${fDef} instanceof RegExp)`, () => gen.assign(fType, (0, codegen_1._)`${fDef}.type || "string"`).assign(format2, (0, codegen_1._)`${fDef}.validate`), () => gen.assign(fType, (0, codegen_1._)`"string"`).assign(format2, fDef));
        cxt.fail$data((0, codegen_1.or)(unknownFmt(), invalidFmt()));
        function unknownFmt() {
          if (opts.strictSchema === false) return codegen_1.nil;
          return (0, codegen_1._)`${schemaCode} && !${format2}`;
        }
        function invalidFmt() {
          const callFormat = schemaEnv.$async ? (0, codegen_1._)`(${fDef}.async ? await ${format2}(${data}) : ${format2}(${data}))` : (0, codegen_1._)`${format2}(${data})`;
          const validData = (0, codegen_1._)`(typeof ${format2} == "function" ? ${callFormat} : ${format2}.test(${data}))`;
          return (0, codegen_1._)`${format2} && ${format2} !== true && ${fType} === ${ruleType} && !${validData}`;
        }
      }
      function validateFormat() {
        const formatDef = self2.formats[schema];
        if (!formatDef) {
          unknownFormat();
          return;
        }
        if (formatDef === true) return;
        const [fmtType, format2, fmtRef] = getFormat(formatDef);
        if (fmtType === ruleType) cxt.pass(validCondition());
        function unknownFormat() {
          if (opts.strictSchema === false) {
            self2.logger.warn(unknownMsg());
            return;
          }
          throw new Error(unknownMsg());
          function unknownMsg() {
            return `unknown format "${schema}" ignored in schema at path "${errSchemaPath}"`;
          }
        }
        function getFormat(fmtDef) {
          const code = fmtDef instanceof RegExp ? (0, codegen_1.regexpCode)(fmtDef) : opts.code.formats ? (0, codegen_1._)`${opts.code.formats}${(0, codegen_1.getProperty)(schema)}` : void 0;
          const fmt = gen.scopeValue("formats", {
            key: schema,
            ref: fmtDef,
            code
          });
          if (typeof fmtDef == "object" && !(fmtDef instanceof RegExp)) return [
            fmtDef.type || "string",
            fmtDef.validate,
            (0, codegen_1._)`${fmt}.validate`
          ];
          return [
            "string",
            fmtDef,
            fmt
          ];
        }
        function validCondition() {
          if (typeof formatDef == "object" && !(formatDef instanceof RegExp) && formatDef.async) {
            if (!schemaEnv.$async) throw new Error("async format in sync schema");
            return (0, codegen_1._)`await ${fmtRef}(${data})`;
          }
          return typeof format2 == "function" ? (0, codegen_1._)`${fmtRef}(${data})` : (0, codegen_1._)`${fmtRef}.test(${data})`;
        }
      }
    }
  };
  exports2.default = def;
}));
var require_format = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const format2 = [require_format$1().default];
  exports2.default = format2;
}));
var require_metadata = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.contentVocabulary = exports2.metadataVocabulary = void 0;
  exports2.metadataVocabulary = [
    "title",
    "description",
    "default",
    "deprecated",
    "readOnly",
    "writeOnly",
    "examples"
  ];
  exports2.contentVocabulary = [
    "contentMediaType",
    "contentEncoding",
    "contentSchema"
  ];
}));
var require_draft7 = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const core_1 = require_core();
  const validation_1 = require_validation();
  const applicator_1 = require_applicator();
  const format_1 = require_format();
  const metadata_1 = require_metadata();
  const draft7Vocabularies = [
    core_1.default,
    validation_1.default,
    (0, applicator_1.default)(),
    format_1.default,
    metadata_1.metadataVocabulary,
    metadata_1.contentVocabulary
  ];
  exports2.default = draft7Vocabularies;
}));
var require_types = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.DiscrError = void 0;
  var DiscrError;
  (function(DiscrError2) {
    DiscrError2["Tag"] = "tag";
    DiscrError2["Mapping"] = "mapping";
  })(DiscrError || (exports2.DiscrError = DiscrError = {}));
}));
var require_discriminator = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const codegen_1 = require_codegen();
  const types_1 = require_types();
  const compile_1 = require_compile();
  const ref_error_1 = require_ref_error();
  const util_1 = require_util();
  const def = {
    keyword: "discriminator",
    type: "object",
    schemaType: "object",
    error: {
      message: ({ params: { discrError, tagName } }) => discrError === types_1.DiscrError.Tag ? `tag "${tagName}" must be string` : `value of tag "${tagName}" must be in oneOf`,
      params: ({ params: { discrError, tag, tagName } }) => (0, codegen_1._)`{error: ${discrError}, tag: ${tagName}, tagValue: ${tag}}`
    },
    code(cxt) {
      const { gen, data, schema, parentSchema, it } = cxt;
      const { oneOf } = parentSchema;
      if (!it.opts.discriminator) throw new Error("discriminator: requires discriminator option");
      const tagName = schema.propertyName;
      if (typeof tagName != "string") throw new Error("discriminator: requires propertyName");
      if (schema.mapping) throw new Error("discriminator: mapping is not supported");
      if (!oneOf) throw new Error("discriminator: requires oneOf keyword");
      const valid = gen.let("valid", false);
      const tag = gen.const("tag", (0, codegen_1._)`${data}${(0, codegen_1.getProperty)(tagName)}`);
      gen.if((0, codegen_1._)`typeof ${tag} == "string"`, () => validateMapping(), () => cxt.error(false, {
        discrError: types_1.DiscrError.Tag,
        tag,
        tagName
      }));
      cxt.ok(valid);
      function validateMapping() {
        const mapping = getMapping();
        gen.if(false);
        for (const tagValue in mapping) {
          gen.elseIf((0, codegen_1._)`${tag} === ${tagValue}`);
          gen.assign(valid, applyTagSchema(mapping[tagValue]));
        }
        gen.else();
        cxt.error(false, {
          discrError: types_1.DiscrError.Mapping,
          tag,
          tagName
        });
        gen.endIf();
      }
      function applyTagSchema(schemaProp) {
        const _valid = gen.name("valid");
        const schCxt = cxt.subschema({
          keyword: "oneOf",
          schemaProp
        }, _valid);
        cxt.mergeEvaluated(schCxt, codegen_1.Name);
        return _valid;
      }
      function getMapping() {
        var _a2;
        const oneOfMapping = {};
        const topRequired = hasRequired(parentSchema);
        let tagRequired = true;
        for (let i = 0; i < oneOf.length; i++) {
          let sch = oneOf[i];
          if ((sch === null || sch === void 0 ? void 0 : sch.$ref) && !(0, util_1.schemaHasRulesButRef)(sch, it.self.RULES)) {
            const ref = sch.$ref;
            sch = compile_1.resolveRef.call(it.self, it.schemaEnv.root, it.baseId, ref);
            if (sch instanceof compile_1.SchemaEnv) sch = sch.schema;
            if (sch === void 0) throw new ref_error_1.default(it.opts.uriResolver, it.baseId, ref);
          }
          const propSch = (_a2 = sch === null || sch === void 0 ? void 0 : sch.properties) === null || _a2 === void 0 ? void 0 : _a2[tagName];
          if (typeof propSch != "object") throw new Error(`discriminator: oneOf subschemas (or referenced schemas) must have "properties/${tagName}"`);
          tagRequired = tagRequired && (topRequired || hasRequired(sch));
          addMappings(propSch, i);
        }
        if (!tagRequired) throw new Error(`discriminator: "${tagName}" must be required`);
        return oneOfMapping;
        function hasRequired({ required: required2 }) {
          return Array.isArray(required2) && required2.includes(tagName);
        }
        function addMappings(sch, i) {
          if (sch.const) addMapping(sch.const, i);
          else if (sch.enum) for (const tagValue of sch.enum) addMapping(tagValue, i);
          else throw new Error(`discriminator: "properties/${tagName}" must have "const" or "enum"`);
        }
        function addMapping(tagValue, i) {
          if (typeof tagValue != "string" || tagValue in oneOfMapping) throw new Error(`discriminator: "${tagName}" values must be unique strings`);
          oneOfMapping[tagValue] = i;
        }
      }
    }
  };
  exports2.default = def;
}));
var require_json_schema_draft_07 = /* @__PURE__ */ __commonJSMin(((exports2, module2) => {
  module2.exports = {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://json-schema.org/draft-07/schema#",
    "title": "Core schema meta-schema",
    "definitions": {
      "schemaArray": {
        "type": "array",
        "minItems": 1,
        "items": { "$ref": "#" }
      },
      "nonNegativeInteger": {
        "type": "integer",
        "minimum": 0
      },
      "nonNegativeIntegerDefault0": { "allOf": [{ "$ref": "#/definitions/nonNegativeInteger" }, { "default": 0 }] },
      "simpleTypes": { "enum": [
        "array",
        "boolean",
        "integer",
        "null",
        "number",
        "object",
        "string"
      ] },
      "stringArray": {
        "type": "array",
        "items": { "type": "string" },
        "uniqueItems": true,
        "default": []
      }
    },
    "type": ["object", "boolean"],
    "properties": {
      "$id": {
        "type": "string",
        "format": "uri-reference"
      },
      "$schema": {
        "type": "string",
        "format": "uri"
      },
      "$ref": {
        "type": "string",
        "format": "uri-reference"
      },
      "$comment": { "type": "string" },
      "title": { "type": "string" },
      "description": { "type": "string" },
      "default": true,
      "readOnly": {
        "type": "boolean",
        "default": false
      },
      "examples": {
        "type": "array",
        "items": true
      },
      "multipleOf": {
        "type": "number",
        "exclusiveMinimum": 0
      },
      "maximum": { "type": "number" },
      "exclusiveMaximum": { "type": "number" },
      "minimum": { "type": "number" },
      "exclusiveMinimum": { "type": "number" },
      "maxLength": { "$ref": "#/definitions/nonNegativeInteger" },
      "minLength": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
      "pattern": {
        "type": "string",
        "format": "regex"
      },
      "additionalItems": { "$ref": "#" },
      "items": {
        "anyOf": [{ "$ref": "#" }, { "$ref": "#/definitions/schemaArray" }],
        "default": true
      },
      "maxItems": { "$ref": "#/definitions/nonNegativeInteger" },
      "minItems": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
      "uniqueItems": {
        "type": "boolean",
        "default": false
      },
      "contains": { "$ref": "#" },
      "maxProperties": { "$ref": "#/definitions/nonNegativeInteger" },
      "minProperties": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
      "required": { "$ref": "#/definitions/stringArray" },
      "additionalProperties": { "$ref": "#" },
      "definitions": {
        "type": "object",
        "additionalProperties": { "$ref": "#" },
        "default": {}
      },
      "properties": {
        "type": "object",
        "additionalProperties": { "$ref": "#" },
        "default": {}
      },
      "patternProperties": {
        "type": "object",
        "additionalProperties": { "$ref": "#" },
        "propertyNames": { "format": "regex" },
        "default": {}
      },
      "dependencies": {
        "type": "object",
        "additionalProperties": { "anyOf": [{ "$ref": "#" }, { "$ref": "#/definitions/stringArray" }] }
      },
      "propertyNames": { "$ref": "#" },
      "const": true,
      "enum": {
        "type": "array",
        "items": true,
        "minItems": 1,
        "uniqueItems": true
      },
      "type": { "anyOf": [{ "$ref": "#/definitions/simpleTypes" }, {
        "type": "array",
        "items": { "$ref": "#/definitions/simpleTypes" },
        "minItems": 1,
        "uniqueItems": true
      }] },
      "format": { "type": "string" },
      "contentMediaType": { "type": "string" },
      "contentEncoding": { "type": "string" },
      "if": { "$ref": "#" },
      "then": { "$ref": "#" },
      "else": { "$ref": "#" },
      "allOf": { "$ref": "#/definitions/schemaArray" },
      "anyOf": { "$ref": "#/definitions/schemaArray" },
      "oneOf": { "$ref": "#/definitions/schemaArray" },
      "not": { "$ref": "#" }
    },
    "default": true
  };
}));
var require_ajv = /* @__PURE__ */ __commonJSMin(((exports2, module2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.MissingRefError = exports2.ValidationError = exports2.CodeGen = exports2.Name = exports2.nil = exports2.stringify = exports2.str = exports2._ = exports2.KeywordCxt = exports2.Ajv = void 0;
  const core_1 = require_core$1();
  const draft7_1 = require_draft7();
  const discriminator_1 = require_discriminator();
  const draft7MetaSchema = require_json_schema_draft_07();
  const META_SUPPORT_DATA = ["/properties"];
  const META_SCHEMA_ID = "http://json-schema.org/draft-07/schema";
  var Ajv = class extends core_1.default {
    _addVocabularies() {
      super._addVocabularies();
      draft7_1.default.forEach((v) => this.addVocabulary(v));
      if (this.opts.discriminator) this.addKeyword(discriminator_1.default);
    }
    _addDefaultMetaSchema() {
      super._addDefaultMetaSchema();
      if (!this.opts.meta) return;
      const metaSchema = this.opts.$data ? this.$dataMetaSchema(draft7MetaSchema, META_SUPPORT_DATA) : draft7MetaSchema;
      this.addMetaSchema(metaSchema, META_SCHEMA_ID, false);
      this.refs["http://json-schema.org/schema"] = META_SCHEMA_ID;
    }
    defaultMeta() {
      return this.opts.defaultMeta = super.defaultMeta() || (this.getSchema(META_SCHEMA_ID) ? META_SCHEMA_ID : void 0);
    }
  };
  exports2.Ajv = Ajv;
  module2.exports = exports2 = Ajv;
  module2.exports.Ajv = Ajv;
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.default = Ajv;
  var validate_1 = require_validate();
  Object.defineProperty(exports2, "KeywordCxt", {
    enumerable: true,
    get: function() {
      return validate_1.KeywordCxt;
    }
  });
  var codegen_1 = require_codegen();
  Object.defineProperty(exports2, "_", {
    enumerable: true,
    get: function() {
      return codegen_1._;
    }
  });
  Object.defineProperty(exports2, "str", {
    enumerable: true,
    get: function() {
      return codegen_1.str;
    }
  });
  Object.defineProperty(exports2, "stringify", {
    enumerable: true,
    get: function() {
      return codegen_1.stringify;
    }
  });
  Object.defineProperty(exports2, "nil", {
    enumerable: true,
    get: function() {
      return codegen_1.nil;
    }
  });
  Object.defineProperty(exports2, "Name", {
    enumerable: true,
    get: function() {
      return codegen_1.Name;
    }
  });
  Object.defineProperty(exports2, "CodeGen", {
    enumerable: true,
    get: function() {
      return codegen_1.CodeGen;
    }
  });
  var validation_error_1 = require_validation_error();
  Object.defineProperty(exports2, "ValidationError", {
    enumerable: true,
    get: function() {
      return validation_error_1.default;
    }
  });
  var ref_error_1 = require_ref_error();
  Object.defineProperty(exports2, "MissingRefError", {
    enumerable: true,
    get: function() {
      return ref_error_1.default;
    }
  });
}));
var require_formats = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.formatNames = exports2.fastFormats = exports2.fullFormats = void 0;
  function fmtDef(validate2, compare) {
    return {
      validate: validate2,
      compare
    };
  }
  exports2.fullFormats = {
    date: fmtDef(date6, compareDate),
    time: fmtDef(getTime(true), compareTime),
    "date-time": fmtDef(getDateTime(true), compareDateTime),
    "iso-time": fmtDef(getTime(), compareIsoTime),
    "iso-date-time": fmtDef(getDateTime(), compareIsoDateTime),
    duration: /^P(?!$)((\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?|(\d+W)?)$/,
    uri: uri2,
    "uri-reference": /^(?:[a-z][a-z0-9+\-.]*:)?(?:\/?\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){3}|(?:(?:[0-9a-f]{1,4}:){0,2}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){2}|(?:(?:[0-9a-f]{1,4}:){0,3}[0-9a-f]{1,4})?::[0-9a-f]{1,4}:|(?:(?:[0-9a-f]{1,4}:){0,4}[0-9a-f]{1,4})?::)(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'"()*+,;=]|%[0-9a-f]{2})*)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})*)*)?(?:\?(?:[a-z0-9\-._~!$&'"()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'"()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i,
    "uri-template": /^(?:(?:[^\x00-\x20"'<>%\\^`{|}]|%[0-9a-f]{2})|\{[+#./;?&=,!@|]?(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?(?:,(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?)*\})*$/i,
    url: /^(?:https?|ftp):\/\/(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u{00a1}-\u{ffff}]+-)*[a-z0-9\u{00a1}-\u{ffff}]+)(?:\.(?:[a-z0-9\u{00a1}-\u{ffff}]+-)*[a-z0-9\u{00a1}-\u{ffff}]+)*(?:\.(?:[a-z\u{00a1}-\u{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/iu,
    email: /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i,
    hostname: /^(?=.{1,253}\.?$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*\.?$/i,
    ipv4: /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/,
    ipv6: /^((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))$/i,
    regex: regex2,
    uuid: /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i,
    "json-pointer": /^(?:\/(?:[^~/]|~0|~1)*)*$/,
    "json-pointer-uri-fragment": /^#(?:\/(?:[a-z0-9_\-.!$&'()*+,;:=@]|%[0-9a-f]{2}|~0|~1)*)*$/i,
    "relative-json-pointer": /^(?:0|[1-9][0-9]*)(?:#|(?:\/(?:[^~/]|~0|~1)*)*)$/,
    byte,
    int32: {
      type: "number",
      validate: validateInt32
    },
    int64: {
      type: "number",
      validate: validateInt64
    },
    float: {
      type: "number",
      validate: validateNumber
    },
    double: {
      type: "number",
      validate: validateNumber
    },
    password: true,
    binary: true
  };
  exports2.fastFormats = {
    ...exports2.fullFormats,
    date: fmtDef(/^\d\d\d\d-[0-1]\d-[0-3]\d$/, compareDate),
    time: fmtDef(/^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i, compareTime),
    "date-time": fmtDef(/^\d\d\d\d-[0-1]\d-[0-3]\dt(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i, compareDateTime),
    "iso-time": fmtDef(/^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)?$/i, compareIsoTime),
    "iso-date-time": fmtDef(/^\d\d\d\d-[0-1]\d-[0-3]\d[t\s](?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)?$/i, compareIsoDateTime),
    uri: /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/)?[^\s]*$/i,
    "uri-reference": /^(?:(?:[a-z][a-z0-9+\-.]*:)?\/?\/)?(?:[^\\\s#][^\s#]*)?(?:#[^\\\s]*)?$/i,
    email: /^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/i
  };
  exports2.formatNames = Object.keys(exports2.fullFormats);
  function isLeapYear2(year) {
    return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
  }
  const DATE2 = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
  const DAYS2 = [
    0,
    31,
    28,
    31,
    30,
    31,
    30,
    31,
    31,
    30,
    31,
    30,
    31
  ];
  function date6(str) {
    const matches = DATE2.exec(str);
    if (!matches) return false;
    const year = +matches[1];
    const month = +matches[2];
    const day = +matches[3];
    return month >= 1 && month <= 12 && day >= 1 && day <= (month === 2 && isLeapYear2(year) ? 29 : DAYS2[month]);
  }
  function compareDate(d1, d2) {
    if (!(d1 && d2)) return void 0;
    if (d1 > d2) return 1;
    if (d1 < d2) return -1;
    return 0;
  }
  const TIME2 = /^(\d\d):(\d\d):(\d\d(?:\.\d+)?)(z|([+-])(\d\d)(?::?(\d\d))?)?$/i;
  function getTime(strictTimeZone) {
    return function time4(str) {
      const matches = TIME2.exec(str);
      if (!matches) return false;
      const hr = +matches[1];
      const min = +matches[2];
      const sec = +matches[3];
      const tz = matches[4];
      const tzSign = matches[5] === "-" ? -1 : 1;
      const tzH = +(matches[6] || 0);
      const tzM = +(matches[7] || 0);
      if (tzH > 23 || tzM > 59 || strictTimeZone && !tz) return false;
      if (hr <= 23 && min <= 59 && sec < 60) return true;
      const utcMin = min - tzM * tzSign;
      const utcHr = hr - tzH * tzSign - (utcMin < 0 ? 1 : 0);
      return (utcHr === 23 || utcHr === -1) && (utcMin === 59 || utcMin === -1) && sec < 61;
    };
  }
  function compareTime(s1, s2) {
    if (!(s1 && s2)) return void 0;
    const t1 = (/* @__PURE__ */ new Date("2020-01-01T" + s1)).valueOf();
    const t2 = (/* @__PURE__ */ new Date("2020-01-01T" + s2)).valueOf();
    if (!(t1 && t2)) return void 0;
    return t1 - t2;
  }
  function compareIsoTime(t1, t2) {
    if (!(t1 && t2)) return void 0;
    const a1 = TIME2.exec(t1);
    const a2 = TIME2.exec(t2);
    if (!(a1 && a2)) return void 0;
    t1 = a1[1] + a1[2] + a1[3];
    t2 = a2[1] + a2[2] + a2[3];
    if (t1 > t2) return 1;
    if (t1 < t2) return -1;
    return 0;
  }
  const DATE_TIME_SEPARATOR2 = /t|\s/i;
  function getDateTime(strictTimeZone) {
    const time4 = getTime(strictTimeZone);
    return function date_time2(str) {
      const dateTime = str.split(DATE_TIME_SEPARATOR2);
      return dateTime.length === 2 && date6(dateTime[0]) && time4(dateTime[1]);
    };
  }
  function compareDateTime(dt1, dt2) {
    if (!(dt1 && dt2)) return void 0;
    const d1 = new Date(dt1).valueOf();
    const d2 = new Date(dt2).valueOf();
    if (!(d1 && d2)) return void 0;
    return d1 - d2;
  }
  function compareIsoDateTime(dt1, dt2) {
    if (!(dt1 && dt2)) return void 0;
    const [d1, t1] = dt1.split(DATE_TIME_SEPARATOR2);
    const [d2, t2] = dt2.split(DATE_TIME_SEPARATOR2);
    const res = compareDate(d1, d2);
    if (res === void 0) return void 0;
    return res || compareTime(t1, t2);
  }
  const NOT_URI_FRAGMENT2 = /\/|:/;
  const URI = /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){3}|(?:(?:[0-9a-f]{1,4}:){0,2}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){2}|(?:(?:[0-9a-f]{1,4}:){0,3}[0-9a-f]{1,4})?::[0-9a-f]{1,4}:|(?:(?:[0-9a-f]{1,4}:){0,4}[0-9a-f]{1,4})?::)(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'()*+,;=]|%[0-9a-f]{2})*)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)(?:\?(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i;
  function uri2(str) {
    return NOT_URI_FRAGMENT2.test(str) && URI.test(str);
  }
  const BYTE = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/gm;
  function byte(str) {
    BYTE.lastIndex = 0;
    return BYTE.test(str);
  }
  const MIN_INT32 = -(2 ** 31);
  const MAX_INT32 = 2 ** 31 - 1;
  function validateInt32(value) {
    return Number.isInteger(value) && value <= MAX_INT32 && value >= MIN_INT32;
  }
  function validateInt64(value) {
    return Number.isInteger(value);
  }
  function validateNumber() {
    return true;
  }
  const Z_ANCHOR2 = /[^\\]\\Z/;
  function regex2(str) {
    if (Z_ANCHOR2.test(str)) return false;
    try {
      new RegExp(str);
      return true;
    } catch (e) {
      return false;
    }
  }
}));
var require_limit = /* @__PURE__ */ __commonJSMin(((exports2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.formatLimitDefinition = void 0;
  const ajv_1 = require_ajv();
  const codegen_1 = require_codegen();
  const ops = codegen_1.operators;
  const KWDs = {
    formatMaximum: {
      okStr: "<=",
      ok: ops.LTE,
      fail: ops.GT
    },
    formatMinimum: {
      okStr: ">=",
      ok: ops.GTE,
      fail: ops.LT
    },
    formatExclusiveMaximum: {
      okStr: "<",
      ok: ops.LT,
      fail: ops.GTE
    },
    formatExclusiveMinimum: {
      okStr: ">",
      ok: ops.GT,
      fail: ops.LTE
    }
  };
  const error2 = {
    message: ({ keyword, schemaCode }) => (0, codegen_1.str)`should be ${KWDs[keyword].okStr} ${schemaCode}`,
    params: ({ keyword, schemaCode }) => (0, codegen_1._)`{comparison: ${KWDs[keyword].okStr}, limit: ${schemaCode}}`
  };
  exports2.formatLimitDefinition = {
    keyword: Object.keys(KWDs),
    type: "string",
    schemaType: "string",
    $data: true,
    error: error2,
    code(cxt) {
      const { gen, data, schemaCode, keyword, it } = cxt;
      const { opts, self: self2 } = it;
      if (!opts.validateFormats) return;
      const fCxt = new ajv_1.KeywordCxt(it, self2.RULES.all.format.definition, "format");
      if (fCxt.$data) validate$DataFormat();
      else validateFormat();
      function validate$DataFormat() {
        const fmts = gen.scopeValue("formats", {
          ref: self2.formats,
          code: opts.code.formats
        });
        const fmt = gen.const("fmt", (0, codegen_1._)`${fmts}[${fCxt.schemaCode}]`);
        cxt.fail$data((0, codegen_1.or)((0, codegen_1._)`typeof ${fmt} != "object"`, (0, codegen_1._)`${fmt} instanceof RegExp`, (0, codegen_1._)`typeof ${fmt}.compare != "function"`, compareCode(fmt)));
      }
      function validateFormat() {
        const format2 = fCxt.schema;
        const fmtDef = self2.formats[format2];
        if (!fmtDef || fmtDef === true) return;
        if (typeof fmtDef != "object" || fmtDef instanceof RegExp || typeof fmtDef.compare != "function") throw new Error(`"${keyword}": format "${format2}" does not define "compare" function`);
        const fmt = gen.scopeValue("formats", {
          key: format2,
          ref: fmtDef,
          code: opts.code.formats ? (0, codegen_1._)`${opts.code.formats}${(0, codegen_1.getProperty)(format2)}` : void 0
        });
        cxt.fail$data(compareCode(fmt));
      }
      function compareCode(fmt) {
        return (0, codegen_1._)`${fmt}.compare(${data}, ${schemaCode}) ${KWDs[keyword].fail} 0`;
      }
    },
    dependencies: ["format"]
  };
  const formatLimitPlugin = (ajv) => {
    ajv.addKeyword(exports2.formatLimitDefinition);
    return ajv;
  };
  exports2.default = formatLimitPlugin;
}));
var require_dist = /* @__PURE__ */ __commonJSMin(((exports2, module2) => {
  Object.defineProperty(exports2, "__esModule", { value: true });
  const formats_1 = require_formats();
  const limit_1 = require_limit();
  const codegen_1 = require_codegen();
  const fullName = new codegen_1.Name("fullFormats");
  const fastName = new codegen_1.Name("fastFormats");
  const formatsPlugin = (ajv, opts = { keywords: true }) => {
    if (Array.isArray(opts)) {
      addFormats(ajv, opts, formats_1.fullFormats, fullName);
      return ajv;
    }
    const [formats, exportName] = opts.mode === "fast" ? [formats_1.fastFormats, fastName] : [formats_1.fullFormats, fullName];
    addFormats(ajv, opts.formats || formats_1.formatNames, formats, exportName);
    if (opts.keywords) (0, limit_1.default)(ajv);
    return ajv;
  };
  formatsPlugin.get = (name, mode = "full") => {
    const f = (mode === "fast" ? formats_1.fastFormats : formats_1.fullFormats)[name];
    if (!f) throw new Error(`Unknown format "${name}"`);
    return f;
  };
  function addFormats(ajv, list, fs3, exportName) {
    var _a2;
    var _b;
    (_a2 = (_b = ajv.opts.code).formats) !== null && _a2 !== void 0 || (_b.formats = (0, codegen_1._)`require("ajv-formats/dist/formats").${exportName}`);
    for (const f of list) ajv.addFormat(f, fs3[f]);
  }
  module2.exports = exports2 = formatsPlugin;
  Object.defineProperty(exports2, "__esModule", { value: true });
  exports2.default = formatsPlugin;
}));
var import_ajv = require_ajv();
var import_dist = /* @__PURE__ */ __toESM2(require_dist(), 1);
function createDefaultAjvInstance() {
  const ajv = new import_ajv.Ajv({
    strict: false,
    validateFormats: true,
    validateSchema: false,
    allErrors: true
  });
  (0, import_dist.default)(ajv);
  return ajv;
}
var AjvJsonSchemaValidator = class {
  _ajv;
  /**
  * Create an AJV validator
  *
  * @param ajv - Optional pre-configured AJV instance. If not provided, a default instance will be created.
  *
  * @example Use default configuration (recommended for most cases)
  * ```ts source="./ajvProvider.examples.ts#AjvJsonSchemaValidator_default"
  * const validator = new AjvJsonSchemaValidator();
  * ```
  *
  * @example Provide custom AJV instance for advanced configuration
  * ```ts source="./ajvProvider.examples.ts#AjvJsonSchemaValidator_constructor_withFormats"
  * const ajv = new Ajv({ validateFormats: true });
  * addFormats(ajv);
  * const validator = new AjvJsonSchemaValidator(ajv);
  * ```
  */
  constructor(ajv) {
    this._ajv = ajv ?? createDefaultAjvInstance();
  }
  /**
  * Create a validator for the given JSON Schema
  *
  * The validator is compiled once and can be reused multiple times.
  * If the schema has an `$id`, it will be cached by AJV automatically.
  *
  * @param schema - Standard JSON Schema object
  * @returns A validator function that validates input data
  */
  getValidator(schema) {
    const ajvValidator = "$id" in schema && typeof schema.$id === "string" ? this._ajv.getSchema(schema.$id) ?? this._ajv.compile(schema) : this._ajv.compile(schema);
    return (input) => {
      return ajvValidator(input) ? {
        valid: true,
        data: input,
        errorMessage: void 0
      } : {
        valid: false,
        data: void 0,
        errorMessage: this._ajv.errorsText(ajvValidator.errors)
      };
    };
  }
};

// ../../node_modules/@modelcontextprotocol/client/dist/shimsNode.mjs
var CORS_IS_POSSIBLE = false;

// ../../node_modules/pkce-challenge/dist/index.node.js
var crypto2;
crypto2 = globalThis.crypto?.webcrypto ?? // Node.js [18-16] REPL
globalThis.crypto ?? // Node.js >18
import("node:crypto").then((m) => m.webcrypto);
async function getRandomValues(size) {
  return (await crypto2).getRandomValues(new Uint8Array(size));
}
async function random(size) {
  const mask = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~";
  const evenDistCutoff = Math.pow(2, 8) - Math.pow(2, 8) % mask.length;
  let result = "";
  while (result.length < size) {
    const randomBytes = await getRandomValues(size - result.length);
    for (const randomByte of randomBytes) {
      if (randomByte < evenDistCutoff) {
        result += mask[randomByte % mask.length];
      }
    }
  }
  return result;
}
async function generateVerifier(length) {
  return await random(length);
}
async function generateChallenge(code_verifier) {
  const buffer = await (await crypto2).subtle.digest("SHA-256", new TextEncoder().encode(code_verifier));
  return btoa(String.fromCharCode(...new Uint8Array(buffer))).replace(/\//g, "_").replace(/\+/g, "-").replace(/=/g, "");
}
async function pkceChallenge(length) {
  if (!length)
    length = 43;
  if (length < 43 || length > 128) {
    throw `Expected a length between 43 and 128. Received ${length}.`;
  }
  const verifier = await generateVerifier(length);
  const challenge = await generateChallenge(verifier);
  return {
    code_verifier: verifier,
    code_challenge: challenge
  };
}

// ../../node_modules/eventsource-parser/dist/index.js
var ParseError = class extends Error {
  constructor(message, options) {
    super(message), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
  }
};
function noop(_arg) {
}
function createParser(callbacks) {
  if (typeof callbacks == "function")
    throw new TypeError(
      "`callbacks` must be an object, got a function instead. Did you mean `{onEvent: fn}`?"
    );
  const { onEvent = noop, onError = noop, onRetry = noop, onComment } = callbacks;
  let incompleteLine = "", isFirstChunk = true, id, data = "", eventType = "";
  function feed(newChunk) {
    const chunk = isFirstChunk ? newChunk.replace(/^\xEF\xBB\xBF/, "") : newChunk, [complete, incomplete] = splitLines(`${incompleteLine}${chunk}`);
    for (const line of complete)
      parseLine(line);
    incompleteLine = incomplete, isFirstChunk = false;
  }
  function parseLine(line) {
    if (line === "") {
      dispatchEvent();
      return;
    }
    if (line.startsWith(":")) {
      onComment && onComment(line.slice(line.startsWith(": ") ? 2 : 1));
      return;
    }
    const fieldSeparatorIndex = line.indexOf(":");
    if (fieldSeparatorIndex !== -1) {
      const field = line.slice(0, fieldSeparatorIndex), offset = line[fieldSeparatorIndex + 1] === " " ? 2 : 1, value = line.slice(fieldSeparatorIndex + offset);
      processField(field, value, line);
      return;
    }
    processField(line, "", line);
  }
  function processField(field, value, line) {
    switch (field) {
      case "event":
        eventType = value;
        break;
      case "data":
        data = `${data}${value}
`;
        break;
      case "id":
        id = value.includes("\0") ? void 0 : value;
        break;
      case "retry":
        /^\d+$/.test(value) ? onRetry(parseInt(value, 10)) : onError(
          new ParseError(`Invalid \`retry\` value: "${value}"`, {
            type: "invalid-retry",
            value,
            line
          })
        );
        break;
      default:
        onError(
          new ParseError(
            `Unknown field "${field.length > 20 ? `${field.slice(0, 20)}\u2026` : field}"`,
            { type: "unknown-field", field, value, line }
          )
        );
        break;
    }
  }
  function dispatchEvent() {
    data.length > 0 && onEvent({
      id,
      event: eventType || void 0,
      // If the data buffer's last character is a U+000A LINE FEED (LF) character,
      // then remove the last character from the data buffer.
      data: data.endsWith(`
`) ? data.slice(0, -1) : data
    }), id = void 0, data = "", eventType = "";
  }
  function reset(options = {}) {
    incompleteLine && options.consume && parseLine(incompleteLine), isFirstChunk = true, id = void 0, data = "", eventType = "", incompleteLine = "";
  }
  return { feed, reset };
}
function splitLines(chunk) {
  const lines = [];
  let incompleteLine = "", searchIndex = 0;
  for (; searchIndex < chunk.length; ) {
    const crIndex = chunk.indexOf("\r", searchIndex), lfIndex = chunk.indexOf(`
`, searchIndex);
    let lineEnd = -1;
    if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = Math.min(crIndex, lfIndex) : crIndex !== -1 ? crIndex === chunk.length - 1 ? lineEnd = -1 : lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1) {
      incompleteLine = chunk.slice(searchIndex);
      break;
    } else {
      const line = chunk.slice(searchIndex, lineEnd);
      lines.push(line), searchIndex = lineEnd + 1, chunk[searchIndex - 1] === "\r" && chunk[searchIndex] === `
` && searchIndex++;
    }
  }
  return [lines, incompleteLine];
}

// ../../node_modules/@modelcontextprotocol/client/dist/index.mjs
var import_node_process = __toESM(require("node:process"), 1);
var import_node_stream = require("node:stream");
var import_cross_spawn = __toESM(require_cross_spawn(), 1);

// ../../node_modules/eventsource-parser/dist/stream.js
var EventSourceParserStream = class extends TransformStream {
  constructor({ onError, onRetry, onComment } = {}) {
    let parser;
    super({
      start(controller) {
        parser = createParser({
          onEvent: (event) => {
            controller.enqueue(event);
          },
          onError(error2) {
            onError === "terminate" ? controller.error(error2) : typeof onError == "function" && onError(error2);
          },
          onRetry,
          onComment
        });
      },
      transform(chunk) {
        parser.feed(chunk);
      }
    });
  }
};

// ../../node_modules/@modelcontextprotocol/client/dist/index.mjs
function assertToolsCallTaskCapability(requests, method, entityName) {
  if (!requests) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `${entityName} does not support task creation (required for ${method})`);
  switch (method) {
    case "tools/call":
      if (!requests.tools?.call) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `${entityName} does not support task creation for tools/call (required for ${method})`);
      break;
    default:
      break;
  }
}
function assertClientRequestTaskCapability(requests, method, entityName) {
  if (!requests) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `${entityName} does not support task creation (required for ${method})`);
  switch (method) {
    case "sampling/createMessage":
      if (!requests.sampling?.createMessage) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `${entityName} does not support task creation for sampling/createMessage (required for ${method})`);
      break;
    case "elicitation/create":
      if (!requests.elicitation?.create) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `${entityName} does not support task creation for elicitation/create (required for ${method})`);
      break;
    default:
      break;
  }
}
function isOAuthClientProvider(provider) {
  if (provider == null) return false;
  const p = provider;
  return typeof p.tokens === "function" && typeof p.clientInformation === "function";
}
async function handleOAuthUnauthorized(provider, ctx) {
  const { resourceMetadataUrl, scope } = extractWWWAuthenticateParams(ctx.response);
  if (await auth(provider, {
    serverUrl: ctx.serverUrl,
    resourceMetadataUrl,
    scope,
    fetchFn: ctx.fetchFn
  }) !== "AUTHORIZED") throw new UnauthorizedError();
}
function adaptOAuthProvider(provider) {
  return {
    token: async () => {
      return (await provider.tokens())?.access_token;
    },
    onUnauthorized: async (ctx) => handleOAuthUnauthorized(provider, ctx)
  };
}
var UnauthorizedError = class extends Error {
  constructor(message) {
    super(message ?? "Unauthorized");
  }
};
function isClientAuthMethod(method) {
  return [
    "client_secret_basic",
    "client_secret_post",
    "none"
  ].includes(method);
}
var AUTHORIZATION_CODE_RESPONSE_TYPE = "code";
var AUTHORIZATION_CODE_CHALLENGE_METHOD = "S256";
function selectClientAuthMethod(clientInformation, supportedMethods) {
  const hasClientSecret = clientInformation.client_secret !== void 0;
  if ("token_endpoint_auth_method" in clientInformation && clientInformation.token_endpoint_auth_method && isClientAuthMethod(clientInformation.token_endpoint_auth_method) && (supportedMethods.length === 0 || supportedMethods.includes(clientInformation.token_endpoint_auth_method))) return clientInformation.token_endpoint_auth_method;
  if (supportedMethods.length === 0) return hasClientSecret ? "client_secret_basic" : "none";
  if (hasClientSecret && supportedMethods.includes("client_secret_basic")) return "client_secret_basic";
  if (hasClientSecret && supportedMethods.includes("client_secret_post")) return "client_secret_post";
  if (supportedMethods.includes("none")) return "none";
  return hasClientSecret ? "client_secret_post" : "none";
}
function applyClientAuthentication(method, clientInformation, headers, params) {
  const { client_id, client_secret } = clientInformation;
  switch (method) {
    case "client_secret_basic":
      applyBasicAuth(client_id, client_secret, headers);
      return;
    case "client_secret_post":
      applyPostAuth(client_id, client_secret, params);
      return;
    case "none":
      applyPublicAuth(client_id, params);
      return;
    default:
      throw new Error(`Unsupported client authentication method: ${method}`);
  }
}
function applyBasicAuth(clientId, clientSecret, headers) {
  if (!clientSecret) throw new Error("client_secret_basic authentication requires a client_secret");
  const credentials = btoa(`${clientId}:${clientSecret}`);
  headers.set("Authorization", `Basic ${credentials}`);
}
function applyPostAuth(clientId, clientSecret, params) {
  params.set("client_id", clientId);
  if (clientSecret) params.set("client_secret", clientSecret);
}
function applyPublicAuth(clientId, params) {
  params.set("client_id", clientId);
}
async function parseErrorResponse(input) {
  const statusCode = input instanceof Response ? input.status : void 0;
  const body = input instanceof Response ? await input.text() : input;
  try {
    const result = OAuthErrorResponseSchema.parse(JSON.parse(body));
    return OAuthError.fromResponse(result);
  } catch (error2) {
    const errorMessage = `${statusCode ? `HTTP ${statusCode}: ` : ""}Invalid OAuth error response: ${error2}. Raw body: ${body}`;
    return new OAuthError(OAuthErrorCode.ServerError, errorMessage);
  }
}
async function auth(provider, options) {
  try {
    return await authInternal(provider, options);
  } catch (error2) {
    if (error2 instanceof OAuthError) {
      if (error2.code === OAuthErrorCode.InvalidClient || error2.code === OAuthErrorCode.UnauthorizedClient) {
        await provider.invalidateCredentials?.("all");
        return await authInternal(provider, options);
      } else if (error2.code === OAuthErrorCode.InvalidGrant) {
        await provider.invalidateCredentials?.("tokens");
        return await authInternal(provider, options);
      }
    }
    throw error2;
  }
}
function determineScope(options) {
  const { requestedScope, resourceMetadata, authServerMetadata, clientMetadata } = options;
  let effectiveScope = requestedScope || resourceMetadata?.scopes_supported?.join(" ") || clientMetadata.scope;
  if (effectiveScope && authServerMetadata?.scopes_supported?.includes("offline_access") && !effectiveScope.split(" ").includes("offline_access") && clientMetadata.grant_types?.includes("refresh_token")) effectiveScope = `${effectiveScope} offline_access`;
  return effectiveScope;
}
async function authInternal(provider, { serverUrl, authorizationCode, scope, resourceMetadataUrl, fetchFn }) {
  const cachedState = await provider.discoveryState?.();
  let resourceMetadata;
  let authorizationServerUrl;
  let metadata;
  let effectiveResourceMetadataUrl = resourceMetadataUrl;
  if (!effectiveResourceMetadataUrl && cachedState?.resourceMetadataUrl) effectiveResourceMetadataUrl = new URL(cachedState.resourceMetadataUrl);
  if (cachedState?.authorizationServerUrl) {
    authorizationServerUrl = cachedState.authorizationServerUrl;
    resourceMetadata = cachedState.resourceMetadata;
    metadata = cachedState.authorizationServerMetadata ?? await discoverAuthorizationServerMetadata(authorizationServerUrl, { fetchFn });
    if (!resourceMetadata) try {
      resourceMetadata = await discoverOAuthProtectedResourceMetadata(serverUrl, { resourceMetadataUrl: effectiveResourceMetadataUrl }, fetchFn);
    } catch (error2) {
      if (error2 instanceof TypeError) throw error2;
    }
    if (metadata !== cachedState.authorizationServerMetadata || resourceMetadata !== cachedState.resourceMetadata) await provider.saveDiscoveryState?.({
      authorizationServerUrl: String(authorizationServerUrl),
      resourceMetadataUrl: effectiveResourceMetadataUrl?.toString(),
      resourceMetadata,
      authorizationServerMetadata: metadata
    });
  } else {
    const serverInfo = await discoverOAuthServerInfo(serverUrl, {
      resourceMetadataUrl: effectiveResourceMetadataUrl,
      fetchFn
    });
    authorizationServerUrl = serverInfo.authorizationServerUrl;
    metadata = serverInfo.authorizationServerMetadata;
    resourceMetadata = serverInfo.resourceMetadata;
    await provider.saveDiscoveryState?.({
      authorizationServerUrl: String(authorizationServerUrl),
      resourceMetadataUrl: effectiveResourceMetadataUrl?.toString(),
      resourceMetadata,
      authorizationServerMetadata: metadata
    });
  }
  await provider.saveAuthorizationServerUrl?.(String(authorizationServerUrl));
  const resource = await selectResourceURL(serverUrl, provider, resourceMetadata);
  if (resource) await provider.saveResourceUrl?.(String(resource));
  const resolvedScope = determineScope({
    requestedScope: scope,
    resourceMetadata,
    authServerMetadata: metadata,
    clientMetadata: provider.clientMetadata
  });
  let clientInformation = await Promise.resolve(provider.clientInformation());
  if (!clientInformation) {
    if (authorizationCode !== void 0) throw new Error("Existing OAuth client information is required when exchanging an authorization code");
    const supportsUrlBasedClientId = metadata?.client_id_metadata_document_supported === true;
    const clientMetadataUrl = provider.clientMetadataUrl;
    if (clientMetadataUrl && !isHttpsUrl(clientMetadataUrl)) throw new OAuthError(OAuthErrorCode.InvalidClientMetadata, `clientMetadataUrl must be a valid HTTPS URL with a non-root pathname, got: ${clientMetadataUrl}`);
    if (supportsUrlBasedClientId && clientMetadataUrl) {
      clientInformation = { client_id: clientMetadataUrl };
      await provider.saveClientInformation?.(clientInformation);
    } else {
      if (!provider.saveClientInformation) throw new Error("OAuth client information must be saveable for dynamic registration");
      const fullInformation = await registerClient(authorizationServerUrl, {
        metadata,
        clientMetadata: provider.clientMetadata,
        scope: resolvedScope,
        fetchFn
      });
      await provider.saveClientInformation(fullInformation);
      clientInformation = fullInformation;
    }
  }
  const nonInteractiveFlow = !provider.redirectUrl;
  if (authorizationCode !== void 0 || nonInteractiveFlow) {
    const tokens$1 = await fetchToken(provider, authorizationServerUrl, {
      metadata,
      resource,
      authorizationCode,
      scope: resolvedScope,
      fetchFn
    });
    await provider.saveTokens(tokens$1);
    return "AUTHORIZED";
  }
  const tokens = await provider.tokens();
  if (tokens?.refresh_token) try {
    const newTokens = await refreshAuthorization(authorizationServerUrl, {
      metadata,
      clientInformation,
      refreshToken: tokens.refresh_token,
      resource,
      addClientAuthentication: provider.addClientAuthentication,
      fetchFn
    });
    await provider.saveTokens(newTokens);
    return "AUTHORIZED";
  } catch (error2) {
    if (!(error2 instanceof OAuthError) || error2.code === OAuthErrorCode.ServerError) {
    } else throw error2;
  }
  const state = provider.state ? await provider.state() : void 0;
  const { authorizationUrl, codeVerifier } = await startAuthorization(authorizationServerUrl, {
    metadata,
    clientInformation,
    state,
    redirectUrl: provider.redirectUrl,
    scope: resolvedScope,
    resource
  });
  await provider.saveCodeVerifier(codeVerifier);
  await provider.redirectToAuthorization(authorizationUrl);
  return "REDIRECT";
}
function isHttpsUrl(value) {
  if (!value) return false;
  try {
    const url2 = new URL(value);
    return url2.protocol === "https:" && url2.pathname !== "/";
  } catch {
    return false;
  }
}
async function selectResourceURL(serverUrl, provider, resourceMetadata) {
  const defaultResource = resourceUrlFromServerUrl(serverUrl);
  if (provider.validateResourceURL) return await provider.validateResourceURL(defaultResource, resourceMetadata?.resource);
  if (!resourceMetadata) return;
  if (!checkResourceAllowed({
    requestedResource: defaultResource,
    configuredResource: resourceMetadata.resource
  })) throw new Error(`Protected resource ${resourceMetadata.resource} does not match expected ${defaultResource} (or origin)`);
  return new URL(resourceMetadata.resource);
}
function extractWWWAuthenticateParams(res) {
  const authenticateHeader = res.headers.get("WWW-Authenticate");
  if (!authenticateHeader) return {};
  const [type, scheme] = authenticateHeader.split(" ");
  if (type?.toLowerCase() !== "bearer" || !scheme) return {};
  const resourceMetadataMatch = extractFieldFromWwwAuth(res, "resource_metadata") || void 0;
  let resourceMetadataUrl;
  if (resourceMetadataMatch) try {
    resourceMetadataUrl = new URL(resourceMetadataMatch);
  } catch {
  }
  const scope = extractFieldFromWwwAuth(res, "scope") || void 0;
  const error2 = extractFieldFromWwwAuth(res, "error") || void 0;
  return {
    resourceMetadataUrl,
    scope,
    error: error2
  };
}
function extractFieldFromWwwAuth(response, fieldName) {
  const wwwAuthHeader = response.headers.get("WWW-Authenticate");
  if (!wwwAuthHeader) return null;
  const pattern = new RegExp(String.raw`${fieldName}=(?:"([^"]+)"|([^\s,]+))`);
  const match = wwwAuthHeader.match(pattern);
  if (match) {
    const result = match[1] || match[2];
    if (result) return result;
  }
  return null;
}
async function discoverOAuthProtectedResourceMetadata(serverUrl, opts, fetchFn = fetch) {
  const response = await discoverMetadataWithFallback(serverUrl, "oauth-protected-resource", fetchFn, {
    protocolVersion: opts?.protocolVersion,
    metadataUrl: opts?.resourceMetadataUrl
  });
  if (!response || response.status === 404) {
    await response?.text?.().catch(() => {
    });
    throw new Error(`Resource server does not implement OAuth 2.0 Protected Resource Metadata.`);
  }
  if (!response.ok) {
    await response.text?.().catch(() => {
    });
    throw new Error(`HTTP ${response.status} trying to load well-known OAuth protected resource metadata.`);
  }
  return OAuthProtectedResourceMetadataSchema.parse(await response.json());
}
async function fetchWithCorsRetry(url2, headers, fetchFn = fetch) {
  try {
    return await fetchFn(url2, { headers });
  } catch (error2) {
    if (!(error2 instanceof TypeError) || !CORS_IS_POSSIBLE) throw error2;
    if (headers) try {
      return await fetchFn(url2, {});
    } catch (retryError) {
      if (!(retryError instanceof TypeError)) throw retryError;
      return;
    }
    return;
  }
}
function buildWellKnownPath(wellKnownPrefix, pathname = "", options = {}) {
  if (pathname.endsWith("/")) pathname = pathname.slice(0, -1);
  return options.prependPathname ? `${pathname}/.well-known/${wellKnownPrefix}` : `/.well-known/${wellKnownPrefix}${pathname}`;
}
async function tryMetadataDiscovery(url2, protocolVersion, fetchFn = fetch) {
  return await fetchWithCorsRetry(url2, { "MCP-Protocol-Version": protocolVersion }, fetchFn);
}
function shouldAttemptFallback(response, pathname) {
  if (!response) return true;
  if (pathname === "/") return false;
  return response.status >= 400 && response.status < 500 || response.status === 502;
}
async function discoverMetadataWithFallback(serverUrl, wellKnownType, fetchFn, opts) {
  const issuer = new URL(serverUrl);
  const protocolVersion = opts?.protocolVersion ?? LATEST_PROTOCOL_VERSION;
  let url2;
  if (opts?.metadataUrl) url2 = new URL(opts.metadataUrl);
  else {
    const wellKnownPath = buildWellKnownPath(wellKnownType, issuer.pathname);
    url2 = new URL(wellKnownPath, opts?.metadataServerUrl ?? issuer);
    url2.search = issuer.search;
  }
  let response = await tryMetadataDiscovery(url2, protocolVersion, fetchFn);
  if (!opts?.metadataUrl && shouldAttemptFallback(response, issuer.pathname)) response = await tryMetadataDiscovery(new URL(`/.well-known/${wellKnownType}`, issuer), protocolVersion, fetchFn);
  return response;
}
function buildDiscoveryUrls(authorizationServerUrl) {
  const url2 = typeof authorizationServerUrl === "string" ? new URL(authorizationServerUrl) : authorizationServerUrl;
  const hasPath = url2.pathname !== "/";
  const urlsToTry = [];
  if (!hasPath) {
    urlsToTry.push({
      url: new URL("/.well-known/oauth-authorization-server", url2.origin),
      type: "oauth"
    }, {
      url: new URL(`/.well-known/openid-configuration`, url2.origin),
      type: "oidc"
    });
    return urlsToTry;
  }
  let pathname = url2.pathname;
  if (pathname.endsWith("/")) pathname = pathname.slice(0, -1);
  urlsToTry.push({
    url: new URL(`/.well-known/oauth-authorization-server${pathname}`, url2.origin),
    type: "oauth"
  }, {
    url: new URL(`/.well-known/openid-configuration${pathname}`, url2.origin),
    type: "oidc"
  }, {
    url: new URL(`${pathname}/.well-known/openid-configuration`, url2.origin),
    type: "oidc"
  });
  return urlsToTry;
}
async function discoverAuthorizationServerMetadata(authorizationServerUrl, { fetchFn = fetch, protocolVersion = LATEST_PROTOCOL_VERSION } = {}) {
  const headers = {
    "MCP-Protocol-Version": protocolVersion,
    Accept: "application/json"
  };
  const urlsToTry = buildDiscoveryUrls(authorizationServerUrl);
  for (const { url: endpointUrl, type } of urlsToTry) {
    const response = await fetchWithCorsRetry(endpointUrl, headers, fetchFn);
    if (!response)
      continue;
    if (!response.ok) {
      await response.text?.().catch(() => {
      });
      if (response.status >= 400 && response.status < 500 || response.status === 502) continue;
      throw new Error(`HTTP ${response.status} trying to load ${type === "oauth" ? "OAuth" : "OpenID provider"} metadata from ${endpointUrl}`);
    }
    return type === "oauth" ? OAuthMetadataSchema.parse(await response.json()) : OpenIdProviderDiscoveryMetadataSchema.parse(await response.json());
  }
}
async function discoverOAuthServerInfo(serverUrl, opts) {
  let resourceMetadata;
  let authorizationServerUrl;
  try {
    resourceMetadata = await discoverOAuthProtectedResourceMetadata(serverUrl, { resourceMetadataUrl: opts?.resourceMetadataUrl }, opts?.fetchFn);
    if (resourceMetadata.authorization_servers && resourceMetadata.authorization_servers.length > 0) authorizationServerUrl = resourceMetadata.authorization_servers[0];
  } catch (error2) {
    if (error2 instanceof TypeError) throw error2;
  }
  if (!authorizationServerUrl) authorizationServerUrl = String(new URL("/", serverUrl));
  const authorizationServerMetadata = await discoverAuthorizationServerMetadata(authorizationServerUrl, { fetchFn: opts?.fetchFn });
  return {
    authorizationServerUrl,
    authorizationServerMetadata,
    resourceMetadata
  };
}
async function startAuthorization(authorizationServerUrl, { metadata, clientInformation, redirectUrl, scope, state, resource }) {
  let authorizationUrl;
  if (metadata) {
    authorizationUrl = new URL(metadata.authorization_endpoint);
    if (!metadata.response_types_supported.includes(AUTHORIZATION_CODE_RESPONSE_TYPE)) throw new Error(`Incompatible auth server: does not support response type ${AUTHORIZATION_CODE_RESPONSE_TYPE}`);
    if (metadata.code_challenge_methods_supported && !metadata.code_challenge_methods_supported.includes(AUTHORIZATION_CODE_CHALLENGE_METHOD)) throw new Error(`Incompatible auth server: does not support code challenge method ${AUTHORIZATION_CODE_CHALLENGE_METHOD}`);
  } else authorizationUrl = new URL("/authorize", authorizationServerUrl);
  const challenge = await pkceChallenge();
  const codeVerifier = challenge.code_verifier;
  const codeChallenge = challenge.code_challenge;
  authorizationUrl.searchParams.set("response_type", AUTHORIZATION_CODE_RESPONSE_TYPE);
  authorizationUrl.searchParams.set("client_id", clientInformation.client_id);
  authorizationUrl.searchParams.set("code_challenge", codeChallenge);
  authorizationUrl.searchParams.set("code_challenge_method", AUTHORIZATION_CODE_CHALLENGE_METHOD);
  authorizationUrl.searchParams.set("redirect_uri", String(redirectUrl));
  if (state) authorizationUrl.searchParams.set("state", state);
  if (scope) authorizationUrl.searchParams.set("scope", scope);
  if (scope?.split(" ").includes("offline_access")) authorizationUrl.searchParams.append("prompt", "consent");
  if (resource) authorizationUrl.searchParams.set("resource", resource.href);
  return {
    authorizationUrl,
    codeVerifier
  };
}
function prepareAuthorizationCodeRequest(authorizationCode, codeVerifier, redirectUri) {
  return new URLSearchParams({
    grant_type: "authorization_code",
    code: authorizationCode,
    code_verifier: codeVerifier,
    redirect_uri: String(redirectUri)
  });
}
async function executeTokenRequest(authorizationServerUrl, { metadata, tokenRequestParams, clientInformation, addClientAuthentication, resource, fetchFn }) {
  const tokenUrl = metadata?.token_endpoint ? new URL(metadata.token_endpoint) : new URL("/token", authorizationServerUrl);
  const headers = new Headers({
    "Content-Type": "application/x-www-form-urlencoded",
    Accept: "application/json"
  });
  if (resource) tokenRequestParams.set("resource", resource.href);
  if (addClientAuthentication) await addClientAuthentication(headers, tokenRequestParams, tokenUrl, metadata);
  else if (clientInformation) applyClientAuthentication(selectClientAuthMethod(clientInformation, metadata?.token_endpoint_auth_methods_supported ?? []), clientInformation, headers, tokenRequestParams);
  const response = await (fetchFn ?? fetch)(tokenUrl, {
    method: "POST",
    headers,
    body: tokenRequestParams
  });
  if (!response.ok) throw await parseErrorResponse(response);
  const json2 = await response.json();
  try {
    return OAuthTokensSchema.parse(json2);
  } catch (parseError) {
    if (typeof json2 === "object" && json2 !== null && "error" in json2) throw await parseErrorResponse(JSON.stringify(json2));
    throw parseError;
  }
}
async function refreshAuthorization(authorizationServerUrl, { metadata, clientInformation, refreshToken, resource, addClientAuthentication, fetchFn }) {
  return {
    refresh_token: refreshToken,
    ...await executeTokenRequest(authorizationServerUrl, {
      metadata,
      tokenRequestParams: new URLSearchParams({
        grant_type: "refresh_token",
        refresh_token: refreshToken
      }),
      clientInformation,
      addClientAuthentication,
      resource,
      fetchFn
    })
  };
}
async function fetchToken(provider, authorizationServerUrl, { metadata, resource, authorizationCode, scope, fetchFn } = {}) {
  const effectiveScope = scope ?? provider.clientMetadata.scope;
  let tokenRequestParams;
  if (provider.prepareTokenRequest) tokenRequestParams = await provider.prepareTokenRequest(effectiveScope);
  if (!tokenRequestParams) {
    if (!authorizationCode) throw new Error("Either provider.prepareTokenRequest() or authorizationCode is required");
    if (!provider.redirectUrl) throw new Error("redirectUrl is required for authorization_code flow");
    tokenRequestParams = prepareAuthorizationCodeRequest(authorizationCode, await provider.codeVerifier(), provider.redirectUrl);
  }
  const clientInformation = await provider.clientInformation();
  return executeTokenRequest(authorizationServerUrl, {
    metadata,
    tokenRequestParams,
    clientInformation: clientInformation ?? void 0,
    addClientAuthentication: provider.addClientAuthentication,
    resource,
    fetchFn
  });
}
async function registerClient(authorizationServerUrl, { metadata, clientMetadata, scope, fetchFn }) {
  let registrationUrl;
  if (metadata) {
    if (!metadata.registration_endpoint) throw new Error("Incompatible auth server: does not support dynamic client registration");
    registrationUrl = new URL(metadata.registration_endpoint);
  } else registrationUrl = new URL("/register", authorizationServerUrl);
  const response = await (fetchFn ?? fetch)(registrationUrl, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      ...clientMetadata,
      ...scope === void 0 ? {} : { scope }
    })
  });
  if (!response.ok) throw await parseErrorResponse(response);
  return OAuthClientInformationFullSchema.parse(await response.json());
}
var ExperimentalClientTasks = class {
  constructor(_client) {
    this._client = _client;
  }
  get _module() {
    return this._client.taskManager;
  }
  /**
  * Calls a tool and returns an AsyncGenerator that yields response messages.
  * The generator is guaranteed to end with either a `'result'` or `'error'` message.
  *
  * This method provides streaming access to tool execution, allowing you to
  * observe intermediate task status updates for long-running tool calls.
  * Automatically validates structured output if the tool has an `outputSchema`.
  *
  * @example
  * ```ts source="./client.examples.ts#ExperimentalClientTasks_callToolStream"
  * const stream = client.experimental.tasks.callToolStream({ name: 'myTool', arguments: {} });
  * for await (const message of stream) {
  *     switch (message.type) {
  *         case 'taskCreated': {
  *             console.log('Tool execution started:', message.task.taskId);
  *             break;
  *         }
  *         case 'taskStatus': {
  *             console.log('Tool status:', message.task.status);
  *             break;
  *         }
  *         case 'result': {
  *             console.log('Tool result:', message.result);
  *             break;
  *         }
  *         case 'error': {
  *             console.error('Tool error:', message.error);
  *             break;
  *         }
  *     }
  * }
  * ```
  *
  * @param params - Tool call parameters (name and arguments)
  * @param options - Optional request options (timeout, signal, task creation params, etc.)
  * @returns AsyncGenerator that yields {@linkcode ResponseMessage} objects
  *
  * @experimental
  */
  async *callToolStream(params, options) {
    const clientInternal = this._client;
    const optionsWithTask = {
      ...options,
      task: options?.task ?? (clientInternal.isToolTask(params.name) ? {} : void 0)
    };
    const stream = this._module.requestStream({
      method: "tools/call",
      params
    }, CallToolResultSchema, optionsWithTask);
    const validator = clientInternal.getToolOutputValidator(params.name);
    for await (const message of stream) {
      if (message.type === "result" && validator && "content" in message.result) {
        const result = message.result;
        if (!result.structuredContent && !result.isError) {
          yield {
            type: "error",
            error: new ProtocolError(ProtocolErrorCode.InvalidRequest, `Tool ${params.name} has an output schema but did not return structured content`)
          };
          return;
        }
        if (result.structuredContent) try {
          const validationResult = validator(result.structuredContent);
          if (!validationResult.valid) {
            yield {
              type: "error",
              error: new ProtocolError(ProtocolErrorCode.InvalidParams, `Structured content does not match the tool's output schema: ${validationResult.errorMessage}`)
            };
            return;
          }
        } catch (error2) {
          if (error2 instanceof ProtocolError) {
            yield {
              type: "error",
              error: error2
            };
            return;
          }
          yield {
            type: "error",
            error: new ProtocolError(ProtocolErrorCode.InvalidParams, `Failed to validate structured content: ${error2 instanceof Error ? error2.message : String(error2)}`)
          };
          return;
        }
      }
      yield message;
    }
  }
  /**
  * Gets the current status of a task.
  *
  * @param taskId - The task identifier
  * @param options - Optional request options
  * @returns The task status
  *
  * @experimental
  */
  async getTask(taskId, options) {
    return this._module.getTask({ taskId }, options);
  }
  /**
  * Retrieves the result of a completed task.
  *
  * @param taskId - The task identifier
  * @param options - Optional request options
  * @returns The task result. The payload structure matches the result type of the
  *   original request (e.g., a `tools/call` task returns a `CallToolResult`).
  *
  * @experimental
  */
  async getTaskResult(taskId, options) {
    return this._module.getTaskResult({ taskId }, GetTaskPayloadResultSchema, options);
  }
  /**
  * Lists tasks with optional pagination.
  *
  * @param cursor - Optional pagination cursor
  * @param options - Optional request options
  * @returns List of tasks with optional next cursor
  *
  * @experimental
  */
  async listTasks(cursor, options) {
    return this._module.listTasks(cursor ? { cursor } : void 0, options);
  }
  /**
  * Cancels a running task.
  *
  * @param taskId - The task identifier
  * @param options - Optional request options
  *
  * @experimental
  */
  async cancelTask(taskId, options) {
    return this._module.cancelTask({ taskId }, options);
  }
  /**
  * Sends a request and returns an AsyncGenerator that yields response messages.
  * The generator is guaranteed to end with either a `'result'` or `'error'` message.
  *
  * This method provides streaming access to request processing, allowing you to
  * observe intermediate task status updates for task-augmented requests.
  *
  * @example
  * ```ts source="./client.examples.ts#ExperimentalClientTasks_requestStream"
  * const stream = client.experimental.tasks.requestStream({ method: 'tools/call', params: { name: 'my-tool', arguments: {} } }, options);
  * for await (const message of stream) {
  *     switch (message.type) {
  *         case 'taskCreated': {
  *             console.log('Task created:', message.task.taskId);
  *             break;
  *         }
  *         case 'taskStatus': {
  *             console.log('Task status:', message.task.status);
  *             break;
  *         }
  *         case 'result': {
  *             console.log('Final result:', message.result);
  *             break;
  *         }
  *         case 'error': {
  *             console.error('Error:', message.error);
  *             break;
  *         }
  *     }
  * }
  * ```
  *
  * @param request - The request to send
  * @param options - Optional request options (timeout, signal, task creation params, etc.)
  * @returns AsyncGenerator that yields {@linkcode ResponseMessage} objects
  *
  * @experimental
  */
  requestStream(request, options) {
    const resultSchema = getResultSchema(request.method);
    return this._module.requestStream(request, resultSchema, options);
  }
};
function applyElicitationDefaults(schema, data) {
  if (!schema || data === null || typeof data !== "object") return;
  if (schema.type === "object" && schema.properties && typeof schema.properties === "object") {
    const obj = data;
    const props = schema.properties;
    for (const key of Object.keys(props)) {
      const propSchema = props[key];
      if (obj[key] === void 0 && Object.prototype.hasOwnProperty.call(propSchema, "default")) obj[key] = propSchema.default;
      if (obj[key] !== void 0) applyElicitationDefaults(propSchema, obj[key]);
    }
  }
  if (Array.isArray(schema.anyOf)) {
    for (const sub of schema.anyOf) if (typeof sub !== "boolean") applyElicitationDefaults(sub, data);
  }
  if (Array.isArray(schema.oneOf)) {
    for (const sub of schema.oneOf) if (typeof sub !== "boolean") applyElicitationDefaults(sub, data);
  }
}
function getSupportedElicitationModes(capabilities) {
  if (!capabilities) return {
    supportsFormMode: false,
    supportsUrlMode: false
  };
  const hasFormCapability = capabilities.form !== void 0;
  const hasUrlCapability = capabilities.url !== void 0;
  return {
    supportsFormMode: hasFormCapability || !hasFormCapability && !hasUrlCapability,
    supportsUrlMode: hasUrlCapability
  };
}
var Client = class extends Protocol {
  _serverCapabilities;
  _serverVersion;
  _negotiatedProtocolVersion;
  _capabilities;
  _instructions;
  _jsonSchemaValidator;
  _cachedToolOutputValidators = /* @__PURE__ */ new Map();
  _cachedKnownTaskTools = /* @__PURE__ */ new Set();
  _cachedRequiredTaskTools = /* @__PURE__ */ new Set();
  _experimental;
  _listChangedDebounceTimers = /* @__PURE__ */ new Map();
  _pendingListChangedConfig;
  _enforceStrictCapabilities;
  /**
  * Initializes this client with the given name and version information.
  */
  constructor(_clientInfo, options) {
    super({
      ...options,
      tasks: extractTaskManagerOptions(options?.capabilities?.tasks)
    });
    this._clientInfo = _clientInfo;
    this._capabilities = options?.capabilities ? { ...options.capabilities } : {};
    this._jsonSchemaValidator = options?.jsonSchemaValidator ?? new AjvJsonSchemaValidator();
    this._enforceStrictCapabilities = options?.enforceStrictCapabilities ?? false;
    if (options?.capabilities?.tasks) {
      const { taskStore, taskMessageQueue, defaultTaskPollInterval, maxTaskQueueSize, ...wireCapabilities } = options.capabilities.tasks;
      this._capabilities.tasks = wireCapabilities;
    }
    if (options?.listChanged) this._pendingListChangedConfig = options.listChanged;
  }
  buildContext(ctx, _transportInfo) {
    return ctx;
  }
  /**
  * Set up handlers for list changed notifications based on config and server capabilities.
  * This should only be called after initialization when server capabilities are known.
  * Handlers are silently skipped if the server doesn't advertise the corresponding listChanged capability.
  * @internal
  */
  _setupListChangedHandlers(config2) {
    if (config2.tools && this._serverCapabilities?.tools?.listChanged) this._setupListChangedHandler("tools", "notifications/tools/list_changed", config2.tools, async () => {
      return (await this.listTools()).tools;
    });
    if (config2.prompts && this._serverCapabilities?.prompts?.listChanged) this._setupListChangedHandler("prompts", "notifications/prompts/list_changed", config2.prompts, async () => {
      return (await this.listPrompts()).prompts;
    });
    if (config2.resources && this._serverCapabilities?.resources?.listChanged) this._setupListChangedHandler("resources", "notifications/resources/list_changed", config2.resources, async () => {
      return (await this.listResources()).resources;
    });
  }
  /**
  * Access experimental features.
  *
  * WARNING: These APIs are experimental and may change without notice.
  *
  * @experimental
  */
  get experimental() {
    if (!this._experimental) this._experimental = { tasks: new ExperimentalClientTasks(this) };
    return this._experimental;
  }
  /**
  * Registers new capabilities. This can only be called before connecting to a transport.
  *
  * The new capabilities will be merged with any existing capabilities previously given (e.g., at initialization).
  */
  registerCapabilities(capabilities) {
    if (this.transport) throw new Error("Cannot register capabilities after connecting to transport");
    this._capabilities = mergeCapabilities(this._capabilities, capabilities);
  }
  /**
  * Registers a handler for server-initiated requests (sampling, elicitation, roots).
  * The client must declare the corresponding capability for the handler to be accepted.
  * Replaces any previously registered handler for the same method.
  *
  * For `sampling/createMessage` and `elicitation/create`, the handler is automatically
  * wrapped with schema validation for both the incoming request and the returned result.
  *
  * @example Handling a sampling request
  * ```ts source="./client.examples.ts#Client_setRequestHandler_sampling"
  * client.setRequestHandler('sampling/createMessage', async request => {
  *     const lastMessage = request.params.messages.at(-1);
  *     console.log('Sampling request:', lastMessage);
  *
  *     // In production, send messages to your LLM here
  *     return {
  *         model: 'my-model',
  *         role: 'assistant' as const,
  *         content: {
  *             type: 'text' as const,
  *             text: 'Response from the model'
  *         }
  *     };
  * });
  * ```
  */
  setRequestHandler(method, handler) {
    if (method === "elicitation/create") {
      const wrappedHandler = async (request, ctx) => {
        const validatedRequest = parseSchema(ElicitRequestSchema, request);
        if (!validatedRequest.success) {
          const errorMessage = validatedRequest.error instanceof Error ? validatedRequest.error.message : String(validatedRequest.error);
          throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Invalid elicitation request: ${errorMessage}`);
        }
        const { params } = validatedRequest.data;
        params.mode = params.mode ?? "form";
        const { supportsFormMode, supportsUrlMode } = getSupportedElicitationModes(this._capabilities.elicitation);
        if (params.mode === "form" && !supportsFormMode) throw new ProtocolError(ProtocolErrorCode.InvalidParams, "Client does not support form-mode elicitation requests");
        if (params.mode === "url" && !supportsUrlMode) throw new ProtocolError(ProtocolErrorCode.InvalidParams, "Client does not support URL-mode elicitation requests");
        const result = await Promise.resolve(handler(request, ctx));
        if (params.task) {
          const taskValidationResult = parseSchema(CreateTaskResultSchema, result);
          if (!taskValidationResult.success) {
            const errorMessage = taskValidationResult.error instanceof Error ? taskValidationResult.error.message : String(taskValidationResult.error);
            throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Invalid task creation result: ${errorMessage}`);
          }
          return taskValidationResult.data;
        }
        const validationResult = parseSchema(ElicitResultSchema, result);
        if (!validationResult.success) {
          const errorMessage = validationResult.error instanceof Error ? validationResult.error.message : String(validationResult.error);
          throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Invalid elicitation result: ${errorMessage}`);
        }
        const validatedResult = validationResult.data;
        const requestedSchema = params.mode === "form" ? params.requestedSchema : void 0;
        if (params.mode === "form" && validatedResult.action === "accept" && validatedResult.content && requestedSchema && this._capabilities.elicitation?.form?.applyDefaults) try {
          applyElicitationDefaults(requestedSchema, validatedResult.content);
        } catch {
        }
        return validatedResult;
      };
      return super.setRequestHandler(method, wrappedHandler);
    }
    if (method === "sampling/createMessage") {
      const wrappedHandler = async (request, ctx) => {
        const validatedRequest = parseSchema(CreateMessageRequestSchema, request);
        if (!validatedRequest.success) {
          const errorMessage = validatedRequest.error instanceof Error ? validatedRequest.error.message : String(validatedRequest.error);
          throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Invalid sampling request: ${errorMessage}`);
        }
        const { params } = validatedRequest.data;
        const result = await Promise.resolve(handler(request, ctx));
        if (params.task) {
          const taskValidationResult = parseSchema(CreateTaskResultSchema, result);
          if (!taskValidationResult.success) {
            const errorMessage = taskValidationResult.error instanceof Error ? taskValidationResult.error.message : String(taskValidationResult.error);
            throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Invalid task creation result: ${errorMessage}`);
          }
          return taskValidationResult.data;
        }
        const validationResult = parseSchema(params.tools || params.toolChoice ? CreateMessageResultWithToolsSchema : CreateMessageResultSchema, result);
        if (!validationResult.success) {
          const errorMessage = validationResult.error instanceof Error ? validationResult.error.message : String(validationResult.error);
          throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Invalid sampling result: ${errorMessage}`);
        }
        return validationResult.data;
      };
      return super.setRequestHandler(method, wrappedHandler);
    }
    return super.setRequestHandler(method, handler);
  }
  assertCapability(capability, method) {
    if (!this._serverCapabilities?.[capability]) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `Server does not support ${capability} (required for ${method})`);
  }
  /**
  * Connects to a server via the given transport and performs the MCP initialization handshake.
  *
  * @example Basic usage (stdio)
  * ```ts source="./client.examples.ts#Client_connect_stdio"
  * const client = new Client({ name: 'my-client', version: '1.0.0' });
  * const transport = new StdioClientTransport({ command: 'my-mcp-server' });
  * await client.connect(transport);
  * ```
  *
  * @example Streamable HTTP with SSE fallback
  * ```ts source="./client.examples.ts#Client_connect_sseFallback"
  * const baseUrl = new URL(url);
  *
  * try {
  *     // Try modern Streamable HTTP transport first
  *     const client = new Client({ name: 'my-client', version: '1.0.0' });
  *     const transport = new StreamableHTTPClientTransport(baseUrl);
  *     await client.connect(transport);
  *     return { client, transport };
  * } catch {
  *     // Fall back to legacy SSE transport
  *     const client = new Client({ name: 'my-client', version: '1.0.0' });
  *     const transport = new SSEClientTransport(baseUrl);
  *     await client.connect(transport);
  *     return { client, transport };
  * }
  * ```
  */
  async connect(transport, options) {
    await super.connect(transport);
    if (transport.sessionId !== void 0) {
      if (this._negotiatedProtocolVersion !== void 0 && transport.setProtocolVersion) transport.setProtocolVersion(this._negotiatedProtocolVersion);
      return;
    }
    try {
      const result = await this._requestWithSchema({
        method: "initialize",
        params: {
          protocolVersion: this._supportedProtocolVersions[0] ?? LATEST_PROTOCOL_VERSION,
          capabilities: this._capabilities,
          clientInfo: this._clientInfo
        }
      }, InitializeResultSchema, options);
      if (result === void 0) throw new Error(`Server sent invalid initialize result: ${result}`);
      if (!this._supportedProtocolVersions.includes(result.protocolVersion)) throw new Error(`Server's protocol version is not supported: ${result.protocolVersion}`);
      this._serverCapabilities = result.capabilities;
      this._serverVersion = result.serverInfo;
      this._negotiatedProtocolVersion = result.protocolVersion;
      if (transport.setProtocolVersion) transport.setProtocolVersion(result.protocolVersion);
      this._instructions = result.instructions;
      await this.notification({ method: "notifications/initialized" });
      if (this._pendingListChangedConfig) {
        this._setupListChangedHandlers(this._pendingListChangedConfig);
        this._pendingListChangedConfig = void 0;
      }
    } catch (error2) {
      this.close();
      throw error2;
    }
  }
  /**
  * After initialization has completed, this will be populated with the server's reported capabilities.
  */
  getServerCapabilities() {
    return this._serverCapabilities;
  }
  /**
  * After initialization has completed, this will be populated with information about the server's name and version.
  */
  getServerVersion() {
    return this._serverVersion;
  }
  /**
  * After initialization has completed, this will be populated with the protocol version negotiated
  * during the initialize handshake. When manually reconstructing a transport for reconnection, pass this
  * value to the new transport so it continues sending the required `mcp-protocol-version` header.
  */
  getNegotiatedProtocolVersion() {
    return this._negotiatedProtocolVersion;
  }
  /**
  * After initialization has completed, this may be populated with information about the server's instructions.
  */
  getInstructions() {
    return this._instructions;
  }
  assertCapabilityForMethod(method) {
    switch (method) {
      case "logging/setLevel":
        if (!this._serverCapabilities?.logging) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `Server does not support logging (required for ${method})`);
        break;
      case "prompts/get":
      case "prompts/list":
        if (!this._serverCapabilities?.prompts) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `Server does not support prompts (required for ${method})`);
        break;
      case "resources/list":
      case "resources/templates/list":
      case "resources/read":
      case "resources/subscribe":
      case "resources/unsubscribe":
        if (!this._serverCapabilities?.resources) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `Server does not support resources (required for ${method})`);
        if (method === "resources/subscribe" && !this._serverCapabilities.resources.subscribe) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `Server does not support resource subscriptions (required for ${method})`);
        break;
      case "tools/call":
      case "tools/list":
        if (!this._serverCapabilities?.tools) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `Server does not support tools (required for ${method})`);
        break;
      case "completion/complete":
        if (!this._serverCapabilities?.completions) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `Server does not support completions (required for ${method})`);
        break;
      case "initialize":
        break;
      case "ping":
        break;
    }
  }
  assertNotificationCapability(method) {
    switch (method) {
      case "notifications/roots/list_changed":
        if (!this._capabilities.roots?.listChanged) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `Client does not support roots list changed notifications (required for ${method})`);
        break;
      case "notifications/initialized":
        break;
      case "notifications/cancelled":
        break;
      case "notifications/progress":
        break;
    }
  }
  assertRequestHandlerCapability(method) {
    switch (method) {
      case "sampling/createMessage":
        if (!this._capabilities.sampling) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `Client does not support sampling capability (required for ${method})`);
        break;
      case "elicitation/create":
        if (!this._capabilities.elicitation) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `Client does not support elicitation capability (required for ${method})`);
        break;
      case "roots/list":
        if (!this._capabilities.roots) throw new SdkError(SdkErrorCode.CapabilityNotSupported, `Client does not support roots capability (required for ${method})`);
        break;
      case "ping":
        break;
    }
  }
  assertTaskCapability(method) {
    assertToolsCallTaskCapability(this._serverCapabilities?.tasks?.requests, method, "Server");
  }
  assertTaskHandlerCapability(method) {
    assertClientRequestTaskCapability(this._capabilities?.tasks?.requests, method, "Client");
  }
  async ping(options) {
    return this._requestWithSchema({ method: "ping" }, EmptyResultSchema, options);
  }
  /** Requests argument autocompletion suggestions from the server for a prompt or resource. */
  async complete(params, options) {
    return this._requestWithSchema({
      method: "completion/complete",
      params
    }, CompleteResultSchema, options);
  }
  /** Sets the minimum severity level for log messages sent by the server. */
  async setLoggingLevel(level, options) {
    return this._requestWithSchema({
      method: "logging/setLevel",
      params: { level }
    }, EmptyResultSchema, options);
  }
  /** Retrieves a prompt by name from the server, passing the given arguments for template substitution. */
  async getPrompt(params, options) {
    return this._requestWithSchema({
      method: "prompts/get",
      params
    }, GetPromptResultSchema, options);
  }
  /**
  * Lists available prompts. Results may be paginated — loop on `nextCursor` to collect all pages.
  *
  * Returns an empty list if the server does not advertise prompts capability
  * (or throws if {@linkcode ClientOptions.enforceStrictCapabilities} is enabled).
  *
  * @example
  * ```ts source="./client.examples.ts#Client_listPrompts_pagination"
  * const allPrompts: Prompt[] = [];
  * let cursor: string | undefined;
  * do {
  *     const { prompts, nextCursor } = await client.listPrompts({ cursor });
  *     allPrompts.push(...prompts);
  *     cursor = nextCursor;
  * } while (cursor);
  * console.log(
  *     'Available prompts:',
  *     allPrompts.map(p => p.name)
  * );
  * ```
  */
  async listPrompts(params, options) {
    if (!this._serverCapabilities?.prompts && !this._enforceStrictCapabilities) {
      console.debug("Client.listPrompts() called but server does not advertise prompts capability - returning empty list");
      return { prompts: [] };
    }
    return this._requestWithSchema({
      method: "prompts/list",
      params
    }, ListPromptsResultSchema, options);
  }
  /**
  * Lists available resources. Results may be paginated — loop on `nextCursor` to collect all pages.
  *
  * Returns an empty list if the server does not advertise resources capability
  * (or throws if {@linkcode ClientOptions.enforceStrictCapabilities} is enabled).
  *
  * @example
  * ```ts source="./client.examples.ts#Client_listResources_pagination"
  * const allResources: Resource[] = [];
  * let cursor: string | undefined;
  * do {
  *     const { resources, nextCursor } = await client.listResources({ cursor });
  *     allResources.push(...resources);
  *     cursor = nextCursor;
  * } while (cursor);
  * console.log(
  *     'Available resources:',
  *     allResources.map(r => r.name)
  * );
  * ```
  */
  async listResources(params, options) {
    if (!this._serverCapabilities?.resources && !this._enforceStrictCapabilities) {
      console.debug("Client.listResources() called but server does not advertise resources capability - returning empty list");
      return { resources: [] };
    }
    return this._requestWithSchema({
      method: "resources/list",
      params
    }, ListResourcesResultSchema, options);
  }
  /**
  * Lists available resource URI templates for dynamic resources. Results may be paginated — see {@linkcode listResources | listResources()} for the cursor pattern.
  *
  * Returns an empty list if the server does not advertise resources capability
  * (or throws if {@linkcode ClientOptions.enforceStrictCapabilities} is enabled).
  */
  async listResourceTemplates(params, options) {
    if (!this._serverCapabilities?.resources && !this._enforceStrictCapabilities) {
      console.debug("Client.listResourceTemplates() called but server does not advertise resources capability - returning empty list");
      return { resourceTemplates: [] };
    }
    return this._requestWithSchema({
      method: "resources/templates/list",
      params
    }, ListResourceTemplatesResultSchema, options);
  }
  /** Reads the contents of a resource by URI. */
  async readResource(params, options) {
    return this._requestWithSchema({
      method: "resources/read",
      params
    }, ReadResourceResultSchema, options);
  }
  /** Subscribes to change notifications for a resource. The server must support resource subscriptions. */
  async subscribeResource(params, options) {
    return this._requestWithSchema({
      method: "resources/subscribe",
      params
    }, EmptyResultSchema, options);
  }
  /** Unsubscribes from change notifications for a resource. */
  async unsubscribeResource(params, options) {
    return this._requestWithSchema({
      method: "resources/unsubscribe",
      params
    }, EmptyResultSchema, options);
  }
  /**
  * Calls a tool on the connected server and returns the result. Automatically validates structured output
  * if the tool has an `outputSchema`.
  *
  * Tool results have two error surfaces: `result.isError` for tool-level failures (the tool ran but reported
  * a problem), and thrown {@linkcode ProtocolError} for protocol-level failures or {@linkcode SdkError} for
  * SDK-level issues (timeouts, missing capabilities).
  *
  * For task-based execution with streaming behavior, use {@linkcode ExperimentalClientTasks.callToolStream | client.experimental.tasks.callToolStream()} instead.
  *
  * @example Basic usage
  * ```ts source="./client.examples.ts#Client_callTool_basic"
  * const result = await client.callTool({
  *     name: 'calculate-bmi',
  *     arguments: { weightKg: 70, heightM: 1.75 }
  * });
  *
  * // Tool-level errors are returned in the result, not thrown
  * if (result.isError) {
  *     console.error('Tool error:', result.content);
  *     return;
  * }
  *
  * console.log(result.content);
  * ```
  *
  * @example Structured output
  * ```ts source="./client.examples.ts#Client_callTool_structuredOutput"
  * const result = await client.callTool({
  *     name: 'calculate-bmi',
  *     arguments: { weightKg: 70, heightM: 1.75 }
  * });
  *
  * // Machine-readable output for the client application
  * if (result.structuredContent) {
  *     console.log(result.structuredContent); // e.g. { bmi: 22.86 }
  * }
  * ```
  */
  async callTool(params, options) {
    if (this.isToolTaskRequired(params.name)) throw new ProtocolError(ProtocolErrorCode.InvalidRequest, `Tool "${params.name}" requires task-based execution. Use client.experimental.tasks.callToolStream() instead.`);
    const result = await this._requestWithSchema({
      method: "tools/call",
      params
    }, CallToolResultSchema, options);
    const validator = this.getToolOutputValidator(params.name);
    if (validator) {
      if (!result.structuredContent && !result.isError) throw new ProtocolError(ProtocolErrorCode.InvalidRequest, `Tool ${params.name} has an output schema but did not return structured content`);
      if (result.structuredContent) try {
        const validationResult = validator(result.structuredContent);
        if (!validationResult.valid) throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Structured content does not match the tool's output schema: ${validationResult.errorMessage}`);
      } catch (error2) {
        if (error2 instanceof ProtocolError) throw error2;
        throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Failed to validate structured content: ${error2 instanceof Error ? error2.message : String(error2)}`);
      }
    }
    return result;
  }
  isToolTask(toolName) {
    if (!this._serverCapabilities?.tasks?.requests?.tools?.call) return false;
    return this._cachedKnownTaskTools.has(toolName);
  }
  /**
  * Check if a tool requires task-based execution.
  * Unlike {@linkcode isToolTask} which includes `'optional'` tools, this only checks for `'required'`.
  */
  isToolTaskRequired(toolName) {
    return this._cachedRequiredTaskTools.has(toolName);
  }
  /**
  * Cache validators for tool output schemas.
  * Called after {@linkcode listTools | listTools()} to pre-compile validators for better performance.
  */
  cacheToolMetadata(tools) {
    this._cachedToolOutputValidators.clear();
    this._cachedKnownTaskTools.clear();
    this._cachedRequiredTaskTools.clear();
    for (const tool of tools) {
      if (tool.outputSchema) {
        const toolValidator = this._jsonSchemaValidator.getValidator(tool.outputSchema);
        this._cachedToolOutputValidators.set(tool.name, toolValidator);
      }
      const taskSupport = tool.execution?.taskSupport;
      if (taskSupport === "required" || taskSupport === "optional") this._cachedKnownTaskTools.add(tool.name);
      if (taskSupport === "required") this._cachedRequiredTaskTools.add(tool.name);
    }
  }
  /**
  * Get cached validator for a tool
  */
  getToolOutputValidator(toolName) {
    return this._cachedToolOutputValidators.get(toolName);
  }
  /**
  * Lists available tools. Results may be paginated — loop on `nextCursor` to collect all pages.
  *
  * Returns an empty list if the server does not advertise tools capability
  * (or throws if {@linkcode ClientOptions.enforceStrictCapabilities} is enabled).
  *
  * @example
  * ```ts source="./client.examples.ts#Client_listTools_pagination"
  * const allTools: Tool[] = [];
  * let cursor: string | undefined;
  * do {
  *     const { tools, nextCursor } = await client.listTools({ cursor });
  *     allTools.push(...tools);
  *     cursor = nextCursor;
  * } while (cursor);
  * console.log(
  *     'Available tools:',
  *     allTools.map(t => t.name)
  * );
  * ```
  */
  async listTools(params, options) {
    if (!this._serverCapabilities?.tools && !this._enforceStrictCapabilities) {
      console.debug("Client.listTools() called but server does not advertise tools capability - returning empty list");
      return { tools: [] };
    }
    const result = await this._requestWithSchema({
      method: "tools/list",
      params
    }, ListToolsResultSchema, options);
    this.cacheToolMetadata(result.tools);
    return result;
  }
  /**
  * Set up a single list changed handler.
  * @internal
  */
  _setupListChangedHandler(listType, notificationMethod, options, fetcher) {
    const parseResult = parseSchema(ListChangedOptionsBaseSchema, options);
    if (!parseResult.success) throw new Error(`Invalid ${listType} listChanged options: ${parseResult.error.message}`);
    if (typeof options.onChanged !== "function") throw new TypeError(`Invalid ${listType} listChanged options: onChanged must be a function`);
    const { autoRefresh, debounceMs } = parseResult.data;
    const { onChanged } = options;
    const refresh = async () => {
      if (!autoRefresh) {
        onChanged(null, null);
        return;
      }
      try {
        onChanged(null, await fetcher());
      } catch (error2) {
        onChanged(error2 instanceof Error ? error2 : new Error(String(error2)), null);
      }
    };
    const handler = () => {
      if (debounceMs) {
        const existingTimer = this._listChangedDebounceTimers.get(listType);
        if (existingTimer) clearTimeout(existingTimer);
        const timer = setTimeout(refresh, debounceMs);
        this._listChangedDebounceTimers.set(listType, timer);
      } else refresh();
    };
    this.setNotificationHandler(notificationMethod, handler);
  }
  /** Notifies the server that the client's root list has changed. Requires the `roots.listChanged` capability. */
  async sendRootsListChanged() {
    return this.notification({ method: "notifications/roots/list_changed" });
  }
};
var DEFAULT_INHERITED_ENV_VARS = import_node_process.default.platform === "win32" ? [
  "APPDATA",
  "HOMEDRIVE",
  "HOMEPATH",
  "LOCALAPPDATA",
  "PATH",
  "PROCESSOR_ARCHITECTURE",
  "SYSTEMDRIVE",
  "SYSTEMROOT",
  "TEMP",
  "USERNAME",
  "USERPROFILE",
  "PROGRAMFILES"
] : [
  "HOME",
  "LOGNAME",
  "PATH",
  "SHELL",
  "TERM",
  "USER"
];
function getDefaultEnvironment() {
  const env = {};
  for (const key of DEFAULT_INHERITED_ENV_VARS) {
    const value = import_node_process.default.env[key];
    if (value === void 0) continue;
    if (value.startsWith("()")) continue;
    env[key] = value;
  }
  return env;
}
var StdioClientTransport = class {
  _process;
  _readBuffer = new ReadBuffer();
  _serverParams;
  _stderrStream = null;
  onclose;
  onerror;
  onmessage;
  constructor(server2) {
    this._serverParams = server2;
    if (server2.stderr === "pipe" || server2.stderr === "overlapped") this._stderrStream = new import_node_stream.PassThrough();
  }
  /**
  * Starts the server process and prepares to communicate with it.
  */
  async start() {
    if (this._process) throw new Error("StdioClientTransport already started! If using Client class, note that connect() calls start() automatically.");
    return new Promise((resolve, reject) => {
      this._process = (0, import_cross_spawn.default)(this._serverParams.command, this._serverParams.args ?? [], {
        env: {
          ...getDefaultEnvironment(),
          ...this._serverParams.env
        },
        stdio: [
          "pipe",
          "pipe",
          this._serverParams.stderr ?? "inherit"
        ],
        shell: false,
        windowsHide: import_node_process.default.platform === "win32",
        cwd: this._serverParams.cwd
      });
      this._process.on("error", (error2) => {
        reject(error2);
        this.onerror?.(error2);
      });
      this._process.on("spawn", () => {
        resolve();
      });
      this._process.on("close", (_code) => {
        this._process = void 0;
        this.onclose?.();
      });
      this._process.stdin?.on("error", (error2) => {
        this.onerror?.(error2);
      });
      this._process.stdout?.on("data", (chunk) => {
        this._readBuffer.append(chunk);
        this.processReadBuffer();
      });
      this._process.stdout?.on("error", (error2) => {
        this.onerror?.(error2);
      });
      if (this._stderrStream && this._process.stderr) this._process.stderr.pipe(this._stderrStream);
    });
  }
  /**
  * The `stderr` stream of the child process, if {@linkcode StdioServerParameters.stderr} was set to `"pipe"` or `"overlapped"`.
  *
  * If `stderr` piping was requested, a `PassThrough` stream is returned _immediately_, allowing callers to
  * attach listeners before the `start` method is invoked. This prevents loss of any early
  * error output emitted by the child process.
  */
  get stderr() {
    if (this._stderrStream) return this._stderrStream;
    return this._process?.stderr ?? null;
  }
  /**
  * The child process pid spawned by this transport.
  *
  * This is only available after the transport has been started.
  */
  get pid() {
    return this._process?.pid ?? null;
  }
  processReadBuffer() {
    while (true) try {
      const message = this._readBuffer.readMessage();
      if (message === null) break;
      this.onmessage?.(message);
    } catch (error2) {
      this.onerror?.(error2);
    }
  }
  async close() {
    if (this._process) {
      const processToClose = this._process;
      this._process = void 0;
      const closePromise = new Promise((resolve) => {
        processToClose.once("close", () => {
          resolve();
        });
      });
      try {
        processToClose.stdin?.end();
      } catch {
      }
      await Promise.race([closePromise, new Promise((resolve) => setTimeout(resolve, 2e3).unref())]);
      if (processToClose.exitCode === null) {
        try {
          processToClose.kill("SIGTERM");
        } catch {
        }
        await Promise.race([closePromise, new Promise((resolve) => setTimeout(resolve, 2e3).unref())]);
      }
      if (processToClose.exitCode === null) try {
        processToClose.kill("SIGKILL");
      } catch {
      }
    }
    this._readBuffer.clear();
  }
  send(message) {
    return new Promise((resolve) => {
      if (!this._process?.stdin) throw new SdkError(SdkErrorCode.NotConnected, "Not connected");
      const json2 = serializeMessage(message);
      if (this._process.stdin.write(json2)) resolve();
      else this._process.stdin.once("drain", resolve);
    });
  }
};
var DEFAULT_STREAMABLE_HTTP_RECONNECTION_OPTIONS = {
  initialReconnectionDelay: 1e3,
  maxReconnectionDelay: 3e4,
  reconnectionDelayGrowFactor: 1.5,
  maxRetries: 2
};
var StreamableHTTPClientTransport = class {
  _abortController;
  _url;
  _resourceMetadataUrl;
  _scope;
  _requestInit;
  _authProvider;
  _oauthProvider;
  _fetch;
  _fetchWithInit;
  _sessionId;
  _reconnectionOptions;
  _protocolVersion;
  _lastUpscopingHeader;
  _serverRetryMs;
  _reconnectionScheduler;
  _cancelReconnection;
  onclose;
  onerror;
  onmessage;
  constructor(url2, opts) {
    this._url = url2;
    this._resourceMetadataUrl = void 0;
    this._scope = void 0;
    this._requestInit = opts?.requestInit;
    if (isOAuthClientProvider(opts?.authProvider)) {
      this._oauthProvider = opts.authProvider;
      this._authProvider = adaptOAuthProvider(opts.authProvider);
    } else this._authProvider = opts?.authProvider;
    this._fetch = opts?.fetch;
    this._fetchWithInit = createFetchWithInit(opts?.fetch, opts?.requestInit);
    this._sessionId = opts?.sessionId;
    this._protocolVersion = opts?.protocolVersion;
    this._reconnectionOptions = opts?.reconnectionOptions ?? DEFAULT_STREAMABLE_HTTP_RECONNECTION_OPTIONS;
    this._reconnectionScheduler = opts?.reconnectionScheduler;
  }
  async _commonHeaders() {
    const headers = {};
    const token = await this._authProvider?.token();
    if (token) headers["Authorization"] = `Bearer ${token}`;
    if (this._sessionId) headers["mcp-session-id"] = this._sessionId;
    if (this._protocolVersion) headers["mcp-protocol-version"] = this._protocolVersion;
    const extraHeaders = normalizeHeaders(this._requestInit?.headers);
    return new Headers({
      ...headers,
      ...extraHeaders
    });
  }
  async _startOrAuthSse(options, isAuthRetry = false) {
    const { resumptionToken } = options;
    try {
      const headers = await this._commonHeaders();
      headers.set("Accept", "text/event-stream");
      if (resumptionToken) headers.set("last-event-id", resumptionToken);
      const response = await (this._fetch ?? fetch)(this._url, {
        ...this._requestInit,
        method: "GET",
        headers,
        signal: this._abortController?.signal
      });
      if (!response.ok) {
        if (response.status === 401 && this._authProvider) {
          if (response.headers.has("www-authenticate")) {
            const { resourceMetadataUrl, scope } = extractWWWAuthenticateParams(response);
            this._resourceMetadataUrl = resourceMetadataUrl;
            this._scope = scope;
          }
          if (this._authProvider.onUnauthorized && !isAuthRetry) {
            await this._authProvider.onUnauthorized({
              response,
              serverUrl: this._url,
              fetchFn: this._fetchWithInit
            });
            await response.text?.().catch(() => {
            });
            return this._startOrAuthSse(options, true);
          }
          await response.text?.().catch(() => {
          });
          if (isAuthRetry) throw new SdkError(SdkErrorCode.ClientHttpAuthentication, "Server returned 401 after re-authentication", { status: 401 });
          throw new UnauthorizedError();
        }
        await response.text?.().catch(() => {
        });
        if (response.status === 405) return;
        throw new SdkError(SdkErrorCode.ClientHttpFailedToOpenStream, `Failed to open SSE stream: ${response.statusText}`, {
          status: response.status,
          statusText: response.statusText
        });
      }
      this._handleSseStream(response.body, options, true);
    } catch (error2) {
      this.onerror?.(error2);
      throw error2;
    }
  }
  /**
  * Calculates the next reconnection delay using a backoff algorithm
  *
  * @param attempt Current reconnection attempt count for the specific stream
  * @returns Time to wait in milliseconds before next reconnection attempt
  */
  _getNextReconnectionDelay(attempt) {
    if (this._serverRetryMs !== void 0) return this._serverRetryMs;
    const initialDelay = this._reconnectionOptions.initialReconnectionDelay;
    const growFactor = this._reconnectionOptions.reconnectionDelayGrowFactor;
    const maxDelay = this._reconnectionOptions.maxReconnectionDelay;
    return Math.min(initialDelay * Math.pow(growFactor, attempt), maxDelay);
  }
  /**
  * Schedule a reconnection attempt using server-provided retry interval or backoff
  *
  * @param lastEventId The ID of the last received event for resumability
  * @param attemptCount Current reconnection attempt count for this specific stream
  */
  _scheduleReconnection(options, attemptCount = 0) {
    const maxRetries = this._reconnectionOptions.maxRetries;
    if (attemptCount >= maxRetries) {
      this.onerror?.(/* @__PURE__ */ new Error(`Maximum reconnection attempts (${maxRetries}) exceeded.`));
      return;
    }
    const delay = this._getNextReconnectionDelay(attemptCount);
    const reconnect = () => {
      this._cancelReconnection = void 0;
      if (this._abortController?.signal.aborted) return;
      this._startOrAuthSse(options).catch((error2) => {
        this.onerror?.(/* @__PURE__ */ new Error(`Failed to reconnect SSE stream: ${error2 instanceof Error ? error2.message : String(error2)}`));
        try {
          this._scheduleReconnection(options, attemptCount + 1);
        } catch (scheduleError) {
          this.onerror?.(scheduleError instanceof Error ? scheduleError : new Error(String(scheduleError)));
        }
      });
    };
    if (this._reconnectionScheduler) {
      const cancel = this._reconnectionScheduler(reconnect, delay, attemptCount);
      this._cancelReconnection = typeof cancel === "function" ? cancel : void 0;
    } else {
      const handle = setTimeout(reconnect, delay);
      this._cancelReconnection = () => clearTimeout(handle);
    }
  }
  _handleSseStream(stream, options, isReconnectable) {
    if (!stream) return;
    const { onresumptiontoken, replayMessageId } = options;
    let lastEventId;
    let hasPrimingEvent = false;
    let receivedResponse = false;
    const processStream = async () => {
      try {
        const reader = stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream({ onRetry: (retryMs) => {
          this._serverRetryMs = retryMs;
        } })).getReader();
        while (true) {
          const { value: event, done } = await reader.read();
          if (done) break;
          if (event.id) {
            lastEventId = event.id;
            hasPrimingEvent = true;
            onresumptiontoken?.(event.id);
          }
          if (!event.data) continue;
          if (!event.event || event.event === "message") try {
            const message = JSONRPCMessageSchema.parse(JSON.parse(event.data));
            if (isJSONRPCResultResponse(message) || isJSONRPCErrorResponse(message)) {
              receivedResponse = true;
              if (replayMessageId !== void 0) message.id = replayMessageId;
            }
            this.onmessage?.(message);
          } catch (error2) {
            this.onerror?.(error2);
          }
        }
        if ((isReconnectable || hasPrimingEvent) && !receivedResponse && this._abortController && !this._abortController.signal.aborted) this._scheduleReconnection({
          resumptionToken: lastEventId,
          onresumptiontoken,
          replayMessageId
        }, 0);
      } catch (error2) {
        this.onerror?.(/* @__PURE__ */ new Error(`SSE stream disconnected: ${error2}`));
        if ((isReconnectable || hasPrimingEvent) && !receivedResponse && this._abortController && !this._abortController.signal.aborted) try {
          this._scheduleReconnection({
            resumptionToken: lastEventId,
            onresumptiontoken,
            replayMessageId
          }, 0);
        } catch (error$1) {
          this.onerror?.(/* @__PURE__ */ new Error(`Failed to reconnect: ${error$1 instanceof Error ? error$1.message : String(error$1)}`));
        }
      }
    };
    processStream();
  }
  async start() {
    if (this._abortController) throw new Error("StreamableHTTPClientTransport already started! If using Client class, note that connect() calls start() automatically.");
    this._abortController = new AbortController();
  }
  /**
  * Call this method after the user has finished authorizing via their user agent and is redirected back to the MCP client application. This will exchange the authorization code for an access token, enabling the next connection attempt to successfully auth.
  */
  async finishAuth(authorizationCode) {
    if (!this._oauthProvider) throw new UnauthorizedError("finishAuth requires an OAuthClientProvider");
    if (await auth(this._oauthProvider, {
      serverUrl: this._url,
      authorizationCode,
      resourceMetadataUrl: this._resourceMetadataUrl,
      scope: this._scope,
      fetchFn: this._fetchWithInit
    }) !== "AUTHORIZED") throw new UnauthorizedError("Failed to authorize");
  }
  async close() {
    try {
      this._cancelReconnection?.();
    } finally {
      this._cancelReconnection = void 0;
      this._abortController?.abort();
      this.onclose?.();
    }
  }
  async send(message, options) {
    return this._send(message, options, false);
  }
  async _send(message, options, isAuthRetry) {
    try {
      const { resumptionToken, onresumptiontoken } = options || {};
      if (resumptionToken) {
        this._startOrAuthSse({
          resumptionToken,
          replayMessageId: isJSONRPCRequest(message) ? message.id : void 0
        }).catch((error2) => this.onerror?.(error2));
        return;
      }
      const headers = await this._commonHeaders();
      headers.set("content-type", "application/json");
      headers.set("accept", "application/json, text/event-stream");
      const init = {
        ...this._requestInit,
        method: "POST",
        headers,
        body: JSON.stringify(message),
        signal: this._abortController?.signal
      };
      const response = await (this._fetch ?? fetch)(this._url, init);
      const sessionId = response.headers.get("mcp-session-id");
      if (sessionId) this._sessionId = sessionId;
      if (!response.ok) {
        if (response.status === 401 && this._authProvider) {
          if (response.headers.has("www-authenticate")) {
            const { resourceMetadataUrl, scope } = extractWWWAuthenticateParams(response);
            this._resourceMetadataUrl = resourceMetadataUrl;
            this._scope = scope;
          }
          if (this._authProvider.onUnauthorized && !isAuthRetry) {
            await this._authProvider.onUnauthorized({
              response,
              serverUrl: this._url,
              fetchFn: this._fetchWithInit
            });
            await response.text?.().catch(() => {
            });
            return this._send(message, options, true);
          }
          await response.text?.().catch(() => {
          });
          if (isAuthRetry) throw new SdkError(SdkErrorCode.ClientHttpAuthentication, "Server returned 401 after re-authentication", { status: 401 });
          throw new UnauthorizedError();
        }
        const text = await response.text?.().catch(() => null);
        if (response.status === 403 && this._oauthProvider) {
          const { resourceMetadataUrl, scope, error: error2 } = extractWWWAuthenticateParams(response);
          if (error2 === "insufficient_scope") {
            const wwwAuthHeader = response.headers.get("WWW-Authenticate");
            if (this._lastUpscopingHeader === wwwAuthHeader) throw new SdkError(SdkErrorCode.ClientHttpForbidden, "Server returned 403 after trying upscoping", {
              status: 403,
              text
            });
            if (scope) this._scope = scope;
            if (resourceMetadataUrl) this._resourceMetadataUrl = resourceMetadataUrl;
            this._lastUpscopingHeader = wwwAuthHeader ?? void 0;
            if (await auth(this._oauthProvider, {
              serverUrl: this._url,
              resourceMetadataUrl: this._resourceMetadataUrl,
              scope: this._scope,
              fetchFn: this._fetchWithInit
            }) !== "AUTHORIZED") throw new UnauthorizedError();
            return this._send(message, options, isAuthRetry);
          }
        }
        throw new SdkError(SdkErrorCode.ClientHttpNotImplemented, `Error POSTing to endpoint: ${text}`, {
          status: response.status,
          text
        });
      }
      this._lastUpscopingHeader = void 0;
      if (response.status === 202) {
        await response.text?.().catch(() => {
        });
        if (isInitializedNotification(message)) this._startOrAuthSse({ resumptionToken: void 0 }).catch((error2) => this.onerror?.(error2));
        return;
      }
      const hasRequests = (Array.isArray(message) ? message : [message]).some((msg) => "method" in msg && "id" in msg && msg.id !== void 0);
      const contentType = response.headers.get("content-type");
      if (hasRequests) if (contentType?.includes("text/event-stream")) this._handleSseStream(response.body, { onresumptiontoken }, false);
      else if (contentType?.includes("application/json")) {
        const data = await response.json();
        const responseMessages = Array.isArray(data) ? data.map((msg) => JSONRPCMessageSchema.parse(msg)) : [JSONRPCMessageSchema.parse(data)];
        for (const msg of responseMessages) this.onmessage?.(msg);
      } else {
        await response.text?.().catch(() => {
        });
        throw new SdkError(SdkErrorCode.ClientHttpUnexpectedContent, `Unexpected content type: ${contentType}`, { contentType });
      }
      else await response.text?.().catch(() => {
      });
    } catch (error2) {
      this.onerror?.(error2);
      throw error2;
    }
  }
  get sessionId() {
    return this._sessionId;
  }
  /**
  * Terminates the current session by sending a `DELETE` request to the server.
  *
  * Clients that no longer need a particular session
  * (e.g., because the user is leaving the client application) SHOULD send an
  * HTTP `DELETE` to the MCP endpoint with the `Mcp-Session-Id` header to explicitly
  * terminate the session.
  *
  * The server MAY respond with HTTP `405 Method Not Allowed`, indicating that
  * the server does not allow clients to terminate sessions.
  */
  async terminateSession() {
    if (!this._sessionId) return;
    try {
      const headers = await this._commonHeaders();
      const init = {
        ...this._requestInit,
        method: "DELETE",
        headers,
        signal: this._abortController?.signal
      };
      const response = await (this._fetch ?? fetch)(this._url, init);
      await response.text?.().catch(() => {
      });
      if (!response.ok && response.status !== 405) throw new SdkError(SdkErrorCode.ClientHttpFailedToTerminateSession, `Failed to terminate session: ${response.statusText}`, {
        status: response.status,
        statusText: response.statusText
      });
      this._sessionId = void 0;
    } catch (error2) {
      this.onerror?.(error2);
      throw error2;
    }
  }
  setProtocolVersion(version2) {
    this._protocolVersion = version2;
  }
  get protocolVersion() {
    return this._protocolVersion;
  }
  /**
  * Resume an SSE stream from a previous event ID.
  * Opens a `GET` SSE connection with `Last-Event-ID` header to replay missed events.
  *
  * @param lastEventId The event ID to resume from
  * @param options Optional callback to receive new resumption tokens
  */
  async resumeStream(lastEventId, options) {
    await this._startOrAuthSse({
      resumptionToken: lastEventId,
      onresumptiontoken: options?.onresumptiontoken
    });
  }
};

// bin/picktime-mcp-runtime.mjs
var CLIENT_INFO = {
  name: "picktime-agentd",
  version: DEFAULT_CLIENT_VERSION
};
var DEFAULT_TIMEOUT_MS = 12e4;
var DEFAULT_TASK_TTL_MS = 6e4;
var DEFAULT_LOG_LEVEL = process.env.PICKTIME_MCP_LOG_LEVEL || "info";
var MAX_EVENT_HISTORY = 100;
var MAX_PROGRESS_EVENTS = 200;
var MAX_STDERR_CHARS = 131072;
function isPlainObject4(value) {
  return Boolean(value) && typeof value === "object" && !Array.isArray(value);
}
function normalizeTimeout(timeoutMs) {
  const numeric = Number(timeoutMs);
  if (Number.isFinite(numeric) && numeric > 0) {
    return numeric;
  }
  return DEFAULT_TIMEOUT_MS;
}
function appendBounded(list, value, limit) {
  list.push(value);
  if (list.length > limit) {
    list.splice(0, list.length - limit);
  }
}
function looksLikeWindowsPath(value) {
  return /^[A-Za-z]:[\\/]/.test(String(value || "")) || String(value || "").startsWith("\\\\");
}
function serializeError(error2) {
  if (error2 instanceof Error) {
    return {
      name: error2.name,
      message: error2.message,
      code: error2.code || null
    };
  }
  return {
    name: "Error",
    message: String(error2),
    code: null
  };
}
function hasUriScheme(value) {
  const text = String(value || "");
  if (looksLikeWindowsPath(text)) {
    return false;
  }
  return /^[A-Za-z][A-Za-z0-9+.-]*:/.test(text);
}
function normalizeStringMap(value) {
  if (!isPlainObject4(value)) {
    return void 0;
  }
  const normalized = {};
  for (const [key, item] of Object.entries(value)) {
    if (!key || item == null) {
      continue;
    }
    normalized[String(key)] = String(item);
  }
  return Object.keys(normalized).length > 0 ? normalized : void 0;
}
function buildSessionCacheKey(server2) {
  return import_node_crypto.default.createHash("sha256").update(
    JSON.stringify({
      command: server2?.command || null,
      args: Array.isArray(server2?.args) ? server2.args.map((item) => String(item)) : [],
      cwd: server2?.cwd || null,
      env: normalizeStringMap(server2?.env) || null,
      url: server2?.url || null,
      headers: normalizeStringMap(server2?.headers) || null
    })
  ).digest("hex");
}
function toRootEntries(rootValues) {
  const seen = /* @__PURE__ */ new Set();
  const roots = [];
  for (const rootValue of Array.isArray(rootValues) ? rootValues : []) {
    if (typeof rootValue !== "string") {
      continue;
    }
    const trimmed = rootValue.trim();
    if (!trimmed) {
      continue;
    }
    let uri2 = null;
    let name = null;
    if (hasUriScheme(trimmed)) {
      try {
        const url2 = new URL(trimmed);
        uri2 = url2.toString();
        if (url2.protocol === "file:") {
          const filePath = decodeURIComponent(
            url2.pathname.replace(/^\/([A-Za-z]:)/, "$1")
          );
          name = import_node_path3.default.basename(filePath) || filePath;
        } else {
          name = url2.pathname || url2.host || url2.toString();
        }
      } catch {
        continue;
      }
    } else {
      const resolved = import_node_path3.default.resolve(trimmed);
      uri2 = (0, import_node_url.pathToFileURL)(resolved).toString();
      name = import_node_path3.default.basename(resolved) || resolved;
    }
    if (!uri2 || seen.has(uri2)) {
      continue;
    }
    seen.add(uri2);
    roots.push({ uri: uri2, name });
  }
  roots.sort((left, right) => left.uri.localeCompare(right.uri));
  return roots;
}
function computeRootsFingerprint(roots) {
  return import_node_crypto.default.createHash("sha256").update(JSON.stringify(roots)).digest("hex");
}
function recordEvent(entry, type, payload = {}) {
  entry.eventSequence += 1;
  appendBounded(
    entry.events,
    {
      sequence: entry.eventSequence,
      received_at: (/* @__PURE__ */ new Date()).toISOString(),
      type,
      ...payload
    },
    MAX_EVENT_HISTORY
  );
}
function createProgressTracker(entry) {
  const progressEvents = [];
  const progressToken = import_node_crypto.default.randomUUID();
  entry.progressListeners.set(progressToken, (progress) => {
    appendBounded(
      progressEvents,
      {
        ...progress,
        received_at: (/* @__PURE__ */ new Date()).toISOString()
      },
      MAX_PROGRESS_EVENTS
    );
  });
  return {
    progressEvents,
    progressToken,
    dispose() {
      entry.progressListeners.delete(progressToken);
    }
  };
}
function getSessionMetadata(entry) {
  return {
    transport: entry.transportKind,
    session_id: entry.transport?.sessionId || null,
    pid: entry.transport instanceof StdioClientTransport ? entry.transport.pid : null
  };
}
function createTransport(entry) {
  if (entry.server?.url) {
    const headers = normalizeStringMap(entry.server.headers);
    return new StreamableHTTPClientTransport(new URL(entry.server.url), {
      requestInit: headers ? { headers } : void 0
    });
  }
  const serverEnv = normalizeStringMap(entry.server.env);
  const env = {
    ...process.env,
    ...serverEnv || {}
  };
  return new StdioClientTransport({
    command: entry.server.command,
    args: Array.isArray(entry.server.args) ? entry.server.args.map((item) => String(item)) : [],
    cwd: entry.server.cwd || void 0,
    env,
    stderr: "pipe"
  });
}
function attachStderrCapture(entry, transport) {
  if (!(transport instanceof StdioClientTransport)) {
    return;
  }
  const stderr = transport.stderr;
  if (!stderr || typeof stderr.on !== "function") {
    return;
  }
  stderr.on("data", (chunk) => {
    entry.stderr += Buffer.from(chunk).toString("utf8");
    if (entry.stderr.length > MAX_STDERR_CHARS) {
      entry.stderr = entry.stderr.slice(-MAX_STDERR_CHARS);
    }
  });
}
async function listAllPages(fetchPage, collectionKey, getIdentity) {
  const items = [];
  const seen = /* @__PURE__ */ new Set();
  let cursor = null;
  while (true) {
    const page = await fetchPage(cursor ? { cursor } : void 0);
    const pageItems = Array.isArray(page?.[collectionKey]) ? page[collectionKey] : [];
    for (const item of pageItems) {
      const identity = getIdentity ? getIdentity(item) : null;
      if (identity && seen.has(identity)) {
        continue;
      }
      if (identity) {
        seen.add(identity);
      }
      items.push(item);
    }
    const nextCursor = typeof page?.nextCursor === "string" ? page.nextCursor : typeof page?.next_cursor === "string" ? page.next_cursor : null;
    if (!nextCursor) {
      break;
    }
    cursor = nextCursor;
  }
  return items;
}
function createRequestOptions(timeoutMs, progressEvents) {
  const timeout = normalizeTimeout(timeoutMs);
  return {
    timeout,
    maxTotalTimeout: timeout,
    resetTimeoutOnProgress: true,
    onprogress: progressEvents ? (progress) => {
      appendBounded(
        progressEvents,
        {
          ...progress,
          received_at: (/* @__PURE__ */ new Date()).toISOString()
        },
        MAX_PROGRESS_EVENTS
      );
    } : void 0
  };
}
function createPicktimeMcpRuntime(options = {}) {
  const sessions = /* @__PURE__ */ new Map();
  async function syncRoots(entry, notifyServer = true) {
    const rootValues = typeof options.buildRoots === "function" ? options.buildRoots(entry.server) : [];
    const roots = toRootEntries(rootValues);
    const fingerprint = computeRootsFingerprint(roots);
    if (notifyServer && entry.rootsFingerprint && entry.rootsFingerprint !== fingerprint) {
      try {
        await entry.client.sendRootsListChanged();
        recordEvent(entry, "roots_list_changed", { root_count: roots.length });
      } catch (error2) {
        recordEvent(entry, "roots_list_changed_error", {
          error: serializeError(error2)
        });
      }
    }
    entry.roots = roots;
    entry.rootsFingerprint = fingerprint;
    return roots;
  }
  function markCatalogDirty(entry, listType, items = null, error2 = null) {
    if (listType === "tools" && Array.isArray(items)) {
      entry.tools = items;
      entry.toolMetadataLoaded = true;
    }
    if (listType === "prompts" && Array.isArray(items)) {
      entry.prompts = items;
    }
    if (listType === "resources" && Array.isArray(items)) {
      entry.resources = items;
    }
    recordEvent(entry, `${listType}_list_changed`, {
      error: error2 ? serializeError(error2) : null,
      item_count: Array.isArray(items) ? items.length : null
    });
    if (typeof options.onCatalogInvalidated === "function") {
      options.onCatalogInvalidated(entry.name);
    }
  }
  async function connectSession(entry, timeoutMs) {
    const client = new Client(CLIENT_INFO, {
      capabilities: {
        roots: {
          listChanged: true
        },
        tasks: {
          requests: {
            tools: {
              call: {}
            }
          }
        }
      },
      listChanged: {
        tools: {
          debounceMs: 0,
          onChanged: (error2, tools) => {
            markCatalogDirty(entry, "tools", tools, error2);
          }
        },
        prompts: {
          debounceMs: 0,
          onChanged: (error2, prompts) => {
            markCatalogDirty(entry, "prompts", prompts, error2);
          }
        },
        resources: {
          debounceMs: 0,
          onChanged: (error2, resources) => {
            markCatalogDirty(entry, "resources", resources, error2);
          }
        }
      }
    });
    client.onclose = () => {
      recordEvent(entry, "session_closed", getSessionMetadata(entry));
      if (sessions.get(entry.name) === entry) {
        sessions.delete(entry.name);
      }
    };
    client.onerror = (error2) => {
      recordEvent(entry, "session_error", {
        error: serializeError(error2)
      });
    };
    client.fallbackNotificationHandler = async (notification) => {
      recordEvent(entry, "notification", {
        method: notification.method,
        params: notification.params || null
      });
    };
    client.setRequestHandler("roots/list", async () => ({
      roots: await syncRoots(entry, false)
    }));
    client.setNotificationHandler("notifications/message", async (notification) => {
      recordEvent(entry, "log", {
        level: notification.params?.level || null,
        data: notification.params?.data ?? null
      });
    });
    client.setNotificationHandler(
      "notifications/resources/updated",
      async (notification) => {
        recordEvent(entry, "resource_updated", {
          uri: notification.params?.uri || null
        });
        if (typeof options.onCatalogInvalidated === "function") {
          options.onCatalogInvalidated(entry.name);
        }
      }
    );
    client.setNotificationHandler("notifications/progress", async (notification) => {
      const progressToken = notification.params?.progressToken;
      const payload = {
        progress: notification.params?.progress ?? null,
        total: notification.params?.total ?? null,
        message: notification.params?.message ?? null,
        progressToken: progressToken ?? null
      };
      recordEvent(entry, "progress", payload);
      if (progressToken == null) {
        return;
      }
      const listener = entry.progressListeners.get(String(progressToken));
      if (listener) {
        listener(payload);
      }
    });
    client.setNotificationHandler("notifications/tasks/status", async (notification) => {
      recordEvent(entry, "task_status", {
        task: notification.params?.task || null
      });
    });
    const transport = createTransport(entry);
    attachStderrCapture(entry, transport);
    entry.client = client;
    entry.transport = transport;
    await client.connect(transport, { timeout: normalizeTimeout(timeoutMs) });
    await syncRoots(entry, false);
    if (client.getServerCapabilities()?.logging) {
      try {
        await client.setLoggingLevel(DEFAULT_LOG_LEVEL);
      } catch (error2) {
        recordEvent(entry, "logging_level_error", {
          error: serializeError(error2)
        });
      }
    }
    recordEvent(entry, "session_connected", {
      ...getSessionMetadata(entry),
      protocol_version: client.getNegotiatedProtocolVersion() || null,
      server_info: client.getServerVersion() || null
    });
  }
  async function closeSessionInternal(entry, reason = "manual") {
    if (!entry) {
      return;
    }
    if (sessions.get(entry.name) === entry) {
      sessions.delete(entry.name);
    }
    recordEvent(entry, "session_closing", { reason });
    try {
      if (entry.client) {
        await entry.client.close();
        return;
      }
      if (entry.transport) {
        await entry.transport.close();
      }
    } catch {
      if (entry.transport) {
        try {
          await entry.transport.close();
        } catch {
        }
      }
    }
  }
  async function getOrCreateSession(server2, timeoutMs) {
    const name = String(server2?.name || "").trim();
    if (!name) {
      throw new Error("mcp_server_name_required");
    }
    const cacheKey = buildSessionCacheKey(server2);
    let entry = sessions.get(name);
    if (entry && entry.cacheKey !== cacheKey) {
      await closeSessionInternal(entry, "config_changed");
      entry = null;
    }
    if (!entry) {
      entry = {
        name,
        server: { ...server2, name },
        cacheKey,
        transportKind: server2?.url ? "http" : "stdio",
        client: null,
        transport: null,
        connectPromise: null,
        discoveryPromise: null,
        roots: [],
        rootsFingerprint: null,
        tools: [],
        prompts: [],
        resources: [],
        resourceTemplates: [],
        toolMetadataLoaded: false,
        progressListeners: /* @__PURE__ */ new Map(),
        stderr: "",
        events: [],
        eventSequence: 0
      };
      sessions.set(name, entry);
      const connectPromise = connectSession(entry, timeoutMs).catch(async (error2) => {
        if (sessions.get(name) === entry) {
          sessions.delete(name);
        }
        try {
          await closeSessionInternal(entry, "connect_failed");
        } catch {
        }
        throw error2;
      });
      entry.connectPromise = connectPromise;
      try {
        await connectPromise;
      } finally {
        entry.connectPromise = null;
      }
    } else if (entry.connectPromise) {
      await entry.connectPromise;
    }
    entry.server = { ...server2, name };
    await syncRoots(entry, true);
    return entry;
  }
  async function discoverServer(server2, options2 = {}) {
    const entry = await getOrCreateSession(server2, options2.timeoutMs);
    if (!entry.discoveryPromise) {
      const promise2 = (async () => {
        const timeout = normalizeTimeout(options2.timeoutMs);
        const requestOptions = { timeout };
        const [tools, prompts, resources, resourceTemplates] = await Promise.all([
          listAllPages(
            (params) => entry.client.listTools(params, requestOptions),
            "tools",
            (tool) => typeof tool?.name === "string" && tool.name.trim() ? tool.name.trim() : null
          ),
          listAllPages(
            (params) => entry.client.listPrompts(params, requestOptions),
            "prompts",
            (prompt) => typeof prompt?.name === "string" && prompt.name.trim() ? prompt.name.trim() : null
          ),
          listAllPages(
            (params) => entry.client.listResources(params, requestOptions),
            "resources",
            (resource) => typeof resource?.uri === "string" && resource.uri.trim() ? resource.uri.trim() : null
          ),
          listAllPages(
            (params) => entry.client.listResourceTemplates(params, requestOptions),
            "resourceTemplates",
            (resourceTemplate) => typeof resourceTemplate?.uriTemplate === "string" && resourceTemplate.uriTemplate.trim() ? resourceTemplate.uriTemplate.trim() : null
          )
        ]);
        entry.tools = tools;
        entry.prompts = prompts;
        entry.resources = resources;
        entry.resourceTemplates = resourceTemplates;
        entry.toolMetadataLoaded = true;
        return {
          protocolVersion: entry.client.getNegotiatedProtocolVersion() || null,
          serverInfo: entry.client.getServerVersion() || null,
          serverCapabilities: entry.client.getServerCapabilities() || null,
          instructions: entry.client.getInstructions() || null,
          tools,
          prompts,
          resources,
          resourceTemplates,
          stderr: entry.stderr,
          session: getSessionMetadata(entry)
        };
      })();
      entry.discoveryPromise = promise2;
      try {
        return await promise2;
      } finally {
        if (entry.discoveryPromise === promise2) {
          entry.discoveryPromise = null;
        }
      }
    }
    return entry.discoveryPromise;
  }
  async function ensureToolMetadata(entry, timeoutMs) {
    if (entry.toolMetadataLoaded) {
      return;
    }
    await discoverServer(entry.server, { timeoutMs });
  }
  async function invokeToolWithTasks(entry, toolName, argumentsPayload, timeoutMs, startSequence, progressTracker) {
    const requestOptions = {
      ...createRequestOptions(timeoutMs),
      task: {
        ttl: normalizeTimeout(timeoutMs) || DEFAULT_TASK_TTL_MS
      }
    };
    const taskEvents = [];
    let latestTask = null;
    let finalResult = null;
    try {
      for await (const message of entry.client.experimental.tasks.callToolStream(
        {
          name: toolName,
          arguments: argumentsPayload || {},
          _meta: {
            progressToken: progressTracker.progressToken
          }
        },
        requestOptions
      )) {
        if (message.type === "taskCreated" || message.type === "taskStatus") {
          latestTask = message.task;
          appendBounded(
            taskEvents,
            {
              type: message.type,
              task: message.task
            },
            MAX_EVENT_HISTORY
          );
          continue;
        }
        if (message.type === "result") {
          if (message.result && Array.isArray(message.result.content)) {
            finalResult = message.result;
            continue;
          }
          if (message.result?.task) {
            latestTask = message.result.task;
          }
          continue;
        }
        if (message.type === "error") {
          throw message.error;
        }
      }
      if (!finalResult && latestTask?.taskId) {
        finalResult = await entry.client.experimental.tasks.getTaskResult(
          latestTask.taskId,
          createRequestOptions(timeoutMs)
        );
      }
      if (!finalResult) {
        throw new Error("mcp_task_result_missing");
      }
      await new Promise((resolve) => setImmediate(resolve));
      return {
        ok: true,
        response_payload: finalResult,
        stderr: entry.stderr,
        protocol_version: entry.client.getNegotiatedProtocolVersion() || null,
        server_info: entry.client.getServerVersion() || null,
        server_capabilities: entry.client.getServerCapabilities() || null,
        instructions: entry.client.getInstructions() || null,
        mcp_progress: progressTracker.progressEvents,
        mcp_notifications: entry.events.filter(
          (event) => event.sequence > startSequence
        ),
        mcp_task: latestTask,
        mcp_task_events: taskEvents,
        mcp_session: getSessionMetadata(entry)
      };
    } finally {
      progressTracker.dispose();
    }
  }
  async function invokeTool(server2, toolName, argumentsPayload, options2 = {}) {
    const entry = await getOrCreateSession(server2, options2.timeoutMs);
    await ensureToolMetadata(entry, options2.timeoutMs);
    const progressTracker = createProgressTracker(entry);
    const startSequence = entry.eventSequence;
    let progressTrackerOwned = true;
    try {
      const result = await entry.client.callTool(
        {
          name: toolName,
          arguments: argumentsPayload || {},
          _meta: {
            progressToken: progressTracker.progressToken
          }
        },
        createRequestOptions(options2.timeoutMs)
      );
      await new Promise((resolve) => setImmediate(resolve));
      return {
        ok: true,
        response_payload: result,
        stderr: entry.stderr,
        protocol_version: entry.client.getNegotiatedProtocolVersion() || null,
        server_info: entry.client.getServerVersion() || null,
        server_capabilities: entry.client.getServerCapabilities() || null,
        instructions: entry.client.getInstructions() || null,
        mcp_progress: progressTracker.progressEvents,
        mcp_notifications: entry.events.filter(
          (event) => event.sequence > startSequence
        ),
        mcp_task: null,
        mcp_task_events: [],
        mcp_session: getSessionMetadata(entry)
      };
    } catch (error2) {
      if (error2 instanceof ProtocolError && /requires task-based execution/i.test(error2.message)) {
        progressTrackerOwned = false;
        return invokeToolWithTasks(
          entry,
          toolName,
          argumentsPayload,
          options2.timeoutMs,
          startSequence,
          progressTracker
        );
      }
      throw error2;
    } finally {
      if (progressTrackerOwned) {
        progressTracker.dispose();
      }
    }
  }
  return {
    async discoverServer(server2, options2 = {}) {
      return discoverServer(server2, options2);
    },
    async invokeTool(server2, toolName, argumentsPayload, options2 = {}) {
      return invokeTool(server2, toolName, argumentsPayload, options2);
    },
    async closeServer(serverName) {
      const entry = sessions.get(String(serverName || ""));
      if (!entry) {
        return;
      }
      await closeSessionInternal(entry, "manual");
    },
    async closeAll() {
      const entries = Array.from(sessions.values());
      for (const entry of entries) {
        await closeSessionInternal(entry, "shutdown");
      }
    }
  };
}

// bin/picktime-agentd.mjs
var daemonState = {
  startedAt: (/* @__PURE__ */ new Date()).toISOString(),
  wsConnected: false,
  wsLastError: null,
  wsConnectedAt: null,
  lastHeartbeatAt: null,
  reconnectTimer: null,
  heartbeatTimer: null,
  websocket: null,
  approvals: /* @__PURE__ */ new Map(),
  pendingRuns: /* @__PURE__ */ new Map(),
  runHistory: [],
  fileWatches: /* @__PURE__ */ new Map(),
  fileWatchEvents: [],
  processes: /* @__PURE__ */ new Map(),
  processCleanupTimer: null,
  mcpDiscoveryCache: /* @__PURE__ */ new Map(),
  clawHubSearchCache: /* @__PURE__ */ new Map(),
  currentPort: null,
  currentToken: null,
  shuttingDown: false,
  deviceFingerprint: null,
  deviceFingerprintError: null,
  deviceFingerprintPromise: null
};
var MAX_RUN_HISTORY = 100;
var MAX_WATCH_EVENTS = 500;
var MAX_PROCESS_HISTORY = 100;
var PROCESS_OUTPUT_DIR = import_node_path4.default.join(stateDir, "process-output");
var DEFAULT_PROCESS_ENTRY_TTL_MS = 30 * 60 * 1e3;
var MIN_PROCESS_ENTRY_TTL_MS = 60 * 1e3;
var MAX_PROCESS_ENTRY_TTL_MS = 3 * 60 * 60 * 1e3;
var DEFAULT_PROCESS_FORCE_KILL_GRACE_MS = 2e3;
var PROCESS_STREAM_DRAIN_GRACE_MS = 150;
var REMOTE_MANAGED_PROCESS_PROGRESS_INTERVAL_MS = 1e3;
var MAX_PROCESS_OUTPUT_SLICE_BYTES = 1024 * 1024;
var MCP_DISCOVERY_CACHE_TTL_MS = 5 * 60 * 1e3;
var MCP_DISCOVERY_ERROR_CACHE_TTL_MS = 30 * 1e3;
var CLAWHUB_SEARCH_CACHE_TTL_MS = 5 * 60 * 1e3;
var CLAWHUB_SEARCH_RESULT_LIMIT = 10;
var DEVICE_FINGERPRINT_VERSION = "desktop-v1";
var DEVICE_FINGERPRINT_COMMAND_TIMEOUT_MS = 4e3;
var MANAGED_PYTHON_SOURCE = "python-build-standalone";
var MANAGED_PYTHON_RELEASE_URL = process.env.PICKTIME_MANAGED_PYTHON_RELEASE_URL || "https://api.github.com/repos/astral-sh/python-build-standalone/releases/latest";
var MANAGED_PYTHON_VERSION_PREFIX = process.env.PICKTIME_MANAGED_PYTHON_VERSION_PREFIX || "3.12.";
var MANAGED_PYTHON_REQUEST_TIMEOUT_MS = Math.max(
  3e4,
  Number(process.env.PICKTIME_MANAGED_PYTHON_REQUEST_TIMEOUT_MS) || 12e4
);
var MANAGED_PYTHON_DOWNLOAD_TIMEOUT_MS = Math.max(
  MANAGED_PYTHON_REQUEST_TIMEOUT_MS,
  Number(process.env.PICKTIME_MANAGED_PYTHON_DOWNLOAD_TIMEOUT_MS) || 3e5
);
var MANAGED_PYTHON_DOWNLOAD_ATTEMPTS = Math.max(
  1,
  Number(process.env.PICKTIME_MANAGED_PYTHON_DOWNLOAD_ATTEMPTS) || 3
);
var CLAWHUB_SKILL_API_BASE_URL = process.env.PICKTIME_CLAWHUB_SKILL_API_BASE_URL || "https://clawhub.ai/api/v1/skills";
var CLAWHUB_SEARCH_API_BASE_URL = process.env.PICKTIME_CLAWHUB_SEARCH_API_BASE_URL || "https://clawhub.ai/api/v1/search";
var CLAWHUB_SKILL_DOWNLOAD_BASE_URL = process.env.PICKTIME_CLAWHUB_DOWNLOAD_BASE_URL || "https://wry-manatee-359.convex.site/api/v1/download";
var SMITHERY_CLI_PACKAGE_SPEC = process.env.PICKTIME_SMITHERY_CLI_PACKAGE_SPEC || "@smithery/cli@4.7.4";
var SMITHERY_REMOTE_PACKAGE_SPEC = process.env.PICKTIME_SMITHERY_REMOTE_PACKAGE_SPEC || "mcp-remote";
var SMITHERY_API_BASE_URL = normalizeServerUrl(
  process.env.PICKTIME_SMITHERY_API_BASE_URL || "https://api.smithery.ai"
);
var SMITHERY_REMOTE_BASE_URL = normalizeServerUrl(
  process.env.PICKTIME_SMITHERY_REMOTE_BASE_URL || "https://server.smithery.ai"
);
var managedPythonRootDir = import_node_path4.default.join(stateDir, "runtimes", "python");
var managedPythonCurrentDir = import_node_path4.default.join(managedPythonRootDir, "current");
var managedPythonMetadataPath = import_node_path4.default.join(
  managedPythonRootDir,
  "runtime.json"
);
var internalCliDir = resolveCurrentModuleDir();
var managedPythonInstallPromise = null;
var SUPPORTED_COMMAND_SHELLS = /* @__PURE__ */ new Set(["auto", "cmd", "powershell", "pwsh"]);
var WINDOWS_BATCH_EXTENSIONS = /* @__PURE__ */ new Set([".bat", ".cmd"]);
var WINDOWS_SHELL_BUILTINS = /* @__PURE__ */ new Set([
  "assoc",
  "break",
  "call",
  "cd",
  "chdir",
  "cls",
  "color",
  "copy",
  "date",
  "del",
  "dir",
  "echo",
  "endlocal",
  "erase",
  "exit",
  "for",
  "ftype",
  "goto",
  "if",
  "md",
  "mkdir",
  "mklink",
  "move",
  "path",
  "pause",
  "popd",
  "prompt",
  "pushd",
  "rd",
  "rem",
  "ren",
  "rename",
  "rmdir",
  "set",
  "setlocal",
  "shift",
  "start",
  "time",
  "title",
  "tree",
  "type",
  "ver",
  "vol"
]);
function resolveCurrentModuleDir() {
  const scriptPath = typeof __filename === "string" && __filename ? __filename : typeof process.argv[1] === "string" && process.argv[1].trim() ? import_node_path4.default.resolve(process.argv[1]) : import_node_path4.default.join(process.cwd(), "picktime-agentd.mjs");
  return import_node_path4.default.dirname(scriptPath);
}
function generateId() {
  return import_node_crypto2.default.randomUUID();
}
function isPlainObject5(value) {
  return Boolean(value) && typeof value === "object" && !Array.isArray(value);
}
function normalizeMcpServerTransport(server2) {
  return server2?.url ? "http" : "stdio";
}
function describeMcpServer(server2) {
  return server2?.url ? `HTTP ${server2.url}` : `stdio ${server2?.command || ""}`.trim() || null;
}
function normalizeMcpServerInfo(serverInfo) {
  if (!isPlainObject5(serverInfo)) {
    return null;
  }
  return {
    name: typeof serverInfo.name === "string" && serverInfo.name.trim() ? serverInfo.name.trim() : null,
    title: typeof serverInfo.title === "string" && serverInfo.title.trim() ? serverInfo.title.trim() : null,
    version: typeof serverInfo.version === "string" && serverInfo.version.trim() ? serverInfo.version.trim() : null
  };
}
function normalizeMcpTool(tool) {
  const inputSchema = isPlainObject5(tool?.inputSchema) ? tool.inputSchema : isPlainObject5(tool?.input_schema) ? tool.input_schema : {};
  const outputSchema = isPlainObject5(tool?.outputSchema) ? tool.outputSchema : isPlainObject5(tool?.output_schema) ? tool.output_schema : null;
  return {
    name: String(tool?.name || ""),
    title: typeof tool?.title === "string" && tool.title.trim() ? tool.title.trim() : null,
    description: typeof tool?.description === "string" && tool.description.trim() ? tool.description.trim() : null,
    input_schema: inputSchema,
    output_schema: outputSchema,
    annotations: isPlainObject5(tool?.annotations) ? tool.annotations : null,
    execution: isPlainObject5(tool?.execution) ? tool.execution : null
  };
}
function normalizeMcpPrompt(prompt) {
  return {
    name: String(prompt?.name || ""),
    title: typeof prompt?.title === "string" && prompt.title.trim() ? prompt.title.trim() : null,
    description: typeof prompt?.description === "string" && prompt.description.trim() ? prompt.description.trim() : null,
    arguments: Array.isArray(prompt?.arguments) ? prompt.arguments.map((argument) => ({
      name: String(argument?.name || ""),
      description: typeof argument?.description === "string" && argument.description.trim() ? argument.description.trim() : null,
      required: argument?.required === true
    })).filter((argument) => argument.name) : [],
    _meta: isPlainObject5(prompt?._meta) ? prompt._meta : null
  };
}
function normalizeMcpResource(resource) {
  return {
    uri: typeof resource?.uri === "string" && resource.uri.trim() ? resource.uri.trim() : null,
    name: typeof resource?.name === "string" && resource.name.trim() ? resource.name.trim() : null,
    title: typeof resource?.title === "string" && resource.title.trim() ? resource.title.trim() : null,
    description: typeof resource?.description === "string" && resource.description.trim() ? resource.description.trim() : null,
    mime_type: typeof resource?.mimeType === "string" && resource.mimeType.trim() ? resource.mimeType.trim() : null,
    size: Number.isFinite(resource?.size) && Number(resource.size) >= 0 ? Number(resource.size) : null,
    annotations: isPlainObject5(resource?.annotations) ? resource.annotations : null,
    _meta: isPlainObject5(resource?._meta) ? resource._meta : null
  };
}
function normalizeMcpResourceTemplate(resourceTemplate) {
  return {
    uri_template: typeof resourceTemplate?.uriTemplate === "string" && resourceTemplate.uriTemplate.trim() ? resourceTemplate.uriTemplate.trim() : null,
    name: typeof resourceTemplate?.name === "string" && resourceTemplate.name.trim() ? resourceTemplate.name.trim() : null,
    title: typeof resourceTemplate?.title === "string" && resourceTemplate.title.trim() ? resourceTemplate.title.trim() : null,
    description: typeof resourceTemplate?.description === "string" && resourceTemplate.description.trim() ? resourceTemplate.description.trim() : null,
    mime_type: typeof resourceTemplate?.mimeType === "string" && resourceTemplate.mimeType.trim() ? resourceTemplate.mimeType.trim() : null,
    annotations: isPlainObject5(resourceTemplate?.annotations) ? resourceTemplate.annotations : null,
    _meta: isPlainObject5(resourceTemplate?._meta) ? resourceTemplate._meta : null
  };
}
function normalizeMcpCatalogError(error2) {
  const message = error2 instanceof Error ? error2.message : String(error2);
  const [code] = String(message || "mcp_error").split(":", 1);
  return {
    code: code || "mcp_error",
    message: message || "mcp_error"
  };
}
function pickFirstString(value) {
  if (typeof value === "string" && value.trim()) {
    return value.trim();
  }
  if (!Array.isArray(value)) {
    return null;
  }
  const match = value.find(
    (item) => typeof item === "string" && item.trim().length > 0
  );
  return typeof match === "string" ? match.trim() : null;
}
function normalizeTimestamp(value) {
  const numeric = Number(value);
  if (!Number.isFinite(numeric) || numeric <= 0) {
    return null;
  }
  const millis = numeric >= 10 ** 12 ? numeric : numeric * 1e3;
  const date6 = new Date(millis);
  return Number.isNaN(date6.getTime()) ? null : date6.toISOString();
}
function normalizeClawHubSearchResult(item) {
  const slug = pickFirstString(item?.slug) || pickFirstString(item?.id) || pickFirstString(item?.skill?.slug);
  if (!slug) {
    return null;
  }
  const displayName = pickFirstString(item?.displayName) || pickFirstString(item?.display_name) || pickFirstString(item?.name) || pickFirstString(item?.skill?.displayName) || pickFirstString(item?.skill?.display_name) || pickFirstString(item?.skill?.name) || slug;
  const score = Number(item?.score);
  return {
    slug,
    displayName,
    summary: pickFirstString(item?.summary) || pickFirstString(item?.description) || pickFirstString(item?.skill?.summary) || pickFirstString(item?.skill?.description) || null,
    version: pickFirstString(item?.version) || pickFirstString(item?.latestVersion?.version) || null,
    score: Number.isFinite(score) ? score : 0,
    updatedAt: normalizeTimestamp(item?.updatedAt ?? item?.updated_at)
  };
}
function buildClawHubSearchUrl(query) {
  const url2 = new URL(String(CLAWHUB_SEARCH_API_BASE_URL).replace(/\/+$/, ""));
  url2.searchParams.set("q", query);
  return url2.toString();
}
function buildClawHubSearchCacheKey(query, limit) {
  return `${String(query || "").trim().toLowerCase()}::${Number(limit) || CLAWHUB_SEARCH_RESULT_LIMIT}`;
}
async function searchClawHubSkills(query, options = {}) {
  const normalizedQuery = String(query || "").trim();
  const limit = Math.max(
    1,
    Math.min(
      CLAWHUB_SEARCH_RESULT_LIMIT,
      Number(options.limit) || CLAWHUB_SEARCH_RESULT_LIMIT
    )
  );
  if (!normalizedQuery) {
    return {
      query: "",
      generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
      results: []
    };
  }
  const cacheKey = buildClawHubSearchCacheKey(normalizedQuery, limit);
  const cached2 = daemonState.clawHubSearchCache.get(cacheKey);
  if (options.refresh !== true && cached2?.expiresAt && cached2.expiresAt > Date.now() && cached2.payload) {
    return cached2.payload;
  }
  const payload = await fetchJson(buildClawHubSearchUrl(normalizedQuery));
  const rawResults = Array.isArray(payload?.results) ? payload.results : Array.isArray(payload?.items) ? payload.items : Array.isArray(payload) ? payload : [];
  const deduped = /* @__PURE__ */ new Map();
  for (const item of rawResults) {
    const normalized = normalizeClawHubSearchResult(item);
    if (!normalized) {
      continue;
    }
    const existing = deduped.get(normalized.slug);
    if (!existing || normalized.score > existing.score) {
      deduped.set(normalized.slug, normalized);
    }
  }
  const results = Array.from(deduped.values()).sort((left, right) => {
    if (right.score !== left.score) {
      return right.score - left.score;
    }
    const rightTime = Date.parse(right.updatedAt || "") || 0;
    const leftTime = Date.parse(left.updatedAt || "") || 0;
    if (rightTime !== leftTime) {
      return rightTime - leftTime;
    }
    return left.displayName.localeCompare(right.displayName);
  }).slice(0, limit);
  const response = {
    query: normalizedQuery,
    generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
    results
  };
  daemonState.clawHubSearchCache.set(cacheKey, {
    expiresAt: Date.now() + CLAWHUB_SEARCH_CACHE_TTL_MS,
    payload: response
  });
  return response;
}
function normalizeSmitheryQualifiedPath(qualifiedName) {
  return String(qualifiedName || "").trim().split("/").map((segment) => segment.trim()).filter(Boolean).map((segment) => encodeURIComponent(segment)).join("/");
}
function buildSmitheryConnectionUrl(qualifiedName, connectionUrl = null) {
  const normalizedConnectionUrl = normalizeServerUrl(connectionUrl || "");
  if (normalizedConnectionUrl) {
    return normalizedConnectionUrl;
  }
  const encodedQualifiedName = normalizeSmitheryQualifiedPath(qualifiedName);
  if (!encodedQualifiedName) {
    throw new Error("smithery_qualified_name_required");
  }
  return `${SMITHERY_REMOTE_BASE_URL}/${encodedQualifiedName}`;
}
function buildSmitheryRemoteUrl(qualifiedName, connectionUrl = null) {
  return `${buildSmitheryConnectionUrl(qualifiedName, connectionUrl)}/mcp`;
}
function sanitizeSmitheryIdentifierSegment(value) {
  const normalized = String(value || "").trim().replace(/[^A-Za-z0-9]+/g, "_").replace(/^_+|_+$/g, "").toUpperCase();
  return normalized || "VALUE";
}
function buildSmitheryConfigEnvName(qualifiedName, fieldKey) {
  return [
    "PICKTIME",
    "SMITHERY",
    sanitizeSmitheryIdentifierSegment(qualifiedName),
    sanitizeSmitheryIdentifierSegment(fieldKey)
  ].join("_");
}
function pickSmitheryFieldType(schema) {
  return pickFirstString(schema?.type) || "string";
}
function isSmitherySecretField(fieldKey, schema) {
  const format2 = pickFirstString(schema?.format);
  if (format2 === "password" || format2 === "secret") {
    return true;
  }
  return /secret|token|api[_-]?key|password/i.test(String(fieldKey || ""));
}
function normalizeSmitheryConfigField(fieldKey, schema, required2) {
  const xFrom = isPlainObject5(schema?.["x-from"]) ? schema["x-from"] : null;
  const queryName = typeof xFrom?.query === "string" && xFrom.query.trim() ? xFrom.query.trim() : null;
  const headerName = typeof xFrom?.header === "string" && xFrom.header.trim() ? xFrom.header.trim() : null;
  return {
    key: String(fieldKey || "").trim(),
    title: typeof schema?.title === "string" && schema.title.trim() || String(fieldKey || "").trim(),
    description: typeof schema?.description === "string" && schema.description.trim() ? schema.description.trim() : null,
    type: pickSmitheryFieldType(schema),
    required: required2 === true,
    enumValues: Array.isArray(schema?.enum) ? schema.enum.map((item) => String(item)) : [],
    defaultValue: Object.prototype.hasOwnProperty.call(schema || {}, "default") ? schema.default : void 0,
    source: queryName ? "query" : headerName ? "header" : "unknown",
    sourceName: queryName || headerName || null,
    secret: isSmitherySecretField(fieldKey, schema),
    examples: Array.isArray(schema?.examples) ? schema.examples.slice(0, 3) : []
  };
}
function normalizeSmitherySearchResult(item) {
  const qualifiedName = pickFirstString(item?.qualifiedName) || pickFirstString(item?.qualified_name) || pickFirstString(item?.slug) || pickFirstString(item?.id);
  if (!qualifiedName) {
    return null;
  }
  return {
    name: pickFirstString(item?.name) || qualifiedName,
    qualifiedName,
    description: pickFirstString(item?.description) || null,
    useCount: Math.max(0, Number(item?.useCount ?? item?.use_count ?? 0) || 0),
    connectionUrl: buildSmitheryConnectionUrl(
      qualifiedName,
      pickFirstString(item?.connectionUrl) || pickFirstString(item?.connection_url)
    )
  };
}
function normalizeSmitheryToolSummary(tool) {
  const name = pickFirstString(tool?.name);
  if (!name) {
    return null;
  }
  return {
    name,
    description: pickFirstString(tool?.description) || null
  };
}
function normalizeSmitheryServerDetail(rawDetail, fallback = {}) {
  const qualifiedName = pickFirstString(rawDetail?.qualifiedName) || pickFirstString(rawDetail?.qualified_name) || pickFirstString(fallback?.qualifiedName);
  if (!qualifiedName) {
    throw new Error("smithery_qualified_name_required");
  }
  const configSchema = isPlainObject5(rawDetail?.configSchema) ? rawDetail.configSchema : isPlainObject5(rawDetail?.connections?.[0]?.configSchema) ? rawDetail.connections[0].configSchema : {};
  const configProperties = isPlainObject5(configSchema.properties) ? configSchema.properties : {};
  const requiredFields = new Set(
    Array.isArray(configSchema.required) ? configSchema.required.map((item) => String(item || "").trim()).filter(Boolean) : []
  );
  const configFields = Object.entries(configProperties).map(
    ([fieldKey, schema]) => normalizeSmitheryConfigField(
      fieldKey,
      isPlainObject5(schema) ? schema : {},
      requiredFields.has(fieldKey)
    )
  ).filter((field) => field.key);
  const connectionUrl = buildSmitheryConnectionUrl(
    qualifiedName,
    pickFirstString(rawDetail?.connectionUrl) || pickFirstString(rawDetail?.connection_url) || pickFirstString(fallback?.connectionUrl)
  );
  const tools = Array.isArray(rawDetail?.tools) ? rawDetail.tools.map((tool) => normalizeSmitheryToolSummary(tool)).filter(Boolean) : [];
  return {
    qualifiedName,
    displayName: pickFirstString(rawDetail?.displayName) || pickFirstString(rawDetail?.display_name) || pickFirstString(rawDetail?.name) || qualifiedName,
    description: pickFirstString(rawDetail?.description) || null,
    connectionUrl,
    remoteUrl: buildSmitheryRemoteUrl(qualifiedName, connectionUrl),
    deploymentUrl: pickFirstString(rawDetail?.deploymentUrl) || pickFirstString(rawDetail?.deployment_url) || null,
    homepageUrl: pickFirstString(rawDetail?.homepageUrl) || pickFirstString(rawDetail?.homepage_url) || pickFirstString(rawDetail?.homepage) || pickFirstString(rawDetail?.website) || null,
    configSchema,
    configFields,
    requiresConfig: configFields.some((field) => field.required),
    toolCount: tools.length,
    tools
  };
}
function resolveSmitheryInstallName(qualifiedName, requestedName) {
  const candidate = String(requestedName || "").trim();
  if (candidate) {
    return candidate;
  }
  const normalized = String(qualifiedName || "").trim().replace(/[^A-Za-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "");
  if (!normalized) {
    throw new Error("smithery_install_name_required");
  }
  return normalized;
}
function coerceSmitheryConfigValue(field, rawValue) {
  if (typeof rawValue === "undefined" || rawValue === null || typeof rawValue === "string" && rawValue.trim() === "") {
    if (field.required) {
      throw new Error(`smithery_config_required:${field.key}`);
    }
    return null;
  }
  if (field.type === "integer") {
    const nextValue2 = Number(rawValue);
    if (!Number.isInteger(nextValue2)) {
      throw new Error(`smithery_config_invalid_integer:${field.key}`);
    }
    return nextValue2;
  }
  if (field.type === "number") {
    const nextValue2 = Number(rawValue);
    if (!Number.isFinite(nextValue2)) {
      throw new Error(`smithery_config_invalid_number:${field.key}`);
    }
    return nextValue2;
  }
  if (field.type === "boolean") {
    if (typeof rawValue === "boolean") {
      return rawValue;
    }
    const normalized = String(rawValue).trim().toLowerCase();
    if (["true", "1", "yes", "on"].includes(normalized)) {
      return true;
    }
    if (["false", "0", "no", "off"].includes(normalized)) {
      return false;
    }
    throw new Error(`smithery_config_invalid_boolean:${field.key}`);
  }
  const nextValue = String(rawValue);
  if (field.enumValues.length > 0 && !field.enumValues.includes(nextValue)) {
    throw new Error(`smithery_config_invalid_enum:${field.key}`);
  }
  return nextValue;
}
function collectSmitheryInstallConfig(detail, rawConfig) {
  const input = isPlainObject5(rawConfig) ? rawConfig : {};
  const nextConfig = {};
  for (const field of detail.configFields || []) {
    const rawValue = Object.prototype.hasOwnProperty.call(input, field.key) ? input[field.key] : void 0;
    const coerced = coerceSmitheryConfigValue(field, rawValue);
    if (coerced !== null) {
      nextConfig[field.key] = coerced;
    }
  }
  return nextConfig;
}
function hasProxyEnvironment() {
  return [
    process.env.HTTPS_PROXY,
    process.env.HTTP_PROXY,
    process.env.NO_PROXY,
    process.env.https_proxy,
    process.env.http_proxy,
    process.env.no_proxy
  ].some((value) => typeof value === "string" && value.trim().length > 0);
}
function buildSmitheryMcpServerConfig(detail, installName, rawConfig) {
  const resolvedConfig = collectSmitheryInstallConfig(detail, rawConfig);
  const remoteUrl = new URL(detail.remoteUrl);
  const env = {};
  const headerArgs = [];
  const queryPlaceholders = [];
  for (const field of detail.configFields || []) {
    if (!Object.prototype.hasOwnProperty.call(resolvedConfig, field.key)) {
      continue;
    }
    const value = resolvedConfig[field.key];
    const envName = buildSmitheryConfigEnvName(detail.qualifiedName, field.key);
    env[envName] = String(value);
    if (field.source === "query") {
      const placeholder = "${" + envName + "}";
      remoteUrl.searchParams.set(field.sourceName || field.key, placeholder);
      queryPlaceholders.push({
        encoded: encodeURIComponent(placeholder),
        raw: placeholder
      });
      continue;
    }
    if (field.source === "header") {
      headerArgs.push(
        "--header",
        `${field.sourceName || field.key}:\${` + envName + "}"
      );
      continue;
    }
    throw new Error(`smithery_config_source_unknown:${field.key}`);
  }
  let remoteUrlText = remoteUrl.toString();
  for (const placeholder of queryPlaceholders) {
    remoteUrlText = remoteUrlText.replaceAll(
      placeholder.encoded,
      placeholder.raw
    );
  }
  const args = process.platform === "win32" ? ["-y", SMITHERY_REMOTE_PACKAGE_SPEC, remoteUrlText] : ["-y", SMITHERY_REMOTE_PACKAGE_SPEC, remoteUrlText];
  args.push(...headerArgs);
  if (hasProxyEnvironment()) {
    args.push("--enable-proxy");
  }
  return {
    command: resolveNpxCommand(),
    args,
    env: Object.keys(env).length > 0 ? env : null,
    enabled: true,
    source: "smithery",
    label: detail.displayName || installName,
    description: detail.description || `Smithery remote MCP: ${detail.qualifiedName}`,
    smithery: {
      qualifiedName: detail.qualifiedName,
      displayName: detail.displayName,
      connectionUrl: detail.connectionUrl,
      remoteUrl: remoteUrlText,
      installedAt: (/* @__PURE__ */ new Date()).toISOString(),
      config: resolvedConfig,
      configFields: (detail.configFields || []).map((field) => ({
        key: field.key,
        source: field.source,
        sourceName: field.sourceName || null
      }))
    }
  };
}
function resolveNpxCommand() {
  return process.platform === "win32" ? "npx.cmd" : "npx";
}
async function executeSmitheryCliJson(args, options = {}) {
  let result;
  try {
    result = await executeSimpleCommand(
      resolveNpxCommand(),
      ["-y", SMITHERY_CLI_PACKAGE_SPEC, ...args],
      {
        timeoutMs: options.timeoutMs || 12e4
      }
    );
  } catch (error2) {
    const message = error2 instanceof Error ? error2.message : String(error2);
    if (/ENOENT/i.test(message)) {
      throw new Error("smithery_cli_not_available");
    }
    throw new Error(`smithery_cli_failed:${message}`);
  }
  try {
    return JSON.parse(result.stdout || "null");
  } catch {
    throw new Error("smithery_cli_invalid_json");
  }
}
async function searchSmitheryMcpServers(params) {
  const query = String(params?.query || params?.term || "").trim();
  if (!query) {
    throw new Error("smithery_query_required");
  }
  const limit = Math.max(1, Math.min(20, Number(params?.limit) || 8));
  const payload = await executeSmitheryCliJson(
    ["--json", "mcp", "search", query, "--limit", String(limit)],
    { timeoutMs: 12e4 }
  );
  const rawItems = Array.isArray(payload) ? payload : Array.isArray(payload?.results) ? payload.results : [];
  const results = rawItems.map((item) => normalizeSmitherySearchResult(item)).filter(Boolean);
  return {
    query,
    generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
    results
  };
}
async function getSmitheryMcpServerDetails(params) {
  const qualifiedName = String(params?.qualifiedName || "").trim();
  if (!qualifiedName) {
    throw new Error("smithery_qualified_name_required");
  }
  const detailUrl = `${SMITHERY_API_BASE_URL}/servers/${normalizeSmitheryQualifiedPath(qualifiedName)}`;
  const rawDetail = await fetchJson(detailUrl, { timeoutMs: 3e4 });
  return normalizeSmitheryServerDetail(rawDetail, {
    qualifiedName,
    connectionUrl: typeof params?.connectionUrl === "string" ? params.connectionUrl : null
  });
}
async function installSmitheryMcpServer(params) {
  const detail = await getSmitheryMcpServerDetails(params);
  const installName = resolveSmitheryInstallName(
    detail.qualifiedName,
    params?.installName || params?.name
  );
  const config2 = loadMcpConfig();
  config2.mcpServers ||= {};
  if (config2.mcpServers[installName] && params?.overwrite !== true) {
    throw new Error("mcp_server_already_exists");
  }
  const serverConfig = buildSmitheryMcpServerConfig(
    detail,
    installName,
    params?.config
  );
  config2.mcpServers[installName] = serverConfig;
  saveMcpConfig(config2);
  clearMcpDiscoveryCache(installName);
  appendAudit("smithery_mcp_installed", {
    name: installName,
    qualifiedName: detail.qualifiedName,
    remoteUrl: serverConfig.smithery?.remoteUrl || null
  });
  triggerCapabilitySync();
  return {
    ok: true,
    name: installName,
    qualifiedName: detail.qualifiedName,
    remoteUrl: serverConfig.smithery?.remoteUrl || detail.remoteUrl,
    transport: "stdio_proxy",
    server: {
      name: installName,
      ...serverConfig
    }
  };
}
function coerceMcpArguments(value) {
  if (isPlainObject5(value)) {
    return value;
  }
  if (typeof value === "string") {
    const trimmed = value.trim();
    if (!trimmed) {
      return {};
    }
    try {
      const parsed = JSON.parse(trimmed);
      if (isPlainObject5(parsed)) {
        return parsed;
      }
      return { value: parsed };
    } catch {
      return { value };
    }
  }
  if (value == null) {
    return {};
  }
  return { value };
}
function buildMcpDiscoveryCacheKey(server2) {
  return import_node_crypto2.default.createHash("sha256").update(
    JSON.stringify({
      enabled: server2?.enabled !== false,
      source: server2?.source || "managed",
      url: server2?.url || null,
      headers: isPlainObject5(server2?.headers) ? server2.headers : null,
      command: server2?.command || null,
      args: Array.isArray(server2?.args) ? server2.args.map((item) => String(item)) : [],
      cwd: server2?.cwd || null,
      env: isPlainObject5(server2?.env) ? server2.env : null
    })
  ).digest("hex");
}
function clearMcpDiscoveryCache(serverName) {
  const closeSession = arguments[1]?.closeSession === true;
  if (serverName) {
    const normalizedServerName = String(serverName);
    daemonState.mcpDiscoveryCache.delete(normalizedServerName);
    if (closeSession) {
      void mcpRuntime.closeServer(normalizedServerName).catch((error2) => {
        appendDaemonLog("warn", "mcp_session_close_failed", {
          server_name: normalizedServerName,
          error: error2 instanceof Error ? error2.message : String(error2)
        });
      });
    }
    return;
  }
  daemonState.mcpDiscoveryCache.clear();
  if (closeSession) {
    void mcpRuntime.closeAll().catch((error2) => {
      appendDaemonLog("warn", "mcp_session_close_failed", {
        server_name: null,
        error: error2 instanceof Error ? error2.message : String(error2)
      });
    });
  }
}
function readCachedMcpDiscovery(serverName, server2) {
  const cacheKey = String(serverName || "");
  const cachedEntry = daemonState.mcpDiscoveryCache.get(cacheKey);
  if (!cachedEntry) {
    return null;
  }
  if (cachedEntry.cacheKey !== buildMcpDiscoveryCacheKey(server2) || cachedEntry.expiresAt <= Date.now()) {
    daemonState.mcpDiscoveryCache.delete(cacheKey);
    return null;
  }
  return {
    ...cachedEntry.value,
    cached: true
  };
}
function writeCachedMcpDiscovery(serverName, server2, value) {
  daemonState.mcpDiscoveryCache.set(String(serverName || ""), {
    cacheKey: buildMcpDiscoveryCacheKey(server2),
    expiresAt: Date.now() + (value?.error ? MCP_DISCOVERY_ERROR_CACHE_TTL_MS : MCP_DISCOVERY_CACHE_TTL_MS),
    value
  });
  return {
    ...value,
    cached: false
  };
}
function buildMcpCatalogPreview() {
  return listManagedMcpServers().map((server2) => {
    const cached2 = readCachedMcpDiscovery(server2.name, server2);
    return {
      name: server2.name,
      enabled: server2.enabled !== false,
      transport: normalizeMcpServerTransport(server2),
      discovery_state: cached2?.discovery_state || (server2.enabled === false ? "disabled" : server2.url ? "unsupported" : "unknown"),
      tool_count: cached2?.tool_count || 0,
      discovered_at: cached2?.discovered_at || null,
      tools: Array.isArray(cached2?.tools) ? cached2.tools.map((tool) => ({
        name: tool.name,
        title: tool.title || null,
        description: tool.description || null
      })) : [],
      error: cached2?.error || null
    };
  });
}
function normalizeMcpRootValue(value) {
  if (typeof value !== "string" || !value.trim()) {
    return null;
  }
  const trimmed = value.trim();
  if (/^[A-Za-z]:[\\/]/.test(trimmed) || trimmed.startsWith("\\\\")) {
    return import_node_path4.default.resolve(trimmed);
  }
  if (/^[A-Za-z][A-Za-z0-9+.-]*:/.test(trimmed)) {
    return trimmed;
  }
  return import_node_path4.default.resolve(trimmed);
}
function buildMcpRootsForServer(server2) {
  const config2 = getConfig();
  const filesystemPolicy = config2?.policies?.filesystem || {};
  const roots = [];
  const seen = /* @__PURE__ */ new Set();
  if (filesystemPolicy.mode === "allowlist") {
    for (const rootValue of Array.isArray(filesystemPolicy.allowedRoots) ? filesystemPolicy.allowedRoots : []) {
      const normalizedRoot = normalizeMcpRootValue(rootValue);
      if (!normalizedRoot || seen.has(normalizedRoot)) {
        continue;
      }
      seen.add(normalizedRoot);
      roots.push(normalizedRoot);
    }
  }
  const cwdRoot = normalizeMcpRootValue(server2?.cwd || null);
  if (cwdRoot && !seen.has(cwdRoot)) {
    seen.add(cwdRoot);
    roots.push(cwdRoot);
  }
  const stateRoot = normalizeMcpRootValue(stateDir);
  if (stateRoot && !seen.has(stateRoot)) {
    roots.push(stateRoot);
  }
  return roots;
}
var mcpRuntime = createPicktimeMcpRuntime({
  buildRoots: (server2) => buildMcpRootsForServer(server2),
  onCatalogInvalidated: (serverName) => clearMcpDiscoveryCache(serverName)
});
function pushWatchEvent(event) {
  daemonState.fileWatchEvents.push({
    id: generateId(),
    at: (/* @__PURE__ */ new Date()).toISOString(),
    ...event
  });
  if (daemonState.fileWatchEvents.length > MAX_WATCH_EVENTS) {
    daemonState.fileWatchEvents.splice(
      0,
      daemonState.fileWatchEvents.length - MAX_WATCH_EVENTS
    );
  }
}
function getManagedProcess(processId) {
  const processEntry = daemonState.processes.get(String(processId || ""));
  if (!processEntry) {
    throw new Error("process_not_found");
  }
  return processEntry;
}
function clampInteger(value, { fallback, min = 0, max = Number.MAX_SAFE_INTEGER } = {}) {
  const numeric = Number(value);
  if (!Number.isFinite(numeric)) {
    return fallback;
  }
  return Math.min(max, Math.max(min, Math.floor(numeric)));
}
function clampProcessEntryTtlMs(value) {
  return clampInteger(value, {
    fallback: DEFAULT_PROCESS_ENTRY_TTL_MS,
    min: MIN_PROCESS_ENTRY_TTL_MS,
    max: MAX_PROCESS_ENTRY_TTL_MS
  });
}
function trimOutputTail(text, maxBytes) {
  const buffer = Buffer.from(String(text || ""), "utf8");
  if (buffer.length <= maxBytes) {
    return buffer.toString("utf8");
  }
  return buffer.subarray(buffer.length - maxBytes).toString("utf8");
}
function safeRemoveFile(filePath) {
  try {
    import_node_fs2.default.rmSync(filePath, { force: true });
  } catch {
  }
}
function createProcessOutputStore(processId, options = {}) {
  ensureDir(PROCESS_OUTPUT_DIR);
  const inlineLimitBytes = clampInteger(options.inlineLimitBytes, {
    fallback: 65536,
    min: 1024,
    max: MAX_PROCESS_OUTPUT_SLICE_BYTES
  });
  const maxStoredBytes = clampInteger(options.maxStoredBytes, {
    fallback: Math.max(inlineLimitBytes * 8, 1024 * 1024),
    min: inlineLimitBytes,
    max: 128 * 1024 * 1024
  });
  const basePath = import_node_path4.default.join(PROCESS_OUTPUT_DIR, processId);
  const stdoutPath = `${basePath}.stdout.log`;
  const stderrPath = `${basePath}.stderr.log`;
  const combinedPath = `${basePath}.combined.log`;
  return {
    stdout_path: stdoutPath,
    stderr_path: stderrPath,
    combined_path: combinedPath,
    stdout_bytes: 0,
    stderr_bytes: 0,
    combined_bytes: 0,
    stdout_tail: "",
    stderr_tail: "",
    combined_tail: "",
    output_truncated: false,
    discarded_bytes: 0,
    inline_limit_bytes: inlineLimitBytes,
    max_stored_bytes: maxStoredBytes,
    _closed: false,
    _truncateNoticeInjected: false,
    _stdoutFd: import_node_fs2.default.openSync(stdoutPath, "w"),
    _stderrFd: import_node_fs2.default.openSync(stderrPath, "w"),
    _combinedFd: import_node_fs2.default.openSync(combinedPath, "w")
  };
}
function injectProcessOutputTruncationNotice(store, streamName) {
  if (store._truncateNoticeInjected) {
    return;
  }
  store._truncateNoticeInjected = true;
  const notice = `
[output truncated after ${store.max_stored_bytes} bytes]
`;
  if (streamName === "stdout") {
    store.stdout_tail = trimOutputTail(
      `${store.stdout_tail}${notice}`,
      store.inline_limit_bytes
    );
  }
  if (streamName === "stderr") {
    store.stderr_tail = trimOutputTail(
      `${store.stderr_tail}${notice}`,
      store.inline_limit_bytes
    );
  }
  store.combined_tail = trimOutputTail(
    `${store.combined_tail}${notice}`,
    store.inline_limit_bytes
  );
}
function appendToProcessOutput(store, streamName, chunk) {
  if (!store || store._closed) {
    return 0;
  }
  const text = typeof chunk === "string" ? chunk : String(chunk || "");
  if (!text) {
    return 0;
  }
  const buffer = Buffer.from(text, "utf8");
  if (buffer.length === 0) {
    return 0;
  }
  if (store.output_truncated) {
    store.discarded_bytes += buffer.length;
    return 0;
  }
  const remainingCapacity = Math.max(
    0,
    store.max_stored_bytes - store.combined_bytes
  );
  if (remainingCapacity === 0) {
    store.output_truncated = true;
    store.discarded_bytes += buffer.length;
    injectProcessOutputTruncationNotice(store, streamName);
    return 0;
  }
  const writableBuffer = buffer.length > remainingCapacity ? buffer.subarray(0, remainingCapacity) : buffer;
  const writableText = writableBuffer.toString("utf8");
  if (streamName === "stdout") {
    import_node_fs2.default.writeSync(store._stdoutFd, writableBuffer);
    store.stdout_bytes += writableBuffer.length;
    store.stdout_tail = trimOutputTail(
      `${store.stdout_tail}${writableText}`,
      store.inline_limit_bytes
    );
  } else {
    import_node_fs2.default.writeSync(store._stderrFd, writableBuffer);
    store.stderr_bytes += writableBuffer.length;
    store.stderr_tail = trimOutputTail(
      `${store.stderr_tail}${writableText}`,
      store.inline_limit_bytes
    );
  }
  import_node_fs2.default.writeSync(store._combinedFd, writableBuffer);
  store.combined_bytes += writableBuffer.length;
  store.combined_tail = trimOutputTail(
    `${store.combined_tail}${writableText}`,
    store.inline_limit_bytes
  );
  if (writableBuffer.length < buffer.length) {
    store.output_truncated = true;
    store.discarded_bytes += buffer.length - writableBuffer.length;
    injectProcessOutputTruncationNotice(store, streamName);
  }
  return writableBuffer.length;
}
function closeProcessOutputStore(store) {
  if (!store || store._closed) {
    return;
  }
  for (const fd of [store._stdoutFd, store._stderrFd, store._combinedFd]) {
    try {
      import_node_fs2.default.closeSync(fd);
    } catch {
    }
  }
  store._closed = true;
}
function deleteProcessOutputStore(store) {
  if (!store) {
    return;
  }
  closeProcessOutputStore(store);
  safeRemoveFile(store.stdout_path);
  safeRemoveFile(store.stderr_path);
  safeRemoveFile(store.combined_path);
}
function resolveProcessOutputMeta(processEntry, streamName = "combined") {
  const normalizedStream = String(streamName || "combined").toLowerCase();
  if (normalizedStream === "stdout") {
    return {
      stream: "stdout",
      path: processEntry.output.stdout_path,
      totalBytes: processEntry.output.stdout_bytes
    };
  }
  if (normalizedStream === "stderr") {
    return {
      stream: "stderr",
      path: processEntry.output.stderr_path,
      totalBytes: processEntry.output.stderr_bytes
    };
  }
  if (normalizedStream !== "combined") {
    throw new Error("process_output_stream_invalid");
  }
  return {
    stream: "combined",
    path: processEntry.output.combined_path,
    totalBytes: processEntry.output.combined_bytes
  };
}
function readUtf8FileSlice(filePath, offsetValue, maxBytesValue) {
  if (!import_node_fs2.default.existsSync(filePath)) {
    return {
      offset: 0,
      bytesRead: 0,
      totalBytes: 0,
      content: ""
    };
  }
  const fd = import_node_fs2.default.openSync(filePath, "r");
  try {
    const stats = import_node_fs2.default.fstatSync(fd);
    const offset = clampInteger(offsetValue, {
      fallback: 0,
      min: 0,
      max: stats.size
    });
    const maxBytes = clampInteger(maxBytesValue, {
      fallback: 65536,
      min: 0,
      max: MAX_PROCESS_OUTPUT_SLICE_BYTES
    });
    const available = Math.max(0, stats.size - offset);
    const bytesToRead = Math.min(maxBytes, available);
    if (bytesToRead === 0) {
      return {
        offset,
        bytesRead: 0,
        totalBytes: stats.size,
        content: ""
      };
    }
    const buffer = Buffer.alloc(bytesToRead);
    const bytesRead = import_node_fs2.default.readSync(fd, buffer, 0, bytesToRead, offset);
    return {
      offset,
      bytesRead,
      totalBytes: stats.size,
      content: buffer.subarray(0, bytesRead).toString("utf8")
    };
  } finally {
    try {
      import_node_fs2.default.closeSync(fd);
    } catch {
    }
  }
}
function cleanupManagedProcessEntry(processEntry, options = {}) {
  if (!processEntry) {
    return;
  }
  for (const timerKey of [
    "timeout",
    "no_output_timeout",
    "force_kill_timeout",
    "finalize_timeout"
  ]) {
    if (processEntry[timerKey]) {
      clearTimeout(processEntry[timerKey]);
      processEntry[timerKey] = null;
    }
  }
  if (!processEntry._listenersRemoved && processEntry.child) {
    if (processEntry._onStdout && processEntry.child.stdout) {
      processEntry.child.stdout.removeListener("data", processEntry._onStdout);
    }
    if (processEntry._onStderr && processEntry.child.stderr) {
      processEntry.child.stderr.removeListener("data", processEntry._onStderr);
    }
    if (processEntry._onError) {
      processEntry.child.removeListener("error", processEntry._onError);
    }
    if (processEntry._onExit) {
      processEntry.child.removeListener("exit", processEntry._onExit);
    }
    if (processEntry._onClose) {
      processEntry.child.removeListener("close", processEntry._onClose);
    }
    processEntry._listenersRemoved = true;
  }
  if (processEntry.child) {
    try {
      processEntry.child.stdin?.destroy?.();
    } catch {
    }
    try {
      processEntry.child.stdout?.destroy?.();
    } catch {
    }
    try {
      processEntry.child.stderr?.destroy?.();
    } catch {
    }
  }
  closeProcessOutputStore(processEntry.output);
  if (options.deleteOutput === true) {
    deleteProcessOutputStore(processEntry.output);
  }
}
function pruneFinishedProcesses() {
  const ttlMs = clampProcessEntryTtlMs(
    getConfig().policies?.processes?.finishedEntryTtlMs
  );
  const cutoff = Date.now() - ttlMs;
  for (const [processId, processEntry] of daemonState.processes.entries()) {
    if (processEntry.status === "running" || processEntry.status === "terminating") {
      continue;
    }
    const endedAt = Date.parse(processEntry.ended_at || "");
    if (Number.isFinite(endedAt) && endedAt < cutoff) {
      cleanupManagedProcessEntry(processEntry, { deleteOutput: true });
      daemonState.processes.delete(processId);
    }
  }
}
function ensureProcessCleanupTimer() {
  if (daemonState.processCleanupTimer) {
    return;
  }
  const ttlMs = clampProcessEntryTtlMs(
    getConfig().policies?.processes?.finishedEntryTtlMs
  );
  daemonState.processCleanupTimer = setInterval(
    pruneFinishedProcesses,
    Math.max(3e4, Math.floor(ttlMs / 6))
  );
  daemonState.processCleanupTimer.unref?.();
}
function trimFinishedProcessHistory() {
  if (daemonState.processes.size <= MAX_PROCESS_HISTORY) {
    return;
  }
  const finishedProcesses = Array.from(daemonState.processes.values()).filter(
    (item) => item.status !== "running" && item.status !== "terminating"
  ).sort(
    (a, b) => String(a.ended_at || "").localeCompare(String(b.ended_at || ""))
  );
  while (daemonState.processes.size > MAX_PROCESS_HISTORY && finishedProcesses.length > 0) {
    const stale = finishedProcesses.shift();
    if (!stale) {
      continue;
    }
    cleanupManagedProcessEntry(stale, { deleteOutput: true });
    daemonState.processes.delete(stale.id);
  }
}
function toProcessSummary(processEntry) {
  return {
    id: processEntry.id,
    run_kind: processEntry.run_kind,
    summary: processEntry.summary,
    skill_name: processEntry.skill_name ?? null,
    pid: processEntry.pid,
    command: processEntry.command,
    args: processEntry.args,
    cwd: processEntry.cwd,
    status: processEntry.status,
    exit_code: processEntry.exit_code ?? null,
    signal: processEntry.signal ?? null,
    started_at: processEntry.started_at,
    ended_at: processEntry.ended_at ?? null,
    stdout: processEntry.output?.stdout_tail || "",
    stderr: processEntry.output?.stderr_tail || "",
    combined_output: processEntry.output?.combined_tail || "",
    stdout_bytes: processEntry.output?.stdout_bytes || 0,
    stderr_bytes: processEntry.output?.stderr_bytes || 0,
    combined_bytes: processEntry.output?.combined_bytes || 0,
    stdout_path: processEntry.output?.stdout_path || null,
    stderr_path: processEntry.output?.stderr_path || null,
    combined_path: processEntry.output?.combined_path || null,
    last_output_at: processEntry.last_output_at ?? null,
    error_message: processEntry.error_message ?? null,
    timed_out: processEntry.timed_out === true,
    output_truncated: processEntry.output?.output_truncated === true,
    discarded_bytes: processEntry.output?.discarded_bytes || 0
  };
}
function toRemoteManagedProcessProgressSummary(summary) {
  return {
    id: summary.id,
    run_kind: summary.run_kind,
    summary: summary.summary,
    skill_name: summary.skill_name,
    pid: summary.pid,
    command: summary.command,
    args: summary.args,
    cwd: summary.cwd,
    status: summary.status,
    exit_code: summary.exit_code,
    signal: summary.signal,
    started_at: summary.started_at,
    ended_at: summary.ended_at,
    stdout_bytes: summary.stdout_bytes,
    stderr_bytes: summary.stderr_bytes,
    combined_bytes: summary.combined_bytes,
    stdout_path: summary.stdout_path,
    stderr_path: summary.stderr_path,
    combined_path: summary.combined_path,
    last_output_at: summary.last_output_at,
    error_message: summary.error_message,
    timed_out: summary.timed_out,
    output_truncated: summary.output_truncated,
    discarded_bytes: summary.discarded_bytes
  };
}
function buildManagedProcessProgressUpdate(processEntry) {
  const summary = toProcessSummary(processEntry);
  return {
    signature: JSON.stringify({
      status: summary.status,
      exit_code: summary.exit_code,
      signal: summary.signal,
      timed_out: summary.timed_out,
      stdout_bytes: summary.stdout_bytes,
      stderr_bytes: summary.stderr_bytes,
      combined_bytes: summary.combined_bytes,
      last_output_at: summary.last_output_at,
      error_message: summary.error_message,
      output_truncated: summary.output_truncated
    }),
    event: {
      response_payload: {
        process: toRemoteManagedProcessProgressSummary(summary),
        process_id: summary.id,
        exit_code: summary.exit_code,
        signal: summary.signal,
        timed_out: summary.timed_out,
        output_truncated: summary.output_truncated
      },
      stdout: summary.stdout,
      stderr: summary.stderr,
      error_message: summary.error_message
    }
  };
}
function getConfig() {
  const config2 = loadConfig();
  config2.device ||= {};
  config2.device.serverUrl ||= "";
  config2.device.deviceId ||= "";
  config2.device.deviceToken ||= "";
  config2.device.fingerprint ||= "";
  config2.device.fingerprintVersion ||= DEVICE_FINGERPRINT_VERSION;
  config2.device.name ||= import_node_os2.default.hostname() || "picktime-device";
  config2.device.platform ||= process.platform;
  config2.device.deviceFamily ||= process.platform === "darwin" ? "mac" : process.platform;
  config2.device.clientVersion ||= DEFAULT_CLIENT_VERSION;
  config2.device.autoConnect ??= true;
  config2.device.heartbeatIntervalMs ||= 3e4;
  config2.daemon ||= {};
  config2.daemon.port ||= defaultDaemonPort;
  config2.daemon.authToken ||= import_node_crypto2.default.randomBytes(24).toString("hex");
  config2.approvals ||= { autoApprove: false };
  const capabilityPreferences = getCapabilityPreferences(config2);
  config2.capabilities.profile = capabilityPreferences.profile;
  delete config2.capabilities.fileMode;
  config2.capabilities.enabledPacks = capabilityPreferences.enabledPacks;
  saveConfig(config2);
  return config2;
}
function getRuntimeMeta() {
  const config2 = getConfig();
  return {
    pid: process.pid,
    port: Number(config2.daemon.port || defaultDaemonPort),
    token: String(config2.daemon.authToken || ""),
    startedAt: daemonState.startedAt,
    logPath: daemonLogPath
  };
}
function persistRuntimeMeta() {
  const runtime = getRuntimeMeta();
  daemonState.currentPort = runtime.port;
  daemonState.currentToken = runtime.token;
  saveRuntime(runtime);
}
function jsonResponse(res, statusCode, body) {
  const raw = JSON.stringify(body);
  res.writeHead(statusCode, {
    "Content-Type": "application/json; charset=utf-8",
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Headers": "Content-Type, X-Picktime-Token",
    "Content-Length": Buffer.byteLength(raw)
  });
  res.end(raw);
}
function assertAuthorized(req) {
  const token = req.headers["x-picktime-token"];
  if (!token || String(token) !== daemonState.currentToken) {
    const error2 = new Error("not_authorized");
    error2.statusCode = 401;
    throw error2;
  }
}
async function buildStatus() {
  const config2 = getConfig();
  const mcpServers = listManagedMcpServers();
  const managedPython = await getManagedPythonStatus();
  const capabilityPreferences = getCapabilityPreferences(config2);
  const filesystemPolicy = config2.policies?.filesystem || {};
  const localDeviceFingerprint = await resolveLocalDeviceFingerprint();
  return {
    ok: true,
    platform: process.platform,
    pid: process.pid,
    started_at: daemonState.startedAt,
    state_dir: stateDir,
    config_path: configPath,
    runtime_path: runtimePath,
    mcp_config_path: mcpConfigPath,
    skills_dir: skillsDir,
    daemon_port: daemonState.currentPort,
    daemon_running: true,
    ws_connected: daemonState.wsConnected,
    ws_connected_at: daemonState.wsConnectedAt,
    ws_last_error: daemonState.wsLastError,
    last_heartbeat_at: daemonState.lastHeartbeatAt,
    approval_count: daemonState.approvals.size,
    recent_run_count: daemonState.runHistory.length,
    skill_count: listSkillDirs().length,
    mcp_server_count: mcpServers.length,
    capability_profile: capabilityPreferences.profile,
    enabled_capability_packs: capabilityPreferences.enabledPacks,
    recommended_skills: capabilityPreferences.recommendedSkills.map(
      (item) => item.name
    ),
    filesystem_access_mode: filesystemPolicy.mode || "standard",
    allowed_roots: [...filesystemPolicy.allowedRoots || []],
    protected_write_roots: [...filesystemPolicy.protectedWriteRoots || []],
    device_configured: Boolean(
      config2.device?.serverUrl && config2.device?.deviceId && config2.device?.deviceToken
    ),
    device: {
      configured: Boolean(
        config2.device?.serverUrl && config2.device?.deviceId && config2.device?.deviceToken
      ),
      serverUrl: config2.device?.serverUrl || "",
      deviceId: config2.device?.deviceId || "",
      fingerprint: localDeviceFingerprint?.fingerprint || config2.device?.fingerprint || "",
      fingerprintVersion: localDeviceFingerprint?.version || config2.device?.fingerprintVersion || DEVICE_FINGERPRINT_VERSION,
      name: config2.device?.name || "",
      autoConnect: config2.device?.autoConnect !== false
    },
    managed_python: managedPython
  };
}
function createRunHistoryEntry({ runId, traceId, payload, source }) {
  const entry = {
    id: generateId(),
    run_id: runId || null,
    trace_id: traceId || null,
    source: source || "remote",
    run_type: String(payload?.run_type || "unknown"),
    status: "running",
    summary: payload?.run_type === "install_skill" ? `install skill ${payload.skill_reference || payload.sourcePath || ""}`.trim() : payload?.run_type === "search_skill_catalog" ? `search skill catalog ${payload.query || ""}`.trim() : payload?.run_type === "inspect_skill" ? `inspect skill ${payload.skill_name || payload.name || ""}`.trim() : payload?.run_type === "invoke_skill" ? `invoke skill ${payload.skill_name || ""}`.trim() : payload?.run_type === "start_skill" ? `start skill ${payload.skill_name || ""}`.trim() : payload?.run_type === "get_mcp_catalog" ? `get mcp catalog ${payload.server_name || payload.name || ""}`.trim() : payload?.run_type === "write_file" ? `write file ${payload.path || ""}`.trim() : payload?.run_type === "read_file" ? `read file ${payload.path || ""}`.trim() : payload?.run_type === "watch_path" ? `watch path ${payload.path || ""}`.trim() : payload?.run_type === "list_watches" ? "list file watches" : payload?.run_type === "poll_watch_events" ? `poll watch ${payload.watch_id || ""}`.trim() : payload?.run_type === "unwatch_path" ? `unwatch ${payload.watch_id || ""}`.trim() : payload?.run_type === "start_process" ? `start process ${payload.command || ""}`.trim() : payload?.run_type === "list_processes" ? "list processes" : payload?.run_type === "get_process" ? `get process ${payload.process_id || ""}`.trim() : payload?.run_type === "stop_process" ? `stop process ${payload.process_id || ""}`.trim() : payload?.run_type === "get_clipboard" ? "read clipboard" : payload?.run_type === "set_clipboard" ? "write clipboard" : payload?.run_type === "show_notification" ? `show notification ${payload.title || ""}`.trim() : payload?.run_type === "invoke_mcp_tool" ? `mcp ${payload.server_name || ""}/${payload.tool_name || ""}`.trim() : payload?.run_type === "command" ? `command ${payload.command || ""}`.trim() : "run",
    request_payload: payload || {},
    response_payload: null,
    stdout: null,
    stderr: null,
    error_message: null,
    started_at: (/* @__PURE__ */ new Date()).toISOString(),
    completed_at: null
  };
  daemonState.runHistory.unshift(entry);
  if (daemonState.runHistory.length > MAX_RUN_HISTORY) {
    daemonState.runHistory.length = MAX_RUN_HISTORY;
  }
  return entry;
}
function completeRunHistoryEntry(entry, result) {
  if (!entry) {
    return;
  }
  entry.status = result?.status || (result?.ok === false ? "failed" : "completed");
  entry.response_payload = result?.response_payload || null;
  entry.stdout = result?.stdout || null;
  entry.stderr = result?.stderr || null;
  entry.error_message = result?.error_message || null;
  entry.completed_at = (/* @__PURE__ */ new Date()).toISOString();
}
function updateRunHistoryEntry(entry, patch) {
  if (!entry) {
    return;
  }
  Object.assign(entry, patch || {});
}
function listRunHistory(limit = 20) {
  return daemonState.runHistory.slice(0, Math.max(1, Number(limit || 20)));
}
function getSkillVenvInfo(skillName) {
  const venvPath = import_node_path4.default.join(venvsDir, skillName);
  const pythonExecutable = process.platform === "win32" ? import_node_path4.default.join(venvPath, "Scripts", "python.exe") : import_node_path4.default.join(venvPath, "bin", "python");
  return {
    venvPath,
    pythonExecutable,
    exists: import_node_fs2.default.existsSync(pythonExecutable)
  };
}
var NON_EXECUTABLE_SKILL_DIRS = /* @__PURE__ */ new Set([
  "bin",
  "dist",
  "node_modules",
  "scripts",
  "src",
  "target",
  "venv",
  ".venv"
]);
var NON_EXECUTABLE_SKILL_FILES = /* @__PURE__ */ new Set([
  "cargo.toml",
  "go.mod",
  "main.py",
  "main.js",
  "main.mjs",
  "main.cjs",
  "main.ts",
  "package.json",
  "pom.xml",
  "pyproject.toml",
  "requirements.txt",
  "runner.py",
  "runner.js",
  "runner.mjs",
  "runner.cjs",
  "runner.ts",
  "setup.py"
]);
var NON_EXECUTABLE_SKILL_EXTENSIONS = /* @__PURE__ */ new Set([
  ".bat",
  ".cjs",
  ".cmd",
  ".dll",
  ".dylib",
  ".exe",
  ".jar",
  ".js",
  ".mjs",
  ".mts",
  ".ps1",
  ".py",
  ".sh",
  ".so",
  ".ts",
  ".tsx"
]);
function inferKnowledgeSkillRuntime(skillRoot) {
  if (!import_node_fs2.default.existsSync(import_node_path4.default.join(skillRoot, "SKILL.md"))) {
    return null;
  }
  let hasSkillInstructions = false;
  for (const entry of import_node_fs2.default.readdirSync(skillRoot, { withFileTypes: true })) {
    const normalizedName = entry.name.toLowerCase();
    if (entry.isDirectory()) {
      if (NON_EXECUTABLE_SKILL_DIRS.has(normalizedName)) {
        return null;
      }
      continue;
    }
    if (normalizedName === "skill.md") {
      hasSkillInstructions = true;
      continue;
    }
    if (NON_EXECUTABLE_SKILL_FILES.has(normalizedName)) {
      return null;
    }
    if (NON_EXECUTABLE_SKILL_EXTENSIONS.has(import_node_path4.default.extname(normalizedName))) {
      return null;
    }
  }
  if (!hasSkillInstructions) {
    return null;
  }
  return {
    kind: "knowledge",
    readiness: "ready",
    hasRunner: false,
    requiresSetup: false,
    environment: "instructions",
    executable: null,
    script: null,
    requirements: null,
    venvPath: null
  };
}
function buildSkillRuntimeSummary(skillEntry) {
  const storedRuntime = isPlainObject5(skillEntry.runtime) ? skillEntry.runtime : null;
  const configuredRunner = skillEntry.runner || readSkillRunner(skillEntry.name);
  const inferredPythonRunner = inferSkillRunnerFromFiles(skillEntry.path);
  const inferredKnowledgeRuntime = inferKnowledgeSkillRuntime(skillEntry.path);
  const venvInfo = getSkillVenvInfo(skillEntry.name);
  if (storedRuntime?.kind === "python" || inferredPythonRunner?.script) {
    const requirements = pickFirstString(storedRuntime?.requirements) || inferredPythonRunner?.requirements || null;
    const script = pickFirstString(storedRuntime?.script) || inferredPythonRunner?.script || null;
    const executable = pickFirstString(storedRuntime?.executable) || configuredRunner?.command || null;
    const environment = pickFirstString(storedRuntime?.environment) || (venvInfo.exists ? "venv" : executable ? "system" : null);
    let readiness = pickFirstString(storedRuntime?.readiness) || "installed";
    if (requirements) {
      readiness = environment === "venv" && venvInfo.exists ? "ready" : "needs_setup";
    } else if (executable || script) {
      readiness = "ready";
    }
    return {
      kind: "python",
      readiness,
      hasRunner: Boolean(executable || script),
      requiresSetup: Boolean(requirements),
      environment,
      executable,
      script,
      requirements,
      venvPath: venvInfo.exists || environment === "venv" ? venvInfo.venvPath : null,
      installedAt: skillEntry.installed_at || null,
      preparedAt: pickFirstString(storedRuntime?.preparedAt) || null,
      lastError: pickFirstString(storedRuntime?.lastError) || null,
      managedPythonError: pickFirstString(storedRuntime?.managedPythonError) || null
    };
  }
  if (configuredRunner?.command) {
    return {
      kind: "command",
      readiness: "ready",
      hasRunner: true,
      requiresSetup: false,
      environment: pickFirstString(storedRuntime?.environment) || "custom",
      executable: configuredRunner.command,
      script: null,
      requirements: null,
      venvPath: null,
      installedAt: skillEntry.installed_at || null,
      preparedAt: pickFirstString(storedRuntime?.preparedAt) || skillEntry.installed_at || null,
      lastError: pickFirstString(storedRuntime?.lastError) || null,
      managedPythonError: pickFirstString(storedRuntime?.managedPythonError) || null
    };
  }
  if (storedRuntime?.kind === "knowledge" || inferredKnowledgeRuntime) {
    return {
      kind: "knowledge",
      readiness: pickFirstString(storedRuntime?.readiness) || inferredKnowledgeRuntime?.readiness || "ready",
      hasRunner: false,
      requiresSetup: false,
      environment: pickFirstString(storedRuntime?.environment) || inferredKnowledgeRuntime?.environment || "instructions",
      executable: null,
      script: null,
      requirements: null,
      venvPath: null,
      installedAt: skillEntry.installed_at || null,
      preparedAt: pickFirstString(storedRuntime?.preparedAt) || skillEntry.installed_at || null,
      lastError: pickFirstString(storedRuntime?.lastError) || null,
      managedPythonError: pickFirstString(storedRuntime?.managedPythonError) || null
    };
  }
  return {
    kind: pickFirstString(storedRuntime?.kind) || "unknown",
    readiness: pickFirstString(storedRuntime?.readiness) || "installed",
    hasRunner: false,
    requiresSetup: Boolean(storedRuntime?.requiresSetup),
    environment: pickFirstString(storedRuntime?.environment) || null,
    executable: pickFirstString(storedRuntime?.executable) || null,
    script: pickFirstString(storedRuntime?.script) || null,
    requirements: pickFirstString(storedRuntime?.requirements) || null,
    venvPath: venvInfo.exists ? venvInfo.venvPath : null,
    installedAt: skillEntry.installed_at || null,
    preparedAt: pickFirstString(storedRuntime?.preparedAt) || null,
    lastError: pickFirstString(storedRuntime?.lastError) || null,
    managedPythonError: pickFirstString(storedRuntime?.managedPythonError) || null
  };
}
function enrichSkillEntry(skillEntry) {
  const runner = skillEntry.runner || readSkillRunner(skillEntry.name);
  return {
    ...skillEntry,
    runner,
    runtime: buildSkillRuntimeSummary({
      ...skillEntry,
      runner
    })
  };
}
function listSkills() {
  return listSkillDirs().map((name) => enrichSkillEntry(getSkillEntry(name)));
}
function readSkillDocument(skillMdPath) {
  if (!import_node_fs2.default.existsSync(skillMdPath)) {
    return null;
  }
  try {
    const result = readLocalFile({
      path: skillMdPath,
      encoding: "utf8"
    });
    return {
      ok: result.ok !== false,
      path: result.path,
      encoding: result.encoding,
      content: typeof result.content === "string" ? result.content : null,
      bytes: Number.isFinite(result.bytes) ? result.bytes : null,
      modified_at: result.modified_at || null,
      mime_type: result.mime_type || null,
      media_kind: result.media_kind || null,
      error_message: result.error_message || null
    };
  } catch (error2) {
    return {
      ok: false,
      path: skillMdPath,
      encoding: "utf8",
      content: null,
      bytes: null,
      modified_at: null,
      mime_type: "text/markdown",
      media_kind: "text",
      error_message: error2 instanceof Error ? error2.message : String(error2)
    };
  }
}
function inspectSkill(name) {
  if (!name) {
    throw new Error("skill_name_required");
  }
  const entry = enrichSkillEntry(getSkillEntry(name));
  const skillMdPath = import_node_path4.default.join(entry.path, "SKILL.md");
  const skillDocument = entry.runtime?.kind === "knowledge" || entry.runtime?.hasRunner === false ? readSkillDocument(skillMdPath) : null;
  return {
    ...entry,
    exists: import_node_fs2.default.existsSync(entry.path),
    skillMdPath,
    skillDocument
  };
}
function inferSkillRunnerFromFiles(skillRoot) {
  const scriptsDir = import_node_path4.default.join(skillRoot, "scripts");
  if (!import_node_fs2.default.existsSync(scriptsDir)) {
    return null;
  }
  const pythonScripts = import_node_fs2.default.readdirSync(scriptsDir, { withFileTypes: true }).filter((entry) => entry.isFile() && entry.name.endsWith(".py")).map((entry) => entry.name).sort();
  if (pythonScripts.length === 1) {
    return {
      type: "python",
      script: import_node_path4.default.join("scripts", pythonScripts[0]),
      requirements: import_node_fs2.default.existsSync(import_node_path4.default.join(scriptsDir, "requirements.txt")) ? import_node_path4.default.join("scripts", "requirements.txt") : null
    };
  }
  return null;
}
function getDefaultPythonCommand() {
  return process.env.PYTHON || "python";
}
function getEnvValue(name) {
  return process.env[name] || process.env[name.toLowerCase()] || null;
}
function sanitizeUrlForDisplay(value) {
  const normalized = String(value || "").trim();
  if (!normalized) {
    return null;
  }
  try {
    const parsed = new URL(normalized);
    if (parsed.username) {
      parsed.username = "***";
    }
    if (parsed.password) {
      parsed.password = "***";
    }
    return parsed.toString();
  } catch {
    return normalized.replace(/\/\/([^/@:]+):([^@]+)@/, "//***:***@");
  }
}
var DEVICE_FINGERPRINT_PLACEHOLDERS = /* @__PURE__ */ new Set([
  "",
  "default string",
  "none",
  "not available",
  "n/a",
  "system serial number",
  "to be filled by o.e.m.",
  "to be filled by oem",
  "unknown"
]);
function normalizeDeviceFingerprintComponent(value) {
  const normalized = String(value ?? "").replace(/\0/g, "").trim().toLowerCase().replace(/\s+/g, " ");
  if (!normalized || DEVICE_FINGERPRINT_PLACEHOLDERS.has(normalized)) {
    return null;
  }
  return normalized;
}
function uniqueDeviceFingerprintComponents(values) {
  const seen = /* @__PURE__ */ new Set();
  const components = [];
  for (const value of values) {
    const normalized = normalizeDeviceFingerprintComponent(value);
    if (!normalized || seen.has(normalized)) {
      continue;
    }
    seen.add(normalized);
    components.push(normalized);
  }
  return components;
}
function parseDeviceFingerprintTestComponents() {
  const raw = process.env.PICKTIME_DEVICE_FINGERPRINT_TEST_COMPONENTS;
  if (!raw || !raw.trim()) {
    return [];
  }
  try {
    const parsed = JSON.parse(raw);
    if (Array.isArray(parsed)) {
      return parsed.map((item) => String(item));
    }
  } catch {
  }
  return raw.split(/\r?\n|\|\|/).map((item) => item.trim()).filter(Boolean);
}
function buildDeviceFingerprintPayload(components) {
  const normalizedComponents = uniqueDeviceFingerprintComponents(components);
  if (!normalizedComponents.length) {
    throw new Error("device_fingerprint_sources_unavailable");
  }
  return {
    fingerprint: import_node_crypto2.default.createHash("sha256").update(
      JSON.stringify({
        version: DEVICE_FINGERPRINT_VERSION,
        platform: process.platform,
        components: normalizedComponents
      })
    ).digest("hex"),
    version: DEVICE_FINGERPRINT_VERSION
  };
}
async function collectWindowsDeviceFingerprintComponents() {
  const script = `
$ErrorActionPreference = 'SilentlyContinue'
$items = New-Object System.Collections.Generic.List[string]
$machineGuid = (Get-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\Cryptography' -Name MachineGuid).MachineGuid
if ($machineGuid) { $items.Add("machine-guid=$machineGuid") }
$systemUuid = (Get-CimInstance -ClassName Win32_ComputerSystemProduct).UUID
if ($systemUuid) { $items.Add("system-uuid=$systemUuid") }
$biosSerial = (Get-CimInstance -ClassName Win32_BIOS).SerialNumber
if ($biosSerial) { $items.Add("bios-serial=$biosSerial") }
$boardSerial = (Get-CimInstance -ClassName Win32_BaseBoard).SerialNumber
if ($boardSerial) { $items.Add("board-serial=$boardSerial") }
$items -join [Environment]::NewLine
`;
  try {
    const result = await executeSimpleCommand(
      "powershell",
      ["-NoProfile", "-Command", script],
      { timeoutMs: DEVICE_FINGERPRINT_COMMAND_TIMEOUT_MS }
    );
    return result.stdout.split(/\r?\n/).map((item) => item.trim()).filter(Boolean);
  } catch {
    return [];
  }
}
async function collectMacDeviceFingerprintComponents() {
  const components = [];
  try {
    const result = await executeSimpleCommand(
      "ioreg",
      ["-rd1", "-c", "IOPlatformExpertDevice"],
      { timeoutMs: DEVICE_FINGERPRINT_COMMAND_TIMEOUT_MS }
    );
    const platformUuid = result.stdout.match(
      /"IOPlatformUUID"\s*=\s*"([^"]+)"/i
    )?.[1];
    const platformSerial = result.stdout.match(
      /"IOPlatformSerialNumber"\s*=\s*"([^"]+)"/i
    )?.[1];
    if (platformUuid) {
      components.push(`platform-uuid=${platformUuid}`);
    }
    if (platformSerial) {
      components.push(`serial-number=${platformSerial}`);
    }
  } catch {
  }
  if (components.length > 0) {
    return components;
  }
  try {
    const result = await executeSimpleCommand(
      "system_profiler",
      ["SPHardwareDataType"],
      { timeoutMs: DEVICE_FINGERPRINT_COMMAND_TIMEOUT_MS }
    );
    const hardwareUuid = result.stdout.match(/Hardware UUID:\s*(.+)$/im)?.[1];
    const serialNumber = result.stdout.match(
      /Serial Number \(system\):\s*(.+)$/im
    )?.[1];
    if (hardwareUuid) {
      components.push(`hardware-uuid=${hardwareUuid}`);
    }
    if (serialNumber) {
      components.push(`serial-number=${serialNumber}`);
    }
  } catch {
  }
  return components;
}
async function collectLinuxDeviceFingerprintComponents() {
  const candidates = [
    ["product-uuid", "/sys/class/dmi/id/product_uuid"],
    ["board-serial", "/sys/class/dmi/id/board_serial"],
    ["machine-id", "/etc/machine-id"],
    ["dbus-machine-id", "/var/lib/dbus/machine-id"]
  ];
  return candidates.map(([label, filePath]) => {
    try {
      if (!import_node_fs2.default.existsSync(filePath)) {
        return null;
      }
      const value = import_node_fs2.default.readFileSync(filePath, "utf8").trim();
      return value ? `${label}=${value}` : null;
    } catch {
      return null;
    }
  }).filter(Boolean);
}
async function collectLocalDeviceFingerprintComponents() {
  const testComponents = parseDeviceFingerprintTestComponents();
  if (testComponents.length > 0) {
    return testComponents;
  }
  if (process.platform === "win32") {
    return collectWindowsDeviceFingerprintComponents();
  }
  if (process.platform === "darwin") {
    return collectMacDeviceFingerprintComponents();
  }
  return collectLinuxDeviceFingerprintComponents();
}
function readStoredLocalDeviceFingerprint(config2 = getConfig()) {
  const fingerprint = String(config2.device?.fingerprint || "").trim().toLowerCase();
  const version2 = String(config2.device?.fingerprintVersion || "").trim();
  if (!fingerprint || version2 !== DEVICE_FINGERPRINT_VERSION) {
    return null;
  }
  return {
    fingerprint,
    version: version2
  };
}
async function resolveLocalDeviceFingerprint(options = {}) {
  const { persist = true, required: required2 = false } = options;
  if (daemonState.deviceFingerprint?.fingerprint && daemonState.deviceFingerprint?.version === DEVICE_FINGERPRINT_VERSION) {
    return daemonState.deviceFingerprint;
  }
  const stored = readStoredLocalDeviceFingerprint();
  if (stored) {
    daemonState.deviceFingerprint = stored;
    daemonState.deviceFingerprintError = null;
    return stored;
  }
  if (!daemonState.deviceFingerprintPromise) {
    daemonState.deviceFingerprintPromise = (async () => {
      const fingerprintState2 = buildDeviceFingerprintPayload(
        await collectLocalDeviceFingerprintComponents()
      );
      daemonState.deviceFingerprint = fingerprintState2;
      daemonState.deviceFingerprintError = null;
      if (persist) {
        const config2 = getConfig();
        config2.device.fingerprint = fingerprintState2.fingerprint;
        config2.device.fingerprintVersion = fingerprintState2.version;
        saveConfig(config2);
      }
      appendAudit("device_fingerprint_ready", {
        version: fingerprintState2.version
      });
      return fingerprintState2;
    })().catch((error2) => {
      const message = error2 instanceof Error ? error2.message : String(error2);
      if (daemonState.deviceFingerprintError !== message) {
        appendDaemonLog("warn", "device_fingerprint_unavailable", {
          error: message
        });
        daemonState.deviceFingerprintError = message;
      }
      daemonState.deviceFingerprint = null;
      return null;
    }).finally(() => {
      daemonState.deviceFingerprintPromise = null;
    });
  }
  const fingerprintState = await daemonState.deviceFingerprintPromise;
  if (!fingerprintState && required2) {
    throw new Error(
      daemonState.deviceFingerprintError || "device_fingerprint_unavailable"
    );
  }
  return fingerprintState;
}
function getManagedPythonProxyConfig() {
  const candidates = [
    "PICKTIME_HTTPS_PROXY",
    "HTTPS_PROXY",
    "PICKTIME_HTTP_PROXY",
    "HTTP_PROXY",
    "ALL_PROXY"
  ];
  for (const name of candidates) {
    const value = String(getEnvValue(name) || "").trim();
    if (!value) {
      continue;
    }
    return {
      url: value,
      source: name,
      display_url: sanitizeUrlForDisplay(value)
    };
  }
  return {
    url: null,
    source: null,
    display_url: null
  };
}
function expandUserPath(candidate) {
  const normalized = String(candidate || "").trim();
  if (normalized === "~") {
    return import_node_os2.default.homedir();
  }
  if (normalized.startsWith("~/") || normalized.startsWith("~\\")) {
    return import_node_path4.default.join(import_node_os2.default.homedir(), normalized.slice(2));
  }
  return normalized;
}
function isLikelyLocalPath(reference) {
  const normalized = String(reference || "").trim();
  if (!normalized) {
    return false;
  }
  if (/^[a-zA-Z]:[\\/]/.test(normalized)) {
    return true;
  }
  if (/^(~[\\/]|\.{1,2}[\\/])/.test(normalized)) {
    return true;
  }
  return import_node_fs2.default.existsSync(import_node_path4.default.resolve(expandUserPath(normalized)));
}
function isLikelyClawHubSlug(value) {
  return /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(String(value || "").trim());
}
function slugifyClawHubReference(value) {
  return String(value || "").normalize("NFKD").replace(/[\u0300-\u036f]/g, "").toLowerCase().replace(/&/g, " and ").replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").replace(/-{2,}/g, "-");
}
function uniqueNonEmptyStrings(values) {
  const out = [];
  const seen = /* @__PURE__ */ new Set();
  for (const item of values || []) {
    const normalized = String(item || "").trim();
    if (!normalized || seen.has(normalized)) {
      continue;
    }
    seen.add(normalized);
    out.push(normalized);
  }
  return out;
}
function extractClawHubSlugFromUrl(value) {
  try {
    const parsed = new URL(String(value || "").trim());
    if (!/(^|\.)clawhub\.ai$/i.test(parsed.hostname)) {
      return null;
    }
    const slugFromQuery = String(parsed.searchParams.get("slug") || "").trim();
    if (slugFromQuery) {
      return decodeURIComponent(slugFromQuery);
    }
    const segments = parsed.pathname.split("/").filter(Boolean).map((segment) => decodeURIComponent(segment).trim()).filter(Boolean);
    const skillsIndex = segments.findIndex(
      (segment) => segment.toLowerCase() === "skills"
    );
    if (skillsIndex >= 0 && segments[skillsIndex + 1]) {
      return segments[skillsIndex + 1];
    }
    return segments.at(-1) || null;
  } catch {
    return null;
  }
}
function resolveSkillInstallReference(reference, options = {}) {
  const rawReference = String(reference || "").trim();
  if (!rawReference) {
    throw new Error("skill_reference_required");
  }
  const clawHubCandidate = rawReference.toLowerCase().startsWith("clawhub:") ? rawReference.slice("clawhub:".length).trim() : rawReference;
  if (/^https?:\/\//i.test(rawReference)) {
    const urlSlug = extractClawHubSlugFromUrl(rawReference);
    if (!urlSlug) {
      throw new Error("skill_url_provider_not_supported");
    }
    const slugified2 = slugifyClawHubReference(urlSlug);
    return {
      provider: "clawhub",
      rawReference,
      displayName: slugified2 || urlSlug,
      slugCandidates: uniqueNonEmptyStrings(
        isLikelyClawHubSlug(urlSlug) ? [urlSlug, slugified2] : [slugified2, urlSlug]
      )
    };
  }
  if (/^https?:\/\//i.test(clawHubCandidate)) {
    const urlSlug = extractClawHubSlugFromUrl(clawHubCandidate);
    if (!urlSlug) {
      throw new Error("skill_url_provider_not_supported");
    }
    const slugified2 = slugifyClawHubReference(urlSlug);
    return {
      provider: "clawhub",
      rawReference,
      displayName: slugified2 || urlSlug,
      slugCandidates: uniqueNonEmptyStrings(
        isLikelyClawHubSlug(urlSlug) ? [urlSlug, slugified2] : [slugified2, urlSlug]
      )
    };
  }
  if (isLikelyLocalPath(rawReference)) {
    if (options.allowLocalPath === false) {
      throw new Error("local_skill_path_not_supported_for_remote_install");
    }
    const sourcePath = import_node_path4.default.resolve(expandUserPath(rawReference));
    return {
      provider: "local_path",
      rawReference,
      displayName: import_node_path4.default.basename(sourcePath),
      sourcePath
    };
  }
  const slugified = slugifyClawHubReference(clawHubCandidate);
  const slugCandidates = uniqueNonEmptyStrings(
    isLikelyClawHubSlug(clawHubCandidate) ? [clawHubCandidate, slugified] : [slugified, clawHubCandidate]
  );
  if (slugCandidates.length === 0) {
    throw new Error("clawhub_skill_reference_invalid");
  }
  return {
    provider: "clawhub",
    rawReference,
    displayName: slugCandidates[0],
    slugCandidates
  };
}
function buildClawHubSkillMetadataUrl(slug) {
  return `${String(CLAWHUB_SKILL_API_BASE_URL).replace(/\/+$/, "")}/${encodeURIComponent(slug)}`;
}
function buildClawHubSkillDownloadUrl(slug) {
  const url2 = new URL(CLAWHUB_SKILL_DOWNLOAD_BASE_URL);
  url2.searchParams.set("slug", slug);
  return url2.toString();
}
function isCommandUnavailableError(error2) {
  const message = error2 instanceof Error ? error2.message : String(error2);
  return /ENOENT|not recognized|executable file not found|spawn .* ENOENT/i.test(
    message
  );
}
function isRetryableHttpStatus(statusCode) {
  const status = Number(statusCode);
  return status === 408 || status === 425 || status === 429 || status >= 500;
}
function isHttpNotFoundError(error2) {
  const message = error2 instanceof Error ? error2.message : String(error2);
  return /(?:fetch|download)_failed:404:/i.test(message);
}
function isRetryableNetworkError(error2) {
  const message = error2 instanceof Error ? error2.message : String(error2);
  return /command_timeout|timed out|ETIMEDOUT|ECONNRESET|ECONNREFUSED|EAI_AGAIN|ENOTFOUND|fetch failed|socket hang up|Could not resolve host|Failed to connect|Proxy CONNECT aborted|TLS|SSL/i.test(
    message
  );
}
async function sleep(ms) {
  await new Promise((resolve) => setTimeout(resolve, ms));
}
function appendCurlHeaders(args, headers = {}) {
  for (const [name, value] of Object.entries(headers)) {
    if (value === void 0 || value === null || value === "") {
      continue;
    }
    args.push("-H", `${name}: ${value}`);
  }
}
async function executeCurl(args, options = {}) {
  const commands = process.platform === "win32" ? ["curl.exe", "curl"] : ["curl"];
  let lastError = null;
  for (const command of commands) {
    try {
      return await executeSimpleCommand(command, args, {
        timeoutMs: options.timeoutMs || MANAGED_PYTHON_DOWNLOAD_TIMEOUT_MS
      });
    } catch (error2) {
      lastError = error2;
      if (!isCommandUnavailableError(error2)) {
        throw error2;
      }
    }
  }
  throw lastError || new Error("curl_not_available");
}
async function fetchTextWithCurl(url2, options = {}) {
  const attempts = Math.max(
    1,
    Number(options.attempts) || MANAGED_PYTHON_DOWNLOAD_ATTEMPTS
  );
  const timeoutMs = Math.max(
    1e3,
    Number(options.timeoutMs) || MANAGED_PYTHON_REQUEST_TIMEOUT_MS
  );
  const proxy = getManagedPythonProxyConfig();
  const args = [
    "-L",
    "-f",
    "-sS",
    "--connect-timeout",
    "20",
    "--max-time",
    String(Math.max(30, Math.ceil(timeoutMs / 1e3))),
    "--retry",
    String(Math.max(0, attempts - 1)),
    "--retry-delay",
    "1",
    "--retry-all-errors"
  ];
  appendCurlHeaders(args, options.headers || {});
  if (proxy.url) {
    args.push("--proxy", proxy.url);
  }
  args.push(url2);
  const result = await executeCurl(args, { timeoutMs });
  return result.stdout;
}
async function fetchTextWithFetch(url2, options = {}) {
  const attempts = Math.max(
    1,
    Number(options.attempts) || MANAGED_PYTHON_DOWNLOAD_ATTEMPTS
  );
  const timeoutMs = Math.max(
    1e3,
    Number(options.timeoutMs) || MANAGED_PYTHON_REQUEST_TIMEOUT_MS
  );
  let lastError = null;
  for (let attempt = 1; attempt <= attempts; attempt += 1) {
    const controller = new AbortController();
    const timeout = setTimeout(() => controller.abort(), timeoutMs);
    try {
      const response = await fetch(url2, {
        ...options,
        signal: controller.signal
      });
      if (!response.ok) {
        const error2 = new Error(`fetch_failed:${response.status}:${url2}`);
        error2.statusCode = response.status;
        throw error2;
      }
      return await response.text();
    } catch (error2) {
      lastError = error2;
      const statusCode = error2?.statusCode;
      if (attempt >= attempts || !isRetryableHttpStatus(statusCode) && !isRetryableNetworkError(error2)) {
        throw error2;
      }
      await sleep(Math.min(1e3 * attempt, 3e3));
    } finally {
      clearTimeout(timeout);
    }
  }
  throw lastError || new Error(`fetch_failed:unknown:${url2}`);
}
async function fetchText(url2, options = {}) {
  const proxy = getManagedPythonProxyConfig();
  if (proxy.url) {
    return fetchTextWithCurl(url2, options);
  }
  try {
    return await fetchTextWithCurl(url2, options);
  } catch (error2) {
    if (!isCommandUnavailableError(error2)) {
      throw error2;
    }
    return fetchTextWithFetch(url2, options);
  }
}
function createManagedPythonError(code, userMessage, hint, details) {
  const error2 = new Error([userMessage, hint].filter(Boolean).join(" "));
  error2.code = code;
  error2.userMessage = userMessage;
  error2.hint = hint || null;
  error2.details = details || null;
  error2.isManagedPythonError = true;
  return error2;
}
function normalizeManagedPythonInstallError(error2, context = {}) {
  if (error2?.isManagedPythonError) {
    return error2;
  }
  const rawMessage = error2 instanceof Error ? error2.message : String(error2);
  const proxySentence = context.proxy?.display_url ? `\u5F53\u524D\u4EE3\u7406\u4E3A ${context.proxy.display_url}\u3002` : "\u5F53\u524D\u6CA1\u6709\u68C0\u6D4B\u5230\u4EE3\u7406\u914D\u7F6E\u3002";
  const targetSentence = context.targetTriple ? `\u5F53\u524D\u76EE\u6807\u5E73\u53F0\u662F ${context.targetTriple}\u3002` : "";
  let code = "managed_python_install_failed";
  let userMessage = "\u53D7\u7BA1 Python \u5B89\u88C5\u5931\u8D25\u3002";
  let hint = "\u8BF7\u7A0D\u540E\u91CD\u8BD5\uFF1B\u5982\u679C\u5F53\u524D\u7F51\u7EDC\u9700\u8981\u4EE3\u7406\uFF0C\u8BF7\u5148\u8BBE\u7F6E PICKTIME_HTTPS_PROXY \u6216 HTTPS_PROXY\u3002";
  if (rawMessage.includes("managed_python_platform_unsupported")) {
    code = "managed_python_platform_unsupported";
    userMessage = "\u5F53\u524D\u5E73\u53F0\u6682\u4E0D\u652F\u6301\u81EA\u52A8\u5B89\u88C5\u53D7\u7BA1 Python\u3002";
    hint = targetSentence || "\u8BF7\u5148\u624B\u52A8\u51C6\u5907 Python\uFF0C\u518D\u901A\u8FC7 PICKTIME_PYTHON_PATH \u6307\u5411\u5B83\u3002";
  } else if (rawMessage.includes("managed_python_asset_not_found")) {
    code = "managed_python_asset_not_found";
    userMessage = "\u6CA1\u6709\u627E\u5230\u5339\u914D\u5F53\u524D\u7CFB\u7EDF\u67B6\u6784\u7684\u53D7\u7BA1 Python \u5B89\u88C5\u5305\u3002";
    hint = `${targetSentence} \u53EF\u4EE5\u7A0D\u540E\u91CD\u8BD5\uFF0C\u6216\u5148\u7528 PICKTIME_PYTHON_PATH \u6307\u5411\u73B0\u6210 Python\u3002`.trim();
  } else if (/407|proxy|Proxy CONNECT|tunnel/i.test(rawMessage) && context.proxy?.display_url) {
    code = "managed_python_proxy_failed";
    userMessage = "\u53D7\u7BA1 Python \u65E0\u6CD5\u901A\u8FC7\u5F53\u524D\u4EE3\u7406\u4E0B\u8F7D\u3002";
    hint = `${proxySentence} \u8BF7\u786E\u8BA4\u4EE3\u7406\u53EF\u8BBF\u95EE github.com \u548C release-assets.githubusercontent.com\u3002`;
  } else if (/ENOTFOUND|EAI_AGAIN|Could not resolve host|Could not resolve proxy/i.test(
    rawMessage
  )) {
    code = "managed_python_dns_failed";
    userMessage = "\u65E0\u6CD5\u89E3\u6790\u53D7\u7BA1 Python \u7684\u4E0B\u8F7D\u5730\u5740\u6216\u4EE3\u7406\u5730\u5740\u3002";
    hint = `${proxySentence} \u8BF7\u68C0\u67E5 DNS\u3001\u7F51\u7EDC\u8FDE\u901A\u6027\uFF0C\u6216\u6539\u7528\u53EF\u8BBF\u95EE GitHub \u7684\u7F51\u7EDC\u540E\u91CD\u8BD5\u3002`;
  } else if (/command_timeout|timed out|ETIMEDOUT|Failed to connect|ECONNRESET|ECONNREFUSED/i.test(
    rawMessage
  )) {
    code = "managed_python_network_timeout";
    userMessage = "\u4E0B\u8F7D\u53D7\u7BA1 Python \u8D85\u65F6\u6216\u8FDE\u63A5\u88AB\u4E2D\u65AD\u3002";
    hint = `${proxySentence} \u5982\u679C\u516C\u53F8\u7F51\u7EDC\u8981\u6C42\u4EE3\u7406\uFF0C\u8BF7\u5148\u8BBE\u7F6E PICKTIME_HTTPS_PROXY \u6216 HTTPS_PROXY\uFF0C\u7136\u540E\u91CD\u8BD5\u3002`;
  } else if (/fetch_failed:401|download_failed:401|fetch_failed:403|download_failed:403/i.test(
    rawMessage
  )) {
    code = "managed_python_download_denied";
    userMessage = "\u53D7\u7BA1 Python \u4E0B\u8F7D\u88AB\u8FDC\u7AEF\u62D2\u7EDD\u3002";
    hint = `${proxySentence} \u8BF7\u786E\u8BA4\u5F53\u524D\u7F51\u7EDC\u6CA1\u6709\u62E6\u622A GitHub \u4E0B\u8F7D\u8BF7\u6C42\u3002`;
  } else if (/fetch_failed:429|download_failed:429|too many requests/i.test(rawMessage)) {
    code = "managed_python_rate_limited";
    userMessage = "\u53D7\u7BA1 Python \u4E0B\u8F7D\u88AB\u9650\u6D41\u4E86\u3002";
    hint = "\u7A0D\u540E\u518D\u8BD5\u901A\u5E38\u5C31\u80FD\u6062\u590D\uFF1B\u5982\u679C\u8D70\u5171\u4EAB\u51FA\u53E3\u4EE3\u7406\uFF0C\u4E5F\u8BF7\u786E\u8BA4\u4EE3\u7406\u6CA1\u6709\u89E6\u53D1\u4E0A\u6E38\u9650\u6D41\u3002";
  } else if (/fetch_failed:5\d\d|download_failed:5\d\d/i.test(rawMessage)) {
    code = "managed_python_remote_failed";
    userMessage = "\u53D7\u7BA1 Python \u4E0B\u8F7D\u6E90\u6682\u65F6\u4E0D\u53EF\u7528\u3002";
    hint = "\u5DF2\u7ECF\u81EA\u52A8\u91CD\u8BD5\u8FC7\u51E0\u6B21\u4E86\uFF1B\u53EF\u4EE5\u7A0D\u540E\u518D\u8BD5\uFF0C\u6216\u5148\u4E34\u65F6\u4F7F\u7528\u7CFB\u7EDF Python\u3002";
  } else if (/extract|tar|gzip|archive/i.test(rawMessage)) {
    code = "managed_python_extract_failed";
    userMessage = "\u53D7\u7BA1 Python \u5DF2\u4E0B\u8F7D\uFF0C\u4F46\u89E3\u538B\u5931\u8D25\u3002";
    hint = "\u8BF7\u786E\u8BA4\u78C1\u76D8\u7A7A\u95F4\u8DB3\u591F\uFF0C\u5E76\u5220\u9664 ~/.picktime/runtimes/python \u540E\u91CD\u8BD5\u3002";
  } else if (/ensurepip|pip/i.test(rawMessage)) {
    code = "managed_python_packaging_failed";
    userMessage = "\u53D7\u7BA1 Python \u5DF2\u4E0B\u8F7D\uFF0C\u4F46 pip \u521D\u59CB\u5316\u5931\u8D25\u3002";
    hint = "\u53EF\u4EE5\u76F4\u63A5\u91CD\u8BD5\uFF1B\u5982\u679C\u4ECD\u5931\u8D25\uFF0C\u8BF7\u5220\u9664 ~/.picktime/runtimes/python \u540E\u518D\u8BD5\u4E00\u6B21\u3002";
  } else if (isCommandUnavailableError(error2) && context.proxy?.display_url) {
    code = "managed_python_proxy_tool_unavailable";
    userMessage = "\u5F53\u524D\u73AF\u5883\u9700\u8981\u901A\u8FC7\u4EE3\u7406\u4E0B\u8F7D\uFF0C\u4F46\u7CFB\u7EDF\u6CA1\u6709\u53EF\u7528\u7684 curl\u3002";
    hint = "\u8BF7\u5B89\u88C5 curl\uFF0C\u6216\u5207\u6362\u5230\u53EF\u4EE5\u76F4\u8FDE GitHub \u7684\u7F51\u7EDC\u540E\u91CD\u8BD5\u3002";
  }
  return createManagedPythonError(
    code,
    userMessage,
    hint,
    rawMessage.slice(0, 1e3)
  );
}
function toManagedPythonErrorRecord(error2) {
  return {
    message: error2?.userMessage || error2?.message || String(error2),
    code: error2?.code || "managed_python_install_failed",
    hint: error2?.hint || null,
    details: error2?.details || (error2 instanceof Error ? error2.message : String(error2)),
    at: (/* @__PURE__ */ new Date()).toISOString()
  };
}
function getManagedPythonTargetTriple() {
  if (process.platform === "win32") {
    if (process.arch === "x64") {
      return "x86_64-pc-windows-msvc";
    }
    if (process.arch === "arm64") {
      return "aarch64-pc-windows-msvc";
    }
    if (process.arch === "ia32") {
      return "i686-pc-windows-msvc";
    }
  }
  if (process.platform === "darwin") {
    if (process.arch === "x64") {
      return "x86_64-apple-darwin";
    }
    if (process.arch === "arm64") {
      return "aarch64-apple-darwin";
    }
  }
  if (process.platform === "linux") {
    if (process.arch === "x64") {
      return "x86_64-unknown-linux-gnu";
    }
    if (process.arch === "arm64") {
      return "aarch64-unknown-linux-gnu";
    }
  }
  throw new Error(
    `managed_python_platform_unsupported:${process.platform}:${process.arch}`
  );
}
function isPythonExecutableName(name) {
  const normalized = String(name || "").toLowerCase();
  return normalized === "python.exe" || normalized === "python" || normalized === "python3" || /^python3(\.\d+)?(\.exe)?$/.test(normalized);
}
function isVirtualEnvTemplateExecutable(candidate) {
  const normalized = String(candidate || "").replaceAll("\\", "/").toLowerCase();
  return normalized.includes("/lib/venv/");
}
function collectManagedPythonExecutableCandidates(rootDir) {
  const installRoots = [
    rootDir,
    import_node_path4.default.join(rootDir, "python"),
    import_node_path4.default.join(rootDir, "install"),
    import_node_path4.default.join(rootDir, "python", "install")
  ];
  const candidateSuffixes = process.platform === "win32" ? ["python.exe", import_node_path4.default.join("bin", "python.exe")] : ["bin/python3", "bin/python", "python3", "python"];
  const candidates = [];
  const seen = /* @__PURE__ */ new Set();
  const pushCandidate = (candidate) => {
    const normalized = import_node_path4.default.normalize(candidate);
    if (seen.has(normalized) || isVirtualEnvTemplateExecutable(normalized)) {
      return;
    }
    seen.add(normalized);
    candidates.push(normalized);
  };
  for (const installRoot of installRoots) {
    for (const suffix of candidateSuffixes) {
      pushCandidate(import_node_path4.default.join(installRoot, suffix));
    }
  }
  const queue = [{ dir: rootDir, depth: 0 }];
  while (queue.length > 0) {
    const current = queue.shift();
    if (!current || current.depth > 3 || !import_node_fs2.default.existsSync(current.dir)) {
      continue;
    }
    for (const entry of import_node_fs2.default.readdirSync(current.dir, { withFileTypes: true })) {
      const entryPath = import_node_path4.default.join(current.dir, entry.name);
      if (entry.isFile() && isPythonExecutableName(entry.name)) {
        pushCandidate(entryPath);
        continue;
      }
      if (entry.isDirectory() && !["dlls", "include", "lib", "libs", "share", "__pycache__"].includes(
        entry.name.toLowerCase()
      )) {
        queue.push({ dir: entryPath, depth: current.depth + 1 });
      }
    }
  }
  return candidates;
}
function readManagedPythonMetadata() {
  return readJson(managedPythonMetadataPath, null);
}
function getManagedPythonExecutableSync() {
  const metadata = readManagedPythonMetadata();
  if (metadata?.executable && import_node_fs2.default.existsSync(metadata.executable)) {
    return metadata.executable;
  }
  if (!import_node_fs2.default.existsSync(managedPythonCurrentDir)) {
    return null;
  }
  const candidates = [
    import_node_path4.default.join(managedPythonCurrentDir, "python", "python.exe"),
    import_node_path4.default.join(managedPythonCurrentDir, "python", "python"),
    import_node_path4.default.join(managedPythonCurrentDir, "bin", "python3"),
    import_node_path4.default.join(managedPythonCurrentDir, "bin", "python")
  ];
  for (const candidate of candidates) {
    if (import_node_fs2.default.existsSync(candidate)) {
      return candidate;
    }
  }
  return null;
}
function rewritePythonCommandIfNeeded(command, args, options = {}) {
  const normalizedCommand = String(command || "").trim().toLowerCase();
  const commandBasename = import_node_path4.default.basename(normalizedCommand).toLowerCase().replace(/\.exe$/, "");
  const isPip = commandBasename === "pip" || commandBasename === "pip3";
  const isPython = commandBasename === "python" || commandBasename === "python3";
  if (!isPip && !isPython) {
    return { command, args, rewritten: false };
  }
  const commandIsPathLike = import_node_path4.default.isAbsolute(normalizedCommand) || normalizedCommand.startsWith(".") || normalizedCommand.includes("\\") || normalizedCommand.includes("/");
  const managedPython = getManagedPythonExecutableSync();
  if (!commandIsPathLike && managedPython) {
    appendDaemonLog("debug", "python_command_preferring_managed", {
      command,
      isPip,
      isPython,
      managedPython
    });
    if (isPip) {
      return {
        command: managedPython,
        args: ["-m", "pip", ...args],
        rewritten: true
      };
    }
    return {
      command: managedPython,
      args: [...args],
      rewritten: true
    };
  }
  if (!commandIsPathLike) {
    const resolvedPath = resolveWindowsCommandPath(command, options);
    if (resolvedPath) {
      appendDaemonLog("debug", "python_command_found_in_path", {
        command,
        resolvedPath
      });
      return { command, args, rewritten: false };
    }
  }
  appendDaemonLog("debug", "python_command_not_in_path", {
    command,
    isPip,
    isPython,
    managedPython
  });
  if (!managedPython) {
    return { command, args, rewritten: false };
  }
  if (isPip) {
    return {
      command: managedPython,
      args: ["-m", "pip", ...args],
      rewritten: true
    };
  }
  if (isPython) {
    return {
      command: managedPython,
      args: [...args],
      rewritten: true
    };
  }
  return { command, args, rewritten: false };
}
function toManagedPythonStatus(metadata, executable) {
  const proxy = getManagedPythonProxyConfig();
  const installed = Boolean(executable);
  return {
    source: metadata?.source || MANAGED_PYTHON_SOURCE,
    installed,
    path: import_node_fs2.default.existsSync(managedPythonCurrentDir) ? managedPythonCurrentDir : null,
    executable: installed ? executable : null,
    release_tag: metadata?.release_tag || null,
    asset_name: metadata?.asset_name || null,
    target_triple: metadata?.target_triple || null,
    version_prefix: metadata?.version_prefix || MANAGED_PYTHON_VERSION_PREFIX,
    installed_at: installed ? metadata?.installed_at || null : null,
    installing: Boolean(managedPythonInstallPromise),
    proxy_configured: Boolean(proxy.url),
    proxy_url: proxy.display_url,
    proxy_source: proxy.source,
    last_error: installed ? null : metadata?.last_error || null,
    last_error_code: installed ? null : metadata?.last_error_code || null,
    last_error_hint: installed ? null : metadata?.last_error_hint || null,
    last_error_details: installed ? null : metadata?.last_error_details || null,
    last_error_at: installed ? null : metadata?.last_error_at || null
  };
}
async function resolveManagedPythonExecutable(rootDir) {
  for (const candidate of collectManagedPythonExecutableCandidates(rootDir)) {
    const resolved = await probePythonExecutable(candidate);
    if (resolved) {
      return resolved;
    }
  }
  throw new Error("managed_python_executable_not_found");
}
async function getManagedPythonStatus() {
  const metadata = readManagedPythonMetadata();
  if (!import_node_fs2.default.existsSync(managedPythonCurrentDir)) {
    return toManagedPythonStatus(metadata, null);
  }
  if (metadata?.executable && import_node_fs2.default.existsSync(metadata.executable)) {
    const resolved = await probePythonExecutable(metadata.executable);
    if (resolved) {
      return toManagedPythonStatus(metadata, resolved);
    }
  }
  try {
    const resolved = await resolveManagedPythonExecutable(
      managedPythonCurrentDir
    );
    return toManagedPythonStatus(metadata, resolved);
  } catch {
    return toManagedPythonStatus(metadata, null);
  }
}
async function resolveConfiguredPythonExecutable() {
  for (const candidate of [
    process.env.PICKTIME_PYTHON_PATH,
    process.env.PYTHON
  ]) {
    const resolved = await probePythonExecutable(candidate);
    if (resolved) {
      return resolved;
    }
  }
  return null;
}
function isWindowsAppsPythonAlias(candidate) {
  return process.platform === "win32" && String(candidate || "").toLowerCase().includes("\\windowsapps\\");
}
function collectWindowsPythonCandidates() {
  if (process.platform !== "win32") {
    return [];
  }
  const localAppData = process.env.LOCALAPPDATA || import_node_path4.default.join(import_node_os2.default.homedir(), "AppData", "Local");
  const userProfile = process.env.USERPROFILE || import_node_os2.default.homedir();
  const directCandidates = [
    import_node_path4.default.join("C:\\", "ProgramData", "anaconda3", "python.exe"),
    import_node_path4.default.join("C:\\", "ProgramData", "miniconda3", "python.exe"),
    import_node_path4.default.join(userProfile, "anaconda3", "python.exe"),
    import_node_path4.default.join(userProfile, "miniconda3", "python.exe"),
    import_node_path4.default.join(localAppData, "Programs", "Python", "Python312", "python.exe"),
    import_node_path4.default.join(localAppData, "Programs", "Python", "Python311", "python.exe"),
    import_node_path4.default.join(localAppData, "Programs", "Python", "Python310", "python.exe")
  ];
  const discoveredFromPrograms = [];
  const pythonRoot = import_node_path4.default.join(localAppData, "Programs", "Python");
  if (import_node_fs2.default.existsSync(pythonRoot)) {
    for (const entry of import_node_fs2.default.readdirSync(pythonRoot, { withFileTypes: true })) {
      if (!entry.isDirectory()) {
        continue;
      }
      discoveredFromPrograms.push(
        import_node_path4.default.join(pythonRoot, entry.name, "python.exe")
      );
    }
  }
  return [...directCandidates, ...discoveredFromPrograms].filter(
    (candidate) => import_node_fs2.default.existsSync(candidate)
  );
}
async function probePythonExecutable(candidate) {
  const normalizedCandidate = String(candidate || "").trim();
  if (!normalizedCandidate || isWindowsAppsPythonAlias(normalizedCandidate)) {
    return null;
  }
  if ((normalizedCandidate.includes("\\") || normalizedCandidate.includes("/")) && !import_node_fs2.default.existsSync(normalizedCandidate)) {
    return null;
  }
  try {
    const result = await executeSimpleCommand(
      normalizedCandidate,
      ["-c", "import sys; print(sys.executable)"],
      { timeoutMs: 3e4 }
    );
    const resolved = result.stdout.split(/\r?\n/).map((line) => line.trim()).filter(Boolean).at(-1);
    if (!resolved || isWindowsAppsPythonAlias(resolved)) {
      return null;
    }
    if (import_node_fs2.default.existsSync(resolved)) {
      return resolved;
    }
    if (import_node_fs2.default.existsSync(normalizedCandidate)) {
      return normalizedCandidate;
    }
  } catch {
    return null;
  }
  return null;
}
var systemPythonExecutablePromise = null;
async function resolveSystemPythonExecutable() {
  if (!systemPythonExecutablePromise) {
    systemPythonExecutablePromise = (async () => {
      const configured = await resolveConfiguredPythonExecutable();
      if (configured) {
        return configured;
      }
      const candidates = [];
      const pushCandidate = (value) => {
        const normalized = String(value || "").trim();
        if (!normalized || candidates.includes(normalized)) {
          return;
        }
        candidates.push(normalized);
      };
      if (process.platform === "win32") {
        try {
          const whereResult = await executeSimpleCommand(
            "where.exe",
            ["python"],
            {
              timeoutMs: 1e4
            }
          );
          for (const line of whereResult.stdout.split(/\r?\n/)) {
            pushCandidate(line);
          }
        } catch {
        }
        for (const candidate of collectWindowsPythonCandidates()) {
          pushCandidate(candidate);
        }
      }
      pushCandidate(getDefaultPythonCommand());
      for (const candidate of candidates) {
        const resolved = await probePythonExecutable(candidate);
        if (resolved) {
          return resolved;
        }
      }
      throw new Error(
        "python_executable_not_found: set PICKTIME_PYTHON_PATH or ensure a real python.exe is available before WindowsApps aliases"
      );
    })().catch((error2) => {
      systemPythonExecutablePromise = null;
      throw error2;
    });
  }
  return systemPythonExecutablePromise;
}
function selectManagedPythonAsset(release, targetTriple) {
  const assets = Array.isArray(release?.assets) ? release.assets : [];
  const prefix = `cpython-${MANAGED_PYTHON_VERSION_PREFIX}`;
  const suffix = `-${targetTriple}-install_only.tar.gz`;
  const asset = [...assets].filter(
    (item) => typeof item?.name === "string" && item.name.startsWith(prefix) && item.name.endsWith(suffix) && typeof item?.browser_download_url === "string"
  ).sort(
    (left, right) => String(right.name).localeCompare(String(left.name))
  )[0];
  if (!asset) {
    throw new Error(
      `managed_python_asset_not_found:${targetTriple}:${MANAGED_PYTHON_VERSION_PREFIX}`
    );
  }
  return asset;
}
async function fetchLatestManagedPythonRelease() {
  return fetchJson(MANAGED_PYTHON_RELEASE_URL, {
    headers: {
      Accept: "application/vnd.github+json",
      "User-Agent": "PickTime-Agent"
    },
    timeoutMs: MANAGED_PYTHON_REQUEST_TIMEOUT_MS,
    attempts: MANAGED_PYTHON_DOWNLOAD_ATTEMPTS
  });
}
async function executeSimpleCommand(command, args, options = {}) {
  const mergedEnv = mergeEnvironment(options.env || {});
  const spawnSpec = resolveBatchSpawn(command, args || [], {
    cwd: options.cwd,
    env: mergedEnv
  });
  return await new Promise((resolve, reject) => {
    const child = (0, import_node_child_process.spawn)(spawnSpec.command, spawnSpec.args, {
      cwd: options.cwd,
      env: mergedEnv,
      stdio: ["ignore", "pipe", "pipe"],
      shell: false,
      windowsHide: true,
      windowsVerbatimArguments: spawnSpec.windowsVerbatimArguments
    });
    let stdout = "";
    let stderr = "";
    const timeout = setTimeout(() => {
      child.kill("SIGTERM");
      reject(new Error("command_timeout"));
    }, options.timeoutMs || 12e4);
    child.stdout.on("data", (chunk) => {
      stdout += chunk.toString("utf8");
    });
    child.stderr.on("data", (chunk) => {
      stderr += chunk.toString("utf8");
    });
    child.on("error", (error2) => {
      clearTimeout(timeout);
      reject(error2);
    });
    child.on("close", (code) => {
      clearTimeout(timeout);
      if (code !== 0) {
        reject(new Error(stderr || stdout || `command_exit_${code ?? -1}`));
        return;
      }
      resolve({ stdout, stderr });
    });
  });
}
async function executeSimpleCommandWithInput(command, args, input, options = {}) {
  const mergedEnv = mergeEnvironment(options.env || {});
  const spawnSpec = resolveBatchSpawn(command, args || [], {
    cwd: options.cwd,
    env: mergedEnv
  });
  return await new Promise((resolve, reject) => {
    const child = (0, import_node_child_process.spawn)(spawnSpec.command, spawnSpec.args, {
      cwd: options.cwd,
      env: mergedEnv,
      stdio: ["pipe", "pipe", "pipe"],
      shell: false,
      windowsHide: true,
      windowsVerbatimArguments: spawnSpec.windowsVerbatimArguments
    });
    let stdout = "";
    let stderr = "";
    const timeout = setTimeout(() => {
      child.kill("SIGTERM");
      reject(new Error("command_timeout"));
    }, options.timeoutMs || 12e4);
    child.stdout.on("data", (chunk) => {
      stdout += chunk.toString("utf8");
    });
    child.stderr.on("data", (chunk) => {
      stderr += chunk.toString("utf8");
    });
    child.on("error", (error2) => {
      clearTimeout(timeout);
      reject(error2);
    });
    child.on("close", (code) => {
      clearTimeout(timeout);
      if (code !== 0) {
        reject(new Error(stderr || stdout || `command_exit_${code ?? -1}`));
        return;
      }
      resolve({ stdout, stderr });
    });
    child.stdin.end(input ?? "");
  });
}
function escapeXml(value) {
  return String(value || "").replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;").replaceAll("'", "&apos;");
}
function escapeAppleScriptString(value) {
  return String(value || "").replaceAll("\\", "\\\\").replaceAll('"', '\\"');
}
async function readClipboardNative() {
  const config2 = getConfig();
  if (config2.policies?.system?.clipboardEnabled === false) {
    throw new Error("clipboard_disabled");
  }
  if (process.platform === "win32") {
    const result = await executeSimpleCommand("powershell", [
      "-NoProfile",
      "-Command",
      "Get-Clipboard"
    ]);
    return result.stdout.replace(/\r?\n$/, "");
  }
  if (process.platform === "darwin") {
    const result = await executeSimpleCommand("pbpaste", []);
    return result.stdout;
  }
  throw new Error("clipboard_unsupported_platform");
}
async function writeClipboardNative(content) {
  const config2 = getConfig();
  if (config2.policies?.system?.clipboardEnabled === false) {
    throw new Error("clipboard_disabled");
  }
  const normalizedContent = String(content ?? "");
  if (process.platform === "win32") {
    await executeSimpleCommandWithInput(
      "powershell",
      ["-NoProfile", "-Command", "Set-Clipboard"],
      normalizedContent
    );
    return;
  }
  if (process.platform === "darwin") {
    await executeSimpleCommandWithInput("pbcopy", [], normalizedContent);
    return;
  }
  throw new Error("clipboard_unsupported_platform");
}
async function showNotificationNative(title, body) {
  const config2 = getConfig();
  if (config2.policies?.system?.notificationsEnabled === false) {
    throw new Error("notifications_disabled");
  }
  const normalizedTitle = String(title || "").trim();
  const normalizedBody = String(body || "");
  if (!normalizedTitle) {
    throw new Error("notification_title_required");
  }
  if (process.platform === "win32") {
    const xml = `<toast><visual><binding template="ToastGeneric"><text>${escapeXml(normalizedTitle)}</text><text>${escapeXml(normalizedBody)}</text></binding></visual></toast>`;
    const script = `
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType=WindowsRuntime] > $null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType=WindowsRuntime] > $null
$xml = @"
${xml}
"@
$doc = New-Object Windows.Data.Xml.Dom.XmlDocument
$doc.LoadXml($xml)
$toast = [Windows.UI.Notifications.ToastNotification]::new($doc)
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("PickTime")
$notifier.Show($toast)
`;
    await executeSimpleCommand("powershell", [
      "-NoProfile",
      "-Command",
      script
    ]);
    return;
  }
  if (process.platform === "darwin") {
    await executeSimpleCommand("osascript", [
      "-e",
      `display notification "${escapeAppleScriptString(normalizedBody)}" with title "${escapeAppleScriptString(normalizedTitle)}"`
    ]);
    return;
  }
  throw new Error("notification_unsupported_platform");
}
async function extractZip(zipPath, destinationPath) {
  ensureDir(destinationPath);
  if (process.platform === "win32") {
    await executeSimpleCommand("powershell", [
      "-NoProfile",
      "-Command",
      `Expand-Archive -Path '${zipPath.replace(/'/g, "''")}' -DestinationPath '${destinationPath.replace(/'/g, "''")}' -Force`
    ]);
    return;
  }
  await executeSimpleCommand("unzip", ["-o", zipPath, "-d", destinationPath]);
}
async function extractTarGz(archivePath, destinationPath) {
  ensureDir(destinationPath);
  const commands = process.platform === "win32" ? ["tar.exe", "tar"] : ["tar"];
  let lastError = null;
  for (const command of commands) {
    try {
      await executeSimpleCommand(
        command,
        ["-xzf", archivePath, "-C", destinationPath],
        { timeoutMs: 3e5 }
      );
      return;
    } catch (error2) {
      lastError = error2;
    }
  }
  if (process.platform === "win32") {
    await executeSimpleCommand(
      "powershell",
      [
        "-NoProfile",
        "-Command",
        `tar -xzf '${archivePath.replace(/'/g, "''")}' -C '${destinationPath.replace(/'/g, "''")}'`
      ],
      { timeoutMs: 3e5 }
    );
    return;
  }
  throw lastError || new Error("managed_python_extract_failed");
}
async function fetchJson(url2, options = {}) {
  const rawText = await fetchText(url2, options);
  try {
    return JSON.parse(rawText);
  } catch {
    throw new Error(`json_parse_failed:${url2}`);
  }
}
async function downloadFileWithCurl(url2, targetPath, options = {}) {
  const attempts = Math.max(
    1,
    Number(options.attempts) || MANAGED_PYTHON_DOWNLOAD_ATTEMPTS
  );
  const timeoutMs = Math.max(
    1e3,
    Number(options.timeoutMs) || MANAGED_PYTHON_DOWNLOAD_TIMEOUT_MS
  );
  const proxy = getManagedPythonProxyConfig();
  const args = [
    "-L",
    "-f",
    "-sS",
    "--connect-timeout",
    "20",
    "--max-time",
    String(Math.max(60, Math.ceil(timeoutMs / 1e3))),
    "--retry",
    String(Math.max(0, attempts - 1)),
    "--retry-delay",
    "1",
    "--retry-all-errors",
    "-o",
    targetPath
  ];
  appendCurlHeaders(args, options.headers || {});
  if (proxy.url) {
    args.push("--proxy", proxy.url);
  }
  args.push(url2);
  await executeCurl(args, { timeoutMs });
  return targetPath;
}
async function downloadFileWithFetch(url2, targetPath, options = {}) {
  const attempts = Math.max(
    1,
    Number(options.attempts) || MANAGED_PYTHON_DOWNLOAD_ATTEMPTS
  );
  const timeoutMs = Math.max(
    1e3,
    Number(options.timeoutMs) || MANAGED_PYTHON_DOWNLOAD_TIMEOUT_MS
  );
  let lastError = null;
  for (let attempt = 1; attempt <= attempts; attempt += 1) {
    const controller = new AbortController();
    const timeout = setTimeout(() => controller.abort(), timeoutMs);
    try {
      const response = await fetch(url2, {
        ...options,
        signal: controller.signal
      });
      if (!response.ok) {
        const error2 = new Error(`download_failed:${response.status}:${url2}`);
        error2.statusCode = response.status;
        throw error2;
      }
      const buffer = Buffer.from(await response.arrayBuffer());
      import_node_fs2.default.writeFileSync(targetPath, buffer);
      return targetPath;
    } catch (error2) {
      lastError = error2;
      const statusCode = error2?.statusCode;
      if (attempt >= attempts || !isRetryableHttpStatus(statusCode) && !isRetryableNetworkError(error2)) {
        throw error2;
      }
      await sleep(Math.min(1e3 * attempt, 3e3));
    } finally {
      clearTimeout(timeout);
    }
  }
  throw lastError || new Error(`download_failed:unknown:${url2}`);
}
async function downloadFile(url2, targetPath, options = {}) {
  const proxy = getManagedPythonProxyConfig();
  if (proxy.url) {
    return downloadFileWithCurl(url2, targetPath, options);
  }
  try {
    return await downloadFileWithCurl(url2, targetPath, options);
  } catch (error2) {
    if (!isCommandUnavailableError(error2)) {
      throw error2;
    }
    return downloadFileWithFetch(url2, targetPath, options);
  }
}
async function ensurePythonPackagingTools(pythonExecutable) {
  try {
    await executeSimpleCommand(pythonExecutable, ["-m", "pip", "--version"], {
      timeoutMs: 3e4
    });
    return;
  } catch {
  }
  await executeSimpleCommand(
    pythonExecutable,
    ["-m", "ensurepip", "--upgrade"],
    { timeoutMs: 18e4 }
  );
  await executeSimpleCommand(pythonExecutable, ["-m", "pip", "--version"], {
    timeoutMs: 3e4
  });
}
async function installManagedPythonRuntime() {
  const targetTriple = getManagedPythonTargetTriple();
  const proxy = getManagedPythonProxyConfig();
  let release = null;
  let asset = null;
  const tempDir = (0, import_node_fs3.mkdtempSync)(
    import_node_path4.default.join(import_node_os2.default.tmpdir(), "picktime-managed-python-")
  );
  let archivePath = null;
  const stagingDir = import_node_path4.default.join(
    managedPythonRootDir,
    `staging-${Date.now()}-${process.pid}`
  );
  try {
    release = await fetchLatestManagedPythonRelease();
    asset = selectManagedPythonAsset(release, targetTriple);
    archivePath = import_node_path4.default.join(tempDir, asset.name);
    ensureDir(managedPythonRootDir);
    await downloadFile(asset.browser_download_url, archivePath, {
      headers: {
        Accept: "application/octet-stream",
        "User-Agent": "PickTime-Agent"
      },
      timeoutMs: MANAGED_PYTHON_DOWNLOAD_TIMEOUT_MS,
      attempts: MANAGED_PYTHON_DOWNLOAD_ATTEMPTS
    });
    import_node_fs2.default.rmSync(stagingDir, { recursive: true, force: true });
    ensureDir(stagingDir);
    await extractTarGz(archivePath, stagingDir);
    const stagingExecutable = await resolveManagedPythonExecutable(stagingDir);
    await ensurePythonPackagingTools(stagingExecutable);
    import_node_fs2.default.rmSync(managedPythonCurrentDir, { recursive: true, force: true });
    import_node_fs2.default.renameSync(stagingDir, managedPythonCurrentDir);
    const executable = await resolveManagedPythonExecutable(
      managedPythonCurrentDir
    );
    const metadata = {
      source: MANAGED_PYTHON_SOURCE,
      release_tag: String(release?.tag_name || ""),
      asset_name: asset.name,
      target_triple: targetTriple,
      version_prefix: MANAGED_PYTHON_VERSION_PREFIX,
      executable,
      installed_at: (/* @__PURE__ */ new Date()).toISOString(),
      proxy_configured: Boolean(proxy.url),
      proxy_url: proxy.display_url,
      proxy_source: proxy.source,
      last_error: null,
      last_error_code: null,
      last_error_hint: null,
      last_error_details: null,
      last_error_at: null
    };
    writeJson(managedPythonMetadataPath, metadata);
    appendAudit("managed_python_installed", {
      releaseTag: metadata.release_tag,
      assetName: metadata.asset_name,
      targetTriple: metadata.target_triple,
      executable
    });
    return {
      ...toManagedPythonStatus(metadata, executable),
      installed_now: true
    };
  } catch (error2) {
    const managedPythonError = normalizeManagedPythonInstallError(error2, {
      proxy,
      targetTriple,
      assetName: asset?.name || null
    });
    const errorRecord = toManagedPythonErrorRecord(managedPythonError);
    writeJson(managedPythonMetadataPath, {
      source: MANAGED_PYTHON_SOURCE,
      release_tag: String(release?.tag_name || ""),
      asset_name: asset?.name || null,
      target_triple: targetTriple,
      version_prefix: MANAGED_PYTHON_VERSION_PREFIX,
      executable: null,
      installed_at: null,
      proxy_configured: Boolean(proxy.url),
      proxy_url: proxy.display_url,
      proxy_source: proxy.source,
      last_error: errorRecord.message,
      last_error_code: errorRecord.code,
      last_error_hint: errorRecord.hint,
      last_error_details: errorRecord.details,
      last_error_at: errorRecord.at
    });
    appendAudit("managed_python_install_failed", {
      code: errorRecord.code,
      message: errorRecord.message,
      hint: errorRecord.hint,
      proxyUrl: proxy.display_url,
      targetTriple,
      assetName: asset?.name || null
    });
    throw managedPythonError;
  } finally {
    import_node_fs2.default.rmSync(stagingDir, { recursive: true, force: true });
    import_node_fs2.default.rmSync(tempDir, { recursive: true, force: true });
  }
}
async function ensureManagedPythonRuntime() {
  const currentStatus = await getManagedPythonStatus();
  if (currentStatus.installed && currentStatus.executable) {
    return currentStatus;
  }
  if (!managedPythonInstallPromise) {
    managedPythonInstallPromise = (async () => {
      try {
        return await installManagedPythonRuntime();
      } finally {
        managedPythonInstallPromise = null;
      }
    })();
  }
  return managedPythonInstallPromise;
}
async function resolvePreferredPythonRuntime(options = {}) {
  const configured = await resolveConfiguredPythonExecutable();
  if (configured) {
    return {
      source: "configured",
      executable: configured,
      managed_python: await getManagedPythonStatus()
    };
  }
  const managedPython = await getManagedPythonStatus();
  if (managedPython.installed && managedPython.executable) {
    return {
      source: "managed",
      executable: managedPython.executable,
      managed_python: managedPython
    };
  }
  if (options.installManaged === true) {
    try {
      const installedManagedPython = await ensureManagedPythonRuntime();
      return {
        source: "managed",
        executable: installedManagedPython.executable,
        managed_python: installedManagedPython
      };
    } catch (error2) {
      const fallbackExecutable = await resolveSystemPythonExecutable().catch(
        () => null
      );
      if (fallbackExecutable) {
        appendAudit("managed_python_install_failed_fallback_to_system", {
          error: error2 instanceof Error ? error2.message : String(error2),
          executable: fallbackExecutable
        });
        return {
          source: "system",
          executable: fallbackExecutable,
          managed_python: managedPython,
          managed_python_error: error2 instanceof Error ? error2.message : String(error2)
        };
      }
      throw error2;
    }
  }
  return {
    source: "system",
    executable: await resolveSystemPythonExecutable(),
    managed_python: managedPython
  };
}
function buildPythonSkillRuntimeState(runner, options = {}) {
  return {
    kind: "python",
    readiness: options.readiness || (runner?.requirements ? "needs_setup" : "ready"),
    requiresSetup: Boolean(runner?.requirements),
    environment: options.environment || null,
    executable: options.executable || null,
    script: runner?.script || null,
    requirements: runner?.requirements || null,
    venvPath: options.venvPath || null,
    preparedAt: options.preparedAt || null,
    lastError: options.lastError || null,
    managedPythonError: options.managedPythonError || null
  };
}
async function buildInitialPythonSkillInstallState(options = {}) {
  const inferredRunner = options.inferredRunner;
  if (inferredRunner?.type !== "python") {
    return {
      initialRunner: null,
      initialRuntime: null
    };
  }
  const preferredRuntime = await resolvePreferredPythonRuntime().catch(
    (error2) => ({
      source: "system",
      executable: getDefaultPythonCommand(),
      managed_python_error: error2 instanceof Error ? error2.message : String(error2)
    })
  );
  const executable = preferredRuntime.executable || getDefaultPythonCommand();
  return {
    initialRunner: {
      command: executable,
      args: [inferredRunner.script],
      cwd: options.skillPath
    },
    initialRuntime: buildPythonSkillRuntimeState(inferredRunner, {
      readiness: inferredRunner.requirements ? "needs_setup" : "ready",
      environment: preferredRuntime.source || "system",
      executable,
      preparedAt: inferredRunner.requirements ? null : options.installedAt,
      managedPythonError: preferredRuntime.managed_python_error || null
    })
  };
}
async function setupPythonSkillEnvironment(skillName) {
  const skillPath = import_node_path4.default.join(skillsDir, skillName);
  const runner = inferSkillRunnerFromFiles(skillPath);
  if (!runner?.script) {
    throw new Error("python_skill_runner_not_found");
  }
  const venvPath = import_node_path4.default.join(venvsDir, skillName);
  const venvPython = process.platform === "win32" ? import_node_path4.default.join(venvPath, "Scripts", "python.exe") : import_node_path4.default.join(venvPath, "bin", "python");
  const pythonRuntime = await resolvePreferredPythonRuntime({
    installManaged: true
  });
  ensureDir(venvsDir);
  const resolvedVenvPython = await probePythonExecutable(venvPython);
  if (!resolvedVenvPython) {
    import_node_fs2.default.rmSync(venvPath, { recursive: true, force: true });
    await executeSimpleCommand(
      pythonRuntime.executable,
      ["-m", "venv", venvPath],
      {
        timeoutMs: 18e4
      }
    );
  }
  await ensurePythonPackagingTools(venvPython);
  if (runner.requirements) {
    await executeSimpleCommand(
      venvPython,
      ["-m", "pip", "install", "-r", runner.requirements],
      {
        cwd: skillPath,
        timeoutMs: 3e5
      }
    );
  }
  const preparedAt = (/* @__PURE__ */ new Date()).toISOString();
  const skillEntry = updateSkillEntry(skillName, {
    runner: {
      command: venvPython,
      args: [runner.script],
      cwd: skillPath
    },
    runtime: buildPythonSkillRuntimeState(runner, {
      readiness: "ready",
      environment: "venv",
      executable: venvPython,
      venvPath,
      preparedAt,
      managedPythonError: pythonRuntime.managed_python_error || null
    })
  });
  appendAudit("python_skill_env_prepared", {
    skillName,
    pythonSource: pythonRuntime.source,
    pythonExecutable: pythonRuntime.executable,
    venvPath,
    requirementsInstalled: Boolean(runner.requirements),
    managedPythonError: pythonRuntime.managed_python_error || null
  });
  return enrichSkillEntry(skillEntry);
}
async function installSkillFromClawHub(params) {
  const resolvedTarget = params?.resolvedTarget || resolveSkillInstallReference(
    params?.skillReference ?? params?.sourcePath ?? params?.slug,
    { allowLocalPath: false }
  );
  const allowSuspicious = params?.allowSuspicious === true;
  const installDeps = params?.installDeps === true;
  if (resolvedTarget.provider !== "clawhub") {
    throw new Error("skill_install_provider_not_supported");
  }
  let metadata = null;
  let resolvedSlug = "";
  let lastNotFoundError = null;
  for (const slugCandidate of resolvedTarget.slugCandidates || []) {
    try {
      metadata = await fetchJson(buildClawHubSkillMetadataUrl(slugCandidate));
      resolvedSlug = String(metadata?.skill?.slug || slugCandidate).trim() || slugCandidate;
      break;
    } catch (error2) {
      if (isHttpNotFoundError(error2)) {
        lastNotFoundError = error2;
        continue;
      }
      throw error2;
    }
  }
  if (!metadata || !resolvedSlug) {
    throw lastNotFoundError || new Error(`clawhub_skill_not_found:${resolvedTarget.rawReference}`);
  }
  const verdict = metadata?.moderation?.verdict || "none";
  if (verdict === "suspicious" && !allowSuspicious) {
    throw new Error("clawhub_skill_suspicious");
  }
  if (metadata?.moderation?.isMalwareBlocked) {
    throw new Error("clawhub_skill_blocked");
  }
  const tempDir = (0, import_node_fs3.mkdtempSync)(
    import_node_path4.default.join(import_node_os2.default.tmpdir(), `picktime-${resolvedSlug}-`)
  );
  const zipPath = import_node_path4.default.join(tempDir, `${resolvedSlug}.zip`);
  const downloadUrl = buildClawHubSkillDownloadUrl(resolvedSlug);
  await downloadFile(downloadUrl, zipPath);
  await extractZip(zipPath, tempDir);
  const skillName = String(metadata?.skill?.slug || resolvedSlug);
  const destination = import_node_path4.default.join(skillsDir, skillName);
  import_node_fs2.default.rmSync(destination, { recursive: true, force: true });
  ensureDir(destination);
  for (const entry of import_node_fs2.default.readdirSync(tempDir, { withFileTypes: true })) {
    if (entry.name.endsWith(".zip")) {
      continue;
    }
    const sourcePath = import_node_path4.default.join(tempDir, entry.name);
    const targetPath = import_node_path4.default.join(destination, entry.name);
    if (entry.isDirectory()) {
      copyDirRecursive(sourcePath, targetPath);
    } else {
      import_node_fs2.default.copyFileSync(sourcePath, targetPath);
    }
  }
  const inferredRunner = inferSkillRunnerFromFiles(destination);
  const inferredKnowledgeRuntime = inferKnowledgeSkillRuntime(destination);
  const installedAt = (/* @__PURE__ */ new Date()).toISOString();
  const { initialRunner, initialRuntime } = await buildInitialPythonSkillInstallState({
    inferredRunner,
    installedAt,
    skillPath: destination
  });
  updateSkillEntry(skillName, {
    enabled: true,
    source: "clawhub",
    label: pickFirstString(metadata?.skill?.displayName) || pickFirstString(metadata?.skill?.display_name) || pickFirstString(metadata?.skill?.name) || skillName,
    description: pickFirstString(metadata?.skill?.summary) || pickFirstString(metadata?.skill?.description) || null,
    slug: resolvedSlug,
    owner: metadata?.owner?.handle || null,
    version: metadata?.latestVersion?.version || null,
    moderation: verdict,
    installed_at: installedAt,
    runtime: initialRuntime || inferredKnowledgeRuntime,
    runner: initialRunner,
    capabilities: metadata?.skill?.capabilities || metadata?.capabilities || null
  });
  if (installDeps && inferredRunner?.type === "python") {
    await setupPythonSkillEnvironment(skillName);
  }
  const installedSkill = enrichSkillEntry(getSkillEntry(skillName));
  appendAudit("skill_installed_from_clawhub", {
    skillName,
    requestedReference: resolvedTarget.rawReference,
    slug: resolvedSlug,
    version: metadata?.latestVersion?.version || null,
    moderation: verdict,
    installDeps
  });
  triggerCapabilitySync();
  return {
    ok: true,
    installed: skillName,
    requested_reference: resolvedTarget.rawReference,
    slug: resolvedSlug,
    resolved_slug: resolvedSlug,
    version: metadata?.latestVersion?.version || null,
    moderation: verdict,
    path: destination,
    runner: installedSkill.runner,
    runtime: installedSkill.runtime
  };
}
async function installSkillFromPath(sourcePath, options = {}) {
  if (!sourcePath) {
    throw new Error("skill_source_path_required");
  }
  const absoluteSource = import_node_path4.default.resolve(sourcePath);
  const name = import_node_path4.default.basename(absoluteSource);
  if (!import_node_fs2.default.existsSync(import_node_path4.default.join(absoluteSource, "SKILL.md"))) {
    throw new Error("skill_md_not_found");
  }
  const destination = import_node_path4.default.join(skillsDir, name);
  ensureDir(skillsDir);
  copyDirRecursive(absoluteSource, destination);
  const inferredRunner = inferSkillRunnerFromFiles(destination);
  const inferredKnowledgeRuntime = inferKnowledgeSkillRuntime(destination);
  const installedAt = (/* @__PURE__ */ new Date()).toISOString();
  const { initialRunner, initialRuntime } = await buildInitialPythonSkillInstallState({
    inferredRunner,
    installedAt,
    skillPath: destination
  });
  updateSkillEntry(name, {
    enabled: true,
    source: "local_path",
    installed_at: installedAt,
    runner: initialRunner,
    runtime: initialRuntime || inferredKnowledgeRuntime
  });
  if (options.installDeps === true && inferredRunner?.type === "python") {
    await setupPythonSkillEnvironment(name);
  }
  const installedSkill = enrichSkillEntry(getSkillEntry(name));
  appendAudit("skill_installed", { name, sourcePath: absoluteSource });
  triggerCapabilitySync();
  return {
    ok: true,
    installed: name,
    path: destination,
    runner: installedSkill.runner,
    runtime: installedSkill.runtime
  };
}
async function installSkillFromReference(params, options = {}) {
  const resolvedTarget = resolveSkillInstallReference(
    params?.skillReference ?? params?.sourcePath ?? params?.slug,
    { allowLocalPath: options.allowLocalPath !== false }
  );
  if (resolvedTarget.provider === "local_path") {
    return installSkillFromPath(resolvedTarget.sourcePath, {
      installDeps: params?.installDeps === true
    });
  }
  return installSkillFromClawHub({
    ...params,
    resolvedTarget
  });
}
function uninstallSkill(name) {
  if (!name) {
    throw new Error("skill_name_required");
  }
  import_node_fs2.default.rmSync(import_node_path4.default.join(skillsDir, name), { recursive: true, force: true });
  removeSkillEntry(name);
  appendAudit("skill_removed", { name });
  triggerCapabilitySync();
  return { ok: true, removed: name };
}
function listMcpServers() {
  return listManagedMcpServers();
}
function inspectMcpServer(name) {
  if (!name) {
    throw new Error("mcp_name_required");
  }
  const config2 = loadMcpConfig();
  return config2.mcpServers?.[name] || null;
}
function saveMcpServer(name, value) {
  const config2 = loadMcpConfig();
  config2.mcpServers ||= {};
  config2.mcpServers[name] = value;
  saveMcpConfig(config2);
  clearMcpDiscoveryCache(name, { closeSession: true });
  appendAudit("mcp_server_saved", {
    name,
    transport: value.url ? "http" : "stdio"
  });
  triggerCapabilitySync();
  return { ok: true, name, ...value };
}
async function pairDevice(params) {
  const {
    serverUrl,
    accessToken,
    name,
    platform,
    deviceFamily,
    clientVersion
  } = params || {};
  if (!serverUrl || !accessToken || !name) {
    throw new Error("device_pair_requires_server_url_access_token_name");
  }
  const normalizedServerUrl = normalizeServerUrl(serverUrl);
  const localDeviceFingerprint = await resolveLocalDeviceFingerprint({
    required: true
  });
  const response = await fetch(`${normalizedServerUrl}/api/v1/devices/pair`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${accessToken}`
    },
    body: JSON.stringify({
      name,
      platform: platform || process.platform,
      device_family: deviceFamily || (process.platform === "darwin" ? "mac" : process.platform),
      client_version: clientVersion || DEFAULT_CLIENT_VERSION,
      device_fingerprint: localDeviceFingerprint.fingerprint,
      device_fingerprint_version: localDeviceFingerprint.version,
      metadata: {
        managed_by: "picktime-agentd",
        device_identity_strategy: "stable_device_fingerprint"
      }
    })
  });
  if (!response.ok) {
    const text = await response.text();
    throw new Error(`device_pair_failed:${response.status}:${text}`);
  }
  const data = await response.json();
  const config2 = getConfig();
  config2.device = {
    ...config2.device,
    serverUrl: normalizedServerUrl,
    deviceId: String(data.device?.id || ""),
    deviceToken: String(data.device_token || ""),
    name: String(data.device?.name || name),
    platform: String(data.device?.platform || platform || process.platform),
    deviceFamily: String(
      data.device?.device_family || deviceFamily || (process.platform === "darwin" ? "mac" : process.platform)
    ),
    clientVersion: String(
      data.device?.client_version || clientVersion || DEFAULT_CLIENT_VERSION
    ),
    autoConnect: true
  };
  saveConfig(config2);
  appendAudit("device_paired", {
    deviceId: config2.device.deviceId,
    serverUrl: normalizedServerUrl
  });
  disconnectWebsocket();
  scheduleReconnect(10);
  return data;
}
function setDeviceCredentials(params) {
  const {
    serverUrl,
    deviceId,
    deviceToken,
    name,
    platform,
    deviceFamily,
    clientVersion,
    autoConnect = true
  } = params || {};
  if (!serverUrl || !deviceId || !deviceToken) {
    throw new Error("device_credentials_required");
  }
  const config2 = getConfig();
  config2.device = {
    ...config2.device,
    serverUrl: normalizeServerUrl(serverUrl),
    deviceId: String(deviceId),
    deviceToken: String(deviceToken),
    name: name || config2.device.name,
    platform: platform || config2.device.platform,
    deviceFamily: deviceFamily || config2.device.deviceFamily,
    clientVersion: clientVersion || config2.device.clientVersion,
    autoConnect: autoConnect !== false
  };
  saveConfig(config2);
  appendAudit("device_credentials_updated", {
    deviceId: config2.device.deviceId
  });
  disconnectWebsocket();
  scheduleReconnect(10);
  return { ok: true, device: config2.device };
}
function clearDeviceCredentials() {
  const config2 = getConfig();
  config2.device = {
    ...config2.device,
    serverUrl: "",
    deviceId: "",
    deviceToken: ""
  };
  saveConfig(config2);
  disconnectWebsocket();
  appendAudit("device_credentials_cleared");
  return { ok: true };
}
function listApprovals() {
  return Array.from(daemonState.approvals.values()).sort(
    (a, b) => a.created_at.localeCompare(b.created_at)
  );
}
function findApprovalByRunId(runId) {
  const normalizedRunId = String(runId || "").trim();
  if (!normalizedRunId) {
    return null;
  }
  return Array.from(daemonState.approvals.values()).find(
    (approval) => approval.run_id === normalizedRunId
  ) || null;
}
function buildApprovalFromDecisionMessage(message) {
  const runId = String(message?.run_id || "").trim();
  if (!runId) {
    return null;
  }
  const traceId = message?.trace_id ? String(message.trace_id).trim() : "";
  const rawApproval = message?.approval && typeof message.approval === "object" && !Array.isArray(message.approval) ? message.approval : null;
  const requestPayloadCandidates = [
    message?.request_payload,
    message?.payload,
    rawApproval?.details
  ];
  const payload = requestPayloadCandidates.find(
    (candidate) => candidate && typeof candidate === "object" && !Array.isArray(candidate)
  ) || null;
  if (!payload) {
    return null;
  }
  const historyEntry = createRunHistoryEntry({
    runId,
    traceId: traceId || null,
    payload,
    source: "remote"
  });
  const kind = typeof rawApproval?.kind === "string" && rawApproval.kind.trim() ? rawApproval.kind.trim() : typeof payload.run_type === "string" && payload.run_type.trim() ? payload.run_type.trim() : null;
  const summary = typeof rawApproval?.summary === "string" && rawApproval.summary.trim() ? rawApproval.summary.trim() : historyEntry.summary || (kind ? `Approve ${kind}` : "Approve requested action");
  const approvalResponsePayload = {
    approval: {
      kind,
      summary,
      details: rawApproval?.details && typeof rawApproval.details === "object" && !Array.isArray(rawApproval.details) ? rawApproval.details : payload
    }
  };
  updateRunHistoryEntry(historyEntry, {
    status: "approval_required",
    response_payload: approvalResponsePayload
  });
  return {
    id: generateId(),
    run_id: runId,
    trace_id: traceId || null,
    created_at: (/* @__PURE__ */ new Date()).toISOString(),
    kind,
    summary,
    payload,
    historyEntry
  };
}
async function resolveApproval(params) {
  const { approvalId, decision } = params || {};
  const approval = daemonState.approvals.get(String(approvalId || ""));
  if (!approval) {
    throw new Error("approval_not_found");
  }
  daemonState.approvals.delete(approval.id);
  appendAudit("approval_resolved", {
    approvalId: approval.id,
    decision,
    runId: approval.run_id
  });
  if (decision === "approve") {
    executeApprovedRun(approval).catch((error2) => {
      appendDaemonLog("error", "approval_execution_failed", {
        approvalId: approval.id,
        error: error2 instanceof Error ? error2.message : String(error2)
      });
    });
    return { ok: true, status: "approved" };
  }
  updateRunHistoryEntry(approval.historyEntry, {
    status: "failed",
    error_message: "approval_denied",
    completed_at: (/* @__PURE__ */ new Date()).toISOString()
  });
  await sendRunResult(approval.run_id, approval.trace_id, {
    ok: false,
    error_message: "approval_denied",
    stderr: "approval_denied"
  });
  return { ok: true, status: "denied" };
}
async function triggerCapabilitySync() {
  const socket = daemonState.websocket;
  if (!socket || socket.readyState !== WebSocket.OPEN) {
    return;
  }
  socket.send(
    JSON.stringify({
      event: "capability_snapshot",
      ...buildCapabilitySnapshot()
    })
  );
}
function buildCapabilitySnapshot() {
  const config2 = getConfig();
  const capabilityPreferences = getCapabilityPreferences(config2);
  const filesystemPolicy = config2.policies?.filesystem || {};
  const cachedMcpCatalog = buildMcpCatalogPreview();
  const cachedMcpCatalogByName = new Map(
    cachedMcpCatalog.map((item) => [item.name, item])
  );
  const skills = listSkills().map(
    (item) => toCapabilityItem(item.name, {
      label: item.label || item.name,
      description: item.description || null,
      enabled: item.enabled,
      metadata: {
        path: item.path,
        slug: item.slug || null,
        hasRunner: Boolean(item.runtime?.hasRunner),
        runtime: item.runtime || null,
        capabilities: item.capabilities || null
      }
    })
  );
  const mcpServers = listManagedMcpServers().map(
    (item) => toCapabilityItem(item.name, {
      enabled: item.enabled !== false,
      description: describeMcpServer(item),
      metadata: {
        transport: normalizeMcpServerTransport(item),
        discovery_state: cachedMcpCatalogByName.get(item.name)?.discovery_state || (item.enabled === false ? "disabled" : "unknown"),
        tool_count: cachedMcpCatalogByName.get(item.name)?.tool_count ?? null,
        discovered_at: cachedMcpCatalogByName.get(item.name)?.discovered_at || null
      }
    })
  );
  const aiProviders = (config2.aiProviders || []).map(
    (item) => toCapabilityItem(item.name || item.id || "provider", {
      enabled: item.enabled !== false,
      description: item.description || null,
      metadata: item.metadata || null
    })
  );
  const tools = [
    toCapabilityItem("command", {
      description: "Execute a local command with policy checks."
    }),
    toCapabilityItem("read_file", {
      description: "Read a local file under the current filesystem policy."
    }),
    toCapabilityItem("write_file", {
      description: "Write a local file under the current filesystem policy."
    }),
    toCapabilityItem("watch_path", {
      description: "Watch a local file or directory under the current filesystem policy."
    }),
    toCapabilityItem("list_watches", {
      description: "List active local file watches."
    }),
    toCapabilityItem("poll_watch_events", {
      description: "Poll accumulated local file watch events."
    }),
    toCapabilityItem("unwatch_path", {
      description: "Stop watching a local path."
    }),
    toCapabilityItem("start_process", {
      description: "Start a managed local process."
    }),
    toCapabilityItem("list_processes", {
      description: "List managed local processes."
    }),
    toCapabilityItem("get_process", {
      description: "Inspect a managed local process."
    }),
    toCapabilityItem("stop_process", {
      description: "Stop a managed local process."
    }),
    toCapabilityItem("install_skill", {
      description: "Install a ClawHub skill by slug, skill name, or URL."
    }),
    toCapabilityItem("search_skill_catalog", {
      description: "Search installable skills from the remote catalog with natural language."
    }),
    toCapabilityItem("inspect_skill", {
      description: "Inspect an installed local skill to read runtime state, capability manifest, and file paths."
    }),
    toCapabilityItem("invoke_skill", {
      description: "Run a configured local skill runner. Prefer skills that declare a compatible capability manifest for the target format and action."
    }),
    toCapabilityItem("invoke_mcp_tool", {
      description: "Call a configured local MCP tool."
    }),
    toCapabilityItem("get_mcp_catalog", {
      description: "Inspect a configured local MCP server and fetch tool schemas progressively."
    }),
    toCapabilityItem("search_smithery_mcp_servers", {
      description: "Search Smithery MCP servers with natural language."
    }),
    toCapabilityItem("install_smithery_mcp_server", {
      description: "Install a Smithery remote MCP server as a local stdio proxy."
    }),
    toCapabilityItem("get_clipboard", {
      description: "Read the local clipboard."
    }),
    toCapabilityItem("set_clipboard", {
      description: "Write the local clipboard."
    }),
    toCapabilityItem("show_notification", {
      description: "Show a local system notification."
    }),
    toCapabilityItem("hotkey", {
      description: "Register and dispatch app-level local hotkeys."
    })
  ];
  return {
    skills,
    mcp_servers: mcpServers,
    ai_providers: aiProviders,
    tools,
    raw_payload: {
      generated_at: (/* @__PURE__ */ new Date()).toISOString(),
      capability_profile: capabilityPreferences.profile,
      enabled_capability_packs: capabilityPreferences.enabledPacks,
      filesystem_policy: {
        mode: filesystemPolicy.mode || "standard",
        allowed_roots: [...filesystemPolicy.allowedRoots || []],
        protected_write_roots: [
          ...filesystemPolicy.protectedWriteRoots || []
        ],
        require_approval_for_overwrite: filesystemPolicy.requireApprovalForOverwrite !== false
      },
      capability_packs: capabilityPreferences.packs,
      recommended_skills: capabilityPreferences.recommendedSkills,
      pack_catalog: Object.keys(capabilityPackCatalog),
      cached_mcp_catalog: cachedMcpCatalog
    }
  };
}
function assertFilesystemPathAccess(targetPath, accessKind) {
  const config2 = getConfig();
  const decision = getFilesystemPathAccessDecision(
    targetPath,
    config2.policies?.filesystem,
    accessKind
  );
  if (!decision.allowed) {
    throw new Error(decision.reason || "filesystem_access_denied");
  }
  return decision;
}
function scheduleReconnect(delayMs = 3e3) {
  if (daemonState.shuttingDown) {
    return;
  }
  clearTimeout(daemonState.reconnectTimer);
  daemonState.reconnectTimer = setTimeout(() => {
    connectDeviceLoop().catch((error2) => {
      daemonState.wsLastError = error2 instanceof Error ? error2.message : String(error2);
      appendDaemonLog("error", "device_connect_loop_failed", {
        error: daemonState.wsLastError
      });
      scheduleReconnect(5e3);
    });
  }, delayMs);
}
function disconnectWebsocket() {
  clearInterval(daemonState.heartbeatTimer);
  daemonState.heartbeatTimer = null;
  if (daemonState.websocket) {
    try {
      daemonState.websocket.close();
    } catch {
    }
  }
  daemonState.websocket = null;
  daemonState.wsConnected = false;
  daemonState.wsConnectedAt = null;
}
function sendSocketJson(payload) {
  const socket = daemonState.websocket;
  if (!socket || socket.readyState !== WebSocket.OPEN) {
    return false;
  }
  try {
    socket.send(JSON.stringify(payload));
    return true;
  } catch (error2) {
    appendDaemonLog("error", "device_ws_send_failed", {
      event: payload?.event || null,
      error: error2 instanceof Error ? error2.message : String(error2)
    });
    return false;
  }
}
async function connectDeviceLoop() {
  const config2 = getConfig();
  if (daemonState.websocket || !config2.device?.autoConnect) {
    return;
  }
  if (!config2.device?.serverUrl || !config2.device?.deviceId || !config2.device?.deviceToken) {
    return;
  }
  if (typeof WebSocket === "undefined") {
    daemonState.wsLastError = "websocket_not_available";
    return;
  }
  const localDeviceFingerprint = await resolveLocalDeviceFingerprint();
  const url2 = websocketUrlForServer(config2.device.serverUrl);
  const socket = new WebSocket(
    `${url2}?device_id=${encodeURIComponent(config2.device.deviceId)}&device_token=${encodeURIComponent(config2.device.deviceToken)}`
  );
  daemonState.websocket = socket;
  socket.addEventListener("open", () => {
    daemonState.wsConnected = true;
    daemonState.wsConnectedAt = (/* @__PURE__ */ new Date()).toISOString();
    daemonState.wsLastError = null;
    appendDaemonLog("info", "device_ws_connected", {
      deviceId: config2.device.deviceId,
      url: url2
    });
    if (!sendSocketJson({
      event: "hello",
      name: config2.device.name,
      platform: config2.device.platform,
      device_family: config2.device.deviceFamily,
      client_version: config2.device.clientVersion,
      device_fingerprint: localDeviceFingerprint?.fingerprint || config2.device.fingerprint || void 0,
      device_fingerprint_version: localDeviceFingerprint?.version || config2.device.fingerprintVersion || void 0,
      metadata: {
        runtime: "picktime-agentd",
        pid: process.pid
      }
    })) {
      daemonState.wsLastError = "device_ws_not_connected";
    }
    triggerCapabilitySync();
    clearInterval(daemonState.heartbeatTimer);
    daemonState.heartbeatTimer = setInterval(
      () => {
        daemonState.lastHeartbeatAt = (/* @__PURE__ */ new Date()).toISOString();
        if (!sendSocketJson({
          event: "heartbeat",
          status: daemonState.approvals.size > 0 ? "pairing" : "online"
        })) {
          daemonState.wsLastError = "device_ws_not_connected";
        }
      },
      Math.max(5e3, Number(config2.device.heartbeatIntervalMs || 3e4))
    );
  });
  socket.addEventListener("message", (event) => {
    const text = typeof event.data === "string" ? event.data : Buffer.from(event.data).toString("utf8");
    handleSocketMessage(text).catch((error2) => {
      daemonState.wsLastError = error2 instanceof Error ? error2.message : String(error2);
      appendDaemonLog("error", "device_ws_message_failed", {
        error: daemonState.wsLastError
      });
    });
  });
  socket.addEventListener("error", (event) => {
    const errorObj = event?.error || event;
    const message = errorObj?.message || errorObj?.code || (typeof errorObj === "string" ? errorObj : "device_ws_error");
    daemonState.wsLastError = message;
    appendDaemonLog("error", "device_ws_error", {
      error: message,
      code: errorObj?.code || null,
      errno: errorObj?.errno || null
    });
  });
  socket.addEventListener("close", (event) => {
    daemonState.wsConnected = false;
    daemonState.wsConnectedAt = null;
    daemonState.websocket = null;
    clearInterval(daemonState.heartbeatTimer);
    daemonState.heartbeatTimer = null;
    const code = event?.code;
    const reason = event?.reason || null;
    if (code && code !== 1e3 && code !== 1001) {
      appendDaemonLog("warn", "device_ws_closed", { code, reason });
    }
    if (!daemonState.shuttingDown) {
      scheduleReconnect(5e3);
    }
  });
}
async function handleSocketMessage(raw) {
  const message = JSON.parse(raw);
  if (message.event === "approval_decision") {
    const runId2 = String(message.run_id || "").trim();
    const decision = String(message.decision || "").trim().toLowerCase();
    if (!runId2 || decision !== "approve" && decision !== "deny") {
      appendDaemonLog("warn", "approval_decision_invalid", {
        runId: runId2,
        decision
      });
      return;
    }
    let approval2 = findApprovalByRunId(runId2);
    if (!approval2) {
      approval2 = buildApprovalFromDecisionMessage(message);
      if (!approval2) {
        appendDaemonLog("warn", "approval_decision_missing", {
          runId: runId2,
          decision
        });
        return;
      }
      daemonState.approvals.set(approval2.id, approval2);
      appendDaemonLog("info", "approval_decision_rehydrated", {
        runId: runId2,
        decision,
        kind: approval2.kind || null
      });
    }
    await resolveApproval({
      approvalId: approval2.id,
      decision
    });
    return;
  }
  if (message.event !== "invoke") {
    return;
  }
  const runId = String(message.run_id || "");
  const traceId = message.trace_id ? String(message.trace_id) : null;
  const payload = message.payload || {};
  appendAudit(
    "device_invoke_received",
    {
      runId,
      traceId,
      runType: payload.run_type || null
    },
    { payload }
  );
  const historyEntry = createRunHistoryEntry({
    runId,
    traceId,
    payload,
    source: "remote"
  });
  const approval = createApprovalIfRequired({
    runId,
    traceId,
    payload,
    historyEntry
  });
  if (approval) {
    daemonState.approvals.set(approval.id, approval);
    updateRunHistoryEntry(historyEntry, {
      status: "approval_required",
      response_payload: {
        approval: {
          kind: approval.kind,
          summary: approval.summary
        }
      }
    });
    await sendApprovalRequired(runId, traceId, approval);
    return;
  }
  await executeRun({ runId, traceId, payload, historyEntry });
}
function createApprovalIfRequired({ runId, traceId, payload, historyEntry }) {
  const config2 = getConfig();
  if (config2.approvals?.autoApprove) {
    return null;
  }
  const runType = String(payload.run_type || "");
  if (runType === "command") {
    const normalizedInvocation = normalizeCommandInvocation(
      payload.command,
      payload.args
    );
    const commandContext = buildCommandPolicyContext({
      command: normalizedInvocation.command,
      args: normalizedInvocation.args,
      rawCommand: String(payload.command || ""),
      shell: payload.shell
    });
    if (!commandMatchesAnyPattern(commandContext, [
      ...config2.policies?.commands?.blockedExecutables || []
    ]) && commandMatchesAnyPattern(commandContext, [
      ...config2.policies?.commands?.approvalPatterns || []
    ])) {
      const commandPreview = truncateOutput(
        commandContext.rawCommand || [commandContext.command, ...commandContext.args].join(" "),
        160
      );
      return {
        id: generateId(),
        run_id: runId,
        trace_id: traceId,
        created_at: (/* @__PURE__ */ new Date()).toISOString(),
        kind: "command",
        summary: `Approve risky command ${commandPreview}`.trim() || "Approve risky command",
        payload,
        historyEntry
      };
    }
  }
  if (runType === "write_file") {
    const targetPath = String(payload.path || "");
    if (isProtectedWritePath(targetPath, config2.policies?.filesystem)) {
      return {
        id: generateId(),
        run_id: runId,
        trace_id: traceId,
        created_at: (/* @__PURE__ */ new Date()).toISOString(),
        kind: "write_file",
        summary: `Approve protected write ${targetPath}`,
        payload,
        historyEntry
      };
    }
    if (import_node_fs2.default.existsSync(targetPath) && config2.policies?.filesystem?.requireApprovalForOverwrite !== false) {
      return {
        id: generateId(),
        run_id: runId,
        trace_id: traceId,
        created_at: (/* @__PURE__ */ new Date()).toISOString(),
        kind: "write_file",
        summary: `Approve overwrite ${targetPath}`,
        payload,
        historyEntry
      };
    }
  }
  return null;
}
async function executeApprovedRun(approval) {
  await executeRun({
    runId: approval.run_id,
    traceId: approval.trace_id,
    payload: approval.payload,
    historyEntry: approval.historyEntry,
    approvalContext: {
      approved: true,
      kind: approval.kind || null
    }
  });
}
async function executeRun({
  runId,
  traceId,
  payload,
  historyEntry = null,
  approvalContext = null
}) {
  const resolvedHistoryEntry = historyEntry || createRunHistoryEntry({
    runId,
    traceId,
    payload,
    source: "remote"
  });
  daemonState.pendingRuns.set(runId, { runId, traceId, payload });
  try {
    updateRunHistoryEntry(resolvedHistoryEntry, {
      status: "running",
      completed_at: null,
      error_message: null
    });
    await sendRunStarted(runId, traceId);
    if (String(payload?.run_type || "") === "start_process") {
      await executeRemoteManagedProcessRun({
        runId,
        traceId,
        payload,
        historyEntry: resolvedHistoryEntry,
        approvalContext
      });
      return;
    }
    const result = normalizeExecutorResult(
      await runExecutor(payload, { approvalContext })
    );
    completeRunHistoryEntry(resolvedHistoryEntry, result);
    await sendRunResult(runId, traceId, result);
  } catch (error2) {
    const failure = {
      ok: false,
      error_message: error2 instanceof Error ? error2.message : String(error2),
      stderr: error2 instanceof Error ? error2.message : String(error2)
    };
    completeRunHistoryEntry(resolvedHistoryEntry, failure);
    await sendRunResult(runId, traceId, failure);
  } finally {
    daemonState.pendingRuns.delete(runId);
  }
}
async function sendRunResult(runId, traceId, result) {
  appendAudit(
    "device_invoke_finished",
    {
      runId,
      traceId,
      ok: result.ok !== false,
      error: result.error_message || null
    },
    {
      response_payload: result.response_payload || null,
      stdout: typeof result.stdout === "string" ? result.stdout : null,
      stderr: typeof result.stderr === "string" ? result.stderr : null
    }
  );
  if (!daemonState.websocket || daemonState.websocket.readyState !== WebSocket.OPEN) {
    return;
  }
  sendSocketJson({
    event: "result",
    run_id: runId,
    ok: result.ok !== false,
    response_payload: result.response_payload || null,
    stdout: typeof result.stdout === "string" ? result.stdout : null,
    stderr: typeof result.stderr === "string" ? result.stderr : null,
    error_message: result.error_message || null
  });
}
async function sendRunStarted(runId, traceId) {
  if (!daemonState.websocket || daemonState.websocket.readyState !== WebSocket.OPEN) {
    return;
  }
  sendSocketJson({
    event: "run_started",
    run_id: runId,
    trace_id: traceId
  });
}
async function sendRunProgress(runId, traceId, progress) {
  if (!daemonState.websocket || daemonState.websocket.readyState !== WebSocket.OPEN) {
    return;
  }
  sendSocketJson({
    event: "run_progress",
    run_id: runId,
    trace_id: traceId,
    response_payload: progress.response_payload || null,
    stdout: typeof progress.stdout === "string" ? progress.stdout : null,
    stderr: typeof progress.stderr === "string" ? progress.stderr : null,
    error_message: progress.error_message || null
  });
}
async function executeRemoteManagedProcessRun({
  runId,
  traceId,
  payload,
  historyEntry,
  approvalContext = null
}) {
  const processEntry = createManagedProcess(
    {
      ...payload,
      historyEntry
    },
    {
      kind: "process",
      approvalContext
    }
  );
  const initialProgress = buildManagedProcessProgressUpdate(processEntry);
  updateRunHistoryEntry(historyEntry, {
    response_payload: initialProgress.event.response_payload
  });
  let lastProgressSignature = initialProgress.signature;
  await sendRunProgress(runId, traceId, initialProgress.event);
  while (true) {
    const outcome = await Promise.race([
      processEntry.result_promise.then((result) => ({ kind: "result", result })),
      sleep(REMOTE_MANAGED_PROCESS_PROGRESS_INTERVAL_MS).then(() => ({ kind: "tick" }))
    ]);
    if (outcome.kind === "result") {
      await sendRunResult(runId, traceId, outcome.result);
      return;
    }
    const nextProgress = buildManagedProcessProgressUpdate(processEntry);
    if (nextProgress.signature === lastProgressSignature) {
      continue;
    }
    lastProgressSignature = nextProgress.signature;
    await sendRunProgress(runId, traceId, nextProgress.event);
  }
}
async function sendApprovalRequired(runId, traceId, approval) {
  appendAudit("device_invoke_waiting_approval", {
    runId,
    traceId,
    approvalId: approval.id,
    kind: approval.kind
  });
  if (!daemonState.websocket || daemonState.websocket.readyState !== WebSocket.OPEN) {
    return;
  }
  sendSocketJson({
    event: "approval_required",
    run_id: runId,
    trace_id: traceId,
    kind: approval.kind || null,
    summary: approval.summary || null,
    details: approval.payload || null
  });
}
function validateWritePath(targetPath) {
  assertFilesystemPathAccess(targetPath, "write");
}
function validateReadPath(targetPath) {
  assertFilesystemPathAccess(targetPath, "read");
}
function validateCommandWorkingDirectory(targetPath) {
  assertFilesystemPathAccess(targetPath, "cwd");
}
function validateCommandPayload(payload) {
  const config2 = getConfig();
  const rawCommand = String(payload?.command || "").trim();
  const { command, args, commandInputWasCombined } = normalizeCommandInvocation(
    payload.command,
    payload.args
  );
  const shell = normalizeCommandShell(payload?.shell);
  if (!command) {
    throw new Error("command_required");
  }
  const commandName = import_node_path4.default.basename(command).toLowerCase();
  const commandContext = buildCommandPolicyContext({
    command,
    args,
    rawCommand,
    shell
  });
  const blockedExecutables = (config2.policies?.commands?.blockedExecutables || []).map((item) => String(item).toLowerCase());
  if (commandMatchesAnyPattern(commandContext, blockedExecutables)) {
    throw new Error("command_blocked");
  }
  const allowedExecutables = (config2.policies?.commands?.allowedExecutables || []).map((item) => String(item).toLowerCase());
  if (allowedExecutables.length > 0 && !allowedExecutables.includes(commandName)) {
    throw new Error("command_not_allowlisted");
  }
  if (payload.cwd) {
    validateCommandWorkingDirectory(String(payload.cwd));
  }
  const envEntries = Object.entries(payload.env || {});
  const maxEnvVars = Number(config2.policies?.commands?.maxEnvVars || 64);
  if (envEntries.length > maxEnvVars) {
    throw new Error("too_many_env_vars");
  }
  return {
    command,
    args,
    commandName,
    commandInputWasCombined,
    rawCommand,
    shell
  };
}
async function runExecutor(payload, options = {}) {
  const runType = String(payload.run_type || "");
  if (runType === "command") {
    return executeCommand(payload, options);
  }
  if (runType === "write_file") {
    return writeLocalFile(payload);
  }
  if (runType === "read_file") {
    return readLocalFile(payload);
  }
  if (runType === "watch_path") {
    return watchLocalPath(payload);
  }
  if (runType === "list_watches") {
    return listLocalWatches();
  }
  if (runType === "poll_watch_events") {
    return pollLocalWatchEvents(payload);
  }
  if (runType === "unwatch_path") {
    return unwatchLocalPath(payload);
  }
  if (runType === "start_process") {
    return startLocalProcess(payload);
  }
  if (runType === "list_processes") {
    return listLocalProcesses();
  }
  if (runType === "get_process") {
    return getLocalProcess(payload);
  }
  if (runType === "stop_process") {
    return stopLocalProcess(payload);
  }
  if (runType === "install_skill") {
    return installSkillFromReference(
      {
        skillReference: payload.skill_reference ?? payload.sourcePath,
        installDeps: payload.install_deps === true,
        allowSuspicious: payload.allow_suspicious === true
      },
      { allowLocalPath: false }
    );
  }
  if (runType === "search_skill_catalog") {
    return searchClawHubSkills(payload.query, {
      limit: payload.limit,
      refresh: payload.refresh === true
    });
  }
  if (runType === "inspect_skill") {
    return inspectSkill(payload.skill_name ?? payload.name);
  }
  if (runType === "invoke_skill") {
    return invokeSkill(payload);
  }
  if (runType === "get_mcp_catalog") {
    return getMcpCatalog({
      name: payload.server_name ?? payload.name,
      refresh: payload.refresh === true,
      timeout_ms: payload.timeout_ms ?? payload.timeoutMs
    });
  }
  if (runType === "invoke_mcp_tool") {
    return invokeMcpTool(payload);
  }
  if (runType === "get_clipboard") {
    return {
      ok: true,
      content: await readClipboardNative()
    };
  }
  if (runType === "set_clipboard") {
    await writeClipboardNative(
      payload.content ?? payload.clipboard_content ?? ""
    );
    return {
      ok: true,
      content: String(payload.content ?? payload.clipboard_content ?? "")
    };
  }
  if (runType === "show_notification") {
    await showNotificationNative(payload.title, payload.body || "");
    return {
      ok: true,
      title: String(payload.title || ""),
      body: String(payload.body || "")
    };
  }
  throw new Error("unsupported_run_type");
}
function normalizeExecutorResult(result) {
  if (result === null || typeof result === "undefined") {
    return { ok: true, response_payload: null };
  }
  if (Array.isArray(result)) {
    return { ok: true, response_payload: { items: result } };
  }
  if (typeof result !== "object") {
    return { ok: true, response_payload: { value: result } };
  }
  const normalized = { ...result };
  normalized.ok = normalized.ok !== false;
  if (!Object.prototype.hasOwnProperty.call(normalized, "response_payload")) {
    const { ok, stdout, stderr, error_message, ...rest } = normalized;
    normalized.response_payload = Object.keys(rest).length > 0 ? rest : null;
    normalized.ok = ok !== false;
    normalized.stdout = stdout ?? null;
    normalized.stderr = stderr ?? null;
    normalized.error_message = error_message ?? null;
  }
  return normalized;
}
var IMAGE_EXTENSIONS = /* @__PURE__ */ new Set([
  "jpg",
  "jpeg",
  "png",
  "webp",
  "gif",
  "bmp",
  "heic",
  "heif",
  "tif",
  "tiff"
]);
var MIME_TYPE_BY_EXTENSION = {
  txt: "text/plain",
  md: "text/markdown",
  markdown: "text/markdown",
  csv: "text/csv",
  html: "text/html",
  htm: "text/html",
  xml: "application/xml",
  yaml: "application/yaml",
  yml: "application/yaml",
  json: "application/json",
  pdf: "application/pdf",
  jpg: "image/jpeg",
  jpeg: "image/jpeg",
  png: "image/png",
  webp: "image/webp",
  gif: "image/gif",
  bmp: "image/bmp",
  heic: "image/heic",
  heif: "image/heif",
  tif: "image/tiff",
  tiff: "image/tiff",
  docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
};
function getDocumentFormat(sourcePath) {
  return import_node_path4.default.extname(String(sourcePath || "")).toLowerCase().replace(/^\./, "") || "unknown";
}
function inferMimeTypeFromPath(sourcePath) {
  const format2 = getDocumentFormat(sourcePath);
  if (Object.prototype.hasOwnProperty.call(MIME_TYPE_BY_EXTENSION, format2)) {
    return MIME_TYPE_BY_EXTENSION[format2];
  }
  return "application/octet-stream";
}
function bufferLooksBinary(buffer) {
  if (!buffer || buffer.length === 0) {
    return false;
  }
  const sample = buffer.subarray(0, Math.min(buffer.length, 4096));
  let suspiciousBytes = 0;
  for (const byte of sample) {
    if (byte === 0) {
      return true;
    }
    const isControl = byte < 32 && ![9, 10, 13].includes(byte);
    if (isControl) {
      suspiciousBytes += 1;
    }
  }
  return suspiciousBytes / sample.length > 0.1;
}
function buildManagedProcessPolicy(config2, kind, params = {}) {
  const policy = kind === "process" ? config2.policies?.processes || {} : config2.policies?.commands || {};
  const defaultTimeoutMs = kind === "process" ? 24 * 60 * 60 * 1e3 : 12e4;
  const defaultStoredBytes = kind === "process" ? 16 * 1024 * 1024 : 4 * 1024 * 1024;
  return {
    timeoutMs: clampInteger(
      params?.timeoutMs ?? params?.timeout_ms ?? policy.defaultTimeoutMs,
      {
        fallback: defaultTimeoutMs,
        min: 1,
        max: 7 * 24 * 60 * 60 * 1e3
      }
    ),
    noOutputTimeoutMs: clampInteger(
      params?.noOutputTimeoutMs ?? params?.no_output_timeout_ms ?? policy.defaultNoOutputTimeoutMs,
      {
        fallback: 0,
        min: 0,
        max: 7 * 24 * 60 * 60 * 1e3
      }
    ),
    inlineLimitBytes: clampInteger(policy.maxOutputBytes, {
      fallback: 65536,
      min: 1024,
      max: MAX_PROCESS_OUTPUT_SLICE_BYTES
    }),
    maxStoredBytes: clampInteger(policy.maxStoredBytes, {
      fallback: defaultStoredBytes,
      min: 1024,
      max: 128 * 1024 * 1024
    }),
    forceKillGraceMs: clampInteger(
      params?.forceKillGraceMs ?? params?.force_kill_grace_ms ?? policy.forceKillGraceMs,
      {
        fallback: DEFAULT_PROCESS_FORCE_KILL_GRACE_MS,
        min: 100,
        max: 60 * 1e3
      }
    ),
    detached: kind === "process" && params?.detached === true && process.platform !== "win32"
  };
}
function resolveManagedProcessSummary(params, kind, commandName, command) {
  if (typeof params?.summary === "string" && params.summary.trim()) {
    return params.summary.trim();
  }
  if (kind === "skill") {
    return `skill ${String(params?.skill_name || params?.skillName || commandName || command || "").trim()}`.trim();
  }
  if (kind === "process") {
    return `process ${String(command || commandName || "").trim()}`.trim();
  }
  return `command ${String(command || commandName || "").trim()}`.trim();
}
function buildManagedProcessResult(processEntry) {
  const summary = toProcessSummary(processEntry);
  return {
    ok: summary.status === "completed",
    status: summary.status,
    process_id: summary.id,
    exit_code: summary.exit_code,
    signal: summary.signal,
    timed_out: summary.timed_out,
    error_message: summary.error_message,
    stdout: summary.stdout,
    stderr: summary.stderr,
    combined_output: summary.combined_output,
    stdout_bytes: summary.stdout_bytes,
    stderr_bytes: summary.stderr_bytes,
    combined_bytes: summary.combined_bytes,
    stdout_path: summary.stdout_path,
    stderr_path: summary.stderr_path,
    combined_path: summary.combined_path,
    last_output_at: summary.last_output_at,
    output_truncated: summary.output_truncated,
    discarded_bytes: summary.discarded_bytes,
    response_payload: {
      process: summary,
      exit_code: summary.exit_code,
      signal: summary.signal,
      timed_out: summary.timed_out,
      output_truncated: summary.output_truncated
    }
  };
}
function killManagedProcessTree(processEntry) {
  const pid = processEntry.child?.pid ?? processEntry.pid;
  if (!Number.isFinite(pid) || Number(pid) <= 0) {
    try {
      processEntry.child?.kill("SIGKILL");
    } catch {
    }
    return;
  }
  if (process.platform === "win32") {
    try {
      (0, import_node_child_process.spawn)("taskkill", ["/F", "/T", "/PID", String(pid)], {
        stdio: "ignore",
        detached: true,
        windowsHide: true
      });
      return;
    } catch {
    }
  }
  if (processEntry.detached === true) {
    try {
      process.kill(-pid, "SIGKILL");
      return;
    } catch {
    }
  }
  try {
    processEntry.child?.kill("SIGKILL");
  } catch {
  }
}
function resetManagedProcessNoOutputTimeout(processEntry) {
  if (processEntry.no_output_timeout) {
    clearTimeout(processEntry.no_output_timeout);
    processEntry.no_output_timeout = null;
  }
  if (!processEntry.no_output_timeout_ms || processEntry.finalized) {
    return;
  }
  processEntry.no_output_timeout = setTimeout(() => {
    if (processEntry.status !== "running" && processEntry.status !== "terminating") {
      return;
    }
    requestManagedProcessTermination(processEntry, "SIGTERM", {
      reason: "no-output-timeout",
      markTimedOut: true,
      timeoutCode: "command_no_output_timeout",
      forceKillGraceMs: processEntry.force_kill_grace_ms
    });
  }, processEntry.no_output_timeout_ms);
  processEntry.no_output_timeout.unref?.();
}
function finalizeManagedProcess(processEntry) {
  if (processEntry.finalized) {
    return;
  }
  processEntry.finalized = true;
  cleanupManagedProcessEntry(processEntry);
  const result = buildManagedProcessResult(processEntry);
  if (processEntry.history_entry) {
    completeRunHistoryEntry(processEntry.history_entry, result);
  }
  if (processEntry.resolve_result) {
    const resolve = processEntry.resolve_result;
    processEntry.resolve_result = null;
    resolve(result);
  }
  trimFinishedProcessHistory();
}
function scheduleManagedProcessFinalize(processEntry, immediate = false) {
  if (processEntry.finalized) {
    return;
  }
  if (immediate) {
    if (processEntry.finalize_timeout) {
      clearTimeout(processEntry.finalize_timeout);
      processEntry.finalize_timeout = null;
    }
    finalizeManagedProcess(processEntry);
    return;
  }
  if (processEntry.finalize_timeout) {
    return;
  }
  processEntry.finalize_timeout = setTimeout(() => {
    processEntry.finalize_timeout = null;
    finalizeManagedProcess(processEntry);
  }, PROCESS_STREAM_DRAIN_GRACE_MS);
  processEntry.finalize_timeout.unref?.();
}
function requestManagedProcessTermination(processEntry, signal = "SIGTERM", options = {}) {
  if (!processEntry.child || processEntry.status !== "running" && processEntry.status !== "terminating") {
    return;
  }
  processEntry.termination_reason = options.reason || "manual-stop";
  processEntry.requested_signal = signal;
  processEntry.status = "terminating";
  if (options.markTimedOut) {
    processEntry.timed_out = true;
    processEntry.error_message ||= options.timeoutCode || "command_timeout";
  }
  try {
    if (signal === "SIGKILL") {
      killManagedProcessTree(processEntry);
    } else {
      processEntry.child.kill(signal);
    }
  } catch (error2) {
    processEntry.error_message ||= error2 instanceof Error ? error2.message : String(error2);
    scheduleManagedProcessFinalize(processEntry, true);
    return;
  }
  const forceKillGraceMs = clampInteger(options.forceKillGraceMs, {
    fallback: 0,
    min: 0,
    max: 60 * 1e3
  });
  if (signal !== "SIGKILL" && forceKillGraceMs > 0) {
    if (processEntry.force_kill_timeout) {
      clearTimeout(processEntry.force_kill_timeout);
    }
    processEntry.force_kill_timeout = setTimeout(() => {
      if (processEntry.status === "running" || processEntry.status === "terminating") {
        processEntry.requested_signal = "SIGKILL";
        killManagedProcessTree(processEntry);
      }
    }, forceKillGraceMs);
    processEntry.force_kill_timeout.unref?.();
  }
}
function touchManagedProcessOutput(processEntry, streamName, chunk) {
  appendToProcessOutput(
    processEntry.output,
    streamName,
    chunk.toString("utf8")
  );
  processEntry.last_output_at = (/* @__PURE__ */ new Date()).toISOString();
  resetManagedProcessNoOutputTimeout(processEntry);
}
function createManagedProcess(params, { kind = "process", approvalContext = null } = {}) {
  const config2 = getConfig();
  const payload = {
    command: params?.command,
    args: params?.args,
    cwd: params?.cwd || null,
    env: params?.env || {},
    shell: params?.shell || null
  };
  const {
    command,
    commandName,
    args,
    commandInputWasCombined,
    rawCommand,
    shell
  } = validateCommandPayload(payload);
  const runKind = kind === "skill" ? "skill" : kind === "command" ? "command" : "process";
  const skillName = typeof params?.skill_name === "string" ? params.skill_name.trim() : typeof params?.skillName === "string" ? params.skillName.trim() : null;
  const summary = resolveManagedProcessSummary(
    params,
    kind,
    commandName,
    command
  );
  const cwd = params?.cwd ? import_node_path4.default.resolve(String(params.cwd)) : void 0;
  const mergedEnv = preferInternalCliPath(
    commandName,
    mergeEnvironment(params?.env || {})
  );
  const rewritten = rewritePythonCommandIfNeeded(command, args, {
    cwd,
    env: mergedEnv
  });
  if (rewritten.rewritten) {
    appendDaemonLog("info", "python_command_rewritten", {
      originalCommand: command,
      originalArgs: args,
      newCommand: rewritten.command,
      newArgs: rewritten.args
    });
  }
  const effectiveCommand = rewritten.command;
  const effectiveArgs = rewritten.args;
  const spawnSpec = resolveBatchSpawn(effectiveCommand, effectiveArgs, {
    cwd,
    env: mergedEnv,
    shell,
    rawCommand: rewritten.rewritten ? null : rawCommand,
    commandInputWasCombined: rewritten.rewritten ? false : commandInputWasCombined
  });
  const policy = buildManagedProcessPolicy(config2, kind, params);
  const processId = generateId();
  const output = createProcessOutputStore(processId, {
    inlineLimitBytes: policy.inlineLimitBytes,
    maxStoredBytes: policy.maxStoredBytes
  });
  let child;
  try {
    child = (0, import_node_child_process.spawn)(spawnSpec.command, spawnSpec.args, {
      cwd,
      env: mergedEnv,
      stdio: ["ignore", "pipe", "pipe"],
      shell: false,
      detached: policy.detached,
      windowsHide: true,
      windowsVerbatimArguments: spawnSpec.windowsVerbatimArguments
    });
  } catch (error2) {
    deleteProcessOutputStore(output);
    throw error2;
  }
  const processEntry = {
    id: processId,
    run_kind: runKind,
    summary,
    skill_name: skillName || null,
    pid: child.pid ?? null,
    command,
    args,
    cwd: cwd || null,
    status: "running",
    exit_code: null,
    signal: null,
    started_at: (/* @__PURE__ */ new Date()).toISOString(),
    ended_at: null,
    last_output_at: null,
    error_message: null,
    timed_out: false,
    detached: policy.detached,
    child,
    output,
    timeout: null,
    no_output_timeout: null,
    no_output_timeout_ms: policy.noOutputTimeoutMs,
    force_kill_timeout: null,
    force_kill_grace_ms: policy.forceKillGraceMs,
    finalize_timeout: null,
    termination_reason: null,
    requested_signal: null,
    history_entry: params?.historyEntry || params?.history_entry || null,
    finalized: false,
    resolve_result: null,
    result_promise: null,
    _listenersRemoved: false,
    _onStdout: null,
    _onStderr: null,
    _onError: null,
    _onExit: null,
    _onClose: null
  };
  processEntry.result_promise = new Promise((resolve) => {
    processEntry.resolve_result = resolve;
  });
  const handleStdout = (chunk) => {
    touchManagedProcessOutput(processEntry, "stdout", chunk);
  };
  const handleStderr = (chunk) => {
    touchManagedProcessOutput(processEntry, "stderr", chunk);
  };
  const handleError = (error2) => {
    if (processEntry.finalized) {
      return;
    }
    processEntry.status = processEntry.timed_out ? "timed_out" : "failed";
    processEntry.error_message = error2 instanceof Error ? error2.message : String(error2);
    processEntry.ended_at = (/* @__PURE__ */ new Date()).toISOString();
    scheduleManagedProcessFinalize(processEntry, true);
  };
  const handleExit = (code, signal) => {
    if (processEntry.finalized) {
      return;
    }
    processEntry.exit_code = typeof code === "number" ? code : null;
    processEntry.signal = signal || processEntry.requested_signal || null;
    processEntry.ended_at = (/* @__PURE__ */ new Date()).toISOString();
    if (processEntry.force_kill_timeout) {
      clearTimeout(processEntry.force_kill_timeout);
      processEntry.force_kill_timeout = null;
    }
    if (processEntry.timed_out) {
      processEntry.status = "timed_out";
      processEntry.error_message ||= processEntry.termination_reason === "no-output-timeout" ? "command_no_output_timeout" : "command_timeout";
    } else if (processEntry.signal) {
      processEntry.status = processEntry.termination_reason === "manual-stop" ? "terminated" : "failed";
    } else if ((code ?? 1) === 0) {
      processEntry.status = "completed";
    } else {
      processEntry.status = "failed";
      processEntry.error_message ||= `command_exit_${code ?? -1}`;
    }
    scheduleManagedProcessFinalize(processEntry);
  };
  const handleClose = () => {
    if (processEntry.finalized) {
      return;
    }
    if (processEntry.status !== "running") {
      scheduleManagedProcessFinalize(processEntry, true);
    }
  };
  processEntry._onStdout = handleStdout;
  processEntry._onStderr = handleStderr;
  processEntry._onError = handleError;
  processEntry._onExit = handleExit;
  processEntry._onClose = handleClose;
  child.stdout.on("data", handleStdout);
  child.stderr.on("data", handleStderr);
  child.once("error", handleError);
  child.once("exit", handleExit);
  child.once("close", handleClose);
  processEntry.timeout = setTimeout(() => {
    requestManagedProcessTermination(processEntry, "SIGTERM", {
      reason: "overall-timeout",
      markTimedOut: true,
      timeoutCode: "command_timeout",
      forceKillGraceMs: policy.forceKillGraceMs
    });
  }, policy.timeoutMs);
  processEntry.timeout.unref?.();
  resetManagedProcessNoOutputTimeout(processEntry);
  daemonState.processes.set(processId, processEntry);
  ensureProcessCleanupTimer();
  trimFinishedProcessHistory();
  appendAudit(
    "local_process_started",
    {
      processId,
      pid: processEntry.pid,
      kind,
      commandName
    },
    {
      command,
      args,
      cwd,
      detached: policy.detached,
      timeout_ms: policy.timeoutMs,
      no_output_timeout_ms: policy.noOutputTimeoutMs || null,
      command_input_was_combined: commandInputWasCombined,
      spawn_command: spawnSpec.command,
      spawn_args: spawnSpec.args
    }
  );
  return processEntry;
}
async function waitForManagedProcessResult(processEntry, timeoutMs) {
  if (processEntry.finalized) {
    return buildManagedProcessResult(processEntry);
  }
  const waitTimeoutMs = clampInteger(timeoutMs, {
    fallback: 0,
    min: 0,
    max: 30 * 60 * 1e3
  });
  if (waitTimeoutMs <= 0) {
    return await processEntry.result_promise;
  }
  const timedResult = await Promise.race([
    processEntry.result_promise,
    new Promise((resolve) => {
      const timer = setTimeout(() => resolve(null), waitTimeoutMs);
      timer.unref?.();
    })
  ]);
  return timedResult || buildManagedProcessResult(processEntry);
}
function buildProcessOutputSlice(processEntry, params = {}, defaultStream = "combined") {
  const meta3 = resolveProcessOutputMeta(
    processEntry,
    params?.stream || defaultStream
  );
  const slice = readUtf8FileSlice(
    meta3.path,
    params?.cursor ?? params?.offset ?? 0,
    params?.maxBytes ?? params?.max_bytes ?? 65536
  );
  return {
    process_id: processEntry.id,
    stream: meta3.stream,
    offset: slice.offset,
    next_offset: slice.offset + slice.bytesRead,
    bytes_read: slice.bytesRead,
    total_bytes: meta3.totalBytes,
    has_more: slice.offset + slice.bytesRead < meta3.totalBytes,
    content: slice.content,
    path: meta3.path,
    status: processEntry.status,
    exit_code: processEntry.exit_code ?? null,
    signal: processEntry.signal ?? null,
    timed_out: processEntry.timed_out === true,
    error_message: processEntry.error_message ?? null
  };
}
async function executeCommand(payload, options = {}) {
  const processEntry = createManagedProcess(payload, {
    kind: "command",
    approvalContext: options.approvalContext || null
  });
  return await waitForManagedProcessResult(processEntry);
}
function getRequestedSkillName(payload) {
  return String(payload?.skill_name || payload?.name || "").trim();
}
function getRequestedSkillArgs(payload) {
  return Array.isArray(payload?.skill_args) ? payload.skill_args : Array.isArray(payload?.args) ? payload.args : [];
}
function buildSkillExecutionFailureResult(skillEntry, errorMessage, reason) {
  return {
    ok: false,
    status: "failed",
    process_id: null,
    exit_code: null,
    signal: null,
    timed_out: false,
    error_message: errorMessage,
    stdout: "",
    stderr: "",
    combined_output: "",
    stdout_bytes: 0,
    stderr_bytes: 0,
    combined_bytes: 0,
    stdout_path: null,
    stderr_path: null,
    combined_path: null,
    last_output_at: null,
    output_truncated: false,
    discarded_bytes: 0,
    response_payload: {
      skill_name: skillEntry?.name || null,
      reason: reason || null,
      runtime: skillEntry?.runtime || null,
      skill_md_path: skillEntry?.skillMdPath || null,
      skill_document: skillEntry?.skillDocument || null,
      has_runner: Boolean(skillEntry?.runner?.command)
    }
  };
}
function resolveSkillExecutionPrerequisites(skillName) {
  const skillEntry = inspectSkill(skillName);
  if (skillEntry.runtime?.kind === "knowledge") {
    return {
      skillEntry,
      failure: buildSkillExecutionFailureResult(
        skillEntry,
        "knowledge_skill_not_executable",
        "knowledge"
      )
    };
  }
  if (!skillEntry.runner?.command) {
    return {
      skillEntry,
      failure: buildSkillExecutionFailureResult(
        skillEntry,
        "skill_runner_not_configured",
        "missing_runner"
      )
    };
  }
  return {
    skillEntry,
    failure: null
  };
}
function resolveSkillProcessLaunch(payload, skillEntry = null) {
  const skillName = getRequestedSkillName(payload);
  if (!skillName) {
    throw new Error("skill_name_required");
  }
  const resolvedSkillEntry = skillEntry && skillEntry.name === skillName ? skillEntry : null;
  const runner = resolvedSkillEntry?.runner || readSkillRunner(skillName);
  if (!runner?.command) {
    throw new Error("skill_runner_not_configured");
  }
  return {
    skill_name: skillName,
    command: runner.command,
    args: [
      ...Array.isArray(runner.args) ? runner.args : [],
      ...getRequestedSkillArgs(payload)
    ],
    cwd: runner.cwd || resolvedSkillEntry?.path || import_node_path4.default.join(skillsDir, skillName),
    env: runner.env || {},
    timeout_ms: payload?.timeout_ms || payload?.timeoutMs || null,
    no_output_timeout_ms: payload?.no_output_timeout_ms || payload?.noOutputTimeoutMs || null,
    summary: `skill ${skillName}`
  };
}
function startSkillManagedProcess(payload, { historyRunType = "start_skill" } = {}) {
  const skillName = getRequestedSkillName(payload);
  if (!skillName) {
    throw new Error("skill_name_required");
  }
  const requestedSkillArgs = getRequestedSkillArgs(payload);
  const historyEntry = createRunHistoryEntry({
    runId: null,
    traceId: null,
    payload: {
      run_type: historyRunType,
      skill_name: skillName,
      skill_args: requestedSkillArgs,
      timeout_ms: payload?.timeout_ms || payload?.timeoutMs || null,
      no_output_timeout_ms: payload?.no_output_timeout_ms || payload?.noOutputTimeoutMs || null
    },
    source: "local"
  });
  try {
    const { skillEntry, failure } = resolveSkillExecutionPrerequisites(skillName);
    if (failure) {
      completeRunHistoryEntry(historyEntry, failure);
      return { processEntry: null, historyEntry, result: failure };
    }
    const launch = resolveSkillProcessLaunch(payload, skillEntry);
    const processEntry = createManagedProcess(
      {
        ...launch,
        historyEntry
      },
      { kind: "skill" }
    );
    updateRunHistoryEntry(historyEntry, {
      response_payload: {
        process: toProcessSummary(processEntry),
        process_id: processEntry.id
      }
    });
    return { processEntry, historyEntry, result: null };
  } catch (error2) {
    completeRunHistoryEntry(historyEntry, {
      ok: false,
      status: "failed",
      error_message: error2 instanceof Error ? error2.message : String(error2),
      stderr: error2 instanceof Error ? error2.message : String(error2)
    });
    throw error2;
  }
}
async function writeLocalFile(payload) {
  const config2 = getConfig();
  const targetPath = import_node_path4.default.resolve(String(payload.path || ""));
  const content = String(payload.content || "");
  validateWritePath(targetPath);
  const maxWriteBytes = Number(
    config2.policies?.filesystem?.maxWriteBytes || 1024 * 1024
  );
  if (Buffer.byteLength(content, "utf8") > maxWriteBytes) {
    throw new Error("file_too_large");
  }
  if (import_node_fs2.default.existsSync(targetPath) && payload.overwrite === false) {
    throw new Error("file_exists");
  }
  ensureDir(import_node_path4.default.dirname(targetPath));
  import_node_fs2.default.writeFileSync(targetPath, content, "utf8");
  return {
    ok: true,
    response_payload: {
      path: targetPath,
      bytes_written: Buffer.byteLength(content, "utf8")
    }
  };
}
async function invokeSkill(payload) {
  const { processEntry, result } = startSkillManagedProcess(payload, {
    historyRunType: "invoke_skill"
  });
  if (result) {
    return result;
  }
  return await waitForManagedProcessResult(processEntry);
}
async function runLocalSkill(params) {
  const skillName = String(params?.name || "");
  if (!skillName) {
    throw new Error("skill_name_required");
  }
  const payload = {
    skill_name: skillName,
    skill_args: Array.isArray(params?.args) ? params.args : [],
    timeout_ms: params?.timeoutMs || null,
    no_output_timeout_ms: params?.noOutputTimeoutMs || params?.no_output_timeout_ms || null
  };
  return await invokeSkill(payload);
}
function startLocalSkillProcess(params) {
  const { processEntry, result } = startSkillManagedProcess({
    skill_name: params?.name,
    skill_args: Array.isArray(params?.args) ? params.args : [],
    timeoutMs: params?.timeoutMs || null,
    noOutputTimeoutMs: params?.noOutputTimeoutMs || params?.no_output_timeout_ms || null
  });
  if (!processEntry) {
    throw new Error(result?.error_message || "skill_not_executable");
  }
  return toProcessSummary(processEntry);
}
async function runLocalMcpTool(params) {
  const payload = {
    server_name: String(params?.server_name || params?.serverName || ""),
    tool_name: String(params?.tool_name || params?.toolName || ""),
    arguments: coerceMcpArguments(params?.arguments),
    timeout_ms: params?.timeout_ms || params?.timeoutMs || null
  };
  if (!payload.server_name) {
    throw new Error("mcp_server_name_required");
  }
  if (!payload.tool_name) {
    throw new Error("mcp_tool_name_required");
  }
  const historyEntry = createRunHistoryEntry({
    runId: null,
    traceId: null,
    payload: {
      run_type: "invoke_mcp_tool",
      ...payload
    },
    source: "local"
  });
  try {
    const result = await invokeMcpTool(payload);
    completeRunHistoryEntry(historyEntry, result);
    return result;
  } catch (error2) {
    const failure = {
      ok: false,
      error_message: error2 instanceof Error ? error2.message : String(error2),
      stderr: error2 instanceof Error ? error2.message : String(error2)
    };
    completeRunHistoryEntry(historyEntry, failure);
    throw error2;
  }
}
async function runLocalCommand(params) {
  const payload = {
    run_type: "command",
    command: String(params?.command || ""),
    args: typeof params?.args === "undefined" ? [] : params.args,
    cwd: params?.cwd || null,
    env: params?.env || {},
    shell: params?.shell || null,
    timeout_ms: params?.timeoutMs || params?.timeout_ms || null,
    no_output_timeout_ms: params?.noOutputTimeoutMs || params?.no_output_timeout_ms || null
  };
  const historyEntry = createRunHistoryEntry({
    runId: null,
    traceId: null,
    payload,
    source: "local"
  });
  try {
    const result = await executeCommand(payload);
    completeRunHistoryEntry(historyEntry, result);
    return result;
  } catch (error2) {
    const failure = normalizeExecutorResult({
      ok: false,
      status: "failed",
      process_id: null,
      exit_code: null,
      signal: null,
      timed_out: false,
      error_message: error2 instanceof Error ? error2.message : String(error2),
      stdout: "",
      stderr: error2 instanceof Error ? error2.message : String(error2),
      combined_output: "",
      stdout_bytes: 0,
      stderr_bytes: 0,
      combined_bytes: 0,
      stdout_path: null,
      stderr_path: null,
      combined_path: null,
      last_output_at: null,
      output_truncated: false,
      discarded_bytes: 0
    });
    completeRunHistoryEntry(historyEntry, failure);
    return failure;
  }
}
function readLocalFile(params) {
  const targetPath = import_node_path4.default.resolve(String(params?.path || ""));
  if (!targetPath) {
    throw new Error("path_required");
  }
  validateReadPath(targetPath);
  if (!import_node_fs2.default.existsSync(targetPath)) {
    throw new Error("file_not_found");
  }
  const stats = import_node_fs2.default.statSync(targetPath);
  if (!stats.isFile()) {
    throw new Error("path_not_file");
  }
  const config2 = getConfig();
  const maxReadBytes = Number(
    params?.maxBytes || params?.max_bytes || config2.policies?.filesystem?.maxReadBytes || 1024 * 1024
  );
  if (stats.size > maxReadBytes) {
    throw new Error("file_too_large");
  }
  const requestedEncoding = String(params?.encoding || "utf8").toLowerCase();
  const buffer = import_node_fs2.default.readFileSync(targetPath);
  const extension = getDocumentFormat(targetPath);
  const mimeType = inferMimeTypeFromPath(targetPath);
  const isImage = IMAGE_EXTENSIONS.has(extension);
  const looksBinary = bufferLooksBinary(buffer);
  const shouldReturnBase64 = requestedEncoding === "base64" || isImage || looksBinary;
  if (shouldReturnBase64) {
    return {
      ok: true,
      path: targetPath,
      encoding: "base64",
      requested_encoding: requestedEncoding,
      content: buffer.toString("base64"),
      bytes: buffer.length,
      modified_at: stats.mtime.toISOString(),
      mime_type: mimeType,
      media_kind: isImage ? "image" : "binary"
    };
  }
  return {
    ok: true,
    path: targetPath,
    encoding: "utf8",
    requested_encoding: requestedEncoding,
    content: buffer.toString("utf8"),
    bytes: buffer.length,
    modified_at: stats.mtime.toISOString(),
    mime_type: mimeType,
    media_kind: "text"
  };
}
async function writeLocalFileRpc(params) {
  const payload = {
    run_type: "write_file",
    path: params?.path,
    content: params?.content ?? "",
    overwrite: params?.overwrite !== false
  };
  const historyEntry = createRunHistoryEntry({
    runId: null,
    traceId: null,
    payload,
    source: "local"
  });
  try {
    const result = await writeLocalFile(payload);
    completeRunHistoryEntry(historyEntry, result);
    return result;
  } catch (error2) {
    const failure = {
      ok: false,
      error_message: error2 instanceof Error ? error2.message : String(error2),
      stderr: error2 instanceof Error ? error2.message : String(error2)
    };
    completeRunHistoryEntry(historyEntry, failure);
    throw error2;
  }
}
function watchLocalPath(params) {
  const config2 = getConfig();
  if (config2.policies?.filesystem?.allowWatch === false) {
    throw new Error("file_watch_disabled");
  }
  const targetPath = import_node_path4.default.resolve(String(params?.path || ""));
  if (!targetPath) {
    throw new Error("path_required");
  }
  validateReadPath(targetPath);
  const recursive = params?.recursive === true;
  const watchId = generateId();
  const watcher = import_node_fs2.default.watch(targetPath, { recursive }, (eventType, filename) => {
    pushWatchEvent({
      watch_id: watchId,
      path: targetPath,
      event_type: String(eventType || "change"),
      filename: filename ? String(filename) : null
    });
  });
  daemonState.fileWatches.set(watchId, {
    id: watchId,
    path: targetPath,
    recursive,
    created_at: (/* @__PURE__ */ new Date()).toISOString(),
    watcher
  });
  return listLocalWatches().find((item) => item.id === watchId);
}
function listLocalWatches() {
  return Array.from(daemonState.fileWatches.values()).map((item) => ({
    id: item.id,
    path: item.path,
    recursive: item.recursive,
    created_at: item.created_at
  }));
}
function pollLocalWatchEvents(params) {
  const watchId = params?.watchId ? String(params.watchId) : params?.watch_id ? String(params.watch_id) : null;
  const clear = params?.clear !== false;
  const events = daemonState.fileWatchEvents.filter(
    (item) => !watchId || item.watch_id === watchId
  );
  if (clear) {
    daemonState.fileWatchEvents = daemonState.fileWatchEvents.filter(
      (item) => watchId && item.watch_id !== watchId
    );
  }
  return events;
}
function unwatchLocalPath(params) {
  const watchId = String(params?.watchId || params?.watch_id || "");
  const watchEntry = daemonState.fileWatches.get(watchId);
  if (!watchEntry) {
    throw new Error("watch_not_found");
  }
  watchEntry.watcher.close();
  daemonState.fileWatches.delete(watchId);
  daemonState.fileWatchEvents = daemonState.fileWatchEvents.filter(
    (item) => item.watch_id !== watchId
  );
  return { ok: true, watch_id: watchId };
}
function startLocalProcess(params) {
  const payload = {
    run_type: "start_process",
    command: String(params?.command || ""),
    args: typeof params?.args === "undefined" ? [] : params.args,
    cwd: params?.cwd || null,
    env: params?.env || {},
    shell: params?.shell || null,
    timeout_ms: params?.timeoutMs || params?.timeout_ms || null,
    no_output_timeout_ms: params?.noOutputTimeoutMs || params?.no_output_timeout_ms || null,
    detached: params?.detached === true
  };
  const historyEntry = createRunHistoryEntry({
    runId: null,
    traceId: null,
    payload,
    source: "local"
  });
  try {
    const processEntry = createManagedProcess(
      { ...params, historyEntry },
      { kind: "process" }
    );
    updateRunHistoryEntry(historyEntry, {
      response_payload: {
        process: toProcessSummary(processEntry),
        process_id: processEntry.id
      }
    });
    return toProcessSummary(processEntry);
  } catch (error2) {
    completeRunHistoryEntry(historyEntry, {
      ok: false,
      status: "failed",
      error_message: error2 instanceof Error ? error2.message : String(error2),
      stderr: error2 instanceof Error ? error2.message : String(error2)
    });
    throw error2;
  }
}
function listLocalProcesses() {
  return Array.from(daemonState.processes.values()).map((item) => toProcessSummary(item)).sort((a, b) => String(b.started_at).localeCompare(String(a.started_at)));
}
function getLocalProcess(params) {
  return toProcessSummary(
    getManagedProcess(params?.processId || params?.process_id)
  );
}
async function waitLocalProcess(params) {
  const processEntry = getManagedProcess(
    params?.processId || params?.process_id
  );
  return await waitForManagedProcessResult(
    processEntry,
    params?.timeoutMs || params?.timeout_ms || 0
  );
}
function pollLocalProcessOutput(params) {
  const processEntry = getManagedProcess(
    params?.processId || params?.process_id
  );
  return buildProcessOutputSlice(processEntry, params, "combined");
}
function readLocalProcessLog(params) {
  const processEntry = getManagedProcess(
    params?.processId || params?.process_id
  );
  return buildProcessOutputSlice(
    processEntry,
    params,
    params?.stream || "combined"
  );
}
function stopLocalProcess(params) {
  const processEntry = getManagedProcess(
    params?.processId || params?.process_id
  );
  if (processEntry.status !== "running" && processEntry.status !== "terminating") {
    return toProcessSummary(processEntry);
  }
  const signal = String(params?.signal || "SIGTERM");
  requestManagedProcessTermination(processEntry, signal, {
    reason: "manual-stop",
    forceKillGraceMs: signal === "SIGKILL" ? 0 : processEntry.force_kill_grace_ms
  });
  appendAudit("local_process_stopped", {
    processId: processEntry.id,
    pid: processEntry.pid,
    signal
  });
  return toProcessSummary(processEntry);
}
async function getClipboardRpc() {
  const historyEntry = createRunHistoryEntry({
    runId: null,
    traceId: null,
    payload: { run_type: "get_clipboard" },
    source: "local"
  });
  try {
    const content = await readClipboardNative();
    const result = { ok: true, content };
    completeRunHistoryEntry(historyEntry, normalizeExecutorResult(result));
    return result;
  } catch (error2) {
    const failure = {
      ok: false,
      error_message: error2 instanceof Error ? error2.message : String(error2)
    };
    completeRunHistoryEntry(historyEntry, normalizeExecutorResult(failure));
    throw error2;
  }
}
async function setClipboardRpc(params) {
  const content = String(params?.content ?? "");
  const historyEntry = createRunHistoryEntry({
    runId: null,
    traceId: null,
    payload: { run_type: "set_clipboard", content },
    source: "local"
  });
  try {
    await writeClipboardNative(content);
    const result = { ok: true, content };
    completeRunHistoryEntry(historyEntry, normalizeExecutorResult(result));
    return result;
  } catch (error2) {
    const failure = {
      ok: false,
      error_message: error2 instanceof Error ? error2.message : String(error2)
    };
    completeRunHistoryEntry(historyEntry, normalizeExecutorResult(failure));
    throw error2;
  }
}
async function showNotificationRpc(params) {
  const title = String(params?.title || "");
  const body = String(params?.body || "");
  const historyEntry = createRunHistoryEntry({
    runId: null,
    traceId: null,
    payload: { run_type: "show_notification", title, body },
    source: "local"
  });
  try {
    await showNotificationNative(title, body);
    const result = { ok: true, title, body };
    completeRunHistoryEntry(historyEntry, normalizeExecutorResult(result));
    return result;
  } catch (error2) {
    const failure = {
      ok: false,
      error_message: error2 instanceof Error ? error2.message : String(error2)
    };
    completeRunHistoryEntry(historyEntry, normalizeExecutorResult(failure));
    throw error2;
  }
}
function splitCommandText(value) {
  const text = String(value || "").trim();
  if (!text) {
    return [];
  }
  const tokens = [];
  let current = "";
  let quote = null;
  for (let index = 0; index < text.length; index += 1) {
    const char = text[index];
    if (quote) {
      if (char === quote) {
        quote = null;
        continue;
      }
      if (char === "\\" && quote === '"' && index + 1 < text.length && (text[index + 1] === quote || text[index + 1] === "\\")) {
        current += text[index + 1];
        index += 1;
        continue;
      }
      current += char;
      continue;
    }
    if (char === '"' || char === "'") {
      quote = char;
      continue;
    }
    if (/\s/.test(char)) {
      if (current) {
        tokens.push(current);
        current = "";
      }
      continue;
    }
    current += char;
  }
  if (current) {
    tokens.push(current);
  }
  return tokens;
}
function normalizeCommandInvocation(commandValue, argsValue) {
  const rawCommand = String(commandValue || "").trim();
  let args = [];
  if (Array.isArray(argsValue)) {
    args = argsValue.map((item) => String(item));
  } else if (typeof argsValue === "string") {
    args = splitCommandText(argsValue);
  }
  if (!rawCommand) {
    return {
      command: "",
      args,
      commandInputWasCombined: false
    };
  }
  if (args.length > 0) {
    return {
      command: rawCommand,
      args,
      commandInputWasCombined: false
    };
  }
  const tokens = splitCommandText(rawCommand);
  if (tokens.length > 1) {
    return {
      command: tokens[0],
      args: tokens.slice(1),
      commandInputWasCombined: true
    };
  }
  return {
    command: tokens[0] || rawCommand,
    args: [],
    commandInputWasCombined: false
  };
}
function mergeEnvironment(overrides = {}) {
  const merged = {
    ...process.env
  };
  if (process.platform !== "win32") {
    return {
      ...merged,
      ...overrides || {}
    };
  }
  for (const [name, value] of Object.entries(overrides || {})) {
    const existingKey = Object.keys(merged).find(
      (candidate) => candidate.toLowerCase() === String(name).toLowerCase()
    );
    if (existingKey) {
      delete merged[existingKey];
    }
    merged[name] = value;
  }
  return merged;
}
function getPathEnvironmentValue(env = process.env) {
  return env.PATH || env.Path || "";
}
function getInternalCliDirFromEnv(env = process.env) {
  const envKey = Object.keys(env || {}).find(
    (candidate) => candidate.toLowerCase() === "picktime_internal_cli_dir"
  );
  const rawValue = envKey ? env[envKey] : "";
  if (typeof rawValue === "string" && rawValue.trim()) {
    return import_node_path4.default.resolve(rawValue.trim());
  }
  return internalCliDir;
}
function preferInternalCliPath(commandName, env) {
  if (process.platform !== "win32") {
    return env;
  }
  const normalizedCommandName = String(commandName || "").trim().toLowerCase();
  if (!normalizedCommandName || !["picktime", "picktime-agentd"].includes(normalizedCommandName)) {
    return env;
  }
  const preferredCliDir = getInternalCliDirFromEnv(env);
  const wrapperPath = import_node_path4.default.join(
    preferredCliDir,
    `${normalizedCommandName}.cmd`
  );
  if (!import_node_fs2.default.existsSync(wrapperPath)) {
    return env;
  }
  const currentPath = getPathEnvironmentValue(env);
  return mergeEnvironment({
    ...env,
    PATH: currentPath ? `${preferredCliDir}${import_node_path4.default.delimiter}${currentPath}` : preferredCliDir
  });
}
function getWindowsPathExtensions(env = process.env) {
  const raw = String(env.PATHEXT || env.Pathext || ".COM;.EXE;.BAT;.CMD");
  const extensions = raw.split(";").map((item) => item.trim().toLowerCase()).filter(Boolean);
  return extensions.length > 0 ? [...new Set(extensions)] : [".com", ".exe", ".bat", ".cmd"];
}
function hasExplicitPath(command) {
  return /[\\/]/.test(String(command || ""));
}
function resolveWindowsCommandPath(command, options = {}) {
  const rawCommand = String(command || "").trim();
  if (!rawCommand) {
    return null;
  }
  const env = mergeEnvironment(options.env || {});
  const cwd = options.cwd ? import_node_path4.default.resolve(String(options.cwd)) : process.cwd();
  const extension = import_node_path4.default.extname(rawCommand);
  const suffixes = extension ? [""] : getWindowsPathExtensions(env);
  const candidates = [];
  const pushCandidate = (candidate) => {
    if (!candidate || candidates.includes(candidate)) {
      return;
    }
    candidates.push(candidate);
  };
  if (import_node_path4.default.isAbsolute(rawCommand)) {
    pushCandidate(rawCommand);
  } else if (hasExplicitPath(rawCommand) || rawCommand.startsWith(".")) {
    pushCandidate(import_node_path4.default.resolve(cwd, rawCommand));
  } else {
    for (const dir of getPathEnvironmentValue(env).split(import_node_path4.default.delimiter).map((item) => item.trim()).filter(Boolean)) {
      pushCandidate(import_node_path4.default.join(dir, rawCommand));
    }
  }
  for (const candidate of candidates) {
    for (const suffix of suffixes) {
      const resolved = suffix ? `${candidate}${suffix}` : candidate;
      if (!import_node_fs2.default.existsSync(resolved)) {
        continue;
      }
      try {
        if (import_node_fs2.default.statSync(resolved).isFile()) {
          return resolved;
        }
      } catch {
      }
    }
  }
  return null;
}
function isWindowsBatchCommand(command) {
  return process.platform === "win32" && WINDOWS_BATCH_EXTENSIONS.has(
    import_node_path4.default.extname(String(command || "")).toLowerCase()
  );
}
function isWindowsShellBuiltin(command) {
  if (process.platform !== "win32") {
    return false;
  }
  const normalizedCommand = import_node_path4.default.basename(String(command || "").trim()).toLowerCase();
  return Boolean(normalizedCommand) && WINDOWS_SHELL_BUILTINS.has(normalizedCommand);
}
function normalizeCommandShell(value) {
  const normalizedValue = String(value || "auto").trim().toLowerCase();
  if (!normalizedValue || normalizedValue === "auto") {
    return "auto";
  }
  if (!SUPPORTED_COMMAND_SHELLS.has(normalizedValue)) {
    throw new Error("command_shell_invalid");
  }
  return normalizedValue;
}
function quotePowerShellToken(value) {
  return `'${String(value || "").replaceAll("'", "''")}'`;
}
function buildPowerShellCommand(command, args = [], options = {}) {
  if (options.rawCommand) {
    return String(options.rawCommand).trim();
  }
  return [
    "&",
    quotePowerShellToken(command),
    ...args.map((item) => quotePowerShellToken(item))
  ].join(" ");
}
function resolveExplicitShellSpawn(command, args = [], options = {}) {
  const shell = normalizeCommandShell(options.shell);
  if (shell === "auto") {
    return null;
  }
  const normalizedCommand = String(command || "").trim();
  const normalizedArgs = Array.isArray(args) ? args.map((item) => String(item)) : [];
  const env = mergeEnvironment(options.env || {});
  const rawCommand = String(options.rawCommand || "").trim();
  const useRawCommand = Boolean(
    rawCommand && options.commandInputWasCombined === true
  );
  if (shell === "cmd") {
    if (process.platform !== "win32") {
      throw new Error("command_shell_unsupported:cmd");
    }
    return {
      command: env.ComSpec || process.env.ComSpec || "cmd.exe",
      args: useRawCommand ? ["/d", "/s", "/c", rawCommand] : ["/d", "/s", "/c", normalizedCommand, ...normalizedArgs],
      windowsVerbatimArguments: false
    };
  }
  return {
    command: shell === "pwsh" ? "pwsh" : process.platform === "win32" ? "powershell.exe" : "powershell",
    args: [
      "-NoLogo",
      "-NoProfile",
      "-NonInteractive",
      "-Command",
      buildPowerShellCommand(normalizedCommand, normalizedArgs, {
        rawCommand: useRawCommand ? rawCommand : null
      })
    ],
    windowsVerbatimArguments: false
  };
}
function resolveBatchSpawn(command, args = [], options = {}) {
  const normalizedCommand = String(command || "").trim();
  const normalizedArgs = Array.isArray(args) ? args.map((item) => String(item)) : [];
  const explicitShellSpawn = resolveExplicitShellSpawn(
    normalizedCommand,
    normalizedArgs,
    options
  );
  if (explicitShellSpawn) {
    return explicitShellSpawn;
  }
  if (process.platform !== "win32") {
    return {
      command: normalizedCommand,
      args: normalizedArgs,
      windowsVerbatimArguments: false
    };
  }
  const env = mergeEnvironment(options.env || {});
  if (!hasExplicitPath(normalizedCommand) && isWindowsShellBuiltin(normalizedCommand)) {
    return {
      command: env.ComSpec || process.env.ComSpec || "cmd.exe",
      args: ["/d", "/s", "/c", normalizedCommand, ...normalizedArgs],
      windowsVerbatimArguments: false
    };
  }
  const resolvedCommand = resolveWindowsCommandPath(normalizedCommand, options) || normalizedCommand;
  if (!isWindowsBatchCommand(resolvedCommand)) {
    return {
      command: resolvedCommand,
      args: normalizedArgs,
      windowsVerbatimArguments: false
    };
  }
  return {
    command: env.ComSpec || process.env.ComSpec || "cmd.exe",
    args: ["/d", "/s", "/c", "call", resolvedCommand, ...normalizedArgs],
    windowsVerbatimArguments: false
  };
}
async function inspectConfiguredMcpServer(server2, options = {}) {
  const base = {
    name: server2.name,
    label: typeof server2.label === "string" && server2.label.trim() || server2.name,
    description: typeof server2.description === "string" && server2.description.trim() || describeMcpServer(server2),
    enabled: server2.enabled !== false,
    source: server2.source || "managed",
    transport: normalizeMcpServerTransport(server2),
    command: server2.command || null,
    args: Array.isArray(server2.args) ? server2.args.map((item) => String(item)) : [],
    cwd: server2.cwd || null,
    url: server2.url || null
  };
  if (base.enabled === false) {
    return {
      ...base,
      discovery_state: "disabled",
      cached: false,
      discovered: false,
      discovered_at: null,
      protocol_version: null,
      server_info: null,
      server_capabilities: null,
      instructions: null,
      tools: [],
      tool_count: 0,
      prompts: [],
      prompt_count: 0,
      resources: [],
      resource_count: 0,
      resource_templates: [],
      resource_template_count: 0,
      stderr: null,
      mcp_session: null,
      error: null
    };
  }
  if (options?.refresh !== true) {
    const cached2 = readCachedMcpDiscovery(server2.name, server2);
    if (cached2) {
      return cached2;
    }
  }
  try {
    const discovery = await mcpRuntime.discoverServer(server2, {
      timeoutMs: Number(options?.timeout_ms || options?.timeoutMs || 12e4)
    });
    const tools = discovery.tools.map(normalizeMcpTool).filter((tool) => tool.name);
    const prompts = discovery.prompts.map(normalizeMcpPrompt).filter((prompt) => prompt.name);
    const resources = discovery.resources.filter((resource) => resource?.uri).map(normalizeMcpResource).filter((resource) => resource.uri);
    const resourceTemplates = discovery.resourceTemplates.map(normalizeMcpResourceTemplate).filter((resourceTemplate) => resourceTemplate.uri_template);
    return writeCachedMcpDiscovery(server2.name, server2, {
      ...base,
      discovery_state: "ready",
      cached: false,
      discovered: true,
      discovered_at: (/* @__PURE__ */ new Date()).toISOString(),
      protocol_version: discovery.protocolVersion,
      server_info: normalizeMcpServerInfo(discovery.serverInfo),
      server_capabilities: discovery.serverCapabilities || null,
      instructions: typeof discovery.instructions === "string" && discovery.instructions.trim() ? discovery.instructions.trim() : null,
      tools,
      tool_count: tools.length,
      prompts,
      prompt_count: prompts.length,
      resources,
      resource_count: resources.length,
      resource_templates: resourceTemplates,
      resource_template_count: resourceTemplates.length,
      stderr: discovery.stderr ? truncateOutput(discovery.stderr, 65536) : null,
      mcp_session: discovery.session || null,
      error: null
    });
  } catch (error2) {
    return writeCachedMcpDiscovery(server2.name, server2, {
      ...base,
      discovery_state: "error",
      cached: false,
      discovered: false,
      discovered_at: (/* @__PURE__ */ new Date()).toISOString(),
      protocol_version: null,
      server_info: null,
      server_capabilities: null,
      instructions: null,
      tools: [],
      tool_count: 0,
      prompts: [],
      prompt_count: 0,
      resources: [],
      resource_count: 0,
      resource_templates: [],
      resource_template_count: 0,
      stderr: null,
      mcp_session: null,
      error: normalizeMcpCatalogError(error2)
    });
  }
}
async function getMcpCatalog(params) {
  const targetName = String(params?.name || params?.serverName || "").trim();
  const servers = listManagedMcpServers();
  const filteredServers = targetName ? servers.filter((server2) => server2.name === targetName) : servers;
  if (targetName && filteredServers.length === 0) {
    throw new Error("mcp_server_not_found");
  }
  const catalogServers = [];
  for (const server2 of filteredServers) {
    catalogServers.push(await inspectConfiguredMcpServer(server2, params));
  }
  return {
    generated_at: (/* @__PURE__ */ new Date()).toISOString(),
    servers: catalogServers
  };
}
async function invokeMcpTool(payload) {
  const serverName = String(payload.server_name || payload.serverName || "");
  const toolName = String(payload.tool_name || payload.toolName || "");
  const timeoutMs = Number(payload.timeout_ms || payload.timeoutMs || 12e4);
  const config2 = loadMcpConfig();
  const server2 = config2.mcpServers?.[serverName];
  if (!server2 || server2.enabled === false) {
    throw new Error("mcp_server_not_found");
  }
  if (!toolName) {
    throw new Error("mcp_tool_name_required");
  }
  if (!server2.url && !server2.command) {
    throw new Error("mcp_server_command_required");
  }
  return mcpRuntime.invokeTool(
    {
      ...server2,
      name: serverName
    },
    toolName,
    coerceMcpArguments(payload.arguments),
    { timeoutMs }
  );
}
function getLogTail(lines = 100) {
  try {
    const raw = import_node_fs2.default.readFileSync(daemonLogPath, "utf8");
    return raw.trim().split(/\r?\n/).slice(-Math.max(1, Number(lines))).join("\n");
  } catch {
    return "";
  }
}
async function handleRpc(method, params) {
  if (method === "ping") {
    return { ok: true, pid: process.pid };
  }
  if (method === "get_status") {
    return buildStatus();
  }
  if (method === "doctor") {
    const managedPython = await getManagedPythonStatus();
    return {
      ok: true,
      checks: [
        { name: "state_dir", ok: import_node_fs2.default.existsSync(stateDir), path: stateDir },
        { name: "config", ok: import_node_fs2.default.existsSync(configPath), path: configPath },
        { name: "mcp", ok: import_node_fs2.default.existsSync(mcpConfigPath), path: mcpConfigPath },
        { name: "runtime", ok: import_node_fs2.default.existsSync(runtimePath), path: runtimePath }
      ],
      managed_python: managedPython
    };
  }
  if (method === "list_skills") {
    return listSkills();
  }
  if (method === "inspect_skill") {
    return inspectSkill(params?.name);
  }
  if (method === "install_skill") {
    return installSkillFromReference(
      {
        skillReference: params?.skillReference ?? params?.sourcePath,
        allowSuspicious: params?.allowSuspicious === true,
        installDeps: params?.installDeps === true
      },
      { allowLocalPath: true }
    );
  }
  if (method === "uninstall_skill") {
    return uninstallSkill(params?.name);
  }
  if (method === "set_skill_enabled") {
    return {
      ok: true,
      skill: setSkillEnabled(params?.name, params?.enabled !== false)
    };
  }
  if (method === "set_skill_runner") {
    return {
      ok: true,
      skill: setSkillRunner(params?.name, params?.runner || null)
    };
  }
  if (method === "setup_python_skill_env") {
    return {
      ok: true,
      skill: await setupPythonSkillEnvironment(params?.name),
      managed_python: await getManagedPythonStatus()
    };
  }
  if (method === "ensure_managed_python") {
    return {
      ok: true,
      managed_python: await ensureManagedPythonRuntime()
    };
  }
  if (method === "get_capability_preferences") {
    return getCapabilityPreferences(getConfig());
  }
  if (method === "update_capability_preferences") {
    const capabilityPreferences = setCapabilityPreferences({
      profile: params?.profile,
      enabledPacks: params?.enabledPacks
    });
    await triggerCapabilitySync();
    return { ok: true, capability_preferences: capabilityPreferences };
  }
  if (method === "run_skill") {
    return runLocalSkill(params);
  }
  if (method === "start_skill_process") {
    return startLocalSkillProcess(params);
  }
  if (method === "run_mcp_tool") {
    return runLocalMcpTool(params);
  }
  if (method === "run_local_command") {
    return runLocalCommand(params);
  }
  if (method === "read_file") {
    return readLocalFile(params);
  }
  if (method === "write_file") {
    return writeLocalFileRpc(params);
  }
  if (method === "watch_path") {
    return watchLocalPath(params);
  }
  if (method === "list_watches") {
    return listLocalWatches();
  }
  if (method === "poll_watch_events") {
    return pollLocalWatchEvents(params);
  }
  if (method === "unwatch_path") {
    return unwatchLocalPath(params);
  }
  if (method === "start_process") {
    return startLocalProcess(params);
  }
  if (method === "list_processes") {
    return listLocalProcesses();
  }
  if (method === "get_process") {
    return getLocalProcess(params);
  }
  if (method === "wait_process") {
    return waitLocalProcess(params);
  }
  if (method === "poll_process_output") {
    return pollLocalProcessOutput(params);
  }
  if (method === "read_process_log") {
    return readLocalProcessLog(params);
  }
  if (method === "stop_process") {
    return stopLocalProcess(params);
  }
  if (method === "get_clipboard") {
    return getClipboardRpc();
  }
  if (method === "set_clipboard") {
    return setClipboardRpc(params);
  }
  if (method === "show_notification") {
    return showNotificationRpc(params);
  }
  if (method === "list_mcp_servers") {
    return listMcpServers();
  }
  if (method === "search_clawhub_skills") {
    return searchClawHubSkills(params?.query, {
      limit: params?.limit,
      refresh: params?.refresh === true
    });
  }
  if (method === "search_smithery_mcp_servers") {
    return searchSmitheryMcpServers(params);
  }
  if (method === "get_smithery_mcp_server_details") {
    return getSmitheryMcpServerDetails(params);
  }
  if (method === "install_smithery_mcp_server") {
    return installSmitheryMcpServer(params);
  }
  if (method === "inspect_mcp_server") {
    return inspectMcpServer(params?.name);
  }
  if (method === "get_mcp_catalog") {
    return getMcpCatalog(params);
  }
  if (method === "add_mcp_stdio") {
    return saveMcpServer(params?.name, {
      command: params?.command,
      args: Array.isArray(params?.args) ? params.args : [],
      cwd: params?.cwd || null,
      env: params?.env || null,
      enabled: true
    });
  }
  if (method === "add_mcp_http") {
    return saveMcpServer(params?.name, {
      url: params?.url,
      headers: params?.headers || null,
      enabled: true
    });
  }
  if (method === "remove_mcp_server") {
    const config2 = loadMcpConfig();
    delete config2.mcpServers?.[params?.name];
    saveMcpConfig(config2);
    clearMcpDiscoveryCache(params?.name, { closeSession: true });
    triggerCapabilitySync();
    return { ok: true, removed: params?.name };
  }
  if (method === "set_mcp_enabled") {
    const config2 = loadMcpConfig();
    if (!config2.mcpServers?.[params?.name]) {
      throw new Error("mcp_server_not_found");
    }
    config2.mcpServers[params.name].enabled = params?.enabled !== false;
    saveMcpConfig(config2);
    clearMcpDiscoveryCache(params?.name, { closeSession: true });
    triggerCapabilitySync();
    return { ok: true, name: params.name, enabled: params?.enabled !== false };
  }
  if (method === "pair_device") {
    return pairDevice(params);
  }
  if (method === "set_device_credentials") {
    return setDeviceCredentials(params);
  }
  if (method === "clear_device_credentials") {
    return clearDeviceCredentials();
  }
  if (method === "get_device_status") {
    return (await buildStatus()).device;
  }
  if (method === "list_approvals") {
    return listApprovals();
  }
  if (method === "list_run_history") {
    return listRunHistory(params?.limit);
  }
  if (method === "resolve_approval") {
    return resolveApproval(params);
  }
  if (method === "get_logs") {
    return { ok: true, logs: getLogTail(params?.lines) };
  }
  if (method === "sync_capabilities") {
    await triggerCapabilitySync();
    return { ok: true };
  }
  if (method === "shutdown") {
    setTimeout(() => {
      shutdown().catch(() => {
        process.exit(0);
      });
    }, 10);
    return { ok: true };
  }
  throw new Error("method_not_found");
}
var server = import_node_http.default.createServer(async (req, res) => {
  try {
    if (req.method === "OPTIONS") {
      res.writeHead(204, {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "Content-Type, X-Picktime-Token",
        "Access-Control-Allow-Methods": "GET,POST,OPTIONS"
      });
      res.end();
      return;
    }
    if (req.method === "GET" && req.url === "/health") {
      jsonResponse(res, 200, {
        ok: true,
        pid: process.pid,
        startedAt: daemonState.startedAt
      });
      return;
    }
    assertAuthorized(req);
    if (req.method === "POST" && req.url === "/rpc") {
      const chunks = [];
      for await (const chunk of req) {
        chunks.push(Buffer.from(chunk));
      }
      const body = JSON.parse(Buffer.concat(chunks).toString("utf8") || "{}");
      const result = await handleRpc(body.method, body.params || {});
      jsonResponse(res, 200, { ok: true, result });
      return;
    }
    jsonResponse(res, 404, { ok: false, error: "not_found" });
  } catch (error2) {
    jsonResponse(res, error2.statusCode || 500, {
      ok: false,
      error: error2 instanceof Error ? error2.message : String(error2)
    });
  }
});
async function shutdown() {
  if (daemonState.shuttingDown) {
    return;
  }
  daemonState.shuttingDown = true;
  disconnectWebsocket();
  await mcpRuntime.closeAll().catch((error2) => {
    appendDaemonLog("warn", "mcp_session_close_failed", {
      server_name: null,
      error: error2 instanceof Error ? error2.message : String(error2)
    });
  });
  for (const watchEntry of daemonState.fileWatches.values()) {
    try {
      watchEntry.watcher.close();
    } catch {
    }
  }
  daemonState.fileWatches.clear();
  daemonState.fileWatchEvents = [];
  if (daemonState.processCleanupTimer) {
    clearInterval(daemonState.processCleanupTimer);
    daemonState.processCleanupTimer = null;
  }
  for (const processEntry of daemonState.processes.values()) {
    cleanupManagedProcessEntry(processEntry);
    if (processEntry.status === "running" || processEntry.status === "terminating") {
      requestManagedProcessTermination(processEntry, "SIGTERM", {
        reason: "shutdown",
        forceKillGraceMs: 250
      });
    }
  }
  clearTimeout(daemonState.reconnectTimer);
  clearRuntime();
  await new Promise((resolve) => server.close(resolve));
  process.exit(0);
}
process.on("SIGTERM", () => {
  shutdown().catch(() => process.exit(0));
});
process.on("SIGINT", () => {
  shutdown().catch(() => process.exit(0));
});
process.on("uncaughtException", (error2) => {
  appendDaemonLog("error", "uncaught_exception", { error: error2.message });
});
process.on("unhandledRejection", (reason) => {
  appendDaemonLog("error", "unhandled_rejection", {
    error: reason instanceof Error ? reason.message : String(reason)
  });
});
ensureDir(stateDir);
ensureDir(skillsDir);
ensureDir(import_node_path4.default.dirname(daemonLogPath));
persistRuntimeMeta();
appendDaemonLog("info", "agentd_starting", {
  pid: process.pid,
  port: daemonState.currentPort
});
server.listen(daemonState.currentPort, "127.0.0.1", () => {
  appendDaemonLog("info", "agentd_listening", {
    port: daemonState.currentPort
  });
  void resolveLocalDeviceFingerprint().catch(() => {
  });
  scheduleReconnect(50);
});
