fix: support multiple tool use (#1192)
This commit is contained in:
@@ -119,10 +119,10 @@ M.parse_messages = function(opts)
|
||||
role = "assistant",
|
||||
content = {},
|
||||
}
|
||||
if tool_history.response_content then
|
||||
if tool_history.tool_use.response_content then
|
||||
msg.content[#msg.content + 1] = {
|
||||
type = "text",
|
||||
text = tool_history.response_content,
|
||||
text = tool_history.tool_use.response_content,
|
||||
}
|
||||
end
|
||||
msg.content[#msg.content + 1] = {
|
||||
@@ -177,24 +177,33 @@ M.parse_response = function(ctx, data_stream, event_state, opts)
|
||||
local ok, jsn = pcall(vim.json.decode, data_stream)
|
||||
if not ok then return end
|
||||
if jsn.content_block.type == "tool_use" then
|
||||
ctx.tool_use = {
|
||||
if not ctx.tool_use_list then ctx.tool_use_list = {} end
|
||||
local tool_use = {
|
||||
name = jsn.content_block.name,
|
||||
id = jsn.content_block.id,
|
||||
input_json = "",
|
||||
response_content = nil,
|
||||
}
|
||||
table.insert(ctx.tool_use_list, tool_use)
|
||||
elseif jsn.content_block.type == "text" then
|
||||
ctx.response_content = ""
|
||||
end
|
||||
elseif event_state == "content_block_delta" then
|
||||
local ok, jsn = pcall(vim.json.decode, data_stream)
|
||||
if not ok then return end
|
||||
if ctx.tool_use and jsn.delta.type == "input_json_delta" then
|
||||
ctx.tool_use.input_json = ctx.tool_use.input_json .. jsn.delta.partial_json
|
||||
if ctx.tool_use_list and jsn.delta.type == "input_json_delta" then
|
||||
local tool_use = ctx.tool_use_list[#ctx.tool_use_list]
|
||||
tool_use.input_json = tool_use.input_json .. jsn.delta.partial_json
|
||||
return
|
||||
elseif ctx.response_content and jsn.delta.type == "text_delta" then
|
||||
ctx.response_content = ctx.response_content .. jsn.delta.text
|
||||
end
|
||||
opts.on_chunk(jsn.delta.text)
|
||||
elseif event_state == "content_block_stop" then
|
||||
if ctx.tool_use_list then
|
||||
local tool_use = ctx.tool_use_list[#ctx.tool_use_list]
|
||||
if tool_use.response_content == nil then tool_use.response_content = ctx.response_content end
|
||||
end
|
||||
elseif event_state == "message_delta" then
|
||||
local ok, jsn = pcall(vim.json.decode, data_stream)
|
||||
if not ok then return end
|
||||
@@ -204,8 +213,7 @@ M.parse_response = function(ctx, data_stream, event_state, opts)
|
||||
opts.on_stop({
|
||||
reason = "tool_use",
|
||||
usage = jsn.usage,
|
||||
tool_use = ctx.tool_use,
|
||||
response_content = ctx.response_content,
|
||||
tool_use_list = ctx.tool_use_list,
|
||||
})
|
||||
end
|
||||
return
|
||||
|
||||
@@ -75,6 +75,7 @@ local DressingState = { winid = nil, input_winid = nil, input_bufnr = nil }
|
||||
---@field name string
|
||||
---@field id string
|
||||
---@field input_json string
|
||||
---@field response_content? string
|
||||
---
|
||||
---@class AvanteLLMStartCallbackOptions
|
||||
---@field usage? AvanteLLMUsage
|
||||
@@ -83,8 +84,7 @@ local DressingState = { winid = nil, input_winid = nil, input_bufnr = nil }
|
||||
---@field reason "complete" | "tool_use" | "error"
|
||||
---@field error? string | table
|
||||
---@field usage? AvanteLLMUsage
|
||||
---@field tool_use? AvanteLLMToolUse
|
||||
---@field response_content? string
|
||||
---@field tool_use_list? AvanteLLMToolUse[]
|
||||
---
|
||||
---@alias AvanteStreamParser fun(line: string, handler_opts: AvanteHandlerOptions): nil
|
||||
---@alias AvanteLLMStartCallback fun(opts: AvanteLLMStartCallbackOptions): nil
|
||||
|
||||
@@ -29,6 +29,7 @@ local P = require("avante.providers")
|
||||
---@field arguments string
|
||||
---
|
||||
---@class OpenAIMessageToolCall
|
||||
---@field index integer
|
||||
---@field id string
|
||||
---@field type "function"
|
||||
---@field function OpenAIMessageToolCallFunction
|
||||
@@ -210,8 +211,7 @@ M.parse_response = function(ctx, data_stream, _, opts)
|
||||
opts.on_stop({
|
||||
reason = "tool_use",
|
||||
usage = jsn.usage,
|
||||
tool_use = ctx.tool_use,
|
||||
response_content = ctx.response_content,
|
||||
tool_use_list = ctx.tool_use_list,
|
||||
})
|
||||
elseif choice.delta.reasoning_content and choice.delta.reasoning_content ~= vim.NIL then
|
||||
if ctx.returned_think_start_tag == nil or not ctx.returned_think_start_tag then
|
||||
@@ -229,14 +229,17 @@ M.parse_response = function(ctx, data_stream, _, opts)
|
||||
opts.on_chunk(choice.delta.reasoning)
|
||||
elseif choice.delta.tool_calls then
|
||||
local tool_call = choice.delta.tool_calls[1]
|
||||
if not ctx.tool_use then
|
||||
ctx.tool_use = {
|
||||
if not ctx.tool_use_list then ctx.tool_use_list = {} end
|
||||
if not ctx.tool_use_list[tool_call.index + 1] then
|
||||
local tool_use = {
|
||||
name = tool_call["function"].name,
|
||||
id = tool_call.id,
|
||||
input_json = "",
|
||||
}
|
||||
ctx.tool_use_list[tool_call.index + 1] = tool_use
|
||||
else
|
||||
ctx.tool_use.input_json = ctx.tool_use.input_json .. tool_call["function"].arguments
|
||||
local tool_use = ctx.tool_use_list[tool_call.index + 1]
|
||||
tool_use.input_json = tool_use.input_json .. tool_call["function"].arguments
|
||||
end
|
||||
elseif choice.delta.content then
|
||||
if
|
||||
|
||||
Reference in New Issue
Block a user