ms-enclave 0.0.0__py3-none-any.whl → 0.0.1__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.

Potentially problematic release.


This version of ms-enclave might be problematic. Click here for more details.

Files changed (43) hide show
  1. ms_enclave/__init__.py +2 -2
  2. ms_enclave/cli/__init__.py +1 -0
  3. ms_enclave/cli/base.py +20 -0
  4. ms_enclave/cli/cli.py +27 -0
  5. ms_enclave/cli/start_server.py +84 -0
  6. ms_enclave/sandbox/__init__.py +27 -0
  7. ms_enclave/sandbox/boxes/__init__.py +16 -0
  8. ms_enclave/sandbox/boxes/base.py +267 -0
  9. ms_enclave/sandbox/boxes/docker_notebook.py +216 -0
  10. ms_enclave/sandbox/boxes/docker_sandbox.py +252 -0
  11. ms_enclave/sandbox/manager/__init__.py +11 -0
  12. ms_enclave/sandbox/manager/base.py +155 -0
  13. ms_enclave/sandbox/manager/http_manager.py +405 -0
  14. ms_enclave/sandbox/manager/local_manager.py +295 -0
  15. ms_enclave/sandbox/model/__init__.py +21 -0
  16. ms_enclave/sandbox/model/base.py +36 -0
  17. ms_enclave/sandbox/model/config.py +97 -0
  18. ms_enclave/sandbox/model/requests.py +57 -0
  19. ms_enclave/sandbox/model/responses.py +57 -0
  20. ms_enclave/sandbox/server/__init__.py +0 -0
  21. ms_enclave/sandbox/server/server.py +195 -0
  22. ms_enclave/sandbox/tools/__init__.py +4 -0
  23. ms_enclave/sandbox/tools/base.py +95 -0
  24. ms_enclave/sandbox/tools/sandbox_tool.py +46 -0
  25. ms_enclave/sandbox/tools/sandbox_tools/__init__.py +4 -0
  26. ms_enclave/sandbox/tools/sandbox_tools/file_operation.py +215 -0
  27. ms_enclave/sandbox/tools/sandbox_tools/notebook_executor.py +167 -0
  28. ms_enclave/sandbox/tools/sandbox_tools/python_executor.py +87 -0
  29. ms_enclave/sandbox/tools/sandbox_tools/shell_executor.py +63 -0
  30. ms_enclave/sandbox/tools/tool_info.py +141 -0
  31. ms_enclave/utils/__init__.py +1 -0
  32. ms_enclave/utils/json_schema.py +208 -0
  33. ms_enclave/utils/logger.py +106 -0
  34. ms_enclave/version.py +2 -2
  35. ms_enclave-0.0.1.dist-info/METADATA +314 -0
  36. ms_enclave-0.0.1.dist-info/RECORD +40 -0
  37. {ms_enclave-0.0.0.dist-info → ms_enclave-0.0.1.dist-info}/WHEEL +1 -1
  38. ms_enclave-0.0.1.dist-info/entry_points.txt +2 -0
  39. ms_enclave/run_server.py +0 -21
  40. ms_enclave-0.0.0.dist-info/METADATA +0 -329
  41. ms_enclave-0.0.0.dist-info/RECORD +0 -8
  42. {ms_enclave-0.0.0.dist-info → ms_enclave-0.0.1.dist-info}/licenses/LICENSE +0 -0
  43. {ms_enclave-0.0.0.dist-info → ms_enclave-0.0.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,208 @@
1
+ import types
2
+ import typing
3
+ from copy import deepcopy
4
+ from dataclasses import is_dataclass
5
+ from datetime import date, datetime, time
6
+ from enum import EnumMeta
7
+ from typing import (
8
+ Any,
9
+ Dict,
10
+ List,
11
+ Literal,
12
+ Optional,
13
+ Set,
14
+ Tuple,
15
+ Type,
16
+ Union,
17
+ cast,
18
+ get_args,
19
+ get_origin,
20
+ get_type_hints,
21
+ is_typeddict,
22
+ )
23
+
24
+ from pydantic import BaseModel, Field
25
+
26
+ JSONType = Literal['string', 'integer', 'number', 'boolean', 'array', 'object', 'null']
27
+ """Valid types within JSON schema."""
28
+
29
+
30
+ class JSONSchema(BaseModel):
31
+ """JSON Schema for type."""
32
+
33
+ type: Optional[JSONType] = Field(default=None)
34
+ """JSON type of tool parameter."""
35
+
36
+ format: Optional[str] = Field(default=None)
37
+ """Format of the parameter (e.g. date-time)."""
38
+
39
+ description: Optional[str] = Field(default=None)
40
+ """Parameter description."""
41
+
42
+ default: Any = Field(default=None)
43
+ """Default value for parameter."""
44
+
45
+ enum: Optional[List[Any]] = Field(default=None)
46
+ """Valid values for enum parameters."""
47
+
48
+ items: Optional['JSONSchema'] = Field(default=None)
49
+ """Valid type for array parameters."""
50
+
51
+ properties: Optional[Dict[str, 'JSONSchema']] = Field(default=None)
52
+ """Valid fields for object parametrs."""
53
+
54
+ additionalProperties: Optional[Union['JSONSchema', bool]] = Field(default=None)
55
+ """Are additional properties allowed?"""
56
+
57
+ anyOf: Optional[List['JSONSchema']] = Field(default=None)
58
+ """Valid types for union parameters."""
59
+
60
+ required: Optional[List[str]] = Field(default=None)
61
+ """Required fields for object parameters."""
62
+
63
+
64
+ def json_schema(t: Type[Any]) -> JSONSchema:
65
+ """Provide a JSON Schema for the specified type.
66
+
67
+ Schemas can be automatically inferred for a wide variety of
68
+ Python class types including Pydantic BaseModel, dataclasses,
69
+ and typed dicts.
70
+
71
+ Args:
72
+ t: Python type
73
+
74
+ Returns:
75
+ JSON Schema for type.
76
+ """
77
+ origin = get_origin(t)
78
+ args = get_args(t)
79
+
80
+ if origin is None:
81
+ if t is int:
82
+ return JSONSchema(type='integer')
83
+ elif t is float:
84
+ return JSONSchema(type='number')
85
+ elif t is str:
86
+ return JSONSchema(type='string')
87
+ elif t is bool:
88
+ return JSONSchema(type='boolean')
89
+ elif t is datetime:
90
+ return JSONSchema(type='string', format='date-time')
91
+ elif t is date:
92
+ return JSONSchema(type='string', format='date')
93
+ elif t is time:
94
+ return JSONSchema(type='string', format='time')
95
+ elif t is list or t is set:
96
+ return JSONSchema(type='array', items=JSONSchema())
97
+ elif t is dict:
98
+ return JSONSchema(type='object', additionalProperties=JSONSchema())
99
+ elif (is_dataclass(t) or is_typeddict(t) or (isinstance(t, type) and issubclass(t, BaseModel))):
100
+ return cls_json_schema(t)
101
+ elif isinstance(t, EnumMeta):
102
+ return JSONSchema(enum=[item.value for item in t])
103
+ elif t is type(None):
104
+ return JSONSchema(type='null')
105
+ else:
106
+ return JSONSchema()
107
+ elif (origin is list or origin is List or origin is tuple or origin is Tuple or origin is set or origin is Set):
108
+ return JSONSchema(type='array', items=json_schema(args[0]) if args else JSONSchema())
109
+ elif origin is dict or origin is Dict:
110
+ return JSONSchema(
111
+ type='object',
112
+ additionalProperties=json_schema(args[1]) if len(args) > 1 else JSONSchema(),
113
+ )
114
+ elif origin is Union or origin is types.UnionType:
115
+ return JSONSchema(anyOf=[json_schema(arg) for arg in args])
116
+ elif origin is Optional:
117
+ return JSONSchema(anyOf=[json_schema(arg) for arg in args] + [JSONSchema(type='null')])
118
+ elif origin is typing.Literal:
119
+ return JSONSchema(enum=list(args))
120
+
121
+ return JSONSchema() # Default case if we can't determine the type
122
+
123
+
124
+ def cls_json_schema(cls: Type[Any]) -> JSONSchema:
125
+ properties: Dict[str, JSONSchema] = {}
126
+ required: List[str] = []
127
+
128
+ if is_dataclass(cls):
129
+ fields = cls.__dataclass_fields__ # type: ignore
130
+ for name, field in fields.items():
131
+ properties[name] = json_schema(field.type) # type: ignore
132
+ if field.default == field.default_factory:
133
+ required.append(name)
134
+ elif isinstance(cls, type) and issubclass(cls, BaseModel):
135
+ schema = cls.model_json_schema()
136
+ schema = resolve_schema_references(schema)
137
+ for name, prop in schema.get('properties', {}).items():
138
+ properties[name] = JSONSchema(**prop)
139
+ required = schema.get('required', [])
140
+ elif is_typeddict(cls):
141
+ annotations = get_type_hints(cls)
142
+ for name, type_hint in annotations.items():
143
+ properties[name] = json_schema(type_hint)
144
+ if name in cls.__required_keys__:
145
+ required.append(name)
146
+
147
+ return JSONSchema(
148
+ type='object',
149
+ properties=properties,
150
+ required=required if required else None,
151
+ additionalProperties=False,
152
+ )
153
+
154
+
155
+ def python_type_to_json_type(python_type: Optional[str]) -> JSONType:
156
+ if python_type == 'str':
157
+ return 'string'
158
+ elif python_type == 'int':
159
+ return 'integer'
160
+ elif python_type == 'float':
161
+ return 'number'
162
+ elif python_type == 'bool':
163
+ return 'boolean'
164
+ elif python_type == 'list':
165
+ return 'array'
166
+ elif python_type == 'dict':
167
+ return 'object'
168
+ elif python_type == 'None':
169
+ return 'null'
170
+ elif python_type is None:
171
+ # treat 'unknown' as string as anything can be converted to string
172
+ return 'string'
173
+ else:
174
+ raise ValueError(f'Unsupported type: {python_type} for Python to JSON conversion.')
175
+
176
+
177
+ def resolve_schema_references(schema: Dict[str, Any]) -> Dict[str, Any]:
178
+ """Resolves all $ref references in a JSON schema by inlining the definitions."""
179
+ schema = deepcopy(schema)
180
+ definitions = schema.pop('$defs', {})
181
+
182
+ def _resolve_refs(obj: Any) -> Any:
183
+ if isinstance(obj, dict):
184
+ if '$ref' in obj and obj['$ref'].startswith('#/$defs/'):
185
+ ref_key = obj['$ref'].split('/')[-1]
186
+ if ref_key in definitions:
187
+ # Replace with a deep copy of the definition
188
+ resolved = deepcopy(definitions[ref_key])
189
+ # Process any nested references in the definition
190
+ resolved = _resolve_refs(resolved)
191
+
192
+ # Merge in the current object fields, which should take priority
193
+ # This means that if you have e.g.
194
+ # {"$ref": "#/$defs/SubType", "description": "subtype of type SubType"},
195
+ # and SubType resolves to
196
+ # {"description": "The SubType Class", "parameters": {"param1": {"type": "string"}}},
197
+ # the final result will be:
198
+ # {"description": "subtype of type SubType", "parameters": {"param1": {"type": "string"}}}
199
+ return resolved | {k: o for k, o in obj.items() if k != '$ref'}
200
+
201
+ # Process all entries in the dictionary
202
+ return {k: _resolve_refs(v) for k, v in obj.items()}
203
+ elif isinstance(obj, list):
204
+ return [_resolve_refs(item) for item in obj]
205
+ else:
206
+ return obj
207
+
208
+ return cast(Dict[str, Any], _resolve_refs(schema))
@@ -0,0 +1,106 @@
1
+ # Copyright (c) Alibaba, Inc. and its affiliates.
2
+ import importlib.util
3
+ import logging
4
+ import os
5
+ from contextlib import contextmanager
6
+ from types import MethodType
7
+ from typing import Optional
8
+
9
+ init_loggers = {}
10
+
11
+ logger_format = logging.Formatter('[%(levelname)s:%(name)s] %(message)s')
12
+
13
+ info_set = set()
14
+ warning_set = set()
15
+
16
+
17
+ def info_once(self, msg, *args, **kwargs):
18
+ hash_id = kwargs.get('hash_id') or msg
19
+ if hash_id in info_set:
20
+ return
21
+ info_set.add(hash_id)
22
+ self.info(msg)
23
+
24
+
25
+ def warning_once(self, msg, *args, **kwargs):
26
+ hash_id = kwargs.get('hash_id') or msg
27
+ if hash_id in warning_set:
28
+ return
29
+ warning_set.add(hash_id)
30
+ self.warning(msg)
31
+
32
+
33
+ def get_logger(log_file: Optional[str] = None, log_level: Optional[int] = None, file_mode: str = 'w'):
34
+ """ Get logging logger
35
+
36
+ Args:
37
+ log_file: Log filename, if specified, file handler will be added to
38
+ logger
39
+ log_level: Logging level.
40
+ file_mode: Specifies the mode to open the file, if filename is
41
+ specified (if filemode is unspecified, it defaults to 'w').
42
+ """
43
+ if log_level is None:
44
+ log_level = os.getenv('LOG_LEVEL', 'INFO').upper()
45
+ log_level = getattr(logging, log_level, logging.INFO)
46
+ logger_name = __name__.split('.')[0]
47
+ logger = logging.getLogger(logger_name)
48
+ logger.propagate = False
49
+ if logger_name in init_loggers:
50
+ add_file_handler_if_needed(logger, log_file, file_mode, log_level)
51
+ return logger
52
+
53
+ # handle duplicate logs to the console
54
+ # Starting in 1.8.0, PyTorch DDP attaches a StreamHandler <stderr> (NOTSET)
55
+ # to the root logger. As logger.propagate is True by default, this root
56
+ # level handler causes logging messages from rank>0 processes to
57
+ # unexpectedly show up on the console, creating much unwanted clutter.
58
+ # To fix this issue, we set the root logger's StreamHandler, if any, to log
59
+ # at the ERROR level.
60
+ for handler in logger.root.handlers:
61
+ if type(handler) is logging.StreamHandler:
62
+ handler.setLevel(logging.ERROR)
63
+
64
+ stream_handler = logging.StreamHandler()
65
+ handlers = [stream_handler]
66
+
67
+ if log_file is not None:
68
+ file_handler = logging.FileHandler(log_file, file_mode)
69
+ handlers.append(file_handler)
70
+
71
+ for handler in handlers:
72
+ handler.setFormatter(logger_format)
73
+ handler.setLevel(log_level)
74
+ logger.addHandler(handler)
75
+
76
+ logger.setLevel(log_level)
77
+ init_loggers[logger_name] = True
78
+ logger.info_once = MethodType(info_once, logger)
79
+ logger.warning_once = MethodType(warning_once, logger)
80
+ return logger
81
+
82
+
83
+ logger = get_logger()
84
+ # ms_logger = get_ms_logger()
85
+
86
+ logger.handlers[0].setFormatter(logger_format)
87
+ # ms_logger.handlers[0].setFormatter(logger_format)
88
+ log_level = os.getenv('LOG_LEVEL', 'INFO').upper()
89
+ # ms_logger.setLevel(log_level)
90
+
91
+
92
+ def add_file_handler_if_needed(logger, log_file, file_mode, log_level):
93
+ for handler in logger.handlers:
94
+ if isinstance(handler, logging.FileHandler):
95
+ return
96
+
97
+ if importlib.util.find_spec('torch') is not None:
98
+ is_worker0 = int(os.getenv('LOCAL_RANK', -1)) in {-1, 0}
99
+ else:
100
+ is_worker0 = True
101
+
102
+ if is_worker0 and log_file is not None:
103
+ file_handler = logging.FileHandler(log_file, file_mode)
104
+ file_handler.setFormatter(logger_format)
105
+ file_handler.setLevel(log_level)
106
+ logger.addHandler(file_handler)
ms_enclave/version.py CHANGED
@@ -1,2 +1,2 @@
1
- __version__ = '0.0.0'
2
- __release_date__ = '2025-09-11 20:00:00'
1
+ __version__ = '0.0.1'
2
+ __release_date__ = '2025-10-20 12:00:00'
@@ -0,0 +1,314 @@
1
+ Metadata-Version: 2.4
2
+ Name: ms-enclave
3
+ Version: 0.0.1
4
+ Summary: Modularized and Stable Sandbox runtime environment.
5
+ Author: ModelScope team
6
+ Author-email: contact@modelscope.cn
7
+ License: Apache License 2.0
8
+ Project-URL: Homepage, https://github.com/modelscope/ms-enclave
9
+ Keywords: python,llm,sandbox
10
+ Requires-Python: >=3.10
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: fastapi>=0.104.0
14
+ Requires-Dist: uvicorn[standard]>=0.24.0
15
+ Requires-Dist: openai
16
+ Requires-Dist: pydantic>=2.11.7
17
+ Requires-Dist: requests>=2.32.4
18
+ Requires-Dist: shortuuid>=1.0.13
19
+ Requires-Dist: aiohttp>=3.8.1
20
+ Requires-Dist: docstring-parser
21
+ Provides-Extra: dev
22
+ Requires-Dist: pytest>=8.3.5; extra == "dev"
23
+ Requires-Dist: pytest-asyncio>=0.23.0; extra == "dev"
24
+ Requires-Dist: pre-commit>=4.2.0; extra == "dev"
25
+ Requires-Dist: jupyter-book>=1.0.4.post1; extra == "dev"
26
+ Requires-Dist: pytest-cov>=6.2.1; extra == "dev"
27
+ Provides-Extra: docker
28
+ Requires-Dist: docker>=7.1.0; extra == "docker"
29
+ Requires-Dist: websocket-client; extra == "docker"
30
+ Dynamic: license-file
31
+
32
+ # ms-enclave
33
+
34
+ A modular and stable sandbox runtime environment
35
+
36
+ ## Overview
37
+
38
+ ms-enclave is a modular and stable sandbox runtime environment that provides a secure isolated execution environment for applications. It achieves strong isolation through Docker containers, with accompanying local/HTTP managers and an extensible tool system, enabling you to safely and efficiently execute code in a controlled environment.
39
+
40
+ - 🔒 Secure Isolation: Full isolation and resource limitation based on Docker
41
+ - 🧩 Modular: Extensible sandbox and tools (registration factory)
42
+ - ⚡ Stable Performance: Simple implementation, fast startup, lifecycle management
43
+ - 🌐 Remote Management: Built-in FastAPI service, supports HTTP management
44
+ - 🔧 Tool System: Standardized tools enabled by sandbox type (OpenAI-style schema)
45
+
46
+ ## System Requirements
47
+
48
+ - Python >= 3.10
49
+ - Operating System: Linux, macOS, or Windows with Docker support
50
+ - Docker daemon running locally (Notebook sandbox requires port 8888 open)
51
+
52
+ ## Installation
53
+
54
+ ### Install from PyPI
55
+
56
+ ```bash
57
+ pip install ms-enclave
58
+ ```
59
+
60
+ ### Install from Source
61
+
62
+ ```bash
63
+ git clone https://github.com/modelscope/ms-enclave.git
64
+ cd ms-enclave
65
+ pip install -e .
66
+ ```
67
+
68
+ ## Quick Start: Minimal Example (SandboxFactory)
69
+
70
+ > Tools need to be explicitly enabled in the tools_config setting, otherwise they won't be registered.
71
+
72
+ ```python
73
+ import asyncio
74
+ from ms_enclave.sandbox.boxes import SandboxFactory
75
+ from ms_enclave.sandbox.model import DockerSandboxConfig, SandboxType
76
+
77
+ async def main():
78
+ config = DockerSandboxConfig(
79
+ image='python:3.11-slim',
80
+ memory_limit='512m',
81
+ tools_config={
82
+ 'python_executor': {},
83
+ 'file_operation': {},
84
+ 'shell_executor': {}
85
+ }
86
+ )
87
+
88
+ async with SandboxFactory.create_sandbox(SandboxType.DOCKER, config) as sandbox:
89
+ # 1) Write a file
90
+ await sandbox.execute_tool('file_operation', {
91
+ 'operation': 'write', 'file_path': '/sandbox/hello.txt', 'content': 'hi from enclave'
92
+ })
93
+ # 2) Execute Python code
94
+ result = await sandbox.execute_tool('python_executor', {
95
+ 'code': "print('Hello from sandbox!')\nprint(open('/sandbox/hello.txt').read())"
96
+ })
97
+ print(result.output)
98
+
99
+ asyncio.run(main())
100
+ ```
101
+
102
+ ---
103
+
104
+ ## Typical Usage Scenarios and Examples
105
+
106
+ - Directly using SandboxFactory: Create/destroy sandboxes in a single process—lightweight; suitable for scripts or one-off tasks
107
+ - Using LocalSandboxManager: Manage the lifecycle/cleanup of multiple sandboxes locally; suitable for service-oriented or multi-task parallel scenarios
108
+ - Using HttpSandboxManager: Unified sandbox management through remote HTTP services; suitable for cross-machine/distributed or more isolated deployments
109
+
110
+ ### 1) Direct Sandbox Creation: SandboxFactory (Lightweight, Temporary)
111
+
112
+ Usage Scenarios:
113
+
114
+ - Temporarily execute code in scripts or microservices
115
+ - Fine-grained control over sandbox lifecycle (cleanup upon context exit)
116
+
117
+ Example (Docker Sandbox + Python Execution):
118
+
119
+ ```python
120
+ import asyncio
121
+ from ms_enclave.sandbox.boxes import SandboxFactory
122
+ from ms_enclave.sandbox.model import DockerSandboxConfig, SandboxType
123
+
124
+ async def main():
125
+ cfg = DockerSandboxConfig(
126
+ tools_config={'python_executor': {}}
127
+ )
128
+ async with SandboxFactory.create_sandbox(SandboxType.DOCKER, cfg) as sb:
129
+ r = await sb.execute_tool('python_executor', {
130
+ 'code': 'import platform; print(platform.python_version())'
131
+ })
132
+ print(r.output)
133
+
134
+ asyncio.run(main())
135
+ ```
136
+
137
+ ### 2) Local Unified Management: LocalSandboxManager (Multi-Sandbox, Lifecycle Management)
138
+
139
+ Usage Scenarios:
140
+
141
+ - Create/manage multiple sandboxes within the same process (creation, query, stop, periodic cleanup)
142
+ - Unified view for monitoring stats and health
143
+
144
+ Example:
145
+
146
+ ```python
147
+ import asyncio
148
+ from ms_enclave.sandbox.manager import LocalSandboxManager
149
+ from ms_enclave.sandbox.model import DockerSandboxConfig, SandboxType
150
+
151
+ async def main():
152
+ async with LocalSandboxManager() as manager:
153
+ cfg = DockerSandboxConfig(tools_config={'shell_executor': {}})
154
+ sandbox_id = await manager.create_sandbox(SandboxType.DOCKER, cfg)
155
+
156
+ # Execute command
157
+ res = await manager.execute_tool(sandbox_id, 'shell_executor', {'command': 'echo hello'})
158
+ print(res.output.strip()) # hello
159
+
160
+ # View list
161
+ infos = await manager.list_sandboxes()
162
+ print([i.id for i in infos])
163
+
164
+ # Stop and delete
165
+ await manager.stop_sandbox(sandbox_id)
166
+ await manager.delete_sandbox(sandbox_id)
167
+
168
+ asyncio.run(main())
169
+ ```
170
+
171
+ ### 3) Remote Unified Management: HttpSandboxManager (Cross-Machine/Isolated Deployment)
172
+
173
+ Usage Scenarios:
174
+
175
+ - Run sandbox services on dedicated hosts/containers, invoke remotely via HTTP
176
+ - Share a secure controlled sandbox cluster among multiple applications
177
+
178
+ Start the service (choose one):
179
+
180
+ ```bash
181
+ # Option A: Command line
182
+ ms-enclave server --host 0.0.0.0 --port 8000
183
+
184
+ # Option B: Python script
185
+ python -c "from ms_enclave.sandbox import create_server; create_server().run(host='0.0.0.0', port=8000)"
186
+ ```
187
+
188
+ Client Example:
189
+
190
+ ```python
191
+ import asyncio
192
+ from ms_enclave.sandbox.manager import HttpSandboxManager
193
+ from ms_enclave.sandbox.model import DockerSandboxConfig, SandboxType
194
+
195
+ async def main():
196
+ async with HttpSandboxManager(base_url='http://127.0.0.1:8000') as m:
197
+ cfg = DockerSandboxConfig(tools_config={'python_executor': {}})
198
+ sid = await m.create_sandbox(SandboxType.DOCKER, cfg)
199
+ r = await m.execute_tool(sid, 'python_executor', {'code': 'print("Hello remote")'})
200
+ print(r.output)
201
+ await m.delete_sandbox(sid)
202
+
203
+ asyncio.run(main())
204
+ ```
205
+
206
+ ---
207
+
208
+ ## Sandbox Types and Tool Support
209
+
210
+ Currently Supported Sandbox Types:
211
+
212
+ - DOCKER (General-purpose container execution)
213
+ - Supported tools:
214
+ - python_executor (execute Python code)
215
+ - shell_executor (execute Shell commands)
216
+ - file_operation (read/write/delete/list files)
217
+ - Features: Configurable memory/CPU limits, volume mounts, network toggling, privileged mode, port mapping
218
+
219
+ - DOCKER_NOTEBOOK (Jupyter Kernel Gateway environment)
220
+ - Supported tools:
221
+ - notebook_executor (execute code via Jupyter Kernel, supports context saving)
222
+ - Note: This type only loads notebook_executor; other DOCKER-specific tools won't be enabled in this sandbox.
223
+ - Dependencies: Requires port 8888 exposed and network enabled
224
+
225
+ Tool Loading Rules:
226
+
227
+ - Tools are initialized and made available only when explicitly declared in `tools_config`.
228
+ - Tools validate `required_sandbox_type`; unmatched types will be ignored automatically.
229
+
230
+ Example:
231
+
232
+ ```python
233
+ DockerSandboxConfig(tools_config={'python_executor': {}, 'shell_executor': {}, 'file_operation': {}})
234
+ DockerNotebookConfig(tools_config={'notebook_executor': {}})
235
+ ```
236
+
237
+ ---
238
+
239
+ ## Common Configuration Options
240
+
241
+ - `image`: Docker image name (e.g., `python:3.11-slim` or `jupyter-kernel-gateway`)
242
+ - `memory_limit`: Memory limit (e.g., `512m` / `1g`)
243
+ - `cpu_limit`: CPU limit (float > 0)
244
+ - `volumes`: Volume mounts, e.g., `{host_path: {"bind": "/container/path", "mode": "rw"}}`
245
+ - `ports`: Port mappings, e.g., `{ "8888/tcp": ("127.0.0.1", 8888) }`
246
+ - `network_enabled`: Enable network (Notebook sandbox requires True)
247
+ - `remove_on_exit`: Automatically remove container on exit (default True)
248
+
249
+ ---
250
+
251
+ ## Error Handling and Debugging
252
+
253
+ ```python
254
+ result = await sandbox.execute_tool('python_executor', {'code': 'print(1/0)'})
255
+ if result.error:
256
+ print('Error:', result.error)
257
+ else:
258
+ print('Output:', result.output)
259
+ ```
260
+
261
+ ---
262
+
263
+ ## Development and Testing
264
+
265
+ ```bash
266
+ # Clone the repository
267
+ git clone https://github.com/modelscope/ms-enclave.git
268
+ cd ms-enclave
269
+
270
+ # Setup virtual environment
271
+ python -m venv venv
272
+ source venv/bin/activate # Windows: venv\Scripts\activate
273
+
274
+ # Install dependencies
275
+ pip install -e ".[dev]"
276
+
277
+ # Run tests
278
+ pytest
279
+
280
+ # Run examples (provided in the repository)
281
+ python examples/sandbox_usage_examples.py
282
+ python examples/local_manager_example.py
283
+ python examples/server_manager_example.py
284
+ ```
285
+
286
+ ---
287
+
288
+ ## Available Tools
289
+
290
+ - `python_executor`: Execute Python code (DOCKER)
291
+ - `shell_executor`: Execute Shell commands (DOCKER)
292
+ - `file_operation`: Read/Write/Delete/List files (DOCKER)
293
+ - `notebook_executor`: Execute via Jupyter Kernel (DOCKER_NOTEBOOK)
294
+ - You can also register custom tools via the Tool factory (`@register_tool`).
295
+
296
+ ---
297
+
298
+ ## Contribution
299
+
300
+ We welcome contributions! Please check [CONTRIBUTING.md](CONTRIBUTING.md) for details.
301
+
302
+ ### Steps to Contribute
303
+
304
+ 1. Fork the repository
305
+ 2. Create a feature branch: `git checkout -b feature/amazing-feature`
306
+ 3. Develop and add tests
307
+ 4. Run local tests: `pytest`
308
+ 5. Commit changes: `git commit -m 'Add amazing feature'`
309
+ 6. Push the branch: `git push origin feature/amazing-feature`
310
+ 7. Submit a Pull Request
311
+
312
+ ## License
313
+
314
+ This project is licensed under the Apache 2.0 License. See [LICENSE](LICENSE) for details.
@@ -0,0 +1,40 @@
1
+ ms_enclave/__init__.py,sha256=IKXP5d9APyqXs14IU1mBKSr8tGxAwxeCtCnAWTOGhAU,98
2
+ ms_enclave/version.py,sha256=iIYc2bTwqcyBXtHZ-2FADaG3HGNUR2-XWW7pz0aAdlc,63
3
+ ms_enclave/cli/__init__.py,sha256=I_ANdxdcIHpkIzIXc1yKOlWwzb4oY0FwTPq1kYtgzQw,50
4
+ ms_enclave/cli/base.py,sha256=m1DFlF16L0Lyrn0YNuFj8ByGjVJIoI0jKzAoodIXjRk,404
5
+ ms_enclave/cli/cli.py,sha256=AoSPw65_7OBMT8qgv5vPz1S3Fo91Y6yluaDGGHbUDj0,693
6
+ ms_enclave/cli/start_server.py,sha256=FPmZ97MhWgsyXkl0y32D4XjhAak5Ogrt0Am3izRdi74,2858
7
+ ms_enclave/sandbox/__init__.py,sha256=OPU_W5fZE98IQ8_pAaYkN66R8TZw435Co8uw9oAyZo4,783
8
+ ms_enclave/sandbox/boxes/__init__.py,sha256=it3KgV8cAU4a1TQbiRz-kg7GgI5xA0nqhEAwEgUnpnQ,356
9
+ ms_enclave/sandbox/boxes/base.py,sha256=bpo5fXtfIUeIlhcQBtNyH1fiPbYJlOfKx2mNyaBiHhA,8376
10
+ ms_enclave/sandbox/boxes/docker_notebook.py,sha256=54s4SlttGpXSckVfncb75i1GsZ6FAkYRPeDVgzzHoQQ,8035
11
+ ms_enclave/sandbox/boxes/docker_sandbox.py,sha256=JhrFbY8k7ELwRsUcdsce9wND8G90xCKW-g3Bc4pJnec,9069
12
+ ms_enclave/sandbox/manager/__init__.py,sha256=juYJsmpRoCThcILPemx6VGU-brpNF4cbu1FPV-d-tNc,255
13
+ ms_enclave/sandbox/manager/base.py,sha256=nXznN8Yysgkgk8i-yfL29gAh2gBi3sdxPlQciTXnN9g,3848
14
+ ms_enclave/sandbox/manager/http_manager.py,sha256=heVehKDi_VMJ5g9BllzmK5o68c2WIagsl8e231SKWtc,15731
15
+ ms_enclave/sandbox/manager/local_manager.py,sha256=lv7vF9sNOThOPnCz9RzNTcD0pZHoNTVldVanfF7c5Io,9402
16
+ ms_enclave/sandbox/model/__init__.py,sha256=3Sbj5Id77MWOZ1IXHGbu2q5U_p5KMLKvwZEr1QwjUqg,550
17
+ ms_enclave/sandbox/model/base.py,sha256=1X5PaCUC5B5GXVCTzQMybqF7t0QEhWUkYFC-DyYXdhY,709
18
+ ms_enclave/sandbox/model/config.py,sha256=SfOUlzFY5pdzRX3Ts0T9Lc0J-WbbbNKLFnVMKiLXfyY,4580
19
+ ms_enclave/sandbox/model/requests.py,sha256=JDcANsACotKscWrLs0H_-J8BKEhALUNgmJnoy6_JZuA,2336
20
+ ms_enclave/sandbox/model/responses.py,sha256=AZ_BspRHzlqIP7xjI9XnoeQasIzJqMV8Qmdw23-KAwA,2502
21
+ ms_enclave/sandbox/server/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
22
+ ms_enclave/sandbox/server/server.py,sha256=SrSxl_C-4IudD5fXbi_cJ9EUv6A0EqxGmW9Opw4lsQE,7029
23
+ ms_enclave/sandbox/tools/__init__.py,sha256=McNOuC9E9Xsckp7vOAxv9TP5q6XEIUAsLbnme3kSvJI,167
24
+ ms_enclave/sandbox/tools/base.py,sha256=vLTJMFOQnfKyTyMMcgeq5f7KBFEFV5y7PFZmb4Ss0KE,2555
25
+ ms_enclave/sandbox/tools/sandbox_tool.py,sha256=iATkvNax7aae53ZnmMwmQDuDb5qcXEUI-aPXD5eVfzo,1370
26
+ ms_enclave/sandbox/tools/tool_info.py,sha256=A3RZLETWG9834J7r2KZfG6_JdV4UWnrFKSrEBfeCLHE,5223
27
+ ms_enclave/sandbox/tools/sandbox_tools/__init__.py,sha256=Mtm2jQTrztLbRVDBiitogIB3GdIrwF3Hwk5KlruyntQ,176
28
+ ms_enclave/sandbox/tools/sandbox_tools/file_operation.py,sha256=EdTvIjxnnG6kQ9y-6WESofyO6edqe_zee1wp-hsUBu0,8692
29
+ ms_enclave/sandbox/tools/sandbox_tools/notebook_executor.py,sha256=kZf9QA0yk-aFOJjtAbzYdKfNwjcoBSrscT9mWsv906w,6029
30
+ ms_enclave/sandbox/tools/sandbox_tools/python_executor.py,sha256=SlhenlO09B_eVfcnrqSZAVYC_JCMws0eXj-caR2BTmw,3259
31
+ ms_enclave/sandbox/tools/sandbox_tools/shell_executor.py,sha256=d5qPv6O1K1V91fsGAf9MiArcRuLtjFWEthi4iwtmAEE,2263
32
+ ms_enclave/utils/__init__.py,sha256=KYWYfti4m8TcWzjOfmollEfEArTgTasq2Zuaz9AkzZI,31
33
+ ms_enclave/utils/json_schema.py,sha256=hBhdMilb9_7JZaFZBb_THPrq8N4m_rJPD43mfwnTx14,7431
34
+ ms_enclave/utils/logger.py,sha256=kMqK-5d1S5uPmalHtEW8NuUiwuq3xR75i9xih4ayT58,3459
35
+ ms_enclave-0.0.1.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
36
+ ms_enclave-0.0.1.dist-info/METADATA,sha256=IBvVyPpZG1grwPjwhoCquEu-Qdms9z0eF1gCY5DSlHQ,9959
37
+ ms_enclave-0.0.1.dist-info/WHEEL,sha256=SmOxYU7pzNKBqASvQJ7DjX3XGUF92lrGhMb3R6_iiqI,91
38
+ ms_enclave-0.0.1.dist-info/entry_points.txt,sha256=Av3oIAE91Jj-742sTPA90ktrrq8lDowFC2RfXLwM8JM,58
39
+ ms_enclave-0.0.1.dist-info/top_level.txt,sha256=V_Q9rBOF-RGwACDP9ppukoyjaOtpjdht7dhe7StS86A,11
40
+ ms_enclave-0.0.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.9.0)
2
+ Generator: setuptools (79.0.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ ms-enclave = ms_enclave.cli.cli:run_cmd