Skip to content

Commit 43c5fa7

Browse files
authored
fix: restore model from session messages on load (#346)
1 parent 51922dd commit 43c5fa7

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

lua/opencode/core.lua

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,15 +528,30 @@ M.ensure_current_mode = Promise.async(function()
528528
return true
529529
end)
530530

531-
---Initialize current model if it's not already set.
532-
---@return string|nil The current model (or the default model, if configured)
531+
---Initialize current model from messages or config.
532+
---@return string|nil The current model
533533
M.initialize_current_model = Promise.async(function()
534+
if state.messages then
535+
for i = #state.messages, 1, -1 do
536+
local msg = state.messages[i]
537+
if msg and msg.info and msg.info.modelID and msg.info.providerID then
538+
local model_str = msg.info.providerID .. '/' .. msg.info.modelID
539+
if state.current_model ~= model_str then
540+
state.model.set_model(model_str)
541+
end
542+
if msg.info.mode and state.current_mode ~= msg.info.mode then
543+
state.model.set_mode(msg.info.mode)
544+
end
545+
return state.current_model
546+
end
547+
end
548+
end
549+
534550
if state.current_model then
535551
return state.current_model
536552
end
537553

538554
local cfg = require('opencode.config_file').get_opencode_config():await()
539-
540555
if cfg and cfg.model and cfg.model ~= '' then
541556
state.model.set_model(cfg.model)
542557
end

lua/opencode/ui/renderer.lua

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,6 @@ local function fetch_session()
9393
return require('opencode.session').get_messages(session)
9494
end
9595

96-
---Set the current model/mode from the most recent assistant message
97-
local function set_model_and_mode_from_messages()
98-
if not state.messages then
99-
return
100-
end
101-
for i = #state.messages, 1, -1 do
102-
local message = state.messages[i]
103-
if message and message.info and message.info.modelID and message.info.providerID then
104-
state.model.set_model(message.info.providerID .. '/' .. message.info.modelID)
105-
if message.info.mode then
106-
state.model.set_mode(message.info.mode)
107-
end
108-
return
109-
end
110-
end
111-
require('opencode.core').initialize_current_model()
112-
end
113-
11496
---Render all messages and parts from session_data into the output buffer
11597
---Called after a full session fetch or when revert state changes
11698
---@param session_data OpencodeMessage[]
@@ -122,7 +104,6 @@ function M._render_full_session_data(session_data)
122104
end
123105

124106
local revert_index = nil
125-
local set_mode_from_messages = not state.current_model
126107

127108
flush.begin_bulk_mode()
128109

@@ -163,9 +144,7 @@ function M._render_full_session_data(session_data)
163144
flush.flush()
164145
flush.end_bulk_mode()
165146

166-
if set_mode_from_messages then
167-
set_model_and_mode_from_messages()
168-
end
147+
require('opencode.core').initialize_current_model()
169148

170149
M.scroll_to_bottom(true)
171150

0 commit comments

Comments
 (0)