from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel import httpx import logging import sys import traceback import os logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", handlers=[logging.StreamHandler(sys.stdout)]) logger = logging.getLogger(__name__) app = FastAPI() app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"]) class MessageRequest(BaseModel): message: str LANGGRAPH_URL = os.getenv("LANGGRAPH_URL", "http://langgraph-service:8090") @app.post("/chat") async def chat(request: MessageRequest): """Updated chat endpoint that routes through LangGraph Supervisor.""" logger.info(f"Gateway: Received message: {request.message}") try: # Call LangGraph Supervisor instead of direct brain async with httpx.AsyncClient(timeout=httpx.Timeout(60.0, connect=10.0)) as client: response = await client.post( f"{LANGGRAPH_URL}/query", json={"query": request.message} ) if response.status_code == 200: result = response.json() logger.info(f"Gateway: Response from {result.get('agent_used', 'unknown')} agent") return {"response": result["response"]} else: logger.error(f"Gateway: LangGraph error {response.status_code}") return {"response": "Error: Orchestration service unavailable"} except Exception as e: logger.error(f"Gateway: Error routing through LangGraph: {traceback.format_exc()}") return {"response": "Error: Unable to process your request at this time."} @app.get("/health") async def health(): return {"status": "healthy", "service": "chat-gateway"}