Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

feat(search): gate Jina Reader fallback

xiaowenzihwl3435637e6b6f
diff --git a/jiuwenswarm/agents/harness/common/tools/web_fetch_tools.py b/jiuwenswarm/agents/harness/common/tools/web_fetch_tools.py
index a749116bf2..890da1ebd1 100644
--- a/jiuwenswarm/agents/harness/common/tools/web_fetch_tools.py
+++ b/jiuwenswarm/agents/harness/common/tools/web_fetch_tools.py
@@ -22,6 +22,7 @@ _USER_AGENT = (
_REQUEST_HEADERS = {"User-Agent": _USER_AGENT}
_FREE_SEARCH_PROXY_URL_ENV = "FREE_SEARCH_PROXY_URL"
_FREE_SEARCH_SSL_VERIFY_ENV = "FREE_SEARCH_SSL_VERIFY"
+_JINA_FETCH_ENABLED_ENV = "JIUWENSWARM_ENABLE_JINA_FETCH"
_FREE_SEARCH_DEFAULT_NO_PROXY = (
"127.0.0.1,.huawei.com,localhost,local,.local,10.155.97.247,.myhuaweicloud.com"
)
@@ -202,7 +203,10 @@ def _fetch_via_jina_reader_sync(url: str, timeout_seconds: int) -> dict[str, str
def _fetch_webpage_sync(url: str, timeout_seconds: int) -> dict[str, str | int]:
response = _http_get(url, headers=_REQUEST_HEADERS, timeout=timeout_seconds)
- if response.status_code in {401, 403, 429}:
+ if response.status_code in {401, 403, 429} and _env_bool(
+ _JINA_FETCH_ENABLED_ENV,
+ default=False,
+ ):
return _fetch_via_jina_reader_sync(url, timeout_seconds)
response.raise_for_status()
diff --git a/jiuwenswarm/resources/.env.template b/jiuwenswarm/resources/.env.template
index c6738e3b17..5e57ec5048 100644
--- a/jiuwenswarm/resources/.env.template
+++ b/jiuwenswarm/resources/.env.template
@@ -63,6 +63,9 @@ FREE_SEARCH_SSL_VERIFY=false
# DuckDuckGo HTML endpoint. html.duckduckgo.com is usually less likely to return
# anti-bot pages than duckduckgo.com/html behind corporate proxies.
FREE_SEARCH_DDG_URL=https://html.duckduckgo.com/html/
+# Allow mcp_fetch_webpage to send blocked URLs to the external Jina Reader
+# service after a direct request returns 401, 403, or 429. Disabled by default.
+JIUWENSWARM_ENABLE_JINA_FETCH=0
# Browser MCP runtime (optional)
# 0: disabled, 1: enabled
diff --git a/tests/unit_tests/tools/test_web_fetch_tools.py b/tests/unit_tests/tools/test_web_fetch_tools.py
new file mode 100644
index 0000000000..f15ba7fc6b
--- /dev/null
+++ b/tests/unit_tests/tools/test_web_fetch_tools.py
@@ -0,0 +1,114 @@
+from __future__ import annotations
+
+import importlib.util
+import sys
+import types
+from pathlib import Path
+from unittest.mock import Mock, patch
+
+import pytest
+import requests
+
+
+_REPO_ROOT = Path(__file__).resolve().parents[3]
+_MODULE_PATH = (
+ _REPO_ROOT
+ / "jiuwenswarm"
+ / "agents"
+ / "harness"
+ / "common"
+ / "tools"
+ / "web_fetch_tools.py"
+)
+
+
+def _load_module():
+ if "web_fetch_tools_mod" in sys.modules:
+ return sys.modules["web_fetch_tools_mod"]
+
+ tools_pkg = sys.modules.get("jiuwenswarm.agents.harness.common.tools")
+ if tools_pkg is None:
+ tools_pkg = types.ModuleType("jiuwenswarm.agents.harness.common.tools")
+ tools_pkg.__path__ = [str(_MODULE_PATH.parent)]
+ tools_pkg.__package__ = "jiuwenswarm.agents.harness.common.tools"
+ sys.modules["jiuwenswarm.agents.harness.common.tools"] = tools_pkg
+
+ spec = importlib.util.spec_from_file_location("web_fetch_tools_mod", str(_MODULE_PATH))
+ mod = importlib.util.module_from_spec(spec)
+ sys.modules["web_fetch_tools_mod"] = mod
+ spec.loader.exec_module(mod)
+ return mod
+
+
+_mod = _load_module()
+
+
+def _response(status_code: int, *, url: str = "https://example.com") -> Mock:
+ response = Mock(spec=requests.Response)
+ response.status_code = status_code
+ response.url = url
+ response.headers = {"Content-Type": "text/html; charset=utf-8"}
+ response.content = b"<html><title>Example</title><body>content</body></html>"
+ response.encoding = "utf-8"
+ response.apparent_encoding = "utf-8"
+ if status_code >= 400:
+ response.raise_for_status.side_effect = requests.HTTPError(
+ f"{status_code} response",
+ response=response,
+ )
+ return response
+
+
[email protected]("status_code", [401, 403, 429])
+def test_jina_fallback_is_disabled_by_default(monkeypatch, status_code):
+ monkeypatch.delenv("JIUWENSWARM_ENABLE_JINA_FETCH", raising=False)
+ response = _response(status_code)
+
+ with (
+ patch.object(_mod, "_http_get", return_value=response),
+ patch.object(_mod, "_fetch_via_jina_reader_sync") as jina_fetch,
+ pytest.raises(requests.HTTPError),
+ ):
+ _mod._fetch_webpage_sync("https://example.com", 30)
+
+ jina_fetch.assert_not_called()
+
+
[email protected]("enabled_value", ["1", "true", "yes", "on", "enabled"])
+def test_jina_fallback_can_be_enabled(monkeypatch, enabled_value):
+ monkeypatch.setenv("JIUWENSWARM_ENABLE_JINA_FETCH", enabled_value)
+ response = _response(403)
+ fallback_result = {
+ "url": "https://example.com",
+ "status_code": 200,
+ "title": "",
+ "content": "from jina",
+ }
+
+ with (
+ patch.object(_mod, "_http_get", return_value=response),
+ patch.object(
+ _mod,
+ "_fetch_via_jina_reader_sync",
+ return_value=fallback_result,
+ ) as jina_fetch,
+ ):
+ result = _mod._fetch_webpage_sync("https://example.com", 30)
+
+ assert result == fallback_result
+ jina_fetch.assert_called_once_with("https://example.com", 30)
+
+
+def test_successful_direct_fetch_does_not_use_jina(monkeypatch):
+ monkeypatch.setenv("JIUWENSWARM_ENABLE_JINA_FETCH", "true")
+ response = _response(200)
+
+ with (
+ patch.object(_mod, "_http_get", return_value=response),
+ patch.object(_mod, "_fetch_via_jina_reader_sync") as jina_fetch,
+ ):
+ result = _mod._fetch_webpage_sync("https://example.com", 30)
+
+ assert result["status_code"] == 200
+ assert result["title"] == "Example"
+ jina_fetch.assert_not_called()