Repository navigation
Expand file tree
/
Copy pathjob.lua
More file actions
105 lines (92 loc) · 2.5 KB
/
Copy pathjob.lua
File metadata and controls
105 lines (92 loc) · 2.5 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
local context = require('opencode.context')
local state = require('opencode.state')
local util = require('opencode.util')
local Job = require('plenary.job')
local M = {}
---@param opts? {no_context: boolean, model?: string, agent?: string} Optional settings for execution
function M.build_args(prompt, opts)
opts = opts or {}
if not prompt then
return nil
end
local message = opts.no_context == true and prompt or context.format_message(prompt)
local args = { 'run', message }
if state.active_session then
table.insert(args, '-s')
table.insert(args, state.active_session.name)
end
if opts.agent or state.current_mode then
table.insert(args, '--agent')
table.insert(args, opts.agent or state.current_mode)
end
if opts.model or state.current_model then
table.insert(args, '--model')
table.insert(args, opts.model or state.current_model)
end
table.insert(args, '--print-logs')
return args
end
--- Executes the opencode command with the given prompt and handlers
---@param prompt string The user prompt to send to opencode
---@param handlers table A table containing handler functions
---@param opts? RunOpts Optional settings for execution
function M.execute(prompt, handlers, opts)
if not prompt then
return nil
end
local args = M.build_args(prompt, opts)
state.opencode_run_job = Job:new({
interactive = false,
command = 'opencode',
args = args,
on_start = function()
vim.schedule(function()
handlers.on_start()
end)
end,
on_stdout = function(_, out)
if out then
vim.schedule(function()
handlers.on_output(out)
end)
end
end,
on_stderr = function(err, data)
vim.schedule(function()
---@see https://github.com/sst/opencode/issues/369
if err then
handlers.on_error(data)
else
handlers.on_output(data)
end
end)
end,
on_exit = function(data, code)
if code == nil then
vim.schedule(function()
handlers.on_interrupt()
end)
return
end
if code ~= 0 then
vim.schedule(function()
handlers.on_error(util.strip_ansi(table.concat(data._stderr_results, '\n')))
end)
return
end
vim.schedule(function()
handlers.on_exit(code)
end)
end,
})
state.opencode_run_job:start()
end
function M.stop(job)
if job then
pcall(function()
vim.uv.process_kill(job.handle)
job:shutdown()
end)
end
end
return M