@mastra/platform-workspace 1.11.1-alpha.0 → 1.12.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.
@@ -1,4 +1,4 @@
1
- import type { CommandsApi } from './_types/@internal_workspace/dist/index.js';
1
+ import type { CommandsApi } from './_types/@internal_workspace@0.0.20/dist/index.js';
2
2
  import type { EntryInfo } from 'e2b';
3
3
  import type { DirectExecOptions, DirectExecResult, ExecLease } from './direct-exec.js';
4
4
  export interface E2BExecLease extends ExecLease {
package/dist/index.cjs CHANGED
@@ -1051,6 +1051,20 @@ var FilesApiFilesystem = class extends _mastra_core_workspace.MastraFilesystem {
1051
1051
  throw new Error(`${operation} ${subject} failed (exit ${result.exitCode}): ${result.stderr.trim()}`);
1052
1052
  }
1053
1053
  };
1054
+ const KILL_DESCENDANTS_TIMEOUT_MS = 1e4;
1055
+ /**
1056
+ * A shell command that kills every process `pid` started, at any depth, but
1057
+ * not `pid` itself. It pauses `pid` first: a wrapper like npm exits on its own
1058
+ * once its child dies, and `kill(pid)` would then report it wasn't running.
1059
+ */
1060
+ function killDescendants(pid) {
1061
+ return [
1062
+ `kill -STOP ${pid}`,
1063
+ "kill_children() { for child in $(pgrep -P \"$1\"); do kill -STOP \"$child\"; (kill_children \"$child\"); kill -KILL \"$child\"; done; }",
1064
+ `kill_children ${pid}`,
1065
+ "true"
1066
+ ].join("\n");
1067
+ }
1054
1068
  /**
1055
1069
  * Output callbacks for `run`/`connect` that hold output until the handle
1056
1070
  * exists. The SDK's handle reads events from construction, before `run()`
@@ -1153,7 +1167,16 @@ var CommandsApiProcessHandle = class extends _mastra_core_workspace.ProcessHandl
1153
1167
  }
1154
1168
  async kill() {
1155
1169
  if (this.exitCode !== void 0) return false;
1156
- return this.manager.runCommands("kill", (commands) => commands.kill(this.inner.pid));
1170
+ return this.manager.runCommands("kill", async (commands) => {
1171
+ try {
1172
+ await (await commands.run(killDescendants(this.inner.pid), {
1173
+ background: true,
1174
+ stdin: false,
1175
+ timeoutMs: KILL_DESCENDANTS_TIMEOUT_MS
1176
+ })).wait();
1177
+ } catch {}
1178
+ return commands.kill(this.inner.pid);
1179
+ });
1157
1180
  }
1158
1181
  async sendStdin(data) {
1159
1182
  if (this.exitCode !== void 0) throw new Error(`Process ${this.pid} has already exited with code ${this.exitCode}`);
@@ -1733,6 +1756,8 @@ var SandboxExecTransportError = class extends Error {
1733
1756
  this.wsEndpoint = diagnostics.wsEndpoint;
1734
1757
  }
1735
1758
  };
1759
+ /** E2B's sandbox control port (envd). Never a preview. */
1760
+ const E2B_CONTROL_PORT = 49983;
1736
1761
  /**
1737
1762
  * Thrown when `/exec-lease` returns 410 Gone — the sandbox has been destroyed
1738
1763
  * (Railway destroy, quota reclamation, etc.). The client cannot recover from
@@ -1922,6 +1947,41 @@ var PlatformSandbox = class PlatformSandbox extends _mastra_core_workspace.Mastr
1922
1947
  get sandboxId() {
1923
1948
  return this.status === "running" ? this._sandboxId : void 0;
1924
1949
  }
1950
+ _networking = { getPortUrl: async (port) => (await this._previewGrant(port))?.url ?? null };
1951
+ /**
1952
+ * A cookie that signs a browser you control, such as an agent's own
1953
+ * browser, in to a port for an hour, so it opens the port's URL without
1954
+ * signing in. Never share it.
1955
+ *
1956
+ * `null` in the same cases as `networking.getPortUrl()`.
1957
+ */
1958
+ async getPortAuthCookie(port) {
1959
+ return (await this._previewGrant(port))?.cookie ?? null;
1960
+ }
1961
+ async _previewGrant(port) {
1962
+ if (!Number.isInteger(port) || port < 1 || port > 65535 || port === E2B_CONTROL_PORT) return null;
1963
+ if (this._client.sandboxProvider !== "e2b") return null;
1964
+ const sandboxId = this.sandboxId;
1965
+ if (!sandboxId) return null;
1966
+ try {
1967
+ const { url, cookie } = await (await this._request(`/sandbox/${encodeURIComponent(sandboxId)}/preview-grants`, {
1968
+ method: "POST",
1969
+ headers: { "content-type": "application/json" },
1970
+ body: JSON.stringify({ port })
1971
+ })).json();
1972
+ return {
1973
+ url: `${new URL(url).origin}/`,
1974
+ cookie
1975
+ };
1976
+ } catch (error) {
1977
+ if (error instanceof PlatformApiError && error.status === 410) {
1978
+ if (this._sandboxId === sandboxId) this._clearDestroyedState(sandboxId);
1979
+ return null;
1980
+ }
1981
+ if (error instanceof PlatformApiError && error.status === 404) return null;
1982
+ throw error;
1983
+ }
1984
+ }
1925
1985
  _client;
1926
1986
  _usesProviderRoutes;
1927
1987
  _environmentId;
@@ -2022,6 +2082,7 @@ var PlatformSandbox = class PlatformSandbox extends _mastra_core_workspace.Mastr
2022
2082
  this._hasRecoveryKey = options.id !== void 0;
2023
2083
  this.id = options.id ?? this.generateId();
2024
2084
  this._client = new PlatformClient(options);
2085
+ if (this._client.sandboxProvider === "e2b") this.networking = this._networking;
2025
2086
  this._environmentId = options.environmentId ?? process.env.MASTRA_ENVIRONMENT_ID ?? "";
2026
2087
  if (!this._environmentId && !options.sandboxId) throw new Error("environmentId is required");
2027
2088
  this._sandboxId = options.sandboxId;