from quart import Quart, Response, request, send_from_directory, websocket import json import asyncio def start_server(app): server = Quart(__name__, static_folder='../frontend/build/web') @server.route('/') async def index(): return await send_from_directory(server.static_folder, 'index.html') @server.route('/') async def serve_static(path): return await send_from_directory(server.static_folder, path) @server.route('/user/signup', methods=['POST']) async def signup(): data = await request.get_json() user_id = None username = data['username'] email = data['email'] password = data['password'] user_id = app.create_new_user(username, email, password) if user_id is None: return {"error": "existing credentials"} app.generate_default_user_data(user_id) # token = auth_manager.dump_token(user_id) return {"userId": user_id} @server.websocket('/ws') async def ws(): print("incoming ws") # Authenticate before dedicating any resources auth_req = json.loads(await websocket.receive()) if "username" not in auth_req or "password" not in auth_req: print("Invalid auth request:", auth_req) await websocket.send(json.dumps({'error': 'invalid auth'})) return user_id = app.verify_user_credentials(auth_req['username'], auth_req['password']) if user_id is None: await websocket.send(json.dumps({'error': 'invalid credentials'})) return send_queue = asyncio.Queue() app.add_user_connection(user_id, send_queue) async def sender_task(): nonlocal send_queue while True: msg = await send_queue.get() await websocket.send(json.dumps(msg)) async def send(msg): await send_queue.put(msg) asyncio.create_task(sender_task()) try: while True: req = json.loads(await websocket.receive()) action = req['action'] body = req.get('body', {}) nonce = req.get('nonce', None) if action == "ping": print("ping") await send({'message': "pong"}) elif action == "create-agent": agent_id = app.create_agent( user_id=user_id, name=body['name'], description=body['description'], picture=body.get('picture'), kind=body['kind'], data=body['data'] ) await send({'message': "agent created", 'agentId': agent_id, 'nonce': nonce}) elif action == "get-agents": agents_data = app.get_agents(user_id) await send({'message': "agents retrieved", 'agents': agents_data, 'nonce': nonce}) elif action == "create-chat": chat_id = app.create_chat( user_id=user_id, name=body['name'], description=body.get('description'), picture=body.get('picture') ) await send({'message': "chat created", 'chatId': chat_id, 'nonce': nonce}) elif action == "get-chats": chats_data = app.get_chats(user_id) await send({'message': "chats retrieved", 'chats': chats_data, 'nonce': nonce}) elif action == "get-chat-messages": messages_data = app.get_chat_messages( body['chatId'], earliest_index=body.get('earliestIndex', 0) ) await send({'message': "chat messages retrieved", 'messages': messages_data, 'nonce': nonce}) elif action == "send-chat-message": message = app.send_chat_message( chat_id=body['chatId'], content=body['content'], ) await send({'message': "message sent", 'messageId': message['id'], 'nonce': nonce}) else: print("Unknown message: " + action) finally: app.remove_user_connection(user_id, send_queue) server.run(debug=True)