mergestorm 0.3.7 → 0.3.9

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/README.md CHANGED
@@ -6,20 +6,21 @@ Requires **Node.js 22+**.
6
6
 
7
7
  ## Install
8
8
 
9
- One-liner (installs the global `mergestorm` binary, then sign in via your browser):
9
+ Recommended — install from npm (same package the one-liner uses):
10
10
 
11
11
  ```bash
12
- curl -fsSL https://mergestorm.ai/install.sh | bash
12
+ npm install -g mergestorm
13
+ mergestorm --version # confirm install
13
14
  mergestorm login
15
+ # or the short alias (same binary):
16
+ mg login
14
17
  ```
15
18
 
16
- Or install directly with npm:
19
+ Optional curl installer (checks Node 22+, then runs `npm install -g mergestorm`):
17
20
 
18
21
  ```bash
19
- npm install -g mergestorm
22
+ curl -fsSL https://mergestorm.ai/install.sh | bash
20
23
  mergestorm login
21
- # or the short alias (same binary):
22
- mg login
23
24
  ```
24
25
 
25
26
  After install, both `mergestorm` and `mg` invoke the same CLI.
@@ -27,7 +28,7 @@ After install, both `mergestorm` and `mg` invoke the same CLI.
27
28
  ## Source
28
29
 
29
30
  Public source (MIT): [github.com/marginsystems/mergestorm-cli](https://github.com/marginsystems/mergestorm-cli).
30
- Tags there match npm versions (`v0.3.7`, …).
31
+ Tags there match npm versions (`v0.3.9`, …). Clone, install, and build:
31
32
 
32
33
  ```bash
33
34
  git clone https://github.com/marginsystems/mergestorm-cli.git
@@ -35,25 +36,7 @@ cd mergestorm-cli
35
36
  npm install
36
37
  npm run build
37
38
  node dist/cli.js --help
38
- ```
39
-
40
- ### From this monorepo (development / local UI testing)
41
-
42
- You do **not** need to bump/publish npm to try shell layout changes. From the repo root:
43
-
44
- ```bash
45
- # Fastest — runs TypeScript via tsx (no build, no version bump):
46
- npm run dev --workspace=mergestorm
47
-
48
- # Or build once, then run the compiled binary:
49
- npm run build --workspace=mergestorm
50
- node packages/mergestorm-cli/dist/cli.js
51
-
52
- # Optional: expose a local binary as `mergestorm` without publishing
53
- npm run build --workspace=mergestorm
54
- npm link --workspace=mergestorm
55
- mergestorm # uses your local build
56
- # later: npm unlink -g mergestorm && npm install -g mergestorm
39
+ # optional: npm link # puts `mergestorm` / `mg` on your PATH
57
40
  ```
58
41
 
59
42
  ## Interactive shell
@@ -61,13 +44,13 @@ mergestorm # uses your local build
61
44
  On a TTY, bare `mergestorm` (or `mergestorm shell`) opens a branded REPL: a **full-width** welcome panel (logo + status on the left, tips / what's new on the right) and a **full-width** bordered input box with a live `/` slash-command dropdown. Non-TTY (CI/pipes) prints usage instead — no hanging prompt.
62
45
 
63
46
  ```
64
- ╭─ mergestorm v0.3.7 ──────────────────────────────────────────────────────────╮
47
+ ╭─ mergestorm v0.3.9 ──────────────────────────────────────────────────────────╮
65
48
  │ ▟██████████▛ Tips for getting started │
66
- │ ▜████████▛ review review main...HEAD │
49
+ │ ▜████████▛ review review origin/HEAD or main │
67
50
  │ ▝▜████▛▘ stack create → submit → restack → land │
68
51
  │ ▜██▛ /help list all commands │
69
52
  │ ██ │
70
- │ ▝▘ What's new in v0.3.7 │
53
+ │ ▝▘ What's new in v0.3.9 │
71
54
  │ ● msk… · maelstrom • MIT license + public source (mergestorm-cli) │
72
55
  │ [████░░░░░░░░░░] 12% used │
73
56
  ╰──────────────────────────────────────────────────────────────────────────────╯
@@ -75,7 +58,7 @@ mergestorm
75
58
  ╭──────────────────────────────────────────────────────────────────────────────╮
76
59
  │ › /re │
77
60
  ╰──────────────────────────────────────────────────────────────────────────────╯
78
- /review Review git diff (default main...HEAD)
61
+ /review Review git diff (default origin/HEAD or main or master)
79
62
 
80
63
  mergestorm> credits
81
64
  mergestorm> branches
@@ -97,8 +80,8 @@ mergestorm shell Explicit shell entry
97
80
  mergestorm login Sign in via browser; stores an API key in ~/.mergestorm/config.json
98
81
  mergestorm login --key Paste an existing API key instead (headless/CI)
99
82
  mergestorm logout Remove the stored API key
100
- mergestorm review [base] [head] Review git diff base...head (default: main...HEAD)
101
- mergestorm status <job_id> Poll a review job and print JSON
83
+ mergestorm review [base] [head] Review git diff base...head (default: origin/HEAD or main or master)
84
+ mergestorm status <job_id> Fetch a review job and print JSON (one shot)
102
85
  mergestorm credits [--json] Standard + premium credit balance (usage bars)
103
86
  mergestorm jobs [n] [--json] Recent review jobs (default 10, max 50)
104
87
  mergestorm branches [n] Recently reviewed branches (arrow-pick on TTY)
@@ -110,12 +93,12 @@ mergestorm stack submit Push layers, open chained PRs (`gh`), register v
110
93
  mergestorm stack list [--json] List registered stacks
111
94
  mergestorm stack adopt <owner/repo>#<pr> Import an existing open PR chain
112
95
  mergestorm stack restack <stack-id> Restack descendants
113
- mergestorm stack land <stack-id> Land the bottom open PR
114
- mergestorm stack auto-land on|off <stack-id> Toggle auto-land when green
96
+ mergestorm stack land <stack-id> Land / promote (into review unit when present)
97
+ mergestorm stack auto-promote on|off <stack-id> Toggle auto-promote when green
115
98
  mergestorm stack reset --force Clear local authoring state (not branches/PRs)
116
99
  ```
117
100
 
118
- `mg` is a short alias for `mergestorm` (same binary), e.g. `mg stack create`. Happy path: **create → commit → submit → restack → land**. `stack adopt` is for importing a chain that already exists on GitHub (legacy / Graphite). Restack/land use the same login key (`/api/v1/stacks`).
101
+ `mg` is a short alias for `mergestorm` (same binary), e.g. `mg stack create`. Happy path: **create → commit → submit → restack → land**. On review-unit stacks, `stack land` **promotes** the tip into the unit (same gates as the dashboard); otherwise it lands the bottom open PR. `stack auto-promote` turns on land-when-green for a registered stack (`auto-land` remains a deprecated alias). `stack adopt` is for importing a chain that already exists on GitHub (legacy / Graphite). Restack/land use the same login key (`/api/v1/stacks`).
119
102
 
120
103
  `stack create` is local-only: it checks out a new branch from the current tip (or `--onto <branch>`), discovers trunk (`main` / `master` / `origin/HEAD`, overridable with `--trunk`), and records `{ branch, parentBranch }` in CLI-managed state under **`~/.mergestorm/stacks/<repo-id>/stack.json`**. The CLI updates this automatically; never edit it. Linked worktrees share state, while independent clones remain isolated. Creating onto trunk with a non-empty active stack starts a new one. No repository file, GitHub call, or API call is made.
121
104
 
@@ -127,7 +110,7 @@ Legacy repo-local `.mergestorm/stack.json` state migrates automatically on the n
127
110
 
128
111
  Runs a browser device-authorization flow: prints a short code, opens `https://mergestorm.ai/cli/auth`, and — once you approve — mints and stores an API key in `~/.mergestorm/config.json` (mode `600`). No key copy/paste needed.
129
112
 
130
- For headless or CI environments, use `mergestorm login --key` to paste an existing `msk_live_…` key (create one on the dashboard [API tab](https://mergestorm.ai/dashboard/api)).
113
+ For headless or CI environments, use `mergestorm login --key` to paste an existing `msk_live_…` key (create one on the dashboard [Settings → API](https://mergestorm.ai/settings#api)). On a TTY the paste is hidden (not echoed); the CLI verifies the key with the API before saving.
131
114
 
132
115
  ### `logout`
133
116
 
@@ -135,10 +118,10 @@ Removes `~/.mergestorm/config.json` so the stored key is no longer used.
135
118
 
136
119
  ### `review [base] [head]`
137
120
 
138
- Collects `git diff base...head` (default `main...HEAD`), attaches the changed file contents (up to 40 files, skipping files > 400 KB), submits the job, and polls until it finishes (~3 min max). The thread slug is derived from the current branch (`local/<branch>`), so repeated runs on the same branch chain their findings. Programmatic callers can provide explicit review context through the Review Jobs API.
121
+ Collects `git diff base...head` (default: discovered trunk — `origin/HEAD`, `main`, or `master`), attaches the changed file contents (up to 40 files, skipping files > 400 KB), submits the job, and polls until it finishes (~3 min max). The thread slug is derived from the current branch (`local/<branch>`), so repeated runs on the same branch chain their findings. Programmatic callers can provide explicit review context through the Review Jobs API.
139
122
 
140
123
  ```bash
141
- mergestorm review # main...HEAD
124
+ mergestorm review # discovered trunk (origin/HEAD, main, or master)...HEAD
142
125
  mergestorm review develop # develop...HEAD
143
126
  mergestorm review main feat/x # main...feat/x
144
127
  ```
package/dist/api.d.ts CHANGED
@@ -1,11 +1,21 @@
1
1
  import { type Config } from "./config.js";
2
- export declare function apiFetch(cfg: Config, route: string, init?: RequestInit & {
2
+ /** Default per-request timeout so a hung API cannot stall the shell/CI forever. */
3
+ export declare const DEFAULT_API_TIMEOUT_MS = 30000;
4
+ /** Message prefix of the CommandError thrown when a request exceeds its timeout. */
5
+ export declare const API_TIMEOUT_PREFIX = "Request timed out after";
6
+ export type ApiFetchInit = RequestInit & {
3
7
  json?: unknown;
4
- }): Promise<{
8
+ /** Override {@link DEFAULT_API_TIMEOUT_MS}. */
9
+ timeoutMs?: number;
10
+ };
11
+ export declare function apiFetch(cfg: Config, route: string, init?: ApiFetchInit): Promise<{
5
12
  status: number;
6
13
  body: unknown;
7
14
  }>;
8
- export declare function devicePost(base: string, route: string, json: unknown): Promise<{
15
+ export declare function devicePost(base: string, route: string, json: unknown, opts?: {
16
+ timeoutMs?: number;
17
+ signal?: AbortSignal;
18
+ }): Promise<{
9
19
  status: number;
10
20
  body: any;
11
21
  }>;
@@ -79,7 +89,9 @@ export type ThreadDetail = {
79
89
  }[];
80
90
  };
81
91
  /** Fetch /api/v1/me; returns null on 404/network so callers can degrade. */
82
- export declare function getMe(cfg?: Config): Promise<MeResponse | null>;
92
+ export declare function getMe(cfg?: Config, opts?: {
93
+ timeoutMs?: number;
94
+ }): Promise<MeResponse | null>;
83
95
  export declare function listJobs(limit?: number, cfg?: Config): Promise<JobListItem[]>;
84
96
  export type StackLayerDto = {
85
97
  branch: string;
package/dist/api.js CHANGED
@@ -1,21 +1,77 @@
1
1
  import { apiBase, loadConfig, resolveApiKey } from "./config.js";
2
2
  import { CommandError } from "./errors.js";
3
+ /** Default per-request timeout so a hung API cannot stall the shell/CI forever. */
4
+ export const DEFAULT_API_TIMEOUT_MS = 30_000;
5
+ /** Message prefix of the CommandError thrown when a request exceeds its timeout. */
6
+ export const API_TIMEOUT_PREFIX = "Request timed out after";
7
+ function isAbortLike(err) {
8
+ return (err instanceof Error &&
9
+ (err.name === "AbortError" || err.name === "TimeoutError"));
10
+ }
11
+ /**
12
+ * Combine an optional caller signal with a ref'd timeout.
13
+ * (Built-in `AbortSignal.timeout` unrefs its timer, which can leave hung
14
+ * fetches without a live timer in edge cases / tests.)
15
+ */
16
+ function withRequestSignal(userSignal, timeoutMs) {
17
+ const ac = new AbortController();
18
+ const timer = setTimeout(() => {
19
+ ac.abort(new DOMException("The operation was aborted due to timeout", "TimeoutError"));
20
+ }, timeoutMs);
21
+ const onUserAbort = () => {
22
+ ac.abort(userSignal.reason);
23
+ };
24
+ if (userSignal) {
25
+ if (userSignal.aborted)
26
+ onUserAbort();
27
+ else
28
+ userSignal.addEventListener("abort", onUserAbort, { once: true });
29
+ }
30
+ return {
31
+ signal: ac.signal,
32
+ dispose: () => {
33
+ clearTimeout(timer);
34
+ userSignal?.removeEventListener("abort", onUserAbort);
35
+ },
36
+ };
37
+ }
38
+ /** Map hung-request aborts to CommandError; preserve caller cancel (Ctrl+C detach). */
39
+ function rethrowFetchAbort(err, timeoutMs) {
40
+ if (isAbortLike(err)) {
41
+ if (err.name === "AbortError")
42
+ throw err;
43
+ throw new CommandError(`${API_TIMEOUT_PREFIX} ${Math.round(timeoutMs / 1000)}s. Check network connectivity and API status.`, 1, "api_timeout");
44
+ }
45
+ throw err;
46
+ }
3
47
  export async function apiFetch(cfg, route, init = {}) {
4
48
  const key = resolveApiKey(cfg);
5
49
  if (!key) {
6
- throw new CommandError("No API key. Run `mergestorm login` or set MERGESTORM_API_KEY.");
50
+ throw new CommandError("No API key. Run `mergestorm login` or set MERGESTORM_API_KEY.", 1, "missing_api_key");
7
51
  }
52
+ const { json, timeoutMs = DEFAULT_API_TIMEOUT_MS, signal: userSignal, ...rest } = init;
8
53
  const headers = {
9
54
  Authorization: `Bearer ${key}`,
10
- ...init.headers,
55
+ ...rest.headers,
11
56
  };
12
- let body = init.body;
13
- if (init.json !== undefined) {
57
+ let body = rest.body;
58
+ if (json !== undefined) {
14
59
  headers["Content-Type"] = "application/json";
15
- body = JSON.stringify(init.json);
60
+ body = JSON.stringify(json);
61
+ }
62
+ const { signal, dispose } = withRequestSignal(userSignal, timeoutMs);
63
+ let res;
64
+ let text = "";
65
+ try {
66
+ res = await fetch(`${apiBase(cfg)}${route}`, { ...rest, headers, body, signal });
67
+ text = await res.text();
68
+ }
69
+ catch (err) {
70
+ rethrowFetchAbort(err, timeoutMs);
71
+ }
72
+ finally {
73
+ dispose();
16
74
  }
17
- const res = await fetch(`${apiBase(cfg)}${route}`, { ...init, headers, body });
18
- const text = await res.text();
19
75
  let parsed = text;
20
76
  try {
21
77
  parsed = JSON.parse(text);
@@ -23,15 +79,31 @@ export async function apiFetch(cfg, route, init = {}) {
23
79
  catch {
24
80
  // keep text
25
81
  }
82
+ if (res.status === 401) {
83
+ throw new CommandError("API key invalid or revoked. Run `mergestorm login`.", 1, "auth_invalid");
84
+ }
26
85
  return { status: res.status, body: parsed };
27
86
  }
28
- export async function devicePost(base, route, json) {
29
- const res = await fetch(`${base}${route}`, {
30
- method: "POST",
31
- headers: { "Content-Type": "application/json" },
32
- body: JSON.stringify(json),
33
- });
34
- const text = await res.text();
87
+ export async function devicePost(base, route, json, opts = {}) {
88
+ const timeoutMs = opts.timeoutMs ?? DEFAULT_API_TIMEOUT_MS;
89
+ const { signal, dispose } = withRequestSignal(opts.signal, timeoutMs);
90
+ let res;
91
+ let text = "";
92
+ try {
93
+ res = await fetch(`${base}${route}`, {
94
+ method: "POST",
95
+ headers: { "Content-Type": "application/json" },
96
+ body: JSON.stringify(json),
97
+ signal,
98
+ });
99
+ text = await res.text();
100
+ }
101
+ catch (err) {
102
+ rethrowFetchAbort(err, timeoutMs);
103
+ }
104
+ finally {
105
+ dispose();
106
+ }
35
107
  let parsed = text;
36
108
  try {
37
109
  parsed = JSON.parse(text);
@@ -42,17 +114,26 @@ export async function devicePost(base, route, json) {
42
114
  return { status: res.status, body: parsed };
43
115
  }
44
116
  /** Fetch /api/v1/me; returns null on 404/network so callers can degrade. */
45
- export async function getMe(cfg) {
117
+ export async function getMe(cfg, opts) {
46
118
  const resolved = cfg ?? (await loadConfig());
47
119
  try {
48
- const { status, body } = await apiFetch(resolved, "/api/v1/me");
120
+ const { status, body } = await apiFetch(resolved, "/api/v1/me", {
121
+ timeoutMs: opts?.timeoutMs,
122
+ });
49
123
  if (status === 404)
50
124
  return null;
51
125
  if (status !== 200)
52
126
  return null;
53
127
  return body;
54
128
  }
55
- catch {
129
+ catch (err) {
130
+ // 401 (and missing-key) must surface — do not misdiagnose as offline/old API.
131
+ // Timeouts degrade like other network failures (banner / soft probes).
132
+ if (err instanceof CommandError) {
133
+ if (err.code === "api_timeout")
134
+ return null;
135
+ throw err;
136
+ }
56
137
  return null;
57
138
  }
58
139
  }
package/dist/cli.js CHANGED
@@ -1,84 +1,32 @@
1
1
  #!/usr/bin/env node
2
2
  import { stdin as input, stdout as output } from "node:process";
3
- import { DEFAULT_API } from "./config.js";
4
3
  import { CommandError } from "./errors.js";
5
- import { cmdBranches, cmdChain } from "./commands/branches.js";
6
- import { cmdCredits } from "./commands/credits.js";
7
- import { cmdJobs, cmdThread } from "./commands/jobs.js";
8
- import { cmdLogin } from "./commands/login.js";
9
- import { cmdLogout } from "./commands/logout.js";
10
- import { cmdReview } from "./commands/review.js";
11
- import { cmdStack } from "./commands/stack.js";
12
- import { cmdStatus } from "./commands/status.js";
13
- import { cmdWhoami } from "./commands/whoami.js";
4
+ import { dispatchCommand, formatUsageBody, } from "./commands/registry.js";
5
+ import { isSupportedNode, unsupportedNodeMessage, } from "./node-version.js";
14
6
  import { runShell } from "./shell.js";
15
7
  import { ansi } from "./ui/ansi.js";
16
8
  import { printWordmark } from "./ui/banner.js";
9
+ import { cliVersion } from "./version.js";
10
+ if (!isSupportedNode()) {
11
+ console.error(unsupportedNodeMessage());
12
+ process.exit(1);
13
+ }
17
14
  function usage() {
18
15
  printWordmark();
19
16
  console.log(`Usage:
20
- mergestorm Interactive shell (TTY)
21
- mergestorm shell Explicit shell entry
22
- mergestorm login Sign in via browser and store an API key
23
- mergestorm login --key Paste an existing API key instead (headless/CI)
24
- mergestorm logout Remove the stored API key
25
- mergestorm review [base] [head] Review git diff base...head (default: main...HEAD)
26
- mergestorm status <job_id> Poll a review job (JSON)
27
- mergestorm credits [--json] Credit balance (usage bars)
28
- mergestorm jobs [n] [--json] Recent review jobs
29
- mergestorm branches [n] Recently reviewed branches (pick on TTY)
30
- mergestorm chain [slug] Branch review-chain timeline (default: current)
31
- mergestorm whoami [--json] Key + plan + API base
32
- mergestorm thread <slug> Jobs in a review thread
33
- mergestorm stack create [name] New local stack layer (CLI-managed authoring state)
34
- mergestorm stack submit Push active local stack, open PRs, register stack
35
- mergestorm stack reset --force Clear this repo's pre-submit authoring state
36
- mergestorm stack list [--json] List registered stacks
37
- mergestorm stack adopt <owner/repo>#<pr> Import an existing open PR chain
38
- mergestorm stack restack <stack-id> Restack stack descendants
39
- mergestorm stack land <stack-id> Land bottom PR (or promote into unit)
40
- mergestorm stack auto-promote on|off <stack-id> Toggle auto-promote when green
41
-
42
- mg is a short alias for mergestorm (same binary).
43
-
44
- Env:
45
- MERGESTORM_API_KEY API key (overrides config)
46
- MERGESTORM_API_URL API base (default ${DEFAULT_API})
17
+ ${formatUsageBody()}
47
18
  `);
48
19
  }
49
20
  async function runCommand(cmd, args) {
50
- if (cmd === "login")
51
- return cmdLogin(args);
52
- if (cmd === "logout")
53
- return cmdLogout();
54
- if (cmd === "review")
55
- return cmdReview(args);
56
- if (cmd === "status") {
57
- if (!args[0])
58
- throw new CommandError("usage: mergestorm status <job_id>");
59
- return cmdStatus(args[0], { format: "json" });
21
+ if (cmd === "help" || cmd === "?") {
22
+ usage();
23
+ return;
60
24
  }
61
- if (cmd === "credits" || cmd === "usage")
62
- return cmdCredits(args);
63
- if (cmd === "jobs")
64
- return cmdJobs(args);
65
- if (cmd === "branches" || cmd === "chains")
66
- return cmdBranches(args);
67
- if (cmd === "chain")
68
- return cmdChain(args);
69
- if (cmd === "whoami")
70
- return cmdWhoami(args);
71
- if (cmd === "thread") {
72
- if (!args[0])
73
- throw new CommandError("usage: mergestorm thread <slug>");
74
- return cmdThread(args[0], args.slice(1));
25
+ const ok = await dispatchCommand(cmd, args, { mode: "oneshot" });
26
+ if (!ok) {
27
+ usage();
28
+ throw new CommandError(`unknown command: ${cmd}`);
75
29
  }
76
- if (cmd === "stack")
77
- return cmdStack(args);
78
- if (cmd === "shell")
79
- return runShell();
80
- usage();
81
- throw new CommandError(`unknown command: ${cmd}`);
82
30
  }
83
31
  async function main() {
84
32
  const [, , cmd, ...args] = process.argv;
@@ -90,10 +38,14 @@ async function main() {
90
38
  usage();
91
39
  return;
92
40
  }
93
- if (cmd === "-h" || cmd === "--help") {
41
+ if (cmd === "-h" || cmd === "--help" || cmd === "help" || cmd === "?") {
94
42
  usage();
95
43
  return;
96
44
  }
45
+ if (cmd === "-v" || cmd === "--version" || cmd === "version") {
46
+ console.log(cliVersion());
47
+ return;
48
+ }
97
49
  try {
98
50
  await runCommand(cmd, args);
99
51
  }
@@ -45,7 +45,7 @@ export async function cmdJobs(args) {
45
45
  console.log(ansi.dim(fmt(header)));
46
46
  for (const r of rows)
47
47
  console.log(fmt(r));
48
- console.log(ansi.dim(" branches: `branches` · full history: https://mergestorm.ai/dashboard/api"));
48
+ console.log(ansi.dim(" branches: `branches` · full history: https://mergestorm.ai/settings#api"));
49
49
  }
50
50
  /** Numbered chain timeline for a thread (branch/PR meta when present). */
51
51
  export function formatThreadTimeline(data) {
@@ -1,2 +1,8 @@
1
- import { Interface } from "node:readline/promises";
2
- export declare function cmdLogin(args: string[], rl?: Interface): Promise<void>;
1
+ import { type Config } from "../config.js";
2
+ import { type MeResponse } from "../api.js";
3
+ /**
4
+ * Probe /api/v1/me with an in-memory config. Does not write disk.
5
+ * Throws CommandError when the key must not be saved.
6
+ */
7
+ export declare function validateApiKey(cfg: Config): Promise<MeResponse>;
8
+ export declare function cmdLogin(args: string[]): Promise<void>;
@@ -1,11 +1,10 @@
1
1
  import { spawn } from "node:child_process";
2
2
  import { platform } from "node:os";
3
- import { createInterface } from "node:readline/promises";
4
- import { stdin as input, stdout as output } from "node:process";
5
3
  import { apiBase, configPath, loadConfig, saveConfig } from "../config.js";
6
- import { devicePost } from "../api.js";
4
+ import { devicePost, getMe } from "../api.js";
7
5
  import { CommandError } from "../errors.js";
8
6
  import { ansi } from "../ui/ansi.js";
7
+ import { readSecretLine } from "../ui/secret-input.js";
9
8
  function openBrowser(url) {
10
9
  const os = platform();
11
10
  const cmd = os === "darwin" ? "open" : os === "win32" ? "cmd" : "xdg-open";
@@ -19,29 +18,42 @@ function openBrowser(url) {
19
18
  // URL is printed for manual open
20
19
  }
21
20
  }
22
- async function loginWithKey(existingRl) {
23
- if (!existingRl)
24
- input.resume();
25
- const rl = existingRl ?? createInterface({ input, output });
26
- const ownRl = !existingRl;
21
+ /**
22
+ * Probe /api/v1/me with an in-memory config. Does not write disk.
23
+ * Throws CommandError when the key must not be saved.
24
+ */
25
+ export async function validateApiKey(cfg) {
27
26
  try {
28
- const key = (await rl.question("Paste your Mergestorm API key (msk_live_...): ")).trim();
29
- if (!key.startsWith("msk_live_")) {
30
- throw new CommandError("Key should start with msk_live_");
27
+ const me = await getMe(cfg);
28
+ if (!me) {
29
+ throw new CommandError("Could not verify API key (API unreachable or too old). Key was not saved.");
31
30
  }
32
- const cfg = await loadConfig();
33
- cfg.apiKey = key;
34
- await saveConfig(cfg);
35
- console.log(`Saved to ${configPath()}`);
31
+ return me;
36
32
  }
37
- finally {
38
- if (ownRl)
39
- rl.close();
33
+ catch (err) {
34
+ if (err instanceof CommandError && err.code === "auth_invalid") {
35
+ throw new CommandError("API key invalid or revoked. Key was not saved. Check the key and run `mergestorm login --key` again.", 1, "auth_invalid");
36
+ }
37
+ throw err;
38
+ }
39
+ }
40
+ async function loginWithKey() {
41
+ // Always use muted secret input on TTY so the key never lands in
42
+ // question()/history echo.
43
+ const key = (await readSecretLine("Paste your Mergestorm API key (msk_live_...): ")).trim();
44
+ if (!key.startsWith("msk_live_")) {
45
+ throw new CommandError("Key should start with msk_live_");
40
46
  }
47
+ const cfg = await loadConfig();
48
+ cfg.apiKey = key;
49
+ const me = await validateApiKey(cfg);
50
+ await saveConfig(cfg);
51
+ const prefix = me.key?.prefix?.trim() || "msk_live_…";
52
+ console.log(`Verified ${prefix}… · saved to ${configPath()}`);
41
53
  }
42
- export async function cmdLogin(args, rl) {
54
+ export async function cmdLogin(args) {
43
55
  if (args.includes("--key")) {
44
- await loginWithKey(rl);
56
+ await loginWithKey();
45
57
  return;
46
58
  }
47
59
  const cfg = await loadConfig();
@@ -89,7 +101,7 @@ export async function cmdLogin(args, rl) {
89
101
  continue;
90
102
  }
91
103
  if (err === "key_limit_reached") {
92
- throw new CommandError("You have reached the max active API keys. Revoke one at https://mergestorm.ai/dashboard/api and try again.");
104
+ throw new CommandError("You have reached the max active API keys. Revoke one at https://mergestorm.ai/settings#api and try again.");
93
105
  }
94
106
  if (err === "expired_token") {
95
107
  throw new CommandError("Login code expired. Run `mergestorm login` again.");
@@ -0,0 +1,32 @@
1
+ import type { CommandSpec } from "../ui/prompt.js";
2
+ /** Shared dispatch context for oneshot argv and the interactive shell. */
3
+ export type CommandContext = {
4
+ mode: "oneshot" | "shell";
5
+ signal?: AbortSignal;
6
+ /** Shell: refresh banner after login/logout. */
7
+ afterAuth?: () => Promise<void>;
8
+ };
9
+ export type RegistryCommand = {
10
+ name: string;
11
+ /** Alternate names that resolve to the same handler (also listed in shell autocomplete). */
12
+ aliases?: string[];
13
+ /** Short line for shell `/help` and autocomplete. */
14
+ summary: string;
15
+ /** Lines under oneshot `Usage:` (already prefixed with `mergestorm …`). */
16
+ usage: string[];
17
+ run: (args: string[], ctx: CommandContext) => Promise<void>;
18
+ };
19
+ /**
20
+ * Single source of truth for CLI commands (STRUCT-01).
21
+ * Derive oneshot usage, shell COMMANDS, and both dispatchers from this table.
22
+ */
23
+ export declare const COMMAND_REGISTRY: RegistryCommand[];
24
+ export declare function findCommand(name: string): RegistryCommand | undefined;
25
+ /** Autocomplete / `/help` list for the interactive shell. */
26
+ export declare function shellCommandSpecs(): CommandSpec[];
27
+ export declare function formatUsageBody(): string;
28
+ /**
29
+ * Run a registered command. Returns false when `name` is unknown
30
+ * (caller prints usage / shell hint).
31
+ */
32
+ export declare function dispatchCommand(name: string, args: string[], ctx: CommandContext): Promise<boolean>;