From e82b159f6583a531f13e4e5d0185346dac7d75b2 Mon Sep 17 00:00:00 2001 From: Dmitry Torokhov Date: Thu, 17 Jul 2025 20:58:24 -0700 Subject: [PATCH] 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. --- lua/avante/history/helpers.lua | 18 ++++++++++++++++++ lua/avante/llm_tools/dispatch_agent.lua | 9 +-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lua/avante/history/helpers.lua b/lua/avante/history/helpers.lua index 4284f71..66f7f4c 100644 --- a/lua/avante/history/helpers.lua +++ b/lua/avante/history/helpers.lua @@ -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 diff --git a/lua/avante/llm_tools/dispatch_agent.lua b/lua/avante/llm_tools/dispatch_agent.lua index 3717209..ae99ffe 100644 --- a/lua/avante/llm_tools/dispatch_agent.lua +++ b/lua/avante/llm_tools/dispatch_agent.lua @@ -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