Skip to content

Commit 649c860

Browse files
feat: Add on_create_session_response hook for ACP session creation feedback (#186)
I'm currently using this hook to reset some state whenever a session is created. For example, I'm storing the context used/total via the `on_session_update` hook, but if a new session is created, those values need to be reset. I also considered a `on_new_session` hook as an alternative, however I feel like tying the hooks to the ACP events makes more sense, as it's closer to the protocol and thus doesn't have to be adapted as much when the internals of agentic change. This is based on top of #161 since for my use-case, the state I'm reseting is displayed in the header, so the header needs to be re-rendered --------- Signed-off-by: Chance Zibolski <chance.zibolski@gmail.com> Co-authored-by: Carlos Gomes <carlos-algms@users.noreply.github.com>
1 parent 29a929f commit 649c860

5 files changed

Lines changed: 45 additions & 4 deletions

File tree

.editorconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ trim_trailing_whitespace = false
1515

1616
[*.lua]
1717
indent_size = 4
18+
insert_final_newline = true
1819

1920
[Makefile]
20-
indent_style = tab
21+
indent_style = tab

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,21 @@ integrating with other plugins.
839839
--- @type agentic.PartialUserConfig
840840
opts = {
841841
hooks = {
842+
-- Called when a new ACP session is created (or fails to create)
843+
on_create_session_response = function(data)
844+
-- data.session_id: string|nil - The ACP session ID (nil if err is set)
845+
-- data.tab_page_id: number - The Neovim tabpage ID
846+
-- data.response: table|nil - The ACP session creation response (nil if err is set)
847+
-- data.err: table|nil - Error details if session creation failed
848+
if data.response then
849+
vim.notify("New session: " .. data.response.sessionId)
850+
end
851+
852+
if vim.api.nvim_tabpage_is_valid(data.tab_page_id) then
853+
vim.t[data.tab_page_id].agentic_usage = nil
854+
end
855+
end,
856+
842857
-- Called when the user submits a prompt
843858
on_prompt_submit = function(data)
844859
-- data.prompt: string - The user's prompt text

doc/agentic.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,12 @@ Event hooks ~
601601
--- @type agentic.PartialUserConfig
602602
opts = {
603603
hooks = {
604+
on_create_session_response = function(data)
605+
-- data.response: table|nil - The ACP session creation response (nil if err is set)
606+
-- data.err: table|nil - Error details if session creation failed
607+
-- data.session_id: string
608+
-- data.tab_page_id: number
609+
end,
604610
on_prompt_submit = function(data)
605611
-- data.prompt: string
606612
-- data.session_id: string
@@ -934,4 +940,4 @@ Persistent ACP message log:
934940
`~/.cache/nvim/agentic_debug.log`
935941

936942
==============================================================================
937-
vim:tw=78:ts=8:ft=help:norl:
943+
vim:tw=78:ts=8:ft=help:norl:

lua/agentic/config_default.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
---
2424
--- @alias agentic.UserConfig.Headers table<agentic.ui.ChatWidget.PanelNames, agentic.ui.ChatWidget.HeaderParts|agentic.UserConfig.HeaderRenderFn|nil>
2525

26+
--- Data passed to the on_create_session_response hook
27+
--- @class agentic.UserConfig.CreateSessionResponseData
28+
--- @field session_id string|nil
29+
--- @field tab_page_id number
30+
--- @field response agentic.acp.SessionCreationResponse|nil
31+
--- @field err? agentic.acp.ACPError
32+
2633
--- Data passed to the on_prompt_submit hook
2734
--- @class agentic.UserConfig.PromptSubmitData
2835
--- @field prompt string The user's prompt text
@@ -164,6 +171,7 @@
164171
--- @field tool_calls agentic.UserConfig.Folding.ToolCalls
165172

166173
--- @class agentic.UserConfig.Hooks
174+
--- @field on_create_session_response? fun(data: agentic.UserConfig.CreateSessionResponseData): nil
167175
--- @field on_prompt_submit? fun(data: agentic.UserConfig.PromptSubmitData): nil
168176
--- @field on_response_complete? fun(data: agentic.UserConfig.ResponseCompleteData): nil
169177
--- @field on_session_update? fun(data: agentic.UserConfig.SessionUpdateData): nil
@@ -470,6 +478,7 @@ local ConfigDefault = {
470478
},
471479

472480
hooks = {
481+
on_create_session_response = nil,
473482
on_prompt_submit = nil,
474483
on_response_complete = nil,
475484
on_session_update = nil,

lua/agentic/session_manager.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ local FILE_MUTATING_KINDS = {
2727
}
2828

2929
--- Safely invoke a user-configured hook
30-
--- @param hook_name "on_prompt_submit" | "on_response_complete" | "on_session_update" | "on_file_edit"
31-
--- @param data agentic.UserConfig.PromptSubmitData | agentic.UserConfig.ResponseCompleteData | agentic.UserConfig.SessionUpdateData | agentic.UserConfig.FileEditData
30+
--- @param hook_name "on_create_session_response" | "on_prompt_submit" | "on_response_complete" | "on_session_update" | "on_file_edit"
31+
--- @param data agentic.UserConfig.CreateSessionResponseData | agentic.UserConfig.PromptSubmitData | agentic.UserConfig.ResponseCompleteData | agentic.UserConfig.SessionUpdateData | agentic.UserConfig.FileEditData
3232
function P.invoke_hook(hook_name, data)
3333
local hook = Config.hooks and Config.hooks[hook_name]
3434

@@ -953,6 +953,16 @@ function SessionManager:new_session(opts)
953953
self.agent:create_session(handlers, function(response, err)
954954
self.status_animation:stop()
955955

956+
--- @type agentic.UserConfig.CreateSessionResponseData
957+
local hook_data = {
958+
session_id = response and response.sessionId,
959+
tab_page_id = self.tab_page_id,
960+
response = response,
961+
err = err,
962+
}
963+
964+
P.invoke_hook("on_create_session_response", hook_data)
965+
956966
if err or not response then
957967
-- no log here, already logged in create_session
958968
self.session_id = nil

0 commit comments

Comments
 (0)