diff --git a/lua/opencode/core.lua b/lua/opencode/core.lua index 567952c6..2ce7710b 100644 --- a/lua/opencode/core.lua +++ b/lua/opencode/core.lua @@ -528,15 +528,30 @@ M.ensure_current_mode = Promise.async(function() return true end) ----Initialize current model if it's not already set. ----@return string|nil The current model (or the default model, if configured) +---Initialize current model from messages or config. +---@return string|nil The current model M.initialize_current_model = Promise.async(function() + if state.messages then + for i = #state.messages, 1, -1 do + local msg = state.messages[i] + if msg and msg.info and msg.info.modelID and msg.info.providerID then + local model_str = msg.info.providerID .. '/' .. msg.info.modelID + if state.current_model ~= model_str then + state.model.set_model(model_str) + end + if msg.info.mode and state.current_mode ~= msg.info.mode then + state.model.set_mode(msg.info.mode) + end + return state.current_model + end + end + end + if state.current_model then return state.current_model end local cfg = require('opencode.config_file').get_opencode_config():await() - if cfg and cfg.model and cfg.model ~= '' then state.model.set_model(cfg.model) end diff --git a/lua/opencode/ui/renderer.lua b/lua/opencode/ui/renderer.lua index 98e25c38..a7c52b45 100644 --- a/lua/opencode/ui/renderer.lua +++ b/lua/opencode/ui/renderer.lua @@ -93,24 +93,6 @@ local function fetch_session() return require('opencode.session').get_messages(session) end ----Set the current model/mode from the most recent assistant message -local function set_model_and_mode_from_messages() - if not state.messages then - return - end - for i = #state.messages, 1, -1 do - local message = state.messages[i] - if message and message.info and message.info.modelID and message.info.providerID then - state.model.set_model(message.info.providerID .. '/' .. message.info.modelID) - if message.info.mode then - state.model.set_mode(message.info.mode) - end - return - end - end - require('opencode.core').initialize_current_model() -end - ---Render all messages and parts from session_data into the output buffer ---Called after a full session fetch or when revert state changes ---@param session_data OpencodeMessage[] @@ -122,7 +104,6 @@ function M._render_full_session_data(session_data) end local revert_index = nil - local set_mode_from_messages = not state.current_model flush.begin_bulk_mode() @@ -163,9 +144,7 @@ function M._render_full_session_data(session_data) flush.flush() flush.end_bulk_mode() - if set_mode_from_messages then - set_model_and_mode_from_messages() - end + require('opencode.core').initialize_current_model() M.scroll_to_bottom(true)