Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: switch conversations by LLM profile name (#978)

Engel Nystee94749c708a
diff --git a/__tests__/api/agent-server-conversation-service.test.ts b/__tests__/api/agent-server-conversation-service.test.ts
index b8d2b24b0d..4c7d2c573d 100644
--- a/__tests__/api/agent-server-conversation-service.test.ts
+++ b/__tests__/api/agent-server-conversation-service.test.ts
@@ -99,6 +99,7 @@ describe("AgentServerConversationService", () => {
mockHttpDelete.mockReset();
mockGetProfile.mockReset();
mockActivateProfile.mockReset();
+ mockSwitchProfile.mockReset();
mockSwitchLLM.mockReset();
vi.mocked(ConversationClient).mockClear();
vi.mocked(FileClient).mockClear();
@@ -628,27 +629,17 @@ describe("AgentServerConversationService", () => {
__resetActiveStoreForTests();
});
- it("swaps the conversation's LLM via /switch_llm when a conversationId is provided", async () => {
- const llmConfig = {
- model: "litellm_proxy/claude-haiku",
- api_key: "encrypted-key",
- base_url: "encrypted-url",
- };
- mockGetProfile.mockResolvedValue({
- name: "haiku",
- config: llmConfig,
- api_key_set: true,
- });
- mockSwitchLLM.mockResolvedValue(undefined);
+ it("switches the conversation by profile name when a conversationId is provided", async () => {
+ mockSwitchProfile.mockResolvedValue(undefined);
await AgentServerConversationService.switchProfile("conv-1", "haiku");
- expect(mockGetProfile).toHaveBeenCalledWith("haiku", {
- exposeSecrets: "encrypted",
- });
- expect(mockSwitchLLM).toHaveBeenCalledWith("conv-1", llmConfig);
- // Per-convo path: global default is left untouched.
+ expect(mockSwitchProfile).toHaveBeenCalledWith("conv-1", "haiku");
+ // Per-convo path: global default is left untouched and profile secrets are
+ // never fetched into the UI.
expect(mockActivateProfile).not.toHaveBeenCalled();
+ expect(mockGetProfile).not.toHaveBeenCalled();
+ expect(mockSwitchLLM).not.toHaveBeenCalled();
});
it("activates the profile globally when called without a conversationId", async () => {
@@ -663,6 +654,7 @@ describe("AgentServerConversationService", () => {
expect(mockActivateProfile).toHaveBeenCalledWith("haiku");
// Home-page path: don't touch any conversation's LLM.
expect(mockGetProfile).not.toHaveBeenCalled();
+ expect(mockSwitchProfile).not.toHaveBeenCalled();
expect(mockSwitchLLM).not.toHaveBeenCalled();
});
@@ -684,6 +676,7 @@ describe("AgentServerConversationService", () => {
);
expect(mockActivateProfile).not.toHaveBeenCalled();
expect(mockGetProfile).not.toHaveBeenCalled();
+ expect(mockSwitchProfile).not.toHaveBeenCalled();
expect(mockSwitchLLM).not.toHaveBeenCalled();
});
});
diff --git a/src/api/conversation-service/agent-server-conversation-service.api.ts b/src/api/conversation-service/agent-server-conversation-service.api.ts
index 35ed548149..d31dcf6b00 100644
--- a/src/api/conversation-service/agent-server-conversation-service.api.ts
+++ b/src/api/conversation-service/agent-server-conversation-service.api.ts
@@ -1,7 +1,4 @@
-import {
- ConversationSortOrder,
- type LLMConfig,
-} from "@openhands/typescript-client";
+import { ConversationSortOrder } from "@openhands/typescript-client";
import {
ConversationClient,
FileClient,
@@ -66,11 +63,6 @@ const INVALID_CONVERSATION_RESPONSE_MESSAGE =
"Unable to load conversations because the selected agent server returned " +
"data this UI does not understand. Check the backend URL/session key and " +
"update the agent server if needed.";
-const INVALID_PROFILE_CONFIG_MESSAGE =
- "Unable to switch LLM profiles because the selected agent server returned " +
- "profile data this UI does not understand. Check the backend URL/session " +
- "key and update the agent server if needed.";
-
function invalidConversationResponse(): Error {
return new Error(INVALID_CONVERSATION_RESPONSE_MESSAGE);
}
@@ -79,10 +71,6 @@ function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
-function isLLMConfig(value: unknown): value is LLMConfig {
- return isRecord(value) && typeof value.model === "string";
-}
-
function numberOrNull(value: unknown): number | null {
return typeof value === "number" ? value : null;
}
@@ -665,13 +653,14 @@ class AgentServerConversationService {
/**
* Switches the LLM profile for the running conversation when one is open
- * (POST /switch_llm β€” per-conversation swap, doesn't change the user's
+ * (POST /switch_profile β€” per-conversation swap, doesn't change the user's
* default profile). When called without a conversationId (home page),
* falls back to POST /activate so the next conversation created picks up
* the chosen profile.
*
- * The /switch_llm body needs the LLM config, which we fetch with encrypted
- * secrets β€” same flow as conversation-start.
+ * The per-conversation endpoint accepts only the profile name, so the UI does
+ * not need to fetch or forward profile secrets. That keeps switching working
+ * even when the agent server has no OH_SECRET_KEY for encrypted secret export.
*/
static async switchProfile(
conversationId: string | null,
@@ -683,29 +672,22 @@ class AgentServerConversationService {
);
}
- const profilesClient = new ProfilesClient(getAgentServerClientOptions());
-
if (!conversationId) {
- await profilesClient.activateProfile(profileName);
+ await new ProfilesClient(getAgentServerClientOptions()).activateProfile(
+ profileName,
+ );
return;
}
- const profile = await profilesClient.getProfile(profileName, {
- exposeSecrets: "encrypted",
- });
- if (!isLLMConfig(profile.config)) {
- throw new Error(INVALID_PROFILE_CONFIG_MESSAGE);
- }
-
- await new ConversationClient(getAgentServerClientOptions()).switchLLM(
+ await new ConversationClient(getAgentServerClientOptions()).switchProfile(
conversationId,
- profile.config,
+ profileName,
);
}
/**
* Switches the model of a running ACP conversation in place (POST
- * /switch_acp_model β€” the ACP analog of {@link switchProfile}'s /switch_llm).
+ * /switch_acp_model β€” the ACP analog of {@link switchProfile}'s /switch_profile).
* The agent-server calls the ACP wrapper's ``session/set_model`` on the live
* session, preserving context. Mirrors {@link switchProfile}'s
* local-backend-only guard and per-conversation ConversationClient call.
diff --git a/src/hooks/chat/use-model-interceptor.ts b/src/hooks/chat/use-model-interceptor.ts
index df25a4e6bc..2b1b94d592 100644
--- a/src/hooks/chat/use-model-interceptor.ts
+++ b/src/hooks/chat/use-model-interceptor.ts
@@ -44,9 +44,8 @@ export const useModelInterceptor = (
const arg = trimmed.slice(MODEL_COMMAND.length).trim();
if (arg) {
- // `activateProfile` is global; works whether or not we have an active
- // conversation. Pass conversationId through so the inline "Switched
- // to" message gets anchored when one is open.
+ // Switch the running conversation when one is open; otherwise activate
+ // the profile globally so the next conversation starts with it.
switchAndLog(conversationId ?? null, arg);
return;
}
diff --git a/src/hooks/mutation/use-switch-llm-profile.ts b/src/hooks/mutation/use-switch-llm-profile.ts
index a2e7b7494c..a157dd66a9 100644
--- a/src/hooks/mutation/use-switch-llm-profile.ts
+++ b/src/hooks/mutation/use-switch-llm-profile.ts
@@ -9,7 +9,7 @@ import { invalidateConversationQueries } from "./conversation-mutation-utils";
interface SwitchLlmProfileVars {
/**
- * When set, the conversation's running LLM is swapped via /switch_llm and
+ * When set, the conversation's running LLM is swapped via /switch_profile and
* the user's global default profile is untouched. When null (home page),
* the profile is activated globally instead.
*/