-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathapi.lua
More file actions
1397 lines (1219 loc) · 38.6 KB
/
api.lua
File metadata and controls
1397 lines (1219 loc) · 38.6 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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local core = require('opencode.core')
local util = require('opencode.util')
local session = require('opencode.session')
local config_file = require('opencode.config_file')
local state = require('opencode.state')
local input_window = require('opencode.ui.input_window')
local ui = require('opencode.ui.ui')
local icons = require('opencode.ui.icons')
local git_review = require('opencode.git_review')
local history = require('opencode.history')
local config = require('opencode.config')
local M = {}
function M.swap_position()
require('opencode.ui.ui').swap_position()
end
function M.toggle_zoom()
require('opencode.ui.ui').toggle_zoom()
end
function M.open_input()
core.open({ new_session = false, focus = 'input', start_insert = true })
end
function M.open_input_new_session()
core.open({ new_session = true, focus = 'input', start_insert = true })
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()
-- need to trigger a re-render here to re-display the session
ui.render_output()
return
end
ui.close_windows(state.windows)
end
function M.paste_image()
core.paste_image_from_clipboard()
end
function M.toggle(new_session)
if state.windows == nil then
local focus = state.last_focused_opencode_window or 'input' ---@cast focus 'input' | 'output'
core.open({ new_session = new_session == true, focus = focus, start_insert = false })
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' ---@cast focus 'input' | 'output'
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.cancel()
core.cancel()
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, opts)
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.id 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()
if not state.windows then
return
end
local prev_prompt = history.prev()
if prev_prompt then
input_window.set_content(prev_prompt)
require('opencode.ui.mention').restore_mentions(state.windows.input_buf)
end
end
function M.next_history()
if not state.windows then
return
end
local next_prompt = history.next()
if next_prompt then
input_window.set_content(next_prompt)
require('opencode.ui.mention').restore_mentions(state.windows.input_buf)
end
end
function M.prev_prompt_history()
local config = require('opencode.config')
local key = config.get_key_for_function('input_window', 'prev_prompt_history')
if key ~= '<up>' then
return M.prev_history()
end
local current_line = vim.api.nvim_win_get_cursor(0)[1]
local at_boundary = current_line <= 1
if at_boundary then
return M.prev_history()
end
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, false, true), 'n', false)
end
function M.next_prompt_history()
local config = require('opencode.config')
local key = config.get_key_for_function('input_window', 'next_prompt_history')
if key ~= '<down>' then
return M.next_history()
end
local current_line = vim.api.nvim_win_get_cursor(0)[1]
local at_boundary = current_line >= vim.api.nvim_buf_line_count(0)
if at_boundary then
return M.next_history()
end
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, false, true), 'n', false)
end
function M.next_message()
require('opencode.ui.navigation').goto_next_message()
end
function M.prev_message()
require('opencode.ui.navigation').goto_prev_message()
end
function M.submit_input_prompt()
if state.display_route then
-- we're displaying /help or something similar, need to clear that and refresh
-- the session data before sending the command
state.display_route = nil
ui.render_output(true)
end
input_window.handle_submit()
end
function M.mention_file()
local picker = require('opencode.ui.file_picker')
local context = require('opencode.context')
require('opencode.ui.mention').mention(function(mention_cb)
picker.pick(function(file)
mention_cb(file.path)
context.add_file(file.path)
end)
end)
end
function M.mention()
local config = require('opencode.config')
local char = config.get_key_for_function('input_window', 'mention')
ui.focus_input({ restore_position = false, start_insert = true })
require('opencode.ui.completion').trigger_completion(char)()
end
function M.context_items()
local config = require('opencode.config')
local char = config.get_key_for_function('input_window', 'context_items')
ui.focus_input({ restore_position = true, start_insert = true })
require('opencode.ui.completion').trigger_completion(char)()
end
function M.slash_commands()
local config = require('opencode.config')
local char = config.get_key_for_function('input_window', 'slash_commands')
ui.focus_input({ restore_position = false, start_insert = true })
require('opencode.ui.completion').trigger_completion(char)()
end
function M.focus_input()
ui.focus_input({ restore_position = true, start_insert = true })
end
function M.debug_output()
local config = require('opencode.config')
if not config.debug.enabled then
vim.notify('Debugging is not enabled in the config', vim.log.levels.WARN)
return
end
local debug_helper = require('opencode.ui.debug_helper')
debug_helper.debug_output()
end
function M.debug_message()
local config = require('opencode.config')
if not config.debug.enabled then
vim.notify('Debugging is not enabled in the config', vim.log.levels.WARN)
return
end
local debug_helper = require('opencode.ui.debug_helper')
debug_helper.debug_message()
end
function M.debug_session()
local config = require('opencode.config')
if not config.debug.enabled then
vim.notify('Debugging is not enabled in the config', vim.log.levels.WARN)
return
end
local debug_helper = require('opencode.ui.debug_helper')
debug_helper.debug_session()
end
function M.initialize()
local id = require('opencode.id')
local new_session = core.create_new_session('AGENTS.md Initialization')
if not new_session then
vim.notify('Failed to create new session', vim.log.levels.ERROR)
return
end
if not core.initialize_current_model() or not state.current_model then
vim.notify('No model selected', 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()
state.api_client:init_session(state.active_session.id, {
providerID = providerId,
modelID = modelId,
messageID = id.ascending('message'),
})
end
function M.agent_plan()
require('opencode.core').switch_to_mode('plan')
end
function M.agent_build()
require('opencode.core').switch_to_mode('build')
end
function M.select_agent()
local modes = config_file.get_opencode_agents()
vim.ui.select(modes, {
prompt = 'Select mode:',
}, function(selection)
if not selection then
return
end
require('opencode.core').switch_to_mode(selection)
end)
end
function M.switch_mode()
local modes = config_file.get_opencode_agents() --[[@as string[] ]]
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
require('opencode.core').switch_to_mode(modes[next_index])
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',
'',
'Use `:Opencode <subcommand>` to run commands. Examples:',
'',
'- `:Opencode open input` - Open the input window',
'- `:Opencode session new` - Create a new session',
'- `:Opencode diff open` - Open diff view',
'',
'### Subcommands',
'',
'| Command | Description |',
'|--------------|-------------|',
}, false)
if not state.windows or not state.windows.output_win then
return
end
local max_desc_length = vim.api.nvim_win_get_width(state.windows.output_win) - 22
local sorted_commands = vim.tbl_keys(M.commands)
table.sort(sorted_commands)
for _, name in ipairs(sorted_commands) do
local def = M.commands[name]
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('| %-12s | %-' .. max_desc_length .. 's |', name, desc))
end
table.insert(msg, '')
table.insert(msg, 'For slash commands (e.g., /models, /help), type `/` in the input window.')
table.insert(msg, '')
ui.render_lines(msg)
end
function M.mcp()
local mcp = config_file.get_mcp_servers()
if not mcp then
vim.notify('No MCP configuration found. Please check your opencode config file.', vim.log.levels.WARN)
return
end
state.display_route = '/mcp'
M.open_input()
local msg = M.with_header({
'### Available MCP servers',
'',
'| Name | Type | cmd/url |',
'|--------|------|---------|',
})
for name, def in pairs(mcp) do
local cmd_or_url
if def.type == 'local' then
cmd_or_url = def.command and table.concat(def.command, ' ')
elseif def.type == 'remote' then
cmd_or_url = def.url
end
table.insert(
msg,
string.format(
'| %s %-10s | %s | %s |',
(def.enabled and icons.get('status_on') or icons.get('status_off')),
name,
def.type,
cmd_or_url
)
)
end
table.insert(msg, '')
ui.render_lines(msg)
end
function M.commands_list()
local commands = config_file.get_user_commands()
if not commands then
vim.notify('No user commands found. Please check your opencode config file.', vim.log.levels.WARN)
return
end
state.display_route = '/commands'
M.open_input()
local msg = M.with_header({
'### Available User Commands',
'',
'| Name | Description |Arguments|',
'|------|-------------|---------|',
})
for name, def in pairs(commands) do
local desc = def.description or ''
table.insert(msg, string.format('| %s | %s | %s |', name, desc, tostring(config_file.command_takes_arguments(def))))
end
table.insert(msg, '')
ui.render_lines(msg)
end
function M.current_model()
return core.initialize_current_model()
end
--- Runs a user-defined command by name.
--- @param name string The name of the user command to run.
--- @param args? string[] Additional arguments to pass to the command.
function M.run_user_command(name, args)
M.open_input()
if not state.active_session then
vim.notify('No active session', vim.log.levels.WARN)
return
end
state.api_client
:send_command(state.active_session.id, {
command = name,
arguments = table.concat(args or {}, ' '),
})
:and_then(function()
vim.schedule(function()
require('opencode.history').write('/' .. name .. ' ' .. table.concat(args or {}, ' '))
end)
end)
end
--- Compacts the current session by removing unnecessary data.
--- @param current_session? Session The session to compact. Defaults to the active session.
function M.compact_session(current_session)
current_session = current_session or state.active_session
if not current_session then
vim.notify('No active session to compact', vim.log.levels.WARN)
return
end
local current_model = state.current_model
if not current_model then
vim.notify('No model selected', vim.log.levels.ERROR)
return
end
local providerId, modelId = current_model:match('^(.-)/(.+)$')
if not providerId or not modelId then
vim.notify('Invalid model format: ' .. tostring(current_model), vim.log.levels.ERROR)
return
end
state.api_client
:summarize_session(current_session.id, {
providerID = providerId,
modelID = modelId,
})
:and_then(function()
vim.schedule(function()
vim.notify('Session compacted successfully', vim.log.levels.INFO)
end)
end)
:catch(function(err)
vim.schedule(function()
vim.notify('Failed to compact session: ' .. vim.inspect(err), vim.log.levels.ERROR)
end)
end)
end
function M.share()
if not state.active_session then
vim.notify('No active session to share', vim.log.levels.WARN)
return
end
state.api_client
:share_session(state.active_session.id)
:and_then(function(response)
vim.schedule(function()
if response and response.share and response.share.url then
vim.fn.setreg('+', response.share.url)
vim.notify('Session link copied to clipboard successfully: ' .. response.share.url, vim.log.levels.INFO)
else
vim.notify('Session shared but no link received', vim.log.levels.WARN)
end
end)
end)
:catch(function(err)
vim.schedule(function()
vim.notify('Failed to share session: ' .. vim.inspect(err), vim.log.levels.ERROR)
end)
end)
end
function M.unshare()
if not state.active_session then
vim.notify('No active session to unshare', vim.log.levels.WARN)
return
end
state.api_client
:unshare_session(state.active_session.id)
:and_then(function(response)
vim.schedule(function()
vim.notify('Session unshared successfully', vim.log.levels.INFO)
end)
end)
:catch(function(err)
vim.schedule(function()
vim.notify('Failed to unshare session: ' .. vim.inspect(err), vim.log.levels.ERROR)
end)
end)
end
---@param messageId? string
function M.undo(messageId)
if not state.active_session then
vim.notify('No active session to undo', vim.log.levels.WARN)
return
end
local message_to_revert = messageId or state.last_user_message and state.last_user_message.info.id
if not message_to_revert then
vim.notify('No user message to undo', vim.log.levels.WARN)
return
end
state.api_client
:revert_message(state.active_session.id, {
messageID = message_to_revert,
})
:and_then(function(response)
vim.schedule(function()
vim.cmd('checktime')
end)
end)
:catch(function(err)
vim.schedule(function()
vim.notify('Failed to undo last message: ' .. vim.inspect(err), vim.log.levels.ERROR)
end)
end)
end
function M.timeline()
local user_messages = {}
for _, msg in ipairs(state.messages or {}) do
local parts = msg.parts or {}
local is_summary = #parts == 1 and parts[1].synthetic == true
if msg.info.role == 'user' and not is_summary then
table.insert(user_messages, msg)
end
end
if #user_messages == 0 then
vim.notify('No user messages in the current session', vim.log.levels.WARN)
return
end
local timeline_picker = require('opencode.ui.timeline_picker')
timeline_picker.pick(user_messages, function(selected_msg)
if selected_msg then
require('opencode.ui.navigation').goto_message_by_id(selected_msg.info.id)
end
end)
end
--- Forks the current session from a specific user message.
---@param message_id? string The ID of the user message to fork from. If not provided, uses the last user message.
function M.fork_session(message_id)
if not state.active_session then
vim.notify('No active session to fork', vim.log.levels.WARN)
return
end
local message_to_fork = message_id or state.last_user_message and state.last_user_message.info.id
if not message_to_fork then
vim.notify('No user message to fork from', vim.log.levels.WARN)
return
end
state.api_client
:fork_session(state.active_session.id, {
messageID = message_to_fork,
})
:and_then(function(response)
vim.schedule(function()
if response and response.id then
vim.notify('Session forked successfully. New session ID: ' .. response.id, vim.log.levels.INFO)
core.switch_session(response.id)
else
vim.notify('Session forked but no new session ID received', vim.log.levels.WARN)
end
end)
end)
:catch(function(err)
vim.schedule(function()
vim.notify('Failed to fork session: ' .. vim.inspect(err), vim.log.levels.ERROR)
end)
end)
end
---@param current_session? Session
--- @param new_title? string
function M.rename_session(current_session, new_title)
local promise = require('opencode.promise').new()
current_session = current_session or vim.deepcopy(state.active_session) --[[@as Session]]
if not current_session then
vim.notify('No active session to rename', vim.log.levels.WARN)
promise:resolve(nil)
return promise
end
local function rename_session_with_title(title)
state.api_client
:update_session(current_session.id, { title = title })
:catch(function(err)
vim.schedule(function()
vim.notify('Failed to rename session: ' .. vim.inspect(err), vim.log.levels.ERROR)
end)
end)
:and_then(function()
current_session.title = title
if state.active_session and state.active_session.id == current_session.id then
local session_obj = session.get_by_id(current_session.id)
if session_obj then
session_obj.title = title
state.active_session = vim.deepcopy(session_obj)
end
end
promise:resolve(current_session)
end)
end
if new_title and new_title ~= '' then
rename_session_with_title(new_title)
return promise
end
vim.schedule(function()
vim.ui.input({ prompt = 'New session name: ', default = current_session.title or '' }, function(input)
if input and input ~= '' then
rename_session_with_title(input)
else
promise:resolve(nil)
end
end)
end)
return promise
end
-- Returns the ID of the next user message after the current undo point
-- This is a port of the opencode tui logic
-- https://github.com/sst/opencode/blob/dev/packages/tui/internal/components/chat/messages.go#L1199
function find_next_message_for_redo()
if not state.active_session then
return nil
end
local revert_time = 0
local revert = state.active_session.revert
if not revert then
return nil
end
for _, message in ipairs(state.messages or {}) do
if message.info.id == revert.messageID then
revert_time = math.floor(message.info.time.created)
break
end
if revert.partID and revert.partID ~= '' then
for _, part in ipairs(message.parts) do
if part.id == revert.partID and part.state and part.state.time then
revert_time = math.floor(part.state.time.start)
break
end
end
end
end
-- Find next user message after revert time
local next_message_id = nil
for _, msg in ipairs(state.messages or {}) do
if msg.info.role == 'user' and msg.info.time.created > revert_time then
next_message_id = msg.info.id
break
end
end
return next_message_id
end
function M.redo()
if not state.active_session then
vim.notify('No active session to redo', vim.log.levels.WARN)
return
end
if not state.active_session.revert or state.active_session.revert.messageID == '' then
vim.notify('Nothing to redo', vim.log.levels.WARN)
return
end
if not state.messages then
return
end
local next_message_id = find_next_message_for_redo()
if not next_message_id then
state.api_client
:unrevert_messages(state.active_session.id)
:and_then(function(response)
vim.schedule(function()
vim.cmd('checktime')
end)
end)
:catch(function(err)
vim.schedule(function()
vim.notify('Failed to redo message: ' .. vim.inspect(err), vim.log.levels.ERROR)
end)
end)
else
-- Calling revert on a "later" message is like a redo
state.api_client
:revert_message(state.active_session.id, {
messageID = next_message_id,
})
:and_then(function(response)
vim.schedule(function()
vim.cmd('checktime')
end)
end)
:catch(function(err)
vim.schedule(function()
vim.notify('Failed to redo message: ' .. vim.inspect(err), vim.log.levels.ERROR)
end)
end)
end
end
---@param answer? 'once'|'always'|'reject'
function M.respond_to_permission(answer)
answer = answer or 'once'
if not state.current_permission then
vim.notify('No permission request to accept', vim.log.levels.WARN)
return
end
state.api_client
:respond_to_permission(state.current_permission.sessionID, state.current_permission.id, { response = answer })
:catch(function(err)
vim.schedule(function()
vim.notify('Failed to reply to permission: ' .. vim.inspect(err), vim.log.levels.ERROR)
end)
end)
end
function M.permission_accept()
M.respond_to_permission('once')
end
function M.permission_accept_all()
M.respond_to_permission('always')
end
function M.permission_deny()
M.respond_to_permission('reject')
end
function M.toggle_tool_output()
config.values.ui.output.tools.show_output = not config.ui.output.tools.show_output
ui.render_output()
end
M.commands = {
open = {
desc = 'Open opencode window (input/output)',
completions = { 'input', 'output' },
fn = function(args)
local target = args[1] or 'input'
if target == 'input' then
M.open_input()
elseif target == 'output' then
M.open_output()
else
vim.notify('Invalid target. Use: input or output', vim.log.levels.ERROR)
end
end,
},
close = {
desc = 'Close opencode windows',
fn = function(args)
M.close()
end,
},
cancel = {
desc = 'Cancel running request',
fn = M.cancel,
},
toggle = {
desc = 'Toggle opencode windows',
fn = M.toggle,
},
toggle_focus = {
desc = 'Toggle focus between opencode and code',
fn = M.toggle_focus,
},
toggle_pane = {
desc = 'Toggle between input/output panes',
fn = M.toggle_pane,
},
toggle_zoom = {
desc = 'Toggle window zoom',
fn = M.toggle_zoom,
},
swap = {
desc = 'Swap pane position left/right',
fn = M.swap_position,
},
session = {
desc = 'Manage sessions (new/select/child/compact/share/unshare/rename)',
completions = { 'new', 'select', 'child', 'compact', 'share', 'unshare', 'agents_init', 'rename' },
fn = function(args)
local subcmd = args[1]
if subcmd == 'new' then
local title = table.concat(vim.list_slice(args, 2), ' ')
if title and title ~= '' then
local new_session = core.create_new_session(title)
if not new_session then
vim.notify('Failed to create new session', vim.log.levels.ERROR)
return
end
state.active_session = new_session
M.open_input()
else
M.open_input_new_session()
end
elseif subcmd == 'select' then
M.select_session()
elseif subcmd == 'child' then
M.select_child_session()
elseif subcmd == 'compact' then
M.compact_session()
elseif subcmd == 'share' then
M.share()
elseif subcmd == 'unshare' then
M.unshare()