scalebox-sdk 0.1.4__py3-none-any.whl → 0.1.25__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.
Files changed (68) hide show
  1. scalebox/__init__.py +1 -1
  2. scalebox/api/__init__.py +128 -128
  3. scalebox/api/client/__init__.py +8 -8
  4. scalebox/api/client/api/sandboxes/get_sandboxes.py +5 -3
  5. scalebox/api/client/api/sandboxes/get_sandboxes_sandbox_id_metrics.py +2 -2
  6. scalebox/api/client/api/sandboxes/post_sandboxes.py +2 -2
  7. scalebox/api/client/client.py +288 -288
  8. scalebox/api/client/models/listed_sandbox.py +11 -9
  9. scalebox/api/client/models/new_sandbox.py +1 -1
  10. scalebox/api/client/models/sandbox.py +125 -125
  11. scalebox/api/client/models/sandbox_state.py +1 -0
  12. scalebox/api/client/types.py +46 -46
  13. scalebox/code_interpreter/code_interpreter_async.py +370 -369
  14. scalebox/code_interpreter/code_interpreter_sync.py +318 -317
  15. scalebox/connection_config.py +92 -92
  16. scalebox/csx_desktop/main.py +12 -12
  17. scalebox/generated/api_pb2_connect.py +17 -66
  18. scalebox/sandbox_async/commands/command.py +307 -307
  19. scalebox/sandbox_async/commands/command_handle.py +187 -187
  20. scalebox/sandbox_async/commands/pty.py +187 -187
  21. scalebox/sandbox_async/filesystem/filesystem.py +557 -557
  22. scalebox/sandbox_async/filesystem/watch_handle.py +61 -61
  23. scalebox/sandbox_async/main.py +647 -646
  24. scalebox/sandbox_async/sandbox_api.py +365 -365
  25. scalebox/sandbox_async/utils.py +7 -7
  26. scalebox/sandbox_sync/__init__.py +2 -2
  27. scalebox/sandbox_sync/commands/command.py +300 -300
  28. scalebox/sandbox_sync/commands/command_handle.py +150 -150
  29. scalebox/sandbox_sync/commands/pty.py +181 -181
  30. scalebox/sandbox_sync/filesystem/filesystem.py +543 -543
  31. scalebox/sandbox_sync/filesystem/watch_handle.py +66 -66
  32. scalebox/sandbox_sync/main.py +789 -790
  33. scalebox/sandbox_sync/sandbox_api.py +356 -356
  34. scalebox/test/CODE_INTERPRETER_TESTS_READY.md +256 -256
  35. scalebox/test/README.md +164 -164
  36. scalebox/test/aclient.py +72 -72
  37. scalebox/test/code_interpreter_centext.py +21 -21
  38. scalebox/test/code_interpreter_centext_sync.py +21 -21
  39. scalebox/test/code_interpreter_test.py +1 -1
  40. scalebox/test/code_interpreter_test_sync.py +1 -1
  41. scalebox/test/run_all_validation_tests.py +334 -334
  42. scalebox/test/test_basic.py +78 -78
  43. scalebox/test/test_code_interpreter_async_comprehensive.py +2653 -2653
  44. scalebox/test/{test_code_interpreter_e2bsync_comprehensive.py → test_code_interpreter_execcode.py} +328 -392
  45. scalebox/test/test_code_interpreter_sync_comprehensive.py +3416 -3412
  46. scalebox/test/test_csx_desktop_examples.py +130 -0
  47. scalebox/test/test_sandbox_async_comprehensive.py +736 -738
  48. scalebox/test/test_sandbox_stress_and_edge_cases.py +778 -778
  49. scalebox/test/test_sandbox_sync_comprehensive.py +779 -770
  50. scalebox/test/test_sandbox_usage_examples.py +987 -987
  51. scalebox/test/testacreate.py +24 -24
  52. scalebox/test/testagetinfo.py +18 -18
  53. scalebox/test/testcodeinterpreter_async.py +508 -508
  54. scalebox/test/testcodeinterpreter_sync.py +239 -239
  55. scalebox/test/testcomputeuse.py +2 -2
  56. scalebox/test/testnovnc.py +12 -12
  57. scalebox/test/testsandbox_api.py +15 -0
  58. scalebox/test/testsandbox_async.py +202 -118
  59. scalebox/test/testsandbox_sync.py +71 -38
  60. scalebox/version.py +2 -2
  61. {scalebox_sdk-0.1.4.dist-info → scalebox_sdk-0.1.25.dist-info}/METADATA +104 -103
  62. {scalebox_sdk-0.1.4.dist-info → scalebox_sdk-0.1.25.dist-info}/RECORD +66 -66
  63. scalebox/test/test_code_interpreter_e2basync_comprehensive.py +0 -2655
  64. scalebox/test/test_e2b_first.py +0 -11
  65. {scalebox_sdk-0.1.4.dist-info → scalebox_sdk-0.1.25.dist-info}/WHEEL +0 -0
  66. {scalebox_sdk-0.1.4.dist-info → scalebox_sdk-0.1.25.dist-info}/entry_points.txt +0 -0
  67. {scalebox_sdk-0.1.4.dist-info → scalebox_sdk-0.1.25.dist-info}/licenses/LICENSE +0 -0
  68. {scalebox_sdk-0.1.4.dist-info → scalebox_sdk-0.1.25.dist-info}/top_level.txt +0 -0
@@ -1,307 +1,307 @@
1
- from typing import Dict, List, Literal, Optional, Union, overload
2
-
3
- import aiohttp
4
-
5
- from ... import csx_connect
6
- from ...connection_config import (
7
- KEEPALIVE_PING_HEADER,
8
- KEEPALIVE_PING_INTERVAL_SEC,
9
- ConnectionConfig,
10
- Username,
11
- )
12
- from ...exceptions import SandboxException
13
- from ...generated import api_pb2, api_pb2_connect
14
- from ...generated.rpc import handle_rpc_exception
15
- from ...sandbox.commands.command_handle import CommandResult
16
- from ...sandbox.commands.main import ProcessInfo
17
- from ...sandbox_async.commands.command_handle import AsyncCommandHandle, Stderr, Stdout
18
- from ...sandbox_async.utils import OutputHandler
19
-
20
-
21
- class Commands:
22
- """
23
- Module for executing commands in the sandbox.
24
- """
25
-
26
- def __init__(
27
- self,
28
- envd_api_url: str,
29
- connection_config: ConnectionConfig,
30
- pool: aiohttp.ClientSession,
31
- ) -> None:
32
- self._connection_config = connection_config
33
- self._rpc = api_pb2_connect.AsyncProcessClient(
34
- envd_api_url,
35
- http_client=pool
36
- )
37
- self._headers=connection_config.headers
38
- self._pool = pool
39
-
40
- async def list(
41
- self,
42
- request_timeout: Optional[float] = None,
43
- ) -> List[ProcessInfo]:
44
- """
45
- Lists all running commands and PTY sessions.
46
-
47
- :param request_timeout: Timeout for the request in **seconds**
48
-
49
- :return: List of running commands and PTY sessions
50
- """
51
- try:
52
- res = await self._rpc.list(
53
- api_pb2.ListRequest(),
54
- self._headers,
55
- timeout_seconds=self._connection_config.get_request_timeout(
56
- request_timeout
57
- ),
58
- )
59
- return [
60
- ProcessInfo(
61
- pid=p.pid,
62
- tag=p.tag,
63
- cmd=p.config.cmd,
64
- args=list(p.config.args),
65
- envs=dict(p.config.envs),
66
- cwd=p.config.cwd,
67
- )
68
- for p in res.processes
69
- ]
70
- except Exception as e:
71
- raise handle_rpc_exception(e)
72
-
73
- async def kill(
74
- self,
75
- pid: int,
76
- request_timeout: Optional[float] = None,
77
- ) -> bool:
78
- """
79
- Kill a running command specified by its process ID.
80
- It uses `SIGKILL` signal to kill the command.
81
-
82
- :param pid: Process ID of the command. You can get the list of processes using `sandbox.commands.list()`
83
- :param request_timeout: Timeout for the request in **seconds**
84
-
85
- :return: `True` if the command was killed, `False` if the command was not found
86
- """
87
- try:
88
- await self._rpc.send_signal(
89
- api_pb2.SendSignalRequest(
90
- process=api_pb2.ProcessSelector(pid=pid),
91
- signal=api_pb2.Signal.SIGNAL_SIGKILL,
92
- ),
93
- self._headers,
94
- timeout_seconds=self._connection_config.get_request_timeout(
95
- request_timeout
96
- ),
97
- )
98
- return True
99
- except Exception as e:
100
- if "not found" in str(e):
101
- return False
102
- raise handle_rpc_exception(e)
103
-
104
- async def send_stdin(
105
- self,
106
- pid: int,
107
- data: str,
108
- request_timeout: Optional[float] = None,
109
- ) -> None:
110
- """
111
- Send data to command stdin.
112
-
113
- :param pid Process ID of the command. You can get the list of processes using `sandbox.commands.list()`.
114
- :param data: Data to send to the command
115
- :param request_timeout: Timeout for the request in **seconds**
116
- """
117
- try:
118
- await self._rpc.send_input(
119
- api_pb2.SendInputRequest(
120
- process=api_pb2.ProcessSelector(pid=pid),
121
- input=api_pb2.ProcessInput(
122
- stdin=data.encode(),
123
- ),
124
- ),
125
- self._headers,
126
- timeout_seconds=self._connection_config.get_request_timeout(
127
- request_timeout
128
- ),
129
- )
130
- except Exception as e:
131
- print(e)
132
- raise handle_rpc_exception(e)
133
-
134
- @overload
135
- async def run(
136
- self,
137
- cmd: str,
138
- background: Union[Literal[False], None] = None,
139
- envs: Optional[Dict[str, str]] = None,
140
- user: Username = "user",
141
- cwd: Optional[str] = None,
142
- on_stdout: Optional[OutputHandler[Stdout]] = None,
143
- on_stderr: Optional[OutputHandler[Stderr]] = None,
144
- timeout: Optional[float] = 60,
145
- request_timeout: Optional[float] = None,
146
- ) -> CommandResult:
147
- """
148
- Start a new command and wait until it finishes executing.
149
-
150
- :param cmd: Command to execute
151
- :param background: **`False` if the command should be executed in the foreground**, `True` if the command should be executed in the background
152
- :param envs: Environment variables used for the command
153
- :param user: User to run the command as
154
- :param cwd: Working directory to run the command
155
- :param on_stdout: Callback for command stdout output
156
- :param on_stderr: Callback for command stderr output
157
- :param timeout: Timeout for the command connection in **seconds**. Using `0` will not limit the command connection time
158
- :param request_timeout: Timeout for the request in **seconds**
159
-
160
- :return: `CommandResult` result of the command execution
161
- """
162
- ...
163
-
164
- @overload
165
- async def run(
166
- self,
167
- cmd: str,
168
- background: Literal[True],
169
- envs: Optional[Dict[str, str]] = None,
170
- user: Username = "user",
171
- cwd: Optional[str] = None,
172
- on_stdout: Optional[OutputHandler[Stdout]] = None,
173
- on_stderr: Optional[OutputHandler[Stderr]] = None,
174
- timeout: Optional[float] = 60,
175
- request_timeout: Optional[float] = None,
176
- ) -> AsyncCommandHandle:
177
- """
178
- Start a new command and return a handle to interact with it.
179
-
180
- :param cmd: Command to execute
181
- :param background: `False` if the command should be executed in the foreground, **`True` if the command should be executed in the background**
182
- :param envs: Environment variables used for the command
183
- :param user: User to run the command as
184
- :param cwd: Working directory to run the command
185
- :param on_stdout: Callback for command stdout output
186
- :param on_stderr: Callback for command stderr output
187
- :param timeout: Timeout for the command connection in **seconds**. Using `0` will not limit the command connection time
188
- :param request_timeout: Timeout for the request in **seconds**
189
-
190
- :return: `AsyncCommandHandle` handle to interact with the running command
191
- """
192
- ...
193
-
194
- async def run(
195
- self,
196
- cmd: str,
197
- background: Union[bool, None] = None,
198
- envs: Optional[Dict[str, str]] = None,
199
- user: Username = "user",
200
- cwd: Optional[str] = None,
201
- on_stdout: Optional[OutputHandler[Stdout]] = None,
202
- on_stderr: Optional[OutputHandler[Stderr]] = None,
203
- timeout: Optional[float] = 60,
204
- request_timeout: Optional[float] = None,
205
- ):
206
- proc = await self._start(
207
- cmd,
208
- envs,
209
- user,
210
- cwd,
211
- timeout,
212
- request_timeout,
213
- on_stdout=on_stdout,
214
- on_stderr=on_stderr,
215
- )
216
- return proc if background else await proc.wait()
217
-
218
- async def _start(
219
- self,
220
- cmd: str,
221
- envs: Optional[Dict[str, str]] = None,
222
- user: Username = "user",
223
- cwd: Optional[str] = None,
224
- timeout: Optional[float] = 60,
225
- request_timeout: Optional[float] = None,
226
- on_stdout: Optional[OutputHandler[Stdout]] = None,
227
- on_stderr: Optional[OutputHandler[Stderr]] = None,
228
- ) -> AsyncCommandHandle:
229
- try:
230
- events = self._rpc.start(
231
- api_pb2.StartRequest(
232
- process=api_pb2.ProcessConfig(
233
- cmd="/bin/bash",
234
- envs=envs,
235
- args=["-l", "-c", cmd],
236
- cwd=cwd,
237
- ),
238
- ),
239
- self._headers,
240
- timeout_seconds=self._connection_config.get_request_timeout(
241
- request_timeout
242
- ),
243
- )
244
- start_event = await events.__anext__()
245
-
246
- if not start_event.HasField("event"):
247
- raise SandboxException(
248
- f"Failed to start process: expected start event, got {start_event}"
249
- )
250
-
251
- return AsyncCommandHandle(
252
- pid=start_event.event.start.pid,
253
- handle_kill=lambda: self.kill(start_event.event.start.pid),
254
- events=events,
255
- on_stdout=on_stdout,
256
- on_stderr=on_stderr,
257
- )
258
- except Exception as e:
259
- raise handle_rpc_exception(e)
260
-
261
- async def connect(
262
- self,
263
- pid: int,
264
- timeout: Optional[float] = 60,
265
- request_timeout: Optional[float] = None,
266
- on_stdout: Optional[OutputHandler[Stdout]] = None,
267
- on_stderr: Optional[OutputHandler[Stderr]] = None,
268
- ) -> AsyncCommandHandle:
269
- """
270
- Connects to a running command.
271
- You can use `AsyncCommandHandle.wait()` to wait for the command to finish and get execution results.
272
-
273
- :param pid: Process ID of the command to connect to. You can get the list of processes using `sandbox.commands.list()`
274
- :param request_timeout: Request timeout in **seconds**
275
- :param timeout: Timeout for the command connection in **seconds**. Using `0` will not limit the command connection time
276
- :param on_stdout: Callback for command stdout output
277
- :param on_stderr: Callback for command stderr output
278
-
279
- :return: `AsyncCommandHandle` handle to interact with the running command
280
- """
281
- events = self._rpc.connect(
282
- api_pb2.ConnectRequest(
283
- process=api_pb2.ProcessSelector(pid=pid),
284
- ),
285
- self._headers,
286
- timeout_seconds=self._connection_config.get_request_timeout(
287
- request_timeout
288
- ),
289
- )
290
-
291
- try:
292
- start_event = await events.__anext__()
293
-
294
- if not start_event.HasField("event"):
295
- raise SandboxException(
296
- f"Failed to connect to process: expected start event, got {start_event}"
297
- )
298
-
299
- return AsyncCommandHandle(
300
- pid=start_event.event.start.pid,
301
- handle_kill=lambda: self.kill(start_event.event.start.pid),
302
- events=events,
303
- on_stdout=on_stdout,
304
- on_stderr=on_stderr,
305
- )
306
- except Exception as e:
307
- raise handle_rpc_exception(e)
1
+ from typing import Dict, List, Literal, Optional, Union, overload
2
+
3
+ import aiohttp
4
+
5
+ from ... import csx_connect
6
+ from ...connection_config import (
7
+ KEEPALIVE_PING_HEADER,
8
+ KEEPALIVE_PING_INTERVAL_SEC,
9
+ ConnectionConfig,
10
+ Username,
11
+ )
12
+ from ...exceptions import SandboxException
13
+ from ...generated import api_pb2, api_pb2_connect
14
+ from ...generated.rpc import handle_rpc_exception
15
+ from ...sandbox.commands.command_handle import CommandResult
16
+ from ...sandbox.commands.main import ProcessInfo
17
+ from ...sandbox_async.commands.command_handle import AsyncCommandHandle, Stderr, Stdout
18
+ from ...sandbox_async.utils import OutputHandler
19
+
20
+
21
+ class Commands:
22
+ """
23
+ Module for executing commands in the sandbox.
24
+ """
25
+
26
+ def __init__(
27
+ self,
28
+ envd_api_url: str,
29
+ connection_config: ConnectionConfig,
30
+ pool: aiohttp.ClientSession,
31
+ ) -> None:
32
+ self._connection_config = connection_config
33
+ self._rpc = api_pb2_connect.AsyncProcessClient(
34
+ envd_api_url,
35
+ http_client=pool
36
+ )
37
+ self._headers=connection_config.headers
38
+ self._pool = pool
39
+
40
+ async def list(
41
+ self,
42
+ request_timeout: Optional[float] = None,
43
+ ) -> List[ProcessInfo]:
44
+ """
45
+ Lists all running commands and PTY sessions.
46
+
47
+ :param request_timeout: Timeout for the request in **seconds**
48
+
49
+ :return: List of running commands and PTY sessions
50
+ """
51
+ try:
52
+ res = await self._rpc.list(
53
+ api_pb2.ListRequest(),
54
+ self._headers,
55
+ timeout_seconds=self._connection_config.get_request_timeout(
56
+ request_timeout
57
+ ),
58
+ )
59
+ return [
60
+ ProcessInfo(
61
+ pid=p.pid,
62
+ tag=p.tag,
63
+ cmd=p.config.cmd,
64
+ args=list(p.config.args),
65
+ envs=dict(p.config.envs),
66
+ cwd=p.config.cwd,
67
+ )
68
+ for p in res.processes
69
+ ]
70
+ except Exception as e:
71
+ raise handle_rpc_exception(e)
72
+
73
+ async def kill(
74
+ self,
75
+ pid: int,
76
+ request_timeout: Optional[float] = None,
77
+ ) -> bool:
78
+ """
79
+ Kill a running command specified by its process ID.
80
+ It uses `SIGKILL` signal to kill the command.
81
+
82
+ :param pid: Process ID of the command. You can get the list of processes using `sandbox.commands.list()`
83
+ :param request_timeout: Timeout for the request in **seconds**
84
+
85
+ :return: `True` if the command was killed, `False` if the command was not found
86
+ """
87
+ try:
88
+ await self._rpc.send_signal(
89
+ api_pb2.SendSignalRequest(
90
+ process=api_pb2.ProcessSelector(pid=pid),
91
+ signal=api_pb2.Signal.SIGNAL_SIGKILL,
92
+ ),
93
+ self._headers,
94
+ timeout_seconds=self._connection_config.get_request_timeout(
95
+ request_timeout
96
+ ),
97
+ )
98
+ return True
99
+ except Exception as e:
100
+ if "not found" in str(e):
101
+ return False
102
+ raise handle_rpc_exception(e)
103
+
104
+ async def send_stdin(
105
+ self,
106
+ pid: int,
107
+ data: str,
108
+ request_timeout: Optional[float] = None,
109
+ ) -> None:
110
+ """
111
+ Send data to command stdin.
112
+
113
+ :param pid Process ID of the command. You can get the list of processes using `sandbox.commands.list()`.
114
+ :param data: Data to send to the command
115
+ :param request_timeout: Timeout for the request in **seconds**
116
+ """
117
+ try:
118
+ await self._rpc.send_input(
119
+ api_pb2.SendInputRequest(
120
+ process=api_pb2.ProcessSelector(pid=pid),
121
+ input=api_pb2.ProcessInput(
122
+ stdin=data.encode(),
123
+ ),
124
+ ),
125
+ self._headers,
126
+ timeout_seconds=self._connection_config.get_request_timeout(
127
+ request_timeout
128
+ ),
129
+ )
130
+ except Exception as e:
131
+ print(e)
132
+ raise handle_rpc_exception(e)
133
+
134
+ @overload
135
+ async def run(
136
+ self,
137
+ cmd: str,
138
+ background: Union[Literal[False], None] = None,
139
+ envs: Optional[Dict[str, str]] = None,
140
+ user: Username = "user",
141
+ cwd: Optional[str] = None,
142
+ on_stdout: Optional[OutputHandler[Stdout]] = None,
143
+ on_stderr: Optional[OutputHandler[Stderr]] = None,
144
+ timeout: Optional[float] = 60,
145
+ request_timeout: Optional[float] = None,
146
+ ) -> CommandResult:
147
+ """
148
+ Start a new command and wait until it finishes executing.
149
+
150
+ :param cmd: Command to execute
151
+ :param background: **`False` if the command should be executed in the foreground**, `True` if the command should be executed in the background
152
+ :param envs: Environment variables used for the command
153
+ :param user: User to run the command as
154
+ :param cwd: Working directory to run the command
155
+ :param on_stdout: Callback for command stdout output
156
+ :param on_stderr: Callback for command stderr output
157
+ :param timeout: Timeout for the command connection in **seconds**. Using `0` will not limit the command connection time
158
+ :param request_timeout: Timeout for the request in **seconds**
159
+
160
+ :return: `CommandResult` result of the command execution
161
+ """
162
+ ...
163
+
164
+ @overload
165
+ async def run(
166
+ self,
167
+ cmd: str,
168
+ background: Literal[True],
169
+ envs: Optional[Dict[str, str]] = None,
170
+ user: Username = "user",
171
+ cwd: Optional[str] = None,
172
+ on_stdout: Optional[OutputHandler[Stdout]] = None,
173
+ on_stderr: Optional[OutputHandler[Stderr]] = None,
174
+ timeout: Optional[float] = 60,
175
+ request_timeout: Optional[float] = None,
176
+ ) -> AsyncCommandHandle:
177
+ """
178
+ Start a new command and return a handle to interact with it.
179
+
180
+ :param cmd: Command to execute
181
+ :param background: `False` if the command should be executed in the foreground, **`True` if the command should be executed in the background**
182
+ :param envs: Environment variables used for the command
183
+ :param user: User to run the command as
184
+ :param cwd: Working directory to run the command
185
+ :param on_stdout: Callback for command stdout output
186
+ :param on_stderr: Callback for command stderr output
187
+ :param timeout: Timeout for the command connection in **seconds**. Using `0` will not limit the command connection time
188
+ :param request_timeout: Timeout for the request in **seconds**
189
+
190
+ :return: `AsyncCommandHandle` handle to interact with the running command
191
+ """
192
+ ...
193
+
194
+ async def run(
195
+ self,
196
+ cmd: str,
197
+ background: Union[bool, None] = None,
198
+ envs: Optional[Dict[str, str]] = None,
199
+ user: Username = "user",
200
+ cwd: Optional[str] = None,
201
+ on_stdout: Optional[OutputHandler[Stdout]] = None,
202
+ on_stderr: Optional[OutputHandler[Stderr]] = None,
203
+ timeout: Optional[float] = 60,
204
+ request_timeout: Optional[float] = None,
205
+ ):
206
+ proc = await self._start(
207
+ cmd,
208
+ envs,
209
+ user,
210
+ cwd,
211
+ timeout,
212
+ request_timeout,
213
+ on_stdout=on_stdout,
214
+ on_stderr=on_stderr,
215
+ )
216
+ return proc if background else await proc.wait()
217
+
218
+ async def _start(
219
+ self,
220
+ cmd: str,
221
+ envs: Optional[Dict[str, str]] = None,
222
+ user: Username = "user",
223
+ cwd: Optional[str] = None,
224
+ timeout: Optional[float] = 60,
225
+ request_timeout: Optional[float] = None,
226
+ on_stdout: Optional[OutputHandler[Stdout]] = None,
227
+ on_stderr: Optional[OutputHandler[Stderr]] = None,
228
+ ) -> AsyncCommandHandle:
229
+ try:
230
+ events = self._rpc.start(
231
+ api_pb2.StartRequest(
232
+ process=api_pb2.ProcessConfig(
233
+ cmd="/bin/bash",
234
+ envs=envs,
235
+ args=["-l", "-c", cmd],
236
+ cwd=cwd,
237
+ ),
238
+ ),
239
+ self._headers,
240
+ timeout_seconds=self._connection_config.get_request_timeout(
241
+ request_timeout
242
+ ),
243
+ )
244
+ start_event = await events.__anext__()
245
+
246
+ if not start_event.HasField("event"):
247
+ raise SandboxException(
248
+ f"Failed to start process: expected start event, got {start_event}"
249
+ )
250
+
251
+ return AsyncCommandHandle(
252
+ pid=start_event.event.start.pid,
253
+ handle_kill=lambda: self.kill(start_event.event.start.pid),
254
+ events=events,
255
+ on_stdout=on_stdout,
256
+ on_stderr=on_stderr,
257
+ )
258
+ except Exception as e:
259
+ raise handle_rpc_exception(e)
260
+
261
+ async def connect(
262
+ self,
263
+ pid: int,
264
+ timeout: Optional[float] = 60,
265
+ request_timeout: Optional[float] = None,
266
+ on_stdout: Optional[OutputHandler[Stdout]] = None,
267
+ on_stderr: Optional[OutputHandler[Stderr]] = None,
268
+ ) -> AsyncCommandHandle:
269
+ """
270
+ Connects to a running command.
271
+ You can use `AsyncCommandHandle.wait()` to wait for the command to finish and get execution results.
272
+
273
+ :param pid: Process ID of the command to connect to. You can get the list of processes using `sandbox.commands.list()`
274
+ :param request_timeout: Request timeout in **seconds**
275
+ :param timeout: Timeout for the command connection in **seconds**. Using `0` will not limit the command connection time
276
+ :param on_stdout: Callback for command stdout output
277
+ :param on_stderr: Callback for command stderr output
278
+
279
+ :return: `AsyncCommandHandle` handle to interact with the running command
280
+ """
281
+ events = self._rpc.connect(
282
+ api_pb2.ConnectRequest(
283
+ process=api_pb2.ProcessSelector(pid=pid),
284
+ ),
285
+ self._headers,
286
+ timeout_seconds=self._connection_config.get_request_timeout(
287
+ request_timeout
288
+ ),
289
+ )
290
+
291
+ try:
292
+ start_event = await events.__anext__()
293
+
294
+ if not start_event.HasField("event"):
295
+ raise SandboxException(
296
+ f"Failed to connect to process: expected start event, got {start_event}"
297
+ )
298
+
299
+ return AsyncCommandHandle(
300
+ pid=start_event.event.start.pid,
301
+ handle_kill=lambda: self.kill(start_event.event.start.pid),
302
+ events=events,
303
+ on_stdout=on_stdout,
304
+ on_stderr=on_stderr,
305
+ )
306
+ except Exception as e:
307
+ raise handle_rpc_exception(e)