feat(cli): add watch --block subcommand with documented exit codes

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mika Kuns
2026-05-20 16:29:38 +02:00
parent bc53daf6e6
commit 1c2c1d2f7e
2 changed files with 171 additions and 0 deletions

View File

@@ -289,6 +289,55 @@ program
}
});
program
.command("watch")
.description(
"Block until one message arrives for --name, print it, and exit. Designed to be run as a Claude Code background bash task so its output surfaces via BashOutput.",
)
.requiredOption("--name <name>", "Mailbox to watch")
.option("--block", "Long-poll for a message (default behavior; flag accepted for clarity)")
.option(
"--timeout <seconds>",
"Long-poll timeout in seconds (1..300, default 25)",
(v) => parseInt(v, 10),
25,
)
.option("--url <url>", "Daemon base URL", DEFAULT_URL)
.action(async (opts: { name: string; block?: boolean; timeout: number; url: string }) => {
const url = `${opts.url}/v1/watch?name=${encodeURIComponent(opts.name)}&timeout=${opts.timeout}`;
let res: Response;
try {
res = await fetch(url, { headers: { "X-Mailbox": opts.name, Accept: "application/json" } });
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
console.error(`Could not reach daemon at ${opts.url}: ${msg}`);
process.exit(2);
}
if (res.status === 204) {
process.exit(3);
}
if (res.status === 200) {
const body = (await res.json()) as { from: string; body: string; sentAt: string };
process.stdout.write(`[Claude-Mailbox] Mail from ${body.from}:\n${body.body}\n`);
process.exit(0);
}
if (res.status === 409) {
const body = (await res.json().catch(() => ({}))) as { to?: string };
const newName = body.to ?? "<unknown>";
process.stdout.write(
`[Claude-Mailbox] Mailbox renamed to '${newName}'. Restart watcher with --name ${newName}.\n`,
);
process.exit(0);
}
const text = await res.text().catch(() => "");
console.error(`watch failed: HTTP ${res.status}${text ? `${text}` : ""}`);
process.exit(1);
});
program
.command("mcp-stdio")
.description(