|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import useSWR from "swr"; |
| 4 | +import { errorHandlingFetcher } from "@/lib/fetcher"; |
| 5 | +import { |
| 6 | + LLMProviderDescriptor, |
| 7 | + WellKnownLLMProviderDescriptor, |
| 8 | +} from "@/app/admin/configuration/llm/interfaces"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Fetches configured LLM providers accessible to the current user. |
| 12 | + * |
| 13 | + * @param personaId - Optional persona ID for RBAC-scoped providers. |
| 14 | + * - `undefined`: public providers only (`/api/llm/provider`) |
| 15 | + * - `number`: persona-specific providers with RBAC enforcement |
| 16 | + */ |
| 17 | +export function useLLMProviders(personaId?: number) { |
| 18 | + const url = |
| 19 | + typeof personaId === "number" |
| 20 | + ? `/api/llm/persona/${personaId}/providers` |
| 21 | + : "/api/llm/provider"; |
| 22 | + |
| 23 | + const { data, error, mutate } = useSWR<LLMProviderDescriptor[] | undefined>( |
| 24 | + url, |
| 25 | + errorHandlingFetcher, |
| 26 | + { |
| 27 | + revalidateOnFocus: false, // Cache aggressively for performance |
| 28 | + dedupingInterval: 60000, // Dedupe requests within 1 minute |
| 29 | + } |
| 30 | + ); |
| 31 | + |
| 32 | + return { |
| 33 | + llmProviders: data, |
| 34 | + isLoading: !error && !data, |
| 35 | + error, |
| 36 | + refetch: mutate, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Fetches the list of well-known (built-in) LLM providers and their models. |
| 42 | + * |
| 43 | + * Returns provider descriptors including known models and recommended defaults |
| 44 | + * for each supported provider (OpenAI, Anthropic, Vertex AI, Bedrock, etc.). |
| 45 | + */ |
| 46 | +export function useWellKnownLLMProviders() { |
| 47 | + const { |
| 48 | + data: wellKnownLLMProviders, |
| 49 | + error, |
| 50 | + isLoading, |
| 51 | + mutate, |
| 52 | + } = useSWR<WellKnownLLMProviderDescriptor[]>( |
| 53 | + "/api/admin/llm/built-in/options", |
| 54 | + errorHandlingFetcher |
| 55 | + ); |
| 56 | + |
| 57 | + return { |
| 58 | + wellKnownLLMProviders: wellKnownLLMProviders ?? null, |
| 59 | + isLoading, |
| 60 | + error, |
| 61 | + mutate, |
| 62 | + }; |
| 63 | +} |
0 commit comments