Python
Web search settings: one key per provider, test-and-save, fetch switch
Rohit C Prasad6e844c35799a
diff --git a/coworker/server/app.py b/coworker/server/app.pyindex c7f8e29c..c4d4f672 100644--- a/coworker/server/app.py+++ b/coworker/server/app.py@@ -2349,12 +2349,34 @@ def create_app(manager: SessionManager) -> FastAPI:def web_search_get() -> dict[str, Any]:return manager.get_web_search()+ def _web_search_reply(result: dict[str, Any]) -> Any:+ # Settings routes: a refusal is a 400 with a plain message, never a key.+ if result.get("ok"):+ return result+ return JSONResponse({"ok": False, "error": result.get("error", "")}, status_code=400)+@app.post("/v1/web-search")- def web_search_set(body: dict) -> dict[str, Any]:+ def web_search_set(body: dict) -> Any:provider = (body or {}).get("provider", "")if not provider:- return {"ok": False, "error": "provider required"}- return manager.set_web_search(provider, (body or {}).get("api_key"))+ return _web_search_reply({"ok": False, "error": "provider required"})+ return _web_search_reply(manager.set_web_search(provider))++ @app.put("/v1/web-search/keys/{name}")+ def web_search_key_put(name: str, body: dict) -> Any:+ return _web_search_reply(+ manager.set_web_search_key(name, str((body or {}).get("api_key", "")))+ )++ @app.delete("/v1/web-search/keys/{name}")+ def web_search_key_delete(name: str) -> Any:+ return _web_search_reply(manager.clear_web_search_key(name))++ @app.post("/v1/web-search/fetch-via-provider")+ def web_search_fetch_via_provider(body: dict) -> Any:+ return _web_search_reply(+ manager.set_fetch_via_provider(bool((body or {}).get("enabled", False)))+ )# -- model providers (OpenAI, Ollama, …) ------------------------------------@app.get("/v1/providers")diff --git a/coworker/server/manager.py b/coworker/server/manager.pyindex 4846ae56..4eecac05 100644--- a/coworker/server/manager.py+++ b/coworker/server/manager.py@@ -4014,40 +4014,147 @@ class SessionManager:return {"ok": True}# -- web search -------------------------------------------------------------- def get_web_search(self) -> dict[str, Any]:+ # Settings ▸ Customization ▸ Web search. The profile `web_search:default` holds the+ # active provider, one key per provider (`keys`) and the fetch-through-provider switch;+ # `coworker.web.tool.load_profile` reads the pre-Customization `{provider, api_key}`+ # shape too, and the first write here lands the new one.+ def _web_profile(self) -> dict[str, Any]:+ from ..web.tool import PROFILE, load_profile++ raw = self.secrets.get(PROFILE) or {}+ profile = load_profile(self.secrets)+ if raw and ("api_key" in raw or "keys" not in raw):+ self.secrets.put(PROFILE, profile) # one-time migration of the legacy shape+ return profile++ def _save_web_profile(self, profile: dict[str, Any]) -> None:+ from ..web.tool import PROFILE++ self.secrets.put(PROFILE, profile)++ def _active_web_provider(self, profile: dict[str, Any]) -> str:from ..config import load_config- from ..web import provider_names- profile = self.secrets.get("web_search:default") or {}- provider = (- profile.get("provider") or load_config().web_search_provider or "duckduckgo"- )+ return profile.get("provider") or load_config().web_search_provider or "duckduckgo"++ def _forget_search_grants(self) -> None:+ # §1.9: "Always allow searches this session" is consent to a NAMED destination —+ # the card says which provider the queries go to. A new provider is a new+ # destination, so every live session's grant dies with the old one. (Scheduled+ # tasks that name-allow web_search are unaffected: their approver re-allows.)+ for engine in self._engines.values():+ engine.permissions.session_allow_tools.discard("web_search")++ def get_web_search(self) -> dict[str, Any]:+ """The Web search section: the active provider, the fetch switch, and every provider+ with its key state. Never the keys themselves."""+ from ..web import provider_names+ from ..web.providers import provider_class+ from ..web.tool import env_key_var, stored_key++ profile = self._web_profile()+ rows: list[dict[str, Any]] = []+ for name in provider_names(): # registry order; duckduckgo first+ cls = provider_class(name)+ assert cls is not None+ stored = bool(stored_key(profile, name))+ var = None if stored else env_key_var(name)+ rows.append(+ {+ "name": name,+ "display_name": cls.display_name,+ "key_mode": cls.key_mode,+ "key_url": cls.key_url,+ "has_key": stored or bool(var),+ "key_source": "store" if stored else ("env" if var else None),+ "key_env": var or "",+ "supports_fetch": cls.supports_fetch,+ }+ )return {- "provider": provider,- "has_key": bool(profile.get("api_key")),- "providers": provider_names(),+ "provider": self._active_web_provider(profile),+ "fetch_via_provider": bool(profile.get("fetch_via_provider")),+ "providers": rows,}- def set_web_search(- self, provider: str, api_key: Optional[str] = None- ) -> dict[str, Any]:+ def _web_key_at_hand(self, profile: dict[str, Any], name: str) -> bool:+ from ..web.tool import env_key_var, stored_key++ return bool(stored_key(profile, name) or env_key_var(name))++ def set_web_search(self, provider: str) -> dict[str, Any]:+ """Make `provider` the active search service. A service that needs a key cannot be+ chosen until one is stored or in the environment."""from ..web import provider_names+ from ..web.providers import provider_class- if provider not in provider_names():+ cls = provider_class(provider)+ if provider not in provider_names() or cls is None:return {"ok": False, "error": f"unknown provider: {provider}"}- before = self.get_web_search()["provider"]- profile: dict[str, Any] = {"provider": provider}- if api_key:- profile["api_key"] = api_key- self.secrets.put("web_search:default", profile)- # §1.9: "Always allow searches this session" is consent to a NAMED destination —- # the card says which provider the queries go to. A new provider is a new- # destination, so every live session's grant dies with the old one. (Scheduled- # tasks that name-allow web_search are unaffected: their approver re-allows.)+ profile = self._web_profile()+ if cls.key_mode == "required" and not self._web_key_at_hand(profile, provider):+ return {+ "ok": False,+ "error": f"{cls.display_name} needs an API key before it can be chosen. Add one first.",+ }+ before = self._active_web_provider(profile)+ profile["provider"] = provider+ self._save_web_profile(profile)if provider != before:- for engine in self._engines.values():- engine.permissions.session_allow_tools.discard("web_search")- return {"ok": True, "provider": provider}+ self._forget_search_grants()+ return {"ok": True, **self.get_web_search()}++ def set_web_search_key(self, name: str, api_key: str) -> dict[str, Any]:+ """"Test and save": one search with the key, then store it under that provider.+ Other providers' keys are untouched."""+ from ..web.providers import provider_class, verify_key++ cls = provider_class(name)+ if cls is None:+ return {"ok": False, "error": f"unknown provider: {name}"}+ if cls.key_mode == "none":+ return {"ok": False, "error": f"{cls.display_name} does not take a key."}+ api_key = (api_key or "").strip()+ if not api_key:+ return {"ok": False, "error": "api_key required"}+ error = verify_key(name, api_key)+ if error:+ return {"ok": False, "error": error}+ profile = self._web_profile()+ profile.setdefault("keys", {})[name] = api_key+ self._save_web_profile(profile)+ return {"ok": True, **self.get_web_search()}++ def clear_web_search_key(self, name: str) -> dict[str, Any]:+ """Forget a stored key. If that leaves the active provider with no key at all, the+ keyless default takes over rather than every search failing."""+ from ..web.providers import provider_class++ cls = provider_class(name)+ if cls is None:+ return {"ok": False, "error": f"unknown provider: {name}"}+ profile = self._web_profile()+ profile.get("keys", {}).pop(name, None)+ active = self._active_web_provider(profile)+ if (+ active == name+ and cls.key_mode == "required"+ and not self._web_key_at_hand(profile, name)+ ):+ profile["provider"] = "duckduckgo"+ self._save_web_profile(profile)+ self._forget_search_grants()+ else:+ self._save_web_profile(profile)+ return {"ok": True, **self.get_web_search()}++ def set_fetch_via_provider(self, enabled: bool) -> dict[str, Any]:+ """"Fetch pages through the search service" — honoured only while the active+ provider offers it (`coworker.web.tool.fetch_provider`)."""+ profile = self._web_profile()+ profile["fetch_via_provider"] = bool(enabled)+ self._save_web_profile(profile)+ return {"ok": True, **self.get_web_search()}# -- model providers (OpenAI, Ollama, …) ------------------------------------def get_providers(self) -> list[dict[str, Any]]:diff --git a/coworker/web/__init__.py b/coworker/web/__init__.pyindex 5f2e3610..b7133183 100644--- a/coworker/web/__init__.py+++ b/coworker/web/__init__.py@@ -16,7 +16,14 @@ from .providers import (provider_names,)from .fetch import make_web_fetch_tool-from .tool import make_web_search_tool, provider_host, provider_name, resolve_provider+from .tool import (+ fetch_provider,+ fetch_provider_name,+ make_web_search_tool,+ provider_host,+ provider_name,+ resolve_provider,+)__all__ = ["SearchResult",@@ -35,4 +42,6 @@ __all__ = ["provider_name","provider_host","resolve_provider",+ "fetch_provider",+ "fetch_provider_name",]diff --git a/coworker/web/providers.py b/coworker/web/providers.pyindex a0caa44b..a4886ebb 100644--- a/coworker/web/providers.py+++ b/coworker/web/providers.py@@ -40,10 +40,20 @@ class WebSearchProvider(ABC):# Keyless provider that still uses a key when one is configured (better quota/features).# `build_provider` passes the key through instead of dropping it.accepts_key: bool = False+ # Whether the provider can also return a page's text (`fetch_page`), so `web_fetch` can+ # go through it instead of fetching from this machine (Settings ▸ Customization).+ supports_fetch: bool = False+ # Per-call HTTP timeout. Settings' "Test and save" shortens it for its one check.+ timeout: float = _TIMEOUT@abstractmethoddef search(self, query: str, max_results: int = 5) -> list[SearchResult]: ...+ def fetch_page(self, url: str) -> str:+ """The readable text of `url`, fetched by the provider. Only for providers with+ `supports_fetch`; the rest raise."""+ raise NotImplementedError(f"{self.name} does not fetch pages")+class DuckDuckGoProvider(WebSearchProvider):"""Keyless default via the `ddgs` library."""@@ -104,7 +114,7 @@ class YouProvider(WebSearchProvider):self._KEYED_URL if self.api_key else self._KEYLESS_URL,headers=headers,params={"query": query, "count": max_results},- timeout=_TIMEOUT,+ timeout=self.timeout,)resp.raise_for_status()data = resp.json()@@ -127,6 +137,7 @@ class TavilyProvider(WebSearchProvider):key_url = "https://tavily.com"key_mode = "required"requires_key = True+ supports_fetch = Truedef __init__(self, api_key: str) -> None:self.api_key = api_key@@ -137,7 +148,7 @@ class TavilyProvider(WebSearchProvider):resp = httpx.post("https://api.tavily.com/search",json={"api_key": self.api_key, "query": query, "max_results": max_results},- timeout=_TIMEOUT,+ timeout=self.timeout,)resp.raise_for_status()data = resp.json()@@ -150,6 +161,27 @@ class TavilyProvider(WebSearchProvider):for r in data.get("results", [])]+ def fetch_page(self, url: str) -> str:+ # https://docs.tavily.com/documentation/api-reference/endpoint/extract — POST+ # {urls}, bearer auth; the text is results[].raw_content, failures land in+ # failed_results[] even on a 200.+ import httpx++ resp = httpx.post(+ "https://api.tavily.com/extract",+ headers={"Authorization": f"Bearer {self.api_key}"},+ json={"urls": [url]},+ timeout=self.timeout,+ )+ resp.raise_for_status()+ data = resp.json()+ for r in data.get("results") or []:+ if r.get("raw_content"):+ return str(r["raw_content"])+ failed = data.get("failed_results") or []+ detail = failed[0].get("error", "") if failed else "no content returned"+ raise RuntimeError(f"Tavily could not extract the page: {detail}")+class BraveProvider(WebSearchProvider):name = "brave"@@ -171,7 +203,7 @@ class BraveProvider(WebSearchProvider):"Accept": "application/json",},params={"q": query, "count": max_results},- timeout=_TIMEOUT,+ timeout=self.timeout,)resp.raise_for_status()data = resp.json()@@ -209,7 +241,7 @@ class SerperProvider(WebSearchProvider):"https://google.serper.dev/search",headers={"X-API-KEY": self.api_key},json={"q": query, "num": max_results},- timeout=_TIMEOUT,+ timeout=self.timeout,)resp.raise_for_status()data = resp.json()@@ -229,6 +261,7 @@ class ExaProvider(WebSearchProvider):key_url = "https://exa.ai"key_mode = "required"requires_key = True+ supports_fetch = Truedef __init__(self, api_key: str) -> None:self.api_key = api_key@@ -249,7 +282,7 @@ class ExaProvider(WebSearchProvider):"numResults": max_results,"contents": {"highlights": True},},- timeout=_TIMEOUT,+ timeout=self.timeout,)resp.raise_for_status()data = resp.json()@@ -262,6 +295,34 @@ class ExaProvider(WebSearchProvider):for r in data.get("results", [])]+ def fetch_page(self, url: str) -> str:+ # https://exa.ai/docs/reference/get-contents — POST {urls, text: true}; the text is+ # results[].text, and statuses[] says per URL whether the crawl succeeded.+ import httpx++ resp = httpx.post(+ "https://api.exa.ai/contents",+ headers={+ "x-api-key": self.api_key,+ "Content-Type": "application/json",+ "x-exa-integration": "andrewyng/openworker-integration",+ },+ json={"urls": [url], "text": True},+ timeout=self.timeout,+ )+ resp.raise_for_status()+ data = resp.json()+ for r in data.get("results") or []:+ if r.get("text"):+ return str(r["text"])+ detail = "no content returned"+ for st in data.get("statuses") or []:+ if st.get("status") == "error":+ err = st.get("error") or {}+ detail = f"{err.get('tag', 'error')} (HTTP {err.get('httpStatusCode', '?')})"+ break+ raise RuntimeError(f"Exa could not fetch the page: {detail}")+class FirecrawlProvider(WebSearchProvider):"""Firecrawl web search (https://docs.firecrawl.dev) via the v2 REST API."""@@ -271,6 +332,7 @@ class FirecrawlProvider(WebSearchProvider):key_url = "https://firecrawl.dev"key_mode = "required"requires_key = True+ supports_fetch = Truedef __init__(self, api_key: str) -> None:self.api_key = api_key@@ -282,7 +344,7 @@ class FirecrawlProvider(WebSearchProvider):"https://api.firecrawl.dev/v2/search",headers={"Authorization": f"Bearer {self.api_key}"},json={"query": query, "limit": max_results},- timeout=_TIMEOUT,+ timeout=self.timeout,)resp.raise_for_status()data = resp.json()@@ -295,6 +357,27 @@ class FirecrawlProvider(WebSearchProvider):for r in (data.get("data", {}) or {}).get("web", [])]+ def fetch_page(self, url: str) -> str:+ # https://docs.firecrawl.dev/api-reference/endpoint/scrape — POST /v2/scrape+ # {url, formats: ["markdown"]}, bearer auth; the text is data.markdown.+ import httpx++ resp = httpx.post(+ "https://api.firecrawl.dev/v2/scrape",+ headers={"Authorization": f"Bearer {self.api_key}"},+ json={"url": url, "formats": ["markdown"]},+ timeout=self.timeout,+ )+ resp.raise_for_status()+ data = resp.json()+ text = (data.get("data") or {}).get("markdown") if isinstance(data, dict) else None+ if not text:+ detail = data.get("error") if isinstance(data, dict) else None+ raise RuntimeError(+ f"Firecrawl could not scrape the page: {detail or 'no content returned'}"+ )+ return str(text)+_PROVIDERS = {"duckduckgo": DuckDuckGoProvider,@@ -320,3 +403,23 @@ def build_provider(name: str, api_key: Optional[str] = None) -> WebSearchProvidedef provider_names() -> list[str]:return list(_PROVIDERS)+++def provider_class(name: str) -> Optional[type[WebSearchProvider]]:+ return _PROVIDERS.get(name)+++_VERIFY_TIMEOUT = 10.0+++def verify_key(name: str, api_key: str) -> Optional[str]:+ """Run one search with `api_key` ("Test and save"). None when it worked, else the+ provider's error text — with the key scrubbed, should a library echo it."""+ try:+ provider = build_provider(name, api_key)+ provider.timeout = _VERIFY_TIMEOUT+ provider.search("openworker", max_results=1)+ except Exception as exc: # noqa: BLE001 - the text goes to the person as-is+ text = str(exc) or exc.__class__.__name__+ return text.replace(api_key, "•••") if api_key else text+ return Nonediff --git a/coworker/web/tool.py b/coworker/web/tool.pyindex 3ba299c2..dec2ac72 100644--- a/coworker/web/tool.py+++ b/coworker/web/tool.py@@ -1,9 +1,12 @@"""The `web_search` tool + provider resolution.Provider selection (in order): the SecretStore profile `web_search:default` (`{provider,-api_key}`) → the `web_search_provider` config value → the keyless `duckduckgo` default. Keys-resolve `${VAR}` through the SecretStore. The tool is read-only; results are external and must-be treated as untrusted data, not instructions.+keys: {name: key}, fetch_via_provider}`; Settings ▸ Customization writes it) → the+`web_search_provider` config value → the keyless `duckduckgo` default. The active provider's+key is its stored one, else `{NAME}_API_KEY` from the environment. Every provider keeps its+own key, so switching back costs nothing; the pre-Customization shape `{provider, api_key}` is+read as `keys[provider]`. The tool is read-only; results are external and must be treated as+untrusted data, not instructions."""from __future__ import annotations@@ -14,7 +17,9 @@ from typing import Any, Callable, Optionalimport aisuite as aifrom ..secrets import SecretStore-from .providers import WebSearchProvider, build_provider+from .providers import WebSearchProvider, build_provider, provider_class++PROFILE = "web_search:default"_SCHEMA = {"type": "function",@@ -73,28 +78,78 @@ def provider_host(secrets: Optional[SecretStore] = None) -> str:hosts = PROVIDER_HOSTS.get(name, "")if isinstance(hosts, str):return hosts- return hosts[-1] if (profile.get("api_key") or _env_key(name)) else hosts[0]+ return hosts[-1] if (stored_key(profile, name) or _env_key(name)) else hosts[0]def resolve_provider(secrets: Optional[SecretStore] = None, *, default: str = "duckduckgo") -> WebSearchProvider:profile, name = _lookup(secrets, default)- api_key = profile.get("api_key") or _env_key(name)- return build_provider(name, api_key)+ return build_provider(name, stored_key(profile, name) or _env_key(name))+++def fetch_provider(secrets: Optional[SecretStore] = None) -> Optional[WebSearchProvider]:+ """The provider `web_fetch` should read pages through, or None to fetch locally: the+ person turned "Fetch pages through the search service" on, the active provider offers+ it, and its key is at hand. Never raises — a missing key simply means a local fetch."""+ profile, name = _lookup(secrets, "duckduckgo")+ if not profile.get("fetch_via_provider"):+ return None+ cls = provider_class(name)+ if cls is None or not cls.supports_fetch:+ return None+ try:+ return build_provider(name, stored_key(profile, name) or _env_key(name))+ except ValueError:+ return None+++def fetch_provider_name(secrets: Optional[SecretStore] = None) -> str:+ """The NAME of the provider `web_fetch` goes through, `''` when it fetches locally.+ For the web_fetch approval card, resolved when the card is raised."""+ p = fetch_provider(secrets)+ return p.name if p is not None else ""+++def load_profile(secrets: Optional[SecretStore] = None) -> dict:+ """The `web_search:default` profile in today's shape: `{provider, keys: {name: key},+ fetch_via_provider}`. The pre-Customization shape `{provider, api_key}` is read as+ `keys[provider]` (the migration is read-side; the manager writes the new shape back+ the first time it touches the profile)."""+ secrets = secrets or SecretStore()+ raw = secrets.get(PROFILE) or {}+ keys = dict(raw.get("keys") or {})+ legacy = raw.get("api_key")+ if legacy and raw.get("provider") and raw["provider"] not in keys:+ keys[raw["provider"]] = legacy+ profile = {+ "provider": raw.get("provider") or "",+ "keys": {k: v for k, v in keys.items() if v},+ "fetch_via_provider": bool(raw.get("fetch_via_provider")),+ }+ return profile+++def stored_key(profile: dict, name: str) -> Optional[str]:+ return (profile.get("keys") or {}).get(name) or Nonedef _lookup(secrets: Optional[SecretStore], default: str) -> tuple[dict, str]:"""The `web_search:default` profile and the provider name it (or the config) selects."""- secrets = secrets or SecretStore()- profile = secrets.get("web_search:default") or {}+ profile = load_profile(secrets)return profile, profile.get("provider") or _config_provider() or defaultdef _env_key(name: str) -> Optional[str]:+ var = env_key_var(name)+ return os.environ[var] if var else None+++def env_key_var(name: str) -> Optional[str]:+ """The environment variable that carries `name`'s key right now, or None."""for var in (f"{name.upper()}_API_KEY", *_KEY_ENV_ALIASES.get(name, ())):if os.environ.get(var):- return os.environ[var]+ return varreturn Nonediff --git a/tests/test_web_search.py b/tests/test_web_search.pyindex 7210daa3..7e5ac282 100644--- a/tests/test_web_search.py+++ b/tests/test_web_search.py@@ -397,30 +397,188 @@ def test_resolve_provider_from_secretstore(tmp_path):assert p.name == "tavily" and p.api_key == "tvly-123"-def test_web_search_rest(tmp_path, monkeypatch):+def _rest_client(tmp_path, monkeypatch):from fastapi.testclient import TestClientfrom coworker.server.app import create_appfrom coworker.server.manager import SessionManagermonkeypatch.setenv("COWORKER_STATE_DIR", str(tmp_path / "state"))- client = TestClient(create_app(SessionManager(data_dir=tmp_path / "data")))+ for name in provider_names():+ monkeypatch.delenv(f"{name.upper()}_API_KEY", raising=False)+ monkeypatch.delenv("YDC_API_KEY", raising=False)+ manager = SessionManager(data_dir=tmp_path / "data")+ return TestClient(create_app(manager)), manager- assert client.get("/v1/web-search").json()["provider"] == "duckduckgo"- assert (- client.post(- "/v1/web-search", json={"provider": "tavily", "api_key": "sk-secret-xyz"}- ).json()["ok"]- is True- )++def test_web_search_rest_lists_providers_in_registry_order(tmp_path, monkeypatch):+ client, _ = _rest_client(tmp_path, monkeypatch)got = client.get("/v1/web-search").json()- assert got["provider"] == "tavily" and got["has_key"] is True- assert (- "sk-secret-xyz" not in client.get("/v1/web-search").text- ) # key never returned- assert (- client.post("/v1/web-search", json={"provider": "nope"}).json()["ok"] is False- )+ assert got["provider"] == "duckduckgo" and got["fetch_via_provider"] is False+ names = [p["name"] for p in got["providers"]]+ assert names == provider_names() and names[0] == "duckduckgo"+ by = {p["name"]: p for p in got["providers"]}+ assert by["duckduckgo"] == {+ "name": "duckduckgo", "display_name": "DuckDuckGo", "key_mode": "none", "key_url": "",+ "has_key": False, "key_source": None, "key_env": "", "supports_fetch": False,+ }+ assert by["tavily"]["key_mode"] == "required" and by["tavily"]["supports_fetch"] is True+ assert {n for n, p in by.items() if p["supports_fetch"]} == {"exa", "firecrawl", "tavily"}+ assert by["you"]["key_mode"] == "optional" and by["you"]["key_url"]+++def test_web_search_rest_key_from_environment(tmp_path, monkeypatch):+ client, _ = _rest_client(tmp_path, monkeypatch)+ monkeypatch.setenv("BRAVE_API_KEY", "brv-env")+ row = next(p for p in client.get("/v1/web-search").json()["providers"] if p["name"] == "brave")+ assert row["has_key"] and row["key_source"] == "env" and row["key_env"] == "BRAVE_API_KEY"+ assert "brv-env" not in client.get("/v1/web-search").text+ # With a key in the environment the provider can be chosen.+ assert client.post("/v1/web-search", json={"provider": "brave"}).json()["provider"] == "brave"+++def test_web_search_rest_refuses_a_keyless_required_provider(tmp_path, monkeypatch):+ client, _ = _rest_client(tmp_path, monkeypatch)+ resp = client.post("/v1/web-search", json={"provider": "tavily"})+ assert resp.status_code == 400+ assert resp.json()["ok"] is False and "Tavily needs an API key" in resp.json()["error"]+ assert client.get("/v1/web-search").json()["provider"] == "duckduckgo"+ assert client.post("/v1/web-search", json={"provider": "nope"}).status_code == 400+ assert client.post("/v1/web-search", json={}).status_code == 400+ # You.com works keyless, so it can be chosen without a key.+ assert client.post("/v1/web-search", json={"provider": "you"}).json()["provider"] == "you"+++def test_web_search_rest_test_and_save_key(tmp_path, monkeypatch):+ """PUT runs one search with the key before storing it; a failure returns the provider's+ error and stores nothing. Each provider keeps its own key."""+ from coworker.web import providers as providers_mod++ searched = []++ class Verify(TavilyProvider):+ def search(self, query, max_results=5):+ searched.append((self.name, self.api_key, query, max_results, self.timeout))+ if self.api_key == "bad":+ raise RuntimeError("401 Unauthorized for key bad")+ return []++ monkeypatch.setitem(providers_mod._PROVIDERS, "tavily", Verify)+ client, manager = _rest_client(tmp_path, monkeypatch)++ resp = client.put("/v1/web-search/keys/tavily", json={"api_key": "bad"})+ assert resp.status_code == 400+ assert resp.json()["error"] == "401 Unauthorized for key •••" # never the key+ assert client.get("/v1/web-search").json()["providers"][2]["has_key"] is False++ resp = client.put("/v1/web-search/keys/tavily", json={"api_key": "tvly-good"})+ assert resp.status_code == 200 and resp.json()["ok"] is True+ assert searched[-1] == ("tavily", "tvly-good", "openworker", 1, providers_mod._VERIFY_TIMEOUT)+ row = next(p for p in resp.json()["providers"] if p["name"] == "tavily")+ assert row["has_key"] and row["key_source"] == "store"+ assert "tvly-good" not in resp.text+ # The key is stored per provider; the active provider is unchanged until chosen.+ assert manager.secrets.get("web_search:default")["keys"] == {"tavily": "tvly-good"}+ assert resp.json()["provider"] == "duckduckgo"+ assert client.post("/v1/web-search", json={"provider": "tavily"}).json()["provider"] == "tavily"++ # Empty, keyless and unknown providers are refused.+ assert client.put("/v1/web-search/keys/tavily", json={"api_key": " "}).status_code == 400+ assert client.put("/v1/web-search/keys/duckduckgo", json={"api_key": "x"}).status_code == 400+ assert client.put("/v1/web-search/keys/nope", json={"api_key": "x"}).status_code == 400+++def test_web_search_rest_switching_keeps_other_keys(tmp_path, monkeypatch):+ from coworker.web import providers as providers_mod++ class Ok(WebSearchProvider):+ name = "stub"+ display_name = "Stub"+ key_mode = "required"+ requires_key = True++ def __init__(self, api_key):+ self.api_key = api_key++ def search(self, query, max_results=5):+ return []++ monkeypatch.setitem(providers_mod._PROVIDERS, "tavily", type("T", (Ok,), {"name": "tavily"}))+ monkeypatch.setitem(providers_mod._PROVIDERS, "exa", type("E", (Ok,), {"name": "exa"}))+ client, manager = _rest_client(tmp_path, monkeypatch)+ assert client.put("/v1/web-search/keys/tavily", json={"api_key": "t-1"}).json()["ok"]+ assert client.put("/v1/web-search/keys/exa", json={"api_key": "e-1"}).json()["ok"]+ assert client.post("/v1/web-search", json={"provider": "tavily"}).json()["ok"]+ assert client.post("/v1/web-search", json={"provider": "exa"}).json()["ok"]+ assert client.post("/v1/web-search", json={"provider": "tavily"}).json()["ok"]+ assert manager.secrets.get("web_search:default")["keys"] == {"tavily": "t-1", "exa": "e-1"}++ # DELETE forgets one key; the other stays. Removing the active provider's only key+ # falls back to the keyless default instead of leaving every search failing.+ got = client.delete("/v1/web-search/keys/exa").json()+ assert got["ok"] and got["provider"] == "tavily"+ assert manager.secrets.get("web_search:default")["keys"] == {"tavily": "t-1"}+ got = client.delete("/v1/web-search/keys/tavily").json()+ assert got["provider"] == "duckduckgo"+ assert manager.secrets.get("web_search:default")["keys"] == {}+ assert client.delete("/v1/web-search/keys/nope").status_code == 400+++def test_web_search_rest_fetch_via_provider(tmp_path, monkeypatch):+ client, manager = _rest_client(tmp_path, monkeypatch)+ got = client.post("/v1/web-search/fetch-via-provider", json={"enabled": True}).json()+ assert got["ok"] and got["fetch_via_provider"] is True+ assert manager.secrets.get("web_search:default")["fetch_via_provider"] is True+ assert client.get("/v1/web-search").json()["fetch_via_provider"] is True+ got = client.post("/v1/web-search/fetch-via-provider", json={"enabled": False}).json()+ assert got["fetch_via_provider"] is False+++def test_legacy_profile_is_read_as_a_per_provider_key(tmp_path, monkeypatch):+ """The pre-Customization `{provider, api_key}` profile: resolution still finds the key,+ and the first write through the manager lands the new shape."""+ from coworker.web import provider_host, resolve_provider+ from coworker.web.tool import load_profile++ monkeypatch.delenv("TAVILY_API_KEY", raising=False)+ secrets = SecretStore(tmp_path / "secrets.json")+ secrets.put("web_search:default", {"provider": "tavily", "api_key": "tvly-old"})+ assert load_profile(secrets) == {+ "provider": "tavily", "keys": {"tavily": "tvly-old"}, "fetch_via_provider": False,+ }+ assert resolve_provider(secrets).api_key == "tvly-old"+ assert provider_host(secrets) == "api.tavily.com"++ from coworker.server.manager import SessionManager++ monkeypatch.setenv("COWORKER_STATE_DIR", str(tmp_path / "state"))+ manager = SessionManager(data_dir=tmp_path / "data")+ manager.secrets = secrets+ got = manager.get_web_search()+ assert got["provider"] == "tavily"+ assert next(p for p in got["providers"] if p["name"] == "tavily")["key_source"] == "store"+ assert secrets.get("web_search:default") == {+ "provider": "tavily", "keys": {"tavily": "tvly-old"}, "fetch_via_provider": False,+ }+++def test_resolve_provider_uses_the_active_providers_own_key(tmp_path, monkeypatch):+ """Keys are per provider: the active one's stored key, then its environment variable,+ never another provider's key."""+ from coworker.web import resolve_provider++ monkeypatch.delenv("EXA_API_KEY", raising=False)+ monkeypatch.delenv("TAVILY_API_KEY", raising=False)+ secrets = SecretStore(tmp_path / "secrets.json")+ secrets.put("web_search:default", {"provider": "exa", "keys": {"tavily": "t-1", "exa": "e-1"}})+ assert resolve_provider(secrets).api_key == "e-1"+ secrets.put("web_search:default", {"provider": "tavily", "keys": {"tavily": "t-1", "exa": "e-1"}})+ assert resolve_provider(secrets).api_key == "t-1"+ secrets.put("web_search:default", {"provider": "exa", "keys": {"tavily": "t-1"}})+ with pytest.raises(ValueError):+ resolve_provider(secrets) # tavily's key is not exa's+ monkeypatch.setenv("EXA_API_KEY", "e-env")+ assert resolve_provider(secrets).api_key == "e-env"def test_engine_registers_web_search(tmp_path):