From 448efbb8423c319e6d10e527dc0827c22b3748ff Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 1 May 2025 05:19:54 +0800 Subject: [PATCH] fix: disable compact history messages for dispatch agent (#1954) --- lua/avante/llm.lua | 46 ++++++++++++++----------- lua/avante/llm_tools/dispatch_agent.lua | 1 + lua/avante/types.lua | 1 + 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/lua/avante/llm.lua b/lua/avante/llm.lua index ddb2fe4..fa2795d 100644 --- a/lua/avante/llm.lua +++ b/lua/avante/llm.lua @@ -290,28 +290,34 @@ function M.generate_prompts(opts) local final_history_messages = {} if opts.history_messages then - if Config.history.max_tokens > 0 then remaining_tokens = math.min(Config.history.max_tokens, remaining_tokens) end - -- Traverse the history in reverse, keeping only the latest history until the remaining tokens are exhausted and the first message role is "user" - local history_messages = {} - for i = #opts.history_messages, 1, -1 do - local message = opts.history_messages[i] - local tokens = Utils.tokens.calculate_tokens(message.message.content) - remaining_tokens = remaining_tokens - tokens - if remaining_tokens > 0 then - table.insert(history_messages, 1, message) - else - break + if opts.disable_compact_history_messages then + final_history_messages = vim.list_extend(final_history_messages, opts.history_messages) + else + if Config.history.max_tokens > 0 then + remaining_tokens = math.min(Config.history.max_tokens, remaining_tokens) end + -- Traverse the history in reverse, keeping only the latest history until the remaining tokens are exhausted and the first message role is "user" + local history_messages = {} + for i = #opts.history_messages, 1, -1 do + local message = opts.history_messages[i] + local tokens = Utils.tokens.calculate_tokens(message.message.content) + remaining_tokens = remaining_tokens - tokens + if remaining_tokens > 0 then + table.insert(history_messages, 1, message) + else + break + end + end + + if #history_messages == 0 then + history_messages = vim.list_slice(opts.history_messages, #opts.history_messages - 1, #opts.history_messages) + end + + dropped_history_messages = vim.list_slice(opts.history_messages, 1, #opts.history_messages - #history_messages) + + -- prepend the history messages to the messages table + vim.iter(history_messages):each(function(msg) table.insert(final_history_messages, msg) end) end - - if #history_messages == 0 then - history_messages = vim.list_slice(opts.history_messages, #opts.history_messages - 1, #opts.history_messages) - end - - dropped_history_messages = vim.list_slice(opts.history_messages, 1, #opts.history_messages - #history_messages) - - -- prepend the history messages to the messages table - vim.iter(history_messages):each(function(msg) table.insert(final_history_messages, msg) end) end -- Utils.debug("opts.history_messages", opts.history_messages) diff --git a/lua/avante/llm_tools/dispatch_agent.lua b/lua/avante/llm_tools/dispatch_agent.lua index fe9ed7f..2a25c35 100644 --- a/lua/avante/llm_tools/dispatch_agent.lua +++ b/lua/avante/llm_tools/dispatch_agent.lua @@ -94,6 +94,7 @@ When you're done, provide a clear and concise summary of what you found.]]):gsub local stream_options = { ask = true, + disable_compact_history_messages = true, memory = memory_content, code_lang = "unknown", provider = Providers[Config.provider], diff --git a/lua/avante/types.lua b/lua/avante/types.lua index 64cc57f..2135a77 100644 --- a/lua/avante/types.lua +++ b/lua/avante/types.lua @@ -349,6 +349,7 @@ vim.g.avante_login = vim.g.avante_login ---@field update_snippets? string[] ---@field prompt_opts? AvantePromptOptions ---@field session_ctx? table +---@field disable_compact_history_messages? boolean --- ---@class AvanteLLMToolHistory ---@field tool_result? AvanteLLMToolResult