You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5 KiB
Python

from __future__ import annotations
from typing import Any
from fastapi import APIRouter, Request, Response
from app.a2a.agent_card import build_agent_card
from app.a2a.models import A2ARpcError, A2ARpcRequest, A2ARpcResponse
from app.config import get_settings
settings = get_settings()
router = APIRouter(tags=["a2a"])
@router.get("/.well-known/agent-card.json")
def get_agent_card(request: Request, response: Response) -> dict[str, Any]:
response.headers["A2A-Version"] = "1.0"
return build_agent_card(settings=settings, request=request)
@router.post("/a2a/rpc", response_model=A2ARpcResponse)
def a2a_rpc(payload: A2ARpcRequest, response: Response) -> A2ARpcResponse:
response.headers["A2A-Version"] = "1.0"
if payload.jsonrpc != "2.0":
return _error_response(
request_id=payload.id,
code=-32600,
message="Invalid Request: jsonrpc must be '2.0'.",
)
if payload.method in {"ping", "health.ping", "health/ping"}:
return A2ARpcResponse(
id=payload.id,
result={"status": "ok", "agent": settings.a2a_agent_name},
)
return _error_response(
request_id=payload.id,
code=-32601,
message=f"Method '{payload.method}' is not implemented yet.",
)
def _error_response(request_id: str | int | None, code: int, message: str) -> A2ARpcResponse:
return A2ARpcResponse(
id=request_id,
error=A2ARpcError(code=code, message=message),
)