fix: better tool name (#2671)

This commit is contained in:
yetone
2025-09-02 16:02:13 +08:00
committed by GitHub
parent a4a4c91711
commit 968a9a065d
2 changed files with 40 additions and 15 deletions

View File

@@ -291,7 +291,11 @@ function M.get_tool_display_name(message)
local item = message.message.content[1] local item = message.message.content[1]
local tool_name = item.name local native_tool_name = item.name
if native_tool_name == "other" and message.acp_tool_call then
native_tool_name = message.acp_tool_call.title or "Other"
end
local tool_name = native_tool_name
if message.displayed_tool_name then if message.displayed_tool_name then
tool_name = message.displayed_tool_name tool_name = message.displayed_tool_name
else else
@@ -325,7 +329,7 @@ function M.get_tool_display_name(message)
end end
end end
end end
if param then tool_name = item.name .. "(" .. param .. ")" end if param then tool_name = native_tool_name .. "(" .. param .. ")" end
end end
---@cast tool_name string ---@cast tool_name string

View File

@@ -1,3 +1,6 @@
local Config = require("avante.config")
local Utils = require("avante.utils")
---@class avante.acp.ClientCapabilities ---@class avante.acp.ClientCapabilities
---@field fs avante.acp.FileSystemCapability ---@field fs avante.acp.FileSystemCapability
@@ -178,6 +181,7 @@
---@field agent_capabilities avante.acp.AgentCapabilities|nil ---@field agent_capabilities avante.acp.AgentCapabilities|nil
---@field config ACPConfig ---@field config ACPConfig
---@field callbacks table<number, fun(result: table|nil, err: avante.acp.ACPError|nil)> ---@field callbacks table<number, fun(result: table|nil, err: avante.acp.ACPError|nil)>
---@field debug_log_file file*|nil
local ACPClient = {} local ACPClient = {}
-- ACP Error codes -- ACP Error codes
@@ -217,6 +221,8 @@ ACPClient.ERROR_CODES = {
---@param config ACPConfig ---@param config ACPConfig
---@return avante.acp.ACPClient ---@return avante.acp.ACPClient
function ACPClient:new(config) function ACPClient:new(config)
local debug_log_file
if Config.debug then debug_log_file = io.open("/tmp/avante-acp-session.log", "a") end
local client = setmetatable({ local client = setmetatable({
id_counter = 0, id_counter = 0,
protocol_version = 1, protocol_version = 1,
@@ -226,6 +232,7 @@ function ACPClient:new(config)
writeTextFile = true, writeTextFile = true,
}, },
}, },
debug_log_file = debug_log_file,
pending_responses = {}, pending_responses = {},
callbacks = {}, callbacks = {},
transport = nil, transport = nil,
@@ -321,12 +328,12 @@ function ACPClient:_create_stdio_transport()
end end
---@diagnostic disable-next-line: missing-fields ---@diagnostic disable-next-line: missing-fields
local handle = uv.spawn(self.config.command, { local handle, pid = uv.spawn(self.config.command, {
args = args, args = args,
env = final_env, env = final_env,
stdio = { stdin, stdout, stderr }, stdio = { stdin, stdout, stderr },
}, function(code, signal) }, function(code, signal)
vim.print("ACP agent exited with code " .. code .. " and signal " .. signal) Utils.debug("ACP agent exited with code " .. code .. " and signal " .. signal)
self:_set_state("disconnected") self:_set_state("disconnected")
if transport_self.process then if transport_self.process then
@@ -343,6 +350,8 @@ function ACPClient:_create_stdio_transport()
end end
end) end)
Utils.debug("Spawned ACP agent process with PID " .. tostring(pid))
if not handle then if not handle then
self:_set_state("error") self:_set_state("error")
error("Failed to spawn ACP agent process") error("Failed to spawn ACP agent process")
@@ -357,7 +366,6 @@ function ACPClient:_create_stdio_transport()
-- Read stdout -- Read stdout
local buffer = "" local buffer = ""
stdout:read_start(function(err, data) stdout:read_start(function(err, data)
-- if data then vim.print("ACP stdout: " .. vim.inspect(data)) end
if err then if err then
vim.notify("ACP stdout error: " .. err, vim.log.levels.ERROR) vim.notify("ACP stdout error: " .. err, vim.log.levels.ERROR)
self:_set_state("error") self:_set_state("error")
@@ -443,6 +451,10 @@ function ACPClient:_send_request(method, params, callback)
if callback then self.callbacks[id] = callback end if callback then self.callbacks[id] = callback end
local data = vim.json.encode(message) .. "\n" local data = vim.json.encode(message) .. "\n"
if self.debug_log_file then
self.debug_log_file:write("request: " .. data .. string.rep("=", 100) .. "\n")
self.debug_log_file:flush()
end
if not self.transport:send(data) then return nil end if not self.transport:send(data) then return nil end
if not callback then return self:_wait_response(id) end if not callback then return self:_wait_response(id) end
@@ -476,6 +488,10 @@ function ACPClient:_send_notification(method, params)
} }
local data = vim.json.encode(message) .. "\n" local data = vim.json.encode(message) .. "\n"
if self.debug_log_file then
self.debug_log_file:write("notification: " .. data .. string.rep("=", 100) .. "\n")
self.debug_log_file:flush()
end
self.transport:send(data) self.transport:send(data)
end end
@@ -487,7 +503,10 @@ function ACPClient:_send_result(id, result)
local message = { jsonrpc = "2.0", id = id, result = result } local message = { jsonrpc = "2.0", id = id, result = result }
local data = vim.json.encode(message) .. "\n" local data = vim.json.encode(message) .. "\n"
-- vim.print("Sending result: " .. data) if self.debug_log_file then
self.debug_log_file:write("request: " .. data .. string.rep("=", 100) .. "\n")
self.debug_log_file:flush()
end
self.transport:send(data) self.transport:send(data)
end end
@@ -507,12 +526,15 @@ end
---Handle received message ---Handle received message
---@param message table ---@param message table
function ACPClient:_handle_message(message) function ACPClient:_handle_message(message)
-- vim.print("Received message: " .. vim.inspect(message))
-- Check if this is a notification (has method but no id, or has both method and id for notifications) -- Check if this is a notification (has method but no id, or has both method and id for notifications)
if message.method and not message.result and not message.error then if message.method and not message.result and not message.error then
-- This is a notification -- This is a notification
self:_handle_notification(message.id, message.method, message.params) self:_handle_notification(message.id, message.method, message.params)
elseif message.id and (message.result or message.error) then elseif message.id and (message.result or message.error) then
if self.debug_log_file then
self.debug_log_file:write("response: " .. vim.inspect(message) .. string.rep("=", 100) .. "\n")
self.debug_log_file:flush()
end
local callback = self.callbacks[message.id] local callback = self.callbacks[message.id]
if callback then if callback then
callback(message.result, message.error) callback(message.result, message.error)
@@ -530,12 +552,11 @@ end
---@param method string ---@param method string
---@param params table ---@param params table
function ACPClient:_handle_notification(message_id, method, params) function ACPClient:_handle_notification(message_id, method, params)
-- local f = io.open("/tmp/session.txt", "a") if self.debug_log_file then
-- if f then self.debug_log_file:write("method: " .. method .. "\n")
-- f:write("method: " .. method .. "\n") self.debug_log_file:write(vim.inspect(params) .. "\n" .. string.rep("=", 100) .. "\n")
-- f:write(vim.inspect(params) .. "\n" .. string.rep("=", 100) .. "\n") self.debug_log_file:flush()
-- f:close() end
-- end
if method == "session/update" then if method == "session/update" then
self:_handle_session_update(params) self:_handle_session_update(params)
elseif method == "session/request_permission" then elseif method == "session/request_permission" then
@@ -686,11 +707,11 @@ function ACPClient:initialize()
local auth_method = self.config.auth_method local auth_method = self.config.auth_method
if auth_method then if auth_method then
vim.print("Authenticating with method " .. auth_method) Utils.debug("Authenticating with method " .. auth_method)
self:authenticate(auth_method) self:authenticate(auth_method)
self:_set_state("ready") self:_set_state("ready")
else else
vim.print("No authentication method found or specified") Utils.debug("No authentication method found or specified")
self:_set_state("ready") self:_set_state("ready")
end end
end end