3838M .switch_session = Promise .async (function (session_id )
3939 local selected_session = session .get_by_id (session_id ):await ()
4040
41- state .current_model = nil
42- state .current_mode = nil
41+ state .model . clear_model ()
42+ state .model . clear_mode ()
4343 M .ensure_current_mode ():await ()
4444
45- state .active_session = selected_session
46- state .restore_points = {}
45+ state .session . set_active ( selected_session )
46+ state .session . reset_restore_points ()
4747 if state .is_visible () then
4848 ui .focus_input ()
4949 else
@@ -74,7 +74,7 @@ M.check_cwd = function()
7474 { current_cwd = state .current_cwd , new_cwd = vim .fn .getcwd () }
7575 )
7676 state .current_cwd = vim .fn .getcwd ()
77- state .active_session = nil
77+ state .session . clear_active ()
7878 context .unload_attachments ()
7979 end
8080end
8383M .open = Promise .async (function (opts )
8484 opts = opts or { focus = ' input' , new_session = false }
8585
86- state .is_opening = true
86+ state .ui . set_opening ( true )
8787
8888 if not require (' opencode.ui.ui' ).is_opencode_focused () then
8989 require (' opencode.context' ).load ()
@@ -95,8 +95,8 @@ M.open = Promise.async(function(opts)
9595
9696 if are_windows_closed then
9797 if not ui .is_opencode_focused () then
98- state .last_code_win_before_opencode = vim .api .nvim_get_current_win ()
99- state .current_code_buf = vim .api .nvim_get_current_buf ()
98+ state .ui . set_last_code_window ( vim .api .nvim_get_current_win () )
99+ state .ui . set_current_code_buf ( vim .api .nvim_get_current_buf () )
100100 end
101101
102102 M .is_prompting_allowed ()
@@ -106,10 +106,10 @@ M.open = Promise.async(function(opts)
106106 if not restored then
107107 state .clear_hidden_window_state ()
108108 restoring_hidden = false
109- state .windows = ui .create_windows ()
109+ state .ui .set_windows ( ui . create_windows () )
110110 end
111111 else
112- state .windows = ui .create_windows ()
112+ state .ui .set_windows ( ui . create_windows () )
113113 end
114114 end
115115
@@ -122,29 +122,29 @@ M.open = Promise.async(function(opts)
122122 local server = server_job .ensure_server ():await ()
123123
124124 if not server then
125- state .is_opening = false
125+ state .ui . set_opening ( false )
126126 return Promise .new ():reject (' Server failed to start' )
127127 end
128128
129129 M .check_cwd ()
130130
131131 local ok , err = pcall (function ()
132132 if opts .new_session then
133- state .active_session = nil
133+ state .session . clear_active ()
134134 state .last_sent_context = nil
135135 context .unload_attachments ()
136136
137137 M .ensure_current_mode ():await ()
138138
139- state .active_session = M .create_new_session ():await ()
139+ state .session . set_active ( M .create_new_session ():await () )
140140 log .debug (' Created new session on open' , { session = state .active_session .id })
141141 else
142142 M .ensure_current_mode ():await ()
143143
144144 if not state .active_session then
145- state .active_session = session .get_last_workspace_session ():await ()
145+ state .session .set_active ( session . get_last_workspace_session ():await () )
146146 if not state .active_session then
147- state .active_session = M .create_new_session ():await ()
147+ state .session . set_active ( M .create_new_session ():await () )
148148 end
149149 else
150150 if not state .display_route and are_windows_closed and not restoring_hidden then
@@ -157,10 +157,10 @@ M.open = Promise.async(function(opts)
157157 end
158158 end
159159
160- state .is_opencode_focused = true
160+ state .ui . set_panel_focused ( true )
161161 end )
162162
163- state .is_opening = false
163+ state .ui . set_opening ( false )
164164
165165 if not ok then
166166 vim .notify (' Error opening panel: ' .. tostring (err ), vim .log .levels .ERROR )
@@ -198,17 +198,17 @@ M.send_message = Promise.async(function(prompt, opts)
198198 if opts .model then
199199 local provider , model = opts .model :match (' ^(.-)/(.+)$' )
200200 params .model = { providerID = provider , modelID = model }
201- state .current_model = opts .model
201+ state .model . set_model ( opts .model )
202202
203203 if opts .variant then
204204 params .variant = opts .variant
205- state .current_variant = opts .variant
205+ state .model . set_variant ( opts .variant )
206206 end
207207 end
208208
209209 if opts .agent then
210210 params .agent = opts .agent
211- state .current_mode = opts .agent
211+ state .model . set_mode ( opts .agent )
212212 end
213213
214214 params .parts = context .format_message (prompt , opts .context ):await ()
@@ -293,12 +293,10 @@ function M.configure_provider()
293293 return
294294 end
295295 local model_str = string.format (' %s/%s' , selection .provider , selection .model )
296- state .current_model = model_str
296+ state .model . set_model ( model_str )
297297
298298 if state .current_mode then
299- local mode_map = vim .deepcopy (state .user_mode_model_map )
300- mode_map [state .current_mode ] = model_str
301- state .user_mode_model_map = mode_map
299+ state .model .set_mode_model_override (state .current_mode , model_str )
302300 end
303301
304302 if state .is_visible () then
@@ -318,7 +316,7 @@ function M.configure_variant()
318316 return
319317 end
320318
321- state .current_variant = selection .name
319+ state .model . set_variant ( selection .name )
322320
323321 if state .is_visible () then
324322 ui .focus_input ()
@@ -378,7 +376,7 @@ M.cycle_variant = Promise.async(function()
378376 next_variant = variants [next_index ]
379377 end
380378
381- state .current_variant = next_variant
379+ state .model . set_variant ( next_variant )
382380
383381 local model_state = require (' opencode.model_state' )
384382 model_state .set_variant (provider , model , next_variant )
@@ -412,11 +410,11 @@ M.cancel = Promise.async(function()
412410 end
413411
414412 -- start a new one
415- state .opencode_server = nil
413+ state .jobs . clear_server ()
416414
417415 -- NOTE: start a new server here to make sure we're subscribed
418416 -- to server events before a user sends a message
419- state .opencode_server = server_job .ensure_server ():await () --[[ @as OpencodeServer]]
417+ state .jobs . set_server ( server_job .ensure_server ():await () --[[ @as OpencodeServer]] )
420418 end
421419 end
422420
@@ -486,18 +484,18 @@ M.switch_to_mode = Promise.async(function(mode)
486484 return false
487485 end
488486
489- state .current_mode = mode
487+ state .model . set_mode ( mode )
490488 local opencode_config = config_file .get_opencode_config ():await () --[[ @as OpencodeConfigFile]]
491489
492490 local agent_config = opencode_config and opencode_config .agent or {}
493491 local mode_config = agent_config [mode ] or {}
494492
495493 if state .user_mode_model_map [mode ] then
496- state .current_model = state .user_mode_model_map [mode ]
494+ state .model . set_model ( state .user_mode_model_map [mode ])
497495 elseif mode_config .model and mode_config .model ~= ' ' then
498- state .current_model = mode_config .model
496+ state .model . set_model ( mode_config .model )
499497 elseif opencode_config and opencode_config .model and opencode_config .model ~= ' ' then
500- state .current_model = opencode_config .model
498+ state .model . set_model ( opencode_config .model )
501499 end
502500 return true
503501end )
@@ -518,10 +516,10 @@ M.ensure_current_mode = Promise.async(function()
518516
519517 -- Try to use the configured default mode if it's available
520518 if default_mode and vim .tbl_contains (available_agents , default_mode ) then
521- state .current_mode = default_mode
519+ state .model . set_mode ( default_mode )
522520 else
523521 -- Fallback to first available agent
524- state .current_mode = available_agents [1 ]
522+ state .model . set_mode ( available_agents [1 ])
525523 end
526524 end
527525 return true
@@ -537,7 +535,7 @@ M.initialize_current_model = Promise.async(function()
537535 local cfg = require (' opencode.config_file' ).get_opencode_config ():await ()
538536
539537 if cfg and cfg .model and cfg .model ~= ' ' then
540- state .current_model = cfg .model
538+ state .model . set_model ( cfg .model )
541539 end
542540
543541 return state .current_model
@@ -583,11 +581,11 @@ M.handle_directory_change = Promise.async(function()
583581 log .debug (' Working directory change %s' , vim .inspect ({ cwd = cwd }))
584582 vim .notify (' Loading last session for new working dir [' .. cwd .. ' ]' , vim .log .levels .INFO )
585583
586- state .active_session = nil
584+ state .session . clear_active ()
587585 state .last_sent_context = nil
588586 context .unload_attachments ()
589587
590- state .active_session = session .get_last_workspace_session ():await () or M .create_new_session ():await ()
588+ state .session .set_active ( session . get_last_workspace_session ():await () or M .create_new_session ():await () )
591589
592590 log .debug (' Loaded session for new working dir ' .. vim .inspect ({ session = state .active_session }))
593591end )
@@ -598,7 +596,7 @@ function M.setup()
598596 state .subscribe (' pending_permissions' , M ._on_current_permission_change )
599597 state .subscribe (' current_model' , function (key , new_val , old_val )
600598 if new_val ~= old_val then
601- state .current_variant = nil
599+ state .model . clear_variant ()
602600
603601 -- Load saved variant for the new model
604602 if new_val then
@@ -607,7 +605,7 @@ function M.setup()
607605 local model_state = require (' opencode.model_state' )
608606 local saved_variant = model_state .get_variant (provider , model )
609607 if saved_variant then
610- state .current_variant = saved_variant
608+ state .model . set_variant ( saved_variant )
611609 end
612610 end
613611 end
0 commit comments