opensandbox-code-interpreter 0.1.0__py3-none-any.whl
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.
- code_interpreter/__init__.py +45 -0
- code_interpreter/adapters/__init__.py +30 -0
- code_interpreter/adapters/code_adapter.py +268 -0
- code_interpreter/adapters/converter/__init__.py +26 -0
- code_interpreter/adapters/converter/code_execution_converter.py +108 -0
- code_interpreter/adapters/factory.py +58 -0
- code_interpreter/code_interpreter.py +343 -0
- code_interpreter/models/__init__.py +28 -0
- code_interpreter/models/code.py +67 -0
- code_interpreter/models/code_sync.py +41 -0
- code_interpreter/py.typed +0 -0
- code_interpreter/services/__init__.py +24 -0
- code_interpreter/services/code.py +149 -0
- code_interpreter/sync/__init__.py +18 -0
- code_interpreter/sync/adapters/__init__.py +26 -0
- code_interpreter/sync/adapters/code_adapter.py +215 -0
- code_interpreter/sync/adapters/factory.py +54 -0
- code_interpreter/sync/code_interpreter.py +286 -0
- code_interpreter/sync/services/__init__.py +27 -0
- code_interpreter/sync/services/code.py +122 -0
- opensandbox_code_interpreter-0.1.0.dist-info/METADATA +462 -0
- opensandbox_code_interpreter-0.1.0.dist-info/RECORD +24 -0
- opensandbox_code_interpreter-0.1.0.dist-info/WHEEL +4 -0
- opensandbox_code_interpreter-0.1.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright 2025 Alibaba Group Holding Ltd.
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
#
|
|
16
|
+
"""
|
|
17
|
+
Synchronous code execution service interface.
|
|
18
|
+
|
|
19
|
+
Defines the contract for multi-language code interpretation with context management,
|
|
20
|
+
session persistence, and real-time execution capabilities (SSE streaming), **in blocking form**.
|
|
21
|
+
|
|
22
|
+
This is the sync counterpart of :mod:`code_interpreter.services.code`.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from typing import Protocol
|
|
26
|
+
|
|
27
|
+
from opensandbox.models.execd import Execution
|
|
28
|
+
from opensandbox.models.execd_sync import ExecutionHandlersSync
|
|
29
|
+
|
|
30
|
+
from code_interpreter.models.code_sync import CodeContextSync
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class CodesSync(Protocol):
|
|
34
|
+
"""
|
|
35
|
+
Code execution service for multi-language code interpretation (sync).
|
|
36
|
+
|
|
37
|
+
This service provides advanced code execution capabilities with context management,
|
|
38
|
+
session persistence, and multi-language support.
|
|
39
|
+
|
|
40
|
+
Supported Languages (typical):
|
|
41
|
+
- Python
|
|
42
|
+
- JavaScript / TypeScript
|
|
43
|
+
- Bash
|
|
44
|
+
- Java
|
|
45
|
+
- Kotlin (depending on server image)
|
|
46
|
+
|
|
47
|
+
Key Features:
|
|
48
|
+
- Execution Contexts: Isolated environments with persistent state
|
|
49
|
+
- Variable Persistence: Variables and imports persist across executions in a context
|
|
50
|
+
- Real-time Interruption: Stop long-running code execution safely
|
|
51
|
+
- Output Streaming: Real-time stdout/stderr via SSE
|
|
52
|
+
|
|
53
|
+
Notes:
|
|
54
|
+
- All methods are **blocking** and executed in the current thread.
|
|
55
|
+
- For non-blocking usage, prefer the async :class:`code_interpreter.services.code.Codes`.
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
def create_context(self, language: str) -> CodeContextSync:
|
|
59
|
+
"""
|
|
60
|
+
Create a new execution context for code interpretation (blocking).
|
|
61
|
+
|
|
62
|
+
An execution context maintains state (variables/imports/working directory) across
|
|
63
|
+
multiple code executions, enabling interactive sessions.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
language: The programming language for this context (e.g., "python", "typescript").
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
A new CodeContextSync.
|
|
70
|
+
|
|
71
|
+
Raises:
|
|
72
|
+
SandboxException: If the language is not supported or context creation fails.
|
|
73
|
+
"""
|
|
74
|
+
...
|
|
75
|
+
|
|
76
|
+
def run(
|
|
77
|
+
self,
|
|
78
|
+
code: str,
|
|
79
|
+
*,
|
|
80
|
+
context: CodeContextSync | None = None,
|
|
81
|
+
handlers: ExecutionHandlersSync | None = None,
|
|
82
|
+
) -> Execution:
|
|
83
|
+
"""
|
|
84
|
+
Execute code within the specified context (blocking).
|
|
85
|
+
|
|
86
|
+
This method runs the provided code string in the language interpreter, capturing output,
|
|
87
|
+
errors, and execution metadata. Execution happens within the context's environment,
|
|
88
|
+
preserving variable state and working directory.
|
|
89
|
+
|
|
90
|
+
Execution behavior:
|
|
91
|
+
- Blocking: The call does not return until the stream finishes.
|
|
92
|
+
- Stateful: Variables and imports persist in the context.
|
|
93
|
+
- Streaming: Output is processed incrementally as SSE events arrive.
|
|
94
|
+
- Interruptible: Can be stopped using :meth:`interrupt`.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
code: Source code to execute.
|
|
98
|
+
context: Execution context (language + optional id). If None, a temporary Python context is used.
|
|
99
|
+
handlers: Optional streaming handlers for stdout/stderr/events.
|
|
100
|
+
|
|
101
|
+
Returns:
|
|
102
|
+
Execution with stdout/stderr/events and execution metadata.
|
|
103
|
+
|
|
104
|
+
Raises:
|
|
105
|
+
SandboxException: If execution fails or times out.
|
|
106
|
+
"""
|
|
107
|
+
...
|
|
108
|
+
|
|
109
|
+
def interrupt(self, execution_id: str) -> None:
|
|
110
|
+
"""
|
|
111
|
+
Interrupt a currently running code execution.
|
|
112
|
+
|
|
113
|
+
This method attempts to safely terminate a running execution, cleaning up resources and
|
|
114
|
+
keeping the interpreter in a consistent state.
|
|
115
|
+
|
|
116
|
+
Args:
|
|
117
|
+
execution_id: The unique identifier of the execution to interrupt.
|
|
118
|
+
|
|
119
|
+
Raises:
|
|
120
|
+
SandboxException: If interruption fails.
|
|
121
|
+
"""
|
|
122
|
+
...
|
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: opensandbox-code-interpreter
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: OpenSandbox Code Interpreter Python SDK - Advanced code execution with persistent contexts
|
|
5
|
+
Project-URL: Homepage, https://github.com/alibaba/OpenSandbox
|
|
6
|
+
Project-URL: Repository, https://github.com/alibaba/OpenSandbox
|
|
7
|
+
Project-URL: Documentation, https://docs.opensandbox.io
|
|
8
|
+
Project-URL: Issues, https://github.com/alibaba/OpenSandbox/issues
|
|
9
|
+
Author-email: OpenSandbox Team <ninan.nn@alibaba-inc.com>
|
|
10
|
+
License: Apache License
|
|
11
|
+
Version 2.0, January 2004
|
|
12
|
+
http://www.apache.org/licenses/
|
|
13
|
+
|
|
14
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
15
|
+
|
|
16
|
+
1. Definitions.
|
|
17
|
+
|
|
18
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
19
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
20
|
+
|
|
21
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
22
|
+
the copyright owner that is granting the License.
|
|
23
|
+
|
|
24
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
25
|
+
other entities that control, are controlled by, or are under common
|
|
26
|
+
control with that entity. For the purposes of this definition,
|
|
27
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
28
|
+
direction or management of such entity, whether by contract or
|
|
29
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
30
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
31
|
+
|
|
32
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
33
|
+
exercising permissions granted by this License.
|
|
34
|
+
|
|
35
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
36
|
+
including but not limited to software source code, documentation
|
|
37
|
+
source, and configuration files.
|
|
38
|
+
|
|
39
|
+
"Object" form shall mean any form resulting from mechanical
|
|
40
|
+
transformation or translation of a Source form, including but
|
|
41
|
+
not limited to compiled object code, generated documentation,
|
|
42
|
+
and conversions to other media types.
|
|
43
|
+
|
|
44
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
45
|
+
Object form, made available under the License, as indicated by a
|
|
46
|
+
copyright notice that is included in or attached to the work
|
|
47
|
+
(an example is provided in the Appendix below).
|
|
48
|
+
|
|
49
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
50
|
+
form, that is based on (or derived from) the Work and for which the
|
|
51
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
52
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
53
|
+
of this License, Derivative Works shall not include works that remain
|
|
54
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
55
|
+
the Work and Derivative Works thereof.
|
|
56
|
+
|
|
57
|
+
"Contribution" shall mean any work of authorship, including
|
|
58
|
+
the original version of the Work and any modifications or additions
|
|
59
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
60
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
61
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
62
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
63
|
+
means any form of electronic, verbal, or written communication sent
|
|
64
|
+
to the Licensor or its representatives, including but not limited to
|
|
65
|
+
communication on electronic mailing lists, source code control systems,
|
|
66
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
67
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
68
|
+
excluding communication that is conspicuously marked or otherwise
|
|
69
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
70
|
+
|
|
71
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
72
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
73
|
+
subsequently incorporated within the Work.
|
|
74
|
+
|
|
75
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
76
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
77
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
78
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
79
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
80
|
+
Work and such Derivative Works in Source or Object form.
|
|
81
|
+
|
|
82
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
83
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
84
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
85
|
+
(except as stated in this section) patent license to make, have made,
|
|
86
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
87
|
+
where such license applies only to those patent claims licensable
|
|
88
|
+
by such Contributor that are necessarily infringed by their
|
|
89
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
90
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
91
|
+
institute patent litigation against any entity (including a
|
|
92
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
93
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
94
|
+
or contributory patent infringement, then any patent licenses
|
|
95
|
+
granted to You under this License for that Work shall terminate
|
|
96
|
+
as of the date such litigation is filed.
|
|
97
|
+
|
|
98
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
99
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
100
|
+
modifications, and in Source or Object form, provided that You
|
|
101
|
+
meet the following conditions:
|
|
102
|
+
|
|
103
|
+
(a) You must give any other recipients of the Work or
|
|
104
|
+
Derivative Works a copy of this License; and
|
|
105
|
+
|
|
106
|
+
(b) You must cause any modified files to carry prominent notices
|
|
107
|
+
stating that You changed the files; and
|
|
108
|
+
|
|
109
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
110
|
+
that You distribute, all copyright, patent, trademark, and
|
|
111
|
+
attribution notices from the Source form of the Work,
|
|
112
|
+
excluding those notices that do not pertain to any part of
|
|
113
|
+
the Derivative Works; and
|
|
114
|
+
|
|
115
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
116
|
+
distribution, then any Derivative Works that You distribute must
|
|
117
|
+
include a readable copy of the attribution notices contained
|
|
118
|
+
within such NOTICE file, excluding those notices that do not
|
|
119
|
+
pertain to any part of the Derivative Works, in at least one
|
|
120
|
+
of the following places: within a NOTICE text file distributed
|
|
121
|
+
as part of the Derivative Works; within the Source form or
|
|
122
|
+
documentation, if provided along with the Derivative Works; or,
|
|
123
|
+
within a display generated by the Derivative Works, if and
|
|
124
|
+
wherever such third-party notices normally appear. The contents
|
|
125
|
+
of the NOTICE file are for informational purposes only and
|
|
126
|
+
do not modify the License. You may add Your own attribution
|
|
127
|
+
notices within Derivative Works that You distribute, alongside
|
|
128
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
129
|
+
that such additional attribution notices cannot be construed
|
|
130
|
+
as modifying the License.
|
|
131
|
+
|
|
132
|
+
You may add Your own copyright statement to Your modifications and
|
|
133
|
+
may provide additional or different license terms and conditions
|
|
134
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
135
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
136
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
137
|
+
the conditions stated in this License.
|
|
138
|
+
|
|
139
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
140
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
141
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
142
|
+
this License, without any additional terms or conditions.
|
|
143
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
144
|
+
the terms of any separate license agreement you may have executed
|
|
145
|
+
with Licensor regarding such Contributions.
|
|
146
|
+
|
|
147
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
148
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
149
|
+
except as required for reasonable and customary use in describing the
|
|
150
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
151
|
+
|
|
152
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
153
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
154
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
155
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
156
|
+
implied, including, without limitation, any warranties or conditions
|
|
157
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
158
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
159
|
+
appropriateness of using or redistributing the Work and assume any
|
|
160
|
+
risks associated with Your exercise of permissions under this License.
|
|
161
|
+
|
|
162
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
163
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
164
|
+
unless required by applicable law (such as deliberate and grossly
|
|
165
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
166
|
+
liable to You for damages, including any direct, indirect, special,
|
|
167
|
+
incidental, or consequential damages of any character arising as a
|
|
168
|
+
result of this License or out of the use or inability to use the
|
|
169
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
170
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
171
|
+
other commercial damages or losses), even if such Contributor
|
|
172
|
+
has been advised of the possibility of such damages.
|
|
173
|
+
|
|
174
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
175
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
176
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
177
|
+
or other liability obligations and/or rights consistent with this
|
|
178
|
+
License. However, in accepting such obligations, You may act only
|
|
179
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
180
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
181
|
+
defend, and hold each Contributor harmless for any liability
|
|
182
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
183
|
+
of your accepting any such warranty or additional liability.
|
|
184
|
+
|
|
185
|
+
END OF TERMS AND CONDITIONS
|
|
186
|
+
|
|
187
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
188
|
+
|
|
189
|
+
To apply the Apache License to your work, attach the following
|
|
190
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
191
|
+
replaced with your own identifying information. (Don't include
|
|
192
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
193
|
+
comment syntax for the file format. We also recommend that a
|
|
194
|
+
file or class name and description of purpose be included on the
|
|
195
|
+
same "printed page" as the copyright notice for easier
|
|
196
|
+
identification within third-party archives.
|
|
197
|
+
|
|
198
|
+
Copyright [yyyy] [name of copyright owner]
|
|
199
|
+
|
|
200
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
201
|
+
you may not use this file except in compliance with the License.
|
|
202
|
+
You may obtain a copy of the License at
|
|
203
|
+
|
|
204
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
205
|
+
|
|
206
|
+
Unless required by applicable law or agreed to in writing, software
|
|
207
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
208
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
209
|
+
See the License for the specific language governing permissions and
|
|
210
|
+
limitations under the License.
|
|
211
|
+
License-File: LICENSE
|
|
212
|
+
Keywords: code-execution,code-interpreter,opensandbox,sandbox,sdk
|
|
213
|
+
Classifier: Development Status :: 3 - Alpha
|
|
214
|
+
Classifier: Intended Audience :: Developers
|
|
215
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
216
|
+
Classifier: Operating System :: OS Independent
|
|
217
|
+
Classifier: Programming Language :: Python :: 3
|
|
218
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
219
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
220
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
221
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
222
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
223
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
224
|
+
Classifier: Typing :: Typed
|
|
225
|
+
Requires-Python: >=3.10
|
|
226
|
+
Requires-Dist: opensandbox<0.2.0,>=0.1.0.dev0
|
|
227
|
+
Requires-Dist: pydantic<3.0,>=2.0.0
|
|
228
|
+
Description-Content-Type: text/markdown
|
|
229
|
+
|
|
230
|
+
# Alibaba Code Interpreter SDK for Python
|
|
231
|
+
|
|
232
|
+
English | [中文](README_zh.md)
|
|
233
|
+
|
|
234
|
+
A Python SDK for executing code in secure, isolated sandboxes. It provides a high-level API for running Python, Java,
|
|
235
|
+
Go, TypeScript, and other languages safely, with support for code execution contexts.
|
|
236
|
+
|
|
237
|
+
## Prerequisites
|
|
238
|
+
|
|
239
|
+
This SDK requires a Docker image containing the Code Interpreter runtime environment. You must use the
|
|
240
|
+
`opensandbox/code-interpreter` image (or a derivative) which includes pre-installed runtimes for Python, Java, Go,
|
|
241
|
+
Node.js, etc.
|
|
242
|
+
|
|
243
|
+
For detailed information about supported languages and versions, refer to the
|
|
244
|
+
[Environment Documentation](../../../sandboxes/code-interpreter/README.md).
|
|
245
|
+
|
|
246
|
+
## Installation
|
|
247
|
+
|
|
248
|
+
### pip
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
pip install opensandbox-code-interpreter
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### uv
|
|
255
|
+
|
|
256
|
+
```bash
|
|
257
|
+
uv add opensandbox-code-interpreter
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## Quick Start
|
|
261
|
+
|
|
262
|
+
The following example demonstrates how to create a sandbox with a specific runtime configuration and execute a simple
|
|
263
|
+
script.
|
|
264
|
+
|
|
265
|
+
```python
|
|
266
|
+
import asyncio
|
|
267
|
+
from datetime import timedelta
|
|
268
|
+
|
|
269
|
+
from code_interpreter import CodeInterpreter, SupportedLanguage
|
|
270
|
+
from opensandbox import Sandbox
|
|
271
|
+
from opensandbox.config import ConnectionConfig
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
async def main() -> None:
|
|
275
|
+
# 1. Configure connection
|
|
276
|
+
config = ConnectionConfig(
|
|
277
|
+
domain="api.opensandbox.dev",
|
|
278
|
+
api_key="your-api-key",
|
|
279
|
+
request_timeout=timedelta(seconds=60),
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
# 2. Create a Sandbox with the code-interpreter image + runtime versions
|
|
283
|
+
sandbox = await Sandbox.create(
|
|
284
|
+
"opensandbox/code-interpreter:latest",
|
|
285
|
+
connection_config=config,
|
|
286
|
+
entrypoint=["/opt/opensandbox/code-interpreter.sh"],
|
|
287
|
+
env={
|
|
288
|
+
"PYTHON_VERSION": "3.11",
|
|
289
|
+
"JAVA_VERSION": "17",
|
|
290
|
+
"NODE_VERSION": "20",
|
|
291
|
+
"GO_VERSION": "1.24",
|
|
292
|
+
},
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
# 3. Use async context manager to ensure local resources are cleaned up
|
|
296
|
+
async with sandbox:
|
|
297
|
+
# 4. Create CodeInterpreter wrapper
|
|
298
|
+
interpreter = await CodeInterpreter.create(sandbox=sandbox)
|
|
299
|
+
|
|
300
|
+
# 5. Create an execution context (Python)
|
|
301
|
+
context = await interpreter.codes.create_context(SupportedLanguage.PYTHON)
|
|
302
|
+
|
|
303
|
+
# 6. Run code
|
|
304
|
+
result = await interpreter.codes.run(
|
|
305
|
+
"import sys\nprint(sys.version)\nresult = 2 + 2\nresult",
|
|
306
|
+
context=context,
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
# 7. Print output
|
|
310
|
+
if result.result:
|
|
311
|
+
print(result.result[0].text)
|
|
312
|
+
|
|
313
|
+
# 8. Cleanup remote instance (optional but recommended)
|
|
314
|
+
await interpreter.kill()
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
if __name__ == "__main__":
|
|
318
|
+
asyncio.run(main())
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
### Synchronous Quick Start
|
|
322
|
+
|
|
323
|
+
If you prefer a synchronous API, use `SandboxSync` + `CodeInterpreterSync`:
|
|
324
|
+
|
|
325
|
+
```python
|
|
326
|
+
from datetime import timedelta
|
|
327
|
+
|
|
328
|
+
import httpx
|
|
329
|
+
from code_interpreter import CodeInterpreterSync
|
|
330
|
+
from opensandbox import SandboxSync
|
|
331
|
+
from opensandbox.config import ConnectionConfigSync
|
|
332
|
+
|
|
333
|
+
config = ConnectionConfigSync(
|
|
334
|
+
domain="api.opensandbox.dev",
|
|
335
|
+
api_key="your-api-key",
|
|
336
|
+
request_timeout=timedelta(seconds=60),
|
|
337
|
+
transport=httpx.HTTPTransport(limits=httpx.Limits(max_connections=20)),
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
sandbox = SandboxSync.create(
|
|
341
|
+
"opensandbox/code-interpreter:latest",
|
|
342
|
+
connection_config=config,
|
|
343
|
+
entrypoint=["/opt/opensandbox/code-interpreter.sh"],
|
|
344
|
+
env={"PYTHON_VERSION": "3.11"},
|
|
345
|
+
)
|
|
346
|
+
with sandbox:
|
|
347
|
+
interpreter = CodeInterpreterSync.create(sandbox=sandbox)
|
|
348
|
+
result = interpreter.codes.run("result = 2 + 2\nresult")
|
|
349
|
+
if result.result:
|
|
350
|
+
print(result.result[0].text)
|
|
351
|
+
interpreter.kill()
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
## Runtime Configuration
|
|
355
|
+
|
|
356
|
+
### Docker Image
|
|
357
|
+
|
|
358
|
+
The Code Interpreter SDK relies on a specialized environment. Ensure your sandbox provider has the
|
|
359
|
+
`opensandbox/code-interpreter` image available.
|
|
360
|
+
|
|
361
|
+
### Language Version Selection
|
|
362
|
+
|
|
363
|
+
You can specify the desired version of a programming language by setting the corresponding environment variable when
|
|
364
|
+
creating the `Sandbox`.
|
|
365
|
+
|
|
366
|
+
| Language | Environment Variable | Example Value | Default (if unset) |
|
|
367
|
+
| -------- | -------------------- | ------------- | ------------------ |
|
|
368
|
+
| Python | `PYTHON_VERSION` | `3.11` | Image default |
|
|
369
|
+
| Java | `JAVA_VERSION` | `17` | Image default |
|
|
370
|
+
| Node.js | `NODE_VERSION` | `20` | Image default |
|
|
371
|
+
| Go | `GO_VERSION` | `1.24` | Image default |
|
|
372
|
+
|
|
373
|
+
## Usage Examples
|
|
374
|
+
|
|
375
|
+
### 1. Java Code Execution
|
|
376
|
+
|
|
377
|
+
```python
|
|
378
|
+
from code_interpreter import SupportedLanguage
|
|
379
|
+
|
|
380
|
+
ctx = await interpreter.codes.create_context(SupportedLanguage.JAVA)
|
|
381
|
+
execution = await interpreter.codes.run(
|
|
382
|
+
(
|
|
383
|
+
'System.out.println("Calculating sum...");\n'
|
|
384
|
+
+ "int a = 10;\n"
|
|
385
|
+
+ "int b = 20;\n"
|
|
386
|
+
+ "int sum = a + b;\n"
|
|
387
|
+
+ 'System.out.println("Sum: " + sum);\n'
|
|
388
|
+
+ "sum"
|
|
389
|
+
),
|
|
390
|
+
context=ctx,
|
|
391
|
+
)
|
|
392
|
+
|
|
393
|
+
print(execution.id)
|
|
394
|
+
for msg in execution.logs.stdout:
|
|
395
|
+
print(msg.text)
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
### 2. Python with State Persistence
|
|
399
|
+
|
|
400
|
+
Variables defined in one execution are available in subsequent executions within the same context.
|
|
401
|
+
|
|
402
|
+
```python
|
|
403
|
+
from code_interpreter import SupportedLanguage
|
|
404
|
+
|
|
405
|
+
ctx = await interpreter.codes.create_context(SupportedLanguage.PYTHON)
|
|
406
|
+
|
|
407
|
+
await interpreter.codes.run(
|
|
408
|
+
"users = ['Alice', 'Bob', 'Charlie']\nprint(len(users))",
|
|
409
|
+
context=ctx,
|
|
410
|
+
)
|
|
411
|
+
|
|
412
|
+
result = await interpreter.codes.run(
|
|
413
|
+
"users.append('Dave')\nprint(users)\nresult = users\nresult",
|
|
414
|
+
context=ctx,
|
|
415
|
+
)
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
### 3. Streaming Output Handling
|
|
419
|
+
|
|
420
|
+
Handle stdout/stderr and execution events in real-time.
|
|
421
|
+
|
|
422
|
+
```python
|
|
423
|
+
from opensandbox.models.execd import ExecutionHandlers
|
|
424
|
+
from code_interpreter import SupportedLanguage
|
|
425
|
+
|
|
426
|
+
async def on_stdout(msg):
|
|
427
|
+
print("STDOUT:", msg.text)
|
|
428
|
+
|
|
429
|
+
async def on_stderr(msg):
|
|
430
|
+
print("STDERR:", msg.text)
|
|
431
|
+
|
|
432
|
+
handlers = ExecutionHandlers(on_stdout=on_stdout, on_stderr=on_stderr)
|
|
433
|
+
|
|
434
|
+
ctx = await interpreter.codes.create_context(SupportedLanguage.PYTHON)
|
|
435
|
+
await interpreter.codes.run(
|
|
436
|
+
"import time\nfor i in range(5):\n print(i)\n time.sleep(0.5)",
|
|
437
|
+
context=ctx,
|
|
438
|
+
handlers=handlers,
|
|
439
|
+
)
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
### 4. Multi-Language Context Isolation
|
|
443
|
+
|
|
444
|
+
Different languages run in isolated environments.
|
|
445
|
+
|
|
446
|
+
```python
|
|
447
|
+
from code_interpreter import SupportedLanguage
|
|
448
|
+
|
|
449
|
+
py_ctx = await interpreter.codes.create_context(SupportedLanguage.PYTHON)
|
|
450
|
+
go_ctx = await interpreter.codes.create_context(SupportedLanguage.GO)
|
|
451
|
+
|
|
452
|
+
await interpreter.codes.run("print('Running in Python')", context=py_ctx)
|
|
453
|
+
await interpreter.codes.run(
|
|
454
|
+
"package main\nfunc main() { println(\"Running in Go\") }",
|
|
455
|
+
context=go_ctx,
|
|
456
|
+
)
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
## Notes
|
|
460
|
+
|
|
461
|
+
- **Lifecycle**: `CodeInterpreter` wraps an existing `Sandbox` instance and reuses its connection configuration.
|
|
462
|
+
- **Asyncio/event loop**: avoid sharing long-lived clients across multiple event loops (e.g. pytest-asyncio defaults).
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
code_interpreter/__init__.py,sha256=Q8aUmnJ4T74YAWbGM6h59x0BVtj1iqecI1BFT5cmIDY,1506
|
|
2
|
+
code_interpreter/code_interpreter.py,sha256=UxFlURpMipCIU22WcCPINioF6HVTXqYGzfJF2iVx-Fw,11535
|
|
3
|
+
code_interpreter/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
code_interpreter/adapters/__init__.py,sha256=d9_KAYGoHRAtc3_SpYqeRODDQuyO3YVrB2MCRYqorUs,973
|
|
5
|
+
code_interpreter/adapters/code_adapter.py,sha256=WaXu6RMFzCUrwitPGhKEt_yOEzbTX_dpKKdfeZy8hPg,9696
|
|
6
|
+
code_interpreter/adapters/factory.py,sha256=mMStbY7S5iuh4pyazcpUm1gg-jK0ILDK6pjbPLcD0QQ,2032
|
|
7
|
+
code_interpreter/adapters/converter/__init__.py,sha256=MdbzAZ8qvY5K02-3sQUSTqvDZC6mz3FCy3WlJoZMrM4,799
|
|
8
|
+
code_interpreter/adapters/converter/code_execution_converter.py,sha256=nQb85LNhtlooK9u7k_OrMyanHAsibB97f45rkM-hRJc,3163
|
|
9
|
+
code_interpreter/models/__init__.py,sha256=eF43XlKHaGJZs7m-71kOTUOiqs4AQi_695mh-nbzuOc,798
|
|
10
|
+
code_interpreter/models/code.py,sha256=2OhfSBHDXITw7_oHreBKF0goQhkSk0zfNZxhH0c3-Yo,2266
|
|
11
|
+
code_interpreter/models/code_sync.py,sha256=OzassgOsS5QoDA8Z4lus4RxMAL7NfpZ-Yghfk5uigX8,1270
|
|
12
|
+
code_interpreter/services/__init__.py,sha256=1TzXeUf0kiV7FUfnGqtQZNJtR8IssNHCRkonEvQ3mb4,728
|
|
13
|
+
code_interpreter/services/code.py,sha256=_2ppNEBPaOWpdF_UH9RanjXqFWxIxzma3pzL8odyCi0,5272
|
|
14
|
+
code_interpreter/sync/__init__.py,sha256=Hvi_XtzwuFNjpAYuAKw_deui0vgsI39zGaVwv6FKeNo,700
|
|
15
|
+
code_interpreter/sync/code_interpreter.py,sha256=mldsKfAVz7WXCyWudWj5pBKGhsGVatDrwHS-J_yh3IQ,9105
|
|
16
|
+
code_interpreter/sync/adapters/__init__.py,sha256=weUsTURxRkUCCIXF-3wGBkFQNzY6Fs5QJybr85gQDoc,871
|
|
17
|
+
code_interpreter/sync/adapters/code_adapter.py,sha256=UbXdJwhfSwv2BHY0Ur4P2cIK2D35c6Qz1g6ztN_LlaA,8837
|
|
18
|
+
code_interpreter/sync/adapters/factory.py,sha256=7sNQvDxxMo38Pt-Dk4vM4fJOd9xeTJXekSzBr9B-UrQ,1909
|
|
19
|
+
code_interpreter/sync/services/__init__.py,sha256=2ATCxyKIkvKCCxxA8vlozya7N0kzKh5_0XTyA1MyEaY,973
|
|
20
|
+
code_interpreter/sync/services/code.py,sha256=LRjSwk_0GzJb1aAvUjI-dFsyNadbAb7Ikqp_eN2LlHM,4361
|
|
21
|
+
opensandbox_code_interpreter-0.1.0.dist-info/METADATA,sha256=N71LTU6nl1hnFWjxbqOnCoRsnGUodxVX1jNCAsz5kh8,20973
|
|
22
|
+
opensandbox_code_interpreter-0.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
23
|
+
opensandbox_code_interpreter-0.1.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
24
|
+
opensandbox_code_interpreter-0.1.0.dist-info/RECORD,,
|