MCPcopy Create free account
hub / github.com/cocoindex-io/cocoindex-code / _dispatch

Function _dispatch

src/cocoindex_code/daemon.py:457–543  ·  view source on GitHub ↗

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],
)

Source from the content-addressed store, hash-verified

455
456
457async 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)

Callers 1

handle_connectionFunction · 0.85

Calls 15

_search_with_waitFunction · 0.85
SearchResponseClass · 0.85
StopResponseClass · 0.85
DaemonEnvResponseClass · 0.85
DbPathMappingEntryClass · 0.85
get_db_path_mappingsFunction · 0.85
get_host_path_mappingsFunction · 0.85
_handle_doctorFunction · 0.85
ErrorResponseClass · 0.85
get_projectMethod · 0.80

Tested by

no test coverage detected