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
10 changes: 10 additions & 0 deletions lua/opencode/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,16 @@ function M.commands_list()
ui.render_lines(msg)
end

function M.current_model()
if state.current_model and state.current_model ~= "" then
return state.current_model
else
local conf = require('opencode.config_file').get_opencode_config()
local model = conf and conf.model
return model or ''
end
end

--- Runs a user-defined command by name.
--- @param name string The name of the user command to run.
--- @param args? string[] Additional arguments to pass to the command.
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,35 @@ describe('opencode.api', function()
config_file.get_user_commands = original_get_user_commands
end)
end)

describe('current_model', function()
it('returns the current model from state', function()
local original_model = state.current_model
state.current_model = 'testmodel'

local model = api.current_model()
assert.equal('testmodel', model)

state.current_model = original_model
end)

it('falls back to config file model when state.current_model is nil', function()
local original_model = state.current_model
state.current_model = nil

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

config_file.get_opencode_config = function()
return { model = 'testmodel' }
end

local model = api.current_model()

assert.equal('testmodel', model)

config_file.get_opencode_config = original_get_opencode_config
state.current_model = original_model
end)
end)
end)