diff --git a/apps/vscode-e2e/src/visual/__screenshots__/electron-chat-dark-sidebar.png b/apps/vscode-e2e/src/visual/__screenshots__/electron-chat-dark-sidebar.png index dfc6fee680..74cf92c586 100644 Binary files a/apps/vscode-e2e/src/visual/__screenshots__/electron-chat-dark-sidebar.png and b/apps/vscode-e2e/src/visual/__screenshots__/electron-chat-dark-sidebar.png differ diff --git a/packages/types/src/vscode-extension-host.ts b/packages/types/src/vscode-extension-host.ts index 3e23a7934c..4ce335461e 100644 --- a/packages/types/src/vscode-extension-host.ts +++ b/packages/types/src/vscode-extension-host.ts @@ -30,10 +30,12 @@ export interface ProviderInfoParams { apiKey?: string baseUrl?: string modelId?: string + headers?: Record } /** Result of each lookup. The host and the webview share it, so the kind fixes the result type. */ export interface ProviderInfoResultMap { + openAiModels: string[] openRouterKeyInfo: { label: string usage: number diff --git a/src/api/providers/openai.ts b/src/api/providers/openai.ts index 98ea18df8a..2e021b3373 100644 --- a/src/api/providers/openai.ts +++ b/src/api/providers/openai.ts @@ -609,7 +609,12 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl } } -export async function getOpenAiModels(baseUrl?: string, apiKey?: string, openAiHeaders?: Record) { +export async function getOpenAiModels( + baseUrl?: string, + apiKey?: string, + openAiHeaders?: Record, + options: { throwOnError?: boolean } = {}, +) { try { if (!baseUrl) { return [] @@ -639,7 +644,9 @@ export async function getOpenAiModels(baseUrl?: string, apiKey?: string, openAiH const response = await axios.get(`${trimmedBaseUrl}/models`, config) const modelsArray = response.data?.data?.map((model: any) => model.id) || [] return [...new Set(modelsArray)] - } catch { + } catch (error) { + // The provider-info broker needs failures; legacy Settings callers expect an empty list. + if (options.throwOnError) throw error return [] } } diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index 6c9182fcfb..bb186fc6d1 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -73,7 +73,6 @@ import { DEFAULT_CHECKPOINT_TIMEOUT_SECONDS, getModelId, isRetiredProvider, - modelIdKeys, providerIdentifiers, } from "@roo-code/types" import { RateLimitClock, createRateLimitClock } from "../task/RateLimitClock" @@ -98,6 +97,7 @@ import { formatLanguage } from "../../shared/language" import { WebviewMessage } from "../../shared/WebviewMessage" import { EMBEDDING_MODEL_PROFILES } from "../../shared/embeddingModels" import { ProfileValidator } from "../../shared/ProfileValidator" +import { mergeProfileModelPatch } from "./profileModelPatch" import { Terminal } from "../../integrations/terminal/Terminal" import { downloadTask, getTaskFileName } from "../../integrations/misc/export-markdown" @@ -212,22 +212,6 @@ function scheduleTask( }) } -// Keys a model-selector patch may touch: model IDs plus settings reset on model change. -const PROFILE_MODEL_ID_PATCH_KEYS: ReadonlySet = new Set(modelIdKeys) - -const BEDROCK_CUSTOM_ARN_MODEL_ID = "custom-arn" - -// These keys can only be cleared. Setting `awsCustomArn` would let a patch invoke a Bedrock -// resource that the organization allow-list never checked, because the allow-list sees `apiModelId`. -const PROFILE_MODEL_RESET_ONLY_KEYS: ReadonlySet = new Set([ - "reasoningEffort", - "modelMaxTokens", - "modelMaxThinkingTokens", - "awsCustomArn", - "awsCustomArnBaseModelId", - "awsModelContextWindow", -]) - type GetStateOptions = { includeTaskHistory?: boolean } @@ -2294,7 +2278,7 @@ export class ClineProvider } /** - * Applies a model change to the latest stored profile inside the mutation queue, + * Applies a model/reasoning change to the latest stored profile inside the mutation queue, * so a stale webview snapshot cannot overwrite newer profile settings. * A `null` patch value clears the key. */ @@ -2315,45 +2299,11 @@ export class ClineProvider ) } - const merged: Record = { ...stored, apiProvider: storedProvider } - - for (const [key, value] of Object.entries(patch)) { - if (PROFILE_MODEL_ID_PATCH_KEYS.has(key)) { - if (typeof value !== "string" || value.length === 0) { - throw new Error(`Key "${key}" must be a non-empty string in a model patch`) - } - - merged[key] = value - } else if (PROFILE_MODEL_RESET_ONLY_KEYS.has(key)) { - if (value !== null && value !== undefined) { - throw new Error(`Key "${key}" can only be cleared in a model patch`) - } - - delete merged[key] - } else { - throw new Error(`Key "${key}" is not allowed in a model patch`) - } - } - - if (storedProvider === providerIdentifiers.bedrock) { - if (merged.apiModelId === BEDROCK_CUSTOM_ARN_MODEL_ID) { - // The allow-list only sees apiModelId, so a patch must not select an ARN it cannot supply. - if (!merged.awsCustomArn) { - throw new Error( - `Model "${BEDROCK_CUSTOM_ARN_MODEL_ID}" requires an existing custom ARN`, - ) - } - } else { - // A retained ARN would override the allow-listed model, so drop it with its context fields. - for (const key of PROFILE_MODEL_RESET_ONLY_KEYS) { - if (key.startsWith("aws")) delete merged[key] - } - } - } + const merged = mergeProfileModelPatch(stored, expectedProvider, patch) const { organizationAllowList } = await this.getState() - if (!ProfileValidator.isProfileAllowed(merged as ProviderSettings, organizationAllowList)) { + if (!ProfileValidator.isProfileAllowed(merged, organizationAllowList)) { throw new OrganizationAllowListViolationError( t("common:errors.violated_organization_allowlist"), ) @@ -2362,7 +2312,7 @@ export class ClineProvider if (signal.aborted) return try { - await this.persistProviderProfile(name, merged as ProviderSettings, true, signal) + await this.persistProviderProfile(name, merged, true, signal) } catch (persistError) { // Saving and activating are separate writes. Put the previous profile back so // storage, context settings, and the task do not disagree about the model. diff --git a/src/core/webview/__tests__/ClineProvider.apiHandlerRebuild.spec.ts b/src/core/webview/__tests__/ClineProvider.apiHandlerRebuild.spec.ts index 28531e964d..44b0ec90fb 100644 --- a/src/core/webview/__tests__/ClineProvider.apiHandlerRebuild.spec.ts +++ b/src/core/webview/__tests__/ClineProvider.apiHandlerRebuild.spec.ts @@ -5,7 +5,13 @@ import * as vscode from "vscode" import { makeCompositeDisposable } from "../../../test-utils/vscode" import { TelemetryService } from "@roo-code/telemetry" -import { getModelId, RooCodeEventName } from "@roo-code/types" +import { + getModelId, + openAiModelInfoSaneDefaults, + reasoningEffortSettingValues, + RooCodeEventName, + type ProviderSettings, +} from "@roo-code/types" import { ContextProxy } from "../../config/ContextProxy" import type { Mode } from "../../../shared/modes" @@ -444,6 +450,126 @@ describe("ClineProvider - API Handler Rebuild Guard", () => { vi.mocked(vscode.window.showErrorMessage).mockClear() }) + test.each(reasoningEffortSettingValues)("persists a reasoning-only update: %s", async (reasoningEffort) => { + await provider.updateProfileModel("test-config", providerIdentifiers.openrouter, { reasoningEffort }) + expect(provider["providerSettingsManager"].saveConfig).toHaveBeenCalledWith( + "test-config", + expect.objectContaining({ openRouterModelId: "openai/gpt-4", rateLimitSeconds: 5, reasoningEffort }), + ) + expect(vscode.window.showErrorMessage).not.toHaveBeenCalled() + }) + + test.each([true, false])("persists enableReasoningEffort=%s", async (enableReasoningEffort) => { + await provider.updateProfileModel("test-config", providerIdentifiers.openrouter, { enableReasoningEffort }) + expect(provider["providerSettingsManager"].saveConfig).toHaveBeenCalledWith( + "test-config", + expect.objectContaining({ openRouterModelId: "openai/gpt-4", enableReasoningEffort }), + ) + expect(vscode.window.showErrorMessage).not.toHaveBeenCalled() + }) + + test("applies reasoning-only changes to the current task even when its model stays the same", async () => { + const task = new Task({ ...defaultTaskOptions }) + await provider.addClineToStack(task) + await provider.updateProfileModel("test-config", providerIdentifiers.openrouter, { + reasoningEffort: "disable", + enableReasoningEffort: false, + }) + expect(task.updateApiConfiguration).toHaveBeenCalledWith( + expect.objectContaining({ + openRouterModelId: "openai/gpt-4", + reasoningEffort: "disable", + enableReasoningEffort: false, + }), + ) + }) + + test("merges queued effort into the profile written by the preceding model update", async () => { + const manager = provider["providerSettingsManager"] + let latest: ProviderSettings & { name: string } = { + name: "test-config", + apiProvider: providerIdentifiers.openai, + openAiModelId: "old-model", + openAiCustomModelInfo: { ...openAiModelInfoSaneDefaults, description: "original" }, + } + manager.getProfile = vi.fn().mockImplementation(async () => latest) + vi.mocked(manager.saveConfig).mockImplementation(async (name, config) => { + latest = { + ...config, + name, + openAiCustomModelInfo: { + ...(config.openAiCustomModelInfo ?? openAiModelInfoSaneDefaults), + description: "newer metadata", + }, + } + return "test-id" + }) + await Promise.all([ + provider.updateProfileModel("test-config", providerIdentifiers.openai, { openAiModelId: "new-model" }), + provider.updateProfileModel("test-config", providerIdentifiers.openai, { + openAiCustomModelInfo: { reasoningEffort: "high" }, + }), + ]) + expect(manager.saveConfig).toHaveBeenCalledTimes(2) + expect(vi.mocked(manager.saveConfig).mock.calls[1][1]).toMatchObject({ + openAiModelId: "new-model", + openAiCustomModelInfo: { description: "newer metadata", reasoningEffort: "high" }, + }) + }) + + test.each(["high", null] as const)( + "merges nested custom effort %s into stored metadata", + async (reasoningEffort) => { + const info = { ...openAiModelInfoSaneDefaults, contextWindow: 12345, description: "custom metadata" } + const profile: ProviderSettings & { name: string } = { + name: "test-config", + apiProvider: providerIdentifiers.openai, + openAiModelId: "custom-model", + openAiApiKey: "secret", + openAiCustomModelInfo: { ...info, reasoningEffort: "low" }, + } + provider["providerSettingsManager"].getProfile = vi.fn().mockResolvedValue(profile) + await provider.updateProfileModel("test-config", providerIdentifiers.openai, { + openAiCustomModelInfo: { reasoningEffort }, + }) + const saved = vi.mocked(provider["providerSettingsManager"].saveConfig).mock.calls[0][1] + expect(saved).toMatchObject({ openAiModelId: "custom-model", openAiApiKey: "secret" }) + if (reasoningEffort === null) expect(saved.openAiCustomModelInfo).toEqual(info) + else expect(saved.openAiCustomModelInfo).toEqual({ ...info, reasoningEffort }) + expect(profile.openAiCustomModelInfo?.reasoningEffort).toBe("low") + }, + ) + + test.each([ + { apiModelId: "wrong-provider-field" }, + { reasoningEffort: "invalid" }, + { reasoningEffort: true }, + { enableReasoningEffort: "false" }, + { enableReasoningEffort: 1 }, + { openAiCustomModelInfo: { reasoningEffort: "high" } }, + ])("rejects invalid partial updates without persisting: %j", async (patch) => { + await provider.updateProfileModel("test-config", providerIdentifiers.openrouter, patch) + expect(provider["providerSettingsManager"].saveConfig).not.toHaveBeenCalled() + expect(vscode.window.showErrorMessage).toHaveBeenCalled() + }) + + test.each([ + { reasoningEffort: "disable" }, + { reasoningEffort: 1 }, + { reasoningEffort: "high", contextWindow: 1 }, + { openAiApiKey: "replacement" }, + null, + [], + ])("rejects unsafe nested metadata updates: %j", async (openAiCustomModelInfo) => { + provider["providerSettingsManager"].getProfile = vi.fn().mockResolvedValue({ + name: "test-config", + apiProvider: providerIdentifiers.openai, + }) + await provider.updateProfileModel("test-config", providerIdentifiers.openai, { openAiCustomModelInfo }) + expect(provider["providerSettingsManager"].saveConfig).not.toHaveBeenCalled() + expect(vscode.window.showErrorMessage).toHaveBeenCalled() + }) + test("merges the patch into the latest stored profile, keeping newer settings and clearing null keys", async () => { await provider.updateProfileModel("test-config", providerIdentifiers.openrouter, { openRouterModelId: "anthropic/claude", diff --git a/src/core/webview/__tests__/profileModelPatch.spec.ts b/src/core/webview/__tests__/profileModelPatch.spec.ts new file mode 100644 index 0000000000..1ae7887695 --- /dev/null +++ b/src/core/webview/__tests__/profileModelPatch.spec.ts @@ -0,0 +1,216 @@ +import { + modelIdKeys, + openAiModelInfoSaneDefaults, + providerDefinitionList, + providerIdentifiers, + reasoningEffortSettingValues, + reasoningEffortsExtended, + type ProviderDefinition, + type ProviderSettings, +} from "@roo-code/types" + +import { mergeProfileModelPatch } from "../profileModelPatch" + +const stored: ProviderSettings = { + apiProvider: providerIdentifiers.openrouter, + openRouterModelId: "old-model", + openRouterApiKey: "secret", + rateLimitSeconds: 5, + reasoningEffort: "high", + enableReasoningEffort: true, +} + +const customInfo = { + ...openAiModelInfoSaneDefaults, + contextWindow: 12345, + description: "Keep custom metadata", + supportsReasoningEffort: true, + inputPrice: 0.25, + reasoningEffort: "low", +} satisfies NonNullable + +describe("mergeProfileModelPatch", () => { + it.each(providerDefinitionList)( + "only permits the primary model field for $apiProvider", + (definition) => { + const profile: ProviderSettings = { apiProvider: definition.apiProvider } + for (const key of modelIdKeys) { + const merge = () => mergeProfileModelPatch(profile, definition.apiProvider, { [key]: "new-model" }) + if (key === definition.modelIdKey) expect(merge()).toMatchObject({ [key]: "new-model" }) + else expect(merge).toThrow() + } + }, + ) + + it.each(reasoningEffortSettingValues)( + "accepts top-level effort %s without changing the model", + (reasoningEffort) => { + const merged = mergeProfileModelPatch(stored, providerIdentifiers.openrouter, { reasoningEffort }) + expect(merged).toEqual({ ...stored, reasoningEffort }) + expect(stored.reasoningEffort).toBe("high") + }, + ) + + it.each([true, false])("accepts enableReasoningEffort=%s", (enableReasoningEffort) => { + expect(mergeProfileModelPatch(stored, providerIdentifiers.openrouter, { enableReasoningEffort })).toEqual({ + ...stored, + enableReasoningEffort, + }) + }) + + it.each([null, undefined])("clears controls and reset-only fields using %s", (value) => { + const profile: ProviderSettings = { + ...stored, + modelMaxTokens: 10, + modelMaxThinkingTokens: 5, + awsCustomArn: "existing-arn", + awsCustomArnBaseModelId: "base-model", + awsModelContextWindow: 100, + } + const keys = [ + "reasoningEffort", + "enableReasoningEffort", + "modelMaxTokens", + "modelMaxThinkingTokens", + "awsCustomArn", + "awsCustomArnBaseModelId", + "awsModelContextWindow", + ] + const patch = Object.fromEntries(keys.map((key) => [key, value])) + const merged = mergeProfileModelPatch(profile, providerIdentifiers.openrouter, patch) + for (const key of keys) expect(merged).not.toHaveProperty(key) + expect(profile).toHaveProperty("awsCustomArn", "existing-arn") + }) + + it.each(reasoningEffortsExtended)( + "merges nested effort %s without replacing custom metadata", + (reasoningEffort) => { + const profile: ProviderSettings = { + apiProvider: providerIdentifiers.openai, + openAiCustomModelInfo: Object.freeze({ ...customInfo }), + } + const merged = mergeProfileModelPatch(Object.freeze(profile), providerIdentifiers.openai, { + openAiCustomModelInfo: { reasoningEffort }, + }) + expect(merged.openAiCustomModelInfo).toEqual({ ...customInfo, reasoningEffort }) + expect(profile.openAiCustomModelInfo?.reasoningEffort).toBe("low") + }, + ) + + it.each([null, undefined])("clears only the nested effort using %s", (reasoningEffort) => { + const { reasoningEffort: _effort, ...rest } = customInfo + const merged = mergeProfileModelPatch( + { apiProvider: providerIdentifiers.openai, openAiCustomModelInfo: customInfo }, + providerIdentifiers.openai, + { openAiCustomModelInfo: { reasoningEffort } }, + ) + expect(merged.openAiCustomModelInfo).toEqual(rest) + }) + + it.each([null, undefined])("uses existing sane defaults when nested metadata is %s", (openAiCustomModelInfo) => { + const profile: ProviderSettings = { apiProvider: providerIdentifiers.openai, openAiCustomModelInfo } + expect( + mergeProfileModelPatch(profile, providerIdentifiers.openai, { + openAiCustomModelInfo: { reasoningEffort: "high" }, + }).openAiCustomModelInfo, + ).toEqual({ ...openAiModelInfoSaneDefaults, reasoningEffort: "high" }) + expect( + mergeProfileModelPatch(profile, providerIdentifiers.openai, { + openAiCustomModelInfo: { reasoningEffort: null }, + }).openAiCustomModelInfo, + ).toBe(openAiCustomModelInfo) + }) + + it.each([null, undefined, [], "patch", 1, false])("rejects non-object patches: %s", (patch) => { + expect(() => mergeProfileModelPatch(stored, providerIdentifiers.openrouter, patch)).toThrow() + }) + + it.each([ + { openRouterModelId: "" }, + { openRouterModelId: " " }, + { openRouterModelId: 1 }, + { openRouterModelId: null }, + { openRouterModelId: undefined }, + { reasoningEffort: "invalid" }, + { reasoningEffort: 1 }, + { reasoningEffort: false }, + { reasoningEffort: {} }, + { enableReasoningEffort: "false" }, + { enableReasoningEffort: 0 }, + { enableReasoningEffort: [] }, + { apiProvider: providerIdentifiers.openai }, + { openRouterApiKey: "replacement" }, + { openAiApiKey: "replacement" }, + { rateLimitSeconds: 1 }, + { modelTemperature: 0.5 }, + { modelMaxTokens: 1 }, + { modelMaxThinkingTokens: 1 }, + { awsCustomArn: "new-arn" }, + { awsCustomArnBaseModelId: "new-model" }, + { awsModelContextWindow: 1 }, + { openAiCustomModelInfo: { reasoningEffort: "high" } }, + ])("rejects invalid or restricted fields: %j", (patch) => { + expect(() => mergeProfileModelPatch(stored, providerIdentifiers.openrouter, patch)).toThrow() + }) + + it.each([ + null, + undefined, + [], + "high", + { reasoningEffort: "disable" }, + { reasoningEffort: "invalid" }, + { reasoningEffort: 1 }, + { reasoningEffort: false }, + { reasoningEffort: "high", contextWindow: 1 }, + { reasoningEffort: "high", openAiApiKey: "secret" }, + { supportsReasoningEffort: false }, + ])("rejects unsafe custom metadata patches: %j", (openAiCustomModelInfo) => { + expect(() => + mergeProfileModelPatch({ apiProvider: providerIdentifiers.openai }, providerIdentifiers.openai, { + openAiCustomModelInfo, + }), + ).toThrow() + }) + + it("rejects a mismatched or unsupported provider and resolves an unset provider to anthropic", () => { + expect(() => mergeProfileModelPatch(stored, providerIdentifiers.openai, {})).toThrow() + expect(() => mergeProfileModelPatch(stored, "unknown", {})).toThrow() + expect(mergeProfileModelPatch({}, providerIdentifiers.anthropic, { apiModelId: "claude" })).toEqual({ + apiProvider: providerIdentifiers.anthropic, + apiModelId: "claude", + }) + }) + + it("keeps empty patches as no-ops without clearing custom metadata", () => { + expect(mergeProfileModelPatch(stored, providerIdentifiers.openrouter, {})).toEqual(stored) + const profile: ProviderSettings = { apiProvider: providerIdentifiers.openai, openAiCustomModelInfo: customInfo } + expect(mergeProfileModelPatch(profile, providerIdentifiers.openai, { openAiCustomModelInfo: {} })).toEqual( + profile, + ) + }) + + it("preserves Bedrock ARN defenses for model and reasoning-only changes", () => { + const profile: ProviderSettings = { + apiProvider: providerIdentifiers.bedrock, + apiModelId: "custom-arn", + awsCustomArn: "stored-arn", + awsCustomArnBaseModelId: "base-model", + awsModelContextWindow: 100, + } + expect(mergeProfileModelPatch(profile, providerIdentifiers.bedrock, { reasoningEffort: "high" })).toEqual({ + ...profile, + reasoningEffort: "high", + }) + const merged = mergeProfileModelPatch(profile, providerIdentifiers.bedrock, { apiModelId: "catalog-model" }) + expect(merged).not.toHaveProperty("awsCustomArn") + expect(merged).not.toHaveProperty("awsCustomArnBaseModelId") + expect(merged).not.toHaveProperty("awsModelContextWindow") + expect(() => mergeProfileModelPatch(profile, providerIdentifiers.bedrock, { awsCustomArn: null })).toThrow() + expect(() => + mergeProfileModelPatch({ apiProvider: providerIdentifiers.bedrock }, providerIdentifiers.bedrock, { + apiModelId: "custom-arn", + }), + ).toThrow() + }) +}) diff --git a/src/core/webview/__tests__/providerInfoHandler.spec.ts b/src/core/webview/__tests__/providerInfoHandler.spec.ts index e8c4d66f50..511558e414 100644 --- a/src/core/webview/__tests__/providerInfoHandler.spec.ts +++ b/src/core/webview/__tests__/providerInfoHandler.spec.ts @@ -1,11 +1,36 @@ import type { WebviewMessage } from "@roo-code/types" +import axios, { AxiosError, AxiosHeaders } from "axios" import type { ClineProvider } from "../ClineProvider" import { getOpenRouterModelEndpoints } from "../../../api/providers/fetchers/openrouter" +import { getOpenAiModels } from "../../../api/providers/openai" import { handleRequestProviderInfo } from "../providerInfoHandler" vi.mock("../../../api/providers/fetchers/openrouter", () => ({ getOpenRouterModelEndpoints: vi.fn() })) +const OPENAI_FAILURES = [ + [ + "HTTP 401", + new AxiosError( + "Authorization: secret https://private.test returned 401", + "ERR_BAD_REQUEST", + undefined, + undefined, + { + status: 401, + statusText: "Unauthorized", + data: { error: "secret" }, + headers: {}, + config: { + url: "https://private.test/v1/models", + headers: new AxiosHeaders({ Authorization: "Bearer secret" }), + }, + }, + ), + ], + ["network rejection", new Error("Authorization: secret https://private.test refused")], +] as const + const KEY_INFO = { label: "sk-or-v1-...abc", usage: 12.5, @@ -35,7 +60,9 @@ describe("handleRequestProviderInfo", () => { beforeEach(() => { postMessageToWebview.mockReset() + vi.mocked(provider.log).mockReset() vi.mocked(getOpenRouterModelEndpoints).mockReset() + vi.spyOn(axios, "get").mockResolvedValue({ data: { data: [{ id: "model" }, { id: "model" }] } }) fetchSpy = vi.spyOn(globalThis, "fetch") vi.spyOn(console, "error").mockImplementation(() => {}) }) @@ -44,6 +71,117 @@ describe("handleRequestProviderInfo", () => { vi.restoreAllMocks() }) + it("rejects a request with no values without contacting a provider", async () => { + await send(undefined) + + expect(fetchSpy).not.toHaveBeenCalled() + expect(axios.get).not.toHaveBeenCalled() + expect(getOpenRouterModelEndpoints).not.toHaveBeenCalled() + expect(postMessageToWebview).toHaveBeenCalledExactlyOnceWith({ + type: "providerInfo", + requestId: "req-1", + error: "Invalid provider info request", + }) + }) + + it.each([undefined, {}, { Authorization: "custom-token", "X-Provider": "custom" }])( + "fetches OpenAI models with caller credentials and headers %j", + async (headers) => { + await send({ kind: "openAiModels", baseUrl: "https://compatible.test/v1", apiKey: "secret", headers }) + expect(axios.get).toHaveBeenCalledExactlyOnceWith("https://compatible.test/v1/models", { + headers: expect.objectContaining({ ...headers, Authorization: "Bearer secret" }), + }) + expect(postMessageToWebview).toHaveBeenCalledExactlyOnceWith({ + type: "providerInfo", + requestId: "req-1", + values: { data: ["model"] }, + }) + }, + ) + + it.each([null, "invalid", ["header"], { custom: 42 }])("rejects malformed OpenAI headers %j", async (headers) => { + await send({ kind: "openAiModels", headers }) + expect(axios.get).not.toHaveBeenCalled() + expect(postMessageToWebview).toHaveBeenCalledExactlyOnceWith({ + type: "providerInfo", + requestId: "req-1", + error: "Invalid provider info request", + }) + }) + + it.each(OPENAI_FAILURES)( + "sanitizes OpenAI %s from the real helper while retaining correlation", + async (_name, error) => { + vi.mocked(axios.get).mockRejectedValueOnce(error) + await send({ kind: "openAiModels", baseUrl: "https://private.test/v1", apiKey: "secret" }) + expect(axios.get).toHaveBeenCalledExactlyOnceWith("https://private.test/v1/models", { + headers: expect.objectContaining({ Authorization: "Bearer secret" }), + }) + expect(postMessageToWebview).toHaveBeenCalledExactlyOnceWith({ + type: "providerInfo", + requestId: "req-1", + error: "Provider info request failed", + }) + expect(provider.log).toHaveBeenCalledExactlyOnceWith("Failed to fetch provider info (openAiModels)") + expect(console.error).toHaveBeenCalledExactlyOnceWith( + "Error fetching provider info (openAiModels):", + error.name, + ) + const exposed = JSON.stringify([ + postMessageToWebview.mock.calls, + vi.mocked(provider.log).mock.calls, + vi.mocked(console.error).mock.calls, + ]) + expect(exposed).not.toContain("secret") + expect(exposed).not.toContain("private.test") + }, + ) + + it("sanitizes a non-Error provider rejection without leaking its credentials", async () => { + vi.mocked(axios.get).mockRejectedValueOnce("secret https://private.test refused") + + await send({ kind: "openAiModels", baseUrl: "https://private.test/v1", apiKey: "secret" }) + + expect(postMessageToWebview).toHaveBeenCalledExactlyOnceWith({ + type: "providerInfo", + requestId: "req-1", + error: "Provider info request failed", + }) + expect(provider.log).toHaveBeenCalledExactlyOnceWith("Failed to fetch provider info (openAiModels)") + expect(console.error).toHaveBeenCalledExactlyOnceWith("Error fetching provider info (openAiModels):", "unknown") + }) + + it("returns a successful empty OpenAI catalog rather than a broker error", async () => { + vi.mocked(axios.get).mockResolvedValueOnce({ data: { data: [] } }) + await send({ kind: "openAiModels", baseUrl: "https://compatible.test/v1" }) + expect(axios.get).toHaveBeenCalledExactlyOnceWith("https://compatible.test/v1/models", { + headers: expect.not.objectContaining({ Authorization: expect.anything() }), + }) + expect(postMessageToWebview).toHaveBeenCalledExactlyOnceWith({ + type: "providerInfo", + requestId: "req-1", + values: { data: [] }, + }) + expect(provider.log).not.toHaveBeenCalled() + expect(console.error).not.toHaveBeenCalled() + }) + + it.each(OPENAI_FAILURES)("preserves legacy empty-list behavior on %s", async (_name, error) => { + vi.mocked(axios.get).mockRejectedValue(error) + await expect(getOpenAiModels("https://compatible.test/v1", "secret")).resolves.toEqual([]) + await expect( + getOpenAiModels("https://compatible.test/v1", "secret", undefined, { throwOnError: false }), + ).resolves.toEqual([]) + expect(axios.get).toHaveBeenCalledTimes(2) + }) + + it.each(OPENAI_FAILURES)("propagates the original %s only when strict mode is enabled", async (_name, error) => { + vi.mocked(axios.get).mockRejectedValueOnce(error) + await expect( + getOpenAiModels("https://compatible.test/v1", "secret", undefined, { throwOnError: true }), + ).rejects.toBe(error) + }) + it("fetches OpenRouter key info from the default endpoint with a bearer token", async () => { fetchSpy.mockResolvedValue(jsonResponse({ data: KEY_INFO })) diff --git a/src/core/webview/__tests__/webviewMessageHandler.spec.ts b/src/core/webview/__tests__/webviewMessageHandler.spec.ts index 98c306a4f3..2e98f86f56 100644 --- a/src/core/webview/__tests__/webviewMessageHandler.spec.ts +++ b/src/core/webview/__tests__/webviewMessageHandler.spec.ts @@ -1129,6 +1129,36 @@ describe("webviewMessageHandler - requestRouterModels", () => { }) }) + it.each([ + { litellmBaseUrl: "https://profile-b.test/v1", litellmApiKey: "profile-b-secret" }, + { litellmBaseUrl: "https://profile-b.test/v1" }, + ])("uses only request-owned configuration and echoes correlation", async (apiConfiguration) => { + mockGetModels.mockResolvedValue({}) + await webviewMessageHandler(mockClineProvider, { + type: "requestRouterModels", + requestId: "profile-b-request", + apiConfiguration: { apiProvider: providerIdentifiers.litellm, ...apiConfiguration }, + values: { provider: providerIdentifiers.litellm }, + }) + expect(mockClineProvider.getState).not.toHaveBeenCalled() + if (apiConfiguration.litellmApiKey) { + expect(mockGetModels).toHaveBeenCalledWith( + { + provider: providerIdentifiers.litellm, + apiKey: apiConfiguration.litellmApiKey, + baseUrl: apiConfiguration.litellmBaseUrl, + }, + expect.anything(), + ) + } else expect(mockGetModels).not.toHaveBeenCalled() + expect(mockClineProvider.postMessageToWebview).toHaveBeenCalledWith({ + type: "routerModels", + requestId: "profile-b-request", + values: { provider: providerIdentifiers.litellm }, + routerModels: apiConfiguration.litellmApiKey ? { litellm: {} } : {}, + }) + }) + it("successfully fetches models from all providers", async () => { const mockModels: ModelRecord = { "model-1": { diff --git a/src/core/webview/profileModelPatch.ts b/src/core/webview/profileModelPatch.ts new file mode 100644 index 0000000000..0c6f5830e4 --- /dev/null +++ b/src/core/webview/profileModelPatch.ts @@ -0,0 +1,112 @@ +import { + modelIdKeys, + modelInfoSchema, + openAiModelInfoSaneDefaults, + providerDefinitionList, + providerIdentifiers, + providerSettingsSchema, + reasoningEffortSettingSchema, + type ProviderSettings, +} from "@roo-code/types" + +// These fields remain reset-only: in particular, an ARN cannot bypass the model allow-list. +const RESET_ONLY_KEYS = [ + "modelMaxTokens", + "modelMaxThinkingTokens", + "awsCustomArn", + "awsCustomArnBaseModelId", + "awsModelContextWindow", +] as const satisfies readonly (keyof ProviderSettings)[] + +function requireRecord(value: unknown): Record { + if (!value || typeof value !== "object" || Array.isArray(value)) { + throw new Error("A model patch must be an object") + } + + return value as Record +} + +function mergeCustomReasoningEffort( + stored: ProviderSettings["openAiCustomModelInfo"], + value: unknown, +): ProviderSettings["openAiCustomModelInfo"] { + const patch = requireRecord(value) + + if (Object.keys(patch).some((key) => key !== "reasoningEffort")) { + throw new Error("Only reasoningEffort may be patched in openAiCustomModelInfo") + } + + if (!Object.hasOwn(patch, "reasoningEffort")) return stored + + if (patch.reasoningEffort === null || patch.reasoningEffort === undefined) { + if (!stored) return stored + const { reasoningEffort: _effort, ...rest } = stored + return rest + } + + // Model metadata uses the wire effort enum, not the top-level "disable" control value. + const reasoningEffort = modelInfoSchema.shape.reasoningEffort.unwrap().parse(patch.reasoningEffort) + return { ...(stored ?? openAiModelInfoSaneDefaults), reasoningEffort } +} + +function enforceBedrockArnInvariant(merged: ProviderSettings): void { + if (merged.apiProvider !== providerIdentifiers.bedrock) return + + if (merged.apiModelId === "custom-arn") { + if (!merged.awsCustomArn) { + throw new Error('Model "custom-arn" requires an existing custom ARN') + } + } else { + // A retained ARN would override the allow-listed model, so drop its context fields too. + delete merged.awsCustomArn + delete merged.awsCustomArnBaseModelId + delete merged.awsModelContextWindow + } +} + +/** + * Merge only model/reasoning controls into the latest stored profile without mutating it. + * Null/undefined clear controls; model IDs must be non-empty strings. Nested custom model + * metadata is effort-only and uses its own schema. Organization validation remains the caller's job. + */ +export function mergeProfileModelPatch( + stored: ProviderSettings, + expectedProvider: string, + patch: unknown, +): ProviderSettings { + const apiProvider = stored.apiProvider ?? providerIdentifiers.anthropic + const definition = providerDefinitionList.find((entry) => entry.apiProvider === expectedProvider) + if (apiProvider !== expectedProvider || !definition) { + throw new Error("Profile provider does not match a supported expected provider") + } + + const modelIdKey = modelIdKeys.find((key) => key === definition.modelIdKey) + const merged: ProviderSettings = { ...stored, apiProvider } + + for (const [key, value] of Object.entries(requireRecord(patch))) { + if (modelIdKey !== undefined && key === modelIdKey) { + if (typeof value !== "string" || value.trim().length === 0) { + throw new Error(`Key "${key}" must be a non-empty string in a model patch`) + } + merged[modelIdKey] = value + } else if (key === "reasoningEffort") { + if (value === null || value === undefined) delete merged.reasoningEffort + else merged.reasoningEffort = reasoningEffortSettingSchema.parse(value) + } else if (key === "enableReasoningEffort") { + if (value === null || value === undefined) delete merged.enableReasoningEffort + else merged.enableReasoningEffort = providerSettingsSchema.shape.enableReasoningEffort.unwrap().parse(value) + } else if (key === "openAiCustomModelInfo" && apiProvider === providerIdentifiers.openai) { + merged.openAiCustomModelInfo = mergeCustomReasoningEffort(stored.openAiCustomModelInfo, value) + } else { + const resetKey = RESET_ONLY_KEYS.find((candidate) => candidate === key) + if (!resetKey) throw new Error(`Key "${key}" is not allowed in a model patch`) + if (value !== null && value !== undefined) { + throw new Error(`Key "${key}" can only be cleared in a model patch`) + } + delete merged[resetKey] + } + } + + enforceBedrockArnInvariant(merged) + return merged +} diff --git a/src/core/webview/providerInfoHandler.ts b/src/core/webview/providerInfoHandler.ts index aac43cd2cc..5b155ea528 100644 --- a/src/core/webview/providerInfoHandler.ts +++ b/src/core/webview/providerInfoHandler.ts @@ -4,6 +4,7 @@ import type { ProviderInfoKind, ProviderInfoParams, ProviderInfoResultMap, Webvi import type { ClineProvider } from "./ClineProvider" import { getOpenRouterModelEndpoints } from "../../api/providers/fetchers/openrouter" +import { getOpenAiModels } from "../../api/providers/openai" import { toRequestyServiceUrl } from "../../shared/utils/requesty" const FETCH_TIMEOUT_MS = 10_000 @@ -119,6 +120,7 @@ async function getRequestyKeyInfo({ apiKey, baseUrl }: ProviderInfoParams) { } const fetchers: { [K in ProviderInfoKind]: (params: ProviderInfoParams) => Promise } = { + openAiModels: ({ baseUrl, apiKey, headers }) => getOpenAiModels(baseUrl, apiKey, headers, { throwOnError: true }), openRouterKeyInfo: getOpenRouterKeyInfo, openRouterModelProviders: getOpenRouterModelProviders, requestyKeyInfo: getRequestyKeyInfo, @@ -132,6 +134,7 @@ const providerInfoParamsSchema = z.object({ apiKey: z.string().optional(), baseUrl: z.string().optional(), modelId: z.string().optional(), + headers: z.record(z.string(), z.string()).optional(), }) /** diff --git a/src/core/webview/webviewMessageHandler.ts b/src/core/webview/webviewMessageHandler.ts index cf9d209191..06276fee3f 100644 --- a/src/core/webview/webviewMessageHandler.ts +++ b/src/core/webview/webviewMessageHandler.ts @@ -28,6 +28,7 @@ import { OllamaModelsMessageType, OpenAiModelsMessageType, RouterModelsMessageType, + providerSettingsSchema, VsCodeLmModelsMessageType, isTelemetryOptedIn, } from "@roo-code/types" @@ -1178,7 +1179,11 @@ export const webviewMessageHandler = async ( break } case RouterModelsMessageType.requestRouterModels: { - const { apiConfiguration } = await provider.getState() + // A scoped picker request owns its configuration, even if the active profile changes + // while discovery is in flight. Do not merge absent credentials from global state. + const apiConfiguration = message.apiConfiguration + ? providerSettingsSchema.parse(message.apiConfiguration) + : (await provider.getState()).apiConfiguration // Optional single provider filter from webview const requestedProvider = message?.values?.provider @@ -1482,6 +1487,7 @@ export const webviewMessageHandler = async ( await provider.postMessageToWebview({ type: RouterModelsMessageType.routerModels, + requestId: message.requestId, routerModels, values: providerFilter ? { provider: requestedProvider } : undefined, }) diff --git a/webview-ui/playwright/gallery/UnifiedModelPicker.fixture.tsx b/webview-ui/playwright/gallery/UnifiedModelPicker.fixture.tsx new file mode 100644 index 0000000000..b15da24511 --- /dev/null +++ b/webview-ui/playwright/gallery/UnifiedModelPicker.fixture.tsx @@ -0,0 +1,76 @@ +import { useState } from "react" +import { providerIdentifiers, type ProviderSettings } from "@roo-code/types" + +import { ApiConfigSelector } from "@/components/chat/ApiConfigSelector" +import { ProfileQuickSettings } from "@/components/chat/ProfileQuickSettings" + +import { AppProviders } from "../AppProviders" +import { ExtensionStateContextProvider, useExtensionState } from "../ExtensionStateContext" + +const profiles = [ + { id: "work", name: "Work", modelId: "gpt-5" }, + { id: "personal", name: "Personal", modelId: "gpt-5-mini" }, + { id: "review", name: "Review", modelId: "gpt-5.4" }, +] + +/** Real picker, static production catalog, and fresh browser-owned state on every mount. */ +export function UnifiedModelPickerStory() { + return ( + + + + ) +} + +function MountedModelPicker() { + const state = useExtensionState() + const [profileId, setProfileId] = useState("work") + const [pinned, setPinned] = useState>({ work: true }) + const [locked, setLocked] = useState(false) + const profile = profiles.find(({ id }) => id === profileId) ?? profiles[0] + const apiConfiguration: ProviderSettings = { + apiProvider: providerIdentifiers.openaiNative, + apiModelId: profile.modelId, + reasoningEffort: "high", + enableReasoningEffort: true, + } + + return ( + +
+ } + title="Choose profile and model" + triggerClassName="max-w-full self-start" + onChange={(id) => { + const next = profiles.find((candidate) => candidate.id === id) ?? profiles[0] + // The gallery provider accepts live values; keep the picker mounted while switching context. + setProfileId(next.id) + }} + listApiConfigMeta={profiles} + pinnedApiConfigs={pinned} + togglePinnedApiConfig={(id) => setPinned((previous) => ({ ...previous, [id]: !previous[id] }))} + lockApiConfigAcrossModes={locked} + onToggleLockApiConfig={() => setLocked((previous) => !previous)} + /> +
+
+ ) +} diff --git a/webview-ui/playwright/gallery/stories.tsx b/webview-ui/playwright/gallery/stories.tsx index 6459aad7aa..67bde368fc 100644 --- a/webview-ui/playwright/gallery/stories.tsx +++ b/webview-ui/playwright/gallery/stories.tsx @@ -12,6 +12,10 @@ const mermaidGantt = `gantt Ship release :active, release, after scope, 3d` export const stories: Record = { + "unified-model-picker": async () => { + const { UnifiedModelPickerStory } = await import("./UnifiedModelPicker.fixture") + return + }, "terminal-settings": async () => { const [{ AppProviders }, { TerminalSettings }] = await Promise.all([ import("../AppProviders"), diff --git a/webview-ui/src/components/chat/ApiConfigSelector.tsx b/webview-ui/src/components/chat/ApiConfigSelector.tsx index ef5151a00d..69b5837f52 100644 --- a/webview-ui/src/components/chat/ApiConfigSelector.tsx +++ b/webview-ui/src/components/chat/ApiConfigSelector.tsx @@ -1,5 +1,6 @@ -import { useState, useMemo, useCallback } from "react" +import { useState, useMemo, useCallback, useEffect, type ReactNode } from "react" import { Fzf } from "fzf" +import { Brain, ChevronDown } from "lucide-react" import { cn } from "@/lib/utils" import { enabledSelectorTriggerClassName, selectorTriggerClassName } from "@/components/ui/selectorTriggerStyles" @@ -10,10 +11,15 @@ import { vscode } from "@/utils/vscode" import { Button } from "@/components/ui" import { IconButton } from "./IconButton" +import { PickerSearchField } from "./PickerSearchField" +import { handlePickerNavigation, pickerResultsClassName } from "./model-picker/pickerNavigation" interface ApiConfigSelectorProps { value: string displayName: string + modelId?: string + reasoningEffort?: string + quickSettings?: ReactNode disabled?: boolean title: string onChange: (value: string) => void @@ -28,6 +34,9 @@ interface ApiConfigSelectorProps { export const ApiConfigSelector = ({ value, displayName, + modelId, + reasoningEffort, + quickSettings, disabled = false, title, onChange, @@ -41,6 +50,12 @@ export const ApiConfigSelector = ({ const { t } = useAppTranslation() const [open, setOpen] = useState(false) const [searchValue, setSearchValue] = useState("") + const [showProfiles, setShowProfiles] = useState(false) + const [pendingProfileId, setPendingProfileId] = useState(null) + const isProfileView = !quickSettings || showProfiles + useEffect(() => { + if (pendingProfileId === value) setPendingProfileId(null) + }, [value, pendingProfileId]) const portalContainer = useRooPortal("roo-portal") // Create searchable items for fuzzy search. @@ -79,10 +94,13 @@ export const ApiConfigSelector = ({ const handleSelect = useCallback( (configId: string) => { onChange(configId) - setOpen(false) + if (quickSettings) { + setPendingProfileId(configId === value ? null : configId) + setShowProfiles(false) + } else setOpen(false) setSearchValue("") }, - [onChange], + [onChange, quickSettings, value], ) const handleEditClick = useCallback(() => { @@ -97,25 +115,28 @@ export const ApiConfigSelector = ({ return (
handleSelect(config.id)} className={cn( "px-3 py-1.5 text-sm cursor-pointer flex items-center group", "hover:bg-vscode-list-hoverBackground", isCurrentConfig && "bg-vscode-list-activeSelectionBackground text-vscode-list-activeSelectionForeground", )}> -
+
+
{isCurrentConfig && (
@@ -126,7 +147,8 @@ export const ApiConfigSelector = ({
) }, - [value, handleSelect, t, togglePinnedApiConfig], + [value, handleSelect, t, togglePinnedApiConfig, disabled], ) return ( - + { + setOpen(nextOpen) + if (nextOpen) setPendingProfileId(null) + if (!nextOpen) { + setShowProfiles(false) + setSearchValue("") + } + }} + data-testid="api-config-selector-root"> - {displayName} + + + {displayName} + {modelId && <> / {modelId}} + + {reasoningEffort && reasoningEffort !== "disable" && ( + + + )} + -
- {/* Search input or info blurb */} - {listApiConfigMeta.length > 6 ? ( -
- setSearchValue(e.target.value)} - placeholder={t("common:ui.search_placeholder")} - className="w-full h-8 px-2 py-1 text-xs bg-vscode-input-background text-vscode-input-foreground border border-vscode-input-border rounded focus:outline-0" - autoFocus + className="flex max-h-[var(--radix-popover-content-available-height)] w-[300px] max-w-[calc(100vw-16px)] flex-col overflow-hidden p-0"> +
+ {quickSettings && ( +
- ) : ( + + )} + {!isProfileView && + (pendingProfileId && pendingProfileId !== value ? ( +
+ {t("chat:modelPicker.loadingProfile")} +
+ ) : ( + quickSettings + ))} + {isProfileView && (quickSettings || listApiConfigMeta.length > 6) && ( + + )} + {isProfileView && !quickSettings && listApiConfigMeta.length <= 6 && (

{t("prompts:apiConfiguration.select")}

)} - {/* Config list - single scroll container */} {filteredConfigs.length === 0 && searchValue ? ( -
{t("common:ui.no_results")}
+ ) : ( -
+