From fd57bf3c18dbac795a341e206bacbe3b797fca62 Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Mon, 28 Jul 2025 05:26:30 +0200 Subject: [PATCH] fix(functions): properly handle multiple tool calls at once Signed-off-by: Tomas Slusny --- lua/CopilotChat/init.lua | 3 ++- lua/CopilotChat/ui/chat.lua | 29 ++++++++++++----------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index 1f592349..c4da582e 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -926,9 +926,10 @@ function M.ask(prompt, config) for _, tool in ipairs(resolved_tools) do M.chat:add_message({ + id = tool.id, role = 'tool', tool_call_id = tool.id, - content = tool.result .. '\n', + content = '\n' .. tool.result .. '\n', }) end diff --git a/lua/CopilotChat/ui/chat.lua b/lua/CopilotChat/ui/chat.lua index f54e9435..0b1fdc32 100644 --- a/lua/CopilotChat/ui/chat.lua +++ b/lua/CopilotChat/ui/chat.lua @@ -397,22 +397,23 @@ end function Chat:add_message(message, replace) local current_message = self.messages[#self.messages] - local needs_header = false + local is_new = not current_message + or current_message.role ~= message.role + or (message.id and current_message.id ~= message.id) - -- Check if we need to add a header (role change or first message) - if not current_message or current_message.role ~= message.role then - needs_header = true - end - - -- Add appropriate header based on role - if needs_header then + if is_new then + -- Add appropriate header based on role and generate a new ID if not provided message.id = message.id or utils.uuid() local header = self.headers[message.role] if current_message then header = '\n' .. header end + + table.insert(self.messages, message) self:append(header .. '(' .. message.id .. ')' .. self.separator .. '\n\n') + self:append(message.content) elseif replace and current_message then + -- Replace the content of the current message self:append('') self:render() @@ -432,15 +433,9 @@ function Chat:add_message(message, replace) end self:append('') - return - end - - -- Handle message content combining or creation - if current_message and current_message.role == message.role then - current_message.content = current_message.content .. message.content - self:append(message.content) else - table.insert(self.messages, message) + -- Append to the current message + current_message.content = current_message.content .. message.content self:append(message.content) end end @@ -561,7 +556,7 @@ function Chat:render() local current_block = nil local function parse_header(header, line) - return line:match('^' .. vim.pesc(header) .. '%(([%w%-]+)%)' .. vim.pesc(self.separator) .. '$') + return line:match('^' .. vim.pesc(header) .. '%(([^)]+)%)' .. vim.pesc(self.separator) .. '$') end for l, line in ipairs(lines) do