kongxin cee54cdd49
fix(web-fetch): stop sending .env.example placeholder keys to providers (#8206)
* fix(web-fetch): stop sending .env.example placeholder keys to providers

make config and make setup copy .env.example into .env, and the template
set nine search/fetch provider keys to example values such as
JINA_API_KEY=your-jina-api-key. The default keyless Jina Reader sent
that text as a Bearer key, so every web_fetch on a fresh install failed
with 401 while make doctor reported web fetch as configured.

- .env.example comments those nine lines out, like every other credential
  in the template already is.
- provider_credentials.resolve_api_key reads a your-...-key/-token/-secret
  value as unset, so existing .env files that still hold the placeholders
  recover without editing, for every provider that resolves keys there.

* chore: leave the changelog entry to merge time

Per CONTRIBUTING.md (#8202), maintainers add changelog entries when they
merge a pull request.

* fix(web-search): route every provider key and doctor through one placeholder rule

Review follow-up for #8206:

- provider_credentials exposes is_template_placeholder(), the single
  definition of an .env.example example value. key/token/secret must be
  a whole hyphen-separated word, so a real value such as your-monkey or
  your-passkey is no longer dropped.
- Serper, Serply, Sofya, SpecStream, Unbrowse, Webz, Brave, Tencent WSA
  and You.com resolve config and environment keys through
  resolve_api_key instead of private strip-and-send copies, so an old
  .env that still holds the template value no longer sends it.
- make doctor asks the same predicate: a placeholder key is reported as
  still holding the .env.example value instead of "set", and a literal
  placeholder in config.yaml falls back to the environment key as the
  runtime does.
- A census test keeps credential environment reads under community/
  wrapped in resolve_api_key.

* fix(web-search): match shipped placeholders exactly and mirror Browserless precedence in doctor

Second review follow-up for #8206:

- is_template_placeholder() matches exactly the your-... values that
  .env.example ships instead of a pattern, so a token an operator chose
  (a self-hosted Browserless TOKEN such as your-review-team-token) is
  sent again. A test keeps the set equal to the template.
- make doctor mirrors Browserless: a configured token field is
  authoritative, blank or not, and BROWSERLESS_TOKEN is read only when
  the field is omitted. A blank or placeholder token now warns instead
  of reporting the variable, cross-checked against the client.
- The keyless Jina and You.com providers stay ok but name JINA_API_KEY
  or YDC_API_KEY when a placeholder there is ignored, and a literal
  placeholder api_key in config.yaml is named when no env key backs it.

* fix(doctor): match each web provider's key precedence; drop the dead Jina api_key from docs

Found while re-checking #8206 against every provider's runtime:

- Browserless used as web_fetch had no doctor key check; it now gets the
  same authoritative token handling as web_capture.
- InfoQuest reads only INFOQUEST_API_KEY, so doctor no longer counts an
  api_key field in its tool config as configured.
- GroundRoute needs a key for every call but had no doctor entry, so a
  missing key reported ok.
- You.com names a placeholder api_key in config that it ignores.
- The Jina web_fetch docs snippet carried api_key: $JINA_API_KEY, which
  the tool never reads. With JINA_API_KEY now commented out in
  .env.example, that line made config loading fail on a fresh install.
- The provider_credentials docstring now scopes doctor's use of the
  predicate to web provider keys, and the scripts guide no longer says
  .env.example ships live placeholder keys.
2026-11-13 23:23:29 +08:00

294 lines
11 KiB
Python

"""
Web and image search tools powered by the Brave Search API.
Brave Search provides web and image results from an independent search index
via a REST API. An API key is required. Sign up at
https://brave.com/search/api/ to get one.
Unlike the DuckDuckGo ``backend: brave`` option (which scrapes results via the
DDGS aggregator), this provider calls the official Brave Search API directly,
giving structured results, authenticated quota, and a documented SLA.
"""
import json
import logging
import os
import httpx
from langchain.tools import tool
from deerflow.community.provider_credentials import resolve_api_key
from deerflow.community.search_time_range import BRAVE_FRESHNESS_BY_TIME_RANGE, SearchTimeRange
from deerflow.community.url_safety import safe_public_result_url as _safe_public_url
from deerflow.config import get_app_config
logger = logging.getLogger(__name__)
_BRAVE_WEB_ENDPOINT = "https://api.search.brave.com/res/v1/web/search"
_BRAVE_IMAGES_ENDPOINT = "https://api.search.brave.com/res/v1/images/search"
_DEFAULT_MAX_RESULTS = 5
# Brave Search API caps the `count` parameter at 20 results per request.
_BRAVE_WEB_MAX_COUNT = 20
# Brave Image Search supports larger batches than web search.
_BRAVE_IMAGE_MAX_COUNT = 200
_api_key_warned: set[str] = set()
def _get_api_key(tool_name: str = "web_search") -> str | None:
config = get_app_config().get_tool_config(tool_name)
configured = resolve_api_key((config.model_extra or {}).get("api_key")) if config is not None else None
return configured or resolve_api_key(os.getenv("BRAVE_SEARCH_API_KEY"))
def _coerce_max_results(
value: object,
*,
default: int = _DEFAULT_MAX_RESULTS,
max_allowed: int = _BRAVE_WEB_MAX_COUNT,
) -> int:
try:
coerced = int(value)
except (TypeError, ValueError, OverflowError):
logger.warning(
"Invalid Brave Search max_results=%r; using default %s",
value,
default,
)
coerced = default
return max(1, min(coerced, max_allowed))
def _clean_query(query: str, *, max_length: int = 400) -> str:
query = query.strip()
if len(query) > max_length:
query = query[:max_length]
return query
def _missing_key_error(query: str, tool_name: str) -> str:
if tool_name not in _api_key_warned:
_api_key_warned.add(tool_name)
logger.warning(
"Brave Search API key is not set for '%s'. Set BRAVE_SEARCH_API_KEY in your environment or provide api_key in config.yaml. Sign up at https://brave.com/search/api/",
tool_name,
)
return json.dumps(
{"error": "BRAVE_SEARCH_API_KEY is not configured", "query": query},
ensure_ascii=False,
)
def _unexpected_format_error(query: str, *, service_name: str = "Brave Search") -> str:
return json.dumps(
{"error": f"{service_name} returned an unexpected response format", "query": query},
ensure_ascii=False,
)
def _is_url_present(value: object) -> bool:
return isinstance(value, str) and bool(value.strip())
def _brave_get(
endpoint: str,
api_key: str,
query: str,
params: dict[str, object],
*,
service_name: str,
) -> tuple[dict | None, str | None]:
headers = {
"X-Subscription-Token": api_key,
"Accept": "application/json",
}
try:
with httpx.Client(timeout=30) as client:
response = client.get(endpoint, headers=headers, params=params)
response.raise_for_status()
data = response.json()
if not isinstance(data, dict):
logger.error("%s returned an unexpected payload type: %s", service_name, type(data).__name__)
return None, _unexpected_format_error(query, service_name=service_name)
return data, None
except httpx.HTTPStatusError as e:
logger.error("%s API returned HTTP %s: %s", service_name, e.response.status_code, e.response.text)
return None, json.dumps(
{"error": f"{service_name} API error: HTTP {e.response.status_code}", "query": query},
ensure_ascii=False,
)
except Exception as e:
logger.error("%s request failed: %s: %s", service_name, type(e).__name__, e)
return None, json.dumps({"error": str(e), "query": query}, ensure_ascii=False)
@tool("web_search", parse_docstring=True)
def web_search_tool(query: str, max_results: int = 5, time_range: SearchTimeRange | None = None) -> str:
"""Search the web for information using Brave Search.
Args:
query: Search keywords describing what you want to find. Be specific for better results.
max_results: Maximum number of search results to return. Default is 5.
time_range: Optional relative publication/update window. Use only when the request requires recent results.
"""
config = get_app_config().get_tool_config("web_search")
if config is not None and "max_results" in (config.model_extra or {}):
max_results = config.model_extra["max_results"]
count = _coerce_max_results(max_results, max_allowed=_BRAVE_WEB_MAX_COUNT)
query = _clean_query(query)
api_key = _get_api_key("web_search")
if not api_key:
return _missing_key_error(query, "web_search")
params = {"q": query, "count": count, "text_decorations": False}
if time_range is not None:
params["freshness"] = BRAVE_FRESHNESS_BY_TIME_RANGE[time_range]
data, error_json = _brave_get(_BRAVE_WEB_ENDPOINT, api_key, query, params, service_name="Brave Search")
if error_json is not None:
return error_json
web = data.get("web")
if web is None:
web = {}
if not isinstance(web, dict):
logger.error("Brave Search returned unexpected 'web' payload type: %s", type(web).__name__)
return _unexpected_format_error(query)
web_results = web.get("results")
if web_results is None:
web_results = []
if not isinstance(web_results, list):
logger.error("Brave Search returned unexpected 'web.results' payload type: %s", type(web_results).__name__)
return _unexpected_format_error(query)
if not web_results:
return json.dumps({"error": "No results found", "query": query}, ensure_ascii=False)
normalized_results = [
{
"title": r.get("title", ""),
"url": _safe_public_url(r.get("url")),
"content": r.get("description", ""),
}
for r in web_results
if isinstance(r, dict)
]
if not normalized_results:
logger.error("Brave Search returned 'web.results' with no usable result objects")
return _unexpected_format_error(query)
output = {
"query": query,
"total_results": len(normalized_results),
"results": normalized_results,
}
return json.dumps(output, indent=2, ensure_ascii=False)
@tool("image_search", parse_docstring=True)
def image_search_tool(query: str, max_results: int = 5) -> str:
"""Search for images online using Brave Image Search. Use this tool BEFORE image generation to find reference images for characters, portraits, objects, scenes, or any content requiring visual accuracy.
The returned image URLs can be used as reference images in image generation to significantly improve quality.
Args:
query: Search keywords describing the images you want to find. Be specific for better results.
max_results: Maximum number of images to return. Default is 5, capped at 200.
"""
config = get_app_config().get_tool_config("image_search")
extra = (config.model_extra or {}) if config is not None else {}
if "max_results" in extra:
max_results = extra["max_results"]
count = _coerce_max_results(max_results, max_allowed=_BRAVE_IMAGE_MAX_COUNT)
query = _clean_query(query)
api_key = _get_api_key("image_search")
if not api_key:
return _missing_key_error(query, "image_search")
params: dict[str, object] = {"q": query, "count": count}
for key in ("country", "search_lang", "safesearch", "spellcheck"):
if key in extra:
params[key] = extra[key]
data, error_json = _brave_get(
_BRAVE_IMAGES_ENDPOINT,
api_key,
query,
params,
service_name="Brave Image Search",
)
if error_json is not None:
return error_json
images = data.get("results")
if images is None:
images = []
if not isinstance(images, list):
logger.error("Brave Image Search returned unexpected 'results' payload type: %s", type(images).__name__)
return _unexpected_format_error(query, service_name="Brave Image Search")
if not images:
return json.dumps({"error": "No images found", "query": query}, ensure_ascii=False)
normalized_results = []
for item in images:
if not isinstance(item, dict):
continue
thumbnail = item.get("thumbnail") if isinstance(item.get("thumbnail"), dict) else {}
properties = item.get("properties") if isinstance(item.get("properties"), dict) else {}
raw_image = properties.get("url")
raw_thumb = thumbnail.get("src")
raw_source = item.get("url")
safe_image = _safe_public_url(raw_image)
safe_thumb = _safe_public_url(raw_thumb)
safe_source = _safe_public_url(raw_source)
# Surface a URL and remember which dict it came from, so the reported
# width/height describe the URL we actually return rather than a
# dropped one.
if safe_image:
image_url, image_dims = safe_image, properties
elif not _is_url_present(raw_image):
image_url, image_dims = safe_thumb, thumbnail
else:
image_url, image_dims = "", {}
if safe_thumb:
thumbnail_url, thumb_dims = safe_thumb, thumbnail
elif not _is_url_present(raw_thumb):
thumbnail_url, thumb_dims = safe_image, properties
else:
thumbnail_url, thumb_dims = "", {}
if not image_url and not thumbnail_url:
continue
dims = image_dims if image_url else thumb_dims
normalized_results.append(
{
"title": item.get("title", ""),
"image_url": image_url,
"thumbnail_url": thumbnail_url,
"source_url": safe_source,
"source": item.get("source", ""),
"width": dims.get("width"),
"height": dims.get("height"),
}
)
if len(normalized_results) >= count:
break
if not normalized_results:
return json.dumps({"error": "No safe image URLs found", "query": query}, ensure_ascii=False)
output = {
"query": query,
"total_results": len(normalized_results),
"results": normalized_results,
"usage_hint": "Use the 'image_url' values as reference images in image generation. Download them first if needed.",
}
return json.dumps(output, indent=2, ensure_ascii=False)