From d82a19b5ccec01fdcb9ef2f11f45522a20ecf5c8 Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Sat, 2 Aug 2025 18:48:45 +0200 Subject: [PATCH] fix(chat): handle skipped tool calls with explicit error result Previously, unhandled tool calls were simply removed from the assistant message, which could lead to confusion or lack of feedback. Now, when a tool call is skipped, an explicit error result is added to the resolved tools, indicating that the user skipped the function call. This improves transparency and makes the handling of skipped tool calls clearer. Signed-off-by: Tomas Slusny --- lua/CopilotChat/init.lua | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index d2dbee0c..4e81c934 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -971,16 +971,7 @@ function M.ask(prompt, config) prompt = vim.trim(prompt) utils.schedule_main() - if utils.empty(prompt) and utils.empty(resolved_tools) then - if not config.headless then - M.chat:remove_message('user') - finish() - end - return - end - if not config.headless then - -- Remove any tool calls that we did not handle local assistant_message = M.chat:get_message('assistant') if assistant_message and assistant_message.tool_calls then local handled_ids = {} @@ -988,15 +979,15 @@ function M.ask(prompt, config) handled_ids[tool.id] = true end - assistant_message.tool_calls = vim - .iter(assistant_message.tool_calls) - :filter(function(tool_call) - return handled_ids[tool_call.id] - end) - :totable() - - if utils.empty(assistant_message.tool_calls) then - M.chat:remove_message('assistant') + -- If we skipped any tool calls, send that as result + for _, tool_call in ipairs(assistant_message.tool_calls) do + if not handled_ids[tool_call.id] then + table.insert(resolved_tools, { + id = tool_call.id, + result = string.format(BLOCK_OUTPUT_FORMAT, 'error', 'User skipped this function call.'), + }) + handled_ids[tool_call.id] = true + end end end @@ -1020,6 +1011,14 @@ function M.ask(prompt, config) end end + if utils.empty(prompt) and utils.empty(resolved_tools) then + if not config.headless then + M.chat:remove_message('user') + finish() + end + return + end + local ask_ok, ask_response = pcall(client.ask, client, prompt, { headless = config.headless, history = M.chat.messages,