feat: opencode acp provider (#2789)

This commit is contained in:
yetone
2025-10-21 19:03:53 +08:00
committed by GitHub
parent fb28520067
commit 3c3eca8518
2 changed files with 58 additions and 8 deletions

View File

@@ -271,6 +271,10 @@ M._defaults = {
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY"), OPENAI_API_KEY = os.getenv("OPENAI_API_KEY"),
}, },
}, },
["opencode"] = {
command = "opencode",
args = { "acp" },
},
}, },
---To add support for custom provider, follow the format below ---To add support for custom provider, follow the format below
---See https://github.com/yetone/avante.nvim/wiki#custom-providers for more details ---See https://github.com/yetone/avante.nvim/wiki#custom-providers for more details

View File

@@ -348,6 +348,7 @@ function M.get_tool_display_name(message)
if native_tool_name == "other" and message.acp_tool_call then if native_tool_name == "other" and message.acp_tool_call then
native_tool_name = message.acp_tool_call.title or "Other" native_tool_name = message.acp_tool_call.title or "Other"
end end
if message.acp_tool_call and message.acp_tool_call.title then native_tool_name = message.acp_tool_call.title end
local tool_name = native_tool_name 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
@@ -385,6 +386,18 @@ function M.get_tool_display_name(message)
end end
end end
end end
if
not param
and message.acp_tool_call
and message.acp_tool_call.rawInput
and message.acp_tool_call.rawInput.command
then
param = message.acp_tool_call.rawInput.command
pcall(function()
local project_root = Utils.root.get()
param = param:gsub(project_root .. "/?", "")
end)
end
if param then tool_name = native_tool_name .. "(" .. param .. ")" end if param then tool_name = native_tool_name .. "(" .. param .. ")" end
end end
@@ -452,25 +465,54 @@ local function tool_to_lines(item, message, messages, expanded)
end end
table.insert(lines, Line:new({ { decoration }, { "" } })) table.insert(lines, Line:new({ { decoration }, { "" } }))
end end
local add_diff_lines = false
if item.input and type(item.input) == "table" then if item.input and type(item.input) == "table" then
if type(item.input.old_str) == "string" and type(item.input.new_str) == "string" then if type(item.input.old_str) == "string" and type(item.input.new_str) == "string" then
local diff_lines = M.get_diff_lines(item.input.old_str, item.input.new_str, decoration, not expanded) local diff_lines = M.get_diff_lines(item.input.old_str, item.input.new_str, decoration, not expanded)
add_diff_lines = true
vim.list_extend(lines, diff_lines) vim.list_extend(lines, diff_lines)
end end
end end
if message.acp_tool_call and message.acp_tool_call.content then if
local content = message.acp_tool_call.content not add_diff_lines
if content then and message.acp_tool_call
local content_lines = M.get_content_lines(content, decoration, not expanded) and message.acp_tool_call.rawInput
and message.acp_tool_call.rawInput.oldString
then
local diff_lines = M.get_diff_lines(
message.acp_tool_call.rawInput.oldString,
message.acp_tool_call.rawInput.newString,
decoration,
not expanded
)
vim.list_extend(lines, diff_lines)
end
if
message.acp_tool_call
and message.acp_tool_call.rawOutput
and message.acp_tool_call.rawOutput.metadata
and message.acp_tool_call.rawOutput.metadata.preview
then
local preview = message.acp_tool_call.rawOutput.metadata.preview
if preview then
local content_lines = M.get_content_lines(preview, decoration, not expanded)
vim.list_extend(lines, content_lines) vim.list_extend(lines, content_lines)
end end
else else
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, not expanded) local content_lines = M.get_content_lines(content, decoration, not expanded)
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, not expanded)
vim.list_extend(lines, content_lines)
end
end
end end
end end
if #lines <= 1 then if #lines <= 1 then
@@ -480,6 +522,10 @@ local function tool_to_lines(item, message, messages, expanded)
table.insert(lines, Line:new({ { decoration }, { "completed" } })) table.insert(lines, Line:new({ { decoration }, { "completed" } }))
end end
end end
--- remove last empty lines
while #lines > 0 and lines[#lines].sections[2] and lines[#lines].sections[2][1] == "" do
table.remove(lines, #lines)
end
local last_line = lines[#lines] local last_line = lines[#lines]
last_line.sections[1][1] = "╰─ " last_line.sections[1][1] = "╰─ "
return lines return lines