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