cartethyia 2.3.3 → 2.4.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.
Files changed (29) hide show
  1. package/dist/dashboard/assets/{BannedPage-B8MQBL1Y.js → BannedPage-CvnNj-tj.js} +1 -1
  2. package/dist/dashboard/assets/{CliToolDetailPage-DAAUVo7w.js → CliToolDetailPage-DdYuTZKs.js} +1 -1
  3. package/dist/dashboard/assets/{CliToolsPage--hLkg7LU.js → CliToolsPage-Ba55GO-L.js} +1 -1
  4. package/dist/dashboard/assets/{ConsoleLogPage-CRaToMW5.js → ConsoleLogPage-D-OQwsYE.js} +1 -1
  5. package/dist/dashboard/assets/{CustomizationPage-CYiaepa1.js → CustomizationPage-BMqN7mc4.js} +1 -1
  6. package/dist/dashboard/assets/{HealthEventsModal-CjsAm6Gm.js → HealthEventsModal-C5cygmiy.js} +1 -1
  7. package/dist/dashboard/assets/{LoginPage-DVL5oW3v.js → LoginPage-lI7CDb07.js} +1 -1
  8. package/dist/dashboard/assets/{OverviewPage-DJ8WM59t.js → OverviewPage-HaMJcZaC.js} +1 -1
  9. package/dist/dashboard/assets/{ProviderDetailPage-CK0q-HYZ.js → ProviderDetailPage-Pb-0zXVA.js} +1 -1
  10. package/dist/dashboard/assets/{ProvidersPage-BdVj-Vab.js → ProvidersPage-KifbSUkV.js} +1 -1
  11. package/dist/dashboard/assets/{ProxyPage-CTohaici.js → ProxyPage-DKLnIcne.js} +1 -1
  12. package/dist/dashboard/assets/{QuotaPage-CkHjJoim.js → QuotaPage-BzG3XTSg.js} +1 -1
  13. package/dist/dashboard/assets/{SetupPage-CRahC2bj.js → SetupPage-Cu42oonZ.js} +1 -1
  14. package/dist/dashboard/assets/{StudioPage-CxDqECuN.js → StudioPage-DIglSNvf.js} +1 -1
  15. package/dist/dashboard/assets/{UsagePage-TzfUYdQD.js → UsagePage-DQh2rUsY.js} +1 -1
  16. package/dist/dashboard/assets/{entry-KOZCZ9VG.js → entry-6PSJYbzI.js} +3 -3
  17. package/dist/dashboard/assets/{index-Bj96PK8B.js → index-Bysq3XgY.js} +2 -2
  18. package/dist/dashboard/index.html +1 -1
  19. package/package.json +2 -1
  20. package/scripts/cli/cli-launcher.cjs +12 -1
  21. package/scripts/cli/cli.ts +923 -794
  22. package/scripts/cli/menu.ts +107 -0
  23. package/scripts/cli/runtime-state.ts +78 -0
  24. package/scripts/cli/setup.ts +448 -443
  25. package/scripts/cli/version.ts +66 -0
  26. package/scripts/cli/wordmark.ts +58 -0
  27. package/src/boot/app.ts +39 -0
  28. package/src/console/auth/account-reset.ts +67 -0
  29. package/src/console/backup/export-endpoint.ts +111 -0
@@ -0,0 +1,107 @@
1
+ import Enquirer from "enquirer";
2
+
3
+ /**
4
+ * One row in a menu: an action the caller can run, or a rule between groups.
5
+ *
6
+ * Rules are rendered by `enquirer`'s own separator role and are never
7
+ * selectable, so the caller's action list stays a plain array indexed by the
8
+ * rows the operator can actually pick.
9
+ */
10
+ export type MenuRow =
11
+ | { readonly kind: "action"; readonly label: string; readonly hint?: string }
12
+ | { readonly kind: "rule" };
13
+
14
+ /**
15
+ * A single-select menu driven by the arrow keys and Enter.
16
+ *
17
+ * Built on `enquirer` rather than a hand-rolled raw-mode listener: a raw
18
+ * `process.stdin.on("keypress")` handler never fires unless something first
19
+ * calls `readline.emitKeypressEvents`, and the menu rendered as inert text for
20
+ * exactly that reason. `enquirer` owns the keypress plumbing, including Ctrl-C.
21
+ *
22
+ * `header` is printed once here and never handed to `enquirer`: enquirer
23
+ * redraws its whole frame on every keypress, so a header inside the prompt
24
+ * repeated once per keystroke and stacked the wordmark down the terminal.
25
+ *
26
+ * Returns -1 when stdin is not a TTY (piped, CI, or a non-interactive run) or
27
+ * when the operator cancels, so the caller can drain and exit either way.
28
+ */
29
+ export async function chooseFromMenu(
30
+ header: string | readonly string[],
31
+ rows: readonly MenuRow[],
32
+ ): Promise<number> {
33
+ if (!process.stdin.isTTY) return -1;
34
+
35
+ const headerLines = typeof header === "string" ? [header] : header;
36
+ process.stdout.write("\u001b[2J\u001b[H");
37
+ for (const line of headerLines) process.stdout.write(`${line}\n`);
38
+ process.stdout.write("\n");
39
+
40
+ // Enquirer indexes the rows it renders, rules included, so the action index
41
+ // has to be recovered from the label rather than from the position.
42
+ const labels = rows.flatMap((row) => (row.kind === "action" ? [row.label] : []));
43
+
44
+
45
+ try {
46
+ const answer = await Enquirer.prompt<{ choice: string }>({
47
+ type: "select",
48
+ name: "choice",
49
+ message: "Select an action",
50
+ choices: rows.map((row) =>
51
+ row.kind === "rule"
52
+ ? { name: "rule", message: "", role: "separator" }
53
+ : {
54
+ name: row.label,
55
+ message: row.label,
56
+ ...(row.hint === undefined ? {} : { hint: row.hint }),
57
+ },
58
+ ),
59
+ initial: 0,
60
+ });
61
+ return labels.indexOf(answer.choice);
62
+ } catch {
63
+ // Enquirer rejects on Esc and on its own Ctrl-C handling. Return -1 so the
64
+ // caller drains the gateway rather than leaving a detached process behind.
65
+ process.stdout.write("\u001b[2J\u001b[H");
66
+ return -1;
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Asks the operator to type an exact word before a destructive action runs.
72
+ *
73
+ * Enter alone must not be enough: the reset deletes every console credential,
74
+ * so the operator types the word rather than confirming a highlight. Returns
75
+ * false on cancel, on a mismatch, or when stdin is not a TTY.
76
+ */
77
+ export async function confirmByTyping(word: string, message: string): Promise<boolean> {
78
+ if (!process.stdin.isTTY) return false;
79
+ try {
80
+ const answer = await Enquirer.prompt<{ confirmation: string }>({
81
+ type: "input",
82
+ name: "confirmation",
83
+ message: `${message} (type ${word})`,
84
+ validate: (value: string) =>
85
+ value.trim() === word ? true : `type ${word} exactly to confirm, or Ctrl-C to cancel`,
86
+ });
87
+ return answer.confirmation.trim() === word;
88
+ } catch {
89
+ return false;
90
+ }
91
+ }
92
+
93
+ /** A yes/no gate for the step before the typed confirmation. */
94
+ export async function confirmStep(message: string): Promise<boolean> {
95
+ if (!process.stdin.isTTY) return false;
96
+ try {
97
+ const answer = await Enquirer.prompt<{ proceed: boolean }>({
98
+ type: "confirm",
99
+ name: "proceed",
100
+ message,
101
+ initial: false,
102
+ });
103
+ return answer.proceed;
104
+ } catch {
105
+ return false;
106
+ }
107
+ }
@@ -0,0 +1,78 @@
1
+ import { existsSync, readFileSync, writeFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+
4
+ /**
5
+ * The install's drain token, persisted in the config directory.
6
+ *
7
+ * Why it persists instead of being generated per run: `cartethyia stop` runs in
8
+ * a different process, possibly minutes later, and the token is the only way it
9
+ * can authenticate to a gateway it did not start. A token regenerated on each
10
+ * start could never match the gateway that is actually running, which is why
11
+ * `stop` reported "no gateway found" or a 401 on a live gateway.
12
+ *
13
+ * It is kept out of `.env` on purpose: it authorises a shutdown, so it belongs
14
+ * in a machine-written file the operator never hand-edits and never commits.
15
+ * Mode 0600 where the platform honours it.
16
+ */
17
+ const RUNTIME_STATE_FILE = "runtime.json";
18
+
19
+ interface RuntimeState {
20
+ readonly drainToken: string;
21
+ }
22
+
23
+ function statePath(): string | null {
24
+ const configDir = process.env.CARTETHYIA_CONFIG_DIR;
25
+ return configDir ? join(configDir, RUNTIME_STATE_FILE) : null;
26
+ }
27
+
28
+ function readState(): RuntimeState | null {
29
+ const path = statePath();
30
+ if (path === null || !existsSync(path)) return null;
31
+ try {
32
+ const parsed: unknown = JSON.parse(readFileSync(path, "utf8"));
33
+ if (
34
+ parsed !== null &&
35
+ typeof parsed === "object" &&
36
+ "drainToken" in parsed &&
37
+ typeof parsed.drainToken === "string" &&
38
+ parsed.drainToken.length > 0
39
+ ) {
40
+ return { drainToken: parsed.drainToken };
41
+ }
42
+ } catch {
43
+ // A corrupt or stale state file reads as "no token".
44
+ }
45
+ return null;
46
+ }
47
+
48
+ /**
49
+ * Returns the install's drain token, creating and persisting one on first use.
50
+ *
51
+ * Every start calls this, so a gateway started from the menu, from `tray`, or
52
+ * from `cartethyia start` all authenticate with the same token and `stop`
53
+ * always has the one that works.
54
+ */
55
+ export function ensureDrainToken(): string {
56
+ const existing = readState();
57
+ if (existing !== null) return existing.drainToken;
58
+
59
+ const token = crypto.randomUUID().replace(/-/g, "") + crypto.randomUUID().replace(/-/g, "");
60
+ const path = statePath();
61
+ if (path !== null) {
62
+ try {
63
+ writeFileSync(path, JSON.stringify({ drainToken: token }), {
64
+ encoding: "utf8",
65
+ mode: 0o600,
66
+ });
67
+ } catch {
68
+ // A read-only config directory still yields a usable in-process token;
69
+ // only `stop` from another process is lost.
70
+ }
71
+ }
72
+ return token;
73
+ }
74
+
75
+ /** The persisted token, or null when this install has never started. */
76
+ export function readPersistedDrainToken(): string | null {
77
+ return readState()?.drainToken ?? null;
78
+ }