/**
 * AgentsClient — REST client for /api/agents endpoints.
 *
 * Wraps:
 *   POST /api/agents/execute  → AgentsClient.execute()
 *   GET  /api/agents          → AgentsClient.listRegistry()
 *
 * Authentication: Bearer token injected via getToken() on every request.
 * Timeout: 30s for execute (agent calls can be slow), 10s for reads.
 *
 * Sprint: command-center:sprint-524:quick-9
 */
// ─── Helpers ──────────────────────────────────────────────────────────────────
async function buildHeaders(getToken) {
    const token = await getToken();
    return {
        "Content-Type": "application/json",
        Authorization: `Bearer ${token}`,
    };
}
/**
 * Parse the backend /api/agents/execute response into AgentExecuteResult.
 *
 * The backend execute endpoint is synchronous and does not assign a run UUID today.
 * We synthesise a deterministic runId from a timestamp + random suffix so the
 * caller always gets a stable reference for UI links.
 */
function parseExecuteResponse(raw, base) {
    const runId = typeof raw.runId === "string"
        ? raw.runId
        : `exec-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
    const runUrl = `${base}/runs/${runId}`;
    const status = typeof raw.status === "string"
        ? raw.status
        : raw.error !== undefined
            ? "failed"
            : "completed";
    return { runId, runUrl, status };
}
// ─── Factory ──────────────────────────────────────────────────────────────────
export function createAgentsClient(opts) {
    const base = opts.baseUrl.replace(/\/$/, "");
    return {
        async execute(input) {
            const headers = await buildHeaders(opts.getToken);
            const res = await fetch(`${base}/api/agents/execute`, {
                method: "POST",
                headers,
                body: JSON.stringify({
                    agentType: input.agentId,
                    taskType: input.taskType,
                    description: input.description,
                    archive: input.archiveToBrain ?? false,
                }),
                signal: AbortSignal.timeout(30_000),
            });
            if (!res.ok) {
                const body = await res.text().catch(() => String(res.status));
                throw new Error(`AgentsClient.execute failed (${res.status}): ${body}`);
            }
            const raw = (await res.json());
            return parseExecuteResponse(raw, base);
        },
        async listRegistry() {
            const headers = await buildHeaders(opts.getToken);
            const res = await fetch(`${base}/api/agents`, {
                method: "GET",
                headers,
                signal: AbortSignal.timeout(10_000),
            });
            if (!res.ok) {
                const body = await res.text().catch(() => String(res.status));
                throw new Error(`AgentsClient.listRegistry failed (${res.status}): ${body}`);
            }
            const raw = (await res.json());
            const agents = (raw.agents ?? []).map((a) => ({
                agentId: typeof a.type === "string" ? a.type : "unknown",
                type: typeof a.type === "string" ? a.type : "unknown",
                status: "active",
            }));
            return { agents };
        },
    };
}
//# sourceMappingURL=agents.js.map