@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.
Files changed (53) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/LICENSE +201 -0
  3. package/README.md +209 -0
  4. package/dist/cjs/core/errors.d.ts +56 -0
  5. package/dist/cjs/core/errors.js +40 -0
  6. package/dist/cjs/core/index.d.ts +20 -0
  7. package/dist/cjs/core/index.js +22 -0
  8. package/dist/cjs/core/ports.d.ts +232 -0
  9. package/dist/cjs/core/ports.js +55 -0
  10. package/dist/cjs/e2b/categorize-e2b-error.d.ts +21 -0
  11. package/dist/cjs/e2b/categorize-e2b-error.js +27 -0
  12. package/dist/cjs/e2b/default-vite-react-template.d.ts +26 -0
  13. package/dist/cjs/e2b/default-vite-react-template.js +122 -0
  14. package/dist/cjs/e2b/e2b-sandbox-handle.d.ts +131 -0
  15. package/dist/cjs/e2b/e2b-sandbox-handle.js +28 -0
  16. package/dist/cjs/e2b/index.d.ts +18 -0
  17. package/dist/cjs/e2b/index.js +25 -0
  18. package/dist/cjs/e2b/provider.d.ts +63 -0
  19. package/dist/cjs/e2b/provider.js +92 -0
  20. package/dist/cjs/e2b/shell-quote.d.ts +21 -0
  21. package/dist/cjs/e2b/shell-quote.js +24 -0
  22. package/dist/cjs/e2b/to-array-buffer.d.ts +18 -0
  23. package/dist/cjs/e2b/to-array-buffer.js +21 -0
  24. package/dist/cjs/e2b/wrap-e2b-sandbox.d.ts +31 -0
  25. package/dist/cjs/e2b/wrap-e2b-sandbox.js +347 -0
  26. package/dist/cjs/node-worker.d.ts +111 -0
  27. package/dist/cjs/node-worker.js +157 -0
  28. package/dist/cjs/package.json +1 -0
  29. package/dist/core/errors.d.ts +56 -0
  30. package/dist/core/errors.js +36 -0
  31. package/dist/core/index.d.ts +20 -0
  32. package/dist/core/index.js +18 -0
  33. package/dist/core/ports.d.ts +232 -0
  34. package/dist/core/ports.js +54 -0
  35. package/dist/e2b/categorize-e2b-error.d.ts +21 -0
  36. package/dist/e2b/categorize-e2b-error.js +24 -0
  37. package/dist/e2b/default-vite-react-template.d.ts +26 -0
  38. package/dist/e2b/default-vite-react-template.js +119 -0
  39. package/dist/e2b/e2b-sandbox-handle.d.ts +131 -0
  40. package/dist/e2b/e2b-sandbox-handle.js +27 -0
  41. package/dist/e2b/index.d.ts +18 -0
  42. package/dist/e2b/index.js +15 -0
  43. package/dist/e2b/provider.d.ts +63 -0
  44. package/dist/e2b/provider.js +88 -0
  45. package/dist/e2b/shell-quote.d.ts +21 -0
  46. package/dist/e2b/shell-quote.js +21 -0
  47. package/dist/e2b/to-array-buffer.d.ts +18 -0
  48. package/dist/e2b/to-array-buffer.js +18 -0
  49. package/dist/e2b/wrap-e2b-sandbox.d.ts +31 -0
  50. package/dist/e2b/wrap-e2b-sandbox.js +343 -0
  51. package/dist/node-worker.d.ts +111 -0
  52. package/dist/node-worker.js +150 -0
  53. package/package.json +106 -0
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @module @jini-ai/sandbox/e2b
3
+ *
4
+ * The E2B (Firecracker microVM) adapter: `createE2bSandboxProvider` implements
5
+ * `@jini-ai/sandbox/core`'s `SandboxProviderPort` against `@e2b/code-interpreter`, an optional
6
+ * peer dependency of this package. Importing this entry point is what requires it to be
7
+ * installed.
8
+ */
9
+ export { createE2bSandboxProvider } from './provider.js';
10
+ export type { E2bProviderConfig } from './provider.js';
11
+ export { DEFAULT_VITE_REACT_TEMPLATE } from './default-vite-react-template.js';
12
+ export { mapE2bFileChangeKind, wrapE2bSandbox } from './wrap-e2b-sandbox.js';
13
+ export type { E2bSessionConfig } from './wrap-e2b-sandbox.js';
14
+ export { categorizeE2bError } from './categorize-e2b-error.js';
15
+ export { toArrayBuffer } from './to-array-buffer.js';
16
+ export { shellQuote } from './shell-quote.js';
17
+ export type { E2bCommandHandle, E2bCommandResult, E2bCommands, E2bEntryInfo, E2bFilesystem, E2bFileType, E2bFilesystemEvent, E2bFilesystemEventType, E2bRunOptions, E2bSandboxHandle, E2bWatchHandle, E2bWriteEntry, } from './e2b-sandbox-handle.js';
18
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shellQuote = exports.toArrayBuffer = exports.categorizeE2bError = exports.wrapE2bSandbox = exports.mapE2bFileChangeKind = exports.DEFAULT_VITE_REACT_TEMPLATE = exports.createE2bSandboxProvider = void 0;
4
+ /**
5
+ * @module @jini-ai/sandbox/e2b
6
+ *
7
+ * The E2B (Firecracker microVM) adapter: `createE2bSandboxProvider` implements
8
+ * `@jini-ai/sandbox/core`'s `SandboxProviderPort` against `@e2b/code-interpreter`, an optional
9
+ * peer dependency of this package. Importing this entry point is what requires it to be
10
+ * installed.
11
+ */
12
+ var provider_js_1 = require("./provider.js");
13
+ Object.defineProperty(exports, "createE2bSandboxProvider", { enumerable: true, get: function () { return provider_js_1.createE2bSandboxProvider; } });
14
+ var default_vite_react_template_js_1 = require("./default-vite-react-template.js");
15
+ Object.defineProperty(exports, "DEFAULT_VITE_REACT_TEMPLATE", { enumerable: true, get: function () { return default_vite_react_template_js_1.DEFAULT_VITE_REACT_TEMPLATE; } });
16
+ var wrap_e2b_sandbox_js_1 = require("./wrap-e2b-sandbox.js");
17
+ Object.defineProperty(exports, "mapE2bFileChangeKind", { enumerable: true, get: function () { return wrap_e2b_sandbox_js_1.mapE2bFileChangeKind; } });
18
+ Object.defineProperty(exports, "wrapE2bSandbox", { enumerable: true, get: function () { return wrap_e2b_sandbox_js_1.wrapE2bSandbox; } });
19
+ var categorize_e2b_error_js_1 = require("./categorize-e2b-error.js");
20
+ Object.defineProperty(exports, "categorizeE2bError", { enumerable: true, get: function () { return categorize_e2b_error_js_1.categorizeE2bError; } });
21
+ var to_array_buffer_js_1 = require("./to-array-buffer.js");
22
+ Object.defineProperty(exports, "toArrayBuffer", { enumerable: true, get: function () { return to_array_buffer_js_1.toArrayBuffer; } });
23
+ var shell_quote_js_1 = require("./shell-quote.js");
24
+ Object.defineProperty(exports, "shellQuote", { enumerable: true, get: function () { return shell_quote_js_1.shellQuote; } });
25
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @file The E2B adapter's `SandboxProviderPort` implementation.
3
+ *
4
+ * Purpose:
5
+ * Turns a `BootOptions` request into a real `@e2b/code-interpreter` `Sandbox` and wraps it into
6
+ * a `SandboxSession` via `wrap-e2b-sandbox.ts`. This is the only file in `./e2b` that imports
7
+ * the SDK itself — everything else depends on the narrow `E2bSandboxHandle` port, which is why
8
+ * only live SDK integration needs a real API key or network access. The dedicated
9
+ * `__tests__/provider.test.ts` exercises provider translation and failures with a mocked SDK.
10
+ *
11
+ * Architectural role:
12
+ * The adapter's public entry point. `@e2b/code-interpreter` is an optional peer dependency of
13
+ * this package (see package.json); importing this module is what actually requires it to be
14
+ * installed. `./core` never imports from here.
15
+ */
16
+ import { Sandbox } from '@e2b/code-interpreter';
17
+ import type { SandboxProviderPort } from '../core/ports.js';
18
+ import type { E2bSandboxHandle } from './e2b-sandbox-handle.js';
19
+ export interface E2bProviderConfig {
20
+ /** E2B API key. Omit to use the SDK's own `E2B_API_KEY` environment variable fallback — this
21
+ * adapter does not duplicate that lookup. */
22
+ readonly apiKey?: string;
23
+ /** Sandbox lifetime in milliseconds. Omit to use the SDK's own default (5 minutes). */
24
+ readonly timeoutMs?: number;
25
+ /** Absolute path inside the sandbox used as the project root.
26
+ * @default '/home/user/app' */
27
+ readonly projectRoot?: string;
28
+ /** Port `getPreview` asks E2B to forward — the port the caller's dev server binds to.
29
+ * @default 5173 */
30
+ readonly previewPort?: number;
31
+ /** How long `getPreview` waits for a single response before deciding nothing is listening.
32
+ * @default 3000 */
33
+ readonly previewCheckTimeoutMs?: number;
34
+ }
35
+ /**
36
+ * Adapts a real `Sandbox` into the object-argument `E2bSandboxHandle` port.
37
+ * SDK positional arguments are translated only at this boundary. `files` is wrapped with one explicit function per member instead, because
38
+ * TypeScript's assignability check between E2B's overloaded `write` (a single-file form plus a
39
+ * batch form) and this interface's batch-only signature doesn't reliably resolve to the
40
+ * matching overload — seen directly as a real, reproducible compile error, not a
41
+ * theoretical concern — so the real object is never passed through as-is for that one member.
42
+ * Calling `sandbox.files.write(files)` here, with a concrete array argument, invokes real
43
+ * per-call overload resolution correctly; it's only the *object-to-interface* structural check
44
+ * that gets confused.
45
+ *
46
+ * Exported (not part of the package's public `./e2b` barrel) so `__tests__/to-e2b-handle.test.ts`
47
+ * can exercise the translation directly against a fake `Sandbox`-shaped object, rather than only
48
+ * indirectly through `boot`'s mocked-out `Sandbox.create` call.
49
+ */
50
+ export declare function toE2bHandle({ sandbox }: {
51
+ sandbox: Sandbox;
52
+ }): E2bSandboxHandle;
53
+ export declare function createE2bSandboxProvider(_requiredArgs: Record<string, never>, config?: E2bProviderConfig & {
54
+ createSandbox?: (requiredArgs: {
55
+ options: {
56
+ apiKey?: string;
57
+ timeoutMs?: number;
58
+ template?: string;
59
+ };
60
+ }) => Promise<Sandbox>;
61
+ fetchImpl?: typeof fetch;
62
+ }): SandboxProviderPort;
63
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toE2bHandle = toE2bHandle;
4
+ exports.createE2bSandboxProvider = createE2bSandboxProvider;
5
+ /**
6
+ * @file The E2B adapter's `SandboxProviderPort` implementation.
7
+ *
8
+ * Purpose:
9
+ * Turns a `BootOptions` request into a real `@e2b/code-interpreter` `Sandbox` and wraps it into
10
+ * a `SandboxSession` via `wrap-e2b-sandbox.ts`. This is the only file in `./e2b` that imports
11
+ * the SDK itself — everything else depends on the narrow `E2bSandboxHandle` port, which is why
12
+ * only live SDK integration needs a real API key or network access. The dedicated
13
+ * `__tests__/provider.test.ts` exercises provider translation and failures with a mocked SDK.
14
+ *
15
+ * Architectural role:
16
+ * The adapter's public entry point. `@e2b/code-interpreter` is an optional peer dependency of
17
+ * this package (see package.json); importing this module is what actually requires it to be
18
+ * installed. `./core` never imports from here.
19
+ */
20
+ const code_interpreter_1 = require("@e2b/code-interpreter");
21
+ const errors_js_1 = require("../core/errors.js");
22
+ const categorize_e2b_error_js_1 = require("./categorize-e2b-error.js");
23
+ const wrap_e2b_sandbox_js_1 = require("./wrap-e2b-sandbox.js");
24
+ const DEFAULT_PROJECT_ROOT = '/home/user/app';
25
+ const DEFAULT_PREVIEW_PORT = 5173;
26
+ const DEFAULT_PREVIEW_CHECK_TIMEOUT_MS = 3_000;
27
+ /**
28
+ * Adapts a real `Sandbox` into the object-argument `E2bSandboxHandle` port.
29
+ * SDK positional arguments are translated only at this boundary. `files` is wrapped with one explicit function per member instead, because
30
+ * TypeScript's assignability check between E2B's overloaded `write` (a single-file form plus a
31
+ * batch form) and this interface's batch-only signature doesn't reliably resolve to the
32
+ * matching overload — seen directly as a real, reproducible compile error, not a
33
+ * theoretical concern — so the real object is never passed through as-is for that one member.
34
+ * Calling `sandbox.files.write(files)` here, with a concrete array argument, invokes real
35
+ * per-call overload resolution correctly; it's only the *object-to-interface* structural check
36
+ * that gets confused.
37
+ *
38
+ * Exported (not part of the package's public `./e2b` barrel) so `__tests__/to-e2b-handle.test.ts`
39
+ * can exercise the translation directly against a fake `Sandbox`-shaped object, rather than only
40
+ * indirectly through `boot`'s mocked-out `Sandbox.create` call.
41
+ */
42
+ function toE2bHandle({ sandbox }) {
43
+ // Return the port union explicitly so overload compatibility does not require SDK-only members.
44
+ function run(requiredArgs, optionalArgs = {}) {
45
+ const { onStdout, onStderr, ...settings } = optionalArgs;
46
+ const sdkOptions = {
47
+ ...settings,
48
+ ...(onStdout === undefined ? {} : { onStdout: (data) => onStdout({ data }) }),
49
+ ...(onStderr === undefined ? {} : { onStderr: (data) => onStderr({ data }) }),
50
+ };
51
+ return requiredArgs.background === true
52
+ ? sandbox.commands.run(requiredArgs.command, { ...sdkOptions, background: true })
53
+ : sandbox.commands.run(requiredArgs.command, sdkOptions);
54
+ }
55
+ return {
56
+ commands: { run },
57
+ files: {
58
+ write: ({ files }) => sandbox.files.write([...files]),
59
+ read: ({ path, format }) => sandbox.files.read(path, { format }),
60
+ list: ({ path }, opts) => sandbox.files.list(path, opts),
61
+ watchDir: ({ path, onEvent }, opts) => sandbox.files.watchDir(path, onEvent, opts),
62
+ },
63
+ getHost: ({ port }) => sandbox.getHost(port),
64
+ kill: () => sandbox.kill(),
65
+ };
66
+ }
67
+ function createE2bSandboxProvider(_requiredArgs, config = {}) {
68
+ return {
69
+ async boot(_requiredArgs, options) {
70
+ const createOptions = {
71
+ ...(config.apiKey !== undefined ? { apiKey: config.apiKey } : {}),
72
+ ...(config.timeoutMs !== undefined ? { timeoutMs: config.timeoutMs } : {}),
73
+ ...(options?.template !== undefined ? { template: options.template } : {}),
74
+ };
75
+ let sandbox;
76
+ try {
77
+ sandbox = config.createSandbox
78
+ ? await config.createSandbox({ options: createOptions })
79
+ : await code_interpreter_1.Sandbox.create(createOptions);
80
+ }
81
+ catch (error) {
82
+ throw new errors_js_1.SandboxOperationError({ category: (0, categorize_e2b_error_js_1.categorizeE2bError)({ error }), message: 'Failed to create the sandbox' }, { cause: error });
83
+ }
84
+ return (0, wrap_e2b_sandbox_js_1.wrapE2bSandbox)({ handle: toE2bHandle({ sandbox }), config: {
85
+ projectRoot: config.projectRoot ?? DEFAULT_PROJECT_ROOT,
86
+ previewPort: config.previewPort ?? DEFAULT_PREVIEW_PORT,
87
+ previewCheckTimeoutMs: config.previewCheckTimeoutMs ?? DEFAULT_PREVIEW_CHECK_TIMEOUT_MS,
88
+ } }, config.fetchImpl === undefined ? {} : { fetchImpl: config.fetchImpl });
89
+ },
90
+ };
91
+ }
92
+ //# sourceMappingURL=provider.js.map
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @file POSIX single-quote escaping for command arguments.
3
+ *
4
+ * Purpose:
5
+ * E2B's `commands.run` takes one command string, not an argv array, so `runCommand`/
6
+ * `startProcess`/`installDependencies` have to assemble `command + ' ' + args` themselves. A
7
+ * naive join breaks the moment an argument contains a space or a shell metacharacter — exactly
8
+ * the kind of input an AI-generated `npm install <package>` call can produce (scoped package
9
+ * names are fine, but a stray `&&` or unescaped quote in a generated argument is not). Wrapping
10
+ * every argument in single quotes and escaping embedded single quotes is the standard POSIX
11
+ * technique that survives that case: nothing between single quotes is interpreted by the shell
12
+ * except the quote character itself.
13
+ */
14
+ /** Wraps `value` in single quotes for safe interpolation into a POSIX shell command, escaping
15
+ * any single quotes it already contains. The command name itself is not quoted by callers —
16
+ * only arguments — since it is expected to be a trusted program name, not caller-controlled
17
+ * data. */
18
+ export declare function shellQuote({ value }: {
19
+ value: string;
20
+ }): string;
21
+ //# sourceMappingURL=shell-quote.d.ts.map
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ /**
3
+ * @file POSIX single-quote escaping for command arguments.
4
+ *
5
+ * Purpose:
6
+ * E2B's `commands.run` takes one command string, not an argv array, so `runCommand`/
7
+ * `startProcess`/`installDependencies` have to assemble `command + ' ' + args` themselves. A
8
+ * naive join breaks the moment an argument contains a space or a shell metacharacter — exactly
9
+ * the kind of input an AI-generated `npm install <package>` call can produce (scoped package
10
+ * names are fine, but a stray `&&` or unescaped quote in a generated argument is not). Wrapping
11
+ * every argument in single quotes and escaping embedded single quotes is the standard POSIX
12
+ * technique that survives that case: nothing between single quotes is interpreted by the shell
13
+ * except the quote character itself.
14
+ */
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.shellQuote = shellQuote;
17
+ /** Wraps `value` in single quotes for safe interpolation into a POSIX shell command, escaping
18
+ * any single quotes it already contains. The command name itself is not quoted by callers —
19
+ * only arguments — since it is expected to be a trusted program name, not caller-controlled
20
+ * data. */
21
+ function shellQuote({ value }) {
22
+ return `'${value.replaceAll("'", `'\\''`)}'`;
23
+ }
24
+ //# sourceMappingURL=shell-quote.js.map
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @file Converts a `Uint8Array` to the `ArrayBuffer` E2B's `files.write` actually accepts.
3
+ *
4
+ * Purpose:
5
+ * E2B's real `write` signature is `string | ArrayBuffer | Blob | ReadableStream` — no
6
+ * `Uint8Array` overload, even though `SandboxFile.content` in `./core` is `string | Uint8Array`
7
+ * specifically because it's what Node and the browser share natively. A naive `.buffer` access
8
+ * is wrong the moment the `Uint8Array` is a *view* over a larger buffer (e.g. produced by
9
+ * `.subarray()`) rather than owning the whole thing — it would silently include bytes outside
10
+ * the view's range, corrupting the write in a different way than the one this type was widened
11
+ * to prevent.
12
+ */
13
+ /** Returns a fresh `ArrayBuffer` containing exactly `bytes`' own range — safe even when `bytes`
14
+ * is a view with a non-zero `byteOffset` over a larger, shared `buffer`. */
15
+ export declare function toArrayBuffer({ bytes }: {
16
+ bytes: Uint8Array;
17
+ }): ArrayBuffer;
18
+ //# sourceMappingURL=to-array-buffer.d.ts.map
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ /**
3
+ * @file Converts a `Uint8Array` to the `ArrayBuffer` E2B's `files.write` actually accepts.
4
+ *
5
+ * Purpose:
6
+ * E2B's real `write` signature is `string | ArrayBuffer | Blob | ReadableStream` — no
7
+ * `Uint8Array` overload, even though `SandboxFile.content` in `./core` is `string | Uint8Array`
8
+ * specifically because it's what Node and the browser share natively. A naive `.buffer` access
9
+ * is wrong the moment the `Uint8Array` is a *view* over a larger buffer (e.g. produced by
10
+ * `.subarray()`) rather than owning the whole thing — it would silently include bytes outside
11
+ * the view's range, corrupting the write in a different way than the one this type was widened
12
+ * to prevent.
13
+ */
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.toArrayBuffer = toArrayBuffer;
16
+ /** Returns a fresh `ArrayBuffer` containing exactly `bytes`' own range — safe even when `bytes`
17
+ * is a view with a non-zero `byteOffset` over a larger, shared `buffer`. */
18
+ function toArrayBuffer({ bytes }) {
19
+ return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
20
+ }
21
+ //# sourceMappingURL=to-array-buffer.js.map
@@ -0,0 +1,31 @@
1
+ import type { FileChangeKind, SandboxSession } from '../core/ports.js';
2
+ import type { E2bFilesystemEventType, E2bSandboxHandle } from './e2b-sandbox-handle.js';
3
+ /** Resolved (no optionals) configuration `wrapE2bSandbox` needs. Default-filling happens in
4
+ * `provider.ts`, which owns the public, partially-optional config type. */
5
+ export interface E2bSessionConfig {
6
+ /** Absolute path inside the sandbox that `mountFiles`/`runCommand`/`startProcess` treat as
7
+ * the project root, and that `onFileChange` watches (recursively). */
8
+ readonly projectRoot: string;
9
+ /** Port `getPreview` asks E2B to forward — the port the caller's dev server binds to. */
10
+ readonly previewPort: number;
11
+ /** How long `getPreview` waits for a single response before deciding nothing is listening. */
12
+ readonly previewCheckTimeoutMs: number;
13
+ }
14
+ /**
15
+ * Maps one E2B filesystem event kind onto core's three-way `FileChangeKind`, or `null` to mean
16
+ * "don't surface this one." `'chmod'` is a permissions change, not a content change, and is
17
+ * dropped for that reason. `'rename'` is mapped to `modified` rather than guessed as
18
+ * create/delete because E2B's event carries only the one path this adapter sees, not an old/new
19
+ * pair — a wrong create-or-delete guess would be more misleading than the conservative
20
+ * "something at this path changed."
21
+ */
22
+ export declare function mapE2bFileChangeKind({ type }: {
23
+ type: E2bFilesystemEventType;
24
+ }): FileChangeKind | null;
25
+ export declare function wrapE2bSandbox({ handle, config }: {
26
+ handle: E2bSandboxHandle;
27
+ config: E2bSessionConfig;
28
+ }, { fetchImpl }?: {
29
+ fetchImpl?: typeof fetch;
30
+ }): Promise<SandboxSession>;
31
+ //# sourceMappingURL=wrap-e2b-sandbox.d.ts.map