Spaces:
Running
Running
sync: GET /api/search + /api/scrape + /api/search/health on the Space
Browse files
src/browser_automation_agent/main.py
CHANGED
|
@@ -20,7 +20,7 @@ from rich.panel import Panel
|
|
| 20 |
from . import voice
|
| 21 |
from .browser.manager import SUPPORTED_CHANNELS, browser_manager
|
| 22 |
from .chat_agent import CHAT_ERROR_HINTS, classify_chat_error, run_chat, usable_chat_messages
|
| 23 |
-
from .chat_tools import ChatBrowserSession, ToolError, check_url, web_search as chat_web_search
|
| 24 |
from .web_search_providers import combined_search, search_configured as search_backends
|
| 25 |
from .client_research import research_client
|
| 26 |
from .config import settings
|
|
@@ -1069,26 +1069,29 @@ async def search_handler(request: web.Request) -> web.Response:
|
|
| 1069 |
operator configuration; EXA_API_KEY remains a quality upgrade and the
|
| 1070 |
keyless DuckDuckGo fallback keeps the endpoint alive when every provider
|
| 1071 |
fails. 502 only when all backends fail.
|
|
|
|
|
|
|
| 1072 |
"""
|
| 1073 |
-
|
| 1074 |
-
|
| 1075 |
-
|
| 1076 |
-
|
| 1077 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1078 |
if not query:
|
| 1079 |
return web.json_response({"error": "query is required"}, status=400)
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
return web.json_response(
|
| 1087 |
-
{"error": "num_results must be an integer"}, status=400
|
| 1088 |
-
)
|
| 1089 |
-
except Exception: # noqa: BLE001 — defensive; num_results validated below
|
| 1090 |
-
num_results = None
|
| 1091 |
-
limit = num_results if num_results is not None else 8
|
| 1092 |
|
| 1093 |
# Preferred chain: Tavily -> Firecrawl (keys ship with the app).
|
| 1094 |
combined = await combined_search(query, limit)
|
|
@@ -1102,7 +1105,7 @@ async def search_handler(request: web.Request) -> web.Response:
|
|
| 1102 |
payload = await webnav.search(
|
| 1103 |
query,
|
| 1104 |
num_results=limit,
|
| 1105 |
-
category=
|
| 1106 |
)
|
| 1107 |
return web.json_response({"success": True, "engine": "exa", **payload})
|
| 1108 |
except ExaNotConfiguredError:
|
|
@@ -1121,6 +1124,33 @@ async def search_handler(request: web.Request) -> web.Response:
|
|
| 1121 |
)
|
| 1122 |
|
| 1123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1124 |
async def search_visit_handler(request: web.Request) -> web.Response:
|
| 1125 |
"""Navigate a session to a search-result URL (guarded like any other)."""
|
| 1126 |
session_id = request.match_info['session_id']
|
|
@@ -1614,6 +1644,9 @@ def create_app() -> web.Application:
|
|
| 1614 |
http_app.router.add_get('/api/browse/{session_id}/map', browse_map_handler)
|
| 1615 |
http_app.router.add_post('/api/browse/{session_id}/action', browse_action_handler)
|
| 1616 |
http_app.router.add_post('/api/search', search_handler)
|
|
|
|
|
|
|
|
|
|
| 1617 |
http_app.router.add_post('/api/search/{session_id}/visit', search_visit_handler)
|
| 1618 |
http_app.router.add_get('/api/ops/metrics', ops_metrics_handler)
|
| 1619 |
|
|
|
|
| 20 |
from . import voice
|
| 21 |
from .browser.manager import SUPPORTED_CHANNELS, browser_manager
|
| 22 |
from .chat_agent import CHAT_ERROR_HINTS, classify_chat_error, run_chat, usable_chat_messages
|
| 23 |
+
from .chat_tools import ChatBrowserSession, ToolError, check_url, web_fetch, web_search as chat_web_search
|
| 24 |
from .web_search_providers import combined_search, search_configured as search_backends
|
| 25 |
from .client_research import research_client
|
| 26 |
from .config import settings
|
|
|
|
| 1069 |
operator configuration; EXA_API_KEY remains a quality upgrade and the
|
| 1070 |
keyless DuckDuckGo fallback keeps the endpoint alive when every provider
|
| 1071 |
fails. 502 only when all backends fail.
|
| 1072 |
+
|
| 1073 |
+
POST {"query": ..., "num_results"?} or GET /api/search?q=...&num_results=.
|
| 1074 |
"""
|
| 1075 |
+
if request.method == "GET":
|
| 1076 |
+
query = (request.query.get("q") or "").strip()
|
| 1077 |
+
category = request.query.get("category") or None
|
| 1078 |
+
raw_limit = request.query.get("num_results") or request.query.get("max_results")
|
| 1079 |
+
else:
|
| 1080 |
+
try:
|
| 1081 |
+
data = await request.json()
|
| 1082 |
+
except json.JSONDecodeError:
|
| 1083 |
+
return web.json_response({"error": "Invalid JSON body"}, status=400)
|
| 1084 |
+
query = (data.get("query") or "").strip()
|
| 1085 |
+
category = data.get("category") or None
|
| 1086 |
+
raw_limit = data.get("num_results") or data.get("numResults") or data.get("max_results")
|
| 1087 |
if not query:
|
| 1088 |
return web.json_response({"error": "query is required"}, status=400)
|
| 1089 |
+
limit = 8
|
| 1090 |
+
if raw_limit is not None:
|
| 1091 |
+
try:
|
| 1092 |
+
limit = int(raw_limit)
|
| 1093 |
+
except (TypeError, ValueError):
|
| 1094 |
+
return web.json_response({"error": "num_results must be an integer"}, status=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1095 |
|
| 1096 |
# Preferred chain: Tavily -> Firecrawl (keys ship with the app).
|
| 1097 |
combined = await combined_search(query, limit)
|
|
|
|
| 1105 |
payload = await webnav.search(
|
| 1106 |
query,
|
| 1107 |
num_results=limit,
|
| 1108 |
+
category=category,
|
| 1109 |
)
|
| 1110 |
return web.json_response({"success": True, "engine": "exa", **payload})
|
| 1111 |
except ExaNotConfiguredError:
|
|
|
|
| 1124 |
)
|
| 1125 |
|
| 1126 |
|
| 1127 |
+
async def scrape_handler(request: web.Request) -> web.Response:
|
| 1128 |
+
"""POST /api/scrape {"url": ...} — read a page via the app-shipped chain.
|
| 1129 |
+
|
| 1130 |
+
Firecrawl renders JS-heavy pages first; the SSRF-guarded direct GET is the
|
| 1131 |
+
fallback. Returns {"ok", url, provider, title, text}.
|
| 1132 |
+
"""
|
| 1133 |
+
try:
|
| 1134 |
+
data = await request.json()
|
| 1135 |
+
except json.JSONDecodeError:
|
| 1136 |
+
return web.json_response({"error": "Invalid JSON body"}, status=400)
|
| 1137 |
+
url = (data.get("url") or "").strip()
|
| 1138 |
+
if not url:
|
| 1139 |
+
return web.json_response({"error": "url is required"}, status=400)
|
| 1140 |
+
try:
|
| 1141 |
+
result = await web_fetch(url, max_chars=12000)
|
| 1142 |
+
except ToolError as exc:
|
| 1143 |
+
return web.json_response({"error": str(exc), "blocked": True}, status=400)
|
| 1144 |
+
except Exception as exc: # noqa: BLE001 — HTTP boundary maps failures to responses
|
| 1145 |
+
return web.json_response({"error": f"Scrape failed: {exc}"}, status=502)
|
| 1146 |
+
return web.json_response({"ok": bool(result.get("text")), **result})
|
| 1147 |
+
|
| 1148 |
+
|
| 1149 |
+
async def search_health_handler(request: web.Request) -> web.Response:
|
| 1150 |
+
"""GET /api/search/health — which search backends are configured."""
|
| 1151 |
+
return web.json_response({"success": True, **search_backends(), "exa": exa_client.configured})
|
| 1152 |
+
|
| 1153 |
+
|
| 1154 |
async def search_visit_handler(request: web.Request) -> web.Response:
|
| 1155 |
"""Navigate a session to a search-result URL (guarded like any other)."""
|
| 1156 |
session_id = request.match_info['session_id']
|
|
|
|
| 1644 |
http_app.router.add_get('/api/browse/{session_id}/map', browse_map_handler)
|
| 1645 |
http_app.router.add_post('/api/browse/{session_id}/action', browse_action_handler)
|
| 1646 |
http_app.router.add_post('/api/search', search_handler)
|
| 1647 |
+
http_app.router.add_get('/api/search', search_handler)
|
| 1648 |
+
http_app.router.add_get('/api/search/health', search_health_handler)
|
| 1649 |
+
http_app.router.add_post('/api/scrape', scrape_handler)
|
| 1650 |
http_app.router.add_post('/api/search/{session_id}/visit', search_visit_handler)
|
| 1651 |
http_app.router.add_get('/api/ops/metrics', ops_metrics_handler)
|
| 1652 |
|