2025-04-23 21:43:57 +08:00
|
|
|
from fastapi import FastAPI, Request
|
2025-03-30 14:48:57 +08:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2025-04-23 21:43:57 +08:00
|
|
|
from fastapi.responses import JSONResponse
|
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
|
2025-04-02 02:49:35 +08:00
|
|
|
from utils.logger import logger
|
2025-04-06 17:10:18 +08:00
|
|
|
import uuid
|
2025-04-23 21:43:57 +08:00
|
|
|
import time
|
|
|
|
from collections import OrderedDict
|
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-03-30 14:48:57 +08:00
|
|
|
|
2025-04-24 08:45:58 +08:00
|
|
|
# Load environment variables (these will be available through config)
|
2025-03-30 14:48:57 +08:00
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
# Initialize managers
|
|
|
|
db = DBConnection()
|
|
|
|
thread_manager = None
|
2025-04-06 17:10:18 +08:00
|
|
|
instance_id = str(uuid.uuid4())[:8] # Generate instance ID at module load time
|
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):
|
|
|
|
# Startup
|
|
|
|
global thread_manager
|
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-03-30 14:48:57 +08:00
|
|
|
await db.initialize()
|
|
|
|
thread_manager = ThreadManager()
|
|
|
|
|
|
|
|
# Initialize the agent API with shared resources
|
|
|
|
agent_api.initialize(
|
|
|
|
thread_manager,
|
2025-04-06 17:10:18 +08:00
|
|
|
db,
|
|
|
|
instance_id # Pass the instance_id to agent_api
|
2025-03-30 14:48:57 +08:00
|
|
|
)
|
|
|
|
|
2025-04-18 22:04:11 +08:00
|
|
|
# Initialize the sandbox API with shared resources
|
|
|
|
sandbox_api.initialize(db)
|
|
|
|
|
2025-04-24 02:46:22 +08:00
|
|
|
# Redis is no longer needed for a single-server setup
|
|
|
|
# from services import redis
|
|
|
|
# await redis.initialize_async()
|
2025-03-30 14:48:57 +08:00
|
|
|
|
|
|
|
asyncio.create_task(agent_api.restore_running_agent_runs())
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
# Clean up agent resources (including Redis)
|
2025-04-01 10:36:26 +08:00
|
|
|
logger.info("Cleaning up agent resources")
|
2025-03-30 14:48:57 +08:00
|
|
|
await agent_api.cleanup()
|
|
|
|
|
|
|
|
# Clean up database connection
|
2025-04-01 10:36:26 +08:00
|
|
|
logger.info("Disconnecting from database")
|
2025-03-30 14:48:57 +08:00
|
|
|
await db.disconnect()
|
|
|
|
|
|
|
|
app = FastAPI(lifespan=lifespan)
|
|
|
|
|
2025-04-23 21:43:57 +08:00
|
|
|
# @app.middleware("http")
|
|
|
|
# async def log_requests_middleware(request: Request, call_next):
|
|
|
|
# client_ip = request.client.host
|
|
|
|
# logger.info(f"Request from IP {client_ip} to {request.method} {request.url.path}")
|
|
|
|
# response = await call_next(request)
|
|
|
|
# return response
|
|
|
|
|
|
|
|
# @app.middleware("http")
|
|
|
|
# async def throw_error_middleware(request: Request, call_next):
|
|
|
|
# client_ip = request.client.host
|
|
|
|
# if client_ip != "109.49.168.102":
|
|
|
|
# logger.warning(f"Request blocked from IP {client_ip} to {request.method} {request.url.path}")
|
|
|
|
# return JSONResponse(
|
|
|
|
# status_code=403,
|
|
|
|
# content={"error": "Request blocked", "message": "Test DDoS protection"}
|
|
|
|
# )
|
|
|
|
# return await call_next(request)
|
|
|
|
|
|
|
|
# @app.middleware("http")
|
|
|
|
# async def rate_limit_middleware(request: Request, call_next):
|
|
|
|
# global ip_tracker
|
|
|
|
# client_ip = request.client.host
|
|
|
|
|
|
|
|
# # Clean up old entries (older than 5 minutes)
|
|
|
|
# current_time = time.time()
|
|
|
|
# ip_tracker = OrderedDict((ip, ts) for ip, ts in ip_tracker.items()
|
|
|
|
# if current_time - ts < 300)
|
|
|
|
|
|
|
|
# # Check if IP is already tracked
|
|
|
|
# if client_ip in ip_tracker:
|
|
|
|
# ip_tracker[client_ip] = current_time
|
|
|
|
# return await call_next(request)
|
|
|
|
|
|
|
|
# # Check if we've hit the limit
|
|
|
|
# if len(ip_tracker) >= MAX_CONCURRENT_IPS:
|
|
|
|
# logger.warning(f"Rate limit exceeded. Current IPs: {len(ip_tracker)}")
|
|
|
|
# return JSONResponse(
|
|
|
|
# status_code=429,
|
|
|
|
# content={"error": "Too many concurrent connections",
|
|
|
|
# "message": "Maximum number of concurrent connections reached"}
|
|
|
|
# )
|
|
|
|
|
|
|
|
# # Add new IP
|
|
|
|
# ip_tracker[client_ip] = current_time
|
|
|
|
# logger.info(f"New connection from IP {client_ip}. Total connections: {len(ip_tracker)}")
|
|
|
|
# return await call_next(request)
|
|
|
|
|
2025-04-24 11:34:45 +08:00
|
|
|
# Define allowed origins based on environment
|
|
|
|
allowed_origins = ["https://www.suna.so", "https://suna.so", "https://staging.suna.so"]
|
|
|
|
|
|
|
|
# Add staging-specific origins
|
|
|
|
if config.ENV_MODE == EnvMode.STAGING:
|
|
|
|
allowed_origins.append("http://localhost:3000")
|
|
|
|
|
|
|
|
# Add local-specific origins
|
|
|
|
if config.ENV_MODE == EnvMode.LOCAL:
|
|
|
|
allowed_origins.append("http://localhost:3000")
|
|
|
|
|
2025-03-30 14:48:57 +08:00
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
2025-04-24 11:34:45 +08:00
|
|
|
allow_origins=allowed_origins,
|
2025-03-30 14:48:57 +08:00
|
|
|
allow_credentials=True,
|
2025-04-18 21:50:06 +08:00
|
|
|
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
|
|
|
allow_headers=["Content-Type", "Authorization"],
|
2025-03-30 14:48:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Include the agent router with a prefix
|
|
|
|
app.include_router(agent_api.router, prefix="/api")
|
|
|
|
|
2025-04-11 09:35:29 +08:00
|
|
|
# Include the sandbox router with a prefix
|
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-24 11:34:45 +08:00
|
|
|
@app.get("/api/")
|
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")
|
2025-04-06 17:10:18 +08:00
|
|
|
return {
|
|
|
|
"status": "ok",
|
|
|
|
"timestamp": datetime.now(timezone.utc).isoformat(),
|
|
|
|
"instance_id": instance_id
|
|
|
|
}
|
2025-03-30 14:48:57 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import uvicorn
|
2025-04-01 10:36:26 +08:00
|
|
|
logger.info("Starting server on 0.0.0.0:8000")
|
2025-03-30 14:48:57 +08:00
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|