watasu-code-interpreter 0.1.48__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: watasu-code-interpreter
3
- Version: 0.1.48
3
+ Version: 0.1.49
4
4
  Summary: Code Interpreter SDK for Watasu
5
5
  Project-URL: Repository, https://github.com/watasuio/sdk
6
6
  License-Expression: MIT OR Apache-2.0
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "watasu-code-interpreter"
7
- version = "0.1.48"
7
+ version = "0.1.49"
8
8
  description = "Code Interpreter SDK for Watasu"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -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) -> Dict[str, Any]:
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) -> Dict[str, Any]:
44
- return {
45
- "stdout": [message.to_json() for message in self.stdout],
46
- "stderr": [message.to_json() for message in self.stderr],
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) -> Dict[str, Any]:
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) -> Dict[str, Any]:
185
- return {
186
- "results": [result.to_json() for result in self.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
- "execution_count": self.execution_count,
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