/**
 * xAI (Grok) adapter. xAI's API is OpenAI-compatible: `/v1/models` with Bearer
 * auth. Hostname is fixed (api.x.ai).
 */
import { extractModelIds, messageFromStatus, openAiChat, statusFromHttp, timedFetch, } from './http.js';
const XAI_BASE = 'https://api.x.ai/v1';
function headers(ctx) {
    return { authorization: `Bearer ${ctx.apiKey}` };
}
export async function testConnection(ctx) {
    const testedAt = new Date().toISOString();
    if (!ctx.apiKey) {
        return { ok: false, status: 'needs_setup', message: messageFromStatus('needs_setup'), testedAt };
    }
    try {
        const res = await timedFetch(`${XAI_BASE}/models`, { method: 'GET', headers: headers(ctx) }, ctx);
        const status = statusFromHttp(res.status);
        return { ok: status === 'connected', status, message: messageFromStatus(status), testedAt };
    }
    catch {
        return {
            ok: false,
            status: 'provider_down',
            message: messageFromStatus('provider_down'),
            testedAt,
        };
    }
}
export async function listModels(ctx) {
    const res = await timedFetch(`${XAI_BASE}/models`, { method: 'GET', headers: headers(ctx) }, ctx);
    if (!res.ok)
        throw new Error(`xAI model listing failed (${res.status})`);
    return extractModelIds(await res.json());
}
export function generateText(ctx, opts) {
    return openAiChat(XAI_BASE, headers(ctx), ctx, opts);
}
//# sourceMappingURL=xai.js.map