feat: trim think content in history messages (#1178)

This commit is contained in:
yetone 2025-02-04 15:25:36 +08:00 committed by GitHub
parent 85645fd9ab
commit a9ab429699
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 1 deletions

View File

@ -1638,7 +1638,11 @@ function Sidebar:create_input_container(opts)
then
break
end
table.insert(history_messages, 1, { role = "assistant", content = entry.original_response })
table.insert(
history_messages,
1,
{ role = "assistant", content = Utils.trim_think_content(entry.original_response) }
)
local user_content = ""
if entry.selected_file ~= nil then
user_content = user_content .. "SELECTED FILE: " .. entry.selected_file.filepath .. "\n\n"

View File

@ -858,4 +858,6 @@ end
function M.is_same_file(filepath_a, filepath_b) return M.uniform_path(filepath_a) == M.uniform_path(filepath_b) end
function M.trim_think_content(content) return content:gsub("^<think>.-</think>", "", 1) end
return M

View File

@ -121,6 +121,38 @@ describe("Utils", function()
end)
end)
describe("trim_think_content", function()
it("should remove think content", function()
local input = "<think>this should be removed</think> Hello World"
assert.equals(" Hello World", Utils.trim_think_content(input))
end)
it("The think tag that is not in the prefix should not be deleted.", function()
local input = "Hello <think>this should not be removed</think> World"
assert.equals("Hello <think>this should not be removed</think> World", Utils.trim_think_content(input))
end)
it("should handle multiple think blocks", function()
local input = "<think>first</think>middle<think>second</think>"
assert.equals("middle<think>second</think>", Utils.trim_think_content(input))
end)
it("should handle empty think blocks", function()
local input = "<think></think>testtest"
assert.equals("testtest", Utils.trim_think_content(input))
end)
it("should handle empty think blocks", function()
local input = "test<think></think>test"
assert.equals("test<think></think>test", Utils.trim_think_content(input))
end)
it("should handle input without think blocks", function()
local input = "just normal text"
assert.equals("just normal text", Utils.trim_think_content(input))
end)
end)
describe("debounce", function()
it("should debounce function calls", function()
local count = 0