mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-11-12 22:20:33 +00:00
* fix(community): strip provider API keys before they reach a header GroundRoute (from GROUNDROUTE_API_KEY), InfoQuest, Tavily, Exa, Jina, Firecrawl, fastCRW and Crawl4AI put the configured or environment key into an HTTP header as read. A key read from a file keeps its trailing newline (a Kubernetes Secret created with --from-file and mounted through envFrom), and the transport refuses it with an error that quotes the whole header: h11's "Illegal header value b'Bearer <key>\n'" or requests' "... in header value: '<key>\n'". The providers returned that message as the tool result, so the key reached the model and the checkpointed conversation, and GroundRoute, InfoQuest, Jina and Crawl4AI also wrote it to the error log. Resolve each key through provider_credentials.resolve_api_key, as the other bundled providers already strip theirs. Tavily, Exa and Firecrawl now read their environment fallback themselves instead of leaving it to the SDK, which would read the raw value again. A blank configured key now falls back to the environment, as it already does for GroundRoute and the stripping providers. * docs(changelog): reference #7981 for the provider API key fix * fix(community): keep fastCRW from sending the Firecrawl key fastCRW builds the Firecrawl SDK client, and the SDK reads FIRECRAWL_API_KEY whenever api_key is None. Once a blank configured key resolved to None, a fastCRW config with api_key: "" and no CRW_API_KEY sent the Firecrawl key to the fastCRW endpoint. With no api_key configured at all, main already did the same. Pass "" when fastCRW resolves no key of its own; the SDK falls back only on None and sends no Authorization header for an empty key. A loopback test pins the empty, blank and absent cases with FIRECRAWL_API_KEY set. * fix(community): keep a blank environment key away from the SDKs Tavily, Exa and Firecrawl resolved their environment fallback themselves, but passed None when it was blank, and each SDK then re-read the same variable unstripped. Tavily and Firecrawl now pass "", which the SDKs treat as a missing key; Exa sends "" as is, so it fails before any request instead. The fastCRW comment records that only the Firecrawl SDK's v2 client honours "", and the fastCRW regression test now covers web_fetch as well as web_search, so a move to the v1 client would fail it. --------- Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
175 lines
7.1 KiB
Python
175 lines
7.1 KiB
Python
import asyncio
|
|
import inspect
|
|
import json
|
|
import logging
|
|
import os
|
|
|
|
from firecrawl import AsyncFirecrawlApp
|
|
from langchain.tools import tool
|
|
|
|
from deerflow.community.provider_credentials import resolve_api_key
|
|
from deerflow.community.search_max_results import DEFAULT_MAX_RESULTS, coerce_max_results
|
|
from deerflow.community.url_safety import safe_public_result_url, validate_delegated_backend_url, validate_public_http_url
|
|
from deerflow.config import get_app_config
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def _aclose_firecrawl_client(client: AsyncFirecrawlApp) -> None:
|
|
"""Best-effort close of the pooled async HTTP client a per-call app constructed.
|
|
|
|
``AsyncFirecrawlApp`` eagerly builds an ``httpx.AsyncClient``-backed pool in
|
|
its constructor and exposes no public teardown, so reach it through the
|
|
delegating v2 client. The declared dependency range (``firecrawl-py>=1.15.0``)
|
|
includes versions without that attribute, and teardown runs from a
|
|
``finally`` — absence or failure here must never mask the tool's own result.
|
|
"""
|
|
pooled = getattr(getattr(client, "_v2_client", None), "async_http_client", None)
|
|
if pooled is None:
|
|
return
|
|
try:
|
|
close = getattr(pooled, "close", None)
|
|
if not callable(close):
|
|
logger.warning("Firecrawl async HTTP pool has no close method")
|
|
return
|
|
result = close()
|
|
if inspect.isawaitable(result):
|
|
await result
|
|
except Exception:
|
|
logger.warning("Failed to close the Firecrawl async HTTP pool", exc_info=True)
|
|
|
|
|
|
def _get_tool_config_extra(tool_name: str) -> dict:
|
|
config = get_app_config().get_tool_config(tool_name)
|
|
return dict(config.model_extra or {}) if config is not None else {}
|
|
|
|
|
|
def _coerce_bool(value: object, default: bool) -> bool:
|
|
if isinstance(value, bool):
|
|
return value
|
|
if isinstance(value, str):
|
|
normalized = value.strip().lower()
|
|
if normalized in {"1", "true", "yes", "on"}:
|
|
return True
|
|
if normalized in {"0", "false", "no", "off"}:
|
|
return False
|
|
return default
|
|
|
|
|
|
def _validate_backend_base_url(cfg: dict, base_url: str) -> str | None:
|
|
"""Refuse delegation to a self-hosted Firecrawl backend unless its egress is isolated.
|
|
|
|
Firecrawl resolves the target URL, follows redirects, and loads subresources
|
|
in the Firecrawl service's own network namespace, so the target-URL screen
|
|
cannot be enforced end-to-end. Delegation is only safe when the backend's
|
|
outbound network is isolated from private and metadata networks, which the
|
|
operator confirms via ``network_isolation_confirmed``. Blocking; call via
|
|
``asyncio.to_thread``.
|
|
"""
|
|
network_isolation_confirmed = _coerce_bool(cfg.get("network_isolation_confirmed"), False)
|
|
return validate_delegated_backend_url(base_url, network_isolation_confirmed=network_isolation_confirmed)
|
|
|
|
|
|
def _get_firecrawl_client(
|
|
tool_name: str = "web_search",
|
|
*,
|
|
cfg: dict | None = None,
|
|
api_url: str | None = None,
|
|
) -> AsyncFirecrawlApp:
|
|
"""Build a Firecrawl app from one configuration snapshot.
|
|
|
|
``cfg`` is the tool config extras already read by the caller. Resolving the
|
|
API key and endpoint from that same snapshot keeps a backend change made
|
|
between the URL screen and client construction from pairing one revision's
|
|
endpoint with another revision's key.
|
|
"""
|
|
if cfg is None:
|
|
cfg = _get_tool_config_extra(tool_name)
|
|
api_key = resolve_api_key(cfg.get("api_key")) or resolve_api_key(os.getenv("FIRECRAWL_API_KEY"))
|
|
if api_url is None:
|
|
api_url = cfg.get("base_url")
|
|
# The SDK re-reads FIRECRAWL_API_KEY unstripped when api_key is None; "" makes
|
|
# a blank value act as a missing key.
|
|
kwargs = {"api_key": api_key or ""}
|
|
if api_url:
|
|
kwargs["api_url"] = api_url
|
|
return AsyncFirecrawlApp(**kwargs) # type: ignore[arg-type]
|
|
|
|
|
|
@tool("web_search", parse_docstring=True)
|
|
async def web_search_tool(query: str) -> str:
|
|
"""Search the web.
|
|
|
|
Args:
|
|
query: The query to search for.
|
|
"""
|
|
client: AsyncFirecrawlApp | None = None
|
|
try:
|
|
# Config resolution stats and hashes the config files; keep it off the
|
|
# event loop, and build the client from the same read.
|
|
cfg = await asyncio.to_thread(_get_tool_config_extra, "web_search")
|
|
max_results = coerce_max_results(cfg.get("max_results", DEFAULT_MAX_RESULTS), provider="Firecrawl", logger=logger)
|
|
|
|
client = _get_firecrawl_client("web_search", cfg=cfg)
|
|
result = await client.search(query, limit=max_results)
|
|
|
|
# result.web contains list of SearchResultWeb objects
|
|
web_results = result.web or []
|
|
normalized_results = [
|
|
{
|
|
"title": getattr(item, "title", "") or "",
|
|
"url": safe_public_result_url(getattr(item, "url", None)),
|
|
"snippet": getattr(item, "description", "") or "",
|
|
}
|
|
for item in web_results
|
|
]
|
|
json_results = json.dumps(normalized_results, indent=2, ensure_ascii=False)
|
|
return json_results
|
|
except Exception as e:
|
|
return f"Error: {str(e)}"
|
|
finally:
|
|
if client is not None:
|
|
await _aclose_firecrawl_client(client)
|
|
|
|
|
|
@tool("web_fetch", parse_docstring=True)
|
|
async def web_fetch_tool(url: str) -> str:
|
|
"""Fetch the contents of a web page at a given URL.
|
|
Only fetch EXACT URLs that have been provided directly by the user or have been returned in results from the web_search and web_fetch tools.
|
|
This tool can NOT access content that requires authentication, such as private Google Docs or pages behind login walls.
|
|
Do NOT add www. to URLs that do NOT have them.
|
|
URLs must include the schema: https://example.com is a valid URL while example.com is an invalid URL.
|
|
|
|
Args:
|
|
url: The URL to fetch the contents of.
|
|
"""
|
|
client: AsyncFirecrawlApp | None = None
|
|
try:
|
|
# Config resolution stats and hashes the config files; keep it off the event loop.
|
|
cfg = await asyncio.to_thread(_get_tool_config_extra, "web_fetch")
|
|
allow_private_addresses = _coerce_bool(cfg.get("allow_private_addresses"), False)
|
|
url_error = await asyncio.to_thread(validate_public_http_url, url, allow_private_addresses=allow_private_addresses)
|
|
if url_error:
|
|
return url_error
|
|
api_url = cfg.get("base_url")
|
|
if api_url is not None:
|
|
backend_error = await asyncio.to_thread(_validate_backend_base_url, cfg, api_url)
|
|
if backend_error:
|
|
return backend_error
|
|
client = _get_firecrawl_client("web_fetch", cfg=cfg, api_url=api_url)
|
|
result = await client.scrape(url, formats=["markdown"])
|
|
|
|
markdown_content = result.markdown or ""
|
|
metadata = result.metadata
|
|
title = metadata.title if metadata and metadata.title else "Untitled"
|
|
|
|
if not markdown_content:
|
|
return "Error: No content found"
|
|
except Exception as e:
|
|
return f"Error: {str(e)}"
|
|
finally:
|
|
if client is not None:
|
|
await _aclose_firecrawl_client(client)
|
|
|
|
return f"# {title}\n\n{markdown_content[:4096]}"
|