Repository navigation
Expand file tree
/
Copy pathapi.lua
More file actions
752 lines (643 loc) · 18 KB
/
Copy pathapi.lua
File metadata and controls
752 lines (643 loc) · 18 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
local core = require('opencode.core')
local context = require('opencode.context')
local util = require('opencode.util')
local session = require('opencode.session')
local input_window = require('opencode.ui.input_window')
local ui = require('opencode.ui.ui')
local icons = require('opencode.ui.icons')
local state = require('opencode.state')
local git_review = require('opencode.git_review')
local history = require('opencode.history')
local id = require('opencode.id')
local M = {}
function M.swap_position()
require('opencode.ui.ui').swap_position()
end
function M.open_input()
core.open({ new_session = false, focus = 'input' })
vim.cmd('startinsert')
end
function M.open_input_new_session()
core.open({ new_session = true, focus = 'input' })
vim.cmd('startinsert')
end
function M.open_output()
core.open({ new_session = false, focus = 'output' })
end
function M.close()
if state.display_route then
state.display_route = nil
ui.clear_output()
ui.render_output()
ui.scroll_to_bottom()
return
end
ui.close_windows(state.windows)
end
function M.toggle(new_session)
if state.windows == nil then
local focus = state.last_focused_opencode_window or 'input'
core.open({ new_session = new_session == true, focus = focus })
else
M.close()
end
end
function M.toggle_focus(new_session)
if not ui.is_opencode_focused() then
local focus = state.last_focused_opencode_window or 'input'
core.open({ new_session = new_session == true, focus = focus })
else
ui.return_to_last_code_win()
end
end
function M.configure_provider()
core.configure_provider()
end
function M.stop()
core.stop()
end
---@param prompt string
---@param opts? SendMessageOpts
function M.run(prompt, opts)
opts = vim.tbl_deep_extend('force', { new_session = false, focus = 'output' }, opts or {})
core.send_message(prompt, opts)
end
---@param prompt string
---@param opts? SendMessageOpts
function M.run_new_session(prompt, opts)
opts = vim.tbl_deep_extend('force', { new_session = true, focus = 'output' }, opts or {})
core.send_message(prompt, { new_session = true, focus = 'output' })
end
---@param parent_id? string
function M.select_session(parent_id)
core.select_session(parent_id)
end
function M.select_child_session()
core.select_session(state.active_session and state.active_session.name or nil)
end
function M.toggle_pane()
if not state.windows then
core.open({ new_session = false, focus = 'output' })
return
end
ui.toggle_pane()
end
---@param from_snapshot_id? string
---@param to_snapshot_id? string|number
function M.diff_open(from_snapshot_id, to_snapshot_id)
if not state.messages or not state.active_session then
core.open({ new_session = false, focus = 'output' })
end
git_review.review(from_snapshot_id)
end
function M.diff_next()
if not state.windows then
core.open({ new_session = false, focus = 'output' })
end
git_review.next_diff()
end
function M.diff_prev()
if not state.windows then
core.open({ new_session = false, focus = 'output' })
end
git_review.prev_diff()
end
function M.diff_close()
if not state.windows then
core.open({ new_session = false, focus = 'output' })
end
git_review.close_diff()
end
---@param from_snapshot_id? string
function M.diff_revert_all(from_snapshot_id)
if not state.windows then
core.open({ new_session = false, focus = 'output' })
end
git_review.revert_all(from_snapshot_id)
end
---@param from_snapshot_id? string
---@param to_snapshot_id? string
function M.diff_revert_selected_file(from_snapshot_id, to_snapshot_id)
if not state.windows then
core.open({ new_session = false, focus = 'output' })
end
git_review.revert_selected_file(from_snapshot_id)
end
---@param restore_point_id? string
function M.diff_restore_snapshot_file(restore_point_id)
if not state.windows then
core.open({ new_session = false, focus = 'output' })
end
git_review.restore_snapshot_file(restore_point_id)
end
---@param restore_point_id? string
function M.diff_restore_snapshot_all(restore_point_id)
if not state.windows then
core.open({ new_session = false, focus = 'output' })
end
git_review.restore_snapshot_all(restore_point_id)
end
function M.diff_revert_all_last_prompt()
if not state.windows then
core.open({ new_session = false, focus = 'output' })
end
local snapshots = session.get_message_snapshot_ids(state.current_message)
local snapshot_id = snapshots and snapshots[1]
if not snapshot_id then
vim.notify('No snapshots found for the current message', vim.log.levels.WARN)
return
end
git_review.revert_all(snapshot_id)
end
function M.diff_revert_this(snapshot_id)
if not state.windows then
core.open({ new_session = false, focus = 'output' })
end
git_review.revert_current(snapshot_id)
end
function M.diff_revert_this_last_prompt()
if not state.windows then
core.open({ new_session = false, focus = 'output' })
end
local snapshots = session.get_message_snapshot_ids(state.current_message)
local snapshot_id = snapshots and snapshots[1]
if not snapshot_id then
vim.notify('No snapshots found for the current message', vim.log.levels.WARN)
return
end
end
function M.set_review_breakpoint()
vim.notify('Setting review breakpoint is not implemented yet', vim.log.levels.WARN)
git_review.create_snapshot()
end
function M.prev_history()
local prev_prompt = history.prev()
if prev_prompt then
input_window.set_content(prev_prompt)
end
end
function M.next_history()
local next_prompt = history.next()
if next_prompt then
input_window.set_content(next_prompt)
end
end
function M.initialize()
local new_session = core.create_new_session('AGENTS.md Initialization'):wait()
if not new_session then
vim.notify('Failed to create new session', vim.log.levels.ERROR)
return
end
local providerId, modelId = state.current_model:match('^(.-)/(.+)$')
if not providerId or not modelId then
vim.notify('Invalid model format: ' .. tostring(state.current_model), vim.log.levels.ERROR)
return
end
state.active_session = new_session
M.open_input()
core.run_server_api('/session/' .. state.active_session.name .. '/init', 'POST', {
providerID = providerId,
modelID = modelId,
messageID = id.ascending('message'),
})
end
function M.agent_plan()
state.current_mode = 'plan'
require('opencode.ui.topbar').render()
end
function M.agent_build()
state.current_mode = 'build'
require('opencode.ui.topbar').render()
end
function M.select_agent()
local modes = require('opencode.config_file').get_opencode_agents()
vim.ui.select(modes, {
prompt = 'Select mode:',
}, function(selection)
if not selection then
return
end
state.current_mode = selection
require('opencode.ui.topbar').render()
end)
end
function M.switch_to_next_mode()
local modes = require('opencode.config_file').get_opencode_agents()
local current_index = util.index_of(modes, state.current_mode)
if current_index == -1 then
current_index = 0
end
-- Calculate next index, wrapping around if necessary
local next_index = (current_index % #modes) + 1
state.current_mode = modes[next_index]
require('opencode.ui.topbar').render()
end
function M.with_header(lines, show_welcome)
show_welcome = show_welcome or show_welcome
state.display_route = '/header'
local msg = {
'## Opencode.nvim',
'',
' █▀▀█ █▀▀█ █▀▀ █▀▀▄ █▀▀ █▀▀█ █▀▀▄ █▀▀',
' █░░█ █░░█ █▀▀ █░░█ █░░ █░░█ █░░█ █▀▀',
' ▀▀▀▀ █▀▀▀ ▀▀▀ ▀ ▀ ▀▀▀ ▀▀▀▀ ▀▀▀ ▀▀▀',
'',
}
if show_welcome then
table.insert(
msg,
'Welcome to Opencode.nvim! This plugin allows you to interact with AI models directly from Neovim.'
)
table.insert(msg, '')
end
for _, line in ipairs(lines) do
table.insert(msg, line)
end
return msg
end
function M.help()
state.display_route = '/help'
M.open_input()
local msg = M.with_header({
'### Available Commands',
'',
'| Command | Description |',
'|-----------|---------------------|',
}, false)
local max_desc_length = (vim.api.nvim_win_get_width(state.windows.output_win) / 2) - 5
for _, def in pairs(M.commands) do
local desc = def.desc or ''
if #desc > max_desc_length then
desc = desc:sub(1, max_desc_length - 3) .. '...'
end
table.insert(msg, string.format('| %-10s | %s |', def.name, desc))
end
table.insert(msg, '')
ui.render_lines(msg)
end
function M.mcp()
local info = require('opencode.config_file')
local mcp = info.get_mcp_servers()
if not mcp then
ui.notify('No MCP configuration found. Please check your opencode config file.', 'warn')
return
end
state.display_route = '/mcp'
M.open_input()
local msg = M.with_header({
'### Available MCP servers',
'',
'| Name | Type | cmd |',
'|--------|------|-----|',
})
for name, def in pairs(mcp) do
table.insert(
msg,
string.format(
'| %s %-10s | %s | %s |',
(def.enabled and icons.get('status_on') or icons.get('status_off')),
name,
def.type,
table.concat(def.command, ' ')
)
)
end
table.insert(msg, '')
ui.render_lines(msg)
end
function M.run_user_command(name)
M.open_input()
core.run_server_api('/session/' .. state.active_session.name .. '/command', 'POST', {
command = name,
arguments = '',
})
end
-- Command definitions that call the API functions
M.commands = {
swap_position = {
name = 'OpencodeSwapPosition',
desc = 'Swap Opencode pane left/right',
fn = function()
M.swap_position()
end,
},
toggle = {
name = 'Opencode',
desc = 'Open opencode. Close if opened',
fn = function()
M.toggle()
end,
},
toggle_focus = {
name = 'OpencodeToggleFocus',
desc = 'Toggle focus between opencode and last window',
fn = function()
M.toggle_focus()
end,
},
open_input = {
name = 'OpencodeOpenInput',
desc = 'Opens and focuses on input window on insert mode',
fn = function()
M.open_input()
end,
},
open_input_new_session = {
name = 'OpencodeOpenInputNewSession',
slash_cmd = '/new',
desc = 'Opens and focuses on input window on insert mode. Creates a new session',
fn = function()
M.open_input_new_session()
end,
},
open_output = {
name = 'OpencodeOpenOutput',
desc = 'Opens and focuses on output window',
fn = function()
M.open_output()
end,
},
create_new_session = {
name = 'OpencodeCreateNewSession',
desc = 'Create a new opencode session',
fn = function(opts)
local title = opts.args and opts.args:match('^%s*(.+)')
if title and title ~= '' then
core.create_new_session(title, function(new_session)
state.active_session = new_session
M.open_input()
end)
else
vim.notify('Session title cannot be empty', vim.log.levels.ERROR)
end
end,
args = true,
},
close = {
name = 'OpencodeClose',
desc = 'Close UI windows',
fn = function()
M.close()
end,
},
stop = {
name = 'OpencodeStop',
desc = 'Stop opencode while it is running',
fn = function()
M.stop()
end,
},
select_session = {
name = 'OpencodeSelectSession',
slash_cmd = '/sessions',
desc = 'Select and load a opencode session',
fn = function()
M.select_session()
end,
},
select_child_session = {
name = 'OpencodeSelectChildSession',
slash_cmd = '/child-sessions',
desc = 'Select and load a child session of the current session',
fn = function()
M.select_child_session()
end,
},
toggle_pane = {
name = 'OpencodeTogglePane',
desc = 'Toggle between input and output panes',
fn = function()
M.toggle_pane()
end,
},
configure_provider = {
name = 'OpencodeConfigureProvider',
slash_cmd = '/models',
desc = 'Quick provider and model switch from predefined list',
fn = function()
M.configure_provider()
end,
},
run = {
name = 'OpencodeRun',
desc = 'Run opencode with a prompt (continue last session)',
fn = function(opts)
local prompt, rest = opts.args:match('^(.-)%s+(%S+=%S.*)$')
prompt = vim.trim(prompt or opts.args)
local extra_args = util.parse_dot_args(rest or '')
M.run(prompt, extra_args)
end,
args = true,
},
run_new_session = {
name = 'OpencodeRunNewSession',
desc = 'Run opencode with a prompt (new session)',
fn = function(opts)
local prompt, rest = opts.args:match('^(.-)%s+(%S+=%S.*)$')
prompt = vim.trim(prompt or opts.args)
local extra_args = util.parse_dot_args(rest or '')
M.run_new_session(prompt, extra_args)
end,
args = true,
},
diff_open = {
name = 'OpencodeDiff',
desc = 'Opens a diff tab of a modified file since the last opencode prompt',
fn = function()
M.diff_open()
end,
},
diff_next = {
name = 'OpencodeDiffNext',
desc = 'Navigate to next file diff',
fn = function()
M.diff_next()
end,
},
diff_prev = {
name = 'OpencodeDiffPrev',
desc = 'Navigate to previous file diff',
fn = function()
M.diff_prev()
end,
},
diff_close = {
name = 'OpencodeDiffClose',
desc = 'Close diff view tab and return to normal editing',
fn = function()
M.diff_close()
end,
},
diff_revert_all_last_prompt = {
name = 'OpencodeRevertAllLastPrompt',
desc = 'Revert all file changes since the last opencode prompt',
fn = function()
M.diff_revert_all_last_prompt()
end,
},
diff_revert_this_last_prompt = {
name = 'OpencodeRevertThisLastPrompt',
desc = 'Revert current file changes since the last opencode prompt',
fn = function()
M.diff_revert_this_last_prompt()
end,
},
diff_revert_all_session = {
name = 'OpencodeRevertAllSession',
desc = 'Revert all file changes since the last session',
fn = function()
M.diff_revert_all_session()
end,
},
diff_revert_this_session = {
name = 'OpencodeRevertThisSession',
desc = 'Revert current file changes since the last session',
fn = function()
M.diff_revert_this_session()
end,
},
diff_revert_all_to_snapshot = {
name = 'OpencodeRevertAllToSnapshot',
desc = 'Revert all file changes to a specific snapshot',
fn = function(snapshot)
if not snapshot then
vim.notify('Snapshot ID is required', vim.log.levels.ERROR)
return
end
M.diff_revert_all(snapshot)
end,
args = true,
},
diff_revert_this_to_snapshot = {
name = 'OpencodeRevertThisToSnapshot',
desc = 'Revert all file changes to a specific snapshot',
fn = function(snapshot)
if not snapshot then
vim.notify('Snapshot ID is required', vim.log.levels.ERROR)
return
end
M.diff_revert_this(snapshot)
end,
args = true,
},
diff_restore_snapshot_file = {
name = 'OpencodeRestoreSnapshotFile',
desc = 'Restore a file to a specific restore point',
fn = function(snapshot)
if not snapshot then
vim.notify('Snapshot ID is required', vim.log.levels.ERROR)
return
end
M.diff_restore_snapshot_file(snapshot)
end,
args = true,
},
diff_restore_snapshot_all = {
name = 'OpencodeRestoreSnapshotAll',
desc = 'Restore all files to a specific restore point',
fn = function(snapshot)
if not snapshot then
vim.notify('Snapshot ID is required', vim.log.levels.ERROR)
return
end
M.diff_restore_snapshot_all(snapshot)
end,
args = true,
},
set_review_breakpoint = {
name = 'OpencodeSetReviewBreakpoint',
desc = 'Set a review breakpoint to track changes',
fn = function()
M.set_review_breakpoint()
end,
},
init = {
name = 'OpencodeInit',
slash_cmd = '/init',
desc = 'Initialize/Update AGENTS.md file',
fn = function()
M.initialize()
end,
},
help = {
name = 'OpencodeHelp',
slash_cmd = '/help',
desc = 'Display help message',
fn = function()
M.help()
end,
},
mcp = {
name = 'OpencodeMCP',
slash_cmd = '/mcp',
desc = 'Display list of mcp servers',
fn = function()
M.mcp()
end,
},
opencode_mode_plan = {
name = 'OpencodeAgentPlan',
desc = 'Set opencode agent to `plan`. (Tool calling disabled. No editor context besides selections)',
fn = function()
M.agent_plan()
end,
},
opencode_mode_build = {
name = 'OpencodeAgentBuild',
desc = 'Set opencode agent to `build`. (Default with full agent capabilities)',
fn = function()
M.agent_build()
end,
},
open_code_select_mode = {
name = 'OpencodeAgentSelect',
slash_cmd = '/agent',
desc = 'Select opencode agent',
fn = function()
M.select_agent()
end,
},
run_user_command = {
name = 'OpencodeRunUserCommand',
desc = 'Run a user-defined Opencode command by name',
fn = function(opts)
local name = opts.args and opts.args:match('^%s*(%S+)')
if not name or name == '' then
vim.notify('User command name required. Usage: :OpencodeRunUserCommand <name>', vim.log.levels.ERROR)
return
end
M.run_user_command(name)
end,
args = true,
},
}
function M.get_slash_commands()
local commands = vim.tbl_filter(function(cmd)
return cmd.slash_cmd and cmd.slash_cmd ~= ''
end, M.commands)
local user_commands = require('opencode.config_file').get_user_commands()
if user_commands then
for name, _ in pairs(user_commands) do
table.insert(commands, {
slash_cmd = '/' .. name,
desc = 'Run user command: ' .. name,
fn = function()
M.commands.run_user_command.fn({ args = name })
end,
})
end
end
table.sort(commands, function(a, b)
return a.slash_cmd < b.slash_cmd
end)
return commands
end
function M.setup()
for _, cmd in pairs(M.commands) do
vim.api.nvim_create_user_command(cmd.name, cmd.fn, {
desc = cmd.desc,
nargs = cmd.args and '+' or 0,
})
end
end
return M