Skip to content

Commit 4d64eef

Browse files
aweis89pre-commit-ci[bot]deathbeam
authored
feat: Add callback parameter to M.ask function to allow for custom handling of response data
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tomas Slusny <slusnucky@gmail.com>
1 parent 17fbec0 commit 4d64eef

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ chat.ask("Explain how it works.", {
157157
selection = require("CopilotChat.select").buffer,
158158
})
159159

160+
-- Ask a question and do something with the response
161+
chat.ask("Show me something interesting", {
162+
callback = function(response)
163+
print("Response:", response)
164+
end,
165+
})
166+
160167
-- Get all available prompts (can be used for integrations like fzf/telescope)
161168
local prompts = chat.prompts()
162169

@@ -192,6 +199,7 @@ Also see [here](/lua/CopilotChat/config.lua):
192199
name = 'CopilotChat', -- Name to use in chat
193200
separator = '---', -- Separator to use in chat
194201
history_path = vim.fn.stdpath('data') .. '/copilotchat_history', -- Default path to stored history
202+
callback = nil, -- Callback to use when ask response is received
195203
-- default prompts
196204
prompts = {
197205
Explain = {

lua/CopilotChat/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ local select = require('CopilotChat.select')
5959
---@field name string?
6060
---@field separator string?
6161
---@field history_path string?
62+
---@field callback fun(response: string)?
6263
---@field prompts table<string, CopilotChat.config.prompt|string>?
6364
---@field selection nil|fun(source: CopilotChat.config.source):CopilotChat.config.selection?
6465
---@field window CopilotChat.config.window?
@@ -78,6 +79,7 @@ return {
7879
name = 'CopilotChat', -- Name to use in chat
7980
separator = '---', -- Separator to use in chat
8081
history_path = vim.fn.stdpath('data') .. '/copilotchat_history', -- Default path to stored history
82+
callback = nil, -- Callback to use when ask response is received
8183
-- default prompts
8284
prompts = {
8385
Explain = {

lua/CopilotChat/init.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,16 @@ function M.ask(prompt, config, source)
339339
model = config.model,
340340
temperature = config.temperature,
341341
on_error = on_error,
342-
on_done = function(_, token_count)
342+
on_done = function(response, token_count)
343343
vim.schedule(function()
344344
if tiktoken.available() and token_count and token_count > 0 then
345345
append('\n\n' .. token_count .. ' tokens used')
346346
end
347347
append('\n\n' .. config.separator .. '\n\n')
348348
state.chat:finish()
349+
if config.callback then
350+
config.callback(response)
351+
end
349352
end)
350353
end,
351354
on_progress = function(token)

0 commit comments

Comments
 (0)