Spaces:
Running
Running
Commit ·
b5d5f21
1
Parent(s): 3df9df0
load full history
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ import os
|
|
| 4 |
from fastapi import FastAPI, Request, HTTPException
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from jose import jwt, JWTError
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from app_nn import run_chat_app # your Socrates logic
|
| 9 |
|
|
@@ -53,18 +55,49 @@ async def verify_jwt(auth_header: str) -> str:
|
|
| 53 |
|
| 54 |
@APP.post("/chat/send")
|
| 55 |
async def chat_send(req: Request):
|
| 56 |
-
# 1) Auth
|
| 57 |
user_id = await verify_jwt(req.headers.get("authorization"))
|
| 58 |
|
| 59 |
-
# 2) Request body
|
| 60 |
data = await req.json()
|
| 61 |
message = data.get("message", "")
|
| 62 |
username = data.get("username") or "Anonymous"
|
| 63 |
profile = data.get("profile") or {}
|
| 64 |
ui_lang = data.get("ui_lang", "en")
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
# 4) Return JSON
|
| 70 |
return {"reply": reply, "user_id": user_id}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from fastapi import FastAPI, Request, HTTPException
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from jose import jwt, JWTError
|
| 7 |
+
from supabase_ie import load_history_for_display
|
| 8 |
+
from datetime import datetime
|
| 9 |
|
| 10 |
from app_nn import run_chat_app # your Socrates logic
|
| 11 |
|
|
|
|
| 55 |
|
| 56 |
@APP.post("/chat/send")
|
| 57 |
async def chat_send(req: Request):
|
|
|
|
| 58 |
user_id = await verify_jwt(req.headers.get("authorization"))
|
| 59 |
|
|
|
|
| 60 |
data = await req.json()
|
| 61 |
message = data.get("message", "")
|
| 62 |
username = data.get("username") or "Anonymous"
|
| 63 |
profile = data.get("profile") or {}
|
| 64 |
ui_lang = data.get("ui_lang", "en")
|
| 65 |
|
| 66 |
+
reply = run_chat_app(
|
| 67 |
+
user_id=user_id,
|
| 68 |
+
username=username,
|
| 69 |
+
profile=profile,
|
| 70 |
+
ui_lang=ui_lang,
|
| 71 |
+
user_msg=message,
|
| 72 |
+
)
|
| 73 |
|
|
|
|
| 74 |
return {"reply": reply, "user_id": user_id}
|
| 75 |
+
|
| 76 |
+
@APP.post("/chat/history")
|
| 77 |
+
async def chat_history(req: Request):
|
| 78 |
+
auth_header = req.headers.get("authorization", "")
|
| 79 |
+
try:
|
| 80 |
+
user_id = await verify_jwt(auth_header)
|
| 81 |
+
except Exception:
|
| 82 |
+
raise HTTPException(status_code=401, detail="Invalid JWT")
|
| 83 |
+
|
| 84 |
+
if not user_id:
|
| 85 |
+
raise HTTPException(status_code=401, detail="Invalid JWT")
|
| 86 |
+
|
| 87 |
+
msgs = load_history_for_display(user_id=user_id) or []
|
| 88 |
+
|
| 89 |
+
safe_messages = []
|
| 90 |
+
for i, m in enumerate(msgs):
|
| 91 |
+
safe_messages.append({
|
| 92 |
+
"id": m.get("id") or f"{i}-{m.get('role','assistant')}",
|
| 93 |
+
"role": m.get("role", "assistant"),
|
| 94 |
+
"content": m.get("content", ""),
|
| 95 |
+
"time": (
|
| 96 |
+
m.get("time")
|
| 97 |
+
or m.get("timestamp")
|
| 98 |
+
or datetime.utcnow().isoformat()
|
| 99 |
+
),
|
| 100 |
+
})
|
| 101 |
+
|
| 102 |
+
return {"messages": safe_messages}
|
| 103 |
+
|