@mastra/e2b 0.15.0-alpha.1 → 0.15.1-alpha.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/dist/_types/@internal_workspace/dist/commands-api-process-manager.d.ts +99 -0
- package/dist/_types/@internal_workspace/dist/index.d.ts +1 -0
- package/dist/index.cjs +205 -127
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +205 -127
- package/dist/index.js.map +1 -1
- package/dist/sandbox/process-manager.d.ts +7 -18
- package/dist/sandbox/process-manager.d.ts.map +1 -1
- package/dist/utils/template.d.ts +1 -1
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -13,7 +13,7 @@ import { transformSync } from "esbuild";
|
|
|
13
13
|
* contents can never drift under a stable identity hash; bump deliberately
|
|
14
14
|
* (each bump builds new templates).
|
|
15
15
|
*/
|
|
16
|
-
const DEFAULT_NODE_VERSION = "24.
|
|
16
|
+
const DEFAULT_NODE_VERSION = "24.22.1";
|
|
17
17
|
const NODE_VERSION_PATTERN = /^\d+\.\d+\.\d+$/;
|
|
18
18
|
function isNamedTemplateSpec(spec) {
|
|
19
19
|
return typeof spec === "object" && spec !== null && "ref" in spec && "template" in spec;
|
|
@@ -55,7 +55,7 @@ function createDefaultMountableTemplate(options) {
|
|
|
55
55
|
const aptPackages = ["s3fs", "fuse"];
|
|
56
56
|
const cpuCount = options?.cpuCount ?? 2;
|
|
57
57
|
const memoryMB = options?.memoryMB ?? 1024;
|
|
58
|
-
const nodeVersion = options?.nodeVersion ?? "24.
|
|
58
|
+
const nodeVersion = options?.nodeVersion ?? "24.22.1";
|
|
59
59
|
if (!NODE_VERSION_PATTERN.test(nodeVersion)) throw new Error(`Invalid nodeVersion "${nodeVersion}": expected an exact version like "24.20.0"`);
|
|
60
60
|
const config = {
|
|
61
61
|
version: "v4",
|
|
@@ -527,6 +527,201 @@ var FilesApiFilesystem = class extends MastraFilesystem {
|
|
|
527
527
|
throw new Error(`${operation} ${subject} failed (exit ${result.exitCode}): ${result.stderr.trim()}`);
|
|
528
528
|
}
|
|
529
529
|
};
|
|
530
|
+
/**
|
|
531
|
+
* Output callbacks for `run`/`connect` that hold output until the handle
|
|
532
|
+
* exists. The SDK's handle reads events from construction, before `run()`
|
|
533
|
+
* returns, so output that arrives with the start event reaches the
|
|
534
|
+
* callbacks first.
|
|
535
|
+
*/
|
|
536
|
+
function outputUntilHandled() {
|
|
537
|
+
let handle;
|
|
538
|
+
const early = [];
|
|
539
|
+
return {
|
|
540
|
+
onStdout: (data) => {
|
|
541
|
+
if (handle) handle.emitStdout(data);
|
|
542
|
+
else early.push({
|
|
543
|
+
stream: "stdout",
|
|
544
|
+
data
|
|
545
|
+
});
|
|
546
|
+
},
|
|
547
|
+
onStderr: (data) => {
|
|
548
|
+
if (handle) handle.emitStderr(data);
|
|
549
|
+
else early.push({
|
|
550
|
+
stream: "stderr",
|
|
551
|
+
data
|
|
552
|
+
});
|
|
553
|
+
},
|
|
554
|
+
handledBy(created) {
|
|
555
|
+
for (const { stream, data } of early) if (stream === "stdout") created.emitStdout(data);
|
|
556
|
+
else created.emitStderr(data);
|
|
557
|
+
early.length = 0;
|
|
558
|
+
handle = created;
|
|
559
|
+
return created;
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
/** envd runs every command as `/bin/bash -l -c <command>`; show the command itself. */
|
|
564
|
+
function commandOf(proc) {
|
|
565
|
+
const [flag, c, command] = proc.args;
|
|
566
|
+
if (proc.cmd === "/bin/bash" && flag === "-l" && c === "-c" && command !== void 0 && proc.args.length === 3) return command;
|
|
567
|
+
return [proc.cmd, ...proc.args].join(" ");
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* A process started or attached through a {@link CommandsApi}. `kill` and
|
|
571
|
+
* `sendStdin` go back through the manager, so they use whatever commands
|
|
572
|
+
* handle the provider currently supplies.
|
|
573
|
+
*/
|
|
574
|
+
var CommandsApiProcessHandle = class extends ProcessHandle {
|
|
575
|
+
inner;
|
|
576
|
+
manager;
|
|
577
|
+
startTime;
|
|
578
|
+
stdinMode;
|
|
579
|
+
pid;
|
|
580
|
+
/** The exit code `wait()` saw, for a handle that doesn't update its own. */
|
|
581
|
+
waitedExitCode;
|
|
582
|
+
constructor(inner, manager, startTime, stdinMode, options) {
|
|
583
|
+
super(options);
|
|
584
|
+
this.inner = inner;
|
|
585
|
+
this.manager = manager;
|
|
586
|
+
this.startTime = startTime;
|
|
587
|
+
this.stdinMode = stdinMode;
|
|
588
|
+
this.pid = String(inner.pid);
|
|
589
|
+
}
|
|
590
|
+
/** Read from the commands handle, so it reflects server-side state without `wait()`. */
|
|
591
|
+
get exitCode() {
|
|
592
|
+
return this.inner.exitCode ?? this.waitedExitCode;
|
|
593
|
+
}
|
|
594
|
+
/** Wait for the process to exit. Providers whose output stream can drop while the process runs override this. */
|
|
595
|
+
waitForExit() {
|
|
596
|
+
return this.inner.wait();
|
|
597
|
+
}
|
|
598
|
+
async wait() {
|
|
599
|
+
try {
|
|
600
|
+
const result = await this.waitForExit();
|
|
601
|
+
this.waitedExitCode = result.exitCode;
|
|
602
|
+
return {
|
|
603
|
+
success: result.exitCode === 0,
|
|
604
|
+
exitCode: result.exitCode,
|
|
605
|
+
stdout: this.stdout,
|
|
606
|
+
stderr: this.stderr,
|
|
607
|
+
executionTimeMs: Date.now() - this.startTime
|
|
608
|
+
};
|
|
609
|
+
} catch (error) {
|
|
610
|
+
const errorObj = error;
|
|
611
|
+
const reportedExitCode = errorObj.result?.exitCode ?? errorObj.exitCode;
|
|
612
|
+
if (reportedExitCode !== void 0) this.waitedExitCode = reportedExitCode;
|
|
613
|
+
const exitCode = reportedExitCode ?? this.exitCode ?? 1;
|
|
614
|
+
const attachedStdout = errorObj.result?.stdout || errorObj.stdout;
|
|
615
|
+
const attachedStderr = errorObj.result?.stderr || errorObj.stderr;
|
|
616
|
+
if (!this.stdout && !this.stdoutTruncated && attachedStdout) this.emitStdout(attachedStdout);
|
|
617
|
+
if (!this.stderr && !this.stderrTruncated && attachedStderr) this.emitStderr(attachedStderr);
|
|
618
|
+
const stdout = this.stdout;
|
|
619
|
+
const stderr = this.stderr;
|
|
620
|
+
const terminalError = errorObj.result?.error || errorObj.error || (error instanceof Error ? error.message : String(error));
|
|
621
|
+
return {
|
|
622
|
+
success: false,
|
|
623
|
+
exitCode,
|
|
624
|
+
stdout,
|
|
625
|
+
stderr: [stderr, terminalError && !stderr.includes(terminalError) ? `Error: ${terminalError}` : ""].filter(Boolean).join("\n"),
|
|
626
|
+
executionTimeMs: Date.now() - this.startTime
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
async kill() {
|
|
631
|
+
if (this.exitCode !== void 0) return false;
|
|
632
|
+
return this.manager.runCommands("kill", (commands) => commands.kill(this.inner.pid));
|
|
633
|
+
}
|
|
634
|
+
async sendStdin(data) {
|
|
635
|
+
if (this.exitCode !== void 0) throw new Error(`Process ${this.pid} has already exited with code ${this.exitCode}`);
|
|
636
|
+
if (this.stdinMode === "ignore") throw new Error(`Process ${this.pid} was not started with stdin support`);
|
|
637
|
+
await this.manager.runCommands("sendStdin", (commands) => commands.sendStdin(this.inner.pid, data));
|
|
638
|
+
}
|
|
639
|
+
async closeStdin() {
|
|
640
|
+
throw new UnsupportedStdinCloseError("The commands API does not expose a way to close stdin for a running command");
|
|
641
|
+
}
|
|
642
|
+
};
|
|
643
|
+
/**
|
|
644
|
+
* A process manager over E2B's envd commands API. Providers supply the
|
|
645
|
+
* commands handle through {@link runCommands}; everything else is shared.
|
|
646
|
+
*/
|
|
647
|
+
var CommandsApiProcessManager = class extends SandboxProcessManager {
|
|
648
|
+
defaultTimeout;
|
|
649
|
+
constructor({ defaultTimeout, ...options } = {}) {
|
|
650
|
+
super(options);
|
|
651
|
+
this.defaultTimeout = defaultTimeout;
|
|
652
|
+
}
|
|
653
|
+
/** Wrap a started or attached command. Providers override this to supply their own handle. */
|
|
654
|
+
createHandle(inner, stdinMode, options) {
|
|
655
|
+
return new CommandsApiProcessHandle(inner, this, Date.now(), stdinMode, options);
|
|
656
|
+
}
|
|
657
|
+
/** Where a command runs when the caller gives no `cwd`. */
|
|
658
|
+
defaultCwd() {
|
|
659
|
+
return this.sandbox.workingDirectory;
|
|
660
|
+
}
|
|
661
|
+
async spawn(command, options = {}) {
|
|
662
|
+
const envs = Object.fromEntries(Object.entries({ ...options.env }).filter((entry) => entry[1] !== void 0));
|
|
663
|
+
return this.runCommands("spawn", async (commands) => {
|
|
664
|
+
const output = outputUntilHandled();
|
|
665
|
+
const inner = await commands.run(command, {
|
|
666
|
+
background: true,
|
|
667
|
+
stdin: options.stdinMode !== "ignore",
|
|
668
|
+
cwd: options.cwd ?? this.defaultCwd(),
|
|
669
|
+
envs,
|
|
670
|
+
timeoutMs: options.timeout ?? this.defaultTimeout,
|
|
671
|
+
onStdout: output.onStdout,
|
|
672
|
+
onStderr: output.onStderr
|
|
673
|
+
});
|
|
674
|
+
const handle = output.handledBy(this.createHandle(inner, options.stdinMode, options));
|
|
675
|
+
this._tracked.set(handle.pid, handle);
|
|
676
|
+
return handle;
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Running processes as the VM reports them, plus exited processes this
|
|
681
|
+
* manager still holds, so a finished command stays visible until it is released.
|
|
682
|
+
*/
|
|
683
|
+
async list() {
|
|
684
|
+
const infos = (await this.runCommands("list", (commands) => commands.list())).map((proc) => ({
|
|
685
|
+
pid: String(proc.pid),
|
|
686
|
+
command: commandOf(proc),
|
|
687
|
+
running: true
|
|
688
|
+
}));
|
|
689
|
+
const listed = new Set(infos.map((info) => info.pid));
|
|
690
|
+
for (const [pid, handle] of this._tracked) {
|
|
691
|
+
if (handle.exitCode === void 0 || listed.has(pid)) continue;
|
|
692
|
+
infos.push({
|
|
693
|
+
pid,
|
|
694
|
+
command: handle.command,
|
|
695
|
+
running: false,
|
|
696
|
+
exitCode: handle.exitCode
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
return infos;
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* A tracked process, or one this manager didn't start, attached through
|
|
703
|
+
* `connect`. Output from before attaching is not available.
|
|
704
|
+
*/
|
|
705
|
+
async get(pid, _options) {
|
|
706
|
+
const tracked = this._tracked.get(pid);
|
|
707
|
+
if (tracked) return tracked;
|
|
708
|
+
const numericPid = /^\d+$/.test(pid) ? Number(pid) : void 0;
|
|
709
|
+
if (numericPid === void 0) return void 0;
|
|
710
|
+
const output = outputUntilHandled();
|
|
711
|
+
try {
|
|
712
|
+
const inner = await this.runCommands("connect", (commands) => commands.connect(numericPid, {
|
|
713
|
+
onStdout: output.onStdout,
|
|
714
|
+
onStderr: output.onStderr,
|
|
715
|
+
timeoutMs: 0
|
|
716
|
+
}));
|
|
717
|
+
const handle = output.handledBy(this.createHandle(inner, void 0));
|
|
718
|
+
this._tracked.set(handle.pid, handle);
|
|
719
|
+
return handle;
|
|
720
|
+
} catch {
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
};
|
|
530
725
|
//#endregion
|
|
531
726
|
//#region src/sandbox/filesystem.ts
|
|
532
727
|
/**
|
|
@@ -1003,134 +1198,17 @@ Error details: ${verifyResult.stderr || verifyResult.stdout || installResult?.st
|
|
|
1003
1198
|
/**
|
|
1004
1199
|
* E2B Process Manager
|
|
1005
1200
|
*
|
|
1006
|
-
* Implements SandboxProcessManager for E2B cloud sandboxes
|
|
1007
|
-
*
|
|
1201
|
+
* Implements SandboxProcessManager for E2B cloud sandboxes over the E2B SDK's
|
|
1202
|
+
* commands API. The process handling itself is shared with other envd-backed
|
|
1203
|
+
* providers in `@internal/workspace`.
|
|
1008
1204
|
*/
|
|
1009
|
-
|
|
1010
|
-
* Wraps an E2B CommandHandle to conform to Mastra's ProcessHandle.
|
|
1011
|
-
* Not exported — internal to this module.
|
|
1012
|
-
*
|
|
1013
|
-
* Listener dispatch is handled by the base class. The manager's spawn()/get()
|
|
1014
|
-
* methods wire E2B's constructor-time callbacks to handle.emitStdout/emitStderr.
|
|
1015
|
-
*/
|
|
1016
|
-
var E2BProcessHandle = class extends ProcessHandle {
|
|
1017
|
-
pid;
|
|
1018
|
-
_e2bHandle;
|
|
1019
|
-
_sandbox;
|
|
1020
|
-
_startTime;
|
|
1021
|
-
_stdinMode;
|
|
1022
|
-
constructor(e2bHandle, sandbox, startTime, options) {
|
|
1023
|
-
super(options);
|
|
1024
|
-
this.pid = String(e2bHandle.pid);
|
|
1025
|
-
this._e2bHandle = e2bHandle;
|
|
1026
|
-
this._sandbox = sandbox;
|
|
1027
|
-
this._startTime = startTime;
|
|
1028
|
-
this._stdinMode = options?.stdinMode;
|
|
1029
|
-
}
|
|
1030
|
-
/** Delegates to E2B's handle so exitCode reflects server-side state without needing wait(). */
|
|
1031
|
-
get exitCode() {
|
|
1032
|
-
return this._e2bHandle.exitCode;
|
|
1033
|
-
}
|
|
1034
|
-
async wait() {
|
|
1035
|
-
try {
|
|
1036
|
-
const result = await this._e2bHandle.wait();
|
|
1037
|
-
return {
|
|
1038
|
-
success: result.exitCode === 0,
|
|
1039
|
-
exitCode: result.exitCode,
|
|
1040
|
-
stdout: this.stdout,
|
|
1041
|
-
stderr: this.stderr,
|
|
1042
|
-
executionTimeMs: Date.now() - this._startTime
|
|
1043
|
-
};
|
|
1044
|
-
} catch (error) {
|
|
1045
|
-
const errorObj = error;
|
|
1046
|
-
const exitCode = errorObj.result?.exitCode ?? errorObj.exitCode ?? this.exitCode ?? 1;
|
|
1047
|
-
const attachedStdout = errorObj.result?.stdout || errorObj.stdout;
|
|
1048
|
-
const attachedStderr = errorObj.result?.stderr || errorObj.stderr;
|
|
1049
|
-
if (!this.stdout && !this.stdoutTruncated && attachedStdout) this.emitStdout(attachedStdout);
|
|
1050
|
-
if (!this.stderr && !this.stderrTruncated && attachedStderr) this.emitStderr(attachedStderr);
|
|
1051
|
-
const stdout = this.stdout;
|
|
1052
|
-
const stderr = this.stderr;
|
|
1053
|
-
const terminalError = errorObj.result?.error || errorObj.error || (error instanceof Error ? error.message : String(error));
|
|
1054
|
-
return {
|
|
1055
|
-
success: false,
|
|
1056
|
-
exitCode,
|
|
1057
|
-
stdout,
|
|
1058
|
-
stderr: [stderr, terminalError && !stderr.includes(terminalError) ? `Error: ${terminalError}` : ""].filter(Boolean).join("\n"),
|
|
1059
|
-
executionTimeMs: Date.now() - this._startTime
|
|
1060
|
-
};
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
async kill() {
|
|
1064
|
-
if (this.exitCode !== void 0) return false;
|
|
1065
|
-
return this._e2bHandle.kill();
|
|
1066
|
-
}
|
|
1067
|
-
async sendStdin(data) {
|
|
1068
|
-
if (this.exitCode !== void 0) throw new Error(`Process ${this.pid} has already exited with code ${this.exitCode}`);
|
|
1069
|
-
if (this._stdinMode === "ignore") throw new Error(`Process ${this.pid} was not started with stdin support`);
|
|
1070
|
-
await this._sandbox.commands.sendStdin(this._e2bHandle.pid, data);
|
|
1071
|
-
}
|
|
1072
|
-
async closeStdin() {
|
|
1073
|
-
throw new UnsupportedStdinCloseError("E2B SDK does not expose a way to close stdin for a running command");
|
|
1074
|
-
}
|
|
1075
|
-
};
|
|
1076
|
-
var E2BProcessManager = class extends SandboxProcessManager {
|
|
1077
|
-
_defaultTimeout;
|
|
1205
|
+
var E2BProcessManager = class extends CommandsApiProcessManager {
|
|
1078
1206
|
constructor(opts = {}) {
|
|
1079
|
-
super();
|
|
1080
|
-
this._defaultTimeout = opts.defaultTimeout;
|
|
1081
|
-
}
|
|
1082
|
-
async spawn(command, options = {}) {
|
|
1083
|
-
return this.sandbox.retryOnDead(async () => {
|
|
1084
|
-
const e2b = this.sandbox.e2b;
|
|
1085
|
-
const mergedEnv = { ...options.env };
|
|
1086
|
-
const envs = Object.fromEntries(Object.entries(mergedEnv).filter((entry) => entry[1] !== void 0));
|
|
1087
|
-
let handle;
|
|
1088
|
-
handle = new E2BProcessHandle(await e2b.commands.run(command, {
|
|
1089
|
-
background: true,
|
|
1090
|
-
stdin: options.stdinMode !== "ignore",
|
|
1091
|
-
cwd: options.cwd ?? this.sandbox.workingDirectory,
|
|
1092
|
-
envs,
|
|
1093
|
-
timeoutMs: options.timeout ?? this._defaultTimeout,
|
|
1094
|
-
onStdout: (data) => handle.emitStdout(data),
|
|
1095
|
-
onStderr: (data) => handle.emitStderr(data)
|
|
1096
|
-
}), e2b, Date.now(), options);
|
|
1097
|
-
this._tracked.set(handle.pid, handle);
|
|
1098
|
-
return handle;
|
|
1099
|
-
});
|
|
1100
|
-
}
|
|
1101
|
-
/**
|
|
1102
|
-
* List processes by querying E2B's commands API.
|
|
1103
|
-
* E2B manages all state server-side — no local tracking needed.
|
|
1104
|
-
*/
|
|
1105
|
-
async list() {
|
|
1106
|
-
return (await this.sandbox.e2b.commands.list()).map((proc) => ({
|
|
1107
|
-
pid: String(proc.pid),
|
|
1108
|
-
command: [proc.cmd, ...proc.args].join(" "),
|
|
1109
|
-
running: true
|
|
1110
|
-
}));
|
|
1207
|
+
super({ defaultTimeout: opts.defaultTimeout });
|
|
1111
1208
|
}
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
* for processes spawned externally or before reconnection.
|
|
1116
|
-
*/
|
|
1117
|
-
async get(pid) {
|
|
1118
|
-
const tracked = this._tracked.get(pid);
|
|
1119
|
-
if (tracked) return tracked;
|
|
1120
|
-
const numericPid = /^\d+$/.test(pid) ? Number(pid) : void 0;
|
|
1121
|
-
if (numericPid === void 0) return void 0;
|
|
1122
|
-
const e2b = this.sandbox.e2b;
|
|
1123
|
-
let handle;
|
|
1124
|
-
try {
|
|
1125
|
-
handle = new E2BProcessHandle(await e2b.commands.connect(numericPid, {
|
|
1126
|
-
onStdout: (data) => handle.emitStdout(data),
|
|
1127
|
-
onStderr: (data) => handle.emitStderr(data)
|
|
1128
|
-
}), e2b, Date.now());
|
|
1129
|
-
this._tracked.set(handle.pid, handle);
|
|
1130
|
-
return handle;
|
|
1131
|
-
} catch {
|
|
1132
|
-
return;
|
|
1133
|
-
}
|
|
1209
|
+
runCommands(operation, fn) {
|
|
1210
|
+
if (operation === "spawn") return this.sandbox.retryOnDead(() => fn(this.sandbox.e2b.commands));
|
|
1211
|
+
return fn(this.sandbox.e2b.commands);
|
|
1134
1212
|
}
|
|
1135
1213
|
};
|
|
1136
1214
|
//#endregion
|