"""FastAPI entrypoint for the AI Engineer test task."""
from __future__ import annotations
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from app.agent.graph import ProductAgent
from app.schemas import AgentQueryRequest, AgentQueryResponse
logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(application: FastAPI):
"""Initialize shared resources for API process lifetime."""
application.state.agent = ProductAgent()
LOGGER.info("ProductAgent initialized")
yield
app = FastAPI(
title="AI Engineer Test Task API",
version="1.0.0",
description="LangGraph agent with MCP integration",
lifespan=lifespan,
)
@app.get("/health")
async def health() -> dict[str, str]:
return {"status": "ok"}
@app.post("/api/v1/agent/query", response_model=AgentQueryResponse)
async def query_agent(payload: AgentQueryRequest) -> AgentQueryResponse:
agent: ProductAgent | None = getattr(app.state, "agent", None)
if agent is None:
raise HTTPException(status_code=503, detail="Agent is not initialized")
result = await agent.run(payload.query)
return AgentQueryResponse(**result)