const CACHE_TTL_MS = 5 * 60 * 1000;
let cachedModels = null;
let cachedAt = 0;
function getApiBaseUrl() {
    return process.env.SEAWORK_API_BASE_URL || null;
}
function getApiKey() {
    return process.env.ANTHROPIC_API_KEY || process.env.OPENAI_API_KEY || null;
}
async function fetchAllModels() {
    const now = Date.now();
    if (cachedModels && now - cachedAt < CACHE_TTL_MS) {
        return cachedModels;
    }
    const baseUrl = getApiBaseUrl();
    if (!baseUrl)
        return cachedModels ?? [];
    const apiKey = getApiKey();
    try {
        const headers = {};
        if (apiKey) {
            headers.Authorization = `Bearer ${apiKey}`;
        }
        const res = await fetch(`${baseUrl}/api/v1/models`, { headers });
        const json = (await res.json());
        if (json.code !== 0 || !json.data) {
            return cachedModels ?? [];
        }
        // API returns { "display-name": "gateway-model-id" }
        // Use the gateway model ID as the actual id (sent to the API),
        // and the display name as the label (shown in the UI).
        const entries = Object.entries(json.data);
        cachedModels = entries.map(([displayName, gatewayId], index) => ({
            provider: resolveProvider(gatewayId),
            id: gatewayId,
            label: formatLabel(displayName),
            isDefault: index === 0,
        }));
        cachedAt = now;
        return cachedModels;
    }
    catch {
        return cachedModels ?? [];
    }
}
function resolveProvider(modelId) {
    if (modelId.startsWith("claude"))
        return "claude";
    if (modelId.startsWith("gemini"))
        return "claude";
    if (modelId.startsWith("gpt") || modelId.startsWith("o1") || modelId.startsWith("o3")) {
        return "codex";
    }
    return "claude";
}
function formatLabel(id) {
    return id
        .split("-")
        .map((w) => w.charAt(0).toUpperCase() + w.slice(1))
        .join(" ");
}
const FALLBACK_MODELS = [
    {
        provider: "claude",
        id: "claude-opus-4-6[1m]",
        label: "Opus 4.6 1M",
        description: "Opus 4.6 with 1M context window",
        isDefault: false,
    },
    {
        provider: "claude",
        id: "claude-opus-4-6",
        label: "Opus 4.6",
        description: "Opus 4.6 · Most capable for complex work",
        isDefault: true,
    },
    {
        provider: "claude",
        id: "claude-sonnet-4-6",
        label: "Sonnet 4.6",
        description: "Sonnet 4.6 · Best for everyday tasks",
        isDefault: false,
    },
    {
        provider: "claude",
        id: "claude-haiku-4-5",
        label: "Haiku 4.5",
        description: "Haiku 4.5 · Fastest for quick answers",
        isDefault: false,
    },
    {
        provider: "claude",
        id: "gemini-3.1-pro-preview-seawork",
        label: "Gemini 3.1 Pro",
        description: "Gemini 3.1 Pro Preview · Served through the Seawork Claude gateway",
        isDefault: false,
    },
    {
        provider: "codex",
        id: "gpt-5.4",
        label: "GPT 5.4",
        description: "GPT 5.4 · Latest OpenAI model",
        isDefault: true,
    },
    {
        provider: "codex",
        id: "gpt-5.3-codex",
        label: "GPT 5.3 Codex",
        description: "GPT 5.3 Codex · Optimized for coding",
        isDefault: false,
    },
];
export async function getSeaworkModels(provider) {
    const all = await fetchAllModels();
    let filtered = all.filter((m) => m.provider === provider);
    if (filtered.length === 0) {
        filtered = FALLBACK_MODELS.filter((m) => m.provider === provider).map((m) => ({ ...m }));
    }
    if (filtered.length > 0 && !filtered.some((m) => m.isDefault)) {
        filtered[0].isDefault = true;
    }
    return filtered;
}
export function invalidateModelCache() {
    cachedModels = null;
    cachedAt = 0;
}
//# sourceMappingURL=seawork-models.js.map