feat: support redacted thinking (#1492)
This commit is contained in:
@@ -16,74 +16,7 @@ M.role_map = {
|
|||||||
assistant = "assistant",
|
assistant = "assistant",
|
||||||
}
|
}
|
||||||
|
|
||||||
function M.parse_messages(opts)
|
M.parse_messages = Claude.parse_messages
|
||||||
---@type AvanteBedrockClaudeMessage[]
|
|
||||||
local messages = {}
|
|
||||||
|
|
||||||
for _, message in ipairs(opts.messages) do
|
|
||||||
table.insert(messages, {
|
|
||||||
role = M.role_map[message.role],
|
|
||||||
content = {
|
|
||||||
{
|
|
||||||
type = "text",
|
|
||||||
text = message.content,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
if opts.tool_histories then
|
|
||||||
for _, tool_history in ipairs(opts.tool_histories) do
|
|
||||||
if tool_history.tool_use then
|
|
||||||
local msg = {
|
|
||||||
role = "assistant",
|
|
||||||
content = {},
|
|
||||||
}
|
|
||||||
if tool_history.tool_use.thinking_contents then
|
|
||||||
for _, thinking_content in ipairs(tool_history.tool_use.thinking_contents) do
|
|
||||||
msg.content[#msg.content + 1] = {
|
|
||||||
type = "thinking",
|
|
||||||
thinking = thinking_content.content,
|
|
||||||
signature = thinking_content.signature,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if tool_history.tool_use.response_contents then
|
|
||||||
for _, response_content in ipairs(tool_history.tool_use.response_contents) do
|
|
||||||
msg.content[#msg.content + 1] = {
|
|
||||||
type = "text",
|
|
||||||
text = response_content,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
msg.content[#msg.content + 1] = {
|
|
||||||
type = "tool_use",
|
|
||||||
id = tool_history.tool_use.id,
|
|
||||||
name = tool_history.tool_use.name,
|
|
||||||
input = vim.json.decode(tool_history.tool_use.input_json),
|
|
||||||
}
|
|
||||||
messages[#messages + 1] = msg
|
|
||||||
end
|
|
||||||
|
|
||||||
if tool_history.tool_result then
|
|
||||||
messages[#messages + 1] = {
|
|
||||||
role = "user",
|
|
||||||
content = {
|
|
||||||
{
|
|
||||||
type = "tool_result",
|
|
||||||
tool_use_id = tool_history.tool_result.tool_use_id,
|
|
||||||
content = tool_history.tool_result.content,
|
|
||||||
is_error = tool_history.tool_result.is_error,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return messages
|
|
||||||
end
|
|
||||||
|
|
||||||
M.parse_response = Claude.parse_response
|
M.parse_response = Claude.parse_response
|
||||||
|
|
||||||
---@param prompt_opts AvantePromptOptions
|
---@param prompt_opts AvantePromptOptions
|
||||||
|
|||||||
@@ -89,12 +89,20 @@ function M.parse_messages(opts)
|
|||||||
role = "assistant",
|
role = "assistant",
|
||||||
content = {},
|
content = {},
|
||||||
}
|
}
|
||||||
if tool_history.tool_use.thinking_contents then
|
if tool_history.tool_use.thinking_blocks then
|
||||||
for _, thinking_content in ipairs(tool_history.tool_use.thinking_contents) do
|
for _, thinking_block in ipairs(tool_history.tool_use.thinking_blocks) do
|
||||||
msg.content[#msg.content + 1] = {
|
msg.content[#msg.content + 1] = {
|
||||||
type = "thinking",
|
type = "thinking",
|
||||||
thinking = thinking_content.content,
|
thinking = thinking_block.thinking,
|
||||||
signature = thinking_content.signature,
|
signature = thinking_block.signature,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if tool_history.tool_use.redacted_thinking_blocks then
|
||||||
|
for _, redacted_thinking_block in ipairs(tool_history.tool_use.redacted_thinking_blocks) do
|
||||||
|
msg.content[#msg.content + 1] = {
|
||||||
|
type = "redacted_thinking",
|
||||||
|
data = redacted_thinking_block.data,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -208,21 +216,32 @@ function M.parse_response(ctx, data_stream, event_state, opts)
|
|||||||
:filter(function(content_block_) return content_block_.stoppped and content_block_.type == "text" end)
|
:filter(function(content_block_) return content_block_.stoppped and content_block_.type == "text" end)
|
||||||
:map(function(content_block_) return content_block_.text end)
|
:map(function(content_block_) return content_block_.text end)
|
||||||
:totable()
|
:totable()
|
||||||
local thinking_contents = vim
|
local thinking_blocks = vim
|
||||||
.iter(ctx.content_blocks)
|
.iter(ctx.content_blocks)
|
||||||
:filter(function(content_block_) return content_block_.stoppped and content_block_.type == "thinking" end)
|
:filter(function(content_block_) return content_block_.stoppped and content_block_.type == "thinking" end)
|
||||||
:map(
|
:map(function(content_block_)
|
||||||
function(content_block_)
|
---@type AvanteLLMThinkingBlock
|
||||||
return { content = content_block_.thinking, signature = content_block_.signature }
|
return { thinking = content_block_.thinking, signature = content_block_.signature }
|
||||||
end
|
end)
|
||||||
)
|
|
||||||
:totable()
|
:totable()
|
||||||
|
local redacted_thinking_blocks = vim
|
||||||
|
.iter(ctx.content_blocks)
|
||||||
|
:filter(
|
||||||
|
function(content_block_) return content_block_.stoppped and content_block_.type == "redacted_thinking" end
|
||||||
|
)
|
||||||
|
:map(function(content_block_)
|
||||||
|
---@type AvanteLLMRedactedThinkingBlock
|
||||||
|
return { data = content_block_.data }
|
||||||
|
end)
|
||||||
|
:totable()
|
||||||
|
---@type AvanteLLMToolUse
|
||||||
return {
|
return {
|
||||||
name = content_block.name,
|
name = content_block.name,
|
||||||
id = content_block.id,
|
id = content_block.id,
|
||||||
input_json = content_block.input_json,
|
input_json = content_block.input_json,
|
||||||
response_contents = response_contents,
|
response_contents = response_contents,
|
||||||
thinking_contents = thinking_contents,
|
thinking_blocks = thinking_blocks,
|
||||||
|
redacted_thinking_blocks = redacted_thinking_blocks,
|
||||||
}
|
}
|
||||||
end)
|
end)
|
||||||
:totable()
|
:totable()
|
||||||
|
|||||||
@@ -222,12 +222,20 @@ vim.g.avante_login = vim.g.avante_login
|
|||||||
---@field cache_read_input_tokens number
|
---@field cache_read_input_tokens number
|
||||||
---@field output_tokens number
|
---@field output_tokens number
|
||||||
---
|
---
|
||||||
|
---@class AvanteLLMThinkingBlock
|
||||||
|
---@field thinking string
|
||||||
|
---@field signature string
|
||||||
|
---
|
||||||
|
---@class AvanteLLMRedactedThinkingBlock
|
||||||
|
---@field data string
|
||||||
|
---
|
||||||
---@class AvanteLLMToolUse
|
---@class AvanteLLMToolUse
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field id string
|
---@field id string
|
||||||
---@field input_json string
|
---@field input_json string
|
||||||
---@field response_contents? string[]
|
---@field response_contents? string[]
|
||||||
---@field thinking_contents? { content: string, signature: string }[]
|
---@field thinking_blocks? AvanteLLMThinkingBlock[]
|
||||||
|
---@field redacted_thinking_blocks? AvanteLLMRedactedThinkingBlock[]
|
||||||
---
|
---
|
||||||
---@class AvanteLLMStartCallbackOptions
|
---@class AvanteLLMStartCallbackOptions
|
||||||
---@field usage? AvanteLLMUsage
|
---@field usage? AvanteLLMUsage
|
||||||
|
|||||||
Reference in New Issue
Block a user