""" Responses router. OpenAI-compatible /v1/responses endpoint with streaming support. """ from fastapi import APIRouter, HTTPException from sse_starlette.sse import EventSourceResponse from src.core.exceptions import AppException, ModelNotFoundError from src.core.logging_config import get_logger from src.responses import service from src.responses.schemas import Response, ResponseRequest logger = get_logger(__name__) router = APIRouter(prefix="/responses", tags=["responses"]) @router.post("", response_model=Response) async def create_response( request: ResponseRequest, ) -> Response | EventSourceResponse: """ Create a response using Responses API format. Supports: - Reasoning summaries (thinking/reasoning display) - Function calling (tool usage) - Streaming responses - Multi-turn conversations - Error handling Args: request: Response request with model, input, optional reasoning/tools Returns: Response object or SSE stream """ logger.info( "response_request_received", model=request.model, user=request.user, streaming=request.stream, ) try: # Check if this is a Tatlock request - use Steward preprocessing model_id = request.model if "." in model_id: model_id = model_id.split(".", 1)[1] use_steward = model_id.lower() == "tatlock" if request.stream: logger.info("Streaming response requested") if use_steward: logger.info("Streaming with Steward preprocessing for Tatlock request") from src.responses.streaming import StreamingCoordinator coordinator = StreamingCoordinator() return EventSourceResponse(coordinator.stream_response_with_steward(request)) else: return EventSourceResponse(service.create_response_stream(request)) # Non-streaming response if use_steward: logger.info("Using Steward preprocessing for Tatlock request") return await service.create_response_with_steward(request) else: return await service.create_response(request) except ModelNotFoundError as e: logger.error(f"Model not found: {e}") raise HTTPException(status_code=404, detail=str(e)) from e except AppException as e: logger.error(f"Application error: {e}") raise HTTPException(status_code=e.status_code, detail=e.message) from e except Exception as e: logger.error(f"Unexpected error: {e}", exc_info=True) raise HTTPException(status_code=500, detail="Internal server error") from e