@@ -55,9 +55,17 @@ import {
5555 DEFAULT_MODES ,
5656 DEFAULT_CHECKPOINT_TIMEOUT_SECONDS ,
5757 getModelId ,
58+ modelIdKeysByProvider ,
5859 isRetiredProvider ,
5960 providerIdentifiers ,
6061} from "@roo-code/types"
62+
63+ const RESET_ONLY_KEYS : readonly string [ ] = [
64+ "awsCustomArn" ,
65+ "reasoningEffort" ,
66+ "modelMaxTokens" ,
67+ "modelMaxThinkingTokens" ,
68+ ]
6169import { RateLimitClock , createRateLimitClock } from "../task/RateLimitClock"
6270import { TaskRegistry } from "../task/TaskRegistry"
6371import { TaskScheduler } from "../task/TaskScheduler"
@@ -1930,6 +1938,130 @@ export class ClineProvider
19301938 }
19311939 }
19321940
1941+ private getOrganizationAllowListForProfileMutation ( ) {
1942+ if ( ! CloudService . hasInstance ( ) ) {
1943+ return ORGANIZATION_ALLOW_ALL
1944+ }
1945+
1946+ try {
1947+ const cloudService = CloudService . instance
1948+ if ( ! cloudService . isAuthenticated ( ) ) {
1949+ return ORGANIZATION_ALLOW_ALL
1950+ }
1951+ return cloudService . getOrganizationSettings ( ) ?. allowList
1952+ } catch ( error ) {
1953+ this . log (
1954+ `Unable to read organization allow-list for model update: ${ error instanceof Error ? error . message : String ( error ) } ` ,
1955+ )
1956+ return undefined
1957+ }
1958+ }
1959+
1960+ /**
1961+ * Applies a model selection to a stored profile. Everything runs inside one queued
1962+ * mutation so a selection made against a profile that has since been switched away
1963+ * from is dropped instead of saved and reactivated.
1964+ */
1965+ async updateProfileModel ( name : string , expectedProvider : string , patch : Record < string , unknown > ) : Promise < void > {
1966+ try {
1967+ await this . enqueueProviderProfileMutation ( async ( signal ) => {
1968+ // Mirrors the profile name the webview is shown (see getStateToPostToWebview).
1969+ const task = this . getCurrentTask ( )
1970+ const { currentApiConfigName, organizationAllowList : stateOrganizationAllowList } =
1971+ await this . getState ( )
1972+ const visibleProfileName = task ? task . taskApiConfigName : currentApiConfigName
1973+
1974+ if ( visibleProfileName !== name ) {
1975+ this . log ( `Ignoring model update for profile '${ name } ': active profile is '${ visibleProfileName } '` )
1976+ return
1977+ }
1978+
1979+ const { name : _name , id, ...stored } = await this . providerSettingsManager . getProfile ( { name } )
1980+
1981+ if ( signal . aborted ) return
1982+
1983+ // A profile without an explicit provider is treated as OpenRouter, matching the chat ModelSelector.
1984+ const storedProvider = stored . apiProvider ?? providerIdentifiers . openrouter
1985+
1986+ if ( storedProvider !== expectedProvider ) {
1987+ this . log (
1988+ `Ignoring model update for profile '${ name } ': provider is '${ storedProvider } ', expected '${ expectedProvider } '` ,
1989+ )
1990+ return
1991+ }
1992+
1993+ // Only model selection and its side-effect resets may be patched (see handleModelChangeSideEffects).
1994+ const providerModelKey : string | undefined =
1995+ storedProvider === providerIdentifiers . openai
1996+ ? "openAiModelId"
1997+ : modelIdKeysByProvider [ storedProvider as keyof typeof modelIdKeysByProvider ]
1998+ const allowedKeys : ReadonlySet < string > = new Set (
1999+ providerModelKey ? [ providerModelKey , ...RESET_ONLY_KEYS ] : RESET_ONLY_KEYS ,
2000+ )
2001+ const merged : Record < string , unknown > = { ...stored , id }
2002+ for ( const [ key , value ] of Object . entries ( patch ) ) {
2003+ // The provider is never patchable.
2004+ if ( key === "apiProvider" || ! allowedKeys . has ( key ) ) {
2005+ continue
2006+ }
2007+ if ( value !== null && typeof value !== "string" && typeof value !== "number" ) {
2008+ continue
2009+ }
2010+ if ( RESET_ONLY_KEYS . includes ( key ) && value !== null && ! ( key === "awsCustomArn" && value === "" ) ) {
2011+ continue
2012+ }
2013+ merged [ key ] = value === null ? undefined : value
2014+ }
2015+
2016+ const authoritativeOrganizationAllowList = this . getOrganizationAllowListForProfileMutation ( )
2017+ if ( ! authoritativeOrganizationAllowList ) {
2018+ this . log ( `Ignoring model update for profile '${ name } ': organization allow-list is unavailable` )
2019+ vscode . window . showErrorMessage ( t ( "common:errors.violated_organization_allowlist" ) )
2020+ return
2021+ }
2022+ if (
2023+ ! ProfileValidator . isProfileAllowed ( merged as ProviderSettings , authoritativeOrganizationAllowList )
2024+ ) {
2025+ this . log (
2026+ `Ignoring model update for profile '${ name } ': violates authoritative organization allow-list` ,
2027+ )
2028+ vscode . window . showErrorMessage ( t ( "common:errors.violated_organization_allowlist" ) )
2029+ return
2030+ }
2031+ if (
2032+ ! stateOrganizationAllowList . allowAll &&
2033+ ! ProfileValidator . isProfileAllowed ( merged as ProviderSettings , stateOrganizationAllowList )
2034+ ) {
2035+ this . log ( `Ignoring model update for profile '${ name } ': violates state organization allow-list` )
2036+ vscode . window . showErrorMessage ( t ( "common:errors.violated_organization_allowlist" ) )
2037+ return
2038+ }
2039+
2040+ const savedId = await this . providerSettingsManager . saveConfig ( name , merged as ProviderSettings )
2041+
2042+ if ( signal . aborted ) return
2043+
2044+ const { mode } = await this . getState ( )
2045+
2046+ await Promise . all ( [
2047+ this . updateGlobalState ( "listApiConfigMeta" , await this . providerSettingsManager . listConfig ( ) ) ,
2048+ this . updateGlobalState ( "currentApiConfigName" , name ) ,
2049+ this . providerSettingsManager . setModeConfig ( mode , savedId ) ,
2050+ this . contextProxy . setProviderSettings ( merged as ProviderSettings ) ,
2051+ ] )
2052+
2053+ if ( signal . aborted ) return
2054+
2055+ this . updateTaskApiHandlerIfNeeded ( merged as ProviderSettings , { forceRebuild : true } )
2056+ await this . persistStickyProviderProfileToCurrentTask ( name )
2057+ await this . postStateToWebview ( )
2058+ } )
2059+ } catch ( error ) {
2060+ this . log ( `Error updating profile model: ${ JSON . stringify ( error , Object . getOwnPropertyNames ( error ) , 2 ) } ` )
2061+ vscode . window . showErrorMessage ( t ( "common:errors.save_api_config" ) )
2062+ }
2063+ }
2064+
19332065 async deleteProviderProfile ( profileToDelete : ProviderSettingsEntry ) {
19342066 const globalSettings = this . contextProxy . getValues ( )
19352067 let profileToActivate : string | undefined = globalSettings . currentApiConfigName
0 commit comments