@@ -28,7 +28,7 @@ def __init__(self, *args, **kwargs):
28
28
self .stop_event = threading .Event ()
29
29
self .output_queue = None
30
30
self .id = os .getenv ("INTERPRETER_ID" , datetime .now ().timestamp ())
31
- self .print = False # Will print output
31
+ self .print = True # Will print output
32
32
33
33
self .server = Server (self )
34
34
@@ -38,8 +38,6 @@ async def input(self, chunk):
38
38
When it hits an "end" flag, calls interpreter.respond().
39
39
"""
40
40
41
- print ("Received:" , chunk )
42
-
43
41
if "start" in chunk :
44
42
# If the user is starting something, the interpreter should stop.
45
43
if self .respond_thread is not None and self .respond_thread .is_alive ():
@@ -107,7 +105,8 @@ def respond(self, run_code=None):
107
105
print ("\n ------------\n \n ```" + chunk ["format" ], flush = True )
108
106
if "end" in chunk :
109
107
print ("\n ```\n \n ------------\n \n " , flush = True )
110
- print (chunk .get ("content" , "" ), end = "" , flush = True )
108
+ if chunk .get ("format" ) != "active_line" :
109
+ print (chunk .get ("content" , "" ), end = "" , flush = True )
111
110
112
111
self .output_queue .sync_q .put (chunk )
113
112
@@ -176,6 +175,7 @@ async def home():
176
175
<textarea id="messageInput" rows="10" cols="50" autocomplete="off"></textarea>
177
176
<button>Send</button>
178
177
</form>
178
+ <button id="approveCodeButton">Approve Code</button>
179
179
<div id="messages"></div>
180
180
<script>
181
181
var ws = new WebSocket("ws://"""
@@ -191,47 +191,13 @@ async def home():
191
191
lastMessageElement.innerHTML = "<br>"
192
192
}
193
193
var eventData = JSON.parse(event.data);
194
- if (eventData.role == "assistant") {
195
- if (eventData.type == "message") {
196
- if (eventData.start) {
197
- lastMessageElement = document.createElement('p');
198
- document.getElementById('messages').appendChild(lastMessageElement);
199
- lastMessageElement.innerHTML = "<br>";
200
- }
201
- if (eventData.content) {
202
- lastMessageElement.innerHTML += eventData.content;
203
- }
204
- if (eventData.end) {
205
- lastMessageElement.innerHTML += "<br>";
206
- }
207
- } else if (eventData.type == "code") {
208
- if (eventData.start) {
209
- lastMessageElement = document.createElement('p');
210
- document.getElementById('messages').appendChild(lastMessageElement);
211
- lastMessageElement.innerHTML = "<br><br>```" + eventData.format + "<br>";
212
- }
213
- if (eventData.content) {
214
- lastMessageElement.innerHTML += eventData.content;
215
- }
216
- if (eventData.end) {
217
- lastMessageElement.innerHTML += "<br>```<br><br>";
218
- }
219
- }
220
- else if (eventData.role == "computer" && eventData.type == "console") {
221
- if (eventData.start) {
222
- lastMessageElement = document.createElement('p');
223
- document.getElementById('messages').appendChild(lastMessageElement);
224
- lastMessageElement.innerHTML = "<br><br>------<br><br>";
225
- }
226
- if (eventData.content && eventData.format == "output") {
227
- lastMessageElement.innerHTML += eventData.content;
228
- }
229
- if (eventData.end) {
230
- lastMessageElement.innerHTML += "<br><br>------<br><br>";
231
- }
232
- }
194
+
195
+ if ((eventData.role == "assistant" && eventData.type == "message" && eventData.content) ||
196
+ (eventData.role == "computer" && eventData.type == "console" && eventData.format == "output" && eventData.content) ||
197
+ (eventData.role == "assistant" && eventData.type == "code" && eventData.content)) {
198
+ lastMessageElement.innerHTML += eventData.content;
233
199
} else {
234
- lastMessageElement.innerHTML += "<br>" + JSON.stringify(eventData) + "<br>";
200
+ lastMessageElement.innerHTML += "<br><br> " + JSON.stringify(eventData) + "<br> <br>";
235
201
}
236
202
};
237
203
function sendMessage(event) {
@@ -270,6 +236,30 @@ async def home():
270
236
document.getElementById('messages').appendChild(lastMessageElement);
271
237
input.value = '';
272
238
}
239
+ function approveCode() {
240
+ var startCommandBlock = {
241
+ "role": "user",
242
+ "type": "command",
243
+ "start": true
244
+ };
245
+ ws.send(JSON.stringify(startCommandBlock));
246
+
247
+ var commandBlock = {
248
+ "role": "user",
249
+ "type": "command",
250
+ "content": "go"
251
+ };
252
+ ws.send(JSON.stringify(commandBlock));
253
+
254
+ var endCommandBlock = {
255
+ "role": "user",
256
+ "type": "command",
257
+ "end": true
258
+ };
259
+ ws.send(JSON.stringify(endCommandBlock));
260
+ }
261
+
262
+ document.getElementById("approveCodeButton").addEventListener("click", approveCode);
273
263
</script>
274
264
</body>
275
265
</html>
@@ -287,7 +277,7 @@ async def receive_input():
287
277
try :
288
278
data = await websocket .receive ()
289
279
290
- print ("RECIEVED :" , data )
280
+ print ("Received :" , data )
291
281
292
282
if data .get ("type" ) == "websocket.receive" and "text" in data :
293
283
data = json .loads (data ["text" ])
@@ -315,14 +305,30 @@ async def send_output():
315
305
while True :
316
306
try :
317
307
output = await async_interpreter .output ()
318
-
319
- print ("SENDING" , output )
320
-
321
- if isinstance (output , bytes ):
322
- await websocket .send_bytes (output )
308
+ # print("Attempting to send the following output:", output)
309
+
310
+ for attempt in range (100 ):
311
+ try :
312
+ if isinstance (output , bytes ):
313
+ await websocket .send_bytes (output )
314
+ else :
315
+ await websocket .send_text (json .dumps (output ))
316
+ # print("Output sent successfully. Output was:", output)
317
+ break
318
+ except Exception as e :
319
+ print (
320
+ "Failed to send output on attempt number:" ,
321
+ attempt + 1 ,
322
+ ". Output was:" ,
323
+ output ,
324
+ )
325
+ print ("Error:" , str (e ))
326
+ await asyncio .sleep (0.05 )
323
327
else :
324
- # output["time"] = time.time()
325
- await websocket .send_text (json .dumps (output ))
328
+ raise Exception (
329
+ "Failed to send after 100 attempts. Output was:" ,
330
+ str (output ),
331
+ )
326
332
except Exception as e :
327
333
error = traceback .format_exc () + "\n " + str (e )
328
334
error_message = {
@@ -422,7 +428,7 @@ async def get_setting(setting: str):
422
428
port = int (os .getenv ("PORT" , 8000 )) # Default port is 8000
423
429
424
430
# FOR TESTING ONLY
425
- # host = "0.0.0.0"
431
+ host = "0.0.0.0"
426
432
427
433
428
434
class Server :
0 commit comments