@openai/agents-extensions 0.8.5 → 0.9.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 (213) hide show
  1. package/dist/metadata.js +2 -2
  2. package/dist/metadata.mjs +2 -2
  3. package/dist/sandbox/blaxel/index.d.ts +2 -0
  4. package/dist/sandbox/blaxel/index.js +19 -0
  5. package/dist/sandbox/blaxel/index.js.map +1 -0
  6. package/dist/sandbox/blaxel/index.mjs +3 -0
  7. package/dist/sandbox/blaxel/index.mjs.map +1 -0
  8. package/dist/sandbox/blaxel/mounts.d.ts +101 -0
  9. package/dist/sandbox/blaxel/mounts.js +542 -0
  10. package/dist/sandbox/blaxel/mounts.js.map +1 -0
  11. package/dist/sandbox/blaxel/mounts.mjs +529 -0
  12. package/dist/sandbox/blaxel/mounts.mjs.map +1 -0
  13. package/dist/sandbox/blaxel/sandbox.d.ts +155 -0
  14. package/dist/sandbox/blaxel/sandbox.js +1110 -0
  15. package/dist/sandbox/blaxel/sandbox.js.map +1 -0
  16. package/dist/sandbox/blaxel/sandbox.mjs +1072 -0
  17. package/dist/sandbox/blaxel/sandbox.mjs.map +1 -0
  18. package/dist/sandbox/cloudflare/index.d.ts +2 -0
  19. package/dist/sandbox/cloudflare/index.js +19 -0
  20. package/dist/sandbox/cloudflare/index.js.map +1 -0
  21. package/dist/sandbox/cloudflare/index.mjs +3 -0
  22. package/dist/sandbox/cloudflare/index.mjs.map +1 -0
  23. package/dist/sandbox/cloudflare/mounts.d.ts +28 -0
  24. package/dist/sandbox/cloudflare/mounts.js +150 -0
  25. package/dist/sandbox/cloudflare/mounts.js.map +1 -0
  26. package/dist/sandbox/cloudflare/mounts.mjs +143 -0
  27. package/dist/sandbox/cloudflare/mounts.mjs.map +1 -0
  28. package/dist/sandbox/cloudflare/sandbox.d.ts +91 -0
  29. package/dist/sandbox/cloudflare/sandbox.js +1163 -0
  30. package/dist/sandbox/cloudflare/sandbox.js.map +1 -0
  31. package/dist/sandbox/cloudflare/sandbox.mjs +1125 -0
  32. package/dist/sandbox/cloudflare/sandbox.mjs.map +1 -0
  33. package/dist/sandbox/daytona/index.d.ts +2 -0
  34. package/dist/sandbox/daytona/index.js +19 -0
  35. package/dist/sandbox/daytona/index.js.map +1 -0
  36. package/dist/sandbox/daytona/index.mjs +3 -0
  37. package/dist/sandbox/daytona/index.mjs.map +1 -0
  38. package/dist/sandbox/daytona/mounts.d.ts +11 -0
  39. package/dist/sandbox/daytona/mounts.js +17 -0
  40. package/dist/sandbox/daytona/mounts.js.map +1 -0
  41. package/dist/sandbox/daytona/mounts.mjs +12 -0
  42. package/dist/sandbox/daytona/mounts.mjs.map +1 -0
  43. package/dist/sandbox/daytona/sandbox.d.ts +160 -0
  44. package/dist/sandbox/daytona/sandbox.js +938 -0
  45. package/dist/sandbox/daytona/sandbox.js.map +1 -0
  46. package/dist/sandbox/daytona/sandbox.mjs +900 -0
  47. package/dist/sandbox/daytona/sandbox.mjs.map +1 -0
  48. package/dist/sandbox/e2b/index.d.ts +2 -0
  49. package/dist/sandbox/e2b/index.js +19 -0
  50. package/dist/sandbox/e2b/index.js.map +1 -0
  51. package/dist/sandbox/e2b/index.mjs +3 -0
  52. package/dist/sandbox/e2b/index.mjs.map +1 -0
  53. package/dist/sandbox/e2b/mounts.d.ts +11 -0
  54. package/dist/sandbox/e2b/mounts.js +17 -0
  55. package/dist/sandbox/e2b/mounts.js.map +1 -0
  56. package/dist/sandbox/e2b/mounts.mjs +12 -0
  57. package/dist/sandbox/e2b/mounts.mjs.map +1 -0
  58. package/dist/sandbox/e2b/sandbox.d.ts +169 -0
  59. package/dist/sandbox/e2b/sandbox.js +829 -0
  60. package/dist/sandbox/e2b/sandbox.js.map +1 -0
  61. package/dist/sandbox/e2b/sandbox.mjs +791 -0
  62. package/dist/sandbox/e2b/sandbox.mjs.map +1 -0
  63. package/dist/sandbox/modal/index.d.ts +2 -0
  64. package/dist/sandbox/modal/index.js +19 -0
  65. package/dist/sandbox/modal/index.js.map +1 -0
  66. package/dist/sandbox/modal/index.mjs +3 -0
  67. package/dist/sandbox/modal/index.mjs.map +1 -0
  68. package/dist/sandbox/modal/mounts.d.ts +24 -0
  69. package/dist/sandbox/modal/mounts.js +194 -0
  70. package/dist/sandbox/modal/mounts.js.map +1 -0
  71. package/dist/sandbox/modal/mounts.mjs +188 -0
  72. package/dist/sandbox/modal/mounts.mjs.map +1 -0
  73. package/dist/sandbox/modal/sandbox.d.ts +220 -0
  74. package/dist/sandbox/modal/sandbox.js +1621 -0
  75. package/dist/sandbox/modal/sandbox.js.map +1 -0
  76. package/dist/sandbox/modal/sandbox.mjs +1581 -0
  77. package/dist/sandbox/modal/sandbox.mjs.map +1 -0
  78. package/dist/sandbox/runloop/index.d.ts +2 -0
  79. package/dist/sandbox/runloop/index.js +19 -0
  80. package/dist/sandbox/runloop/index.js.map +1 -0
  81. package/dist/sandbox/runloop/index.mjs +3 -0
  82. package/dist/sandbox/runloop/index.mjs.map +1 -0
  83. package/dist/sandbox/runloop/mounts.d.ts +11 -0
  84. package/dist/sandbox/runloop/mounts.js +17 -0
  85. package/dist/sandbox/runloop/mounts.js.map +1 -0
  86. package/dist/sandbox/runloop/mounts.mjs +12 -0
  87. package/dist/sandbox/runloop/mounts.mjs.map +1 -0
  88. package/dist/sandbox/runloop/sandbox.d.ts +266 -0
  89. package/dist/sandbox/runloop/sandbox.js +1390 -0
  90. package/dist/sandbox/runloop/sandbox.js.map +1 -0
  91. package/dist/sandbox/runloop/sandbox.mjs +1346 -0
  92. package/dist/sandbox/runloop/sandbox.mjs.map +1 -0
  93. package/dist/sandbox/shared/archive.d.ts +34 -0
  94. package/dist/sandbox/shared/archive.js +397 -0
  95. package/dist/sandbox/shared/archive.js.map +1 -0
  96. package/dist/sandbox/shared/archive.mjs +389 -0
  97. package/dist/sandbox/shared/archive.mjs.map +1 -0
  98. package/dist/sandbox/shared/editor.d.ts +17 -0
  99. package/dist/sandbox/shared/editor.js +65 -0
  100. package/dist/sandbox/shared/editor.js.map +1 -0
  101. package/dist/sandbox/shared/editor.mjs +61 -0
  102. package/dist/sandbox/shared/editor.mjs.map +1 -0
  103. package/dist/sandbox/shared/environment.d.ts +1 -0
  104. package/dist/sandbox/shared/environment.js +13 -0
  105. package/dist/sandbox/shared/environment.js.map +1 -0
  106. package/dist/sandbox/shared/environment.mjs +2 -0
  107. package/dist/sandbox/shared/environment.mjs.map +1 -0
  108. package/dist/sandbox/shared/git.d.ts +4 -0
  109. package/dist/sandbox/shared/git.js +20 -0
  110. package/dist/sandbox/shared/git.js.map +1 -0
  111. package/dist/sandbox/shared/git.mjs +17 -0
  112. package/dist/sandbox/shared/git.mjs.map +1 -0
  113. package/dist/sandbox/shared/inContainerMounts.d.ts +40 -0
  114. package/dist/sandbox/shared/inContainerMounts.js +670 -0
  115. package/dist/sandbox/shared/inContainerMounts.js.map +1 -0
  116. package/dist/sandbox/shared/inContainerMounts.mjs +662 -0
  117. package/dist/sandbox/shared/inContainerMounts.mjs.map +1 -0
  118. package/dist/sandbox/shared/index.d.ts +23 -0
  119. package/dist/sandbox/shared/index.js +112 -0
  120. package/dist/sandbox/shared/index.js.map +1 -0
  121. package/dist/sandbox/shared/index.mjs +18 -0
  122. package/dist/sandbox/shared/index.mjs.map +1 -0
  123. package/dist/sandbox/shared/localSources.d.ts +8 -0
  124. package/dist/sandbox/shared/localSources.js +378 -0
  125. package/dist/sandbox/shared/localSources.js.map +1 -0
  126. package/dist/sandbox/shared/localSources.mjs +372 -0
  127. package/dist/sandbox/shared/localSources.mjs.map +1 -0
  128. package/dist/sandbox/shared/manifest.d.ts +38 -0
  129. package/dist/sandbox/shared/manifest.js +254 -0
  130. package/dist/sandbox/shared/manifest.js.map +1 -0
  131. package/dist/sandbox/shared/manifest.mjs +233 -0
  132. package/dist/sandbox/shared/manifest.mjs.map +1 -0
  133. package/dist/sandbox/shared/media.d.ts +1 -0
  134. package/dist/sandbox/shared/media.js +10 -0
  135. package/dist/sandbox/shared/media.js.map +1 -0
  136. package/dist/sandbox/shared/media.mjs +2 -0
  137. package/dist/sandbox/shared/media.mjs.map +1 -0
  138. package/dist/sandbox/shared/metadata.d.ts +7 -0
  139. package/dist/sandbox/shared/metadata.js +87 -0
  140. package/dist/sandbox/shared/metadata.js.map +1 -0
  141. package/dist/sandbox/shared/metadata.mjs +81 -0
  142. package/dist/sandbox/shared/metadata.mjs.map +1 -0
  143. package/dist/sandbox/shared/output.d.ts +1 -0
  144. package/dist/sandbox/shared/output.js +8 -0
  145. package/dist/sandbox/shared/output.js.map +1 -0
  146. package/dist/sandbox/shared/output.mjs +2 -0
  147. package/dist/sandbox/shared/output.mjs.map +1 -0
  148. package/dist/sandbox/shared/paths.d.ts +27 -0
  149. package/dist/sandbox/shared/paths.js +245 -0
  150. package/dist/sandbox/shared/paths.js.map +1 -0
  151. package/dist/sandbox/shared/paths.mjs +236 -0
  152. package/dist/sandbox/shared/paths.mjs.map +1 -0
  153. package/dist/sandbox/shared/ports.d.ts +14 -0
  154. package/dist/sandbox/shared/ports.js +75 -0
  155. package/dist/sandbox/shared/ports.js.map +1 -0
  156. package/dist/sandbox/shared/ports.mjs +69 -0
  157. package/dist/sandbox/shared/ports.mjs.map +1 -0
  158. package/dist/sandbox/shared/process.d.ts +16 -0
  159. package/dist/sandbox/shared/process.js +90 -0
  160. package/dist/sandbox/shared/process.js.map +1 -0
  161. package/dist/sandbox/shared/process.mjs +86 -0
  162. package/dist/sandbox/shared/process.mjs.map +1 -0
  163. package/dist/sandbox/shared/pty.d.ts +81 -0
  164. package/dist/sandbox/shared/pty.js +382 -0
  165. package/dist/sandbox/shared/pty.js.map +1 -0
  166. package/dist/sandbox/shared/pty.mjs +335 -0
  167. package/dist/sandbox/shared/pty.mjs.map +1 -0
  168. package/dist/sandbox/shared/runAs.d.ts +46 -0
  169. package/dist/sandbox/shared/runAs.js +149 -0
  170. package/dist/sandbox/shared/runAs.js.map +1 -0
  171. package/dist/sandbox/shared/runAs.mjs +141 -0
  172. package/dist/sandbox/shared/runAs.mjs.map +1 -0
  173. package/dist/sandbox/shared/session.d.ts +16 -0
  174. package/dist/sandbox/shared/session.js +138 -0
  175. package/dist/sandbox/shared/session.js.map +1 -0
  176. package/dist/sandbox/shared/session.mjs +128 -0
  177. package/dist/sandbox/shared/session.mjs.map +1 -0
  178. package/dist/sandbox/shared/sessionBase.d.ts +90 -0
  179. package/dist/sandbox/shared/sessionBase.js +446 -0
  180. package/dist/sandbox/shared/sessionBase.js.map +1 -0
  181. package/dist/sandbox/shared/sessionBase.mjs +442 -0
  182. package/dist/sandbox/shared/sessionBase.mjs.map +1 -0
  183. package/dist/sandbox/shared/sessionState.d.ts +7 -0
  184. package/dist/sandbox/shared/sessionState.js +35 -0
  185. package/dist/sandbox/shared/sessionState.js.map +1 -0
  186. package/dist/sandbox/shared/sessionState.mjs +31 -0
  187. package/dist/sandbox/shared/sessionState.mjs.map +1 -0
  188. package/dist/sandbox/shared/snapshots.d.ts +9 -0
  189. package/dist/sandbox/shared/snapshots.js +62 -0
  190. package/dist/sandbox/shared/snapshots.js.map +1 -0
  191. package/dist/sandbox/shared/snapshots.mjs +57 -0
  192. package/dist/sandbox/shared/snapshots.mjs.map +1 -0
  193. package/dist/sandbox/shared/typeGuards.d.ts +10 -0
  194. package/dist/sandbox/shared/typeGuards.js +50 -0
  195. package/dist/sandbox/shared/typeGuards.js.map +1 -0
  196. package/dist/sandbox/shared/typeGuards.mjs +38 -0
  197. package/dist/sandbox/shared/typeGuards.mjs.map +1 -0
  198. package/dist/sandbox/shared/types.d.ts +24 -0
  199. package/dist/sandbox/shared/types.js +3 -0
  200. package/dist/sandbox/shared/types.js.map +1 -0
  201. package/dist/sandbox/shared/types.mjs +2 -0
  202. package/dist/sandbox/shared/types.mjs.map +1 -0
  203. package/dist/sandbox/vercel/index.d.ts +1 -0
  204. package/dist/sandbox/vercel/index.js +18 -0
  205. package/dist/sandbox/vercel/index.js.map +1 -0
  206. package/dist/sandbox/vercel/index.mjs +2 -0
  207. package/dist/sandbox/vercel/index.mjs.map +1 -0
  208. package/dist/sandbox/vercel/sandbox.d.ts +135 -0
  209. package/dist/sandbox/vercel/sandbox.js +1001 -0
  210. package/dist/sandbox/vercel/sandbox.js.map +1 -0
  211. package/dist/sandbox/vercel/sandbox.mjs +963 -0
  212. package/dist/sandbox/vercel/sandbox.mjs.map +1 -0
  213. package/package.json +108 -4
@@ -0,0 +1,829 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.E2BSandboxClient = exports.E2BSandboxSession = void 0;
37
+ const agents_core_1 = require("@openai/agents-core");
38
+ const sandbox_1 = require("@openai/agents-core/sandbox");
39
+ const shared_1 = require("../shared/index.js");
40
+ const inContainerMounts_1 = require("../shared/inContainerMounts.js");
41
+ const mounts_1 = require("./mounts.js");
42
+ class E2BSandboxSession extends shared_1.RemoteSandboxSessionBase {
43
+ sandbox;
44
+ ptyProcesses = new shared_1.PtyProcessRegistry();
45
+ activeMountPaths = new Set();
46
+ constructor(args) {
47
+ super({
48
+ state: args.state,
49
+ options: {
50
+ providerName: 'E2BSandboxClient',
51
+ providerId: 'e2b',
52
+ concurrencyLimits: args.concurrencyLimits,
53
+ },
54
+ });
55
+ this.sandbox = args.sandbox;
56
+ this.state.pauseOnExitSupported = canPauseE2BSandbox(args.sandbox);
57
+ }
58
+ supportsPty() {
59
+ return typeof this.sandbox.pty?.create === 'function';
60
+ }
61
+ async writeStdin(args) {
62
+ return await (0, shared_1.writePtyStdin)({
63
+ providerName: 'E2BSandboxClient',
64
+ registry: this.ptyProcesses,
65
+ sessionId: args.sessionId,
66
+ chars: args.chars,
67
+ yieldTimeMs: args.yieldTimeMs,
68
+ maxOutputTokens: args.maxOutputTokens,
69
+ });
70
+ }
71
+ async execPtyCommand(args) {
72
+ if (args.runAs) {
73
+ throw new sandbox_1.SandboxUnsupportedFeatureError('E2BSandboxClient tty=true does not support runAs because the E2B SDK PTY API does not expose a user option.', {
74
+ provider: 'e2b',
75
+ feature: 'tty.runAs',
76
+ });
77
+ }
78
+ const pty = this.sandbox.pty;
79
+ if (!pty?.create) {
80
+ throw new sandbox_1.SandboxUnsupportedFeatureError('E2BSandboxClient tty=true requires E2B SDK PTY support.', {
81
+ provider: 'e2b',
82
+ feature: 'tty',
83
+ });
84
+ }
85
+ const start = Date.now();
86
+ const command = (0, shared_1.shellCommandForPty)(args);
87
+ const entry = (0, shared_1.createPtyProcessEntry)({ tty: true });
88
+ const timeoutMs = typeof this.state.commandTimeoutMs === 'number'
89
+ ? this.state.commandTimeoutMs
90
+ : typeof this.state.timeout === 'number'
91
+ ? Math.max(1, Math.trunc(this.state.timeout * 1000))
92
+ : undefined;
93
+ const handle = await pty.create({
94
+ cols: 80,
95
+ rows: 24,
96
+ size: { cols: 80, rows: 24 },
97
+ cwd: this.resolveWorkdir(args.workdir),
98
+ envs: this.state.environment,
99
+ timeoutMs,
100
+ onData: (data) => (0, shared_1.appendPtyOutput)(entry, data),
101
+ });
102
+ entry.terminate = async () => {
103
+ if (handle.kill) {
104
+ await handle.kill();
105
+ }
106
+ else if (pty.kill) {
107
+ await pty.kill(handle.pid, {
108
+ requestTimeoutMs: this.state.requestTimeoutMs,
109
+ });
110
+ }
111
+ };
112
+ if (!pty.sendInput) {
113
+ await entry.terminate().catch(() => { });
114
+ throw new sandbox_1.SandboxUnsupportedFeatureError('E2BSandboxClient tty=true requires E2B SDK PTY stdin support.', {
115
+ provider: 'e2b',
116
+ feature: 'tty.stdin',
117
+ });
118
+ }
119
+ entry.sendInput = async (chars) => {
120
+ await pty.sendInput(handle.pid, new TextEncoder().encode(chars), {
121
+ requestTimeoutMs: this.state.requestTimeoutMs,
122
+ });
123
+ };
124
+ (0, shared_1.watchPtyProcess)(entry, async () => (handle.wait ? await handle.wait() : undefined), (result, error) => exitCodeFromE2BResult(result) ??
125
+ exitCodeFromE2BResult(error) ??
126
+ exitCodeFromE2BResult(handle));
127
+ try {
128
+ await entry.sendInput(`${command}\n`);
129
+ }
130
+ catch (error) {
131
+ await entry.terminate().catch(() => { });
132
+ throw error;
133
+ }
134
+ const { sessionId, pruned } = this.ptyProcesses.register(entry);
135
+ if (pruned) {
136
+ await pruned.terminate?.().catch(() => { });
137
+ }
138
+ return await (0, shared_1.formatPtyExecUpdate)({
139
+ registry: this.ptyProcesses,
140
+ sessionId,
141
+ entry,
142
+ startTime: start,
143
+ yieldTimeMs: args.yieldTimeMs,
144
+ maxOutputTokens: args.maxOutputTokens,
145
+ });
146
+ }
147
+ exposedPortSource() {
148
+ return 'host';
149
+ }
150
+ async resolveRemoteExposedPort(requestedPort) {
151
+ if (!this.sandbox.getHost) {
152
+ throw new sandbox_1.SandboxProviderError('E2BSandboxClient exposed port resolution requires an E2B SDK getHost(port) API.', {
153
+ provider: 'e2b',
154
+ port: requestedPort,
155
+ });
156
+ }
157
+ let host;
158
+ try {
159
+ host = await this.sandbox.getHost(requestedPort);
160
+ }
161
+ catch (error) {
162
+ throw new sandbox_1.SandboxProviderError(`E2BSandboxClient failed to resolve exposed port ${requestedPort}.`, {
163
+ provider: 'e2b',
164
+ port: requestedPort,
165
+ cause: error instanceof Error ? error.message : String(error),
166
+ });
167
+ }
168
+ if (typeof host !== 'string') {
169
+ throw new sandbox_1.SandboxProviderError('E2BSandboxClient exposed port resolution returned a non-string host.', {
170
+ provider: 'e2b',
171
+ port: requestedPort,
172
+ });
173
+ }
174
+ return (0, shared_1.parseExposedPortEndpoint)(host, {
175
+ providerName: 'E2BSandboxClient',
176
+ source: 'host',
177
+ });
178
+ }
179
+ async prepareWorkspaceRoot() {
180
+ const root = this.state.manifest.root;
181
+ await this.ensureDirectory(root);
182
+ const result = await this.sandbox.commands.run(`mkdir -p -- ${(0, shared_1.shellQuote)(root)}`, {
183
+ cwd: '/',
184
+ envs: this.state.environment,
185
+ });
186
+ if ((result.exitCode ?? 1) !== 0) {
187
+ throw new sandbox_1.SandboxProviderError('E2BSandboxClient failed to prepare the workspace root.', {
188
+ provider: 'e2b',
189
+ operation: 'prepare workspace root',
190
+ sandboxId: this.state.sandboxId,
191
+ root,
192
+ stderr: result.stderr ?? '',
193
+ stdout: result.stdout ?? '',
194
+ });
195
+ }
196
+ }
197
+ async persistWorkspace() {
198
+ if (this.state.workspacePersistence === 'snapshot') {
199
+ const archive = await this.persistWorkspaceViaNativeSnapshot();
200
+ if (archive) {
201
+ return archive;
202
+ }
203
+ }
204
+ else {
205
+ (0, shared_1.assertTarWorkspacePersistence)('E2BSandboxClient', this.state.workspacePersistence);
206
+ }
207
+ return await this.persistWorkspaceTar();
208
+ }
209
+ async hydrateWorkspace(data) {
210
+ const snapshotRef = (0, shared_1.decodeNativeSnapshotRef)(data);
211
+ if (snapshotRef?.provider === 'e2b') {
212
+ await this.replaceSandboxFromSnapshot(snapshotRef.snapshotId);
213
+ return;
214
+ }
215
+ if (this.state.workspacePersistence !== 'snapshot') {
216
+ (0, shared_1.assertTarWorkspacePersistence)('E2BSandboxClient', this.state.workspacePersistence);
217
+ }
218
+ await this.hydrateWorkspaceTar(data);
219
+ }
220
+ async persistWorkspaceViaNativeSnapshot() {
221
+ if (this.nativeSnapshotRequiresTarFallback()) {
222
+ return undefined;
223
+ }
224
+ if (!this.sandbox.createSnapshot) {
225
+ return undefined;
226
+ }
227
+ let snapshot;
228
+ try {
229
+ snapshot = await this.sandbox.createSnapshot();
230
+ }
231
+ catch (error) {
232
+ throw new sandbox_1.SandboxProviderError('E2BSandboxClient failed to create a native workspace snapshot.', {
233
+ provider: 'e2b',
234
+ sandboxId: this.state.sandboxId,
235
+ cause: error instanceof Error ? error.message : String(error),
236
+ });
237
+ }
238
+ if (!snapshot.snapshotId) {
239
+ throw new sandbox_1.SandboxProviderError('E2BSandboxClient native snapshot persistence did not return a snapshot id.', {
240
+ provider: 'e2b',
241
+ sandboxId: this.state.sandboxId,
242
+ });
243
+ }
244
+ return (0, shared_1.encodeNativeSnapshotRef)({
245
+ provider: 'e2b',
246
+ snapshotId: snapshot.snapshotId,
247
+ });
248
+ }
249
+ nativeSnapshotRequiresTarFallback() {
250
+ return this.state.manifest.ephemeralPersistencePaths().size > 0;
251
+ }
252
+ async replaceSandboxFromSnapshot(snapshotId) {
253
+ const Sandbox = await loadE2BSandboxClass(this.state.sandboxType);
254
+ const previousSandbox = this.sandbox;
255
+ let sandbox;
256
+ try {
257
+ sandbox = await createSandboxInstance(Sandbox, {
258
+ ...stateToCreateOptions(this.state),
259
+ template: snapshotId,
260
+ }, this.state.environment);
261
+ }
262
+ catch (error) {
263
+ throw new sandbox_1.SandboxProviderError('E2BSandboxClient failed to restore a native workspace snapshot.', {
264
+ provider: 'e2b',
265
+ snapshotId,
266
+ cause: error instanceof Error ? error.message : String(error),
267
+ });
268
+ }
269
+ try {
270
+ await previousSandbox.kill();
271
+ }
272
+ catch (error) {
273
+ await sandbox.kill().catch(() => { });
274
+ throw new sandbox_1.SandboxProviderError('E2BSandboxClient failed to terminate the previous sandbox while restoring a native workspace snapshot.', {
275
+ provider: 'e2b',
276
+ operation: 'restore snapshot',
277
+ sandboxId: previousSandbox.sandboxId,
278
+ replacementSandboxId: sandbox.sandboxId,
279
+ snapshotId,
280
+ cause: error instanceof Error ? error.message : String(error),
281
+ });
282
+ }
283
+ this.sandbox = sandbox;
284
+ this.state.sandboxId = sandbox.sandboxId;
285
+ delete this.state.exposedPorts;
286
+ }
287
+ async close() {
288
+ await (0, shared_1.withSandboxSpan)('sandbox.stop', {
289
+ backend_id: 'e2b',
290
+ sandbox_id: this.state.sandboxId,
291
+ }, async () => {
292
+ await this.ptyProcesses.terminateAll();
293
+ if (this.state.pauseOnExit && canPauseE2BSandbox(this.sandbox)) {
294
+ await this.pauseOrKillAfterPauseFailure('close');
295
+ return;
296
+ }
297
+ await this.unmountActiveMounts();
298
+ await this.sandbox.kill();
299
+ });
300
+ }
301
+ async shutdown(options) {
302
+ await (0, shared_1.withSandboxSpan)('sandbox.shutdown', {
303
+ backend_id: 'e2b',
304
+ sandbox_id: this.state.sandboxId,
305
+ }, async () => {
306
+ await this.ptyProcesses.terminateAll();
307
+ if (this.shouldPauseOnCleanup(options)) {
308
+ return;
309
+ }
310
+ await this.unmountActiveMounts();
311
+ });
312
+ }
313
+ async delete(options) {
314
+ await (0, shared_1.withSandboxSpan)('sandbox.shutdown', {
315
+ backend_id: 'e2b',
316
+ sandbox_id: this.state.sandboxId,
317
+ }, async () => {
318
+ await this.ptyProcesses.terminateAll();
319
+ if (this.shouldPauseOnCleanup(options)) {
320
+ await this.pauseOrKillAfterPauseFailure('cleanup');
321
+ return;
322
+ }
323
+ await this.unmountActiveMounts();
324
+ await this.sandbox.kill();
325
+ });
326
+ }
327
+ shouldPauseOnCleanup(options) {
328
+ return (options?.reason === 'cleanup' &&
329
+ options.preserveOwnedSessions === true &&
330
+ this.state.pauseOnExit &&
331
+ canPauseE2BSandbox(this.sandbox));
332
+ }
333
+ async pauseOrKillAfterPauseFailure(operation) {
334
+ try {
335
+ await this.sandbox.pause();
336
+ return true;
337
+ }
338
+ catch (pauseError) {
339
+ try {
340
+ await this.unmountActiveMounts();
341
+ await this.sandbox.kill();
342
+ }
343
+ catch (killError) {
344
+ throw new sandbox_1.SandboxProviderError('E2BSandboxClient failed to pause and then terminate the sandbox.', {
345
+ provider: 'e2b',
346
+ operation,
347
+ sandboxId: this.state.sandboxId,
348
+ pauseCause: pauseError instanceof Error
349
+ ? pauseError.message
350
+ : String(pauseError),
351
+ killCause: killError instanceof Error
352
+ ? killError.message
353
+ : String(killError),
354
+ });
355
+ }
356
+ this.state.pauseOnExit = false;
357
+ this.state.pauseOnExitSupported = false;
358
+ return false;
359
+ }
360
+ }
361
+ assertExecRunAs(_runAs) {
362
+ void _runAs;
363
+ }
364
+ assertFilesystemRunAs(_runAs) {
365
+ void _runAs;
366
+ }
367
+ manifestMetadataSupport() {
368
+ return shared_1.SANDBOX_MANIFEST_METADATA_SUPPORT;
369
+ }
370
+ manifestMaterializationOptions() {
371
+ return {
372
+ materializeMount: this.materializeMountEntry.bind(this),
373
+ };
374
+ }
375
+ async runRemoteCommand(command, options) {
376
+ const result = await this.runCommandForStatus(command, {
377
+ cwd: options.workdir,
378
+ envs: this.state.environment,
379
+ timeoutMs: options.timeoutMs ??
380
+ (options.kind === 'exec' ? this.commandTimeoutMs() : undefined),
381
+ user: options.runAs,
382
+ });
383
+ return {
384
+ status: result.exitCode ?? 1,
385
+ stdout: result.stdout ?? '',
386
+ stderr: [result.stderr ?? '', result.error ?? '']
387
+ .filter((value) => value.trim().length > 0)
388
+ .join('\n'),
389
+ };
390
+ }
391
+ async mkdirRemote(path) {
392
+ await this.ensureDirectory(path);
393
+ }
394
+ async readRemoteText(path) {
395
+ if (this.sandbox.files.read) {
396
+ const result = await this.sandbox.files.read(path);
397
+ return typeof result === 'string'
398
+ ? result
399
+ : new TextDecoder().decode(result);
400
+ }
401
+ const result = await this.runRemoteCommand(`cat ${(0, shared_1.shellQuote)(path)}`, {
402
+ kind: 'path',
403
+ workdir: this.state.manifest.root,
404
+ });
405
+ if (result.status !== 0) {
406
+ throw new agents_core_1.UserError(`Sandbox path not found: ${path}`);
407
+ }
408
+ return result.stdout ?? '';
409
+ }
410
+ async readRemoteFile(path) {
411
+ if (this.sandbox.files.read) {
412
+ const result = await this.sandbox.files.read(path, { format: 'bytes' });
413
+ return typeof result === 'string'
414
+ ? new TextEncoder().encode(result)
415
+ : Uint8Array.from(result);
416
+ }
417
+ const encoded = await this.sandbox.commands.run(`base64 ${(0, shared_1.shellQuote)(path)}`, {
418
+ cwd: this.state.manifest.root,
419
+ envs: this.state.environment,
420
+ });
421
+ if ((encoded.exitCode ?? 1) !== 0 || !encoded.stdout) {
422
+ throw new agents_core_1.UserError(`Sandbox path not found: ${path}`);
423
+ }
424
+ return Uint8Array.from(Buffer.from(encoded.stdout.replace(/\s+/g, ''), 'base64'));
425
+ }
426
+ async writeRemoteFile(path, content) {
427
+ await this.sandbox.files.write(path, content);
428
+ }
429
+ async deleteRemotePath(path) {
430
+ await this.removeSandboxPath(path);
431
+ }
432
+ async ensureDirectory(path) {
433
+ if (this.sandbox.files.makeDir) {
434
+ await this.sandbox.files.makeDir(path);
435
+ return;
436
+ }
437
+ await this.sandbox.commands.run(`mkdir -p -- ${(0, shared_1.shellQuote)(path)}`, {
438
+ cwd: '/',
439
+ envs: this.state.environment,
440
+ });
441
+ }
442
+ async removeSandboxPath(path) {
443
+ if (this.sandbox.files.remove) {
444
+ await this.sandbox.files.remove(path);
445
+ return;
446
+ }
447
+ await this.sandbox.commands.run(`rm -f -- ${(0, shared_1.shellQuote)(path)}`, {
448
+ cwd: this.state.manifest.root,
449
+ envs: this.state.environment,
450
+ });
451
+ }
452
+ async materializeMountEntry(absolutePath, entry) {
453
+ if (!(0, mounts_1.isE2BCloudBucketMountEntry)(entry)) {
454
+ throw new sandbox_1.SandboxUnsupportedFeatureError('E2BSandboxClient only supports E2BCloudBucketMountStrategy mount entries.', {
455
+ provider: 'e2b',
456
+ feature: 'entry.mountStrategy',
457
+ path: absolutePath,
458
+ mountType: entry.type,
459
+ strategyType: entry.mountStrategy?.type,
460
+ });
461
+ }
462
+ const mountPath = await this.resolveRemotePath(entry.mountPath ?? absolutePath, { forWrite: true });
463
+ await (0, inContainerMounts_1.mountRcloneCloudBucket)({
464
+ providerName: 'E2BSandboxClient',
465
+ providerId: 'e2b',
466
+ strategyType: 'e2b_cloud_bucket',
467
+ entry,
468
+ mountPath,
469
+ pattern: (0, inContainerMounts_1.rclonePatternFromMountStrategy)(entry.mountStrategy),
470
+ runCommand: this.mountCommandRunner(),
471
+ writeFile: this.writeRemoteFile.bind(this),
472
+ packageManagers: ['apt'],
473
+ installRcloneViaScript: true,
474
+ });
475
+ this.activeMountPaths.add(mountPath);
476
+ }
477
+ async unmountActiveMounts() {
478
+ for (const mountPath of [...this.activeMountPaths].reverse()) {
479
+ await (0, inContainerMounts_1.unmountRcloneMount)({
480
+ providerName: 'E2BSandboxClient',
481
+ providerId: 'e2b',
482
+ mountPath,
483
+ runCommand: this.mountCommandRunner(),
484
+ }).catch(() => { });
485
+ this.activeMountPaths.delete(mountPath);
486
+ }
487
+ }
488
+ mountCommandRunner() {
489
+ return async (command, options = {}) => {
490
+ const result = await this.runCommandForStatus(command, {
491
+ cwd: this.state.manifest.root,
492
+ envs: this.state.environment,
493
+ timeoutMs: options.timeoutMs,
494
+ user: options.user,
495
+ });
496
+ return {
497
+ status: result.exitCode ?? 1,
498
+ stdout: result.stdout ?? '',
499
+ stderr: result.stderr ?? result.error ?? '',
500
+ };
501
+ };
502
+ }
503
+ async runCommandForStatus(command, options) {
504
+ try {
505
+ return await this.sandbox.commands.run(command, options);
506
+ }
507
+ catch (error) {
508
+ const result = commandResultFromE2BError(error);
509
+ if (!result) {
510
+ throw error;
511
+ }
512
+ return result;
513
+ }
514
+ }
515
+ commandTimeoutMs() {
516
+ return typeof this.state.commandTimeoutMs === 'number'
517
+ ? this.state.commandTimeoutMs
518
+ : typeof this.state.timeout === 'number'
519
+ ? Math.max(1, Math.trunc(this.state.timeout * 1000))
520
+ : undefined;
521
+ }
522
+ }
523
+ exports.E2BSandboxSession = E2BSandboxSession;
524
+ /**
525
+ * @see {@link https://e2b.dev/docs | E2B docs}.
526
+ * @see {@link https://e2b.dev/docs/sdk-reference/js-sdk/v2.8.0/sandbox | JavaScript SDK Sandbox reference}.
527
+ * @see {@link https://e2b.dev/docs/sdk-reference/code-interpreter-js-sdk/v2.3.2/sandbox | Code Interpreter JavaScript SDK Sandbox reference}.
528
+ */
529
+ class E2BSandboxClient {
530
+ backendId = 'e2b';
531
+ options;
532
+ constructor(options = {}) {
533
+ this.options = options;
534
+ }
535
+ async create(args, manifestOptions) {
536
+ const createArgs = (0, sandbox_1.normalizeSandboxClientCreateArgs)(args, manifestOptions);
537
+ (0, shared_1.assertCoreSnapshotUnsupported)('E2BSandboxClient', createArgs.snapshot);
538
+ const manifest = createArgs.manifest;
539
+ return await (0, shared_1.withSandboxSpan)('sandbox.start', {
540
+ backend_id: this.backendId,
541
+ }, async () => {
542
+ const resolvedOptions = {
543
+ ...this.options,
544
+ ...createArgs.options,
545
+ };
546
+ validateOptions(resolvedOptions);
547
+ (0, shared_1.assertSandboxManifestMetadataSupported)('E2BSandboxClient', manifest, shared_1.SANDBOX_MANIFEST_METADATA_SUPPORT);
548
+ const Sandbox = await loadE2BSandboxClass(resolvedOptions.sandboxType ?? 'e2b');
549
+ const environment = await (0, shared_1.materializeEnvironment)(manifest, resolvedOptions.env);
550
+ const sandbox = await createSandboxInstance(Sandbox, resolvedOptions, environment);
551
+ const session = new E2BSandboxSession({
552
+ sandbox,
553
+ concurrencyLimits: createArgs.concurrencyLimits,
554
+ state: {
555
+ manifest,
556
+ sandboxId: sandbox.sandboxId,
557
+ template: resolvedOptions.template,
558
+ sandboxType: resolvedOptions.sandboxType ?? 'e2b',
559
+ timeout: resolvedOptions.timeout,
560
+ commandTimeoutMs: resolvedOptions.commandTimeoutMs,
561
+ requestTimeoutMs: resolvedOptions.requestTimeoutMs,
562
+ connectionTimeoutMs: resolvedOptions.connectionTimeoutMs,
563
+ onTimeout: resolveE2BOnTimeout(resolvedOptions),
564
+ timeoutAction: resolvedOptions.timeoutAction,
565
+ autoResume: resolvedOptions.autoResume,
566
+ metadata: resolvedOptions.metadata,
567
+ secure: resolvedOptions.secure,
568
+ allowInternetAccess: resolvedOptions.allowInternetAccess,
569
+ configuredExposedPorts: resolvedOptions.exposedPorts,
570
+ workspacePersistence: resolvedOptions.workspacePersistence,
571
+ mcp: resolvedOptions.mcp,
572
+ pauseOnExit: resolvedOptions.pauseOnExit ?? false,
573
+ environment,
574
+ },
575
+ });
576
+ try {
577
+ await session.prepareWorkspaceRoot();
578
+ await session.applyManifest(manifest);
579
+ }
580
+ catch (error) {
581
+ session.state.pauseOnExit = false;
582
+ await (0, shared_1.closeRemoteSessionOnManifestError)('E2B', session, error);
583
+ }
584
+ return session;
585
+ });
586
+ }
587
+ async serializeSessionState(state) {
588
+ return (0, shared_1.serializeRemoteSandboxSessionState)(state);
589
+ }
590
+ canPersistOwnedSessionState(state) {
591
+ return state.pauseOnExit && state.pauseOnExitSupported === true;
592
+ }
593
+ async deserializeSessionState(state) {
594
+ const baseState = (0, shared_1.deserializeRemoteSandboxSessionStateValues)(state, this.options.env);
595
+ return {
596
+ ...state,
597
+ ...baseState,
598
+ sandboxId: (0, shared_1.readString)(state, 'sandboxId'),
599
+ template: (0, shared_1.readOptionalString)(state, 'template'),
600
+ sandboxType: state.sandboxType === 'e2b_code_interpreter'
601
+ ? 'e2b_code_interpreter'
602
+ : 'e2b',
603
+ timeout: (0, shared_1.readOptionalNumber)(state, 'timeout'),
604
+ commandTimeoutMs: (0, shared_1.readOptionalNumber)(state, 'commandTimeoutMs'),
605
+ requestTimeoutMs: (0, shared_1.readOptionalNumber)(state, 'requestTimeoutMs'),
606
+ connectionTimeoutMs: (0, shared_1.readOptionalNumber)(state, 'connectionTimeoutMs'),
607
+ onTimeout: readOptionalE2BTimeoutAction((0, shared_1.readOptionalString)(state, 'onTimeout') ??
608
+ (0, shared_1.readOptionalString)(state, 'timeoutAction')),
609
+ timeoutAction: readOptionalE2BTimeoutAction((0, shared_1.readOptionalString)(state, 'timeoutAction')),
610
+ autoResume: (0, shared_1.readOptionalBoolean)(state, 'autoResume'),
611
+ metadata: (0, shared_1.readOptionalStringRecord)(state.metadata),
612
+ secure: (0, shared_1.readOptionalBoolean)(state, 'secure'),
613
+ allowInternetAccess: (0, shared_1.readOptionalBoolean)(state, 'allowInternetAccess'),
614
+ configuredExposedPorts: (0, shared_1.readOptionalNumberArray)(state.configuredExposedPorts),
615
+ workspacePersistence: readE2BWorkspacePersistence(state.workspacePersistence),
616
+ mcp: (0, shared_1.readOptionalRecord)(state.mcp),
617
+ pauseOnExit: Boolean(state.pauseOnExit),
618
+ pauseOnExitSupported: (0, shared_1.readOptionalBoolean)(state, 'pauseOnExitSupported') ?? false,
619
+ };
620
+ }
621
+ async resume(state) {
622
+ const Sandbox = await loadE2BSandboxClass(state.sandboxType);
623
+ const connect = Sandbox.connect ?? Sandbox.resume;
624
+ if (connect) {
625
+ try {
626
+ const sandbox = await connect.call(Sandbox, state.sandboxId, e2bReconnectOptions(state));
627
+ return new E2BSandboxSession({ state, sandbox });
628
+ }
629
+ catch (error) {
630
+ (0, shared_1.assertResumeRecreateAllowed)(error, {
631
+ providerName: 'E2BSandboxClient',
632
+ provider: 'e2b',
633
+ details: { sandboxId: state.sandboxId },
634
+ });
635
+ // Fall through to recreate from serialized state.
636
+ }
637
+ }
638
+ return await this.create(state.manifest, {
639
+ ...stateToCreateOptions(state),
640
+ env: state.environment,
641
+ });
642
+ }
643
+ }
644
+ exports.E2BSandboxClient = E2BSandboxClient;
645
+ function validateOptions(options) {
646
+ readE2BWorkspacePersistence(options.workspacePersistence);
647
+ resolveE2BOnTimeout(options);
648
+ }
649
+ function resolveE2BOnTimeout(options) {
650
+ return readOptionalE2BTimeoutAction(options.onTimeout ?? options.timeoutAction);
651
+ }
652
+ function readOptionalE2BTimeoutAction(value) {
653
+ if (value === undefined || value === 'pause' || value === 'kill') {
654
+ return value;
655
+ }
656
+ throw new sandbox_1.SandboxUnsupportedFeatureError('E2BSandboxClient onTimeout must be "pause" or "kill".', {
657
+ provider: 'E2BSandboxClient',
658
+ feature: 'onTimeout',
659
+ onTimeout: value,
660
+ });
661
+ }
662
+ function readE2BWorkspacePersistence(value) {
663
+ if (value === undefined ||
664
+ value === true ||
665
+ value === 'tar' ||
666
+ value === 'snapshot') {
667
+ return value;
668
+ }
669
+ throw new sandbox_1.SandboxUnsupportedFeatureError('E2BSandboxClient workspacePersistence must be true, "tar", or "snapshot".', {
670
+ provider: 'E2BSandboxClient',
671
+ feature: 'workspacePersistence',
672
+ workspacePersistence: value,
673
+ });
674
+ }
675
+ function stateToCreateOptions(state) {
676
+ return {
677
+ sandboxType: state.sandboxType,
678
+ template: state.template,
679
+ timeout: state.timeout,
680
+ commandTimeoutMs: state.commandTimeoutMs,
681
+ requestTimeoutMs: state.requestTimeoutMs,
682
+ connectionTimeoutMs: state.connectionTimeoutMs,
683
+ onTimeout: state.onTimeout,
684
+ timeoutAction: state.timeoutAction,
685
+ autoResume: state.autoResume,
686
+ metadata: state.metadata,
687
+ secure: state.secure,
688
+ allowInternetAccess: state.allowInternetAccess,
689
+ exposedPorts: state.configuredExposedPorts,
690
+ workspacePersistence: state.workspacePersistence,
691
+ mcp: state.mcp,
692
+ pauseOnExit: state.pauseOnExit,
693
+ };
694
+ }
695
+ function e2bReconnectOptions(state) {
696
+ const options = {};
697
+ if (state.timeout !== undefined) {
698
+ options.timeoutMs = Math.max(1, Math.trunc(state.timeout * 1000));
699
+ }
700
+ if (state.requestTimeoutMs !== undefined) {
701
+ options.requestTimeoutMs = state.requestTimeoutMs;
702
+ }
703
+ if (state.connectionTimeoutMs !== undefined) {
704
+ options.connectionTimeoutMs = state.connectionTimeoutMs;
705
+ }
706
+ return Object.keys(options).length > 0 ? options : undefined;
707
+ }
708
+ function canPauseE2BSandbox(sandbox) {
709
+ return typeof sandbox.pause === 'function';
710
+ }
711
+ async function loadE2BSandboxClass(sandboxType) {
712
+ const moduleName = sandboxType === 'e2b_code_interpreter' ? '@e2b/code-interpreter' : 'e2b';
713
+ try {
714
+ const Sandbox = sandboxType === 'e2b_code_interpreter'
715
+ ? (await Promise.resolve().then(() => __importStar(require('@e2b/code-interpreter')))).Sandbox
716
+ : (await Promise.resolve().then(() => __importStar(require('e2b')))).Sandbox;
717
+ if (!Sandbox) {
718
+ throw new Error(`Missing Sandbox export from ${moduleName}.`);
719
+ }
720
+ return adaptE2BSandboxClass(Sandbox);
721
+ }
722
+ catch (error) {
723
+ throw new agents_core_1.UserError(`E2B sandbox support requires the optional \`${moduleName}\` package. Install it before using E2B-backed sandbox examples. ${error.message}`);
724
+ }
725
+ }
726
+ function adaptE2BSandboxClass(Sandbox) {
727
+ const sdkSandbox = Sandbox;
728
+ const adapted = {
729
+ create: Sandbox.create.bind(Sandbox),
730
+ };
731
+ if (typeof sdkSandbox.connect === 'function') {
732
+ adapted.connect = sdkSandbox.connect.bind(Sandbox);
733
+ }
734
+ if (typeof sdkSandbox.resume === 'function') {
735
+ adapted.resume = sdkSandbox.resume.bind(Sandbox);
736
+ }
737
+ return adapted;
738
+ }
739
+ async function createSandboxInstance(Sandbox, options, environment) {
740
+ const createOptions = {};
741
+ if (typeof options.timeout === 'number') {
742
+ createOptions.timeoutMs = Math.max(1, Math.trunc(options.timeout * 1000));
743
+ }
744
+ const onTimeout = resolveE2BOnTimeout(options);
745
+ const lifecycle = {};
746
+ if (onTimeout !== undefined) {
747
+ lifecycle.onTimeout = onTimeout;
748
+ }
749
+ if (options.autoResume !== undefined && onTimeout === 'pause') {
750
+ lifecycle.autoResume = options.autoResume;
751
+ }
752
+ if (Object.keys(lifecycle).length > 0) {
753
+ createOptions.lifecycle = lifecycle;
754
+ }
755
+ for (const key of [
756
+ 'commandTimeoutMs',
757
+ 'requestTimeoutMs',
758
+ 'connectionTimeoutMs',
759
+ 'metadata',
760
+ 'secure',
761
+ 'allowInternetAccess',
762
+ 'mcp',
763
+ ]) {
764
+ if (options[key] !== undefined) {
765
+ createOptions[key] = options[key];
766
+ }
767
+ }
768
+ if (options.exposedPorts?.length) {
769
+ createOptions.network = {
770
+ allowPublicTraffic: true,
771
+ };
772
+ }
773
+ if (Object.keys(environment).length > 0) {
774
+ createOptions.envs = environment;
775
+ }
776
+ if (options.template) {
777
+ return await (0, shared_1.withProviderError)('E2BSandboxClient', 'e2b', 'create sandbox', async () => await Sandbox.create(options.template, createOptions), { template: options.template });
778
+ }
779
+ if (Object.keys(createOptions).length > 0) {
780
+ return await (0, shared_1.withProviderError)('E2BSandboxClient', 'e2b', 'create sandbox', async () => await Sandbox.create(createOptions));
781
+ }
782
+ return await (0, shared_1.withProviderError)('E2BSandboxClient', 'e2b', 'create sandbox', async () => await Sandbox.create());
783
+ }
784
+ function commandResultFromE2BError(error) {
785
+ return commandResultFromE2BErrorValue(error, new Set());
786
+ }
787
+ function commandResultFromE2BErrorValue(value, seen) {
788
+ if (!(0, shared_1.isRecord)(value) || seen.has(value)) {
789
+ return undefined;
790
+ }
791
+ seen.add(value);
792
+ const directResult = commandResultFromRecord(value);
793
+ if (directResult) {
794
+ return directResult;
795
+ }
796
+ for (const key of ['result', 'context', 'data', 'details', 'cause']) {
797
+ const nestedResult = commandResultFromE2BErrorValue(value[key], seen);
798
+ if (nestedResult) {
799
+ return nestedResult;
800
+ }
801
+ }
802
+ return undefined;
803
+ }
804
+ function commandResultFromRecord(value) {
805
+ const exitCode = typeof value.exitCode === 'number' ? value.exitCode : undefined;
806
+ const stdout = typeof value.stdout === 'string' ? value.stdout : undefined;
807
+ const stderr = typeof value.stderr === 'string'
808
+ ? value.stderr
809
+ : typeof value.error === 'string'
810
+ ? value.error
811
+ : exitCode !== undefined && typeof value.message === 'string'
812
+ ? value.message
813
+ : undefined;
814
+ if (exitCode === undefined && stdout === undefined && stderr === undefined) {
815
+ return undefined;
816
+ }
817
+ return {
818
+ exitCode: exitCode ?? 1,
819
+ stdout,
820
+ stderr,
821
+ };
822
+ }
823
+ function exitCodeFromE2BResult(value) {
824
+ if (!(0, shared_1.isRecord)(value)) {
825
+ return undefined;
826
+ }
827
+ return typeof value.exitCode === 'number' ? value.exitCode : undefined;
828
+ }
829
+ //# sourceMappingURL=sandbox.js.map