forked from johnseth97/codex.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.lua
More file actions
328 lines (290 loc) · 8.93 KB
/
history.lua
File metadata and controls
328 lines (290 loc) · 8.93 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
local M = {}
local function decode_json(line)
local ok, data = pcall(vim.json.decode, line)
if ok then return data end
ok, data = pcall(vim.fn.json_decode, line)
if ok then return data end
return nil
end
local function codex_home()
return os.getenv('CODEX_HOME') or vim.fn.expand('~/.codex')
end
local function session_files()
local dir = codex_home() .. '/sessions'
return vim.fn.globpath(dir, '**/*.jsonl', true, true)
end
local function parse_session_meta(file)
local lines = vim.fn.readfile(file, '', 1)
if not lines or #lines == 0 then return nil end
local data = decode_json(lines[1])
if not data or data.type ~= 'session_meta' then return nil end
local payload = data.payload or {}
local git = payload.git or {}
return {
id = payload.id,
timestamp = payload.timestamp,
title = payload.title,
cwd = payload.cwd,
source = payload.source,
originator = payload.originator,
model_provider = payload.model_provider,
branch = git.branch,
repository_url = git.repository_url,
file = file,
}
end
local function session_has_content(file)
local lines = vim.fn.readfile(file)
if not lines or #lines <= 1 then
return false
end
for i = 2, #lines do
local data = decode_json(lines[i])
if data then
if data.type and data.type ~= 'session_meta' then
return true
end
if data.payload and (data.payload.messages or data.payload.content) then
return true
end
elseif lines[i] ~= '' then
return true
end
end
return false
end
local function short_time(iso)
if not iso or iso == '' then return '' end
local t = iso:gsub('T', ' '):gsub('Z', '')
return t:sub(1, 16)
end
local function display_line(entry)
local time = short_time(entry.timestamp)
local id = entry.id or 'unknown'
local id_short = entry.id and entry.id:sub(1, 8) or ''
local title = entry.title or id
local cwd = entry.cwd or ''
local branch = entry.branch or ''
local source = entry.source or ''
if source ~= '' then
source = '[' .. source .. ']'
end
local parts = { time, title }
if id_short ~= '' then
table.insert(parts, '[' .. id_short .. ']')
end
if cwd ~= '' then
table.insert(parts, cwd)
end
if branch ~= '' then
table.insert(parts, branch)
end
if source ~= '' then
table.insert(parts, source)
end
return table.concat(parts, ' ')
end
local function load_entries(max_entries)
local config = require('codex').get_config()
local max_files = (config.history and config.history.max_files) or 1000
local files = session_files()
if max_files and #files > max_files then
table.sort(files, function(a, b)
local sa = vim.loop.fs_stat(a)
local sb = vim.loop.fs_stat(b)
local ma = sa and sa.mtime and sa.mtime.sec or 0
local mb = sb and sb.mtime and sb.mtime.sec or 0
return ma > mb
end)
local trimmed = {}
for i = 1, max_files do
trimmed[i] = files[i]
end
files = trimmed
end
local entries = {}
for _, file in ipairs(files) do
local entry = parse_session_meta(file)
local ok_content = true
if config.history and config.history.skip_empty then
ok_content = session_has_content(file)
end
if entry and entry.id and entry.timestamp and ok_content then
table.insert(entries, entry)
end
end
table.sort(entries, function(a, b)
return (a.timestamp or '') > (b.timestamp or '')
end)
if max_entries and #entries > max_entries then
local trimmed = {}
for i = 1, max_entries do
trimmed[i] = entries[i]
end
entries = trimmed
end
return entries
end
function M.latest_session_id()
local config = require('codex').get_config()
local list = load_entries(config.history and config.history.max_entries or 200)
if #list == 0 then return nil end
return list[1].id
end
local function open_telescope(entries)
local ok_telescope = pcall(require, 'telescope')
if not ok_telescope then
return false
end
local ok, pickers = pcall(require, 'telescope.pickers')
if not ok then
return false
end
local finders = require('telescope.finders')
local conf = require('telescope.config').values
local actions = require('telescope.actions')
local action_state = require('telescope.actions.state')
local state = require('codex').get_state()
local default_index = nil
if state and state.last_session_id then
for i, entry in ipairs(entries or {}) do
if entry.id == state.last_session_id then
default_index = i
break
end
end
end
pickers.new({}, {
prompt_title = 'Codex History',
finder = finders.new_table({
results = entries,
entry_maker = function(entry)
return {
value = entry,
display = display_line(entry),
ordinal = table.concat({
entry.timestamp or '',
entry.title or '',
entry.cwd or '',
entry.id or '',
}, ' '),
}
end,
}),
sorter = conf.generic_sorter({}),
default_selection_index = default_index,
attach_mappings = function(prompt_bufnr, map)
local function resume_selected()
local selection = action_state.get_selected_entry()
if not selection or not selection.value then
return
end
actions.close(prompt_bufnr)
local config = require('codex').get_config()
local opts = nil
if config.history and config.history.open_session_in_panel then
opts = { panel = true }
end
require('codex').resume(selection.value.id, opts)
end
local function close_picker()
actions.close(prompt_bufnr)
end
map('i', '<CR>', resume_selected)
map('n', '<CR>', resume_selected)
map('i', '<C-c>', close_picker)
map('n', '<C-c>', close_picker)
return true
end,
}):find()
return true
end
local function close_window(win)
if win and vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_close(win, true)
end
end
function M.build_buffer(entries)
local config = require('codex').get_config()
local list = entries or load_entries(config.history and config.history.max_entries or 200)
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_option(buf, 'bufhidden', 'wipe')
vim.api.nvim_buf_set_option(buf, 'buftype', 'nofile')
vim.api.nvim_buf_set_option(buf, 'swapfile', false)
vim.api.nvim_buf_set_option(buf, 'filetype', 'codex-history')
local header = {
'Codex History',
'Enter: resume q: close /: search',
'',
}
local lines = {}
for _, line in ipairs(header) do
table.insert(lines, line)
end
for _, entry in ipairs(list) do
table.insert(lines, display_line(entry))
end
if #list == 0 then
table.insert(lines, 'No Codex sessions found in ' .. codex_home() .. '/sessions')
end
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
vim.api.nvim_buf_set_option(buf, 'modifiable', false)
vim.b[buf].codex_history_entries = list
vim.b[buf].codex_history_header_len = #header
vim.keymap.set('n', 'q', function()
close_window(vim.api.nvim_get_current_win())
end, { buffer = buf, silent = true })
local config = require('codex').get_config()
if config.keymaps and config.keymaps.quit then
local quit_maps = config.keymaps.quit
if type(quit_maps) == 'string' then
quit_maps = { quit_maps }
end
for _, lhs in ipairs(quit_maps) do
vim.keymap.set('n', lhs, function()
require('codex').close()
end, { buffer = buf, silent = true })
end
end
if config.keymaps and config.keymaps.history then
vim.keymap.set('n', config.keymaps.history, function()
require('codex').toggle_history()
end, { buffer = buf, silent = true })
end
vim.keymap.set('n', '<CR>', function()
local win = vim.api.nvim_get_current_win()
local line = vim.api.nvim_win_get_cursor(win)[1]
local idx = line - (vim.b[buf].codex_history_header_len or 0)
local entry = (vim.b[buf].codex_history_entries or {})[idx]
if not entry then
return
end
local config = require('codex').get_config()
local opts = nil
if config.history and config.history.open_session_in_panel then
opts = { panel = true }
end
require('codex').resume(entry.id, opts)
end, { buffer = buf, silent = true })
return buf
end
function M.open_split(entries)
local buf = M.build_buffer(entries)
vim.cmd('botright split')
local win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(win, buf)
vim.api.nvim_win_set_height(win, math.min(15, vim.o.lines - 4))
return buf, win
end
function M.open(entries)
local config = require('codex').get_config()
local list = entries or load_entries(config.history and config.history.max_entries or 200)
if config.history and config.history.ui == 'telescope' then
if open_telescope(list) then
return
end
vim.notify('[codex.nvim] Telescope not available; falling back to buffer history view', vim.log.levels.WARN)
end
return M.open_split(list)
end
return M