Repository navigation
Expand file tree
/
Copy pathcore.lua
More file actions
283 lines (247 loc) · 7.21 KB
/
Copy pathcore.lua
File metadata and controls
283 lines (247 loc) · 7.21 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
local M = {}
local state = require('opencode.state')
local context = require('opencode.context')
local session = require('opencode.session')
local ui = require('opencode.ui.ui')
local job = require('opencode.job')
local input_window = require('opencode.ui.input_window')
function M.select_session()
local all_sessions = session.get_all_workspace_sessions() or {}
local filtered_sessions = vim.tbl_filter(function(s)
return s.description ~= '' and s ~= nil
end, all_sessions)
ui.select_session(filtered_sessions, function(selected_session)
if not selected_session then
if state.windows then
ui.focus_input()
end
return
end
state.active_session = selected_session
if state.windows then
ui.render_output()
ui.scroll_to_bottom()
ui.focus_input()
else
M.open()
end
end)
end
function M.open(opts)
opts = opts or { focus = 'input', new_session = false }
if not M.opencode_ok() then
return
end
local are_windows_closed = state.windows == nil
if are_windows_closed then
state.windows = ui.create_windows()
end
if opts.new_session then
state.active_session = nil
state.last_sent_context = nil
ui.clear_output()
else
if not state.active_session then
state.active_session = session.get_last_workspace_session()
end
if are_windows_closed or ui.is_output_empty() then
ui.render_output()
ui.scroll_to_bottom()
end
end
if opts.focus == 'input' then
ui.focus_input({ restore_position = are_windows_closed })
elseif opts.focus == 'output' then
ui.focus_output({ restore_position = are_windows_closed })
end
end
function M.run(prompt, opts)
if not M.opencode_ok() then
return false
end
M.before_run(opts)
-- Add small delay to ensure stop is complete
vim.defer_fn(function()
job.execute(prompt, {
on_start = function()
state.was_interrupted = false
M.after_run(prompt)
end,
on_output = function(output)
vim.cmd('checktime')
if output and not state.active_session then
local found = string.match(output, 'sessionID=(ses_%w+)')
if found then
state.active_session = session.get_by_name(found)
state.new_session_name = found
end
end
state.last_output = os.time()
ui.render_output()
end,
on_error = function(err)
vim.notify(err, vim.log.levels.ERROR)
ui.close_windows(state.windows)
end,
on_exit = function()
state.opencode_run_job = nil
state.last_output = os.time()
ui.render_output()
end,
on_interrupt = function()
state.opencode_run_job = nil
state.was_interrupted = true
state.last_output = os.time()
ui.render_output()
vim.notify('Opencode run interrupted by user', vim.log.levels.WARN)
end,
})
end, 10)
end
function M.after_run(prompt)
context.unload_attachments()
state.last_sent_context = vim.deepcopy(context.context)
require('opencode.history').write(prompt)
if state.windows then
ui.render_output()
end
end
function M.before_run(opts)
local is_new_session = opts and opts.new_session or not state.active_session
M.stop()
if is_new_session then
ui.clear_output()
end
opts = opts or {}
M.open({
new_session = is_new_session,
})
end
function M.add_file_to_context()
local picker = require('opencode.ui.file_picker')
require('opencode.ui.mention').mention(function(mention_cb)
picker.pick(function(file)
mention_cb(file.name)
context.add_file(file.path)
end)
end)
end
function M.select_slash_commands()
local custom_command_key = require('opencode.config').get('keymap').window.slash_commands
if not input_window.is_empty() then
if #custom_command_key == 1 then
vim.api.nvim_feedkeys(custom_command_key, 'n', false)
end
return
end
local api = require('opencode.api')
local commands = api.get_slash_commands() or {}
local cmd_len = 0
for _, cmd in ipairs(commands) do
cmd_len = math.max(cmd_len, #cmd.slash_cmd)
end
vim.ui.select(commands, {
prompt = 'Select command:',
format_item = function(item)
return string.format('%-' .. cmd_len .. 's │ %s', item.slash_cmd, item.desc)
end,
}, function(selection)
if selection and selection.fn then
selection.fn()
else
if #custom_command_key == 1 then
vim.cmd('startinsert')
vim.api.nvim_feedkeys(custom_command_key, 'n', false)
end
end
end)
end
function M.configure_provider()
local cfg = require('opencode.config_file')
require('opencode.provider').select(function(selection)
if not selection then
if state.windows then
ui.focus_input()
end
return
end
cfg.set_model(selection.provider, selection.model)
if state.windows then
require('opencode.ui.topbar').render()
ui.focus_input()
else
vim.notify('Changed provider to ' .. selection.display, vim.log.levels.INFO)
end
end)
end
function M.stop()
if state.opencode_run_job then
job.stop(state.opencode_run_job)
end
state.opencode_run_job = nil
if state.windows then
ui.stop_render_output()
ui.render_output()
input_window.set_content('')
require('opencode.history').index = nil
ui.focus_input()
end
end
---@param version string
---@return number|nil, number|nil, number|nil
local function parse_semver(version)
if not version or version == '' then
return nil
end
local major, minor, patch = version:match('(%d+)%.(%d+)%.?(%d*)')
if not major then
return nil
end
return tonumber(major) or 0, tonumber(minor) or 0, tonumber(patch) or 0
end
---@param version string
---@param required_version string
---@return boolean
local function is_version_greater_or_equal(version, required_version)
local major, minor, patch = parse_semver(version)
local req_major, req_minor, req_patch = parse_semver(required_version)
if not major or not req_major then
return false
end
if major ~= req_major then
return major > req_major
end
if minor ~= req_minor then
return minor > req_minor
end
return patch >= req_patch
end
function M.opencode_ok()
if vim.fn.executable('opencode') == 0 then
vim.notify(
'opencode command not found - please install and configure opencode before using this plugin',
vim.log.levels.ERROR
)
return false
end
if not state.opencode_cli_version or state.opencode_cli_version == '' then
local result = vim.system({ 'opencode', '--version' }):wait()
local out = (result and result.stdout or ''):gsub('%s+$', '')
state.opencode_cli_version = out:match('(%d+%%.%d+%%.%d+)') or out
end
local required = state.required_version
local current_version = state.opencode_cli_version
if not current_version or current_version == '' then
vim.notify(string.format('Unable to detect opencode CLI version. Requires >= %s', required), vim.log.levels.ERROR)
return false
end
if not is_version_greater_or_equal(current_version, required) then
vim.notify(
string.format('Unsupported opencode CLI version: %s. Requires >= %s', current_version, required),
vim.log.levels.ERROR
)
return false
end
return true
end
return M