@mrciphersmith/roomyx 0.4.0 → 0.6.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.
@@ -0,0 +1,68 @@
1
+ import { spawn } from "node:child_process";
2
+ import { dirname, join } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+
5
+ /**
6
+ * The launcher that stands between npm's `bin` and roomyx's TypeScript.
7
+ *
8
+ * `bin` used to point straight at a `.ts` file with a `#!/usr/bin/env bun`
9
+ * shebang. On a machine without Bun that fails as
10
+ * `/usr/bin/env: 'bun': No such file or directory` — not one byte of it from
11
+ * roomyx, and there is no way to make it so: a shebang is an `execve` dispatch
12
+ * handled by the kernel, with no slot for a diagnostic and no fallback. When
13
+ * the interpreter is not guaranteed present, the `bin` entry has to be a
14
+ * launcher rather than the program. Node is guaranteed present; npm just ran.
15
+ *
16
+ * `package.json`'s `engines.bun` does not help. npm's engine check only ever
17
+ * knew `node` and `npm` — a package declaring `bun: ">=99.0.0"` installs
18
+ * cleanly under `--engine-strict`. It is a comment that looks like a
19
+ * constraint.
20
+ *
21
+ * This does not transpile anything, and `src/*.ts` remains the shipped
22
+ * artifact. **It also never fetches, installs or bootstraps Bun.** Making an
23
+ * error message nicer by downloading a runtime at first use would reintroduce
24
+ * exactly the install-time network fetch that installer D-01 exists to refuse.
25
+ */
26
+
27
+ const MISSING_BUN = `roomyx needs Bun 1.1+ on your PATH.
28
+
29
+ It ships TypeScript and runs it directly — there is no build step, so Bun is
30
+ the interpreter rather than a build-time dependency.
31
+
32
+ Install: curl -fsSL https://bun.sh/install | bash
33
+ Docs: https://bun.sh
34
+ `;
35
+
36
+ /**
37
+ * @param {string} entryRelativeToBin - the TypeScript entry point, e.g. "../cli.ts"
38
+ */
39
+ export function launch(entryRelativeToBin) {
40
+ const entry = join(dirname(fileURLToPath(import.meta.url)), entryRelativeToBin);
41
+ const child = spawn("bun", [entry, ...process.argv.slice(2)], { stdio: "inherit" });
42
+
43
+ // Forwarded rather than relied upon. `roomyx serve` deregisters its room on
44
+ // SIGTERM, and a launcher that swallowed the signal would leave a registry
45
+ // entry pointing at a process that is gone — the failure this package
46
+ // already had once, from the other direction.
47
+ for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
48
+ process.on(signal, () => child.kill(signal));
49
+ }
50
+
51
+ child.on("error", (error) => {
52
+ if (error.code === "ENOENT") {
53
+ process.stderr.write(MISSING_BUN);
54
+ process.exit(1);
55
+ }
56
+ throw error;
57
+ });
58
+
59
+ child.on("exit", (code, signal) => {
60
+ if (signal) {
61
+ // Report death-by-signal the way a shell expects, rather than
62
+ // flattening it into an exit code the caller cannot distinguish.
63
+ process.kill(process.pid, signal);
64
+ return;
65
+ }
66
+ process.exit(code ?? 1);
67
+ });
68
+ }
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+ import { launch } from "./launch.mjs";
3
+
4
+ // The TUI's own entry, not `cli.ts client`. `client/index.ts` decides whether
5
+ // it was run directly via `import.meta.main`, and `cli.ts`'s `client`
6
+ // subcommand depends on that being false when it does `await import()`. A
7
+ // launcher pointed at the wrong one would run the client twice.
8
+ launch("../client/index.ts");
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ import { launch } from "./launch.mjs";
3
+
4
+ launch("../cli.ts");
@@ -0,0 +1,160 @@
1
+ /**
2
+ * One argument grammar for the whole CLI.
3
+ *
4
+ * There were two hand-rolled scanners with different semantics, and between
5
+ * them they produced this, measured against the published 0.4.0:
6
+ *
7
+ * roomyx serve --port 0 room.jsonl → served a file literally named "--port"
8
+ * roomyx serve --help → STARTED A SERVER
9
+ * roomyx serve room.jsonl --port → port 1, because Number(true) === 1
10
+ * roomyx serve room.jsonl --port abc → NaN, so a silent ephemeral port
11
+ * roomyx skills sync --dryrun → unknown flag, silently ignored
12
+ * roomyx --help → usage on stderr, exit 1
13
+ *
14
+ * Every one of those is the same defect: a scanner that never decided what a
15
+ * flag *is*, so it could not tell a value from a positional, a typo from an
16
+ * option, or a number from a boolean. Declaring the flags fixes the whole
17
+ * class, which is why this is a spec-driven parser rather than six patches.
18
+ *
19
+ * It matters beyond tidiness because an agent is a first-class user of this
20
+ * CLI, and the first thing an agent does with an unknown command is ask for
21
+ * `--help`.
22
+ */
23
+
24
+ export type FlagType = "string" | "number" | "boolean";
25
+
26
+ export interface FlagSpec {
27
+ type: FlagType;
28
+ describe: string;
29
+ }
30
+
31
+ export type FlagSpecs = Record<string, FlagSpec>;
32
+
33
+ export type FlagValues = Record<string, string | number | boolean>;
34
+
35
+ export interface ParsedArgs {
36
+ positionals: string[];
37
+ flags: FlagValues;
38
+ /** True when `--help`/`-h` appeared. Callers print help and exit 0. */
39
+ help: boolean;
40
+ }
41
+
42
+ /** Thrown for anything the caller should report and exit 1 on. */
43
+ export class ArgError extends Error {}
44
+
45
+ /** Levenshtein, for "unknown flag --dryrun; did you mean --dry-run?". */
46
+ function distance(a: string, b: string): number {
47
+ const rows = Array.from({ length: a.length + 1 }, (_, i) => [i, ...Array<number>(b.length).fill(0)]);
48
+ for (let j = 0; j <= b.length; j++) rows[0]![j] = j;
49
+ for (let i = 1; i <= a.length; i++) {
50
+ for (let j = 1; j <= b.length; j++) {
51
+ const substitution = rows[i - 1]![j - 1]! + (a[i - 1] === b[j - 1] ? 0 : 1);
52
+ rows[i]![j] = Math.min(rows[i - 1]![j]! + 1, rows[i]![j - 1]! + 1, substitution);
53
+ }
54
+ }
55
+ return rows[a.length]![b.length]!;
56
+ }
57
+
58
+ function suggest(unknown: string, known: string[]): string | undefined {
59
+ let best: { name: string; d: number } | undefined;
60
+ for (const name of known) {
61
+ const d = distance(unknown, name);
62
+ if (!best || d < best.d) best = { name, d };
63
+ }
64
+ // Two edits is close enough to be a typo and far enough not to be noise.
65
+ return best && best.d <= 2 ? best.name : undefined;
66
+ }
67
+
68
+ export function parseArgs(argv: string[], specs: FlagSpecs): ParsedArgs {
69
+ const positionals: string[] = [];
70
+ const flags: FlagValues = {};
71
+ const known = Object.keys(specs);
72
+ let help = false;
73
+ let sawTerminator = false;
74
+
75
+ for (let i = 0; i < argv.length; i++) {
76
+ const arg = argv[i] as string;
77
+
78
+ if (sawTerminator || !arg.startsWith("--")) {
79
+ if (arg === "-h" && !sawTerminator) {
80
+ help = true;
81
+ continue;
82
+ }
83
+ positionals.push(arg);
84
+ continue;
85
+ }
86
+
87
+ if (arg === "--") {
88
+ sawTerminator = true;
89
+ continue;
90
+ }
91
+
92
+ const eq = arg.indexOf("=");
93
+ const name = (eq === -1 ? arg.slice(2) : arg.slice(2, eq)).trim();
94
+ const inlineValue = eq === -1 ? undefined : arg.slice(eq + 1);
95
+
96
+ if (name === "help") {
97
+ help = true;
98
+ continue;
99
+ }
100
+
101
+ // `specs[name]` resolves through the prototype chain, so `--toString`,
102
+ // `--constructor`, `--valueOf`, `--hasOwnProperty` and `--__proto__` all
103
+ // found a truthy spec, skipped the refusal below, and consumed the next
104
+ // argv token as a value — the module's stated invariant failing for exactly
105
+ // the shape of input it exists to catch.
106
+ const spec = Object.hasOwn(specs, name) ? specs[name] : undefined;
107
+ if (!spec) {
108
+ const hint = suggest(name, known);
109
+ throw new ArgError(
110
+ `Unknown flag --${name}${hint ? `. Did you mean --${hint}?` : "."}` +
111
+ (known.length > 0 ? `\nKnown flags: ${known.map((f) => `--${f}`).join(", ")}` : ""),
112
+ );
113
+ }
114
+
115
+ if (spec.type === "boolean") {
116
+ if (inlineValue !== undefined && inlineValue !== "true" && inlineValue !== "false") {
117
+ throw new ArgError(`--${name} is a switch and takes no value (got "${inlineValue}").`);
118
+ }
119
+ flags[name] = inlineValue !== "false";
120
+ continue;
121
+ }
122
+
123
+ // A value is required, and the next argv entry is only a value if it does
124
+ // not look like a flag. `--port --host x` must fail rather than quietly
125
+ // read `--host` as the port.
126
+ let raw = inlineValue;
127
+ if (raw === undefined) {
128
+ const next = argv[i + 1];
129
+ if (next === undefined || next.startsWith("--")) {
130
+ throw new ArgError(`--${name} needs a value.`);
131
+ }
132
+ raw = next;
133
+ i++;
134
+ }
135
+ if (raw === "") {
136
+ throw new ArgError(`--${name} needs a value.`);
137
+ }
138
+
139
+ if (spec.type === "number") {
140
+ const value = Number(raw);
141
+ if (!Number.isFinite(value)) {
142
+ throw new ArgError(`--${name} needs a number (got "${raw}").`);
143
+ }
144
+ flags[name] = value;
145
+ continue;
146
+ }
147
+
148
+ flags[name] = raw;
149
+ }
150
+
151
+ return { positionals, flags, help };
152
+ }
153
+
154
+ /** Renders a spec as the flag table a `--help` prints. */
155
+ export function renderFlags(specs: FlagSpecs): string {
156
+ const names = Object.keys(specs);
157
+ if (names.length === 0) return "";
158
+ const width = Math.max(...names.map((n) => n.length));
159
+ return names.map((n) => ` --${n.padEnd(width)} ${specs[n]!.describe}`).join("\n");
160
+ }
@@ -0,0 +1,16 @@
1
+ import type { FlagSpecs } from "./args";
2
+
3
+ /**
4
+ * The client's flags, in one place because two entry points take them: the
5
+ * `roomyx-client` binary and `roomyx client`. They were two hand-rolled
6
+ * scanners with different semantics, which is how a bare `--registry` came to
7
+ * resolve to the empty string in one of them and to `true` in the other.
8
+ *
9
+ * This module deliberately imports nothing but a type, so `roomyx --version`
10
+ * does not pay for loading the TUI renderer.
11
+ */
12
+ export const CLIENT_FLAGS: FlagSpecs = {
13
+ room: { type: "string", describe: "Attach to a specific live room by id" },
14
+ connect: { type: "string", describe: "Attach to an explicit MCP URL; the registry is not consulted" },
15
+ registry: { type: "string", describe: "Registry file (default .roomyx/rooms/registry.json)" },
16
+ };