fix: better sidebar (#1603)
* fix: better sidebar * feat: better msg history * fix: tests
This commit is contained in:
@@ -11,14 +11,6 @@ function M.filter_active_entries(entries)
|
||||
for i = #entries, 1, -1 do
|
||||
local entry = entries[i]
|
||||
if entry.reset_memory then break end
|
||||
if
|
||||
entry.request == nil
|
||||
or entry.original_response == nil
|
||||
or entry.request == ""
|
||||
or entry.original_response == ""
|
||||
then
|
||||
break
|
||||
end
|
||||
table.insert(entries_, 1, entry)
|
||||
end
|
||||
|
||||
@@ -30,25 +22,62 @@ end
|
||||
function M.entries_to_llm_messages(entries)
|
||||
local messages = {}
|
||||
for _, entry in ipairs(entries) do
|
||||
local user_content = ""
|
||||
if entry.selected_filepaths ~= nil then
|
||||
user_content = user_content .. "SELECTED FILES:\n\n"
|
||||
if entry.selected_filepaths ~= nil and #entry.selected_filepaths > 0 then
|
||||
local user_content = "SELECTED FILES:\n\n"
|
||||
for _, filepath in ipairs(entry.selected_filepaths) do
|
||||
user_content = user_content .. filepath .. "\n"
|
||||
end
|
||||
table.insert(messages, { role = "user", content = user_content })
|
||||
end
|
||||
if entry.selected_code ~= nil then
|
||||
user_content = user_content
|
||||
.. "SELECTED CODE:\n\n```"
|
||||
local user_content_ = "SELECTED CODE:\n\n```"
|
||||
.. (entry.selected_code.file_type or "")
|
||||
.. (entry.selected_code.path and ":" .. entry.selected_code.path or "")
|
||||
.. "\n"
|
||||
.. entry.selected_code.content
|
||||
.. "\n```\n\n"
|
||||
table.insert(messages, { role = "user", content = user_content_ })
|
||||
end
|
||||
user_content = user_content .. "USER PROMPT:\n\n" .. entry.request
|
||||
table.insert(messages, { role = "user", content = user_content })
|
||||
table.insert(messages, { role = "assistant", content = Utils.trim_think_content(entry.original_response) })
|
||||
if entry.request ~= nil and entry.request ~= "" then
|
||||
table.insert(messages, { role = "user", content = entry.request })
|
||||
end
|
||||
if entry.tool_histories ~= nil and #entry.tool_histories > 0 then
|
||||
for _, tool_history in ipairs(entry.tool_histories) do
|
||||
local assistant_content = {}
|
||||
if tool_history.tool_use ~= nil then
|
||||
if tool_history.tool_use.response_contents ~= nil then
|
||||
for _, response_content in ipairs(tool_history.tool_use.response_contents) do
|
||||
table.insert(assistant_content, { type = "text", text = response_content })
|
||||
end
|
||||
end
|
||||
table.insert(assistant_content, {
|
||||
type = "tool_use",
|
||||
name = tool_history.tool_use.name,
|
||||
id = tool_history.tool_use.id,
|
||||
input = vim.json.decode(tool_history.tool_use.input_json),
|
||||
})
|
||||
end
|
||||
table.insert(messages, {
|
||||
role = "assistant",
|
||||
content = assistant_content,
|
||||
})
|
||||
local user_content = {}
|
||||
if tool_history.tool_result ~= nil and tool_history.tool_result.content ~= nil then
|
||||
table.insert(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
|
||||
table.insert(messages, {
|
||||
role = "user",
|
||||
content = user_content,
|
||||
})
|
||||
end
|
||||
end
|
||||
local assistant_content = Utils.trim_think_content(entry.original_response or "")
|
||||
if assistant_content ~= "" then table.insert(messages, { role = "assistant", content = assistant_content }) end
|
||||
end
|
||||
return messages
|
||||
end
|
||||
|
||||
@@ -10,9 +10,29 @@ local cost_per_token = {
|
||||
}
|
||||
|
||||
--- Calculate the number of tokens in a given text.
|
||||
---@param text string The text to calculate the number of tokens for.
|
||||
---@param content AvanteLLMMessageContent The text to calculate the number of tokens in.
|
||||
---@return integer The number of tokens in the given text.
|
||||
function Tokens.calculate_tokens(text)
|
||||
function Tokens.calculate_tokens(content)
|
||||
local text = ""
|
||||
|
||||
if type(content) == "string" then
|
||||
text = content
|
||||
elseif type(content) == "table" then
|
||||
for _, item in ipairs(content) do
|
||||
if type(item) == "string" then
|
||||
text = text .. item
|
||||
elseif type(item) == "table" and item.type == "text" then
|
||||
text = text .. item.text
|
||||
elseif type(item) == "table" and item.type == "image" then
|
||||
text = text .. item.source.data
|
||||
elseif type(item) == "table" and item.type == "tool_use" then
|
||||
text = text .. item.name .. item.id
|
||||
elseif type(item) == "table" and item.type == "tool_result" then
|
||||
text = text .. item.tool_use_id .. item.content
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if Tokenizer.available() then return Tokenizer.count(text) end
|
||||
|
||||
local tokens = 0
|
||||
|
||||
Reference in New Issue
Block a user