suna/backend/api.py

242 lines
7.5 KiB
Python
Raw Permalink Normal View History

from dotenv import load_dotenv
load_dotenv()
2025-07-07 00:17:29 +08:00
from fastapi import FastAPI, Request, HTTPException, Response, Depends, APIRouter
2025-03-30 14:48:57 +08:00
from fastapi.middleware.cors import CORSMiddleware
2025-06-02 13:44:22 +08:00
from fastapi.responses import JSONResponse, StreamingResponse
from services import redis
2025-06-30 18:57:34 +08:00
import sentry
2025-03-30 14:48:57 +08:00
from contextlib import asynccontextmanager
from agentpress.thread_manager import ThreadManager
from services.supabase import DBConnection
from datetime import datetime, timezone
2025-04-24 11:34:45 +08:00
from utils.config import config, EnvMode
2025-03-30 14:48:57 +08:00
import asyncio
from utils.logger import logger, structlog
2025-04-23 21:43:57 +08:00
import time
from collections import OrderedDict
2025-03-30 14:48:57 +08:00
2025-06-02 13:44:22 +08:00
from pydantic import BaseModel
import uuid
2025-07-14 22:17:10 +08:00
2025-03-30 14:48:57 +08:00
from agent import api as agent_api
2025-07-14 22:17:10 +08:00
2025-04-18 22:04:11 +08:00
from sandbox import api as sandbox_api
2025-04-27 07:47:06 +08:00
from services import billing as billing_api
2025-06-05 15:30:44 +08:00
from flags import api as feature_flags_api
from services import transcription as transcription_api
2025-06-02 13:44:22 +08:00
import sys
2025-06-04 01:34:27 +08:00
from services import email_api
2025-06-30 18:57:34 +08:00
from triggers import api as triggers_api
from services import api_keys_api
2025-06-04 01:34:27 +08:00
2025-03-30 14:48:57 +08:00
2025-06-02 13:44:22 +08:00
if sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
2025-03-30 14:48:57 +08:00
# Initialize managers
db = DBConnection()
2025-04-25 20:23:05 +08:00
instance_id = "single"
2025-03-30 14:48:57 +08:00
2025-04-23 21:43:57 +08:00
# Rate limiter state
ip_tracker = OrderedDict()
MAX_CONCURRENT_IPS = 25
2025-03-30 14:48:57 +08:00
@asynccontextmanager
async def lifespan(app: FastAPI):
2025-08-17 10:10:56 +08:00
logger.debug(f"Starting up FastAPI application with instance ID: {instance_id} in {config.ENV_MODE.value} mode")
2025-04-25 05:15:40 +08:00
try:
await db.initialize()
agent_api.initialize(
db,
instance_id
)
2025-07-08 03:31:12 +08:00
2025-04-25 05:15:40 +08:00
sandbox_api.initialize(db)
# Initialize Redis connection
from services import redis
try:
await redis.initialize_async()
2025-08-17 10:10:56 +08:00
logger.debug("Redis connection initialized successfully")
2025-04-25 05:15:40 +08:00
except Exception as e:
logger.error(f"Failed to initialize Redis connection: {e}")
# Continue without Redis - the application will handle Redis failures gracefully
# Start background tasks
# asyncio.create_task(agent_api.restore_running_agent_runs())
2025-04-25 05:15:40 +08:00
2025-06-30 18:57:34 +08:00
triggers_api.initialize(db)
2025-07-09 02:40:58 +08:00
pipedream_api.initialize(db)
2025-07-29 00:36:07 +08:00
credentials_api.initialize(db)
2025-07-29 13:55:18 +08:00
template_api.initialize(db)
2025-08-02 16:22:17 +08:00
composio_api.initialize(db)
2025-07-09 02:40:58 +08:00
2025-04-25 05:15:40 +08:00
yield
# Clean up agent resources
2025-08-17 10:10:56 +08:00
logger.debug("Cleaning up agent resources")
2025-04-25 05:15:40 +08:00
await agent_api.cleanup()
# Clean up Redis connection
try:
2025-08-17 10:10:56 +08:00
logger.debug("Closing Redis connection")
2025-04-25 05:15:40 +08:00
await redis.close()
2025-08-17 10:10:56 +08:00
logger.debug("Redis connection closed successfully")
2025-04-25 05:15:40 +08:00
except Exception as e:
logger.error(f"Error closing Redis connection: {e}")
# Clean up database connection
2025-08-17 10:10:56 +08:00
logger.debug("Disconnecting from database")
2025-04-25 05:15:40 +08:00
await db.disconnect()
except Exception as e:
logger.error(f"Error during application startup: {e}")
raise
2025-03-30 14:48:57 +08:00
app = FastAPI(lifespan=lifespan)
2025-04-25 06:04:59 +08:00
@app.middleware("http")
async def log_requests_middleware(request: Request, call_next):
structlog.contextvars.clear_contextvars()
request_id = str(uuid.uuid4())
2025-04-25 06:04:59 +08:00
start_time = time.time()
2025-07-07 00:17:29 +08:00
client_ip = request.client.host if request.client else "unknown"
2025-04-25 06:04:59 +08:00
method = request.method
path = request.url.path
query_params = str(request.query_params)
structlog.contextvars.bind_contextvars(
request_id=request_id,
client_ip=client_ip,
method=method,
path=path,
query_params=query_params
)
2025-04-25 06:04:59 +08:00
# Log the incoming request
2025-08-17 10:10:56 +08:00
logger.debug(f"Request started: {method} {path} from {client_ip} | Query: {query_params}")
2025-04-25 06:04:59 +08:00
try:
response = await call_next(request)
process_time = time.time() - start_time
logger.debug(f"Request completed: {method} {path} | Status: {response.status_code} | Time: {process_time:.2f}s")
return response
except Exception as e:
process_time = time.time() - start_time
logger.error(f"Request failed: {method} {path} | Error: {str(e)} | Time: {process_time:.2f}s")
raise
2025-04-23 21:43:57 +08:00
2025-04-24 11:34:45 +08:00
# Define allowed origins based on environment
2025-07-06 12:40:44 +08:00
allowed_origins = ["https://www.suna.so", "https://suna.so"]
allow_origin_regex = None
2025-04-24 11:34:45 +08:00
2025-07-06 12:40:44 +08:00
# Add staging-specific origins
if config.ENV_MODE == EnvMode.LOCAL:
allowed_origins.append("http://localhost:3000")
2025-04-24 11:34:45 +08:00
# Add staging-specific origins
if config.ENV_MODE == EnvMode.STAGING:
allowed_origins.append("https://staging.suna.so")
2025-07-06 12:40:44 +08:00
allowed_origins.append("http://localhost:3000")
allow_origin_regex = r"https://suna-.*-prjcts\.vercel\.app"
2025-04-24 11:34:45 +08:00
2025-03-30 14:48:57 +08:00
app.add_middleware(
CORSMiddleware,
2025-04-24 11:34:45 +08:00
allow_origins=allowed_origins,
allow_origin_regex=allow_origin_regex,
2025-03-30 14:48:57 +08:00
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
allow_headers=["Content-Type", "Authorization", "X-Project-Id", "X-MCP-URL", "X-MCP-Type", "X-MCP-Headers", "X-Refresh-Token", "X-API-Key"],
2025-03-30 14:48:57 +08:00
)
2025-07-07 00:17:29 +08:00
# Create a main API router
api_router = APIRouter()
2025-03-30 14:48:57 +08:00
2025-07-07 00:17:29 +08:00
# Include all API routers without individual prefixes
api_router.include_router(agent_api.router)
api_router.include_router(sandbox_api.router)
api_router.include_router(billing_api.router)
api_router.include_router(feature_flags_api.router)
api_router.include_router(api_keys_api.router)
2025-06-05 15:30:44 +08:00
2025-07-14 18:36:27 +08:00
from mcp_module import api as mcp_api
from credentials import api as credentials_api
from templates import api as template_api
2025-06-02 14:01:09 +08:00
2025-07-07 00:17:29 +08:00
api_router.include_router(mcp_api.router)
2025-07-14 18:36:27 +08:00
api_router.include_router(credentials_api.router, prefix="/secure-mcp")
2025-07-07 00:17:29 +08:00
api_router.include_router(template_api.router, prefix="/templates")
2025-06-02 14:01:09 +08:00
2025-07-07 00:17:29 +08:00
api_router.include_router(transcription_api.router)
api_router.include_router(email_api.router)
2025-06-04 01:34:27 +08:00
2025-06-24 18:30:01 +08:00
from knowledge_base import api as knowledge_base_api
2025-07-07 00:17:29 +08:00
api_router.include_router(knowledge_base_api.router)
2025-06-24 18:30:01 +08:00
2025-07-07 00:17:29 +08:00
api_router.include_router(triggers_api.router)
2025-06-30 18:57:34 +08:00
2025-07-08 17:10:15 +08:00
from pipedream import api as pipedream_api
api_router.include_router(pipedream_api.router)
# MFA functionality moved to frontend
2025-07-27 11:53:25 +08:00
2025-07-12 16:22:33 +08:00
2025-07-27 11:15:33 +08:00
from admin import api as admin_api
api_router.include_router(admin_api.router)
2025-08-01 15:24:16 +08:00
from composio_integration import api as composio_api
api_router.include_router(composio_api.router)
2025-07-07 00:17:29 +08:00
@api_router.get("/health")
2025-03-30 14:48:57 +08:00
async def health_check():
2025-08-17 10:10:56 +08:00
logger.debug("Health check endpoint called")
return {
"status": "ok",
"timestamp": datetime.now(timezone.utc).isoformat(),
"instance_id": instance_id
}
2025-03-30 14:48:57 +08:00
@api_router.get("/health-docker")
async def health_check():
2025-08-17 10:10:56 +08:00
logger.debug("Health docker check endpoint called")
try:
client = await redis.get_client()
await client.ping()
db = DBConnection()
await db.initialize()
db_client = await db.client
await db_client.table("threads").select("thread_id").limit(1).execute()
2025-08-17 10:10:56 +08:00
logger.debug("Health docker check complete")
return {
"status": "ok",
"timestamp": datetime.now(timezone.utc).isoformat(),
"instance_id": instance_id
}
except Exception as e:
logger.error(f"Failed health docker check: {e}")
raise HTTPException(status_code=500, detail="Health check failed")
2025-07-07 00:17:29 +08:00
app.include_router(api_router, prefix="/api")
2025-06-02 13:44:22 +08:00
2025-03-30 14:48:57 +08:00
if __name__ == "__main__":
import uvicorn
2025-04-25 06:04:59 +08:00
2025-06-02 13:44:22 +08:00
if sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
workers = 4
2025-04-25 06:04:59 +08:00
2025-08-17 10:10:56 +08:00
logger.debug(f"Starting server on 0.0.0.0:8000 with {workers} workers")
2025-04-25 06:04:59 +08:00
uvicorn.run(
"api:app",
host="0.0.0.0",
port=8000,
2025-04-27 07:47:06 +08:00
workers=workers,
2025-06-02 14:01:09 +08:00
loop="asyncio"
2025-04-25 06:04:59 +08:00
)