Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import HTMLResponse, FileResponse | |
| from pathlib import Path | |
| from .core.config import settings | |
| from .schemas import OpportunityPlanResponse, OpportunityRequest | |
| from .services.agent import AgentPlanner | |
| app = FastAPI(title=settings.app_name, version=settings.version) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=settings.cors_origins, | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Per-request planner; no global initialization to allow per-request API keys | |
| agent_planner = None | |
| agent_init_error = None | |
| def health() -> dict: | |
| return { | |
| "status": "ok", | |
| "name": settings.app_name, | |
| "version": settings.version, | |
| "agent_ready": True, | |
| "agent_error": None, | |
| } | |
| def index() -> FileResponse: | |
| base = Path(__file__).resolve().parent | |
| return FileResponse((base / "static" / "index.html").as_posix()) | |
| def generate_plan(payload: OpportunityRequest) -> OpportunityPlanResponse: | |
| if not payload.industry.strip(): | |
| raise HTTPException(status_code=422, detail="industry must be non-empty") | |
| if not payload.pain_points: | |
| raise HTTPException(status_code=422, detail="Provide at least one pain point") | |
| try: | |
| planner = AgentPlanner(api_key=getattr(payload, "openai_api_key", None)) | |
| return planner.generate_plan(payload) | |
| except Exception as exc: # surface agent errors to client | |
| raise HTTPException(status_code=502, detail=f"Agent error: {exc}") | |