"use client"; import useSWR from "swr"; import { fetcher } from "./fetcher"; import type { AIModel } from "./types"; export function useModels(all = false) { const { data, error, isLoading, mutate } = useSWR<{ models: AIModel[]; defaultModel: string | null; registry: { refreshedAt?: string } }>(`/api/models${all ? "?all=1" : ""}`, fetcher, { revalidateOnFocus: false, dedupingInterval: 60_000 }); const models = data?.models ?? []; const chat = models.filter(m => m.available && ["chat", "chat-pro", "search"].includes(m.kind)); return { models, chat, defaultModel: data?.defaultModel ?? null, registry: data?.registry, error, isLoading, mutate, byId: (id: string | null | undefined) => models.find(m => m.id === id) }; } export const PROVIDER_LABEL: Record = { openai: "OpenAI", anthropic: "Anthropic", local: "MacLustr local" }; export function shortName(m: AIModel | undefined | null, id?: string | null): string { if (!m) return id ?? "—"; return m.displayName.replace(/^Claude /, "").replace(/ \(\d{4}-\d{2}-\d{2}\)$/, ""); }