refactor(history): add a helper to fetch text from messages and use it

This adds History.Helpers.get_text_data() helper to fetch contents of a
"text" message. This removes the need to know internals of message
structure outside of history module.
This commit is contained in:
Dmitry Torokhov
2025-07-17 20:58:24 -07:00
committed by yetone
parent 663433a5ed
commit e82b159f65
2 changed files with 19 additions and 8 deletions

View File

@@ -2,6 +2,24 @@ local Utils = require("avante.utils")
local M = {}
---If message is a text message return the text.
---@param message avante.HistoryMessage
---@return string | nil
function M.get_text_data(message)
local content = message.message.content
if type(content) == "table" then
assert(#content == 1, "more than one entry in message content")
local item = content[1]
if type(item) == "string" then
return item
elseif type(item) == "table" and item.type == "text" then
return item.content
end
elseif type(content) == "string" then
return content
end
end
---If message is a "tool use" message returns information about the tool invocation.
---@param message avante.HistoryMessage
---@return AvanteLLMToolUse | nil

View File

@@ -140,14 +140,7 @@ function M.on_render(input, opts)
end
if summary then summary = " " .. Utils.icon("🛠️ ") .. summary end
else
local content = msg.message.content
if type(content) == "table" and #content > 0 and type(content[1]) == "table" and content[1].type == "text" then
summary = content[1].content
elseif type(content) == "table" and #content > 0 and type(content[1]) == "string" then
summary = content[1]
elseif type(content) == "string" then
summary = content
end
summary = History.Helpers.get_text_data(msg)
end
if summary then table.insert(tool_use_summary, summary) end
end