Spaces:
Sleeping
Sleeping
| from __future__ import annotations | |
| import json | |
| import os | |
| from pathlib import Path | |
| from src.schemas import SourceCandidate | |
| def fixture_sources(fixtures_path: str | Path = "data/fixtures/demo_sources.json") -> list[SourceCandidate]: | |
| data = json.loads(Path(fixtures_path).read_text(encoding="utf-8")) | |
| return [SourceCandidate.from_dict(item) for item in data] | |
| def search_web(query: str, limit: int = 5) -> list[SourceCandidate]: | |
| mock_mode = os.getenv("MOCK_MODE", "false").lower() == "true" | |
| if mock_mode: | |
| return fixture_sources()[:limit] | |
| if os.getenv("DIRECT_SEARCH_ENABLED", "true").lower() != "true": | |
| return [] | |
| try: | |
| from duckduckgo_search import DDGS | |
| rows = DDGS().text(query, max_results=limit) | |
| sources = [ | |
| SourceCandidate( | |
| title=row.get("title") or "Untitled", | |
| url=row.get("href") or "", | |
| snippet=row.get("body") or "", | |
| source_type="web", | |
| fetch_status="search_only", | |
| ) | |
| for row in rows | |
| ] | |
| return sources | |
| except Exception: | |
| return [] | |