From 08cfa9e52347708a10b84a96b71eb875052b0892 Mon Sep 17 00:00:00 2001 From: killian <63927363+KillianLucas@users.noreply.github.com> Date: Sat, 10 Feb 2024 03:01:41 -0800 Subject: [PATCH] Check for computer messages --- OS/01/conversations/user.json | 2 +- OS/01/device.py | 2 +- OS/01/server.py | 27 ++++++++++++++++++++++----- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/OS/01/conversations/user.json b/OS/01/conversations/user.json index f0d4e3e..88bff71 100644 --- a/OS/01/conversations/user.json +++ b/OS/01/conversations/user.json @@ -1 +1 @@ -[{"role": "user", "type": "message", "content": "This is a microphone. I also have an extra microphone.\n"}] \ No newline at end of file +[{"role": "user", "type": "message", "content": "There's no fence, but it's like independent units.\n"}] \ No newline at end of file diff --git a/OS/01/device.py b/OS/01/device.py index 176c501..8d675e1 100644 --- a/OS/01/device.py +++ b/OS/01/device.py @@ -177,7 +177,7 @@ async def websocket_communication(WS_URL): send_queue.put(result) except: - traceback.print_exc() + # traceback.print_exc() print(f"Connecting to `{WS_URL}`...") await asyncio.sleep(2) diff --git a/OS/01/server.py b/OS/01/server.py index 48b3582..eaeae28 100644 --- a/OS/01/server.py +++ b/OS/01/server.py @@ -112,10 +112,17 @@ async def send_messages(websocket: WebSocket): print("Sending to the device:", type(message), message) await websocket.send_json(message) -async def user_listener(): +async def listener(): audio_bytes = bytearray() while True: - message = await from_user.get() + while True: + if not from_user.empty(): + message = await from_user.get() + break + elif not from_computer.empty(): + message = from_computer.get() + break + await asyncio.sleep(1) if type(message) == str: message = json.loads(message) @@ -188,8 +195,18 @@ async def user_listener(): with open(conversation_history_path, 'w') as file: json.dump(interpreter.messages, file) - print("New message recieved. Breaking.") + print("New user message recieved. Breaking.") + break + + # Also check if there's any new computer messages + if not from_computer.empty(): + + with open(conversation_history_path, 'w') as file: + json.dump(interpreter.messages, file) + + print("New computer message recieved. Breaking.") break + async def stream_or_play_tts(sentence): @@ -208,8 +225,8 @@ from uvicorn import Config, Server if __name__ == "__main__": async def main(): - # Start listening to the user - asyncio.create_task(user_listener()) + # Start listening + asyncio.create_task(listener()) # Start watching the kernel if it's your job to do that if os.getenv('CODE_RUNNER') == "server":