feat: add view_range parameter and remove read_file llm tool (#1690)

This commit is contained in:
yetone
2025-03-24 15:07:14 +08:00
committed by GitHub
parent 4749e4ea1a
commit 49ae3c84fd
10 changed files with 179 additions and 113 deletions

View File

@@ -9,10 +9,10 @@ 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:
[[Launch a new agent that has access to the following tools: `glob`, `grep`, `ls`, `view`. 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 want to read a specific file path, use the `view` 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:
@@ -54,7 +54,7 @@ local function get_available_tools()
require("avante.llm_tools.ls"),
require("avante.llm_tools.grep"),
require("avante.llm_tools.glob"),
require("avante.llm_tools.read_file"),
require("avante.llm_tools.view"),
}
end

View File

@@ -25,7 +25,7 @@ function M.read_file_toplevel_symbols(opts, on_log)
return definitions, 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 }>
---@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, view_range?: integer[] }>
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
@@ -42,14 +42,16 @@ function M.str_replace_editor(opts, on_log, on_complete)
return bufnr
end
if opts.command == "view" then
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 file = io.open(abs_path, "r")
if not file then return false, "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 ""
on_complete(content, nil)
return
local view = require("avante.llm_tools.view")
local opts_ = { path = opts.path }
if opts.view_range then
local start_line, end_line = unpack(opts.view_range)
opts_.view_range = {
start_line = start_line,
end_line = end_line,
}
end
return view(opts_, on_log, on_complete)
end
if opts.command == "str_replace" then
if not Path:new(abs_path):exists() then return false, "File not found: " .. abs_path end
@@ -910,7 +912,7 @@ M._tools = {
},
},
},
require("avante.llm_tools.read_file"),
require("avante.llm_tools.view"),
{
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.",

View File

@@ -1,68 +0,0 @@
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

View File

@@ -0,0 +1,108 @@
local Path = require("plenary.path")
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 = "view"
M.description =
"The view command allows you to examine the contents of a file or list the contents of a directory. It can read the entire file or a specific range of lines. 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 = "path",
description = "The path to the file in the current project scope",
type = "string",
},
{
name = "view_range",
description = "The range of the file to view. This parameter only applies when viewing files, not directories.",
type = "object",
optional = true,
fields = {
{
name = "start_line",
description = "The start line of the range, 1-indexed",
type = "integer",
},
{
name = "end_line",
description = "The end line of the range, 1-indexed, and -1 for the end line means read to the end of the file",
type = "integer",
},
},
},
},
}
---@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<{ path: string, view_range?: { start_line: integer, end_line: integer } }>
function M.func(opts, on_log, on_complete)
if not on_complete then return false, "on_complete not provided" 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
if on_log then on_log("path: " .. abs_path) end
if not Path:new(abs_path):exists() then return false, "Path not found: " .. abs_path end
if Path:new(abs_path):is_dir() then
local files = vim.fn.glob(abs_path .. "/*", false, true)
if #files == 0 then return false, "Directory is empty: " .. abs_path end
local result = {}
for _, file in ipairs(files) do
if not Path:new(file):is_file() then goto continue end
local lines = Utils.read_file_from_buf_or_disk(file)
local content = lines and table.concat(lines, "\n") or ""
table.insert(result, { path = file, content = content })
::continue::
end
on_complete(vim.json.encode(result), nil)
return
end
local file = io.open(abs_path, "r")
if not file then return false, "file not found: " .. abs_path end
local lines = Utils.read_file_from_buf_or_disk(abs_path)
if opts.view_range then
local start_line = opts.view_range.start_line
local end_line = opts.view_range.end_line
if start_line and end_line and lines then lines = vim.list_slice(lines, start_line, end_line) end
end
local content = lines and table.concat(lines, "\n") or ""
on_complete(content, nil)
end
return M