feat: make SearXNG URL env-configurable and support basic auth

Squash merge of SearXNG URL env configuration and basic auth support.
This commit was merged in pull request #7.
This commit is contained in:
2026-06-30 22:48:22 +02:00
parent fd85758e3c
commit 359a8425d5
3 changed files with 59 additions and 55 deletions
+11
View File
@@ -13,6 +13,9 @@ class AgentSettings(BaseSettings):
gitea_repos_root: str = ""
agent_model_id: str = "qwen/qwen3.6-35b-a3b"
agent_max_retries: int = 2
searxng_url: str = ""
searxng_username: str = ""
searxng_password: str = ""
def get_settings() -> AgentSettings:
@@ -28,7 +31,15 @@ GITEA_TOKEN: str = _agent_settings.gitea_token
GITEA_REPOS_ROOT: str = _agent_settings.gitea_repos_root
AGENT_MODEL_ID: str = _agent_settings.agent_model_id
AGENT_MAX_RETRIES: int = _agent_settings.agent_max_retries
SEARXNG_URL: str = _agent_settings.searxng_url
SEARXNG_USERNAME: str = _agent_settings.searxng_username
SEARXNG_PASSWORD: str = _agent_settings.searxng_password
import os
os.environ["GITEA_SERVER_URL"] = GITEA_URL
os.environ["GITEA_SERVER_TOKEN"] = GITEA_TOKEN
os.environ["SEARXNG_URL"] = SEARXNG_URL
os.environ["SEARXNG_USERNAME"] = SEARXNG_USERNAME
os.environ["SEARXNG_PASSWORD"] = SEARXNG_PASSWORD
+21 -21
View File
@@ -20,6 +20,13 @@ import time
from typing import Any
import httpx
from duckduckgo_search import DDGS # type: ignore[import-untyped]
from duckduckgo_search.exceptions import ( # type: ignore[import-untyped]
DuckDuckGoSearchException,
RatelimitException,
)
from gitea.config import SEARXNG_URL, SEARXNG_USERNAME, SEARXNG_PASSWORD
logger: logging.Logger = logging.getLogger("research-tools")
@@ -29,10 +36,12 @@ _USER_AGENT: str = (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
)
# SearXNG instance — override with SEARXNG_URL env var
_SEARXNG_URL: str = os.getenv(
"SEARXNG_URL", "https://searxng.meeks.freeddns.org"
)
# SearXNG instance
_SEARXNG_URL: str = SEARXNG_URL
_SEARXNG_USERNAME: str = SEARXNG_USERNAME
_SEARXNG_PASSWORD: str = SEARXNG_PASSWORD
class ResearchTools:
@@ -178,6 +187,9 @@ class ResearchTools:
Returns formatted results string on success, or None if the instance
is unreachable so the caller can fall through to the next backend.
"""
if not _SEARXNG_URL:
logger.info("SearXNG URL is not configured; skipping SearXNG search.")
return None
params: dict[str, Any] = {
"q": query,
"format": "json",
@@ -187,9 +199,13 @@ class ResearchTools:
if time_range:
params["time_range"] = time_range
auth = None
if _SEARXNG_USERNAME and _SEARXNG_PASSWORD:
auth = (_SEARXNG_USERNAME, _SEARXNG_PASSWORD)
try:
with httpx.Client(
timeout=_DEFAULT_TIMEOUT, follow_redirects=True
timeout=_DEFAULT_TIMEOUT, follow_redirects=True, auth=auth
) as client:
response = client.get(
f"{_SEARXNG_URL}/search",
@@ -307,22 +323,6 @@ class ResearchTools:
logger.info("SearXNG unavailable; falling back to DuckDuckGo")
# --- Tier 2: DDGS library (handles sessions, cookies, rate limits) ---
try:
from duckduckgo_search import DDGS # type: ignore[import-untyped]
except ImportError:
logger.debug("duckduckgo-search not installed; using httpx fallback")
return self._web_search_fallback(query, num_results)
# Import exception types (package versions differ on exact names)
try:
from duckduckgo_search.exceptions import ( # type: ignore[import-untyped]
RatelimitException,
DuckDuckGoSearchException,
)
except ImportError:
RatelimitException = Exception # type: ignore[assignment,misc]
DuckDuckGoSearchException = Exception # type: ignore[assignment,misc]
last_exc: Exception | None = None
for attempt in range(3):
try: