diff --git a/lua/avante/llm.lua b/lua/avante/llm.lua index 22695b7..c9f8481 100644 --- a/lua/avante/llm.lua +++ b/lua/avante/llm.lua @@ -309,10 +309,7 @@ function M.generate_prompts(opts) memory = opts.memory, } - if opts.get_todos then - local todos = opts.get_todos() - if todos and #todos > 0 then template_opts.todos = vim.json.encode(todos) end - end + -- Removed the original todos processing logic, now handled in context_messages local system_prompt if opts.prompt_opts and opts.prompt_opts.system_prompt then @@ -418,6 +415,30 @@ function M.generate_prompts(opts) messages = vim.list_extend(messages, { { role = "user", content = opts.instructions } }) end + if opts.get_todos then + local todos = opts.get_todos() + if todos and #todos > 0 then + -- Remove existing todos-related messages - use more precise tag matching + messages = vim + .iter(messages) + :filter(function(msg) + if not msg.content or type(msg.content) ~= "string" then return true end + -- Only filter out messages that start with and end with to avoid accidentally deleting other messages + return not msg.content:match("^.*$") + end) + :totable() + + -- Add the latest todos to the end of messages, wrapped in tags + local todos_content = vim.json.encode(todos) + table.insert(messages, { + role = "user", + content = "\n" .. todos_content .. "\n", + visible = false, + is_context = true, + }) + end + end + opts.session_ctx = opts.session_ctx or {} opts.session_ctx.system_prompt = system_prompt opts.session_ctx.messages = messages diff --git a/lua/avante/templates/_task-guidelines.avanterules b/lua/avante/templates/_task-guidelines.avanterules index ec2735f..7611c3f 100644 --- a/lua/avante/templates/_task-guidelines.avanterules +++ b/lua/avante/templates/_task-guidelines.avanterules @@ -1,13 +1,3 @@ -{% if todos -%} -==== - -# Task TODOs - -{{todos}} - -==== -{%- endif %} - # Task Management You have access to the add_todos and update_todo_status tools to help you manage and plan tasks. Use these tools VERY frequently to ensure that you are tracking your tasks and giving the user visibility into your progress. These tools are also EXTREMELY helpful for planning tasks, and for breaking down larger complex tasks into smaller steps. If you do not use this tool when planning, you may forget to do important tasks - and that is unacceptable.