From 9f0bb4bfb9253a7293b45a9a97f18e602ec3f50c Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 3 Sep 2025 00:12:49 +0800 Subject: [PATCH] fix: compatible with zed claude code acp (#2676) --- lua/avante/history/render.lua | 29 ++++++++++++++++++++++------- lua/avante/llm.lua | 18 ++++++++++++++++-- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/lua/avante/history/render.lua b/lua/avante/history/render.lua index f0404f6..ff7f165 100644 --- a/lua/avante/history/render.lua +++ b/lua/avante/history/render.lua @@ -11,16 +11,19 @@ local islist = vim.islist or vim.tbl_islist ---Converts text into format suitable for UI ---@param text string ---@param decoration string | nil +---@param filter? fun(text_line: string, idx: integer, len: integer): boolean ---@return avante.ui.Line[] -local function text_to_lines(text, decoration) +local function text_to_lines(text, decoration, filter) local text_lines = vim.split(text, "\n") local lines = {} - for _, text_line in ipairs(text_lines) do + for idx, text_line in ipairs(text_lines) do + if filter and not filter(text_line, idx, #text_lines) then goto continue end if decoration then table.insert(lines, Line:new({ { decoration }, { text_line } })) else table.insert(lines, Line:new({ { text_line } })) end + ::continue:: end return lines end @@ -263,7 +266,11 @@ function M.get_content_lines(content, decoration, truncate) local line_count = 0 if content_item.type == "content" then if content_item.content.type == "text" then - local lines_ = text_to_lines(content_item.content.text, decoration) + local lines_ = text_to_lines(content_item.content.text, decoration, function(text_line, idx, len) + if idx == 1 and text_line:match("^%s*```%s*$") then return false end + if idx == len and text_line:match("^%s*```%s*$") then return false end + return true + end) for idx, line in ipairs(lines_) do if truncate and line_count > 3 then table.insert( @@ -416,12 +423,20 @@ local function tool_to_lines(item, message, messages) vim.list_extend(lines, diff_lines) end end - if result and result.content then - local result_content = result.content - if result_content then - local content_lines = M.get_content_lines(result_content, decoration, true) + if message.acp_tool_call and message.acp_tool_call.content then + local content = message.acp_tool_call.content + if content then + local content_lines = M.get_content_lines(content, decoration, true) vim.list_extend(lines, content_lines) end + else + if result and result.content then + local result_content = result.content + if result_content then + local content_lines = M.get_content_lines(result_content, decoration, true) + vim.list_extend(lines, content_lines) + end + end end if #lines <= 1 then table.insert(lines, Line:new({ { decoration }, { "completed" } })) end local last_line = lines[#lines] diff --git a/lua/avante/llm.lua b/lua/avante/llm.lua index 2ff5400..7bdb21d 100644 --- a/lua/avante/llm.lua +++ b/lua/avante/llm.lua @@ -800,6 +800,7 @@ end ---@param opts AvanteLLMStreamOptions function M._stream_acp(opts) + Utils.debug("use ACP", Config.provider) local Render = require("avante.history.render") ---@type table local tool_call_messages = {} @@ -903,8 +904,12 @@ function M._stream_acp(opts) id = update.toolCallId, name = "", }) + tool_call_messages[update.toolCallId] = tool_call_message tool_call_message.acp_tool_call = update end + if tool_call_message.acp_tool_call then + tool_call_message.acp_tool_call = vim.tbl_deep_extend("force", tool_call_message.acp_tool_call, update) + end tool_call_message.tool_use_logs = tool_call_message.tool_use_logs or {} tool_call_message.tool_use_log_lines = tool_call_message.tool_use_log_lines or {} local tool_result_message @@ -917,7 +922,7 @@ function M._stream_acp(opts) tool_result_message = History.Message:new("assistant", { type = "tool_result", tool_use_id = update.toolCallId, - content = update.content, + content = nil, is_error = update.status == "failed", is_user_declined = update.status == "cancelled", }) @@ -928,7 +933,16 @@ function M._stream_acp(opts) end end, on_request_permission = function(tool_call, options, callback) - local message = add_tool_call_message(tool_call) + local message = tool_call_messages[tool_call.toolCallId] + if not message then + add_tool_call_message(tool_call) + else + if message.acp_tool_call then + message.acp_tool_call = vim.tbl_deep_extend("force", message.acp_tool_call, tool_call) + tool_call = message.acp_tool_call + end + end + ---@cast tool_call avante.acp.ToolCall local items = vim .iter(options) :map(