diff --git a/agent_core/core/impl/settings/manager.py b/agent_core/core/impl/settings/manager.py index adc24202b..8f3365e9a 100644 --- a/agent_core/core/impl/settings/manager.py +++ b/agent_core/core/impl/settings/manager.py @@ -57,7 +57,7 @@ "notion": {"client_id": "", "client_secret": ""}, "outlook": {"client_id": ""}, }, - "web_search": {"google_cse_id": ""}, + "web_search": {"google_cse_id": "", "google_cse_base_url": ""}, "browser": {"port": 7926, "startup_ui": False}, } diff --git a/agent_file_system/AGENT.md b/agent_file_system/AGENT.md index aa9f8339b..060271916 100644 --- a/agent_file_system/AGENT.md +++ b/agent_file_system/AGENT.md @@ -1995,6 +1995,7 @@ endpoints: web_search: google_cse_id: string (Google Custom Search Engine ID for web_search action) + google_cse_base_url: string (override for the Custom Search API root URL; empty uses Google's) ``` diff --git a/app/config.py b/app/config.py index 6d9ee8d1c..399a93d46 100644 --- a/app/config.py +++ b/app/config.py @@ -127,6 +127,7 @@ def _get_default_settings() -> Dict[str, Any]: }, "web_search": { "google_cse_id": "", + "google_cse_base_url": "", }, "gui": { "enabled": True, @@ -518,6 +519,12 @@ def get_web_search_cse_id() -> str: return settings.get("web_search", {}).get("google_cse_id", "") +def get_web_search_cse_base_url() -> str: + """Get Google Custom Search API root URL override (empty for Google's).""" + settings = get_settings() + return settings.get("web_search", {}).get("google_cse_base_url", "") + + def is_prewarm_all_drives_enabled() -> bool: """Whether to pre-warm the find_files index for every local drive at startup. Off by default: then only the agent workspace is pre-warmed.""" diff --git a/app/config/settings.default.json b/app/config/settings.default.json index 753ebddfe..3af37c0d5 100644 --- a/app/config/settings.default.json +++ b/app/config/settings.default.json @@ -74,7 +74,8 @@ } }, "web_search": { - "google_cse_id": "" + "google_cse_id": "", + "google_cse_base_url": "" }, "browser": { "port": 7926, diff --git a/app/data/action/web_search.py b/app/data/action/web_search.py index 71306cbd7..d2e98b1c5 100644 --- a/app/data/action/web_search.py +++ b/app/data/action/web_search.py @@ -156,14 +156,25 @@ def google_cse_search(q, n=5): """Search using Google Custom Search API.""" try: from googleapiclient.discovery import build - from app.config import get_api_key, get_web_search_cse_id + from app.config import ( + get_api_key, + get_web_search_cse_base_url, + get_web_search_cse_id, + ) api_key = get_api_key("google") cse_id = get_web_search_cse_id() if not api_key or not cse_id: raise Exception("No Google API credentials") - service = build("customsearch", "v1", developerKey=api_key) + base_url = get_web_search_cse_base_url() + client_options = {"api_endpoint": base_url} if base_url else None + service = build( + "customsearch", + "v1", + developerKey=api_key, + client_options=client_options, + ) res = service.cse().list(q=q, cx=cse_id, num=min(n + 5, 10)).execute() items = res.get("items", []) diff --git a/app/data/agent_file_system_template/AGENT.md b/app/data/agent_file_system_template/AGENT.md index f64a8ef60..2a8d37cf7 100644 --- a/app/data/agent_file_system_template/AGENT.md +++ b/app/data/agent_file_system_template/AGENT.md @@ -1826,6 +1826,7 @@ endpoints: web_search: google_cse_id: string (Google Custom Search Engine ID for web_search action) + google_cse_base_url: string (override for the Custom Search API root URL; empty uses Google's) ``` diff --git a/tests/test_web_search_cse_base_url.py b/tests/test_web_search_cse_base_url.py new file mode 100644 index 000000000..3c3e72739 --- /dev/null +++ b/tests/test_web_search_cse_base_url.py @@ -0,0 +1,64 @@ +"""web_search sends Google Custom Search requests to web_search.google_cse_base_url when set.""" + +import json +from unittest.mock import patch + +import googleapiclient.discovery +import httplib2 + +import app.config as app_config +from app.data.action.web_search import web_search + +_ITEMS = { + "items": [ + {"title": "Result", "link": "https://example.com/r", "snippet": "Snippet"} + ] +} + + +class _RecordingHttp: + """Stands in for httplib2.Http and records each request URI.""" + + def __init__(self): + self.uris = [] + + def request(self, uri, method="GET", body=None, headers=None, **kwargs): + self.uris.append(uri) + return httplib2.Response({"status": "200"}), json.dumps(_ITEMS).encode() + + +def _run(base_url): + settings = { + "api_keys": {"google": "test-key"}, + "web_search": {"google_cse_id": "test-cx", "google_cse_base_url": base_url}, + } + http = _RecordingHttp() + real_build = googleapiclient.discovery.build + + def _build(*args, **kwargs): + return real_build(*args, http=http, **kwargs) + + with ( + patch.object(app_config, "get_settings", return_value=settings), + patch.object(googleapiclient.discovery, "build", _build), + ): + result = web_search({"query": "craftbot", "num_results": 1}) + return result, http.uris + + +def test_default_uses_google_endpoint(): + result, uris = _run("") + assert result["status"] == "success" + assert result["results"][0]["url"] == "https://example.com/r" + assert len(uris) == 1 + assert uris[0].startswith("https://customsearch.googleapis.com/customsearch/v1?") + assert "cx=test-cx" in uris[0] and "key=test-key" in uris[0] + + +def test_base_url_setting_moves_request(): + result, uris = _run("https://cse.example.com") + assert result["status"] == "success" + assert result["results"][0]["url"] == "https://example.com/r" + assert len(uris) == 1 + assert uris[0].startswith("https://cse.example.com/customsearch/v1?") + assert "cx=test-cx" in uris[0] and "key=test-key" in uris[0]