Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions doc/configuration/chat-buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,31 @@ require("codecompanion").setup({
strategies = {
chat = {
slash_commands = {
["mycmd"] = {
description = "My fancy new command",
["git_files"] = {
description = "List git files",
---@param chat CodeCompanion.Chat
callback = function(chat)
return chat:add_buf_message({ content = "Just writing to the chat buffer" })
local handle = io.popen("git ls-files")
if handle ~= nil then
local result = handle:read("*a")
handle:close()
chat:add_reference({ content = result }, "git", "<git_files>")
else
return vim.notify("No git files available", vim.log.levels.INFO, { title = "CodeCompanion" })
end
end,
opts = {
contains_code = false,
},
},
},
},
},
})
```

Credit to [@lazymaniac](https://github.com/lazymaniac) for the [inspiration](https://github.com/olimorris/codecompanion.nvim/discussions/958).

> [!NOTE]
> You can also point the callback to a lua file that resides within your own configuration

Expand Down
12 changes: 12 additions & 0 deletions lua/codecompanion/strategies/chat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,18 @@ function Chat:done(output)
self:reset()
end

---Add a reference to the chat buffer (Useful for user's adding custom Slash Commands)
---@param data { role: string, content: string }
---@param source string
---@param id string
---@param opts? table Options for the message
function Chat:add_reference(data, source, id, opts)
opts = opts or { reference = id, visible = false }

self.references:add({ source = source, id = id })
self:add_message(data, opts)
end

---Reconcile the references table to the references in the chat buffer
---@return nil
function Chat:check_references()
Expand Down