@@ -5,14 +5,14 @@ local output_window = require('opencode.ui.output_window')
55local Promise = require (' opencode.promise' )
66local 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
1818local 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 ()
157164end
158165
@@ -182,6 +189,25 @@ function M.on_emit_events_finished()
182189 M .scroll_to_bottom ()
183190end
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
187213function 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 )
920946end
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
932950function M ._update_stats_from_message (message )
0 commit comments