@mastra/docker 0.14.0-alpha.0 → 0.14.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +238 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +239 -3
- package/dist/index.js.map +1 -1
- package/dist/sandboxes/index.d.ts +45 -0
- package/dist/sandboxes/index.d.ts.map +1 -0
- package/package.json +3 -2
package/dist/index.cjs
CHANGED
|
@@ -27,10 +27,11 @@ let dockerode = require("dockerode");
|
|
|
27
27
|
dockerode = __toESM(dockerode, 1);
|
|
28
28
|
let tar_stream = require("tar-stream");
|
|
29
29
|
let string_decoder = require("string_decoder");
|
|
30
|
+
let crypto = require("crypto");
|
|
31
|
+
let _docker_sandboxes = require("@docker/sandboxes");
|
|
30
32
|
let path_posix = require("path/posix");
|
|
31
33
|
path_posix = __toESM(path_posix, 1);
|
|
32
34
|
let _grpc_grpc_js = require("@grpc/grpc-js");
|
|
33
|
-
let crypto = require("crypto");
|
|
34
35
|
let child_process = require("child_process");
|
|
35
36
|
// src/abort.ts
|
|
36
37
|
function createAbortError(signal, operation) {
|
|
@@ -1167,6 +1168,241 @@ function toDockerSandboxOptionName(field) {
|
|
|
1167
1168
|
Mounts: "mounts"
|
|
1168
1169
|
}[field] ?? String(field);
|
|
1169
1170
|
}
|
|
1171
|
+
// src/sandboxes/index.ts
|
|
1172
|
+
/** Docker Cloud Sandboxes (microVM), separate from the Docker container provider. */
|
|
1173
|
+
var DockerSandboxesSandbox = class extends _mastra_core_workspace.MastraSandbox {
|
|
1174
|
+
id;
|
|
1175
|
+
name = "Docker Sandboxes";
|
|
1176
|
+
provider = "docker-sandboxes";
|
|
1177
|
+
status = "pending";
|
|
1178
|
+
client;
|
|
1179
|
+
ownsClient;
|
|
1180
|
+
image;
|
|
1181
|
+
environment;
|
|
1182
|
+
pinnedSandboxId;
|
|
1183
|
+
creationKey = (0, crypto.randomUUID)();
|
|
1184
|
+
remoteId;
|
|
1185
|
+
sandbox;
|
|
1186
|
+
createdAt = /* @__PURE__ */ new Date();
|
|
1187
|
+
constructor(options) {
|
|
1188
|
+
super({
|
|
1189
|
+
...options,
|
|
1190
|
+
name: "Docker Sandboxes"
|
|
1191
|
+
});
|
|
1192
|
+
this.id = options.id ?? (0, crypto.randomUUID)();
|
|
1193
|
+
this.remoteId = options.sandboxId;
|
|
1194
|
+
this.pinnedSandboxId = options.sandboxId != null;
|
|
1195
|
+
this.image = options.image;
|
|
1196
|
+
this.environment = options.environment;
|
|
1197
|
+
this.ownsClient = !options.client;
|
|
1198
|
+
this.client = options.client ?? new _docker_sandboxes.Sandboxes();
|
|
1199
|
+
}
|
|
1200
|
+
get sandboxId() {
|
|
1201
|
+
return this.remoteId;
|
|
1202
|
+
}
|
|
1203
|
+
async find() {
|
|
1204
|
+
if (this.remoteId) try {
|
|
1205
|
+
return await this.client.get(this.remoteId);
|
|
1206
|
+
} catch (error) {
|
|
1207
|
+
if (!(error instanceof _docker_sandboxes.NotFoundError) || this.pinnedSandboxId) throw error;
|
|
1208
|
+
this.remoteId = void 0;
|
|
1209
|
+
this.sandbox = void 0;
|
|
1210
|
+
this.creationKey = (0, crypto.randomUUID)();
|
|
1211
|
+
}
|
|
1212
|
+
for await (const sandbox of this.client.all()) if (sandbox.displayName === this.id && sandbox.status !== "deleting" && sandbox.status !== "deleted") return sandbox;
|
|
1213
|
+
}
|
|
1214
|
+
async connect(sandbox) {
|
|
1215
|
+
this.sandbox = sandbox;
|
|
1216
|
+
this.remoteId = sandbox.name;
|
|
1217
|
+
if (sandbox.status === "stopped") this.sandbox = sandbox = await sandbox.start();
|
|
1218
|
+
this.sandbox = await sandbox.waitUntilRunning();
|
|
1219
|
+
if (this.sandbox.status !== "running") throw new _mastra_core_workspace.SandboxNotReadyError(`${this.id} (${this.sandbox.status})`);
|
|
1220
|
+
this.createdAt = this.sandbox.resource.core.createdAt ?? this.createdAt;
|
|
1221
|
+
}
|
|
1222
|
+
async create() {
|
|
1223
|
+
const sandbox = await this.client.create({
|
|
1224
|
+
image: this.image,
|
|
1225
|
+
displayName: this.id,
|
|
1226
|
+
environment: this.environment
|
|
1227
|
+
}, { idempotencyKey: this.creationKey });
|
|
1228
|
+
await this.connect(sandbox);
|
|
1229
|
+
}
|
|
1230
|
+
async stop() {
|
|
1231
|
+
if (this.sandbox) this.sandbox = await (await this.sandbox.stop()).waitUntilStopped();
|
|
1232
|
+
}
|
|
1233
|
+
async destroy() {
|
|
1234
|
+
try {
|
|
1235
|
+
if (this.sandbox) await (await this.sandbox.refresh()).delete();
|
|
1236
|
+
} finally {
|
|
1237
|
+
this.sandbox = void 0;
|
|
1238
|
+
if (this.ownsClient) await this.client.close();
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
/** Run a shell command in the microVM, streaming and retaining bounded output; kill its process if canceled. */
|
|
1242
|
+
async executeCommand(command, args = [], options = {}) {
|
|
1243
|
+
throwIfAborted(options.abortSignal, "command");
|
|
1244
|
+
const limit = options.maxRetainedBytes ?? 1048576;
|
|
1245
|
+
if (limit !== Infinity && (!Number.isSafeInteger(limit) || limit < 0)) throw new RangeError("maxRetainedBytes must be a nonnegative integer or Infinity");
|
|
1246
|
+
await this.ensureRunning();
|
|
1247
|
+
const started = Date.now();
|
|
1248
|
+
const controller = new AbortController();
|
|
1249
|
+
let timedOut = false;
|
|
1250
|
+
const timeout = options.timeout == null ? void 0 : setTimeout(() => {
|
|
1251
|
+
timedOut = true;
|
|
1252
|
+
controller.abort();
|
|
1253
|
+
}, options.timeout);
|
|
1254
|
+
const onAbort = () => controller.abort();
|
|
1255
|
+
options.abortSignal?.addEventListener("abort", onAbort, { once: true });
|
|
1256
|
+
if (options.abortSignal?.aborted) controller.abort();
|
|
1257
|
+
const retained = {
|
|
1258
|
+
stdout: [],
|
|
1259
|
+
stderr: []
|
|
1260
|
+
};
|
|
1261
|
+
const head = {
|
|
1262
|
+
stdout: 0,
|
|
1263
|
+
stderr: 0
|
|
1264
|
+
};
|
|
1265
|
+
const size = {
|
|
1266
|
+
stdout: 0,
|
|
1267
|
+
stderr: 0
|
|
1268
|
+
};
|
|
1269
|
+
const dropped = {
|
|
1270
|
+
stdout: 0,
|
|
1271
|
+
stderr: 0
|
|
1272
|
+
};
|
|
1273
|
+
const decoders = {
|
|
1274
|
+
stdout: new string_decoder.StringDecoder("utf8"),
|
|
1275
|
+
stderr: new string_decoder.StringDecoder("utf8")
|
|
1276
|
+
};
|
|
1277
|
+
const discard = (stream, count) => {
|
|
1278
|
+
while (count > 0) {
|
|
1279
|
+
const chunks = retained[stream];
|
|
1280
|
+
const first = chunks[head[stream]];
|
|
1281
|
+
const removed = Math.min(count, first.length);
|
|
1282
|
+
if (removed === first.length) head[stream]++;
|
|
1283
|
+
else chunks[head[stream]] = first.subarray(removed);
|
|
1284
|
+
size[stream] -= removed;
|
|
1285
|
+
dropped[stream] += removed;
|
|
1286
|
+
count -= removed;
|
|
1287
|
+
}
|
|
1288
|
+
if (head[stream] > 64 && head[stream] * 2 > retained[stream].length) {
|
|
1289
|
+
retained[stream].splice(0, head[stream]);
|
|
1290
|
+
head[stream] = 0;
|
|
1291
|
+
}
|
|
1292
|
+
};
|
|
1293
|
+
const append = (stream, data) => {
|
|
1294
|
+
if (!data) return;
|
|
1295
|
+
(stream === "stdout" ? options.onStdout : options.onStderr)?.(data);
|
|
1296
|
+
const chunk = Buffer.from(data);
|
|
1297
|
+
retained[stream].push(chunk);
|
|
1298
|
+
size[stream] += chunk.length;
|
|
1299
|
+
if (limit !== Infinity && size[stream] > limit) {
|
|
1300
|
+
discard(stream, size[stream] - limit);
|
|
1301
|
+
while (size[stream] && (retained[stream][head[stream]][0] & 192) === 128) discard(stream, 1);
|
|
1302
|
+
}
|
|
1303
|
+
};
|
|
1304
|
+
let process;
|
|
1305
|
+
let exitCode = -1;
|
|
1306
|
+
try {
|
|
1307
|
+
process = await this.sandbox.processes.start({
|
|
1308
|
+
args: [
|
|
1309
|
+
"sh",
|
|
1310
|
+
"-lc",
|
|
1311
|
+
args.length ? `${command} ${args.map(shellQuote$1).join(" ")}` : command
|
|
1312
|
+
],
|
|
1313
|
+
workingDir: options.cwd ?? this.workingDirectory,
|
|
1314
|
+
env: Object.fromEntries(Object.entries({
|
|
1315
|
+
...this.getEnv(),
|
|
1316
|
+
...options.env
|
|
1317
|
+
}).filter((entry) => entry[1] != null))
|
|
1318
|
+
}, {});
|
|
1319
|
+
const running = process;
|
|
1320
|
+
const kill = () => {
|
|
1321
|
+
running.signal("kill").catch(() => {});
|
|
1322
|
+
};
|
|
1323
|
+
controller.signal.addEventListener("abort", kill, { once: true });
|
|
1324
|
+
try {
|
|
1325
|
+
if (controller.signal.aborted) throw createAbortError(controller.signal, "command");
|
|
1326
|
+
const connection = await process.connect(void 0, {
|
|
1327
|
+
signal: controller.signal,
|
|
1328
|
+
reconnect: true
|
|
1329
|
+
});
|
|
1330
|
+
try {
|
|
1331
|
+
for await (const event of connection) if (event.type === "chunk" && (event.stream === "stdout" || event.stream === "stderr")) {
|
|
1332
|
+
const stream = event.stream === "stdout" ? "stdout" : "stderr";
|
|
1333
|
+
append(stream, decoders[stream].write(Buffer.from(event.data ?? [])));
|
|
1334
|
+
} else if (event.type === "exited") exitCode = event.exitCode ?? -1;
|
|
1335
|
+
} finally {
|
|
1336
|
+
await connection.close();
|
|
1337
|
+
}
|
|
1338
|
+
} finally {
|
|
1339
|
+
controller.signal.removeEventListener("abort", kill);
|
|
1340
|
+
if (controller.signal.aborted) kill();
|
|
1341
|
+
}
|
|
1342
|
+
if (exitCode === -1 && !controller.signal.aborted) exitCode = (await process.refresh()).exitCode ?? -1;
|
|
1343
|
+
} catch (error) {
|
|
1344
|
+
if (!controller.signal.aborted) throw error;
|
|
1345
|
+
} finally {
|
|
1346
|
+
if (timeout) clearTimeout(timeout);
|
|
1347
|
+
options.abortSignal?.removeEventListener("abort", onAbort);
|
|
1348
|
+
}
|
|
1349
|
+
append("stdout", decoders.stdout.end());
|
|
1350
|
+
append("stderr", decoders.stderr.end());
|
|
1351
|
+
return {
|
|
1352
|
+
success: exitCode === 0 && !controller.signal.aborted,
|
|
1353
|
+
exitCode,
|
|
1354
|
+
stdout: Buffer.concat(retained.stdout.slice(head.stdout)).toString("utf8"),
|
|
1355
|
+
stderr: Buffer.concat(retained.stderr.slice(head.stderr)).toString("utf8"),
|
|
1356
|
+
executionTimeMs: Date.now() - started,
|
|
1357
|
+
timedOut,
|
|
1358
|
+
killed: controller.signal.aborted,
|
|
1359
|
+
stdoutTruncated: dropped.stdout > 0,
|
|
1360
|
+
stderrTruncated: dropped.stderr > 0,
|
|
1361
|
+
stdoutDroppedBytes: dropped.stdout,
|
|
1362
|
+
stderrDroppedBytes: dropped.stderr
|
|
1363
|
+
};
|
|
1364
|
+
}
|
|
1365
|
+
async readFile(path, encoding) {
|
|
1366
|
+
await this.ensureRunning();
|
|
1367
|
+
return encoding ? this.sandbox.files.read(path, { encoding }) : this.sandbox.files.read(path);
|
|
1368
|
+
}
|
|
1369
|
+
async writeFiles(files, options = {}) {
|
|
1370
|
+
throwIfAborted(options.abortSignal, "writeFiles");
|
|
1371
|
+
for (const file of files) if (file.mode !== void 0) (0, _mastra_core_workspace.validateSandboxFileMode)(file.mode);
|
|
1372
|
+
await this.ensureRunning();
|
|
1373
|
+
for (const file of files) {
|
|
1374
|
+
throwIfAborted(options.abortSignal, "writeFiles");
|
|
1375
|
+
try {
|
|
1376
|
+
await this.sandbox.files.mkdir((0, path.dirname)(file.path), { parents: true }, { signal: options.abortSignal });
|
|
1377
|
+
await this.sandbox.files.write(file.path, file.content, { mode: file.mode }, { signal: options.abortSignal });
|
|
1378
|
+
} catch (error) {
|
|
1379
|
+
if (options.abortSignal?.aborted) throw createAbortError(options.abortSignal, "writeFiles");
|
|
1380
|
+
throw error;
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
getInstructions() {
|
|
1385
|
+
return "Commands and files run inside an isolated Docker Cloud Sandbox microVM.";
|
|
1386
|
+
}
|
|
1387
|
+
getInfo() {
|
|
1388
|
+
return {
|
|
1389
|
+
id: this.id,
|
|
1390
|
+
name: this.name,
|
|
1391
|
+
provider: this.provider,
|
|
1392
|
+
status: this.status,
|
|
1393
|
+
createdAt: this.createdAt,
|
|
1394
|
+
metadata: {
|
|
1395
|
+
sandboxId: this.remoteId,
|
|
1396
|
+
image: this.image
|
|
1397
|
+
}
|
|
1398
|
+
};
|
|
1399
|
+
}
|
|
1400
|
+
};
|
|
1401
|
+
/** Quote a single shell argument without interpreting its contents. */
|
|
1402
|
+
function shellQuote$1(arg) {
|
|
1403
|
+
if (/^[a-zA-Z0-9._\-/=:@]+$/.test(arg)) return arg;
|
|
1404
|
+
return `'${arg.replace(/'/g, "'\\''")}'`;
|
|
1405
|
+
}
|
|
1170
1406
|
// src/provider.ts
|
|
1171
1407
|
const dockerSandboxProvider = {
|
|
1172
1408
|
id: "docker",
|
|
@@ -2950,6 +3186,7 @@ var DockerFactorySandbox = class extends _mastra_core_workspace.FactorySandbox {
|
|
|
2950
3186
|
exports.DockerFactorySandbox = DockerFactorySandbox;
|
|
2951
3187
|
exports.DockerProcessManager = DockerProcessManager;
|
|
2952
3188
|
exports.DockerSandbox = DockerSandbox;
|
|
3189
|
+
exports.DockerSandboxesSandbox = DockerSandboxesSandbox;
|
|
2953
3190
|
exports.DockerTemplate = DockerTemplate;
|
|
2954
3191
|
exports.TEMPLATE_IMAGE_REPO = TEMPLATE_IMAGE_REPO;
|
|
2955
3192
|
exports.createDockerRepoTemplate = createDockerRepoTemplate;
|