From b8427ae53b17e31b9bdb031dc00d06f9233546be Mon Sep 17 00:00:00 2001 From: Guillaume BOEHM Date: Thu, 30 Oct 2025 21:45:03 +0100 Subject: [PATCH 1/3] feat(api): Add current_model function to the API --- lua/opencode/api.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lua/opencode/api.lua b/lua/opencode/api.lua index b22cb587..e3cae834 100644 --- a/lua/opencode/api.lua +++ b/lua/opencode/api.lua @@ -544,6 +544,10 @@ function M.commands_list() ui.render_lines(msg) end +function M.current_model() + return state.current_model or '' +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. From 4a9dffdbd4baff734a90fbfd23a88e088b0e7c70 Mon Sep 17 00:00:00 2001 From: Guillaume BOEHM Date: Tue, 4 Nov 2025 08:03:42 +0100 Subject: [PATCH 2/3] fix(api): Fetch default model in config if the state is not set yet --- lua/opencode/api.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lua/opencode/api.lua b/lua/opencode/api.lua index e3cae834..74c2ab87 100644 --- a/lua/opencode/api.lua +++ b/lua/opencode/api.lua @@ -545,7 +545,13 @@ function M.commands_list() end function M.current_model() - return state.current_model or '' + 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. From 0ad1de0e9a1470c23180964f7001681c02bd6e2c Mon Sep 17 00:00:00 2001 From: Guillaume BOEHM Date: Tue, 4 Nov 2025 19:29:40 +0100 Subject: [PATCH 3/3] test(api): Add current_model api tests --- tests/unit/api_spec.lua | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) 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)