1- """Search provider implementations: SearXNG, Brave, DuckDuckGo, Google PSE, Tavily, Serper ."""
1+ """Search provider implementations, including local Firecrawl and SearXNG ."""
22
33import json
44import logging
1717
1818# Provider registry — maps setting value to (label, needs_key, needs_url)
1919PROVIDER_INFO = {
20+ "firecrawl" : ("Firecrawl (self-hosted)" , False , True ),
2021 "searxng" : ("SearXNG" , False , True ),
2122 "brave" : ("Brave Search" , True , False ),
2223 "duckduckgo" : ("DuckDuckGo" , False , False ),
@@ -47,10 +48,22 @@ def _get_search_instance() -> str:
4748 return SEARXNG_INSTANCE
4849
4950
51+ def _get_firecrawl_instance () -> str :
52+ """Return the self-hosted Firecrawl API URL without a hosted fallback."""
53+ settings = _get_search_settings ()
54+ url = (
55+ settings .get ("firecrawl_url" )
56+ or os .environ .get ("FIRECRAWL_API_URL" )
57+ or "http://localhost:3002"
58+ )
59+ return str (url ).strip ().rstrip ("/" )
60+
61+
5062def _get_provider_key (provider : str ) -> str :
5163 """Return the API key for a specific provider, with legacy fallback."""
5264 settings = _get_search_settings ()
5365 key_map = {
66+ "firecrawl" : "firecrawl_api_key" ,
5467 "brave" : "brave_api_key" ,
5568 "google_pse" : "google_pse_key" ,
5669 "tavily" : "tavily_api_key" ,
@@ -66,6 +79,7 @@ def _get_provider_key(provider: str) -> str:
6679 if legacy :
6780 return legacy
6881 env_map = {
82+ "firecrawl" : "FIRECRAWL_API_KEY" ,
6983 "brave" : "DATA_BRAVE_API_KEY" ,
7084 "google_pse" : "GOOGLE_API_KEY" ,
7185 "tavily" : "TAVILY_API_KEY" ,
@@ -121,6 +135,72 @@ def _safesearch_for(provider: str) -> Optional[str]:
121135 return None
122136
123137
138+ # ── Firecrawl (self-hosted) ──
139+
140+ def firecrawl_search (
141+ query : str ,
142+ count : Optional [int ] = None ,
143+ time_filter : Optional [str ] = None ,
144+ ) -> List [dict ]:
145+ """Search through the local Firecrawl v2 API.
146+
147+ Firecrawl is treated as a local search appliance: the URL defaults to the
148+ loopback service and never falls back to Firecrawl's hosted API. Returning
149+ an empty list on failure lets Diogenes continue through its configured
150+ provider chain (normally the independently managed SearXNG instance).
151+ """
152+ count = count if count is not None else _get_result_count ()
153+ instance = _get_firecrawl_instance ()
154+ if not instance :
155+ logger .warning ("Firecrawl search skipped: no local endpoint configured" )
156+ return []
157+
158+ payload = {
159+ "query" : query ,
160+ "limit" : count ,
161+ "sources" : [{"type" : "web" }],
162+ }
163+ tbs_map = {
164+ "day" : "qdr:d" ,
165+ "week" : "qdr:w" ,
166+ "month" : "qdr:m" ,
167+ "year" : "qdr:y" ,
168+ }
169+ if time_filter in tbs_map :
170+ payload ["tbs" ] = tbs_map [time_filter ]
171+
172+ headers = {"User-Agent" : WEB_FETCH_USER_AGENT }
173+ api_key = _get_provider_key ("firecrawl" )
174+ if api_key :
175+ headers ["Authorization" ] = f"Bearer { api_key } "
176+
177+ try :
178+ response = httpx .post (
179+ f"{ instance } /v2/search" ,
180+ json = payload ,
181+ headers = headers ,
182+ timeout = REQUEST_TIMEOUT ,
183+ )
184+ response .raise_for_status ()
185+ data = response .json ()
186+ web = data .get ("data" , {}).get ("web" , []) if isinstance (data , dict ) else []
187+ results = []
188+ for item in web [:count ]:
189+ if not isinstance (item , dict ) or not item .get ("url" ):
190+ continue
191+ snippet = item .get ("description" ) or item .get ("markdown" ) or ""
192+ results .append ({
193+ "title" : item .get ("title" ) or item ["url" ],
194+ "url" : item ["url" ],
195+ "snippet" : str (snippet )[:2000 ],
196+ })
197+ logger .info ("Firecrawl returned %d results for: %s" , len (results ), query )
198+ return results
199+ except Exception as exc :
200+ logger .warning ("Local Firecrawl search failed: %s" , exc )
201+ return []
202+
203+
124204# ── SearXNG ──
125205
126206_NEWS_HINTS = ("news" , "nyheter" , "headlines" , "breaking" , "latest" , "today" , "idag" )
0 commit comments