Spaces:
Runtime error
Runtime error
Update api.py
Browse files
api.py
CHANGED
@@ -1,13 +1,18 @@
|
|
|
|
|
|
|
|
1 |
|
2 |
-
from fastapi import FastAPI, Query
|
3 |
-
from fastapi.responses import PlainTextResponse
|
4 |
from agent import run_agent
|
5 |
from goal_manager import save_goal, list_goals
|
6 |
from feedback import list_feedback
|
7 |
-
import
|
|
|
|
|
|
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
|
|
11 |
@app.get("/run_task", response_class=PlainTextResponse)
|
12 |
def run_task(task: str = Query(..., description="Task for the AI to perform")):
|
13 |
return run_agent(task)
|
@@ -25,20 +30,18 @@ def show_goals():
|
|
25 |
def show_feedback():
|
26 |
return list_feedback()
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
from agent import run_agent
|
39 |
-
|
40 |
-
app.mount("/static", StaticFiles(directory="."), name="static")
|
41 |
|
|
|
42 |
@app.get("/portal", response_class=HTMLResponse)
|
43 |
async def client_portal():
|
44 |
with open("client_portal.html") as f:
|
@@ -54,18 +57,11 @@ async def dashboard():
|
|
54 |
with open("dashboard.html") as f:
|
55 |
return f.read()
|
56 |
|
|
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
@app.post("/session_task")
|
63 |
-
def session_task(goal: str, x_api_key: str = Header(...), session_id: str = ""):
|
64 |
-
user = authorize(x_api_key)
|
65 |
-
if not user:
|
66 |
-
raise HTTPException(status_code=401, detail="Invalid API key")
|
67 |
-
if not session_id:
|
68 |
-
session_id = create_session(user)
|
69 |
-
output = run_agent(goal)
|
70 |
-
log_interaction(session_id, goal, output)
|
71 |
-
return {"session_id": session_id, "output": output}
|
|
|
1 |
+
from fastapi import FastAPI, Query, Form, Header, HTTPException
|
2 |
+
from fastapi.responses import PlainTextResponse, HTMLResponse
|
3 |
+
from fastapi.staticfiles import StaticFiles
|
4 |
|
|
|
|
|
5 |
from agent import run_agent
|
6 |
from goal_manager import save_goal, list_goals
|
7 |
from feedback import list_feedback
|
8 |
+
from session import log_interaction, create_session
|
9 |
+
from auth import authorize
|
10 |
+
from webhook import router as webhook_router
|
11 |
+
from storage import save_goal
|
12 |
|
13 |
app = FastAPI()
|
14 |
|
15 |
+
# === API Routes ===
|
16 |
@app.get("/run_task", response_class=PlainTextResponse)
|
17 |
def run_task(task: str = Query(..., description="Task for the AI to perform")):
|
18 |
return run_agent(task)
|
|
|
30 |
def show_feedback():
|
31 |
return list_feedback()
|
32 |
|
33 |
+
@app.post("/session_task")
|
34 |
+
def session_task(goal: str, x_api_key: str = Header(...), session_id: str = ""):
|
35 |
+
user = authorize(x_api_key)
|
36 |
+
if not user:
|
37 |
+
raise HTTPException(status_code=401, detail="Invalid API key")
|
38 |
+
if not session_id:
|
39 |
+
session_id = create_session(user)
|
40 |
+
output = run_agent(goal)
|
41 |
+
log_interaction(session_id, goal, output)
|
42 |
+
return {"session_id": session_id, "output": output}
|
|
|
|
|
|
|
43 |
|
44 |
+
# === Web + HTML Routes ===
|
45 |
@app.get("/portal", response_class=HTMLResponse)
|
46 |
async def client_portal():
|
47 |
with open("client_portal.html") as f:
|
|
|
57 |
with open("dashboard.html") as f:
|
58 |
return f.read()
|
59 |
|
60 |
+
# === Static & External Routers ===
|
61 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
62 |
+
app.include_router(webhook_router)
|
63 |
|
64 |
+
# === Dev server (for local testing) ===
|
65 |
+
if __name__ == "__main__":
|
66 |
+
import uvicorn
|
67 |
+
uvicorn.run("api:app", host="0.0.0.0", port=7861, reload=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|