Skip to content

Commit 7f68d85

Browse files
Merge pull request #35 from Axiomatic-AI/update-toos-magic
Update tools magic to handle objects
2 parents e389a8c + cbce5a8 commit 7f68d85

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/axiomatic/magic.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import os
66
import sys
77
import time
8+
import json
9+
import base64
10+
import dill # type: ignore
811
from . import Axiomatic
912

1013

@@ -92,14 +95,6 @@ def ax_tool(self, tool, cell):
9295
9396
This cell won't run as Python code; instead, the text will be sent to the tool_schedule method
9497
of the Axiomatic client.
95-
96-
Available Tools:
97-
- fdtd
98-
- femwell
99-
- jaxfem
100-
- optiland
101-
- pyspice
102-
- sax-gdsfactory
10398
"""
10499
if not tool.strip():
105100
print("Please provider a tool name when calling this magic like: %%tool_schedule [optional_tool_name]")
@@ -126,14 +121,33 @@ def ax_tool(self, tool, cell):
126121
else:
127122
if result.status == "SUCCEEDED":
128123
os.environ["TOOL_RESULT"] = result.output
129-
get_ipython().user_ns["tool_result"] = result.output
130-
print(result.output)
124+
output = json.loads(result.output)
125+
if not output['objects']:
126+
get_ipython().user_ns["tool_result"] = output
127+
else:
128+
get_ipython().user_ns["tool_result"] = {
129+
"messages": output['messages'],
130+
"objects": self._load_objects_from_base64(output['objects'])
131+
}
132+
print("SUCCEEDED: access the execution result with tool_result variable.")
131133
else:
132134
print(result.error_trace)
133135
break
134136
else:
135137
print(output.error_trace)
136138

139+
def _load_objects_from_base64(self, encoded_dict):
140+
loaded_objects = {}
141+
for key, encoded_str in encoded_dict.items():
142+
try:
143+
decoded_bytes = base64.b64decode(encoded_str)
144+
loaded_obj = dill.loads(decoded_bytes)
145+
loaded_objects[key] = loaded_obj
146+
except Exception as e:
147+
print(f"Error loading object for key '{key}': {e}")
148+
loaded_objects[key] = None
149+
return loaded_objects
150+
137151

138152
def ax_help(value: str):
139153
print(

0 commit comments

Comments
 (0)