@@ -122,7 +122,7 @@ def respond(self, run_code=None):
122
122
if self .stop_event .is_set ():
123
123
return
124
124
125
- if self .print or self . debug :
125
+ if self .print :
126
126
if "start" in chunk :
127
127
print ("\n " )
128
128
if chunk ["type" ] in ["code" , "console" ] and "format" in chunk :
@@ -140,12 +140,15 @@ def respond(self, run_code=None):
140
140
)
141
141
print (content , end = "" , flush = True )
142
142
143
+ if self .debug :
144
+ print ("Interpreter produced this chunk:" , chunk )
145
+
143
146
self .output_queue .sync_q .put (chunk )
144
147
145
148
self .output_queue .sync_q .put (complete_message )
146
149
147
150
if self .print or self .debug :
148
- print ("Server response complete." )
151
+ print ("\n Server response complete.\n " )
149
152
150
153
except Exception as e :
151
154
error = traceback .format_exc () + "\n " + str (e )
@@ -464,17 +467,23 @@ async def send_output():
464
467
# First, try to send any unsent messages
465
468
while async_interpreter .unsent_messages :
466
469
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 :
469
475
async_interpreter .unsent_messages .popleft ()
470
- except Exception :
471
- # If we can't send, break and try again later
472
- break
473
476
474
477
# If we've sent all unsent messages, get a new output
475
478
if not async_interpreter .unsent_messages :
476
479
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
+ )
478
487
479
488
except Exception as e :
480
489
error = traceback .format_exc () + "\n " + str (e )
@@ -506,16 +515,19 @@ async def send_message(output):
506
515
# time.sleep(0.5)
507
516
508
517
if websocket .client_state != WebSocketState .CONNECTED :
509
- break
518
+ return False
510
519
511
520
try :
512
521
# print("sending:", output)
513
522
514
523
if isinstance (output , bytes ):
515
524
await websocket .send_bytes (output )
525
+ return True # Haven't set up ack for this
516
526
else :
517
527
if async_interpreter .require_acknowledge :
518
528
output ["id" ] = id
529
+ if async_interpreter .debug :
530
+ print ("Sending this over the websocket:" , output )
519
531
await websocket .send_text (json .dumps (output ))
520
532
521
533
if async_interpreter .require_acknowledge :
@@ -524,31 +536,38 @@ async def send_message(output):
524
536
if id in async_interpreter .acknowledged_outputs :
525
537
async_interpreter .acknowledged_outputs .remove (id )
526
538
acknowledged = True
539
+ if async_interpreter .debug :
540
+ print ("This output was acknowledged:" , output )
527
541
break
528
542
await asyncio .sleep (0.0001 )
529
543
530
544
if acknowledged :
531
- return
545
+ return True
532
546
else :
533
- raise Exception ("Acknowledgement not received." )
547
+ if async_interpreter .debug :
548
+ print ("Acknowledgement not received for:" , output )
549
+ return False
534
550
else :
535
- return
551
+ return True
536
552
537
553
except Exception as e :
538
554
print (
539
555
f"Failed to send output on attempt number: { attempt + 1 } . Output was: { output } "
540
556
)
541
557
print (f"Error: { str (e )} " )
542
- await asyncio .sleep (0.05 )
558
+ traceback .print_exc ()
559
+ await asyncio .sleep (0.01 )
543
560
544
561
# If we've reached this point, we've failed to send after 100 attempts
545
562
if output not in async_interpreter .unsent_messages :
546
- async_interpreter .unsent_messages .append (output )
563
+ print ("Failed to send message:" , output )
564
+ else :
547
565
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 ,
549
568
)
550
- else :
551
- print ( "Why was this already in unsent_messages?" , output )
569
+
570
+ return False
552
571
553
572
await asyncio .gather (receive_input (), send_output ())
554
573
@@ -577,7 +596,8 @@ async def post_input(payload: Dict[str, Any]):
577
596
@router .post ("/settings" )
578
597
async def set_settings (payload : Dict [str , Any ]):
579
598
for key , value in payload .items ():
580
- print (f"Updating settings: { key } = { value } " )
599
+ print ("Updating settings..." )
600
+ # print(f"Updating settings: {key} = {value}")
581
601
if key in ["llm" , "computer" ] and isinstance (value , dict ):
582
602
if key == "auto_run" :
583
603
return {
0 commit comments