Dispatch a request to the appropriate handler. Returns a single Response for most requests, or an AsyncIterator for streaming requests (IndexRequest, SearchRequest when waiting, DoctorRequest).
(
req: Request,
registry: ProjectRegistry,
start_time: float,
on_shutdown: Callable[[], None],
settings_env_names: list[str],
)
| 455 | |
| 456 | |
| 457 | async def _dispatch( |
| 458 | req: Request, |
| 459 | registry: ProjectRegistry, |
| 460 | start_time: float, |
| 461 | on_shutdown: Callable[[], None], |
| 462 | settings_env_names: list[str], |
| 463 | ) -> ( |
| 464 | Response |
| 465 | | AsyncIterator[IndexStreamResponse] |
| 466 | | AsyncIterator[SearchStreamResponse] |
| 467 | | AsyncIterator[DoctorStreamResponse] |
| 468 | ): |
| 469 | """Dispatch a request to the appropriate handler. |
| 470 | |
| 471 | Returns a single Response for most requests, or an AsyncIterator for |
| 472 | streaming requests (IndexRequest, SearchRequest when waiting, DoctorRequest). |
| 473 | """ |
| 474 | try: |
| 475 | if isinstance(req, IndexRequest): |
| 476 | project = await registry.get_project(req.project_root) |
| 477 | return project.stream_index() |
| 478 | |
| 479 | if isinstance(req, SearchRequest): |
| 480 | project = await registry.get_project(req.project_root) |
| 481 | await project.ensure_indexing_started() |
| 482 | |
| 483 | if project.should_wait_for_indexing: |
| 484 | return _search_with_wait(project, req) |
| 485 | |
| 486 | results = await project.search( |
| 487 | query=req.query, |
| 488 | languages=req.languages, |
| 489 | paths=req.paths, |
| 490 | limit=req.limit, |
| 491 | offset=req.offset, |
| 492 | ) |
| 493 | return SearchResponse( |
| 494 | success=True, |
| 495 | results=results, |
| 496 | total_returned=len(results), |
| 497 | offset=req.offset, |
| 498 | ) |
| 499 | |
| 500 | if isinstance(req, ProjectStatusRequest): |
| 501 | project = await registry.get_project(req.project_root) |
| 502 | await project.ensure_indexing_started() |
| 503 | return project.get_status() |
| 504 | |
| 505 | if isinstance(req, DaemonStatusRequest): |
| 506 | return DaemonStatusResponse( |
| 507 | version=__version__, |
| 508 | uptime_seconds=time.monotonic() - start_time, |
| 509 | projects=registry.list_projects(), |
| 510 | ) |
| 511 | |
| 512 | if isinstance(req, RemoveProjectRequest): |
| 513 | registry.remove_project(req.project_root) |
| 514 | return RemoveProjectResponse(ok=True) |
no test coverage detected