@@ -173,6 +173,8 @@ M.setup = function(opts)
173
173
for hook , _ in pairs (M .hooks ) do
174
174
M .helpers .create_user_command (M .config .cmd_prefix .. hook , function (params )
175
175
if M .hooks [hook ] ~= nil then
176
+ M .refresh_state ()
177
+ M .logger .debug (" running hook: " .. hook )
176
178
return M .hooks [hook ](M , params )
177
179
end
178
180
M .logger .error (" The hook '" .. hook .. " ' does not exist." )
@@ -191,6 +193,7 @@ M.setup = function(opts)
191
193
for cmd , _ in pairs (M .cmd ) do
192
194
if M .hooks [cmd ] == nil then
193
195
M .helpers .create_user_command (M .config .cmd_prefix .. cmd , function (params )
196
+ M .logger .debug (" running command: " .. cmd )
194
197
M .refresh_state ()
195
198
M .cmd [cmd ](params )
196
199
end , completions [cmd ])
@@ -211,22 +214,22 @@ M.refresh_state = function(update)
211
214
local state_file = M .config .state_dir .. " /state.json"
212
215
update = update or {}
213
216
214
- local state = {}
217
+ local old_state = vim .deepcopy (M ._state )
218
+
219
+ local disk_state = {}
215
220
if vim .fn .filereadable (state_file ) ~= 0 then
216
- state = M .helpers .file_to_table (state_file ) or {}
221
+ disk_state = M .helpers .file_to_table (state_file ) or {}
217
222
end
218
223
219
- M .logger .debug (" loaded state: " .. vim .inspect (state ))
220
-
221
- if not state .updated then
224
+ if not disk_state .updated then
222
225
local last = M .config .chat_dir .. " /last.md"
223
226
if vim .fn .filereadable (last ) == 1 then
224
227
os.remove (last )
225
228
end
226
229
end
227
230
228
- if not M ._state .updated or (state .updated and M ._state .updated < state .updated ) then
229
- M ._state = state
231
+ if not M ._state .updated or (disk_state .updated and M ._state .updated < disk_state .updated ) then
232
+ M ._state = vim . deepcopy ( disk_state )
230
233
end
231
234
M ._state .updated = os.time ()
232
235
@@ -246,7 +249,20 @@ M.refresh_state = function(update)
246
249
M ._state .last_chat = nil
247
250
end
248
251
249
- M .logger .debug (" stored state: " .. vim .inspect (M ._state ))
252
+ for k , _ in pairs (M ._state ) do
253
+ if M ._state [k ] ~= old_state [k ] or M ._state [k ] ~= disk_state [k ] then
254
+ M .logger .debug (
255
+ string.format (
256
+ " state[%s]: disk=%s old=%s new=%s" ,
257
+ k ,
258
+ vim .inspect (disk_state [k ]),
259
+ vim .inspect (old_state [k ]),
260
+ vim .inspect (M ._state [k ])
261
+ )
262
+ )
263
+ end
264
+ end
265
+
250
266
M .helpers .table_to_file (M ._state , state_file )
251
267
252
268
M .prepare_commands ()
@@ -1498,11 +1514,9 @@ M.cmd.NextAgent = function()
1498
1514
1499
1515
local set_agent = function (agent_name )
1500
1516
if is_chat then
1501
- M ._state .chat_agent = agent_name
1502
1517
M .refresh_state ({ chat_agent = agent_name })
1503
1518
M .logger .info (" Chat agent: " .. M ._state .chat_agent )
1504
1519
else
1505
- M ._state .command_agent = agent_name
1506
1520
M .refresh_state ({ command_agent = agent_name })
1507
1521
M .logger .info (" Command agent: " .. M ._state .command_agent )
1508
1522
end
@@ -1530,7 +1544,7 @@ M.get_command_agent = function(name)
1530
1544
local model = M .agents [name ].model
1531
1545
local system_prompt = M .agents [name ].system_prompt
1532
1546
local provider = M .agents [name ].provider
1533
- M .logger .debug (" Getting command agent: " .. name )
1547
+ M .logger .debug (" getting command agent: " .. name )
1534
1548
return {
1535
1549
cmd_prefix = cmd_prefix ,
1536
1550
name = name ,
@@ -1553,7 +1567,7 @@ M.get_chat_agent = function(name)
1553
1567
local model = M .agents [name ].model
1554
1568
local system_prompt = M .agents [name ].system_prompt
1555
1569
local provider = M .agents [name ].provider
1556
- M .logger .debug (" Getting chat agent: " .. name )
1570
+ M .logger .debug (" getting chat agent: " .. name )
1557
1571
return {
1558
1572
cmd_prefix = cmd_prefix ,
1559
1573
name = name ,
0 commit comments