@@ -1494,6 +1494,9 @@ export default function App() {
14941494 const [ workspaceDefaultModelReady , setWorkspaceDefaultModelReady ] = createSignal ( false ) ;
14951495 const [ legacyDefaultModel , setLegacyDefaultModel ] = createSignal < ModelRef > ( DEFAULT_MODEL ) ;
14961496 const [ defaultModelExplicit , setDefaultModelExplicit ] = createSignal ( false ) ;
1497+ const [ pendingDefaultModelByWorkspace , setPendingDefaultModelByWorkspace ] = createSignal <
1498+ Record < string , string >
1499+ > ( { } ) ;
14971500 const [ autoCompactContextReady , setAutoCompactContextReady ] = createSignal ( false ) ;
14981501 const [ autoCompactContextDirty , setAutoCompactContextDirty ] = createSignal ( false ) ;
14991502 const [ autoCompactContextApplied , setAutoCompactContextApplied ] = createSignal ( true ) ;
@@ -5395,6 +5398,7 @@ export default function App() {
53955398 setDefaultModel ( DEFAULT_MODEL ) ;
53965399 setLegacyDefaultModel ( DEFAULT_MODEL ) ;
53975400 setDefaultModelExplicit ( false ) ;
5401+ setPendingDefaultModelByWorkspace ( { } ) ;
53985402 setShowThinking ( false ) ;
53995403 setHideTitlebar ( false ) ;
54005404 setAutoCompactContext ( false ) ;
@@ -6174,28 +6178,55 @@ export default function App() {
61746178 setModelPickerOpen ( true ) ;
61756179 }
61766180
6181+ function setPendingDefaultModelForWorkspace ( workspaceId : string , model : ModelRef | null ) {
6182+ const id = workspaceId . trim ( ) ;
6183+ if ( ! id ) return ;
6184+ setPendingDefaultModelByWorkspace ( ( current ) => {
6185+ const next = { ...current } ;
6186+ if ( model ) {
6187+ next [ id ] = formatModelRef ( model ) ;
6188+ } else {
6189+ delete next [ id ] ;
6190+ }
6191+ return next ;
6192+ } ) ;
6193+ }
6194+
6195+ function pendingDefaultModelForWorkspace ( workspaceId : string ) {
6196+ const id = workspaceId . trim ( ) ;
6197+ if ( ! id ) return null ;
6198+ return pendingDefaultModelByWorkspace ( ) [ id ] ?? null ;
6199+ }
6200+
6201+ function applyDefaultModelChoice ( next : ModelRef ) {
6202+ const workspaceId = workspaceStore . selectedWorkspaceId ( ) . trim ( ) ;
6203+ if ( workspaceId ) {
6204+ setPendingDefaultModelForWorkspace ( workspaceId , next ) ;
6205+ }
6206+ setDefaultModelExplicit ( true ) ;
6207+ setDefaultModel ( next ) ;
6208+ setLegacyDefaultModel ( next ) ;
6209+ }
6210+
61776211 function applyModelSelection ( next : ModelRef ) {
6178- const restorePromptFocus = modelPickerTarget ( ) === "session" ;
6212+ const target = modelPickerTarget ( ) ;
6213+ const restorePromptFocus = target === "session" ;
61796214
6180- if ( modelPickerTarget ( ) === "default" ) {
6181- setDefaultModelExplicit ( true ) ;
6182- setDefaultModel ( next ) ;
6183- closeModelPicker ( { restorePromptFocus : false } ) ;
6215+ if ( target === "default" ) {
6216+ applyDefaultModelChoice ( next ) ;
61846217 return ;
61856218 }
61866219
61876220 const id = selectedSessionId ( ) ;
61886221 if ( ! id ) {
61896222 setPendingSessionModel ( next ) ;
6190- setDefaultModelExplicit ( true ) ;
6191- setDefaultModel ( next ) ;
6223+ applyDefaultModelChoice ( next ) ;
61926224 closeModelPicker ( { restorePromptFocus } ) ;
61936225 return ;
61946226 }
61956227
61966228 setSessionModelOverrideById ( ( current ) => ( { ...current , [ id ] : next } ) ) ;
6197- setDefaultModelExplicit ( true ) ;
6198- setDefaultModel ( next ) ;
6229+ applyDefaultModelChoice ( next ) ;
61996230 closeModelPicker ( { restorePromptFocus } ) ;
62006231 }
62016232
@@ -7493,12 +7524,34 @@ export default function App() {
74937524 }
74947525 }
74957526
7527+ const pendingModelRef = pendingDefaultModelForWorkspace ( workspaceId ) ;
7528+ const loadedModelRef = configDefault ? formatModelRef ( configDefault ) : null ;
7529+
7530+ if ( pendingModelRef && pendingModelRef !== loadedModelRef ) {
7531+ if ( workspaceType === "local" && workspaceRoot ) {
7532+ setLastKnownConfigSnapshot ( getConfigSnapshot ( configFileContent ) ) ;
7533+ }
7534+
7535+ if ( ! cancelled ) {
7536+ setWorkspaceDefaultModelReady ( true ) ;
7537+ }
7538+ return ;
7539+ }
7540+
7541+ if ( pendingModelRef && loadedModelRef === pendingModelRef ) {
7542+ setPendingDefaultModelForWorkspace ( workspaceId , null ) ;
7543+ }
7544+
74967545 setDefaultModelExplicit ( Boolean ( configDefault ) ) ;
74977546 const nextDefault = configDefault ?? legacyDefaultModel ( ) ;
74987547 const currentDefault = untrack ( defaultModel ) ;
74997548 if ( nextDefault && ! modelEquals ( currentDefault , nextDefault ) ) {
75007549 setDefaultModel ( nextDefault ) ;
75017550 }
7551+ const currentLegacyDefault = untrack ( legacyDefaultModel ) ;
7552+ if ( nextDefault && ! modelEquals ( currentLegacyDefault , nextDefault ) ) {
7553+ setLegacyDefaultModel ( nextDefault ) ;
7554+ }
75027555
75037556 if ( workspaceType === "local" && workspaceRoot ) {
75047557 setLastKnownConfigSnapshot ( getConfigSnapshot ( configFileContent ) ) ;
@@ -7522,6 +7575,7 @@ export default function App() {
75227575 if ( ! defaultModelExplicit ( ) ) return ;
75237576
75247577 const workspace = workspaceStore . selectedWorkspaceDisplay ( ) ;
7578+ const workspaceId = workspaceStore . selectedWorkspaceId ( ) . trim ( ) ;
75257579 if ( workspace . workspaceType !== "local" ) return ;
75267580
75277581 const root = workspaceStore . selectedWorkspacePath ( ) . trim ( ) ;
@@ -7542,25 +7596,41 @@ export default function App() {
75427596 if ( canUseOpenworkServer ) {
75437597 const config = await openworkClient . getConfig ( openworkWorkspaceId ) ;
75447598 const currentModel = typeof config . opencode ?. model === "string" ? parseModelRef ( config . opencode . model ) : null ;
7545- if ( currentModel && modelEquals ( currentModel , nextModel ) ) return ;
7599+ if ( currentModel && modelEquals ( currentModel , nextModel ) ) {
7600+ if ( workspaceId ) {
7601+ setPendingDefaultModelForWorkspace ( workspaceId , null ) ;
7602+ }
7603+ return ;
7604+ }
75467605
75477606 await openworkClient . patchConfig ( openworkWorkspaceId , {
75487607 opencode : { model : formatModelRef ( nextModel ) } ,
75497608 } ) ;
7609+ if ( workspaceId ) {
7610+ setPendingDefaultModelForWorkspace ( workspaceId , null ) ;
7611+ }
75507612 markOpencodeConfigReloadRequired ( ) ;
75517613 return ;
75527614 }
75537615
75547616 const configFile = await readOpencodeConfig ( "project" , root ) ;
75557617 const existingModel = parseDefaultModelFromConfig ( configFile . content ) ;
7556- if ( existingModel && modelEquals ( existingModel , nextModel ) ) return ;
7618+ if ( existingModel && modelEquals ( existingModel , nextModel ) ) {
7619+ if ( workspaceId ) {
7620+ setPendingDefaultModelForWorkspace ( workspaceId , null ) ;
7621+ }
7622+ return ;
7623+ }
75577624
75587625 const content = formatConfigWithDefaultModel ( configFile . content , nextModel ) ;
75597626 const result = await writeOpencodeConfig ( "project" , root , content ) ;
75607627 if ( ! result . ok ) {
75617628 throw new Error ( result . stderr || result . stdout || "Failed to update opencode.json" ) ;
75627629 }
75637630 setLastKnownConfigSnapshot ( getConfigSnapshot ( content ) ) ;
7631+ if ( workspaceId ) {
7632+ setPendingDefaultModelForWorkspace ( workspaceId , null ) ;
7633+ }
75647634 markOpencodeConfigReloadRequired ( ) ;
75657635 } catch ( error ) {
75667636 if ( cancelled ) return ;
0 commit comments