代码提交

This commit is contained in:
董宏凯 committed 2026-08-16 19:17:45 +08:00
1 parent 70da7c5649
commit 178572bc7f
79 files changed
+13456 -798

No files matched your search

@@ -1,9 +1,19 @@
"""
Web Search Tool - Search the web using DuckDuckGo (no API key required).
Web Search Tool - Search using DuckDuckGo (no API key required).
DuckDuckGo's html endpoint rate-limits bursts of requests from the same IP
(403/429 challenge pages). The ``ddgs`` library raises ``DDGSException`` for
those (it does not retry), so rapid consecutive tool calls — e.g. a research
agent firing one query after another — fail on the 2nd/3rd call. We retry with
exponential backoff here and keep the error envelopes distinct so the calling
model can tell "no results" apart from "rate limited, retry later" instead of
inventing its own "network fluctuation" narrative.
"""
import json
import logging
import random
import time
from langchain.tools import tool
@@ -11,13 +21,17 @@ from deerflow.config import get_app_config
logger = logging.getLogger(__name__)
# Transient-failure retries (total attempts = 1 + _MAX_RETRIES).
_MAX_RETRIES = 2
_RETRY_DELAYS = (1.5, 3.0)
def _search_text(
query: str,
max_results: int = 5,
region: str = "wt-wt",
safesearch: str = "moderate",
) -> list[dict]:
) -> list[dict] | None:
"""
Execute text search using DuckDuckGo.
@@ -28,28 +42,49 @@ def _search_text(
safesearch: Safe search level
Returns:
List of search results
List of search results, or ``None`` when every attempt failed
(rate limited / network error) — the caller must not confuse that
with a genuine empty result set.
"""
try:
from ddgs import DDGS
except ImportError:
logger.error("ddgs library not installed. Run: pip install ddgs")
return []
return None
ddgs = DDGS(timeout=30)
try:
results = ddgs.text(
query,
region=region,
safesearch=safesearch,
max_results=max_results,
)
return list(results) if results else []
last_error: Exception | None = None
for attempt in range(_MAX_RETRIES + 1):
try:
results = ddgs.text(
query,
region=region,
safesearch=safesearch,
max_results=max_results,
)
return list(results) if results else []
except Exception as e:
last_error = e
if attempt < _MAX_RETRIES:
delay = _RETRY_DELAYS[attempt] + random.uniform(0, 0.5)
logger.warning(
"DuckDuckGo search attempt %s/%s failed (%s: %s); retrying in %.1fs",
attempt + 1,
_MAX_RETRIES + 1,
type(e).__name__,
e,
delay,
)
time.sleep(delay)
except Exception as e:
logger.error(f"Failed to search web: {e}")
return []
logger.error(
"Failed to search web after %s attempts: %s: %s",
_MAX_RETRIES + 1,
type(last_error).__name__ if last_error else "unknown",
last_error,
)
return None
@tool("web_search", parse_docstring=True)
@@ -60,8 +95,8 @@ def web_search_tool(
"""Search the web for information. Use this tool to find current information, news, articles, and facts from the internet.
Args:
query: Search keywords describing what you want to find. Be specific for better results.
max_results: Maximum number of results to return. Default is 5.
query: Search keywords describing what to find. Be specific for better results.
max_results: Maximum number of search results to return. Default is 5.
"""
config = get_app_config().get_tool_config("web_search")
@@ -74,6 +109,14 @@ def web_search_tool(
max_results=max_results,
)
if results is None:
return json.dumps(
{
"error": ("web search failed after retries (rate limited or network error); wait briefly and retry once with different keywords instead of reporting a network problem to the user"),
"query": query,
},
ensure_ascii=False,
)
if not results:
return json.dumps({"error": "No results found", "query": query}, ensure_ascii=False)