Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions packages/types/src/vscode-extension-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ export interface ProviderInfoParams {
apiKey?: string
baseUrl?: string
modelId?: string
headers?: Record<string, string>
}

/** 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
Expand Down
11 changes: 9 additions & 2 deletions src/api/providers/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,12 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
}
}

export async function getOpenAiModels(baseUrl?: string, apiKey?: string, openAiHeaders?: Record<string, string>) {
export async function getOpenAiModels(
baseUrl?: string,
apiKey?: string,
openAiHeaders?: Record<string, string>,
options: { throwOnError?: boolean } = {},
) {
try {
if (!baseUrl) {
return []
Expand Down Expand Up @@ -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<string>(modelsArray)]
} catch {
} catch (error) {
// The provider-info broker needs failures; legacy Settings callers expect an empty list.
if (options.throwOnError) throw error
return []
}
}
60 changes: 5 additions & 55 deletions src/core/webview/ClineProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ import {
DEFAULT_CHECKPOINT_TIMEOUT_SECONDS,
getModelId,
isRetiredProvider,
modelIdKeys,
providerIdentifiers,
} from "@roo-code/types"
import { RateLimitClock, createRateLimitClock } from "../task/RateLimitClock"
Expand All @@ -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"
Expand Down Expand Up @@ -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<string> = 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<string> = new Set([
"reasoningEffort",
"modelMaxTokens",
"modelMaxThinkingTokens",
"awsCustomArn",
"awsCustomArnBaseModelId",
"awsModelContextWindow",
])

type GetStateOptions = {
includeTaskHistory?: boolean
}
Expand Down Expand Up @@ -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.
*/
Expand All @@ -2315,45 +2299,11 @@ export class ClineProvider
)
}

const merged: Record<string, unknown> = { ...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"),
)
Expand All @@ -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.
Expand Down
128 changes: 127 additions & 1 deletion src/core/webview/__tests__/ClineProvider.apiHandlerRebuild.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
Loading
Loading