@@ -56,6 +56,7 @@ import {
5656 DEFAULT_CHECKPOINT_TIMEOUT_SECONDS ,
5757 getModelId ,
5858 isRetiredProvider ,
59+ modelIdKeys ,
5960 providerIdentifiers ,
6061} from "@roo-code/types"
6162import { RateLimitClock , createRateLimitClock } from "../task/RateLimitClock"
@@ -191,6 +192,17 @@ function scheduleTask(
191192 } )
192193}
193194
195+ // Keys a model-selector patch may touch: model IDs plus settings reset on model change.
196+ const PROFILE_MODEL_PATCH_KEYS : ReadonlySet < string > = new Set ( [
197+ ...modelIdKeys ,
198+ "reasoningEffort" ,
199+ "modelMaxTokens" ,
200+ "modelMaxThinkingTokens" ,
201+ "awsCustomArn" ,
202+ "awsCustomArnBaseModelId" ,
203+ "awsModelContextWindow" ,
204+ ] )
205+
194206type GetStateOptions = {
195207 includeTaskHistory ?: boolean
196208}
@@ -1880,49 +1892,9 @@ export class ClineProvider
18801892 activate : boolean = true ,
18811893 ) : Promise < string | undefined > {
18821894 try {
1883- return await this . enqueueProviderProfileMutation ( async ( signal ) => {
1884- // TODO: Do we need to be calling `activateProfile`? It's not
1885- // clear to me what the source of truth should be; in some cases
1886- // we rely on the `ContextProxy`'s data store and in other cases
1887- // we rely on the `ProviderSettingsManager`'s data store. It might
1888- // be simpler to unify these two.
1889- const id = await this . providerSettingsManager . saveConfig ( name , providerSettings )
1890-
1891- if ( signal . aborted ) return id
1892-
1893- if ( activate ) {
1894- const { mode } = await this . getState ( )
1895-
1896- // These promises do the following:
1897- // 1. Adds or updates the list of provider profiles.
1898- // 2. Sets the current provider profile.
1899- // 3. Sets the current mode's provider profile.
1900- // 4. Copies the provider settings to the context.
1901- //
1902- // Note: 1, 2, and 4 can be done in one `ContextProxy` call:
1903- // this.contextProxy.setValues({ ...providerSettings, listApiConfigMeta: ..., currentApiConfigName: ... })
1904- // We should probably switch to that and verify that it works.
1905- // I left the original implementation in just to be safe.
1906- await Promise . all ( [
1907- this . updateGlobalState ( "listApiConfigMeta" , await this . providerSettingsManager . listConfig ( ) ) ,
1908- this . updateGlobalState ( "currentApiConfigName" , name ) ,
1909- this . providerSettingsManager . setModeConfig ( mode , id ) ,
1910- this . contextProxy . setProviderSettings ( providerSettings ) ,
1911- ] )
1912-
1913- // Change the provider for the current task.
1914- // TODO: We should rename `buildApiHandler` for clarity (e.g. `getProviderClient`).
1915- this . updateTaskApiHandlerIfNeeded ( providerSettings , { forceRebuild : true } )
1916-
1917- // Keep the current task's sticky provider profile in sync with the newly-activated profile.
1918- await this . persistStickyProviderProfileToCurrentTask ( name )
1919- } else {
1920- await this . updateGlobalState ( "listApiConfigMeta" , await this . providerSettingsManager . listConfig ( ) )
1921- }
1922-
1923- await this . postStateToWebview ( )
1924- return id
1925- } )
1895+ return await this . enqueueProviderProfileMutation ( ( signal ) =>
1896+ this . persistProviderProfile ( name , providerSettings , activate , signal ) ,
1897+ )
19261898 } catch ( error ) {
19271899 this . log (
19281900 `Error create new api configuration: ${ JSON . stringify ( error , Object . getOwnPropertyNames ( error ) , 2 ) } ` ,
@@ -1933,6 +1905,106 @@ export class ClineProvider
19331905 }
19341906 }
19351907
1908+ /**
1909+ * Applies a model change to the latest stored profile inside the mutation queue,
1910+ * so a stale webview snapshot cannot overwrite newer profile settings.
1911+ * A `null` patch value clears the key.
1912+ */
1913+ async updateProfileModel ( name : string , expectedProvider : string , patch : Record < string , unknown > ) : Promise < void > {
1914+ try {
1915+ await this . enqueueProviderProfileMutation ( async ( signal ) => {
1916+ const { name : _name , ...stored } = await this . providerSettingsManager . getProfile ( { name } )
1917+
1918+ if ( stored . apiProvider !== expectedProvider ) {
1919+ throw new Error (
1920+ `Profile "${ name } " provider changed from "${ expectedProvider } " to "${ String ( stored . apiProvider ) } "` ,
1921+ )
1922+ }
1923+
1924+ const merged : Record < string , unknown > = { ...stored }
1925+
1926+ for ( const [ key , value ] of Object . entries ( patch ) ) {
1927+ if ( ! PROFILE_MODEL_PATCH_KEYS . has ( key ) ) {
1928+ throw new Error ( `Key "${ key } " is not allowed in a model patch` )
1929+ }
1930+
1931+ if ( value === null || value === undefined ) {
1932+ delete merged [ key ]
1933+ } else {
1934+ merged [ key ] = value
1935+ }
1936+ }
1937+
1938+ const { organizationAllowList } = await this . getState ( )
1939+
1940+ if ( ! ProfileValidator . isProfileAllowed ( merged as ProviderSettings , organizationAllowList ) ) {
1941+ throw new OrganizationAllowListViolationError ( t ( "common:errors.violated_organization_allowlist" ) )
1942+ }
1943+
1944+ if ( signal . aborted ) return
1945+
1946+ await this . persistProviderProfile ( name , merged as ProviderSettings , true , signal )
1947+ } )
1948+ } catch ( error ) {
1949+ this . log ( `Error update profile model: ${ JSON . stringify ( error , Object . getOwnPropertyNames ( error ) , 2 ) } ` )
1950+
1951+ vscode . window . showErrorMessage (
1952+ error instanceof OrganizationAllowListViolationError
1953+ ? error . message
1954+ : t ( "common:errors.create_api_config" ) ,
1955+ )
1956+ }
1957+ }
1958+
1959+ private async persistProviderProfile (
1960+ name : string ,
1961+ providerSettings : ProviderSettings ,
1962+ activate : boolean ,
1963+ signal : AbortSignal ,
1964+ ) : Promise < string | undefined > {
1965+ // TODO: Do we need to be calling `activateProfile`? It's not
1966+ // clear to me what the source of truth should be; in some cases
1967+ // we rely on the `ContextProxy`'s data store and in other cases
1968+ // we rely on the `ProviderSettingsManager`'s data store. It might
1969+ // be simpler to unify these two.
1970+ const id = await this . providerSettingsManager . saveConfig ( name , providerSettings )
1971+
1972+ if ( signal . aborted ) return id
1973+
1974+ if ( activate ) {
1975+ const { mode } = await this . getState ( )
1976+
1977+ // These promises do the following:
1978+ // 1. Adds or updates the list of provider profiles.
1979+ // 2. Sets the current provider profile.
1980+ // 3. Sets the current mode's provider profile.
1981+ // 4. Copies the provider settings to the context.
1982+ //
1983+ // Note: 1, 2, and 4 can be done in one `ContextProxy` call:
1984+ // this.contextProxy.setValues({ ...providerSettings, listApiConfigMeta: ..., currentApiConfigName: ... })
1985+ // We should probably switch to that and verify that it works.
1986+ // I left the original implementation in just to be safe.
1987+ await Promise . all ( [
1988+ this . updateGlobalState ( "listApiConfigMeta" , await this . providerSettingsManager . listConfig ( ) ) ,
1989+ this . updateGlobalState ( "currentApiConfigName" , name ) ,
1990+ this . providerSettingsManager . setModeConfig ( mode , id ) ,
1991+ this . contextProxy . setProviderSettings ( providerSettings ) ,
1992+ ] )
1993+
1994+ // Change the provider for the current task.
1995+ // TODO: We should rename `buildApiHandler` for clarity (e.g. `getProviderClient`).
1996+ this . updateTaskApiHandlerIfNeeded ( providerSettings , { forceRebuild : true } )
1997+
1998+ // Keep the current task's sticky provider profile in sync with the newly-activated profile.
1999+ await this . persistStickyProviderProfileToCurrentTask ( name )
2000+ } else {
2001+ await this . updateGlobalState ( "listApiConfigMeta" , await this . providerSettingsManager . listConfig ( ) )
2002+ }
2003+
2004+ await this . postStateToWebview ( )
2005+ return id
2006+ }
2007+
19362008 async deleteProviderProfile ( profileToDelete : ProviderSettingsEntry ) {
19372009 const globalSettings = this . contextProxy . getValues ( )
19382010 let profileToActivate : string | undefined = globalSettings . currentApiConfigName
0 commit comments