Skip to content

Commit bf6b95f

Browse files
committed
feat(codecompanion): add format_action
1 parent 15d4901 commit bf6b95f

3 files changed

Lines changed: 36 additions & 7 deletions

File tree

doc/extensions/codecompanion.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ require("codecompanion").setup({
2727
show_result_in_chat = true, -- Show mcp tool results in chat
2828
make_vars = true, -- Convert resources to #variables
2929
make_slash_commands = true, -- Add prompts as /slash commands
30+
format_action = function(action_name, tool)
31+
-- Replace 'use_mcp_tool' with actual tool name and params.
32+
if action_name == 'use_mcp_tool' then
33+
local name = string.format(
34+
'%s/%s',
35+
tool.args.server_name,
36+
tool.args.tool_name
37+
)
38+
local tool_input = vim.deepcopy(tool.args.tool_input)
39+
-- Cut too large params.
40+
if name == 'filesystem/edit_file' then
41+
tool_input.edits = '󰩫'
42+
elseif name == 'filesystem/write_file' then
43+
tool_input.content = '󰩫'
44+
end
45+
local args = vim.inspect(tool_input):gsub('%s+', ' ')
46+
return name .. ' ' .. args
47+
end
48+
end,
3049
}
3150
}
3251
}

lua/mcphub/extensions/codecompanion/utils.lua

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function M.create_handler(action_name, has_function_calling, opts)
8787
end
8888

8989
---@param action_name MCPHub.ActionType
90-
---@param tool table
90+
---@param tool CodeCompanion.Agent.Tool
9191
---@param chat any
9292
---@param llm_msg string
9393
---@param is_error boolean
@@ -106,7 +106,10 @@ local function add_tool_output(action_name, tool, chat, llm_msg, is_error, has_f
106106
tool,
107107
text,
108108
(user_msg or show_result_in_chat or is_error) and (user_msg or text)
109-
or string.format("**`%s` Tool**: Successfully finished", action_name)
109+
or string.format(
110+
"**`%s` Tool**: Successfully finished",
111+
opts.format_action and opts.format_action(action_name, tool) or action_name
112+
)
110113
)
111114
for _, image in ipairs(images) do
112115
helpers.add_image(chat, image)
@@ -124,7 +127,10 @@ local function add_tool_output(action_name, tool, chat, llm_msg, is_error, has_f
124127
})
125128
chat:add_buf_message({
126129
role = config.constants.USER_ROLE,
127-
content = string.format("I've shared the result of the `%s` tool with you.\n", action_name),
130+
content = string.format(
131+
"I've shared the result of the `%s` tool with you.\n",
132+
opts.format_action and opts.format_action(action_name, tool) or action_name
133+
),
128134
})
129135
end
130136
end
@@ -154,7 +160,7 @@ function M.create_output_handlers(action_name, has_function_calling, opts)
154160
%s
155161
````
156162
]],
157-
action_name,
163+
opts.format_action and opts.format_action(action_name, self) or action_name,
158164
stderr
159165
)
160166
add_tool_output(action_name, self, agent.chat, err_msg, true, has_function_calling, opts, nil, {})
@@ -179,7 +185,7 @@ function M.create_output_handlers(action_name, has_function_calling, opts)
179185
````
180186
%s
181187
````]],
182-
action_name,
188+
opts.format_action and opts.format_action(action_name, self) or action_name,
183189
result.text
184190
)
185191
end
@@ -204,7 +210,7 @@ function M.create_output_handlers(action_name, has_function_calling, opts)
204210
````
205211
%s
206212
````]],
207-
action_name,
213+
opts.format_action and opts.format_action(action_name, self) or action_name,
208214
string.format("%d image%s returned", #result.images, #result.images > 1 and "s" or "")
209215
)
210216
end
@@ -219,7 +225,10 @@ function M.create_output_handlers(action_name, has_function_calling, opts)
219225
end
220226
end
221227
end
222-
local fallback_to_llm = string.format("**`%s` Tool**: Completed with no output", action_name)
228+
local fallback_to_llm = string.format(
229+
"**`%s` Tool**: Completed with no output",
230+
opts.format_action and opts.format_action(action_name, self) or action_name
231+
)
223232
add_tool_output(
224233
action_name,
225234
self,

lua/mcphub/extensions/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ local M = {}
1212
---@field make_vars boolean Whether to make variables or not
1313
---@field make_slash_commands boolean Whether to make slash commands or not
1414
---@field show_result_in_chat boolean Whether to show the result in chat or not
15+
---@field format_action fun(action_name: MCPHub.ActionType, tool: CodeCompanion.Agent.Tool): string|nil
1516

1617
---@class MCPHub.Extensions.Config
1718
---@field avante MCPHub.Extensions.AvanteConfig Configuration for the Avante extension

0 commit comments

Comments
 (0)