Skip to content

Commit e60f56d

Browse files
committed
refactor: carefully set model from session
When loading a session, clear state.current_model and set it to the model used by the most recent message. Topbar shouldn't set a model as a side effect, it should just display what's been selected. Add `core.initialize_current_model()` which will initialize state.current_model to the default from the config_file if it hasn't been set
1 parent c3ad995 commit e60f56d

File tree

5 files changed

+60
-28
lines changed

5 files changed

+60
-28
lines changed

lua/opencode/api.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ function M.initialize()
355355
vim.notify('Failed to create new session', vim.log.levels.ERROR)
356356
return
357357
end
358-
if not state.current_model then
358+
if not core.initialize_current_model() or not state.current_model then
359359
vim.notify('No model selected', vim.log.levels.ERROR)
360360
return
361361
end

lua/opencode/core.lua

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ function M.select_session(parent_id)
2525
end
2626
return
2727
end
28+
-- clear the model so it can be set by the session. If it doesn't get set
29+
-- then core.get_model() will reset it to the default
30+
state.current_model = nil
2831
state.active_session = selected_session
2932
if state.windows then
3033
state.restore_points = {}
@@ -67,6 +70,8 @@ function M.open(opts)
6770
if opts.new_session then
6871
state.active_session = nil
6972
state.last_sent_context = nil
73+
-- clear current_model here so it can be reset to the default (if one is set)
74+
state.current_model = nil
7075
state.active_session = M.create_new_session()
7176
else
7277
if not state.active_session then
@@ -107,7 +112,7 @@ function M.send_message(prompt, opts)
107112
opts.context = vim.tbl_deep_extend('force', state.current_context_config or {}, opts.context or {})
108113
state.current_context_config = opts.context
109114
context.load()
110-
opts.model = opts.model or state.current_model
115+
opts.model = opts.model or M.initialize_current_model()
111116
opts.agent = opts.agent or state.current_mode or config.default_mode
112117

113118
local params = {}
@@ -325,6 +330,22 @@ function M.ensure_current_mode()
325330
return true
326331
end
327332

333+
---Initialize current model if it's not already set.
334+
---@return string|nil The current model (or the default model, if configured)
335+
function M.initialize_current_model()
336+
if state.current_model then
337+
return state.current_model
338+
end
339+
340+
local config_file = require('opencode.config_file').get_opencode_config()
341+
342+
if config_file and config_file.model and config_file.model ~= '' then
343+
state.current_model = config_file.model
344+
end
345+
346+
return state.current_model
347+
end
348+
328349
function M.setup()
329350
state.subscribe('opencode_server', on_opencode_server)
330351

lua/opencode/state.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local config = require('opencode.config')
2-
31
---@class OpencodeWindowState
42
---@field input_win integer|nil
53
---@field output_win integer|nil

lua/opencode/ui/renderer.lua

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ local output_window = require('opencode.ui.output_window')
55
local Promise = require('opencode.promise')
66
local RenderState = require('opencode.ui.render_state')
77

8-
local M = {}
9-
10-
M._subscriptions = {}
11-
M._prev_line_count = 0
12-
M._render_state = RenderState.new()
13-
M._last_part_formatted = {
14-
part_id = nil,
15-
formatted_data = nil --[[@as Output|nil]],
8+
local M = {
9+
_subscriptions = {},
10+
_prev_line_count = 0,
11+
_render_state = RenderState.new(),
12+
_last_part_formatted = {
13+
part_id = nil,
14+
formatted_data = nil --[[@as Output|nil]],
15+
},
1616
}
1717

1818
local trigger_on_data_rendered = require('opencode.util').debounce(function()
@@ -135,6 +135,10 @@ function M._render_full_session_data(session_data)
135135

136136
-- local event_manager = state.event_manager
137137

138+
-- if we're loading a session and there's no currently selected model, set it
139+
-- from the messages
140+
local set_mode_from_messages = not state.current_model
141+
138142
for i, msg in ipairs(session_data) do
139143
if state.active_session.revert and state.active_session.revert.messageID == msg.info.id then
140144
revert_index = i
@@ -153,6 +157,9 @@ function M._render_full_session_data(session_data)
153157
M._write_formatted_data(formatter._format_revert_message(state.messages, revert_index))
154158
end
155159

160+
if set_mode_from_messages then
161+
M._set_model_from_messages()
162+
end
156163
M.scroll_to_bottom()
157164
end
158165

@@ -182,6 +189,25 @@ function M.on_emit_events_finished()
182189
M.scroll_to_bottom()
183190
end
184191

192+
---Find the most recently used model from the messages
193+
function M._set_model_from_messages()
194+
if not state.messages then
195+
return
196+
end
197+
198+
for i = #state.messages, 1, -1 do
199+
local message = state.messages[i]
200+
201+
if message and message.info and message.info.modelID and message.info.providerID then
202+
state.current_model = message.info.providerID .. '/' .. message.info.modelID
203+
return
204+
end
205+
end
206+
207+
-- we didn't find a model from any messages, set it to the default
208+
require('opencode.core').initialize_current_model()
209+
end
210+
185211
---Auto-scroll to bottom if user was already at bottom
186212
---Respects cursor position if user has scrolled up
187213
function M.scroll_to_bottom()
@@ -919,14 +945,6 @@ function M.get_actions_for_line(line)
919945
return M._render_state:get_actions_at_line(line)
920946
end
921947

922-
---Update stats from all messages in session
923-
---@param messages OpencodeMessage[]
924-
function M._update_stats_from_messages(messages)
925-
for _, msg in ipairs(messages) do
926-
M._update_stats_from_message(msg)
927-
end
928-
end
929-
930948
---Update display stats from a single message
931949
---@param message OpencodeMessage
932950
function M._update_stats_from_message(message)

lua/opencode/ui/topbar.lua

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local config = require('opencode.config')
12
local util = require('opencode.util')
23

34
local M = {}
@@ -10,16 +11,10 @@ local LABELS = {
1011
}
1112

1213
local function format_model_info()
13-
if not state.current_model or state.current_model == '' then
14-
local info = config_file.get_opencode_config()
15-
state.current_model = info and info.model
16-
end
17-
18-
local config = require('opencode.config')
1914
local parts = {}
2015

21-
if config.ui.display_model and state.current_model then
22-
if state.current_model ~= '' then
16+
if config.ui.display_model then
17+
if state.current_model then
2318
table.insert(parts, state.current_model)
2419
end
2520
end

0 commit comments

Comments
 (0)