@thebriangao/totem 1.4.4 → 2.0.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 (763) hide show
  1. package/.dev.vars.example +5 -0
  2. package/.dockerignore +5 -0
  3. package/.env.example +29 -20
  4. package/CHANGELOG.md +99 -1
  5. package/CONTRIBUTING.md +48 -116
  6. package/Dockerfile +46 -12
  7. package/README.md +172 -498
  8. package/SECURITY.md +36 -59
  9. package/TOOLS.md +448 -311
  10. package/WHOOP.md +497 -2659
  11. package/assets/brand/SOURCES.md +38 -0
  12. package/assets/brand/totem.png +0 -0
  13. package/assets/brand/totem.svg +13 -0
  14. package/assets/fonts/Manrope-OFL.txt +93 -0
  15. package/assets/fonts/manrope-latin.woff2 +0 -0
  16. package/dist/build_info.js +5 -0
  17. package/dist/build_info.js.map +1 -0
  18. package/dist/cli/app.js +675 -0
  19. package/dist/cli/app.js.map +1 -0
  20. package/dist/cli/auth.js +297 -0
  21. package/dist/cli/auth.js.map +1 -0
  22. package/dist/cli/auth_flow.js +268 -0
  23. package/dist/cli/auth_flow.js.map +1 -0
  24. package/dist/cli/browser_login.js +368 -0
  25. package/dist/cli/browser_login.js.map +1 -0
  26. package/dist/cli/clients.js +403 -0
  27. package/dist/cli/clients.js.map +1 -0
  28. package/dist/cli/deploy_record.js +96 -0
  29. package/dist/cli/deploy_record.js.map +1 -0
  30. package/dist/cli/index.js +39 -575
  31. package/dist/cli/index.js.map +1 -1
  32. package/dist/cli/links.js +28 -0
  33. package/dist/cli/links.js.map +1 -0
  34. package/dist/cli/logo.js +63 -0
  35. package/dist/cli/logo.js.map +1 -0
  36. package/dist/cli/setup.js +1157 -1017
  37. package/dist/cli/setup.js.map +1 -1
  38. package/dist/cli/spawn.js +80 -0
  39. package/dist/cli/spawn.js.map +1 -0
  40. package/dist/cli/sys.js +219 -0
  41. package/dist/cli/sys.js.map +1 -0
  42. package/dist/cli/ui.js +712 -463
  43. package/dist/cli/ui.js.map +1 -1
  44. package/dist/cli/update.js +425 -282
  45. package/dist/cli/update.js.map +1 -1
  46. package/dist/cloudflare/token_store.js +48 -0
  47. package/dist/cloudflare/token_store.js.map +1 -0
  48. package/dist/cloudflare/worker.js +155 -0
  49. package/dist/cloudflare/worker.js.map +1 -0
  50. package/dist/data/behaviors.js +2733 -617
  51. package/dist/data/behaviors.js.map +1 -1
  52. package/dist/data/endpoint_catalog.js +2681 -0
  53. package/dist/data/endpoint_catalog.js.map +1 -0
  54. package/dist/data/exercises.js +373 -4859
  55. package/dist/data/exercises.js.map +1 -1
  56. package/dist/data/garments.js +19 -0
  57. package/dist/data/garments.js.map +1 -0
  58. package/dist/data/sports.js +219 -208
  59. package/dist/data/sports.js.map +1 -1
  60. package/dist/lib/activity_list.js +123 -0
  61. package/dist/lib/activity_list.js.map +1 -0
  62. package/dist/lib/activity_records.js +195 -0
  63. package/dist/lib/activity_records.js.map +1 -0
  64. package/dist/lib/activity_results.js +44 -0
  65. package/dist/lib/activity_results.js.map +1 -0
  66. package/dist/lib/activity_types.js +164 -0
  67. package/dist/lib/activity_types.js.map +1 -0
  68. package/dist/lib/brand.js +63 -0
  69. package/dist/lib/brand.js.map +1 -0
  70. package/dist/lib/clock.js +24 -0
  71. package/dist/lib/clock.js.map +1 -0
  72. package/dist/lib/coach_session.js +345 -0
  73. package/dist/lib/coach_session.js.map +1 -0
  74. package/dist/lib/concurrency.js.map +1 -1
  75. package/dist/lib/dates.js +19 -0
  76. package/dist/lib/dates.js.map +1 -1
  77. package/dist/lib/env_file.js +195 -0
  78. package/dist/lib/env_file.js.map +1 -0
  79. package/dist/lib/health_plans_write.js +44 -0
  80. package/dist/lib/health_plans_write.js.map +1 -0
  81. package/dist/lib/heart_rate.js +34 -0
  82. package/dist/lib/heart_rate.js.map +1 -0
  83. package/dist/lib/highlight.js +81 -0
  84. package/dist/lib/highlight.js.map +1 -0
  85. package/dist/lib/install_links.js +47 -0
  86. package/dist/lib/install_links.js.map +1 -0
  87. package/dist/lib/instant.js +50 -0
  88. package/dist/lib/instant.js.map +1 -0
  89. package/dist/lib/journal_behaviors.js +382 -0
  90. package/dist/lib/journal_behaviors.js.map +1 -0
  91. package/dist/lib/lift_catalog.js +218 -0
  92. package/dist/lib/lift_catalog.js.map +1 -0
  93. package/dist/lib/lift_history.js +66 -0
  94. package/dist/lib/lift_history.js.map +1 -0
  95. package/dist/lib/lift_sets.js +146 -0
  96. package/dist/lib/lift_sets.js.map +1 -0
  97. package/dist/lib/lift_tool.js +20 -0
  98. package/dist/lib/lift_tool.js.map +1 -0
  99. package/dist/lib/profile_settings.js +101 -0
  100. package/dist/lib/profile_settings.js.map +1 -0
  101. package/dist/lib/screen_content.js +44 -0
  102. package/dist/lib/screen_content.js.map +1 -0
  103. package/dist/lib/timezone.js +48 -6
  104. package/dist/lib/timezone.js.map +1 -1
  105. package/dist/lib/walk.js +6 -1
  106. package/dist/lib/walk.js.map +1 -1
  107. package/dist/lib/weight_readings.js +61 -0
  108. package/dist/lib/weight_readings.js.map +1 -0
  109. package/dist/projections/activities.js +193 -0
  110. package/dist/projections/activities.js.map +1 -0
  111. package/dist/projections/alarm_schedule.js +102 -0
  112. package/dist/projections/alarm_schedule.js.map +1 -0
  113. package/dist/projections/coach.js +203 -0
  114. package/dist/projections/coach.js.map +1 -0
  115. package/dist/projections/community.js +165 -0
  116. package/dist/projections/community.js.map +1 -0
  117. package/dist/projections/health_features.js +167 -0
  118. package/dist/projections/health_features.js.map +1 -0
  119. package/dist/projections/hormonal_insights.js +239 -0
  120. package/dist/projections/hormonal_insights.js.map +1 -0
  121. package/dist/projections/journal_v2.js +157 -0
  122. package/dist/projections/journal_v2.js.map +1 -0
  123. package/dist/projections/lift_cards.js +115 -0
  124. package/dist/projections/lift_cards.js.map +1 -0
  125. package/dist/projections/lift_exercise_v2.js +131 -0
  126. package/dist/projections/lift_exercise_v2.js.map +1 -0
  127. package/dist/projections/lift_library_v2.js +67 -0
  128. package/dist/projections/lift_library_v2.js.map +1 -0
  129. package/dist/projections/lift_sessions.js +211 -0
  130. package/dist/projections/lift_sessions.js.map +1 -0
  131. package/dist/projections/memory.js +95 -0
  132. package/dist/projections/memory.js.map +1 -0
  133. package/dist/projections/milestones.js +119 -0
  134. package/dist/projections/milestones.js.map +1 -0
  135. package/dist/projections/profile_v2.js +150 -0
  136. package/dist/projections/profile_v2.js.map +1 -0
  137. package/dist/projections/settings_v2.js +153 -0
  138. package/dist/projections/settings_v2.js.map +1 -0
  139. package/dist/projections/sleep.js +4 -21
  140. package/dist/projections/sleep.js.map +1 -1
  141. package/dist/projections/v2/calendar.js +43 -0
  142. package/dist/projections/v2/calendar.js.map +1 -0
  143. package/dist/projections/v2/core_common.js +86 -0
  144. package/dist/projections/v2/core_common.js.map +1 -0
  145. package/dist/projections/v2/core_home.js +136 -0
  146. package/dist/projections/v2/core_home.js.map +1 -0
  147. package/dist/projections/v2/core_sleep_need.js +23 -0
  148. package/dist/projections/v2/core_sleep_need.js.map +1 -0
  149. package/dist/projections/v2/day.js +92 -0
  150. package/dist/projections/v2/day.js.map +1 -0
  151. package/dist/projections/v2/heart_rate.js +54 -0
  152. package/dist/projections/v2/heart_rate.js.map +1 -0
  153. package/dist/projections/v2/history.js +140 -0
  154. package/dist/projections/v2/history.js.map +1 -0
  155. package/dist/projections/v2/now.js +39 -0
  156. package/dist/projections/v2/now.js.map +1 -0
  157. package/dist/projections/v2/recovery.js +77 -0
  158. package/dist/projections/v2/recovery.js.map +1 -0
  159. package/dist/projections/v2/sleep.js +218 -0
  160. package/dist/projections/v2/sleep.js.map +1 -0
  161. package/dist/projections/v2/strain.js +39 -0
  162. package/dist/projections/v2/strain.js.map +1 -0
  163. package/dist/projections/v2/stress.js +92 -0
  164. package/dist/projections/v2/stress.js.map +1 -0
  165. package/dist/projections/v2/trend.js +205 -0
  166. package/dist/projections/v2/trend.js.map +1 -0
  167. package/dist/projections/weekly_plan.js +66 -0
  168. package/dist/projections/weekly_plan.js.map +1 -0
  169. package/dist/projections/weight_readings.js +57 -0
  170. package/dist/projections/weight_readings.js.map +1 -0
  171. package/dist/projections/workout.js +37 -15
  172. package/dist/projections/workout.js.map +1 -1
  173. package/dist/schemas/activities.js +123 -0
  174. package/dist/schemas/activities.js.map +1 -0
  175. package/dist/schemas/alarm_schedule.js +62 -0
  176. package/dist/schemas/alarm_schedule.js.map +1 -0
  177. package/dist/schemas/coach.js +76 -8
  178. package/dist/schemas/coach.js.map +1 -1
  179. package/dist/schemas/community.js +70 -0
  180. package/dist/schemas/community.js.map +1 -0
  181. package/dist/schemas/health.js +36 -0
  182. package/dist/schemas/health.js.map +1 -0
  183. package/dist/schemas/hormonal_insights.js +90 -0
  184. package/dist/schemas/hormonal_insights.js.map +1 -0
  185. package/dist/schemas/journal.js +4 -0
  186. package/dist/schemas/journal.js.map +1 -1
  187. package/dist/schemas/journal_v2.js +106 -0
  188. package/dist/schemas/journal_v2.js.map +1 -0
  189. package/dist/schemas/memory.js +42 -0
  190. package/dist/schemas/memory.js.map +1 -0
  191. package/dist/schemas/milestones.js +39 -0
  192. package/dist/schemas/milestones.js.map +1 -0
  193. package/dist/schemas/profile_v2.js +60 -0
  194. package/dist/schemas/profile_v2.js.map +1 -0
  195. package/dist/schemas/settings_v2.js +56 -0
  196. package/dist/schemas/settings_v2.js.map +1 -0
  197. package/dist/schemas/sleep.js +8 -1
  198. package/dist/schemas/sleep.js.map +1 -1
  199. package/dist/schemas/strength.js +10 -1
  200. package/dist/schemas/strength.js.map +1 -1
  201. package/dist/schemas/strength_v2.js +190 -0
  202. package/dist/schemas/strength_v2.js.map +1 -0
  203. package/dist/schemas/v2/calendar.js +8 -0
  204. package/dist/schemas/v2/calendar.js.map +1 -0
  205. package/dist/schemas/v2/core_common.js +42 -0
  206. package/dist/schemas/v2/core_common.js.map +1 -0
  207. package/dist/schemas/v2/day.js +73 -0
  208. package/dist/schemas/v2/day.js.map +1 -0
  209. package/dist/schemas/v2/now.js +25 -0
  210. package/dist/schemas/v2/now.js.map +1 -0
  211. package/dist/schemas/v2/recovery.js +35 -0
  212. package/dist/schemas/v2/recovery.js.map +1 -0
  213. package/dist/schemas/v2/sleep.js +44 -0
  214. package/dist/schemas/v2/sleep.js.map +1 -0
  215. package/dist/schemas/v2/strain.js +16 -0
  216. package/dist/schemas/v2/strain.js.map +1 -0
  217. package/dist/schemas/v2/stress.js +20 -0
  218. package/dist/schemas/v2/stress.js.map +1 -0
  219. package/dist/schemas/v2/trend.js +42 -0
  220. package/dist/schemas/v2/trend.js.map +1 -0
  221. package/dist/schemas/weekly_plan.js +31 -0
  222. package/dist/schemas/weekly_plan.js.map +1 -0
  223. package/dist/schemas/weight.js +28 -0
  224. package/dist/schemas/weight.js.map +1 -0
  225. package/dist/scripts/build_behaviors.js +114 -0
  226. package/dist/scripts/build_behaviors.js.map +1 -0
  227. package/dist/scripts/cognito_bootstrap.js +9 -277
  228. package/dist/scripts/cognito_bootstrap.js.map +1 -1
  229. package/dist/scripts/gen_activity_catalogs.js +104 -0
  230. package/dist/scripts/gen_activity_catalogs.js.map +1 -0
  231. package/dist/server-admin.js +156 -0
  232. package/dist/server-admin.js.map +1 -0
  233. package/dist/server-http.js +270 -48
  234. package/dist/server-http.js.map +1 -1
  235. package/dist/server.js +136 -23
  236. package/dist/server.js.map +1 -1
  237. package/dist/tools/annotations.js +15 -0
  238. package/dist/tools/annotations.js.map +1 -0
  239. package/dist/tools/define.js +31 -0
  240. package/dist/tools/define.js.map +1 -0
  241. package/dist/tools/register.js +59 -94
  242. package/dist/tools/register.js.map +1 -1
  243. package/dist/tools/resources.js +70 -0
  244. package/dist/tools/resources.js.map +1 -0
  245. package/dist/tools/slim_schema.js +90 -0
  246. package/dist/tools/slim_schema.js.map +1 -0
  247. package/dist/tools/v2/activity_delete.js +63 -17
  248. package/dist/tools/v2/activity_delete.js.map +1 -1
  249. package/dist/tools/v2/activity_edit.js +396 -0
  250. package/dist/tools/v2/activity_edit.js.map +1 -0
  251. package/dist/tools/v2/activity_log.js +152 -0
  252. package/dist/tools/v2/activity_log.js.map +1 -0
  253. package/dist/tools/v2/calendar.js +19 -21
  254. package/dist/tools/v2/calendar.js.map +1 -1
  255. package/dist/tools/v2/coach_ask.js +74 -110
  256. package/dist/tools/v2/coach_ask.js.map +1 -1
  257. package/dist/tools/v2/coach_history.js +120 -0
  258. package/dist/tools/v2/coach_history.js.map +1 -0
  259. package/dist/tools/v2/community.js +124 -0
  260. package/dist/tools/v2/community.js.map +1 -0
  261. package/dist/tools/v2/cycle.js +63 -15
  262. package/dist/tools/v2/cycle.js.map +1 -1
  263. package/dist/tools/v2/cycle_log.js +289 -32
  264. package/dist/tools/v2/cycle_log.js.map +1 -1
  265. package/dist/tools/v2/day.js +126 -24
  266. package/dist/tools/v2/day.js.map +1 -1
  267. package/dist/tools/v2/health.js +116 -0
  268. package/dist/tools/v2/health.js.map +1 -0
  269. package/dist/tools/v2/hr_zones_set.js +134 -34
  270. package/dist/tools/v2/hr_zones_set.js.map +1 -1
  271. package/dist/tools/v2/journal.js +75 -17
  272. package/dist/tools/v2/journal.js.map +1 -1
  273. package/dist/tools/v2/journal_log.js +306 -65
  274. package/dist/tools/v2/journal_log.js.map +1 -1
  275. package/dist/tools/v2/lift_exercise.js +125 -22
  276. package/dist/tools/v2/lift_exercise.js.map +1 -1
  277. package/dist/tools/v2/lift_exercise_set.js +291 -0
  278. package/dist/tools/v2/lift_exercise_set.js.map +1 -0
  279. package/dist/tools/v2/lift_library.js +51 -12
  280. package/dist/tools/v2/lift_library.js.map +1 -1
  281. package/dist/tools/v2/lift_log.js +309 -72
  282. package/dist/tools/v2/lift_log.js.map +1 -1
  283. package/dist/tools/v2/lift_sessions.js +248 -0
  284. package/dist/tools/v2/lift_sessions.js.map +1 -0
  285. package/dist/tools/v2/lift_template.js +232 -0
  286. package/dist/tools/v2/lift_template.js.map +1 -0
  287. package/dist/tools/v2/memory.js +45 -0
  288. package/dist/tools/v2/memory.js.map +1 -0
  289. package/dist/tools/v2/memory_set.js +107 -0
  290. package/dist/tools/v2/memory_set.js.map +1 -0
  291. package/dist/tools/v2/milestones.js +50 -0
  292. package/dist/tools/v2/milestones.js.map +1 -0
  293. package/dist/tools/v2/now.js +49 -0
  294. package/dist/tools/v2/now.js.map +1 -0
  295. package/dist/tools/v2/preferences.js +51 -0
  296. package/dist/tools/v2/preferences.js.map +1 -0
  297. package/dist/tools/v2/preferences_set.js +388 -0
  298. package/dist/tools/v2/preferences_set.js.map +1 -0
  299. package/dist/tools/v2/profile.js +54 -22
  300. package/dist/tools/v2/profile.js.map +1 -1
  301. package/dist/tools/v2/profile_update.js +167 -55
  302. package/dist/tools/v2/profile_update.js.map +1 -1
  303. package/dist/tools/v2/raw.js +91 -34
  304. package/dist/tools/v2/raw.js.map +1 -1
  305. package/dist/tools/v2/recovery.js +37 -23
  306. package/dist/tools/v2/recovery.js.map +1 -1
  307. package/dist/tools/v2/sleep.js +52 -16
  308. package/dist/tools/v2/sleep.js.map +1 -1
  309. package/dist/tools/v2/smart_alarm.js +54 -20
  310. package/dist/tools/v2/smart_alarm.js.map +1 -1
  311. package/dist/tools/v2/smart_alarm_set.js +264 -86
  312. package/dist/tools/v2/smart_alarm_set.js.map +1 -1
  313. package/dist/tools/v2/strain.js +15 -15
  314. package/dist/tools/v2/strain.js.map +1 -1
  315. package/dist/tools/v2/stress.js +17 -15
  316. package/dist/tools/v2/stress.js.map +1 -1
  317. package/dist/tools/v2/trend.js +56 -20
  318. package/dist/tools/v2/trend.js.map +1 -1
  319. package/dist/tools/v2/weekly_plan.js +30 -0
  320. package/dist/tools/v2/weekly_plan.js.map +1 -0
  321. package/dist/tools/v2/weekly_plan_set.js +134 -0
  322. package/dist/tools/v2/weekly_plan_set.js.map +1 -0
  323. package/dist/tools/v2/weight.js +56 -0
  324. package/dist/tools/v2/weight.js.map +1 -0
  325. package/dist/tools/v2/weight_log.js +188 -0
  326. package/dist/tools/v2/weight_log.js.map +1 -0
  327. package/dist/tools/v2/workouts.js +159 -25
  328. package/dist/tools/v2/workouts.js.map +1 -1
  329. package/dist/version.js +6 -0
  330. package/dist/version.js.map +1 -0
  331. package/dist/whoop/bff.js +94 -0
  332. package/dist/whoop/bff.js.map +1 -0
  333. package/dist/whoop/client.js +392 -18
  334. package/dist/whoop/client.js.map +1 -1
  335. package/dist/whoop/cognito.js +106 -79
  336. package/dist/whoop/cognito.js.map +1 -1
  337. package/dist/whoop/connect_page.js +165 -0
  338. package/dist/whoop/connect_page.js.map +1 -0
  339. package/dist/whoop/consent_font.js +287 -0
  340. package/dist/whoop/consent_font.js.map +1 -0
  341. package/dist/whoop/consent_logos.js +22 -0
  342. package/dist/whoop/consent_logos.js.map +1 -0
  343. package/dist/whoop/consent_page.js +291 -0
  344. package/dist/whoop/consent_page.js.map +1 -0
  345. package/dist/whoop/constants.js +5 -1
  346. package/dist/whoop/constants.js.map +1 -1
  347. package/dist/whoop/device.js +4 -4
  348. package/dist/whoop/device.js.map +1 -1
  349. package/dist/whoop/errors.js +85 -4
  350. package/dist/whoop/errors.js.map +1 -1
  351. package/dist/whoop/init_timezone.js +57 -17
  352. package/dist/whoop/init_timezone.js.map +1 -1
  353. package/dist/whoop/installation.js +15 -10
  354. package/dist/whoop/installation.js.map +1 -1
  355. package/dist/whoop/json_out.js +52 -6
  356. package/dist/whoop/json_out.js.map +1 -1
  357. package/dist/whoop/oauth_provider.js +81 -78
  358. package/dist/whoop/oauth_provider.js.map +1 -1
  359. package/dist/whoop/token_lock.js +331 -0
  360. package/dist/whoop/token_lock.js.map +1 -0
  361. package/dist/whoop/token_manager.js +536 -42
  362. package/dist/whoop/token_manager.js.map +1 -1
  363. package/dist/whoop/token_store.js +303 -36
  364. package/dist/whoop/token_store.js.map +1 -1
  365. package/dist/whoop/units.js +28 -0
  366. package/dist/whoop/units.js.map +1 -0
  367. package/package.json +29 -13
  368. package/scripts/build-mcpb.mjs +72 -0
  369. package/scripts/embed-consent-font.mjs +34 -0
  370. package/scripts/readme-media.ts +195 -0
  371. package/scripts/tools-md.ts +89 -0
  372. package/scripts/whoop-md.ts +119 -0
  373. package/src/build_info.ts +4 -0
  374. package/src/cli/app.ts +696 -0
  375. package/src/cli/auth.ts +350 -0
  376. package/src/cli/auth_flow.ts +356 -0
  377. package/src/cli/browser_login.ts +402 -0
  378. package/src/cli/clients.ts +467 -0
  379. package/src/cli/deploy_record.ts +100 -0
  380. package/src/cli/index.ts +41 -583
  381. package/src/cli/links.ts +43 -0
  382. package/src/cli/logo.ts +64 -0
  383. package/src/cli/setup.ts +1147 -979
  384. package/src/cli/spawn.ts +88 -0
  385. package/src/cli/sys.ts +267 -0
  386. package/src/cli/ui.ts +778 -436
  387. package/src/cli/update.ts +436 -274
  388. package/src/cloudflare/runtime.d.ts +27 -0
  389. package/src/cloudflare/token_store.ts +57 -0
  390. package/src/cloudflare/worker.ts +174 -0
  391. package/src/data/behaviors.ts +2760 -622
  392. package/src/data/endpoint_catalog.ts +2695 -0
  393. package/src/data/exercises.ts +379 -4861
  394. package/src/data/garments.ts +28 -0
  395. package/src/data/sports.ts +239 -211
  396. package/src/lib/activity_list.ts +150 -0
  397. package/src/lib/activity_records.ts +213 -0
  398. package/src/lib/activity_results.ts +51 -0
  399. package/src/lib/activity_types.ts +176 -0
  400. package/src/lib/brand.ts +71 -0
  401. package/src/lib/clock.ts +16 -0
  402. package/src/lib/coach_session.ts +434 -0
  403. package/src/lib/dates.ts +18 -0
  404. package/src/lib/env_file.ts +191 -0
  405. package/src/lib/health_plans_write.ts +53 -0
  406. package/src/lib/heart_rate.ts +42 -0
  407. package/src/lib/highlight.ts +71 -0
  408. package/src/lib/install_links.ts +61 -0
  409. package/src/lib/instant.ts +54 -0
  410. package/src/lib/journal_behaviors.ts +453 -0
  411. package/src/lib/lift_catalog.ts +258 -0
  412. package/src/lib/lift_history.ts +75 -0
  413. package/src/lib/lift_sets.ts +164 -0
  414. package/src/lib/lift_tool.ts +25 -0
  415. package/src/lib/profile_settings.ts +102 -0
  416. package/src/lib/screen_content.ts +40 -0
  417. package/src/lib/timezone.ts +44 -8
  418. package/src/lib/walk.ts +4 -1
  419. package/src/lib/weight_readings.ts +82 -0
  420. package/src/projections/activities.ts +199 -0
  421. package/src/projections/alarm_schedule.ts +98 -0
  422. package/src/projections/coach.ts +218 -0
  423. package/src/projections/community.ts +187 -0
  424. package/src/projections/health_features.ts +191 -0
  425. package/src/projections/hormonal_insights.ts +277 -0
  426. package/src/projections/journal_v2.ts +173 -0
  427. package/src/projections/lift_cards.ts +131 -0
  428. package/src/projections/lift_exercise_v2.ts +147 -0
  429. package/src/projections/lift_library_v2.ts +77 -0
  430. package/src/projections/lift_sessions.ts +250 -0
  431. package/src/projections/memory.ts +100 -0
  432. package/src/projections/milestones.ts +133 -0
  433. package/src/projections/profile_v2.ts +166 -0
  434. package/src/projections/settings_v2.ts +179 -0
  435. package/src/projections/sleep.ts +7 -21
  436. package/src/projections/v2/calendar.ts +48 -0
  437. package/src/projections/v2/core_common.ts +95 -0
  438. package/src/projections/v2/core_home.ts +177 -0
  439. package/src/projections/v2/core_sleep_need.ts +37 -0
  440. package/src/projections/v2/day.ts +162 -0
  441. package/src/projections/v2/heart_rate.ts +73 -0
  442. package/src/projections/v2/history.ts +153 -0
  443. package/src/projections/v2/now.ts +59 -0
  444. package/src/projections/v2/recovery.ts +120 -0
  445. package/src/projections/v2/sleep.ts +281 -0
  446. package/src/projections/v2/strain.ts +63 -0
  447. package/src/projections/v2/stress.ts +111 -0
  448. package/src/projections/v2/trend.ts +242 -0
  449. package/src/projections/weekly_plan.ts +79 -0
  450. package/src/projections/weight_readings.ts +57 -0
  451. package/src/projections/workout.ts +36 -17
  452. package/src/schemas/activities.ts +136 -0
  453. package/src/schemas/alarm_schedule.ts +70 -0
  454. package/src/schemas/coach.ts +98 -8
  455. package/src/schemas/community.ts +77 -0
  456. package/src/schemas/health.ts +36 -0
  457. package/src/schemas/hormonal_insights.ts +108 -0
  458. package/src/schemas/journal.ts +4 -0
  459. package/src/schemas/journal_v2.ts +112 -0
  460. package/src/schemas/memory.ts +48 -0
  461. package/src/schemas/milestones.ts +48 -0
  462. package/src/schemas/profile_v2.ts +63 -0
  463. package/src/schemas/settings_v2.ts +63 -0
  464. package/src/schemas/sleep.ts +9 -1
  465. package/src/schemas/strength.ts +10 -1
  466. package/src/schemas/strength_v2.ts +210 -0
  467. package/src/schemas/v2/calendar.ts +8 -0
  468. package/src/schemas/v2/core_common.ts +50 -0
  469. package/src/schemas/v2/day.ts +79 -0
  470. package/src/schemas/v2/now.ts +25 -0
  471. package/src/schemas/v2/recovery.ts +38 -0
  472. package/src/schemas/v2/sleep.ts +46 -0
  473. package/src/schemas/v2/strain.ts +16 -0
  474. package/src/schemas/v2/stress.ts +20 -0
  475. package/src/schemas/v2/trend.ts +45 -0
  476. package/src/schemas/weekly_plan.ts +36 -0
  477. package/src/schemas/weight.ts +53 -0
  478. package/src/scripts/build_behaviors.ts +116 -0
  479. package/src/scripts/cognito_bootstrap.ts +10 -249
  480. package/src/scripts/gen_activity_catalogs.ts +121 -0
  481. package/src/server-admin.ts +171 -0
  482. package/src/server-http.ts +290 -53
  483. package/src/server.ts +130 -26
  484. package/src/tools/annotations.ts +25 -0
  485. package/src/tools/define.ts +53 -0
  486. package/src/tools/register.ts +64 -94
  487. package/src/tools/resources.ts +92 -0
  488. package/src/tools/slim_schema.ts +102 -0
  489. package/src/tools/v2/activity_delete.ts +61 -23
  490. package/src/tools/v2/activity_edit.ts +378 -0
  491. package/src/tools/v2/activity_log.ts +154 -0
  492. package/src/tools/v2/calendar.ts +22 -21
  493. package/src/tools/v2/coach_ask.ts +83 -119
  494. package/src/tools/v2/coach_history.ts +140 -0
  495. package/src/tools/v2/community.ts +137 -0
  496. package/src/tools/v2/cycle.ts +78 -20
  497. package/src/tools/v2/cycle_log.ts +291 -40
  498. package/src/tools/v2/day.ts +126 -25
  499. package/src/tools/v2/health.ts +114 -0
  500. package/src/tools/v2/hr_zones_set.ts +132 -45
  501. package/src/tools/v2/journal.ts +78 -23
  502. package/src/tools/v2/journal_log.ts +321 -69
  503. package/src/tools/v2/lift_exercise.ts +135 -27
  504. package/src/tools/v2/lift_exercise_set.ts +272 -0
  505. package/src/tools/v2/lift_library.ts +57 -21
  506. package/src/tools/v2/lift_log.ts +325 -83
  507. package/src/tools/v2/lift_sessions.ts +255 -0
  508. package/src/tools/v2/lift_template.ts +229 -0
  509. package/src/tools/v2/memory.ts +54 -0
  510. package/src/tools/v2/memory_set.ts +110 -0
  511. package/src/tools/v2/milestones.ts +59 -0
  512. package/src/tools/v2/now.ts +61 -0
  513. package/src/tools/v2/preferences.ts +61 -0
  514. package/src/tools/v2/preferences_set.ts +403 -0
  515. package/src/tools/v2/profile.ts +56 -26
  516. package/src/tools/v2/profile_update.ts +151 -60
  517. package/src/tools/v2/raw.ts +89 -36
  518. package/src/tools/v2/recovery.ts +44 -25
  519. package/src/tools/v2/sleep.ts +57 -18
  520. package/src/tools/v2/smart_alarm.ts +62 -24
  521. package/src/tools/v2/smart_alarm_set.ts +240 -90
  522. package/src/tools/v2/strain.ts +19 -16
  523. package/src/tools/v2/stress.ts +22 -17
  524. package/src/tools/v2/trend.ts +58 -20
  525. package/src/tools/v2/weekly_plan.ts +38 -0
  526. package/src/tools/v2/weekly_plan_set.ts +132 -0
  527. package/src/tools/v2/weight.ts +61 -0
  528. package/src/tools/v2/weight_log.ts +193 -0
  529. package/src/tools/v2/workouts.ts +156 -26
  530. package/src/version.ts +5 -0
  531. package/src/whoop/bff.ts +84 -0
  532. package/src/whoop/client.ts +448 -18
  533. package/src/whoop/cognito.ts +110 -99
  534. package/src/whoop/connect_page.ts +183 -0
  535. package/src/whoop/consent_font.ts +289 -0
  536. package/src/whoop/consent_logos.ts +24 -0
  537. package/src/whoop/consent_page.ts +326 -0
  538. package/src/whoop/constants.ts +5 -1
  539. package/src/whoop/device.ts +4 -4
  540. package/src/whoop/errors.ts +89 -4
  541. package/src/whoop/init_timezone.ts +57 -16
  542. package/src/whoop/installation.ts +16 -7
  543. package/src/whoop/json_out.ts +51 -6
  544. package/src/whoop/oauth_provider.ts +84 -91
  545. package/src/whoop/token_lock.ts +345 -0
  546. package/src/whoop/token_manager.ts +580 -53
  547. package/src/whoop/token_store.ts +341 -32
  548. package/src/whoop/units.ts +35 -0
  549. package/wrangler.jsonc +17 -0
  550. package/assets/banner.svg +0 -38
  551. package/dist/data/endpoints.js +0 -320
  552. package/dist/data/endpoints.js.map +0 -1
  553. package/dist/lib/format.js +0 -22
  554. package/dist/lib/format.js.map +0 -1
  555. package/dist/lib/stats.js +0 -32
  556. package/dist/lib/stats.js.map +0 -1
  557. package/dist/projections/calendar.js +0 -33
  558. package/dist/projections/calendar.js.map +0 -1
  559. package/dist/projections/communities.js +0 -105
  560. package/dist/projections/communities.js.map +0 -1
  561. package/dist/projections/cycle.js +0 -81
  562. package/dist/projections/cycle.js.map +0 -1
  563. package/dist/projections/hr_zones.js +0 -35
  564. package/dist/projections/hr_zones.js.map +0 -1
  565. package/dist/projections/journal.js +0 -56
  566. package/dist/projections/journal.js.map +0 -1
  567. package/dist/projections/leaderboard.js +0 -37
  568. package/dist/projections/leaderboard.js.map +0 -1
  569. package/dist/projections/lift_exercise.js +0 -22
  570. package/dist/projections/lift_exercise.js.map +0 -1
  571. package/dist/projections/lift_history.js +0 -133
  572. package/dist/projections/lift_history.js.map +0 -1
  573. package/dist/projections/lift_library.js +0 -71
  574. package/dist/projections/lift_library.js.map +0 -1
  575. package/dist/projections/lift_progression.js +0 -138
  576. package/dist/projections/lift_progression.js.map +0 -1
  577. package/dist/projections/lift_prs.js +0 -20
  578. package/dist/projections/lift_prs.js.map +0 -1
  579. package/dist/projections/live_hr.js +0 -19
  580. package/dist/projections/live_hr.js.map +0 -1
  581. package/dist/projections/live_state.js +0 -23
  582. package/dist/projections/live_state.js.map +0 -1
  583. package/dist/projections/live_stress.js +0 -11
  584. package/dist/projections/live_stress.js.map +0 -1
  585. package/dist/projections/performance_assessment.js +0 -15
  586. package/dist/projections/performance_assessment.js.map +0 -1
  587. package/dist/projections/profile.js +0 -59
  588. package/dist/projections/profile.js.map +0 -1
  589. package/dist/projections/recovery.js +0 -131
  590. package/dist/projections/recovery.js.map +0 -1
  591. package/dist/projections/sleep_need.js +0 -32
  592. package/dist/projections/sleep_need.js.map +0 -1
  593. package/dist/projections/smart_alarm.js +0 -45
  594. package/dist/projections/smart_alarm.js.map +0 -1
  595. package/dist/projections/strain.js +0 -128
  596. package/dist/projections/strain.js.map +0 -1
  597. package/dist/projections/stress.js +0 -86
  598. package/dist/projections/stress.js.map +0 -1
  599. package/dist/projections/today.js +0 -129
  600. package/dist/projections/today.js.map +0 -1
  601. package/dist/projections/trend.js +0 -149
  602. package/dist/projections/trend.js.map +0 -1
  603. package/dist/projections/workouts.js +0 -37
  604. package/dist/projections/workouts.js.map +0 -1
  605. package/dist/schemas/calendar.js +0 -12
  606. package/dist/schemas/calendar.js.map +0 -1
  607. package/dist/schemas/communities.js +0 -51
  608. package/dist/schemas/communities.js.map +0 -1
  609. package/dist/schemas/compare.js +0 -15
  610. package/dist/schemas/compare.js.map +0 -1
  611. package/dist/schemas/leaderboard.js +0 -25
  612. package/dist/schemas/leaderboard.js.map +0 -1
  613. package/dist/schemas/live.js +0 -26
  614. package/dist/schemas/live.js.map +0 -1
  615. package/dist/schemas/performance.js +0 -12
  616. package/dist/schemas/performance.js.map +0 -1
  617. package/dist/schemas/profile.js +0 -39
  618. package/dist/schemas/profile.js.map +0 -1
  619. package/dist/schemas/recovery.js +0 -27
  620. package/dist/schemas/recovery.js.map +0 -1
  621. package/dist/schemas/settings.js +0 -26
  622. package/dist/schemas/settings.js.map +0 -1
  623. package/dist/schemas/sleep_need.js +0 -15
  624. package/dist/schemas/sleep_need.js.map +0 -1
  625. package/dist/schemas/smart_alarm.js +0 -36
  626. package/dist/schemas/smart_alarm.js.map +0 -1
  627. package/dist/schemas/strain.js +0 -24
  628. package/dist/schemas/strain.js.map +0 -1
  629. package/dist/schemas/stress.js +0 -16
  630. package/dist/schemas/stress.js.map +0 -1
  631. package/dist/schemas/today.js +0 -38
  632. package/dist/schemas/today.js.map +0 -1
  633. package/dist/schemas/trend.js +0 -50
  634. package/dist/schemas/trend.js.map +0 -1
  635. package/dist/schemas/womens_health.js +0 -23
  636. package/dist/schemas/womens_health.js.map +0 -1
  637. package/dist/tools/v2/activity_create.js +0 -64
  638. package/dist/tools/v2/activity_create.js.map +0 -1
  639. package/dist/tools/v2/behavior_impact.js +0 -36
  640. package/dist/tools/v2/behavior_impact.js.map +0 -1
  641. package/dist/tools/v2/communities.js +0 -60
  642. package/dist/tools/v2/communities.js.map +0 -1
  643. package/dist/tools/v2/compare.js +0 -67
  644. package/dist/tools/v2/compare.js.map +0 -1
  645. package/dist/tools/v2/endpoints.js +0 -28
  646. package/dist/tools/v2/endpoints.js.map +0 -1
  647. package/dist/tools/v2/hidden_metric.js +0 -28
  648. package/dist/tools/v2/hidden_metric.js.map +0 -1
  649. package/dist/tools/v2/hr_zones.js +0 -24
  650. package/dist/tools/v2/hr_zones.js.map +0 -1
  651. package/dist/tools/v2/journal_autopop.js +0 -19
  652. package/dist/tools/v2/journal_autopop.js.map +0 -1
  653. package/dist/tools/v2/journal_catalog.js +0 -44
  654. package/dist/tools/v2/journal_catalog.js.map +0 -1
  655. package/dist/tools/v2/leaderboard.js +0 -79
  656. package/dist/tools/v2/leaderboard.js.map +0 -1
  657. package/dist/tools/v2/lift_catalog.js +0 -47
  658. package/dist/tools/v2/lift_catalog.js.map +0 -1
  659. package/dist/tools/v2/lift_custom_exercise.js +0 -108
  660. package/dist/tools/v2/lift_custom_exercise.js.map +0 -1
  661. package/dist/tools/v2/lift_history.js +0 -39
  662. package/dist/tools/v2/lift_history.js.map +0 -1
  663. package/dist/tools/v2/lift_progression.js +0 -30
  664. package/dist/tools/v2/lift_progression.js.map +0 -1
  665. package/dist/tools/v2/lift_prs.js +0 -21
  666. package/dist/tools/v2/lift_prs.js.map +0 -1
  667. package/dist/tools/v2/lift_template_save.js +0 -83
  668. package/dist/tools/v2/lift_template_save.js.map +0 -1
  669. package/dist/tools/v2/live_hr.js +0 -21
  670. package/dist/tools/v2/live_hr.js.map +0 -1
  671. package/dist/tools/v2/live_state.js +0 -21
  672. package/dist/tools/v2/live_state.js.map +0 -1
  673. package/dist/tools/v2/live_stress.js +0 -22
  674. package/dist/tools/v2/live_stress.js.map +0 -1
  675. package/dist/tools/v2/performance_assessment.js +0 -38
  676. package/dist/tools/v2/performance_assessment.js.map +0 -1
  677. package/dist/tools/v2/sleep_need.js +0 -21
  678. package/dist/tools/v2/sleep_need.js.map +0 -1
  679. package/dist/tools/v2/sports_catalog.js +0 -26
  680. package/dist/tools/v2/sports_catalog.js.map +0 -1
  681. package/dist/tools/v2/symptom_log.js +0 -72
  682. package/dist/tools/v2/symptom_log.js.map +0 -1
  683. package/dist/tools/v2/today.js +0 -30
  684. package/dist/tools/v2/today.js.map +0 -1
  685. package/dist/tools/v2/workout.js +0 -23
  686. package/dist/tools/v2/workout.js.map +0 -1
  687. package/dist/whoop/build_lift_body.js +0 -67
  688. package/dist/whoop/build_lift_body.js.map +0 -1
  689. package/dist/whoop/session_state.js +0 -34
  690. package/dist/whoop/session_state.js.map +0 -1
  691. package/dist/whoop/types.js +0 -82
  692. package/dist/whoop/types.js.map +0 -1
  693. package/src/data/endpoints.ts +0 -319
  694. package/src/lib/format.ts +0 -27
  695. package/src/lib/stats.ts +0 -35
  696. package/src/projections/calendar.ts +0 -46
  697. package/src/projections/communities.ts +0 -135
  698. package/src/projections/cycle.ts +0 -85
  699. package/src/projections/hr_zones.ts +0 -43
  700. package/src/projections/journal.ts +0 -55
  701. package/src/projections/leaderboard.ts +0 -49
  702. package/src/projections/lift_exercise.ts +0 -29
  703. package/src/projections/lift_history.ts +0 -140
  704. package/src/projections/lift_library.ts +0 -70
  705. package/src/projections/lift_progression.ts +0 -138
  706. package/src/projections/lift_prs.ts +0 -23
  707. package/src/projections/live_hr.ts +0 -21
  708. package/src/projections/live_state.ts +0 -28
  709. package/src/projections/live_stress.ts +0 -13
  710. package/src/projections/performance_assessment.ts +0 -19
  711. package/src/projections/profile.ts +0 -71
  712. package/src/projections/recovery.ts +0 -133
  713. package/src/projections/sleep_need.ts +0 -36
  714. package/src/projections/smart_alarm.ts +0 -70
  715. package/src/projections/strain.ts +0 -131
  716. package/src/projections/stress.ts +0 -95
  717. package/src/projections/today.ts +0 -166
  718. package/src/projections/trend.ts +0 -149
  719. package/src/projections/workouts.ts +0 -36
  720. package/src/schemas/calendar.ts +0 -13
  721. package/src/schemas/communities.ts +0 -56
  722. package/src/schemas/compare.ts +0 -17
  723. package/src/schemas/leaderboard.ts +0 -26
  724. package/src/schemas/live.ts +0 -31
  725. package/src/schemas/performance.ts +0 -13
  726. package/src/schemas/profile.ts +0 -40
  727. package/src/schemas/recovery.ts +0 -28
  728. package/src/schemas/settings.ts +0 -30
  729. package/src/schemas/sleep_need.ts +0 -16
  730. package/src/schemas/smart_alarm.ts +0 -40
  731. package/src/schemas/strain.ts +0 -25
  732. package/src/schemas/stress.ts +0 -17
  733. package/src/schemas/today.ts +0 -39
  734. package/src/schemas/trend.ts +0 -53
  735. package/src/schemas/womens_health.ts +0 -26
  736. package/src/tools/v2/activity_create.ts +0 -74
  737. package/src/tools/v2/behavior_impact.ts +0 -39
  738. package/src/tools/v2/communities.ts +0 -65
  739. package/src/tools/v2/compare.ts +0 -83
  740. package/src/tools/v2/endpoints.ts +0 -32
  741. package/src/tools/v2/hidden_metric.ts +0 -33
  742. package/src/tools/v2/hr_zones.ts +0 -29
  743. package/src/tools/v2/journal_autopop.ts +0 -26
  744. package/src/tools/v2/journal_catalog.ts +0 -48
  745. package/src/tools/v2/leaderboard.ts +0 -92
  746. package/src/tools/v2/lift_catalog.ts +0 -48
  747. package/src/tools/v2/lift_custom_exercise.ts +0 -117
  748. package/src/tools/v2/lift_history.ts +0 -52
  749. package/src/tools/v2/lift_progression.ts +0 -34
  750. package/src/tools/v2/lift_prs.ts +0 -26
  751. package/src/tools/v2/lift_template_save.ts +0 -89
  752. package/src/tools/v2/live_hr.ts +0 -26
  753. package/src/tools/v2/live_state.ts +0 -26
  754. package/src/tools/v2/live_stress.ts +0 -27
  755. package/src/tools/v2/performance_assessment.ts +0 -44
  756. package/src/tools/v2/sleep_need.ts +0 -26
  757. package/src/tools/v2/sports_catalog.ts +0 -32
  758. package/src/tools/v2/symptom_log.ts +0 -78
  759. package/src/tools/v2/today.ts +0 -35
  760. package/src/tools/v2/workout.ts +0 -28
  761. package/src/whoop/build_lift_body.ts +0 -84
  762. package/src/whoop/session_state.ts +0 -41
  763. package/src/whoop/types.ts +0 -89
@@ -0,0 +1,467 @@
1
+ // The AI clients totem connects to, and exactly what each one needs.
2
+ //
3
+ // One registry answers both questions a person has at the end of setup:
4
+ // - "do it for me": `totem local` / `totem connect <client>` write the
5
+ // client's config file or run its own `mcp add` command;
6
+ // - "what do I paste where": the connect card (connectCard) prints the same
7
+ // recipe for every client.
8
+ //
9
+ // A recipe is data: a JSON file entry, a command, or steps in the client's
10
+ // settings. Rendering (recipeLines, connectCard) is pure; only applyRecipe
11
+ // touches the disk or runs anything.
12
+ //
13
+ // Two ways to reach totem:
14
+ // - local: the client starts `node dist/server.js` itself (stdio). No secrets.
15
+ // - remote: the client talks to https://<server>/mcp and signs in through
16
+ // the server's own page with the connector password (OAuth). Nothing
17
+ // secret is pasted into a config file or a command line.
18
+ import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
19
+ import { basename, dirname, join } from "node:path";
20
+ import { appConfigDir } from "./spawn.js";
21
+ import { SERVER_NAME, claudeConnectorLink } from "../lib/install_links.js";
22
+ import { offerLink } from "./links.js";
23
+ import type { Io, Sys } from "./sys.js";
24
+ import { shellLines, type Kit } from "./ui.js";
25
+
26
+ export { SERVER_NAME };
27
+ export const FIRST_QUESTION = '"How did I sleep last night?"';
28
+
29
+ export type Target =
30
+ | { kind: "local"; node: string; serverJs: string }
31
+ | { kind: "remote"; mcpUrl: string };
32
+
33
+ export type Recipe =
34
+ /**
35
+ * Merge `entry` under `key` in a JSON config file. A dotted key ("mcp.servers") names a nested object.
36
+ * `twin`: another place an older version of the client keeps the same server; updated only if it is there.
37
+ * `fallback`: the client's own command, for a file totem won't rewrite (comments, trailing commas).
38
+ */
39
+ | {
40
+ kind: "file"; path: string; key: string; entry: Record<string, unknown>; after: string;
41
+ twin?: { key: string; entry: Record<string, unknown> };
42
+ fallback?: Extract<Recipe, { kind: "command" }>;
43
+ }
44
+ /**
45
+ * Run the client's own command; `then` are follow-up commands or steps.
46
+ * `remove` un-registers an earlier entry, so a re-run replaces it.
47
+ */
48
+ | {
49
+ kind: "command"; argv: string[]; remove: string[]; then: string[]; after: string;
50
+ /** Said before the command runs, when it needs the person (a sign-in page opens). */ before?: string;
51
+ /** Where to run it, when that matters (OpenCode writes the config of the folder it runs in). */ cwd?: string;
52
+ }
53
+ /** A link that opens the client with everything filled in; the person confirms there. */
54
+ | { kind: "link"; url: string; label: string; after: string }
55
+ /** Nothing to write: steps in the client's own settings. */
56
+ | { kind: "steps"; steps: string[] }
57
+ /** This client can't use this kind of install. */
58
+ | { kind: "unavailable"; why: string };
59
+
60
+ export interface Client {
61
+ id: string;
62
+ name: string;
63
+ /** Shown in the connect card (the rest are offered by `totem local` only). */
64
+ featured: boolean;
65
+ /** Is it installed on this machine? Decides the default choice. */
66
+ detect(sys: Sys): boolean;
67
+ recipe(target: Target, sys: Sys): Recipe;
68
+ }
69
+
70
+ const stdio = (t: Extract<Target, { kind: "local" }>): Record<string, unknown> => ({ command: t.node, args: [t.serverJs] });
71
+ /** claude.ai's own "Add custom connector" dialog, filled in. The connector also reaches Claude Desktop and the phone apps. */
72
+ const claudeLink = (mcpUrl: string): Recipe => ({
73
+ kind: "link", url: claudeConnectorLink(mcpUrl), label: "claude.ai",
74
+ after: "In claude.ai, press Add, then Connect, and enter the connector password. Claude Desktop and the phone apps get it too.",
75
+ });
76
+ const needsUrl = (name: string): Recipe => ({ kind: "unavailable", why: `${name} connects over the internet, so it needs a deployed server: totem cloud` });
77
+ const exists = (...parts: string[]): boolean => existsSync(join(...parts));
78
+
79
+ /** Arrow between menu levels, set per render (ASCII terminals get ">"). */
80
+ const PATH_SEP = "\u0001";
81
+ const menu = (...levels: string[]): string => levels.join(` ${PATH_SEP} `);
82
+
83
+ /**
84
+ * OpenCode's global config. It reads `opencode.json` and `opencode.jsonc`; use
85
+ * whichever the person already has, so totem never creates a second file
86
+ * beside theirs.
87
+ */
88
+ function opencodeConfig(sys: Sys): string {
89
+ const dir = join(sys.env.XDG_CONFIG_HOME || join(sys.home, ".config"), "opencode");
90
+ return !existsSync(join(dir, "opencode.json")) && existsSync(join(dir, "opencode.jsonc")) ? join(dir, "opencode.jsonc") : join(dir, "opencode.json");
91
+ }
92
+
93
+ // Checked on 2026-11-21 against each vendor's docs and, where installed, the
94
+ // tool's own --help (Claude Code 2.1, codex-cli 0.154, OpenCode 1.19 and 2.3,
95
+ // VS Code 1.142). What changed since v1:
96
+ // - ChatGPT and Codex are one desktop app (July 2026). The app, the Codex CLI
97
+ // and the IDE extension share ~/.codex/config.toml, so one `codex mcp add`
98
+ // covers all three. chatgpt.com adds outside servers through Plugins.
99
+ // - OpenCode 2 keeps servers under mcp.servers (1.19 reads that too) and has
100
+ // no `enabled` field.
101
+ // - VS Code reads the personal Copilot config, ~/.copilot/mcp-config.json
102
+ // (shared with Copilot CLI). Its profile mcp.json is the deprecated path.
103
+ // - Gemini CLI was retired in June 2026; Antigravity replaced it.
104
+ export const CLIENTS: Client[] = [
105
+ {
106
+ id: "claude", name: "claude.ai", featured: true,
107
+ detect: () => false,
108
+ recipe: (t) => t.kind === "local" ? needsUrl("claude.ai") : claudeLink(t.mcpUrl),
109
+ },
110
+ {
111
+ // A server reaches Claude Desktop through the same claude.ai connector.
112
+ id: "claude-desktop", name: "Claude Desktop", featured: true,
113
+ detect: (sys) => exists(appConfigDir(sys.platform, sys.home, sys.env), "Claude"),
114
+ recipe: (t, sys) => t.kind === "local"
115
+ ? { kind: "file", path: join(appConfigDir(sys.platform, sys.home, sys.env), "Claude", "claude_desktop_config.json"), key: "mcpServers", entry: stdio(t), after: "Quit and reopen Claude Desktop." }
116
+ : claudeLink(t.mcpUrl),
117
+ },
118
+ {
119
+ id: "claude-code", name: "Claude Code", featured: true,
120
+ detect: (sys) => sys.has("claude"),
121
+ recipe: (t) => t.kind === "local"
122
+ ? { kind: "command", argv: ["claude", "mcp", "add", "--scope", "user", SERVER_NAME, "--", t.node, t.serverJs], remove: ["claude", "mcp", "remove", "--scope", "user", SERVER_NAME], then: [], after: "Start a new Claude Code session." }
123
+ : { kind: "command", argv: ["claude", "mcp", "add", "--transport", "http", "--scope", "user", SERVER_NAME, t.mcpUrl], remove: ["claude", "mcp", "remove", "--scope", "user", SERVER_NAME], then: [`In Claude Code: /mcp ${PATH_SEP} ${SERVER_NAME} ${PATH_SEP} Authenticate`], after: `In Claude Code, run /mcp and authenticate ${SERVER_NAME}.` },
124
+ },
125
+ {
126
+ // The ChatGPT desktop app and the Codex CLI: one config, so one entry.
127
+ id: "chatgpt", name: "ChatGPT", featured: true,
128
+ detect: (sys) => sys.has("codex") || exists(sys.home, ".codex"),
129
+ recipe: (t, sys) => {
130
+ // Seen on chatgpt.com 2026-11-22: the Add menu at the top right of Plugins.
131
+ const web = `On chatgpt.com: ${menu("Plugins", "Add", "Add custom MCP server")}. Paste the URL, then Create as a plugin and Continue.`;
132
+ if (!sys.has("codex")) {
133
+ // The desktop app without the codex command: its own settings screen.
134
+ const add = menu("Settings", "MCP servers", "Add server");
135
+ return t.kind === "local"
136
+ ? { kind: "steps", steps: [`In the ChatGPT desktop app: ${add}. Choose STDIO and enter this command, then save and restart:`, `${t.node} ${t.serverJs}`] }
137
+ : { kind: "steps", steps: [`In the ChatGPT desktop app: ${add}. Choose Streamable HTTP, paste the URL, save and restart, then select Authenticate.`, web] };
138
+ }
139
+ return t.kind === "local"
140
+ ? { kind: "command", argv: ["codex", "mcp", "add", SERVER_NAME, "--", t.node, t.serverJs], remove: ["codex", "mcp", "remove", SERVER_NAME], then: [], after: "Restart ChatGPT, or start a new Codex session." }
141
+ // Seen with codex-cli 0.154: for a server that needs sign-in, `mcp add` opens
142
+ // the sign-in page itself and waits for it. `mcp login` is how to do it again.
143
+ : {
144
+ kind: "command", argv: ["codex", "mcp", "add", SERVER_NAME, "--url", t.mcpUrl], remove: ["codex", "mcp", "remove", SERVER_NAME],
145
+ then: [web],
146
+ before: "Codex opens the sign-in page in your browser and waits for you there.",
147
+ after: `If ChatGPT or Codex asks you to sign in again, run: codex mcp login ${SERVER_NAME}`,
148
+ };
149
+ },
150
+ },
151
+ {
152
+ id: "opencode", name: "OpenCode", featured: true,
153
+ detect: (sys) => sys.has("opencode") || exists(sys.env.XDG_CONFIG_HOME || join(sys.home, ".config"), "opencode"),
154
+ recipe: (t, sys) => {
155
+ const entry = t.kind === "local" ? { type: "local", command: [t.node, t.serverJs] } : { type: "remote", url: t.mcpUrl };
156
+ const after = t.kind === "local" ? "Start a new OpenCode session." : `In OpenCode, run /mcps and sign in to ${SERVER_NAME} (or run: opencode mcp auth ${SERVER_NAME}).`;
157
+ return {
158
+ // OpenCode 2 keeps servers under mcp.servers, and 1.18.24+ reads that too.
159
+ kind: "file", path: opencodeConfig(sys), key: "mcp.servers", entry, after,
160
+ // OpenCode 1's own `mcp add` writes mcp.whoop, and 1.x prefers that entry
161
+ // when both exist: keep it pointing at the same place.
162
+ twin: { key: "mcp", entry: { ...entry, enabled: true } },
163
+ // OpenCode allows comments and trailing commas, which totem won't
164
+ // rewrite; OpenCode's own command keeps them. Run from the global config
165
+ // folder, both generations write the global file.
166
+ fallback: {
167
+ kind: "command", cwd: dirname(opencodeConfig(sys)),
168
+ argv: t.kind === "local" ? ["opencode", "mcp", "add", SERVER_NAME, "--", t.node, t.serverJs] : ["opencode", "mcp", "add", SERVER_NAME, "--url", t.mcpUrl],
169
+ remove: [], then: [], after,
170
+ },
171
+ };
172
+ },
173
+ },
174
+ {
175
+ id: "cursor", name: "Cursor", featured: true,
176
+ detect: (sys) => exists(sys.home, ".cursor"),
177
+ recipe: (t, sys) => ({
178
+ kind: "file", path: join(sys.home, ".cursor", "mcp.json"), key: "mcpServers",
179
+ entry: t.kind === "local" ? stdio(t) : { url: t.mcpUrl },
180
+ after: t.kind === "local" ? `In Cursor: ${menu("Settings", "MCP")}, check ${SERVER_NAME} is on.` : `In Cursor: ${menu("Settings", "MCP", SERVER_NAME)}, then log in.`,
181
+ }),
182
+ },
183
+ {
184
+ id: "vscode", name: "VS Code", featured: false,
185
+ detect: (sys) => exists(appConfigDir(sys.platform, sys.home, sys.env), "Code") || exists(sys.env.COPILOT_HOME || join(sys.home, ".copilot")),
186
+ recipe: (t, sys) => ({
187
+ kind: "file", path: join(sys.env.COPILOT_HOME || join(sys.home, ".copilot"), "mcp-config.json"), key: "mcpServers",
188
+ entry: t.kind === "local" ? { type: "stdio", ...stdio(t), tools: ["*"] } : { type: "http", url: t.mcpUrl, tools: ["*"] },
189
+ after: `In VS Code, run "Chat: Open Customizations" and find ${SERVER_NAME} under MCP Servers${t.kind === "local" ? "" : ", then sign in"}.`,
190
+ }),
191
+ },
192
+ {
193
+ id: "antigravity", name: "Antigravity", featured: false,
194
+ detect: (sys) => sys.has("agy") || exists(sys.home, ".gemini", "antigravity") || exists(sys.home, ".gemini", "config"),
195
+ recipe: (t, sys) => ({
196
+ kind: "file", path: join(sys.home, ".gemini", "config", "mcp_config.json"), key: "mcpServers",
197
+ entry: t.kind === "local" ? stdio(t) : { serverUrl: t.mcpUrl },
198
+ after: `In Antigravity, type /mcp and check ${SERVER_NAME} is connected${t.kind === "local" ? "" : " (sign in when it asks)"}.`,
199
+ }),
200
+ },
201
+ ];
202
+
203
+ export function findClient(id: string): Client | undefined {
204
+ const wanted = id.toLowerCase().replace(/[\s_.]+/g, "-");
205
+ // Old names still find the client that took their place.
206
+ const alias: Record<string, string> = {
207
+ "claude.ai": "claude", "claude-ai": "claude", desktop: "claude-desktop", code: "claude-code",
208
+ codex: "chatgpt", "codex-cli": "chatgpt", "chatgpt-desktop": "chatgpt",
209
+ "vs-code": "vscode", copilot: "vscode",
210
+ gemini: "antigravity", "gemini-cli": "antigravity", agy: "antigravity",
211
+ };
212
+ return CLIENTS.find((cl) => cl.id === (alias[wanted] ?? wanted));
213
+ }
214
+
215
+ // ── rendering (pure) ─────────────────────────────────────────────────────────
216
+
217
+ /** `~/…` for a path under the home directory: shorter, and no username on screen. */
218
+ export function tildify(path: string, home: string): string {
219
+ return home && (path === home || path.startsWith(home + "/") || path.startsWith(home + "\\")) ? "~" + path.slice(home.length) : path;
220
+ }
221
+
222
+ const withSep = (text: string, k: Kit): string => text.split(PATH_SEP).join(k.g.arrow);
223
+
224
+ /** The JSON a file recipe adds, as the lines to paste. */
225
+ export function recipeJson(recipe: Extract<Recipe, { kind: "file" }>): string[] {
226
+ const nested = recipe.key.split(".").reduceRight<Record<string, unknown>>((inner, k) => ({ [k]: inner }), { [SERVER_NAME]: recipe.entry });
227
+ return JSON.stringify(nested, null, 2).split("\n");
228
+ }
229
+
230
+ /** What to paste where for one recipe, indented under the client's name. */
231
+ export function recipeLines(recipe: Recipe, k: Kit, sys: Pick<Sys, "home" | "platform">): string[] {
232
+ const width = k.style.width;
233
+ switch (recipe.kind) {
234
+ case "steps":
235
+ return recipe.steps.flatMap((s) => k.para(withSep(s, k), 2));
236
+ case "link":
237
+ return [...k.code([recipe.url], 2), ...k.para(withSep(recipe.after, k), 2)];
238
+ case "unavailable":
239
+ return k.hint(recipe.why, 2);
240
+ case "command":
241
+ return [
242
+ ...k.code(shellLines(recipe.argv, width - 4, sys.platform), 2),
243
+ ...recipe.then.flatMap((t) => (t.includes(PATH_SEP) ? k.para(withSep(t, k), 2) : k.code([t], 2))),
244
+ ];
245
+ case "file":
246
+ return [
247
+ ...k.para(`Add to ${tildify(recipe.path, sys.home)}:`, 2),
248
+ ...k.code(recipeJson(recipe), 2),
249
+ ];
250
+ }
251
+ }
252
+
253
+ export interface CardData {
254
+ target: Target;
255
+ /** Remote only: the connector password to show; null when totem doesn't have it. */
256
+ password?: string | null | undefined;
257
+ /** The password was put on the clipboard. */
258
+ copied?: boolean | undefined;
259
+ }
260
+
261
+ /**
262
+ * The connect card: for every featured client, exactly what to paste where.
263
+ * Remote installs get the URL and password once at the top; every client then
264
+ * needs only the URL, and signs in on the server's own page.
265
+ */
266
+ export function connectCard(k: Kit, sys: Pick<Sys, "home" | "platform" | "has" | "env">, data: CardData): string[] {
267
+ const { target } = data;
268
+ const lines: string[] = [];
269
+ if (target.kind === "remote") {
270
+ lines.push(...k.rows([
271
+ ["URL", k.c.link(target.mcpUrl)],
272
+ ["Password", data.password ? `${k.c.secret(data.password)}${data.copied ? k.c.dim(" (copied to the clipboard)") : ""}` : "the connector password from totem cloud"],
273
+ ]));
274
+ if (data.password) lines.push(...k.hint("totem shows the password this once. Save it in your password manager.", 2));
275
+ lines.push("");
276
+ lines.push(...k.para("Every client below opens a totem page in your browser. Check where access is going, then enter the password."));
277
+ } else {
278
+ lines.push(...k.para("Each client starts the server itself, on this computer:"));
279
+ lines.push(...k.code(shellLines([tildify(target.node, sys.home), tildify(target.serverJs, sys.home)], k.style.width - 4, sys.platform)));
280
+ }
281
+ if (target.kind === "local") {
282
+ // totem can set every one of these up itself: one command each, nothing to
283
+ // paste by hand.
284
+ const able = CLIENTS.filter((cl) => cl.recipe(target, sys as Sys).kind !== "unavailable");
285
+ lines.push("", ...k.para("totem sets a client up for you:"));
286
+ lines.push(...k.rows([
287
+ ...able.map((cl): [string, string] => [cl.name, k.c.accent(`totem connect ${cl.id}`)]),
288
+ ["Another client", k.c.accent("totem connect other")],
289
+ ]));
290
+ lines.push("", ...k.para("claude.ai connects over the internet, so it needs a deployed server: totem cloud"));
291
+ return lines;
292
+ }
293
+ for (const client of CLIENTS.filter((cl) => cl.featured)) {
294
+ const recipe = client.recipe(target, sys as Sys);
295
+ lines.push("", k.c.bold(client.name));
296
+ // A config file is totem's to write (one command), not JSON to paste by hand.
297
+ // `totem connect <client> --print` still shows the entry itself.
298
+ if (recipe.kind === "file") lines.push(...k.code([`totem connect ${client.id}`], 2), ...k.para(withSep(recipe.after, k), 2));
299
+ else lines.push(...recipeLines(recipe, k, sys));
300
+ }
301
+ lines.push("", k.c.bold("Another client"));
302
+ lines.push(...k.para("Give it the URL. If it can't sign in through a browser, send the server's token as a bearer header instead:", 2));
303
+ lines.push(...k.code(["Authorization: Bearer $(totem token)"], 2));
304
+ return lines;
305
+ }
306
+
307
+ // ── applying a recipe ────────────────────────────────────────────────────────
308
+
309
+ /** A client config that totem won't rewrite, with the reason to show the user. */
310
+ export class ConfigNotMergeableError extends Error {
311
+ constructor(message: string) {
312
+ super(message);
313
+ this.name = "ConfigNotMergeableError";
314
+ }
315
+ }
316
+
317
+ // Merge { [key]: { [name]: entry } } into a JSON config without clobbering the
318
+ // user's other servers/settings. Idempotent.
319
+ //
320
+ // The file belongs to another app, and it is read strictly: when it isn't a
321
+ // plain JSON object (VS Code's mcp.json allows comments; hand-edited configs
322
+ // pick up trailing commas) totem can't tell what is in it, so it throws
323
+ // ConfigNotMergeableError and leaves the file exactly as it was. It used to
324
+ // treat that as an empty config and write one back holding only its own entry,
325
+ // deleting every other server. Before a file is changed, the previous contents
326
+ // are copied to `<file>.totem-backup`.
327
+ export function mergeJsonConfig(
328
+ cfgPath: string, key: string, name: string, entry: Record<string, unknown>,
329
+ twin?: { key: string; entry: Record<string, unknown> },
330
+ ): { changed: boolean; backup?: string } {
331
+ let merged: Record<string, unknown> = {};
332
+ let previous: string | null = null;
333
+ if (existsSync(cfgPath)) {
334
+ previous = readFileSync(cfgPath, "utf8");
335
+ if (previous.trim() !== "") {
336
+ let parsed: unknown;
337
+ try {
338
+ parsed = JSON.parse(previous);
339
+ } catch {
340
+ throw new ConfigNotMergeableError(`${cfgPath} isn't plain JSON (comments or a trailing comma?), so totem left it untouched.`);
341
+ }
342
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
343
+ throw new ConfigNotMergeableError(`${cfgPath} doesn't hold a JSON object, so totem left it untouched.`);
344
+ }
345
+ merged = parsed as Record<string, unknown>;
346
+ }
347
+ }
348
+ // Walk to the object that holds the servers ("mcp.servers" is two levels), creating
349
+ // what is missing and refusing anything on the way that isn't an object.
350
+ let holder = merged;
351
+ const path = key.split(".");
352
+ path.forEach((part, i) => {
353
+ const existing = holder[part];
354
+ if (existing !== undefined && (typeof existing !== "object" || existing === null || Array.isArray(existing))) {
355
+ throw new ConfigNotMergeableError(`"${path.slice(0, i + 1).join(".")}" in ${cfgPath} isn't an object, so totem left the file untouched.`);
356
+ }
357
+ const next = { ...((existing as Record<string, unknown> | undefined) ?? {}) };
358
+ holder[part] = next;
359
+ holder = next;
360
+ });
361
+ holder[name] = entry;
362
+ // The same server where an older version of the client looks, if it is there.
363
+ if (twin) {
364
+ let at: unknown = merged;
365
+ for (const part of twin.key.split(".")) at = at && typeof at === "object" && !Array.isArray(at) ? (at as Record<string, unknown>)[part] : undefined;
366
+ const old = at && typeof at === "object" && !Array.isArray(at) ? (at as Record<string, unknown>)[name] : undefined;
367
+ if (old && typeof old === "object" && !Array.isArray(old)) (at as Record<string, unknown>)[name] = twin.entry;
368
+ }
369
+ const next = JSON.stringify(merged, null, 2) + "\n";
370
+ if (previous !== null && previous.trim() === next.trim()) return { changed: false };
371
+ mkdirSync(dirname(cfgPath), { recursive: true });
372
+ let backup: string | undefined;
373
+ if (previous !== null && previous.trim() !== "") {
374
+ backup = `${cfgPath}.totem-backup`;
375
+ copyFileSync(cfgPath, backup);
376
+ }
377
+ writeFileSync(cfgPath, next);
378
+ return backup ? { changed: true, backup } : { changed: true };
379
+ }
380
+
381
+ export interface Applied {
382
+ /** totem did it itself. */
383
+ done: boolean;
384
+ /** What the person still does, in order, for the closing "Next" box. */
385
+ steps: string[];
386
+ }
387
+
388
+ /**
389
+ * Do what a recipe says: write the file, or run the command. Prints the
390
+ * result. When totem can't (a config it won't touch, a CLI that isn't
391
+ * installed, a command that fails) it prints exactly what to do by hand and
392
+ * returns done: false.
393
+ */
394
+ export async function applyRecipe(io: Io, client: Client, recipe: Recipe): Promise<Applied> {
395
+ const { ui, sys } = io;
396
+ const k = ui.kit;
397
+ switch (recipe.kind) {
398
+ case "steps":
399
+ ui.lines(recipeLines(recipe, k, sys));
400
+ return { done: false, steps: [`Add the connector in ${client.name}.`] };
401
+ case "link":
402
+ await offerLink(io, `${recipe.label} with the server filled in`, recipe.url);
403
+ return { done: true, steps: [withSep(recipe.after, k)] };
404
+ case "unavailable":
405
+ ui.warn(recipe.why);
406
+ return { done: false, steps: [] };
407
+ case "file": {
408
+ const shown = tildify(recipe.path, sys.home);
409
+ try {
410
+ const { changed, backup } = mergeJsonConfig(recipe.path, recipe.key, SERVER_NAME, recipe.entry, recipe.twin);
411
+ ui.ok(changed ? `Added the ${SERVER_NAME} server to ${client.name}` : `${client.name} already has the ${SERVER_NAME} server`, shown);
412
+ if (backup) ui.hint(`The previous file is kept as ${basename(backup)}.`);
413
+ return { done: true, steps: [withSep(recipe.after, k)] };
414
+ } catch (error) {
415
+ if (!(error instanceof ConfigNotMergeableError)) throw error;
416
+ // The client's own command edits the file and keeps what totem can't.
417
+ if (recipe.fallback && sys.has(recipe.fallback.argv[0]!)) {
418
+ ui.hint(`${shown} has comments or trailing commas; letting ${client.name} add the server itself.`);
419
+ return applyRecipe(io, client, recipe.fallback);
420
+ }
421
+ ui.warn(error.message.split(recipe.path).join(shown));
422
+ ui.para(`Add this under "${recipe.key}" in that file yourself:`, 2);
423
+ ui.code(JSON.stringify({ [SERVER_NAME]: recipe.entry }, null, 2).split("\n"));
424
+ return { done: false, steps: [`Add the entry above to ${shown}.`, withSep(recipe.after, k)] };
425
+ }
426
+ }
427
+ case "command": {
428
+ const [cmd, ...args] = recipe.argv as [string, ...string[]];
429
+ const byHand = (): void => {
430
+ ui.code(shellLines(recipe.argv, k.style.width - 4, sys.platform));
431
+ };
432
+ if (!sys.has(cmd)) {
433
+ ui.warn(`The ${cmd} command isn't installed here. Once it is, run:`);
434
+ byHand();
435
+ return { done: false, steps: [`Install ${client.name}, then run the command above.`, withSep(recipe.after, k)] };
436
+ }
437
+ if (recipe.before) ui.hint(recipe.before);
438
+ const at = recipe.cwd ? { cwd: recipe.cwd } : undefined;
439
+ let r = await ui.task(`Adding ${SERVER_NAME} to ${client.name}`, () => sys.exec(cmd, args, at));
440
+ let replaced = false;
441
+ // An entry from an earlier setup may point somewhere else (another path,
442
+ // a server since replaced): put this one in its place.
443
+ if (r.code !== 0 && /already exists/i.test(`${r.stderr}\n${r.stdout}`) && recipe.remove.length) {
444
+ const [rm, ...rmArgs] = recipe.remove as [string, ...string[]];
445
+ r = await ui.task(`Replacing ${SERVER_NAME} in ${client.name}`, async () => {
446
+ await sys.exec(rm, rmArgs, at);
447
+ return sys.exec(cmd, args, at);
448
+ });
449
+ replaced = true;
450
+ }
451
+ if (r.code === 0) {
452
+ ui.ok(`${replaced ? "Replaced" : "Added"} the ${SERVER_NAME} server ${replaced ? "in" : "to"} ${client.name}`, `${cmd} ${args.slice(0, 2).join(" ")}`);
453
+ return { done: true, steps: [withSep(recipe.after, k)] };
454
+ }
455
+ ui.problem(`${cmd} ${args.slice(0, 2).join(" ")} failed.`, { detail: lastLine(r) });
456
+ ui.para("Run it yourself:", 2);
457
+ byHand();
458
+ return { done: false, steps: ["Run the command above.", withSep(recipe.after, k)] };
459
+ }
460
+ }
461
+ }
462
+
463
+ /** The last non-empty line a command printed: usually its error. */
464
+ export function lastLine(r: { stdout: string; stderr: string }): string {
465
+ const lines = `${r.stderr}\n${r.stdout}`.split("\n").map((l) => l.trim()).filter(Boolean);
466
+ return lines.length ? lines[lines.length - 1]! : "(it printed nothing)";
467
+ }
@@ -0,0 +1,100 @@
1
+ // Where `totem cloud` / `totem local` set the server up, so `totem auth`,
2
+ // `deploy`, `logs` and `status` know where to act. It also keeps the
3
+ // deployment's MCP_AUTH_TOKEN, which `totem auth` needs to push a login, so it
4
+ // is written 0600 like the token file.
5
+ //
6
+ // It lives in totem's config folder (configDir: ~/.config/totem/deploy.json),
7
+ // with the files that describe the deployment (fly.toml, deploy.env). Never in
8
+ // the install: `npm install -g` replaces that folder on every update, and a
9
+ // root-owned global install can't be written to at all. Installs before 2.0
10
+ // kept it as .totem-deploy.json in the install; that one is moved on first read.
11
+ import { existsSync, readFileSync, rmSync } from "node:fs";
12
+ import { join, resolve, win32 } from "node:path";
13
+ import { configDir, writeFileAtomic } from "../whoop/token_store.js";
14
+ import type { Sys } from "./sys.js";
15
+
16
+ export interface DeployRecord {
17
+ platform: string; // fly | railway | cloudflare | custom | local (cloudrun: deployed by totem 1.x)
18
+ app?: string | undefined; // app/project/service name (none for local)
19
+ url?: string | undefined;
20
+ /** The Railway service totem deployed into (records from 1.x have none). */
21
+ service?: string | undefined;
22
+ region?: string | undefined; // Fly region; Cloud Run (1.x) needs it to target the service
23
+ project?: string | undefined; // Cloud Run GCP project
24
+ /** The deployment's MCP_AUTH_TOKEN (gates POST /admin/tokens). */
25
+ mcp_auth_token?: string | undefined;
26
+ }
27
+
28
+ /** Whose config folder: the person running totem. */
29
+ export type Where = Pick<Sys, "home" | "env" | "platform">;
30
+
31
+ /** A file in totem's config folder. */
32
+ export function configFile(where: Where, name: string): string {
33
+ return (where.platform === "win32" ? win32.join : join)(configDir(where.env, where.platform, where.home), name);
34
+ }
35
+
36
+ export const deployRecordPath = (where: Where): string => configFile(where, "deploy.json");
37
+ /** Where installs before 2.0 kept it. */
38
+ const legacyPath = (root: string): string => resolve(root, ".totem-deploy.json");
39
+
40
+ function parse(path: string): DeployRecord | null {
41
+ if (!existsSync(path)) return null;
42
+ try {
43
+ const r = JSON.parse(readFileSync(path, "utf8")) as DeployRecord;
44
+ return r && typeof r.platform === "string" ? r : null;
45
+ } catch {
46
+ return null;
47
+ }
48
+ }
49
+
50
+ export function readDeployRecord(root: string, where: Where): DeployRecord | null {
51
+ const record = parse(deployRecordPath(where));
52
+ if (record) return record;
53
+ const legacy = parse(legacyPath(root));
54
+ // Move it to the config folder; if that can't be written, it still works from here.
55
+ if (legacy) try { writeDeployRecord(root, legacy, where); } catch { /* read-only: keep using it */ }
56
+ return legacy;
57
+ }
58
+
59
+ export function writeDeployRecord(root: string, record: DeployRecord, where: Where): void {
60
+ const body = Object.fromEntries(Object.entries(record).filter(([, v]) => v !== undefined));
61
+ writeFileAtomic(deployRecordPath(where), JSON.stringify(body, null, 2) + "\n");
62
+ // One record: a stale copy in the install would say something else.
63
+ try { rmSync(legacyPath(root), { force: true }); } catch { /* not ours to remove */ }
64
+ }
65
+
66
+ /** Merge fields into the existing record (no-op when there is none). */
67
+ export function updateDeployRecord(root: string, patch: Partial<DeployRecord>, where: Where): void {
68
+ const current = readDeployRecord(root, where);
69
+ if (current) writeDeployRecord(root, { ...current, ...patch }, where);
70
+ }
71
+
72
+ /** The host a server address belongs to, for a server totem didn't deploy itself (a Deploy button). */
73
+ export function platformForUrl(url: string): string {
74
+ let host = "";
75
+ try { host = new URL(url).hostname; } catch { return "custom"; }
76
+ if (host.endsWith(".workers.dev")) return "cloudflare";
77
+ if (host.endsWith(".up.railway.app")) return "railway";
78
+ if (host.endsWith(".fly.dev")) return "fly";
79
+ return "custom";
80
+ }
81
+
82
+ /** The install's version from its package.json, or null when it can't be read. */
83
+ export function installVersion(root: string): string | null {
84
+ try {
85
+ const v = (JSON.parse(readFileSync(resolve(root, "package.json"), "utf8")) as { version?: unknown }).version;
86
+ return typeof v === "string" && /^[0-9A-Za-z.+-]+$/.test(v) ? v : null;
87
+ } catch {
88
+ return null;
89
+ }
90
+ }
91
+
92
+ /**
93
+ * `docker build` for a self-hosted server, up to the folder to build (the
94
+ * caller adds it, quoted for the shell). It sets the build id to the install's
95
+ * version, so /health and `totem status` say which version runs instead of "dev".
96
+ */
97
+ export function dockerBuildArgs(root: string): string[] {
98
+ const version = installVersion(root);
99
+ return ["docker", "build", ...(version ? ["--build-arg", `TOTEM_BUILD_ID=${version}`] : []), "-t", "totem"];
100
+ }