/**
 * Skills module - provides skill discovery and path utilities.
 *
 * System skills are stored as .md files and converted to TypeScript
 * during build by the generate-skills script. This avoids runtime
 * file I/O and path resolution issues.
 */
import { parse as parseYaml } from "yaml";
import { getErrorMeta, getPrefixedLogger } from "../../util/logger.js";
import { SYSTEM_SKILLS } from "./system/_registry.generated.js";
export * from "./types.js";
const logger = getPrefixedLogger("[Skills]");
/**
 * Parse YAML frontmatter from markdown content.
 *
 * @param content - The markdown content to parse
 * @param sourcePath - Path to the source file (for error logging)
 */
function parseFrontmatter(content, sourcePath) {
    const match = content.match(/^---\n([\s\S]*?)\n---/);
    if (!match?.[1])
        return null;
    try {
        return parseYaml(match[1]);
    }
    catch (error) {
        logger.error(`Failed to parse YAML frontmatter${sourcePath ? ` in ${sourcePath}` : ""}`, getErrorMeta(error));
        return null;
    }
}
/**
 * Get content of a system skill by its relative path.
 *
 * @param relativePath - Path relative to skills/system/ (e.g., "superblocks-frontend/SKILL.md")
 * @returns Skill content or undefined if not found
 */
export function getSystemSkillContent(relativePath) {
    return SYSTEM_SKILLS[relativePath];
}
/**
 * Get all available system skills with their metadata.
 *
 * @returns Array of skill metadata
 */
export function getSystemSkillsMetadata() {
    const skills = [];
    for (const [relativePath, content] of Object.entries(SYSTEM_SKILLS)) {
        // Only process SKILL.md files for metadata
        if (!relativePath.endsWith("SKILL.md")) {
            continue;
        }
        const skillPath = `skills/system/${relativePath}`;
        const frontmatter = parseFrontmatter(content, skillPath);
        if (!frontmatter) {
            logger.warn(`Skill ${skillPath} has no valid frontmatter, skipping`);
            continue;
        }
        const skillDir = relativePath.replace(/\/SKILL\.md$/, "");
        const name = frontmatter.name || skillDir;
        let description = frontmatter.description || "";
        // Clean up multiline description
        description = description.trim().replace(/\s+/g, " ");
        skills.push({
            name,
            description,
            location: `skills/system/${relativePath}`,
        });
    }
    return skills;
}
/**
 * Generate XML representation of available skills for the system prompt.
 *
 * @returns XML string listing all available skills, or null if no skills available
 */
export function generateSkillsXml(options) {
    let skills = getSystemSkillsMetadata();
    if (options?.excludeSkills?.length) {
        skills = skills.filter((s) => !options.excludeSkills.some((pattern) => s.location.includes(pattern)));
    }
    if (skills.length === 0) {
        return null;
    }
    const skillEntries = skills
        .map((skill) => `  <skill>
    <name>${escapeXml(skill.name)}</name>
    <description>${escapeXml(skill.description)}</description>
    <location>${escapeXml(skill.location)}</location>
  </skill>`)
        .join("\n");
    return `<available_skills>\n${skillEntries}\n</available_skills>`;
}
/**
 * Escape special XML characters.
 */
function escapeXml(str) {
    return str
        .replace(/&/g, "&amp;")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;")
        .replace(/"/g, "&quot;")
        .replace(/'/g, "&apos;");
}
//# sourceMappingURL=index.js.map