Fix Brave search auth and prompt variables

This commit is contained in:
2026-05-19 21:33:46 +02:00
parent 68934b23a0
commit 5ed1104c12
8 changed files with 187 additions and 19 deletions

View File

@@ -179,11 +179,18 @@ def _parse_mapping(value: Any, *, field_name: str) -> dict[str, Any]:
return value
def _normalize_brave_api_key(value: Any) -> str | None:
api_key = str(value or "").strip()
if api_key.lower().startswith("bearer "):
api_key = api_key[7:].strip()
return api_key or None
def _parse_web_search_config(raw_ai: dict[str, Any]) -> AIWebSearchConfig:
raw = _parse_mapping(raw_ai.get("web_search"), field_name="ai.web_search")
enabled = _parse_bool(raw.get("enabled", True), field_name="ai.web_search.enabled")
api_key = str(raw.get("api_key") or "").strip() or None
api_key = _normalize_brave_api_key(raw.get("api_key"))
endpoint = str(
raw.get("endpoint") or "https://api.search.brave.com/res/v1/web/search"
).strip()
@@ -209,9 +216,12 @@ def _parse_web_search_config(raw_ai: dict[str, Any]) -> AIWebSearchConfig:
field_name="ai.web_search.extra_snippets",
)
env_search_api_key = os.environ.get("BRAVE_SEARCH_API_KEY")
if env_search_api_key is not None:
api_key = _normalize_brave_api_key(env_search_api_key)
env_api_key = os.environ.get("BRAVE_API_KEY")
if env_api_key is not None:
api_key = env_api_key.strip() or None
api_key = _normalize_brave_api_key(env_api_key)
if os.environ.get("BRAVE_SEARCH_COUNT") is not None:
count = min(
max(_parse_int(os.environ["BRAVE_SEARCH_COUNT"], field_name="BRAVE_SEARCH_COUNT"), 1),