import { ExecutionArtifactKind } from "@stigmer/protos/ai/stigmer/agentic/agentexecution/v1/enum_pb";
import { parse as parseYaml } from "yaml";
const NOT_DETECTED = { detected: false };
const SKILL_MD_ENTRY = "SKILL.md";
/**
 * Checks whether an execution artifact is a skill package (directory
 * artifact whose archive contains `SKILL.md`).
 *
 * This is a pure, synchronous check that requires no network call — it
 * inspects the `entries` field populated by the agent runner at ZIP
 * creation time.
 *
 * Returns `false` for:
 * - FILE artifacts (regardless of name)
 * - DIRECTORY artifacts without `entries` (older artifacts before this field was added)
 * - DIRECTORY artifacts whose entries do not include `SKILL.md`
 *
 * @param artifact - Artifact from `execution.status.artifacts`.
 *
 * @example
 * ```ts
 * if (isSkillPackage(artifact)) {
 *   // Show "Push Skill" CTA
 * }
 * ```
 */
export function isSkillPackage(artifact) {
    return (artifact.kind === ExecutionArtifactKind.DIRECTORY &&
        artifact.entries.includes(SKILL_MD_ENTRY));
}
/**
 * Detects a skill package and extracts metadata from its SKILL.md content.
 *
 * Call this after fetching the SKILL.md content from the ZIP via
 * `useArtifactContent(executionId, storageKey, "SKILL.md")`. The function
 * parses the YAML frontmatter to extract `name` and `description`.
 *
 * **Resilient by design** — any parse failure, missing field, or unexpected
 * structure returns `{ detected: false }`. This function never throws.
 *
 * @param artifact - The execution artifact (must be a DIRECTORY with SKILL.md in entries).
 * @param skillMdContent - Raw text content of SKILL.md fetched from the archive.
 * @returns A discriminated union with skill metadata when detection succeeds.
 *
 * @example
 * ```ts
 * const { content } = useArtifactContent(executionId, storageKey, "SKILL.md");
 * const detection = detectSkillPackage(artifact, content);
 *
 * if (detection.detected) {
 *   console.log(`Skill: ${detection.skillName} (${detection.fileCount} files)`);
 * }
 * ```
 *
 * @see {@link isSkillPackage} for the lightweight check (no content needed)
 * @see {@link useDetectSkillPackage} for the React hook that automates the full flow
 */
export function detectSkillPackage(artifact, skillMdContent) {
    if (!isSkillPackage(artifact)) {
        return NOT_DETECTED;
    }
    const frontmatter = parseSkillFrontmatter(skillMdContent);
    if (!frontmatter) {
        return NOT_DETECTED;
    }
    return {
        detected: true,
        skillName: frontmatter.name,
        skillDescription: frontmatter.description,
        fileCount: artifact.entries.length,
        entries: artifact.entries,
    };
}
/**
 * Parses YAML frontmatter from a SKILL.md file.
 *
 * Expects the standard frontmatter format:
 * ```markdown
 * ---
 * name: my-skill-name
 * description: Optional description
 * ---
 * ```
 *
 * Returns `null` when the content has no valid frontmatter or the `name`
 * field is missing/empty.
 */
function parseSkillFrontmatter(content) {
    const fenceMatch = content.match(/^---\r?\n([\s\S]*?)\r?\n---/);
    if (!fenceMatch) {
        return null;
    }
    let parsed;
    try {
        parsed = parseYaml(fenceMatch[1]);
    }
    catch {
        return null;
    }
    if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
        return null;
    }
    const obj = parsed;
    const name = obj.name;
    if (typeof name !== "string" || name.length === 0) {
        return null;
    }
    const description = typeof obj.description === "string" && obj.description.length > 0
        ? obj.description
        : undefined;
    return { name, description };
}
//# sourceMappingURL=detect-skill-package.js.map