Skip to content

Commit 0aab5de

Browse files
committed
refactor: consolidate LLM provider hooks into hooks/useLLMProviders
Move useLLMProviders from lib/hooks/ and useWellKnownLLMProviders from hooks/ into a single hooks/useLLMProviders.ts file. Update all imports.
1 parent 6ccc24d commit 0aab5de

File tree

10 files changed

+70
-73
lines changed

10 files changed

+70
-73
lines changed

web/src/app/craft/hooks/useBuildSessionController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { usePreProvisionPolling } from "@/app/craft/hooks/usePreProvisionPolling
77
import { CRAFT_SEARCH_PARAM_NAMES } from "@/app/craft/services/searchParams";
88
import { CRAFT_PATH } from "@/app/craft/v1/constants";
99
import { getBuildUserPersona } from "@/app/craft/onboarding/constants";
10-
import { useLLMProviders } from "@/lib/hooks/useLLMProviders";
10+
import { useLLMProviders } from "@/hooks/useLLMProviders";
1111
import { checkPreProvisionedSession } from "@/app/craft/services/apiServices";
1212

1313
interface UseBuildSessionControllerProps {

web/src/app/craft/onboarding/hooks/useOnboardingModal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { useCallback, useState, useMemo, useEffect } from "react";
44
import { useUser } from "@/providers/UserProvider";
5-
import { useLLMProviders } from "@/lib/hooks/useLLMProviders";
5+
import { useLLMProviders } from "@/hooks/useLLMProviders";
66
import { LLMProviderName } from "@/app/admin/configuration/llm/interfaces";
77
import {
88
OnboardingModalMode,

web/src/app/craft/v1/configure/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import Switch from "@/refresh-components/inputs/Switch";
4141
import SimpleTooltip from "@/refresh-components/SimpleTooltip";
4242
import NotAllowedModal from "@/app/craft/onboarding/components/NotAllowedModal";
4343
import { useOnboarding } from "@/app/craft/onboarding/BuildOnboardingProvider";
44-
import { useLLMProviders } from "@/lib/hooks/useLLMProviders";
44+
import { useLLMProviders } from "@/hooks/useLLMProviders";
4545
import { useUser } from "@/providers/UserProvider";
4646
import { getProviderIcon } from "@/app/admin/configuration/llm/utils";
4747
import {

web/src/hooks/useLLMProviders.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

web/src/hooks/useWellKnownLLMProviders.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

web/src/lib/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import { AuthType, NEXT_PUBLIC_CLOUD_ENABLED } from "./constants";
3939
import { useUser } from "@/providers/UserProvider";
4040
import { SEARCH_TOOL_ID } from "@/app/app/components/tools/constants";
4141
import { updateTemperatureOverrideForChatSession } from "@/app/app/services/lib";
42-
import { useLLMProviders } from "./hooks/useLLMProviders";
42+
import { useLLMProviders } from "@/hooks/useLLMProviders";
4343

4444
const CREDENTIAL_URL = "/api/manage/admin/credential";
4545

web/src/lib/hooks/useLLMProviders.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

web/src/refresh-components/onboarding/useOnboardingState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { WellKnownLLMProviderDescriptor } from "@/app/admin/configuration/llm/in
1111
import { updateUserPersonalization } from "@/lib/userSettings";
1212
import { useUser } from "@/providers/UserProvider";
1313
import { MinimalPersonaSnapshot } from "@/app/admin/assistants/interfaces";
14-
import { useLLMProviders } from "@/lib/hooks/useLLMProviders";
14+
import { useLLMProviders } from "@/hooks/useLLMProviders";
1515

1616
export function useOnboardingState(liveAssistant?: MinimalPersonaSnapshot): {
1717
state: OnboardingState;

web/src/refresh-pages/AgentEditorPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as InputLayouts from "@/layouts/input-layouts";
1818
import { useFormikContext } from "formik";
1919
import LLMSelector from "@/components/llm/LLMSelector";
2020
import { parseLlmDescriptor, structureValue } from "@/lib/llmConfig/utils";
21-
import { useLLMProviders } from "@/lib/hooks/useLLMProviders";
21+
import { useLLMProviders } from "@/hooks/useLLMProviders";
2222
import {
2323
STARTER_MESSAGES_EXAMPLES,
2424
MAX_CHARACTERS_STARTER_MESSAGE,

web/src/refresh-pages/admin/LLMConfigurationPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useRef } from "react";
44
import useSWR, { useSWRConfig } from "swr";
55
import { toast } from "@/hooks/useToast";
66
import { errorHandlingFetcher } from "@/lib/fetcher";
7-
import useWellKnownLLMProviders from "@/hooks/useWellKnownLLMProviders";
7+
import { useWellKnownLLMProviders } from "@/hooks/useLLMProviders";
88
import { ThreeDotsLoader } from "@/components/Loading";
99
import { Content, ContentAction } from "@opal/layouts";
1010
import { Button } from "@opal/components";

0 commit comments

Comments
 (0)