π OpenHands: AI-Driven Development
[codex] Recognize subscription LLM profiles as configured (#1458)
Graham Neubig3931f4c30c4e
diff --git a/src/hooks/use-llm-configured.test.tsx b/src/hooks/use-llm-configured.test.tsxnew file mode 100644index 0000000000..c4ae6b0aa1--- /dev/null+++ b/src/hooks/use-llm-configured.test.tsx@@ -0,0 +1,176 @@+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import { renderHook, waitFor } from "@testing-library/react";+import type { ReactNode } from "react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import SettingsService from "#/api/settings-service/settings-service.api";+import OptionService from "#/api/option-service/option-service.api";+import ProfilesService from "#/api/profiles-service/profiles-service.api";+import {+ setActiveSelection,+ setRegisteredBackends,+} from "#/api/backend-registry/active-store";+import type { Backend } from "#/api/backend-registry/types";+import { ActiveBackendProvider } from "#/contexts/active-backend-context";+import { DEFAULT_SETTINGS } from "#/services/settings";+import { useLlmConfigured } from "./use-llm-configured";++vi.mock("#/api/settings-service/settings-service.api", () => ({+ default: {+ getSettings: vi.fn(),+ },+}));++vi.mock("#/api/option-service/option-service.api", () => ({+ default: {+ getConfig: vi.fn(),+ },+}));++vi.mock("#/api/profiles-service/profiles-service.api", () => ({+ default: {+ listProfiles: vi.fn(),+ getProfile: vi.fn(),+ },+}));++const localBackend: Backend = {+ id: "test-local",+ name: "Test Local",+ host: "http://localhost:8001",+ apiKey: "test-key",+ kind: "local",+};++function renderLlmConfiguredHook() {+ const queryClient = new QueryClient({+ defaultOptions: {+ queries: { retry: false },+ },+ });++ const wrapper = ({ children }: { children: ReactNode }) => (+ <QueryClientProvider client={queryClient}>+ <ActiveBackendProvider>{children}</ActiveBackendProvider>+ </QueryClientProvider>+ );++ return renderHook(() => useLlmConfigured(), { wrapper });+}++describe("useLlmConfigured", () => {+ const mockGetSettings = vi.mocked(SettingsService.getSettings);+ const mockGetConfig = vi.mocked(OptionService.getConfig);+ const mockListProfiles = vi.mocked(ProfilesService.listProfiles);+ const mockGetProfile = vi.mocked(ProfilesService.getProfile);++ beforeEach(() => {+ setRegisteredBackends([localBackend]);+ setActiveSelection({ backendId: localBackend.id });++ mockGetSettings.mockResolvedValue({+ ...DEFAULT_SETTINGS,+ llm_api_key_set: false,+ agent_settings: {+ ...(DEFAULT_SETTINGS.agent_settings ?? {}),+ agent_kind: "openhands",+ llm: {+ model: "openai/gpt-5.5",+ auth_type: "subscription",+ subscription_vendor: "openai",+ },+ },+ });+ mockGetConfig.mockResolvedValue({+ feature_flags: { hide_llm_settings: false },+ } as Awaited<ReturnType<typeof OptionService.getConfig>>);+ });++ afterEach(() => {+ vi.clearAllMocks();+ setActiveSelection(null);+ setRegisteredBackends([]);+ });++ it("treats an active subscription LLM profile without an API key as configured", async () => {+ mockListProfiles.mockResolvedValue({+ active_profile: "gpt-5.5-sub",+ profiles: [+ {+ name: "gpt-5.5-sub",+ model: "gpt-5.5",+ base_url: "https://chatgpt.com/backend-api/codex",+ api_key_set: false,+ },+ ],+ });+ mockGetProfile.mockResolvedValue({+ name: "gpt-5.5-sub",+ api_key_set: false,+ config: {+ model: "openai/gpt-5.5",+ auth_type: "subscription",+ subscription_vendor: "openai",+ },+ });++ const { result } = renderLlmConfiguredHook();++ await waitFor(() => expect(result.current.isLoading).toBe(false));++ expect(result.current.isConfigured).toBe(true);+ expect(mockGetProfile).toHaveBeenCalledWith("gpt-5.5-sub");+ });++ it("uses the api_key_set fast path for an API-key profile without fetching profile detail", async () => {+ mockListProfiles.mockResolvedValue({+ active_profile: "gpt-5.5",+ profiles: [+ {+ name: "gpt-5.5",+ model: "gpt-5.5",+ base_url: "https://api.openai.com/v1",+ api_key_set: true,+ },+ ],+ });++ const { result } = renderLlmConfiguredHook();++ await waitFor(() => expect(result.current.isLoading).toBe(false));++ expect(result.current.isConfigured).toBe(true);+ // The fast path must not fetch active profile detail.+ expect(mockGetProfile).not.toHaveBeenCalled();+ });++ it("treats a no-key, non-subscription local profile as not configured", async () => {+ mockListProfiles.mockResolvedValue({+ active_profile: "gpt-5.5",+ profiles: [+ {+ name: "gpt-5.5",+ model: "gpt-5.5",+ base_url: "https://api.openai.com/v1",+ api_key_set: false,+ },+ ],+ });+ mockGetProfile.mockResolvedValue({+ name: "gpt-5.5",+ api_key_set: false,+ config: {+ model: "openai/gpt-5.5",+ // No auth_type: "subscription" β this is a plain key-less profile,+ // not a subscription-authenticated one, so it must be unconfigured.+ },+ });++ const { result } = renderLlmConfiguredHook();++ await waitFor(() => expect(result.current.isLoading).toBe(false));++ expect(result.current.isConfigured).toBe(false);+ // Detail is fetched because the profile is local + active + key-less.+ expect(mockGetProfile).toHaveBeenCalledWith("gpt-5.5");+ });+});diff --git a/src/hooks/use-llm-configured.ts b/src/hooks/use-llm-configured.tsindex 7ece4dea03..183387dcbb 100644--- a/src/hooks/use-llm-configured.ts+++ b/src/hooks/use-llm-configured.ts@@ -1,8 +1,15 @@+import { useQuery } from "@tanstack/react-query";import { useSettings } from "#/hooks/query/use-settings";import { useConfig } from "#/hooks/query/use-config";import { useLlmProfiles } from "#/hooks/query/use-llm-profiles";import { useActiveBackend } from "#/contexts/active-backend-context";import { isSettingsPageHidden } from "#/utils/settings-utils";+import ProfilesService from "#/api/profiles-service/profiles-service.api";+import {+ CONFIG_CACHE_OPTIONS,+ LLM_PROFILES_QUERY_KEYS,+} from "#/hooks/query/query-keys";+import { isSubscriptionLlmConfig } from "#/constants/llm-subscription";interface LlmConfiguredResult {/**@@ -44,7 +51,7 @@ export function useLlmConfigured(): LlmConfiguredResult {isLoading: profilesLoading,isError: profilesError,} = useLlmProfiles();- const { backend } = useActiveBackend();+ const { backend, orgId } = useActiveBackend();const isLocal = backend.kind === "local";const isAcpAgent = settings?.agent_settings?.agent_kind === "acp";@@ -53,17 +60,45 @@ export function useLlmConfigured(): LlmConfiguredResult {(profile) => profile.name === profilesData.active_profile,);const hasActiveProfileApiKey = activeProfile?.api_key_set === true;+ const shouldLoadActiveProfileDetail =+ isLocal && !!activeProfile && !hasActiveProfileApiKey;+ const {+ data: activeProfileDetail,+ isLoading: activeProfileDetailLoading,+ isError: activeProfileDetailError,+ } = useQuery({+ queryKey: [+ ...LLM_PROFILES_QUERY_KEYS.all,+ backend.id,+ orgId,+ "detail",+ activeProfile?.name,+ ],+ queryFn: () => ProfilesService.getProfile(activeProfile!.name),+ ...CONFIG_CACHE_OPTIONS,+ enabled: shouldLoadActiveProfileDetail,+ meta: { disableToast: true },+ });+ const hasActiveProfileSubscription =+ shouldLoadActiveProfileDetail &&+ isSubscriptionLlmConfig(+ activeProfileDetail?.config as Record<string, unknown> | undefined,+ );const llmSettingsHidden = isSettingsPageHidden("/settings/llm",config?.feature_flags,);// In local mode, profiles are the source of truth: a usable LLM must be- // backed by an active profile that still exists and has a key. The raw- // settings key can be a stale copy left behind by a deleted profile+ // backed by an active profile that still exists and is authenticated. API-key+ // profiles use the list endpoint's api_key_set flag; subscription profiles+ // intentionally have no key, so we inspect the active profile detail config.+ // The raw settings key can be a stale copy left behind by a deleted profile// (settings are not cleared on delete), so we don't count it here. Cloud// backends don't use profiles and keep the settings-key signal.- const hasUsableLlm = isLocal ? hasActiveProfileApiKey : hasApiKey;+ const hasUsableActiveProfile =+ hasActiveProfileApiKey || hasActiveProfileSubscription;+ const hasUsableLlm = isLocal ? hasUsableActiveProfile : hasApiKey;// Treat a fetch failure as indeterminate (same as loading) only when it// leaves us with no data to decide from β otherwise a transient network@@ -76,10 +111,17 @@ export function useLlmConfigured(): LlmConfiguredResult {const configIndeterminate = configLoading || (configError && !config);const profilesIndeterminate =profilesLoading || (profilesError && !profilesData);+ const activeProfileDetailIndeterminate =+ shouldLoadActiveProfileDetail &&+ (activeProfileDetailLoading ||+ (activeProfileDetailError && !activeProfileDetail));return {isConfigured: isAcpAgent || llmSettingsHidden || hasUsableLlm,isLoading:- settingsIndeterminate || configIndeterminate || profilesIndeterminate,+ settingsIndeterminate ||+ configIndeterminate ||+ profilesIndeterminate ||+ activeProfileDetailIndeterminate,};}