1
1
import base64
2
+ import dill # type: ignore
3
+ import json
2
4
import requests
3
5
import os
4
6
import time
@@ -192,12 +194,12 @@ def tool_exec(self, tool: str, code: str, poll_interval: int = 3, debug: bool =
192
194
tool_name = tool .strip ()
193
195
code_string = code
194
196
195
- output = self ._ax_client .tools .schedule (
197
+ tool_result = self ._ax_client .tools .schedule (
196
198
tool_name = tool_name ,
197
199
code = code_string ,
198
200
)
199
- if output .is_success is True :
200
- job_id = str (output .job_id )
201
+ if tool_result .is_success is True :
202
+ job_id = str (tool_result .job_id )
201
203
result = self ._ax_client .tools .status (job_id = job_id )
202
204
if debug :
203
205
print (f"job_id: { job_id } " )
@@ -211,11 +213,41 @@ def tool_exec(self, tool: str, code: str, poll_interval: int = 3, debug: bool =
211
213
if debug :
212
214
print (f"status: { result .status } " )
213
215
if result .status == "SUCCEEDED" :
214
- return result .output
216
+ output = json .loads (result .output or "{}" )
217
+ if not output ['objects' ]:
218
+ return result .output
219
+ else :
220
+ return {
221
+ "messages" : output ['messages' ],
222
+ "objects" : self ._load_objects_from_base64 (output ['objects' ])
223
+ }
215
224
else :
216
225
return result .error_trace
217
226
else :
218
- return output .error_trace
227
+ return tool_result .error_trace
228
+
229
+ def load (self , job_id : str , obj_key : str ):
230
+ result = self ._ax_client .tools .status (job_id = job_id )
231
+ if result .status == "SUCCEEDED" :
232
+ output = json .loads (result .output or "{}" )
233
+ if not output ['objects' ]:
234
+ return result .output
235
+ else :
236
+ return self ._load_objects_from_base64 (output ['objects' ])[obj_key ]
237
+ else :
238
+ return result .error_trace
239
+
240
+ def _load_objects_from_base64 (self , encoded_dict ):
241
+ loaded_objects = {}
242
+ for key , encoded_str in encoded_dict .items ():
243
+ try :
244
+ decoded_bytes = base64 .b64decode (encoded_str )
245
+ loaded_obj = dill .loads (decoded_bytes )
246
+ loaded_objects [key ] = loaded_obj
247
+ except Exception as e :
248
+ print (f"Error loading object for key '{ key } ': { e } " )
249
+ loaded_objects [key ] = None
250
+ return loaded_objects
219
251
220
252
221
253
class AsyncAxiomatic (AsyncBaseClient ): ...
0 commit comments