Skip to content

Commit 676b03a

Browse files
committed
feat: better state logging
1 parent 751b49b commit 676b03a

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

lua/gp/init.lua

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ M.setup = function(opts)
173173
for hook, _ in pairs(M.hooks) do
174174
M.helpers.create_user_command(M.config.cmd_prefix .. hook, function(params)
175175
if M.hooks[hook] ~= nil then
176+
M.refresh_state()
177+
M.logger.debug("running hook: " .. hook)
176178
return M.hooks[hook](M, params)
177179
end
178180
M.logger.error("The hook '" .. hook .. "' does not exist.")
@@ -191,6 +193,7 @@ M.setup = function(opts)
191193
for cmd, _ in pairs(M.cmd) do
192194
if M.hooks[cmd] == nil then
193195
M.helpers.create_user_command(M.config.cmd_prefix .. cmd, function(params)
196+
M.logger.debug("running command: " .. cmd)
194197
M.refresh_state()
195198
M.cmd[cmd](params)
196199
end, completions[cmd])
@@ -211,22 +214,22 @@ M.refresh_state = function(update)
211214
local state_file = M.config.state_dir .. "/state.json"
212215
update = update or {}
213216

214-
local state = {}
217+
local old_state = vim.deepcopy(M._state)
218+
219+
local disk_state = {}
215220
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 {}
217222
end
218223

219-
M.logger.debug("loaded state: " .. vim.inspect(state))
220-
221-
if not state.updated then
224+
if not disk_state.updated then
222225
local last = M.config.chat_dir .. "/last.md"
223226
if vim.fn.filereadable(last) == 1 then
224227
os.remove(last)
225228
end
226229
end
227230

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)
230233
end
231234
M._state.updated = os.time()
232235

@@ -246,7 +249,20 @@ M.refresh_state = function(update)
246249
M._state.last_chat = nil
247250
end
248251

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+
250266
M.helpers.table_to_file(M._state, state_file)
251267

252268
M.prepare_commands()
@@ -1498,11 +1514,9 @@ M.cmd.NextAgent = function()
14981514

14991515
local set_agent = function(agent_name)
15001516
if is_chat then
1501-
M._state.chat_agent = agent_name
15021517
M.refresh_state({ chat_agent = agent_name })
15031518
M.logger.info("Chat agent: " .. M._state.chat_agent)
15041519
else
1505-
M._state.command_agent = agent_name
15061520
M.refresh_state({ command_agent = agent_name })
15071521
M.logger.info("Command agent: " .. M._state.command_agent)
15081522
end
@@ -1530,7 +1544,7 @@ M.get_command_agent = function(name)
15301544
local model = M.agents[name].model
15311545
local system_prompt = M.agents[name].system_prompt
15321546
local provider = M.agents[name].provider
1533-
M.logger.debug("Getting command agent: " .. name)
1547+
M.logger.debug("getting command agent: " .. name)
15341548
return {
15351549
cmd_prefix = cmd_prefix,
15361550
name = name,
@@ -1553,7 +1567,7 @@ M.get_chat_agent = function(name)
15531567
local model = M.agents[name].model
15541568
local system_prompt = M.agents[name].system_prompt
15551569
local provider = M.agents[name].provider
1556-
M.logger.debug("Getting chat agent: " .. name)
1570+
M.logger.debug("getting chat agent: " .. name)
15571571
return {
15581572
cmd_prefix = cmd_prefix,
15591573
name = name,

0 commit comments

Comments
 (0)