Revert "refactor: message content (#1424)" (#1442)

This reverts commit ae8497faf1.
This commit is contained in:
yetone
2025-03-01 13:25:51 +08:00
committed by GitHub
parent 5bb055795f
commit 0d592f440c
10 changed files with 124 additions and 186 deletions

View File

@@ -67,43 +67,31 @@ function M.generate_prompts(opts)
if opts.project_context ~= nil and opts.project_context ~= "" and opts.project_context ~= "null" then if opts.project_context ~= nil and opts.project_context ~= "" and opts.project_context ~= "null" then
local project_context = Path.prompts.render_file("_project.avanterules", template_opts) local project_context = Path.prompts.render_file("_project.avanterules", template_opts)
if project_context ~= "" then if project_context ~= "" then table.insert(messages, { role = "user", content = project_context }) end
table.insert(messages, { role = "user", content = { { type = "text", text = project_context } } })
end
end end
if opts.diagnostics ~= nil and opts.diagnostics ~= "" and opts.diagnostics ~= "null" then if opts.diagnostics ~= nil and opts.diagnostics ~= "" and opts.diagnostics ~= "null" then
local diagnostics = Path.prompts.render_file("_diagnostics.avanterules", template_opts) local diagnostics = Path.prompts.render_file("_diagnostics.avanterules", template_opts)
if diagnostics ~= "" then if diagnostics ~= "" then table.insert(messages, { role = "user", content = diagnostics }) end
table.insert(messages, { role = "user", content = { { type = "text", text = diagnostics } } })
end
end end
if (opts.selected_files and #opts.selected_files > 0 or false) or opts.selected_code ~= nil then if (opts.selected_files and #opts.selected_files > 0 or false) or opts.selected_code ~= nil then
local code_context = Path.prompts.render_file("_context.avanterules", template_opts) local code_context = Path.prompts.render_file("_context.avanterules", template_opts)
if code_context ~= "" then if code_context ~= "" then table.insert(messages, { role = "user", content = code_context }) end
table.insert(messages, { role = "user", content = { { type = "text", text = code_context } } })
end
end end
if instructions then if instructions then
if opts.use_xml_format then if opts.use_xml_format then
table.insert(messages, { table.insert(messages, { role = "user", content = string.format("<question>%s</question>", instructions) })
role = "user",
content = { { type = "text", text = string.format("<question>%s</question>", instructions) } },
})
else else
table.insert( table.insert(messages, { role = "user", content = string.format("QUESTION:\n%s", instructions) })
messages,
{ role = "user", content = { { type = "text", text = string.format("QUESTION:\n%s", instructions) } } }
)
end end
end end
local remaining_tokens = max_tokens - Utils.tokens.calculate_tokens(system_prompt) local remaining_tokens = max_tokens - Utils.tokens.calculate_tokens(system_prompt)
for _, message in ipairs(messages) do for _, message in ipairs(messages) do
remaining_tokens = remaining_tokens - Utils.tokens.calculate_message_content_tokens(message.content) remaining_tokens = remaining_tokens - Utils.tokens.calculate_tokens(message.content)
end end
if opts.history_messages then if opts.history_messages then
@@ -112,7 +100,7 @@ function M.generate_prompts(opts)
local history_messages = {} local history_messages = {}
for i = #opts.history_messages, 1, -1 do for i = #opts.history_messages, 1, -1 do
local message = opts.history_messages[i] local message = opts.history_messages[i]
local tokens = Utils.tokens.calculate_message_content_tokens(message.content) local tokens = Utils.tokens.calculate_tokens(message.content)
remaining_tokens = remaining_tokens - tokens remaining_tokens = remaining_tokens - tokens
if remaining_tokens > 0 then if remaining_tokens > 0 then
table.insert(history_messages, message) table.insert(history_messages, message)
@@ -138,7 +126,7 @@ Merge all changes from the <update> snippet into the <code> below.
user_prompt = user_prompt .. string.format("<update>\n%s\n</update>\n", snippet) user_prompt = user_prompt .. string.format("<update>\n%s\n</update>\n", snippet)
end end
user_prompt = user_prompt .. "Provide the complete updated code." user_prompt = user_prompt .. "Provide the complete updated code."
table.insert(messages, { role = "user", content = { { type = "text", text = user_prompt } } }) table.insert(messages, { role = "user", content = user_prompt })
end end
---@type AvantePromptOptions ---@type AvantePromptOptions
@@ -157,7 +145,7 @@ function M.calculate_tokens(opts)
local prompt_opts = M.generate_prompts(opts) local prompt_opts = M.generate_prompts(opts)
local tokens = Utils.tokens.calculate_tokens(prompt_opts.system_prompt) local tokens = Utils.tokens.calculate_tokens(prompt_opts.system_prompt)
for _, message in ipairs(prompt_opts.messages) do for _, message in ipairs(prompt_opts.messages) do
tokens = tokens + Utils.tokens.calculate_message_content_tokens(message.content) tokens = tokens + Utils.tokens.calculate_tokens(message.content)
end end
return tokens return tokens
end end

View File

@@ -5,6 +5,18 @@ local Path = require("plenary.path")
local Scan = require("plenary.scandir") local Scan = require("plenary.scandir")
local Config = require("avante.config") local Config = require("avante.config")
---@class avante.ChatHistoryEntry
---@field timestamp string
---@field provider string
---@field model string
---@field request string
---@field response string
---@field original_response string
---@field selected_file {filepath: string}?
---@field selected_code {filetype: string, content: string}?
---@field reset_memory boolean?
---@field selected_filepaths string[] | nil
---@class avante.Path ---@class avante.Path
---@field history_path Path ---@field history_path Path
---@field cache_path Path ---@field cache_path Path

View File

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

View File

@@ -43,10 +43,7 @@ function M.parse_messages(opts)
---@type {idx: integer, length: integer}[] ---@type {idx: integer, length: integer}[]
local messages_with_length = {} local messages_with_length = {}
for idx, message in ipairs(opts.messages) do for idx, message in ipairs(opts.messages) do
table.insert( table.insert(messages_with_length, { idx = idx, length = Utils.tokens.calculate_tokens(message.content) })
messages_with_length,
{ idx = idx, length = Utils.tokens.calculate_message_content_tokens(message.content) }
)
end end
table.sort(messages_with_length, function(a, b) return a.length > b.length end) table.sort(messages_with_length, function(a, b) return a.length > b.length end)
@@ -58,46 +55,15 @@ function M.parse_messages(opts)
end end
for idx, message in ipairs(opts.messages) do for idx, message in ipairs(opts.messages) do
local content = {}
if type(message.content) == "string" then
table.insert(content, {
type = "text",
text = message.content,
cache_control = top_three[idx] and { type = "ephemeral" } or nil,
})
else
local message_content = message.content
---@cast message_content AvanteLLMMessageContentItem[]
for _, item in ipairs(message_content) do
if type(item) == "string" then
table.insert(content, {
type = "text",
text = item,
cache_control = top_three[idx] and { type = "ephemeral" } or nil,
})
elseif item.type == "text" then
table.insert(content, {
type = "text",
text = item.text,
cache_control = top_three[idx] and { type = "ephemeral" } or nil,
})
elseif item.type == "thinking" then
table.insert(content, {
type = "thinking",
thinking = item.thinking,
signature = item.signature,
})
elseif item.type == "redacted_thinking" then
table.insert(content, {
type = "redacted_thinking",
data = item.data,
})
end
end
end
table.insert(messages, { table.insert(messages, {
role = M.role_map[message.role], role = M.role_map[message.role],
content = content, content = {
{
type = "text",
text = message.content,
cache_control = top_three[idx] and { type = "ephemeral" } or nil,
},
},
}) })
end end
@@ -123,20 +89,12 @@ function M.parse_messages(opts)
role = "assistant", role = "assistant",
content = {}, content = {},
} }
if tool_history.tool_use.thinking_blocks then if tool_history.tool_use.thinking_contents then
for _, thinking_block in ipairs(tool_history.tool_use.thinking_blocks) do for _, thinking_content in ipairs(tool_history.tool_use.thinking_contents) do
msg.content[#msg.content + 1] = { msg.content[#msg.content + 1] = {
type = "thinking", type = "thinking",
thinking = thinking_block.thinking, thinking = thinking_content.content,
signature = thinking_block.signature, signature = thinking_content.signature,
}
end
end
if tool_history.tool_use.redacted_thinking_blocks then
for _, thinking_block in ipairs(tool_history.tool_use.redacted_thinking_blocks) do
msg.content[#msg.content + 1] = {
type = "redacted_thinking",
data = thinking_block.data,
} }
end end
end end
@@ -177,8 +135,6 @@ function M.parse_messages(opts)
end end
function M.parse_response(ctx, data_stream, event_state, opts) function M.parse_response(ctx, data_stream, event_state, opts)
if ctx.resp_filename == nil then ctx.resp_filename = vim.fn.tempname() .. ".txt" end
vim.fn.writefile({ data_stream }, ctx.resp_filename, "a")
if event_state == nil then if event_state == nil then
if data_stream:match('"message_start"') then if data_stream:match('"message_start"') then
event_state = "message_start" event_state = "message_start"
@@ -252,33 +208,24 @@ 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_blocks = vim local thinking_contents = 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_)
return { thinking = content_block_.thinking, signature = content_block_.signature } return { content = 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_) return { data = content_block_.data } end)
:totable()
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_blocks = thinking_blocks, thinking_contents = thinking_contents,
redacted_thinking_blocks = redacted_thinking_blocks,
} }
end) end)
:totable() :totable()
Utils.debug("resp filename", ctx.resp_filename)
opts.on_stop({ opts.on_stop({
reason = "tool_use", reason = "tool_use",
usage = jsn.usage, usage = jsn.usage,

View File

@@ -33,15 +33,9 @@ function M.parse_messages(opts)
end end
end end
prev_role = role prev_role = role
local parts = {} table.insert(contents, { role = M.role_map[role] or role, parts = {
for _, item in ipairs(message.content) do { text = message.content },
if type(item) == "string" then } })
table.insert(parts, { text = item })
elseif item.type == "text" then
table.insert(parts, { text = item.text })
end
end
table.insert(contents, { role = M.role_map[role] or role, parts = parts })
end) end)
if Clipboard.support_paste_image() and opts.image_paths then if Clipboard.support_paste_image() and opts.image_paths then

View File

@@ -71,30 +71,18 @@ function M.parse_messages(opts)
-- NOTE: Handle the case where the selected model is the `o1` model -- NOTE: Handle the case where the selected model is the `o1` model
-- "o1" models are "smart" enough to understand user prompt as a system prompt in this context -- "o1" models are "smart" enough to understand user prompt as a system prompt in this context
if M.is_o_series_model(base.model) then if M.is_o_series_model(base.model) then
table.insert(messages, { role = "user", content = { { type = "text", text = opts.system_prompt } } }) table.insert(messages, { role = "user", content = opts.system_prompt })
else else
table.insert(messages, { role = "system", content = opts.system_prompt }) table.insert(messages, { role = "system", content = opts.system_prompt })
end end
vim.iter(opts.messages):each(function(msg) vim
if type(msg.content) == "string" then .iter(opts.messages)
table.insert(messages, { role = M.role_map[msg.role], content = msg.content }) :each(function(msg) table.insert(messages, { role = M.role_map[msg.role], content = msg.content }) end)
else
local content = {}
for _, item in ipairs(msg.content) do
if type(item) == "string" then
table.insert(content, { type = "text", text = item })
elseif item.type == "text" then
table.insert(content, { type = "text", text = item.text })
end
end
table.insert(messages, { role = M.role_map[msg.role], content = content })
end
end)
if Config.behaviour.support_paste_from_clipboard and opts.image_paths and #opts.image_paths > 0 then if Config.behaviour.support_paste_from_clipboard and opts.image_paths and #opts.image_paths > 0 then
local message_content = messages[#messages].content local message_content = messages[#messages].content
if type(message_content) == "string" then message_content = { { type = "text", text = message_content } } end if type(message_content) ~= "table" then message_content = { type = "text", text = message_content } end
for _, image_path in ipairs(opts.image_paths) do for _, image_path in ipairs(opts.image_paths) do
table.insert(message_content, { table.insert(message_content, {
type = "image_url", type = "image_url",
@@ -115,7 +103,7 @@ function M.parse_messages(opts)
if role == M.role_map["user"] then if role == M.role_map["user"] then
table.insert(final_messages, { role = M.role_map["assistant"], content = "Ok, I understand." }) table.insert(final_messages, { role = M.role_map["assistant"], content = "Ok, I understand." })
else else
table.insert(final_messages, { role = M.role_map["user"], content = { { type = "text", text = "Ok" } } }) table.insert(final_messages, { role = M.role_map["user"], content = "Ok" })
end end
end end
prev_role = role prev_role = role

View File

@@ -392,7 +392,6 @@ local function transform_result_content(selected_files, result_content, prev_fil
elseif line_content == "<think>" then elseif line_content == "<think>" then
is_thinking = true is_thinking = true
last_think_tag_start_line = i last_think_tag_start_line = i
last_think_tag_end_line = 0
elseif line_content == "</think>" then elseif line_content == "</think>" then
is_thinking = false is_thinking = false
last_think_tag_end_line = i last_think_tag_end_line = i
@@ -2357,7 +2356,6 @@ function Sidebar:create_input_container(opts)
end end
end end
---@type AvanteLLMMessage[]
local history_messages = {} local history_messages = {}
for i = #chat_history, 1, -1 do for i = #chat_history, 1, -1 do
local entry = chat_history[i] local entry = chat_history[i]
@@ -2370,9 +2368,11 @@ function Sidebar:create_input_container(opts)
then then
break break
end end
local assistant_content = {} table.insert(
table.insert(assistant_content, { type = "text", text = Utils.trim_think_content(entry.original_response) }) history_messages,
table.insert(history_messages, 1, { role = "assistant", content = assistant_content }) 1,
{ role = "assistant", content = Utils.trim_think_content(entry.original_response) }
)
local user_content = "" local user_content = ""
if entry.selected_file ~= nil then if entry.selected_file ~= nil then
user_content = user_content .. "SELECTED FILE: " .. entry.selected_file.filepath .. "\n\n" user_content = user_content .. "SELECTED FILE: " .. entry.selected_file.filepath .. "\n\n"
@@ -2386,18 +2386,9 @@ function Sidebar:create_input_container(opts)
.. "\n```\n\n" .. "\n```\n\n"
end end
user_content = user_content .. "USER PROMPT:\n\n" .. entry.request user_content = user_content .. "USER PROMPT:\n\n" .. entry.request
table.insert(history_messages, 1, { table.insert(history_messages, 1, { role = "user", content = user_content })
role = "user",
content = {
{
type = "text",
text = user_content,
},
},
})
end end
---@type AvanteGeneratePromptsOptions
return { return {
ask = opts.ask or true, ask = opts.ask or true,
project_context = vim.json.encode(project_context), project_context = vim.json.encode(project_context),

View File

@@ -78,10 +78,7 @@ function Suggestion:suggest()
local history_messages = { local history_messages = {
{ {
role = "user", role = "user",
content = { content = [[
{
type = "text",
text = [[
<filepath>a.py</filepath> <filepath>a.py</filepath>
<code> <code>
L1: def fib L1: def fib
@@ -90,9 +87,7 @@ L3: if __name__ == "__main__":
L4: # just pass L4: # just pass
L5: pass L5: pass
</code> </code>
]], ]],
},
},
}, },
{ {
role = "assistant", role = "assistant",
@@ -100,12 +95,7 @@ L5: pass
}, },
{ {
role = "user", role = "user",
content = { content = '<question>{"insertSpaces":true,"tabSize":4,"indentSize":4,"position":{"row":1,"col":7}}</question>',
{
type = "text",
text = '<question>{"insertSpaces":true,"tabSize":4,"indentSize":4,"position":{"row":1,"col":7}}</question>',
},
},
}, },
{ {
role = "assistant", role = "assistant",

View File

@@ -76,13 +76,9 @@ vim.g.avante_login = vim.g.avante_login
---@field on_chunk AvanteLLMChunkCallback ---@field on_chunk AvanteLLMChunkCallback
---@field on_stop AvanteLLMStopCallback ---@field on_stop AvanteLLMStopCallback
--- ---
---@alias AvanteLLMMessageContentItem string | { type: "text", text: string } | { type: "thinking", thinking: string, signature: string } | { type: "redacted_thinking", data: string }
---
---@alias AvanteLLMMessageContent AvanteLLMMessageContentItem[] | string
---
---@class AvanteLLMMessage ---@class AvanteLLMMessage
---@field role "user" | "assistant" ---@field role "user" | "assistant"
---@field content AvanteLLMMessageContent ---@field content string
--- ---
---@class AvanteLLMToolResult ---@class AvanteLLMToolResult
---@field tool_name string ---@field tool_name string
@@ -231,19 +227,11 @@ vim.g.avante_login = vim.g.avante_login
---@field id string ---@field id string
---@field input_json string ---@field input_json string
---@field response_contents? string[] ---@field response_contents? string[]
---@field thinking_blocks? AvanteLLMThinkingBlock[] ---@field thinking_contents? { content: string, signature: string }[]
---@field redacted_thinking_blocks? AvanteLLMRedactedThinkingBlock[]
--- ---
---@class AvanteLLMStartCallbackOptions ---@class AvanteLLMStartCallbackOptions
---@field usage? AvanteLLMUsage ---@field usage? AvanteLLMUsage
--- ---
---@class AvanteLLMThinkingBlock
---@field thinking string
---@field signature string
---
---@class AvanteLLMRedactedThinkingBlock
---@field data string
---
---@class AvanteLLMStopCallbackOptions ---@class AvanteLLMStopCallbackOptions
---@field reason "complete" | "tool_use" | "error" | "rate_limit" ---@field reason "complete" | "tool_use" | "error" | "rate_limit"
---@field error? string | table ---@field error? string | table
@@ -355,15 +343,3 @@ vim.g.avante_login = vim.g.avante_login
---@field description string ---@field description string
---@field type string ---@field type string
---@field optional? boolean ---@field optional? boolean
---@class avante.ChatHistoryEntry
---@field timestamp string
---@field provider string
---@field model string
---@field request string
---@field response string
---@field original_response string
---@field selected_file {filepath: string}?
---@field selected_code {filetype: string, content: string}?
---@field reset_memory boolean?
---@field selected_filepaths string[] | nil

View File

@@ -9,21 +9,6 @@ local cost_per_token = {
davinci = 0.000002, davinci = 0.000002,
} }
---@param content AvanteLLMMessageContent
---@return integer
function Tokens.calculate_message_content_tokens(content)
if type(content) == "string" then return Tokens.calculate_tokens(content) end
local tokens = 0
for _, item in ipairs(content) do
if type(item) == "string" then
tokens = tokens + Tokens.calculate_tokens(item)
elseif item.type == "text" then
tokens = tokens + Tokens.calculate_tokens(item.text)
end
end
return tokens
end
--- Calculate the number of tokens in a given text. --- Calculate the number of tokens in a given text.
---@param text string The text to calculate the number of tokens for. ---@param text string The text to calculate the number of tokens for.
---@return integer The number of tokens in the given text. ---@return integer The number of tokens in the given text.