backend: implement agents and connect them with chats

This commit is contained in:
Shivering Ocelot committed 2025-05-12 12:55:50 -05:00
1 parent e57e5ba16a
commit 49df72f2a1
9 files changed
+313 -40

No files matched your search

+9 -8
View File
@@ -1,6 +1,7 @@
from quart import Quart, Response, request, send_from_directory, websocket
import json
import asyncio
import nest_asyncio
def start_server(app):
server = Quart(__name__, static_folder='../frontend/build/web')
@@ -49,20 +50,18 @@ def start_server(app):
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:
app.add_user_connection(user_id, send_queue)
asyncio.create_task(sender_task())
while True:
req = json.loads(await websocket.receive())
action = req['action']
@@ -70,7 +69,6 @@ def start_server(app):
nonce = req.get('nonce', None)
if action == "ping":
print("ping")
await send({'message': "pong"})
elif action == "create-agent":
@@ -115,9 +113,12 @@ def start_server(app):
)
await send({'message': "message sent", 'messageId': message['id'], 'nonce': nonce})
else:
print("Unknown message: " + action)
elif action == "send-agent-prompt":
response = await app.send_agent_prompt(body['agentId'], body['content'])
await send({'message': "propmt sent", 'response': response, 'nonce': nonce})
else:
await send({'error': 'unknown action: ' + action})
finally:
app.remove_user_connection(user_id, send_queue)