-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathcontext.lua
More file actions
299 lines (246 loc) · 8.72 KB
/
context.lua
File metadata and controls
299 lines (246 loc) · 8.72 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
local util = require('opencode.util')
local config = require('opencode.config')
local state = require('opencode.state')
local Promise = require('opencode.promise')
local ChatContext = require('opencode.context.chat_context')
local QuickChatContext = require('opencode.context.quick_chat_context')
local BaseContext = require('opencode.context.base_context')
local M = {}
M.ChatContext = ChatContext
M.QuickChatContext = QuickChatContext
-- Provide access to the context state
function M.get_context()
return ChatContext.context
end
--- Formats context for main chat interface (new simplified API)
---@param prompt string The user's instruction/prompt
---@param context_config? OpencodeContextConfig Optional context config
---@param opts? { range?: { start: integer, stop: integer } }
---@return table result { parts: OpencodeMessagePart[] }
M.format_chat_message = function(prompt, context_config, opts)
opts = opts or {}
opts.context_config = context_config
return ChatContext.format_message(prompt, opts)
end
--- Formats context for quick chat interface (new simplified API)
---@param prompt string The user's instruction/prompt
---@param context_config? OpencodeContextConfig Optional context config
---@param opts? { range?: { start: integer, stop: integer } }
---@return table result { text: string, parts: OpencodeMessagePart[] }
M.format_quick_chat_message = function(prompt, context_config, opts)
opts = opts or {}
opts.context_config = context_config
return QuickChatContext.format_message(prompt, opts)
end
function M.get_current_buf()
return BaseContext.get_current_buf()
end
function M.is_context_enabled(context_key, context_config)
return BaseContext.is_context_enabled(context_key, context_config)
end
function M.get_diagnostics(buf, context_config, range)
return BaseContext.get_diagnostics(buf, context_config, range)
end
function M.get_current_file(buf, context_config)
return BaseContext.get_current_file(buf, context_config)
end
function M.get_current_cursor_data(buf, win, context_config)
return BaseContext.get_current_cursor_data(buf, win, context_config)
end
function M.get_current_selection(context_config)
return BaseContext.get_current_selection(context_config)
end
function M.new_selection(file, content, lines)
return BaseContext.new_selection(file, content, lines)
end
-- Delegate global state management to ChatContext
function M.add_selection(selection)
ChatContext.add_selection(selection)
state.context_updated_at = vim.uv.now()
end
function M.remove_selection(selection)
ChatContext.remove_selection(selection)
state.context_updated_at = vim.uv.now()
end
function M.clear_selections()
ChatContext.clear_selections()
end
--- Captures the current visual selection and adds it to the context.
--- This can be called from any buffer at any time, even when the panel is already open.
--- Selections persist across file switches and accumulate across buffers.
---@param range? OpencodeSelectionRange
---@return boolean success Whether a selection was successfully added
function M.add_visual_selection(range)
local buf = vim.api.nvim_get_current_buf()
if not util.is_buf_a_file(buf) then
vim.notify('Cannot add selection: not a file buffer', vim.log.levels.WARN)
return false
end
local current_selection = BaseContext.get_current_selection(nil, range)
if not current_selection then
vim.notify('No visual selection found', vim.log.levels.WARN)
return false
end
local file = BaseContext.get_current_file_for_selection(buf)
if not file then
vim.notify('Cannot determine file for selection', vim.log.levels.WARN)
return false
end
local selection = BaseContext.new_selection(file, current_selection.text, current_selection.lines)
M.add_selection(selection)
vim.notify(
string.format('Selection added from %s (lines %s)', file.name, current_selection.lines),
vim.log.levels.INFO
)
return true
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')
ChatContext.add_file(file)
state.context_updated_at = vim.uv.now()
end
function M.remove_file(file)
file = vim.fn.fnamemodify(file, ':p')
ChatContext.remove_file(file)
state.context_updated_at = vim.uv.now()
end
function M.clear_files()
ChatContext.clear_files()
end
function M.add_subagent(subagent)
ChatContext.add_subagent(subagent)
state.context_updated_at = vim.uv.now()
end
function M.remove_subagent(subagent)
ChatContext.remove_subagent(subagent)
state.context_updated_at = vim.uv.now()
end
function M.clear_subagents()
ChatContext.clear_subagents()
end
function M.unload_attachments()
ChatContext.clear_files()
ChatContext.clear_selections()
end
function M.load()
ChatContext.load()
state.context_updated_at = vim.uv.now()
end
-- Context creation with delta logic (delegates to ChatContext)
function M.delta_context(opts)
return ChatContext.delta_context(opts)
end
---@param prompt string
---@param opts? OpencodeContextConfig|nil
---@return OpencodeMessagePart[]
M.format_message = Promise.async(function(prompt, opts)
local result = ChatContext.format_message(prompt, { context_config = opts }):await()
return result.parts
end)
---@param text string
---@param context_type string|nil
function M.decode_json_context(text, context_type)
local ok, result = pcall(vim.json.decode, text)
if not ok or (context_type and result.context_type ~= context_type) then
return nil
end
return result
end
--- Extracts context from an OpencodeMessage (with parts)
---@param message { parts: OpencodeMessagePart[] }
---@return { prompt: string|nil, selected_text: string|nil, current_file: string|nil, mentioned_files: string[]|nil}
function M.extract_from_opencode_message(message)
local ctx = { prompt = nil, selected_text = nil, current_file = nil }
local handlers = {
text = function(part)
ctx.prompt = ctx.prompt or part.text or ''
end,
text_context = function(part)
local json = M.decode_json_context(part.text, 'selection')
ctx.selected_text = json and json.content or ctx.selected_text
end,
file = function(part)
if not part.source then
ctx.current_file = part.filename
end
end,
}
for _, part in ipairs(message and message.parts or {}) do
local handler = handlers[part.type .. (part.synthetic and '_context' or '')]
if handler then
handler(part)
end
if ctx.prompt and ctx.selected_text and ctx.current_file then
break
end
end
return ctx
end
function M.extract_from_message_legacy(text)
local current_file = M.extract_legacy_tag('current-file', text)
local context = {
prompt = M.extract_legacy_tag('user-query', text) or text,
selected_text = M.extract_legacy_tag('manually-added-selection', text),
current_file = current_file and current_file:match('Path: (.+)') or nil,
}
return context
end
function M.extract_legacy_tag(tag, text)
local start_tag = '<' .. tag .. '>'
local end_tag = '</' .. tag .. '>'
local pattern = vim.pesc(start_tag) .. '(.-)' .. vim.pesc(end_tag)
local content = text:match(pattern)
if content then
return vim.trim(content)
end
-- Fallback to the original method if pattern matching fails
local query_start = text:find(start_tag)
local query_end = text:find(end_tag)
if query_start and query_end then
local query_content = text:sub(query_start + #start_tag, query_end - 1)
return vim.trim(query_content)
end
return nil
end
function M.setup()
local debounced_load = util.debounce(function()
M.load()
end, 200)
state.subscribe({ 'current_code_buf', 'current_context_config', 'is_opencode_focused' }, function()
debounced_load()
end)
local augroup = vim.api.nvim_create_augroup('OpenCodeContext', { clear = true })
vim.api.nvim_create_autocmd('BufWritePost', {
pattern = '*',
group = augroup,
callback = function(args)
local buf = args.buf
local curr_buf = state.current_code_buf or vim.api.nvim_get_current_buf()
if buf == curr_buf and util.is_buf_a_file(buf) then
debounced_load()
end
end,
})
vim.api.nvim_create_autocmd('DiagnosticChanged', {
pattern = '*',
group = augroup,
callback = function(args)
local buf = args.buf
local curr_buf = state.current_code_buf or vim.api.nvim_get_current_buf()
if buf == curr_buf and util.is_buf_a_file(buf) and M.is_context_enabled('diagnostics') then
debounced_load()
end
end,
})
end
return M