feat(node): add TypeScript sibling project for npm-based install
Introduces @kuns/claude-mailbox under node/, a wire-compatible TypeScript port of the .NET daemon that distributes via the public Gitea npm registry. The .NET project stays in src/ClaudeMailbox/ untouched; users pick whichever flavor they prefer. - node/ project: fastify + @modelcontextprotocol/sdk StreamableHTTPServerTransport + better-sqlite3, schema and wire surface match the C# version (port 47822, X-Mailbox header, MCP tool names, snake_case SQLite columns) - Cross-platform autostart: Scheduled Task (Win, no admin) / Windows Service (Win, --service) / launchd (mac) / systemd --user (linux) - 9/9 vitest tests pass; end-to-end /health + send/check round-trip verified - CI split: existing ci.yml/release.yml renamed to *-dotnet.yml with path filters, new ci-node.yml and release-node.yml publish to Gitea npm registry - install.ps1 / install.sh bootstrap one-liners at repo root; homebrew/ contains a tap formula template - README install section reordered: npm path primary, dotnet publish secondary Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
204
node/src/cli.ts
Normal file
204
node/src/cli.ts
Normal file
@@ -0,0 +1,204 @@
|
||||
#!/usr/bin/env node
|
||||
import { Command } from "commander";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { resolveConfig, baseUrl, DEFAULT_PORT } from "./config.js";
|
||||
import { startServer } from "./server.js";
|
||||
import { autostartManager } from "./autostart/index.js";
|
||||
|
||||
function readVersion(): string {
|
||||
try {
|
||||
const here = dirname(fileURLToPath(import.meta.url));
|
||||
const pkg = JSON.parse(readFileSync(join(here, "..", "package.json"), "utf8")) as {
|
||||
version?: string;
|
||||
};
|
||||
return pkg.version ?? "unknown";
|
||||
} catch {
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
const DEFAULT_URL = `http://127.0.0.1:${DEFAULT_PORT}`;
|
||||
|
||||
async function callJson(
|
||||
method: string,
|
||||
url: string,
|
||||
init: { headers?: Record<string, string>; body?: unknown } = {},
|
||||
): Promise<unknown> {
|
||||
const headers: Record<string, string> = { Accept: "application/json", ...(init.headers ?? {}) };
|
||||
let body: string | undefined;
|
||||
if (init.body !== undefined) {
|
||||
headers["Content-Type"] = "application/json";
|
||||
body = JSON.stringify(init.body);
|
||||
}
|
||||
const res = await fetch(url, { method, headers, body });
|
||||
const text = await res.text();
|
||||
if (!res.ok) {
|
||||
throw new Error(`${method} ${url} → ${res.status}: ${text}`);
|
||||
}
|
||||
return text.length ? JSON.parse(text) : null;
|
||||
}
|
||||
|
||||
function reportClientError(err: unknown, url: string): never {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
console.error(`Could not reach daemon at ${url}: ${msg}`);
|
||||
console.error("Is 'claude-mailbox serve' running?");
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const program = new Command();
|
||||
program
|
||||
.name("claude-mailbox")
|
||||
.description("MCP mail server that lets parallel Claude sessions coordinate.")
|
||||
.version(readVersion(), "-V, --version");
|
||||
|
||||
program
|
||||
.command("serve")
|
||||
.description("Run the daemon in the foreground.")
|
||||
.option("--port <port>", "Port to listen on", (v) => parseInt(v, 10))
|
||||
.option("--bind <address>", "Bind address")
|
||||
.option("--db-path <path>", "SQLite database path")
|
||||
.option("--config <path>", "Path to mailbox.json")
|
||||
.action(async (opts: { port?: number; bind?: string; dbPath?: string; config?: string }) => {
|
||||
const cfg = resolveConfig(opts);
|
||||
try {
|
||||
const { app } = await startServer(cfg);
|
||||
app.log.info(`ClaudeMailbox listening on ${baseUrl(cfg)} (db: ${cfg.dbPath})`);
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
if (/EADDRINUSE|already in use/i.test(msg)) {
|
||||
console.error(
|
||||
`Port ${cfg.port} is already in use. Another claude-mailbox instance may be running.`,
|
||||
);
|
||||
process.exit(3);
|
||||
}
|
||||
console.error(msg);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
program
|
||||
.command("send")
|
||||
.description("Send a message via REST.")
|
||||
.requiredOption("--to <name>", "Recipient mailbox")
|
||||
.requiredOption("--from <name>", "Sender mailbox (X-Mailbox header)")
|
||||
.requiredOption("--body <text>", "Message body")
|
||||
.option("--url <url>", "Daemon base URL", DEFAULT_URL)
|
||||
.action(async (opts: { to: string; from: string; body: string; url: string }) => {
|
||||
try {
|
||||
const out = await callJson("POST", `${opts.url}/v1/send`, {
|
||||
headers: { "X-Mailbox": opts.from },
|
||||
body: { to: opts.to, body: opts.body },
|
||||
});
|
||||
console.log(JSON.stringify(out, null, 2));
|
||||
} catch (err) {
|
||||
reportClientError(err, opts.url);
|
||||
}
|
||||
});
|
||||
|
||||
program
|
||||
.command("peek")
|
||||
.description("Non-consuming inbox status.")
|
||||
.requiredOption("--name <name>", "Mailbox name")
|
||||
.option("--url <url>", "Daemon base URL", DEFAULT_URL)
|
||||
.action(async (opts: { name: string; url: string }) => {
|
||||
try {
|
||||
const out = await callJson(
|
||||
"GET",
|
||||
`${opts.url}/v1/peek?name=${encodeURIComponent(opts.name)}`,
|
||||
);
|
||||
console.log(JSON.stringify(out, null, 2));
|
||||
} catch (err) {
|
||||
reportClientError(err, opts.url);
|
||||
}
|
||||
});
|
||||
|
||||
program
|
||||
.command("check")
|
||||
.description("Pull pending messages and mark delivered.")
|
||||
.requiredOption("--name <name>", "Mailbox name (also sent as X-Mailbox)")
|
||||
.option("--url <url>", "Daemon base URL", DEFAULT_URL)
|
||||
.action(async (opts: { name: string; url: string }) => {
|
||||
try {
|
||||
const out = await callJson(
|
||||
"POST",
|
||||
`${opts.url}/v1/check-inbox?name=${encodeURIComponent(opts.name)}`,
|
||||
{ headers: { "X-Mailbox": opts.name } },
|
||||
);
|
||||
console.log(JSON.stringify(out, null, 2));
|
||||
} catch (err) {
|
||||
reportClientError(err, opts.url);
|
||||
}
|
||||
});
|
||||
|
||||
program
|
||||
.command("list")
|
||||
.description("List known mailboxes.")
|
||||
.option("--url <url>", "Daemon base URL", DEFAULT_URL)
|
||||
.action(async (opts: { url: string }) => {
|
||||
try {
|
||||
const out = await callJson("GET", `${opts.url}/v1/list`);
|
||||
console.log(JSON.stringify(out, null, 2));
|
||||
} catch (err) {
|
||||
reportClientError(err, opts.url);
|
||||
}
|
||||
});
|
||||
|
||||
program
|
||||
.command("install-autostart")
|
||||
.description(
|
||||
"Register autostart for the current OS (Scheduled Task / launchd / systemd-user). Use --service on Windows for a Windows Service (admin).",
|
||||
)
|
||||
.option("--service", "Windows: install as a Windows Service (requires admin) instead of a Scheduled Task")
|
||||
.option("--port <port>", "Port to listen on", (v) => parseInt(v, 10))
|
||||
.option("--bind <address>", "Bind address")
|
||||
.option("--db-path <path>", "SQLite database path")
|
||||
.action(async (opts: { service?: boolean; port?: number; bind?: string; dbPath?: string }) => {
|
||||
const mgr = await autostartManager(opts.service ? "service" : "default");
|
||||
await mgr.install({ port: opts.port, bind: opts.bind, dbPath: opts.dbPath });
|
||||
console.log("Autostart installed.");
|
||||
});
|
||||
|
||||
program
|
||||
.command("uninstall-autostart")
|
||||
.description("Remove autostart for the current OS.")
|
||||
.option("--service", "Windows: uninstall the Windows Service variant")
|
||||
.option("--purge", "Also delete database and config")
|
||||
.action(async (opts: { service?: boolean; purge?: boolean }) => {
|
||||
const mgr = await autostartManager(opts.service ? "service" : "default");
|
||||
await mgr.uninstall(!!opts.purge);
|
||||
console.log("Autostart removed.");
|
||||
});
|
||||
|
||||
program
|
||||
.command("start")
|
||||
.description("Start the autostart-managed daemon.")
|
||||
.option("--service", "Windows: target the Windows Service variant")
|
||||
.action(async (opts: { service?: boolean }) => {
|
||||
const mgr = await autostartManager(opts.service ? "service" : "default");
|
||||
await mgr.start();
|
||||
});
|
||||
|
||||
program
|
||||
.command("stop")
|
||||
.description("Stop the autostart-managed daemon.")
|
||||
.option("--service", "Windows: target the Windows Service variant")
|
||||
.action(async (opts: { service?: boolean }) => {
|
||||
const mgr = await autostartManager(opts.service ? "service" : "default");
|
||||
await mgr.stop();
|
||||
});
|
||||
|
||||
program
|
||||
.command("status")
|
||||
.description("Print autostart status (Running | Stopped | NotInstalled).")
|
||||
.option("--service", "Windows: target the Windows Service variant")
|
||||
.action(async (opts: { service?: boolean }) => {
|
||||
const mgr = await autostartManager(opts.service ? "service" : "default");
|
||||
console.log(await mgr.status());
|
||||
});
|
||||
|
||||
program.parseAsync(process.argv).catch((err) => {
|
||||
console.error(err instanceof Error ? err.message : String(err));
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user