Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion agent_core/core/impl/settings/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

Expand Down
1 change: 1 addition & 0 deletions agent_file_system/AGENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
<!-- /schema:settings.json -->

Expand Down
7 changes: 7 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def _get_default_settings() -> Dict[str, Any]:
},
"web_search": {
"google_cse_id": "",
"google_cse_base_url": "",
},
"gui": {
"enabled": True,
Expand Down Expand Up @@ -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."""
Expand Down
3 changes: 2 additions & 1 deletion app/config/settings.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
}
},
"web_search": {
"google_cse_id": ""
"google_cse_id": "",
"google_cse_base_url": ""
},
"browser": {
"port": 7926,
Expand Down
15 changes: 13 additions & 2 deletions app/data/action/web_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", [])

Expand Down
1 change: 1 addition & 0 deletions app/data/agent_file_system_template/AGENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
<!-- /schema:settings.json -->

Expand Down
64 changes: 64 additions & 0 deletions tests/test_web_search_cse_base_url.py
Original file line number Diff line number Diff line change
@@ -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]