withruntime 1.1.1 → 1.4.58

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 (77) hide show
  1. package/README.md +5 -2
  2. package/dist/generated/agentSessions.d.ts +2 -2
  3. package/dist/generated/agentSessions.js +2 -2
  4. package/dist/generated/agents.d.ts +2 -2
  5. package/dist/generated/agents.js +1 -1
  6. package/dist/generated/apps.d.ts +8 -6
  7. package/dist/generated/apps.js +10 -6
  8. package/dist/generated/browsers.d.ts +8 -6
  9. package/dist/generated/browsers.js +10 -6
  10. package/dist/generated/builds.d.ts +8 -6
  11. package/dist/generated/builds.js +10 -6
  12. package/dist/generated/ci.d.ts +106 -6
  13. package/dist/generated/ci.js +14 -6
  14. package/dist/generated/containers.d.ts +8 -6
  15. package/dist/generated/containers.js +10 -6
  16. package/dist/generated/desktops.d.ts +8 -6
  17. package/dist/generated/desktops.js +10 -6
  18. package/dist/generated/dropins/e2b.d.ts +180 -0
  19. package/dist/generated/dropins/e2b.js +342 -0
  20. package/dist/generated/dropins/e2b_code_interpreter.d.ts +180 -0
  21. package/dist/generated/dropins/e2b_code_interpreter.js +342 -0
  22. package/dist/generated/dropins/modal.d.ts +195 -0
  23. package/dist/generated/dropins/modal.js +375 -0
  24. package/dist/generated/dropins/runtime.d.ts +74 -0
  25. package/dist/generated/dropins/runtime.js +180 -0
  26. package/dist/generated/dropins/vercel.d.ts +78 -0
  27. package/dist/generated/dropins/vercel.js +154 -0
  28. package/dist/generated/egressIps.d.ts +2 -2
  29. package/dist/generated/egressIps.js +2 -2
  30. package/dist/generated/functions.d.ts +8 -6
  31. package/dist/generated/functions.js +10 -6
  32. package/dist/generated/identities.d.ts +42 -4
  33. package/dist/generated/identities.js +60 -0
  34. package/dist/generated/images.d.ts +3 -0
  35. package/dist/generated/images.js +4 -0
  36. package/dist/generated/index.d.ts +1 -1
  37. package/dist/generated/jobs.d.ts +2 -2
  38. package/dist/generated/jobs.js +1 -1
  39. package/dist/generated/keys.d.ts +28 -0
  40. package/dist/generated/keys.js +4 -0
  41. package/dist/generated/notebooks.d.ts +8 -6
  42. package/dist/generated/notebooks.js +10 -6
  43. package/dist/generated/ports.d.ts +2 -0
  44. package/dist/generated/projects.d.ts +49 -7
  45. package/dist/generated/projects.js +72 -3
  46. package/dist/generated/roles.d.ts +43 -5
  47. package/dist/generated/roles.js +61 -0
  48. package/dist/generated/routes.js +1 -1
  49. package/dist/generated/runtime.d.ts +49 -3
  50. package/dist/generated/runtime.js +235 -26
  51. package/dist/generated/sandboxes.d.ts +33 -14
  52. package/dist/generated/sandboxes.js +10 -6
  53. package/dist/generated/sites.d.ts +8 -6
  54. package/dist/generated/sites.js +10 -6
  55. package/dist/generated/snapshots.d.ts +7 -2
  56. package/dist/generated/snapshots.js +6 -2
  57. package/dist/generated/usage.d.ts +9 -3
  58. package/dist/generated/volumes.d.ts +30 -11
  59. package/dist/generated/volumes.js +19 -6
  60. package/dist/generated/workspaces.d.ts +8 -6
  61. package/dist/generated/workspaces.js +10 -6
  62. package/dist/v2/cli.d.ts +29 -0
  63. package/dist/v2/cli.js +156 -7
  64. package/dist/v2/login.d.ts +6 -0
  65. package/dist/v2/login.js +2 -1
  66. package/dist/v2/mcp.d.ts +5 -0
  67. package/dist/v2/mcp.js +196 -35
  68. package/dist/v2/openapi.json +3053 -1772
  69. package/dist/v2/shims/base.d.ts +0 -3
  70. package/dist/v2/shims/base.js +2 -7
  71. package/dist/v2/shims/blaxel.js +2 -1
  72. package/dist/v2/shims/daytona.js +3 -2
  73. package/dist/v2/shims/morph.js +3 -1
  74. package/dist/v2/shims/vercel.js +2 -1
  75. package/package.json +3 -2
  76. package/dist/v2/shims/e2b.d.ts +0 -142
  77. package/dist/v2/shims/e2b.js +0 -238
@@ -6,11 +6,8 @@ export interface RuntimeOptions {
6
6
  baseUrl?: string;
7
7
  }
8
8
  export declare function client(o?: RuntimeOptions): Runtime;
9
- /** A shell command run as the rivals run theirs: text in bash, a login shell (the API's own `cmd`), a list as argv. */
10
- export declare function shell(cmd: string | string[]): string | string[];
11
9
  export declare function bytes(data: string | Uint8Array | ArrayBuffer | ArrayBufferView): Uint8Array;
12
10
  export declare const text: (b: Uint8Array) => string;
13
- export declare function base64(data: string | Uint8Array): string;
14
11
  /** A command run to its end: its process, with the output it wrote (empty when none). */
15
12
  export type Ran = Process & {
16
13
  stdout: string;
@@ -5,10 +5,6 @@ import { Runtime } from "../../generated/index.js";
5
5
  export function client(o = {}) {
6
6
  return new Runtime({ ...(o.apiKey ? { apiKey: o.apiKey } : {}), ...(o.baseUrl ? { baseUrl: o.baseUrl } : {}) });
7
7
  }
8
- /** A shell command run as the rivals run theirs: text in bash, a login shell (the API's own `cmd`), a list as argv. */
9
- export function shell(cmd) {
10
- return cmd;
11
- }
12
8
  const enc = new TextEncoder();
13
9
  const dec = new TextDecoder();
14
10
  export function bytes(data) {
@@ -21,10 +17,9 @@ export function bytes(data) {
21
17
  return new Uint8Array(data);
22
18
  }
23
19
  export const text = (b) => dec.decode(b);
24
- export function base64(data) {
25
- const b = bytes(data);
20
+ function base64(data) {
26
21
  let s = "";
27
- for (const c of b)
22
+ for (const c of bytes(data))
28
23
  s += String.fromCharCode(c);
29
24
  return btoa(s);
30
25
  }
@@ -58,7 +58,8 @@ export class SandboxInstance {
58
58
  }
59
59
  static async get(nameOrId, o = {}) {
60
60
  const k = client(o);
61
- const s = await k.sandboxes.create({ name: nameOrId, getOrCreate: true }).catch(() => k.sandboxes.get(nameOrId));
61
+ // A get never creates: the spec's get takes an ID or a name, and a missing one is not found (as Blaxel's is).
62
+ const s = await k.sandboxes.get(nameOrId);
62
63
  return new SandboxInstance(k, s.id, { name: s.info.name ?? s.id });
63
64
  }
64
65
  async previewUrl(port) {
@@ -56,8 +56,9 @@ export class Daytona {
56
56
  ...(params.image ? { image: params.image } : {}),
57
57
  ...(params.labels ? { labels: params.labels } : {}),
58
58
  ...(params.resources?.cpu ? { vcpu: params.resources.cpu } : {}),
59
- ...(params.resources?.memory ? { memoryMiB: params.resources.memory * 1024 } : {}),
60
- ...(params.resources?.disk ? { diskMiB: params.resources.disk * 1024 } : {}),
59
+ // Daytona's sizes are in GiB, as the spec's memoryGB and diskGB are (1 GB is 1,024 MiB).
60
+ ...(params.resources?.memory ? { memoryGB: params.resources.memory } : {}),
61
+ ...(params.resources?.disk ? { diskGB: params.resources.disk } : {}),
61
62
  });
62
63
  return new Sandbox(this.#k, s.id, params.envVars);
63
64
  }
@@ -23,8 +23,10 @@ export class Instance {
23
23
  return new Snapshot((await this.k.sandboxes.snapshot(this.id)).id);
24
24
  }
25
25
  async branch(count = 1) {
26
+ // Morph's branch keeps the snapshot it starts the copies from.
27
+ const snapshot = await this.snapshot();
26
28
  const f = await this.k.sandboxes.fork(this.id, { count });
27
- return { snapshot: new Snapshot(""), instances: f.sandboxes.map((s) => new Instance(this.k, s.id)) };
29
+ return { snapshot, instances: f.sandboxes.map((s) => new Instance(this.k, s.id)) };
28
30
  }
29
31
  async exposeHttpService(name, port) {
30
32
  const p = await this.k.previews.create(this.id, { port, visibility: "public" });
@@ -31,7 +31,8 @@ export class Sandbox {
31
31
  const k = client(params);
32
32
  const s = await k.sandboxes.create({
33
33
  ...(params.resources?.vcpus ? { vcpu: params.resources.vcpus } : {}),
34
- ...(params.timeout ? { timeoutSeconds: Math.ceil(params.timeout / 1000) } : {}),
34
+ // Vercel's timeout ends the sandbox: the spec's deleteAfterSeconds.
35
+ ...(params.timeout ? { deleteAfterSeconds: Math.ceil(params.timeout / 1000) } : {}),
35
36
  });
36
37
  return new Sandbox(k, s.id);
37
38
  }
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "withruntime",
3
- "version": "1.1.1",
3
+ "version": "1.4.58",
4
4
  "description": "Runtime Cloud: Linux sandboxes for agents. new Runtime(), a sandbox handle, every verb of the API typed, the runtime CLI, its MCP bridge and drop-ins for E2B, Daytona, Vercel, Blaxel, Morph, Modal, Cloudflare and Fly.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
7
7
  "mcpName": "com.withruntime/runtime",
8
8
  "exports": {
9
9
  ".": { "types": "./dist/v2/index.d.ts", "default": "./dist/v2/index.js" },
10
- "./e2b": { "types": "./dist/v2/shims/e2b.d.ts", "default": "./dist/v2/shims/e2b.js" },
10
+ "./e2b": { "types": "./dist/generated/dropins/e2b.d.ts", "default": "./dist/generated/dropins/e2b.js" },
11
+ "./e2b/code-interpreter": { "types": "./dist/generated/dropins/e2b_code_interpreter.d.ts", "default": "./dist/generated/dropins/e2b_code_interpreter.js" },
11
12
  "./daytona": { "types": "./dist/v2/shims/daytona.d.ts", "default": "./dist/v2/shims/daytona.js" },
12
13
  "./vercel": { "types": "./dist/v2/shims/vercel.d.ts", "default": "./dist/v2/shims/vercel.js" },
13
14
  "./blaxel": { "types": "./dist/v2/shims/blaxel.d.ts", "default": "./dist/v2/shims/blaxel.js" },
@@ -1,142 +0,0 @@
1
- import { type RuntimeOptions } from "./base.js";
2
- export interface SandboxOpts extends RuntimeOptions {
3
- /** Milliseconds it lives; E2B's default is 300000. */
4
- timeoutMs?: number;
5
- metadata?: Record<string, string>;
6
- envs?: Record<string, string>;
7
- }
8
- export interface CommandResult {
9
- stdout: string;
10
- stderr: string;
11
- exitCode: number;
12
- error?: string;
13
- }
14
- export interface CommandOpts {
15
- cwd?: string;
16
- envs?: Record<string, string>;
17
- timeoutMs?: number;
18
- /** Called with each piece of standard output as it is written. */
19
- onStdout?: (data: string) => void;
20
- /** Called with each piece of standard error as it is written. */
21
- onStderr?: (data: string) => void;
22
- /** Returns at once with a handle to the running command. */
23
- background?: boolean;
24
- stdin?: boolean;
25
- }
26
- /** E2B throws this for a command that exits non-zero. */
27
- export declare class CommandExitError extends Error implements CommandResult {
28
- readonly stdout: string;
29
- readonly stderr: string;
30
- readonly exitCode: number;
31
- constructor(stdout: string, stderr: string, exitCode: number);
32
- }
33
- export interface EntryInfo {
34
- name: string;
35
- path: string;
36
- type: "file" | "dir";
37
- size: number;
38
- modifiedTime: Date;
39
- }
40
- /** A command running in the background: E2B's CommandHandle. */
41
- export declare class CommandHandle {
42
- #private;
43
- readonly pid: string;
44
- private readonly kill_;
45
- private readonly input;
46
- constructor(pid: string, done: Promise<CommandResult>, kill_: () => Promise<boolean>, input: (data: string) => Promise<void>);
47
- /** Waits for it to end; throws CommandExitError for a non-zero exit, as E2B does. */
48
- wait(): Promise<CommandResult>;
49
- kill(): Promise<boolean>;
50
- sendStdin(data: string): Promise<void>;
51
- }
52
- /** One run of runCode: E2B's Execution, its output as text and lines. */
53
- export interface Execution {
54
- text: string | undefined;
55
- results: {
56
- text: string;
57
- }[];
58
- logs: {
59
- stdout: string[];
60
- stderr: string[];
61
- };
62
- error: {
63
- name: string;
64
- value: string;
65
- traceback: string;
66
- } | undefined;
67
- }
68
- export interface SandboxInfoE2B {
69
- sandboxId: string;
70
- templateId: string;
71
- name?: string;
72
- metadata: Record<string, string>;
73
- startedAt: Date;
74
- state: "running" | "paused";
75
- }
76
- /** Sandbox.list's pages: E2B's SandboxPaginator. */
77
- export declare class SandboxPaginator {
78
- #private;
79
- private readonly o;
80
- constructor(o: SandboxOpts & {
81
- limit?: number;
82
- });
83
- get hasNext(): boolean;
84
- nextItems(): Promise<SandboxInfoE2B[]>;
85
- }
86
- export declare class Sandbox {
87
- private readonly rt;
88
- private readonly sbx;
89
- readonly commands: {
90
- run(cmd: string, opts: CommandOpts & {
91
- background: true;
92
- }): Promise<CommandHandle>;
93
- run(cmd: string, opts?: CommandOpts): Promise<CommandResult>;
94
- kill(pid: string): Promise<boolean>;
95
- };
96
- readonly files: {
97
- write(path: string, data: string | ArrayBuffer | Uint8Array): Promise<{
98
- path: string;
99
- name: string;
100
- }>;
101
- read(path: string, opts?: {
102
- format?: "text" | "bytes";
103
- }): Promise<string>;
104
- list(path: string): Promise<EntryInfo[]>;
105
- remove(path: string): Promise<void>;
106
- exists(path: string): Promise<boolean>;
107
- makeDir(path: string): Promise<boolean>;
108
- };
109
- private constructor();
110
- get sandboxId(): string;
111
- static create(templateOrOpts?: string | SandboxOpts, maybe?: SandboxOpts): Promise<Sandbox>;
112
- static connect(sandboxId: string, o?: RuntimeOptions): Promise<Sandbox>;
113
- static kill(sandboxId: string, o?: RuntimeOptions): Promise<boolean>;
114
- /** The org's sandboxes, a page at a time: E2B's paginator. */
115
- static list(o?: SandboxOpts & {
116
- limit?: number;
117
- }): SandboxPaginator;
118
- /** Taken and does nothing: a Runtime sandbox has no time limit; it pauses when idle and lives until deleted. */
119
- static setTimeout(_sandboxId: string, _timeoutMs: number, _o?: RuntimeOptions): Promise<void>;
120
- /** Taken and does nothing: a Runtime sandbox has no time limit; it pauses when idle and lives until deleted. */
121
- setTimeout(_timeoutMs: number): Promise<void>;
122
- getInfo(): Promise<SandboxInfoE2B>;
123
- /** Runs code (Python by default; js, ts, bash) and returns its output. Each
124
- * run is a fresh interpreter: variables do not carry between runs. */
125
- runCode(code: string, o?: {
126
- language?: string;
127
- envs?: Record<string, string>;
128
- timeoutMs?: number;
129
- onStdout?: (d: {
130
- line: string;
131
- }) => void;
132
- onStderr?: (d: {
133
- line: string;
134
- }) => void;
135
- }): Promise<Execution>;
136
- /** The host a port is served at: a preview of it. */
137
- getHost(port: number): Promise<string>;
138
- isRunning(): Promise<boolean>;
139
- pause(): Promise<boolean>;
140
- kill(): Promise<void>;
141
- }
142
- export default Sandbox;
@@ -1,238 +0,0 @@
1
- /* withruntime/e2b: E2B's Sandbox on Runtime. Change the import from "e2b" (or
2
- "@e2b/code-interpreter") to "withruntime/e2b"; RUNTIME_API_KEY replaces E2B_API_KEY.
3
- Every call is the generated core's: the handle's exec, its files, and execStream
4
- for output as it is written. */
5
- import { base64, bytes, client, list, shell, text } from "./base.js";
6
- /** E2B throws this for a command that exits non-zero. */
7
- export class CommandExitError extends Error {
8
- stdout;
9
- stderr;
10
- exitCode;
11
- constructor(stdout, stderr, exitCode) {
12
- super(`Command exited with code ${exitCode}${stderr ? `: ${stderr.trim()}` : ""}`);
13
- this.stdout = stdout;
14
- this.stderr = stderr;
15
- this.exitCode = exitCode;
16
- this.name = "CommandExitError";
17
- }
18
- }
19
- /** A command running in the background: E2B's CommandHandle. */
20
- export class CommandHandle {
21
- pid;
22
- kill_;
23
- input;
24
- #done;
25
- constructor(pid, done, kill_, input) {
26
- this.pid = pid;
27
- this.kill_ = kill_;
28
- this.input = input;
29
- this.#done = done;
30
- }
31
- /** Waits for it to end; throws CommandExitError for a non-zero exit, as E2B does. */
32
- wait() {
33
- return this.#done;
34
- }
35
- kill() {
36
- return this.kill_();
37
- }
38
- sendStdin(data) {
39
- return this.input(data);
40
- }
41
- }
42
- const e2bInfo = (s) => ({
43
- sandboxId: s.id,
44
- templateId: s.image ?? "default",
45
- ...(s.name ? { name: s.name } : {}),
46
- metadata: s.labels,
47
- startedAt: new Date(s.createdAt ?? 0),
48
- state: s.state === "paused" ? "paused" : "running",
49
- });
50
- /** Sandbox.list's pages: E2B's SandboxPaginator. */
51
- export class SandboxPaginator {
52
- o;
53
- #next;
54
- #started = false;
55
- constructor(o) {
56
- this.o = o;
57
- }
58
- get hasNext() {
59
- return !this.#started || this.#next !== undefined;
60
- }
61
- async nextItems() {
62
- const page = await client(this.o).sandboxes.list({
63
- ...(this.o.limit ? { limit: this.o.limit } : {}),
64
- ...(this.#next ? { cursor: this.#next } : {}),
65
- });
66
- this.#started = true;
67
- this.#next = page.next;
68
- return page.items.map(e2bInfo);
69
- }
70
- }
71
- const LANGUAGES = {
72
- python: (f) => ["python3", f],
73
- js: (f) => ["node", f],
74
- javascript: (f) => ["node", f],
75
- ts: (f) => ["bun", f],
76
- typescript: (f) => ["bun", f],
77
- bash: (f) => ["bash", f],
78
- sh: (f) => ["sh", f],
79
- };
80
- const EXT = { python: "py", js: "mjs", javascript: "mjs", ts: "ts", typescript: "ts", bash: "sh", sh: "sh" };
81
- export class Sandbox {
82
- rt;
83
- sbx;
84
- commands;
85
- files;
86
- constructor(rt, sbx, envs = {}) {
87
- this.rt = rt;
88
- this.sbx = sbx;
89
- const self = sbx;
90
- const start = (cmd, o) => {
91
- const stream = self.execStream(shell(cmd), {
92
- ...(o.cwd ? { cwd: o.cwd } : {}),
93
- env: { ...envs, ...o.envs },
94
- ...(o.timeoutMs ? { timeoutSeconds: o.timeoutMs / 1000 } : {}),
95
- ...(o.stdin ? { stdinOpen: true } : {}),
96
- });
97
- const done = (async () => {
98
- let stdout = "";
99
- let stderr = "";
100
- for await (const c of stream) {
101
- if (c.stream === "stderr") {
102
- stderr += c.text;
103
- o.onStderr?.(c.text);
104
- }
105
- else {
106
- stdout += c.text;
107
- o.onStdout?.(c.text);
108
- }
109
- }
110
- const p = await stream.wait();
111
- const exitCode = p.exitCode ?? -1;
112
- if (exitCode !== 0)
113
- throw new CommandExitError(stdout, stderr, exitCode);
114
- return { stdout, stderr, exitCode };
115
- })();
116
- const handle = stream.started().then((p) => new CommandHandle(p.id, done, async () => ((await self.processes.signal(p.id, { signal: "SIGKILL" })), true), async (data) => void (await self.processes.write(p.id, { data: base64(data) }))));
117
- return { handle, done };
118
- };
119
- this.commands = {
120
- run: ((cmd, o = {}) => {
121
- const { handle, done } = start(cmd, o);
122
- if (o.background) {
123
- done.catch(() => undefined);
124
- return handle;
125
- }
126
- return done;
127
- }),
128
- async kill(pid) {
129
- await self.processes.signal(pid, { signal: "SIGKILL" });
130
- return true;
131
- },
132
- };
133
- this.files = {
134
- async write(path, data) {
135
- await self.files.write(path, bytes(data));
136
- return { path, name: path.split("/").pop() ?? path };
137
- },
138
- async read(path) {
139
- return text(await self.files.read(path));
140
- },
141
- list: (path) => list(rt, self.id, path),
142
- async remove(path) {
143
- await self.files.remove(path, { recursive: true });
144
- },
145
- async exists(path) {
146
- return (await self.exec(["test", "-e", path])).exitCode === 0;
147
- },
148
- async makeDir(path) {
149
- return (await self.exec(["mkdir", "-p", path])).exitCode === 0;
150
- },
151
- };
152
- }
153
- get sandboxId() {
154
- return this.sbx.id;
155
- }
156
- static async create(templateOrOpts, maybe) {
157
- const o = (typeof templateOrOpts === "string" ? maybe : templateOrOpts) ?? {};
158
- const image = typeof templateOrOpts === "string" ? templateOrOpts : undefined;
159
- const rt = client(o);
160
- const sbx = await rt.sandboxes.create({
161
- ...(image ? { image } : {}),
162
- ...(o.metadata ? { labels: o.metadata } : {}),
163
- });
164
- return new Sandbox(rt, sbx, o.envs);
165
- }
166
- static async connect(sandboxId, o = {}) {
167
- const rt = client(o);
168
- const sbx = await rt.sandboxes.get(sandboxId);
169
- if (sbx.info.state === "paused")
170
- await sbx.resume();
171
- return new Sandbox(rt, sbx);
172
- }
173
- static async kill(sandboxId, o = {}) {
174
- await client(o).sandboxes.delete(sandboxId);
175
- return true;
176
- }
177
- /** The org's sandboxes, a page at a time: E2B's paginator. */
178
- static list(o = {}) {
179
- return new SandboxPaginator(o);
180
- }
181
- /** Taken and does nothing: a Runtime sandbox has no time limit; it pauses when idle and lives until deleted. */
182
- static async setTimeout(_sandboxId, _timeoutMs, _o = {}) { }
183
- /** Taken and does nothing: a Runtime sandbox has no time limit; it pauses when idle and lives until deleted. */
184
- async setTimeout(_timeoutMs) { }
185
- async getInfo() {
186
- return e2bInfo((await this.sbx.refresh()).info);
187
- }
188
- /** Runs code (Python by default; js, ts, bash) and returns its output. Each
189
- * run is a fresh interpreter: variables do not carry between runs. */
190
- async runCode(code, o = {}) {
191
- const language = (o.language ?? "python").toLowerCase();
192
- const argv = LANGUAGES[language];
193
- if (!argv)
194
- throw new TypeError(`runCode: no language ${language}`);
195
- const file = `/tmp/.run-${crypto.randomUUID()}.${EXT[language]}`;
196
- await this.sbx.files.write(file, code);
197
- const stream = this.sbx.execStream(argv(file), { ...(o.envs ? { env: o.envs } : {}), ...(o.timeoutMs ? { timeoutSeconds: o.timeoutMs / 1000 } : {}) });
198
- let stdout = "";
199
- let stderr = "";
200
- for await (const c of stream) {
201
- if (c.stream === "stderr") {
202
- stderr += c.text;
203
- o.onStderr?.({ line: c.text });
204
- }
205
- else {
206
- stdout += c.text;
207
- o.onStdout?.({ line: c.text });
208
- }
209
- }
210
- const p = await stream.wait();
211
- await this.sbx.files.remove(file).catch(() => undefined);
212
- const lines = (s) => s.split(/(?<=\n)/).filter(Boolean);
213
- const failed = (p.exitCode ?? -1) !== 0;
214
- const last = stderr.trim().split("\n").pop() ?? "";
215
- return {
216
- text: stdout || undefined,
217
- results: stdout ? [{ text: stdout }] : [],
218
- logs: { stdout: lines(stdout), stderr: lines(stderr) },
219
- error: failed ? { name: last.split(":")[0] || "Error", value: last.split(":").slice(1).join(":").trim() || last, traceback: stderr } : undefined,
220
- };
221
- }
222
- /** The host a port is served at: a preview of it. */
223
- async getHost(port) {
224
- const p = await this.rt.previews.create(this.sbx.id, { port, visibility: "public" });
225
- return new URL(p.info.url).host;
226
- }
227
- async isRunning() {
228
- return (await this.sbx.refresh()).info.state === "running";
229
- }
230
- async pause() {
231
- await this.sbx.pause();
232
- return true;
233
- }
234
- async kill() {
235
- await this.sbx.delete();
236
- }
237
- }
238
- export default Sandbox;