scalebox-sdk 0.1.24__py3-none-any.whl → 1.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.
Files changed (87) hide show
  1. scalebox/__init__.py +2 -2
  2. scalebox/api/__init__.py +130 -128
  3. scalebox/api/client/__init__.py +8 -8
  4. scalebox/api/client/api/sandboxes/get_sandboxes_sandbox_id_metrics.py +2 -2
  5. scalebox/api/client/api/sandboxes/post_sandboxes.py +2 -2
  6. scalebox/api/client/api/sandboxes/post_sandboxes_sandbox_id_connect.py +193 -0
  7. scalebox/api/client/client.py +288 -288
  8. scalebox/api/client/models/connect_sandbox.py +59 -0
  9. scalebox/api/client/models/error.py +2 -2
  10. scalebox/api/client/models/listed_sandbox.py +19 -1
  11. scalebox/api/client/models/new_sandbox.py +10 -0
  12. scalebox/api/client/models/sandbox.py +138 -125
  13. scalebox/api/client/models/sandbox_detail.py +24 -0
  14. scalebox/api/client/types.py +46 -46
  15. scalebox/cli.py +125 -125
  16. scalebox/client/aclient.py +57 -57
  17. scalebox/client/client.py +102 -102
  18. scalebox/code_interpreter/__init__.py +12 -12
  19. scalebox/code_interpreter/charts.py +230 -230
  20. scalebox/code_interpreter/constants.py +3 -3
  21. scalebox/code_interpreter/exceptions.py +13 -13
  22. scalebox/code_interpreter/models.py +485 -485
  23. scalebox/connection_config.py +34 -1
  24. scalebox/csx_connect/__init__.py +1 -1
  25. scalebox/csx_connect/client.py +485 -485
  26. scalebox/csx_desktop/main.py +651 -651
  27. scalebox/exceptions.py +83 -83
  28. scalebox/generated/api.py +61 -61
  29. scalebox/generated/api_pb2.py +203 -203
  30. scalebox/generated/api_pb2.pyi +956 -956
  31. scalebox/generated/api_pb2_connect.py +1407 -1407
  32. scalebox/generated/rpc.py +50 -50
  33. scalebox/sandbox/main.py +146 -139
  34. scalebox/sandbox/sandbox_api.py +105 -91
  35. scalebox/sandbox/signature.py +40 -40
  36. scalebox/sandbox/utils.py +34 -34
  37. scalebox/sandbox_async/commands/command.py +307 -307
  38. scalebox/sandbox_async/commands/command_handle.py +187 -187
  39. scalebox/sandbox_async/commands/pty.py +187 -187
  40. scalebox/sandbox_async/filesystem/filesystem.py +557 -557
  41. scalebox/sandbox_async/filesystem/watch_handle.py +61 -61
  42. scalebox/sandbox_async/main.py +228 -46
  43. scalebox/sandbox_async/sandbox_api.py +124 -3
  44. scalebox/sandbox_async/utils.py +7 -7
  45. scalebox/sandbox_sync/__init__.py +2 -2
  46. scalebox/sandbox_sync/commands/command.py +300 -300
  47. scalebox/sandbox_sync/commands/command_handle.py +150 -150
  48. scalebox/sandbox_sync/commands/pty.py +181 -181
  49. scalebox/sandbox_sync/filesystem/filesystem.py +3 -3
  50. scalebox/sandbox_sync/filesystem/watch_handle.py +66 -66
  51. scalebox/sandbox_sync/main.py +208 -133
  52. scalebox/sandbox_sync/sandbox_api.py +119 -3
  53. scalebox/test/CODE_INTERPRETER_TESTS_READY.md +323 -323
  54. scalebox/test/README.md +329 -329
  55. scalebox/test/bedrock_openai_adapter.py +67 -0
  56. scalebox/test/code_interpreter_test.py +34 -34
  57. scalebox/test/code_interpreter_test_sync.py +34 -34
  58. scalebox/test/run_stress_code_interpreter_sync.py +166 -0
  59. scalebox/test/simple_upload_example.py +123 -0
  60. scalebox/test/stabitiy_test.py +310 -0
  61. scalebox/test/test_browser_use.py +25 -0
  62. scalebox/test/test_browser_use_scalebox.py +61 -0
  63. scalebox/test/test_code_interpreter_sync_comprehensive.py +115 -65
  64. scalebox/test/test_connect_pause_async.py +277 -0
  65. scalebox/test/test_connect_pause_sync.py +267 -0
  66. scalebox/test/test_desktop_sandbox_sf.py +117 -0
  67. scalebox/test/test_download_url.py +49 -0
  68. scalebox/test/test_sandbox_async_comprehensive.py +1 -1
  69. scalebox/test/test_sandbox_object_storage_example.py +146 -0
  70. scalebox/test/test_sandbox_object_storage_example_async.py +156 -0
  71. scalebox/test/test_sf.py +137 -0
  72. scalebox/test/test_watch_dir_async.py +56 -0
  73. scalebox/test/testacreate.py +1 -1
  74. scalebox/test/testagetinfo.py +1 -1
  75. scalebox/test/testcomputeuse.py +243 -243
  76. scalebox/test/testsandbox_api.py +13 -0
  77. scalebox/test/testsandbox_sync.py +1 -1
  78. scalebox/test/upload_100mb_example.py +355 -0
  79. scalebox/utils/httpcoreclient.py +297 -297
  80. scalebox/utils/httpxclient.py +403 -403
  81. scalebox/version.py +2 -2
  82. {scalebox_sdk-0.1.24.dist-info → scalebox_sdk-1.0.1.dist-info}/METADATA +1 -1
  83. {scalebox_sdk-0.1.24.dist-info → scalebox_sdk-1.0.1.dist-info}/RECORD +87 -69
  84. {scalebox_sdk-0.1.24.dist-info → scalebox_sdk-1.0.1.dist-info}/WHEEL +1 -1
  85. {scalebox_sdk-0.1.24.dist-info → scalebox_sdk-1.0.1.dist-info}/entry_points.txt +0 -0
  86. {scalebox_sdk-0.1.24.dist-info → scalebox_sdk-1.0.1.dist-info}/licenses/LICENSE +0 -0
  87. {scalebox_sdk-0.1.24.dist-info → scalebox_sdk-1.0.1.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)