withruntime 0.9.0 → 0.10.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 (90) hide show
  1. package/BLAXEL.md +4 -4
  2. package/DAYTONA.md +13 -7
  3. package/E2B.md +12 -8
  4. package/README.md +15 -7
  5. package/README.src.md +15 -7
  6. package/VERCEL.md +13 -3
  7. package/compatibility-lock.json +84 -8
  8. package/dist/blaxel/errors.js +1 -1
  9. package/dist/blaxel/lookup.d.ts +3 -2
  10. package/dist/blaxel/lookup.js +3 -2
  11. package/dist/blaxel/pagination.d.ts +2 -2
  12. package/dist/blaxel/pagination.js +45 -17
  13. package/dist/blaxel/preview.d.ts +2 -1
  14. package/dist/blaxel/preview.js +15 -13
  15. package/dist/blaxel/process.js +56 -10
  16. package/dist/blaxel/sandbox.d.ts +2 -0
  17. package/dist/blaxel/sandbox.js +2 -0
  18. package/dist/blaxel/snapshot.js +3 -5
  19. package/dist/cli.d.ts +2 -0
  20. package/dist/cli.js +189 -54
  21. package/dist/client.d.ts +29 -3
  22. package/dist/client.js +40 -11
  23. package/dist/cloudflare/identity.d.ts +7 -0
  24. package/dist/cloudflare/identity.js +13 -0
  25. package/dist/cloudflare/index.d.ts +9 -0
  26. package/dist/cloudflare/index.js +47 -9
  27. package/dist/cloudflare/worker.d.ts +4 -0
  28. package/dist/cloudflare/worker.js +166 -16
  29. package/dist/codesandbox/guest-service.js +57 -20
  30. package/dist/codesandbox/index.js +42 -8
  31. package/dist/compat/core.d.ts +9 -8
  32. package/dist/compat/core.js +60 -21
  33. package/dist/compat/files.js +22 -4
  34. package/dist/credentials.d.ts +6 -1
  35. package/dist/credentials.js +107 -38
  36. package/dist/daytona/daytona.js +11 -4
  37. package/dist/daytona/filesystem.d.ts +17 -4
  38. package/dist/daytona/filesystem.js +121 -38
  39. package/dist/daytona/index.d.ts +1 -1
  40. package/dist/daytona/sandbox.d.ts +1 -1
  41. package/dist/daytona/sandbox.js +16 -7
  42. package/dist/e2b/client.js +1 -1
  43. package/dist/e2b/commands.d.ts +30 -8
  44. package/dist/e2b/commands.js +142 -54
  45. package/dist/e2b/index.d.ts +1 -0
  46. package/dist/e2b/index.js +1 -0
  47. package/dist/e2b/pty.d.ts +24 -0
  48. package/dist/e2b/pty.js +80 -0
  49. package/dist/e2b/sandbox.d.ts +2 -1
  50. package/dist/e2b/sandbox.js +49 -15
  51. package/dist/file-source.d.ts +16 -0
  52. package/dist/file-source.js +162 -0
  53. package/dist/freestyle/index.d.ts +9 -1
  54. package/dist/freestyle/index.js +38 -5
  55. package/dist/index.d.ts +2 -1
  56. package/dist/login.js +16 -10
  57. package/dist/modal/index.js +2 -1
  58. package/dist/network-cli.js +111 -43
  59. package/dist/openai-agents/index.js +9 -45
  60. package/dist/page.d.ts +1 -1
  61. package/dist/page.js +2 -2
  62. package/dist/products/index.d.ts +12 -0
  63. package/dist/products/index.js +10 -0
  64. package/dist/products/observability.d.ts +44 -0
  65. package/dist/products/observability.js +6 -0
  66. package/dist/products/previews.d.ts +8 -2
  67. package/dist/products/previews.js +11 -3
  68. package/dist/products/volumes.d.ts +42 -0
  69. package/dist/products/volumes.js +36 -0
  70. package/dist/products/watch.js +61 -8
  71. package/dist/proxy.js +3 -1
  72. package/dist/runloop/index.d.ts +13 -27
  73. package/dist/runloop/index.js +149 -52
  74. package/dist/sandbox.d.ts +37 -8
  75. package/dist/sandbox.js +432 -68
  76. package/dist/snapshots.d.ts +9 -4
  77. package/dist/sprites/index.js +69 -33
  78. package/dist/tar.d.ts +3 -1
  79. package/dist/tar.js +140 -14
  80. package/dist/transport.d.ts +4 -7
  81. package/dist/transport.js +205 -122
  82. package/dist/vercel/command.d.ts +1 -1
  83. package/dist/vercel/command.js +3 -2
  84. package/dist/vercel/errors.d.ts +1 -1
  85. package/dist/vercel/errors.js +7 -1
  86. package/dist/vercel/sandbox.d.ts +8 -8
  87. package/dist/vercel/sandbox.js +154 -59
  88. package/dist/vercel/snapshot.d.ts +3 -2
  89. package/dist/vercel/snapshot.js +9 -5
  90. package/package.json +2 -1
@@ -1,6 +1,43 @@
1
1
  import { resolvePath, WHOLE_OUTPUT } from "./context.js";
2
2
  import { DaytonaError, DaytonaFileNotFoundError, guard } from "./errors.js";
3
3
  const PERMISSIONS = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"];
4
+ function downloadRequest(timeout, signal) {
5
+ if (!Number.isFinite(timeout) || timeout < 0)
6
+ throw new DaytonaError("Download timeout must be a nonnegative finite number.");
7
+ signal?.throwIfAborted();
8
+ const deadline = timeout > 0 ? AbortSignal.timeout(Math.ceil(timeout * 1000)) : undefined;
9
+ return {
10
+ timeoutMs: 0,
11
+ ...(deadline || signal
12
+ ? {
13
+ signal: deadline && signal ? AbortSignal.any([deadline, signal]) : (deadline ?? signal),
14
+ }
15
+ : {}),
16
+ };
17
+ }
18
+ /** Wait on client-local setup within the transfer's existing deadline. The
19
+ * setup may finish later; the caller cannot proceed to an upload after abort. */
20
+ function waitForRequest(pending, request) {
21
+ const signal = request.signal;
22
+ if (!signal)
23
+ return pending;
24
+ return new Promise((resolve, reject) => {
25
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors -- rejects with the caller's abort reason as is, as the upstream SDK does
26
+ const abort = () => reject(signal.reason);
27
+ if (signal.aborted)
28
+ abort();
29
+ else
30
+ signal.addEventListener("abort", abort, { once: true });
31
+ pending.then((value) => {
32
+ signal.removeEventListener("abort", abort);
33
+ resolve(value);
34
+ }, (error) => {
35
+ signal.removeEventListener("abort", abort);
36
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors -- passes the pending call's own failure through unchanged
37
+ reject(error);
38
+ });
39
+ });
40
+ }
4
41
  function fileInfo(entry) {
5
42
  const bits = (parseInt(entry.mode, 8) || 0) & 0o777;
6
43
  const kind = entry.type === "directory" ? "d" : entry.type === "symlink" ? "l" : "-";
@@ -15,20 +52,6 @@ function fileInfo(entry) {
15
52
  group: "",
16
53
  };
17
54
  }
18
- async function bytesOf(source) {
19
- if (typeof source === "string") {
20
- const { readFile } = await import("node:fs/promises");
21
- return readFile(source);
22
- }
23
- if (source instanceof Uint8Array)
24
- return source;
25
- if (typeof source.getReader === "function")
26
- return new Uint8Array(await new Response(source).arrayBuffer());
27
- const chunks = [];
28
- for await (const chunk of source)
29
- chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : Buffer.from(chunk));
30
- return Buffer.concat(chunks);
31
- }
32
55
  /** `sandbox.fs`: Daytona's file calls over Runtime's files API. Relative
33
56
  * paths resolve from the working directory, /workspace on Runtime. */
34
57
  export class FileSystem {
@@ -66,36 +89,56 @@ export class FileSystem {
66
89
  if (!removed)
67
90
  throw new DaytonaFileNotFoundError(`${target} does not exist.`, 404);
68
91
  }
69
- async downloadFile(remotePath, localOrTimeout) {
92
+ async downloadFile(remotePath, localOrTimeout, timeout = 30 * 60) {
93
+ // The numeric second argument is the published buffer overload. In that
94
+ // overload zero falls back to the default, as the pinned SDK does.
95
+ const request = downloadRequest(typeof localOrTimeout === "number" && localOrTimeout !== 0 ? localOrTimeout : timeout);
96
+ return this.#downloadFile(remotePath, typeof localOrTimeout === "string" ? localOrTimeout : undefined, request);
97
+ }
98
+ async #downloadFile(remotePath, localPath, request) {
99
+ request.signal?.throwIfAborted();
70
100
  const target = await this.#path(remotePath);
71
101
  const files = await this.#files();
72
- if (typeof localOrTimeout !== "string")
73
- return Buffer.from(await guard("file", () => files.read(target)));
102
+ request.signal?.throwIfAborted();
103
+ if (localPath === undefined)
104
+ return Buffer.from(await guard("file", () => files.read(target, request)));
74
105
  // Streamed to disk, any size, checked against the file's length.
75
- await guard("file", () => files.download(target, localOrTimeout));
106
+ await guard("file", () => files.download(target, localPath, request));
76
107
  }
77
108
  /** The file as it arrives, without holding it in memory. It errors with
78
109
  * `download_incomplete` if it ends short of the file's length. */
79
- async downloadFileStream(remotePath) {
110
+ async downloadFileStream(remotePath, timeoutOrOptions) {
111
+ const options = typeof timeoutOrOptions === "number"
112
+ ? { timeout: timeoutOrOptions }
113
+ : (timeoutOrOptions ?? {});
114
+ const request = downloadRequest(options.timeout ?? 30 * 60, options.signal);
80
115
  const { Readable } = await import("node:stream");
81
116
  const target = await this.#path(remotePath);
82
117
  const files = await this.#files();
83
- const stream = await guard("file", () => files.readStream(target));
118
+ request.signal?.throwIfAborted();
119
+ const stream = await guard("file", () => files.readStream(target, request));
84
120
  return Readable.fromWeb(stream);
85
121
  }
86
122
  /** Several files; a file that fails carries `error` instead of `result`. */
87
- async downloadFiles(files) {
123
+ async downloadFiles(files, timeout = 30 * 60) {
124
+ const request = downloadRequest(timeout);
88
125
  const out = [];
89
126
  for (const file of files) {
127
+ request.signal?.throwIfAborted();
90
128
  try {
91
129
  if (file.destination) {
92
- await this.downloadFile(file.source, file.destination);
130
+ await this.#downloadFile(file.source, file.destination, request);
93
131
  out.push({ source: file.source, result: file.destination });
94
132
  }
95
133
  else
96
- out.push({ source: file.source, result: await this.downloadFile(file.source) });
134
+ out.push({
135
+ source: file.source,
136
+ result: (await this.#downloadFile(file.source, undefined, request)),
137
+ });
97
138
  }
98
139
  catch (error) {
140
+ if (request.signal?.aborted)
141
+ throw error;
99
142
  const failure = error;
100
143
  out.push({
101
144
  source: file.source,
@@ -185,24 +228,64 @@ export class FileSystem {
185
228
  target,
186
229
  ]);
187
230
  }
188
- async uploadFile(source, remotePath) {
189
- const target = await this.#path(remotePath);
190
- const bytes = await bytesOf(source);
191
- const files = await this.#files();
192
- await guard("file", () => files.write(target, bytes));
231
+ async uploadFile(source, remotePath, timeout = 30 * 60) {
232
+ await this.#uploadFile(source, remotePath, downloadRequest(timeout));
193
233
  }
194
- async uploadFileStream(source, remotePath) {
195
- const target = await this.#path(remotePath);
196
- const bytes = await bytesOf(source);
197
- const files = await this.#files();
198
- await guard("file", () => files.write(target, bytes));
234
+ async #uploadFile(source, remotePath, request, onProgress) {
235
+ request.signal?.throwIfAborted();
236
+ const readable = typeof source !== "string" && typeof source.destroy === "function"
237
+ ? source
238
+ : undefined;
239
+ const stopInput = () => {
240
+ readable?.destroy();
241
+ };
242
+ request.signal?.addEventListener("abort", stopInput, { once: true });
243
+ try {
244
+ const target = await waitForRequest(this.#path(remotePath), request);
245
+ request.signal?.throwIfAborted();
246
+ const files = await waitForRequest(this.#files(), request);
247
+ request.signal?.throwIfAborted();
248
+ // Node Readables can emit strings after setEncoding; Daytona uploads
249
+ // their UTF-8 bytes. The generator retains only the current source chunk.
250
+ const input = typeof source === "string" ||
251
+ source instanceof Uint8Array ||
252
+ typeof source.getReader === "function"
253
+ ? source
254
+ : (async function* () {
255
+ for await (const chunk of source)
256
+ yield typeof chunk === "string" ? Buffer.from(chunk) : chunk;
257
+ })();
258
+ await guard("file", () => input instanceof Uint8Array && !onProgress
259
+ ? files.write(target, input, request)
260
+ : files.writeStream(target, input, {
261
+ ...request,
262
+ ...(onProgress ? { onProgress } : {}),
263
+ }));
264
+ }
265
+ finally {
266
+ request.signal?.removeEventListener("abort", stopInput);
267
+ }
268
+ }
269
+ /** Uploads with bounded memory. Unknown-length streams stage in a private
270
+ * temporary file before the native atomic upload; local paths avoid staging. */
271
+ async uploadFileStream(source, remotePath, options = {}) {
272
+ const cancelled = () => new DaytonaError(`Upload cancelled: ${remotePath}`);
273
+ if (options.signal?.aborted)
274
+ throw cancelled();
275
+ const request = downloadRequest(options.timeout ?? 30 * 60, options.signal);
276
+ try {
277
+ await this.#uploadFile(source, remotePath, request, options.onProgress);
278
+ }
279
+ catch (error) {
280
+ if (options.signal?.aborted)
281
+ throw cancelled();
282
+ throw error;
283
+ }
199
284
  }
200
- async uploadFiles(files) {
285
+ async uploadFiles(files, timeout = 30 * 60) {
286
+ const request = downloadRequest(timeout);
201
287
  for (const file of files) {
202
- const target = await this.#path(file.destination);
203
- const bytes = await bytesOf(file.source);
204
- const api = await this.#files();
205
- await guard("file", () => api.write(target, bytes));
288
+ await this.#uploadFile(file.source, file.destination, request);
206
289
  }
207
290
  }
208
291
  }
@@ -1,7 +1,7 @@
1
1
  export { Daytona, CodeLanguage, SnapshotService, VolumeService, DEFAULT_RESOURCES, DEFAULT_AUTO_STOP_MINUTES, lifecycleOf, type CreateSandboxBaseParams, type CreateSandboxFromImageParams, type CreateSandboxFromSnapshotParams, type CreateSnapshotParams, type ForkSandboxParams, type ListSandboxesQuery, type PaginatedSnapshots, type Resources, type Snapshot, type Volume, type VolumeMount, } from "./daytona.js";
2
2
  export { Sandbox, CodeInterpreter, networkRules, windowSeconds, type ExecutionError, type ExecutionResult, type InterpreterContext, type OutputMessage, type PortPreviewUrl, } from "./sandbox.js";
3
3
  export { Process, CodeRunParams, type Command, type ExecuteResponse, type Session, type SessionCommandLogsResponse, type SessionExecuteRequest, type SessionExecuteResponse, } from "./process.js";
4
- export { FileSystem, type FileDownloadRequest, type FileDownloadResponse, type FileInfo, type FilePermissionsParams, type FileUpload, type Match, type ReplaceResult, type SearchFilesResponse, type UploadSource, } from "./filesystem.js";
4
+ export { FileSystem, type FileDownloadRequest, type FileDownloadResponse, type FileInfo, type FilePermissionsParams, type FileUpload, type Match, type ReplaceResult, type SearchFilesResponse, type UploadSource, type UploadProgress, type UploadStreamOptions, } from "./filesystem.js";
5
5
  export { Git, type GitStatus, type ListBranchResponse } from "./git.js";
6
6
  export { Image } from "./image.js";
7
7
  export { resolvePath } from "./context.js";
@@ -162,7 +162,7 @@ export declare class Sandbox {
162
162
  createSnapshot(name: string, _timeout?: number): Promise<void>;
163
163
  _experimental_createSnapshot(name: string, timeout?: number): Promise<void>;
164
164
  get computerUse(): never;
165
- setLabels(): Promise<never>;
165
+ setLabels(labels: Record<string, string>): Promise<Record<string, string>>;
166
166
  recover(): Promise<never>;
167
167
  resize(): Promise<never>;
168
168
  waitForResizeComplete(): Promise<never>;
@@ -22,11 +22,18 @@ export class CodeInterpreter {
22
22
  const env = this.#env();
23
23
  if (!Object.keys(env).length)
24
24
  return "python";
25
- this.#envContext ??= (async () => {
26
- const runtime = await this.#sandbox();
27
- const made = await guard("sandbox", () => runtime.interpreter.contexts.create({ language: "python", cwd: HOME, env }));
28
- return made.id;
29
- })();
25
+ if (!this.#envContext) {
26
+ const pending = (async () => {
27
+ const runtime = await this.#sandbox();
28
+ const made = await guard("sandbox", () => runtime.interpreter.contexts.create({ language: "python", cwd: HOME, env }));
29
+ return made.id;
30
+ })();
31
+ this.#envContext = pending;
32
+ pending.catch(() => {
33
+ if (this.#envContext === pending)
34
+ this.#envContext = undefined;
35
+ });
36
+ }
30
37
  return this.#envContext;
31
38
  }
32
39
  async runCode(code, options = {}) {
@@ -373,8 +380,10 @@ export class Sandbox {
373
380
  get computerUse() {
374
381
  throw new NotSupportedError("Daytona's computer use", "Use Runtime's desktop: `await sandbox.withruntime.desktop.start()` (see the desktop guide).");
375
382
  }
376
- setLabels() {
377
- return refusal("Changing a sandbox's labels", "Runtime sets labels once, at create: pass labels to daytona.create.");
383
+ async setLabels(labels) {
384
+ const internal = Object.fromEntries(Object.entries(this.#rt.info.labels).filter(([key]) => key === "code-toolbox-language" || key.startsWith("compat.")));
385
+ await guard("sandbox", () => this.#rt.update({ labels: { ...labels, ...internal } }));
386
+ return this.labels;
378
387
  }
379
388
  recover() {
380
389
  return refusal("Recovering a failed sandbox", "Create a new sandbox, or start one from a snapshot.");
@@ -60,7 +60,7 @@ export function resetClients() {
60
60
  /** Request options for a Runtime call from E2B's. */
61
61
  export function request(opts = {}) {
62
62
  return {
63
- ...(opts.requestTimeoutMs ? { timeoutMs: opts.requestTimeoutMs } : {}),
63
+ ...(opts.requestTimeoutMs === undefined ? {} : { timeoutMs: opts.requestTimeoutMs }),
64
64
  ...(opts.signal ? { signal: opts.signal } : {}),
65
65
  };
66
66
  }
@@ -1,6 +1,8 @@
1
1
  import type { Process } from "../sandbox.js";
2
+ import type { ProcessInfo as RuntimeProcessInfo } from "../types.js";
3
+ import type { RequestOptions } from "../transport.js";
2
4
  import type { RuntimeSandbox } from "./client.js";
3
- import { type CommandResult } from "./errors.js";
5
+ import { TimeoutError, type CommandResult } from "./errors.js";
4
6
  export type { CommandResult };
5
7
  export type Username = "user" | "root" | (string & {});
6
8
  export interface CommandRequestOpts {
@@ -37,13 +39,29 @@ export interface ProcessInfo {
37
39
  * keep: it is the sandbox's own `env`, added to every command there. */
38
40
  export interface SandboxContext {
39
41
  readonly runtime: RuntimeSandbox;
42
+ readonly requestTimeoutMs?: number;
40
43
  /** Makes /home/user (E2B's home) lead to /workspace (Runtime's), once, when
41
44
  * something names it. */
42
- ensureHome(text: string | undefined): Promise<void>;
45
+ ensureHome(text: string | undefined, options?: RequestOptions): Promise<void>;
43
46
  }
44
47
  /** E2B's pids are numbers; Runtime's process ids are strings. The number is a
45
48
  * 31-bit FNV-1a hash of the id, so every client derives the same one. */
46
49
  export declare function pidOf(processId: string): number;
50
+ export declare function refuseUser(user: Username | undefined): void;
51
+ export declare function timeoutFor(timeoutMs: number | undefined): number;
52
+ /** One budget for lookup and mutation, including retries and queued input. */
53
+ export declare function commandRequest(opts: CommandRequestOpts, defaultTimeoutMs?: number): RequestOptions;
54
+ export declare function findProcess(ctx: SandboxContext, pid: number, options: RequestOptions): Promise<RuntimeProcessInfo | undefined>;
55
+ export declare function resolveProcess(ctx: SandboxContext, pid: number, options: RequestOptions): Promise<Process>;
56
+ export declare function killProcess(ctx: SandboxContext, pid: number, options: RequestOptions): Promise<boolean>;
57
+ export declare function timedOut(timeoutMs: number): TimeoutError;
58
+ /** The subscription deadline includes the opening handshake. */
59
+ export declare function connectionRequest(opts: CommandConnectOpts, defaultTimeoutMs?: number): {
60
+ timeoutMs: number;
61
+ deadline: number | undefined;
62
+ request: RequestOptions;
63
+ failure: (error: unknown) => never;
64
+ };
47
65
  /** `sandbox.commands`: E2B's command module over Runtime's exec and processes. */
48
66
  export declare class Commands {
49
67
  #private;
@@ -61,12 +79,12 @@ export declare class Commands {
61
79
  background?: boolean;
62
80
  }): Promise<CommandHandle | CommandResult>;
63
81
  /** Running commands. */
64
- list(_opts?: CommandRequestOpts): Promise<ProcessInfo[]>;
82
+ list(opts?: CommandRequestOpts): Promise<ProcessInfo[]>;
65
83
  /** Kills a command with SIGKILL, as E2B does. False when there is none. */
66
- kill(pid: number, _opts?: CommandRequestOpts): Promise<boolean>;
84
+ kill(pid: number, opts?: CommandRequestOpts): Promise<boolean>;
67
85
  /** Sends input to a command started with `stdin: true`. */
68
- sendStdin(pid: number, data: string | Uint8Array, _opts?: CommandRequestOpts): Promise<void>;
69
- closeStdin(pid: number, _opts?: CommandRequestOpts): Promise<void>;
86
+ sendStdin(pid: number, data: string | Uint8Array, opts?: CommandRequestOpts): Promise<void>;
87
+ closeStdin(pid: number, opts?: CommandRequestOpts): Promise<void>;
70
88
  /** Attaches to a running command; output from now on reaches the handle. */
71
89
  connect(pid: number, opts?: CommandConnectOpts): Promise<CommandHandle>;
72
90
  }
@@ -83,6 +101,10 @@ export declare class CommandHandle {
83
101
  signal?: AbortSignal;
84
102
  onStdout?: (data: string) => void | Promise<void>;
85
103
  onStderr?: (data: string) => void | Promise<void>;
104
+ onData?: (data: Uint8Array) => void | Promise<void>;
105
+ pty?: boolean;
106
+ requestTimeoutMs?: number;
107
+ deadline?: number;
86
108
  });
87
109
  /** The exit code, or undefined while the command runs. */
88
110
  get exitCode(): number | undefined;
@@ -96,6 +118,6 @@ export declare class CommandHandle {
96
118
  disconnect(): Promise<void>;
97
119
  /** Kills the command with SIGKILL. False when it had already ended. */
98
120
  kill(): Promise<boolean>;
99
- sendStdin(data: string | Uint8Array, _opts?: CommandRequestOpts): Promise<void>;
100
- closeStdin(_opts?: CommandRequestOpts): Promise<void>;
121
+ sendStdin(data: string | Uint8Array, opts?: CommandRequestOpts): Promise<void>;
122
+ closeStdin(opts?: CommandRequestOpts): Promise<void>;
101
123
  }