@mastra/docker 0.14.0-alpha.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { DockerSandbox, type DockerSandboxOptions, type DockerSandboxStartOptions, type DockerTemplateSpec, } from './sandbox/index.js';
2
2
  export { DockerProcessManager } from './sandbox/process-manager.js';
3
+ export { DockerSandboxesSandbox, type DockerSandboxesOptions } from './sandboxes/index.js';
3
4
  export { dockerSandboxProvider } from './provider.js';
4
5
  export * from './template/index.js';
5
6
  export { DockerFactorySandbox } from './factory-sandbox.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,KAAK,oBAAoB,EACzB,KAAK,yBAAyB,EAC9B,KAAK,kBAAkB,GACxB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACnD,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,YAAY,EAAE,2BAA2B,EAAE,4BAA4B,EAAE,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,KAAK,oBAAoB,EACzB,KAAK,yBAAyB,EAC9B,KAAK,kBAAkB,GACxB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,sBAAsB,EAAE,KAAK,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAClF,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACnD,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,YAAY,EAAE,2BAA2B,EAAE,4BAA4B,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/index.js CHANGED
@@ -1,12 +1,13 @@
1
- import { posix } from "path";
1
+ import { dirname, posix } from "path";
2
2
  import { isDeepStrictEqual, promisify } from "util";
3
3
  import { FactorySandbox, MastraSandbox, ProcessHandle, SandboxAbortError, SandboxError, SandboxNotReadyError, SandboxProcessManager, validateSandboxFileMode } from "@mastra/core/workspace";
4
4
  import Docker from "dockerode";
5
5
  import { pack } from "tar-stream";
6
6
  import { StringDecoder } from "string_decoder";
7
+ import { createHash, randomUUID } from "crypto";
8
+ import { NotFoundError, Sandboxes } from "@docker/sandboxes";
7
9
  import posixPath from "path/posix";
8
10
  import { Server, ServerCredentials } from "@grpc/grpc-js";
9
- import { createHash } from "crypto";
10
11
  import { execFile } from "child_process";
11
12
  // src/abort.ts
12
13
  function createAbortError(signal, operation) {
@@ -1143,6 +1144,241 @@ function toDockerSandboxOptionName(field) {
1143
1144
  Mounts: "mounts"
1144
1145
  }[field] ?? String(field);
1145
1146
  }
1147
+ // src/sandboxes/index.ts
1148
+ /** Docker Cloud Sandboxes (microVM), separate from the Docker container provider. */
1149
+ var DockerSandboxesSandbox = class extends MastraSandbox {
1150
+ id;
1151
+ name = "Docker Sandboxes";
1152
+ provider = "docker-sandboxes";
1153
+ status = "pending";
1154
+ client;
1155
+ ownsClient;
1156
+ image;
1157
+ environment;
1158
+ pinnedSandboxId;
1159
+ creationKey = randomUUID();
1160
+ remoteId;
1161
+ sandbox;
1162
+ createdAt = /* @__PURE__ */ new Date();
1163
+ constructor(options) {
1164
+ super({
1165
+ ...options,
1166
+ name: "Docker Sandboxes"
1167
+ });
1168
+ this.id = options.id ?? randomUUID();
1169
+ this.remoteId = options.sandboxId;
1170
+ this.pinnedSandboxId = options.sandboxId != null;
1171
+ this.image = options.image;
1172
+ this.environment = options.environment;
1173
+ this.ownsClient = !options.client;
1174
+ this.client = options.client ?? new Sandboxes();
1175
+ }
1176
+ get sandboxId() {
1177
+ return this.remoteId;
1178
+ }
1179
+ async find() {
1180
+ if (this.remoteId) try {
1181
+ return await this.client.get(this.remoteId);
1182
+ } catch (error) {
1183
+ if (!(error instanceof NotFoundError) || this.pinnedSandboxId) throw error;
1184
+ this.remoteId = void 0;
1185
+ this.sandbox = void 0;
1186
+ this.creationKey = randomUUID();
1187
+ }
1188
+ for await (const sandbox of this.client.all()) if (sandbox.displayName === this.id && sandbox.status !== "deleting" && sandbox.status !== "deleted") return sandbox;
1189
+ }
1190
+ async connect(sandbox) {
1191
+ this.sandbox = sandbox;
1192
+ this.remoteId = sandbox.name;
1193
+ if (sandbox.status === "stopped") this.sandbox = sandbox = await sandbox.start();
1194
+ this.sandbox = await sandbox.waitUntilRunning();
1195
+ if (this.sandbox.status !== "running") throw new SandboxNotReadyError(`${this.id} (${this.sandbox.status})`);
1196
+ this.createdAt = this.sandbox.resource.core.createdAt ?? this.createdAt;
1197
+ }
1198
+ async create() {
1199
+ const sandbox = await this.client.create({
1200
+ image: this.image,
1201
+ displayName: this.id,
1202
+ environment: this.environment
1203
+ }, { idempotencyKey: this.creationKey });
1204
+ await this.connect(sandbox);
1205
+ }
1206
+ async stop() {
1207
+ if (this.sandbox) this.sandbox = await (await this.sandbox.stop()).waitUntilStopped();
1208
+ }
1209
+ async destroy() {
1210
+ try {
1211
+ if (this.sandbox) await (await this.sandbox.refresh()).delete();
1212
+ } finally {
1213
+ this.sandbox = void 0;
1214
+ if (this.ownsClient) await this.client.close();
1215
+ }
1216
+ }
1217
+ /** Run a shell command in the microVM, streaming and retaining bounded output; kill its process if canceled. */
1218
+ async executeCommand(command, args = [], options = {}) {
1219
+ throwIfAborted(options.abortSignal, "command");
1220
+ const limit = options.maxRetainedBytes ?? 1048576;
1221
+ if (limit !== Infinity && (!Number.isSafeInteger(limit) || limit < 0)) throw new RangeError("maxRetainedBytes must be a nonnegative integer or Infinity");
1222
+ await this.ensureRunning();
1223
+ const started = Date.now();
1224
+ const controller = new AbortController();
1225
+ let timedOut = false;
1226
+ const timeout = options.timeout == null ? void 0 : setTimeout(() => {
1227
+ timedOut = true;
1228
+ controller.abort();
1229
+ }, options.timeout);
1230
+ const onAbort = () => controller.abort();
1231
+ options.abortSignal?.addEventListener("abort", onAbort, { once: true });
1232
+ if (options.abortSignal?.aborted) controller.abort();
1233
+ const retained = {
1234
+ stdout: [],
1235
+ stderr: []
1236
+ };
1237
+ const head = {
1238
+ stdout: 0,
1239
+ stderr: 0
1240
+ };
1241
+ const size = {
1242
+ stdout: 0,
1243
+ stderr: 0
1244
+ };
1245
+ const dropped = {
1246
+ stdout: 0,
1247
+ stderr: 0
1248
+ };
1249
+ const decoders = {
1250
+ stdout: new StringDecoder("utf8"),
1251
+ stderr: new StringDecoder("utf8")
1252
+ };
1253
+ const discard = (stream, count) => {
1254
+ while (count > 0) {
1255
+ const chunks = retained[stream];
1256
+ const first = chunks[head[stream]];
1257
+ const removed = Math.min(count, first.length);
1258
+ if (removed === first.length) head[stream]++;
1259
+ else chunks[head[stream]] = first.subarray(removed);
1260
+ size[stream] -= removed;
1261
+ dropped[stream] += removed;
1262
+ count -= removed;
1263
+ }
1264
+ if (head[stream] > 64 && head[stream] * 2 > retained[stream].length) {
1265
+ retained[stream].splice(0, head[stream]);
1266
+ head[stream] = 0;
1267
+ }
1268
+ };
1269
+ const append = (stream, data) => {
1270
+ if (!data) return;
1271
+ (stream === "stdout" ? options.onStdout : options.onStderr)?.(data);
1272
+ const chunk = Buffer.from(data);
1273
+ retained[stream].push(chunk);
1274
+ size[stream] += chunk.length;
1275
+ if (limit !== Infinity && size[stream] > limit) {
1276
+ discard(stream, size[stream] - limit);
1277
+ while (size[stream] && (retained[stream][head[stream]][0] & 192) === 128) discard(stream, 1);
1278
+ }
1279
+ };
1280
+ let process;
1281
+ let exitCode = -1;
1282
+ try {
1283
+ process = await this.sandbox.processes.start({
1284
+ args: [
1285
+ "sh",
1286
+ "-lc",
1287
+ args.length ? `${command} ${args.map(shellQuote$1).join(" ")}` : command
1288
+ ],
1289
+ workingDir: options.cwd ?? this.workingDirectory,
1290
+ env: Object.fromEntries(Object.entries({
1291
+ ...this.getEnv(),
1292
+ ...options.env
1293
+ }).filter((entry) => entry[1] != null))
1294
+ }, {});
1295
+ const running = process;
1296
+ const kill = () => {
1297
+ running.signal("kill").catch(() => {});
1298
+ };
1299
+ controller.signal.addEventListener("abort", kill, { once: true });
1300
+ try {
1301
+ if (controller.signal.aborted) throw createAbortError(controller.signal, "command");
1302
+ const connection = await process.connect(void 0, {
1303
+ signal: controller.signal,
1304
+ reconnect: true
1305
+ });
1306
+ try {
1307
+ for await (const event of connection) if (event.type === "chunk" && (event.stream === "stdout" || event.stream === "stderr")) {
1308
+ const stream = event.stream === "stdout" ? "stdout" : "stderr";
1309
+ append(stream, decoders[stream].write(Buffer.from(event.data ?? [])));
1310
+ } else if (event.type === "exited") exitCode = event.exitCode ?? -1;
1311
+ } finally {
1312
+ await connection.close();
1313
+ }
1314
+ } finally {
1315
+ controller.signal.removeEventListener("abort", kill);
1316
+ if (controller.signal.aborted) kill();
1317
+ }
1318
+ if (exitCode === -1 && !controller.signal.aborted) exitCode = (await process.refresh()).exitCode ?? -1;
1319
+ } catch (error) {
1320
+ if (!controller.signal.aborted) throw error;
1321
+ } finally {
1322
+ if (timeout) clearTimeout(timeout);
1323
+ options.abortSignal?.removeEventListener("abort", onAbort);
1324
+ }
1325
+ append("stdout", decoders.stdout.end());
1326
+ append("stderr", decoders.stderr.end());
1327
+ return {
1328
+ success: exitCode === 0 && !controller.signal.aborted,
1329
+ exitCode,
1330
+ stdout: Buffer.concat(retained.stdout.slice(head.stdout)).toString("utf8"),
1331
+ stderr: Buffer.concat(retained.stderr.slice(head.stderr)).toString("utf8"),
1332
+ executionTimeMs: Date.now() - started,
1333
+ timedOut,
1334
+ killed: controller.signal.aborted,
1335
+ stdoutTruncated: dropped.stdout > 0,
1336
+ stderrTruncated: dropped.stderr > 0,
1337
+ stdoutDroppedBytes: dropped.stdout,
1338
+ stderrDroppedBytes: dropped.stderr
1339
+ };
1340
+ }
1341
+ async readFile(path, encoding) {
1342
+ await this.ensureRunning();
1343
+ return encoding ? this.sandbox.files.read(path, { encoding }) : this.sandbox.files.read(path);
1344
+ }
1345
+ async writeFiles(files, options = {}) {
1346
+ throwIfAborted(options.abortSignal, "writeFiles");
1347
+ for (const file of files) if (file.mode !== void 0) validateSandboxFileMode(file.mode);
1348
+ await this.ensureRunning();
1349
+ for (const file of files) {
1350
+ throwIfAborted(options.abortSignal, "writeFiles");
1351
+ try {
1352
+ await this.sandbox.files.mkdir(dirname(file.path), { parents: true }, { signal: options.abortSignal });
1353
+ await this.sandbox.files.write(file.path, file.content, { mode: file.mode }, { signal: options.abortSignal });
1354
+ } catch (error) {
1355
+ if (options.abortSignal?.aborted) throw createAbortError(options.abortSignal, "writeFiles");
1356
+ throw error;
1357
+ }
1358
+ }
1359
+ }
1360
+ getInstructions() {
1361
+ return "Commands and files run inside an isolated Docker Cloud Sandbox microVM.";
1362
+ }
1363
+ getInfo() {
1364
+ return {
1365
+ id: this.id,
1366
+ name: this.name,
1367
+ provider: this.provider,
1368
+ status: this.status,
1369
+ createdAt: this.createdAt,
1370
+ metadata: {
1371
+ sandboxId: this.remoteId,
1372
+ image: this.image
1373
+ }
1374
+ };
1375
+ }
1376
+ };
1377
+ /** Quote a single shell argument without interpreting its contents. */
1378
+ function shellQuote$1(arg) {
1379
+ if (/^[a-zA-Z0-9._\-/=:@]+$/.test(arg)) return arg;
1380
+ return `'${arg.replace(/'/g, "'\\''")}'`;
1381
+ }
1146
1382
  // src/provider.ts
1147
1383
  const dockerSandboxProvider = {
1148
1384
  id: "docker",
@@ -2923,6 +3159,6 @@ var DockerFactorySandbox = class extends FactorySandbox {
2923
3159
  });
2924
3160
  }
2925
3161
  };
2926
- export { DockerFactorySandbox, DockerProcessManager, DockerSandbox, DockerTemplate, TEMPLATE_IMAGE_REPO, createDockerRepoTemplate, dockerSandboxProvider, secretNames, synthesizeDockerfile, templateIdentity, templateImageTag };
3162
+ export { DockerFactorySandbox, DockerProcessManager, DockerSandbox, DockerSandboxesSandbox, DockerTemplate, TEMPLATE_IMAGE_REPO, createDockerRepoTemplate, dockerSandboxProvider, secretNames, synthesizeDockerfile, templateIdentity, templateImageTag };
2927
3163
 
2928
3164
  //# sourceMappingURL=index.js.map