Skip to content

Commit

Permalink
okay it appears to all work
Browse files Browse the repository at this point in the history
  • Loading branch information
jmilldotdev committed Jan 7, 2025
1 parent fb8deb1 commit bf9fea9
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 296 deletions.
1 change: 0 additions & 1 deletion eve/agent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import yaml
import time
import json
import traceback
Expand Down
190 changes: 0 additions & 190 deletions eve/api.py

This file was deleted.

110 changes: 68 additions & 42 deletions eve/api/api.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,93 @@
import logging
import os
import threading
import modal
from fastapi import FastAPI, Depends, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import APIKeyHeader, HTTPBearer
import logging
from ably import AblyRealtime
from apscheduler.schedulers.background import BackgroundScheduler
from pathlib import Path
from contextlib import asynccontextmanager

from eve import auth
from eve.api.handlers import (
handle_cancel,
handle_chat,
handle_create,
handle_deployment,
handle_schedule,
handle_deployment_create,
handle_deployment_delete,
handle_stream_chat,
handle_trigger_create,
handle_trigger_delete,
)
from eve.api.helpers import load_existing_triggers
from eve.api.requests import (
CancelRequest,
ChatRequest,
ScheduleRequest,
CreateDeploymentRequest,
CreateTriggerRequest,
DeleteDeploymentRequest,
DeleteTriggerRequest,
TaskRequest,
DeployRequest,
)
from eve.deploy import (
authenticate_modal_key,
check_environment_exists,
create_environment,
)
from eve import deploy
from eve.tools.comfyui_tool import convert_tasks2_to_tasks3


# Config and logging setup
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


db = os.getenv("DB", "STAGE").upper()
if db not in ["PROD", "STAGE"]:
raise Exception(f"Invalid environment: {db}. Must be PROD or STAGE")
app_name = "api-prod" if db == "PROD" else "api-stage"

logging.getLogger("ably").setLevel(logging.INFO if db != "PROD" else logging.WARNING)


# FastAPI setup
web_app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
from eve.api.handlers import handle_chat

# Startup
watch_thread = threading.Thread(target=convert_tasks2_to_tasks3, daemon=True)
watch_thread.start()
app.state.watch_thread = watch_thread

app.state.ably_client = AblyRealtime(
key=os.getenv("ABLY_PUBLISHER_KEY"),
)

# Load existing triggers
await load_existing_triggers(scheduler, app.state.ably_client, handle_chat)

yield
# Shutdown
if hasattr(app.state, "watch_thread"):
app.state.watch_thread.join(timeout=5)
if hasattr(app.state, "scheduler"):
app.state.scheduler.shutdown(wait=True)
if hasattr(app.state, "ably_client"):
await app.state.ably_client.close()


web_app = FastAPI(lifespan=lifespan)
web_app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


@web_app.on_event("startup")
async def startup_event():
# Setup
scheduler = BackgroundScheduler()
scheduler.start()
web_app.state.scheduler = scheduler
web_app.state.ably_client = AblyRealtime(os.getenv("ABLY_PUBLISHER_KEY"))


@web_app.on_event("shutdown")
async def shutdown_event():
# Cleanup
if hasattr(web_app.state, "ably_client"):
web_app.state.ably_client.close()
if hasattr(web_app.state, "scheduler"):
web_app.state.scheduler.shutdown()

scheduler = BackgroundScheduler()
scheduler.start()

api_key_header = APIKeyHeader(name="X-Api-Key", auto_error=False)
bearer_scheme = HTTPBearer(auto_error=False)
Expand Down Expand Up @@ -103,25 +122,32 @@ async def stream_chat(
return await handle_stream_chat(request, background_tasks)


@web_app.post("/deployment")
async def deployment(
request: DeployRequest, _: dict = Depends(auth.authenticate_admin)
@web_app.post("/deployments/create")
async def deployment_create(
request: CreateDeploymentRequest, _: dict = Depends(auth.authenticate_admin)
):
return await handle_deployment(request)
return await handle_deployment_create(request)


@web_app.post("/schedule")
async def schedule(
request: ScheduleRequest,
background_tasks: BackgroundTasks,
_: dict = Depends(auth.authenticate_admin),
@web_app.post("/deployments/delete")
async def deployment_delete(
request: DeleteDeploymentRequest, _: dict = Depends(auth.authenticate_admin)
):
return await handle_schedule(
request=request,
background_tasks=background_tasks,
scheduler=web_app.state.scheduler,
ably_client=web_app.state.ably_client,
)
return await handle_deployment_delete(request)


@web_app.post("/triggers/create")
async def trigger_create(
request: CreateTriggerRequest, _: dict = Depends(auth.authenticate_admin)
):
return await handle_trigger_create(request, scheduler, web_app.state.ably_client)


@web_app.post("/triggers/delete")
async def trigger_delete(
request: DeleteTriggerRequest, _: dict = Depends(auth.authenticate_admin)
):
return await handle_trigger_delete(request, scheduler)


# Modal app setup
Expand Down
Loading

0 comments on commit bf9fea9

Please sign in to comment.