"use client"; import { useEffect, useMemo, useState } from "react"; import toast from "react-hot-toast"; import type { ProviderPreset } from "@/lib/types"; export type ProviderSelection = { providerId: string; baseUrl: string; model: string; apiKey: string; }; type LoadedModelsState = | { status: "idle"; models: string[] } | { status: "loading"; models: string[] } | { status: "loaded"; models: string[] } | { status: "error"; models: string[]; message: string }; function uniqSorted(values: string[]) { return [...new Set(values.filter(Boolean))].sort((a, b) => a.localeCompare(b)); } function parseModelIds(json: any): string[] { const data = json?.data; if (Array.isArray(data)) { return uniqSorted( data .map((x) => (typeof x?.id === "string" ? x.id : typeof x?.name === "string" ? x.name : "")) .filter(Boolean) ); } if (Array.isArray(json?.models)) { return uniqSorted( json.models .map((x: any) => (typeof x?.id === "string" ? x.id : typeof x?.name === "string" ? x.name : "")) .filter(Boolean) ); } if (Array.isArray(json)) { return uniqSorted( json .map((x: any) => (typeof x?.id === "string" ? x.id : typeof x?.name === "string" ? x.name : "")) .filter(Boolean) ); } return []; } export function ProviderSelector({ presets, value, onChange }: { presets: ProviderPreset[]; value: ProviderSelection; onChange: (next: ProviderSelection) => void; }) { const preset = presets.find((p) => p.id === value.providerId) ?? presets[0]!; const isCustom = preset.id === "custom"; const modelsUrl = useMemo(() => { const base = value.baseUrl.trim(); if (!base) return ""; return `${base.replace(/\/$/, "")}/models`; }, [value.baseUrl]); const [loadedModels, setLoadedModels] = useState({ status: "idle", models: [] }); async function loadModels({ silent }: { silent?: boolean } = {}) { if (!modelsUrl) return; setLoadedModels((s) => ({ status: "loading", models: s.models })); try { const headers: Record = {}; if (preset.apiKind === "anthropic-messages") { if (value.apiKey.trim()) headers["x-api-key"] = value.apiKey.trim(); headers["anthropic-version"] = "2023-06-01"; } else { if (value.apiKey.trim()) headers.Authorization = `Bearer ${value.apiKey.trim()}`; } const res = await fetch(modelsUrl, { headers, method: "GET" }); const text = await res.text(); if (!res.ok) throw new Error(text || `Model list failed (${res.status}).`); const json = JSON.parse(text) as any; const models = parseModelIds(json); if (models.length === 0) throw new Error("No models returned."); setLoadedModels({ status: "loaded", models }); if (!value.model.trim()) onChange({ ...value, model: models[0]! }); if (!silent) toast.success(`Loaded ${models.length} models.`); } catch (error) { const message = error instanceof Error ? error.message : "Failed to load models."; if (silent) { setLoadedModels((s) => ({ status: s.models.length ? "loaded" : "idle", models: s.models })); return; } setLoadedModels((s) => ({ status: "error", models: s.models, message })); toast.error(message); } } useEffect(() => { setLoadedModels({ status: "idle", models: [] }); if (isCustom) return; if (!modelsUrl) return; void loadModels({ silent: true }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [value.providerId]); useEffect(() => { if (isCustom) return; if (!modelsUrl) return; if (loadedModels.status === "loaded" && loadedModels.models.length) return; if (!value.apiKey.trim()) return; const id = setTimeout(() => { void loadModels({ silent: true }); }, 650); return () => clearTimeout(id); // eslint-disable-next-line react-hooks/exhaustive-deps }, [value.apiKey]); return (
{preset.browserNotes ? (
{preset.browserNotes}
) : null}
); }