"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// ../packages/skills/dist/chunk-IAAI7RH5.js
var AGENTS_PLUGIN_NAME = ".agents";
function createSkillActivationPlan(options) {
  const enabledSources = new Set(_nullishCoalesce(options.enabledSourceSlugs, () => ( [])));
  const selected = new Set(_nullishCoalesce(options.selectedSkillSlugs, () => ( [])));
  const promptMentions = parseSkillMentions(_nullishCoalesce(options.prompt, () => ( "")));
  const filePaths = _nullishCoalesce(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) => _nullishCoalesce(activation.skill.metadata.requiredSources, () => ( []))
  ));
  const missingRequiredSourceSlugs = requiredSourceSlugs.filter((source) => !enabledSources.has(source));
  const policyExtensions = activations.flatMap(({ skill }) => (_nullishCoalesce(skill.metadata.alwaysAllow, () => ( []))).map((toolName) => ({
    toolName,
    scope: { type: "skill", skillSlug: skill.slug }
  })));
  const prerequisiteFiles = activations.map(({ skill }) => `${skill.path}/SKILL.md`).filter((path) => _nullishCoalesce(_optionalChain([options, 'access', _ => _.prerequisiteFileExists, 'optionalCall', _2 => _2(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(_nullishCoalesce(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}`;
}






exports.AGENTS_PLUGIN_NAME = AGENTS_PLUGIN_NAME; exports.createSkillActivationPlan = createSkillActivationPlan; exports.formatSkillDirective = formatSkillDirective; exports.prependSkillDirective = prependSkillDirective;
