// engine/skill-registry/agentskill-adapter.ts — SPEC-150: agentskill.sh registry adapter
// Searches agentskill.sh for installable agent skills.
// NOTE: The agentskill.sh API contract is not yet publicly documented.
//       This module attempts /api/search and returns [] on any failure,
//       satisfying the unified search interface without blocking the caller.
const AGENTSKILL_BASE = 'https://agentskill.sh';
const USER_AGENT = 'Planu/1.0';
/** Attempt to fetch from agentskill.sh search endpoint (best-effort). */
async function fetchAgentSkill(query, stack) {
    try {
        const url = new URL(`${AGENTSKILL_BASE}/api/search`);
        url.searchParams.set('q', query);
        if (stack) {
            url.searchParams.set('stack', stack);
        }
        const response = await globalThis.fetch(url.toString(), {
            headers: {
                'User-Agent': USER_AGENT,
                Accept: 'application/json',
            },
            signal: AbortSignal.timeout(8_000),
        });
        if (!response.ok) {
            return null;
        }
        return (await response.json());
    }
    catch {
        // Network unavailable, API not yet documented, or unexpected format — return null
        return null;
    }
}
/** Map a raw agentskill.sh entry to a unified SkillSearchResult. */
function mapEntry(entry) {
    const name = entry.name ?? 'unknown';
    return {
        name,
        description: entry.description ?? `Skill: ${name}`,
        source: 'agentskill',
        installCommand: `planu registry install agentskill/${name}`,
        url: `${AGENTSKILL_BASE}/skills/${name}`,
        author: entry.author,
        installs: entry.installs,
        license: entry.license,
        tags: entry.tags,
        relevanceScore: 0,
    };
}
/**
 * Search agentskill.sh for skills matching the given query and optional stack.
 * Returns an empty array on network errors or if the API is unavailable.
 * The API contract is not yet confirmed — see module header for context.
 */
export async function searchAgentSkills(query, stack, limit) {
    const data = await fetchAgentSkill(query, stack);
    if (!data) {
        return [];
    }
    const raw = data.skills ?? data.data ?? [];
    const cap = limit ?? raw.length;
    return raw.slice(0, cap).map(mapEntry);
}
//# sourceMappingURL=agentskill-adapter.js.map