2026-01-16 13:22:26 +08:00
|
|
|
import logging
|
|
|
|
|
from collections.abc import AsyncGenerator
|
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
|
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
from src.gateway.config import get_gateway_config
|
2026-02-03 13:41:04 +08:00
|
|
|
from src.gateway.routers import artifacts, mcp, memory, models, skills, uploads
|
2026-01-19 23:23:38 +08:00
|
|
|
|
|
|
|
|
# Configure logging
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
level=logging.INFO,
|
|
|
|
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
|
|
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
|
|
|
)
|
2026-01-16 13:22:26 +08:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
|
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
|
|
|
|
|
"""Application lifespan handler."""
|
|
|
|
|
config = get_gateway_config()
|
|
|
|
|
logger.info(f"Starting API Gateway on {config.host}:{config.port}")
|
2026-01-19 18:57:16 +08:00
|
|
|
|
2026-01-25 22:37:53 +08:00
|
|
|
# NOTE: MCP tools initialization is NOT done here because:
|
|
|
|
|
# 1. Gateway doesn't use MCP tools - they are used by Agents in the LangGraph Server
|
|
|
|
|
# 2. Gateway and LangGraph Server are separate processes with independent caches
|
|
|
|
|
# MCP tools are lazily initialized in LangGraph Server when first needed
|
2026-01-19 18:57:16 +08:00
|
|
|
|
2026-01-16 13:22:26 +08:00
|
|
|
yield
|
|
|
|
|
logger.info("Shutting down API Gateway")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
|
|
|
"""Create and configure the FastAPI application.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Configured FastAPI application instance.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
app = FastAPI(
|
|
|
|
|
title="DeerFlow API Gateway",
|
2026-01-20 13:20:50 +08:00
|
|
|
description="""
|
|
|
|
|
## DeerFlow API Gateway
|
|
|
|
|
|
|
|
|
|
API Gateway for DeerFlow - A LangGraph-based AI agent backend with sandbox execution capabilities.
|
|
|
|
|
|
|
|
|
|
### Features
|
|
|
|
|
|
|
|
|
|
- **Models Management**: Query and retrieve available AI models
|
|
|
|
|
- **MCP Configuration**: Manage Model Context Protocol (MCP) server configurations
|
2026-02-03 13:41:04 +08:00
|
|
|
- **Memory Management**: Access and manage global memory data for personalized conversations
|
2026-01-20 13:57:36 +08:00
|
|
|
- **Skills Management**: Query and manage skills and their enabled status
|
2026-01-20 13:20:50 +08:00
|
|
|
- **Artifacts**: Access thread artifacts and generated files
|
|
|
|
|
- **Health Monitoring**: System health check endpoints
|
|
|
|
|
|
|
|
|
|
### Architecture
|
|
|
|
|
|
|
|
|
|
LangGraph requests are handled by nginx reverse proxy.
|
2026-01-20 13:57:36 +08:00
|
|
|
This gateway provides custom endpoints for models, MCP configuration, skills, and artifacts.
|
2026-01-20 13:20:50 +08:00
|
|
|
""",
|
2026-01-16 13:22:26 +08:00
|
|
|
version="0.1.0",
|
|
|
|
|
lifespan=lifespan,
|
2026-01-20 13:20:50 +08:00
|
|
|
docs_url="/docs",
|
|
|
|
|
redoc_url="/redoc",
|
|
|
|
|
openapi_url="/openapi.json",
|
|
|
|
|
openapi_tags=[
|
|
|
|
|
{
|
|
|
|
|
"name": "models",
|
|
|
|
|
"description": "Operations for querying available AI models and their configurations",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"name": "mcp",
|
|
|
|
|
"description": "Manage Model Context Protocol (MCP) server configurations",
|
|
|
|
|
},
|
2026-02-03 13:41:04 +08:00
|
|
|
{
|
|
|
|
|
"name": "memory",
|
|
|
|
|
"description": "Access and manage global memory data for personalized conversations",
|
|
|
|
|
},
|
2026-01-20 13:57:36 +08:00
|
|
|
{
|
|
|
|
|
"name": "skills",
|
|
|
|
|
"description": "Manage skills and their configurations",
|
|
|
|
|
},
|
2026-01-20 13:20:50 +08:00
|
|
|
{
|
|
|
|
|
"name": "artifacts",
|
|
|
|
|
"description": "Access and download thread artifacts and generated files",
|
|
|
|
|
},
|
2026-01-23 18:47:39 +08:00
|
|
|
{
|
|
|
|
|
"name": "uploads",
|
|
|
|
|
"description": "Upload and manage user files for threads",
|
|
|
|
|
},
|
2026-01-20 13:20:50 +08:00
|
|
|
{
|
|
|
|
|
"name": "health",
|
|
|
|
|
"description": "Health check and system status endpoints",
|
|
|
|
|
},
|
|
|
|
|
],
|
2026-01-16 13:22:26 +08:00
|
|
|
)
|
|
|
|
|
|
2026-01-19 23:23:38 +08:00
|
|
|
# CORS is handled by nginx - no need for FastAPI middleware
|
2026-01-16 13:22:26 +08:00
|
|
|
|
|
|
|
|
# Include routers
|
|
|
|
|
# Models API is mounted at /api/models
|
|
|
|
|
app.include_router(models.router)
|
|
|
|
|
|
2026-01-20 13:20:50 +08:00
|
|
|
# MCP API is mounted at /api/mcp
|
|
|
|
|
app.include_router(mcp.router)
|
|
|
|
|
|
2026-02-03 13:41:04 +08:00
|
|
|
# Memory API is mounted at /api/memory
|
|
|
|
|
app.include_router(memory.router)
|
|
|
|
|
|
2026-01-20 13:57:36 +08:00
|
|
|
# Skills API is mounted at /api/skills
|
|
|
|
|
app.include_router(skills.router)
|
|
|
|
|
|
2026-01-16 23:04:38 +08:00
|
|
|
# Artifacts API is mounted at /api/threads/{thread_id}/artifacts
|
|
|
|
|
app.include_router(artifacts.router)
|
|
|
|
|
|
2026-01-23 18:47:39 +08:00
|
|
|
# Uploads API is mounted at /api/threads/{thread_id}/uploads
|
|
|
|
|
app.include_router(uploads.router)
|
|
|
|
|
|
2026-01-20 13:20:50 +08:00
|
|
|
@app.get("/health", tags=["health"])
|
2026-01-16 13:22:26 +08:00
|
|
|
async def health_check() -> dict:
|
2026-01-20 13:20:50 +08:00
|
|
|
"""Health check endpoint.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Service health status information.
|
|
|
|
|
"""
|
2026-01-16 13:22:26 +08:00
|
|
|
return {"status": "healthy", "service": "deer-flow-gateway"}
|
|
|
|
|
|
|
|
|
|
return app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create app instance for uvicorn
|
|
|
|
|
app = create_app()
|