watasu-code-interpreter 0.1.47__tar.gz → 0.1.49__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {watasu_code_interpreter-0.1.47 → watasu_code_interpreter-0.1.49}/PKG-INFO +1 -1
- {watasu_code_interpreter-0.1.47 → watasu_code_interpreter-0.1.49}/pyproject.toml +1 -1
- {watasu_code_interpreter-0.1.47 → watasu_code_interpreter-0.1.49}/src/watasu_code_interpreter/main.py +18 -30
- {watasu_code_interpreter-0.1.47 → watasu_code_interpreter-0.1.49}/src/watasu_code_interpreter/models.py +42 -16
- {watasu_code_interpreter-0.1.47 → watasu_code_interpreter-0.1.49}/.gitignore +0 -0
- {watasu_code_interpreter-0.1.47 → watasu_code_interpreter-0.1.49}/README.md +0 -0
- {watasu_code_interpreter-0.1.47 → watasu_code_interpreter-0.1.49}/src/watasu_code_interpreter/__init__.py +0 -0
- {watasu_code_interpreter-0.1.47 → watasu_code_interpreter-0.1.49}/src/watasu_code_interpreter/charts.py +0 -0
- {watasu_code_interpreter-0.1.47 → watasu_code_interpreter-0.1.49}/src/watasu_code_interpreter/code_interpreter_async.py +0 -0
- {watasu_code_interpreter-0.1.47 → watasu_code_interpreter-0.1.49}/src/watasu_code_interpreter/code_interpreter_sync.py +0 -0
- {watasu_code_interpreter-0.1.47 → watasu_code_interpreter-0.1.49}/src/watasu_code_interpreter/constants.py +0 -0
- {watasu_code_interpreter-0.1.47 → watasu_code_interpreter-0.1.49}/src/watasu_code_interpreter/exceptions.py +0 -0
- {watasu_code_interpreter-0.1.47 → watasu_code_interpreter-0.1.49}/src/watasu_code_interpreter/py.typed +0 -0
|
@@ -25,13 +25,13 @@ class Sandbox(BaseSandbox):
|
|
|
25
25
|
self,
|
|
26
26
|
code: str,
|
|
27
27
|
language: Optional[str] = None,
|
|
28
|
-
context: Optional[
|
|
29
|
-
on_stdout: Optional[Callable[[OutputMessage],
|
|
30
|
-
on_stderr: Optional[Callable[[OutputMessage],
|
|
31
|
-
on_result: Optional[Callable[[Result],
|
|
32
|
-
on_error: Optional[Callable[[ExecutionError],
|
|
28
|
+
context: Optional[Context] = None,
|
|
29
|
+
on_stdout: Optional[Callable[[OutputMessage], Any]] = None,
|
|
30
|
+
on_stderr: Optional[Callable[[OutputMessage], Any]] = None,
|
|
31
|
+
on_result: Optional[Callable[[Result], Any]] = None,
|
|
32
|
+
on_error: Optional[Callable[[ExecutionError], Any]] = None,
|
|
33
33
|
envs: Optional[Dict[str, str]] = None,
|
|
34
|
-
timeout: Optional[
|
|
34
|
+
timeout: Optional[float] = None,
|
|
35
35
|
request_timeout: Optional[float] = None,
|
|
36
36
|
) -> Execution:
|
|
37
37
|
"""Run Python code in the sandbox and return structured execution output."""
|
|
@@ -78,21 +78,18 @@ class Sandbox(BaseSandbox):
|
|
|
78
78
|
def remove_code_context(
|
|
79
79
|
self,
|
|
80
80
|
context: Union[Context, str],
|
|
81
|
-
request_timeout: Optional[float] = None,
|
|
82
81
|
) -> None:
|
|
83
82
|
"""Remove a persistent code context."""
|
|
84
83
|
|
|
85
84
|
self._require_data_plane().delete_json(
|
|
86
85
|
f"/runtime/v1/code/contexts/{_context_path_id(context)}",
|
|
87
|
-
request_timeout=request_timeout,
|
|
88
86
|
)
|
|
89
87
|
|
|
90
|
-
def list_code_contexts(self
|
|
88
|
+
def list_code_contexts(self) -> List[Context]:
|
|
91
89
|
"""List persistent code contexts."""
|
|
92
90
|
|
|
93
91
|
response = self._require_data_plane().get_json(
|
|
94
92
|
"/runtime/v1/code/contexts",
|
|
95
|
-
request_timeout=request_timeout,
|
|
96
93
|
)
|
|
97
94
|
contexts = response if isinstance(response, list) else response.get("contexts", [])
|
|
98
95
|
return [context_from_api(item) for item in contexts]
|
|
@@ -100,14 +97,12 @@ class Sandbox(BaseSandbox):
|
|
|
100
97
|
def restart_code_context(
|
|
101
98
|
self,
|
|
102
99
|
context: Union[Context, str],
|
|
103
|
-
request_timeout: Optional[float] = None,
|
|
104
100
|
) -> None:
|
|
105
101
|
"""Restart a persistent code context."""
|
|
106
102
|
|
|
107
103
|
self._require_data_plane().post_json(
|
|
108
104
|
f"/runtime/v1/code/contexts/{_context_path_id(context)}/restart",
|
|
109
105
|
json={},
|
|
110
|
-
request_timeout=request_timeout,
|
|
111
106
|
)
|
|
112
107
|
|
|
113
108
|
|
|
@@ -151,13 +146,13 @@ class AsyncSandbox(BaseAsyncSandbox):
|
|
|
151
146
|
self,
|
|
152
147
|
code: str,
|
|
153
148
|
language: Optional[str] = None,
|
|
154
|
-
context: Optional[
|
|
155
|
-
on_stdout: Optional[Callable[[OutputMessage],
|
|
156
|
-
on_stderr: Optional[Callable[[OutputMessage],
|
|
157
|
-
on_result: Optional[Callable[[Result],
|
|
158
|
-
on_error: Optional[Callable[[ExecutionError],
|
|
149
|
+
context: Optional[Context] = None,
|
|
150
|
+
on_stdout: Optional[Callable[[OutputMessage], Any]] = None,
|
|
151
|
+
on_stderr: Optional[Callable[[OutputMessage], Any]] = None,
|
|
152
|
+
on_result: Optional[Callable[[Result], Any]] = None,
|
|
153
|
+
on_error: Optional[Callable[[ExecutionError], Any]] = None,
|
|
159
154
|
envs: Optional[Dict[str, str]] = None,
|
|
160
|
-
timeout: Optional[
|
|
155
|
+
timeout: Optional[float] = None,
|
|
161
156
|
request_timeout: Optional[float] = None,
|
|
162
157
|
) -> Execution:
|
|
163
158
|
"""Run Python code in the sandbox and return structured execution output."""
|
|
@@ -195,46 +190,39 @@ class AsyncSandbox(BaseAsyncSandbox):
|
|
|
195
190
|
async def remove_code_context(
|
|
196
191
|
self,
|
|
197
192
|
context: Union[Context, str],
|
|
198
|
-
request_timeout: Optional[float] = None,
|
|
199
193
|
) -> None:
|
|
200
194
|
"""Remove a persistent code context."""
|
|
201
195
|
|
|
202
196
|
await asyncio.to_thread(
|
|
203
197
|
self._sync.remove_code_context,
|
|
204
198
|
context,
|
|
205
|
-
request_timeout=request_timeout,
|
|
206
199
|
)
|
|
207
200
|
|
|
208
|
-
async def list_code_contexts(
|
|
209
|
-
self, request_timeout: Optional[float] = None
|
|
210
|
-
) -> List[Context]:
|
|
201
|
+
async def list_code_contexts(self) -> List[Context]:
|
|
211
202
|
"""List persistent code contexts."""
|
|
212
203
|
|
|
213
204
|
return await asyncio.to_thread(
|
|
214
205
|
self._sync.list_code_contexts,
|
|
215
|
-
request_timeout=request_timeout,
|
|
216
206
|
)
|
|
217
207
|
|
|
218
208
|
async def restart_code_context(
|
|
219
209
|
self,
|
|
220
210
|
context: Union[Context, str],
|
|
221
|
-
request_timeout: Optional[float] = None,
|
|
222
211
|
) -> None:
|
|
223
212
|
"""Restart a persistent code context."""
|
|
224
213
|
|
|
225
214
|
await asyncio.to_thread(
|
|
226
215
|
self._sync.restart_code_context,
|
|
227
216
|
context,
|
|
228
|
-
request_timeout=request_timeout,
|
|
229
217
|
)
|
|
230
218
|
|
|
231
219
|
|
|
232
220
|
def _emit_callbacks(
|
|
233
221
|
execution: Execution,
|
|
234
|
-
on_stdout: Optional[Callable[[OutputMessage],
|
|
235
|
-
on_stderr: Optional[Callable[[OutputMessage],
|
|
236
|
-
on_result: Optional[Callable[[Result],
|
|
237
|
-
on_error: Optional[Callable[[ExecutionError],
|
|
222
|
+
on_stdout: Optional[Callable[[OutputMessage], Any]],
|
|
223
|
+
on_stderr: Optional[Callable[[OutputMessage], Any]],
|
|
224
|
+
on_result: Optional[Callable[[Result], Any]],
|
|
225
|
+
on_error: Optional[Callable[[ExecutionError], Any]],
|
|
238
226
|
) -> None:
|
|
239
227
|
for message in execution.logs.stdout:
|
|
240
228
|
if on_stdout is not None:
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import json as jsonlib
|
|
3
4
|
import time
|
|
4
5
|
from dataclasses import dataclass, field
|
|
5
6
|
from typing import Any, Callable, Dict, List, Optional, TypeVar
|
|
@@ -25,12 +26,12 @@ class OutputMessage:
|
|
|
25
26
|
def __str__(self) -> str:
|
|
26
27
|
return self.line
|
|
27
28
|
|
|
28
|
-
def to_json(self) ->
|
|
29
|
-
return {
|
|
29
|
+
def to_json(self) -> str:
|
|
30
|
+
return jsonlib.dumps({
|
|
30
31
|
"line": self.line,
|
|
31
32
|
"timestamp": self.timestamp,
|
|
32
33
|
"error": self.error,
|
|
33
|
-
}
|
|
34
|
+
})
|
|
34
35
|
|
|
35
36
|
|
|
36
37
|
@dataclass
|
|
@@ -40,11 +41,11 @@ class Logs:
|
|
|
40
41
|
stdout: List[OutputMessage] = field(default_factory=list)
|
|
41
42
|
stderr: List[OutputMessage] = field(default_factory=list)
|
|
42
43
|
|
|
43
|
-
def to_json(self) ->
|
|
44
|
-
return {
|
|
45
|
-
"stdout": [message
|
|
46
|
-
"stderr": [message
|
|
47
|
-
}
|
|
44
|
+
def to_json(self) -> str:
|
|
45
|
+
return jsonlib.dumps({
|
|
46
|
+
"stdout": [_message_line(message) for message in self.stdout],
|
|
47
|
+
"stderr": [_message_line(message) for message in self.stderr],
|
|
48
|
+
})
|
|
48
49
|
|
|
49
50
|
|
|
50
51
|
@dataclass
|
|
@@ -55,12 +56,12 @@ class ExecutionError:
|
|
|
55
56
|
value: str
|
|
56
57
|
traceback: str
|
|
57
58
|
|
|
58
|
-
def to_json(self) ->
|
|
59
|
-
return {
|
|
59
|
+
def to_json(self) -> str:
|
|
60
|
+
return jsonlib.dumps({
|
|
60
61
|
"name": self.name,
|
|
61
62
|
"value": self.value,
|
|
62
63
|
"traceback": self.traceback,
|
|
63
|
-
}
|
|
64
|
+
})
|
|
64
65
|
|
|
65
66
|
|
|
66
67
|
@dataclass
|
|
@@ -181,13 +182,12 @@ class Execution:
|
|
|
181
182
|
return result.text
|
|
182
183
|
return None
|
|
183
184
|
|
|
184
|
-
def to_json(self) ->
|
|
185
|
-
return {
|
|
186
|
-
"results":
|
|
185
|
+
def to_json(self) -> str:
|
|
186
|
+
return jsonlib.dumps({
|
|
187
|
+
"results": serialize_results(self.results),
|
|
187
188
|
"logs": self.logs.to_json(),
|
|
188
189
|
"error": self.error.to_json() if self.error else None,
|
|
189
|
-
|
|
190
|
-
}
|
|
190
|
+
})
|
|
191
191
|
|
|
192
192
|
|
|
193
193
|
@dataclass(init=False)
|
|
@@ -217,6 +217,32 @@ class Context:
|
|
|
217
217
|
"cwd": self.cwd,
|
|
218
218
|
}
|
|
219
219
|
|
|
220
|
+
@classmethod
|
|
221
|
+
def from_json(cls, data: Dict[str, str]):
|
|
222
|
+
return cls(
|
|
223
|
+
context_id=data.get("id"),
|
|
224
|
+
language=data.get("language"),
|
|
225
|
+
cwd=data.get("cwd"),
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def serialize_results(results: List[Result]) -> List[Dict[str, Any]]:
|
|
230
|
+
serialized = []
|
|
231
|
+
for result in results:
|
|
232
|
+
item = {}
|
|
233
|
+
for key in result.formats():
|
|
234
|
+
if key == "chart" and result.chart is not None:
|
|
235
|
+
item[key] = result.chart.to_dict()
|
|
236
|
+
else:
|
|
237
|
+
item[key] = result[key]
|
|
238
|
+
item["text"] = result.text
|
|
239
|
+
serialized.append(item)
|
|
240
|
+
return serialized
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def _message_line(message: Any) -> str:
|
|
244
|
+
return str(message.line) if hasattr(message, "line") else str(message)
|
|
245
|
+
|
|
220
246
|
|
|
221
247
|
def execution_from_api(payload: Dict[str, Any]) -> Execution:
|
|
222
248
|
execution = payload.get("execution") or payload
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|