feat: fetch tool (#1196)
This commit is contained in:
27
lua/avante/html2md.lua
Normal file
27
lua/avante/html2md.lua
Normal file
@@ -0,0 +1,27 @@
|
||||
---@class AvanteHtml2Md
|
||||
---@field fetch_md fun(url: string): string
|
||||
local _html2md_lib = nil
|
||||
|
||||
local M = {}
|
||||
|
||||
---@return AvanteHtml2Md|nil
|
||||
function M._init_html2md_lib()
|
||||
if _html2md_lib ~= nil then return _html2md_lib end
|
||||
|
||||
local ok, core = pcall(require, "avante_html2md")
|
||||
if not ok then return nil end
|
||||
|
||||
_html2md_lib = core
|
||||
return _html2md_lib
|
||||
end
|
||||
|
||||
function M.setup() vim.defer_fn(M._init_html2md_lib, 1000) end
|
||||
|
||||
function M.fetch_md(url)
|
||||
local html2md_lib = M._init_html2md_lib()
|
||||
if not html2md_lib then return "", "Failed to load avante_html2md" end
|
||||
|
||||
return html2md_lib.fetch_md(url)
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -360,6 +360,7 @@ function M.setup(opts)
|
||||
|
||||
H.load_path()
|
||||
|
||||
require("avante.html2md").setup()
|
||||
require("avante.repo_map").setup()
|
||||
require("avante.path").setup()
|
||||
require("avante.highlights").setup()
|
||||
|
||||
@@ -308,6 +308,18 @@ function M.web_search(opts, on_log)
|
||||
end
|
||||
end
|
||||
|
||||
---@param opts { url: string }
|
||||
---@param on_log? fun(log: string): nil
|
||||
---@return string|nil result
|
||||
---@return string|nil error
|
||||
function M.fetch(opts, on_log)
|
||||
if on_log then on_log("url: " .. opts.url) end
|
||||
local Html2Md = require("avante.html2md")
|
||||
local res = Html2Md.fetch_md(opts.url)
|
||||
if res == nil then return nil, "Failed to fetch markdown" end
|
||||
return res, nil
|
||||
end
|
||||
|
||||
---@class AvanteLLMTool
|
||||
---@field name string
|
||||
---@field description string
|
||||
@@ -715,6 +727,33 @@ M.tools = {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name = "fetch",
|
||||
description = "Fetch markdown from a url",
|
||||
param = {
|
||||
type = "table",
|
||||
fields = {
|
||||
{
|
||||
name = "url",
|
||||
description = "Url to fetch markdown from",
|
||||
type = "string",
|
||||
},
|
||||
},
|
||||
},
|
||||
returns = {
|
||||
{
|
||||
name = "result",
|
||||
description = "Result of the fetch",
|
||||
type = "string",
|
||||
},
|
||||
{
|
||||
name = "error",
|
||||
description = "Error message if the fetch was not successful",
|
||||
type = "string",
|
||||
optional = true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
---@param tools AvanteLLMTool[]
|
||||
|
||||
@@ -13,7 +13,6 @@ local filetype_map = {
|
||||
---@field stringify_definitions fun(lang: string, source: string): string
|
||||
local repo_map_lib = nil
|
||||
|
||||
---@class avante.utils.repo_map
|
||||
local RepoMap = {}
|
||||
|
||||
---@return AvanteRepoMap|nil
|
||||
|
||||
@@ -7,7 +7,6 @@ local lsp = vim.lsp
|
||||
---@class avante.utils: LazyUtilCore
|
||||
---@field tokens avante.utils.tokens
|
||||
---@field root avante.utils.root
|
||||
---@field repo_map avante.utils.repo_map
|
||||
---@field file avante.utils.file
|
||||
local M = {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user