Events / Hooks
In order to enable a tighter integration between CodeCompanion and your Neovim config, the plugin fires events at various points during its lifecycle.
List of Events
The events that are fired from within the plugin are:
CodeCompanionACPSessionPre- Fired after ACP authentication completes but before a new session is established; allows subscribers to modify the connection (e.g. inject MCP servers) synchronouslyCodeCompanionChatACPModeChanged- Fired after the ACP mode has been changed in the chatCodeCompanionACPChatRestored- Fired after an ACP session has been restoredCodeCompanionChatCreated- Fired after a chat has been created for the first timeCodeCompanionChatOpened- Fired after a chat has been openedCodeCompanionChatClosed- Fired after a chat has been permanently closedCodeCompanionChatHidden- Fired after a chat has been hiddenCodeCompanionChatSubmitted- Fired after a chat has been submittedCodeCompanionChatDone- Fired after a chat has received the responseCodeCompanionChatStopped- Fired after a chat has been stoppedCodeCompanionChatCleared- Fired after a chat has been clearedCodeCompanionChatRestored- Fired after a chat has been restored to an editable state (e.g. whenon_before_submitprevents submission)CodeCompanionChatAdapter- Fired after the adapter has been set in the chatCodeCompanionChatModel- Fired after the model has been set in the chatCodeCompanionCLICreated- Fired after a CLI buffer has been created for the first timeCodeCompanionCLIOpened- Fired after a CLI buffer has been openedCodeCompanionCLIClosed- Fired after a CLI buffer has been closedCodeCompanionCLIHidden- Fired after a CLI buffer has been hiddenCodeCompanionCLISent- Fired after data has been sent to a CLI bufferCodeCompanionContextChanged- Fired when the context that a chat buffer follows, changesCodeCompanionToolsStarted- Fired when the tool system has been initiatedCodeCompanionToolsFinished- Fired when the tool system has finished running all toolsCodeCompanionToolAdded- Fired when a tool has been added to a chatCodeCompanionToolApprovalRequested- Fired when a tool is requesting approval to runCodeCompanionToolApprovalFinished- Fired when a user has actioned an approval requestCodeCompanionToolStarted- Fired when a tool has started executingCodeCompanionToolFinished- Fired when a tool has finished executingCodeCompanionInlineStarted- Fired at the start of the Inline interactionCodeCompanionInlineFinished- Fired at the end of the Inline interactionCodeCompanionMCPServerStart- Fired when an MCP server is startedCodeCompanionMCPServerReady- Fired when an MCP server is ready for requestsCodeCompanionMCPServerClosed- Fired when an MCP server is closedCodeCompanionMCPServerToolsLoaded- Fired when tools are loaded for an MCP serverCodeCompanionRequestStarted- Fired at the start of any API requestCodeCompanionRequestStreaming- Fired at the start of a streaming API requestCodeCompanionRequestFinished- Fired at the end of any API request
There are also events that can be utilized to trigger commands from within the plugin:
CodeCompanionChatRefreshCache- Used to refresh conditional elements in the chat buffer
Event Data
Each event also comes with a data payload. For example, with CodeCompanionRequestStarted:
lua
{
buf = 10,
data = {
adapter = {
formatted_name = "Copilot",
model = "o3-mini-2025-01-31",
name = "copilot"
},
bufnr = 10,
id = 6107753,
interaction = "chat"
},
event = "User",
file = "CodeCompanionRequestStarted",
group = 14,
id = 30,
match = "CodeCompanionRequestStarted"
}And the CodeCompanionRequestFinished also has a data.status value.
Consuming an Event
Events can be hooked into as follows:
lua
local group = vim.api.nvim_create_augroup("CodeCompanionHooks", {})
vim.api.nvim_create_autocmd({ "User" }, {
pattern = "CodeCompanionInline*",
group = group,
callback = function(request)
if request.match == "CodeCompanionInlineFinished" then
-- Format the buffer after the inline request has completed
require("conform").format({ bufnr = request.buf })
end
end,
})You can trigger an event with:
lua
vim.api.nvim_exec_autocmds("User", {
pattern = "CodeCompanionChatRefreshCache",
})