Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/opencode/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ function M.initialize()
vim.notify('Failed to create new session', vim.log.levels.ERROR)
return
end
if not state.current_model then
if not core.initialize_current_model() or not state.current_model then
vim.notify('No model selected', vim.log.levels.ERROR)
return
end
Expand Down
23 changes: 22 additions & 1 deletion lua/opencode/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ function M.select_session(parent_id)
end
return
end
-- clear the model so it can be set by the session. If it doesn't get set
-- then core.get_model() will reset it to the default
state.current_model = nil
state.active_session = selected_session
if state.windows then
state.restore_points = {}
Expand Down Expand Up @@ -67,6 +70,8 @@ function M.open(opts)
if opts.new_session then
state.active_session = nil
state.last_sent_context = nil
-- clear current_model here so it can be reset to the default (if one is set)
state.current_model = nil
state.active_session = M.create_new_session()
else
if not state.active_session then
Expand Down Expand Up @@ -107,7 +112,7 @@ function M.send_message(prompt, opts)
opts.context = vim.tbl_deep_extend('force', state.current_context_config or {}, opts.context or {})
state.current_context_config = opts.context
context.load()
opts.model = opts.model or state.current_model
opts.model = opts.model or M.initialize_current_model()
opts.agent = opts.agent or state.current_mode or config.default_mode

local params = {}
Expand Down Expand Up @@ -325,6 +330,22 @@ function M.ensure_current_mode()
return true
end

---Initialize current model if it's not already set.
---@return string|nil The current model (or the default model, if configured)
function M.initialize_current_model()
if state.current_model then
return state.current_model
end

local config_file = require('opencode.config_file').get_opencode_config()

if config_file and config_file.model and config_file.model ~= '' then
state.current_model = config_file.model
end

return state.current_model
end

function M.setup()
state.subscribe('opencode_server', on_opencode_server)

Expand Down
2 changes: 0 additions & 2 deletions lua/opencode/state.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local config = require('opencode.config')

---@class OpencodeWindowState
---@field input_win integer|nil
---@field output_win integer|nil
Expand Down
50 changes: 34 additions & 16 deletions lua/opencode/ui/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ local output_window = require('opencode.ui.output_window')
local Promise = require('opencode.promise')
local RenderState = require('opencode.ui.render_state')

local M = {}

M._subscriptions = {}
M._prev_line_count = 0
M._render_state = RenderState.new()
M._last_part_formatted = {
part_id = nil,
formatted_data = nil --[[@as Output|nil]],
local M = {
_subscriptions = {},
_prev_line_count = 0,
_render_state = RenderState.new(),
_last_part_formatted = {
part_id = nil,
formatted_data = nil --[[@as Output|nil]],
},
}

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

-- local event_manager = state.event_manager

-- if we're loading a session and there's no currently selected model, set it
-- from the messages
local set_mode_from_messages = not state.current_model

for i, msg in ipairs(session_data) do
if state.active_session.revert and state.active_session.revert.messageID == msg.info.id then
revert_index = i
Expand All @@ -153,6 +157,9 @@ function M._render_full_session_data(session_data)
M._write_formatted_data(formatter._format_revert_message(state.messages, revert_index))
end

if set_mode_from_messages then
M._set_model_from_messages()
end
M.scroll_to_bottom()
end

Expand Down Expand Up @@ -182,6 +189,25 @@ function M.on_emit_events_finished()
M.scroll_to_bottom()
end

---Find the most recently used model from the messages
function M._set_model_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.current_model = message.info.providerID .. '/' .. message.info.modelID
return
end
end

-- we didn't find a model from any messages, set it to the default
require('opencode.core').initialize_current_model()
end

---Auto-scroll to bottom if user was already at bottom
---Respects cursor position if user has scrolled up
function M.scroll_to_bottom()
Expand Down Expand Up @@ -919,14 +945,6 @@ function M.get_actions_for_line(line)
return M._render_state:get_actions_at_line(line)
end

---Update stats from all messages in session
---@param messages OpencodeMessage[]
function M._update_stats_from_messages(messages)
for _, msg in ipairs(messages) do
M._update_stats_from_message(msg)
end
end

---Update display stats from a single message
---@param message OpencodeMessage
function M._update_stats_from_message(message)
Expand Down
11 changes: 3 additions & 8 deletions lua/opencode/ui/topbar.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local config = require('opencode.config')
local util = require('opencode.util')

local M = {}
Expand All @@ -10,16 +11,10 @@ local LABELS = {
}

local function format_model_info()
if not state.current_model or state.current_model == '' then
local info = config_file.get_opencode_config()
state.current_model = info and info.model
end

local config = require('opencode.config')
local parts = {}

if config.ui.display_model and state.current_model then
if state.current_model ~= '' then
if config.ui.display_model then
if state.current_model then
table.insert(parts, state.current_model)
end
end
Expand Down