vercel-sandbox-bundle 0.2.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.
Files changed (31) hide show
  1. vercel/sandbox/__init__.py +378 -0
  2. vercel/sandbox/__main__.py +47 -0
  3. vercel/sandbox/_internal/__init__.py +1 -0
  4. vercel/sandbox/_internal/api_client.py +1469 -0
  5. vercel/sandbox/_internal/async_filesystem_handle.py +243 -0
  6. vercel/sandbox/_internal/async_runtime.py +1445 -0
  7. vercel/sandbox/_internal/errors.py +179 -0
  8. vercel/sandbox/_internal/filesystem_handle_common.py +196 -0
  9. vercel/sandbox/_internal/filesystem_handle_core.py +341 -0
  10. vercel/sandbox/_internal/filesystem_write.py +182 -0
  11. vercel/sandbox/_internal/log_stream.py +29 -0
  12. vercel/sandbox/_internal/models.py +741 -0
  13. vercel/sandbox/_internal/options.py +76 -0
  14. vercel/sandbox/_internal/pagination.py +119 -0
  15. vercel/sandbox/_internal/process_output.py +105 -0
  16. vercel/sandbox/_internal/runtime_common.py +533 -0
  17. vercel/sandbox/_internal/service.py +994 -0
  18. vercel/sandbox/_internal/state.py +127 -0
  19. vercel/sandbox/_internal/streaming_archive.py +177 -0
  20. vercel/sandbox/_internal/sync_filesystem_handle.py +227 -0
  21. vercel/sandbox/_internal/sync_runtime.py +1362 -0
  22. vercel/sandbox/_internal/text_reader.py +377 -0
  23. vercel/sandbox/_vendor/__init__.py +1 -0
  24. vercel/sandbox/py.typed +1 -0
  25. vercel/sandbox/sync.py +371 -0
  26. vercel/sandbox/version.py +1 -0
  27. vercel_sandbox_bundle-0.2.0.dist-info/METADATA +49 -0
  28. vercel_sandbox_bundle-0.2.0.dist-info/RECORD +31 -0
  29. vercel_sandbox_bundle-0.2.0.dist-info/WHEEL +4 -0
  30. vercel_sandbox_bundle-0.2.0.dist-info/entry_points.txt +3 -0
  31. vercel_sandbox_bundle-0.2.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,378 @@
1
+ """Sandbox SDK surface."""
2
+
3
+ from collections.abc import AsyncIterator, Mapping
4
+
5
+ from vercel._internal.core.session import get_active_session
6
+ from vercel.sandbox._internal.async_filesystem_handle import (
7
+ SandboxBinaryReader,
8
+ SandboxBinaryWriter,
9
+ SandboxTextReader,
10
+ SandboxTextWriter,
11
+ )
12
+ from vercel.sandbox._internal.async_runtime import (
13
+ CreateSandboxOperation,
14
+ Process,
15
+ ResumeSandboxOperation,
16
+ Sandbox,
17
+ SandboxFilesystem,
18
+ SandboxFilesystemBatch,
19
+ SandboxRuntimeSession,
20
+ Snapshot,
21
+ create_sandbox_operation as _create_sandbox_operation,
22
+ get_sandbox as _get_sandbox,
23
+ get_snapshot as _get_snapshot,
24
+ query_sandboxes as _query_sandboxes,
25
+ query_sessions as _query_sessions,
26
+ query_snapshots as _query_snapshots,
27
+ resume_sandbox_operation as _resume_sandbox_operation,
28
+ )
29
+ from vercel.sandbox._internal.errors import (
30
+ SandboxApiError,
31
+ SandboxCleanupError,
32
+ SandboxCredentialsError,
33
+ SandboxError,
34
+ SandboxFilesystemCommandError,
35
+ SandboxFilesystemError,
36
+ SandboxFilesystemTransferError,
37
+ SandboxFilesystemWriteError,
38
+ SandboxInvalidHandleError,
39
+ SandboxPathNotFoundError,
40
+ SandboxResponseError,
41
+ SandboxStreamError,
42
+ SandboxTerminalStateError,
43
+ SandboxUploadSizeMismatchError,
44
+ )
45
+ from vercel.sandbox._internal.models import (
46
+ CompletedProcess,
47
+ DirectoryEntry,
48
+ DurationInput,
49
+ GitSource,
50
+ NetworkPolicy,
51
+ NetworkPolicyKeyValueMatcher,
52
+ NetworkPolicyMatcher,
53
+ NetworkPolicyRequestMatcher,
54
+ NetworkPolicyRule,
55
+ NetworkPolicySubnets,
56
+ NetworkPolicyTransform,
57
+ ProcessStatus,
58
+ SandboxQuery,
59
+ SandboxQueryByCreatedAt,
60
+ SandboxQueryByCurrentSnapshotId,
61
+ SandboxQueryByName,
62
+ SandboxQueryByStatusUpdatedAt,
63
+ SandboxResources,
64
+ SandboxSource,
65
+ SandboxStatus,
66
+ SnapshotExpiration,
67
+ SnapshotExpirationInput,
68
+ SnapshotRetention,
69
+ SnapshotSource,
70
+ TagFilter,
71
+ TarballSource,
72
+ )
73
+ from vercel.sandbox._internal.options import SandboxServiceOptions
74
+ from vercel.sandbox._internal.service import SandboxService, get_sandbox_service
75
+ from vercel.sandbox._internal.state import SnapshotRetentionState
76
+ from vercel.sandbox._internal.text_reader import TextReader
77
+
78
+ from . import sync
79
+
80
+
81
+ def _service() -> SandboxService:
82
+ return get_sandbox_service(get_active_session())
83
+
84
+
85
+ def create_sandbox(
86
+ *,
87
+ project_id: str | None = None,
88
+ name: str | None = None,
89
+ runtime: str | None = None,
90
+ source: SandboxSource | None = None,
91
+ ports: list[int] | None = None,
92
+ execution_time_limit: DurationInput = None,
93
+ resources: SandboxResources | None = None,
94
+ persistent: bool | None = None,
95
+ network_policy: NetworkPolicy | None = None,
96
+ env: Mapping[str, str] | None = None,
97
+ tags: Mapping[str, str] | None = None,
98
+ snapshot_expiration: SnapshotExpirationInput = None,
99
+ snapshot_retention: SnapshotRetention | None = None,
100
+ destroy: bool = True,
101
+ ) -> CreateSandboxOperation:
102
+ """Prepare an asynchronous sandbox creation operation.
103
+
104
+ Awaiting the returned operation performs no automatic cleanup. Using it as
105
+ an async context manager stops the sandbox on exit and destroys it by
106
+ default.
107
+
108
+ Args:
109
+ project_id: Project that owns the sandbox. Uses the active credentials
110
+ when omitted.
111
+ name: Requested sandbox name. The service generates one when omitted.
112
+ runtime: Runtime image or runtime identifier.
113
+ source: Git, tarball, or snapshot source used to initialize the sandbox.
114
+ ports: Ports to expose from the sandbox.
115
+ execution_time_limit: Maximum session runtime in seconds or as a
116
+ duration.
117
+ resources: Requested CPU and memory resources.
118
+ persistent: Whether the sandbox persists beyond its current session.
119
+ network_policy: Network access policy sent to the Sandbox API.
120
+ env: Environment variables for the sandbox.
121
+ tags: Metadata tags used to organize and query sandboxes.
122
+ snapshot_expiration: Default lifetime for snapshots created from this
123
+ sandbox.
124
+ snapshot_retention: Automatic snapshot retention policy.
125
+ destroy: Whether context-manager exit destroys the sandbox after
126
+ stopping it. Awaiting the operation never triggers cleanup.
127
+
128
+ Returns:
129
+ A single-use awaitable and async context manager for the new sandbox.
130
+
131
+ Raises:
132
+ SandboxTerminalStateError: If creation reaches a terminal failure
133
+ state. Raised when the operation is awaited or entered.
134
+ """
135
+ return _create_sandbox_operation(
136
+ _service(),
137
+ project_id=project_id,
138
+ name=name,
139
+ runtime=runtime,
140
+ source=source,
141
+ ports=ports,
142
+ execution_time_limit=execution_time_limit,
143
+ resources=resources,
144
+ persistent=persistent,
145
+ network_policy=network_policy,
146
+ env=env,
147
+ tags=tags,
148
+ snapshot_expiration=snapshot_expiration,
149
+ snapshot_retention=snapshot_retention,
150
+ destroy=destroy,
151
+ )
152
+
153
+
154
+ async def get_sandbox(
155
+ *,
156
+ name: str,
157
+ project_id: str | None = None,
158
+ include_system_routes: bool | None = None,
159
+ ) -> Sandbox:
160
+ """Fetch a sandbox by name without resuming it.
161
+
162
+ Args:
163
+ name: Sandbox name.
164
+ project_id: Project that owns the sandbox.
165
+ include_system_routes: Whether to include platform-managed routes.
166
+
167
+ Returns:
168
+ A handle for the requested sandbox.
169
+
170
+ Raises:
171
+ SandboxApiError: If the sandbox cannot be retrieved.
172
+ """
173
+ return await _get_sandbox(
174
+ _service(),
175
+ name=name,
176
+ project_id=project_id,
177
+ include_system_routes=include_system_routes,
178
+ )
179
+
180
+
181
+ def resume_sandbox(
182
+ *,
183
+ name: str,
184
+ project_id: str | None = None,
185
+ include_system_routes: bool | None = None,
186
+ ) -> ResumeSandboxOperation:
187
+ """Prepare an asynchronous sandbox resume operation.
188
+
189
+ Awaiting the returned operation performs no automatic cleanup. Using it as
190
+ an async context manager stops the active session on exit.
191
+
192
+ Args:
193
+ name: Sandbox name.
194
+ project_id: Project that owns the sandbox.
195
+ include_system_routes: Whether to include platform-managed routes.
196
+
197
+ Returns:
198
+ A single-use awaitable and async context manager for the sandbox.
199
+
200
+ Raises:
201
+ SandboxApiError: If the sandbox cannot be resumed. Raised when the
202
+ operation is awaited or entered.
203
+ """
204
+ return _resume_sandbox_operation(
205
+ _service(),
206
+ name=name,
207
+ project_id=project_id,
208
+ include_system_routes=include_system_routes,
209
+ )
210
+
211
+
212
+ def query_sandboxes(
213
+ *,
214
+ query: SandboxQuery | None = None,
215
+ project_id: str | None = None,
216
+ page_size: int | None = None,
217
+ cursor: str | None = None,
218
+ ) -> AsyncIterator[Sandbox]:
219
+ """Iterate over sandboxes matching a query.
220
+
221
+ Args:
222
+ query: Ordering and filtering options.
223
+ project_id: Project whose sandboxes should be queried.
224
+ page_size: Maximum number of sandboxes fetched per API request.
225
+ cursor: Cursor at which to begin pagination.
226
+
227
+ Returns:
228
+ An async iterator that transparently follows pagination cursors.
229
+ """
230
+ return _query_sandboxes(
231
+ _service(),
232
+ query=query,
233
+ project_id=project_id,
234
+ page_size=page_size,
235
+ cursor=cursor,
236
+ )
237
+
238
+
239
+ def query_sessions(
240
+ *,
241
+ project_id: str | None = None,
242
+ name: str | None = None,
243
+ page_size: int | None = None,
244
+ cursor: str | None = None,
245
+ sort_order: str | None = None,
246
+ ) -> AsyncIterator[SandboxRuntimeSession]:
247
+ """Iterate over runtime sessions.
248
+
249
+ Args:
250
+ project_id: Project whose sessions should be queried.
251
+ name: Sandbox name used to restrict the results.
252
+ page_size: Maximum number of sessions fetched per API request.
253
+ cursor: Cursor at which to begin pagination.
254
+ sort_order: Result order by creation time, either ``"asc"`` or
255
+ ``"desc"``.
256
+
257
+ Returns:
258
+ An async iterator that transparently follows pagination cursors.
259
+ """
260
+ return _query_sessions(
261
+ _service(),
262
+ project_id=project_id,
263
+ name=name,
264
+ page_size=page_size,
265
+ cursor=cursor,
266
+ sort_order=sort_order,
267
+ )
268
+
269
+
270
+ def query_snapshots(
271
+ *,
272
+ project_id: str | None = None,
273
+ name: str | None = None,
274
+ page_size: int | None = None,
275
+ cursor: str | None = None,
276
+ sort_order: str | None = None,
277
+ ) -> AsyncIterator[Snapshot]:
278
+ """Iterate over snapshots.
279
+
280
+ Args:
281
+ project_id: Project whose snapshots should be queried.
282
+ name: Sandbox name used to restrict the results.
283
+ page_size: Maximum number of snapshots fetched per API request.
284
+ cursor: Cursor at which to begin pagination.
285
+ sort_order: Result order by creation time, either ``"asc"`` or
286
+ ``"desc"``.
287
+
288
+ Returns:
289
+ An async iterator that transparently follows pagination cursors.
290
+ """
291
+ return _query_snapshots(
292
+ _service(),
293
+ project_id=project_id,
294
+ name=name,
295
+ page_size=page_size,
296
+ cursor=cursor,
297
+ sort_order=sort_order,
298
+ )
299
+
300
+
301
+ async def get_snapshot(*, snapshot_id: str) -> Snapshot:
302
+ """Get a snapshot by identifier.
303
+
304
+ Args:
305
+ snapshot_id: Snapshot identifier.
306
+
307
+ Returns:
308
+ A handle for the requested snapshot.
309
+
310
+ Raises:
311
+ SandboxApiError: If the snapshot cannot be retrieved.
312
+ """
313
+ return await _get_snapshot(_service(), snapshot_id=snapshot_id)
314
+
315
+
316
+ __all__ = [
317
+ "SandboxBinaryReader",
318
+ "SandboxBinaryWriter",
319
+ "SandboxTextReader",
320
+ "SandboxTextWriter",
321
+ "Sandbox",
322
+ "CreateSandboxOperation",
323
+ "SandboxApiError",
324
+ "SandboxCleanupError",
325
+ "ProcessStatus",
326
+ "Process",
327
+ "CompletedProcess",
328
+ "SandboxCredentialsError",
329
+ "SandboxError",
330
+ "SandboxFilesystem",
331
+ "SandboxFilesystemBatch",
332
+ "SandboxFilesystemCommandError",
333
+ "SandboxFilesystemError",
334
+ "SandboxFilesystemTransferError",
335
+ "SandboxFilesystemWriteError",
336
+ "SandboxInvalidHandleError",
337
+ "SandboxPathNotFoundError",
338
+ "SandboxUploadSizeMismatchError",
339
+ "NetworkPolicy",
340
+ "NetworkPolicyKeyValueMatcher",
341
+ "NetworkPolicyMatcher",
342
+ "NetworkPolicyRequestMatcher",
343
+ "NetworkPolicyRule",
344
+ "NetworkPolicySubnets",
345
+ "NetworkPolicyTransform",
346
+ "SandboxResources",
347
+ "SandboxQuery",
348
+ "SandboxQueryByCreatedAt",
349
+ "SandboxQueryByCurrentSnapshotId",
350
+ "SandboxQueryByName",
351
+ "SandboxQueryByStatusUpdatedAt",
352
+ "SandboxResponseError",
353
+ "ResumeSandboxOperation",
354
+ "SandboxStreamError",
355
+ "SandboxRuntimeSession",
356
+ "SandboxServiceOptions",
357
+ "SandboxSource",
358
+ "SandboxStatus",
359
+ "SandboxTerminalStateError",
360
+ "DirectoryEntry",
361
+ "GitSource",
362
+ "Snapshot",
363
+ "SnapshotExpiration",
364
+ "SnapshotRetention",
365
+ "SnapshotRetentionState",
366
+ "SnapshotSource",
367
+ "TagFilter",
368
+ "TarballSource",
369
+ "TextReader",
370
+ "create_sandbox",
371
+ "get_sandbox",
372
+ "get_snapshot",
373
+ "query_sandboxes",
374
+ "query_sessions",
375
+ "query_snapshots",
376
+ "resume_sandbox",
377
+ "sync",
378
+ ]
@@ -0,0 +1,47 @@
1
+ """CLI entry point that delegates to npx sandbox."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import shutil
7
+ import sys
8
+
9
+
10
+ def main() -> int:
11
+ """Run npx sandbox with all command line arguments.
12
+
13
+ This delegates to the TypeScript CLI for full functionality including
14
+ interactive shell, SSH, and other commands.
15
+
16
+ Returns:
17
+ Exit code (0 for success, non-zero for error).
18
+ """
19
+ if not shutil.which("npx"):
20
+ print(
21
+ "Error: 'npx' is not available. Please install Node.js and npm to use the CLI.",
22
+ file=sys.stderr,
23
+ )
24
+ print(
25
+ "\nNote: The SDK works without Node.js for programmatic use:",
26
+ file=sys.stderr,
27
+ )
28
+ print(
29
+ " from vercel import sandbox",
30
+ " from vercel.api import session",
31
+ file=sys.stderr,
32
+ )
33
+ print(
34
+ " async with session(): await sandbox.create_sandbox()",
35
+ file=sys.stderr,
36
+ )
37
+ return 1
38
+
39
+ # Replace current process with npx sandbox
40
+ os.execvp("npx", ["npx", "sandbox"] + sys.argv[1:])
41
+
42
+ # This line is never reached (execvp replaces the process)
43
+ return 0
44
+
45
+
46
+ if __name__ == "__main__":
47
+ sys.exit(main())
@@ -0,0 +1 @@
1
+ """Private implementation for the Vercel Sandbox SDK."""