feat: support redacted thinking (#1492)

This commit is contained in:
yetone
2025-03-05 18:53:24 +08:00
committed by GitHub
parent a652db7608
commit cc078a5337
3 changed files with 40 additions and 80 deletions

View File

@@ -16,74 +16,7 @@ M.role_map = {
assistant = "assistant",
}
function M.parse_messages(opts)
---@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_messages = Claude.parse_messages
M.parse_response = Claude.parse_response
---@param prompt_opts AvantePromptOptions

View File

@@ -89,12 +89,20 @@ function M.parse_messages(opts)
role = "assistant",
content = {},
}
if tool_history.tool_use.thinking_contents then
for _, thinking_content in ipairs(tool_history.tool_use.thinking_contents) do
if tool_history.tool_use.thinking_blocks then
for _, thinking_block in ipairs(tool_history.tool_use.thinking_blocks) do
msg.content[#msg.content + 1] = {
type = "thinking",
thinking = thinking_content.content,
signature = thinking_content.signature,
thinking = thinking_block.thinking,
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
@@ -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)
:map(function(content_block_) return content_block_.text end)
:totable()
local thinking_contents = vim
local thinking_blocks = vim
.iter(ctx.content_blocks)
:filter(function(content_block_) return content_block_.stoppped and content_block_.type == "thinking" end)
:map(
function(content_block_)
return { content = content_block_.thinking, signature = content_block_.signature }
end
)
:map(function(content_block_)
---@type AvanteLLMThinkingBlock
return { thinking = content_block_.thinking, signature = content_block_.signature }
end)
: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 {
name = content_block.name,
id = content_block.id,
input_json = content_block.input_json,
response_contents = response_contents,
thinking_contents = thinking_contents,
thinking_blocks = thinking_blocks,
redacted_thinking_blocks = redacted_thinking_blocks,
}
end)
:totable()

View File

@@ -222,12 +222,20 @@ vim.g.avante_login = vim.g.avante_login
---@field cache_read_input_tokens number
---@field output_tokens number
---
---@class AvanteLLMThinkingBlock
---@field thinking string
---@field signature string
---
---@class AvanteLLMRedactedThinkingBlock
---@field data string
---
---@class AvanteLLMToolUse
---@field name string
---@field id string
---@field input_json string
---@field response_contents? string[]
---@field thinking_contents? { content: string, signature: string }[]
---@field thinking_blocks? AvanteLLMThinkingBlock[]
---@field redacted_thinking_blocks? AvanteLLMRedactedThinkingBlock[]
---
---@class AvanteLLMStartCallbackOptions
---@field usage? AvanteLLMUsage