fix: enhance web search functionality with proxy support (#1823)

* fix: enhance web search functionality with proxy support

- Remove unnecessary blank line in README.md
- Add missing closing details tag in both README.md and README_zh.md
- Add proxy support to the web search engine configuration in config.lua
- Ensure the web search function uses the proxy setting when available in init.lua
- Fix a potential nil access in the response body format check in config.lua

Signed-off-by: 范立洲 <fanlizhou@yunqilaohe.com>

* [pre-commit.ci lite] apply automatic fixes

---------

Signed-off-by: 范立洲 <fanlizhou@yunqilaohe.com>
Co-authored-by: 范立洲 <fanlizhou@yunqilaohe.com>
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
doodleEsc
2025-04-07 15:05:22 +08:00
committed by GitHub
parent 40ffc0f5fa
commit 1c36cfc812
4 changed files with 101 additions and 80 deletions

View File

@@ -28,7 +28,6 @@
[查看中文版](README_zh.md)
> [!NOTE]
>
> 🥰 This project is undergoing rapid iterations, and many exciting features will be added successively. Stay tuned!
@@ -436,6 +435,7 @@ _See [config.lua#L9](./lua/avante/config.lua) for the full config_
},
}
```
</details>
## Blink.cmp users
@@ -641,6 +641,7 @@ The following key bindings are available for use with `avante.nvim`:
### Neotree shortcut
In the neotree sidebar, you can also add a new keyboard shortcut to quickly add `file/folder` to `Avante Selected Files`.
<details>
<summary>Neotree configuration</summary>
@@ -685,6 +686,7 @@ return {
},
}
```
</details>
## Commands
@@ -814,6 +816,7 @@ The default is Tavily, and can be changed through configuring `Config.web_search
```lua
web_search_engine = {
provider = "tavily", -- tavily, serpapi, searchapi, google or kagi
proxy = nil, -- proxy support, e.g., http://127.0.0.1:7890
}
```
@@ -906,6 +909,7 @@ Avante allows you to define custom tools that can be used by the AI during code
},
}
```
</details>
## MCP
@@ -927,7 +931,6 @@ Avante leverages [Claude Text Editor Tool](https://docs.anthropic.com/en/docs/bu
> [!NOTE]
> To enable **Claude Text Editor Tool Mode**, you must use the `claude-3-5-sonnet-*` or `claude-3-7-sonnet-*` model with the `claude` provider! This feature is not supported by any other models!
## Custom prompts
By default, `avante.nvim` provides three different modes to interact with: `planning`, `editing`, and `suggesting`, followed with three different prompts per mode.

View File

@@ -28,7 +28,6 @@
[View in English](README.md)
> [!NOTE]
>
> 🥰 该项目正在快速迭代中,许多令人兴奋的功能将陆续添加。敬请期待!
@@ -436,6 +435,7 @@ _请参见 [config.lua#L9](./lua/avante/config.lua) 以获取完整配置_
},
}
```
</details>
## Blink.cmp 用户
@@ -614,7 +614,7 @@ _请参见 [config.lua#L9](./lua/avante/config.lua) 以获取完整配置_
以下键绑定可用于 `avante.nvim`
| 键绑定 | 描述 |
| ----------------------------------------- | -------------------------------------------- |
| ----------------------------------------- | ----------------------------- |
| <kbd>Leader</kbd><kbd>a</kbd><kbd>a</kbd> | 显示侧边栏 |
| <kbd>Leader</kbd><kbd>a</kbd><kbd>t</kbd> | 切换侧边栏可见性 |
| <kbd>Leader</kbd><kbd>a</kbd><kbd>r</kbd> | 刷新侧边栏 |
@@ -641,6 +641,7 @@ _请参见 [config.lua#L9](./lua/avante/config.lua) 以获取完整配置_
### Neotree 快捷方式
在 neotree 侧边栏中,您还可以添加新的键盘快捷方式,以快速将 `file/folder` 添加到 `Avante Selected Files`
<details>
<summary>Neotree 配置</summary>
@@ -685,12 +686,13 @@ return {
},
}
```
</details>
## 命令
| 命令 | 描述 | 示例 |
| ---------------------------------- | ----------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |
| ---------------------------------- | ---------------------------------------------------------------------------------------- | --------------------------------------------------- |
| `:AvanteAsk [question] [position]` | 询问 AI 关于您的代码的问题。可选的 `position` 设置窗口位置和 `ask` 启用/禁用直接询问模式 | `:AvanteAsk position=right Refactor this code here` |
| `:AvanteBuild` | 构建项目的依赖项 | |
| `:AvanteChat` | 启动与 AI 的聊天会话,讨论您的代码库。默认情况下 `ask`=false | |
@@ -707,7 +709,7 @@ return {
## 高亮组
| 高亮组 | 描述 | 备注 |
| --------------------------- | --------------------------------------------- | -------------------------------------------- |
| --------------------------- | -------------------------- | ------------------------------------------ |
| AvanteTitle | 标题 | |
| AvanteReversedTitle | 用于圆角边框 | |
| AvanteSubtitle | 选定代码标题 | |
@@ -811,6 +813,8 @@ Avante 的工具包括一些 Web 搜索引擎,目前支持:
```lua
web_search_engine = {
provider = "tavily", -- tavily, serpapi, searchapi, google 或 kagi
proxy = nil, -- proxy support, e.g., http://127.0.0.1:7890
}
```
@@ -903,6 +907,7 @@ Avante 允许您定义自定义工具AI 可以在代码生成和分析期间
},
}
```
</details>
## MCP
@@ -924,7 +929,6 @@ Avante 利用 [Claude 文本编辑器工具](https://docs.anthropic.com/en/docs/
> [!NOTE]
> 要启用 **Claude 文本编辑器工具模式**,您必须使用 `claude-3-5-sonnet-*` 或 `claude-3-7-sonnet-*` 模型与 `claude` 提供者!此功能不支持任何其他模型!
## 自定义提示
默认情况下,`avante.nvim` 提供三种不同的模式进行交互:`planning``editing``suggesting`,每种模式都有三种不同的提示。
@@ -1010,7 +1014,7 @@ vim.keymap.set("n", "<leader>am", function() vim.api.nvim_exec_autocmds("User",
我们要向以下开源项目的贡献者表示衷心的感谢,他们的代码为 avante.nvim 的开发提供了宝贵的灵感和参考:
| Nvim 插件 | 许可证 | 功能 | 位置 |
| --------------------------------------------------------------------- | ------------------ | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| --------------------------------------------------------------------- | ----------------- | ---------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| [git-conflict.nvim](https://github.com/akinsho/git-conflict.nvim) | 无许可证 | 差异比较功能 | [lua/avante/diff.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/diff.lua) |
| [ChatGPT.nvim](https://github.com/jackMort/ChatGPT.nvim) | Apache 2.0 许可证 | 令牌计数的计算 | [lua/avante/utils/tokens.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/utils/tokens.lua) |
| [img-clip.nvim](https://github.com/HakonHarnes/img-clip.nvim) | MIT 许可证 | 剪贴板图像支持 | [lua/avante/clipboard.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/clipboard.lua) |

View File

@@ -47,6 +47,7 @@ M._defaults = {
},
web_search_engine = {
provider = "tavily",
proxy = nil,
providers = {
tavily = {
api_key_name = "TAVILY_API_KEY",

View File

@@ -212,6 +212,7 @@ end
---@type AvanteLLMToolFunc<{ query: string }>
function M.web_search(opts, on_log)
local provider_type = Config.web_search_engine.provider
local proxy = Config.web_search_engine.proxy
if provider_type == nil then return nil, "Search engine provider is not set" end
if on_log then on_log("provider: " .. provider_type) end
if on_log then on_log("query: " .. opts.query) end
@@ -223,7 +224,7 @@ function M.web_search(opts, on_log)
return nil, "Environment variable " .. search_engine.api_key_name .. " is not set"
end
if provider_type == "tavily" then
local resp = curl.post("https://api.tavily.com/search", {
local curl_opts = {
headers = {
["Content-Type"] = "application/json",
["Authorization"] = "Bearer " .. api_key,
@@ -231,7 +232,9 @@ function M.web_search(opts, on_log)
body = vim.json.encode(vim.tbl_deep_extend("force", {
query = opts.query,
}, search_engine.extra_request_body)),
})
}
if proxy then curl_opts.proxy = proxy end
local resp = curl.post("https://api.tavily.com/search", curl_opts)
if resp.status ~= 200 then return nil, "Error: " .. resp.body end
local jsn = vim.json.decode(resp.body)
return search_engine.format_response_body(jsn)
@@ -244,11 +247,13 @@ function M.web_search(opts, on_log)
for key, value in pairs(query_params) do
query_string = query_string .. key .. "=" .. vim.uri_encode(value) .. "&"
end
local resp = curl.get("https://serpapi.com/search?" .. query_string, {
local curl_opts = {
headers = {
["Content-Type"] = "application/json",
},
})
}
if proxy then curl_opts.proxy = proxy end
local resp = curl.get("https://serpapi.com/search?" .. query_string, curl_opts)
if resp.status ~= 200 then return nil, "Error: " .. resp.body end
local jsn = vim.json.decode(resp.body)
return search_engine.format_response_body(jsn)
@@ -261,11 +266,13 @@ function M.web_search(opts, on_log)
for key, value in pairs(query_params) do
query_string = query_string .. key .. "=" .. vim.uri_encode(value) .. "&"
end
local resp = curl.get("https://searchapi.io/api/v1/search?" .. query_string, {
local curl_opts = {
headers = {
["Content-Type"] = "application/json",
},
})
}
if proxy then curl_opts.proxy = proxy end
local resp = curl.get("https://searchapi.io/api/v1/search?" .. query_string, curl_opts)
if resp.status ~= 200 then return nil, "Error: " .. resp.body end
local jsn = vim.json.decode(resp.body)
return search_engine.format_response_body(jsn)
@@ -283,11 +290,13 @@ function M.web_search(opts, on_log)
for key, value in pairs(query_params) do
query_string = query_string .. key .. "=" .. vim.uri_encode(value) .. "&"
end
local resp = curl.get("https://www.googleapis.com/customsearch/v1?" .. query_string, {
local curl_opts = {
headers = {
["Content-Type"] = "application/json",
},
})
}
if proxy then curl_opts.proxy = proxy end
local resp = curl.get("https://www.googleapis.com/customsearch/v1?" .. query_string, curl_opts)
if resp.status ~= 200 then return nil, "Error: " .. resp.body end
local jsn = vim.json.decode(resp.body)
return search_engine.format_response_body(jsn)
@@ -299,12 +308,14 @@ function M.web_search(opts, on_log)
for key, value in pairs(query_params) do
query_string = query_string .. key .. "=" .. vim.uri_encode(value) .. "&"
end
local resp = curl.get("https://kagi.com/api/v0/search?" .. query_string, {
local curl_opts = {
headers = {
["Authorization"] = "Bot " .. api_key,
["Content-Type"] = "application/json",
},
})
}
if proxy then curl_opts.proxy = proxy end
local resp = curl.get("https://kagi.com/api/v0/search?" .. query_string, curl_opts)
if resp.status ~= 200 then return nil, "Error: " .. resp.body end
local jsn = vim.json.decode(resp.body)
return search_engine.format_response_body(jsn)
@@ -316,12 +327,14 @@ function M.web_search(opts, on_log)
for key, value in pairs(query_params) do
query_string = query_string .. key .. "=" .. vim.uri_encode(value) .. "&"
end
local resp = curl.get("https://api.search.brave.com/res/v1/web/search?" .. query_string, {
local curl_opts = {
headers = {
["Content-Type"] = "application/json",
["X-Subscription-Token"] = api_key,
},
})
}
if proxy then curl_opts.proxy = proxy end
local resp = curl.get("https://api.search.brave.com/res/v1/web/search?" .. query_string, curl_opts)
if resp.status ~= 200 then return nil, "Error: " .. resp.body end
local jsn = vim.json.decode(resp.body)
return search_engine.format_response_body(jsn)