Skip to content

Events ​

CodeCompanion fires User autocmds at points in its lifecycle, so your config can react to a chat opening, a request finishing or a file being edited.

For hooks that can change a chat's state, such as on_before_submit and on_tool_output, see Callbacks.

Consuming an Event ​

To format a buffer once you've kept an inline edit:

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 == "CodeCompanionInlineAccepted" then
      require("conform").format({ bufnr = request.buf })
    end
  end,
})

Event Data ​

Each event carries a data payload. For 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"
}

CodeCompanionRequestFinished adds a status, such as "success", "error" or "cancelled".

Triggering an Event ​

To make the chat buffer re-check which tools and slash commands are enabled:

lua
vim.api.nvim_exec_autocmds("User", {
  pattern = "CodeCompanionChatRefreshCache",
})

List of Events ​

Chat buffer

EventFired
CodeCompanionChatCreatedAfter a chat is created for the first time
CodeCompanionChatOpenedAfter a chat is opened
CodeCompanionChatHiddenAfter a chat is hidden
CodeCompanionChatClosedAfter a chat is closed for good
CodeCompanionChatSubmittedAfter a message is sent
CodeCompanionChatDoneAfter a response is received
CodeCompanionChatStoppedAfter a request is stopped
CodeCompanionChatClearedAfter a chat is cleared
CodeCompanionChatRestoredAfter a chat is made editable again, such as when on_before_submit blocks a message
CodeCompanionChatCompactingWhen a chat starts compacting its messages
CodeCompanionChatAdapterAfter the adapter is set
CodeCompanionChatModelAfter the model is set
CodeCompanionChatTitleAfter the title is set, with the title in the payload
CodeCompanionChatToolAddedAfter a tool is added, with the tool in the payload

Sessions

EventFired
CodeCompanionChatSessionSavedAfter a chat is saved to disk, with its slug in the payload
CodeCompanionChatSessionRestoredAfter a session is restored, with its stem in the payload
CodeCompanionChatSessionsChangedAfter a session is written or deleted

ACP

EventFired
CodeCompanionACPConnectedAfter the connection is authenticated and ready
CodeCompanionACPSessionPreAfter authentication, before a session starts, so you can change the connection, such as adding MCP servers
CodeCompanionACPSessionPostAfter a session starts
CodeCompanionACPChatRestoredAfter a session is restored
CodeCompanionACPCommandsUpdateAfter an agent's commands are loaded, with the commands in the payload
CodeCompanionChatACPConfigChangedAfter an agent's configuration options change, with the config_options in the payload

CLI

EventFired
CodeCompanionCLICreatedAfter a CLI buffer is created for the first time
CodeCompanionCLIOpenedAfter a CLI buffer is opened
CodeCompanionCLIHiddenAfter a CLI buffer is hidden
CodeCompanionCLIClosedAfter a CLI buffer is closed
CodeCompanionCLISentAfter text is sent to a CLI buffer
CodeCompanionCLISubmittedWhen the agent accepts a prompt, however it was typed
CodeCompanionCLIDoneWhen the agent finishes a turn
CodeCompanionCLIApprovalRequestedWhen the agent is waiting on you, with a message in the payload
CodeCompanionCLIApprovalFinishedWhen the agent resumes after waiting

The last four need agent hooks.

Requests

EventFired
CodeCompanionRequestStartedAt the start of a request, or a CLI agent's turn when hooks are set up
CodeCompanionRequestStreamingAt the start of a streaming request
CodeCompanionRequestFinishedAt the end of a request, or a CLI agent's turn when hooks are set up
CodeCompanionInlineStartedAt the start of an inline request
CodeCompanionInlineFinishedAt the end of an inline request, before its edits are reviewed
CodeCompanionInlineAcceptedWhen you keep any of an inline request's edits

Tools

EventFired
CodeCompanionToolsStartedWhen a batch of tools starts
CodeCompanionToolsFinishedWhen a batch of tools finishes, is stopped or is cancelled
CodeCompanionToolStartedWhen a tool starts
CodeCompanionToolFinishedWhen a tool finishes
CodeCompanionToolApprovalRequestedWhen a tool asks for approval
CodeCompanionToolApprovalFinishedWhen you approve or reject a tool
CodeCompanionToolQuestionAskedWhen a tool asks you a question
CodeCompanionToolQuestionAnsweredWhen you answer or skip a question
CodeCompanionToolsJudgeStartedWhen the judge starts vetting a tool call
CodeCompanionToolsJudgeFinishedWhen the judge returns its verdict

Files and diffs

EventFired
CodeCompanionFileEditedAfter a file is edited or created, with the path and the tool that changed it in the payload
CodeCompanionDiffHunkChangedAfter moving to the next or previous hunk in a diff

MCP

EventFired
CodeCompanionMCPServerStartWhen a server starts
CodeCompanionMCPServerReadyWhen a server is ready for requests
CodeCompanionMCPServerToolsLoadedWhen a server's tools are loaded
CodeCompanionMCPServerClosedWhen a server closes

Released under the Apache-2.0 License.