@jini-ai/sandbox 0.4.0
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.
- package/CHANGELOG.md +16 -0
- package/LICENSE +201 -0
- package/README.md +209 -0
- package/dist/cjs/core/errors.d.ts +56 -0
- package/dist/cjs/core/errors.js +40 -0
- package/dist/cjs/core/index.d.ts +20 -0
- package/dist/cjs/core/index.js +22 -0
- package/dist/cjs/core/ports.d.ts +232 -0
- package/dist/cjs/core/ports.js +55 -0
- package/dist/cjs/e2b/categorize-e2b-error.d.ts +21 -0
- package/dist/cjs/e2b/categorize-e2b-error.js +27 -0
- package/dist/cjs/e2b/default-vite-react-template.d.ts +26 -0
- package/dist/cjs/e2b/default-vite-react-template.js +122 -0
- package/dist/cjs/e2b/e2b-sandbox-handle.d.ts +131 -0
- package/dist/cjs/e2b/e2b-sandbox-handle.js +28 -0
- package/dist/cjs/e2b/index.d.ts +18 -0
- package/dist/cjs/e2b/index.js +25 -0
- package/dist/cjs/e2b/provider.d.ts +63 -0
- package/dist/cjs/e2b/provider.js +92 -0
- package/dist/cjs/e2b/shell-quote.d.ts +21 -0
- package/dist/cjs/e2b/shell-quote.js +24 -0
- package/dist/cjs/e2b/to-array-buffer.d.ts +18 -0
- package/dist/cjs/e2b/to-array-buffer.js +21 -0
- package/dist/cjs/e2b/wrap-e2b-sandbox.d.ts +31 -0
- package/dist/cjs/e2b/wrap-e2b-sandbox.js +347 -0
- package/dist/cjs/node-worker.d.ts +111 -0
- package/dist/cjs/node-worker.js +157 -0
- package/dist/cjs/package.json +1 -0
- package/dist/core/errors.d.ts +56 -0
- package/dist/core/errors.js +36 -0
- package/dist/core/index.d.ts +20 -0
- package/dist/core/index.js +18 -0
- package/dist/core/ports.d.ts +232 -0
- package/dist/core/ports.js +54 -0
- package/dist/e2b/categorize-e2b-error.d.ts +21 -0
- package/dist/e2b/categorize-e2b-error.js +24 -0
- package/dist/e2b/default-vite-react-template.d.ts +26 -0
- package/dist/e2b/default-vite-react-template.js +119 -0
- package/dist/e2b/e2b-sandbox-handle.d.ts +131 -0
- package/dist/e2b/e2b-sandbox-handle.js +27 -0
- package/dist/e2b/index.d.ts +18 -0
- package/dist/e2b/index.js +15 -0
- package/dist/e2b/provider.d.ts +63 -0
- package/dist/e2b/provider.js +88 -0
- package/dist/e2b/shell-quote.d.ts +21 -0
- package/dist/e2b/shell-quote.js +21 -0
- package/dist/e2b/to-array-buffer.d.ts +18 -0
- package/dist/e2b/to-array-buffer.js +18 -0
- package/dist/e2b/wrap-e2b-sandbox.d.ts +31 -0
- package/dist/e2b/wrap-e2b-sandbox.js +343 -0
- package/dist/node-worker.d.ts +111 -0
- package/dist/node-worker.js +150 -0
- package/package.json +106 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Backend-neutral execution contracts.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* The vocabulary a host talks to a running sandbox through, with no statement about which
|
|
6
|
+
* backend that is. Every declaration here is an `interface`/`type` that erases at compile time,
|
|
7
|
+
* so this module contributes zero bytes and zero `require()` calls to a consumer's runtime graph.
|
|
8
|
+
* `SandboxOperationError` — the error type these methods reject with — lives in `errors.ts`
|
|
9
|
+
* instead, specifically so that claim stays true here rather than becoming "true except for one
|
|
10
|
+
* class." See `errors.ts`'s own doc comment for why it needs to be real runtime code.
|
|
11
|
+
*
|
|
12
|
+
* How it relates to the package:
|
|
13
|
+
* `core` is the half of `@jini-ai/sandbox` that must stay installable with no adapter present —
|
|
14
|
+
* a host that hasn't decided on E2B/local/WebContainer yet can still depend on `./core` for
|
|
15
|
+
* types and never compile `@e2b/code-interpreter`. That property is enforced the same way
|
|
16
|
+
* `@jini-ai/db`'s `core` enforces it against `better-sqlite3`: `pnpm guard`'s R12 check
|
|
17
|
+
* walks this directory's transitive import closure and fails if it reaches an adapter folder or
|
|
18
|
+
* an adapter package. See `scripts/check-driver-isolation.ts`.
|
|
19
|
+
*
|
|
20
|
+
* Architectural role:
|
|
21
|
+
* Ports. Adapters under `<adapter>/` implement `SandboxProviderPort`; host domain code depends
|
|
22
|
+
* on these and never on an adapter. The direction is one-way — `e2b` may import `core`, never
|
|
23
|
+
* the reverse — which is what lets `local` and `webcontainer` land later without restructuring
|
|
24
|
+
* either side.
|
|
25
|
+
*
|
|
26
|
+
* Three adapters are intended against this shape, only the first of which exists yet:
|
|
27
|
+
* - `e2b` (remote Firecracker microVM, this package's Slice 1) — a `PreviewTarget.url` is a
|
|
28
|
+
* forwarded public host, `boot()` genuinely takes seconds, the project root starts empty.
|
|
29
|
+
* - `local` (desktop: spawn `npm`/`vite` on the user's own machine, not yet built) — a
|
|
30
|
+
* `PreviewTarget.url` is `http://localhost:<port>`, `boot()` can resolve near-instantly, and
|
|
31
|
+
* the project root is the user's real, possibly-non-empty directory.
|
|
32
|
+
* - `webcontainer` (browser-only, blocked on WebContainers' commercial licensing, not yet
|
|
33
|
+
* built) — same shape again, just running in a tab instead of a VM or a desktop process.
|
|
34
|
+
* Both `e2b` and `local` are confirmed shipping targets — a host app that ships as both web and
|
|
35
|
+
* desktop needs the same project editable on either with identical observable results (same
|
|
36
|
+
* files in, same build/preview behavior out), not "one plus a maybe." A remote server-side
|
|
37
|
+
* backend other than E2B (e.g. Modal) is plausible later too; nothing below assumes E2B
|
|
38
|
+
* specifically — see the per-type notes for what was checked against that possibility.
|
|
39
|
+
*
|
|
40
|
+
* Nothing below may assume the sandbox is remote, that a preview is a public URL rather than
|
|
41
|
+
* `localhost`, or that `boot` is slow. A verb that only makes sense for one of the three belongs
|
|
42
|
+
* on that adapter's own class, not here — see the package README for what got excluded on that
|
|
43
|
+
* basis (session reconnection by ID, framework-specific scaffolding verbs).
|
|
44
|
+
*
|
|
45
|
+
* One asymmetry could NOT be unified away, so it's named here instead of papered over: `local`'s
|
|
46
|
+
* project root is the user's real directory and may already contain files this package never
|
|
47
|
+
* mounted (a `.git/` folder, files the user edited outside the sandbox entirely); `e2b`'s (and
|
|
48
|
+
* any other remote adapter's) project root starts empty except for what gets mounted into it.
|
|
49
|
+
* Nothing here promises the project root's contents are fully described by what this session's
|
|
50
|
+
* caller has mounted — code built against this interface must tolerate a project root that
|
|
51
|
+
* already has things in it.
|
|
52
|
+
*/
|
|
53
|
+
/**
|
|
54
|
+
* A file to place at a path relative to the sandbox's project root. `content` is a plain
|
|
55
|
+
* `string` for text — UTF-8 by convention — or raw bytes for anything else. Theme assets under
|
|
56
|
+
* `assets/images|fonts|video|audio` (`.png`, `.woff2`, `.mp4`, …) are exactly why this isn't
|
|
57
|
+
* `string` alone: mounting a `.woff2` as UTF-8 text would silently corrupt it, and "silently" is
|
|
58
|
+
* the dangerous part — it would look like it worked. `Uint8Array` rather than `ArrayBuffer` or a
|
|
59
|
+
* base64 string because it's what both Node (`Buffer` already extends it) and the browser share
|
|
60
|
+
* with no conversion — the same reason `readFile` returns it below.
|
|
61
|
+
*/
|
|
62
|
+
export interface SandboxFile {
|
|
63
|
+
readonly path: string;
|
|
64
|
+
readonly content: string | Uint8Array;
|
|
65
|
+
}
|
|
66
|
+
/** The result of running one command to completion. */
|
|
67
|
+
export interface CommandResult {
|
|
68
|
+
readonly stdout: string;
|
|
69
|
+
readonly stderr: string;
|
|
70
|
+
readonly exitCode: number;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Where the running dev server can currently be reached. Deliberately just a `url`, not a
|
|
74
|
+
* `publicUrl` — a local adapter returns `http://localhost:5173`, a remote one returns a
|
|
75
|
+
* forwarded host. Neither is more "canonical" than the other, and core must not encode a
|
|
76
|
+
* preference between them. Port allocation and port conflicts are a `local`-only concern (an
|
|
77
|
+
* already-bound port is meaningless for a fresh remote VM) and deliberately have no
|
|
78
|
+
* representation here — they surface as a `SandboxOperationError` with category `'port-in-use'`
|
|
79
|
+
* from whichever call first hits the conflict (typically `boot` or `startProcess`), not as a
|
|
80
|
+
* field on this type.
|
|
81
|
+
*/
|
|
82
|
+
export interface PreviewTarget {
|
|
83
|
+
readonly url: string;
|
|
84
|
+
}
|
|
85
|
+
/** What kind of change a sandbox-originated file event represents. */
|
|
86
|
+
export type FileChangeKind = 'created' | 'modified' | 'deleted';
|
|
87
|
+
/**
|
|
88
|
+
* A file change the sandbox made on its own — a lockfile write from `installDependencies`, an
|
|
89
|
+
* HMR-triggered rewrite from the dev server itself. Not a notification of changes the host made
|
|
90
|
+
* through `mountFiles`; those are already known to the caller that made them. `path` carries no
|
|
91
|
+
* content — call `readFile` in response if the caller needs to know what actually changed.
|
|
92
|
+
*/
|
|
93
|
+
export interface FileChangeEvent {
|
|
94
|
+
readonly path: string;
|
|
95
|
+
readonly kind: FileChangeKind;
|
|
96
|
+
}
|
|
97
|
+
/** Stops a previously registered listener from receiving further events. */
|
|
98
|
+
export type Unsubscribe = () => void;
|
|
99
|
+
/** One line of output from a still-running process, tagged by which stream it came from. Shared
|
|
100
|
+
* by `runCommand`'s optional live-output callback and `startProcess`'s `ProcessHandle` — one
|
|
101
|
+
* chunk shape for both rather than a second one for the foreground case. */
|
|
102
|
+
export interface ProcessOutputChunk {
|
|
103
|
+
readonly stream: 'stdout' | 'stderr';
|
|
104
|
+
readonly text: string;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* A still-running process the caller can observe and stop — what `startProcess` returns for a
|
|
108
|
+
* command that is not expected to exit on its own. This exists because `getPreview` is only
|
|
109
|
+
* meaningful once something like a dev server is listening, and a dev server is by definition a
|
|
110
|
+
* command `runCommand`'s "wait for it to finish" contract cannot represent. All three intended
|
|
111
|
+
* adapters have a native shape for this (E2B's `commands.run(..., { background: true })`, a
|
|
112
|
+
* detached `child_process.spawn`, a WebContainer `process.spawn`), so it belongs in core rather
|
|
113
|
+
* than being adapter-specific.
|
|
114
|
+
*/
|
|
115
|
+
export interface ProcessHandle {
|
|
116
|
+
/** Stop the process. Resolves once it has actually stopped. */
|
|
117
|
+
kill(): Promise<void>;
|
|
118
|
+
/** Subscribe to output as it's produced. Returns an `Unsubscribe`. */
|
|
119
|
+
onOutput(requiredArgs: {
|
|
120
|
+
listener: (chunk: ProcessOutputChunk) => void;
|
|
121
|
+
}): Unsubscribe;
|
|
122
|
+
}
|
|
123
|
+
/** Options shared by `runCommand` and `installDependencies` for observing output while the
|
|
124
|
+
* command is still in flight, without changing what either resolves with. */
|
|
125
|
+
export interface RunCommandOptions {
|
|
126
|
+
/** Called for each chunk of output as it's produced. The command the owner most wants to
|
|
127
|
+
* watch live — `npm install` — exits on its own and therefore goes through `runCommand`/
|
|
128
|
+
* `installDependencies`, not `startProcess`; without this, that output would be invisible
|
|
129
|
+
* until the whole install finished and then arrive as one frozen wall of text. */
|
|
130
|
+
readonly onOutput?: (chunk: ProcessOutputChunk) => void;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* One running sandbox instance: a project root plus a process the host can install into,
|
|
134
|
+
* command, preview, and watch. `SandboxProviderPort.boot()` returns this; every other verb
|
|
135
|
+
* happens through it. Every method below rejects with `SandboxOperationError` on failure.
|
|
136
|
+
*/
|
|
137
|
+
export interface SandboxSession {
|
|
138
|
+
/**
|
|
139
|
+
* Write (create or overwrite) files at their given paths. Upsert only — never deletes a file
|
|
140
|
+
* that isn't in `files`, on any adapter. This is not a simplification made for `e2b`'s sake: on
|
|
141
|
+
* `local`, `mountFiles` operates on the user's real project directory, so "replace the tree"
|
|
142
|
+
* would mean deleting files the user never gave this session permission to touch (a `.git/`
|
|
143
|
+
* folder, anything edited outside the sandbox). Because delete-by-omission is unsafe on
|
|
144
|
+
* `local`, upsert-only is the one semantic that's actually safe and correct on every adapter —
|
|
145
|
+
* there's no seam to name here, both backends already want the same behavior. A caller that
|
|
146
|
+
* needs to remove a file is out of scope for this package's first slice.
|
|
147
|
+
*/
|
|
148
|
+
mountFiles(requiredArgs: {
|
|
149
|
+
files: readonly SandboxFile[];
|
|
150
|
+
}): Promise<void>;
|
|
151
|
+
/** Read one file's current bytes. Always returns raw bytes, never a decoded string — a caller
|
|
152
|
+
* that knows a path is text decodes it itself (`new TextDecoder().decode(...)`); guessing
|
|
153
|
+
* encoding from a path or extension is exactly the kind of silent-corruption risk
|
|
154
|
+
* `SandboxFile.content` exists to avoid on the write side. Rejects with category `'not-found'`
|
|
155
|
+
* if nothing exists at `path`. */
|
|
156
|
+
readFile(requiredArgs: {
|
|
157
|
+
path: string;
|
|
158
|
+
}): Promise<Uint8Array>;
|
|
159
|
+
/** List project files under `directory` (default: the project root), as paths relative to the
|
|
160
|
+
* project root — the same convention `SandboxFile.path` and `FileChangeEvent.path` use.
|
|
161
|
+
* Adapters document their traversal limits and excluded dependency/build/VCS directories. */
|
|
162
|
+
listFiles(requiredArgs: Record<string, never>, optionalArgs?: {
|
|
163
|
+
directory?: string;
|
|
164
|
+
}): Promise<readonly string[]>;
|
|
165
|
+
/** Run one command and wait for it to exit. Not for commands that don't exit on their own —
|
|
166
|
+
* see `startProcess` for those. `options.onOutput`, if given, is called with each chunk as
|
|
167
|
+
* it's produced; the command still resolves with the complete `CommandResult` once it exits,
|
|
168
|
+
* callers that only need the final result can ignore the option entirely. */
|
|
169
|
+
runCommand(requiredArgs: {
|
|
170
|
+
command: string;
|
|
171
|
+
}, optionalArgs?: RunCommandOptions & {
|
|
172
|
+
args?: readonly string[];
|
|
173
|
+
}): Promise<CommandResult>;
|
|
174
|
+
/**
|
|
175
|
+
* Install dependencies: the sandbox's package manager with no args when `packages` is
|
|
176
|
+
* omitted, or install exactly the named packages when given. Kept distinct from `runCommand`
|
|
177
|
+
* rather than left as a caller-composed shell string because the three intended adapters have
|
|
178
|
+
* genuinely different install mechanics, not just different shells. Takes the same
|
|
179
|
+
* `RunCommandOptions` as `runCommand` for the same reason: `npm install`'s scrolling output is
|
|
180
|
+
* exactly what a live terminal view needs to show, and it exits on its own rather than running
|
|
181
|
+
* forever, so it belongs here rather than behind `startProcess`.
|
|
182
|
+
*/
|
|
183
|
+
installDependencies(requiredArgs: Record<string, never>, optionalArgs?: RunCommandOptions & {
|
|
184
|
+
packages?: readonly string[];
|
|
185
|
+
}): Promise<CommandResult>;
|
|
186
|
+
/** Start a command that is not expected to exit on its own — a dev server, most importantly.
|
|
187
|
+
* Returns as soon as the process has started, not once it's ready; use `getPreview` to find
|
|
188
|
+
* out once something is actually listening. */
|
|
189
|
+
startProcess(requiredArgs: {
|
|
190
|
+
command: string;
|
|
191
|
+
}, optionalArgs?: {
|
|
192
|
+
args?: readonly string[];
|
|
193
|
+
}): Promise<ProcessHandle>;
|
|
194
|
+
/** Where the dev server can currently be reached. Rejects (category `'timeout'`, typically) if
|
|
195
|
+
* nothing is listening yet. */
|
|
196
|
+
getPreview(): Promise<PreviewTarget>;
|
|
197
|
+
/** Subscribe to file changes the sandbox makes on its own. Returns an `Unsubscribe`. */
|
|
198
|
+
onFileChange(requiredArgs: {
|
|
199
|
+
listener: (event: FileChangeEvent) => void;
|
|
200
|
+
}): Unsubscribe;
|
|
201
|
+
/**
|
|
202
|
+
* Stop the sandbox and free its resources: every process this session started via
|
|
203
|
+
* `startProcess` is killed — a dev server that outlives `teardown` is, on `local`, an orphaned
|
|
204
|
+
* process holding a port on the user's own machine forever, so this is a hard requirement, not
|
|
205
|
+
* a nice-to-have. Never deletes the project root's files. On a remote adapter that's moot (the
|
|
206
|
+
* whole disposable VM goes away, and the user's real files were never in it to begin with); on
|
|
207
|
+
* `local`, where the project root IS the user's real directory, `teardown` must not touch the
|
|
208
|
+
* filesystem at all beyond stopping the processes it started. The session is unusable after
|
|
209
|
+
* this resolves.
|
|
210
|
+
*/
|
|
211
|
+
teardown(): Promise<void>;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* What a provider needs to start a session. Kept minimal on purpose: provider-specific options
|
|
215
|
+
* (an E2B API key, a local working directory root) belong on each adapter's own constructor or
|
|
216
|
+
* options type, not here — folding them in would make `core` describe backends it can't see.
|
|
217
|
+
*/
|
|
218
|
+
export interface BootOptions {
|
|
219
|
+
/** Hint for which base template/image to start from. Meaning is adapter-specific. */
|
|
220
|
+
readonly template?: string;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* The provider-level port: turns a still-cold environment into a running `SandboxSession`.
|
|
224
|
+
* Implemented once per backend. Rejects with `SandboxOperationError` — category `'unavailable'`
|
|
225
|
+
* for "couldn't reach or start the backend at all" (network failure, quota exceeded, `node` not
|
|
226
|
+
* on `PATH`), `'port-in-use'` for a pre-boot port conflict a `local` adapter can detect early,
|
|
227
|
+
* `'permission-denied'` for a rejected credential or an unwritable local directory.
|
|
228
|
+
*/
|
|
229
|
+
export interface SandboxProviderPort {
|
|
230
|
+
boot(requiredArgs: Record<string, never>, optionalArgs?: BootOptions): Promise<SandboxSession>;
|
|
231
|
+
}
|
|
232
|
+
//# sourceMappingURL=ports.d.ts.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file Backend-neutral execution contracts.
|
|
4
|
+
*
|
|
5
|
+
* Purpose:
|
|
6
|
+
* The vocabulary a host talks to a running sandbox through, with no statement about which
|
|
7
|
+
* backend that is. Every declaration here is an `interface`/`type` that erases at compile time,
|
|
8
|
+
* so this module contributes zero bytes and zero `require()` calls to a consumer's runtime graph.
|
|
9
|
+
* `SandboxOperationError` — the error type these methods reject with — lives in `errors.ts`
|
|
10
|
+
* instead, specifically so that claim stays true here rather than becoming "true except for one
|
|
11
|
+
* class." See `errors.ts`'s own doc comment for why it needs to be real runtime code.
|
|
12
|
+
*
|
|
13
|
+
* How it relates to the package:
|
|
14
|
+
* `core` is the half of `@jini-ai/sandbox` that must stay installable with no adapter present —
|
|
15
|
+
* a host that hasn't decided on E2B/local/WebContainer yet can still depend on `./core` for
|
|
16
|
+
* types and never compile `@e2b/code-interpreter`. That property is enforced the same way
|
|
17
|
+
* `@jini-ai/db`'s `core` enforces it against `better-sqlite3`: `pnpm guard`'s R12 check
|
|
18
|
+
* walks this directory's transitive import closure and fails if it reaches an adapter folder or
|
|
19
|
+
* an adapter package. See `scripts/check-driver-isolation.ts`.
|
|
20
|
+
*
|
|
21
|
+
* Architectural role:
|
|
22
|
+
* Ports. Adapters under `<adapter>/` implement `SandboxProviderPort`; host domain code depends
|
|
23
|
+
* on these and never on an adapter. The direction is one-way — `e2b` may import `core`, never
|
|
24
|
+
* the reverse — which is what lets `local` and `webcontainer` land later without restructuring
|
|
25
|
+
* either side.
|
|
26
|
+
*
|
|
27
|
+
* Three adapters are intended against this shape, only the first of which exists yet:
|
|
28
|
+
* - `e2b` (remote Firecracker microVM, this package's Slice 1) — a `PreviewTarget.url` is a
|
|
29
|
+
* forwarded public host, `boot()` genuinely takes seconds, the project root starts empty.
|
|
30
|
+
* - `local` (desktop: spawn `npm`/`vite` on the user's own machine, not yet built) — a
|
|
31
|
+
* `PreviewTarget.url` is `http://localhost:<port>`, `boot()` can resolve near-instantly, and
|
|
32
|
+
* the project root is the user's real, possibly-non-empty directory.
|
|
33
|
+
* - `webcontainer` (browser-only, blocked on WebContainers' commercial licensing, not yet
|
|
34
|
+
* built) — same shape again, just running in a tab instead of a VM or a desktop process.
|
|
35
|
+
* Both `e2b` and `local` are confirmed shipping targets — a host app that ships as both web and
|
|
36
|
+
* desktop needs the same project editable on either with identical observable results (same
|
|
37
|
+
* files in, same build/preview behavior out), not "one plus a maybe." A remote server-side
|
|
38
|
+
* backend other than E2B (e.g. Modal) is plausible later too; nothing below assumes E2B
|
|
39
|
+
* specifically — see the per-type notes for what was checked against that possibility.
|
|
40
|
+
*
|
|
41
|
+
* Nothing below may assume the sandbox is remote, that a preview is a public URL rather than
|
|
42
|
+
* `localhost`, or that `boot` is slow. A verb that only makes sense for one of the three belongs
|
|
43
|
+
* on that adapter's own class, not here — see the package README for what got excluded on that
|
|
44
|
+
* basis (session reconnection by ID, framework-specific scaffolding verbs).
|
|
45
|
+
*
|
|
46
|
+
* One asymmetry could NOT be unified away, so it's named here instead of papered over: `local`'s
|
|
47
|
+
* project root is the user's real directory and may already contain files this package never
|
|
48
|
+
* mounted (a `.git/` folder, files the user edited outside the sandbox entirely); `e2b`'s (and
|
|
49
|
+
* any other remote adapter's) project root starts empty except for what gets mounted into it.
|
|
50
|
+
* Nothing here promises the project root's contents are fully described by what this session's
|
|
51
|
+
* caller has mounted — code built against this interface must tolerate a project root that
|
|
52
|
+
* already has things in it.
|
|
53
|
+
*/
|
|
54
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
55
|
+
//# sourceMappingURL=ports.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Maps whatever the E2B SDK throws onto core's backend-neutral `SandboxErrorCategory`.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* Every `E2bSandboxHandle` call this adapter makes can fail, and every `SandboxSession` method
|
|
6
|
+
* must reject with a `SandboxOperationError` carrying one of core's six categories — not a raw
|
|
7
|
+
* SDK error, which would leak `/e2b`-specific detail through a port meant to hide it.
|
|
8
|
+
*
|
|
9
|
+
* The mapping is by error `.name`, checked against the real `e2b` package's exported error class
|
|
10
|
+
* names (`AuthenticationError`, `FileNotFoundError`, …) — as strings, not as imported classes.
|
|
11
|
+
* Importing those classes here would give `categorizeE2bError` (and therefore
|
|
12
|
+
* `wrap-e2b-sandbox.ts`, which has been import-free of the real SDK by design since Slice 1) a
|
|
13
|
+
* hard dependency on `@e2b/code-interpreter`. Matching by name keeps that boundary: only
|
|
14
|
+
* `provider.ts` needs the real SDK installed to run; this file needs it only to have once read
|
|
15
|
+
* its `.d.ts`, which is a very different kind of dependency and doesn't show up in `import`.
|
|
16
|
+
*/
|
|
17
|
+
import type { SandboxErrorCategory } from '../core/errors.js';
|
|
18
|
+
export declare function categorizeE2bError({ error }: {
|
|
19
|
+
error: unknown;
|
|
20
|
+
}): SandboxErrorCategory;
|
|
21
|
+
//# sourceMappingURL=categorize-e2b-error.d.ts.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.categorizeE2bError = categorizeE2bError;
|
|
4
|
+
/** Real `e2b` SDK error class names this adapter knows how to categorize, keyed to which
|
|
5
|
+
* `SandboxErrorCategory` they mean. Anything not in this map — including a caught value that
|
|
6
|
+
* isn't an `Error` at all — falls through to `'unknown'`. */
|
|
7
|
+
const KNOWN_ERROR_NAMES = {
|
|
8
|
+
AuthenticationError: 'permission-denied',
|
|
9
|
+
FileNotFoundError: 'not-found',
|
|
10
|
+
NotFoundError: 'not-found',
|
|
11
|
+
SandboxNotFoundError: 'not-found',
|
|
12
|
+
TimeoutError: 'timeout',
|
|
13
|
+
RateLimitError: 'unavailable',
|
|
14
|
+
NotEnoughSpaceError: 'unavailable',
|
|
15
|
+
};
|
|
16
|
+
/** The `.name` of whatever was thrown, or `undefined` for a non-`Error` throw (E2B's SDK
|
|
17
|
+
* shouldn't throw a bare string/object, but a caller of this function must not assume). */
|
|
18
|
+
function nameOf(error) {
|
|
19
|
+
return error instanceof Error ? error.name : undefined;
|
|
20
|
+
}
|
|
21
|
+
function categorizeE2bError({ error }) {
|
|
22
|
+
const name = nameOf(error);
|
|
23
|
+
if (name === undefined)
|
|
24
|
+
return 'unknown';
|
|
25
|
+
return KNOWN_ERROR_NAMES[name] ?? 'unknown';
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=categorize-e2b-error.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file A starter Vite + React + Tailwind project, as plain file data.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* Ported from open-lovable's `E2BProvider.setupViteApp` — the same file contents, minus the
|
|
6
|
+
* Python-heredoc plumbing that wrote them. That plumbing existed because open-lovable's E2B
|
|
7
|
+
* usage wrote files by executing a Python script inside the sandbox; the real
|
|
8
|
+
* `@e2b/code-interpreter` filesystem API this adapter uses (`files.write`) takes a plain string
|
|
9
|
+
* directly; a caller mounts this template with one `session.mountFiles({ files: DEFAULT_VITE_REACT_TEMPLATE })`
|
|
10
|
+
* call and no code execution is needed to place it.
|
|
11
|
+
*
|
|
12
|
+
* Architectural role:
|
|
13
|
+
* A convenience export, not part of the adapter's required surface. `SandboxProviderPort`/
|
|
14
|
+
* `SandboxSession` know nothing about Vite, React, or Tailwind — see `core/ports.ts`'s doc
|
|
15
|
+
* comment on why scaffolding a framework starter is a host-level concern, not the port's job.
|
|
16
|
+
* This lives under `./e2b` (not `./core`) for that reason: it is something an E2B-backed host
|
|
17
|
+
* may choose to use, not something every adapter must carry.
|
|
18
|
+
*/
|
|
19
|
+
import type { SandboxFile } from '../core/ports.js';
|
|
20
|
+
/** Same starter app open-lovable's `setupViteApp` produces: a working Vite + React + Tailwind
|
|
21
|
+
* project with one placeholder screen, dropped in as data rather than assembled by a script
|
|
22
|
+
* running inside the sandbox. `allowedHosts` in `vite.config.js` intentionally omits
|
|
23
|
+
* `.vercel.run` (open-lovable supports Vercel Sandbox as a second backend; this package does
|
|
24
|
+
* not — see the README's three-adapter plan). */
|
|
25
|
+
export declare const DEFAULT_VITE_REACT_TEMPLATE: readonly SandboxFile[];
|
|
26
|
+
//# sourceMappingURL=default-vite-react-template.d.ts.map
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_VITE_REACT_TEMPLATE = void 0;
|
|
4
|
+
const PACKAGE_JSON = `{
|
|
5
|
+
"name": "sandbox-app",
|
|
6
|
+
"version": "1.0.0",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"dev": "vite --host",
|
|
10
|
+
"build": "vite build",
|
|
11
|
+
"preview": "vite preview"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"react": "^18.2.0",
|
|
15
|
+
"react-dom": "^18.2.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@vitejs/plugin-react": "^4.0.0",
|
|
19
|
+
"vite": "^4.3.9",
|
|
20
|
+
"tailwindcss": "^3.3.0",
|
|
21
|
+
"postcss": "^8.4.31",
|
|
22
|
+
"autoprefixer": "^10.4.16"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
`;
|
|
26
|
+
const VITE_CONFIG = `import { defineConfig } from 'vite'
|
|
27
|
+
import react from '@vitejs/plugin-react'
|
|
28
|
+
|
|
29
|
+
export default defineConfig({
|
|
30
|
+
plugins: [react()],
|
|
31
|
+
server: {
|
|
32
|
+
host: '0.0.0.0',
|
|
33
|
+
port: 5173,
|
|
34
|
+
strictPort: true,
|
|
35
|
+
hmr: false,
|
|
36
|
+
allowedHosts: ['.e2b.app', '.e2b.dev', 'localhost', '127.0.0.1']
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
`;
|
|
40
|
+
const TAILWIND_CONFIG = `/** @type {import('tailwindcss').Config} */
|
|
41
|
+
export default {
|
|
42
|
+
content: [
|
|
43
|
+
"./index.html",
|
|
44
|
+
"./src/**/*.{js,ts,jsx,tsx}",
|
|
45
|
+
],
|
|
46
|
+
theme: {
|
|
47
|
+
extend: {},
|
|
48
|
+
},
|
|
49
|
+
plugins: [],
|
|
50
|
+
}
|
|
51
|
+
`;
|
|
52
|
+
const POSTCSS_CONFIG = `export default {
|
|
53
|
+
plugins: {
|
|
54
|
+
tailwindcss: {},
|
|
55
|
+
autoprefixer: {},
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
`;
|
|
59
|
+
const INDEX_HTML = `<!DOCTYPE html>
|
|
60
|
+
<html lang="en">
|
|
61
|
+
<head>
|
|
62
|
+
<meta charset="UTF-8" />
|
|
63
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
64
|
+
<title>Sandbox App</title>
|
|
65
|
+
</head>
|
|
66
|
+
<body>
|
|
67
|
+
<div id="root"></div>
|
|
68
|
+
<script type="module" src="/src/main.jsx"></script>
|
|
69
|
+
</body>
|
|
70
|
+
</html>
|
|
71
|
+
`;
|
|
72
|
+
const MAIN_JSX = `import React from 'react'
|
|
73
|
+
import ReactDOM from 'react-dom/client'
|
|
74
|
+
import App from './App.jsx'
|
|
75
|
+
import './index.css'
|
|
76
|
+
|
|
77
|
+
ReactDOM.createRoot(document.getElementById('root')).render(
|
|
78
|
+
<React.StrictMode>
|
|
79
|
+
<App />
|
|
80
|
+
</React.StrictMode>,
|
|
81
|
+
)
|
|
82
|
+
`;
|
|
83
|
+
const APP_JSX = `function App() {
|
|
84
|
+
return (
|
|
85
|
+
<div className="min-h-screen bg-gray-900 text-white flex items-center justify-center p-4">
|
|
86
|
+
<div className="text-center max-w-2xl">
|
|
87
|
+
<p className="text-lg text-gray-400">
|
|
88
|
+
Sandbox Ready<br/>
|
|
89
|
+
Start building your React app with Vite and Tailwind CSS!
|
|
90
|
+
</p>
|
|
91
|
+
</div>
|
|
92
|
+
</div>
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export default App
|
|
97
|
+
`;
|
|
98
|
+
const INDEX_CSS = `@tailwind base;
|
|
99
|
+
@tailwind components;
|
|
100
|
+
@tailwind utilities;
|
|
101
|
+
|
|
102
|
+
body {
|
|
103
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
|
|
104
|
+
background-color: rgb(17 24 39);
|
|
105
|
+
}
|
|
106
|
+
`;
|
|
107
|
+
/** Same starter app open-lovable's `setupViteApp` produces: a working Vite + React + Tailwind
|
|
108
|
+
* project with one placeholder screen, dropped in as data rather than assembled by a script
|
|
109
|
+
* running inside the sandbox. `allowedHosts` in `vite.config.js` intentionally omits
|
|
110
|
+
* `.vercel.run` (open-lovable supports Vercel Sandbox as a second backend; this package does
|
|
111
|
+
* not — see the README's three-adapter plan). */
|
|
112
|
+
exports.DEFAULT_VITE_REACT_TEMPLATE = [
|
|
113
|
+
{ path: 'package.json', content: PACKAGE_JSON },
|
|
114
|
+
{ path: 'vite.config.js', content: VITE_CONFIG },
|
|
115
|
+
{ path: 'tailwind.config.js', content: TAILWIND_CONFIG },
|
|
116
|
+
{ path: 'postcss.config.js', content: POSTCSS_CONFIG },
|
|
117
|
+
{ path: 'index.html', content: INDEX_HTML },
|
|
118
|
+
{ path: 'src/main.jsx', content: MAIN_JSX },
|
|
119
|
+
{ path: 'src/App.jsx', content: APP_JSX },
|
|
120
|
+
{ path: 'src/index.css', content: INDEX_CSS },
|
|
121
|
+
];
|
|
122
|
+
//# sourceMappingURL=default-vite-react-template.js.map
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The narrow slice of the E2B SDK this adapter actually calls.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* `@e2b/code-interpreter`'s real `Sandbox` class is much bigger than what this adapter needs
|
|
6
|
+
* (code-execution contexts, chart rendering, Jupyter — none of it relevant to running `npm`
|
|
7
|
+
* commands and a dev server). Depending on this narrower shape instead of the SDK's own type
|
|
8
|
+
* keeps the wrapping logic in `wrap-e2b-sandbox.ts` testable with a plain object literal: no
|
|
9
|
+
* network call, no API key, and no risk of a test silently depending on SDK internals this
|
|
10
|
+
* adapter never touches.
|
|
11
|
+
*
|
|
12
|
+
* Most of this interface is structurally satisfied by a real `Sandbox` instance directly.
|
|
13
|
+
* `files.write` is the one exception: E2B's real `write` is overloaded (a single-file form plus
|
|
14
|
+
* this interface's batch form), and TypeScript's assignability check between an overloaded
|
|
15
|
+
* source and a single-signature target does not reliably pick the matching overload, producing
|
|
16
|
+
* a confusing arity error rather than a real incompatibility. `provider.ts`'s `toE2bHandle`
|
|
17
|
+
* adapts the real `Sandbox` into this interface with one explicit wrapper function for that
|
|
18
|
+
* reason, rather than passing the SDK object straight through.
|
|
19
|
+
*
|
|
20
|
+
* Architectural role:
|
|
21
|
+
* The one place this package's `/e2b` subpath writes down what it assumes E2B's API looks like.
|
|
22
|
+
* If a real `@e2b/code-interpreter` upgrade changes these members, `provider.ts` (which is typed
|
|
23
|
+
* against both this interface and the SDK's real `Sandbox` type) fails to compile — a fast,
|
|
24
|
+
* local signal, rather than a runtime surprise inside `wrap-e2b-sandbox.ts`.
|
|
25
|
+
*/
|
|
26
|
+
/** The subset of E2B's `CommandResult` this adapter reads. */
|
|
27
|
+
export interface E2bCommandResult {
|
|
28
|
+
readonly stdout: string;
|
|
29
|
+
readonly stderr: string;
|
|
30
|
+
readonly exitCode: number;
|
|
31
|
+
}
|
|
32
|
+
/** The subset of E2B's background `CommandHandle` this adapter reads. */
|
|
33
|
+
export interface E2bCommandHandle {
|
|
34
|
+
kill(): Promise<boolean>;
|
|
35
|
+
}
|
|
36
|
+
export interface E2bRunOptions {
|
|
37
|
+
readonly background?: boolean;
|
|
38
|
+
readonly cwd?: string;
|
|
39
|
+
readonly onStdout?: (requiredArgs: {
|
|
40
|
+
data: string;
|
|
41
|
+
}) => void;
|
|
42
|
+
readonly onStderr?: (requiredArgs: {
|
|
43
|
+
data: string;
|
|
44
|
+
}) => void;
|
|
45
|
+
}
|
|
46
|
+
export interface E2bCommands {
|
|
47
|
+
/** Foreground: resolves once the command exits. Background: resolves once it has started. */
|
|
48
|
+
run(requiredArgs: {
|
|
49
|
+
command: string;
|
|
50
|
+
}, optionalArgs?: Omit<E2bRunOptions, 'background'>): Promise<E2bCommandResult>;
|
|
51
|
+
run(requiredArgs: {
|
|
52
|
+
command: string;
|
|
53
|
+
background: true;
|
|
54
|
+
}, optionalArgs?: Omit<E2bRunOptions, 'background'>): Promise<E2bCommandHandle>;
|
|
55
|
+
}
|
|
56
|
+
/** E2B's filesystem event kinds (real values, lowercase — confirmed against the installed
|
|
57
|
+
* `e2b` package's own `FilesystemEventType` enum, not assumed). `'chmod'` is a permissions
|
|
58
|
+
* change, not a content change — this adapter deliberately does not surface it (see
|
|
59
|
+
* `wrap-e2b-sandbox.ts`). */
|
|
60
|
+
export type E2bFilesystemEventType = 'chmod' | 'create' | 'remove' | 'rename' | 'write';
|
|
61
|
+
export interface E2bFilesystemEvent {
|
|
62
|
+
/** Path relative to the watched directory, per E2B's own `FilesystemEvent.name` doc. */
|
|
63
|
+
readonly name: string;
|
|
64
|
+
readonly type: E2bFilesystemEventType;
|
|
65
|
+
}
|
|
66
|
+
export interface E2bWatchHandle {
|
|
67
|
+
stop(): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
/** One file to write, as E2B's own batch `write` overload takes it. `data` is `string |
|
|
70
|
+
* ArrayBuffer` — not `Uint8Array` — because that's the real signature; converting a
|
|
71
|
+
* `Uint8Array` view to a correctly-scoped `ArrayBuffer` is `wrap-e2b-sandbox.ts`'s job, not
|
|
72
|
+
* this port's. */
|
|
73
|
+
export interface E2bWriteEntry {
|
|
74
|
+
readonly path: string;
|
|
75
|
+
readonly data: string | ArrayBuffer;
|
|
76
|
+
}
|
|
77
|
+
/** E2B's real filesystem-entry kind (`FileType` enum values, confirmed against the installed
|
|
78
|
+
* `e2b` package). Only `'file'` entries are files; `'dir'` and `'symlink'` are not. */
|
|
79
|
+
export type E2bFileType = 'file' | 'dir' | 'symlink';
|
|
80
|
+
/** The subset of E2B's `EntryInfo` this adapter reads from `files.list`. */
|
|
81
|
+
export interface E2bEntryInfo {
|
|
82
|
+
readonly path: string;
|
|
83
|
+
readonly type?: E2bFileType;
|
|
84
|
+
}
|
|
85
|
+
export interface E2bFilesystem {
|
|
86
|
+
/** Batched over E2B's own multi-file `write` overload — one round trip for however many
|
|
87
|
+
* files `mountFiles` is given, not one per file. E2B's real `write` is itself overloaded
|
|
88
|
+
* (a single-file form in addition to this batch one); `provider.ts` adapts the real
|
|
89
|
+
* `Sandbox` into this interface with an explicit wrapper function rather than relying on
|
|
90
|
+
* the real object satisfying this shape structurally — TypeScript's assignability check for
|
|
91
|
+
* a multi-overload source against a single-signature target picks the wrong overload to
|
|
92
|
+
* compare against and reports a confusing arity mismatch, so the real SDK is never passed
|
|
93
|
+
* through as-is. See `provider.ts`'s `toE2bHandle`. */
|
|
94
|
+
write(requiredArgs: {
|
|
95
|
+
files: readonly E2bWriteEntry[];
|
|
96
|
+
}): Promise<unknown>;
|
|
97
|
+
/** Always requested as raw bytes (`format: 'bytes'`) — this adapter never asks E2B to decode
|
|
98
|
+
* text on its behalf, matching `SandboxSession.readFile`'s "always raw bytes" contract. */
|
|
99
|
+
read(requiredArgs: {
|
|
100
|
+
path: string;
|
|
101
|
+
format: 'bytes';
|
|
102
|
+
}): Promise<Uint8Array>;
|
|
103
|
+
/** `depth` bounds how deep E2B's own server-side walk goes — see `MAX_LIST_DEPTH` in
|
|
104
|
+
* `wrap-e2b-sandbox.ts` for why a bound exists at all. */
|
|
105
|
+
list(requiredArgs: {
|
|
106
|
+
path: string;
|
|
107
|
+
}, optionalArgs?: {
|
|
108
|
+
readonly depth?: number;
|
|
109
|
+
}): Promise<readonly E2bEntryInfo[]>;
|
|
110
|
+
watchDir(requiredArgs: {
|
|
111
|
+
path: string;
|
|
112
|
+
onEvent: (event: E2bFilesystemEvent) => void;
|
|
113
|
+
}, opts?: {
|
|
114
|
+
readonly recursive?: boolean;
|
|
115
|
+
}): Promise<E2bWatchHandle>;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The E2B sandbox surface this adapter depends on. A real `Sandbox` instance from
|
|
119
|
+
* `@e2b/code-interpreter` satisfies this structurally; tests substitute a plain object.
|
|
120
|
+
*/
|
|
121
|
+
export interface E2bSandboxHandle {
|
|
122
|
+
readonly commands: E2bCommands;
|
|
123
|
+
readonly files: E2bFilesystem;
|
|
124
|
+
getHost(requiredArgs: {
|
|
125
|
+
port: number;
|
|
126
|
+
}): string;
|
|
127
|
+
/** `true` if the sandbox was killed, `false` if it was already gone — per the real SDK's
|
|
128
|
+
* `Sandbox.kill()` signature. */
|
|
129
|
+
kill(): Promise<boolean>;
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=e2b-sandbox-handle.d.ts.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file The narrow slice of the E2B SDK this adapter actually calls.
|
|
4
|
+
*
|
|
5
|
+
* Purpose:
|
|
6
|
+
* `@e2b/code-interpreter`'s real `Sandbox` class is much bigger than what this adapter needs
|
|
7
|
+
* (code-execution contexts, chart rendering, Jupyter — none of it relevant to running `npm`
|
|
8
|
+
* commands and a dev server). Depending on this narrower shape instead of the SDK's own type
|
|
9
|
+
* keeps the wrapping logic in `wrap-e2b-sandbox.ts` testable with a plain object literal: no
|
|
10
|
+
* network call, no API key, and no risk of a test silently depending on SDK internals this
|
|
11
|
+
* adapter never touches.
|
|
12
|
+
*
|
|
13
|
+
* Most of this interface is structurally satisfied by a real `Sandbox` instance directly.
|
|
14
|
+
* `files.write` is the one exception: E2B's real `write` is overloaded (a single-file form plus
|
|
15
|
+
* this interface's batch form), and TypeScript's assignability check between an overloaded
|
|
16
|
+
* source and a single-signature target does not reliably pick the matching overload, producing
|
|
17
|
+
* a confusing arity error rather than a real incompatibility. `provider.ts`'s `toE2bHandle`
|
|
18
|
+
* adapts the real `Sandbox` into this interface with one explicit wrapper function for that
|
|
19
|
+
* reason, rather than passing the SDK object straight through.
|
|
20
|
+
*
|
|
21
|
+
* Architectural role:
|
|
22
|
+
* The one place this package's `/e2b` subpath writes down what it assumes E2B's API looks like.
|
|
23
|
+
* If a real `@e2b/code-interpreter` upgrade changes these members, `provider.ts` (which is typed
|
|
24
|
+
* against both this interface and the SDK's real `Sandbox` type) fails to compile — a fast,
|
|
25
|
+
* local signal, rather than a runtime surprise inside `wrap-e2b-sandbox.ts`.
|
|
26
|
+
*/
|
|
27
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
+
//# sourceMappingURL=e2b-sandbox-handle.js.map
|