11--- @module " codecompanion"
22
3+ --- @alias CodeCompanion.Tools.Tool.CmdFunc fun ( self : CodeCompanion.Tools.Tool , args : any , input : table , cb : function | nil )
4+
5+ local function should_use_new_api ()
6+ --- @type vim.Version | nil
7+ local cc_version = nil
8+ local _version = require (" codecompanion" ).version
9+ if type (_version ) == " function" then
10+ _version = _version ()
11+ end
12+ if type (_version ) == " string" then
13+ cc_version = vim .version .parse (_version )
14+ end
15+ return cc_version and cc_version .major >= 19
16+ end
17+
318return {
419 chat = {
520 --- @param subcommand VectorCode.CodeCompanion.SubCommand
@@ -8,14 +23,76 @@ return {
823 make_tool = function (subcommand , opts )
924 local has = require (" codecompanion" ).has
1025 if has ~= nil and has (" function-calling" ) then
11- return require (
26+ local tool_cb = require (
1227 string.format (" vectorcode.integrations.codecompanion.%s_tool" , subcommand )
1328 )(opts )
29+ local tool_transformer =
30+ require (" vectorcode.integrations" ).codecompanion .chat .transform
31+ tool_cb .cmds = tool_transformer .cmd (tool_cb .cmds )
32+ tool_cb .output = tool_transformer .output (tool_cb .output )
33+
34+ local require_approval = opts .requires_approval or opts .require_approval_before
35+ local tool_info = {
36+ description = string.format (" Run VectorCode %s tool" , subcommand ),
37+ opts = {
38+ requires_approval = require_approval ,
39+ require_approval_before = require_approval ,
40+ },
41+ }
42+ if should_use_new_api () then
43+ return vim .tbl_deep_extend (" force" , tool_info , tool_cb )
44+ else
45+ tool_info .callback = tool_cb
46+ return tool_info
47+ end
1448 else
1549 error (" Unsupported version of codecompanion!" )
1650 end
1751 end ,
1852
53+ --- compatibility shims for the new tool API.
54+ transform = {
55+ --- @param orig_cmd CodeCompanion.Tools.Tool.CmdFunc | CodeCompanion.Tools.Tool.CmdFunc[]
56+ cmd = function (orig_cmd )
57+ if type (orig_cmd ) == " table" then
58+ return vim
59+ .iter (orig_cmd )
60+ :map (require (" vectorcode.integrations" ).codecompanion .chat .transform .cmd )
61+ :totable ()
62+ end
63+
64+ return function (self , call_args , input , cb )
65+ if cb == nil then
66+ -- `cb` would be included in `input`
67+ return orig_cmd (self , call_args , input .input , input .output_cb )
68+ else
69+ return orig_cmd (self , call_args , input , cb )
70+ end
71+ end
72+ end ,
73+
74+ output = function (orig_cmd )
75+ if type (orig_cmd ) == " table" then
76+ local new_handlers = {}
77+ for k , v in pairs (orig_cmd ) do
78+ new_handlers [k ] =
79+ require (" vectorcode.integrations" ).codecompanion .chat .transform .output (v )
80+ end
81+ return new_handlers
82+ end
83+ return function (self , ...)
84+ local extra_args = { ... }
85+ if # extra_args == 2 then
86+ -- new API
87+ local output , _meta = unpack (extra_args )
88+ return orig_cmd (self , _meta .tools , _meta .cmd , output )
89+ else
90+ return orig_cmd (self , ... )
91+ end
92+ end
93+ end ,
94+ },
95+
1996 prompts = require (" vectorcode.integrations.codecompanion.prompts" ),
2097 },
2198}
0 commit comments