@@ -190,7 +190,10 @@ M.setup = function(opts)
190
190
-- register default commands
191
191
for cmd , _ in pairs (M .cmd ) do
192
192
if M .hooks [cmd ] == nil then
193
- M .helpers .create_user_command (M .config .cmd_prefix .. cmd , M .cmd [cmd ], completions [cmd ])
193
+ M .helpers .create_user_command (M .config .cmd_prefix .. cmd , function (params )
194
+ M .refresh_state ()
195
+ M .cmd [cmd ](params )
196
+ end , completions [cmd ])
194
197
end
195
198
end
196
199
@@ -213,16 +216,30 @@ M.refresh_state = function()
213
216
214
217
M .logger .debug (" loaded state: " .. vim .inspect (state ))
215
218
216
- M ._state .chat_agent = M ._state .chat_agent or state .chat_agent or nil
217
- if M ._state .chat_agent == nil or not M .agents [M ._state .chat_agent ] then
219
+ if not state .updated then
220
+ local last = M .config .chat_dir .. " /last.md"
221
+ if vim .fn .filereadable (last ) == 1 then
222
+ os.remove (last )
223
+ end
224
+ end
225
+
226
+ if not M ._state .updated or (state .updated and M ._state .updated < state .updated ) then
227
+ M ._state = state
228
+ end
229
+ M ._state .updated = os.time ()
230
+
231
+ if not M ._state .chat_agent or not M .agents [M ._state .chat_agent ] then
218
232
M ._state .chat_agent = M ._chat_agents [1 ]
219
233
end
220
234
221
- M ._state .command_agent = M ._state .command_agent or state .command_agent or nil
222
- if not M ._state .command_agent == nil or not M .agents [M ._state .command_agent ] then
235
+ if not M ._state .command_agent or not M .agents [M ._state .command_agent ] then
223
236
M ._state .command_agent = M ._command_agents [1 ]
224
237
end
225
238
239
+ if M ._state .last_chat and vim .fn .filereadable (M ._state .last_chat ) == 0 then
240
+ M ._state .last_chat = nil
241
+ end
242
+
226
243
M .logger .debug (" stored state: " .. vim .inspect (M ._state ))
227
244
M .helpers .table_to_file (M ._state , state_file )
228
245
404
421
M .not_chat = function (buf , file_name )
405
422
file_name = vim .fn .resolve (file_name )
406
423
local chat_dir = vim .fn .resolve (M .config .chat_dir )
407
- if vim .fn .has (" win32" ) == 1 then
408
- file_name = file_name :gsub (" \\ " , " /" )
409
- chat_dir = chat_dir :gsub (" \\ " , " /" )
410
- end
424
+
411
425
if not M .helpers .starts_with (file_name , chat_dir ) then
412
426
return " resolved file (" .. file_name .. " ) not in chat dir (" .. chat_dir .. " )"
413
427
end
@@ -524,13 +538,8 @@ M.prep_chat = function(buf, file_name)
524
538
vim .fn .matchadd (" Conceal" , [[ ^- role: .[^\\]*\zs\\.*\ze]] , 10 , - 1 , { conceal = " …" })
525
539
end
526
540
527
- -- make last.md a symlink to the last opened chat file
528
- local last = M .config .chat_dir .. " /last.md"
529
- if file_name ~= last and vim .fn .has (" win32" ) == 1 then
530
- os.execute (" pwsh -Noprofile -c New-Item -Force -ItemType SymbolicLink -Path " .. last .. " -Target " .. file_name )
531
- elseif file_name ~= last then
532
- os.execute (" ln -sf " .. file_name .. " " .. last )
533
- end
541
+ M ._state .last_chat = file_name
542
+ M .refresh_state ()
534
543
end
535
544
536
545
M .buf_handler = function ()
@@ -810,10 +819,8 @@ M.cmd.ChatToggle = function(params, system_prompt, agent)
810
819
811
820
-- if the range is 2, we want to create a new chat file with the selection
812
821
if params .range ~= 2 then
813
- -- check if last.md chat file exists and open it
814
- local last = M .config .chat_dir .. " /last.md"
815
- if vim .fn .filereadable (last ) == 1 then
816
- -- resolve symlink
822
+ local last = M ._state .last_chat
823
+ if last and vim .fn .filereadable (last ) == 1 then
817
824
last = vim .fn .resolve (last )
818
825
M .open_buf (last , M .resolve_buf_target (params ), M ._toggle_kind .chat , true )
819
826
return
@@ -833,10 +840,9 @@ M.cmd.ChatPaste = function(params)
833
840
-- get current buffer
834
841
local cbuf = vim .api .nvim_get_current_buf ()
835
842
836
- local last = M .config .chat_dir .. " /last.md"
837
-
838
843
-- make new chat if last doesn't exist
839
- if vim .fn .filereadable (last ) ~= 1 then
844
+ local last = M ._state .last_chat
845
+ if not last or vim .fn .filereadable (last ) ~= 1 then
840
846
-- skip rest since new chat will handle snippet on it's own
841
847
M .cmd .ChatNew (params , nil , nil )
842
848
return
0 commit comments