Spaces:
Sleeping
Sleeping
File size: 4,584 Bytes
e44f8be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | from __future__ import annotations
import os
from typing import Any, Dict, Optional
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from models import Action, UrgencyLevel, EmailCategory, EmailAction
from environment import EmailTriageEnv
# βββ App setup βββββββββββββββββββββββββββββββββββββββββββββ
app = FastAPI(
title="OpenEnv Email Triage",
version="1.0.0",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
env = EmailTriageEnv()
STATIC_DIR = os.path.join(os.path.dirname(__file__), "static")
if os.path.isdir(STATIC_DIR):
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
# βββ Endpoints βββββββββββββββββββββββββββββββββββββββββββββ
@app.get("/health")
async def health():
return {"status": "healthy"}
@app.get("/metadata")
async def metadata():
return {
"name": "OpenEnv Email Triage",
"description": "AI-powered email triage environment that classifies emails by urgency, category, and action."
}
@app.post("/mcp")
async def mcp():
return {
"jsonrpc": "2.0",
"result": {
"message": "MCP endpoint active"
},
"id": 1
}
@app.get("/schema")
async def schema():
return {
"action": {
"urgency": [e.value for e in UrgencyLevel],
"category": [e.value for e in EmailCategory],
"action": [e.value for e in EmailAction],
"draft_reply": "string (optional)",
"forward_to": "string (optional)",
"reasoning": "string (optional)"
},
"observation": {
"current_email": "object",
"done": "boolean",
"info": "object"
},
"state": {
"emails_processed": "int",
"current_step": "int",
"task_id": "string"
}
}
# β
FIXED RESET (IMPORTANT)
@app.post("/reset")
async def reset(request: Request):
try:
body = await request.json()
task_id = body.get("task_id", "task_easy") if body else "task_easy"
except:
task_id = "task_easy"
obs = env.reset(task_id=task_id)
return obs.model_dump()
# βββ STEP ENDPOINT βββββββββββββββββββββββββββββββββββββββββ
@app.post("/step")
async def step(request: Request):
try:
data = await request.json()
urgency = UrgencyLevel(data.get("urgency", "medium"))
category = EmailCategory(data.get("category", "other"))
action = EmailAction(data.get("action", "archive"))
act = Action(
urgency=urgency,
category=category,
action=action,
draft_reply=data.get("draft_reply"),
forward_to=data.get("forward_to"),
reasoning=data.get("reasoning"),
)
result = env.step(act)
return result.model_dump()
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
# βββ OTHER ENDPOINTS βββββββββββββββββββββββββββββββββββββββ
@app.get("/state")
async def state():
return env.state().model_dump()
@app.get("/tasks")
async def list_tasks():
return {
"tasks": [
{
"id": "task_easy",
"name": "Binary Spam Detection",
"difficulty": "easy",
"grader": "grade_task_easy",
},
{
"id": "task_medium",
"name": "Priority Inbox Triage",
"difficulty": "medium",
"grader": "grade_task_medium",
},
{
"id": "task_hard",
"name": "Full Triage with Response Drafting",
"difficulty": "hard",
"grader": "grade_task_hard",
},
]
}
@app.get("/")
async def root():
index_path = os.path.join(STATIC_DIR, "index.html")
if os.path.exists(index_path):
return FileResponse(index_path)
return {"message": "OpenEnv Email Triage API running"}
|