Repository navigation
Expand file tree
/
Copy pathsession_formatter.lua
More file actions
479 lines (413 loc) · 14.4 KB
/
Copy pathsession_formatter.lua
File metadata and controls
479 lines (413 loc) · 14.4 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
local context_module = require('opencode.context')
local icons = require('opencode.ui.icons')
local util = require('opencode.util')
local Output = require('opencode.ui.output')
local state = require('opencode.state')
local config = require('opencode.config').get()
local snapshot = require('opencode.snapshot')
local M = {
output = Output.new(),
_messages = {},
_current = nil,
_snapshots = {},
}
M.separator = {
'---',
'',
}
---@param session Session Session ID
---@return string[]|nil Formatted session lines
function M.format_session(session)
if not session or session == '' then
return nil
end
state.messages = require('opencode.session').get_messages(session) or {}
M._snapshots = {}
M.output:clear()
M.output:add_line('')
M.output:add_line('')
for i, msg in ipairs(state.messages) do
M.output:add_lines(M.separator)
state.current_message = msg
if not state.current_model and msg.providerID and msg.providerID ~= '' then
state.current_model = msg.providerID .. '/' .. msg.modelID
end
if msg.tokens and msg.tokens.input > 0 then
state.tokens_count = msg.tokens.input + msg.tokens.output + msg.tokens.cache.read + msg.tokens.cache.write
end
if msg.cost and type(msg.cost) == 'number' then
state.cost = msg.cost
end
M._format_message_header(msg, i)
for j, part in ipairs(msg.parts or {}) do
M._current = { msg_idx = i, part_idx = j, role = msg.role, type = part.type, snapshot = part.snapshot }
M.output:add_metadata(M._current)
if part.type == 'text' and part.text then
if msg.role == 'user' then
M._format_user_message(vim.trim(part.text))
elseif msg.role == 'assistant' then
M._format_assistant_message(vim.trim(part.text))
end
elseif part.type == 'tool' then
M._format_tool(part)
elseif part.type == 'patch' and part.hash then
if M._snapshots[#M._snapshots] ~= part.hash then
table.insert(M._snapshots, part.hash)
end
M._format_patch(part)
end
M.output:add_empty_line()
end
if msg.error and msg.error ~= '' then
M._format_error(msg)
end
end
M.output:add_empty_line()
return M.output:get_lines()
end
---@param line number Buffer line number
---@return {message: Message, part: MessagePart, type: string, msg_idx: number, part_idx: number}|nil
function M.get_message_at_line(line)
local metadata = M.output:get_nearest_metadata(line)
if metadata and metadata.msg_idx and metadata.part_idx then
local msg = state.messages[metadata.msg_idx]
local part = msg.parts[metadata.part_idx]
return {
message = msg,
part = part,
msg_idx = metadata.msg_idx,
part_idx = metadata.part_idx,
}
end
end
---@return string[] Lines from the current output
function M.get_lines()
return M.output:get_lines()
end
function M._format_patch(part)
local restore_points = snapshot.get_restore_points_by_parent(part.hash)
M.output:add_empty_line()
M._format_action(icons.get('snapshot') .. ' **Created Snapshot**', vim.trim(part.hash:sub(1, 8)))
M.output:add_action({
text = '[R]evert file',
type = 'diff_revert_selected_file',
args = { part.hash },
key = 'R',
})
M.output:add_action({
text = 'Revert [A]ll',
type = 'diff_revert_all',
args = { part.hash },
key = 'A',
})
M.output:add_action({
text = '[D]iff',
type = 'diff_open',
args = { part.hash },
key = 'D',
})
if #restore_points > 0 then
for _, restore_point in ipairs(restore_points) do
M.output:add_line(
string.format(
' ' .. icons.get('restore_point') .. ' Restore point `%s` - %s',
restore_point.id:sub(1, 8),
util.time_ago(restore_point.created_at)
)
)
M.output:add_action({
text = 'Restore [A]ll',
type = 'diff_restore_snapshot_all',
args = { part.hash },
key = 'A',
})
M.output:add_action({
text = '[R]estore file',
type = 'diff_restore_snapshot_file',
args = { part.hash },
key = 'R',
})
end
end
end
---@param message Message
function M._format_error(message)
M.output:add_empty_line()
M._format_callout('ERROR', vim.inspect(message.error))
end
---@param message Message
function M._format_message_header(message, msg_idx)
local role = message.role or 'unknown'
local icon = message.role == 'user' and icons.get('header_user') or icons.get('header_assistant')
local time = message.time and message.time.created or nil
local time_text = (time and ' (' .. util.time_ago(time) .. ')' or '')
local role_hl = 'OpencodeMessageRole' .. role:sub(1, 1):upper() .. role:sub(2)
M.output:add_empty_line()
M.output:add_metadata({
msg_idx = msg_idx,
part_idx = 1,
role = role,
type = 'header',
})
M.output:add_extmark(M.output:get_line_count(), {
virt_text = {
{ icon, role_hl },
{ ' ' },
{ role:upper(), role_hl },
{ time_text, 'OpenCodeHint' },
},
virt_text_win_col = -3,
priority = 10,
})
M.output:add_line('')
end
---@param callout string Callout type (e.g., 'ERROR', 'TODO')
function M._format_callout(callout, text, title)
title = title and title .. ' ' or ''
local win_width = vim.api.nvim_win_get_width(state.windows.output_win)
if #text > win_width - 4 then
local ok, substituted = pcall(vim.fn.substitute, text, '\v(.{' .. (win_width - 8) .. '})', '\1\n', 'g')
text = ok and substituted or text
end
local lines = vim.split(text, '\n')
if #lines == 1 and title == '' then
M.output:add_line('> [!' .. callout .. '] ' .. lines[1])
else
M.output:add_line('> [!' .. callout .. ']' .. title)
M.output:add_line('>')
M.output:add_lines(lines, '> ')
end
end
---@param text string
function M._format_user_message(text)
local context = context_module.extract_from_message(text)
local start_line = M.output:get_line_count() - 1
M.output:add_empty_line()
M.output:add_lines(vim.split(context.prompt, '\n'))
if context.selected_text then
M.output:add_lines(vim.split(context.selected_text, '\n'))
end
if context.current_file then
M.output:add_empty_line()
local path = context.current_file
if vim.startswith(path, vim.fn.getcwd()) then
path = path:sub(#vim.fn.getcwd() + 2)
end
M.output:add_line(string.format('[%s](%s)', path, context.current_file))
end
local end_line = M.output:get_line_count()
M._add_vertical_border(start_line, end_line, 'OpencodeMessageRoleUser', -3)
end
---@param text string
function M._format_assistant_message(text)
M.output:add_empty_line()
M.output:add_lines(vim.split(text, '\n'))
end
---@param type string Tool type (e.g., 'run', 'read', 'edit', etc.)
function M._format_action(type, value)
if not type or not value then
return
end
M.output:add_line('**' .. type .. '** ` ' .. value .. ' `')
end
---@param input BashToolInput data for the tool
---@param metadata BashToolMetadata Metadata for the tool use
function M._format_bash_tool(input, metadata)
M._format_action(icons.get('run') .. ' run', input and input.description)
if not config.ui.output.tools.show_output then
return
end
if metadata.stdout then
M._format_code(vim.split('> ' .. input.command or '' .. '\n\n' .. metadata.stdout, '\n'), 'bash')
end
end
---@param tool_type string Tool type (e.g., 'read', 'edit', 'write')
---@param input FileToolInput data for the tool
---@param metadata FileToolMetadata Metadata for the tool use
function M._format_file_tool(tool_type, input, metadata)
local file_name = input and vim.fn.fnamemodify(input.filePath, ':t') or ''
local file_type = input and vim.fn.fnamemodify(input.filePath, ':e') or ''
local icons = { read = icons.get('read'), edit = icons.get('edit'), write = icons.get('write') }
M._format_action(icons[tool_type] .. ' ' .. tool_type, file_name)
if not config.ui.output.tools.show_output then
return
end
if tool_type == 'edit' and metadata.diff then
M._format_diff(metadata.diff, file_type)
elseif tool_type == 'write' and input and input.content then
M._format_code(vim.split(input.content, '\n'), file_type)
end
end
---@param title string
---@param input TodoToolInput
function M._format_todo_tool(title, input)
M._format_action(icons.get('plan') .. ' plan', (title or ''))
if not config.ui.output.tools.show_output then
return
end
M.output:add_empty_line()
local todos = input and input.todos or {}
for _, item in ipairs(todos) do
local statuses = { in_progress = '-', completed = 'x', pending = ' ' }
M.output:add_line(string.format('- [%s] %s ', statuses[item.status], item.content), true)
end
end
---@param input GlobToolInput data for the tool
---@param metadata GlobToolMetadata Metadata for the tool use
function M._format_glob_tool(input, metadata)
M._format_action(icons.get('search') .. ' glob', input and input.pattern)
if not config.ui.output.tools.show_output then
return
end
local prefix = metadata.truncated and ' more than' or ''
M.output:add_line(string.format('Found%s `%d` file(s):', prefix, metadata.count or 0))
end
---@param input GrepToolInput data for the tool
---@param metadata GrepToolMetadata Metadata for the tool use
function M._format_grep_tool(input, metadata)
input = input or { path = '', include = '', pattern = '' }
local grep_str = string.format('%s `` %s', (input.path or input.include) or '', input.pattern or '')
M._format_action(icons.get('search') .. ' grep', grep_str)
if not config.ui.output.tools.show_output then
return
end
local prefix = metadata.truncated and ' more than' or ''
M.output:add_line(string.format('Found%s `%d` match', prefix, metadata.matches or 0))
end
---@param input WebFetchToolInput data for the tool
function M._format_webfetch_tool(input)
M._format_action(icons.get('web') .. ' fetch', input and input.url)
end
---@param input ListToolInput
---@param metadata ListToolMetadata
---@param output string
function M._format_list_tool(input, metadata, output)
M._format_action(icons.get('list') .. ' list', input and input.path or '')
if not config.ui.output.tools.show_output then
return
end
local lines = vim.split(vim.trim(output or ''), '\n')
if #lines < 1 or metadata.count == 0 then
M.output:add_line('No files found.')
return
end
if #lines > 1 then
M.output:add_line('Files:')
for i = 2, #lines do
local file = vim.trim(lines[i])
if file ~= '' then
M.output:add_line(' • ' .. file)
end
end
end
if metadata.truncated then
M.output:add_line(string.format('Results truncated, showing first %d files', metadata.count or '?'))
end
end
---@param part MessagePart
function M._format_tool(part)
M.output:add_empty_line()
local tool = part.tool
if not tool then
return
end
local start_line = M.output:get_line_count() + 1
local input = part.state and part.state.input or {}
local metadata = part.state.metadata or {}
local output = part.state and part.state.output or ''
if tool == 'bash' then
M._format_bash_tool(input --[[@as BashToolInput]], metadata --[[@as BashToolMetadata]])
elseif tool == 'read' or tool == 'edit' or tool == 'write' then
M._format_file_tool(tool, input --[[@as FileToolInput]], metadata --[[@as FileToolMetadata]])
elseif tool == 'todowrite' then
M._format_todo_tool(part.state.title, input --[[@as TodoToolInput]])
elseif tool == 'glob' then
M._format_glob_tool(input --[[@as GlobToolInput]], metadata --[[@as GlobToolMetadata]])
elseif tool == 'list' then
M._format_list_tool(input --[[@as ListToolInput]], metadata --[[@as ListToolMetadata]], output)
elseif tool == 'grep' then
M._format_grep_tool(input --[[@as GrepToolInput]], metadata --[[@as GrepToolMetadata]])
elseif tool == 'webfetch' then
M._format_webfetch_tool(input --[[@as WebFetchToolInput]])
elseif tool == 'task' then
M._format_task_tool(input --[[@as TaskToolInput]], metadata --[[@as TaskToolMetadata]])
else
M._format_action(icons.get('tool') .. ' tool', tool)
end
if part.state and part.state.status == 'error' then
M._format_callout('ERROR', part.state.error)
end
M.output:add_empty_line()
local end_line = M.output:get_line_count()
if end_line - start_line > 1 then
M._add_vertical_border(start_line, end_line - 1, 'OpencodeToolBorder', -1)
end
end
---@param input TaskToolInput data for the tool
---@param metadata TaskToolMetadata Metadata for the tool use
function M._format_task_tool(input, metadata)
M._format_action(icons.get('task') .. ' task', input and input.description)
if not config.ui.output.tools.show_output then
return
end
if metadata.summary and type(metadata.summary) == 'table' then
for _, sub_part in ipairs(metadata.summary) do
if sub_part.type == 'tool' and sub_part.tool then
M._format_tool(sub_part)
end
end
end
end
function M._format_code(lines, language)
M.output:add_empty_line()
M.output:add_line('```' .. (language or ''))
M.output:add_lines(lines)
M.output:add_line('```')
M.output:add_empty_line()
end
function M._format_diff(code, file_type)
M.output:add_empty_line()
M.output:add_line('```' .. file_type)
local lines = vim.split(code, '\n')
if #lines > 5 then
lines = vim.list_slice(lines, 6)
end
for _, line in ipairs(lines) do
local first_char = line:sub(1, 1)
if first_char == '+' or first_char == '-' then
local hl_group = first_char == '+' and 'OpencodeDiffAdd' or 'OpencodeDiffDelete'
M.output:add_line(' ' .. line:sub(2))
local line_idx = M.output:get_line_count()
M.output:add_extmark(line_idx, function()
return {
end_col = 0,
end_row = line_idx + 1,
virt_text = { { first_char, { hl_group } } },
hl_group = hl_group,
hl_eol = true,
priority = 5000,
right_gravity = true,
end_right_gravity = false,
virt_text_hide = false,
virt_text_pos = 'overlay',
virt_text_repeat_linebreak = false,
}
end)
else
M.output:add_line(line)
end
end
M.output:add_line('```')
M.output:add_empty_line()
end
function M._add_vertical_border(start_line, end_line, hl_group, win_col)
for line = start_line, end_line do
M.output:add_extmark(line, {
virt_text = { { require('opencode.ui.icons').get('border'), hl_group } },
virt_text_pos = 'overlay',
virt_text_win_col = win_col,
virt_text_repeat_linebreak = true,
})
end
end
return M