π OpenHands: AI-Driven Development
fix: stop LLM profile switch from reverting on conversation start and fix misleading API-key indicator for new profiles (#648)
Vasco Schiavo54dcd29564fe
diff --git a/__tests__/hooks/mutation/use-switch-llm-profile.test.tsx b/__tests__/hooks/mutation/use-switch-llm-profile.test.tsxnew file mode 100644index 0000000000..41ce813f9b--- /dev/null+++ b/__tests__/hooks/mutation/use-switch-llm-profile.test.tsx@@ -0,0 +1,123 @@+import { renderHook, waitFor } from "@testing-library/react";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import { describe, expect, it, vi, beforeEach } from "vitest";+import { useSwitchLlmProfile } from "#/hooks/mutation/use-switch-llm-profile";+import AgentServerConversationService from "#/api/conversation-service/agent-server-conversation-service.api";+import SettingsService from "#/api/settings-service/settings-service.api";+import {+ LLM_PROFILES_QUERY_KEYS,+ SETTINGS_QUERY_KEYS,+} from "#/hooks/query/query-keys";++vi.mock(+ "#/api/conversation-service/agent-server-conversation-service.api",+ () => ({+ default: {+ switchProfile: vi.fn(),+ },+ }),+);++const renderSwitchHook = () => {+ const queryClient = new QueryClient({+ defaultOptions: {+ queries: { retry: false },+ mutations: { retry: false },+ },+ });+ const invalidateQueriesSpy = vi.spyOn(queryClient, "invalidateQueries");+ const { result } = renderHook(() => useSwitchLlmProfile(), {+ wrapper: ({ children }) => (+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>+ ),+ });+ return { result, invalidateQueriesSpy };+};++describe("useSwitchLlmProfile", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ SettingsService.invalidateCache();+ });++ it("invalidates the settings cache on the home-page activate path (conversationId === null)", async () => {+ vi.mocked(AgentServerConversationService.switchProfile).mockResolvedValue(+ undefined as never,+ );+ const invalidateCacheSpy = vi.spyOn(SettingsService, "invalidateCache");++ const { result, invalidateQueriesSpy } = renderSwitchHook();++ result.current.mutate({ conversationId: null, profileName: "my-profile" });++ await waitFor(() => {+ expect(result.current.isSuccess).toBe(true);+ });++ expect(AgentServerConversationService.switchProfile).toHaveBeenCalledWith(+ null,+ "my-profile",+ );+ // The stale settings cache must be cleared so conversation-start uses the+ // newly activated profile's LLM (the core of bug #640).+ expect(invalidateCacheSpy).toHaveBeenCalled();+ expect(invalidateQueriesSpy).toHaveBeenCalledWith({+ queryKey: LLM_PROFILES_QUERY_KEYS.all,+ });+ expect(invalidateQueriesSpy).toHaveBeenCalledWith({+ queryKey: SETTINGS_QUERY_KEYS.personal(),+ });++ invalidateCacheSpy.mockRestore();+ });++ it("does not touch the settings cache for the per-conversation switch path", async () => {+ vi.mocked(AgentServerConversationService.switchProfile).mockResolvedValue(+ undefined as never,+ );+ const invalidateCacheSpy = vi.spyOn(SettingsService, "invalidateCache");++ const { result, invalidateQueriesSpy } = renderSwitchHook();++ result.current.mutate({+ conversationId: "conv-1",+ profileName: "my-profile",+ });++ await waitFor(() => {+ expect(result.current.isSuccess).toBe(true);+ });++ expect(invalidateCacheSpy).not.toHaveBeenCalled();+ expect(invalidateQueriesSpy).toHaveBeenCalledWith({+ queryKey: LLM_PROFILES_QUERY_KEYS.all,+ });+ expect(invalidateQueriesSpy).toHaveBeenCalledWith({+ queryKey: ["user", "conversation", "conv-1"],+ });+ expect(invalidateQueriesSpy).not.toHaveBeenCalledWith({+ queryKey: SETTINGS_QUERY_KEYS.personal(),+ });++ invalidateCacheSpy.mockRestore();+ });++ it("does not invalidate the settings cache when the switch fails", async () => {+ vi.mocked(AgentServerConversationService.switchProfile).mockRejectedValue(+ new Error("boom"),+ );+ const invalidateCacheSpy = vi.spyOn(SettingsService, "invalidateCache");++ const { result } = renderSwitchHook();++ result.current.mutate({ conversationId: null, profileName: "my-profile" });++ await waitFor(() => {+ expect(result.current.isError).toBe(true);+ });++ expect(invalidateCacheSpy).not.toHaveBeenCalled();++ invalidateCacheSpy.mockRestore();+ });+});diff --git a/__tests__/routes/llm-settings.test.tsx b/__tests__/routes/llm-settings.test.tsxindex 8517d746fe..58b0ab3156 100644--- a/__tests__/routes/llm-settings.test.tsx+++ b/__tests__/routes/llm-settings.test.tsx@@ -26,14 +26,18 @@ function buildSettings(overrides: Partial<Settings> = {}): Settings {};}-function renderLlmSettingsScreen() {- return render(<LlmSettingsScreen />, {+function renderLlmSettingsScreen(+ props: Parameters<typeof LlmSettingsScreen>[0] = {},+) {+ return render(<LlmSettingsScreen {...props} />, {wrapper: ({ children }) => (<MemoryRouter><QueryClientProvider- client={new QueryClient({- defaultOptions: { queries: { retry: false } },- })}+ client={+ new QueryClient({+ defaultOptions: { queries: { retry: false } },+ })+ }>{children}</QueryClientProvider>@@ -47,9 +51,11 @@ function renderLlmSettingsRoute() {wrapper: ({ children }) => (<MemoryRouter><QueryClientProvider- client={new QueryClient({- defaultOptions: { queries: { retry: false } },- })}+ client={+ new QueryClient({+ defaultOptions: { queries: { retry: false } },+ })+ }>{children}</QueryClientProvider>@@ -120,6 +126,42 @@ describe("LlmSettingsScreen", () => {expect(screen.getByTestId("llm-provider-input")).toBeInTheDocument();expect(screen.getByTestId("llm-api-key-input")).toBeInTheDocument();});++ it("shows the API key as set on the global settings page when a key exists", async () => {+ vi.spyOn(SettingsService, "getSettings").mockResolvedValue(+ buildSettings({ llm_model: "openai/gpt-4o", llm_api_key_set: true }),+ );++ renderLlmSettingsScreen();++ await screen.findByTestId("llm-settings-screen");++ expect(screen.getByTestId("set-indicator")).toBeInTheDocument();+ expect(screen.getByTestId("llm-api-key-input")).toHaveValue("");+ });++ it("does not show a 'key set' indicator for a brand-new embedded profile even when a global key exists (bug #640)", async () => {+ // A global key exists, but a fresh profile form must look unset so the user+ // knows they have to enter one β otherwise the profile saves with no key.+ vi.spyOn(SettingsService, "getSettings").mockResolvedValue(+ buildSettings({ llm_model: "openai/gpt-4o", llm_api_key_set: true }),+ );++ renderLlmSettingsScreen({+ embedded: true,+ hideSaveButton: true,+ initialValueOverrides: {+ "llm.model": "",+ "llm.api_key": "",+ "llm.base_url": "",+ },+ });++ await screen.findByTestId("llm-settings-screen");++ expect(screen.getByTestId("llm-api-key-input")).toHaveValue("");+ expect(screen.queryByTestId("set-indicator")).not.toBeInTheDocument();+ });});describe("LlmSettingsRoute - backend mode rendering", () => {@@ -181,4 +223,4 @@ describe("LlmSettingsRoute - backend mode rendering", () => {// Should NOT show the "Add LLM Profile" buttonexpect(screen.queryByTestId("add-llm-profile")).not.toBeInTheDocument();});-});\ No newline at end of file+});diff --git a/src/hooks/mutation/use-switch-llm-profile.ts b/src/hooks/mutation/use-switch-llm-profile.tsindex 9db1bc2a18..23f30b653e 100644--- a/src/hooks/mutation/use-switch-llm-profile.ts+++ b/src/hooks/mutation/use-switch-llm-profile.ts@@ -1,6 +1,10 @@import { useMutation, useQueryClient } from "@tanstack/react-query";import AgentServerConversationService from "#/api/conversation-service/agent-server-conversation-service.api";-import { LLM_PROFILES_QUERY_KEYS } from "#/hooks/query/query-keys";+import SettingsService from "#/api/settings-service/settings-service.api";+import {+ LLM_PROFILES_QUERY_KEYS,+ SETTINGS_QUERY_KEYS,+} from "#/hooks/query/query-keys";interface SwitchLlmProfileVars {/**@@ -33,6 +37,15 @@ export const useSwitchLlmProfile = () => {queryClient.invalidateQueries({queryKey: ["user", "conversation", conversationId],});+ } else {+ // Home-page activate path (same server endpoint as+ // useActivateLlmProfile): clear the SettingsService cache so the next+ // conversation-start reads the newly activated profile's LLM config+ // instead of the stale encrypted settings.+ SettingsService.invalidateCache();+ queryClient.invalidateQueries({+ queryKey: SETTINGS_QUERY_KEYS.personal(),+ });}},// Caller renders an inline message + handles error toast manually.diff --git a/src/routes/llm-settings.tsx b/src/routes/llm-settings.tsxindex 6e6576e46b..e0df4dd2a1 100644--- a/src/routes/llm-settings.tsx+++ b/src/routes/llm-settings.tsx@@ -157,6 +157,16 @@ export function LlmSettingsScreen({: "";const showOpenHandsApiKeyHelp = modelValue.startsWith("openhands/");+ const apiKeyValue =+ typeof values["llm.api_key"] === "string" ? values["llm.api_key"] : "";+ // For embedded profile forms (create/edit) the global+ // `llm_api_key_set` flag is misleading: a brand-new profile would show a+ // "key set" indicator just because some other profile has a key. Reflect+ // the form's own key state instead so create mode starts visibly unset.+ const apiKeyIsSet = embedded+ ? apiKeyValue.length > 0+ : Boolean(settings?.llm_api_key_set);+const renderApiKeyInput = (testId: string, helpTestId: string) => (<><SettingsInput@@ -164,18 +174,12 @@ export function LlmSettingsScreen({label={t(I18nKey.SETTINGS_FORM$API_KEY)}type="password"className="w-full"- value={- typeof values["llm.api_key"] === "string"- ? values["llm.api_key"]- : ""- }- placeholder={settings?.llm_api_key_set ? "<hidden>" : ""}+ value={apiKeyValue}+ placeholder={apiKeyIsSet ? "<hidden>" : ""}onChange={(value) => onChange("llm.api_key", value)}isDisabled={isDisabled}startContent={- settings?.llm_api_key_set ? (- <KeyStatusIcon isSet={settings.llm_api_key_set} />- ) : undefined+ apiKeyIsSet ? <KeyStatusIcon isSet={apiKeyIsSet} /> : undefined}/>@@ -257,7 +261,7 @@ export function LlmSettingsScreen({</div>);},- [defaultModel, settings?.llm_api_key_set, t],+ [defaultModel, embedded, settings?.llm_api_key_set, t],);const buildPayload = React.useCallback(