Skip to content

Commit bc2bc12

Browse files
olimorriscleong14
authored andcommitted
feat(chat): easier to add refs from external sources (olimorris#960)
1 parent 5e65460 commit bc2bc12

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

doc/configuration/chat-buffer.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,31 @@ require("codecompanion").setup({
8989
strategies = {
9090
chat = {
9191
slash_commands = {
92-
["mycmd"] = {
93-
description = "My fancy new command",
92+
["git_files"] = {
93+
description = "List git files",
9494
---@param chat CodeCompanion.Chat
9595
callback = function(chat)
96-
return chat:add_buf_message({ content = "Just writing to the chat buffer" })
96+
local handle = io.popen("git ls-files")
97+
if handle ~= nil then
98+
local result = handle:read("*a")
99+
handle:close()
100+
chat:add_reference({ content = result }, "git", "<git_files>")
101+
else
102+
return vim.notify("No git files available", vim.log.levels.INFO, { title = "CodeCompanion" })
103+
end
97104
end,
105+
opts = {
106+
contains_code = false,
107+
},
98108
},
99109
},
100110
},
101111
},
102112
})
103113
```
104114

115+
Credit to [@lazymaniac](https://github.com/lazymaniac) for the [inspiration](https://github.com/olimorris/codecompanion.nvim/discussions/958).
116+
105117
> [!NOTE]
106118
> You can also point the callback to a lua file that resides within your own configuration
107119

lua/codecompanion/strategies/chat/init.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,18 @@ function Chat:done(output)
841841
self:reset()
842842
end
843843

844+
---Add a reference to the chat buffer (Useful for user's adding custom Slash Commands)
845+
---@param data { role: string, content: string }
846+
---@param source string
847+
---@param id string
848+
---@param opts? table Options for the message
849+
function Chat:add_reference(data, source, id, opts)
850+
opts = opts or { reference = id, visible = false }
851+
852+
self.references:add({ source = source, id = id })
853+
self:add_message(data, opts)
854+
end
855+
844856
---Reconcile the references table to the references in the chat buffer
845857
---@return nil
846858
function Chat:check_references()

0 commit comments

Comments
 (0)