@munitoai/cli 0.2.0 → 0.3.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/cli.js CHANGED
@@ -10,28 +10,33 @@ import {
10
10
  DEFAULT_MODEL,
11
11
  DEFAULT_SMALL_MODEL,
12
12
  ENV_KEY,
13
+ ENV_REGISTRY_TOKEN,
13
14
  MissingKeyError,
14
15
  PACKAGES,
15
16
  PLUGINS,
16
17
  PROVIDERS,
18
+ TemplateError,
17
19
  WiringError,
18
20
  authPath,
19
21
  clear,
20
22
  createAgentSandbox,
23
+ deployBody,
21
24
  drop,
22
25
  findBase,
23
26
  job,
24
27
  mask,
28
+ registryAuth,
25
29
  require as require2,
26
30
  resolveHost,
27
31
  runAgent,
28
32
  save,
33
+ waitForBuild,
29
34
  warm,
30
35
  wire
31
- } from "./chunk-7OXZJKC2.js";
36
+ } from "./chunk-EZEXBJTZ.js";
32
37
 
33
38
  // src/cli.ts
34
- import { runMain } from "citty";
39
+ import { runCommand, runMain } from "citty";
35
40
 
36
41
  // src/core/workspace.ts
37
42
  import { execFile } from "child_process";
@@ -83,8 +88,8 @@ function isBinary(buffer) {
83
88
  return buffer.subarray(0, 8e3).includes(0);
84
89
  }
85
90
  function describeUpload(files) {
86
- const bytes = files.reduce((sum, file) => sum + Buffer.byteLength(file.content), 0);
87
- const size = bytes > 1 << 20 ? `${(bytes / (1 << 20)).toFixed(1)} MiB` : `${Math.ceil(bytes / 1024)} KiB`;
91
+ const bytes2 = files.reduce((sum, file) => sum + Buffer.byteLength(file.content), 0);
92
+ const size = bytes2 > 1 << 20 ? `${(bytes2 / (1 << 20)).toFixed(1)} MiB` : `${Math.ceil(bytes2 / 1024)} KiB`;
88
93
  return `${files.length} files, ${size}`;
89
94
  }
90
95
  async function applyPatch(patch, dir) {
@@ -105,7 +110,7 @@ async function applyPatch(patch, dir) {
105
110
  }
106
111
 
107
112
  // src/cli/main.ts
108
- import { defineCommand as defineCommand8 } from "citty";
113
+ import { defineCommand as defineCommand9 } from "citty";
109
114
 
110
115
  // src/cli/commands/agent.ts
111
116
  import { defineCommand } from "citty";
@@ -230,11 +235,16 @@ function duration(ms) {
230
235
  const minutes = Math.floor(ms / 6e4);
231
236
  return `${minutes} min ${Math.round(ms % 6e4 / 1e3)} s`;
232
237
  }
238
+ function bytes(value) {
239
+ if (value === null || value === void 0) return "-";
240
+ if (value >= 1024 ** 3) return `${(value / 1024 ** 3).toFixed(1)} GiB`;
241
+ return `${Math.max(1, Math.round(value / 1024 ** 2))} MiB`;
242
+ }
233
243
  function table(rows, p, heading = true) {
234
244
  if (!rows.length) return "";
235
- const widths = rows[0].map((_, column) => Math.max(...rows.map((row2) => (row2[column] ?? "").length)));
236
- return rows.map((row2, index) => {
237
- const line = row2.map((cell, column) => (cell ?? "").padEnd(widths[column])).join(" ").trimEnd();
245
+ const widths = rows[0].map((_, column) => Math.max(...rows.map((row3) => (row3[column] ?? "").length)));
246
+ return rows.map((row3, index) => {
247
+ const line = row3.map((cell, column) => (cell ?? "").padEnd(widths[column])).join(" ").trimEnd();
238
248
  return heading && index === 0 ? p.dim(line) : line;
239
249
  }).join("\n");
240
250
  }
@@ -819,7 +829,7 @@ var mcp = defineCommand6({
819
829
  description: "Run one shell command on a fresh Linux microVM and return its output. The sandbox is deleted afterwards. Use this for code you do not trust, or for a build that must start clean.",
820
830
  inputSchema: {
821
831
  command: z.string().describe("The command, which runs in bash."),
822
- template: z.string().default("base").describe("base, ubuntu, agent or desktop."),
832
+ template: z.string().default("base").describe("base, ubuntu, agent, desktop, or the name of a custom template."),
823
833
  internet: z.boolean().default(false).describe("Allow outbound connections."),
824
834
  timeout: z.number().int().positive().default(120).describe("Seconds the command may take.")
825
835
  }
@@ -924,7 +934,11 @@ var run2 = defineCommand7({
924
934
  args: {
925
935
  ...commonArgs,
926
936
  command: { type: "positional", required: true, description: "The command to run." },
927
- template: { type: "string", description: "A template. The default is base.", default: "base" },
937
+ template: {
938
+ type: "string",
939
+ description: "base, ubuntu, agent, desktop, or the name of a custom template. The default is base.",
940
+ default: "base"
941
+ },
928
942
  size: { type: "string", description: "tiny, small, default or large.", default: "small" },
929
943
  internet: { type: "boolean", description: "Allow outbound connections." },
930
944
  timeout: { type: "string", description: "Seconds the command may take." }
@@ -980,9 +994,9 @@ async function stream(client, id, command, out2, timeout, cwd) {
980
994
  var rm2 = defineCommand7({
981
995
  meta: { name: "rm", description: "Delete sandboxes." },
982
996
  args: { ...commonArgs, ids: { type: "positional", required: true, description: "One or more sandbox ids." } },
983
- async run({ args, rawArgs }) {
997
+ async run({ args, rawArgs: rawArgs2 }) {
984
998
  const { client, out: out2 } = await context(args);
985
- const ids = rawArgs.filter((value) => value.startsWith("sbx_"));
999
+ const ids = rawArgs2.filter((value) => value.startsWith("sbx_"));
986
1000
  for (const id of ids.length ? ids : [args.ids]) {
987
1001
  await client.deleteSandbox(id);
988
1002
  out2.step("deleted", id);
@@ -1086,7 +1100,11 @@ var create = defineCommand7({
1086
1100
  meta: { name: "create", description: "Create a sandbox and leave it running." },
1087
1101
  args: {
1088
1102
  ...commonArgs,
1089
- template: { type: "string", description: "base, ubuntu, agent or desktop.", default: AGENT_TEMPLATE },
1103
+ template: {
1104
+ type: "string",
1105
+ description: "base, ubuntu, agent, desktop, or the name of a custom template.",
1106
+ default: AGENT_TEMPLATE
1107
+ },
1090
1108
  agent: { type: "string", description: `Point one harness at a provider: ${AGENTS.join(", ")}.` },
1091
1109
  provider: { type: "string", description: `Where the model comes from: ${PROVIDERS.join(", ")}.` },
1092
1110
  auth: { type: "string", description: "token or subscription." },
@@ -1119,9 +1137,164 @@ var create = defineCommand7({
1119
1137
  }
1120
1138
  });
1121
1139
 
1140
+ // src/cli/commands/templates.ts
1141
+ import { defineCommand as defineCommand8 } from "citty";
1142
+ var str2 = (value) => typeof value === "string" && value ? value : void 0;
1143
+ var nameArg2 = {
1144
+ name: { type: "positional", required: true, description: "The name or the id of the template." }
1145
+ };
1146
+ async function readStdin() {
1147
+ if (process.stdin.isTTY) throw new TemplateError(`Pipe the token to --password-stdin, or set ${ENV_REGISTRY_TOKEN}.`);
1148
+ const chunks = [];
1149
+ for await (const chunk of process.stdin) chunks.push(Buffer.from(chunk));
1150
+ return Buffer.concat(chunks).toString("utf8");
1151
+ }
1152
+ var deploy = defineCommand8({
1153
+ meta: {
1154
+ name: "deploy",
1155
+ description: "Build a container image into a template. A name that exists gets a new version."
1156
+ },
1157
+ args: {
1158
+ ...commonArgs,
1159
+ name: {
1160
+ type: "positional",
1161
+ required: true,
1162
+ description: "The template name: lower case letters, digits and hyphens."
1163
+ },
1164
+ image: { type: "positional", required: true, description: "The image, such as ghcr.io/acme/agent:1.4." },
1165
+ username: { type: "string", description: "The user name of a private registry." },
1166
+ "password-stdin": {
1167
+ type: "boolean",
1168
+ description: `Read the registry password or token from standard input. Or set ${ENV_REGISTRY_TOKEN}.`
1169
+ },
1170
+ wait: { type: "boolean", description: "Wait until the build ends. Exit 1 when it fails." }
1171
+ },
1172
+ async run({ args }) {
1173
+ if (args.password !== void 0) {
1174
+ throw new TemplateError(`There is no --password. Use --password-stdin, or set ${ENV_REGISTRY_TOKEN}.`);
1175
+ }
1176
+ const { auth, ignored } = registryAuth({
1177
+ username: str2(args.username),
1178
+ stdin: args["password-stdin"] ? await readStdin() : void 0,
1179
+ env: process.env[ENV_REGISTRY_TOKEN]
1180
+ });
1181
+ const body = deployBody(String(args.name), String(args.image), auth);
1182
+ const { client, out: out2 } = await context(args);
1183
+ if (ignored) out2.warn(`${ENV_REGISTRY_TOKEN} is set without --username, so the deploy sends no credential.`);
1184
+ const template = await client.deployTemplate(body);
1185
+ out2.event({ type: "template", ...template });
1186
+ out2.step("deploying", `${template.name} v${template.build.version} ${template.build.image}`);
1187
+ if (args.wait) {
1188
+ await follow(client, out2, template.id);
1189
+ return;
1190
+ }
1191
+ out2.line(out2.p.dim(` munito templates get ${template.name} --wait`));
1192
+ out2.result("", { template: template.name, id: template.id, version: template.build.version });
1193
+ }
1194
+ });
1195
+ var ls2 = defineCommand8({
1196
+ meta: { name: "ls", description: "List the templates of your workspace." },
1197
+ args: commonArgs,
1198
+ async run({ args }) {
1199
+ const { client, out: out2 } = await context(args);
1200
+ const templates2 = await client.listTemplates();
1201
+ for (const template of templates2) out2.event({ type: "template", ...template });
1202
+ if (out2.mode === "json") return;
1203
+ if (!templates2.length) {
1204
+ out2.line(out2.p.dim("No templates. Deploy one with `munito templates deploy <name> <image>`."));
1205
+ return;
1206
+ }
1207
+ const rows = [["NAME", "STATUS", "VERSION", "IMAGE", "SIZE", "NEWEST BUILD"], ...templates2.map(row2)];
1208
+ out2.line(table(rows, out2.p));
1209
+ }
1210
+ });
1211
+ function row2(template) {
1212
+ const serving = template.version ? `v${template.version}` : "-";
1213
+ const { build } = template;
1214
+ return [
1215
+ template.name,
1216
+ template.status,
1217
+ serving,
1218
+ template.image ?? "-",
1219
+ bytes(template.size_bytes),
1220
+ `v${build.version} ${build.status}`
1221
+ ];
1222
+ }
1223
+ var get = defineCommand8({
1224
+ meta: { name: "get", description: "Show a template and its newest build." },
1225
+ args: {
1226
+ ...commonArgs,
1227
+ ...nameArg2,
1228
+ wait: { type: "boolean", description: "Wait until the newest build ends. Exit 1 when it fails." }
1229
+ },
1230
+ async run({ args }) {
1231
+ const { client, out: out2 } = await context(args);
1232
+ if (args.wait) {
1233
+ await follow(client, out2, String(args.name));
1234
+ return;
1235
+ }
1236
+ show(out2, await client.getTemplate(String(args.name)));
1237
+ }
1238
+ });
1239
+ var rm3 = defineCommand8({
1240
+ meta: { name: "rm", description: "Delete templates. Their running sandboxes keep running." },
1241
+ args: { ...commonArgs, names: { type: "positional", required: true, description: "One or more template names." } },
1242
+ async run({ args }) {
1243
+ const { client, out: out2 } = await context(args);
1244
+ const names = args._.length ? args._ : [String(args.names)];
1245
+ for (const name of names) {
1246
+ await client.deleteTemplate(name);
1247
+ out2.event({ type: "template.deleted", name });
1248
+ out2.step("deleted", name);
1249
+ }
1250
+ }
1251
+ });
1252
+ async function follow(client, out2, ref) {
1253
+ const template = await waitForBuild(client, ref, {
1254
+ onEvent: (event) => {
1255
+ out2.event({ ...event });
1256
+ out2.step(event.status, `${event.name} v${event.version}`);
1257
+ }
1258
+ });
1259
+ show(out2, template);
1260
+ if (template.build.status === "failed") process.exitCode = 1;
1261
+ }
1262
+ function show(out2, template) {
1263
+ out2.event({ type: "template", ...template });
1264
+ const { build } = template;
1265
+ out2.result("", {
1266
+ template: template.name,
1267
+ id: template.id,
1268
+ status: template.status,
1269
+ version: template.version ?? null,
1270
+ build: { version: build.version, status: build.status }
1271
+ });
1272
+ if (out2.mode === "json") return;
1273
+ out2.step(template.name, template.status);
1274
+ const facts = [
1275
+ ["id", template.id],
1276
+ ["serving", template.version ? `v${template.version} ${template.image ?? ""}` : "none yet"],
1277
+ ["size", bytes(template.size_bytes)],
1278
+ ["digest", template.digest ?? "-"],
1279
+ ["build", `v${build.version} ${build.status} ${build.image}`]
1280
+ ];
1281
+ if (build.error) facts.push(["error", build.error.message]);
1282
+ out2.line(
1283
+ table(facts, out2.p, false).split("\n").map((line) => ` ${line}`).join("\n")
1284
+ );
1285
+ if (build.status === "failed" && build.log) {
1286
+ out2.line(out2.p.dim(" the end of the build output:"));
1287
+ for (const line of build.log.replace(/\n$/, "").split("\n")) out2.line(out2.p.dim(` ${line}`));
1288
+ }
1289
+ }
1290
+ var templates = defineCommand8({
1291
+ meta: { name: "templates", description: "Start sandboxes from your own container image." },
1292
+ subCommands: { deploy, ls: ls2, get, rm: rm3 }
1293
+ });
1294
+
1122
1295
  // src/cli/main.ts
1123
- var VERSION = true ? "0.2.0" : "0.0.0-dev";
1124
- var main = defineCommand8({
1296
+ var VERSION = true ? "0.3.0" : "0.0.0-dev";
1297
+ var main = defineCommand9({
1125
1298
  meta: {
1126
1299
  name: "munito",
1127
1300
  // Without this, `munito --version` exits with "No version specified".
@@ -1146,7 +1319,8 @@ var main = defineCommand8({
1146
1319
  rm: rm2,
1147
1320
  pause,
1148
1321
  resume,
1149
- fork
1322
+ fork,
1323
+ templates
1150
1324
  }
1151
1325
  });
1152
1326
 
@@ -1158,7 +1332,7 @@ function report2(error) {
1158
1332
  channel2.error(error.message);
1159
1333
  return 2;
1160
1334
  }
1161
- if (error instanceof WiringError || error instanceof WorkspaceError) {
1335
+ if (error instanceof WiringError || error instanceof WorkspaceError || error instanceof TemplateError) {
1162
1336
  channel2.error(error.message);
1163
1337
  return 2;
1164
1338
  }
@@ -1176,7 +1350,12 @@ function report2(error) {
1176
1350
  process.on("unhandledRejection", (reason) => {
1177
1351
  process.exit(report2(reason));
1178
1352
  });
1179
- await runMain(main).catch((error) => {
1180
- process.exit(report2(error));
1181
- });
1353
+ var rawArgs = process.argv.slice(2);
1354
+ var own = rawArgs.some((arg) => arg === "--help" || arg === "-h") || rawArgs.length === 1 && (rawArgs[0] === "--version" || rawArgs[0] === "-v");
1355
+ if (own) await runMain(main);
1356
+ else
1357
+ await runCommand(main, { rawArgs }).catch(async (error) => {
1358
+ if (error instanceof Error && error.name === "CLIError") await runMain(main);
1359
+ process.exit(report2(error));
1360
+ });
1182
1361
  //# sourceMappingURL=cli.js.map