refactor: llm tools (#1675)
This commit is contained in:
@@ -8,6 +8,7 @@ local Utils = require("avante.utils")
|
||||
local Config = require("avante.config")
|
||||
local Path = require("avante.path")
|
||||
local Providers = require("avante.providers")
|
||||
local LLMToolHelpers = require("avante.llm_tools.helpers")
|
||||
local LLMTools = require("avante.llm_tools")
|
||||
|
||||
---@class avante.LLM
|
||||
@@ -125,6 +126,7 @@ end
|
||||
---@param opts AvanteGeneratePromptsOptions
|
||||
---@return AvantePromptOptions
|
||||
function M.generate_prompts(opts)
|
||||
if opts.prompt_opts then return opts.prompt_opts end
|
||||
local provider = opts.provider or Providers[Config.provider]
|
||||
local mode = opts.mode or "planning"
|
||||
---@type AvanteProviderFunctor | AvanteBedrockProviderFunctor
|
||||
@@ -480,7 +482,7 @@ end
|
||||
---@param opts AvanteLLMStreamOptions
|
||||
function M._stream(opts)
|
||||
-- Reset the cancellation flag at the start of a new request
|
||||
if LLMTools then LLMTools.is_cancelled = false end
|
||||
if LLMToolHelpers then LLMToolHelpers.is_cancelled = false end
|
||||
|
||||
local provider = opts.provider or Providers[Config.provider]
|
||||
|
||||
@@ -519,7 +521,7 @@ function M._stream(opts)
|
||||
---@param error string | nil
|
||||
local function handle_tool_result(result, error)
|
||||
-- Special handling for cancellation signal from tools
|
||||
if error == LLMTools.CANCEL_TOKEN then
|
||||
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")
|
||||
return opts.on_stop({ reason = "cancelled", tool_histories = tool_histories })
|
||||
@@ -722,10 +724,10 @@ function M.stream(opts)
|
||||
end
|
||||
|
||||
function M.cancel_inflight_request()
|
||||
if LLMTools.is_cancelled ~= nil then LLMTools.is_cancelled = true end
|
||||
if LLMTools.confirm_popup ~= nil then
|
||||
LLMTools.confirm_popup:cancel()
|
||||
LLMTools.confirm_popup = nil
|
||||
if LLMToolHelpers.is_cancelled ~= nil then LLMToolHelpers.is_cancelled = true end
|
||||
if LLMToolHelpers.confirm_popup ~= nil then
|
||||
LLMToolHelpers.confirm_popup:cancel()
|
||||
LLMToolHelpers.confirm_popup = nil
|
||||
end
|
||||
|
||||
api.nvim_exec_autocmds("User", { pattern = M.CANCEL_PATTERN })
|
||||
|
||||
5
lua/avante/llm_tools/base.lua
Normal file
5
lua/avante/llm_tools/base.lua
Normal file
@@ -0,0 +1,5 @@
|
||||
local M = {}
|
||||
|
||||
function M:__call(opts, on_log, on_complete) return self.func(opts, on_log, on_complete) end
|
||||
|
||||
return M
|
||||
224
lua/avante/llm_tools/bash.lua
Normal file
224
lua/avante/llm_tools/bash.lua
Normal file
@@ -0,0 +1,224 @@
|
||||
local Path = require("plenary.path")
|
||||
local Utils = require("avante.utils")
|
||||
local Helpers = require("avante.llm_tools.helpers")
|
||||
local Base = require("avante.llm_tools.base")
|
||||
|
||||
---@class AvanteLLMTool
|
||||
local M = setmetatable({}, Base)
|
||||
|
||||
M.name = "bash"
|
||||
|
||||
M.description =
|
||||
[[Executes a given bash command in a persistent shell session with optional timeout, ensuring proper handling and security measures.
|
||||
|
||||
Before executing the command, please follow these steps:
|
||||
|
||||
1. Directory Verification:
|
||||
- If the command will create new directories or files, first use the LS tool to verify the parent directory exists and is the correct location
|
||||
- For example, before running "mkdir foo/bar", first use LS to check that "foo" exists and is the intended parent directory
|
||||
|
||||
2. Security Check:
|
||||
- For security and to limit the threat of a prompt injection attack, some commands are limited or banned. If you use a disallowed command, you will receive an error message explaining the restriction. Explain the error to the User.
|
||||
- Verify that the command is not one of the banned commands: ${BANNED_COMMANDS.join(', ')}.
|
||||
|
||||
3. Command Execution:
|
||||
- After ensuring proper quoting, execute the command.
|
||||
- Capture the output of the command.
|
||||
|
||||
4. Output Processing:
|
||||
- If the output exceeds ${MAX_OUTPUT_LENGTH} characters, output will be truncated before being returned to you.
|
||||
- Prepare the output for display to the user.
|
||||
|
||||
5. Return Result:
|
||||
- Provide the processed output of the command.
|
||||
- If any errors occurred during execution, include those in the output.
|
||||
|
||||
Usage notes:
|
||||
- The command argument is required.
|
||||
- You can specify an optional timeout in milliseconds (up to 600000ms / 10 minutes). If not specified, commands will timeout after 30 minutes.
|
||||
- VERY IMPORTANT: You MUST avoid using search commands like \`find\` and \`grep\`. Instead use ${GrepTool.name}, ${GlobTool.name}, or ${AgentTool.name} to search. You MUST avoid read tools like \`cat\`, \`head\`, \`tail\`, and \`ls\`, and use ${FileReadTool.name} and ${LSTool.name} to read files.
|
||||
- When issuing multiple commands, use the ';' or '&&' operator to separate them. DO NOT use newlines (newlines are ok in quoted strings).
|
||||
- IMPORTANT: All commands share the same shell session. Shell state (environment variables, virtual environments, current directory, etc.) persist between commands. For example, if you set an environment variable as part of a command, the environment variable will persist for subsequent commands.
|
||||
- Try to maintain your current working directory throughout the session by using absolute paths and avoiding usage of \`cd\`. You may use \`cd\` if the User explicitly requests it.
|
||||
<good-example>
|
||||
pytest /foo/bar/tests
|
||||
</good-example>
|
||||
<bad-example>
|
||||
cd /foo/bar && pytest tests
|
||||
</bad-example>
|
||||
|
||||
# Committing changes with git
|
||||
|
||||
When the user asks you to create a new git commit, follow these steps carefully:
|
||||
|
||||
1. Start with a single message that contains exactly three tool_use blocks that do the following (it is VERY IMPORTANT that you send these tool_use blocks in a single message, otherwise it will feel slow to the user!):
|
||||
- Run a git status command to see all untracked files.
|
||||
- Run a git diff command to see both staged and unstaged changes that will be committed.
|
||||
- Run a git log command to see recent commit messages, so that you can follow this repository's commit message style.
|
||||
|
||||
2. Use the git context at the start of this conversation to determine which files are relevant to your commit. Add relevant untracked files to the staging area. Do not commit files that were already modified at the start of this conversation, if they are not relevant to your commit.
|
||||
|
||||
3. Analyze all staged changes (both previously staged and newly added) and draft a commit message. Wrap your analysis process in <commit_analysis> tags:
|
||||
|
||||
<commit_analysis>
|
||||
- List the files that have been changed or added
|
||||
- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
|
||||
- Brainstorm the purpose or motivation behind these changes
|
||||
- Do not use tools to explore code, beyond what is available in the git context
|
||||
- Assess the impact of these changes on the overall project
|
||||
- Check for any sensitive information that shouldn't be committed
|
||||
- Draft a concise (1-2 sentences) commit message that focuses on the "why" rather than the "what"
|
||||
- Ensure your language is clear, concise, and to the point
|
||||
- Ensure the message accurately reflects the changes and their purpose (i.e. "add" means a wholly new feature, "update" means an enhancement to an existing feature, "fix" means a bug fix, etc.)
|
||||
- Ensure the message is not generic (avoid words like "Update" or "Fix" without context)
|
||||
- Review the draft message to ensure it accurately reflects the changes and their purpose
|
||||
</commit_analysis>
|
||||
|
||||
4. Create the commit with a message ending with:
|
||||
🤖 Generated with [avante.nvim](https://github.com/yetone/avante.nvim)
|
||||
Co-Authored-By: avante.nvim <noreply-avante@yetone.ai>
|
||||
|
||||
- In order to ensure good formatting, ALWAYS pass the commit message via a HEREDOC, a la this example:
|
||||
<example>
|
||||
git commit -m "$(cat <<'EOF'
|
||||
Commit message here.
|
||||
|
||||
🤖 Generated with [avante.nvim](https://github.com/yetone/avante.nvim)
|
||||
Co-Authored-By: avante.nvim <noreply-avante@yetone.ai>
|
||||
EOF
|
||||
)"
|
||||
</example>
|
||||
|
||||
5. If the commit fails due to pre-commit hook changes, retry the commit ONCE to include these automated changes. If it fails again, it usually means a pre-commit hook is preventing the commit. If the commit succeeds but you notice that files were modified by the pre-commit hook, you MUST amend your commit to include them.
|
||||
|
||||
6. Finally, run git status to make sure the commit succeeded.
|
||||
|
||||
Important notes:
|
||||
- When possible, combine the "git add" and "git commit" commands into a single "git commit -am" command, to speed things up
|
||||
- However, be careful not to stage files (e.g. with \`git add .\`) for commits that aren't part of the change, they may have untracked files they want to keep around, but not commit.
|
||||
- NEVER update the git config
|
||||
- DO NOT push to the remote repository
|
||||
- IMPORTANT: Never use git commands with the -i flag (like git rebase -i or git add -i) since they require interactive input which is not supported.
|
||||
- If there are no changes to commit (i.e., no untracked files and no modifications), do not create an empty commit
|
||||
- Ensure your commit message is meaningful and concise. It should explain the purpose of the changes, not just describe them.
|
||||
- Return an empty response - the user will see the git output directly
|
||||
|
||||
# Creating pull requests
|
||||
Use the gh command via the Bash tool for ALL GitHub-related tasks including working with issues, pull requests, checks, and releases. If given a Github URL use the gh command to get the information needed.
|
||||
|
||||
IMPORTANT: When the user asks you to create a pull request, follow these steps carefully:
|
||||
|
||||
1. Understand the current state of the branch. Remember to send a single message that contains multiple tool_use blocks (it is VERY IMPORTANT that you do this in a single message, otherwise it will feel slow to the user!):
|
||||
- Run a git status command to see all untracked files.
|
||||
- Run a git diff command to see both staged and unstaged changes that will be committed.
|
||||
- Check if the current branch tracks a remote branch and is up to date with the remote, so you know if you need to push to the remote
|
||||
- Run a git log command and \`git diff main...HEAD\` to understand the full commit history for the current branch (from the time it diverged from the \`main\` branch.)
|
||||
|
||||
2. Create new branch if needed
|
||||
|
||||
3. Commit changes if needed
|
||||
|
||||
4. Push to remote with -u flag if needed
|
||||
|
||||
5. Analyze all changes that will be included in the pull request, making sure to look at all relevant commits (not just the latest commit, but all commits that will be included in the pull request!), and draft a pull request summary. Wrap your analysis process in <pr_analysis> tags:
|
||||
|
||||
<pr_analysis>
|
||||
- List the commits since diverging from the main branch
|
||||
- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
|
||||
- Brainstorm the purpose or motivation behind these changes
|
||||
- Assess the impact of these changes on the overall project
|
||||
- Do not use tools to explore code, beyond what is available in the git context
|
||||
- Check for any sensitive information that shouldn't be committed
|
||||
- Draft a concise (1-2 bullet points) pull request summary that focuses on the "why" rather than the "what"
|
||||
- Ensure the summary accurately reflects all changes since diverging from the main branch
|
||||
- Ensure your language is clear, concise, and to the point
|
||||
- Ensure the summary accurately reflects the changes and their purpose (ie. "add" means a wholly new feature, "update" means an enhancement to an existing feature, "fix" means a bug fix, etc.)
|
||||
- Ensure the summary is not generic (avoid words like "Update" or "Fix" without context)
|
||||
- Review the draft summary to ensure it accurately reflects the changes and their purpose
|
||||
</pr_analysis>
|
||||
|
||||
6. Create PR using gh pr create with the format below. Use a HEREDOC to pass the body to ensure correct formatting.
|
||||
<example>
|
||||
gh pr create --title "the pr title" --body "$(cat <<'EOF'
|
||||
## Summary
|
||||
<1-3 bullet points>
|
||||
|
||||
## Test plan
|
||||
[Checklist of TODOs for testing the pull request...]
|
||||
|
||||
🤖 Generated with ${process.env.USER_TYPE === 'ant' ? `[${PRODUCT_NAME}](${PRODUCT_URL})` : PRODUCT_NAME}
|
||||
EOF
|
||||
)"
|
||||
</example>
|
||||
|
||||
Important:
|
||||
- Return an empty response - the user will see the gh output directly
|
||||
- Never update git config]]
|
||||
|
||||
---@type AvanteLLMToolParam
|
||||
M.param = {
|
||||
type = "table",
|
||||
fields = {
|
||||
{
|
||||
name = "rel_path",
|
||||
description = "Relative path to the project directory, as cwd",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "command",
|
||||
description = "Command to run",
|
||||
type = "string",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
---@type AvanteLLMToolReturn[]
|
||||
M.returns = {
|
||||
{
|
||||
name = "stdout",
|
||||
description = "Output of the command",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "error",
|
||||
description = "Error message if the command was not run successfully",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
}
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string, command: string }>
|
||||
function M.func(opts, on_log, on_complete)
|
||||
local abs_path = Helpers.get_abs_path(opts.rel_path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
if not Path:new(abs_path):exists() then return false, "Path not found: " .. abs_path end
|
||||
if on_log then on_log("command: " .. opts.command) end
|
||||
---change cwd to abs_path
|
||||
---@param output string
|
||||
---@param exit_code integer
|
||||
---@return string | boolean | nil result
|
||||
---@return string | nil error
|
||||
local function handle_result(output, exit_code)
|
||||
if exit_code ~= 0 then
|
||||
if output then return false, "Error: " .. output .. "; Error code: " .. tostring(exit_code) end
|
||||
return false, "Error code: " .. tostring(exit_code)
|
||||
end
|
||||
return output, nil
|
||||
end
|
||||
if not on_complete then return false, "on_complete not provided" end
|
||||
Helpers.confirm(
|
||||
"Are you sure you want to run the command: `" .. opts.command .. "` in the directory: " .. abs_path,
|
||||
function(ok)
|
||||
if not ok then
|
||||
on_complete(false, "User canceled")
|
||||
return
|
||||
end
|
||||
Utils.shell_run_async(opts.command, "bash -c", function(output, exit_code)
|
||||
local result, err = handle_result(output, exit_code)
|
||||
on_complete(result, err)
|
||||
end, abs_path)
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
return M
|
||||
118
lua/avante/llm_tools/dispatch_agent.lua
Normal file
118
lua/avante/llm_tools/dispatch_agent.lua
Normal file
@@ -0,0 +1,118 @@
|
||||
local Providers = require("avante.providers")
|
||||
local Config = require("avante.config")
|
||||
local Utils = require("avante.utils")
|
||||
local Base = require("avante.llm_tools.base")
|
||||
|
||||
---@class AvanteLLMTool
|
||||
local M = setmetatable({}, Base)
|
||||
|
||||
M.name = "dispatch_agent"
|
||||
|
||||
M.description =
|
||||
[[Launch a new agent that has access to the following tools: `glob`, `grep`, `ls`, `read_file`. When you are searching for a keyword or file and are not confident that you will find the right match on the first try, use the Agent tool to perform the search for you. For example:
|
||||
|
||||
- If you are searching for a keyword like "config" or "logger", the Agent tool is appropriate
|
||||
- If you want to read a specific file path, use the `read_file` or `glob` tool instead of the `dispatch_agent` tool, to find the match more quickly
|
||||
- If you are searching for a specific class definition like "class Foo", use the `glob` tool instead, to find the match more quickly
|
||||
|
||||
Usage notes:
|
||||
1. Launch multiple agents concurrently whenever possible, to maximize performance
|
||||
2. When the agent is done, it will return a single message back to you
|
||||
3. Each agent invocation is stateless
|
||||
4. The agent's outputs should generally be trusted]]
|
||||
|
||||
---@type AvanteLLMToolParam
|
||||
M.param = {
|
||||
type = "table",
|
||||
fields = {
|
||||
{
|
||||
name = "prompt",
|
||||
description = "The task for the agent to perform",
|
||||
type = "string",
|
||||
},
|
||||
},
|
||||
required = { "prompt" },
|
||||
}
|
||||
|
||||
---@type AvanteLLMToolReturn[]
|
||||
M.returns = {
|
||||
{
|
||||
name = "result",
|
||||
description = "The result of the agent",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "error",
|
||||
description = "The error message if the agent fails",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
}
|
||||
|
||||
local function get_available_tools()
|
||||
return {
|
||||
require("avante.llm_tools.ls"),
|
||||
require("avante.llm_tools.grep"),
|
||||
require("avante.llm_tools.glob"),
|
||||
require("avante.llm_tools.read_file"),
|
||||
}
|
||||
end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ prompt: string }>
|
||||
function M.func(opts, on_log, on_complete)
|
||||
local Llm = require("avante.llm")
|
||||
if not on_complete then return false, "on_complete not provided" end
|
||||
local prompt = opts.prompt
|
||||
local tools = get_available_tools()
|
||||
local start_time = os.date("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
if on_log then on_log("prompt: " .. prompt) end
|
||||
|
||||
local system_prompt = ([[You are a helpful assistant with access to various tools.
|
||||
Your task is to help the user with their request: "${prompt}"
|
||||
Be thorough and use the tools available to you to find the most relevant information.
|
||||
When you're done, provide a clear and concise summary of what you found.]]):gsub("${prompt}", prompt)
|
||||
|
||||
local total_tokens = 0
|
||||
local final_response = ""
|
||||
Llm._stream({
|
||||
ask = true,
|
||||
code_lang = "unknown",
|
||||
provider = Providers[Config.provider],
|
||||
prompt_opts = {
|
||||
system_prompt = system_prompt,
|
||||
tools = tools,
|
||||
messages = {
|
||||
{ role = "user", content = prompt },
|
||||
},
|
||||
},
|
||||
on_start = function(_) end,
|
||||
on_chunk = function(chunk)
|
||||
if not chunk then return end
|
||||
final_response = final_response .. chunk
|
||||
total_tokens = total_tokens + (#vim.split(chunk, " ") * 1.3)
|
||||
end,
|
||||
on_stop = function(stop_opts)
|
||||
if stop_opts.error ~= nil then
|
||||
local err = string.format("dispatch_agent failed: %s", vim.inspect(stop_opts.error))
|
||||
on_complete(err, nil)
|
||||
return
|
||||
end
|
||||
local end_time = os.date("%Y-%m-%d %H:%M:%S")
|
||||
local elapsed_time = Utils.datetime_diff(tostring(start_time), tostring(end_time))
|
||||
local tool_use_count = stop_opts.tool_histories and #stop_opts.tool_histories or 0
|
||||
local summary = "Done ("
|
||||
.. (tool_use_count <= 1 and "1 tool use" or tool_use_count .. " tool uses")
|
||||
.. " · "
|
||||
.. math.ceil(total_tokens)
|
||||
.. " tokens · "
|
||||
.. elapsed_time
|
||||
.. "s)"
|
||||
Utils.debug("summary", summary)
|
||||
local response = string.format("Final response:\n%s\n\nSummary:\n%s", summary, final_response)
|
||||
on_complete(response, nil)
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
53
lua/avante/llm_tools/glob.lua
Normal file
53
lua/avante/llm_tools/glob.lua
Normal file
@@ -0,0 +1,53 @@
|
||||
local Helpers = require("avante.llm_tools.helpers")
|
||||
local Base = require("avante.llm_tools.base")
|
||||
|
||||
---@class AvanteLLMTool
|
||||
local M = setmetatable({}, Base)
|
||||
|
||||
M.name = "glob"
|
||||
|
||||
M.description = 'Fast file pattern matching using glob patterns like "**/*.js", in current project scope'
|
||||
|
||||
---@type AvanteLLMToolParam
|
||||
M.param = {
|
||||
type = "table",
|
||||
fields = {
|
||||
{
|
||||
name = "pattern",
|
||||
description = "Glob pattern",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "rel_path",
|
||||
description = "Relative path to the project directory, as cwd",
|
||||
type = "string",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
---@type AvanteLLMToolReturn[]
|
||||
M.returns = {
|
||||
{
|
||||
name = "matches",
|
||||
description = "List of matched files",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "err",
|
||||
description = "Error message",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
}
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string, pattern: string }>
|
||||
function M.func(opts, on_log)
|
||||
local abs_path = Helpers.get_abs_path(opts.rel_path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return "", "No permission to access path: " .. abs_path end
|
||||
if on_log then on_log("path: " .. abs_path) end
|
||||
if on_log then on_log("pattern: " .. opts.pattern) end
|
||||
local files = vim.fn.glob(abs_path .. "/" .. opts.pattern, true, true)
|
||||
return vim.json.encode(files), nil
|
||||
end
|
||||
|
||||
return M
|
||||
117
lua/avante/llm_tools/grep.lua
Normal file
117
lua/avante/llm_tools/grep.lua
Normal file
@@ -0,0 +1,117 @@
|
||||
local Path = require("plenary.path")
|
||||
local Utils = require("avante.utils")
|
||||
local Helpers = require("avante.llm_tools.helpers")
|
||||
local Base = require("avante.llm_tools.base")
|
||||
|
||||
---@class AvanteLLMTool
|
||||
local M = setmetatable({}, Base)
|
||||
|
||||
M.name = "grep"
|
||||
|
||||
M.description = "Search for a keyword in a directory using grep in current project scope"
|
||||
|
||||
---@type AvanteLLMToolParam
|
||||
M.param = {
|
||||
type = "table",
|
||||
fields = {
|
||||
{
|
||||
name = "rel_path",
|
||||
description = "Relative path to the project directory",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "query",
|
||||
description = "Query to search for",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "case_sensitive",
|
||||
description = "Whether to search case sensitively",
|
||||
type = "boolean",
|
||||
default = false,
|
||||
optional = true,
|
||||
},
|
||||
{
|
||||
name = "include_pattern",
|
||||
description = "Glob pattern to include files",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
{
|
||||
name = "exclude_pattern",
|
||||
description = "Glob pattern to exclude files",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
---@type AvanteLLMToolReturn[]
|
||||
M.returns = {
|
||||
{
|
||||
name = "files",
|
||||
description = "List of files that match the keyword",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "error",
|
||||
description = "Error message if the directory was not searched successfully",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
}
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string, query: string, case_sensitive?: boolean, include_pattern?: string, exclude_pattern?: string }>
|
||||
function M.func(opts, on_log)
|
||||
local abs_path = Helpers.get_abs_path(opts.rel_path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return "", "No permission to access path: " .. abs_path end
|
||||
if not Path:new(abs_path):exists() then return "", "No such file or directory: " .. abs_path end
|
||||
|
||||
---check if any search cmd is available
|
||||
local search_cmd = vim.fn.exepath("rg")
|
||||
if search_cmd == "" then search_cmd = vim.fn.exepath("ag") end
|
||||
if search_cmd == "" then search_cmd = vim.fn.exepath("ack") end
|
||||
if search_cmd == "" then search_cmd = vim.fn.exepath("grep") end
|
||||
if search_cmd == "" then return "", "No search command found" end
|
||||
|
||||
---execute the search command
|
||||
local cmd = ""
|
||||
if search_cmd:find("rg") then
|
||||
cmd = string.format("%s --files-with-matches --hidden", search_cmd)
|
||||
if opts.case_sensitive then
|
||||
cmd = string.format("%s --case-sensitive", cmd)
|
||||
else
|
||||
cmd = string.format("%s --ignore-case", cmd)
|
||||
end
|
||||
if opts.include_pattern then cmd = string.format("%s --glob '%s'", cmd, opts.include_pattern) end
|
||||
if opts.exclude_pattern then cmd = string.format("%s --glob '!%s'", cmd, opts.exclude_pattern) end
|
||||
cmd = string.format("%s '%s' %s", cmd, opts.query, abs_path)
|
||||
elseif search_cmd:find("ag") then
|
||||
cmd = string.format("%s --nocolor --nogroup --hidden", search_cmd)
|
||||
if opts.case_sensitive then cmd = string.format("%s --case-sensitive", cmd) end
|
||||
if opts.include_pattern then cmd = string.format("%s --ignore '!%s'", cmd, opts.include_pattern) end
|
||||
if opts.exclude_pattern then cmd = string.format("%s --ignore '%s'", cmd, opts.exclude_pattern) end
|
||||
cmd = string.format("%s '%s' %s", cmd, opts.query, abs_path)
|
||||
elseif search_cmd:find("ack") then
|
||||
cmd = string.format("%s --nocolor --nogroup --hidden", search_cmd)
|
||||
if opts.case_sensitive then cmd = string.format("%s --smart-case", cmd) end
|
||||
if opts.exclude_pattern then cmd = string.format("%s --ignore-dir '%s'", cmd, opts.exclude_pattern) end
|
||||
cmd = string.format("%s '%s' %s", cmd, opts.query, abs_path)
|
||||
elseif search_cmd:find("grep") then
|
||||
cmd = string.format("cd %s && git ls-files -co --exclude-standard | xargs %s -rH", abs_path, search_cmd, abs_path)
|
||||
if not opts.case_sensitive then cmd = string.format("%s -i", cmd) end
|
||||
if opts.include_pattern then cmd = string.format("%s --include '%s'", cmd, opts.include_pattern) end
|
||||
if opts.exclude_pattern then cmd = string.format("%s --exclude '%s'", cmd, opts.exclude_pattern) end
|
||||
cmd = string.format("%s '%s'", cmd, opts.query)
|
||||
end
|
||||
|
||||
Utils.debug("cmd", cmd)
|
||||
if on_log then on_log("Running command: " .. cmd) end
|
||||
local result = vim.fn.system(cmd)
|
||||
|
||||
local filepaths = vim.split(result, "\n")
|
||||
|
||||
return vim.json.encode(filepaths), nil
|
||||
end
|
||||
|
||||
return M
|
||||
64
lua/avante/llm_tools/helpers.lua
Normal file
64
lua/avante/llm_tools/helpers.lua
Normal file
@@ -0,0 +1,64 @@
|
||||
local Utils = require("avante.utils")
|
||||
local Path = require("plenary.path")
|
||||
|
||||
local M = {}
|
||||
|
||||
M.CANCEL_TOKEN = "__CANCELLED__"
|
||||
|
||||
-- Track cancellation state
|
||||
M.is_cancelled = false
|
||||
---@type avante.ui.Confirm
|
||||
M.confirm_popup = nil
|
||||
|
||||
---@param rel_path string
|
||||
---@return string
|
||||
function M.get_abs_path(rel_path)
|
||||
if Path:new(rel_path):is_absolute() then return rel_path end
|
||||
local project_root = Utils.get_project_root()
|
||||
local p = tostring(Path:new(project_root):joinpath(rel_path):absolute())
|
||||
if p:sub(-2) == "/." then p = p:sub(1, -3) end
|
||||
return p
|
||||
end
|
||||
|
||||
---@param message string
|
||||
---@param callback fun(yes: boolean)
|
||||
---@param opts? { focus?: boolean }
|
||||
---@return avante.ui.Confirm | nil
|
||||
function M.confirm(message, callback, opts)
|
||||
local Confirm = require("avante.ui.confirm")
|
||||
local sidebar = require("avante").get()
|
||||
if not sidebar or not sidebar.input_container or not sidebar.input_container.winid then
|
||||
Utils.error("Avante sidebar not found", { title = "Avante" })
|
||||
callback(false)
|
||||
return
|
||||
end
|
||||
local confirm_opts = vim.tbl_deep_extend("force", { container_winid = sidebar.input_container.winid }, opts or {})
|
||||
M.confirm_popup = Confirm:new(message, callback, confirm_opts)
|
||||
M.confirm_popup:open()
|
||||
return M.confirm_popup
|
||||
end
|
||||
|
||||
---@param abs_path string
|
||||
---@return boolean
|
||||
function M.is_ignored(abs_path)
|
||||
local project_root = Utils.get_project_root()
|
||||
local gitignore_path = project_root .. "/.gitignore"
|
||||
local gitignore_patterns, gitignore_negate_patterns = Utils.parse_gitignore(gitignore_path)
|
||||
-- The checker should only take care of the path inside the project root
|
||||
-- Specifically, it should not check the project root itself
|
||||
-- Otherwise if the binary is named the same as the project root (such as Go binary), any paths
|
||||
-- insde the project root will be ignored
|
||||
local rel_path = Utils.make_relative_path(abs_path, project_root)
|
||||
return Utils.is_ignored(rel_path, gitignore_patterns, gitignore_negate_patterns)
|
||||
end
|
||||
|
||||
---@param abs_path string
|
||||
---@return boolean
|
||||
function M.has_permission_to_access(abs_path)
|
||||
if not Path:new(abs_path):is_absolute() then return false end
|
||||
local project_root = Utils.get_project_root()
|
||||
if abs_path:sub(1, #project_root) ~= project_root then return false end
|
||||
return not M.is_ignored(abs_path)
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -5,171 +5,15 @@ local Config = require("avante.config")
|
||||
local RagService = require("avante.rag_service")
|
||||
local Diff = require("avante.diff")
|
||||
local Highlights = require("avante.highlights")
|
||||
local Helpers = require("avante.llm_tools.helpers")
|
||||
|
||||
---@class AvanteRagService
|
||||
local M = {}
|
||||
|
||||
M.CANCEL_TOKEN = "__CANCELLED__"
|
||||
|
||||
-- Track cancellation state
|
||||
M.is_cancelled = false
|
||||
---@type avante.ui.Confirm
|
||||
M.confirm_popup = nil
|
||||
|
||||
---@param rel_path string
|
||||
---@return string
|
||||
local function get_abs_path(rel_path)
|
||||
if Path:new(rel_path):is_absolute() then return rel_path end
|
||||
local project_root = Utils.get_project_root()
|
||||
local p = tostring(Path:new(project_root):joinpath(rel_path):absolute())
|
||||
if p:sub(-2) == "/." then p = p:sub(1, -3) end
|
||||
return p
|
||||
end
|
||||
|
||||
---@param message string
|
||||
---@param callback fun(yes: boolean)
|
||||
---@param opts? { focus?: boolean }
|
||||
---@return avante.ui.Confirm | nil
|
||||
function M.confirm(message, callback, opts)
|
||||
local Confirm = require("avante.ui.confirm")
|
||||
local sidebar = require("avante").get()
|
||||
if not sidebar or not sidebar.input_container or not sidebar.input_container.winid then
|
||||
Utils.error("Avante sidebar not found", { title = "Avante" })
|
||||
callback(false)
|
||||
return
|
||||
end
|
||||
local confirm_opts = vim.tbl_deep_extend("force", { container_winid = sidebar.input_container.winid }, opts or {})
|
||||
M.confirm_popup = Confirm:new(message, callback, confirm_opts)
|
||||
M.confirm_popup:open()
|
||||
return M.confirm_popup
|
||||
end
|
||||
|
||||
---@param abs_path string
|
||||
---@return boolean
|
||||
local function is_ignored(abs_path)
|
||||
local project_root = Utils.get_project_root()
|
||||
local gitignore_path = project_root .. "/.gitignore"
|
||||
local gitignore_patterns, gitignore_negate_patterns = Utils.parse_gitignore(gitignore_path)
|
||||
-- The checker should only take care of the path inside the project root
|
||||
-- Specifically, it should not check the project root itself
|
||||
-- Otherwise if the binary is named the same as the project root (such as Go binary), any paths
|
||||
-- insde the project root will be ignored
|
||||
local rel_path = Utils.make_relative_path(abs_path, project_root)
|
||||
return Utils.is_ignored(rel_path, gitignore_patterns, gitignore_negate_patterns)
|
||||
end
|
||||
|
||||
---@param abs_path string
|
||||
---@return boolean
|
||||
local function has_permission_to_access(abs_path)
|
||||
if not Path:new(abs_path):is_absolute() then return false end
|
||||
local project_root = Utils.get_project_root()
|
||||
if abs_path:sub(1, #project_root) ~= project_root then return false end
|
||||
return not is_ignored(abs_path)
|
||||
end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string, pattern: string }>
|
||||
function M.glob(opts, on_log)
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return "", "No permission to access path: " .. abs_path end
|
||||
if on_log then on_log("path: " .. abs_path) end
|
||||
if on_log then on_log("pattern: " .. opts.pattern) end
|
||||
local files = vim.fn.glob(abs_path .. "/" .. opts.pattern, true, true)
|
||||
return vim.json.encode(files), nil
|
||||
end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string, max_depth?: integer }>
|
||||
function M.list_files(opts, on_log)
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return "", "No permission to access path: " .. abs_path end
|
||||
if on_log then on_log("path: " .. abs_path) end
|
||||
if on_log then on_log("max depth: " .. tostring(opts.max_depth)) end
|
||||
local files = Utils.scan_directory({
|
||||
directory = abs_path,
|
||||
add_dirs = true,
|
||||
max_depth = opts.max_depth,
|
||||
})
|
||||
local filepaths = {}
|
||||
for _, file in ipairs(files) do
|
||||
local uniform_path = Utils.uniform_path(file)
|
||||
table.insert(filepaths, uniform_path)
|
||||
end
|
||||
return vim.json.encode(filepaths), nil
|
||||
end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string, keyword: string }>
|
||||
function M.search_files(opts, on_log)
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return "", "No permission to access path: " .. abs_path end
|
||||
if on_log then on_log("path: " .. abs_path) end
|
||||
if on_log then on_log("keyword: " .. opts.keyword) end
|
||||
local files = Utils.scan_directory({
|
||||
directory = abs_path,
|
||||
})
|
||||
local filepaths = {}
|
||||
for _, file in ipairs(files) do
|
||||
if file:find(opts.keyword) then table.insert(filepaths, file) end
|
||||
end
|
||||
return vim.json.encode(filepaths), nil
|
||||
end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string, query: string, case_sensitive?: boolean, include_pattern?: string, exclude_pattern?: string }>
|
||||
function M.grep_search(opts, on_log)
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return "", "No permission to access path: " .. abs_path end
|
||||
if not Path:new(abs_path):exists() then return "", "No such file or directory: " .. abs_path end
|
||||
|
||||
---check if any search cmd is available
|
||||
local search_cmd = vim.fn.exepath("rg")
|
||||
if search_cmd == "" then search_cmd = vim.fn.exepath("ag") end
|
||||
if search_cmd == "" then search_cmd = vim.fn.exepath("ack") end
|
||||
if search_cmd == "" then search_cmd = vim.fn.exepath("grep") end
|
||||
if search_cmd == "" then return "", "No search command found" end
|
||||
|
||||
---execute the search command
|
||||
local cmd = ""
|
||||
if search_cmd:find("rg") then
|
||||
cmd = string.format("%s --files-with-matches --hidden", search_cmd)
|
||||
if opts.case_sensitive then
|
||||
cmd = string.format("%s --case-sensitive", cmd)
|
||||
else
|
||||
cmd = string.format("%s --ignore-case", cmd)
|
||||
end
|
||||
if opts.include_pattern then cmd = string.format("%s --glob '%s'", cmd, opts.include_pattern) end
|
||||
if opts.exclude_pattern then cmd = string.format("%s --glob '!%s'", cmd, opts.exclude_pattern) end
|
||||
cmd = string.format("%s '%s' %s", cmd, opts.query, abs_path)
|
||||
elseif search_cmd:find("ag") then
|
||||
cmd = string.format("%s --nocolor --nogroup --hidden", search_cmd)
|
||||
if opts.case_sensitive then cmd = string.format("%s --case-sensitive", cmd) end
|
||||
if opts.include_pattern then cmd = string.format("%s --ignore '!%s'", cmd, opts.include_pattern) end
|
||||
if opts.exclude_pattern then cmd = string.format("%s --ignore '%s'", cmd, opts.exclude_pattern) end
|
||||
cmd = string.format("%s '%s' %s", cmd, opts.query, abs_path)
|
||||
elseif search_cmd:find("ack") then
|
||||
cmd = string.format("%s --nocolor --nogroup --hidden", search_cmd)
|
||||
if opts.case_sensitive then cmd = string.format("%s --smart-case", cmd) end
|
||||
if opts.exclude_pattern then cmd = string.format("%s --ignore-dir '%s'", cmd, opts.exclude_pattern) end
|
||||
cmd = string.format("%s '%s' %s", cmd, opts.query, abs_path)
|
||||
elseif search_cmd:find("grep") then
|
||||
cmd = string.format("cd %s && git ls-files -co --exclude-standard | xargs %s -rH", abs_path, search_cmd, abs_path)
|
||||
if not opts.case_sensitive then cmd = string.format("%s -i", cmd) end
|
||||
if opts.include_pattern then cmd = string.format("%s --include '%s'", cmd, opts.include_pattern) end
|
||||
if opts.exclude_pattern then cmd = string.format("%s --exclude '%s'", cmd, opts.exclude_pattern) end
|
||||
cmd = string.format("%s '%s'", cmd, opts.query)
|
||||
end
|
||||
|
||||
Utils.debug("cmd", cmd)
|
||||
if on_log then on_log("Running command: " .. cmd) end
|
||||
local result = vim.fn.system(cmd)
|
||||
|
||||
local filepaths = vim.split(result, "\n")
|
||||
|
||||
return vim.json.encode(filepaths), nil
|
||||
end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string }>
|
||||
function M.read_file_toplevel_symbols(opts, on_log)
|
||||
local RepoMap = require("avante.repo_map")
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return "", "No permission to access path: " .. abs_path end
|
||||
local abs_path = Helpers.get_abs_path(opts.rel_path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return "", "No permission to access path: " .. abs_path end
|
||||
if on_log then on_log("path: " .. abs_path) end
|
||||
if not Path:new(abs_path):exists() then return "", "File does not exists: " .. abs_path end
|
||||
local filetype = RepoMap.get_ts_lang(abs_path)
|
||||
@@ -181,25 +25,13 @@ function M.read_file_toplevel_symbols(opts, on_log)
|
||||
return definitions, nil
|
||||
end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string }>
|
||||
function M.read_file(opts, on_log)
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return "", "No permission to access path: " .. abs_path end
|
||||
if on_log then on_log("path: " .. abs_path) end
|
||||
local file = io.open(abs_path, "r")
|
||||
if not file then return "", "file not found: " .. abs_path end
|
||||
local lines = Utils.read_file_from_buf_or_disk(abs_path)
|
||||
local content = lines and table.concat(lines, "\n") or ""
|
||||
return content, nil
|
||||
end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ command: "view" | "str_replace" | "create" | "insert" | "undo_edit", path: string, old_str?: string, new_str?: string, file_text?: string, insert_line?: integer, new_str?: string }>
|
||||
function M.str_replace_editor(opts, on_log, on_complete)
|
||||
if on_log then on_log("command: " .. opts.command) end
|
||||
if on_log then on_log("path: " .. vim.inspect(opts.path)) end
|
||||
if not on_complete then return false, "on_complete not provided" end
|
||||
local abs_path = get_abs_path(opts.path)
|
||||
if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
local abs_path = Helpers.get_abs_path(opts.path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
local sidebar = require("avante").get()
|
||||
if not sidebar then return false, "Avante sidebar not found" end
|
||||
local get_bufnr = function()
|
||||
@@ -288,7 +120,7 @@ function M.str_replace_editor(opts, on_log, on_complete)
|
||||
vim.cmd("normal! zz")
|
||||
vim.api.nvim_set_current_win(current_winid)
|
||||
local augroup = vim.api.nvim_create_augroup("avante_str_replace_editor", { clear = true })
|
||||
local confirm = M.confirm("Are you sure you want to apply this modification?", function(ok)
|
||||
local confirm = Helpers.confirm("Are you sure you want to apply this modification?", function(ok)
|
||||
pcall(vim.api.nvim_del_augroup_by_id, augroup)
|
||||
vim.api.nvim_set_current_win(sidebar.code.winid)
|
||||
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", true)
|
||||
@@ -328,7 +160,7 @@ function M.str_replace_editor(opts, on_log, on_complete)
|
||||
local lines = vim.split(opts.file_text, "\n")
|
||||
local bufnr = get_bufnr()
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
||||
M.confirm("Are you sure you want to create this file?", function(ok)
|
||||
Helpers.confirm("Are you sure you want to create this file?", function(ok)
|
||||
if not ok then
|
||||
-- close the buffer
|
||||
vim.api.nvim_buf_delete(bufnr, { force = true })
|
||||
@@ -368,7 +200,7 @@ function M.str_replace_editor(opts, on_log, on_complete)
|
||||
hl_eol = true,
|
||||
hl_mode = "combine",
|
||||
})
|
||||
M.confirm("Are you sure you want to insert these lines?", function(ok)
|
||||
Helpers.confirm("Are you sure you want to insert these lines?", function(ok)
|
||||
clear_highlights()
|
||||
if not ok then
|
||||
on_complete(false, "User canceled")
|
||||
@@ -383,7 +215,7 @@ function M.str_replace_editor(opts, on_log, on_complete)
|
||||
if not Path:new(abs_path):exists() then return false, "File not found: " .. abs_path end
|
||||
if not Path:new(abs_path):is_file() then return false, "Path is not a file: " .. abs_path end
|
||||
local bufnr = get_bufnr()
|
||||
M.confirm("Are you sure you want to undo edit this file?", function(ok)
|
||||
Helpers.confirm("Are you sure you want to undo edit this file?", function(ok)
|
||||
if not ok then
|
||||
on_complete(false, "User canceled")
|
||||
return
|
||||
@@ -403,8 +235,8 @@ end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ abs_path: string }>
|
||||
function M.read_global_file(opts, on_log)
|
||||
local abs_path = get_abs_path(opts.abs_path)
|
||||
if is_ignored(abs_path) then return "", "This file is ignored: " .. abs_path end
|
||||
local abs_path = Helpers.get_abs_path(opts.abs_path)
|
||||
if Helpers.is_ignored(abs_path) then return "", "This file is ignored: " .. abs_path end
|
||||
if on_log then on_log("path: " .. abs_path) end
|
||||
local file = io.open(abs_path, "r")
|
||||
if not file then return "", "file not found: " .. abs_path end
|
||||
@@ -415,12 +247,12 @@ end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ abs_path: string, content: string }>
|
||||
function M.write_global_file(opts, on_log, on_complete)
|
||||
local abs_path = get_abs_path(opts.abs_path)
|
||||
if is_ignored(abs_path) then return false, "This file is ignored: " .. abs_path end
|
||||
local abs_path = Helpers.get_abs_path(opts.abs_path)
|
||||
if Helpers.is_ignored(abs_path) then return false, "This file is ignored: " .. abs_path end
|
||||
if on_log then on_log("path: " .. abs_path) end
|
||||
if on_log then on_log("content: " .. opts.content) end
|
||||
if not on_complete then return false, "on_complete not provided" end
|
||||
M.confirm("Are you sure you want to write to the file: " .. abs_path, function(ok)
|
||||
Helpers.confirm("Are you sure you want to write to the file: " .. abs_path, function(ok)
|
||||
if not ok then
|
||||
on_complete(false, "User canceled")
|
||||
return
|
||||
@@ -438,8 +270,8 @@ end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string }>
|
||||
function M.create_file(opts, on_log)
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
local abs_path = Helpers.get_abs_path(opts.rel_path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
if on_log then on_log("path: " .. abs_path) end
|
||||
---create directory if it doesn't exist
|
||||
local dir = Path:new(abs_path):parent()
|
||||
@@ -456,16 +288,18 @@ end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string, new_rel_path: string }>
|
||||
function M.rename_file(opts, on_log, on_complete)
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
local abs_path = Helpers.get_abs_path(opts.rel_path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
if not Path:new(abs_path):exists() then return false, "File not found: " .. abs_path end
|
||||
if not Path:new(abs_path):is_file() then return false, "Path is not a file: " .. abs_path end
|
||||
local new_abs_path = get_abs_path(opts.new_rel_path)
|
||||
local new_abs_path = Helpers.get_abs_path(opts.new_rel_path)
|
||||
if on_log then on_log(abs_path .. " -> " .. new_abs_path) end
|
||||
if not has_permission_to_access(new_abs_path) then return false, "No permission to access path: " .. new_abs_path end
|
||||
if not Helpers.has_permission_to_access(new_abs_path) then
|
||||
return false, "No permission to access path: " .. new_abs_path
|
||||
end
|
||||
if Path:new(new_abs_path):exists() then return false, "File already exists: " .. new_abs_path end
|
||||
if not on_complete then return false, "on_complete not provided" end
|
||||
M.confirm("Are you sure you want to rename the file: " .. abs_path .. " to: " .. new_abs_path, function(ok)
|
||||
Helpers.confirm("Are you sure you want to rename the file: " .. abs_path .. " to: " .. new_abs_path, function(ok)
|
||||
if not ok then
|
||||
on_complete(false, "User canceled")
|
||||
return
|
||||
@@ -477,12 +311,14 @@ end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string, new_rel_path: string }>
|
||||
function M.copy_file(opts, on_log)
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
local abs_path = Helpers.get_abs_path(opts.rel_path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
if not Path:new(abs_path):exists() then return false, "File not found: " .. abs_path end
|
||||
if not Path:new(abs_path):is_file() then return false, "Path is not a file: " .. abs_path end
|
||||
local new_abs_path = get_abs_path(opts.new_rel_path)
|
||||
if not has_permission_to_access(new_abs_path) then return false, "No permission to access path: " .. new_abs_path end
|
||||
local new_abs_path = Helpers.get_abs_path(opts.new_rel_path)
|
||||
if not Helpers.has_permission_to_access(new_abs_path) then
|
||||
return false, "No permission to access path: " .. new_abs_path
|
||||
end
|
||||
if Path:new(new_abs_path):exists() then return false, "File already exists: " .. new_abs_path end
|
||||
if on_log then on_log("Copying file: " .. abs_path .. " to " .. new_abs_path) end
|
||||
Path:new(new_abs_path):write(Path:new(abs_path):read())
|
||||
@@ -491,12 +327,12 @@ end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string }>
|
||||
function M.delete_file(opts, on_log, on_complete)
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
local abs_path = Helpers.get_abs_path(opts.rel_path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
if not Path:new(abs_path):exists() then return false, "File not found: " .. abs_path end
|
||||
if not Path:new(abs_path):is_file() then return false, "Path is not a file: " .. abs_path end
|
||||
if not on_complete then return false, "on_complete not provided" end
|
||||
M.confirm("Are you sure you want to delete the file: " .. abs_path, function(ok)
|
||||
Helpers.confirm("Are you sure you want to delete the file: " .. abs_path, function(ok)
|
||||
if not ok then
|
||||
on_complete(false, "User canceled")
|
||||
return
|
||||
@@ -509,11 +345,11 @@ end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string }>
|
||||
function M.create_dir(opts, on_log, on_complete)
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
local abs_path = Helpers.get_abs_path(opts.rel_path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
if Path:new(abs_path):exists() then return false, "Directory already exists: " .. abs_path end
|
||||
if not on_complete then return false, "on_complete not provided" end
|
||||
M.confirm("Are you sure you want to create the directory: " .. abs_path, function(ok)
|
||||
Helpers.confirm("Are you sure you want to create the directory: " .. abs_path, function(ok)
|
||||
if not ok then
|
||||
on_complete(false, "User canceled")
|
||||
return
|
||||
@@ -526,33 +362,38 @@ end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string, new_rel_path: string }>
|
||||
function M.rename_dir(opts, on_log, on_complete)
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
local abs_path = Helpers.get_abs_path(opts.rel_path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
if not Path:new(abs_path):exists() then return false, "Directory not found: " .. abs_path end
|
||||
if not Path:new(abs_path):is_dir() then return false, "Path is not a directory: " .. abs_path end
|
||||
local new_abs_path = get_abs_path(opts.new_rel_path)
|
||||
if not has_permission_to_access(new_abs_path) then return false, "No permission to access path: " .. new_abs_path end
|
||||
local new_abs_path = Helpers.get_abs_path(opts.new_rel_path)
|
||||
if not Helpers.has_permission_to_access(new_abs_path) then
|
||||
return false, "No permission to access path: " .. new_abs_path
|
||||
end
|
||||
if Path:new(new_abs_path):exists() then return false, "Directory already exists: " .. new_abs_path end
|
||||
if not on_complete then return false, "on_complete not provided" end
|
||||
M.confirm("Are you sure you want to rename directory " .. abs_path .. " to " .. new_abs_path .. "?", function(ok)
|
||||
if not ok then
|
||||
on_complete(false, "User canceled")
|
||||
return
|
||||
Helpers.confirm(
|
||||
"Are you sure you want to rename directory " .. abs_path .. " to " .. new_abs_path .. "?",
|
||||
function(ok)
|
||||
if not ok then
|
||||
on_complete(false, "User canceled")
|
||||
return
|
||||
end
|
||||
if on_log then on_log("Renaming directory: " .. abs_path .. " to " .. new_abs_path) end
|
||||
os.rename(abs_path, new_abs_path)
|
||||
on_complete(true, nil)
|
||||
end
|
||||
if on_log then on_log("Renaming directory: " .. abs_path .. " to " .. new_abs_path) end
|
||||
os.rename(abs_path, new_abs_path)
|
||||
on_complete(true, nil)
|
||||
end)
|
||||
)
|
||||
end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string }>
|
||||
function M.delete_dir(opts, on_log, on_complete)
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
local abs_path = Helpers.get_abs_path(opts.rel_path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
if not Path:new(abs_path):exists() then return false, "Directory not found: " .. abs_path end
|
||||
if not Path:new(abs_path):is_dir() then return false, "Path is not a directory: " .. abs_path end
|
||||
if not on_complete then return false, "on_complete not provided" end
|
||||
M.confirm("Are you sure you want to delete the directory: " .. abs_path, function(ok)
|
||||
Helpers.confirm("Are you sure you want to delete the directory: " .. abs_path, function(ok)
|
||||
if not ok then
|
||||
on_complete(false, "User canceled")
|
||||
return
|
||||
@@ -563,40 +404,6 @@ function M.delete_dir(opts, on_log, on_complete)
|
||||
end)
|
||||
end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string, command: string }>
|
||||
function M.bash(opts, on_log, on_complete)
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
if not Path:new(abs_path):exists() then return false, "Path not found: " .. abs_path end
|
||||
if on_log then on_log("command: " .. opts.command) end
|
||||
---change cwd to abs_path
|
||||
---@param output string
|
||||
---@param exit_code integer
|
||||
---@return string | boolean | nil result
|
||||
---@return string | nil error
|
||||
local function handle_result(output, exit_code)
|
||||
if exit_code ~= 0 then
|
||||
if output then return false, "Error: " .. output .. "; Error code: " .. tostring(exit_code) end
|
||||
return false, "Error code: " .. tostring(exit_code)
|
||||
end
|
||||
return output, nil
|
||||
end
|
||||
if not on_complete then return false, "on_complete not provided" end
|
||||
M.confirm(
|
||||
"Are you sure you want to run the command: `" .. opts.command .. "` in the directory: " .. abs_path,
|
||||
function(ok)
|
||||
if not ok then
|
||||
on_complete(false, "User canceled")
|
||||
return
|
||||
end
|
||||
Utils.shell_run_async(opts.command, "bash -c", function(output, exit_code)
|
||||
local result, err = handle_result(output, exit_code)
|
||||
on_complete(result, err)
|
||||
end, abs_path)
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ query: string }>
|
||||
function M.web_search(opts, on_log)
|
||||
local provider_type = Config.web_search_engine.provider
|
||||
@@ -802,6 +609,10 @@ function M.git_commit(opts, on_log, on_complete)
|
||||
for line in opts.message:gmatch("[^\r\n]+") do
|
||||
commit_msg_lines[#commit_msg_lines + 1] = line:gsub('"', '\\"')
|
||||
end
|
||||
|
||||
commit_msg_lines[#commit_msg_lines + 1] = ""
|
||||
commit_msg_lines[#commit_msg_lines + 1] = "🤖 Generated with [avante.nvim](https://github.com/yetone/avante.nvim)"
|
||||
commit_msg_lines[#commit_msg_lines + 1] = "Co-Authored-By: avante.nvim <noreply-avante@yetone.ai>"
|
||||
if git_user ~= "" and git_email ~= "" then
|
||||
commit_msg_lines[#commit_msg_lines + 1] = string.format("Signed-off-by: %s <%s>", git_user, git_email)
|
||||
end
|
||||
@@ -812,7 +623,7 @@ function M.git_commit(opts, on_log, on_complete)
|
||||
if not on_complete then return false, "on_complete not provided" end
|
||||
|
||||
-- Confirm with user
|
||||
M.confirm("Are you sure you want to commit with message:\n" .. full_commit_msg, function(ok)
|
||||
Helpers.confirm("Are you sure you want to commit with message:\n" .. full_commit_msg, function(ok)
|
||||
if not ok then
|
||||
on_complete(false, "User canceled")
|
||||
return
|
||||
@@ -866,14 +677,14 @@ end
|
||||
|
||||
---@type AvanteLLMToolFunc<{ code: string, rel_path: string, container_image?: string }>
|
||||
function M.python(opts, on_log, on_complete)
|
||||
local abs_path = get_abs_path(opts.rel_path)
|
||||
if not has_permission_to_access(abs_path) then return nil, "No permission to access path: " .. abs_path end
|
||||
local abs_path = Helpers.get_abs_path(opts.rel_path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return nil, "No permission to access path: " .. abs_path end
|
||||
if not Path:new(abs_path):exists() then return nil, "Path not found: " .. abs_path end
|
||||
if on_log then on_log("cwd: " .. abs_path) end
|
||||
if on_log then on_log("code:\n" .. opts.code) end
|
||||
local container_image = opts.container_image or "python:3.11-slim-bookworm"
|
||||
if not on_complete then return nil, "on_complete not provided" end
|
||||
M.confirm(
|
||||
Helpers.confirm(
|
||||
"Are you sure you want to run the following python code in the `"
|
||||
.. container_image
|
||||
.. "` container, in the directory: `"
|
||||
@@ -948,38 +759,8 @@ end
|
||||
|
||||
---@type AvanteLLMTool[]
|
||||
M._tools = {
|
||||
{
|
||||
name = "glob",
|
||||
description = 'Fast file pattern matching using glob patterns like "**/*.js", in current project scope',
|
||||
param = {
|
||||
type = "table",
|
||||
fields = {
|
||||
{
|
||||
name = "pattern",
|
||||
description = "Glob pattern",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "rel_path",
|
||||
description = "Relative path to the project directory, as cwd",
|
||||
type = "string",
|
||||
},
|
||||
},
|
||||
},
|
||||
returns = {
|
||||
{
|
||||
name = "matches",
|
||||
description = "List of matched files",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "err",
|
||||
description = "Error message",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
},
|
||||
},
|
||||
require("avante.llm_tools.dispatch_agent"),
|
||||
require("avante.llm_tools.glob"),
|
||||
{
|
||||
name = "rag_search",
|
||||
enabled = function() return Config.rag_service.enabled and RagService.is_ready() end,
|
||||
@@ -1100,121 +881,8 @@ M._tools = {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name = "list_files",
|
||||
description = "List files in current project scope",
|
||||
param = {
|
||||
type = "table",
|
||||
fields = {
|
||||
{
|
||||
name = "rel_path",
|
||||
description = "Relative path to the project directory",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "max_depth",
|
||||
description = "Maximum depth of the directory",
|
||||
type = "integer",
|
||||
},
|
||||
},
|
||||
},
|
||||
returns = {
|
||||
{
|
||||
name = "files",
|
||||
description = "List of filepaths in the directory",
|
||||
type = "string[]",
|
||||
},
|
||||
{
|
||||
name = "error",
|
||||
description = "Error message if the directory was not listed successfully",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name = "search_files",
|
||||
description = "Search for files in current project scope",
|
||||
param = {
|
||||
type = "table",
|
||||
fields = {
|
||||
{
|
||||
name = "rel_path",
|
||||
description = "Relative path to the project directory",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "keyword",
|
||||
description = "Keyword to search for",
|
||||
type = "string",
|
||||
},
|
||||
},
|
||||
},
|
||||
returns = {
|
||||
{
|
||||
name = "files",
|
||||
description = "List of filepaths that match the keyword",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "error",
|
||||
description = "Error message if the directory was not searched successfully",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name = "grep_search",
|
||||
description = "Search for a keyword in a directory using grep in current project scope",
|
||||
param = {
|
||||
type = "table",
|
||||
fields = {
|
||||
{
|
||||
name = "rel_path",
|
||||
description = "Relative path to the project directory",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "query",
|
||||
description = "Query to search for",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "case_sensitive",
|
||||
description = "Whether to search case sensitively",
|
||||
type = "boolean",
|
||||
default = false,
|
||||
optional = true,
|
||||
},
|
||||
{
|
||||
name = "include_pattern",
|
||||
description = "Glob pattern to include files",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
{
|
||||
name = "exclude_pattern",
|
||||
description = "Glob pattern to exclude files",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
},
|
||||
},
|
||||
returns = {
|
||||
{
|
||||
name = "files",
|
||||
description = "List of files that match the keyword",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "error",
|
||||
description = "Error message if the directory was not searched successfully",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
},
|
||||
},
|
||||
require("avante.llm_tools.ls"),
|
||||
require("avante.llm_tools.grep"),
|
||||
{
|
||||
name = "read_file_toplevel_symbols",
|
||||
description = "Read the top-level symbols of a file in current project scope",
|
||||
@@ -1242,48 +910,7 @@ M._tools = {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name = "read_file",
|
||||
description = "Read the contents of a file in current project scope. If the file content is already in the context, do not use this tool.",
|
||||
enabled = function(opts)
|
||||
if opts.user_input:match("@read_global_file") then return false end
|
||||
for _, message in ipairs(opts.history_messages) do
|
||||
if message.role == "user" then
|
||||
local content = message.content
|
||||
if type(content) == "string" and content:match("@read_global_file") then return false end
|
||||
if type(content) == "table" then
|
||||
for _, item in ipairs(content) do
|
||||
if type(item) == "string" and item:match("@read_global_file") then return false end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return true
|
||||
end,
|
||||
param = {
|
||||
type = "table",
|
||||
fields = {
|
||||
{
|
||||
name = "rel_path",
|
||||
description = "Relative path to the file in current project scope",
|
||||
type = "string",
|
||||
},
|
||||
},
|
||||
},
|
||||
returns = {
|
||||
{
|
||||
name = "content",
|
||||
description = "Contents of the file",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "error",
|
||||
description = "Error message if the file was not read successfully",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
},
|
||||
},
|
||||
require("avante.llm_tools.read_file"),
|
||||
{
|
||||
name = "read_global_file",
|
||||
description = "Read the contents of a file in the global scope. If the file content is already in the context, do not use this tool.",
|
||||
@@ -1545,38 +1172,7 @@ M._tools = {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name = "bash",
|
||||
description = "Run a bash command in current project scope. Can't use search commands like find/grep or read tools like cat/ls. Can't use it to read files or modify files.",
|
||||
param = {
|
||||
type = "table",
|
||||
fields = {
|
||||
{
|
||||
name = "rel_path",
|
||||
description = "Relative path to the project directory, as cwd",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "command",
|
||||
description = "Command to run",
|
||||
type = "string",
|
||||
},
|
||||
},
|
||||
},
|
||||
returns = {
|
||||
{
|
||||
name = "stdout",
|
||||
description = "Output of the command",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "error",
|
||||
description = "Error message if the command was not run successfully",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
},
|
||||
},
|
||||
require("avante.llm_tools.bash"),
|
||||
{
|
||||
name = "web_search",
|
||||
description = "Search the web",
|
||||
@@ -1694,13 +1290,13 @@ function M.process_tool_use(tools, tool_use, on_log, on_complete)
|
||||
Utils.debug("use tool", tool_use.name, tool_use.input_json)
|
||||
|
||||
-- Check if execution is already cancelled
|
||||
if M.is_cancelled then
|
||||
if Helpers.is_cancelled then
|
||||
Utils.debug("Tool execution cancelled before starting: " .. tool_use.name)
|
||||
if on_complete then
|
||||
on_complete(nil, M.CANCEL_TOKEN)
|
||||
on_complete(nil, Helpers.CANCEL_TOKEN)
|
||||
return
|
||||
end
|
||||
return nil, M.CANCEL_TOKEN
|
||||
return nil, Helpers.CANCEL_TOKEN
|
||||
end
|
||||
|
||||
local func
|
||||
@@ -1725,13 +1321,13 @@ function M.process_tool_use(tools, tool_use, on_log, on_complete)
|
||||
100,
|
||||
100,
|
||||
vim.schedule_wrap(function()
|
||||
if M.is_cancelled then
|
||||
if Helpers.is_cancelled then
|
||||
Utils.debug("Tool execution cancelled during execution: " .. tool_use.name)
|
||||
if cancel_timer and not cancel_timer:is_closing() then
|
||||
cancel_timer:stop()
|
||||
cancel_timer:close()
|
||||
end
|
||||
on_complete(nil, M.CANCEL_TOKEN)
|
||||
on_complete(nil, Helpers.CANCEL_TOKEN)
|
||||
end
|
||||
end)
|
||||
)
|
||||
@@ -1748,9 +1344,9 @@ function M.process_tool_use(tools, tool_use, on_log, on_complete)
|
||||
end
|
||||
|
||||
-- Check for cancellation one more time before processing result
|
||||
if M.is_cancelled then
|
||||
if Helpers.is_cancelled then
|
||||
if on_log then on_log(tool_use.name, "cancelled during result handling") end
|
||||
return nil, M.CANCEL_TOKEN
|
||||
return nil, Helpers.CANCEL_TOKEN
|
||||
end
|
||||
|
||||
if on_log then on_log(tool_use.name, "tool finished") end
|
||||
@@ -1770,12 +1366,12 @@ function M.process_tool_use(tools, tool_use, on_log, on_complete)
|
||||
|
||||
local result, err = func(input_json, function(log)
|
||||
-- Check for cancellation during logging
|
||||
if M.is_cancelled then return end
|
||||
if Helpers.is_cancelled then return end
|
||||
if on_log then on_log(tool_use.name, log) end
|
||||
end, function(result, err)
|
||||
-- Check for cancellation before completing
|
||||
if M.is_cancelled then
|
||||
if on_complete then on_complete(nil, M.CANCEL_TOKEN) end
|
||||
if Helpers.is_cancelled then
|
||||
if on_complete then on_complete(nil, Helpers.CANCEL_TOKEN) end
|
||||
return
|
||||
end
|
||||
|
||||
63
lua/avante/llm_tools/ls.lua
Normal file
63
lua/avante/llm_tools/ls.lua
Normal file
@@ -0,0 +1,63 @@
|
||||
local Utils = require("avante.utils")
|
||||
local Helpers = require("avante.llm_tools.helpers")
|
||||
local Base = require("avante.llm_tools.base")
|
||||
|
||||
---@class AvanteLLMTool
|
||||
local M = setmetatable({}, Base)
|
||||
|
||||
M.name = "ls"
|
||||
|
||||
M.description = "List files and directories in a given path in current project scope"
|
||||
|
||||
---@type AvanteLLMToolParam
|
||||
M.param = {
|
||||
type = "table",
|
||||
fields = {
|
||||
{
|
||||
name = "rel_path",
|
||||
description = "Relative path to the project directory",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "max_depth",
|
||||
description = "Maximum depth of the directory",
|
||||
type = "integer",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
---@type AvanteLLMToolReturn[]
|
||||
M.returns = {
|
||||
{
|
||||
name = "entries",
|
||||
description = "List of file paths and directorie paths in the given directory",
|
||||
type = "string[]",
|
||||
},
|
||||
{
|
||||
name = "error",
|
||||
description = "Error message if the directory was not listed successfully",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
}
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string, max_depth?: integer }>
|
||||
function M.func(opts, on_log)
|
||||
local abs_path = Helpers.get_abs_path(opts.rel_path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return "", "No permission to access path: " .. abs_path end
|
||||
if on_log then on_log("path: " .. abs_path) end
|
||||
if on_log then on_log("max depth: " .. tostring(opts.max_depth)) end
|
||||
local files = Utils.scan_directory({
|
||||
directory = abs_path,
|
||||
add_dirs = true,
|
||||
max_depth = opts.max_depth,
|
||||
})
|
||||
local filepaths = {}
|
||||
for _, file in ipairs(files) do
|
||||
local uniform_path = Utils.uniform_path(file)
|
||||
table.insert(filepaths, uniform_path)
|
||||
end
|
||||
return vim.json.encode(filepaths), nil
|
||||
end
|
||||
|
||||
return M
|
||||
68
lua/avante/llm_tools/read_file.lua
Normal file
68
lua/avante/llm_tools/read_file.lua
Normal file
@@ -0,0 +1,68 @@
|
||||
local Utils = require("avante.utils")
|
||||
local Base = require("avante.llm_tools.base")
|
||||
local Helpers = require("avante.llm_tools.helpers")
|
||||
|
||||
---@class AvanteLLMTool
|
||||
local M = setmetatable({}, Base)
|
||||
|
||||
M.name = "read_file"
|
||||
|
||||
M.description =
|
||||
"Read the contents of a file in current project scope. If the file content is already in the context, do not use this tool."
|
||||
|
||||
M.enabled = function(opts)
|
||||
if opts.user_input:match("@read_global_file") then return false end
|
||||
for _, message in ipairs(opts.history_messages) do
|
||||
if message.role == "user" then
|
||||
local content = message.content
|
||||
if type(content) == "string" and content:match("@read_global_file") then return false end
|
||||
if type(content) == "table" then
|
||||
for _, item in ipairs(content) do
|
||||
if type(item) == "string" and item:match("@read_global_file") then return false end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
---@type AvanteLLMToolParam
|
||||
M.param = {
|
||||
type = "table",
|
||||
fields = {
|
||||
{
|
||||
name = "rel_path",
|
||||
description = "Relative path to the file in current project scope",
|
||||
type = "string",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
---@type AvanteLLMToolReturn[]
|
||||
M.returns = {
|
||||
{
|
||||
name = "content",
|
||||
description = "Contents of the file",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "error",
|
||||
description = "Error message if the file was not read successfully",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
}
|
||||
|
||||
---@type AvanteLLMToolFunc<{ rel_path: string }>
|
||||
function M.func(opts, on_log)
|
||||
local abs_path = Helpers.get_abs_path(opts.rel_path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return "", "No permission to access path: " .. abs_path end
|
||||
if on_log then on_log("path: " .. abs_path) end
|
||||
local file = io.open(abs_path, "r")
|
||||
if not file then return "", "file not found: " .. abs_path end
|
||||
local lines = Utils.read_file_from_buf_or_disk(abs_path)
|
||||
local content = lines and table.concat(lines, "\n") or ""
|
||||
return content, nil
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -318,14 +318,14 @@ vim.g.avante_login = vim.g.avante_login
|
||||
---@field memory string | nil
|
||||
---
|
||||
---@class AvanteGeneratePromptsOptions: AvanteTemplateOptions
|
||||
---@field ask boolean
|
||||
---@field instructions? string
|
||||
---@field mode AvanteLlmMode
|
||||
---@field mode? AvanteLlmMode
|
||||
---@field provider AvanteProviderFunctor | AvanteBedrockProviderFunctor | nil
|
||||
---@field tools? AvanteLLMTool[]
|
||||
---@field tool_histories? AvanteLLMToolHistory[]
|
||||
---@field original_code? string
|
||||
---@field update_snippets? string[]
|
||||
---@field prompt_opts? AvantePromptOptions
|
||||
---
|
||||
---@class AvanteLLMToolHistory
|
||||
---@field tool_result? AvanteLLMToolResult
|
||||
|
||||
Reference in New Issue
Block a user