@@ -1223,27 +1223,35 @@ function M:_handle_server_content(params)
12231223 -- IMPORTANT: Return immediately - do NOT display anything for toolCallPrepare
12241224 return
12251225 elseif content .type == " toolCalled" then
1226+ local tool_text = " "
1227+
12261228 -- Add diff to current tool call if present in toolCalled content
12271229 if self ._current_tool_call and content .details then
12281230 self ._current_tool_call .details = content .details
12291231 end
12301232
12311233 -- Show the final accumulated tool call if we have one
12321234 if self ._is_tool_call_streaming and self ._current_tool_call then
1233- self :_display_tool_call ()
1235+ tool_text = self :_display_tool_call ()
12341236 end
12351237
12361238 -- Show the tool result
1237- local tool_text = string.format (" ✅ **Tool Result**: %s" , content .name or " unknown" )
1239+ local tool_log = string.format (" **Tool Result**: %s" , content .name or " unknown" )
12381240 if content .outputs and # content .outputs > 0 then
12391241 for _ , output in ipairs (content .outputs ) do
12401242 if output .type == " text" and output .content then
1241- tool_text = tool_text .. " \n " .. output .content
1243+ tool_log = tool_log .. " \n " .. output .content
12421244 end
12431245 end
12441246 end
1245- self :_add_message (" assistant" , tool_text )
1246-
1247+ Logger .debug (tool_log )
1248+
1249+ local tool_text_completed = " ✅ " .. (content .summary or content .name or " Tool call completed" )
1250+
1251+ if not self :_replace_message (tool_text , tool_text_completed ) then
1252+ self :_add_message (" assistant" , tool_text_completed )
1253+ end
1254+
12471255 -- Clean up tool call state
12481256 self :_finalize_tool_call ()
12491257 end
@@ -1519,15 +1527,20 @@ function M:_handle_tool_call_prepare(content)
15191527 self ._is_tool_call_streaming = true
15201528 self ._current_tool_call = {
15211529 name = " " ,
1522- arguments = " "
1530+ arguments = " " ,
1531+ summary = " "
15231532 }
15241533 end
1525-
1534+
15261535 -- Accumulate tool call data
15271536 if content .name then
15281537 self ._current_tool_call .name = content .name
15291538 end
1530-
1539+
1540+ if content .summary then
1541+ self ._current_tool_call .summary = content .summary
1542+ end
1543+
15311544 if content .argumentsText then
15321545 self ._current_tool_call .arguments = (self ._current_tool_call .arguments or " " ) .. content .argumentsText
15331546 end
@@ -1537,26 +1550,72 @@ function M:_handle_tool_call_prepare(content)
15371550 end
15381551end
15391552
1553+ --- @return string tool text
15401554function M :_display_tool_call ()
1541- if not self ._current_tool_call then return end
1542-
1543- local tool_text = string.format (" 🔧 **Tool Call**: %s" , self ._current_tool_call .name or " unknown" )
1544-
1555+ if not self ._current_tool_call then return " " end
1556+
1557+ local tool_text = " 🔧 " .. (self ._current_tool_call .summary or " Tool call prepared" )
1558+ local tool_log = string.format (" **Tool Call**: %s" , self ._current_tool_call .name or " unknown" )
1559+
15451560 if self ._current_tool_call .arguments and self ._current_tool_call .arguments ~= " " then
1546- tool_text = tool_text .. " \n ```json\n " .. self ._current_tool_call .arguments .. " \n ```"
1561+ tool_log = tool_log .. " \n ```json\n " .. self ._current_tool_call .arguments .. " \n ```"
15471562 end
1548-
1563+
15491564
15501565 if self ._current_tool_call .details and self ._current_tool_call .details .diff then
1551- tool_text = tool_text .. " \n\n **Diff**:\n ```diff\n " .. self ._current_tool_call .details .diff .. " \n ```"
1566+ tool_log = tool_log .. " \n\n **Diff**:\n ```diff\n " .. self ._current_tool_call .details .diff .. " \n ```"
15521567 end
15531568
1569+ Logger .debug (tool_log )
15541570 self :_add_message (" assistant" , tool_text )
1571+
1572+ return tool_text
15551573end
15561574
15571575function M :_finalize_tool_call ()
15581576 self ._current_tool_call = nil
15591577 self ._is_tool_call_streaming = false
15601578end
15611579
1580+ --- @param target string
1581+ --- @param replacement string
1582+ --- @return boolean changed True if any replacement was made
1583+ function M :_replace_message (target , replacement )
1584+ local chat = self .containers .chat
1585+
1586+ if not chat or not vim .api .nvim_buf_is_valid (chat .bufnr ) then
1587+ Logger .warn (" Cannot replace message: chat buffer not available" )
1588+ return false
1589+ end
1590+
1591+ if not target or target == " " then
1592+ Logger .warn (" Cannot replace message: empty target" )
1593+ return false
1594+ end
1595+
1596+ if not replacement then
1597+ Logger .warn (" Cannot replace message: empty replacement" )
1598+ return false
1599+ end
1600+
1601+ local changed = false
1602+
1603+ self :_safe_buffer_update (chat .bufnr , function ()
1604+ local lines = vim .api .nvim_buf_get_lines (chat .bufnr , 0 , - 1 , false )
1605+
1606+ for i = # lines , 1 , - 1 do
1607+ local line = lines [i ] or " "
1608+ local s_idx , e_idx = line :find (target , 1 , true )
1609+ if s_idx then
1610+ local new_line = (line :sub (1 , s_idx - 1 )) .. replacement .. (line :sub (e_idx + 1 ))
1611+ vim .api .nvim_buf_set_lines (chat .bufnr , i - 1 , i , false , { new_line })
1612+ changed = true
1613+ break
1614+ end
1615+ end
1616+ end )
1617+
1618+ return changed
1619+ end
1620+
15621621return M
0 commit comments