Back to Codecompanion Nvim

The Chat Buffer

.codecompanion/chat.md

19.18.04.7 KB
Original Source

The Chat Buffer

The chat buffer is a Neovim buffer which allows a user to interact with an LLM. The buffer is formatted as Markdown with a user's content residing under a H2 header. The user types their message, saves the buffer and the plugin then uses Tree-sitter to parse the buffer, extracting the contents and sending to an adapter which connects to the user's chosen LLM. The response back from the LLM is streamed into the buffer under another H2 header. The user is then free to respond back to the LLM.

The file below is the entry point for the chat strategy. All methods directly relating to the chat buffer reside here.

@./lua/codecompanion/interactions/chat/init.lua

Message Stack

Messages in the chat buffer are lua table objects as seen. They contain the role of the message (user, assistant, system), the content of the message, and any additional metadata such as visibility and sometimes type. The latter are important when data is being written to the buffer with add_buf_message as they allow the chat ui builder pattern to determine the type of message that it's receiving and therefore determine how to format it. In the example shared in below, you can see how a user has prompted the LLM and the LLM's response:

lua
{ {
    _meta = {
      cycle = 1,
      id = 708950413,
      estimated_tokens = 20,
      tag = "system_prompt_from_config",
    },
    opts = {
      visible = false
    },
    role = "system"
    content = "A system prompt",
  }, {
    _meta = {
      cycle = 1,
      estimated_tokens = 20,
      id = 533315931,
      sent = true
    },
    opts = {
      visible = true
    },
    role = "user"
    content = "Are you working? Sharing a file with you",
  },
 {
    _meta = {
      cycle = 1,
      estimated_tokens = 3556,
      id = 1048633318,
      index = 5,
      sent = true,
      source = "editor_context",
      tag = "file"
    },
    content = "<attachment filepath=\"/Users/Oli/some_path/some_file.lua\">An example file</attachment>",
    context = {
      id = "<file>some_path/some_file.lua</file>",
      path = "/Users/Oli/some_path/some_file.lua",
    },
    opts = {
      visible = false
    },
    role = "user"
  },
  {
    _meta = {
      cycle = 1,
      id = 1141409506,
    },
    opts = {
      visible = true
    },
    role = "llm"
    content = "Yes, I am active and ready to assist you with programming tasks, code explanations, reviews, or Neovim-related questions. What would you like to do next?",
  } }

Message Tags

Messages may carry a _meta.tag value identifying the origin or kind of the message. Tags are how downstream code (adapters, compaction, system prompt management, etc.) recognises and acts on specific message types. The full set of tags is centralised at lua/codecompanion/interactions/shared/tags.lua — refer to that module rather than repeating the string literals in code.

TagSet byRead by
buffershared/slash_commands/buffer.lua, shared/editor_context/buffer.lua, shared/editor_context/buffers.luaadapters/acp/helpers.lua
compact_summarychat/context_management/compaction.luachat/context_management/compaction.lua (re-run replacement)
diagnosticsshared/editor_context/diagnostics.lua
diffshared/editor_context/diff.lua
editor_contextlegacy editor_context emitterschat/slash_commands/builtin/compact.lua
fileshared/slash_commands/file.luaadapters/acp/helpers.lua, chat/slash_commands/builtin/compact.lua
from_custom_promptinteractions/init.lua (custom prompt library entries)
imagechat/init.lua:add_image_messageevery HTTP adapter, adapters/acp/helpers.lua, title generation
messagesshared/editor_context/messages.lua
quickfixshared/editor_context/quickfix.lua
rulesshared/rules/helpers.luachat/slash_commands/builtin/compact.lua, title generation
selectionshared/editor_context/selection.lua
system_prompt_from_configchat/init.lua:set_system_promptchat/tool_registry.lua, title generation
terminalshared/editor_context/terminal.lua
toolchat/tool_registry.luachat/tool_registry.lua (group tear-down)
tool_outputadapters (via format_response)chat/ui/init.lua (rendering spacing)
tool_system_promptchat/tool_registry.lua:add_tool_system_prompt
viewportshared/editor_context/viewport.lua

Inline-only tags (system_tag, visual) live entirely inside interactions/inline/ and are not part of the shared module.

String values are stable — they appear in stored chats and are read by external adapters. Adding a new tag is safe; renaming a value is a breaking change.