// ../packages/skills/dist/chunk-IAAI7RH5.js
var AGENTS_PLUGIN_NAME = ".agents";
function createSkillActivationPlan(options) {
  const enabledSources = new Set(options.enabledSourceSlugs ?? []);
  const selected = new Set(options.selectedSkillSlugs ?? []);
  const promptMentions = parseSkillMentions(options.prompt ?? "");
  const filePaths = options.filePaths ?? [];
  const activations = [];
  const seen = /* @__PURE__ */ new Set();
  for (const skill of options.skills) {
    const reason = activationReason(skill, {
      selected,
      promptMentions,
      filePaths
    });
    if (!reason || seen.has(skill.slug)) continue;
    seen.add(skill.slug);
    activations.push({ skill, reason });
  }
  const requiredSourceSlugs = unique(activations.flatMap(
    (activation) => activation.skill.metadata.requiredSources ?? []
  ));
  const missingRequiredSourceSlugs = requiredSourceSlugs.filter((source) => !enabledSources.has(source));
  const policyExtensions = activations.flatMap(({ skill }) => (skill.metadata.alwaysAllow ?? []).map((toolName) => ({
    toolName,
    scope: { type: "skill", skillSlug: skill.slug }
  })));
  const prerequisiteFiles = activations.map(({ skill }) => `${skill.path}/SKILL.md`).filter((path) => options.prerequisiteFileExists?.(path) ?? true);
  return {
    activeSkillSlugs: activations.map((activation) => activation.skill.slug),
    activations,
    requiredSourceSlugs,
    missingRequiredSourceSlugs,
    policyExtensions,
    providerInstructions: buildProviderInstructions(activations),
    prerequisiteFiles
  };
}
function activationReason(skill, context) {
  if (context.promptMentions.has(skill.slug)) return "prompt-mention";
  if (context.selected.has(skill.slug)) return "host-selection";
  if (matchesAnyGlob(skill.metadata.globs ?? [], context.filePaths)) return "file-glob";
  return void 0;
}
function parseSkillMentions(prompt) {
  const mentions = /* @__PURE__ */ new Set();
  for (const match of prompt.matchAll(/@([A-Za-z0-9_-]+)/g)) {
    mentions.add(match[1]);
  }
  return mentions;
}
function matchesAnyGlob(globs, filePaths) {
  return globs.some((glob) => filePaths.some((filePath) => globMatches(glob, filePath)));
}
function buildProviderInstructions(activations) {
  return activations.map(({ skill }) => `# ${skill.metadata.name}

${skill.content}`).join("\n\n");
}
function unique(values) {
  return [...new Set(values)];
}
function globMatches(pattern, value) {
  const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, "__DOUBLE_STAR__").replace(/\*/g, "[^/]*").replace(/__DOUBLE_STAR__/g, ".*");
  return new RegExp(`^${escaped}$`).test(value);
}
function formatSkillDirective(prerequisiteFiles) {
  if (prerequisiteFiles.length === 0) return void 0;
  const fileList = prerequisiteFiles.map((f) => `- ${f}`).join("\n");
  return [
    "Before proceeding with the user's request, you MUST read the following skill instruction files:",
    fileList,
    "Do not take any other action until you have read these files."
  ].join("\n");
}
function prependSkillDirective(message, prerequisiteFiles) {
  const directive = formatSkillDirective(prerequisiteFiles);
  if (!directive) return message;
  return `${directive}

${message}`;
}

export {
  AGENTS_PLUGIN_NAME,
  createSkillActivationPlan,
  formatSkillDirective,
  prependSkillDirective
};
