Spaces:
Sleeping
Sleeping
| from __future__ import annotations | |
| import os | |
| from typing import List | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel, EmailStr, Field | |
| from .service import FlightBookingAgentService, TripRequest | |
| def parse_allowed_origins() -> List[str]: | |
| raw = os.getenv("ALLOWED_ORIGINS", "*") | |
| origins = [item.strip() for item in raw.split(",") if item.strip()] | |
| return origins or ["*"] | |
| app = FastAPI( | |
| title="Flight Agent API", | |
| version="1.1.0", | |
| description="FastAPI wrapper for the agentic flight booking demo.", | |
| ) | |
| allow_origins = parse_allowed_origins() | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=allow_origins, | |
| allow_credentials=False if allow_origins == ["*"] else True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| service = FlightBookingAgentService() | |
| class ChatRequest(BaseModel): | |
| thread_id: str = Field(..., description="Stable conversation or user id") | |
| user_msg: str | |
| class SearchRequest(BaseModel): | |
| thread_id: str | |
| trip: TripRequest | |
| class HoldRequest(BaseModel): | |
| thread_id: str | |
| choice_index: int = Field(..., ge=1, le=3) | |
| class ConfirmRequest(BaseModel): | |
| thread_id: str | |
| email: EmailStr | |
| def root() -> dict: | |
| return { | |
| "name": "Flight Agent API", | |
| "status": "ok", | |
| "docs": "/docs", | |
| "health": "/health", | |
| "cors": allow_origins, | |
| "storage": service.get_storage_info(), | |
| } | |
| def health() -> dict: | |
| return {"status": "ok", "storage": service.get_storage_info()} | |
| def chat(payload: ChatRequest) -> dict: | |
| return service.chat(payload.thread_id, payload.user_msg) | |
| def search(payload: SearchRequest) -> dict: | |
| return service.search(payload.thread_id, payload.trip) | |
| def hold(payload: HoldRequest) -> dict: | |
| return service.hold(payload.thread_id, payload.choice_index) | |
| def confirm(payload: ConfirmRequest) -> dict: | |
| return service.confirm(payload.thread_id, str(payload.email)) | |
| def get_state(thread_id: str) -> dict: | |
| return {"thread_id": thread_id, "state": service.get_state(thread_id)} | |
| def reset_state(thread_id: str) -> dict: | |
| service.reset_state(thread_id) | |
| return {"thread_id": thread_id, "deleted": True} | |