Repository navigation
Expand file tree
/
Copy pathquestion_window.lua
More file actions
598 lines (509 loc) · 15.9 KB
/
Copy pathquestion_window.lua
File metadata and controls
598 lines (509 loc) · 15.9 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
local state = require('opencode.state')
local icons = require('opencode.ui.icons')
local Dialog = require('opencode.ui.dialog')
local Promise = require('opencode.promise')
local config = require('opencode.config')
local session_scope = require('opencode.ui.session_scope')
local M = {}
M._current_question = nil
M._current_question_index = 1
M._collected_answers = {}
M._answering = false
M._dialog = nil
---@param index integer
---@return string[]|nil
local function get_answer_for_index(index)
local answer = M._collected_answers[index]
if type(answer) ~= 'table' or #answer == 0 then
return nil
end
return answer
end
---@return boolean
local function has_all_answers()
local request = M._current_question
local questions = request and request.questions or {}
if #questions == 0 then
return false
end
for i = 1, #questions do
if not get_answer_for_index(i) then
return false
end
end
return true
end
---@return integer|nil
local function get_next_unanswered_question_index()
local request = M._current_question
local questions = request and request.questions or {}
if #questions == 0 then
return nil
end
for i = 1, #questions do
if not get_answer_for_index(i) then
return i
end
end
end
---@param question_request OpencodeQuestionRequest|nil
---@return boolean
function M.matches_active_question(question_request)
return question_request ~= nil
and M._current_question ~= nil
and question_request.id ~= nil
and M._current_question.id == question_request.id
end
---@param question_request OpencodeQuestionRequest|nil
---@return boolean
local function has_tool_identifiers(question_request)
local tool = question_request and question_request.tool
return tool ~= nil and ((tool.callID and tool.callID ~= '') or (tool.messageID and tool.messageID ~= ''))
end
---@param part OpencodeMessagePart|nil
---@param question_request OpencodeQuestionRequest|nil
---@return boolean
local function question_part_matches_request(part, question_request)
if not part or part.tool ~= 'question' or not question_request then
return false
end
local tool = question_request.tool
if not tool then
return false
end
if tool.callID and tool.callID ~= '' and part.callID ~= tool.callID then
return false
end
if tool.messageID and tool.messageID ~= '' and part.messageID ~= tool.messageID then
return false
end
return true
end
---@param parts OpencodeMessagePart[]|nil
---@param question_request OpencodeQuestionRequest|nil
---@return OpencodeMessagePart|nil
local function find_matching_question_part(parts, question_request)
for _, part in ipairs(parts or {}) do
if question_part_matches_request(part, question_request) then
return part
end
end
end
---@param question_request OpencodeQuestionRequest|nil
---@return OpencodeMessagePart|nil
local function get_question_part(question_request)
if not has_tool_identifiers(question_request) then
return nil
end
local tool = question_request.tool
local tool_message_id = tool and tool.messageID
if tool_message_id and state.messages then
for _, message in ipairs(state.messages) do
if message.info and message.info.id == tool_message_id then
local part = find_matching_question_part(message.parts, question_request)
if part then
return part
end
end
end
end
if question_request and question_request.sessionID and question_request.sessionID ~= '' then
local render_state = require('opencode.ui.renderer.ctx').render_state
return find_matching_question_part(
render_state:get_child_session_parts(question_request.sessionID),
question_request
)
end
end
---@param question_request OpencodeQuestionRequest|nil
---@return boolean
local function is_resolved_question_request(question_request)
local part = get_question_part(question_request)
if not part or not part.state then
return false
end
local metadata = part.state.metadata
if metadata and metadata.answers and #metadata.answers > 0 then
return true
end
local status = part.state.status
return status ~= nil and status ~= '' and status ~= 'pending' and status ~= 'running'
end
---Request the renderer to show the current question display.
local function render_question()
require('opencode.ui.renderer.events').render_question_display()
end
---Request the renderer to remove the current question display.
local function clear_question()
require('opencode.ui.renderer.events').clear_question_display()
end
---@param question_request OpencodeQuestionRequest
function M.show_question(question_request)
if not question_request or not question_request.questions or #question_request.questions == 0 then
return
end
if is_resolved_question_request(question_request) then
return
end
M._current_question = question_request
M._collected_answers = {}
M._current_question_index = 1
if config.ui.questions and config.ui.questions.use_vim_ui_select then
M._show_question_with_vim_ui_select()
else
M._setup_dialog()
render_question()
end
end
---@param session_id string|nil
function M.restore_pending_question(session_id)
if not state.api_client or not session_id or session_id == '' then
return Promise.new():resolve(nil)
end
if M.has_question() and session_scope.belongs_to_active_session(M._current_question) then
if not is_resolved_question_request(M._current_question) then
return Promise.new():resolve(nil)
end
M.clear_question()
end
return state.api_client
:list_questions()
:and_then(function(requests)
if not requests or type(requests) ~= 'table' then
return
end
for _, request in ipairs(requests) do
if
request
and request.questions
and #request.questions > 0
and session_scope.belongs_to_active_session(request)
and not is_resolved_question_request(request)
then
if M.matches_active_question(request) then
return
end
M.show_question(request)
return
end
end
end)
:catch(function(err)
vim.schedule(function()
vim.notify('Failed to restore pending question: ' .. vim.inspect(err), vim.log.levels.WARN)
end)
end)
end
---Reset the current question state and remove any dialog UI.
function M.clear_question()
M._clear_dialog()
M._current_question = nil
M._current_question_index = 1
M._collected_answers = {}
M._answering = false
render_question()
end
---@return OpencodeQuestionInfo|nil
function M.get_current_question_info()
if not M._current_question or not M._current_question.questions then
return nil
end
local questions = M._current_question.questions
local idx = M._current_question_index
return (idx > 0 and idx <= #questions) and questions[idx] or nil
end
---@return boolean
function M.has_question()
return M._current_question ~= nil and M.get_current_question_info() ~= nil
end
---@param answer_value string|string[]
local function answer_current_question(answer_value)
local request = M._current_question
if not request then
return
end
M._collected_answers[M._current_question_index] = type(answer_value) == 'table' and answer_value or { answer_value }
if has_all_answers() then
M._send_reply(request.id, M._collected_answers)
M.clear_question()
else
M._current_question_index = get_next_unanswered_question_index() or M._current_question_index
M._answering = false
M._clear_dialog()
M._setup_dialog()
end
render_question()
-- Use schedule to ensure UI updates happen after all state changes
vim.schedule(function()
require('opencode.ui.renderer').scroll_to_bottom(true)
end)
end
---@param options OpencodeQuestionOption[]
---@return integer|nil
local function find_other_option(options)
for i, opt in ipairs(options) do
if vim.startswith(opt.label:lower(), 'other') then
return i
end
end
return nil
end
---@param question_info OpencodeQuestionInfo
---@return integer
local function get_total_options(question_info)
local has_other = find_other_option(question_info.options) ~= nil
return has_other and #question_info.options or (#question_info.options + 1)
end
---@param option_index number
function M._answer_with_option(option_index)
local question_info = M.get_current_question_info()
if not question_info or not question_info.options then
return
end
local other_index = find_other_option(question_info.options)
local total_options = get_total_options(question_info)
if option_index < 1 or option_index > total_options then
vim.notify('Invalid option selected', vim.log.levels.WARN)
return
end
if (not other_index and option_index == total_options) or (other_index and option_index == other_index) then
M._answer_with_custom()
return
end
answer_current_question(question_info.options[option_index].label)
end
---Prompt for a free-form answer to the active question.
function M._answer_with_custom()
vim.ui.input({ prompt = 'Enter your response: ' }, function(input)
if input and input ~= '' then
answer_current_question(input)
elseif M._current_question.id then
M._send_reject(M._current_question.id)
M.clear_question()
render_question()
end
end)
end
---@param options OpencodeQuestionOption[]
---@return OpencodeQuestionOption[]
local function add_other_if_missing(options)
if find_other_option(options) ~= nil then
return options
end
local result = vim.deepcopy(options)
table.insert(result, { label = 'Other', description = 'Type your own answer' })
return result
end
---@param output Output
local function format_question_tabs(output)
local request = M._current_question
if not request or #request.questions <= 1 then
return
end
local line = ''
local segments = {}
for i, question in ipairs(request.questions) do
local label = question.header ~= '' and question.header or ('Q' .. i)
local is_active = i == M._current_question_index
local is_done = get_answer_for_index(i) ~= nil
local marker = is_done and icons.get('completed') or ' '
local segment = string.format(' %d [%s] %s ', i, label, marker)
if #line > 0 then
line = line .. ' '
end
local start_col = #line
line = line .. segment
table.insert(segments, {
start_col = start_col,
end_col = #line,
hl_group = is_active and 'OpencodeQuestionTabActive'
or (is_done and 'OpencodeQuestionTabDone' or 'OpencodeQuestionTabPending'),
})
end
local line_idx = output:add_line(line)
for _, segment in ipairs(segments) do
output:add_extmark(line_idx - 1, {
start_col = segment.start_col,
end_col = segment.end_col,
hl_group = segment.hl_group,
} --[[@as OutputExtmark]])
end
output:add_line('')
end
---@param output Output
function M.format_display(output)
if not M.has_question() or M._answering then
return
end
local question_info = M.get_current_question_info()
if not question_info or not M._dialog then
return
end
local icons = require('opencode.ui.icons')
local progress = ''
if M._current_question and #M._current_question.questions > 1 then
progress = string.format(' (%d/%d)', M._current_question_index, #M._current_question.questions)
end
format_question_tabs(output)
-- Prepare options
local options_to_display = add_other_if_missing(question_info.options)
local options = {}
for i, option in ipairs(options_to_display) do
table.insert(options, {
label = option.label,
description = option.description,
})
end
-- Use dialog's format_dialog method
M._dialog:format_dialog(output, {
title = icons.get('question') .. ' Question' .. progress,
title_hl = 'OpencodeQuestionTitle',
border_hl = 'OpencodeQuestionBorder',
content = vim.split(question_info.question, '\n'),
options = options,
unfocused_message = 'Focus Opencode window to answer question',
})
end
---Create the in-buffer dialog used to answer the active question.
function M._setup_dialog()
if not M.has_question() then
return
end
M._clear_dialog()
local question_info = M.get_current_question_info()
if not question_info or not state.windows or not state.windows.output_buf then
return
end
local buf = state.windows.output_buf
---@return boolean
local function check_focused()
local ui = require('opencode.ui.ui')
return ui.is_opencode_focused() and M.has_question()
end
---@param index integer
local function on_select(index)
if not check_focused() then
return
end
M._answering = true
render_question()
M._clear_dialog()
vim.defer_fn(function()
M._answer_with_option(index)
end, 100)
end
---Reject the current question if the dialog is dismissed.
local function on_dismiss()
if not check_focused() then
return
end
M._send_reject(M._current_question.id)
M.clear_question()
render_question()
end
---Refresh the rendered question state after navigation changes.
local function on_navigate()
render_question()
end
---@param index integer
local function on_navigate_group(index)
M._current_question_index = index
render_question()
end
---@return integer
local function get_option_count()
local question_info = M.get_current_question_info()
return question_info and get_total_options(question_info) or 0
end
M._dialog = Dialog.new({
buffer = buf,
on_select = on_select,
on_dismiss = on_dismiss,
on_navigate = on_navigate,
on_navigate_group = on_navigate_group,
get_option_count = get_option_count,
get_group_count = function()
return M._current_question and #M._current_question.questions or 0
end,
check_focused = check_focused,
namespace_prefix = 'opencode_question',
keymaps = {
left = { 'h', '<Left>' },
right = { 'l', '<Right>' },
},
})
M._dialog:set_group_selection(M._current_question_index)
M._dialog:setup()
end
---Tear down the active question dialog, if any.
function M._clear_dialog()
if M._dialog then
M._dialog:teardown()
M._dialog = nil
end
end
---Show question using vim.ui.select
function M._show_question_with_vim_ui_select()
if not M.has_question() then
return
end
local question_info = M.get_current_question_info()
if not question_info or not question_info.options then
return
end
local options_to_display = add_other_if_missing(question_info.options)
local progress = ''
if M._current_question and #M._current_question.questions > 1 then
progress = string.format(' (%d/%d)', M._current_question_index, #M._current_question.questions)
end
local prompt = question_info.question .. progress
local choices = {}
for i, option in ipairs(options_to_display) do
table.insert(choices, option.label)
end
vim.ui.select(choices, {
prompt = prompt,
format_item = function(item)
return item
end,
}, function(choice)
if not choice then
-- User cancelled
if M._current_question and M._current_question.id then
M._send_reject(M._current_question.id)
end
M.clear_question()
return
end
-- Find the selected option index
local selected_index = nil
for i, option in ipairs(options_to_display) do
if option.label == choice then
selected_index = i
break
end
end
if selected_index then
M._answering = true
M._answer_with_option(selected_index)
end
end)
end
---@param request_id string
---@param answers string[][]
function M._send_reply(request_id, answers)
if state.api_client then
state.api_client:reply_question(request_id, answers):catch(function(err)
vim.notify('Failed to reply to question: ' .. vim.inspect(err), vim.log.levels.ERROR)
end)
end
end
---@param request_id string
function M._send_reject(request_id)
if state.api_client then
state.api_client:reject_question(request_id):catch(function(err)
vim.notify('Failed to reject question: ' .. vim.inspect(err), vim.log.levels.ERROR)
end)
end
end
return M