// Force strict mode and setup for ESM
"use strict";
import {
  createDebugLogger
} from "./chunk-R5PDRHEF.js";
import {
  init_esbuild_shims
} from "./chunk-A4BMJM77.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/core/src/utils/xml.ts
init_esbuild_shims();
function escapeXml(text) {
  return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
}
__name(escapeXml, "escapeXml");
var XML_TAG_CANDIDATE_RE = /<[^<>]*>/g;
function isXmlWhitespace(char) {
  return char !== void 0 && /\s/.test(char);
}
__name(isXmlWhitespace, "isXmlWhitespace");
function isSystemReminderTagIgnorable(char) {
  const codePoint = char.codePointAt(0);
  if (codePoint === void 0) return false;
  return codePoint === 173 || codePoint === 1564 || codePoint === 12644 || codePoint === 65279 || codePoint === 65440 || codePoint >= 0 && codePoint <= 31 || codePoint >= 127 && codePoint <= 159 || codePoint >= 4447 && codePoint <= 4448 || codePoint >= 6068 && codePoint <= 6069 || codePoint >= 6155 && codePoint <= 6159 || codePoint >= 8203 && codePoint <= 8207 || codePoint >= 8234 && codePoint <= 8238 || codePoint >= 8288 && codePoint <= 8303 || codePoint >= 65024 && codePoint <= 65039 || codePoint >= 65520 && codePoint <= 65528 || codePoint >= 113824 && codePoint <= 113827 || codePoint >= 119155 && codePoint <= 119162 || codePoint >= 917504 && codePoint <= 921599;
}
__name(isSystemReminderTagIgnorable, "isSystemReminderTagIgnorable");
function normalizeSystemReminderCandidateTag(tag) {
  let normalized = "";
  for (const char of tag) {
    if (!isSystemReminderTagIgnorable(char)) {
      normalized += char;
    }
  }
  return normalized.toLowerCase();
}
__name(normalizeSystemReminderCandidateTag, "normalizeSystemReminderCandidateTag");
function getSystemReminderTagKind(tag) {
  const normalized = normalizeSystemReminderCandidateTag(tag);
  const len = normalized.length;
  if (len < 2 || normalized[0] !== "<" || normalized[len - 1] !== ">") {
    return void 0;
  }
  let i = 1;
  while (i < len && isXmlWhitespace(normalized[i])) i++;
  let closing = false;
  if (normalized[i] === "/") {
    closing = true;
    i++;
  }
  while (i < len && isXmlWhitespace(normalized[i])) i++;
  const TAG_NAME = "system-reminder";
  if (normalized.slice(i, i + TAG_NAME.length) !== TAG_NAME) {
    return void 0;
  }
  i += TAG_NAME.length;
  if (i < len - 1 && isXmlWhitespace(normalized[i])) {
    while (i < len && isXmlWhitespace(normalized[i])) i++;
    while (i < len && normalized[i] !== ">") i++;
  }
  while (i < len && isXmlWhitespace(normalized[i])) i++;
  if (normalized[i] === "/") i++;
  while (i < len && isXmlWhitespace(normalized[i])) i++;
  if (i !== len - 1 || normalized[i] !== ">") {
    return void 0;
  }
  return closing ? "closing" : "other";
}
__name(getSystemReminderTagKind, "getSystemReminderTagKind");
function escapeSystemReminderTag(tag) {
  const tagKind = getSystemReminderTagKind(tag);
  if (tagKind === "closing") {
    return "<\\/system-reminder>";
  }
  if (tagKind === "other") {
    return escapeXml(tag);
  }
  return tag;
}
__name(escapeSystemReminderTag, "escapeSystemReminderTag");
function escapeSystemReminderTags(text) {
  return text.replace(XML_TAG_CANDIDATE_RE, escapeSystemReminderTag);
}
__name(escapeSystemReminderTags, "escapeSystemReminderTags");

// packages/core/src/hooks/registerSkillHooks.ts
init_esbuild_shims();
var debugLogger = createDebugLogger("SKILL_HOOKS");
function registerSkillHooks(sessionHooksManager, sessionId, skill) {
  if (!skill.hooks) {
    debugLogger.debug(`Skill '${skill.name}' has no hooks to register`);
    return 0;
  }
  const hooksSettings = skill.hooks;
  let registeredCount = 0;
  for (const eventName of Object.keys(hooksSettings)) {
    const matchers = hooksSettings[eventName];
    if (!matchers) continue;
    for (const matcher of matchers) {
      const matcherPattern = matcher.matcher || "";
      for (const hook of matcher.hooks) {
        if (hook.type === "function" /* Function */) {
          debugLogger.debug(
            "Skipping function hook from skill (not supported in frontmatter)"
          );
          continue;
        }
        const hookConfig = prepareHookConfig(
          hook,
          skill.skillRoot
        );
        sessionHooksManager.addSessionHook(
          sessionId,
          eventName,
          matcherPattern,
          hookConfig,
          { skillRoot: skill.skillRoot }
        );
        registeredCount++;
        debugLogger.debug(
          `Registered hook for ${eventName} with matcher '${matcherPattern}' from skill '${skill.name}'`
        );
      }
    }
  }
  if (registeredCount > 0) {
    debugLogger.info(
      `Registered ${registeredCount} hooks from skill '${skill.name}'`
    );
  }
  return registeredCount;
}
__name(registerSkillHooks, "registerSkillHooks");
function prepareHookConfig(hook, skillRoot) {
  if (hook.type === "command" && skillRoot) {
    return {
      ...hook,
      env: {
        ...hook.env,
        QWEN_SKILL_ROOT: skillRoot
      }
    };
  }
  return hook;
}
__name(prepareHookConfig, "prepareHookConfig");

// packages/core/src/tools/skill-utils.ts
init_esbuild_shims();
function buildSkillLlmContent(baseDir, body) {
  return `Base directory for this skill: ${baseDir}
Important: ALWAYS resolve absolute paths from this base directory when working with skills.

${body}
`;
}
__name(buildSkillLlmContent, "buildSkillLlmContent");
function applySkillAllowedTools(permissionManager, allowedTools) {
  if (!permissionManager || !allowedTools?.length) {
    return;
  }
  for (const rule of allowedTools) {
    permissionManager.addSessionAllowRule(rule);
  }
}
__name(applySkillAllowedTools, "applySkillAllowedTools");

export {
  escapeXml,
  escapeSystemReminderTags,
  registerSkillHooks,
  buildSkillLlmContent,
  applySkillAllowedTools
};
/**
 * @license
 * Copyright 2025 Qwen
 * SPDX-License-Identifier: Apache-2.0
 */
/**
 * @license
 * Copyright 2026 Qwen Team
 * SPDX-License-Identifier: Apache-2.0
 */
