cartethyia 2.5.1 → 2.5.3

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 (235) hide show
  1. package/.env.example +5 -1
  2. package/dist/dashboard/assets/{AccountCooldown-ChpdzxRH.js → AccountCooldown-Bjo92Qat.js} +1 -1
  3. package/dist/dashboard/assets/AccountCooldown-Bjo92Qat.js.gz +0 -0
  4. package/dist/dashboard/assets/{BannedPage-ChqnTRpZ.js → BannedPage-hfCzi-Ux.js} +1 -1
  5. package/dist/dashboard/assets/BannedPage-hfCzi-Ux.js.gz +0 -0
  6. package/dist/dashboard/assets/CliToolDetailPage-DLGqYEwI.js +1 -0
  7. package/dist/dashboard/assets/CliToolDetailPage-DLGqYEwI.js.gz +0 -0
  8. package/dist/dashboard/assets/{CliToolsPage-BXUaRCQQ.js → CliToolsPage-DEDyKBlC.js} +1 -1
  9. package/dist/dashboard/assets/CliToolsPage-DEDyKBlC.js.gz +0 -0
  10. package/dist/dashboard/assets/CombosPage-CfuYiQ8Z.js +1 -0
  11. package/dist/dashboard/assets/CombosPage-CfuYiQ8Z.js.gz +0 -0
  12. package/dist/dashboard/assets/ConsoleLogPage-bsLuZmYH.js +1 -0
  13. package/dist/dashboard/assets/ConsoleLogPage-bsLuZmYH.js.gz +0 -0
  14. package/dist/dashboard/assets/CustomizationPage-_UWhwziJ.js +1 -0
  15. package/dist/dashboard/assets/CustomizationPage-_UWhwziJ.js.gz +0 -0
  16. package/dist/dashboard/assets/HealthEventsModal-Cjfc5WFS.js +1 -0
  17. package/dist/dashboard/assets/HealthEventsModal-Cjfc5WFS.js.gz +0 -0
  18. package/dist/dashboard/assets/LoginPage-DDVIfZGW.js +1 -0
  19. package/dist/dashboard/assets/LoginPage-DDVIfZGW.js.gz +0 -0
  20. package/dist/dashboard/assets/ModelPicker-CWkTUtpX.js +1 -0
  21. package/dist/dashboard/assets/ModelPicker-CWkTUtpX.js.gz +0 -0
  22. package/dist/dashboard/assets/OverviewPage-B53YYMng.js +1 -0
  23. package/dist/dashboard/assets/OverviewPage-B53YYMng.js.gz +0 -0
  24. package/dist/dashboard/assets/ProviderDetailPage-B0KaAQf0.js +3 -0
  25. package/dist/dashboard/assets/ProviderDetailPage-B0KaAQf0.js.gz +0 -0
  26. package/dist/dashboard/assets/ProvidersPage-Dx31DgIO.js +1 -0
  27. package/dist/dashboard/assets/ProvidersPage-Dx31DgIO.js.gz +0 -0
  28. package/dist/dashboard/assets/ProxyPage-BpnR4kXT.js +7 -0
  29. package/dist/dashboard/assets/ProxyPage-BpnR4kXT.js.gz +0 -0
  30. package/dist/dashboard/assets/QuotaPage-BpOQCnun.js +1 -0
  31. package/dist/dashboard/assets/QuotaPage-BpOQCnun.js.gz +0 -0
  32. package/dist/dashboard/assets/SettingsPage-DV1eo2J_.js +1 -0
  33. package/dist/dashboard/assets/SettingsPage-DV1eo2J_.js.gz +0 -0
  34. package/dist/dashboard/assets/SetupPage-D6pmQ86L.js +1 -0
  35. package/dist/dashboard/assets/SetupPage-D6pmQ86L.js.gz +0 -0
  36. package/dist/dashboard/assets/SortableList-DWvKiuSl.js +1 -0
  37. package/dist/dashboard/assets/SortableList-DWvKiuSl.js.gz +0 -0
  38. package/dist/dashboard/assets/{StudioPage-DqK5E26W.js → StudioPage-CmkUrgdj.js} +3 -3
  39. package/dist/dashboard/assets/StudioPage-CmkUrgdj.js.gz +0 -0
  40. package/dist/dashboard/assets/UsagePage-B47uOydr.js +56 -0
  41. package/dist/dashboard/assets/UsagePage-B47uOydr.js.gz +0 -0
  42. package/dist/dashboard/assets/{api-keys-BmDbsior.js → api-keys-uNywYlfv.js} +1 -1
  43. package/dist/dashboard/assets/api-keys-uNywYlfv.js.gz +0 -0
  44. package/dist/dashboard/assets/{app-C-Y4oZT0.js → app-CafWtVXA.js} +1 -1
  45. package/dist/dashboard/assets/{app-C-Y4oZT0.js.gz → app-CafWtVXA.js.gz} +0 -0
  46. package/dist/dashboard/assets/app-DxivpfYG.css +2 -0
  47. package/dist/dashboard/assets/app-DxivpfYG.css.gz +0 -0
  48. package/dist/dashboard/assets/{assert-BHfaei-X.js → assert-NHwYq95t.js} +1 -1
  49. package/dist/dashboard/assets/{assert-BHfaei-X.js.gz → assert-NHwYq95t.js.gz} +0 -0
  50. package/dist/dashboard/assets/badge-6uzDDvqs.js +1 -0
  51. package/dist/dashboard/assets/badge-6uzDDvqs.js.gz +0 -0
  52. package/dist/dashboard/assets/banner-4_WNg2WZ.js +1 -0
  53. package/dist/dashboard/assets/banner-4_WNg2WZ.js.gz +0 -0
  54. package/dist/dashboard/assets/card--AtFedAq.js +1 -0
  55. package/dist/dashboard/assets/card--AtFedAq.js.gz +0 -0
  56. package/dist/dashboard/assets/{cli-tools-B0wBq-RL.js → cli-tools-B3e7dx3N.js} +1 -1
  57. package/dist/dashboard/assets/cli-tools-B3e7dx3N.js.gz +0 -0
  58. package/dist/dashboard/assets/clipboard-button-NreUx6c8.js +1 -0
  59. package/dist/dashboard/assets/clipboard-button-NreUx6c8.js.gz +0 -0
  60. package/dist/dashboard/assets/dialog-BtpsJU98.js +1 -0
  61. package/dist/dashboard/assets/dialog-BtpsJU98.js.gz +0 -0
  62. package/dist/dashboard/assets/drawer-WwwtzihF.js +1 -0
  63. package/dist/dashboard/assets/drawer-WwwtzihF.js.gz +0 -0
  64. package/dist/dashboard/assets/entry-CkryrIea.js +4 -0
  65. package/dist/dashboard/assets/entry-CkryrIea.js.gz +0 -0
  66. package/dist/dashboard/assets/entry-TJIye_jT.js +1 -0
  67. package/dist/dashboard/assets/entry-TJIye_jT.js.gz +0 -0
  68. package/dist/dashboard/assets/{entry-D4O2229X.js → entry-jy9_4-Ke.js} +1 -1
  69. package/dist/dashboard/assets/entry-jy9_4-Ke.js.gz +0 -0
  70. package/dist/dashboard/assets/github-badge-D_eM3IgG.js +1 -0
  71. package/dist/dashboard/assets/github-badge-D_eM3IgG.js.gz +0 -0
  72. package/dist/dashboard/assets/{index-CcD1eSJ6.js → index-BydyKaDz.js} +2 -2
  73. package/dist/dashboard/assets/index-BydyKaDz.js.gz +0 -0
  74. package/dist/dashboard/assets/inline-BcNGF8Gr.js +1 -0
  75. package/dist/dashboard/assets/inline-BcNGF8Gr.js.gz +0 -0
  76. package/dist/dashboard/assets/input-BG41et9G.js +1 -0
  77. package/dist/dashboard/assets/input-BG41et9G.js.gz +0 -0
  78. package/dist/dashboard/assets/layout-C8mer_Ak.js +1 -0
  79. package/dist/dashboard/assets/layout-C8mer_Ak.js.gz +0 -0
  80. package/dist/dashboard/assets/list-row-Gh_W4OR_.js +1 -0
  81. package/dist/dashboard/assets/list-row-Gh_W4OR_.js.gz +0 -0
  82. package/dist/dashboard/assets/{live-yfEJFF9T.js → live-BJGYRBnR.js} +1 -1
  83. package/dist/dashboard/assets/live-BJGYRBnR.js.gz +0 -0
  84. package/dist/dashboard/assets/{model-bans-CT-TxSNX.js → model-bans-DeVLDA7I.js} +1 -1
  85. package/dist/dashboard/assets/model-bans-DeVLDA7I.js.gz +0 -0
  86. package/dist/dashboard/assets/numbers-Bs4LOOuf.js +2 -0
  87. package/dist/dashboard/assets/numbers-Bs4LOOuf.js.gz +0 -0
  88. package/dist/dashboard/assets/{routing-Cs6jl49L.js → routing-CcusqXA7.js} +1 -1
  89. package/dist/dashboard/assets/routing-CcusqXA7.js.gz +0 -0
  90. package/dist/dashboard/assets/select-BO6h8yNv.js +41 -0
  91. package/dist/dashboard/assets/select-BO6h8yNv.js.gz +0 -0
  92. package/dist/dashboard/assets/{settings-CfcTcOyU.js → settings-mpmXTgUa.js} +1 -1
  93. package/dist/dashboard/assets/settings-mpmXTgUa.js.gz +0 -0
  94. package/dist/dashboard/assets/stack-DwLmjUFK.js +1 -0
  95. package/dist/dashboard/assets/stack-DwLmjUFK.js.gz +0 -0
  96. package/dist/dashboard/assets/state-DYdQmdAl.js +1 -0
  97. package/dist/dashboard/assets/state-DYdQmdAl.js.gz +0 -0
  98. package/dist/dashboard/assets/switch-CidSfbG7.js +1 -0
  99. package/dist/dashboard/assets/switch-CidSfbG7.js.gz +0 -0
  100. package/dist/dashboard/assets/{useMutation-BQKMcJ1H.js → useMutation-w4u4CVue.js} +1 -1
  101. package/dist/dashboard/assets/useMutation-w4u4CVue.js.gz +0 -0
  102. package/dist/dashboard/index.html +1 -1
  103. package/dist/dashboard/index.html.gz +0 -0
  104. package/dist/dashboard/precompressed-assets.json +51 -54
  105. package/package.json +1 -1
  106. package/scripts/cli/command/console-menu.ts +248 -246
  107. package/scripts/cli/command/tray.ts +208 -204
  108. package/src/boot/app.ts +3 -2
  109. package/src/config/spec.ts +26 -2
  110. package/src/console/backup/routes.ts +52 -1
  111. package/src/console/backup/service.ts +60 -4
  112. package/src/console/observability/contracts.ts +11 -1
  113. package/src/console/observability/store.ts +9 -6
  114. package/src/console/share/share-router.ts +6 -9
  115. package/src/observability/payload-capture.ts +366 -344
  116. package/src/observability/payload-store.ts +12 -2
  117. package/src/observability/telemetry-status.ts +28 -16
  118. package/src/persistence/stores/api-key-store.ts +6 -0
  119. package/src/persistence/stores/share-store.ts +583 -556
  120. package/src/persistence/stores/telemetry-store.ts +1 -1
  121. package/src/protocol/response/gemini.ts +18 -4
  122. package/src/providers/discovery/openrouter-free-roster.ts +3 -11
  123. package/src/providers/discovery/probe-phases.ts +42 -5
  124. package/src/providers/discovery/probing-service.ts +25 -2
  125. package/src/providers/integrations/antigravity/antigravity-quota.ts +11 -3
  126. package/src/providers/integrations/buddy/buddy-catalog-shared.ts +4 -4
  127. package/src/providers/integrations/buddy/workbuddy/shared.ts +201 -200
  128. package/src/providers/integrations/claude/claude.ts +3 -1
  129. package/src/providers/integrations/cline/cline-quota.ts +8 -11
  130. package/src/providers/integrations/cloudflareai/cloudflareai-models.ts +3 -1
  131. package/src/providers/integrations/inferhub.ts +3 -1
  132. package/src/providers/integrations/kilo/kilo.ts +0 -1
  133. package/src/providers/integrations/kiro/kiro-catalog.ts +8 -2
  134. package/src/providers/integrations/opencode/provider.ts +532 -532
  135. package/src/providers/integrations/openrouter/provider.ts +5 -15
  136. package/src/providers/integrations/tokenharbor.ts +2 -2
  137. package/src/providers/integrations/trae/trae-catalog.ts +5 -3
  138. package/src/providers/operations/account-health-service.ts +23 -0
  139. package/src/providers/operations/client-versions.ts +9 -2
  140. package/src/providers/provider-metadata.ts +7 -1
  141. package/src/security/api-key-auth.ts +23 -0
  142. package/src/transport/dispatch/attempt-finalize.ts +46 -5
  143. package/src/transport/dispatch/proxy-request.ts +9 -0
  144. package/src/transport/dispatch/streaming/stream.ts +16 -2
  145. package/src/transport/guards/auth-guard.ts +2 -3
  146. package/src/transport/routing/route-catalog.ts +806 -799
  147. package/dist/dashboard/assets/AccountCooldown-ChpdzxRH.js.gz +0 -0
  148. package/dist/dashboard/assets/BannedPage-ChqnTRpZ.js.gz +0 -0
  149. package/dist/dashboard/assets/CliToolDetailPage-Bb9CNuoh.js +0 -1
  150. package/dist/dashboard/assets/CliToolDetailPage-Bb9CNuoh.js.gz +0 -0
  151. package/dist/dashboard/assets/CliToolsPage-BXUaRCQQ.js.gz +0 -0
  152. package/dist/dashboard/assets/CombosPage-CisNZOUt.js +0 -1
  153. package/dist/dashboard/assets/CombosPage-CisNZOUt.js.gz +0 -0
  154. package/dist/dashboard/assets/ConsoleLogPage-BOpI_LOH.js +0 -1
  155. package/dist/dashboard/assets/ConsoleLogPage-BOpI_LOH.js.gz +0 -0
  156. package/dist/dashboard/assets/CustomizationPage-DAkdvJ4k.js +0 -1
  157. package/dist/dashboard/assets/CustomizationPage-DAkdvJ4k.js.gz +0 -0
  158. package/dist/dashboard/assets/HealthEventsModal-DeT-VXHd.js +0 -1
  159. package/dist/dashboard/assets/HealthEventsModal-DeT-VXHd.js.gz +0 -0
  160. package/dist/dashboard/assets/LoginPage-CytxSeGP.js +0 -1
  161. package/dist/dashboard/assets/LoginPage-CytxSeGP.js.gz +0 -0
  162. package/dist/dashboard/assets/ModelPicker-oQ-SDXp8.js +0 -1
  163. package/dist/dashboard/assets/ModelPicker-oQ-SDXp8.js.gz +0 -0
  164. package/dist/dashboard/assets/OverviewPage-CU7S1wJs.js +0 -1
  165. package/dist/dashboard/assets/OverviewPage-CU7S1wJs.js.gz +0 -0
  166. package/dist/dashboard/assets/ProviderDetailPage-DLm2baot.js +0 -3
  167. package/dist/dashboard/assets/ProviderDetailPage-DLm2baot.js.gz +0 -0
  168. package/dist/dashboard/assets/ProvidersPage-DOXfuzfL.js +0 -1
  169. package/dist/dashboard/assets/ProvidersPage-DOXfuzfL.js.gz +0 -0
  170. package/dist/dashboard/assets/ProxyPage-wvk0ciQN.js +0 -7
  171. package/dist/dashboard/assets/ProxyPage-wvk0ciQN.js.gz +0 -0
  172. package/dist/dashboard/assets/QuotaPage-Cn_fcUHE.js +0 -1
  173. package/dist/dashboard/assets/QuotaPage-Cn_fcUHE.js.gz +0 -0
  174. package/dist/dashboard/assets/SettingsPage-8-eVadhc.js +0 -1
  175. package/dist/dashboard/assets/SettingsPage-8-eVadhc.js.gz +0 -0
  176. package/dist/dashboard/assets/SetupPage-uEIXPyGO.js +0 -1
  177. package/dist/dashboard/assets/SetupPage-uEIXPyGO.js.gz +0 -0
  178. package/dist/dashboard/assets/SortableList-DCY0TPTZ.js +0 -1
  179. package/dist/dashboard/assets/SortableList-DCY0TPTZ.js.gz +0 -0
  180. package/dist/dashboard/assets/StudioPage-DqK5E26W.js.gz +0 -0
  181. package/dist/dashboard/assets/UsagePage-38JfiZ0H.js +0 -56
  182. package/dist/dashboard/assets/UsagePage-38JfiZ0H.js.gz +0 -0
  183. package/dist/dashboard/assets/api-keys-BmDbsior.js.gz +0 -0
  184. package/dist/dashboard/assets/app-BbpSarVV.css +0 -2
  185. package/dist/dashboard/assets/app-BbpSarVV.css.gz +0 -0
  186. package/dist/dashboard/assets/badge-DpXT4Ixt.js +0 -1
  187. package/dist/dashboard/assets/badge-DpXT4Ixt.js.gz +0 -0
  188. package/dist/dashboard/assets/banner-CSAAWLJv.js +0 -1
  189. package/dist/dashboard/assets/banner-CSAAWLJv.js.gz +0 -0
  190. package/dist/dashboard/assets/card-DEhRXXw4.js +0 -1
  191. package/dist/dashboard/assets/card-DEhRXXw4.js.gz +0 -0
  192. package/dist/dashboard/assets/check-D69FzdvC.js +0 -1
  193. package/dist/dashboard/assets/check-D69FzdvC.js.gz +0 -0
  194. package/dist/dashboard/assets/cli-tools-B0wBq-RL.js.gz +0 -0
  195. package/dist/dashboard/assets/clipboard-button-DOo6EU8O.js +0 -1
  196. package/dist/dashboard/assets/clipboard-button-DOo6EU8O.js.gz +0 -0
  197. package/dist/dashboard/assets/copy-ngmQ9zcZ.js +0 -1
  198. package/dist/dashboard/assets/copy-ngmQ9zcZ.js.gz +0 -0
  199. package/dist/dashboard/assets/dialog-BvQeBMfP.js +0 -1
  200. package/dist/dashboard/assets/dialog-BvQeBMfP.js.gz +0 -0
  201. package/dist/dashboard/assets/drawer-CMwPXwQB.js +0 -1
  202. package/dist/dashboard/assets/drawer-CMwPXwQB.js.gz +0 -0
  203. package/dist/dashboard/assets/entry--yj84zk3.js +0 -4
  204. package/dist/dashboard/assets/entry--yj84zk3.js.gz +0 -0
  205. package/dist/dashboard/assets/entry-D4O2229X.js.gz +0 -0
  206. package/dist/dashboard/assets/entry-DKvr5Q9K.js +0 -1
  207. package/dist/dashboard/assets/entry-DKvr5Q9K.js.gz +0 -0
  208. package/dist/dashboard/assets/github-badge-zliLvBwr.js +0 -1
  209. package/dist/dashboard/assets/github-badge-zliLvBwr.js.gz +0 -0
  210. package/dist/dashboard/assets/index-CcD1eSJ6.js.gz +0 -0
  211. package/dist/dashboard/assets/inline-D1IlZZ8H.js +0 -1
  212. package/dist/dashboard/assets/inline-D1IlZZ8H.js.gz +0 -0
  213. package/dist/dashboard/assets/input-BpVZdrbc.js +0 -1
  214. package/dist/dashboard/assets/input-BpVZdrbc.js.gz +0 -0
  215. package/dist/dashboard/assets/layout-BioLW7gN.js +0 -1
  216. package/dist/dashboard/assets/layout-BioLW7gN.js.gz +0 -0
  217. package/dist/dashboard/assets/list-row-CwVcGAIc.js +0 -1
  218. package/dist/dashboard/assets/list-row-CwVcGAIc.js.gz +0 -0
  219. package/dist/dashboard/assets/live-yfEJFF9T.js.gz +0 -0
  220. package/dist/dashboard/assets/model-bans-CT-TxSNX.js.gz +0 -0
  221. package/dist/dashboard/assets/numbers-CFL4_Dqc.js +0 -1
  222. package/dist/dashboard/assets/numbers-CFL4_Dqc.js.gz +0 -0
  223. package/dist/dashboard/assets/routing-Cs6jl49L.js.gz +0 -0
  224. package/dist/dashboard/assets/select-XPJ-dIw3.js +0 -41
  225. package/dist/dashboard/assets/select-XPJ-dIw3.js.gz +0 -0
  226. package/dist/dashboard/assets/settings-CfcTcOyU.js.gz +0 -0
  227. package/dist/dashboard/assets/stack-C9_uUjaA.js +0 -1
  228. package/dist/dashboard/assets/stack-C9_uUjaA.js.gz +0 -0
  229. package/dist/dashboard/assets/state-DA0B95UQ.js +0 -1
  230. package/dist/dashboard/assets/state-DA0B95UQ.js.gz +0 -0
  231. package/dist/dashboard/assets/switch-Cittxy-x.js +0 -1
  232. package/dist/dashboard/assets/switch-Cittxy-x.js.gz +0 -0
  233. package/dist/dashboard/assets/toast-CNt_jzO4.js +0 -2
  234. package/dist/dashboard/assets/toast-CNt_jzO4.js.gz +0 -0
  235. package/dist/dashboard/assets/useMutation-BQKMcJ1H.js.gz +0 -0
@@ -1,246 +1,248 @@
1
- import { existsSync } from "node:fs";
2
- import { tmpdir } from "node:os";
3
- import { join } from "node:path";
4
- import { randomUUID } from "node:crypto";
5
- import { isNewerRelease } from "../../../src/console/updates/version-compare";
6
- import { resolvePackagePath } from "../../../src/config/package-paths";
7
- import { resolvePort } from "../../../src/config/runtime";
8
- import { ensureDrainToken } from "../gateway/drain-token";
9
- import { startGateway } from "../gateway/spawn";
10
- import { stopGateway } from "../gateway/stop";
11
- import { ENV_PATH, readEnvFile } from "../support/env";
12
- import { readInstalledVersion, readLatestPublishedVersion } from "../support/version";
13
- import { launchBackgroundWithStatus, openUrl } from "../ui/background";
14
- import { chooseFromMenu, confirmByTyping, confirmStep } from "../ui/prompt";
15
- import { renderWordmark } from "../ui/wordmark";
16
- import { exportBackup, resetConsoleAccount } from "./admin";
17
- import { updateCommand } from "./update";
18
-
19
- /**
20
- * The default `carte` experience: start the gateway, then offer an arrow-key
21
- * menu. This needs no desktop environment, so it works over SSH, on a headless
22
- * box, and on Windows where the tray icon lands in the hidden-icons overflow
23
- * and is never seen. `carte --tray` opts into the icon instead.
24
- */
25
- export async function startMenu(): Promise<void> {
26
- // A fresh install has no config file yet, and the gateway refuses to boot
27
- // without CARTETHYIA_DB_MODE — which surfaced as a stack trace about a
28
- // missing database mode rather than "you have not set this up". Run setup
29
- // first, the way `carte` is expected to behave on a new install.
30
- const configured = existsSync(ENV_PATH) && (await readEnvFile(ENV_PATH)).CARTETHYIA_DB_MODE;
31
- if (!configured) {
32
- console.log(
33
- existsSync(ENV_PATH)
34
- ? "Cartethyia is missing its database mode. Starting setup.\n"
35
- : "Cartethyia is not set up yet. Starting setup.\n",
36
- );
37
- // Dynamic on purpose: `setup` pulls in the whole wizard and the database
38
- // mode resolver, and it is only reachable on a first run. A static import
39
- // would pay that cost on every `carte`, which is the one path that has to
40
- // stay fast.
41
- const { setup } = await import("./setup");
42
- await setup();
43
- const after = existsSync(ENV_PATH) ? (await readEnvFile(ENV_PATH)).CARTETHYIA_DB_MODE : undefined;
44
- if (!after) {
45
- console.error(
46
- `Setup did not record CARTETHYIA_DB_MODE in ${ENV_PATH}; run \`cartethyia setup\` and try again.`,
47
- );
48
- process.exitCode = 1;
49
- return;
50
- }
51
- process.env.CARTETHYIA_DB_MODE = after;
52
- }
53
-
54
- const port = resolvePort();
55
- const drainToken = ensureDrainToken();
56
- const dashboardUrl = `http://127.0.0.1:${port}/console`;
57
- const currentVersion = await readInstalledVersion();
58
-
59
- // The gateway starts in the background: the menu is usable before it has
60
- // finished booting, and awaiting it cost ~670 ms of blank terminal on every
61
- // start. `ready` resolves when the health probe passes; actions that need a
62
- // listening gateway await it, so nothing races.
63
- const ready = startGateway(port, drainToken);
64
- let gateway: Bun.Subprocess | null = null;
65
- void ready.then((child) => {
66
- gateway = child;
67
- child.unref();
68
- }).catch((error: unknown) => {
69
- console.error(
70
- `The gateway did not start: ${error instanceof Error ? error.message : "unknown error"}`,
71
- );
72
- });
73
-
74
- let stopping = false;
75
- let handedOff = false;
76
- const shutDown = async (): Promise<void> => {
77
- if (stopping) return;
78
- stopping = true;
79
- const child = gateway ?? (await ready.catch(() => null));
80
- if (child) await stopGateway(child, port, drainToken);
81
- };
82
- // Ctrl-C has to stop the gateway, and the drain endpoint cannot be called
83
- // from an exit path: it is async, and enquirer's own Ctrl-C handler calls
84
- // process.exit(130) while a prompt is live. So the gateway is a child in this
85
- // process group — a terminal's Ctrl-C signals the whole group, and the
86
- // gateway drains through the SIGINT handler it already has in src/main.ts —
87
- // and `process.on("exit")` is the synchronous backstop for every other way
88
- // this process can end (a closed terminal, a crash, a signal Bun cannot
89
- // deliver on Windows). That handler can only call synchronous APIs, which is
90
- // why it signals the child rather than awaiting the drain endpoint.
91
- // `handedOff` is set by "Hide to tray": the gateway is being given to a tray
92
- // that will own it, so this process must not kill it on the way out.
93
- process.on("exit", () => {
94
- if (stopping || handedOff || gateway === null) return;
95
- try {
96
- gateway.kill();
97
- } catch {
98
- // Already gone; nothing to stop.
99
- }
100
- });
101
- const interrupt = async (): Promise<void> => {
102
- process.stdout.write("\u001b[2J\u001b[H");
103
- console.log("Stopping Cartethyia…");
104
- await shutDown();
105
- process.exit(0);
106
- };
107
- process.on("SIGTERM", () => void interrupt());
108
- process.on("SIGINT", () => void interrupt());
109
-
110
- let latestVersion = await readLatestPublishedVersion();
111
- let running = true;
112
-
113
- const header = [...renderWordmark(), `Cartethyia ${currentVersion} \u00b7 ${dashboardUrl}`];
114
-
115
- while (running) {
116
- const upToDate = latestVersion === null || !isNewerRelease(currentVersion, latestVersion);
117
- const updateLabel =
118
- latestVersion === null
119
- ? "Check for updates"
120
- : upToDate
121
- ? `Up to date (${currentVersion})`
122
- : `Update to ${latestVersion}`;
123
-
124
- // Anything that talks to the gateway waits for it to be listening; the menu
125
- // itself does not.
126
- const whenReady = async (): Promise<void> => {
127
- await ready.catch(() => undefined);
128
- };
129
-
130
- const actions: readonly (() => Promise<void>)[] = [
131
- async () => openUrl(dashboardUrl),
132
- async () => {
133
- // Deleting the console account is the only real password recovery:
134
- // /change-password needs the current password, and first-boot setup
135
- // only runs when no user exists. Removing the user puts the wizard
136
- // back. Providers, keys, and routing are untouched.
137
- console.log("");
138
- console.log("This deletes every console account and its sessions.");
139
- console.log("The next visit to /console asks you to create a new one.");
140
- console.log("Providers, API keys, and routing are not touched.");
141
- console.log("");
142
- if (!(await confirmStep("Continue with the reset?"))) {
143
- console.log("Cancelled.");
144
- return;
145
- }
146
- if (!(await confirmByTyping("RESET", "Confirm deleting the console account"))) {
147
- console.log("Cancelled.");
148
- return;
149
- }
150
- await whenReady();
151
- const status = await resetConsoleAccount(port, drainToken);
152
- console.log(
153
- status === 0
154
- ? `Console account removed. Open ${dashboardUrl} to create a new one.`
155
- : "Reset failed; see the gateway log.",
156
- );
157
- },
158
- async () => {
159
- // Export straight to the download directory. The console page asks for
160
- // the operator password, which is exactly the thing a locked-out
161
- // operator does not have; the CLI already runs as the OS user who owns
162
- // the database, so the loopback token is the whole gate.
163
- await whenReady();
164
- const result = await exportBackup(port, drainToken);
165
- if (result === null) return;
166
- console.log(
167
- `Backup written to ${result.path} (${(result.bytes / 1024).toFixed(1)} KiB, ${Object.keys(result.counts).length} tables).`,
168
- );
169
- console.log("Telemetry is not included: it is per-request history, not configuration.");
170
- },
171
- async () => {
172
- if (upToDate) return;
173
- // npm cannot replace this package's directory while this process is
174
- // running out of it: on Windows the rename fails with EBUSY and npm
175
- // exits 14. Even with the gateway stopped, the CLI itself is executing
176
- // from that directory, so an in-process install can never succeed.
177
- // Print the command and exit, the way 9router does.
178
- await shutDown();
179
- console.log("");
180
- console.log(`Update available: ${currentVersion} \u2192 ${latestVersion}`);
181
- console.log("");
182
- console.log("Run this now that Cartethyia has stopped:");
183
- console.log("");
184
- console.log(` ${updateCommand()}`);
185
- console.log("");
186
- running = false;
187
- process.exitCode = 0;
188
- return;
189
- },
190
- async () => {
191
- // Drain first, then start the tray.
192
- //
193
- // The tray's own startup probe refuses to boot when the port is busy,
194
- // so starting it before this process has let go made it fail with
195
- // "already running on port" every time — while the menu still printed
196
- // that everything was running, because a detached child with no stdio
197
- // cannot report anything back. Releasing the port first is the fix, and
198
- // waiting on the child's signal is what stops the menu from claiming
199
- // success for a child that died.
200
- console.log("Stopping the gateway to free the port…");
201
- running = false;
202
- stopping = true;
203
- handedOff = true;
204
- await shutDown();
205
- console.log("Starting the tray in the background…");
206
- const signalPath = join(tmpdir(), `cartethyia-tray-menu-${process.pid}-${randomUUID()}.json`);
207
- try {
208
- const url = await launchBackgroundWithStatus(
209
- ["run", resolvePackagePath("scripts", "cli", "main.ts"), "tray"],
210
- { signalPath },
211
- );
212
- console.log(`Cartethyia is running in the background at ${url || dashboardUrl}.`);
213
- console.log("The tray icon owns it; use Exit there to stop it.");
214
- } catch (error) {
215
- handedOff = false;
216
- console.error(
217
- `Could not hand off to the tray: ${error instanceof Error ? error.message : "unknown error"}`,
218
- );
219
- console.error("Cartethyia is stopped. Start it again from this menu.");
220
- }
221
- },
222
- async () => {
223
- running = false;
224
- },
225
- ];
226
-
227
- const choice = await chooseFromMenu(header, [
228
- { kind: "action", label: "Open Console", hint: `browser \u00b7 ${dashboardUrl}` },
229
- { kind: "action", label: "Reset console password", hint: "deletes the account" },
230
- { kind: "action", label: "Backup", hint: "export config to Downloads" },
231
- { kind: "action", label: updateLabel, hint: upToDate ? "open the repository" : "installs now" },
232
- { kind: "action", label: "Hide to tray", hint: "keep the gateway running" },
233
- { kind: "rule" },
234
- { kind: "action", label: "Exit", hint: "stop the gateway" },
235
- ],
236
- );
237
- if (choice === -1) break;
238
- const action = actions[choice];
239
- if (action) await action();
240
- }
241
- // "Hide to tray" has already given the gateway to the tray. Draining here
242
- // would stop the very process the tray just adopted, which is why the menu
243
- // appeared to hand over a gateway that was not listening.
244
- if (handedOff) return;
245
- await shutDown();
246
- }
1
+ import { existsSync } from "node:fs";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { randomUUID } from "node:crypto";
5
+ import { isNewerRelease } from "../../../src/console/updates/version-compare";
6
+ import { resolvePackagePath } from "../../../src/config/package-paths";
7
+ import { resolvePort } from "../../../src/config/runtime";
8
+ import { ensureDrainToken } from "../gateway/drain-token";
9
+ import { startGateway } from "../gateway/spawn";
10
+ import { stopGateway } from "../gateway/stop";
11
+ import { ENV_PATH, readEnvFile } from "../support/env";
12
+ import { readInstalledVersion, readLatestPublishedVersion } from "../support/version";
13
+ import { launchBackgroundWithStatus, openUrl } from "../ui/background";
14
+ import { chooseFromMenu, confirmByTyping, confirmStep } from "../ui/prompt";
15
+ import { renderWordmark } from "../ui/wordmark";
16
+ import { exportBackup, resetConsoleAccount } from "./admin";
17
+ import { updateCommand } from "./update";
18
+
19
+ /**
20
+ * The default `carte` experience: start the gateway, then offer an arrow-key
21
+ * menu. This needs no desktop environment, so it works over SSH, on a headless
22
+ * box, and on Windows where the tray icon lands in the hidden-icons overflow
23
+ * and is never seen. `carte --tray` opts into the icon instead.
24
+ */
25
+ export async function startMenu(): Promise<void> {
26
+ // A fresh install has no config file yet, and the gateway refuses to boot
27
+ // without CARTETHYIA_DB_MODE — which surfaced as a stack trace about a
28
+ // missing database mode rather than "you have not set this up". Run setup
29
+ // first, the way `carte` is expected to behave on a new install.
30
+ const configured = existsSync(ENV_PATH) && (await readEnvFile(ENV_PATH)).CARTETHYIA_DB_MODE;
31
+ if (!configured) {
32
+ console.log(
33
+ existsSync(ENV_PATH)
34
+ ? "Cartethyia is missing its database mode. Starting setup.\n"
35
+ : "Cartethyia is not set up yet. Starting setup.\n",
36
+ );
37
+ // Dynamic on purpose: `setup` pulls in the whole wizard and the database
38
+ // mode resolver, and it is only reachable on a first run. A static import
39
+ // would pay that cost on every `carte`, which is the one path that has to
40
+ // stay fast.
41
+ const { setup } = await import("./setup");
42
+ await setup();
43
+ const after = existsSync(ENV_PATH) ? (await readEnvFile(ENV_PATH)).CARTETHYIA_DB_MODE : undefined;
44
+ if (!after) {
45
+ console.error(
46
+ `Setup did not record CARTETHYIA_DB_MODE in ${ENV_PATH}; run \`cartethyia setup\` and try again.`,
47
+ );
48
+ process.exitCode = 1;
49
+ return;
50
+ }
51
+ process.env.CARTETHYIA_DB_MODE = after;
52
+ }
53
+
54
+ const port = resolvePort();
55
+ const drainToken = ensureDrainToken();
56
+ const dashboardUrl = `http://127.0.0.1:${port}/console`;
57
+ const currentVersion = await readInstalledVersion();
58
+
59
+ // The gateway starts in the background: the menu is usable before it has
60
+ // finished booting, and awaiting it cost ~670 ms of blank terminal on every
61
+ // start. `ready` resolves when the health probe passes; actions that need a
62
+ // listening gateway await it, so nothing races.
63
+ const ready = startGateway(port, drainToken);
64
+ let gateway: Bun.Subprocess | null = null;
65
+ void ready.then((child) => {
66
+ gateway = child;
67
+ child.unref();
68
+ }).catch((error: unknown) => {
69
+ console.error(
70
+ `The gateway did not start: ${error instanceof Error ? error.message : "unknown error"}`,
71
+ );
72
+ });
73
+
74
+ let stopping = false;
75
+ let handedOff = false;
76
+ const shutDown = async (): Promise<void> => {
77
+ if (stopping) return;
78
+ stopping = true;
79
+ const child = gateway ?? (await ready.catch(() => null));
80
+ if (child) await stopGateway(child, port, drainToken);
81
+ };
82
+ // Ctrl-C has to stop the gateway, and the drain endpoint cannot be called
83
+ // from an exit path: it is async, and enquirer's own Ctrl-C handler calls
84
+ // process.exit(130) while a prompt is live. So the gateway is a child in this
85
+ // process group — a terminal's Ctrl-C signals the whole group, and the
86
+ // gateway drains through the SIGINT handler it already has in src/main.ts —
87
+ // and `process.on("exit")` is the synchronous backstop for every other way
88
+ // this process can end (a closed terminal, a crash, a signal Bun cannot
89
+ // deliver on Windows). That handler can only call synchronous APIs, which is
90
+ // why it signals the child rather than awaiting the drain endpoint.
91
+ // `handedOff` is set by "Hide to tray": the gateway is being given to a tray
92
+ // that will own it, so this process must not kill it on the way out.
93
+ process.on("exit", () => {
94
+ if (stopping || handedOff || gateway === null) return;
95
+ try {
96
+ gateway.kill();
97
+ } catch {
98
+ // Already gone; nothing to stop.
99
+ }
100
+ });
101
+ const interrupt = async (): Promise<void> => {
102
+ process.stdout.write("\u001b[2J\u001b[H");
103
+ console.log("Stopping Cartethyia…");
104
+ await shutDown();
105
+ process.exit(0);
106
+ };
107
+ process.on("SIGTERM", () => void interrupt());
108
+ process.on("SIGINT", () => void interrupt());
109
+
110
+ let latestVersion = await readLatestPublishedVersion();
111
+ let running = true;
112
+
113
+ const header = [...renderWordmark(), `Cartethyia ${currentVersion} \u00b7 ${dashboardUrl}`];
114
+
115
+ while (running) {
116
+ const upToDate = latestVersion === null || !isNewerRelease(currentVersion, latestVersion);
117
+ const updateLabel =
118
+ latestVersion === null
119
+ ? "Check for updates"
120
+ : upToDate
121
+ ? `Up to date (${currentVersion})`
122
+ : `Update to ${latestVersion}`;
123
+
124
+ // Anything that talks to the gateway waits for it to be listening; the menu
125
+ // itself does not.
126
+ const whenReady = async (): Promise<void> => {
127
+ await ready.catch(() => undefined);
128
+ };
129
+
130
+ const actions: readonly (() => Promise<void>)[] = [
131
+ async () => openUrl(dashboardUrl),
132
+ async () => {
133
+ // Deleting the console account is the only real password recovery:
134
+ // /change-password needs the current password, and first-boot setup
135
+ // only runs when no user exists. Removing the user puts the wizard
136
+ // back. Providers, keys, and routing are untouched.
137
+ console.log("");
138
+ console.log("This deletes every console account and its sessions.");
139
+ console.log("The next visit to /console asks you to create a new one.");
140
+ console.log("Providers, API keys, and routing are not touched.");
141
+ console.log("");
142
+ if (!(await confirmStep("Continue with the reset?"))) {
143
+ console.log("Cancelled.");
144
+ return;
145
+ }
146
+ if (!(await confirmByTyping("RESET", "Confirm deleting the console account"))) {
147
+ console.log("Cancelled.");
148
+ return;
149
+ }
150
+ await whenReady();
151
+ const status = await resetConsoleAccount(port, drainToken);
152
+ console.log(
153
+ status === 0
154
+ ? `Console account removed. Open ${dashboardUrl} to create a new one.`
155
+ : "Reset failed; see the gateway log.",
156
+ );
157
+ },
158
+ async () => {
159
+ // Export straight to the download directory. The console page asks for
160
+ // the operator password, which is exactly the thing a locked-out
161
+ // operator does not have; the CLI already runs as the OS user who owns
162
+ // the database, so the loopback token is the whole gate.
163
+ await whenReady();
164
+ const result = await exportBackup(port, drainToken);
165
+ if (result === null) return;
166
+ console.log(
167
+ `Backup written to ${result.path} (${(result.bytes / 1024).toFixed(1)} KiB, ${Object.keys(result.counts).length} tables).`,
168
+ );
169
+ console.log("Telemetry is not included: it is per-request history, not configuration.");
170
+ },
171
+ async () => {
172
+ if (upToDate) return;
173
+ // npm cannot replace this package's directory while this process is
174
+ // running out of it: on Windows the rename fails with EBUSY and npm
175
+ // exits 14. Even with the gateway stopped, the CLI itself is executing
176
+ // from that directory, so an in-process install can never succeed.
177
+ // Print the command and exit, the way 9router does.
178
+ await shutDown();
179
+ console.log("");
180
+ console.log(`Update available: ${currentVersion} \u2192 ${latestVersion}`);
181
+ console.log("");
182
+ console.log("Run this now that Cartethyia has stopped:");
183
+ console.log("");
184
+ console.log(` ${updateCommand()}`);
185
+ console.log("");
186
+ console.log("This terminal will stay open for 180s so you can copy the command, then exit.");
187
+ running = false;
188
+ await new Promise<void>((resolve) => setTimeout(resolve, 180_000));
189
+ console.log("Session ended. Run the command above to update.");
190
+ process.exit(0);
191
+ },
192
+ async () => {
193
+ // Drain first, then start the tray.
194
+ //
195
+ // The tray's own startup probe refuses to boot when the port is busy,
196
+ // so starting it before this process has let go made it fail with
197
+ // "already running on port" every time — while the menu still printed
198
+ // that everything was running, because a detached child with no stdio
199
+ // cannot report anything back. Releasing the port first is the fix, and
200
+ // waiting on the child's signal is what stops the menu from claiming
201
+ // success for a child that died.
202
+ console.log("Stopping the gateway to free the port…");
203
+ running = false;
204
+ stopping = true;
205
+ handedOff = true;
206
+ await shutDown();
207
+ console.log("Starting the tray in the background…");
208
+ const signalPath = join(tmpdir(), `cartethyia-tray-menu-${process.pid}-${randomUUID()}.json`);
209
+ try {
210
+ const url = await launchBackgroundWithStatus(
211
+ ["run", resolvePackagePath("scripts", "cli", "main.ts"), "tray"],
212
+ { signalPath },
213
+ );
214
+ console.log(`Cartethyia is running in the background at ${url || dashboardUrl}.`);
215
+ console.log("The tray icon owns it; use Exit there to stop it.");
216
+ } catch (error) {
217
+ handedOff = false;
218
+ console.error(
219
+ `Could not hand off to the tray: ${error instanceof Error ? error.message : "unknown error"}`,
220
+ );
221
+ console.error("Cartethyia is stopped. Start it again from this menu.");
222
+ }
223
+ },
224
+ async () => {
225
+ running = false;
226
+ },
227
+ ];
228
+
229
+ const choice = await chooseFromMenu(header, [
230
+ { kind: "action", label: "Open Console", hint: `browser \u00b7 ${dashboardUrl}` },
231
+ { kind: "action", label: "Reset console password", hint: "deletes the account" },
232
+ { kind: "action", label: "Backup", hint: "export config to Downloads" },
233
+ { kind: "action", label: updateLabel, hint: upToDate ? "open the repository" : "installs now" },
234
+ { kind: "action", label: "Hide to tray", hint: "keep the gateway running" },
235
+ { kind: "rule" },
236
+ { kind: "action", label: "Exit", hint: "stop the gateway" },
237
+ ],
238
+ );
239
+ if (choice === -1) break;
240
+ const action = actions[choice];
241
+ if (action) await action();
242
+ }
243
+ // "Hide to tray" has already given the gateway to the tray. Draining here
244
+ // would stop the very process the tray just adopted, which is why the menu
245
+ // appeared to hand over a gateway that was not listening.
246
+ if (handedOff) return;
247
+ await shutDown();
248
+ }