-
Notifications
You must be signed in to change notification settings - Fork 164
refactor(routes): move search domain into routes/search/ subpackage #5779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RaresKeY
merged 1 commit into
odysseus-dev:dev
from
ydonghao:refactor/routes-search-to-subdir
Jul 28, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """Search route domain package (slice 2j, #4082/#4071). | ||
| Contains search_routes.py, migrated from the flat routes/ directory. | ||
| Backward-compat shim at routes/search_routes.py re-exports from here. | ||
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| """Search routes — /api/search/config GET, /api/search POST.""" | ||
|
|
||
| import logging | ||
| from typing import Dict, Any | ||
|
|
||
| from fastapi import APIRouter, Request | ||
|
|
||
| import time | ||
|
|
||
| from services.search import get_search_config, comprehensive_web_search, PROVIDER_INFO | ||
| from services.search.core import _call_provider | ||
| from services.search.providers import _get_provider_key, _get_search_instance | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def _request_values(request: Request) -> Dict[str, Any]: | ||
| """Accept JSON, form data, or query params for search endpoints. | ||
|
|
||
| The browser UI posts FormData, while the agent's generic app_api tool | ||
| posts JSON. FastAPI Form(...) rejects JSON with a 422 before our handler | ||
| runs, which made the model think SearXNG was broken. | ||
| """ | ||
| values: Dict[str, Any] = dict(request.query_params) | ||
| content_type = (request.headers.get("content-type") or "").lower() | ||
| try: | ||
| if "application/json" in content_type: | ||
| body = await request.json() | ||
| if isinstance(body, dict): | ||
| values.update(body) | ||
| else: | ||
| form = await request.form() | ||
| values.update(dict(form)) | ||
| except Exception: | ||
| pass | ||
| return values | ||
|
|
||
|
|
||
| def setup_search_routes(config) -> APIRouter: | ||
| router = APIRouter(tags=["search"]) | ||
|
|
||
| @router.get("/api/search/config") | ||
| async def get_search_settings() -> Dict[str, Any]: | ||
| return get_search_config() | ||
|
|
||
| @router.post("/api/search") | ||
| async def do_web_search(request: Request) -> Dict[str, Any]: | ||
| """Standalone web search — returns context string + source list. | ||
|
|
||
| Used by Compare mode to pre-search once and share results across panes. | ||
| """ | ||
| values = await _request_values(request) | ||
| query = str(values.get("query") or values.get("q") or "").strip() | ||
| if not query: | ||
| return {"context": "", "sources": [], "error": "query is required"} | ||
| time_filter = values.get("time_filter") or values.get("freshness") | ||
| if time_filter is not None: | ||
| time_filter = str(time_filter).strip() or None | ||
| try: | ||
| context, sources = comprehensive_web_search( | ||
| query, return_sources=True, time_filter=time_filter, | ||
| ) | ||
| return {"context": context, "sources": sources} | ||
| except Exception as e: | ||
| logger.error(f"Standalone web search failed: {e}") | ||
| return {"context": "", "sources": [], "error": str(e)} | ||
Check warningCode scanning / CodeQL Information exposure through an exception Medium Stack trace information Error loading related location Loading |
||
|
|
||
| @router.get("/api/search/providers") | ||
| async def list_search_providers(): | ||
| """Return available search providers with config status.""" | ||
| providers = [] | ||
| for pid, (label, needs_key, needs_url) in PROVIDER_INFO.items(): | ||
| if pid == "disabled": | ||
| continue | ||
| available = True | ||
| if needs_key and not _get_provider_key(pid): | ||
| available = False | ||
| if needs_url and pid == "searxng" and not _get_search_instance(): | ||
| available = False | ||
| providers.append({ | ||
| "id": pid, | ||
| "label": label, | ||
| "available": available, | ||
| }) | ||
| return providers | ||
|
|
||
| @router.post("/api/search/query") | ||
| async def search_with_provider(request: Request) -> Dict[str, Any]: | ||
| """Search using a specific provider. Used by compare search mode.""" | ||
| values = await _request_values(request) | ||
| query = str(values.get("query") or values.get("q") or "").strip() | ||
| provider = str(values.get("provider") or "").strip() | ||
| try: | ||
| count = int(values.get("count") or values.get("limit") or 10) | ||
| except Exception: | ||
| count = 10 | ||
| if not query: | ||
| return {"results": [], "provider": provider, "error": "query is required"} | ||
| if provider not in PROVIDER_INFO or provider == "disabled": | ||
| return {"results": [], "provider": provider, "error": "Unknown provider"} | ||
| t0 = time.time() | ||
| try: | ||
| results = _call_provider(provider, query, min(count, 20)) | ||
| elapsed = round(time.time() - t0, 2) | ||
| return {"results": results, "provider": provider, "time": elapsed} | ||
| except Exception as e: | ||
| elapsed = round(time.time() - t0, 2) | ||
| logger.error(f"Search provider {provider} failed: {e}") | ||
| return {"results": [], "provider": provider, "time": elapsed, "error": str(e)} | ||
Check warningCode scanning / CodeQL Information exposure through an exception Medium Stack trace information Error loading related location Loading |
||
|
RaresKeY marked this conversation as resolved.
Dismissed
|
||
|
|
||
| return router | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,111 +1,13 @@ | ||
| """Search routes — /api/search/config GET, /api/search POST.""" | ||
| """Backward-compat shim — canonical location is routes/search/search_routes.py. | ||
|
|
||
| import logging | ||
| from typing import Dict, Any | ||
| This module is replaced in ``sys.modules`` by the canonical module object so | ||
| that ``import routes.search_routes`` and ``from routes.search_routes import X`` | ||
| keep resolving to the canonical module. Keeps existing import paths working | ||
| after slice 2j (#4082/#4071). | ||
| """ | ||
|
|
||
| from fastapi import APIRouter, Request | ||
| import sys as _sys | ||
|
|
||
| import time | ||
| from routes.search import search_routes as _canonical # noqa: F401 | ||
|
|
||
| from services.search import get_search_config, comprehensive_web_search, PROVIDER_INFO | ||
| from services.search.core import _call_provider | ||
| from services.search.providers import _get_provider_key, _get_search_instance | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def _request_values(request: Request) -> Dict[str, Any]: | ||
| """Accept JSON, form data, or query params for search endpoints. | ||
|
|
||
| The browser UI posts FormData, while the agent's generic app_api tool | ||
| posts JSON. FastAPI Form(...) rejects JSON with a 422 before our handler | ||
| runs, which made the model think SearXNG was broken. | ||
| """ | ||
| values: Dict[str, Any] = dict(request.query_params) | ||
| content_type = (request.headers.get("content-type") or "").lower() | ||
| try: | ||
| if "application/json" in content_type: | ||
| body = await request.json() | ||
| if isinstance(body, dict): | ||
| values.update(body) | ||
| else: | ||
| form = await request.form() | ||
| values.update(dict(form)) | ||
| except Exception: | ||
| pass | ||
| return values | ||
|
|
||
|
|
||
| def setup_search_routes(config) -> APIRouter: | ||
| router = APIRouter(tags=["search"]) | ||
|
|
||
| @router.get("/api/search/config") | ||
| async def get_search_settings() -> Dict[str, Any]: | ||
| return get_search_config() | ||
|
|
||
| @router.post("/api/search") | ||
| async def do_web_search(request: Request) -> Dict[str, Any]: | ||
| """Standalone web search — returns context string + source list. | ||
|
|
||
| Used by Compare mode to pre-search once and share results across panes. | ||
| """ | ||
| values = await _request_values(request) | ||
| query = str(values.get("query") or values.get("q") or "").strip() | ||
| if not query: | ||
| return {"context": "", "sources": [], "error": "query is required"} | ||
| time_filter = values.get("time_filter") or values.get("freshness") | ||
| if time_filter is not None: | ||
| time_filter = str(time_filter).strip() or None | ||
| try: | ||
| context, sources = comprehensive_web_search( | ||
| query, return_sources=True, time_filter=time_filter, | ||
| ) | ||
| return {"context": context, "sources": sources} | ||
| except Exception as e: | ||
| logger.error(f"Standalone web search failed: {e}") | ||
| return {"context": "", "sources": [], "error": str(e)} | ||
|
|
||
| @router.get("/api/search/providers") | ||
| async def list_search_providers(): | ||
| """Return available search providers with config status.""" | ||
| providers = [] | ||
| for pid, (label, needs_key, needs_url) in PROVIDER_INFO.items(): | ||
| if pid == "disabled": | ||
| continue | ||
| available = True | ||
| if needs_key and not _get_provider_key(pid): | ||
| available = False | ||
| if needs_url and pid == "searxng" and not _get_search_instance(): | ||
| available = False | ||
| providers.append({ | ||
| "id": pid, | ||
| "label": label, | ||
| "available": available, | ||
| }) | ||
| return providers | ||
|
|
||
| @router.post("/api/search/query") | ||
| async def search_with_provider(request: Request) -> Dict[str, Any]: | ||
| """Search using a specific provider. Used by compare search mode.""" | ||
| values = await _request_values(request) | ||
| query = str(values.get("query") or values.get("q") or "").strip() | ||
| provider = str(values.get("provider") or "").strip() | ||
| try: | ||
| count = int(values.get("count") or values.get("limit") or 10) | ||
| except Exception: | ||
| count = 10 | ||
| if not query: | ||
| return {"results": [], "provider": provider, "error": "query is required"} | ||
| if provider not in PROVIDER_INFO or provider == "disabled": | ||
| return {"results": [], "provider": provider, "error": "Unknown provider"} | ||
| t0 = time.time() | ||
| try: | ||
| results = _call_provider(provider, query, min(count, 20)) | ||
| elapsed = round(time.time() - t0, 2) | ||
| return {"results": results, "provider": provider, "time": elapsed} | ||
| except Exception as e: | ||
| elapsed = round(time.time() - t0, 2) | ||
| logger.error(f"Search provider {provider} failed: {e}") | ||
| return {"results": [], "provider": provider, "time": elapsed, "error": str(e)} | ||
|
|
||
| return router | ||
| _sys.modules[__name__] = _canonical |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| """Regression test for the search route shim (slice 2j, #4082/#4071).""" | ||
|
|
||
| import importlib | ||
|
|
||
| import routes.search_routes as _shim_search # noqa: F401 | ||
|
|
||
|
|
||
| def test_legacy_and_canonical_search_module_are_same_object(): | ||
| legacy = importlib.import_module("routes.search_routes") | ||
| canonical = importlib.import_module("routes.search.search_routes") | ||
| assert legacy is canonical |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.