|  |  |  | @ -4,6 +4,7 @@ import json | 
			
		
	
		
			
				
					|  |  |  |  | import time | 
			
		
	
		
			
				
					|  |  |  |  | import queue | 
			
		
	
		
			
				
					|  |  |  |  | import os | 
			
		
	
		
			
				
					|  |  |  |  | import logging | 
			
		
	
		
			
				
					|  |  |  |  | import traceback | 
			
		
	
		
			
				
					|  |  |  |  | from queue import Queue | 
			
		
	
		
			
				
					|  |  |  |  | from threading import Thread | 
			
		
	
	
		
			
				
					|  |  |  | @ -22,6 +23,9 @@ from utils.kernel import put_kernel_messages_into_queue | 
			
		
	
		
			
				
					|  |  |  |  | from i import configure_interpreter | 
			
		
	
		
			
				
					|  |  |  |  | from interpreter import interpreter | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | # Configure logging | 
			
		
	
		
			
				
					|  |  |  |  | logging.basicConfig(format='%(message)s', level=logging.getLevelName(os.getenv('DEBUG_LEVEL', 'INFO').upper())) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | app = FastAPI() | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | conversation_history_path = Path(__file__).parent / 'conversations' / 'user.json' | 
			
		
	
	
		
			
				
					|  |  |  | @ -64,10 +68,10 @@ if os.getenv('CODE_RUNNER') == "device": | 
			
		
	
		
			
				
					|  |  |  |  |                 to_device.put({"role": "assistant", "type": "code", "format": "python", "end": True}) | 
			
		
	
		
			
				
					|  |  |  |  |              | 
			
		
	
		
			
				
					|  |  |  |  |             # Stream the response | 
			
		
	
		
			
				
					|  |  |  |  |             print("Waiting for the device to respond...") | 
			
		
	
		
			
				
					|  |  |  |  |             logging.info("Waiting for the device to respond...") | 
			
		
	
		
			
				
					|  |  |  |  |             while True: | 
			
		
	
		
			
				
					|  |  |  |  |                 chunk = from_computer.get() | 
			
		
	
		
			
				
					|  |  |  |  |                 print("Server recieved from device:", chunk) | 
			
		
	
		
			
				
					|  |  |  |  |                 logging.info(f"Server received from device: {chunk}") | 
			
		
	
		
			
				
					|  |  |  |  |                 if "end" in chunk: | 
			
		
	
		
			
				
					|  |  |  |  |                     break | 
			
		
	
		
			
				
					|  |  |  |  |                 yield chunk | 
			
		
	
	
		
			
				
					|  |  |  | @ -94,7 +98,7 @@ async def websocket_endpoint(websocket: WebSocket): | 
			
		
	
		
			
				
					|  |  |  |  |         await asyncio.gather(receive_task, send_task) | 
			
		
	
		
			
				
					|  |  |  |  |     except Exception as e: | 
			
		
	
		
			
				
					|  |  |  |  |         traceback.print_exc() | 
			
		
	
		
			
				
					|  |  |  |  |         print(f"Connection lost. Error: {e}") | 
			
		
	
		
			
				
					|  |  |  |  |         logging.info(f"Connection lost. Error: {e}") | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | async def receive_messages(websocket: WebSocket): | 
			
		
	
		
			
				
					|  |  |  |  |     while True: | 
			
		
	
	
		
			
				
					|  |  |  | @ -109,7 +113,7 @@ async def receive_messages(websocket: WebSocket): | 
			
		
	
		
			
				
					|  |  |  |  | async def send_messages(websocket: WebSocket): | 
			
		
	
		
			
				
					|  |  |  |  |     while True: | 
			
		
	
		
			
				
					|  |  |  |  |         message = await to_device.get() | 
			
		
	
		
			
				
					|  |  |  |  |         print("Sending to the device:", type(message), message) | 
			
		
	
		
			
				
					|  |  |  |  |         logging.debug(f"Sending to the device: {type(message)} {message}") | 
			
		
	
		
			
				
					|  |  |  |  |         await websocket.send_json(message) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | async def listener(): | 
			
		
	
	
		
			
				
					|  |  |  | @ -159,7 +163,7 @@ async def listener(): | 
			
		
	
		
			
				
					|  |  |  |  |          | 
			
		
	
		
			
				
					|  |  |  |  |         for chunk in interpreter.chat(messages, stream=True, display=False): | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             print("Got chunk:", chunk) | 
			
		
	
		
			
				
					|  |  |  |  |             logging.debug("Got chunk:", chunk) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             # Send it to the user | 
			
		
	
		
			
				
					|  |  |  |  |             await to_device.put(chunk) | 
			
		
	
	
		
			
				
					|  |  |  | @ -195,7 +199,7 @@ async def listener(): | 
			
		
	
		
			
				
					|  |  |  |  |                 with open(conversation_history_path, 'w') as file: | 
			
		
	
		
			
				
					|  |  |  |  |                     json.dump(interpreter.messages, file) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |                 print("New user message recieved. Breaking.") | 
			
		
	
		
			
				
					|  |  |  |  |                 logging.info("New user message recieved. Breaking.") | 
			
		
	
		
			
				
					|  |  |  |  |                 break | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             # Also check if there's any new computer messages | 
			
		
	
	
		
			
				
					|  |  |  | @ -204,7 +208,7 @@ async def listener(): | 
			
		
	
		
			
				
					|  |  |  |  |                 with open(conversation_history_path, 'w') as file: | 
			
		
	
		
			
				
					|  |  |  |  |                     json.dump(interpreter.messages, file) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |                 print("New computer message recieved. Breaking.") | 
			
		
	
		
			
				
					|  |  |  |  |                 logging.info("New computer message recieved. Breaking.") | 
			
		
	
		
			
				
					|  |  |  |  |                 break | 
			
		
	
		
			
				
					|  |  |  |  |              | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
	
		
			
				
					|  |  |  | @ -236,7 +240,7 @@ if __name__ == "__main__": | 
			
		
	
		
			
				
					|  |  |  |  |         if not server_url: | 
			
		
	
		
			
				
					|  |  |  |  |             raise ValueError("The environment variable SERVER_URL is not set. Please set it to proceed.") | 
			
		
	
		
			
				
					|  |  |  |  |         parsed_url = urllib.parse.urlparse(server_url) | 
			
		
	
		
			
				
					|  |  |  |  |         print("Starting `server.py`...") | 
			
		
	
		
			
				
					|  |  |  |  |         logging.info("Starting `server.py`...") | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         config = Config(app, host=parsed_url.hostname, port=parsed_url.port, lifespan='on') | 
			
		
	
		
			
				
					|  |  |  |  |         server = Server(config) | 
			
		
	
	
		
			
				
					|  |  |  | 
 |