"use client" import { useState, useTransition } from "react" import { toast } from "sonner" import { Sparkles, Check, AlertTriangle } from "lucide-react" import { setAiProviderAction } from "@/app/actions/admin" type Provider = "openai" | "anthropic" const LABELS: Record = { openai: "OpenAI", anthropic: "Anthropic (Claude)" } export function AiProviderToggle({ selected, effective, openaiConfigured, anthropicConfigured, openaiModel, anthropicModel, }: { selected: Provider effective: Provider openaiConfigured: boolean anthropicConfigured: boolean openaiModel: string anthropicModel: string }) { const [current, setCurrent] = useState(selected) const [pending, startTransition] = useTransition() const configured: Record = { openai: openaiConfigured, anthropic: anthropicConfigured } const models: Record = { openai: openaiModel, anthropic: anthropicModel } function choose(next: Provider) { if (next === current || pending) return const prev = current setCurrent(next) startTransition(async () => { try { await setAiProviderAction(next) toast.success(`AI provider set to ${LABELS[next]}`) } catch { setCurrent(prev) // revert optimistic change toast.error("Couldn't switch the AI provider. Try again.") } }) } // When the selected provider has no key on the server, AI falls back to the // other configured provider (see lib/ai/provider). Surface that clearly. const fallbackActive = effective !== current const noneConfigured = !openaiConfigured && !anthropicConfigured const options: Provider[] = ["openai", "anthropic"] return (

AI provider

Choose which LLM powers all AI features (assistant, recommendations, predictions, summaries, receipts). Applies to everyone immediately.

{options.map((p) => { const active = current === p return ( ) })}
{noneConfigured ? (

No AI provider key is set on the server — AI features return a 503 until{" "} OPENAI_API_KEY or ANTHROPIC_API_KEY is configured.

) : fallbackActive ? (

{LABELS[current]} has no API key on this server, so AI is temporarily running on{" "} {LABELS[effective]}. Add the key to use {LABELS[current]}.

) : null}
) }