forked from sudo-tee/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_context.lua
More file actions
568 lines (485 loc) · 15.8 KB
/
chat_context.lua
File metadata and controls
568 lines (485 loc) · 15.8 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
local base_context = require('opencode.context.base_context')
local util = require('opencode.util')
local state = require('opencode.state')
local Promise = require('opencode.promise')
local M = {}
M.context = {
mentioned_files = {},
selections = {},
mentioned_subagents = {},
current_file = nil,
cursor_data = nil,
linter_errors = nil,
}
---@param path string
---@param prompt? string
---@return OpencodeMessagePart
local function format_file_part(path, prompt)
local rel_path = vim.fn.fnamemodify(path, ':~:.')
local mention = '@' .. rel_path
local pos = prompt and prompt:find(mention)
pos = pos and pos - 1 or 0 -- convert to 0-based index
local ext = vim.fn.fnamemodify(path, ':e'):lower()
local mime_type = 'text/plain'
if ext == 'png' then
mime_type = 'image/png'
elseif ext == 'jpg' or ext == 'jpeg' then
mime_type = 'image/jpeg'
elseif ext == 'gif' then
mime_type = 'image/gif'
elseif ext == 'webp' then
mime_type = 'image/webp'
end
local file_part = { filename = rel_path, type = 'file', mime = mime_type, url = 'file://' .. path }
if prompt then
file_part.source = {
path = path,
type = 'file',
text = { start = pos, value = mention, ['end'] = pos + #mention },
}
end
return file_part
end
---@param selection OpencodeContextSelection
---@return OpencodeMessagePart
local function format_selection_part(selection)
local lang = util.get_markdown_filetype(selection.file and selection.file.name or '') or ''
return {
type = 'text',
metadata = {
context_type = 'selection',
},
text = vim.json.encode({
context_type = 'selection',
file = selection.file,
content = string.format('`````%s\n%s\n`````', lang, selection.content),
lines = selection.lines,
}),
synthetic = true,
}
end
---@param diagnostics OpencodeDiagnostic[]
---@param range? { start_line: integer, end_line: integer }|nil
---@return OpencodeMessagePart
local function format_diagnostics_part(diagnostics, range)
local diag_list = {}
for _, diag in ipairs(diagnostics) do
if not range or (diag.lnum >= range.start_line and diag.lnum <= range.end_line) then
local short_msg = diag.message:gsub('%s+', ' '):gsub('^%s', ''):gsub('%s$', '')
table.insert(
diag_list,
{ msg = short_msg, severity = diag.severity, pos = 'l' .. diag.lnum + 1 .. ':c' .. diag.col + 1 }
)
end
end
return {
type = 'text',
metadata = {
context_type = 'diagnostics',
},
text = vim.json.encode({ context_type = 'diagnostics', content = diag_list }),
synthetic = true,
}
end
---@param cursor_data table
---@param get_current_buf fun(): integer|nil Function to get current buffer
---@return OpencodeMessagePart
local function format_cursor_data_part(cursor_data, get_current_buf)
local buf = (get_current_buf() or 0) --[[@as integer]]
local lang = util.get_markdown_filetype(vim.api.nvim_buf_get_name(buf)) or ''
return {
type = 'text',
metadata = {
context_type = 'cursor-data',
lang = lang,
},
text = vim.json.encode({
context_type = 'cursor-data',
line = cursor_data.line,
column = cursor_data.column,
line_content = string.format('`````%s\n%s\n`````', lang, cursor_data.line_content),
lines_before = cursor_data.lines_before,
lines_after = cursor_data.lines_after,
}),
synthetic = true,
}
end
---@param agent string
---@param prompt string
---@return OpencodeMessagePart
local function format_subagents_part(agent, prompt)
local mention = '@' .. agent
local pos = prompt:find(mention)
pos = pos and pos - 1 or 0 -- convert to 0-based index
return {
type = 'agent',
name = agent,
source = { value = mention, start = pos, ['end'] = pos + #mention },
}
end
---@param buf integer
---@return OpencodeMessagePart
local function format_buffer_part(buf)
local file = vim.api.nvim_buf_get_name(buf)
local rel_path = vim.fn.fnamemodify(file, ':~:.')
return {
type = 'text',
text = table.concat(vim.api.nvim_buf_get_lines(buf, 0, -1, false), '\n'),
metadata = {
context_type = 'file-content',
filename = rel_path,
mime = 'text/plain',
},
synthetic = true,
}
end
---@param diff_text string
---@return OpencodeMessagePart
local function format_git_diff_part(diff_text)
return {
type = 'text',
metadata = {
context_type = 'git-diff',
},
text = diff_text,
synthetic = true,
}
end
-- Global context management functions
function M.add_selection(selection)
-- Ensure selections is always a table
if not M.context.selections then
M.context.selections = {}
end
table.insert(M.context.selections, selection)
state.context_updated_at = vim.uv.now()
end
function M.remove_selection(selection)
if not M.context.selections then
M.context.selections = {}
return
end
for i, sel in ipairs(M.context.selections) do
if sel.file.path == selection.file.path and sel.lines == selection.lines then
table.remove(M.context.selections, i)
break
end
end
state.context_updated_at = vim.uv.now()
end
function M.clear_selections()
M.context.selections = {}
end
function M.add_file(file)
local is_file = vim.fn.filereadable(file) == 1
local is_dir = vim.fn.isdirectory(file) == 1
if not is_file and not is_dir then
vim.notify('File not added to context. Could not read.')
return
end
if not util.is_path_in_cwd(file) and not util.is_temp_path(file, 'pasted_image') then
vim.notify('File not added to context. Must be inside current working directory.')
return
end
file = vim.fn.fnamemodify(file, ':p')
if not M.context.mentioned_files then
M.context.mentioned_files = {}
end
if not vim.tbl_contains(M.context.mentioned_files, file) then
table.insert(M.context.mentioned_files, file)
end
state.context_updated_at = vim.uv.now()
end
function M.remove_file(file)
if not M.context.mentioned_files then
M.context.mentioned_files = {}
return
end
file = vim.fn.fnamemodify(file, ':p')
for i, f in ipairs(M.context.mentioned_files) do
if f == file then
table.remove(M.context.mentioned_files, i)
break
end
end
state.context_updated_at = vim.uv.now()
end
function M.clear_files()
M.context.mentioned_files = {}
end
function M.add_subagent(subagent)
-- Ensure mentioned_subagents is always a table
if not M.context.mentioned_subagents then
M.context.mentioned_subagents = {}
end
if not vim.tbl_contains(M.context.mentioned_subagents, subagent) then
table.insert(M.context.mentioned_subagents, subagent)
end
state.context_updated_at = vim.uv.now()
end
function M.remove_subagent(subagent)
if not M.context.mentioned_subagents then
M.context.mentioned_subagents = {}
return
end
for i, a in ipairs(M.context.mentioned_subagents) do
if a == subagent then
table.remove(M.context.mentioned_subagents, i)
break
end
end
state.context_updated_at = vim.uv.now()
end
function M.clear_subagents()
M.context.mentioned_subagents = {}
end
function M.unload_attachments()
M.context.mentioned_files = {}
M.context.selections = {}
end
function M.get_mentioned_files()
return M.context.mentioned_files or {}
end
function M.get_selections()
return M.context.selections or {}
end
function M.get_mentioned_subagents()
return M.context.mentioned_subagents or {}
end
-- Chat-context-aware get_diagnostics that considers stored selections
---@param buf integer
---@param context_config? OpencodeContextConfig
---@param range? { start_line: integer, end_line: integer }
---@return OpencodeDiagnostic[]|nil
function M.get_diagnostics(buf, context_config, range)
-- Use explicit range if provided
if range then
return base_context.get_diagnostics(buf, context_config, range)
end
if M.context.selections and #M.context.selections > 0 then
local selection_ranges = {}
for _, sel in ipairs(M.context.selections) do
if sel.lines then
-- Handle both formats: "1, 5" and "1-5"
local start_line, end_line = sel.lines:match('(%d+)[,%-]%s*(%d+)')
if not start_line then
-- Single line case like "5, 5" or just "5"
start_line = sel.lines:match('(%d+)')
end_line = start_line
end
if start_line then
local start_num = tonumber(start_line)
local end_num = tonumber(end_line)
if start_num and end_num then
-- Convert to 0-based
local selection_range = {
start_line = start_num - 1,
end_line = end_num - 1,
}
table.insert(selection_ranges, selection_range)
end
end
end
end
if #selection_ranges > 0 then
return base_context.get_diagnostics(buf, context_config, selection_ranges)
end
end
return base_context.get_diagnostics(buf, context_config, nil)
end
---@param current_file table|nil
---@return boolean, boolean -- should_update, is_different_file
function M.should_update_current_file(current_file)
if not M.context.current_file then
return current_file ~= nil, false
end
if not current_file then
return false, false
end
-- Different file name means update needed
if M.context.current_file.name ~= current_file.name then
return true, true
end
-- Same file, check modification time
local file_path = current_file.path
if not file_path or vim.fn.filereadable(file_path) ~= 1 then
return false, false
end
local stat = vim.uv.fs_stat(file_path)
if not (stat and stat.mtime and stat.mtime.sec) then
return false, false
end
local file_mtime_sec = stat.mtime.sec --[[@as number]]
local last_sent_mtime = M.context.current_file.sent_at_mtime or 0
return file_mtime_sec > last_sent_mtime, false
end
-- Load function that populates the global context state
-- This is the core loading logic that was originally in the main context module
function M.load()
if not state.active_session then
return
end
local buf, win = base_context.get_current_buf()
if not buf or not win then
return
end
local current_file = base_context.get_current_file(buf)
local cursor_data = base_context.get_current_cursor_data(buf, win)
local should_update_file, is_different_file = M.should_update_current_file(current_file)
if should_update_file then
if is_different_file then
M.context.selections = {}
end
M.context.current_file = current_file
if M.context.current_file then
M.context.current_file.sent_at = nil
M.context.current_file.sent_at_mtime = nil
end
end
M.context.cursor_data = cursor_data
M.context.linter_errors = M.get_diagnostics(buf, nil, nil)
-- Handle current selection
local current_selection = base_context.get_current_selection()
if current_selection and M.context.current_file then
local selection =
base_context.new_selection(M.context.current_file, current_selection.text, current_selection.lines)
M.add_selection(selection)
end
end
---@param current_file table
local function set_file_sent_timestamps(current_file)
if not current_file then
return
end
current_file.sent_at = vim.uv.now()
local stat = vim.uv.fs_stat(current_file.path)
if stat and stat.mtime and stat.mtime.sec then
current_file.sent_at_mtime = stat.mtime.sec
end
end
-- This function creates a context snapshot with delta logic against the last sent context
function M.delta_context(opts)
local config = require('opencode.config')
opts = opts or state.current_context_config or config.context
if opts.enabled == false then
return {
current_file = nil,
mentioned_files = nil,
selections = nil,
linter_errors = nil,
cursor_data = nil,
mentioned_subagents = nil,
}
end
local buf, win = base_context.get_current_buf()
if not buf then
return {}
end
local ctx = vim.deepcopy(M.context)
if ctx.current_file and M.context.current_file then
set_file_sent_timestamps(M.context.current_file)
set_file_sent_timestamps(ctx.current_file)
end
-- no need to send subagents again
local last_context = state.last_sent_context
if last_context then
if
ctx.mentioned_subagents
and last_context.mentioned_subagents
and vim.deep_equal(ctx.mentioned_subagents, last_context.mentioned_subagents)
then
ctx.mentioned_subagents = nil
M.context.mentioned_subagents = nil
end
end
state.context_updated_at = vim.uv.now()
return ctx
end
--- Formats context as structured message parts for the main chat interface
--- This is the main function that includes global state (mentioned files, selections, etc.)
---@param prompt string The user's instruction/prompt
---@param opts? { range?: { start: integer, stop: integer }, context_config?: OpencodeContextConfig }
---@return table result { parts: OpencodeMessagePart[] }
M.format_message = Promise.async(function(prompt, opts)
opts = opts or {}
local context_config = opts.context_config
local buf, win = base_context.get_current_buf()
local range = opts.range
local parts = {}
for _, file_path in ipairs(M.context.mentioned_files or {}) do
table.insert(parts, format_file_part(file_path, prompt))
end
for _, agent in ipairs(M.context.mentioned_subagents or {}) do
table.insert(parts, format_subagents_part(agent, prompt))
end
if not buf then
table.insert(parts, { type = 'text', text = prompt })
return { parts = parts }
end
if M.context.current_file and not M.context.current_file.sent_at then
table.insert(parts, format_file_part(M.context.current_file.path))
set_file_sent_timestamps(M.context.current_file)
end
if base_context.is_context_enabled('selection', context_config) then
local selections = {}
if range and range.start and range.stop then
local file = base_context.get_current_file(buf, context_config)
if file then
local selection = base_context.new_selection(
file,
table.concat(
vim.api.nvim_buf_get_lines(buf, math.floor(range.start) - 1, math.floor(range.stop), false),
'\n'
),
string.format('%d-%d', math.floor(range.start), math.floor(range.stop))
)
table.insert(selections, selection)
end
end
local current_selection = base_context.get_current_selection(context_config)
if current_selection then
local file = base_context.get_current_file(buf, context_config)
if file then
local selection = base_context.new_selection(file, current_selection.text, current_selection.lines)
table.insert(selections, selection)
end
end
for _, sel in ipairs(M.context.selections or {}) do
table.insert(selections, sel)
end
for _, sel in ipairs(selections) do
table.insert(parts, format_selection_part(sel))
end
end
if base_context.is_context_enabled('buffer', context_config) then
table.insert(parts, format_buffer_part(buf))
end
local diag_range = nil
if range then
diag_range = { start_line = math.floor(range.start) - 1, end_line = math.floor(range.stop) - 1 }
end
local diagnostics = M.get_diagnostics(buf, context_config, diag_range)
if diagnostics and #diagnostics > 0 then
table.insert(parts, format_diagnostics_part(diagnostics, diag_range))
end
if base_context.is_context_enabled('cursor_data', context_config) then
local cursor_data = base_context.get_current_cursor_data(buf, win, context_config)
if cursor_data then
table.insert(
parts,
format_cursor_data_part(cursor_data, function()
return buf
end)
)
end
end
if base_context.is_context_enabled('git_diff', context_config) then
local diff_text = base_context.get_git_diff(context_config):await()
if diff_text and diff_text ~= '' then
table.insert(parts, format_git_diff_part(diff_text))
end
end
table.insert(parts, { type = 'text', text = prompt })
return { parts = parts }
end)
return M