@munitoai/cli 0.3.0 → 0.5.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
@@ -16,15 +16,19 @@ import {
16
16
  PLUGINS,
17
17
  PROVIDERS,
18
18
  TemplateError,
19
+ WORKDIR,
19
20
  WiringError,
20
21
  authPath,
21
22
  clear,
22
23
  createAgentSandbox,
23
24
  deployBody,
24
25
  drop,
26
+ endpoints,
25
27
  findBase,
28
+ findProxy,
26
29
  job,
27
30
  mask,
31
+ opencodeMajor,
28
32
  registryAuth,
29
33
  require as require2,
30
34
  resolveHost,
@@ -33,7 +37,7 @@ import {
33
37
  waitForBuild,
34
38
  warm,
35
39
  wire
36
- } from "./chunk-EZEXBJTZ.js";
40
+ } from "./chunk-JHSIZLVR.js";
37
41
 
38
42
  // src/cli.ts
39
43
  import { runCommand, runMain } from "citty";
@@ -54,10 +58,10 @@ async function collect(dir) {
54
58
  const root = resolve(dir ?? process.cwd());
55
59
  const listed = await listTracked(root);
56
60
  if (!listed) return [];
57
- const files = [];
61
+ const files2 = [];
58
62
  let total = 0;
59
63
  for (const relative of listed) {
60
- if (files.length >= MAX_FILES) break;
64
+ if (files2.length >= MAX_FILES) break;
61
65
  const absolute = join(root, relative);
62
66
  let info;
63
67
  try {
@@ -70,9 +74,9 @@ async function collect(dir) {
70
74
  const buffer = await readFile(absolute);
71
75
  if (isBinary(buffer)) continue;
72
76
  total += info.size;
73
- files.push({ path: relative, content: buffer.toString("utf8") });
77
+ files2.push({ path: relative, content: buffer.toString("utf8") });
74
78
  }
75
- return files;
79
+ return files2;
76
80
  }
77
81
  async function listTracked(root) {
78
82
  try {
@@ -87,10 +91,10 @@ async function listTracked(root) {
87
91
  function isBinary(buffer) {
88
92
  return buffer.subarray(0, 8e3).includes(0);
89
93
  }
90
- function describeUpload(files) {
91
- const bytes2 = files.reduce((sum, file) => sum + Buffer.byteLength(file.content), 0);
94
+ function describeUpload(files2) {
95
+ const bytes2 = files2.reduce((sum, file) => sum + Buffer.byteLength(file.content), 0);
92
96
  const size = bytes2 > 1 << 20 ? `${(bytes2 / (1 << 20)).toFixed(1)} MiB` : `${Math.ceil(bytes2 / 1024)} KiB`;
93
- return `${files.length} files, ${size}`;
97
+ return `${files2.length} files, ${size}`;
94
98
  }
95
99
  async function applyPatch(patch, dir) {
96
100
  const root = resolve(dir ?? process.cwd());
@@ -110,7 +114,7 @@ async function applyPatch(patch, dir) {
110
114
  }
111
115
 
112
116
  // src/cli/main.ts
113
- import { defineCommand as defineCommand9 } from "citty";
117
+ import { defineCommand as defineCommand11 } from "citty";
114
118
 
115
119
  // src/cli/commands/agent.ts
116
120
  import { defineCommand } from "citty";
@@ -186,24 +190,24 @@ function cube(p) {
186
190
  function out(json, stream2 = process.stdout) {
187
191
  const p = palette(stream2);
188
192
  const mode = json ? "json" : p.depth === 0 ? "plain" : "rich";
189
- const write = (text2) => stream2.write(`${text2}
193
+ const write2 = (text2) => stream2.write(`${text2}
190
194
  `);
191
195
  return {
192
196
  mode,
193
197
  p,
194
198
  line(text2 = "") {
195
- if (mode !== "json") write(text2);
199
+ if (mode !== "json") write2(text2);
196
200
  },
197
201
  step(label, detail) {
198
202
  if (mode === "json") return;
199
203
  const mark = mode === "rich" ? p.primary("\u203A") : ">";
200
- write(detail ? `${mark} ${label} ${p.dim(detail)}` : `${mark} ${label}`);
204
+ write2(detail ? `${mark} ${label} ${p.dim(detail)}` : `${mark} ${label}`);
201
205
  },
202
206
  event(body) {
203
- if (mode === "json") write(JSON.stringify(body));
207
+ if (mode === "json") write2(JSON.stringify(body));
204
208
  },
205
209
  result(text2, body) {
206
- if (mode === "json") write(JSON.stringify({ type: "result", ...body, result: text2 }));
210
+ if (mode === "json") write2(JSON.stringify({ type: "result", ...body, result: text2 }));
207
211
  },
208
212
  warn(text2) {
209
213
  if (mode === "json") {
@@ -591,7 +595,7 @@ ${CONSOLE_KEYS}`, "API key");
591
595
  key = String(answer).trim();
592
596
  }
593
597
  const probe = new Client({ apiKey: key, baseUrl: host, attempts: 1 });
594
- await probe.listSandboxes();
598
+ await probe.listSandboxes({ limit: 1 });
595
599
  const path = await save(key, host);
596
600
  out2.step("saved", `${mask(key)} for ${host}`);
597
601
  out2.line(out2.p.dim(` ${path}, readable by you only`));
@@ -611,25 +615,110 @@ var whoami = defineCommand3({
611
615
  args: commonArgs,
612
616
  async run({ args }) {
613
617
  const ctx = await context(args);
614
- const sandboxes = await ctx.client.listSandboxes();
615
- const running = sandboxes.filter((box) => box.status === "running").length;
618
+ const running = (await ctx.client.listSandboxes({ status: "running" })).length;
616
619
  ctx.out.step("host", ctx.host);
617
620
  ctx.out.step("key", `${ctx.source}`);
618
- ctx.out.step("sandboxes", `${running} running, ${sandboxes.length} known`);
619
- ctx.out.result("", { host: ctx.host, source: ctx.source, running, total: sandboxes.length });
621
+ ctx.out.step("sandboxes", `${running} running`);
622
+ ctx.out.result("", { host: ctx.host, source: ctx.source, running });
620
623
  }
621
624
  });
622
625
 
626
+ // src/cli/commands/files.ts
627
+ import { readFile as readFile2 } from "fs/promises";
628
+ import { defineCommand as defineCommand4 } from "citty";
629
+
630
+ // src/core/files.ts
631
+ function fileSize(bytes2) {
632
+ const units = ["B", "KiB", "MiB", "GiB", "TiB"];
633
+ let value = bytes2;
634
+ let unit = 0;
635
+ while (value >= 1024 && unit < units.length - 1) {
636
+ value /= 1024;
637
+ unit++;
638
+ }
639
+ return unit === 0 ? `${value} B` : `${value.toFixed(1)} ${units[unit]}`;
640
+ }
641
+ function fileRow(entry) {
642
+ const modified = new Date(entry.modified_at * 1e3).toISOString().slice(0, 16).replace("T", " ");
643
+ return [entry.name, entry.type, entry.type === "file" ? fileSize(entry.size) : "-", modified];
644
+ }
645
+
646
+ // src/cli/commands/files.ts
647
+ var str2 = (value) => typeof value === "string" && value ? value : void 0;
648
+ var idArg = { id: { type: "positional", required: true, description: "The sandbox id." } };
649
+ async function readStdin() {
650
+ if (process.stdin.isTTY) throw new Error("Pipe the content to the command, or name a local file with --from.");
651
+ const chunks = [];
652
+ for await (const chunk of process.stdin) chunks.push(Buffer.from(chunk));
653
+ return Buffer.concat(chunks);
654
+ }
655
+ var ls = defineCommand4({
656
+ meta: { name: "ls", description: "List a directory of a sandbox." },
657
+ args: {
658
+ ...commonArgs,
659
+ ...idArg,
660
+ path: { type: "positional", required: false, description: `The directory. The default is ${WORKDIR}.` }
661
+ },
662
+ async run({ args }) {
663
+ const { client, out: out2 } = await context(args);
664
+ const entries = await client.listFiles(String(args.id), str2(args.path) ?? WORKDIR);
665
+ for (const entry of entries) out2.event({ type: "file", entry });
666
+ if (out2.mode === "json") return;
667
+ if (entries.length) out2.line(table([["NAME", "TYPE", "SIZE", "MODIFIED"], ...entries.map(fileRow)], out2.p));
668
+ else out2.line(out2.p.dim("No files."));
669
+ }
670
+ });
671
+ var read = defineCommand4({
672
+ meta: { name: "read", description: "Print a file of a sandbox." },
673
+ args: {
674
+ ...commonArgs,
675
+ ...idArg,
676
+ path: { type: "positional", required: true, description: "The file in the sandbox." }
677
+ },
678
+ async run({ args }) {
679
+ const { client, out: out2 } = await context(args);
680
+ const path = String(args.path);
681
+ const content = await client.readFileBytes(String(args.id), path);
682
+ if (out2.mode === "json") {
683
+ out2.event({ type: "content", path, size: content.length, content: new TextDecoder().decode(content) });
684
+ return;
685
+ }
686
+ await new Promise((done, fail) => process.stdout.write(content, (error) => error ? fail(error) : done()));
687
+ }
688
+ });
689
+ var write = defineCommand4({
690
+ meta: { name: "write", description: "Write a file of a sandbox, from standard input or from a local file." },
691
+ args: {
692
+ ...commonArgs,
693
+ ...idArg,
694
+ path: { type: "positional", required: true, description: "The file in the sandbox. Its folders are created." },
695
+ from: { type: "string", description: "A local file to send. The default is standard input." }
696
+ },
697
+ async run({ args }) {
698
+ const from = str2(args.from);
699
+ const content = from ? await readFile2(from) : await readStdin();
700
+ const { client, out: out2 } = await context(args);
701
+ const path = String(args.path);
702
+ await client.writeFile(String(args.id), path, content);
703
+ out2.step("wrote", `${path} ${fileSize(content.length)}`);
704
+ out2.result("", { path, size: content.length });
705
+ }
706
+ });
707
+ var files = defineCommand4({
708
+ meta: { name: "files", description: "List, read and write the files of a sandbox." },
709
+ subCommands: { ls, read, write }
710
+ });
711
+
623
712
  // src/cli/commands/herdr.ts
624
713
  import { spawn as spawn2, spawnSync } from "child_process";
625
- import { defineCommand as defineCommand4 } from "citty";
626
- var herdr = defineCommand4({
714
+ import { defineCommand as defineCommand5 } from "citty";
715
+ var herdr = defineCommand5({
627
716
  meta: {
628
717
  name: "herdr",
629
718
  description: "Run coding agents on Munito sandboxes from herdr."
630
719
  },
631
720
  subCommands: {
632
- add: defineCommand4({
721
+ add: defineCommand5({
633
722
  meta: { name: "add", description: "Create a sandbox and add it to herdr as a machine." },
634
723
  args: {
635
724
  ...commonArgs,
@@ -681,11 +770,21 @@ var herdr = defineCommand4({
681
770
  });
682
771
 
683
772
  // src/cli/commands/init.ts
773
+ import { execFile as execFile2 } from "child_process";
684
774
  import { mkdir, writeFile as writeFile2 } from "fs/promises";
685
775
  import { homedir } from "os";
686
776
  import { dirname } from "path";
687
- import { defineCommand as defineCommand5 } from "citty";
688
- var init = defineCommand5({
777
+ import { promisify as promisify2 } from "util";
778
+ import { defineCommand as defineCommand6 } from "citty";
779
+ async function installedOpencode() {
780
+ try {
781
+ const { stdout } = await promisify2(execFile2)("opencode", ["--version"], { timeout: 1e4 });
782
+ return opencodeMajor(stdout);
783
+ } catch {
784
+ return null;
785
+ }
786
+ }
787
+ var init = defineCommand6({
689
788
  meta: {
690
789
  name: "init",
691
790
  description: "Point a coding agent on this machine at Munito AI."
@@ -718,6 +817,11 @@ var init = defineCommand5({
718
817
  out2.result("", { agent: name, plugin, guide });
719
818
  return;
720
819
  }
820
+ if (name === "opencode" && (await installedOpencode() ?? 2) < 2) {
821
+ throw new Error(
822
+ "munito init needs OpenCode 2, and this machine has OpenCode 1. See https://opencode.ai/docs/#cli"
823
+ );
824
+ }
721
825
  const wiring = wire(
722
826
  name,
723
827
  args.provider,
@@ -749,6 +853,9 @@ var init = defineCommand5({
749
853
  out2.line(` export ${key}=${shown}`);
750
854
  }
751
855
  }
856
+ if (name === "opencode" && exports.length) {
857
+ out2.line(out2.p.dim(" Then restart the server of OpenCode: opencode service restart"));
858
+ }
752
859
  if (wiring.unset.length && !args["dry-run"]) {
753
860
  out2.line();
754
861
  out2.line(out2.p.dim(` Clear these if your shell sets them: ${wiring.unset.join(", ")}`));
@@ -764,14 +871,14 @@ var init = defineCommand5({
764
871
  });
765
872
 
766
873
  // src/cli/commands/mcp.ts
767
- import { defineCommand as defineCommand6 } from "citty";
874
+ import { defineCommand as defineCommand7 } from "citty";
768
875
  function text(body) {
769
876
  return { content: [{ type: "text", text: typeof body === "string" ? body : JSON.stringify(body, null, 2) }] };
770
877
  }
771
878
  function failed(message) {
772
879
  return { ...text(message), isError: true };
773
880
  }
774
- var mcp = defineCommand6({
881
+ var mcp = defineCommand7({
775
882
  meta: {
776
883
  name: "mcp",
777
884
  description: "Serve the Munito AI tools over MCP on stdio, for a coding agent to call."
@@ -799,7 +906,7 @@ var mcp = defineCommand6({
799
906
  max_turns: z.number().int().positive().optional()
800
907
  }
801
908
  },
802
- (async ({ prompt, agent: agent2, provider, model, files, max_turns }) => {
909
+ (async ({ prompt, agent: agent2, provider, model, files: files2, max_turns }) => {
803
910
  try {
804
911
  const run3 = await runAgent(client, {
805
912
  agent: agent2,
@@ -807,7 +914,7 @@ var mcp = defineCommand6({
807
914
  provider,
808
915
  auth: "token",
809
916
  model,
810
- files,
917
+ files: files2,
811
918
  maxTurns: max_turns
812
919
  });
813
920
  return text({
@@ -834,18 +941,18 @@ var mcp = defineCommand6({
834
941
  timeout: z.number().int().positive().default(120).describe("Seconds the command may take.")
835
942
  }
836
943
  },
837
- (async ({ command, template, internet, timeout }) => {
944
+ (async ({ command, template, internet, timeout: timeout2 }) => {
838
945
  let id = "";
839
946
  try {
840
947
  const box = await client.createSandbox({
841
948
  template,
842
949
  size: "small",
843
950
  internet,
844
- timeout: Math.max(timeout + 60, 120),
951
+ timeout: Math.max(timeout2 + 60, 120),
845
952
  metadata: { "munito.cli": "mcp" }
846
953
  });
847
954
  id = box.id;
848
- const out2 = await client.exec(id, { command, timeout });
955
+ const out2 = await client.exec(id, { command, timeout: timeout2 });
849
956
  return text({ exit_code: out2.exit_code, stdout: out2.stdout, stderr: out2.stderr });
850
957
  } catch (error) {
851
958
  return failed(`The command did not run: ${error.message}`);
@@ -870,9 +977,168 @@ var mcp = defineCommand6({
870
977
  }
871
978
  });
872
979
 
980
+ // src/cli/commands/proxies.ts
981
+ import { defineCommand as defineCommand8 } from "citty";
982
+ var proxyArg = {
983
+ proxy: { type: "positional", required: true, description: "The name or the id of the proxy." }
984
+ };
985
+ var create = defineCommand8({
986
+ meta: { name: "create", description: "Create a proxy: fixed public addresses for your sandboxes." },
987
+ args: {
988
+ ...commonArgs,
989
+ name: { type: "positional", required: true, description: "The name: lower case letters, digits and hyphens." },
990
+ dedicated: { type: "boolean", description: "IPv4 addresses that no other customer uses, at a price." }
991
+ },
992
+ async run({ args }) {
993
+ const { client, out: out2 } = await context(args);
994
+ const proxy = await client.createProxy({ name: String(args.name), ipv4: args.dedicated ? "dedicated" : "shared" });
995
+ show(out2, proxy);
996
+ out2.line(out2.p.dim(` munito create --proxy ${proxy.name}`));
997
+ }
998
+ });
999
+ var ls2 = defineCommand8({
1000
+ meta: { name: "ls", description: "List the proxies of your workspace." },
1001
+ args: commonArgs,
1002
+ async run({ args }) {
1003
+ const { client, out: out2 } = await context(args);
1004
+ const proxies2 = await client.listProxies();
1005
+ for (const proxy of proxies2) out2.event({ type: "proxy", ...proxy });
1006
+ if (out2.mode === "json") return;
1007
+ if (!proxies2.length) {
1008
+ out2.line(out2.p.dim("No proxies. Create one with `munito proxies create <name>`."));
1009
+ return;
1010
+ }
1011
+ const rows = [
1012
+ ["NAME", "IPV4", "ADDRESSES", "IPV6 BLOCK", "OPEN PORTS"],
1013
+ ...proxies2.map((proxy) => [
1014
+ proxy.name,
1015
+ proxy.ipv4.mode,
1016
+ proxy.ipv4.addresses.join(" ") || "-",
1017
+ proxy.ipv6 ?? "-",
1018
+ String(proxy.ports.length)
1019
+ ])
1020
+ ];
1021
+ out2.line(table(rows, out2.p));
1022
+ }
1023
+ });
1024
+ var get = defineCommand8({
1025
+ meta: { name: "get", description: "Show a proxy, its addresses and its open ports." },
1026
+ args: { ...commonArgs, ...proxyArg },
1027
+ async run({ args }) {
1028
+ const { client, out: out2 } = await context(args);
1029
+ show(out2, await findProxy(client, String(args.proxy)));
1030
+ }
1031
+ });
1032
+ var rm2 = defineCommand8({
1033
+ meta: { name: "rm", description: "Delete proxies that no running or paused sandbox uses." },
1034
+ args: { ...commonArgs, proxies: { type: "positional", required: true, description: "One or more proxies." } },
1035
+ async run({ args }) {
1036
+ const { client, out: out2 } = await context(args);
1037
+ const refs = args._.length ? args._ : [String(args.proxies)];
1038
+ for (const ref of refs) {
1039
+ const proxy = await findProxy(client, ref);
1040
+ await client.deleteProxy(proxy.id);
1041
+ out2.event({ type: "proxy.deleted", id: proxy.id, name: proxy.name });
1042
+ out2.step("deleted", proxy.name);
1043
+ }
1044
+ }
1045
+ });
1046
+ var open = defineCommand8({
1047
+ meta: { name: "open", description: "Let the internet reach one port of a sandbox of the proxy." },
1048
+ args: {
1049
+ ...commonArgs,
1050
+ ...proxyArg,
1051
+ sandbox: { type: "positional", required: true, description: "The sandbox id." },
1052
+ port: { type: "positional", required: true, description: "The port that a server in the sandbox listens on." },
1053
+ udp: { type: "boolean", description: "A UDP port. The default is TCP." },
1054
+ "public-port": { type: "string", description: "The port outside, for a dedicated proxy. The default is the port." }
1055
+ },
1056
+ async run({ args }) {
1057
+ const { client, out: out2 } = await context(args);
1058
+ const proxy = await findProxy(client, String(args.proxy));
1059
+ const sandbox = String(args.sandbox);
1060
+ const opened = await client.openProxyPort(proxy.id, {
1061
+ sandbox_id: sandbox,
1062
+ port: Number(args.port),
1063
+ protocol: args.udp ? "udp" : "tcp",
1064
+ public_port: args["public-port"] ? Number(args["public-port"]) : void 0
1065
+ });
1066
+ const ipv6 = (await client.getSandbox(sandbox).catch(() => void 0))?.proxy?.ipv6;
1067
+ const reach = endpoints(proxy, opened, ipv6);
1068
+ out2.event({ type: "proxy.port", ...opened, endpoints: reach });
1069
+ out2.step("open", `${opened.protocol} ${opened.port} of ${sandbox}`);
1070
+ for (const endpoint of reach) out2.line(` ${endpoint}`);
1071
+ out2.result("", { port: opened.id, endpoints: reach });
1072
+ }
1073
+ });
1074
+ var close = defineCommand8({
1075
+ meta: { name: "close", description: "Close an open port of a proxy." },
1076
+ args: {
1077
+ ...commonArgs,
1078
+ ...proxyArg,
1079
+ port: { type: "positional", required: true, description: "The id of the open port, from `munito proxies get`." }
1080
+ },
1081
+ async run({ args }) {
1082
+ const { client, out: out2 } = await context(args);
1083
+ const proxy = await findProxy(client, String(args.proxy));
1084
+ await client.closeProxyPort(proxy.id, String(args.port));
1085
+ out2.event({ type: "proxy.port.closed", id: String(args.port) });
1086
+ out2.step("closed", String(args.port));
1087
+ }
1088
+ });
1089
+ function show(out2, proxy) {
1090
+ out2.event({ type: "proxy", ...proxy });
1091
+ out2.result("", { proxy: proxy.name, id: proxy.id, ipv4: proxy.ipv4.addresses, ipv6: proxy.ipv6 ?? null });
1092
+ if (out2.mode === "json") return;
1093
+ out2.step(proxy.name, proxy.ipv4.mode);
1094
+ const facts = [
1095
+ ["id", proxy.id],
1096
+ ["ipv4", proxy.ipv4.addresses.join(" ") || "-"],
1097
+ ["ipv6", proxy.ipv6 ?? "-"]
1098
+ ];
1099
+ const lines = table(facts, out2.p, false).split("\n");
1100
+ for (const line of lines) out2.line(` ${line}`);
1101
+ if (!proxy.ports.length) return;
1102
+ const rows = [
1103
+ ["PORT ID", "SANDBOX", "PROTOCOL", "PORT", "PUBLIC PORT"],
1104
+ ...proxy.ports.map((port2) => [
1105
+ port2.id,
1106
+ port2.sandbox_id,
1107
+ port2.protocol,
1108
+ String(port2.port),
1109
+ String(port2.public_port)
1110
+ ])
1111
+ ];
1112
+ for (const line of table(rows, out2.p).split("\n")) out2.line(` ${line}`);
1113
+ }
1114
+ var proxies = defineCommand8({
1115
+ meta: { name: "proxies", description: "Give sandboxes fixed public addresses. A preview." },
1116
+ subCommands: { create, ls: ls2, get, rm: rm2, open, close }
1117
+ });
1118
+
873
1119
  // src/cli/commands/sandboxes.ts
874
1120
  import { spawn as spawn3 } from "child_process";
875
- import { defineCommand as defineCommand7 } from "citty";
1121
+ import { defineCommand as defineCommand9 } from "citty";
1122
+
1123
+ // src/core/sandboxes.ts
1124
+ var LIVE_READ = ["running", "paused", "pending", "running"];
1125
+ async function liveSandboxes(client) {
1126
+ const byId = /* @__PURE__ */ new Map();
1127
+ for (const status of LIVE_READ) {
1128
+ for (const box of await client.listSandboxes({ status })) byId.set(box.id, box);
1129
+ }
1130
+ return [...byId.values()].sort((a, b) => a.id < b.id ? 1 : -1);
1131
+ }
1132
+ function timeoutSeconds(value) {
1133
+ if (!/^\d+$/.test(value.trim())) {
1134
+ throw new Error(`The timeout is a whole number of seconds, such as 3600. It was ${JSON.stringify(value)}.`);
1135
+ }
1136
+ return Number(value);
1137
+ }
1138
+ async function newestSandboxes(client, limit) {
1139
+ const boxes = await client.listSandboxes({ limit: limit + 1 });
1140
+ return { sandboxes: boxes.slice(0, limit), older: boxes.length > limit };
1141
+ }
876
1142
 
877
1143
  // src/core/ssh.ts
878
1144
  var LOOPBACK = "127.0.0.1";
@@ -908,28 +1174,43 @@ function sshArgv(user, host, forwards = []) {
908
1174
  }
909
1175
 
910
1176
  // src/cli/commands/sandboxes.ts
911
- var ls = defineCommand7({
1177
+ var ALL_LIMIT = 100;
1178
+ var ls3 = defineCommand9({
912
1179
  meta: { name: "ls", description: "List your sandboxes." },
913
- args: { ...commonArgs, all: { type: "boolean", alias: "a", description: "Stopped ones too." } },
1180
+ args: {
1181
+ ...commonArgs,
1182
+ all: { type: "boolean", alias: "a", description: "Stopped and failed ones too, the newest first." },
1183
+ limit: { type: "string", description: `The most sandboxes to list. With --all, the default is ${ALL_LIMIT}.` }
1184
+ },
914
1185
  async run({ args }) {
915
1186
  const { client, out: out2 } = await context(args);
916
- const boxes = await client.listSandboxes();
917
- const shown = args.all ? boxes : boxes.filter((box) => box.status !== "stopped");
1187
+ const limit = args.limit === void 0 ? void 0 : Number(args.limit);
1188
+ if (limit !== void 0 && (!Number.isInteger(limit) || limit < 1)) {
1189
+ throw new Error("--limit takes a whole number above 0.");
1190
+ }
1191
+ let shown;
1192
+ let older;
1193
+ if (args.all) {
1194
+ ({ sandboxes: shown, older } = await newestSandboxes(client, limit ?? ALL_LIMIT));
1195
+ } else {
1196
+ const live = await liveSandboxes(client);
1197
+ shown = live.slice(0, limit);
1198
+ older = live.length > shown.length;
1199
+ }
918
1200
  for (const box of shown) out2.event({ type: "sandbox", ...box });
919
- if (out2.mode === "json") return;
920
- if (!shown.length) {
921
- out2.line(out2.p.dim('No sandboxes. Start one with `munito agent "..."`.'));
922
- return;
1201
+ if (out2.mode !== "json") {
1202
+ if (shown.length) out2.line(table([["ID", "STATUS", "TEMPLATE", "SIZE", "AGE"], ...shown.map(row)], out2.p));
1203
+ else out2.line(out2.p.dim('No sandboxes. Start one with `munito agent "..."`.'));
923
1204
  }
924
- const rows = [["ID", "STATUS", "TEMPLATE", "SIZE", "AGE"], ...shown.map(row)];
925
- out2.line(table(rows, out2.p));
1205
+ if (older) out2.warn(`The list stops at ${shown.length}. Older sandboxes exist. Use --limit to get more.`);
926
1206
  }
927
1207
  });
1208
+ var PROXY_FLAG = "The name or the id of a proxy: the sandbox gets its fixed public addresses. A preview.";
928
1209
  function row(box) {
929
1210
  const age = box.created_at ? duration(Date.now() - box.created_at * 1e3) : "";
930
1211
  return [box.id, box.status, box.template, `${box.vcpus}c ${Math.round(box.memory_mib / 1024)}g`, age];
931
1212
  }
932
- var run2 = defineCommand7({
1213
+ var run2 = defineCommand9({
933
1214
  meta: { name: "run", description: "Run one command in a throwaway sandbox." },
934
1215
  args: {
935
1216
  ...commonArgs,
@@ -941,16 +1222,19 @@ var run2 = defineCommand7({
941
1222
  },
942
1223
  size: { type: "string", description: "tiny, small, default or large.", default: "small" },
943
1224
  internet: { type: "boolean", description: "Allow outbound connections." },
1225
+ proxy: { type: "string", description: PROXY_FLAG },
944
1226
  timeout: { type: "string", description: "Seconds the command may take." }
945
1227
  },
946
1228
  async run({ args }) {
947
1229
  const { client, out: out2 } = await context(args);
1230
+ const proxy = args.proxy ? (await findProxy(client, args.proxy)).id : void 0;
948
1231
  const box = await client.createSandbox({
949
1232
  template: args.template,
950
1233
  size: args.size,
951
1234
  internet: Boolean(args.internet),
952
1235
  timeout: 600,
953
- metadata: { "munito.cli": "run" }
1236
+ metadata: { "munito.cli": "run" },
1237
+ proxy
954
1238
  });
955
1239
  try {
956
1240
  const exit = await stream(client, box.id, args.command, out2, args.timeout);
@@ -961,7 +1245,7 @@ var run2 = defineCommand7({
961
1245
  }
962
1246
  }
963
1247
  });
964
- var exec = defineCommand7({
1248
+ var exec = defineCommand9({
965
1249
  meta: { name: "exec", description: "Run a command in a sandbox you already have." },
966
1250
  args: {
967
1251
  ...commonArgs,
@@ -976,12 +1260,12 @@ var exec = defineCommand7({
976
1260
  if (exit !== 0) process.exitCode = exit;
977
1261
  }
978
1262
  });
979
- async function stream(client, id, command, out2, timeout, cwd) {
1263
+ async function stream(client, id, command, out2, timeout2, cwd) {
980
1264
  let exit = 0;
981
1265
  const events = client.execStream(id, {
982
1266
  command,
983
1267
  cwd,
984
- timeout: timeout ? Number(timeout) : void 0
1268
+ timeout: timeout2 ? Number(timeout2) : void 0
985
1269
  });
986
1270
  for await (const event of events) {
987
1271
  if (event.type === "stdout") out2.raw(event.data);
@@ -991,7 +1275,7 @@ async function stream(client, id, command, out2, timeout, cwd) {
991
1275
  }
992
1276
  return exit;
993
1277
  }
994
- var rm2 = defineCommand7({
1278
+ var rm3 = defineCommand9({
995
1279
  meta: { name: "rm", description: "Delete sandboxes." },
996
1280
  args: { ...commonArgs, ids: { type: "positional", required: true, description: "One or more sandbox ids." } },
997
1281
  async run({ args, rawArgs: rawArgs2 }) {
@@ -1003,7 +1287,7 @@ var rm2 = defineCommand7({
1003
1287
  }
1004
1288
  }
1005
1289
  });
1006
- var pause = defineCommand7({
1290
+ var pause = defineCommand9({
1007
1291
  meta: { name: "pause", description: "Pause a persistent sandbox." },
1008
1292
  args: { ...commonArgs, id: { type: "positional", required: true, description: "The sandbox id." } },
1009
1293
  async run({ args }) {
@@ -1012,7 +1296,7 @@ var pause = defineCommand7({
1012
1296
  out2.step("paused", box.id);
1013
1297
  }
1014
1298
  });
1015
- var resume = defineCommand7({
1299
+ var resume = defineCommand9({
1016
1300
  meta: { name: "resume", description: "Resume a paused sandbox." },
1017
1301
  args: { ...commonArgs, id: { type: "positional", required: true, description: "The sandbox id." } },
1018
1302
  async run({ args }) {
@@ -1021,7 +1305,27 @@ var resume = defineCommand7({
1021
1305
  out2.step("resumed", box.id);
1022
1306
  }
1023
1307
  });
1024
- var fork = defineCommand7({
1308
+ var timeout = defineCommand9({
1309
+ meta: { name: "timeout", description: "Give a running sandbox a new end, in seconds from now." },
1310
+ args: {
1311
+ ...commonArgs,
1312
+ id: { type: "positional", required: true, description: "The sandbox id." },
1313
+ seconds: {
1314
+ type: "positional",
1315
+ required: true,
1316
+ description: "Seconds from now until the sandbox stops, or pauses when it is persistent."
1317
+ }
1318
+ },
1319
+ async run({ args }) {
1320
+ const seconds = timeoutSeconds(args.seconds);
1321
+ const { client, out: out2 } = await context(args);
1322
+ const box = await client.setSandboxTimeout(args.id, seconds);
1323
+ const end = box.expires_at ? new Date(box.expires_at * 1e3).toISOString().replace(".000Z", "Z") : void 0;
1324
+ out2.step("timeout", end ? `${box.id} ends at ${end}` : box.id);
1325
+ out2.result("", { sandbox_id: box.id, expires_at: box.expires_at });
1326
+ }
1327
+ });
1328
+ var fork = defineCommand9({
1025
1329
  meta: { name: "fork", description: "Start new sandboxes from a persistent one, as it is now." },
1026
1330
  args: {
1027
1331
  ...commonArgs,
@@ -1039,7 +1343,7 @@ var fork = defineCommand7({
1039
1343
  out2.result("", { forks: forks.map((box) => box.id) });
1040
1344
  }
1041
1345
  });
1042
- var ssh = defineCommand7({
1346
+ var ssh = defineCommand9({
1043
1347
  meta: { name: "ssh", description: "Open a shell in a sandbox." },
1044
1348
  args: {
1045
1349
  ...commonArgs,
@@ -1073,7 +1377,7 @@ var ssh = defineCommand7({
1073
1377
  );
1074
1378
  }
1075
1379
  });
1076
- var open = defineCommand7({
1380
+ var open2 = defineCommand9({
1077
1381
  meta: { name: "open", description: "Open a sandbox port, or its desktop, in your browser." },
1078
1382
  args: {
1079
1383
  ...commonArgs,
@@ -1096,7 +1400,7 @@ var open = defineCommand7({
1096
1400
  openInBrowser(signed.url);
1097
1401
  }
1098
1402
  });
1099
- var create = defineCommand7({
1403
+ var create2 = defineCommand9({
1100
1404
  meta: { name: "create", description: "Create a sandbox and leave it running." },
1101
1405
  args: {
1102
1406
  ...commonArgs,
@@ -1112,10 +1416,13 @@ var create = defineCommand7({
1112
1416
  size: { type: "string", description: "tiny, small, default or large.", default: "default" },
1113
1417
  internet: { type: "boolean", description: "Allow outbound connections.", default: true },
1114
1418
  persistent: { type: "boolean", description: "Keep files and processes across a pause." },
1419
+ proxy: { type: "string", description: PROXY_FLAG },
1115
1420
  timeout: { type: "string", description: "Seconds the sandbox may live." }
1116
1421
  },
1117
1422
  async run({ args }) {
1118
1423
  const { client, out: out2 } = await context(args);
1424
+ if (args.proxy && args.agent) throw new Error("A sandbox with --agent takes no --proxy yet.");
1425
+ const proxy = args.proxy ? (await findProxy(client, args.proxy)).id : void 0;
1119
1426
  const common = {
1120
1427
  template: args.template,
1121
1428
  size: args.size,
@@ -1130,26 +1437,27 @@ var create = defineCommand7({
1130
1437
  auth: args.auth,
1131
1438
  model: args.model,
1132
1439
  metadata: { "munito.cli": "create", "munito.agent": args.agent }
1133
- }) : await client.createSandbox({ ...common, metadata: { "munito.cli": "create" } });
1440
+ }) : await client.createSandbox({ ...common, metadata: { "munito.cli": "create" }, proxy });
1134
1441
  out2.step("created", box.id);
1442
+ if (box.proxy?.ipv6) out2.line(out2.p.dim(` ipv6 ${box.proxy.ipv6}`));
1135
1443
  out2.line(out2.p.dim(` munito ssh ${box.id}`));
1136
- out2.result("", { sandbox_id: box.id });
1444
+ out2.result("", { sandbox_id: box.id, ...box.proxy ? { proxy: box.proxy } : {} });
1137
1445
  }
1138
1446
  });
1139
1447
 
1140
1448
  // src/cli/commands/templates.ts
1141
- import { defineCommand as defineCommand8 } from "citty";
1142
- var str2 = (value) => typeof value === "string" && value ? value : void 0;
1449
+ import { defineCommand as defineCommand10 } from "citty";
1450
+ var str3 = (value) => typeof value === "string" && value ? value : void 0;
1143
1451
  var nameArg2 = {
1144
1452
  name: { type: "positional", required: true, description: "The name or the id of the template." }
1145
1453
  };
1146
- async function readStdin() {
1454
+ async function readStdin2() {
1147
1455
  if (process.stdin.isTTY) throw new TemplateError(`Pipe the token to --password-stdin, or set ${ENV_REGISTRY_TOKEN}.`);
1148
1456
  const chunks = [];
1149
1457
  for await (const chunk of process.stdin) chunks.push(Buffer.from(chunk));
1150
1458
  return Buffer.concat(chunks).toString("utf8");
1151
1459
  }
1152
- var deploy = defineCommand8({
1460
+ var deploy = defineCommand10({
1153
1461
  meta: {
1154
1462
  name: "deploy",
1155
1463
  description: "Build a container image into a template. A name that exists gets a new version."
@@ -1174,8 +1482,8 @@ var deploy = defineCommand8({
1174
1482
  throw new TemplateError(`There is no --password. Use --password-stdin, or set ${ENV_REGISTRY_TOKEN}.`);
1175
1483
  }
1176
1484
  const { auth, ignored } = registryAuth({
1177
- username: str2(args.username),
1178
- stdin: args["password-stdin"] ? await readStdin() : void 0,
1485
+ username: str3(args.username),
1486
+ stdin: args["password-stdin"] ? await readStdin2() : void 0,
1179
1487
  env: process.env[ENV_REGISTRY_TOKEN]
1180
1488
  });
1181
1489
  const body = deployBody(String(args.name), String(args.image), auth);
@@ -1192,7 +1500,7 @@ var deploy = defineCommand8({
1192
1500
  out2.result("", { template: template.name, id: template.id, version: template.build.version });
1193
1501
  }
1194
1502
  });
1195
- var ls2 = defineCommand8({
1503
+ var ls4 = defineCommand10({
1196
1504
  meta: { name: "ls", description: "List the templates of your workspace." },
1197
1505
  args: commonArgs,
1198
1506
  async run({ args }) {
@@ -1220,7 +1528,7 @@ function row2(template) {
1220
1528
  `v${build.version} ${build.status}`
1221
1529
  ];
1222
1530
  }
1223
- var get = defineCommand8({
1531
+ var get2 = defineCommand10({
1224
1532
  meta: { name: "get", description: "Show a template and its newest build." },
1225
1533
  args: {
1226
1534
  ...commonArgs,
@@ -1233,10 +1541,10 @@ var get = defineCommand8({
1233
1541
  await follow(client, out2, String(args.name));
1234
1542
  return;
1235
1543
  }
1236
- show(out2, await client.getTemplate(String(args.name)));
1544
+ show2(out2, await client.getTemplate(String(args.name)));
1237
1545
  }
1238
1546
  });
1239
- var rm3 = defineCommand8({
1547
+ var rm4 = defineCommand10({
1240
1548
  meta: { name: "rm", description: "Delete templates. Their running sandboxes keep running." },
1241
1549
  args: { ...commonArgs, names: { type: "positional", required: true, description: "One or more template names." } },
1242
1550
  async run({ args }) {
@@ -1256,10 +1564,10 @@ async function follow(client, out2, ref) {
1256
1564
  out2.step(event.status, `${event.name} v${event.version}`);
1257
1565
  }
1258
1566
  });
1259
- show(out2, template);
1567
+ show2(out2, template);
1260
1568
  if (template.build.status === "failed") process.exitCode = 1;
1261
1569
  }
1262
- function show(out2, template) {
1570
+ function show2(out2, template) {
1263
1571
  out2.event({ type: "template", ...template });
1264
1572
  const { build } = template;
1265
1573
  out2.result("", {
@@ -1287,14 +1595,14 @@ function show(out2, template) {
1287
1595
  for (const line of build.log.replace(/\n$/, "").split("\n")) out2.line(out2.p.dim(` ${line}`));
1288
1596
  }
1289
1597
  }
1290
- var templates = defineCommand8({
1598
+ var templates = defineCommand10({
1291
1599
  meta: { name: "templates", description: "Start sandboxes from your own container image." },
1292
- subCommands: { deploy, ls: ls2, get, rm: rm3 }
1600
+ subCommands: { deploy, ls: ls4, get: get2, rm: rm4 }
1293
1601
  });
1294
1602
 
1295
1603
  // src/cli/main.ts
1296
- var VERSION = true ? "0.3.0" : "0.0.0-dev";
1297
- var main = defineCommand9({
1604
+ var VERSION = true ? "0.5.0" : "0.0.0-dev";
1605
+ var main = defineCommand11({
1298
1606
  meta: {
1299
1607
  name: "munito",
1300
1608
  // Without this, `munito --version` exits with "No version specified".
@@ -1310,17 +1618,20 @@ var main = defineCommand9({
1310
1618
  login,
1311
1619
  logout,
1312
1620
  whoami,
1313
- ls,
1314
- create,
1621
+ ls: ls3,
1622
+ create: create2,
1315
1623
  run: run2,
1316
1624
  exec,
1317
1625
  ssh,
1318
- open,
1319
- rm: rm2,
1626
+ open: open2,
1627
+ rm: rm3,
1320
1628
  pause,
1321
1629
  resume,
1630
+ timeout,
1322
1631
  fork,
1323
- templates
1632
+ files,
1633
+ templates,
1634
+ proxies
1324
1635
  }
1325
1636
  });
1326
1637