// src/actions/skills/passthrough.ts
import { createRequire } from "node:module";
import chalk from "chalk";

// src/lib/crossSpawn.ts
import crossSpawn from "cross-spawn";
function spawn(command, args, options) {
  return crossSpawn(command, args, options);
}

// src/actions/skills/passthrough.ts
var ANSI_PREFIX = String.raw`\x1B\[[\d;]*m`;
var COMMAND_PREFIX = String.raw`(^|[\s$()\x60>]|${ANSI_PREFIX})`;
var SKILLS_COMMANDS = [
  "<command>",
  "add",
  "a",
  "use",
  "remove",
  "rm",
  "list",
  "ls",
  "find",
  "update",
  "upgrade",
  "experimental_install",
  "init",
  "experimental_sync",
  "defaults",
  "lint",
  "updo"
].join("|");
var NPX_SKILLS_PATTERN = new RegExp(String.raw`${COMMAND_PREFIX}npx(?:\s+-y)?\s+skills(?:@latest)?(?=\W|$)`, "g");
var BARE_USAGE_SKILLS_PATTERN = new RegExp(
  String.raw`^(\s*(?:${ANSI_PREFIX})*Usage:(?:${ANSI_PREFIX})*\s*)skills(?=\s+<command>)`,
  "gm"
);
var BARE_PROMPT_SKILLS_PATTERN = new RegExp(
  String.raw`^(\s*(?:${ANSI_PREFIX})*\$(?:${ANSI_PREFIX})*\s*)skills(?=\s+(?:${SKILLS_COMMANDS})(?:\W|$))`,
  "gm"
);
var require2 = createRequire(import.meta.url);
var rewriteSkillsRefs = (text) => {
  const withNpx = text.replaceAll(NPX_SKILLS_PATTERN, (_match, prefix) => `${prefix}xy skills`);
  return withNpx.replaceAll(BARE_USAGE_SKILLS_PATTERN, "$1xy skills").replaceAll(BARE_PROMPT_SKILLS_PATTERN, "$1xy skills");
};
var resolveBundledSkillsCli = () => {
  return require2.resolve("skills/bin/cli.mjs");
};
var resolveSkillsInvocation = async (args, options = {}) => {
  const resolveCli = options.resolveBundledCli ?? resolveBundledSkillsCli;
  const writeNotice = options.writeNotice ?? ((message) => console.error(message));
  try {
    const cliPath = resolveCli();
    return {
      args: [cliPath, ...args],
      command: process.execPath,
      fallback: false
    };
  } catch {
    writeNotice(chalk.red("skills package not found. Reinstall @xylabs/toolchain dependencies."));
    return void 0;
  }
};
var runSkills = async (args, options = {}) => {
  const invocation = await resolveSkillsInvocation(args, options);
  if (invocation === void 0) return 1;
  const isInteractive = options.interactive ?? false;
  const stdio = isInteractive ? "inherit" : ["inherit", "pipe", "pipe"];
  return new Promise((resolve) => {
    const child = spawn(invocation.command, invocation.args, {
      env: { FORCE_COLOR: "3", ...process.env },
      stdio
    });
    if (!isInteractive) {
      child.stdout?.setEncoding("utf8");
      child.stderr?.setEncoding("utf8");
      child.stdout?.on("data", (chunk) => {
        process.stdout.write(rewriteSkillsRefs(chunk));
      });
      child.stderr?.on("data", (chunk) => {
        process.stderr.write(rewriteSkillsRefs(chunk));
      });
    }
    child.on("error", () => resolve(1));
    child.on("close", (code) => resolve(code ?? 1));
  });
};

// src/actions/skills/defaults.ts
var XYO_SKILLS_SOURCE = "XYOracleNetwork/xyo-skills";
var XYO_DEFAULT_SKILLS = [
  "xl1-build",
  "xl1-knowledge",
  "xl1-patterns",
  "xl1-scaffold",
  "xy-development",
  "xy-toolchain",
  "xyo-knowledge"
];
var AGENT_FLAGS = /* @__PURE__ */ new Set(["-a", "--agent"]);
var hasSkillsAgentFlag = (args) => {
  return args.some((arg) => AGENT_FLAGS.has(arg) || arg.startsWith("--agent="));
};
var skillsDefaultsArgs = (args) => {
  const installSelector = hasSkillsAgentFlag(args) ? ["--skill", "*"] : ["--all"];
  return ["add", XYO_SKILLS_SOURCE, ...installSelector, ...args];
};
var skillsDefaults = (args) => {
  return runSkills(skillsDefaultsArgs(args));
};
export {
  XYO_DEFAULT_SKILLS,
  XYO_SKILLS_SOURCE,
  hasSkillsAgentFlag,
  skillsDefaults,
  skillsDefaultsArgs
};
//# sourceMappingURL=defaults.mjs.map
