fix: make acp client send requests async (#2848)

This commit is contained in:
Peter Cardenas
2025-12-20 11:32:04 -08:00
committed by GitHub
parent 2afb70537f
commit cf352f6f46
2 changed files with 137 additions and 99 deletions

View File

@@ -977,6 +977,7 @@ function M._stream_acp(opts)
return message
end
local acp_client = opts.acp_client
local session_id = opts.acp_session_id
if not acp_client then
local acp_config = vim.tbl_deep_extend("force", acp_provider, {
---@type ACPHandlers
@@ -1298,22 +1299,46 @@ function M._stream_acp(opts)
},
})
acp_client = ACPClient:new(acp_config)
acp_client:connect()
-- Register ACP client for global cleanup on exit (Fix Issue #2749)
local client_id = "acp_" .. tostring(acp_client) .. "_" .. os.time()
local ok, Avante = pcall(require, "avante")
if ok and Avante.register_acp_client then Avante.register_acp_client(client_id, acp_client) end
acp_client:connect(function(conn_err)
if conn_err then
opts.on_stop({ reason = "error", error = conn_err })
return
end
-- If we create a new client and it does not support sesion loading,
-- remove the old session
if not acp_client.agent_capabilities.loadSession then opts.acp_session_id = nil end
if opts.on_save_acp_client then opts.on_save_acp_client(acp_client) end
-- Register ACP client for global cleanup on exit (Fix Issue #2749)
local client_id = "acp_" .. tostring(acp_client) .. "_" .. os.time()
local ok, Avante = pcall(require, "avante")
if ok and Avante.register_acp_client then Avante.register_acp_client(client_id, acp_client) end
-- If we create a new client and it does not support sesion loading,
-- remove the old session
if not acp_client.agent_capabilities.loadSession then opts.acp_session_id = nil end
if opts.on_save_acp_client then opts.on_save_acp_client(acp_client) end
session_id = opts.acp_session_id
if not session_id then
M._create_acp_session_and_continue(opts, acp_client)
else
if opts.just_connect_acp_client then return end
M._continue_stream_acp(opts, acp_client, session_id)
end
end)
return
elseif not session_id then
M._create_acp_session_and_continue(opts, acp_client)
return
end
local session_id = opts.acp_session_id
if not session_id then
local project_root = Utils.root.get()
local session_id_, err = acp_client:create_session(project_root, {})
if opts.just_connect_acp_client then return end
M._continue_stream_acp(opts, acp_client, session_id)
end
---@param opts AvanteLLMStreamOptions
---@param acp_client avante.acp.ACPClient
function M._create_acp_session_and_continue(opts, acp_client)
local project_root = Utils.root.get()
acp_client:create_session(project_root, {}, function(session_id_, err)
if err then
opts.on_stop({ reason = "error", error = err })
return
@@ -1322,10 +1347,18 @@ function M._stream_acp(opts)
opts.on_stop({ reason = "error", error = "Failed to create session" })
return
end
session_id = session_id_
if opts.on_save_acp_session_id then opts.on_save_acp_session_id(session_id) end
end
if opts.just_connect_acp_client then return end
opts.acp_session_id = session_id_
if opts.on_save_acp_session_id then opts.on_save_acp_session_id(session_id_) end
if opts.just_connect_acp_client then return end
M._continue_stream_acp(opts, acp_client, session_id_)
end)
end
---@param opts AvanteLLMStreamOptions
---@param acp_client avante.acp.ACPClient
---@param session_id string
function M._continue_stream_acp(opts, acp_client, session_id)
local prompt = {}
local donot_use_builtin_system_prompt = opts.history_messages ~= nil and #opts.history_messages > 0
if donot_use_builtin_system_prompt then