fix: compatible with zed claude code acp (#2676)
This commit is contained in:
@@ -11,16 +11,19 @@ local islist = vim.islist or vim.tbl_islist
|
|||||||
---Converts text into format suitable for UI
|
---Converts text into format suitable for UI
|
||||||
---@param text string
|
---@param text string
|
||||||
---@param decoration string | nil
|
---@param decoration string | nil
|
||||||
|
---@param filter? fun(text_line: string, idx: integer, len: integer): boolean
|
||||||
---@return avante.ui.Line[]
|
---@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 text_lines = vim.split(text, "\n")
|
||||||
local lines = {}
|
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
|
if decoration then
|
||||||
table.insert(lines, Line:new({ { decoration }, { text_line } }))
|
table.insert(lines, Line:new({ { decoration }, { text_line } }))
|
||||||
else
|
else
|
||||||
table.insert(lines, Line:new({ { text_line } }))
|
table.insert(lines, Line:new({ { text_line } }))
|
||||||
end
|
end
|
||||||
|
::continue::
|
||||||
end
|
end
|
||||||
return lines
|
return lines
|
||||||
end
|
end
|
||||||
@@ -263,7 +266,11 @@ function M.get_content_lines(content, decoration, truncate)
|
|||||||
local line_count = 0
|
local line_count = 0
|
||||||
if content_item.type == "content" then
|
if content_item.type == "content" then
|
||||||
if content_item.content.type == "text" 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
|
for idx, line in ipairs(lines_) do
|
||||||
if truncate and line_count > 3 then
|
if truncate and line_count > 3 then
|
||||||
table.insert(
|
table.insert(
|
||||||
@@ -416,12 +423,20 @@ local function tool_to_lines(item, message, messages)
|
|||||||
vim.list_extend(lines, diff_lines)
|
vim.list_extend(lines, diff_lines)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if result and result.content then
|
if message.acp_tool_call and message.acp_tool_call.content then
|
||||||
local result_content = result.content
|
local content = message.acp_tool_call.content
|
||||||
if result_content then
|
if content then
|
||||||
local content_lines = M.get_content_lines(result_content, decoration, true)
|
local content_lines = M.get_content_lines(content, decoration, true)
|
||||||
vim.list_extend(lines, content_lines)
|
vim.list_extend(lines, content_lines)
|
||||||
end
|
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
|
end
|
||||||
if #lines <= 1 then table.insert(lines, Line:new({ { decoration }, { "completed" } })) end
|
if #lines <= 1 then table.insert(lines, Line:new({ { decoration }, { "completed" } })) end
|
||||||
local last_line = lines[#lines]
|
local last_line = lines[#lines]
|
||||||
|
|||||||
@@ -800,6 +800,7 @@ end
|
|||||||
|
|
||||||
---@param opts AvanteLLMStreamOptions
|
---@param opts AvanteLLMStreamOptions
|
||||||
function M._stream_acp(opts)
|
function M._stream_acp(opts)
|
||||||
|
Utils.debug("use ACP", Config.provider)
|
||||||
local Render = require("avante.history.render")
|
local Render = require("avante.history.render")
|
||||||
---@type table<string, avante.HistoryMessage>
|
---@type table<string, avante.HistoryMessage>
|
||||||
local tool_call_messages = {}
|
local tool_call_messages = {}
|
||||||
@@ -903,8 +904,12 @@ function M._stream_acp(opts)
|
|||||||
id = update.toolCallId,
|
id = update.toolCallId,
|
||||||
name = "",
|
name = "",
|
||||||
})
|
})
|
||||||
|
tool_call_messages[update.toolCallId] = tool_call_message
|
||||||
tool_call_message.acp_tool_call = update
|
tool_call_message.acp_tool_call = update
|
||||||
end
|
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_logs = tool_call_message.tool_use_logs or {}
|
||||||
tool_call_message.tool_use_log_lines = tool_call_message.tool_use_log_lines or {}
|
tool_call_message.tool_use_log_lines = tool_call_message.tool_use_log_lines or {}
|
||||||
local tool_result_message
|
local tool_result_message
|
||||||
@@ -917,7 +922,7 @@ function M._stream_acp(opts)
|
|||||||
tool_result_message = History.Message:new("assistant", {
|
tool_result_message = History.Message:new("assistant", {
|
||||||
type = "tool_result",
|
type = "tool_result",
|
||||||
tool_use_id = update.toolCallId,
|
tool_use_id = update.toolCallId,
|
||||||
content = update.content,
|
content = nil,
|
||||||
is_error = update.status == "failed",
|
is_error = update.status == "failed",
|
||||||
is_user_declined = update.status == "cancelled",
|
is_user_declined = update.status == "cancelled",
|
||||||
})
|
})
|
||||||
@@ -928,7 +933,16 @@ function M._stream_acp(opts)
|
|||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
on_request_permission = function(tool_call, options, callback)
|
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
|
local items = vim
|
||||||
.iter(options)
|
.iter(options)
|
||||||
:map(
|
:map(
|
||||||
|
|||||||
Reference in New Issue
Block a user