diff --git a/lua/avante/llm.lua b/lua/avante/llm.lua index d892755..4148719 100644 --- a/lua/avante/llm.lua +++ b/lua/avante/llm.lua @@ -865,30 +865,69 @@ function M._stream(opts) end return opts.on_stop({ reason = "cancelled" }) end - if stop_opts.reason == "tool_use" then - local tool_use_list = {} ---@type AvanteLLMToolUse[] - local tool_result_seen = {} - local history_messages = opts.get_history_messages and opts.get_history_messages() or {} - for idx = #history_messages, 1, -1 do - local message = history_messages[idx] - local content = message.message.content - if type(content) ~= "table" or #content == 0 then goto continue end - if content[1].type == "tool_use" then - if not tool_result_seen[content[1].id] then - table.insert(tool_use_list, 1, content[1]) + local tool_use_list = {} ---@type AvanteLLMToolUse[] + local tool_result_seen = {} + local history_messages = opts.get_history_messages and opts.get_history_messages() or {} + for idx = #history_messages, 1, -1 do + local message = history_messages[idx] + local content = message.message.content + if type(content) ~= "table" or #content == 0 then goto continue end + local is_break = false + for _, item in ipairs(content) do + if item.type == "tool_use" then + if not tool_result_seen[item.id] then + table.insert(tool_use_list, 1, item) else + is_break = true break end end - if content[1].type == "tool_result" then tool_result_seen[content[1].tool_use_id] = true end - ::continue:: + if item.type == "tool_result" then tool_result_seen[item.tool_use_id] = true end end - local sorted_tool_use_list = {} ---@type AvanteLLMToolUse[] - for _, tool_use in vim.spairs(tool_use_list) do - table.insert(sorted_tool_use_list, tool_use) - end - return handle_next_tool_use(sorted_tool_use_list, 1, {}) + if is_break then break end + ::continue:: end + local sorted_tool_use_list = {} ---@type AvanteLLMToolUse[] + for _, tool_use in vim.spairs(tool_use_list) do + table.insert(sorted_tool_use_list, tool_use) + end + if stop_opts.reason == "complete" and Config.mode == "agentic" then + if #sorted_tool_use_list == 0 then + local completed_attempt_completion_tool_use = nil + for idx = #history_messages, 1, -1 do + local message = history_messages[idx] + if message.is_user_submission then break end + if not Utils.is_tool_use_message(message) then goto continue end + if message.message.content[1].name ~= "attempt_completion" then break end + completed_attempt_completion_tool_use = message + if message then break end + ::continue:: + end + if not completed_attempt_completion_tool_use and opts.on_messages_add then + local message = HistoryMessage:new({ + role = "user", + content = "\ncontinue\n", + }, { + just_for_display = true, + }) + opts.on_messages_add({ message }) + local new_opts = vim.tbl_deep_extend("force", opts, { + history_messages = opts.get_history_messages(), + }) + if provider.get_rate_limit_sleep_time then + local sleep_time = provider:get_rate_limit_sleep_time(resp_headers) + if sleep_time and sleep_time > 0 then + Utils.info("Rate limit reached. Sleeping for " .. sleep_time .. " seconds ...") + vim.defer_fn(function() M._stream(new_opts) end, sleep_time * 1000) + return + end + end + M._stream(new_opts) + return + end + end + end + if stop_opts.reason == "tool_use" then return handle_next_tool_use(sorted_tool_use_list, 1, {}) end if stop_opts.reason == "rate_limit" then local msg_content = "*[Rate limit reached. Retrying in " .. stop_opts.retry_after .. " seconds ...]*" if opts.on_chunk then opts.on_chunk("\n" .. msg_content .. "\n") end diff --git a/lua/avante/llm_tools/attempt_completion.lua b/lua/avante/llm_tools/attempt_completion.lua new file mode 100644 index 0000000..4bc1d83 --- /dev/null +++ b/lua/avante/llm_tools/attempt_completion.lua @@ -0,0 +1,75 @@ +local Base = require("avante.llm_tools.base") +local Config = require("avante.config") +-- local HistoryMessage = require("avante.history_message") + +---@alias AttemptCompletionInput {result: string, command?: string} + +---@class AvanteLLMTool +local M = setmetatable({}, Base) + +M.name = "attempt_completion" + +M.description = [[ +After each tool use, the user will respond with the result of that tool use, i.e. if it succeeded or failed, along with any reasons for failure. Once you've received the results of tool uses and can confirm that the task is complete, use this tool to present the result of your work to the user. Optionally you may provide a CLI command to showcase the result of your work. The user may respond with feedback if they are not satisfied with the result, which you can use to make improvements and try again. +IMPORTANT NOTE: This tool CANNOT be used until you've confirmed from the user that any previous tool uses were successful. Failure to do so will result in code corruption and system failure. Before using this tool, you must ask yourself in tags if you've confirmed from the user that any previous tool uses were successful. If not, then DO NOT use this tool. +]] + +M.enabled = function() return Config.mode == "agentic" end + +---@type AvanteLLMToolParam +M.param = { + type = "table", + fields = { + { + name = "result", + description = "The result of the task. Formulate this result in a way that is final and does not require further input from the user. Don't end your result with questions or offers for further assistance.", + type = "string", + }, + { + name = "command", + description = [[A CLI command to execute to show a live demo of the result to the user. For example, use \`open index.html\` to display a created html website, or \`open localhost:3000\` to display a locally running development server. But DO NOT use commands like \`echo\` or \`cat\` that merely print text. This command should be valid for the current operating system. Ensure the command is properly formatted and does not contain any harmful instructions.]], + type = "string", + optional = true, + }, + }, +} + +---@type AvanteLLMToolReturn[] +M.returns = { + { + name = "success", + description = "Whether the task was completed successfully", + type = "boolean", + }, + { + name = "error", + description = "Error message if the file was not read successfully", + type = "string", + optional = true, + }, +} + +---@type AvanteLLMToolOnRender +function M.on_render() return {} end + +---@type AvanteLLMToolFunc +function M.func(opts, on_log, on_complete, session_ctx) + if not on_complete then return false, "on_complete not provided" end + local sidebar = require("avante").get() + if not sidebar then return false, "Avante sidebar not found" end + session_ctx.attempt_completion_is_called = true + -- local message = HistoryMessage:new({ + -- role = "assistant", + -- content = opts.result, + -- }, { + -- just_for_display = true, + -- }) + -- sidebar:add_history_messages({ message }) + if opts.command then + require("avante.llm_tools.bash").func({ command = opts.command }, on_log, on_complete, session_ctx) + else + on_complete(true, nil) + end +end + +return M diff --git a/lua/avante/llm_tools/init.lua b/lua/avante/llm_tools/init.lua index 733826e..d39e350 100644 --- a/lua/avante/llm_tools/init.lua +++ b/lua/avante/llm_tools/init.lua @@ -1029,6 +1029,7 @@ M._tools = { }, require("avante.llm_tools.get_diagnostics"), require("avante.llm_tools.bash"), + require("avante.llm_tools.attempt_completion"), { name = "web_search", description = "Search the web", diff --git a/lua/avante/templates/_tools-guidelines.avanterules b/lua/avante/templates/_tools-guidelines.avanterules index 77418f4..1cce91e 100644 --- a/lua/avante/templates/_tools-guidelines.avanterules +++ b/lua/avante/templates/_tools-guidelines.avanterules @@ -1,23 +1,26 @@ Don't directly search for code context in historical messages. Instead, prioritize using tools to obtain context first, then use context from historical messages as a secondary source, since context from historical messages is often not up to date. -Tools Usage Guide: - - You have access to tools, but only use them when necessary. If a tool is not required, respond as normal. - - Please DON'T be so aggressive in using tools, as many tasks can be better completed without tools. - - Files will be provided to you as context through tag! - - Before using the `view` tool each time, always repeatedly check whether the file is already in the tag. If it is already there, do not use the `view` tool, just read the file content directly from the tag. - - If you use the `view` tool when file content is already provided in the tag, you will be fired! - - If the `rag_search` tool exists, prioritize using it to do the search! - - If the `rag_search` tool exists, only use tools like `search_keyword` `search_files` `view` `list_files` etc when absolutely necessary! - - Keep the `query` parameter of `rag_search` tool as concise as possible! Try to keep it within five English words! - - If you encounter a URL, prioritize using the `fetch` tool to obtain its content. - - If you have information that you don't know, please proactively use the tools provided by users! Especially the `web_search` tool. - - When available tools cannot meet the requirements, please try to use the `run_command` tool to solve the problem whenever possible. - - When attempting to modify a file that is not in the context, please first use the `list_files` tool and `search_files` tool to check if the file you want to modify exists, then use the `view` tool to read the file content. Don't modify blindly! - - When generating files, first use `list_files` tool to read the directory structure, don't generate blindly! - - When creating files, first check if the directory exists. If it doesn't exist, create the directory before creating the file. - - After `web_search` tool returns, if you don't get detailed enough information, do not continue use `web_search` tool, just continue using the `fetch` tool to get more information you need from the links in the search results. - - For any mathematical calculation problems, please prioritize using the `python` tool to solve them. Please try to avoid mathematical symbols in the return value of the `python` tool for mathematical problems and directly output human-readable results, because large models don't understand mathematical symbols, they only understand human natural language. - - Do not use the `python` tool to read or modify files! If you use the `python` tool to read or modify files, you will be fired!!!!! - - Do not use the `bash` tool to read or modify files! If you use the `bash` tool to read or modify files, you will be fired!!!!! - - If you are provided with the `write_file` tool, there's no need to output your change suggestions, just directly use the `write_file` tool to complete the changes. - - Before each tool call, explain the reason why you're using the tool +==== + +TOOLS USAGE GUIDE + +- You have access to tools, but only use them when necessary. If a tool is not required, respond as normal. +- Please DON'T be so aggressive in using tools, as many tasks can be better completed without tools. +- Files will be provided to you as context through tag! +- Before using the `view` tool each time, always repeatedly check whether the file is already in the tag. If it is already there, do not use the `view` tool, just read the file content directly from the tag. +- If you use the `view` tool when file content is already provided in the tag, you will be fired! +- If the `rag_search` tool exists, prioritize using it to do the search! +- If the `rag_search` tool exists, only use tools like `search_keyword` `search_files` `view` `list_files` etc when absolutely necessary! +- Keep the `query` parameter of `rag_search` tool as concise as possible! Try to keep it within five English words! +- If you encounter a URL, prioritize using the `fetch` tool to obtain its content. +- If you have information that you don't know, please proactively use the tools provided by users! Especially the `web_search` tool. +- When available tools cannot meet the requirements, please try to use the `run_command` tool to solve the problem whenever possible. +- When attempting to modify a file that is not in the context, please first use the `list_files` tool and `search_files` tool to check if the file you want to modify exists, then use the `view` tool to read the file content. Don't modify blindly! +- When generating files, first use `list_files` tool to read the directory structure, don't generate blindly! +- When creating files, first check if the directory exists. If it doesn't exist, create the directory before creating the file. +- After `web_search` tool returns, if you don't get detailed enough information, do not continue use `web_search` tool, just continue using the `fetch` tool to get more information you need from the links in the search results. +- For any mathematical calculation problems, please prioritize using the `python` tool to solve them. Please try to avoid mathematical symbols in the return value of the `python` tool for mathematical problems and directly output human-readable results, because large models don't understand mathematical symbols, they only understand human natural language. +- Do not use the `run_python` tool to read or modify files! If you use the `python` tool to read or modify files, you will be fired!!!!! +- Do not use the `bash` tool to read or modify files! If you use the `bash` tool to read or modify files, you will be fired!!!!! +- If you are provided with the `write_file` tool, there's no need to output your change suggestions, just directly use the `write_file` tool to complete the changes. +- Before each tool call, explain the reason why you're using the tool diff --git a/lua/avante/templates/agentic.avanterules b/lua/avante/templates/agentic.avanterules index 0290953..cd2a6b4 100644 --- a/lua/avante/templates/agentic.avanterules +++ b/lua/avante/templates/agentic.avanterules @@ -1,10 +1,33 @@ {% extends "base.avanterules" %} {% block extra_prompt %} -Always reply to the user in the same language they are using. -Don't just provide code suggestions, use the `replace_in_file` tool to help users fulfill their needs. +==== + +RULES + +- Always reply to the user in the same language they are using. + +- Don't just provide code suggestions, use the `replace_in_file` tool to help users fulfill their needs. + +- After the tool call is complete, please do not output the entire file content. + +- Before calling the tool, be sure to explain the reason for calling the tool. + +- Do not ask for more information than necessary. Use the tools provided to accomplish the user's request efficiently and effectively. When you've completed your task, you must use the attempt_completion tool to present the result to the user. The user may provide feedback, which you can use to make improvements and try again. + +- NEVER end attempt_completion result with a question or request to engage in further conversation! Formulate the end of your result in a way that is final and does not require further input from the user. + +==== + +OBJECTIVE + +You accomplish a given task iteratively, breaking it down into clear steps and working through them methodically. + +1. Analyze the user's task and set clear, achievable goals to accomplish it. Prioritize these goals in a logical order. +2. Work through these goals sequentially, utilizing available tools one at a time as necessary. Each goal should correspond to a distinct step in your problem-solving process. You will be informed on the work completed and what's remaining as you go. +3. Remember, you have extensive capabilities with access to a wide range of tools that can be used in powerful and clever ways as necessary to accomplish each goal. Before calling a tool, do some analysis within tags. First, analyze the file structure provided in environment_details to gain context and insights for proceeding effectively. Then, think about which of the provided tools is the most relevant tool to accomplish the user's task. Next, go through each of the required parameters of the relevant tool and determine if the user has directly provided or given enough information to infer a value. When deciding if the parameter can be inferred, carefully consider all the context to see if it supports a specific value. If all of the required parameters are present or can be reasonably inferred, close the thinking tag and proceed with the tool use. BUT, if one of the values for a required parameter is missing, DO NOT invoke the tool (not even with fillers for the missing params) and instead, ask the user to provide the missing parameters using the ask_followup_question tool. DO NOT ask for more information on optional parameters if it is not provided. +4. Once you've completed the user's task, you must use the attempt_completion tool to present the result of the task to the user. You may also provide a CLI command to showcase the result of your task; this can be particularly useful for web development tasks, where you can run e.g. \`open index.html\` to show the website you've built. +5. The user may provide feedback, which you can use to make improvements and try again. But DO NOT continue in pointless back and forth conversations, i.e. don't end your responses with questions or offers for further assistance.` -After the tool call is complete, please do not output the entire file content. -Before calling the tool, be sure to explain the reason for calling the tool. {% endblock %} diff --git a/lua/avante/templates/base.avanterules b/lua/avante/templates/base.avanterules index a6a6dc8..3a8a144 100644 --- a/lua/avante/templates/base.avanterules +++ b/lua/avante/templates/base.avanterules @@ -9,7 +9,10 @@ Memory is crucial, you must follow the instructions in ! {% include "_tools-guidelines.avanterules" %} {% if system_info -%} -Use the appropriate shell based on the user's system info: +==== + +SYSTEM INFORMATION + {{system_info}} {%- endif %} diff --git a/lua/avante/types.lua b/lua/avante/types.lua index 7862b7b..c390973 100644 --- a/lua/avante/types.lua +++ b/lua/avante/types.lua @@ -378,6 +378,8 @@ vim.g.avante_login = vim.g.avante_login --- session_ctx?: table) --- : (boolean | string | nil, string | nil) --- +--- @alias AvanteLLMToolOnRender fun(input: T, logs: string[]): avante.ui.Line[] +--- ---@class AvanteLLMTool ---@field name string ---@field description? string @@ -386,6 +388,7 @@ vim.g.avante_login = vim.g.avante_login ---@field param AvanteLLMToolParam ---@field returns AvanteLLMToolReturn[] ---@field enabled? fun(opts: { user_input: string, history_messages: AvanteLLMMessage[] }): boolean +---@field on_render? AvanteLLMToolOnRender ---@class AvanteLLMToolPublic : AvanteLLMTool ---@field func AvanteLLMToolFunc diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index 3baa087..93b2bbd 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -1494,6 +1494,10 @@ function M.message_content_item_to_lines(item, message, messages) local lines = {} local state = "generating" local hl = "AvanteStateSpinnerToolCalling" + local ok, llm_tool = pcall(require, "avante.llm_tools." .. item.name) + if ok then + if llm_tool.on_render then return llm_tool.on_render(item.input, message.tool_use_logs) end + end local tool_result_message = M.get_tool_result_message(message, messages) if tool_result_message then local tool_result = tool_result_message.message.content[1]