Python
Add Exa web search provider
keskuccb631f84649
diff --git a/coworker/config.py b/coworker/config.pyindex 2801708c..76837f1c 100644--- a/coworker/config.py+++ b/coworker/config.py@@ -89,7 +89,8 @@ class Config:auto_approve_shadow: bool = Falsehost: str = "127.0.0.1"port: int = 8765- # Web search provider: "duckduckgo" (keyless default) | "tavily" | "brave" | "serper" (need a key).+ # Web search provider: "duckduckgo" (keyless default) | "tavily" | "brave" | "serper"+ # | "exa" (the keyed ones need a key).web_search_provider: str = "duckduckgo"# OpenWorker Cloud (sign-in + managed connectors). Config, never constants:# dev/staging/BYO-VPC deployments point these at their own instances.diff --git a/coworker/web/__init__.py b/coworker/web/__init__.pyindex 276e34d7..048df5b1 100644--- a/coworker/web/__init__.py+++ b/coworker/web/__init__.py@@ -5,6 +5,7 @@ from __future__ import annotationsfrom .providers import (BraveProvider,DuckDuckGoProvider,+ ExaProvider,SearchResult,SerperProvider,TavilyProvider,@@ -22,6 +23,7 @@ __all__ = ["TavilyProvider","BraveProvider","SerperProvider",+ "ExaProvider","build_provider","provider_names","make_web_search_tool",diff --git a/coworker/web/providers.py b/coworker/web/providers.pyindex d8af91c0..c526433a 100644--- a/coworker/web/providers.py+++ b/coworker/web/providers.py@@ -1,8 +1,8 @@"""Web search providers — a keyless default + pluggable third-party services.-`duckduckgo` works with no API key (our "starting version of our own"). `tavily`, `brave` and-`serper` give better results but need a key (configured via the SecretStore / env). All-providers return a uniform `list[SearchResult]`; the heavy client libs are lazy-imported.+`duckduckgo` works with no API key (our "starting version of our own"). `tavily`, `brave`,+`serper` and `exa` give better results but need a key (configured via the SecretStore / env).+All providers return a uniform `list[SearchResult]`; the heavy client libs are lazy-imported."""from __future__ import annotations@@ -156,11 +156,48 @@ class SerperProvider(WebSearchProvider):]+class ExaProvider(WebSearchProvider):+ name = "exa"+ requires_key = True++ def __init__(self, api_key: str) -> None:+ self.api_key = api_key++ def search(self, query: str, max_results: int = 5) -> list[SearchResult]:+ import httpx++ resp = httpx.post(+ "https://api.exa.ai/search",+ headers={+ "x-api-key": self.api_key,+ "Content-Type": "application/json",+ "x-exa-integration": "andrewyng/openworker-integration",+ },+ json={+ "query": query,+ "type": "auto",+ "numResults": max_results,+ "contents": {"highlights": True},+ },+ timeout=_TIMEOUT,+ )+ data = resp.json()+ return [+ SearchResult(+ title=r.get("title") or "",+ url=r.get("url") or "",+ snippet=" ".join(r.get("highlights") or []),+ )+ for r in data.get("results", [])+ ]++_PROVIDERS = {"duckduckgo": DuckDuckGoProvider,"tavily": TavilyProvider,"brave": BraveProvider,"serper": SerperProvider,+ "exa": ExaProvider,}diff --git a/tests/test_web_search.py b/tests/test_web_search.pyindex 2cd3e27c..c0d7e8fd 100644--- a/tests/test_web_search.py+++ b/tests/test_web_search.py@@ -1,7 +1,7 @@"""Tests for web search — provider abstraction, the tool, and config resolution.No network: a FakeProvider is injected; third-party key handling and the REST config path-are exercised without hitting DuckDuckGo/Tavily/Brave/Serper.+are exercised without hitting DuckDuckGo/Tavily/Brave/Serper/Exa."""from __future__ import annotations@@ -20,6 +20,7 @@ from coworker.web.providers import (_PROVIDERS,BraveProvider,DuckDuckGoProvider,+ ExaProvider,SerperProvider,TavilyProvider,WebSearchProvider,@@ -87,6 +88,9 @@ def test_build_provider_third_party_requires_key():assert isinstance(build_provider("brave", "brv-x"), BraveProvider)assert isinstance(build_provider("serper", "srp-x"), SerperProvider)assert "serper" in provider_names()+ with pytest.raises(ValueError):+ build_provider("exa") # no key+ assert isinstance(build_provider("exa", "exa-x"), ExaProvider)def test_serper_sends_correct_request_and_maps_organic_results(monkeypatch):@@ -130,6 +134,45 @@ def test_serper_sends_correct_request_and_maps_organic_results(monkeypatch):assert results[0].snippet == "An example snippet."+def test_exa_provider_parses_highlights(monkeypatch):+ import httpx++ captured = {}++ def fake_post(url, headers=None, json=None, timeout=None):+ captured.update(url=url, headers=headers, json=json)+ return httpx.Response(+ 200,+ json={+ "results": [+ {+ "title": "Exa",+ "url": "https://exa.ai",+ "highlights": ["neural search", "for AI"],+ },+ {"title": None, "url": "https://exa.ai/docs"},+ ]+ },+ )++ monkeypatch.setattr(httpx, "post", fake_post)+ results = ExaProvider("exa-x").search("what is exa", max_results=2)++ assert captured["url"] == "https://api.exa.ai/search"+ assert captured["headers"]["x-api-key"] == "exa-x"+ assert captured["headers"]["x-exa-integration"] == "andrewyng/openworker-integration"+ assert captured["json"]["numResults"] == 2+ assert captured["json"]["contents"] == {"highlights": True}+ assert [r.to_dict() for r in results] == [+ {+ "title": "Exa",+ "url": "https://exa.ai",+ "snippet": "neural search for AI",+ },+ {"title": "", "url": "https://exa.ai/docs", "snippet": ""},+ ]++def test_keyed_providers_raise_on_http_errors(monkeypatch):"""A 401/429/5xx surfaces as an error instead of an empty result list."""import httpx