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
local project_context = Path.prompts.render_file("_project.avanterules", template_opts)
if project_context ~= "" then
table.insert(messages, { role = "user", content = { { type = "text", text = project_context } } })
end
if project_context ~= "" then table.insert(messages, { role = "user", content = project_context }) end
end
if opts.diagnostics ~= nil and opts.diagnostics ~= "" and opts.diagnostics ~= "null" then
local diagnostics = Path.prompts.render_file("_diagnostics.avanterules", template_opts)
if diagnostics ~= "" then
table.insert(messages, { role = "user", content = { { type = "text", text = diagnostics } } })
end
if diagnostics ~= "" then table.insert(messages, { role = "user", content = diagnostics }) end
end
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)
if code_context ~= "" then
table.insert(messages, { role = "user", content = { { type = "text", text = code_context } } })
end
if code_context ~= "" then table.insert(messages, { role = "user", content = code_context }) end
end
if instructions then
if opts.use_xml_format then
table.insert(messages, {
role = "user",
content = { { type = "text", text = string.format("<question>%s</question>", instructions) } },
})
table.insert(messages, { role = "user", content = string.format("<question>%s</question>", instructions) })
else
table.insert(
messages,
{ role = "user", content = { { type = "text", text = string.format("QUESTION:\n%s", instructions) } } }
)
table.insert(messages, { role = "user", content = string.format("QUESTION:\n%s", instructions) })
end
end
local remaining_tokens = max_tokens - Utils.tokens.calculate_tokens(system_prompt)
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
if opts.history_messages then
@@ -112,7 +100,7 @@ function M.generate_prompts(opts)
local history_messages = {}
for i = #opts.history_messages, 1, -1 do
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
if remaining_tokens > 0 then
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)
end
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
---@type AvantePromptOptions
@@ -157,7 +145,7 @@ function M.calculate_tokens(opts)
local prompt_opts = M.generate_prompts(opts)
local tokens = Utils.tokens.calculate_tokens(prompt_opts.system_prompt)
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
return tokens
end