Spaces:
Sleeping
Sleeping
File size: 3,600 Bytes
bd195b4 713c6eb | 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 | from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse, JSONResponse
from pydantic import BaseModel
import os
import glob
import uvicorn
import sys
# 1. PATH FIX: Ensure the root directory is in the system path
# This allows 'server' to find 'agents', 'utility', and 'tools'
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if root_path not in sys.path:
sys.path.append(root_path)
# 2. IMPORT FIX
try:
# Try importing as a sibling (standard for 'python server/app.py')
import group_chat
user, manager, notification_agent = group_chat.user, group_chat.manager, group_chat.notification_agent
except (ImportError, ModuleNotFoundError):
# Try importing via the server package
from server.group_chat import user, manager, notification_agent
app = FastAPI()
# --- SECURITY FIX ---
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# --- AUTOMATED JUDGE RESET ENDPOINT ---
@app.post("/reset")
def reset():
try:
if hasattr(user, 'clear_history'): user.clear_history()
if hasattr(manager, 'clear_history'): manager.clear_history()
if hasattr(notification_agent, 'clear_history'): notification_agent.clear_history()
# Look for scripts in the root directory (one level up from /server)
for ext in ["/*.bat", "/*.sh", "/*.txt"]:
for filepath in glob.glob(root_path + ext):
try: os.remove(filepath)
except: pass
return JSONResponse({"status": "reset successful"})
except Exception as e:
return JSONResponse({"status": "error", "details": str(e)})
# --- HUMAN UI ENDPOINTS ---
static_path = os.path.join(root_path, "static")
if os.path.exists(static_path):
app.mount("/static", StaticFiles(directory=static_path), name="static")
@app.get("/")
def serve_ui():
index_path = os.path.join(static_path, "index.html")
if os.path.exists(index_path):
return FileResponse(index_path)
return JSONResponse({"error": "Frontend files not found"}, status_code=404)
# --- AI COMMUNICATION ENDPOINTS ---
class ChatRequest(BaseModel):
message: str
@app.post("/chat")
def chat_with_mtor(request: ChatRequest):
# This now calls your actual agent logic
try:
# We use a helper to get the response from your agents
user.initiate_chat(recipient=manager, message=request.message, clear_history=False)
# Get the last message from the chat history
last_msg = manager.last_message()["content"]
return {"reply": last_msg}
except Exception as e:
return {"reply": f"Error: {str(e)}"}
class EscalateRequest(BaseModel):
issue: str
ticket_id: str
@app.post("/escalate")
def escalate_issue(request: EscalateRequest):
notification_message = (
f"🚨 Unresolved IT Issue\n\n"
f"User reported: '{request.issue}'\n"
f"📄 Ticket ID: {request.ticket_id}"
)
reply = notification_agent.generate_reply(
messages=[{"role": "user", "content": notification_message}],
sender=user
)
final_reply = reply.get("content") if isinstance(reply, dict) else str(reply)
return {"reply": final_reply}
# --- ENTRY POINT ---
def main():
# Inside the container, we run from the root
uvicorn.run("server.app:app", host="0.0.0.0", port=8000, reload=False)
if __name__ == "__main__":
main() |