-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathcore.lua
More file actions
369 lines (309 loc) · 11 KB
/
core.lua
File metadata and controls
369 lines (309 loc) · 11 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
-- This file was written by an automated tool.
local state = require('opencode.state')
local context = require('opencode.context')
local session = require('opencode.session')
local ui = require('opencode.ui.ui')
local server_job = require('opencode.server_job')
local input_window = require('opencode.ui.input_window')
local util = require('opencode.util')
local config = require('opencode.config')
local M = {}
M._abort_count = 0
---@param parent_id string?
function M.select_session(parent_id)
local all_sessions = session.get_all_workspace_sessions() or {}
local filtered_sessions = vim.tbl_filter(function(s)
return s.description ~= '' and s ~= nil and s.parentID == parent_id
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
M.switch_session(selected_session.id)
end)
end
function M.switch_session(session_id)
local selected_session = session.get_by_id(session_id)
-- clear the model so it can be set by the session. If it doesn't get set
-- then core.get_model() will reset it to the default
state.current_model = nil
state.active_session = selected_session
if state.windows then
state.restore_points = {}
ui.focus_input()
else
M.open()
end
end
---@param opts? OpenOpts
function M.open(opts)
opts = opts or { focus = 'input', new_session = false }
if not state.opencode_server or not state.opencode_server:is_running() then
state.opencode_server = server_job.ensure_server() --[[@as OpencodeServer]]
end
M.ensure_current_mode()
local are_windows_closed = state.windows == nil
if not require('opencode.ui.ui').is_opencode_focused() then
require('opencode.context').load()
end
if are_windows_closed then
-- Check if whether prompting will be allowed
local mentioned_files = context.context.mentioned_files or {}
local allowed, err_msg = util.check_prompt_allowed(config.prompt_guard, mentioned_files)
if not allowed then
vim.notify(err_msg or 'Prompts will be denied by prompt_guard', vim.log.levels.WARN)
end
state.windows = ui.create_windows()
end
if opts.new_session then
state.active_session = nil
state.last_sent_context = nil
-- clear current_model here so it can be reset to the default (if one is set)
state.current_model = nil
state.active_session = M.create_new_session()
else
if not state.active_session then
state.active_session = session.get_last_workspace_session()
if not state.active_session then
state.active_session = M.create_new_session()
end
else
if not state.display_route and are_windows_closed then
-- We're not displaying /help or something like that but we have an active session
-- and the windows were closed so we need to do a full refresh. This mostly happens
-- when opening the window after having closed it since we're not currently clearing
-- the session on api.close()
ui.render_output()
end
end
end
if opts.focus == 'input' then
ui.focus_input({ restore_position = are_windows_closed, start_insert = opts.start_insert == true })
elseif opts.focus == 'output' then
ui.focus_output({ restore_position = are_windows_closed })
end
state.is_opencode_focused = true
end
--- Sends a message to the active session, creating one if necessary.
--- @param prompt string The message prompt to send.
--- @param opts? SendMessageOpts
function M.send_message(prompt, opts)
if not state.active_session or not state.active_session.id then
return false
end
local mentioned_files = context.context.mentioned_files or {}
local allowed, err_msg = util.check_prompt_allowed(config.prompt_guard, mentioned_files)
if not allowed then
vim.notify(err_msg or 'Prompt denied by prompt_guard', vim.log.levels.ERROR)
return
end
opts = opts or {}
opts.context = vim.tbl_deep_extend('force', state.current_context_config or {}, opts.context or {})
state.current_context_config = opts.context
context.load()
opts.model = opts.model or M.initialize_current_model()
opts.agent = opts.agent or state.current_mode or config.default_mode
local params = {}
if opts.model then
local provider, model = opts.model:match('^(.-)/(.+)$')
params.model = { providerID = provider, modelID = model }
state.current_model = opts.model
end
if opts.agent then
params.agent = opts.agent
state.current_mode = opts.agent
end
params.parts = context.format_message(prompt, opts.context)
M.before_run(opts)
state.api_client
:create_message(state.active_session.id, params)
:and_then(function(response)
if not response or not response.info or not response.parts then
-- fall back to full render. incremental render is handled
-- event manager
ui.render_output()
end
M.after_run(prompt)
end)
:catch(function(err)
vim.notify('Error sending message to session: ' .. vim.inspect(err), vim.log.levels.ERROR)
M.cancel()
end)
end
---@param title? string
---@return Session?
function M.create_new_session(title)
local session_response = state.api_client
:create_session(title and { title = title } or false)
:catch(function(err)
vim.notify('Error creating new session: ' .. vim.inspect(err), vim.log.levels.ERROR)
end)
:wait()
if session_response and session_response.id then
local new_session = session.get_by_id(session_response.id)
return new_session
end
end
---@param prompt string
function M.after_run(prompt)
context.unload_attachments()
state.last_sent_context = vim.deepcopy(context.context)
require('opencode.history').write(prompt)
M._abort_count = 0
end
---@param opts? SendMessageOpts
function M.before_run(opts)
local is_new_session = opts and opts.new_session or not state.active_session
opts = opts or {}
M.open({
new_session = is_new_session,
})
end
function M.configure_provider()
require('opencode.provider').select(function(selection)
if not selection then
if state.windows then
ui.focus_input()
end
return
end
local model_str = string.format('%s/%s', selection.provider, selection.model)
state.current_model = model_str
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.cancel()
if state.windows and state.active_session then
if state.is_running() then
M._abort_count = M._abort_count + 1
-- if there's a current permission, reject it
if state.current_permission then
require('opencode.api').permission_deny()
end
local ok, result = pcall(function()
return state.api_client:abort_session(state.active_session.id):wait()
end)
if not ok then
vim.notify('Abort error: ' .. vim.inspect(result))
end
if M._abort_count >= 3 then
vim.notify('Re-starting Opencode server')
M._abort_count = 0
-- close existing server
if state.opencode_server then
state.opencode_server:shutdown():wait()
end
-- start a new one
state.opencode_server = nil
-- NOTE: start a new server here to make sure we're subscribed
-- to server events before a user sends a message
state.opencode_server = server_job.ensure_server() --[[@as OpencodeServer]]
end
end
require('opencode.ui.footer').clear()
input_window.set_content('')
require('opencode.history').index = nil
ui.focus_input()
end
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 util.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
local function on_opencode_server()
state.current_permission = nil
end
--- Switches the current mode to the specified agent.
--- @param mode string|nil The agent/mode to switch to
--- @return boolean success Returns true if the mode was switched successfully, false otherwise
function M.switch_to_mode(mode)
if not mode or mode == '' then
vim.notify('Mode cannot be empty', vim.log.levels.ERROR)
return false
end
local config_file = require('opencode.config_file')
local available_agents = config_file.get_opencode_agents()
if not vim.tbl_contains(available_agents, mode) then
vim.notify(
string.format('Invalid mode "%s". Available modes: %s', mode, table.concat(available_agents, ', ')),
vim.log.levels.ERROR
)
return false
end
state.current_mode = mode
return true
end
--- Ensure the current_mode is set using the config.default_mode or falling back to the first available agent.
--- @return boolean success Returns true if current_mode is set
function M.ensure_current_mode()
if state.current_mode == nil then
local config_file = require('opencode.config_file')
local available_agents = config_file.get_opencode_agents()
if not available_agents or #available_agents == 0 then
vim.notify('No available agents found', vim.log.levels.ERROR)
return false
end
local default_mode = config.default_mode
-- Try to use the configured default mode if it's available
if default_mode and vim.tbl_contains(available_agents, default_mode) then
state.current_mode = default_mode
else
-- Fallback to first available agent
state.current_mode = available_agents[1]
end
end
return true
end
---Initialize current model if it's not already set.
---@return string|nil The current model (or the default model, if configured)
function M.initialize_current_model()
if state.current_model then
return state.current_model
end
local config_file = require('opencode.config_file').get_opencode_config()
if config_file and config_file.model and config_file.model ~= '' then
state.current_model = config_file.model
end
return state.current_model
end
function M.setup()
state.subscribe('opencode_server', on_opencode_server)
vim.schedule(function()
M.opencode_ok()
end)
local OpencodeApiClient = require('opencode.api_client')
state.api_client = OpencodeApiClient.create()
end
return M