feat: add Brave Search as web search engine provider (#1481)
* feat: add Brave Search as web search engine provider * docs: update README
This commit is contained in:
@@ -161,6 +161,28 @@ M._defaults = {
|
||||
return "", nil
|
||||
end,
|
||||
},
|
||||
brave = {
|
||||
api_key_name = "BRAVE_API_KEY",
|
||||
extra_request_body = {
|
||||
count = "10",
|
||||
result_filter = "web",
|
||||
},
|
||||
format_response_body = function(body)
|
||||
if body.web == nil then return "", nil end
|
||||
|
||||
local jsn = vim.iter(body.web.results):map(
|
||||
function(result)
|
||||
return {
|
||||
title = result.title,
|
||||
url = result.url,
|
||||
snippet = result.description,
|
||||
}
|
||||
end
|
||||
)
|
||||
|
||||
return vim.json.encode(jsn), nil
|
||||
end,
|
||||
},
|
||||
},
|
||||
},
|
||||
---@type AvanteSupportedProvider
|
||||
|
||||
@@ -378,6 +378,23 @@ function M.web_search(opts, on_log)
|
||||
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)
|
||||
elseif provider_type == "brave" then
|
||||
local query_params = vim.tbl_deep_extend("force", {
|
||||
q = opts.query,
|
||||
}, search_engine.extra_request_body)
|
||||
local query_string = ""
|
||||
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, {
|
||||
headers = {
|
||||
["Content-Type"] = "application/json",
|
||||
["X-Subscription-Token"] = api_key,
|
||||
},
|
||||
})
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user