Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 12 additions & 17 deletions lua/CopilotChat/ui/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading