@@ -122,7 +122,7 @@ def respond(self, run_code=None):
122122 if self .stop_event .is_set ():
123123 return
124124
125- if self .print or self . debug :
125+ if self .print :
126126 if "start" in chunk :
127127 print ("\n " )
128128 if chunk ["type" ] in ["code" , "console" ] and "format" in chunk :
@@ -140,12 +140,15 @@ def respond(self, run_code=None):
140140 )
141141 print (content , end = "" , flush = True )
142142
143+ if self .debug :
144+ print ("Interpreter produced this chunk:" , chunk )
145+
143146 self .output_queue .sync_q .put (chunk )
144147
145148 self .output_queue .sync_q .put (complete_message )
146149
147150 if self .print or self .debug :
148- print ("Server response complete." )
151+ print ("\n Server response complete.\n " )
149152
150153 except Exception as e :
151154 error = traceback .format_exc () + "\n " + str (e )
@@ -464,17 +467,23 @@ async def send_output():
464467 # First, try to send any unsent messages
465468 while async_interpreter .unsent_messages :
466469 output = async_interpreter .unsent_messages [0 ]
467- try :
468- await send_message (output )
470+ if async_interpreter .debug :
471+ print ("This was unsent, sending it again:" , output )
472+
473+ success = await send_message (output )
474+ if success :
469475 async_interpreter .unsent_messages .popleft ()
470- except Exception :
471- # If we can't send, break and try again later
472- break
473476
474477 # If we've sent all unsent messages, get a new output
475478 if not async_interpreter .unsent_messages :
476479 output = await async_interpreter .output ()
477- await send_message (output )
480+ success = await send_message (output )
481+ if not success :
482+ async_interpreter .unsent_messages .append (output )
483+ if async_interpreter .debug :
484+ print (
485+ f"Added message to unsent_messages queue after failed attempts: { output } "
486+ )
478487
479488 except Exception as e :
480489 error = traceback .format_exc () + "\n " + str (e )
@@ -506,16 +515,19 @@ async def send_message(output):
506515 # time.sleep(0.5)
507516
508517 if websocket .client_state != WebSocketState .CONNECTED :
509- break
518+ return False
510519
511520 try :
512521 # print("sending:", output)
513522
514523 if isinstance (output , bytes ):
515524 await websocket .send_bytes (output )
525+ return True # Haven't set up ack for this
516526 else :
517527 if async_interpreter .require_acknowledge :
518528 output ["id" ] = id
529+ if async_interpreter .debug :
530+ print ("Sending this over the websocket:" , output )
519531 await websocket .send_text (json .dumps (output ))
520532
521533 if async_interpreter .require_acknowledge :
@@ -524,31 +536,38 @@ async def send_message(output):
524536 if id in async_interpreter .acknowledged_outputs :
525537 async_interpreter .acknowledged_outputs .remove (id )
526538 acknowledged = True
539+ if async_interpreter .debug :
540+ print ("This output was acknowledged:" , output )
527541 break
528542 await asyncio .sleep (0.0001 )
529543
530544 if acknowledged :
531- return
545+ return True
532546 else :
533- raise Exception ("Acknowledgement not received." )
547+ if async_interpreter .debug :
548+ print ("Acknowledgement not received for:" , output )
549+ return False
534550 else :
535- return
551+ return True
536552
537553 except Exception as e :
538554 print (
539555 f"Failed to send output on attempt number: { attempt + 1 } . Output was: { output } "
540556 )
541557 print (f"Error: { str (e )} " )
542- await asyncio .sleep (0.05 )
558+ traceback .print_exc ()
559+ await asyncio .sleep (0.01 )
543560
544561 # If we've reached this point, we've failed to send after 100 attempts
545562 if output not in async_interpreter .unsent_messages :
546- async_interpreter .unsent_messages .append (output )
563+ print ("Failed to send message:" , output )
564+ else :
547565 print (
548- f"Added message to unsent_messages queue after failed attempts: { output } "
566+ "Failed to send message, also it was already in unsent queue???:" ,
567+ output ,
549568 )
550- else :
551- print ( "Why was this already in unsent_messages?" , output )
569+
570+ return False
552571
553572 await asyncio .gather (receive_input (), send_output ())
554573
@@ -577,7 +596,8 @@ async def post_input(payload: Dict[str, Any]):
577596 @router .post ("/settings" )
578597 async def set_settings (payload : Dict [str , Any ]):
579598 for key , value in payload .items ():
580- print (f"Updating settings: { key } = { value } " )
599+ print ("Updating settings..." )
600+ # print(f"Updating settings: {key} = {value}")
581601 if key in ["llm" , "computer" ] and isinstance (value , dict ):
582602 if key == "auto_run" :
583603 return {
0 commit comments