forked from sudo-tee/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.lua
More file actions
113 lines (96 loc) · 2.85 KB
/
agent.lua
File metadata and controls
113 lines (96 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
local config_file = require('opencode.config_file')
---@type OpencodeState
local state = require('opencode.state')
local util = require('opencode.util')
local Promise = require('opencode.promise')
local agent_model = require('opencode.services.agent_model')
local M = {
actions = {},
}
---@param message string
local function invalid_arguments(message)
error({
code = 'invalid_arguments',
message = message,
}, 0)
end
function M.actions.configure_provider()
agent_model.configure_provider()
end
function M.actions.configure_variant()
agent_model.configure_variant()
end
function M.actions.cycle_variant()
agent_model.cycle_variant()
end
function M.actions.agent_plan()
agent_model.switch_to_mode('plan')
end
function M.actions.agent_build()
agent_model.switch_to_mode('build')
end
M.actions.select_agent = Promise.async(function()
local modes = config_file.get_opencode_agents():await()
local picker = require('opencode.ui.picker')
picker.select(modes, {
prompt = 'Select mode:',
}, function(selection)
if not selection then
return
end
agent_model.switch_to_mode(selection)
end)
end)
M.actions.switch_mode = Promise.async(function()
local modes = config_file.get_opencode_agents():await() --[[@as string[] ]]
local current_index = util.index_of(modes, state.store.get('current_mode'))
if current_index == -1 then
current_index = 0
end
local next_index = (current_index % #modes) + 1
agent_model.switch_to_mode(modes[next_index])
end)
M.actions.current_model = Promise.async(function()
return agent_model.initialize_current_model()
end)
local agent_subcommands = { 'plan', 'build', 'select' }
---@type table<string, fun(): any>
local agent_subcommand_calls = {
plan = M.actions.agent_plan,
build = M.actions.agent_build,
select = M.actions.select_agent,
}
M.command_defs = {
agent = {
desc = 'Manage agents (plan/build/select)',
completions = agent_subcommands,
nested_subcommand = { allow_empty = false },
execute = function(args)
local action = agent_subcommand_calls[args[1]]
if not action then
invalid_arguments('Invalid agent subcommand. Use: ' .. table.concat(agent_subcommands, ', '))
end
return action()
end,
},
models = {
desc = 'Switch provider/model',
execute = M.actions.configure_provider,
},
-- action name aliases for keymap compatibility
configure_provider = { desc = 'Configure provider', execute = M.actions.configure_provider },
configure_variant = { desc = 'Configure model variant', execute = M.actions.configure_variant },
variant = {
desc = 'Switch model variant',
execute = M.actions.configure_variant,
},
cycle_variant = {
desc = 'Cycle model variant',
execute = M.actions.cycle_variant,
},
switch_mode = {
desc = 'Cycle agent mode',
execute = M.actions.switch_mode,
},
}
return M