fix(web): back off Brave search rate limits

This commit is contained in:
hanyuanling
2026-05-16 00:12:50 +08:00
committed by Xubin Ren
parent 0f3677c0d8
commit b2ac609bb5
2 changed files with 77 additions and 11 deletions
+24 -10
View File
@@ -272,23 +272,37 @@ class WebSearchTool(Tool):
logger.warning("BRAVE_API_KEY not set, falling back to DuckDuckGo")
return await self._search_duckduckgo(query, n)
try:
headers = {
"Accept": "application/json",
"X-Subscription-Token": api_key,
"User-Agent": self.user_agent,
}
async with httpx.AsyncClient(proxy=self.proxy) as client:
r = await client.get(
"https://api.search.brave.com/res/v1/web/search",
params={"q": query, "count": n},
headers={
"Accept": "application/json",
"X-Subscription-Token": api_key,
"User-Agent": self.user_agent,
},
timeout=10.0,
)
for attempt in range(2):
r = await client.get(
"https://api.search.brave.com/res/v1/web/search",
params={"q": query, "count": n},
headers=headers,
timeout=10.0,
)
if r.status_code != 429:
break
if attempt == 0:
logger.warning("Brave search rate limited; retrying once in 1.0s")
await asyncio.sleep(1.0)
r.raise_for_status()
items = [
{"title": x.get("title", ""), "url": x.get("url", ""), "content": x.get("description", "")}
for x in r.json().get("web", {}).get("results", [])
]
return _format_results(query, items, n)
except httpx.HTTPStatusError as e:
if e.response.status_code == 429:
return (
"Error: Brave search rate limited after retry. "
"Retry later or reduce consecutive web_search calls."
)
return f"Error: {e}"
except Exception as e:
return f"Error: {e}"