"""The Orchestration engine's A2A server: `python -m orchestration.main`.""" from __future__ import annotations import logging from collections.abc import AsyncIterator, Sequence from contextlib import asynccontextmanager import click import httpx import uvicorn from a2a.server.request_handlers.default_request_handler_v2 import ( DefaultRequestHandlerV2, ) from a2a.server.routes.jsonrpc_routes import create_jsonrpc_routes from a2a.server.tasks import InMemoryTaskStore from a2a.utils.constants import AGENT_CARD_WELL_KNOWN_PATH from starlette.applications import Starlette from starlette.requests import Request from starlette.responses import JSONResponse from starlette.routing import Route from orchestration.agents import Agent, load_routes from orchestration.agents import Sequence as AgentSequence from orchestration.card import A2A_PATH, build_card, card_document from orchestration.config import OrchestrationSettings from orchestration.relay import AgentClients, RelayExecutor LEGACY_CARD_PATH = "/.well-known/agent.json" def create_app( agents: Sequence[Agent], settings: OrchestrationSettings | None = None, *, httpx_client: httpx.AsyncClient | None = None, clients: AgentClients | None = None, sequences: Sequence[AgentSequence] = (), ) -> Starlette: """The server. `httpx_client` is how it reaches the agents; one is made (and closed) here when none is given. `clients` replaces how agents are called altogether, for tests.""" settings = settings or OrchestrationSettings() own_client = httpx_client is None client = httpx_client or httpx.AsyncClient( timeout=httpx.Timeout(None, connect=settings.CONNECT_TIMEOUT_SECONDS) ) card = build_card(settings, agents, sequences) served_card = card_document(card, agents, sequences) handler = DefaultRequestHandlerV2( agent_executor=RelayExecutor( agents, clients or AgentClients(client), settings.HEARTBEAT_SECONDS, sequences, ), task_store=InMemoryTaskStore(), agent_card=card, ) async def agent_card(request: Request) -> JSONResponse: return JSONResponse(served_card) async def health(request: Request) -> JSONResponse: return JSONResponse( { "status": "healthy", "agent": settings.AGENT_NAME, "version": settings.AGENT_VERSION, "agents": [agent.work for agent in agents], "sequences": [sequence.work for sequence in sequences], } ) @asynccontextmanager async def lifespan(app: Starlette) -> AsyncIterator[None]: try: yield finally: await handler.aclose() if own_client: await client.aclose() # The card names /a2a; the root serves the same handler, because the KAB # platform calls the address an agent was registered with. jsonrpc = [ route for path in (A2A_PATH, "/") for route in create_jsonrpc_routes(handler, path, enable_v0_3_compat=True) ] return Starlette( routes=[ Route("/health", health, methods=["GET"]), Route(AGENT_CARD_WELL_KNOWN_PATH, agent_card, methods=["GET"]), Route(LEGACY_CARD_PATH, agent_card, methods=["GET"]), *jsonrpc, ], lifespan=lifespan, ) @click.command() @click.option("--host", default=None, help="Host to bind (default A2A_HOST)") @click.option("--port", default=None, type=int, help="Port (default A2A_PORT)") def main(host: str | None, port: int | None) -> None: settings = OrchestrationSettings() logging.basicConfig(level=settings.LOG_LEVEL.upper()) agents, sequences = load_routes(settings.AGENTS_FILE) uvicorn.run( create_app(agents, settings, sequences=sequences), host=host or settings.A2A_HOST, port=port or settings.A2A_PORT, ) if __name__ == "__main__": main()