lua/codecompanion/prompt_library/builtins/upgrades/tools.md
I need you to update my custom CodeCompanion.nvim tool to the new tools API. The signatures have changed as follows:
cmds functionsSynchronous tools (return a result table):
-- OLD:
function(self, args, input)
-- NEW (unchanged for sync):
function(self, args, opts)
-- `opts` contains: { input = any, output_cb = fun(msg: table) }
-- For sync tools that don't use input or output_cb, the signature change is cosmetic.
-- Return: { status = "success"|"error", data = string }
Asynchronous tools (use a callback):
-- OLD:
function(self, args, _, cb)
cb({ status = "success", data = result })
end
-- NEW:
function(self, args, opts)
local cb = opts.output_cb
cb({ status = "success", data = result })
end
Consecutive cmds (chained functions that pass data forward):
-- OLD: second function received previous output as 3rd positional arg
function(self, args, input)
-- NEW: previous output is in opts.input
function(self, args, opts)
local input = opts.input
end
output callbackssuccess:
-- OLD:
success = function(self, tools, cmd, stdout)
local chat = tools.chat
-- NEW:
success = function(self, stdout, meta)
local chat = meta.tools.chat
-- meta.cmd is also available if needed
error:
-- OLD:
error = function(self, tools, cmd, stderr)
local chat = tools.chat
-- NEW:
error = function(self, stderr, meta)
local chat = meta.tools.chat
rejected:
-- OLD:
rejected = function(self, tools, cmd, opts)
helpers.rejected(self, { tools = tools, message = "..." })
-- NEW:
rejected = function(self, meta)
-- meta already contains { tools, cmd, opts }
local message = "The user rejected ..."
meta = vim.tbl_extend("force", { message = message }, meta or {})
helpers.rejected(self, meta)
prompt:
-- OLD:
prompt = function(self, tools)
-- NEW:
prompt = function(self, meta)
-- meta contains { tools }
cmd_string:
-- OLD:
cmd_string = function(self, tools)
-- NEW:
cmd_string = function(self, meta)
-- meta contains { tools }
cancelled:
-- OLD:
cancelled = function(self, tools, cmd)
local chat = tools.chat
-- NEW:
cancelled = function(self, meta)
local chat = meta.tools.chat
-- meta.cmd is also available
handlers callbackssetup:
-- OLD:
setup = function(self, tools)
-- NEW:
setup = function(self, meta)
-- meta contains { tools }
on_exit:
-- OLD:
on_exit = function(self, tools)
-- NEW:
on_exit = function(self, meta)
-- meta contains { tools }
prompt_condition:
-- OLD:
prompt_condition = function(self, tools)
-- NEW:
prompt_condition = function(self, meta)
-- meta contains { tools }
The core change is: positional arguments have been replaced with structured tables.
cmds functions: (self, args, opts) where opts = { input, output_cb }output.* callbacks: (self, stdout_or_stderr, meta) where meta = { tools, cmd }output.rejected: (self, meta) where meta = { tools, cmd, opts }output.prompt / output.cmd_string: (self, meta) where meta = { tools }handlers.*: (self, meta) where meta = { tools }helpers.rejected: (self, opts) where opts = { tools, message, reason }Anywhere you previously accessed tools.chat, you now access meta.tools.chat.
Please update my tool below to use the new API signatures. Only change the function signatures and how arguments are accessed — do not alter any business logic.
My tool can be found in #{buffer}.