diff --git a/lua/opencode/api.lua b/lua/opencode/api.lua index b22cb587..74c2ab87 100644 --- a/lua/opencode/api.lua +++ b/lua/opencode/api.lua @@ -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. diff --git a/tests/unit/api_spec.lua b/tests/unit/api_spec.lua index 17cfba93..e9ad0f92 100644 --- a/tests/unit/api_spec.lua +++ b/tests/unit/api_spec.lua @@ -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)