diff --git a/lua/avante/llm.lua b/lua/avante/llm.lua index 615794c..a31ed5b 100644 --- a/lua/avante/llm.lua +++ b/lua/avante/llm.lua @@ -699,7 +699,16 @@ function M._stream(opts) -- Special handling for cancellation signal from tools if error == LLMToolHelpers.CANCEL_TOKEN then Utils.debug("Tool execution was cancelled by user") - opts.on_chunk("\n*[Request cancelled by user during tool execution.]*\n") + if opts.on_chunk then opts.on_chunk("\n*[Request cancelled by user during tool execution.]*\n") end + if opts.on_messages_add then + local message = HistoryMessage:new({ + role = "assistant", + content = "\n*[Request cancelled by user during tool execution.]*\n", + }, { + just_for_display = true, + }) + opts.on_messages_add({ message }) + end return opts.on_stop({ reason = "cancelled" }) end @@ -725,8 +734,10 @@ function M._stream(opts) if opts.on_chunk then opts.on_chunk("\n*[Request cancelled by user.]*\n") end if opts.on_messages_add then local message = HistoryMessage:new({ - role = "user", - content = "[Request cancelled by user.]", + role = "assistant", + content = "\n*[Request cancelled by user.]*\n", + }, { + just_for_display = true, }) opts.on_messages_add({ message }) end diff --git a/lua/avante/llm_tools/init.lua b/lua/avante/llm_tools/init.lua index 9ff979f..80ec481 100644 --- a/lua/avante/llm_tools/init.lua +++ b/lua/avante/llm_tools/init.lua @@ -1148,6 +1148,7 @@ function M.process_tool_use(tools, tool_use, on_log, on_complete, session_ctx) cancel_timer:stop() cancel_timer:close() end + Helpers.is_cancelled = false on_complete(nil, Helpers.CANCEL_TOKEN) end end) @@ -1191,6 +1192,7 @@ function M.process_tool_use(tools, tool_use, on_log, on_complete, session_ctx) end, function(result, err) -- Check for cancellation before completing if Helpers.is_cancelled then + Helpers.is_cancelled = false if on_complete then on_complete(nil, Helpers.CANCEL_TOKEN) end return end diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index 7df1882..b267458 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -1513,9 +1513,8 @@ end ---@param messages avante.HistoryMessage[] ---@return avante.ui.Line[] function M.message_to_lines(message, messages) - local Line = require("avante.ui.line") local content = message.message.content - if type(content) == "string" then return { Line:new({ { content } }) } end + if type(content) == "string" then return M.text_to_lines(content) end if vim.islist(content) then local lines = {} for _, item in ipairs(content) do