@@ -75,34 +75,42 @@ async def output(self):
7575 return await self .output_queue .async_q .get ()
7676
7777 def respond (self , run_code = None ):
78- if run_code == None :
79- run_code = self .auto_run
78+ try :
79+ if run_code == None :
80+ run_code = self .auto_run
8081
81- for chunk in self ._respond_and_store ():
82- if chunk ["type" ] == "confirmation" :
83- if run_code :
84- continue # We don't need to send out confirmation chunks on the server. I don't even like them.
85- else :
86- break
82+ for chunk in self ._respond_and_store ():
83+ if chunk ["type" ] == "confirmation" :
84+ if run_code :
85+ continue # We don't need to send out confirmation chunks on the server. I don't even like them.
86+ else :
87+ break
8788
88- if self .stop_event .is_set ():
89- return
89+ if self .stop_event .is_set ():
90+ return
9091
91- if self .print :
92- if "start" in chunk :
93- print ("\n " )
94- if chunk ["type" ] in ["code" , "console" ] and "format" in chunk :
92+ if self .print :
9593 if "start" in chunk :
96- print ("\n ------------\n \n ```" + chunk ["format" ], flush = True )
97- if "end" in chunk :
98- print ("\n ```\n \n ------------\n \n " , flush = True )
99- print (chunk .get ("content" , "" ), end = "" , flush = True )
100-
101- self .output_queue .sync_q .put (chunk )
102-
103- self .output_queue .sync_q .put (
104- {"role" : "server" , "type" : "status" , "content" : "complete" }
105- )
94+ print ("\n " )
95+ if chunk ["type" ] in ["code" , "console" ] and "format" in chunk :
96+ if "start" in chunk :
97+ print ("\n ------------\n \n ```" + chunk ["format" ], flush = True )
98+ if "end" in chunk :
99+ print ("\n ```\n \n ------------\n \n " , flush = True )
100+ print (chunk .get ("content" , "" ), end = "" , flush = True )
101+
102+ self .output_queue .sync_q .put (chunk )
103+
104+ self .output_queue .sync_q .put (
105+ {"role" : "server" , "type" : "status" , "content" : "complete" }
106+ )
107+ except Exception as e :
108+ error_message = {
109+ "role" : "server" ,
110+ "type" : "error" ,
111+ "content" : traceback .format_exc () + "\n " + str (e ),
112+ }
113+ self .output_queue .sync_q .put (error_message )
106114
107115 def accumulate (self , chunk ):
108116 """
@@ -202,13 +210,29 @@ async def send_output():
202210 finally :
203211 await websocket .close ()
204212
213+ # TODO
214+ @router .post ("/" )
215+ async def post_input (payload : Dict [str , Any ]):
216+ # This doesn't work, but something like this should exist
217+ query = payload .get ("query" )
218+ if not query :
219+ return {"error" : "Query is required." }, 400
220+ try :
221+ async_interpreter .input .put (query )
222+ return {"status" : "success" }
223+ except Exception as e :
224+ return {"error" : str (e )}, 500
225+
205226 @router .post ("/run" )
206227 async def run_code (payload : Dict [str , Any ]):
207228 language , code = payload .get ("language" ), payload .get ("code" )
208229 if not (language and code ):
209230 return {"error" : "Both 'language' and 'code' are required." }, 400
210231 try :
211- return {"output" : async_interpreter .computer .run (language , code )}
232+ print (f"Running { language } :" , code )
233+ output = async_interpreter .computer .run (language , code )
234+ print ("Output:" , output )
235+ return {"output" : output }
212236 except Exception as e :
213237 return {"error" : str (e )}, 500
214238
@@ -217,10 +241,20 @@ async def set_settings(payload: Dict[str, Any]):
217241 for key , value in payload .items ():
218242 print (f"Updating settings: { key } = { value } " )
219243 if key in ["llm" , "computer" ] and isinstance (value , dict ):
220- for sub_key , sub_value in value .items ():
221- setattr (getattr (async_interpreter , key ), sub_key , sub_value )
222- else :
244+ if hasattr (async_interpreter , key ):
245+ for sub_key , sub_value in value .items ():
246+ if hasattr (getattr (async_interpreter , key ), sub_key ):
247+ setattr (getattr (async_interpreter , key ), sub_key , sub_value )
248+ else :
249+ return {
250+ "error" : f"Sub-setting { sub_key } not found in { key } "
251+ }, 404
252+ else :
253+ return {"error" : f"Setting { key } not found" }, 404
254+ elif hasattr (async_interpreter , key ):
223255 setattr (async_interpreter , key , value )
256+ else :
257+ return {"error" : f"Setting { key } not found" }, 404
224258
225259 return {"status" : "success" }
226260
@@ -238,8 +272,14 @@ async def get_setting(setting: str):
238272 return router
239273
240274
275+ host = os .getenv (
276+ "HOST" , "127.0.0.1"
277+ ) # IP address for localhost, used for local testing
278+ port = int (os .getenv ("PORT" , 8000 )) # Default port is 8000
279+
280+
241281class Server :
242- def __init__ (self , async_interpreter , host = "0.0.0.0" , port = 8000 ):
282+ def __init__ (self , async_interpreter , host = host , port = port ):
243283 self .app = FastAPI ()
244284 router = create_router (async_interpreter )
245285 self .app .include_router (router )
0 commit comments