@mastra/vercel 1.3.0 → 1.4.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/CHANGELOG.md +22 -0
- package/dist/index.cjs +849 -837
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +845 -832
- package/dist/index.js.map +1 -1
- package/dist/microvm/process-manager.d.ts.map +1 -1
- package/package.json +9 -8
package/dist/index.cjs
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _mastra_core_workspace = require("@mastra/core/workspace");
|
|
3
|
+
let _vercel_sandbox = require("@vercel/sandbox");
|
|
4
|
+
//#region src/executor/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* Executor function source code deployed to Vercel as a serverless function.
|
|
7
|
+
*
|
|
8
|
+
* Returns a string because it's sent as inline file content via
|
|
9
|
+
* the Vercel Deployments API — no separate build step needed.
|
|
10
|
+
*
|
|
11
|
+
* The secret and user env vars are embedded as constants in the source
|
|
12
|
+
* because the Vercel Deployments API does not support setting env vars
|
|
13
|
+
* directly — they must be configured via vercel.json or project settings.
|
|
14
|
+
*/
|
|
9
15
|
function getExecutorSource(secret, env) {
|
|
10
|
-
|
|
11
|
-
|
|
16
|
+
const envEntries = Object.entries(env).map(([k, v]) => ` ${JSON.stringify(k)}: ${JSON.stringify(v)}`).join(",\n");
|
|
17
|
+
return `
|
|
12
18
|
const { execFileSync } = require('child_process');
|
|
13
19
|
|
|
14
20
|
const SANDBOX_SECRET = ${JSON.stringify(secret)};
|
|
@@ -39,7 +45,7 @@ module.exports = async (req, res) => {
|
|
|
39
45
|
// When args is empty the caller sent a full shell command string
|
|
40
46
|
// (e.g. "echo hello" or "ls -la | grep foo"). Run it through
|
|
41
47
|
// /bin/sh so builtins and pipes work. When args is non-empty the
|
|
42
|
-
// caller split the command properly
|
|
48
|
+
// caller split the command properly — use execFileSync to avoid
|
|
43
49
|
// shell injection.
|
|
44
50
|
const useShell = !args || args.length === 0;
|
|
45
51
|
const execCommand = useShell ? '/bin/sh' : command;
|
|
@@ -81,843 +87,849 @@ module.exports = async (req, res) => {
|
|
|
81
87
|
};
|
|
82
88
|
`;
|
|
83
89
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/serverless/index.ts
|
|
92
|
+
const LOG_PREFIX$1 = "[VercelServerlessSandbox]";
|
|
93
|
+
const VERCEL_API_BASE = "https://api.vercel.com";
|
|
94
|
+
/**
|
|
95
|
+
* Shell-quote an argument for safe interpolation into a shell command string.
|
|
96
|
+
* Mirrors the implementation in packages/core/src/workspace/sandbox/utils.ts
|
|
97
|
+
* which is not publicly exported.
|
|
98
|
+
*/
|
|
88
99
|
function shellQuote(arg) {
|
|
89
|
-
|
|
90
|
-
|
|
100
|
+
if (/^[a-zA-Z0-9._\-\/=:@]+$/.test(arg)) return arg;
|
|
101
|
+
return `'${arg.replace(/'/g, "'\\''")}'`;
|
|
91
102
|
}
|
|
92
|
-
var VercelServerlessSandbox = class extends
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
*/
|
|
369
|
-
async _acquireProtectionBypass(projectId) {
|
|
370
|
-
const projResp = await this._vercelFetch(`/v9/projects/${projectId}`);
|
|
371
|
-
if (projResp.ok) {
|
|
372
|
-
const project = await projResp.json();
|
|
373
|
-
const existing = project.protectionBypass ? Object.keys(project.protectionBypass)[0] : void 0;
|
|
374
|
-
if (existing) {
|
|
375
|
-
this.logger.debug(`${LOG_PREFIX} Using existing protection bypass token`);
|
|
376
|
-
return existing;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
this.logger.debug(`${LOG_PREFIX} Creating protection bypass token...`);
|
|
380
|
-
const createResp = await this._vercelFetch(`/v1/projects/${projectId}/protection-bypass`, {
|
|
381
|
-
method: "PATCH",
|
|
382
|
-
body: JSON.stringify({})
|
|
383
|
-
});
|
|
384
|
-
if (createResp.ok) {
|
|
385
|
-
const result = await createResp.json();
|
|
386
|
-
const created = result.protectionBypass ? Object.keys(result.protectionBypass)[0] : void 0;
|
|
387
|
-
if (created) {
|
|
388
|
-
this.logger.debug(`${LOG_PREFIX} Protection bypass token created`);
|
|
389
|
-
return created;
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
this.logger.debug(`${LOG_PREFIX} Could not acquire protection bypass token (project may not require it)`);
|
|
393
|
-
return null;
|
|
394
|
-
}
|
|
395
|
-
_executorHeaders() {
|
|
396
|
-
const headers = {
|
|
397
|
-
"Content-Type": "application/json",
|
|
398
|
-
Authorization: `Bearer ${this._secret}`
|
|
399
|
-
};
|
|
400
|
-
if (this._protectionBypass) {
|
|
401
|
-
headers["x-vercel-protection-bypass"] = this._protectionBypass;
|
|
402
|
-
}
|
|
403
|
-
return headers;
|
|
404
|
-
}
|
|
405
|
-
async _vercelFetch(path, options = {}) {
|
|
406
|
-
const url = new URL(path, VERCEL_API_BASE);
|
|
407
|
-
if (this._teamId) {
|
|
408
|
-
url.searchParams.set("teamId", this._teamId);
|
|
409
|
-
}
|
|
410
|
-
return fetch(url.toString(), {
|
|
411
|
-
...options,
|
|
412
|
-
headers: {
|
|
413
|
-
"Content-Type": "application/json",
|
|
414
|
-
Authorization: `Bearer ${this._token}`,
|
|
415
|
-
...options.headers ?? {}
|
|
416
|
-
}
|
|
417
|
-
});
|
|
418
|
-
}
|
|
103
|
+
var VercelServerlessSandbox = class extends _mastra_core_workspace.MastraSandbox {
|
|
104
|
+
id;
|
|
105
|
+
name = "VercelServerlessSandbox";
|
|
106
|
+
provider = "vercel-serverless";
|
|
107
|
+
status = "pending";
|
|
108
|
+
_token;
|
|
109
|
+
_teamId;
|
|
110
|
+
_projectName;
|
|
111
|
+
_regions;
|
|
112
|
+
_maxDuration;
|
|
113
|
+
_memory;
|
|
114
|
+
_env;
|
|
115
|
+
_commandTimeout;
|
|
116
|
+
_instructionsOverride;
|
|
117
|
+
_secret;
|
|
118
|
+
_deploymentUrl = null;
|
|
119
|
+
_deploymentId = null;
|
|
120
|
+
_protectionBypass = null;
|
|
121
|
+
_createdAt = null;
|
|
122
|
+
constructor(options = {}) {
|
|
123
|
+
super({ name: "VercelServerlessSandbox" });
|
|
124
|
+
this.id = `vercel-serverless-sandbox-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
|
|
125
|
+
this._token = options.token || process.env.VERCEL_TOKEN || "";
|
|
126
|
+
this._teamId = options.teamId;
|
|
127
|
+
this._projectName = options.projectName;
|
|
128
|
+
this._regions = options.regions ?? ["iad1"];
|
|
129
|
+
this._maxDuration = options.maxDuration ?? 60;
|
|
130
|
+
this._memory = options.memory ?? 1024;
|
|
131
|
+
this._env = options.env ?? {};
|
|
132
|
+
this._commandTimeout = options.commandTimeout ?? 55e3;
|
|
133
|
+
this._instructionsOverride = options.instructions;
|
|
134
|
+
this._secret = crypto.randomUUID();
|
|
135
|
+
}
|
|
136
|
+
async start() {
|
|
137
|
+
if (this._deploymentUrl) return;
|
|
138
|
+
if (!this._token) throw new Error(`${LOG_PREFIX$1} Missing Vercel API token. Set VERCEL_TOKEN env var or pass token option.`);
|
|
139
|
+
if (this._deploymentId) {
|
|
140
|
+
this.logger.debug(`${LOG_PREFIX$1} Cleaning up stale deployment ${this._deploymentId} before restarting...`);
|
|
141
|
+
try {
|
|
142
|
+
const resp = await this._vercelFetch(`/v13/deployments/${this._deploymentId}`, { method: "DELETE" });
|
|
143
|
+
if (!resp.ok && resp.status !== 404) this.logger.warn(`${LOG_PREFIX$1} Failed to delete stale deployment: ${resp.status}`);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
this.logger.warn(`${LOG_PREFIX$1} Error deleting stale deployment:`, error);
|
|
146
|
+
}
|
|
147
|
+
this._deploymentId = null;
|
|
148
|
+
}
|
|
149
|
+
this.logger.debug(`${LOG_PREFIX$1} Deploying executor function...`);
|
|
150
|
+
const vercelJson = JSON.stringify({
|
|
151
|
+
functions: { "api/execute.js": {
|
|
152
|
+
memory: this._memory,
|
|
153
|
+
maxDuration: this._maxDuration
|
|
154
|
+
} },
|
|
155
|
+
regions: this._regions
|
|
156
|
+
});
|
|
157
|
+
const deploymentBody = {
|
|
158
|
+
name: this._projectName ?? `mastra-sandbox-${this.id}`,
|
|
159
|
+
files: [{
|
|
160
|
+
file: "api/execute.js",
|
|
161
|
+
data: getExecutorSource(this._secret, this._env)
|
|
162
|
+
}, {
|
|
163
|
+
file: "vercel.json",
|
|
164
|
+
data: vercelJson
|
|
165
|
+
}],
|
|
166
|
+
projectSettings: { framework: null },
|
|
167
|
+
target: "production"
|
|
168
|
+
};
|
|
169
|
+
const createResp = await this._vercelFetch("/v13/deployments", {
|
|
170
|
+
method: "POST",
|
|
171
|
+
body: JSON.stringify(deploymentBody)
|
|
172
|
+
});
|
|
173
|
+
if (!createResp.ok) {
|
|
174
|
+
const errorBody = await createResp.text();
|
|
175
|
+
throw new Error(`${LOG_PREFIX$1} Failed to create deployment: ${createResp.status} ${errorBody}`);
|
|
176
|
+
}
|
|
177
|
+
const deployment = await createResp.json();
|
|
178
|
+
this._deploymentId = deployment.id;
|
|
179
|
+
this.logger.debug(`${LOG_PREFIX$1} Deployment created: ${deployment.id}, polling for READY...`);
|
|
180
|
+
const maxWaitMs = 12e4;
|
|
181
|
+
const pollIntervalMs = 3e3;
|
|
182
|
+
const deadline = Date.now() + maxWaitMs;
|
|
183
|
+
while (Date.now() < deadline) {
|
|
184
|
+
const statusResp = await this._vercelFetch(`/v13/deployments/${deployment.id}`);
|
|
185
|
+
if (!statusResp.ok) throw new Error(`${LOG_PREFIX$1} Failed to check deployment status: ${statusResp.status}`);
|
|
186
|
+
const statusBody = await statusResp.json();
|
|
187
|
+
if (statusBody.readyState === "READY") {
|
|
188
|
+
this._deploymentUrl = `https://${statusBody.url}`;
|
|
189
|
+
this._createdAt = /* @__PURE__ */ new Date();
|
|
190
|
+
this.logger.debug(`${LOG_PREFIX$1} Deployment ready: ${this._deploymentUrl}`);
|
|
191
|
+
if (deployment.projectId) try {
|
|
192
|
+
this._protectionBypass = await this._acquireProtectionBypass(deployment.projectId);
|
|
193
|
+
} catch {}
|
|
194
|
+
try {
|
|
195
|
+
await fetch(`${this._deploymentUrl}/api/execute`, {
|
|
196
|
+
method: "POST",
|
|
197
|
+
headers: this._executorHeaders(),
|
|
198
|
+
body: JSON.stringify({
|
|
199
|
+
command: "echo",
|
|
200
|
+
args: ["warm"]
|
|
201
|
+
})
|
|
202
|
+
});
|
|
203
|
+
} catch {}
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (statusBody.readyState === "ERROR" || statusBody.readyState === "CANCELED") throw new Error(`${LOG_PREFIX$1} Deployment failed with state: ${statusBody.readyState}`);
|
|
207
|
+
await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
|
|
208
|
+
}
|
|
209
|
+
throw new Error(`${LOG_PREFIX$1} Deployment timed out after ${maxWaitMs}ms`);
|
|
210
|
+
}
|
|
211
|
+
async stop() {
|
|
212
|
+
this._deploymentUrl = null;
|
|
213
|
+
}
|
|
214
|
+
async destroy() {
|
|
215
|
+
if (this._deploymentId) {
|
|
216
|
+
try {
|
|
217
|
+
const resp = await this._vercelFetch(`/v13/deployments/${this._deploymentId}`, { method: "DELETE" });
|
|
218
|
+
if (!resp.ok) if (resp.status === 404) this.logger.debug(`${LOG_PREFIX$1} Deployment already deleted (404)`);
|
|
219
|
+
else this.logger.warn(`${LOG_PREFIX$1} Failed to delete deployment: ${resp.status}`);
|
|
220
|
+
} catch (error) {
|
|
221
|
+
this.logger.warn(`${LOG_PREFIX$1} Error deleting deployment:`, error);
|
|
222
|
+
}
|
|
223
|
+
this._deploymentId = null;
|
|
224
|
+
}
|
|
225
|
+
this._deploymentUrl = null;
|
|
226
|
+
this._protectionBypass = null;
|
|
227
|
+
}
|
|
228
|
+
async executeCommand(command, args, options) {
|
|
229
|
+
await this.ensureRunning();
|
|
230
|
+
if (!this._deploymentUrl) throw new _mastra_core_workspace.SandboxNotReadyError(this.id);
|
|
231
|
+
const fullCommand = args?.length ? `${command} ${args.map((a) => shellQuote(a)).join(" ")}` : command;
|
|
232
|
+
this.logger.debug(`${LOG_PREFIX$1} Executing: ${fullCommand}`);
|
|
233
|
+
const body = {
|
|
234
|
+
command,
|
|
235
|
+
args: args ?? [],
|
|
236
|
+
env: options?.env ?? {},
|
|
237
|
+
cwd: options?.cwd ?? "/tmp",
|
|
238
|
+
timeout: options?.timeout ?? this._commandTimeout
|
|
239
|
+
};
|
|
240
|
+
const maxRetries = 2;
|
|
241
|
+
let lastError = null;
|
|
242
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) try {
|
|
243
|
+
const resp = await fetch(`${this._deploymentUrl}/api/execute`, {
|
|
244
|
+
method: "POST",
|
|
245
|
+
headers: this._executorHeaders(),
|
|
246
|
+
body: JSON.stringify(body),
|
|
247
|
+
signal: options?.abortSignal
|
|
248
|
+
});
|
|
249
|
+
if ((resp.status === 429 || resp.status === 502 || resp.status === 503) && attempt < maxRetries) {
|
|
250
|
+
this.logger.debug(`${LOG_PREFIX$1} Retryable status ${resp.status}, attempt ${attempt + 1}/${maxRetries}`);
|
|
251
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3 * (attempt + 1)));
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (resp.status === 504) return {
|
|
255
|
+
command: fullCommand,
|
|
256
|
+
args,
|
|
257
|
+
success: false,
|
|
258
|
+
exitCode: 124,
|
|
259
|
+
stdout: "",
|
|
260
|
+
stderr: "Function execution timed out (504 Gateway Timeout)",
|
|
261
|
+
executionTimeMs: options?.timeout ?? this._commandTimeout,
|
|
262
|
+
timedOut: true
|
|
263
|
+
};
|
|
264
|
+
if (!resp.ok) {
|
|
265
|
+
const errorText = await resp.text();
|
|
266
|
+
throw new Error(`${LOG_PREFIX$1} Execute failed: ${resp.status} ${errorText}`);
|
|
267
|
+
}
|
|
268
|
+
const result = await resp.json();
|
|
269
|
+
if (options?.onStdout && result.stdout) options.onStdout(result.stdout);
|
|
270
|
+
if (options?.onStderr && result.stderr) options.onStderr(result.stderr);
|
|
271
|
+
return {
|
|
272
|
+
command: fullCommand,
|
|
273
|
+
args,
|
|
274
|
+
success: result.success,
|
|
275
|
+
exitCode: result.exitCode,
|
|
276
|
+
stdout: result.stdout,
|
|
277
|
+
stderr: result.stderr,
|
|
278
|
+
executionTimeMs: result.executionTimeMs,
|
|
279
|
+
timedOut: result.timedOut
|
|
280
|
+
};
|
|
281
|
+
} catch (error) {
|
|
282
|
+
if (error instanceof DOMException && error.name === "AbortError") throw error;
|
|
283
|
+
lastError = error;
|
|
284
|
+
if (attempt < maxRetries) {
|
|
285
|
+
this.logger.debug(`${LOG_PREFIX$1} Request error, attempt ${attempt + 1}/${maxRetries}:`, error);
|
|
286
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3 * (attempt + 1)));
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
throw lastError ?? /* @__PURE__ */ new Error(`${LOG_PREFIX$1} executeCommand failed after retries`);
|
|
291
|
+
}
|
|
292
|
+
getInstructions(opts) {
|
|
293
|
+
if (this._instructionsOverride === void 0) return this._getDefaultInstructions();
|
|
294
|
+
if (typeof this._instructionsOverride === "string") return this._instructionsOverride;
|
|
295
|
+
const defaultInstructions = this._getDefaultInstructions();
|
|
296
|
+
return this._instructionsOverride({
|
|
297
|
+
defaultInstructions,
|
|
298
|
+
requestContext: opts?.requestContext
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
_getDefaultInstructions() {
|
|
302
|
+
return [
|
|
303
|
+
"Vercel serverless sandbox.",
|
|
304
|
+
"Limitations:",
|
|
305
|
+
"- Stateless: no persistent filesystem between invocations.",
|
|
306
|
+
"- No interactive shell or streaming stdin.",
|
|
307
|
+
"- No long-running or background processes.",
|
|
308
|
+
`- Maximum execution time: ${this._maxDuration} seconds.`,
|
|
309
|
+
"- Only /tmp is writable (ephemeral, cleared between invocations).",
|
|
310
|
+
"- Shell commands (pipes, builtins) are supported via /bin/sh -c."
|
|
311
|
+
].join("\n");
|
|
312
|
+
}
|
|
313
|
+
async getInfo() {
|
|
314
|
+
return {
|
|
315
|
+
id: this.id,
|
|
316
|
+
name: this.name,
|
|
317
|
+
provider: this.provider,
|
|
318
|
+
status: this.status,
|
|
319
|
+
createdAt: this._createdAt ?? /* @__PURE__ */ new Date(),
|
|
320
|
+
metadata: {
|
|
321
|
+
deploymentId: this._deploymentId,
|
|
322
|
+
deploymentUrl: this._deploymentUrl,
|
|
323
|
+
regions: this._regions,
|
|
324
|
+
maxDuration: this._maxDuration,
|
|
325
|
+
memory: this._memory
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Fetch an existing protection bypass token for the project, or create one
|
|
331
|
+
* if none exists. Returns the token string, or null if acquisition fails.
|
|
332
|
+
*/
|
|
333
|
+
async _acquireProtectionBypass(projectId) {
|
|
334
|
+
const projResp = await this._vercelFetch(`/v9/projects/${projectId}`);
|
|
335
|
+
if (projResp.ok) {
|
|
336
|
+
const project = await projResp.json();
|
|
337
|
+
const existing = project.protectionBypass ? Object.keys(project.protectionBypass)[0] : void 0;
|
|
338
|
+
if (existing) {
|
|
339
|
+
this.logger.debug(`${LOG_PREFIX$1} Using existing protection bypass token`);
|
|
340
|
+
return existing;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
this.logger.debug(`${LOG_PREFIX$1} Creating protection bypass token...`);
|
|
344
|
+
const createResp = await this._vercelFetch(`/v1/projects/${projectId}/protection-bypass`, {
|
|
345
|
+
method: "PATCH",
|
|
346
|
+
body: JSON.stringify({})
|
|
347
|
+
});
|
|
348
|
+
if (createResp.ok) {
|
|
349
|
+
const result = await createResp.json();
|
|
350
|
+
const created = result.protectionBypass ? Object.keys(result.protectionBypass)[0] : void 0;
|
|
351
|
+
if (created) {
|
|
352
|
+
this.logger.debug(`${LOG_PREFIX$1} Protection bypass token created`);
|
|
353
|
+
return created;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
this.logger.debug(`${LOG_PREFIX$1} Could not acquire protection bypass token (project may not require it)`);
|
|
357
|
+
return null;
|
|
358
|
+
}
|
|
359
|
+
_executorHeaders() {
|
|
360
|
+
const headers = {
|
|
361
|
+
"Content-Type": "application/json",
|
|
362
|
+
Authorization: `Bearer ${this._secret}`
|
|
363
|
+
};
|
|
364
|
+
if (this._protectionBypass) headers["x-vercel-protection-bypass"] = this._protectionBypass;
|
|
365
|
+
return headers;
|
|
366
|
+
}
|
|
367
|
+
async _vercelFetch(path, options = {}) {
|
|
368
|
+
const url = new URL(path, VERCEL_API_BASE);
|
|
369
|
+
if (this._teamId) url.searchParams.set("teamId", this._teamId);
|
|
370
|
+
return fetch(url.toString(), {
|
|
371
|
+
...options,
|
|
372
|
+
headers: {
|
|
373
|
+
"Content-Type": "application/json",
|
|
374
|
+
Authorization: `Bearer ${this._token}`,
|
|
375
|
+
...options.headers ?? {}
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
}
|
|
419
379
|
};
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
380
|
+
//#endregion
|
|
381
|
+
//#region src/serverless-provider.ts
|
|
382
|
+
const vercelServerlessSandboxProvider = {
|
|
383
|
+
id: "vercel-serverless",
|
|
384
|
+
name: "Vercel Sandbox (Serverless)",
|
|
385
|
+
description: "Serverless sandbox powered by Vercel Functions",
|
|
386
|
+
configSchema: {
|
|
387
|
+
type: "object",
|
|
388
|
+
properties: {
|
|
389
|
+
token: {
|
|
390
|
+
type: "string",
|
|
391
|
+
description: "Vercel API token"
|
|
392
|
+
},
|
|
393
|
+
teamId: {
|
|
394
|
+
type: "string",
|
|
395
|
+
description: "Vercel team ID"
|
|
396
|
+
},
|
|
397
|
+
projectName: {
|
|
398
|
+
type: "string",
|
|
399
|
+
description: "Existing Vercel project name"
|
|
400
|
+
},
|
|
401
|
+
regions: {
|
|
402
|
+
type: "array",
|
|
403
|
+
description: "Deployment regions",
|
|
404
|
+
items: { type: "string" },
|
|
405
|
+
default: ["iad1"]
|
|
406
|
+
},
|
|
407
|
+
maxDuration: {
|
|
408
|
+
type: "number",
|
|
409
|
+
description: "Function max duration in seconds",
|
|
410
|
+
default: 60
|
|
411
|
+
},
|
|
412
|
+
memory: {
|
|
413
|
+
type: "number",
|
|
414
|
+
description: "Function memory in MB",
|
|
415
|
+
default: 1024
|
|
416
|
+
},
|
|
417
|
+
env: {
|
|
418
|
+
type: "object",
|
|
419
|
+
description: "Environment variables",
|
|
420
|
+
additionalProperties: { type: "string" }
|
|
421
|
+
},
|
|
422
|
+
commandTimeout: {
|
|
423
|
+
type: "number",
|
|
424
|
+
description: "Per-invocation timeout in ms",
|
|
425
|
+
default: 55e3
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
},
|
|
429
|
+
createSandbox: (config) => new VercelServerlessSandbox(config)
|
|
449
430
|
};
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
431
|
+
//#endregion
|
|
432
|
+
//#region src/microvm/process-manager.ts
|
|
433
|
+
/**
|
|
434
|
+
* Vercel Sandbox (MicroVM) Process Manager
|
|
435
|
+
*
|
|
436
|
+
* Implements SandboxProcessManager for the `@vercel/sandbox` SDK. Each spawn()
|
|
437
|
+
* starts a detached command via `sandbox.runCommand({ detached: true })` and
|
|
438
|
+
* streams its output through the command's async `logs()` iterator.
|
|
439
|
+
*
|
|
440
|
+
* The Vercel Sandbox SDK does not expose a stdin channel for running commands,
|
|
441
|
+
* so `sendStdin()` throws a clear "not supported" error.
|
|
442
|
+
*/
|
|
443
|
+
/**
|
|
444
|
+
* Wraps a detached Vercel Sandbox {@link Command} to conform to Mastra's
|
|
445
|
+
* ProcessHandle. Not exported — internal to this module.
|
|
446
|
+
*/
|
|
447
|
+
var VercelSandboxProcessHandle = class extends _mastra_core_workspace.ProcessHandle {
|
|
448
|
+
pid;
|
|
449
|
+
_command;
|
|
450
|
+
_startTime;
|
|
451
|
+
_timeout;
|
|
452
|
+
_exitCode;
|
|
453
|
+
_waitPromise = null;
|
|
454
|
+
_streamingPromise = null;
|
|
455
|
+
_killed = false;
|
|
456
|
+
constructor(command, startTime, options) {
|
|
457
|
+
super(options);
|
|
458
|
+
this.pid = command.cmdId;
|
|
459
|
+
this._command = command;
|
|
460
|
+
this._startTime = startTime;
|
|
461
|
+
this._timeout = options?.timeout;
|
|
462
|
+
}
|
|
463
|
+
get exitCode() {
|
|
464
|
+
return this._exitCode;
|
|
465
|
+
}
|
|
466
|
+
/** @internal Set by the process manager after streaming starts. */
|
|
467
|
+
set streamingPromise(p) {
|
|
468
|
+
this._streamingPromise = p;
|
|
469
|
+
}
|
|
470
|
+
async wait() {
|
|
471
|
+
if (!this._waitPromise) this._waitPromise = this._doWait();
|
|
472
|
+
return this._waitPromise;
|
|
473
|
+
}
|
|
474
|
+
async _doWait() {
|
|
475
|
+
const finishedPromise = this._command.wait().then((finished) => {
|
|
476
|
+
if (this._exitCode === void 0) this._exitCode = finished.exitCode;
|
|
477
|
+
}).catch(() => {
|
|
478
|
+
if (this._exitCode === void 0) this._exitCode = 1;
|
|
479
|
+
});
|
|
480
|
+
if (this._timeout) {
|
|
481
|
+
let timeoutId;
|
|
482
|
+
const timeoutPromise = new Promise((resolve) => {
|
|
483
|
+
timeoutId = setTimeout(() => resolve("timeout"), this._timeout);
|
|
484
|
+
});
|
|
485
|
+
const outcome = await Promise.race([finishedPromise.then(() => "done"), timeoutPromise]);
|
|
486
|
+
clearTimeout(timeoutId);
|
|
487
|
+
if (outcome === "timeout") {
|
|
488
|
+
await this.kill();
|
|
489
|
+
this._exitCode = 124;
|
|
490
|
+
await this._streamingPromise?.catch(() => {});
|
|
491
|
+
return {
|
|
492
|
+
success: false,
|
|
493
|
+
exitCode: 124,
|
|
494
|
+
stdout: this.stdout,
|
|
495
|
+
stderr: this.stderr || `Command timed out after ${this._timeout}ms`,
|
|
496
|
+
executionTimeMs: Date.now() - this._startTime,
|
|
497
|
+
killed: true,
|
|
498
|
+
timedOut: true
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
} else await finishedPromise;
|
|
502
|
+
await this._streamingPromise?.catch(() => {});
|
|
503
|
+
if (this._killed) return {
|
|
504
|
+
success: false,
|
|
505
|
+
exitCode: this._exitCode ?? 137,
|
|
506
|
+
stdout: this.stdout,
|
|
507
|
+
stderr: this.stderr,
|
|
508
|
+
executionTimeMs: Date.now() - this._startTime,
|
|
509
|
+
killed: true,
|
|
510
|
+
timedOut: false
|
|
511
|
+
};
|
|
512
|
+
return {
|
|
513
|
+
success: this._exitCode === 0,
|
|
514
|
+
exitCode: this._exitCode ?? 1,
|
|
515
|
+
stdout: this.stdout,
|
|
516
|
+
stderr: this.stderr,
|
|
517
|
+
executionTimeMs: Date.now() - this._startTime
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
async kill() {
|
|
521
|
+
if (this._exitCode !== void 0 && !this._killed) return false;
|
|
522
|
+
this._killed = true;
|
|
523
|
+
if (this._exitCode === void 0) this._exitCode = 137;
|
|
524
|
+
try {
|
|
525
|
+
await this._command.kill();
|
|
526
|
+
} catch {}
|
|
527
|
+
return true;
|
|
528
|
+
}
|
|
529
|
+
async sendStdin(_data) {
|
|
530
|
+
throw new Error("VercelSandbox does not support sending stdin to running processes.");
|
|
531
|
+
}
|
|
532
|
+
async closeStdin() {
|
|
533
|
+
throw new _mastra_core_workspace.UnsupportedStdinCloseError("VercelSandbox does not support closing stdin for running processes.");
|
|
534
|
+
}
|
|
544
535
|
};
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
}
|
|
536
|
+
/**
|
|
537
|
+
* Vercel Sandbox implementation of SandboxProcessManager. Uses one detached
|
|
538
|
+
* `runCommand` per spawned process.
|
|
539
|
+
*/
|
|
540
|
+
var VercelSandboxProcessManager = class extends _mastra_core_workspace.SandboxProcessManager {
|
|
541
|
+
async spawn(command, options = {}) {
|
|
542
|
+
const mergedEnv = {
|
|
543
|
+
...this.env,
|
|
544
|
+
...options.env
|
|
545
|
+
};
|
|
546
|
+
const env = Object.fromEntries(Object.entries(mergedEnv).filter((entry) => entry[1] !== void 0));
|
|
547
|
+
const cmd = await this.sandbox.sandbox.runCommand({
|
|
548
|
+
cmd: "sh",
|
|
549
|
+
args: ["-c", command],
|
|
550
|
+
...options.cwd ? { cwd: options.cwd } : {},
|
|
551
|
+
...Object.keys(env).length ? { env } : {},
|
|
552
|
+
detached: true
|
|
553
|
+
});
|
|
554
|
+
const handle = new VercelSandboxProcessHandle(cmd, Date.now(), options);
|
|
555
|
+
handle.streamingPromise = (async () => {
|
|
556
|
+
for await (const log of cmd.logs()) if (log.stream === "stdout") handle.emitStdout(log.data);
|
|
557
|
+
else handle.emitStderr(log.data);
|
|
558
|
+
})().catch(() => {});
|
|
559
|
+
this._tracked.set(handle.pid, handle);
|
|
560
|
+
return handle;
|
|
561
|
+
}
|
|
562
|
+
async list() {
|
|
563
|
+
const result = [];
|
|
564
|
+
for (const [pid, handle] of this._tracked) result.push({
|
|
565
|
+
pid,
|
|
566
|
+
command: handle.command,
|
|
567
|
+
running: handle.exitCode === void 0,
|
|
568
|
+
exitCode: handle.exitCode
|
|
569
|
+
});
|
|
570
|
+
return result;
|
|
571
|
+
}
|
|
582
572
|
};
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
573
|
+
//#endregion
|
|
574
|
+
//#region src/microvm/index.ts
|
|
575
|
+
const LOG_PREFIX = "[VercelSandbox]";
|
|
576
|
+
/**
|
|
577
|
+
* Vercel Sandbox (MicroVM) provider for Mastra workspaces.
|
|
578
|
+
*
|
|
579
|
+
* @example Basic usage
|
|
580
|
+
* ```typescript
|
|
581
|
+
* import { Workspace } from '@mastra/core/workspace';
|
|
582
|
+
* import { VercelSandbox } from '@mastra/vercel';
|
|
583
|
+
*
|
|
584
|
+
* const workspace = new Workspace({
|
|
585
|
+
* sandbox: new VercelSandbox({ runtime: 'node24', timeout: 600_000 }),
|
|
586
|
+
* });
|
|
587
|
+
*
|
|
588
|
+
* const result = await workspace.sandbox.executeCommand('node', ['--version']);
|
|
589
|
+
* ```
|
|
590
|
+
*/
|
|
591
|
+
var VercelSandbox = class VercelSandbox extends _mastra_core_workspace.MastraSandbox {
|
|
592
|
+
id;
|
|
593
|
+
name = "VercelSandbox";
|
|
594
|
+
provider = "vercel-sandbox";
|
|
595
|
+
status = "pending";
|
|
596
|
+
/**
|
|
597
|
+
* Networking capability: public HTTPS URLs for exposed ports.
|
|
598
|
+
* A port must be declared in `ports` at construction time for a URL to exist.
|
|
599
|
+
*/
|
|
600
|
+
networking = { getPortUrl: async (port) => {
|
|
601
|
+
const sandbox = this._sandbox ?? await this.attach();
|
|
602
|
+
if (!sandbox) return null;
|
|
603
|
+
try {
|
|
604
|
+
return sandbox.domain(port);
|
|
605
|
+
} catch {
|
|
606
|
+
return null;
|
|
607
|
+
}
|
|
608
|
+
} };
|
|
609
|
+
_sandbox = null;
|
|
610
|
+
/** True when `_sandbox` came from a resume-less lookup — `start()` must still resume it. */
|
|
611
|
+
_attachedWithoutResume = false;
|
|
612
|
+
_createdAt = null;
|
|
613
|
+
_sandboxName;
|
|
614
|
+
_token;
|
|
615
|
+
_teamId;
|
|
616
|
+
_projectId;
|
|
617
|
+
_runtime;
|
|
618
|
+
_timeout;
|
|
619
|
+
_vcpus;
|
|
620
|
+
_ports;
|
|
621
|
+
_env;
|
|
622
|
+
_metadata;
|
|
623
|
+
_instructionsOverride;
|
|
624
|
+
_constructorOptions;
|
|
625
|
+
constructor(options = {}) {
|
|
626
|
+
super({
|
|
627
|
+
...options,
|
|
628
|
+
name: "VercelSandbox",
|
|
629
|
+
processes: new VercelSandboxProcessManager({ env: options.env ?? {} })
|
|
630
|
+
});
|
|
631
|
+
this.id = options.id ?? `vercel-sandbox-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
|
|
632
|
+
this._sandboxName = options.sandboxName;
|
|
633
|
+
this._token = options.token ?? process.env.VERCEL_TOKEN;
|
|
634
|
+
this._teamId = options.teamId ?? process.env.VERCEL_TEAM_ID;
|
|
635
|
+
this._projectId = options.projectId ?? process.env.VERCEL_PROJECT_ID;
|
|
636
|
+
this._runtime = options.runtime ?? "node24";
|
|
637
|
+
this._timeout = options.timeout ?? 3e5;
|
|
638
|
+
this._vcpus = options.resources?.vcpus;
|
|
639
|
+
this._ports = options.ports;
|
|
640
|
+
this._env = options.env ?? {};
|
|
641
|
+
this._metadata = options.metadata ?? {};
|
|
642
|
+
this._instructionsOverride = options.instructions;
|
|
643
|
+
this._constructorOptions = { ...options };
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* Construct a sibling `VercelSandbox` that inherits this sandbox's
|
|
647
|
+
* configuration (credentials, runtime, resources, ports, metadata,
|
|
648
|
+
* instructions) with per-instance overrides.
|
|
649
|
+
*
|
|
650
|
+
* Performs no I/O — the sandbox clone provisions a fresh MicroVM on its
|
|
651
|
+
* own `start()` (Vercel sandboxes cannot be reconnected to, so
|
|
652
|
+
* `options.sandboxId` is ignored). Use it when one configured sandbox acts
|
|
653
|
+
* as the template for a fleet of independent sandboxes (e.g. one per
|
|
654
|
+
* project).
|
|
655
|
+
*
|
|
656
|
+
* `options.idleTimeoutMinutes` maps to the Vercel sandbox `timeout` (ms).
|
|
657
|
+
*/
|
|
658
|
+
clone(options = {}) {
|
|
659
|
+
const { id: _id, sandboxName: _sandboxName, ...base } = this._constructorOptions;
|
|
660
|
+
return new VercelSandbox({
|
|
661
|
+
...base,
|
|
662
|
+
...options.id !== void 0 && { id: options.id },
|
|
663
|
+
...options.env !== void 0 && { env: options.env },
|
|
664
|
+
...options.idleTimeoutMinutes !== void 0 && { timeout: options.idleTimeoutMinutes * 6e4 }
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* The underlying `@vercel/sandbox` instance.
|
|
669
|
+
* Throws if the sandbox has not been started yet.
|
|
670
|
+
*/
|
|
671
|
+
get sandbox() {
|
|
672
|
+
if (!this._sandbox) throw new _mastra_core_workspace.SandboxNotReadyError(this.id);
|
|
673
|
+
return this._sandbox;
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* Attach to an existing named sandbox WITHOUT resuming it (`resume: false`),
|
|
677
|
+
* so lookups (URL resolution, stop, destroy) never wake a stopped sandbox
|
|
678
|
+
* and start billing. Returns null when unnamed or when no sandbox with this
|
|
679
|
+
* name exists.
|
|
680
|
+
*/
|
|
681
|
+
async attach() {
|
|
682
|
+
if (this._sandbox) return this._sandbox;
|
|
683
|
+
if (!this._sandboxName) return null;
|
|
684
|
+
try {
|
|
685
|
+
this._sandbox = await _vercel_sandbox.Sandbox.get({
|
|
686
|
+
name: this._sandboxName,
|
|
687
|
+
resume: false,
|
|
688
|
+
...this.credentialParams()
|
|
689
|
+
});
|
|
690
|
+
this._attachedWithoutResume = true;
|
|
691
|
+
return this._sandbox;
|
|
692
|
+
} catch {
|
|
693
|
+
return null;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* The token is the credential: when provided, the SDK requires teamId and
|
|
698
|
+
* projectId alongside it. Without a token the SDK authenticates via the
|
|
699
|
+
* `VERCEL_OIDC_TOKEN`; a stray teamId/projectId (for example, an exported
|
|
700
|
+
* `VERCEL_TEAM_ID` in the shell) must not break OIDC auth, so it is ignored.
|
|
701
|
+
*/
|
|
702
|
+
credentialParams() {
|
|
703
|
+
if (!this._token) return {};
|
|
704
|
+
if (!(this._teamId && this._projectId)) throw new Error(`${LOG_PREFIX} Incomplete credentials. Provide token, teamId, and projectId together (or the VERCEL_TOKEN, VERCEL_TEAM_ID, and VERCEL_PROJECT_ID env vars), or omit the token to use the VERCEL_OIDC_TOKEN.`);
|
|
705
|
+
return {
|
|
706
|
+
token: this._token,
|
|
707
|
+
teamId: this._teamId,
|
|
708
|
+
projectId: this._projectId
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
async start() {
|
|
712
|
+
if (this._sandbox && !this._attachedWithoutResume) return;
|
|
713
|
+
this.logger.debug(`${LOG_PREFIX} Creating sandbox...`, {
|
|
714
|
+
runtime: this._runtime,
|
|
715
|
+
timeout: this._timeout
|
|
716
|
+
});
|
|
717
|
+
const params = {
|
|
718
|
+
runtime: this._runtime,
|
|
719
|
+
timeout: this._timeout,
|
|
720
|
+
...this._vcpus ? { resources: { vcpus: this._vcpus } } : {},
|
|
721
|
+
...this._ports?.length ? { ports: this._ports } : {},
|
|
722
|
+
...Object.keys(this._env).length ? { env: this._env } : {},
|
|
723
|
+
...this.credentialParams()
|
|
724
|
+
};
|
|
725
|
+
this._sandbox = this._sandboxName ? await _vercel_sandbox.Sandbox.getOrCreate({
|
|
726
|
+
...params,
|
|
727
|
+
name: this._sandboxName
|
|
728
|
+
}) : await _vercel_sandbox.Sandbox.create(params);
|
|
729
|
+
this._attachedWithoutResume = false;
|
|
730
|
+
this._createdAt = /* @__PURE__ */ new Date();
|
|
731
|
+
this.logger.debug(`${LOG_PREFIX} Sandbox ready: ${this._sandbox.name}`);
|
|
732
|
+
}
|
|
733
|
+
/** Snapshot-stop: the filesystem persists and a named sandbox resumes on the next `start()`. */
|
|
734
|
+
async stop() {
|
|
735
|
+
const sandbox = this._sandbox ?? await this.attach();
|
|
736
|
+
if (!sandbox) return;
|
|
737
|
+
await sandbox.stop();
|
|
738
|
+
this._sandbox = null;
|
|
739
|
+
this._attachedWithoutResume = false;
|
|
740
|
+
}
|
|
741
|
+
/** Permanently delete the sandbox and its snapshots. */
|
|
742
|
+
async destroy() {
|
|
743
|
+
const sandbox = this._sandbox ?? await this.attach();
|
|
744
|
+
if (!sandbox) return;
|
|
745
|
+
await sandbox.delete();
|
|
746
|
+
this._sandbox = null;
|
|
747
|
+
this._attachedWithoutResume = false;
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
750
|
+
* Bulk-write files into the sandbox filesystem via the SDK's native upload.
|
|
751
|
+
* Relative paths resolve against /vercel/sandbox.
|
|
752
|
+
*/
|
|
753
|
+
async writeFiles(files) {
|
|
754
|
+
await this.ensureRunning();
|
|
755
|
+
await this.sandbox.writeFiles(files.map((f) => ({
|
|
756
|
+
path: f.path,
|
|
757
|
+
content: f.content
|
|
758
|
+
})));
|
|
759
|
+
}
|
|
760
|
+
async executeCommand(command, args, options) {
|
|
761
|
+
await this.ensureRunning();
|
|
762
|
+
const startTime = Date.now();
|
|
763
|
+
const fullCommand = args?.length ? `${command} ${args.join(" ")}` : command;
|
|
764
|
+
this.logger.debug(`${LOG_PREFIX} Executing: ${fullCommand}`, { cwd: options?.cwd });
|
|
765
|
+
const mergedEnv = {
|
|
766
|
+
...this._env,
|
|
767
|
+
...options?.env
|
|
768
|
+
};
|
|
769
|
+
const env = Object.fromEntries(Object.entries(mergedEnv).filter((entry) => entry[1] !== void 0));
|
|
770
|
+
let timeoutId;
|
|
771
|
+
const abortController = new AbortController();
|
|
772
|
+
const forwardAbort = () => abortController.abort();
|
|
773
|
+
if (options?.abortSignal) if (options.abortSignal.aborted) abortController.abort();
|
|
774
|
+
else options.abortSignal.addEventListener("abort", forwardAbort, { once: true });
|
|
775
|
+
const signal = abortController.signal;
|
|
776
|
+
const timeoutPromise = options?.timeout ? new Promise((resolve) => {
|
|
777
|
+
timeoutId = setTimeout(() => resolve("timeout"), options.timeout);
|
|
778
|
+
}) : null;
|
|
779
|
+
try {
|
|
780
|
+
const commandPromise = this.sandbox.runCommand({
|
|
781
|
+
cmd: command,
|
|
782
|
+
args: args ?? [],
|
|
783
|
+
...options?.cwd ? { cwd: options.cwd } : {},
|
|
784
|
+
...Object.keys(env).length ? { env } : {},
|
|
785
|
+
signal
|
|
786
|
+
});
|
|
787
|
+
const finished = timeoutPromise ? await Promise.race([commandPromise, timeoutPromise]) : await commandPromise;
|
|
788
|
+
if (finished === "timeout") {
|
|
789
|
+
abortController.abort();
|
|
790
|
+
return {
|
|
791
|
+
command: fullCommand,
|
|
792
|
+
args,
|
|
793
|
+
success: false,
|
|
794
|
+
exitCode: 124,
|
|
795
|
+
stdout: "",
|
|
796
|
+
stderr: `Command timed out after ${options.timeout}ms`,
|
|
797
|
+
executionTimeMs: Date.now() - startTime,
|
|
798
|
+
timedOut: true
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
const [stdout, stderr] = await Promise.all([finished.stdout(), finished.stderr()]);
|
|
802
|
+
if (options?.onStdout && stdout) options.onStdout(stdout);
|
|
803
|
+
if (options?.onStderr && stderr) options.onStderr(stderr);
|
|
804
|
+
return {
|
|
805
|
+
command: fullCommand,
|
|
806
|
+
args,
|
|
807
|
+
success: finished.exitCode === 0,
|
|
808
|
+
exitCode: finished.exitCode,
|
|
809
|
+
stdout,
|
|
810
|
+
stderr,
|
|
811
|
+
executionTimeMs: Date.now() - startTime
|
|
812
|
+
};
|
|
813
|
+
} finally {
|
|
814
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
815
|
+
options?.abortSignal?.removeEventListener("abort", forwardAbort);
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
getInfo() {
|
|
819
|
+
const domains = {};
|
|
820
|
+
if (this._sandbox && this._ports?.length) for (const port of this._ports) try {
|
|
821
|
+
domains[port] = this._sandbox.domain(port);
|
|
822
|
+
} catch {}
|
|
823
|
+
return {
|
|
824
|
+
id: this.id,
|
|
825
|
+
name: this.name,
|
|
826
|
+
provider: this.provider,
|
|
827
|
+
status: this.status,
|
|
828
|
+
createdAt: this._createdAt ?? /* @__PURE__ */ new Date(),
|
|
829
|
+
metadata: {
|
|
830
|
+
...this._metadata,
|
|
831
|
+
sandboxName: this._sandbox?.name,
|
|
832
|
+
runtime: this._runtime,
|
|
833
|
+
timeout: this._timeout,
|
|
834
|
+
...this._vcpus ? { vcpus: this._vcpus } : {},
|
|
835
|
+
...this._ports?.length ? {
|
|
836
|
+
ports: this._ports,
|
|
837
|
+
domains
|
|
838
|
+
} : {}
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
getInstructions(opts) {
|
|
843
|
+
if (this._instructionsOverride === void 0) return this._getDefaultInstructions();
|
|
844
|
+
if (typeof this._instructionsOverride === "string") return this._instructionsOverride;
|
|
845
|
+
const defaultInstructions = this._getDefaultInstructions();
|
|
846
|
+
return this._instructionsOverride({
|
|
847
|
+
defaultInstructions,
|
|
848
|
+
requestContext: opts?.requestContext
|
|
849
|
+
});
|
|
850
|
+
}
|
|
851
|
+
_getDefaultInstructions() {
|
|
852
|
+
return [
|
|
853
|
+
"Vercel Sandbox: an ephemeral Firecracker MicroVM running Amazon Linux 2023.",
|
|
854
|
+
`- Runtime: ${this._runtime}. Working directory defaults to /vercel/sandbox.`,
|
|
855
|
+
this._sandboxName ? "- Persistent filesystem: stopping snapshots the filesystem and the named sandbox resumes it on the next start. Processes do not survive a stop." : "- Persistent filesystem within the session; state is lost when the sandbox stops.",
|
|
856
|
+
"- Runs as the vercel-sandbox user with sudo access (install packages via dnf).",
|
|
857
|
+
`- The sandbox auto-terminates after ${Math.round(this._timeout / 1e3)} seconds.`,
|
|
858
|
+
...this._ports?.length ? [`- Exposed ports: ${this._ports.join(", ")} (reachable via public HTTPS domains).`] : [],
|
|
859
|
+
"- Background/long-running processes are supported via the process tools.",
|
|
860
|
+
"- Filesystem mounting (FUSE) is not supported."
|
|
861
|
+
].join("\n");
|
|
862
|
+
}
|
|
872
863
|
};
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
864
|
+
//#endregion
|
|
865
|
+
//#region src/microvm-provider.ts
|
|
866
|
+
const vercelSandboxProvider = {
|
|
867
|
+
id: "vercel-sandbox",
|
|
868
|
+
name: "Vercel Sandbox (MicroVM)",
|
|
869
|
+
description: "Ephemeral Firecracker MicroVM sandbox powered by Vercel Sandbox",
|
|
870
|
+
configSchema: {
|
|
871
|
+
type: "object",
|
|
872
|
+
properties: {
|
|
873
|
+
token: {
|
|
874
|
+
type: "string",
|
|
875
|
+
description: "Vercel API token (falls back to VERCEL_TOKEN; omit to use OIDC)"
|
|
876
|
+
},
|
|
877
|
+
teamId: {
|
|
878
|
+
type: "string",
|
|
879
|
+
description: "Vercel team ID (falls back to VERCEL_TEAM_ID)"
|
|
880
|
+
},
|
|
881
|
+
projectId: {
|
|
882
|
+
type: "string",
|
|
883
|
+
description: "Vercel project ID (falls back to VERCEL_PROJECT_ID)"
|
|
884
|
+
},
|
|
885
|
+
runtime: {
|
|
886
|
+
type: "string",
|
|
887
|
+
description: "Sandbox runtime",
|
|
888
|
+
enum: [
|
|
889
|
+
"node24",
|
|
890
|
+
"node22",
|
|
891
|
+
"node26",
|
|
892
|
+
"python3.13"
|
|
893
|
+
],
|
|
894
|
+
default: "node24"
|
|
895
|
+
},
|
|
896
|
+
timeout: {
|
|
897
|
+
type: "number",
|
|
898
|
+
description: "Auto-terminate timeout in milliseconds",
|
|
899
|
+
default: 3e5
|
|
900
|
+
},
|
|
901
|
+
vcpus: {
|
|
902
|
+
type: "number",
|
|
903
|
+
description: "Number of vCPUs (2048 MB memory per vCPU)"
|
|
904
|
+
},
|
|
905
|
+
ports: {
|
|
906
|
+
type: "array",
|
|
907
|
+
description: "Ports to expose (up to 4)",
|
|
908
|
+
items: { type: "number" }
|
|
909
|
+
},
|
|
910
|
+
env: {
|
|
911
|
+
type: "object",
|
|
912
|
+
description: "Environment variables",
|
|
913
|
+
additionalProperties: { type: "string" }
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
},
|
|
917
|
+
createSandbox: (config) => new VercelSandbox({
|
|
918
|
+
token: config.token,
|
|
919
|
+
teamId: config.teamId,
|
|
920
|
+
projectId: config.projectId,
|
|
921
|
+
runtime: config.runtime,
|
|
922
|
+
timeout: config.timeout,
|
|
923
|
+
...config.vcpus ? { resources: { vcpus: config.vcpus } } : {},
|
|
924
|
+
ports: config.ports,
|
|
925
|
+
env: config.env
|
|
926
|
+
})
|
|
915
927
|
};
|
|
916
|
-
|
|
928
|
+
//#endregion
|
|
917
929
|
exports.VercelSandbox = VercelSandbox;
|
|
918
930
|
exports.VercelSandboxProcessManager = VercelSandboxProcessManager;
|
|
919
931
|
exports.VercelServerlessSandbox = VercelServerlessSandbox;
|
|
920
932
|
exports.vercelSandboxProvider = vercelSandboxProvider;
|
|
921
933
|
exports.vercelServerlessSandboxProvider = vercelServerlessSandboxProvider;
|
|
922
|
-
|
|
934
|
+
|
|
923
935
|
//# sourceMappingURL=index.cjs.map
|