suna/backend/api.py

211 lines
6.5 KiB
Python
Raw Normal View History

2025-06-02 13:44:22 +08:00
from fastapi import FastAPI, Request, HTTPException, Response, Depends
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
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
from dotenv import load_dotenv
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-06-02 14:01:09 +08:00
from typing import Dict, Any
2025-03-30 14:48:57 +08:00
2025-06-02 13:44:22 +08:00
from pydantic import BaseModel
import uuid
2025-03-30 14:48:57 +08:00
# Import the agent API module
from agent import api as agent_api
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 14:01:09 +08:00
from services.mcp_custom import discover_custom_tools
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
2025-06-04 01:34:27 +08:00
2025-03-30 14:48:57 +08:00
load_dotenv()
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-04-24 08:45:58 +08:00
logger.info(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
)
sandbox_api.initialize(db)
# Initialize Redis connection
from services import redis
try:
await redis.initialize_async()
logger.info("Redis connection initialized successfully")
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
# Initialize triggers API
triggers_api.initialize(db)
2025-07-01 02:03:46 +08:00
unified_oauth_api.initialize(db)
2025-06-30 18:57:34 +08:00
2025-04-25 05:15:40 +08:00
yield
# Clean up agent resources
logger.info("Cleaning up agent resources")
await agent_api.cleanup()
# Clean up Redis connection
try:
2025-04-25 20:23:05 +08:00
logger.info("Closing Redis connection")
2025-04-25 05:15:40 +08:00
await redis.close()
2025-04-25 20:23:05 +08:00
logger.info("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
logger.info("Disconnecting from database")
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()
client_ip = request.client.host
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
logger.info(f"Request started: {method} {path} from {client_ip} | Query: {query_params}")
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
allowed_origins = ["https://www.suna.so", "https://suna.so", "http://localhost:3000"]
allow_origin_regex = None
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")
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", "DELETE", "OPTIONS"],
2025-06-14 15:08:58 +08:00
allow_headers=["Content-Type", "Authorization", "X-Project-Id"],
2025-03-30 14:48:57 +08:00
)
app.include_router(agent_api.router, prefix="/api")
2025-04-18 22:04:11 +08:00
app.include_router(sandbox_api.router, prefix="/api")
2025-04-11 09:35:29 +08:00
2025-04-27 07:47:06 +08:00
app.include_router(billing_api.router, prefix="/api")
2025-06-05 15:30:44 +08:00
app.include_router(feature_flags_api.router, prefix="/api")
2025-05-25 02:17:55 +08:00
from mcp_local import api as mcp_api
2025-06-07 14:24:59 +08:00
from mcp_local import secure_api as secure_mcp_api
2025-06-02 14:01:09 +08:00
2025-05-24 18:39:58 +08:00
app.include_router(mcp_api.router, prefix="/api")
2025-06-07 14:24:59 +08:00
app.include_router(secure_mcp_api.router, prefix="/api/secure-mcp")
2025-06-02 14:01:09 +08:00
app.include_router(transcription_api.router, prefix="/api")
2025-06-04 01:34:27 +08:00
app.include_router(email_api.router, prefix="/api")
2025-06-24 18:30:01 +08:00
from knowledge_base import api as knowledge_base_api
app.include_router(knowledge_base_api.router, prefix="/api")
2025-06-30 18:57:34 +08:00
from triggers import api as triggers_api
2025-07-01 02:03:46 +08:00
from triggers import unified_oauth_api
2025-06-30 18:57:34 +08:00
app.include_router(triggers_api.router)
2025-07-01 02:03:46 +08:00
app.include_router(unified_oauth_api.router)
2025-06-30 18:57:34 +08:00
2025-04-26 07:15:47 +08:00
@app.get("/api/health")
2025-03-30 14:48:57 +08:00
async def health_check():
"""Health check endpoint to verify API is working."""
2025-04-01 10:36:26 +08:00
logger.info("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
2025-06-02 13:44:22 +08:00
class CustomMCPDiscoverRequest(BaseModel):
2025-06-02 14:01:09 +08:00
type: str
2025-06-02 13:44:22 +08:00
config: Dict[str, Any]
@app.post("/api/mcp/discover-custom-tools")
async def discover_custom_mcp_tools(request: CustomMCPDiscoverRequest):
try:
2025-06-02 14:01:09 +08:00
return await discover_custom_tools(request.type, request.config)
2025-06-02 13:44:22 +08:00
except HTTPException:
raise
except Exception as e:
logger.error(f"Error discovering custom MCP tools: {e}")
raise HTTPException(status_code=500, detail=str(e))
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
logger.info(f"Starting server on 0.0.0.0:8000 with {workers} workers")
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
)