// src/actions/skills/versions.ts
import semver from "semver";

// src/actions/skills/defaults.ts
var XYO_SKILLS_SOURCE = "XYOracleNetwork/xyo-skills";

// src/actions/skills/installed.ts
import {
  existsSync,
  readdirSync,
  readFileSync,
  statSync
} from "node:fs";
import PATH from "node:path";
var VERSION_PATTERN = /metadata:\s*\n(?:[ \t]+[^\n]+\n)*?[ \t]+version:\s*([^\s#]+)/;
var readSkillVersion = (skillDir) => {
  const skillMdPath = PATH.resolve(skillDir, "SKILL.md");
  if (!existsSync(skillMdPath)) return void 0;
  try {
    const content = readFileSync(skillMdPath, "utf8");
    const match = VERSION_PATTERN.exec(content);
    return match?.[1]?.trim();
  } catch {
    return void 0;
  }
};

// src/actions/skills/versions.ts
var VERSION_PATTERN2 = /metadata:\s*\n(?:[ \t]+[^\n]+\n)*?[ \t]+version:\s*([^\s#]+)/;
var upstreamSkillUrl = (skillName) => {
  return `https://raw.githubusercontent.com/${XYO_SKILLS_SOURCE}/main/skills/${skillName}/SKILL.md`;
};
var parseSkillVersion = (content) => {
  const match = VERSION_PATTERN2.exec(content);
  return match?.[1]?.trim();
};
var createGitHubSkillVersionFetcher = () => {
  const cache = /* @__PURE__ */ new Map();
  return {
    fetchSkillContent: async (skillName) => {
      if (cache.has(skillName)) return cache.get(skillName);
      try {
        const res = await fetch(upstreamSkillUrl(skillName));
        if (!res.ok) {
          cache.set(skillName, void 0);
          return;
        }
        const content = await res.text();
        cache.set(skillName, content);
        return content;
      } catch {
        cache.set(skillName, void 0);
        return;
      }
    }
  };
};
var fetchLatestSkillVersions = async (skillNames, fetcher = createGitHubSkillVersionFetcher()) => {
  const latest = /* @__PURE__ */ new Map();
  await Promise.all(skillNames.map(async (skillName) => {
    const content = await fetcher.fetchSkillContent(skillName);
    latest.set(skillName, content == null ? void 0 : parseSkillVersion(content));
  }));
  return latest;
};
var isSkillVersionOutdated = (installedVersion, latestVersion) => {
  if (latestVersion == null) return false;
  if (installedVersion == null) return true;
  const installedCoerced = semver.coerce(installedVersion);
  const latestCoerced = semver.coerce(latestVersion);
  if (installedCoerced == null || latestCoerced == null) {
    return installedVersion !== latestVersion;
  }
  return semver.lt(installedCoerced, latestCoerced);
};
export {
  createGitHubSkillVersionFetcher,
  fetchLatestSkillVersions,
  isSkillVersionOutdated,
  parseSkillVersion,
  readSkillVersion as readInstalledSkillVersion
};
//# sourceMappingURL=versions.mjs.map
