From b7959191a33d3736a4a807e9610aadba51264627 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 7 Jun 2025 17:18:37 +0800 Subject: [PATCH] fix: claude assistant content cannot end with trailing whitespace (#2186) --- lua/avante/providers/claude.lua | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lua/avante/providers/claude.lua b/lua/avante/providers/claude.lua index 264046e..8d9c3c8 100644 --- a/lua/avante/providers/claude.lua +++ b/lua/avante/providers/claude.lua @@ -77,15 +77,19 @@ function M:parse_messages(opts) local content_items = message.content local message_content = {} if type(content_items) == "string" then - table.insert(message_content, { - type = "text", - text = message.content, - cache_control = top_two[idx] and { type = "ephemeral" } or nil, - }) + if message.role == "assistant" then content_items = content_items:gsub("%s+$", "") end + if content_items ~= "" then + table.insert(message_content, { + type = "text", + text = content_items, + cache_control = top_two[idx] and { type = "ephemeral" } or nil, + }) + end elseif type(content_items) == "table" then ---@cast content_items AvanteLLMMessageContentItem[] for _, item in ipairs(content_items) do if type(item) == "string" then + if message.role == "assistant" then item = item:gsub("%s+$", "") end table.insert( message_content, { type = "text", text = item, cache_control = top_two[idx] and { type = "ephemeral" } or nil }