Skip to content

Commit 3cc6e77

Browse files
committed
fix(user_commands): respect active model for user_commands
User commands will now use either the configured model for the command or the active_model. This should fix #129
1 parent b72d46b commit 3cc6e77

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

lua/opencode/api.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,15 @@ end
566566
--- @param args? string[] Additional arguments to pass to the command.
567567
function M.run_user_command(name, args)
568568
M.open_input()
569+
local user_commands = config_file.get_user_commands()
570+
local command_cfg = user_commands and user_commands[name]
571+
if not command_cfg then
572+
vim.notify('Unknown user command: ' .. name, vim.log.levels.WARN)
573+
return
574+
end
575+
576+
local model = command_cfg.model or state.current_model
577+
local agent = command_cfg.agent or state.current_mode
569578

570579
if not state.active_session then
571580
vim.notify('No active session', vim.log.levels.WARN)
@@ -575,6 +584,8 @@ function M.run_user_command(name, args)
575584
:send_command(state.active_session.id, {
576585
command = name,
577586
arguments = table.concat(args or {}, ' '),
587+
model = model,
588+
agent = agent,
578589
})
579590
:and_then(function()
580591
vim.schedule(function()

tests/unit/api_spec.lua

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,81 @@ describe('opencode.api', function()
433433
config_file.get_user_commands = original_get_user_commands
434434
end)
435435

436+
describe('user command model/agent selection', function()
437+
it('invokes run with correct model and agent', function()
438+
local config_file = require('opencode.config_file')
439+
local original_get_user_commands = config_file.get_user_commands
440+
441+
config_file.get_user_commands = function()
442+
return {
443+
['test-no-model'] = { description = 'Run tests', template = 'Run tests with $ARGUMENTS' },
444+
['test-with-model'] = {
445+
description = 'Run tests',
446+
template = 'Run tests with $ARGUMENTS',
447+
model = 'openai/gpt-4',
448+
agent = 'tester',
449+
},
450+
}
451+
end
452+
453+
local original_active_session = state.active_session
454+
state.active_session = { id = 'test-session' }
455+
456+
local original_api_client = state.api_client
457+
local send_command_calls = {}
458+
state.api_client = {
459+
send_command = function(self, session_id, command_data)
460+
table.insert(send_command_calls, { session_id = session_id, command_data = command_data })
461+
return {
462+
and_then = function()
463+
return {}
464+
end,
465+
}
466+
end,
467+
}
468+
469+
stub(api, 'open_input')
470+
471+
local slash_commands = api.get_slash_commands()
472+
473+
local test_no_model_cmd = nil
474+
local test_with_model_cmd = nil
475+
476+
for _, cmd in ipairs(slash_commands) do
477+
if cmd.slash_cmd == '/test-no-model' then
478+
test_no_model_cmd = cmd
479+
elseif cmd.slash_cmd == '/test-with-model' then
480+
test_with_model_cmd = cmd
481+
end
482+
end
483+
484+
assert.truthy(test_no_model_cmd, 'Should find /test-no-model command')
485+
assert.truthy(test_with_model_cmd, 'Should find /test-with-model command')
486+
487+
test_no_model_cmd.fn()
488+
assert.equal(1, #send_command_calls)
489+
assert.equal('test-session', send_command_calls[1].session_id)
490+
assert.equal('test-no-model', send_command_calls[1].command_data.command)
491+
assert.equal('', send_command_calls[1].command_data.arguments)
492+
assert.equal(nil, send_command_calls[1].command_data.model)
493+
assert.equal(nil, send_command_calls[1].command_data.agent)
494+
495+
send_command_calls = {}
496+
497+
test_with_model_cmd.fn()
498+
assert.equal(1, #send_command_calls)
499+
assert.equal('test-session', send_command_calls[1].session_id)
500+
assert.equal('test-with-model', send_command_calls[1].command_data.command)
501+
assert.equal('', send_command_calls[1].command_data.arguments)
502+
assert.equal('openai/gpt-4', send_command_calls[1].command_data.model)
503+
assert.equal('tester', send_command_calls[1].command_data.agent)
504+
505+
config_file.get_user_commands = original_get_user_commands
506+
state.active_session = original_active_session
507+
state.api_client = original_api_client
508+
end)
509+
end)
510+
436511
it('uses default description when none provided', function()
437512
local config_file = require('opencode.config_file')
438513
local original_get_user_commands = config_file.get_user_commands

0 commit comments

Comments
 (0)