@thebriangao/totem 1.4.4

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 (405) hide show
  1. package/.dockerignore +23 -0
  2. package/.env.example +66 -0
  3. package/CHANGELOG.md +283 -0
  4. package/CONTRIBUTING.md +149 -0
  5. package/Dockerfile +53 -0
  6. package/LICENSE +37 -0
  7. package/README.md +587 -0
  8. package/SECURITY.md +79 -0
  9. package/TOOLS.md +429 -0
  10. package/WHOOP.md +2673 -0
  11. package/assets/banner.svg +38 -0
  12. package/dist/cli/index.js +589 -0
  13. package/dist/cli/index.js.map +1 -0
  14. package/dist/cli/setup.js +1127 -0
  15. package/dist/cli/setup.js.map +1 -0
  16. package/dist/cli/ui.js +512 -0
  17. package/dist/cli/ui.js.map +1 -0
  18. package/dist/cli/update.js +465 -0
  19. package/dist/cli/update.js.map +1 -0
  20. package/dist/data/behaviors.js +2777 -0
  21. package/dist/data/behaviors.js.map +1 -0
  22. package/dist/data/endpoints.js +320 -0
  23. package/dist/data/endpoints.js.map +1 -0
  24. package/dist/data/exercises.js +4863 -0
  25. package/dist/data/exercises.js.map +1 -0
  26. package/dist/data/sports.js +212 -0
  27. package/dist/data/sports.js.map +1 -0
  28. package/dist/lib/concurrency.js +27 -0
  29. package/dist/lib/concurrency.js.map +1 -0
  30. package/dist/lib/dates.js +29 -0
  31. package/dist/lib/dates.js.map +1 -0
  32. package/dist/lib/format.js +22 -0
  33. package/dist/lib/format.js.map +1 -0
  34. package/dist/lib/stats.js +32 -0
  35. package/dist/lib/stats.js.map +1 -0
  36. package/dist/lib/timezone.js +249 -0
  37. package/dist/lib/timezone.js.map +1 -0
  38. package/dist/lib/walk.js +396 -0
  39. package/dist/lib/walk.js.map +1 -0
  40. package/dist/projections/behavior_impact.js +96 -0
  41. package/dist/projections/behavior_impact.js.map +1 -0
  42. package/dist/projections/calendar.js +33 -0
  43. package/dist/projections/calendar.js.map +1 -0
  44. package/dist/projections/communities.js +105 -0
  45. package/dist/projections/communities.js.map +1 -0
  46. package/dist/projections/cycle.js +81 -0
  47. package/dist/projections/cycle.js.map +1 -0
  48. package/dist/projections/hr_zones.js +35 -0
  49. package/dist/projections/hr_zones.js.map +1 -0
  50. package/dist/projections/journal.js +56 -0
  51. package/dist/projections/journal.js.map +1 -0
  52. package/dist/projections/leaderboard.js +37 -0
  53. package/dist/projections/leaderboard.js.map +1 -0
  54. package/dist/projections/lift_exercise.js +22 -0
  55. package/dist/projections/lift_exercise.js.map +1 -0
  56. package/dist/projections/lift_history.js +133 -0
  57. package/dist/projections/lift_history.js.map +1 -0
  58. package/dist/projections/lift_library.js +71 -0
  59. package/dist/projections/lift_library.js.map +1 -0
  60. package/dist/projections/lift_progression.js +138 -0
  61. package/dist/projections/lift_progression.js.map +1 -0
  62. package/dist/projections/lift_prs.js +20 -0
  63. package/dist/projections/lift_prs.js.map +1 -0
  64. package/dist/projections/live_hr.js +19 -0
  65. package/dist/projections/live_hr.js.map +1 -0
  66. package/dist/projections/live_state.js +23 -0
  67. package/dist/projections/live_state.js.map +1 -0
  68. package/dist/projections/live_stress.js +11 -0
  69. package/dist/projections/live_stress.js.map +1 -0
  70. package/dist/projections/performance_assessment.js +15 -0
  71. package/dist/projections/performance_assessment.js.map +1 -0
  72. package/dist/projections/profile.js +59 -0
  73. package/dist/projections/profile.js.map +1 -0
  74. package/dist/projections/recovery.js +131 -0
  75. package/dist/projections/recovery.js.map +1 -0
  76. package/dist/projections/sleep.js +256 -0
  77. package/dist/projections/sleep.js.map +1 -0
  78. package/dist/projections/sleep_need.js +32 -0
  79. package/dist/projections/sleep_need.js.map +1 -0
  80. package/dist/projections/smart_alarm.js +45 -0
  81. package/dist/projections/smart_alarm.js.map +1 -0
  82. package/dist/projections/strain.js +128 -0
  83. package/dist/projections/strain.js.map +1 -0
  84. package/dist/projections/stress.js +86 -0
  85. package/dist/projections/stress.js.map +1 -0
  86. package/dist/projections/today.js +129 -0
  87. package/dist/projections/today.js.map +1 -0
  88. package/dist/projections/trend.js +149 -0
  89. package/dist/projections/trend.js.map +1 -0
  90. package/dist/projections/workout.js +161 -0
  91. package/dist/projections/workout.js.map +1 -0
  92. package/dist/projections/workouts.js +37 -0
  93. package/dist/projections/workouts.js.map +1 -0
  94. package/dist/schemas/calendar.js +12 -0
  95. package/dist/schemas/calendar.js.map +1 -0
  96. package/dist/schemas/coach.js +11 -0
  97. package/dist/schemas/coach.js.map +1 -0
  98. package/dist/schemas/communities.js +51 -0
  99. package/dist/schemas/communities.js.map +1 -0
  100. package/dist/schemas/compare.js +15 -0
  101. package/dist/schemas/compare.js.map +1 -0
  102. package/dist/schemas/escape.js +18 -0
  103. package/dist/schemas/escape.js.map +1 -0
  104. package/dist/schemas/journal.js +69 -0
  105. package/dist/schemas/journal.js.map +1 -0
  106. package/dist/schemas/leaderboard.js +25 -0
  107. package/dist/schemas/leaderboard.js.map +1 -0
  108. package/dist/schemas/live.js +26 -0
  109. package/dist/schemas/live.js.map +1 -0
  110. package/dist/schemas/performance.js +12 -0
  111. package/dist/schemas/performance.js.map +1 -0
  112. package/dist/schemas/primitives.js +29 -0
  113. package/dist/schemas/primitives.js.map +1 -0
  114. package/dist/schemas/profile.js +39 -0
  115. package/dist/schemas/profile.js.map +1 -0
  116. package/dist/schemas/recovery.js +27 -0
  117. package/dist/schemas/recovery.js.map +1 -0
  118. package/dist/schemas/settings.js +26 -0
  119. package/dist/schemas/settings.js.map +1 -0
  120. package/dist/schemas/sleep.js +38 -0
  121. package/dist/schemas/sleep.js.map +1 -0
  122. package/dist/schemas/sleep_need.js +15 -0
  123. package/dist/schemas/sleep_need.js.map +1 -0
  124. package/dist/schemas/smart_alarm.js +36 -0
  125. package/dist/schemas/smart_alarm.js.map +1 -0
  126. package/dist/schemas/strain.js +24 -0
  127. package/dist/schemas/strain.js.map +1 -0
  128. package/dist/schemas/strength.js +170 -0
  129. package/dist/schemas/strength.js.map +1 -0
  130. package/dist/schemas/stress.js +16 -0
  131. package/dist/schemas/stress.js.map +1 -0
  132. package/dist/schemas/today.js +38 -0
  133. package/dist/schemas/today.js.map +1 -0
  134. package/dist/schemas/trend.js +50 -0
  135. package/dist/schemas/trend.js.map +1 -0
  136. package/dist/schemas/womens_health.js +23 -0
  137. package/dist/schemas/womens_health.js.map +1 -0
  138. package/dist/schemas/workouts.js +50 -0
  139. package/dist/schemas/workouts.js.map +1 -0
  140. package/dist/scripts/cognito_bootstrap.js +278 -0
  141. package/dist/scripts/cognito_bootstrap.js.map +1 -0
  142. package/dist/server-http.js +221 -0
  143. package/dist/server-http.js.map +1 -0
  144. package/dist/server.js +74 -0
  145. package/dist/server.js.map +1 -0
  146. package/dist/tools/register.js +117 -0
  147. package/dist/tools/register.js.map +1 -0
  148. package/dist/tools/v2/activity_create.js +64 -0
  149. package/dist/tools/v2/activity_create.js.map +1 -0
  150. package/dist/tools/v2/activity_delete.js +26 -0
  151. package/dist/tools/v2/activity_delete.js.map +1 -0
  152. package/dist/tools/v2/behavior_impact.js +36 -0
  153. package/dist/tools/v2/behavior_impact.js.map +1 -0
  154. package/dist/tools/v2/calendar.js +28 -0
  155. package/dist/tools/v2/calendar.js.map +1 -0
  156. package/dist/tools/v2/coach_ask.js +117 -0
  157. package/dist/tools/v2/coach_ask.js.map +1 -0
  158. package/dist/tools/v2/communities.js +60 -0
  159. package/dist/tools/v2/communities.js.map +1 -0
  160. package/dist/tools/v2/compare.js +67 -0
  161. package/dist/tools/v2/compare.js.map +1 -0
  162. package/dist/tools/v2/cycle.js +23 -0
  163. package/dist/tools/v2/cycle.js.map +1 -0
  164. package/dist/tools/v2/cycle_log.js +43 -0
  165. package/dist/tools/v2/cycle_log.js.map +1 -0
  166. package/dist/tools/v2/day.js +32 -0
  167. package/dist/tools/v2/day.js.map +1 -0
  168. package/dist/tools/v2/endpoints.js +28 -0
  169. package/dist/tools/v2/endpoints.js.map +1 -0
  170. package/dist/tools/v2/hidden_metric.js +28 -0
  171. package/dist/tools/v2/hidden_metric.js.map +1 -0
  172. package/dist/tools/v2/hr_zones.js +24 -0
  173. package/dist/tools/v2/hr_zones.js.map +1 -0
  174. package/dist/tools/v2/hr_zones_set.js +51 -0
  175. package/dist/tools/v2/hr_zones_set.js.map +1 -0
  176. package/dist/tools/v2/journal.js +26 -0
  177. package/dist/tools/v2/journal.js.map +1 -0
  178. package/dist/tools/v2/journal_autopop.js +19 -0
  179. package/dist/tools/v2/journal_autopop.js.map +1 -0
  180. package/dist/tools/v2/journal_catalog.js +44 -0
  181. package/dist/tools/v2/journal_catalog.js.map +1 -0
  182. package/dist/tools/v2/journal_log.js +78 -0
  183. package/dist/tools/v2/journal_log.js.map +1 -0
  184. package/dist/tools/v2/leaderboard.js +79 -0
  185. package/dist/tools/v2/leaderboard.js.map +1 -0
  186. package/dist/tools/v2/lift_catalog.js +47 -0
  187. package/dist/tools/v2/lift_catalog.js.map +1 -0
  188. package/dist/tools/v2/lift_custom_exercise.js +108 -0
  189. package/dist/tools/v2/lift_custom_exercise.js.map +1 -0
  190. package/dist/tools/v2/lift_exercise.js +31 -0
  191. package/dist/tools/v2/lift_exercise.js.map +1 -0
  192. package/dist/tools/v2/lift_history.js +39 -0
  193. package/dist/tools/v2/lift_history.js.map +1 -0
  194. package/dist/tools/v2/lift_library.js +22 -0
  195. package/dist/tools/v2/lift_library.js.map +1 -0
  196. package/dist/tools/v2/lift_log.js +95 -0
  197. package/dist/tools/v2/lift_log.js.map +1 -0
  198. package/dist/tools/v2/lift_progression.js +30 -0
  199. package/dist/tools/v2/lift_progression.js.map +1 -0
  200. package/dist/tools/v2/lift_prs.js +21 -0
  201. package/dist/tools/v2/lift_prs.js.map +1 -0
  202. package/dist/tools/v2/lift_template_save.js +83 -0
  203. package/dist/tools/v2/lift_template_save.js.map +1 -0
  204. package/dist/tools/v2/live_hr.js +21 -0
  205. package/dist/tools/v2/live_hr.js.map +1 -0
  206. package/dist/tools/v2/live_state.js +21 -0
  207. package/dist/tools/v2/live_state.js.map +1 -0
  208. package/dist/tools/v2/live_stress.js +22 -0
  209. package/dist/tools/v2/live_stress.js.map +1 -0
  210. package/dist/tools/v2/performance_assessment.js +38 -0
  211. package/dist/tools/v2/performance_assessment.js.map +1 -0
  212. package/dist/tools/v2/profile.js +28 -0
  213. package/dist/tools/v2/profile.js.map +1 -0
  214. package/dist/tools/v2/profile_update.js +66 -0
  215. package/dist/tools/v2/profile_update.js.map +1 -0
  216. package/dist/tools/v2/raw.js +43 -0
  217. package/dist/tools/v2/raw.js.map +1 -0
  218. package/dist/tools/v2/recovery.js +31 -0
  219. package/dist/tools/v2/recovery.js.map +1 -0
  220. package/dist/tools/v2/sleep.js +23 -0
  221. package/dist/tools/v2/sleep.js.map +1 -0
  222. package/dist/tools/v2/sleep_need.js +21 -0
  223. package/dist/tools/v2/sleep_need.js.map +1 -0
  224. package/dist/tools/v2/smart_alarm.js +24 -0
  225. package/dist/tools/v2/smart_alarm.js.map +1 -0
  226. package/dist/tools/v2/smart_alarm_set.js +94 -0
  227. package/dist/tools/v2/smart_alarm_set.js.map +1 -0
  228. package/dist/tools/v2/sports_catalog.js +26 -0
  229. package/dist/tools/v2/sports_catalog.js.map +1 -0
  230. package/dist/tools/v2/strain.js +23 -0
  231. package/dist/tools/v2/strain.js.map +1 -0
  232. package/dist/tools/v2/stress.js +23 -0
  233. package/dist/tools/v2/stress.js.map +1 -0
  234. package/dist/tools/v2/symptom_log.js +72 -0
  235. package/dist/tools/v2/symptom_log.js.map +1 -0
  236. package/dist/tools/v2/today.js +30 -0
  237. package/dist/tools/v2/today.js.map +1 -0
  238. package/dist/tools/v2/trend.js +26 -0
  239. package/dist/tools/v2/trend.js.map +1 -0
  240. package/dist/tools/v2/workout.js +23 -0
  241. package/dist/tools/v2/workout.js.map +1 -0
  242. package/dist/tools/v2/workouts.js +32 -0
  243. package/dist/tools/v2/workouts.js.map +1 -0
  244. package/dist/whoop/build_lift_body.js +67 -0
  245. package/dist/whoop/build_lift_body.js.map +1 -0
  246. package/dist/whoop/client.js +84 -0
  247. package/dist/whoop/client.js.map +1 -0
  248. package/dist/whoop/cognito.js +110 -0
  249. package/dist/whoop/cognito.js.map +1 -0
  250. package/dist/whoop/constants.js +4 -0
  251. package/dist/whoop/constants.js.map +1 -0
  252. package/dist/whoop/device.js +103 -0
  253. package/dist/whoop/device.js.map +1 -0
  254. package/dist/whoop/errors.js +42 -0
  255. package/dist/whoop/errors.js.map +1 -0
  256. package/dist/whoop/init_timezone.js +62 -0
  257. package/dist/whoop/init_timezone.js.map +1 -0
  258. package/dist/whoop/installation.js +62 -0
  259. package/dist/whoop/installation.js.map +1 -0
  260. package/dist/whoop/json_out.js +9 -0
  261. package/dist/whoop/json_out.js.map +1 -0
  262. package/dist/whoop/oauth_provider.js +297 -0
  263. package/dist/whoop/oauth_provider.js.map +1 -0
  264. package/dist/whoop/session_state.js +34 -0
  265. package/dist/whoop/session_state.js.map +1 -0
  266. package/dist/whoop/token_manager.js +60 -0
  267. package/dist/whoop/token_manager.js.map +1 -0
  268. package/dist/whoop/token_store.js +47 -0
  269. package/dist/whoop/token_store.js.map +1 -0
  270. package/dist/whoop/types.js +82 -0
  271. package/dist/whoop/types.js.map +1 -0
  272. package/dist/whoop/write_safety.js +23 -0
  273. package/dist/whoop/write_safety.js.map +1 -0
  274. package/package.json +66 -0
  275. package/src/cli/index.ts +597 -0
  276. package/src/cli/setup.ts +1106 -0
  277. package/src/cli/ui.ts +505 -0
  278. package/src/cli/update.ts +442 -0
  279. package/src/data/behaviors.ts +2784 -0
  280. package/src/data/endpoints.ts +319 -0
  281. package/src/data/exercises.ts +4868 -0
  282. package/src/data/sports.ts +218 -0
  283. package/src/lib/concurrency.ts +28 -0
  284. package/src/lib/dates.ts +35 -0
  285. package/src/lib/format.ts +27 -0
  286. package/src/lib/stats.ts +35 -0
  287. package/src/lib/timezone.ts +269 -0
  288. package/src/lib/walk.ts +418 -0
  289. package/src/projections/behavior_impact.ts +98 -0
  290. package/src/projections/calendar.ts +46 -0
  291. package/src/projections/communities.ts +135 -0
  292. package/src/projections/cycle.ts +85 -0
  293. package/src/projections/hr_zones.ts +43 -0
  294. package/src/projections/journal.ts +55 -0
  295. package/src/projections/leaderboard.ts +49 -0
  296. package/src/projections/lift_exercise.ts +29 -0
  297. package/src/projections/lift_history.ts +140 -0
  298. package/src/projections/lift_library.ts +70 -0
  299. package/src/projections/lift_progression.ts +138 -0
  300. package/src/projections/lift_prs.ts +23 -0
  301. package/src/projections/live_hr.ts +21 -0
  302. package/src/projections/live_state.ts +28 -0
  303. package/src/projections/live_stress.ts +13 -0
  304. package/src/projections/performance_assessment.ts +19 -0
  305. package/src/projections/profile.ts +71 -0
  306. package/src/projections/recovery.ts +133 -0
  307. package/src/projections/sleep.ts +276 -0
  308. package/src/projections/sleep_need.ts +36 -0
  309. package/src/projections/smart_alarm.ts +70 -0
  310. package/src/projections/strain.ts +131 -0
  311. package/src/projections/stress.ts +95 -0
  312. package/src/projections/today.ts +166 -0
  313. package/src/projections/trend.ts +149 -0
  314. package/src/projections/workout.ts +168 -0
  315. package/src/projections/workouts.ts +36 -0
  316. package/src/schemas/calendar.ts +13 -0
  317. package/src/schemas/coach.ts +11 -0
  318. package/src/schemas/communities.ts +56 -0
  319. package/src/schemas/compare.ts +17 -0
  320. package/src/schemas/escape.ts +21 -0
  321. package/src/schemas/journal.ts +80 -0
  322. package/src/schemas/leaderboard.ts +26 -0
  323. package/src/schemas/live.ts +31 -0
  324. package/src/schemas/performance.ts +13 -0
  325. package/src/schemas/primitives.ts +35 -0
  326. package/src/schemas/profile.ts +40 -0
  327. package/src/schemas/recovery.ts +28 -0
  328. package/src/schemas/settings.ts +30 -0
  329. package/src/schemas/sleep.ts +40 -0
  330. package/src/schemas/sleep_need.ts +16 -0
  331. package/src/schemas/smart_alarm.ts +40 -0
  332. package/src/schemas/strain.ts +25 -0
  333. package/src/schemas/strength.ts +189 -0
  334. package/src/schemas/stress.ts +17 -0
  335. package/src/schemas/today.ts +39 -0
  336. package/src/schemas/trend.ts +53 -0
  337. package/src/schemas/womens_health.ts +26 -0
  338. package/src/schemas/workouts.ts +55 -0
  339. package/src/scripts/cognito_bootstrap.ts +249 -0
  340. package/src/server-http.ts +241 -0
  341. package/src/server.ts +85 -0
  342. package/src/tools/register.ts +120 -0
  343. package/src/tools/v2/activity_create.ts +74 -0
  344. package/src/tools/v2/activity_delete.ts +33 -0
  345. package/src/tools/v2/behavior_impact.ts +39 -0
  346. package/src/tools/v2/calendar.ts +33 -0
  347. package/src/tools/v2/coach_ask.ts +131 -0
  348. package/src/tools/v2/communities.ts +65 -0
  349. package/src/tools/v2/compare.ts +83 -0
  350. package/src/tools/v2/cycle.ts +28 -0
  351. package/src/tools/v2/cycle_log.ts +51 -0
  352. package/src/tools/v2/day.ts +37 -0
  353. package/src/tools/v2/endpoints.ts +32 -0
  354. package/src/tools/v2/hidden_metric.ts +33 -0
  355. package/src/tools/v2/hr_zones.ts +29 -0
  356. package/src/tools/v2/hr_zones_set.ts +57 -0
  357. package/src/tools/v2/journal.ts +31 -0
  358. package/src/tools/v2/journal_autopop.ts +26 -0
  359. package/src/tools/v2/journal_catalog.ts +48 -0
  360. package/src/tools/v2/journal_log.ts +84 -0
  361. package/src/tools/v2/leaderboard.ts +92 -0
  362. package/src/tools/v2/lift_catalog.ts +48 -0
  363. package/src/tools/v2/lift_custom_exercise.ts +117 -0
  364. package/src/tools/v2/lift_exercise.ts +35 -0
  365. package/src/tools/v2/lift_history.ts +52 -0
  366. package/src/tools/v2/lift_library.ts +28 -0
  367. package/src/tools/v2/lift_log.ts +102 -0
  368. package/src/tools/v2/lift_progression.ts +34 -0
  369. package/src/tools/v2/lift_prs.ts +26 -0
  370. package/src/tools/v2/lift_template_save.ts +89 -0
  371. package/src/tools/v2/live_hr.ts +26 -0
  372. package/src/tools/v2/live_state.ts +26 -0
  373. package/src/tools/v2/live_stress.ts +27 -0
  374. package/src/tools/v2/performance_assessment.ts +44 -0
  375. package/src/tools/v2/profile.ts +33 -0
  376. package/src/tools/v2/profile_update.ts +76 -0
  377. package/src/tools/v2/raw.ts +50 -0
  378. package/src/tools/v2/recovery.ts +36 -0
  379. package/src/tools/v2/sleep.ts +28 -0
  380. package/src/tools/v2/sleep_need.ts +26 -0
  381. package/src/tools/v2/smart_alarm.ts +29 -0
  382. package/src/tools/v2/smart_alarm_set.ts +103 -0
  383. package/src/tools/v2/sports_catalog.ts +32 -0
  384. package/src/tools/v2/strain.ts +28 -0
  385. package/src/tools/v2/stress.ts +28 -0
  386. package/src/tools/v2/symptom_log.ts +78 -0
  387. package/src/tools/v2/today.ts +35 -0
  388. package/src/tools/v2/trend.ts +31 -0
  389. package/src/tools/v2/workout.ts +28 -0
  390. package/src/tools/v2/workouts.ts +37 -0
  391. package/src/whoop/build_lift_body.ts +84 -0
  392. package/src/whoop/client.ts +118 -0
  393. package/src/whoop/cognito.ts +150 -0
  394. package/src/whoop/constants.ts +3 -0
  395. package/src/whoop/device.ts +106 -0
  396. package/src/whoop/errors.ts +41 -0
  397. package/src/whoop/init_timezone.ts +64 -0
  398. package/src/whoop/installation.ts +58 -0
  399. package/src/whoop/json_out.ts +9 -0
  400. package/src/whoop/oauth_provider.ts +377 -0
  401. package/src/whoop/session_state.ts +41 -0
  402. package/src/whoop/token_manager.ts +76 -0
  403. package/src/whoop/token_store.ts +45 -0
  404. package/src/whoop/types.ts +89 -0
  405. package/src/whoop/write_safety.ts +30 -0
@@ -0,0 +1,218 @@
1
+ // AUTO-GENERATED from /activities-service/v1/sports/history?countryCode=US
2
+ // 203 entries with numeric sport_id ↔ display name mapping.
3
+ // Used by whoop_activity_create. Regenerate via:
4
+ // DUMMY_EMAIL=... DUMMY_PASSWORD=... npx tsx src/scripts/verify_sport_ids.ts
5
+
6
+ export interface Sport {
7
+ readonly id: number;
8
+ readonly name: string;
9
+ }
10
+
11
+ export const SPORTS: readonly Sport[] = [
12
+ { id: -1, name: "Activity" },
13
+ { id: 0, name: "Running" },
14
+ { id: 1, name: "Cycling" },
15
+ { id: 16, name: "Baseball" },
16
+ { id: 17, name: "Basketball" },
17
+ { id: 18, name: "Rowing" },
18
+ { id: 19, name: "Fencing" },
19
+ { id: 20, name: "Field Hockey" },
20
+ { id: 21, name: "American Football" },
21
+ { id: 22, name: "Golf" },
22
+ { id: 24, name: "Ice Hockey" },
23
+ { id: 25, name: "Lacrosse" },
24
+ { id: 27, name: "Rugby" },
25
+ { id: 28, name: "Sailing" },
26
+ { id: 29, name: "Skiing" },
27
+ { id: 30, name: "Soccer" },
28
+ { id: 31, name: "Softball" },
29
+ { id: 32, name: "Squash" },
30
+ { id: 33, name: "Swimming" },
31
+ { id: 34, name: "Tennis" },
32
+ { id: 35, name: "Track & Field" },
33
+ { id: 36, name: "Volleyball" },
34
+ { id: 37, name: "Water Polo" },
35
+ { id: 38, name: "Wrestling" },
36
+ { id: 39, name: "Boxing" },
37
+ { id: 42, name: "Dance" },
38
+ { id: 43, name: "Pilates" },
39
+ { id: 44, name: "Yoga" },
40
+ { id: 45, name: "Weightlifting" },
41
+ { id: 46, name: "Canoeing" },
42
+ { id: 47, name: "Cross Country Skiing" },
43
+ { id: 48, name: "Functional Fitness" },
44
+ { id: 49, name: "Duathlon" },
45
+ { id: 50, name: "Machine Workout" },
46
+ { id: 51, name: "Gymnastics" },
47
+ { id: 52, name: "Hiking" },
48
+ { id: 53, name: "Horseback Riding" },
49
+ { id: 54, name: "Jogging" },
50
+ { id: 55, name: "Kayaking" },
51
+ { id: 56, name: "Martial Arts" },
52
+ { id: 57, name: "Mountain Biking" },
53
+ { id: 58, name: "Obstacle Racing" },
54
+ { id: 59, name: "Powerlifting" },
55
+ { id: 60, name: "Rock Climbing" },
56
+ { id: 61, name: "Paddleboarding" },
57
+ { id: 62, name: "Triathlon" },
58
+ { id: 63, name: "Walking" },
59
+ { id: 64, name: "Surfing" },
60
+ { id: 65, name: "Elliptical" },
61
+ { id: 66, name: "Stairmaster" },
62
+ { id: 67, name: "Plyometrics" },
63
+ { id: 68, name: "Spinning" },
64
+ { id: 69, name: "Sex" },
65
+ { id: 70, name: "Meditation" },
66
+ { id: 71, name: "Other" },
67
+ { id: 72, name: "Pit Practice" },
68
+ { id: 73, name: "Diving" },
69
+ { id: 74, name: "Operations - Tactical" },
70
+ { id: 75, name: "Operations - Medical" },
71
+ { id: 76, name: "Operations - Flying" },
72
+ { id: 77, name: "Operations - Water" },
73
+ { id: 82, name: "Ultimate Frisbee" },
74
+ { id: 83, name: "Vertical Climber" },
75
+ { id: 84, name: "Jumping Rope" },
76
+ { id: 85, name: "Australian Rules Football" },
77
+ { id: 86, name: "Skateboarding" },
78
+ { id: 87, name: "Coaching" },
79
+ { id: 88, name: "Ice Bath" },
80
+ { id: 89, name: "Commuting" },
81
+ { id: 90, name: "Gaming" },
82
+ { id: 91, name: "Snowboarding" },
83
+ { id: 92, name: "Motocross" },
84
+ { id: 93, name: "Caddying" },
85
+ { id: 94, name: "Obstacle Course Racing" },
86
+ { id: 95, name: "Motor Racing" },
87
+ { id: 96, name: "HIIT" },
88
+ { id: 97, name: "Spin" },
89
+ { id: 98, name: "Jiu Jitsu" },
90
+ { id: 99, name: "Manual Labor" },
91
+ { id: 100, name: "Cricket" },
92
+ { id: 101, name: "Pickleball" },
93
+ { id: 102, name: "Inline Skating" },
94
+ { id: 103, name: "Box Fitness" },
95
+ { id: 104, name: "Spikeball" },
96
+ { id: 105, name: "Wheelchair Pushing" },
97
+ { id: 106, name: "Paddle Tennis" },
98
+ { id: 107, name: "Barre" },
99
+ { id: 108, name: "Stage Performance" },
100
+ { id: 109, name: "High Stress Work" },
101
+ { id: 110, name: "Parkour" },
102
+ { id: 111, name: "Gaelic Football" },
103
+ { id: 112, name: "Hurling/Camogie" },
104
+ { id: 113, name: "Circus Arts" },
105
+ { id: 116, name: "Resonance Frequency Breathing" },
106
+ { id: 121, name: "Massage Therapy" },
107
+ { id: 123, name: "Strength Trainer" },
108
+ { id: 125, name: "Watching Sports" },
109
+ { id: 126, name: "Assault Bike" },
110
+ { id: 127, name: "Kickboxing" },
111
+ { id: 128, name: "Stretching" },
112
+ { id: 131, name: "Other - Recovery" },
113
+ { id: 230, name: "Table Tennis/Ping Pong" },
114
+ { id: 231, name: "Badminton" },
115
+ { id: 232, name: "Netball" },
116
+ { id: 233, name: "Dry Sauna" },
117
+ { id: 234, name: "Disc Golf" },
118
+ { id: 235, name: "Yard Work/Gardening" },
119
+ { id: 236, name: "Air Compression" },
120
+ { id: 237, name: "Percussive Massage" },
121
+ { id: 238, name: "Paintball" },
122
+ { id: 239, name: "Ice Skating" },
123
+ { id: 240, name: "Handball" },
124
+ { id: 241, name: "Percussive Massage (Hypervolt)" },
125
+ { id: 242, name: "Air Compression (Normatec)" },
126
+ { id: 243, name: "Increase Relaxation" },
127
+ { id: 244, name: "Increase Alertness" },
128
+ { id: 245, name: "Breathwork" },
129
+ { id: 246, name: "Non-Sleep Deep Rest" },
130
+ { id: 247, name: "Steam Room" },
131
+ { id: 248, name: "F45 Training" },
132
+ { id: 249, name: "Padel" },
133
+ { id: 250, name: "Barry's" },
134
+ { id: 251, name: "Dedicated Parenting" },
135
+ { id: 252, name: "Stroller Walking" },
136
+ { id: 253, name: "Stroller Jogging" },
137
+ { id: 254, name: "Toddlerwearing" },
138
+ { id: 255, name: "Babywearing" },
139
+ { id: 256, name: "Playing with Child" },
140
+ { id: 257, name: "Cuddling with Child" },
141
+ { id: 258, name: "Barre3" },
142
+ { id: 259, name: "Hot Yoga" },
143
+ { id: 261, name: "Stadium Steps" },
144
+ { id: 262, name: "Polo" },
145
+ { id: 263, name: "Musical Performance" },
146
+ { id: 264, name: "Kite Boarding" },
147
+ { id: 265, name: "Restorative Yoga" },
148
+ { id: 266, name: "Dog Walking" },
149
+ { id: 267, name: "Water Skiing" },
150
+ { id: 268, name: "Wakeboarding" },
151
+ { id: 269, name: "Cooking" },
152
+ { id: 270, name: "Cleaning" },
153
+ { id: 271, name: "Warm Bath" },
154
+ { id: 272, name: "Public Speaking" },
155
+ { id: 274, name: "Race Walking" },
156
+ { id: 275, name: "Driving" },
157
+ { id: 276, name: "solidcore" },
158
+ { id: 277, name: "Refereeing" },
159
+ { id: 278, name: "Rucking" },
160
+ { id: 279, name: "Whoop Labs" },
161
+ { id: 280, name: "Cheerleading" },
162
+ { id: 281, name: "Scootering" },
163
+ { id: 282, name: "Foam Rolling" },
164
+ { id: 283, name: "Fishing" },
165
+ { id: 284, name: "Thrill Ride" },
166
+ { id: 285, name: "Bowling" },
167
+ { id: 286, name: "Knitting" },
168
+ { id: 287, name: "Curling" },
169
+ { id: 288, name: "Tai Chi" },
170
+ { id: 289, name: "QiGong" },
171
+ { id: 290, name: "Reformer Pilates" },
172
+ { id: 291, name: "Sculpt Yoga" },
173
+ { id: 292, name: "Breakdancing" },
174
+ { id: 293, name: "Snow Shoveling" },
175
+ { id: 294, name: "Trail Running" },
176
+ { id: 295, name: "Winter Biathlon" },
177
+ { id: 296, name: "Nordic Walking" },
178
+ { id: 297, name: "Ski Touring" },
179
+ { id: 298, name: "Darts" },
180
+ { id: 299, name: "Chess" },
181
+ { id: 300, name: "Acupuncture" },
182
+ { id: 301, name: "Mountaineering" },
183
+ { id: 302, name: "Muay Thai" },
184
+ { id: 303, name: "Taekwondo" },
185
+ { id: 304, name: "Judo" },
186
+ { id: 305, name: "Roller Hockey" },
187
+ { id: 306, name: "Bartending" },
188
+ { id: 307, name: "Chiropractor" },
189
+ { id: 308, name: "Bodybuilding" },
190
+ { id: 309, name: "Sprint Training" },
191
+ { id: 310, name: "Freediving" },
192
+ { id: 311, name: "Sound Healing" },
193
+ { id: 312, name: "Nursing a Baby" },
194
+ { id: 313, name: "Pumping" },
195
+ { id: 314, name: "Hot Dog Challenge" },
196
+ { id: 315, name: "DJ" },
197
+ { id: 316, name: "Bouldering" },
198
+ { id: 317, name: "Archery" },
199
+ { id: 318, name: "Racquetball" },
200
+ { id: 319, name: "Firefighting" },
201
+ { id: 320, name: "Red Light Therapy" },
202
+ { id: 321, name: "Hot Tub" },
203
+ { id: 322, name: "Skydiving" },
204
+ { id: 323, name: "Sport Fishing" },
205
+ { id: 324, name: "Infrared Sauna" },
206
+ { id: 325, name: "Ballet" },
207
+ { id: 326, name: "Snowshoeing" },
208
+ { id: 327, name: "Billiards" },
209
+ { id: 328, name: "Poker" },
210
+ { id: 329, name: "Trampoline" },
211
+ { id: 330, name: "Unicycling" },
212
+ { id: 331, name: "Bright Light Therapy" },
213
+ { id: 332, name: "Cold Shower" },
214
+ { id: 333, name: "Contrast Therapy" },
215
+ ] as const;
216
+
217
+ export const SPORTS_BY_ID = new Map<number, Sport>(SPORTS.map((s) => [s.id, s]));
218
+ export const SPORTS_BY_NAME = new Map<string, Sport>(SPORTS.map((s) => [s.name.toLowerCase(), s]));
@@ -0,0 +1,28 @@
1
+ // Run async thunks with bounded concurrency and a small random pre-delay, so a
2
+ // fan-out of identical data requests doesn't leave the process as a single
3
+ // millisecond-aligned burst (a pattern no human-driven app produces, and an
4
+ // easy heuristic to flag). The jitter is cosmetic request timing only — not
5
+ // security-sensitive — but crypto.randomInt keeps the codebase free of
6
+ // Math.random.
7
+ import { randomInt } from "node:crypto";
8
+
9
+ export async function runPaced<T>(
10
+ thunks: Array<() => Promise<T>>,
11
+ opts: { concurrency?: number; jitterMaxMs?: number } = {},
12
+ ): Promise<T[]> {
13
+ const concurrency = Math.max(1, opts.concurrency ?? 4);
14
+ const jitterMaxMs = opts.jitterMaxMs ?? 120;
15
+ const results = new Array<T>(thunks.length);
16
+ let next = 0;
17
+ async function worker(): Promise<void> {
18
+ for (;;) {
19
+ const i = next++;
20
+ if (i >= thunks.length) return;
21
+ if (jitterMaxMs > 0) await new Promise((r) => setTimeout(r, randomInt(0, jitterMaxMs + 1)));
22
+ results[i] = await thunks[i]!();
23
+ }
24
+ }
25
+ const workers = Array.from({ length: Math.min(concurrency, thunks.length) }, () => worker());
26
+ await Promise.all(workers);
27
+ return results;
28
+ }
@@ -0,0 +1,35 @@
1
+ import { zonedParts } from "./timezone.js";
2
+
3
+ export function isoDay(d: Date = new Date()): string {
4
+ // YYYY-MM-DD in the USER's timezone, not the server's. On a UTC host (Fly,
5
+ // Docker) `d.getDate()` returns the UTC calendar day — a day ahead of the
6
+ // user's local day during their evening, so "today" queries broke. zonedParts
7
+ // resolves the calendar day in the configured WHOOP_TIMEZONE / profile TZ.
8
+ const { year, month, day } = zonedParts(d);
9
+ return `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
10
+ }
11
+
12
+ export function todayIso(): string {
13
+ return isoDay(new Date());
14
+ }
15
+
16
+ export interface PgRange {
17
+ start: string;
18
+ end: string | null;
19
+ }
20
+
21
+ export function parsePgRange(s: string): PgRange {
22
+ // Closed-end: "['2026-05-23T07:35:46.220Z','2026-05-23T15:35:33.560Z')"
23
+ // Open-end: "['2026-05-23T07:35:46.220Z',)"
24
+ const closed = s.match(/^[\[\(]'([^']+)','([^']+)'[\]\)]$/);
25
+ if (closed) return { start: closed[1]!, end: closed[2]! };
26
+ const open = s.match(/^[\[\(]'([^']+)',\)?[\]\)]?$/);
27
+ if (open) return { start: open[1]!, end: null };
28
+ throw new Error(`Invalid PG range string: ${s}`);
29
+ }
30
+
31
+ export function rangeFromDays(days: number, now: Date = new Date()): { start: string; end: string } {
32
+ const end = now;
33
+ const start = new Date(end.getTime() - days * 24 * 60 * 60 * 1000);
34
+ return { start: start.toISOString(), end: end.toISOString() };
35
+ }
@@ -0,0 +1,27 @@
1
+ const KJ_PER_CALORIE = 4.184;
2
+ const M_PER_FT = 0.3048;
3
+ const LB_PER_KG = 2.20462;
4
+
5
+ export function kjToCal(kj: number): number {
6
+ return Math.round(kj / KJ_PER_CALORIE);
7
+ }
8
+
9
+ export function metersToFeet(m: number): number {
10
+ return Math.round((m / M_PER_FT) * 10) / 10;
11
+ }
12
+
13
+ export function metersToCm(m: number): number {
14
+ return Math.round(m * 100);
15
+ }
16
+
17
+ export function kgToLb(kg: number): number {
18
+ return Math.round(kg * LB_PER_KG * 10) / 10;
19
+ }
20
+
21
+ export function msToMinutes(ms: number): number {
22
+ return Math.round(ms / 60000);
23
+ }
24
+
25
+ export function secondsToMinutes(s: number): number {
26
+ return Math.round(s / 60);
27
+ }
@@ -0,0 +1,35 @@
1
+ export function mean(xs: Array<number | null | undefined>): number | null {
2
+ const clean = xs.filter((v): v is number => typeof v === "number" && Number.isFinite(v));
3
+ if (clean.length === 0) return null;
4
+ return clean.reduce((a, b) => a + b, 0) / clean.length;
5
+ }
6
+
7
+ export interface TimedPoint {
8
+ t: number; // epoch ms
9
+ v: number;
10
+ }
11
+
12
+ const MS_PER_DAY = 86_400_000;
13
+
14
+ export function slopePerDay(points: TimedPoint[]): number | null {
15
+ if (points.length < 2) return null;
16
+ const xs = points.map((p) => p.t / MS_PER_DAY);
17
+ const ys = points.map((p) => p.v);
18
+ const xMean = xs.reduce((a, b) => a + b, 0) / xs.length;
19
+ const yMean = ys.reduce((a, b) => a + b, 0) / ys.length;
20
+ let num = 0;
21
+ let den = 0;
22
+ for (let i = 0; i < xs.length; i++) {
23
+ const dx = xs[i]! - xMean;
24
+ num += dx * (ys[i]! - yMean);
25
+ den += dx * dx;
26
+ }
27
+ if (den === 0) return 0;
28
+ return num / den;
29
+ }
30
+
31
+ export function deltaVsWindow(latest: number, window: number[]): number | null {
32
+ const m = mean(window);
33
+ if (m === null) return null;
34
+ return latest - m;
35
+ }
@@ -0,0 +1,269 @@
1
+ // Convert Whoop's UTC timestamps (which end in "Z") to ISO 8601 strings with
2
+ // an explicit offset for the user's timezone (e.g., "2026-05-25T15:30:00-07:00").
3
+ //
4
+ // Why: Whoop returns every timestamp in UTC, so without conversion the AI sees
5
+ // "22:30:00Z" and (correctly) treats it as 10:30 PM UTC. For Brian in San Jose
6
+ // at PDT/UTC-7 that's actually 3:30 PM local, but the AI can't infer that
7
+ // without knowing the user's TZ.
8
+ //
9
+ // Approach: write the explicit offset into every timestamp so it's unambiguous.
10
+ // "2026-05-25T15:30:00-07:00" means "3:30 PM in a -07:00 zone" — same instant
11
+ // as the original UTC timestamp, but the AI now knows the local clock value
12
+ // without needing extra context.
13
+ //
14
+ // Config:
15
+ // WHOOP_TIMEZONE=America/Los_Angeles (IANA name; default = system TZ)
16
+ //
17
+ // Date-only strings ("2026-05-25") and timestamps that already carry an offset
18
+ // pass through unchanged — only "*Z" UTC timestamps are converted.
19
+
20
+ // Matches a UTC timestamp in any of the forms Whoop emits: "...Z", "...+0000",
21
+ // or "...+00:00" (the journal / pg-range endpoints use the +0000 form, the
22
+ // deep-dive endpoints use Z). All three mean UTC and should be localized.
23
+ const UTC_ISO_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|\+00:?00)$/;
24
+ // Trailing UTC marker (Z / +0000 / +00:00) — used to strip before re-suffixing.
25
+ const UTC_SUFFIX_RE = /(?:Z|\+00:?00)$/;
26
+ // Matches both Whoop's "+0000"/"-0700" form and standard "+00:00"/"-07:00" form.
27
+ const FIXED_OFFSET_RE = /^([+-])(\d{2}):?(\d{2})$/;
28
+
29
+ // Module-level cache populated by `setProfileTimezone()` at server boot. This
30
+ // is Tier 2 of the priority chain — for users who didn't set `WHOOP_TIMEZONE`
31
+ // explicitly, we auto-detect from their Whoop profile's `timezone_offset`
32
+ // (which their phone keeps current).
33
+ let cachedProfileTz: string | null = null;
34
+
35
+ export function setProfileTimezone(tzOrOffset: string | null): void {
36
+ cachedProfileTz = tzOrOffset;
37
+ }
38
+
39
+ export function getProfileTimezone(): string | null {
40
+ return cachedProfileTz;
41
+ }
42
+
43
+ /**
44
+ * Resolves the timezone the server should use for outgoing timestamps.
45
+ *
46
+ * Priority chain:
47
+ * 1. WHOOP_TIMEZONE env var (IANA name, explicit override)
48
+ * 2. Cached profile offset (auto-detected from Whoop, e.g. "-0700")
49
+ * 3. Server's system timezone (Intl default — UTC inside Docker / Fly)
50
+ */
51
+ export function getTimezone(): string {
52
+ return (
53
+ process.env.WHOOP_TIMEZONE
54
+ ?? cachedProfileTz
55
+ ?? Intl.DateTimeFormat().resolvedOptions().timeZone
56
+ );
57
+ }
58
+
59
+ export function isUtcIso(s: string): boolean {
60
+ return UTC_ISO_RE.test(s);
61
+ }
62
+
63
+ const pad2 = (n: number): string => String(n).padStart(2, "0");
64
+
65
+ /**
66
+ * Return the UTC offset (e.g. "-07:00") observed in `tz` at the given instant.
67
+ * Handles IANA names (via Intl) and fixed offsets ("-0700" / "-07:00").
68
+ */
69
+ function offsetAt(instant: Date, tz: string): string {
70
+ const fixed = parseFixedOffset(tz);
71
+ if (fixed) return `${fixed.sign}${fixed.hh}:${fixed.mm}`;
72
+ const parts = new Intl.DateTimeFormat("en-US", {
73
+ timeZone: tz,
74
+ timeZoneName: "longOffset",
75
+ year: "numeric",
76
+ month: "2-digit",
77
+ day: "2-digit",
78
+ }).formatToParts(instant);
79
+ const raw = parts.find((p) => p.type === "timeZoneName")?.value ?? "GMT";
80
+ const m = raw.match(/GMT([+-])(\d{1,2})(?::?(\d{2}))?/);
81
+ if (!m) return "+00:00";
82
+ return `${m[1]}${m[2]!.padStart(2, "0")}:${(m[3] ?? "00").padStart(2, "0")}`;
83
+ }
84
+
85
+ /**
86
+ * Build an ISO-8601 timestamp with explicit offset for a wall-clock time on a
87
+ * given calendar date in `tz`. Used by projections (stress, in-workout HR) whose
88
+ * source data carries only local clock labels ("9:38 AM") with no absolute
89
+ * timestamp.
90
+ *
91
+ * wallClockIso("2026-05-30", 9, 38, "America/Los_Angeles")
92
+ * → "2026-05-30T09:38:00-07:00"
93
+ *
94
+ * The offset is resolved for that wall-clock instant, so it's DST-correct except
95
+ * within the ~1h fold around a transition, which is immaterial here.
96
+ */
97
+ export function wallClockIso(
98
+ date: string,
99
+ hour: number,
100
+ minute: number,
101
+ tz: string = getTimezone(),
102
+ ): string {
103
+ const ref = new Date(`${date}T${pad2(hour)}:${pad2(minute)}:00Z`);
104
+ const offset = Number.isNaN(ref.getTime()) ? "+00:00" : offsetAt(ref, tz);
105
+ return `${date}T${pad2(hour)}:${pad2(minute)}:00${offset}`;
106
+ }
107
+
108
+ /** "9:38 AM" / "11:52 PM" / "14:05" → minutes since midnight (0..1439), or null. */
109
+ export function clockLabelToMinutes(label: string | null): number | null {
110
+ if (!label) return null;
111
+ const m = label.trim().match(/^(\d{1,2}):(\d{2})\s*([AP]M)?$/i);
112
+ if (!m) return null;
113
+ let h = Number(m[1]);
114
+ const min = Number(m[2]);
115
+ const ap = m[3]?.toUpperCase();
116
+ if (ap === "PM" && h !== 12) h += 12;
117
+ if (ap === "AM" && h === 12) h = 0;
118
+ if (h > 23 || min > 59) return null;
119
+ return h * 60 + min;
120
+ }
121
+
122
+ function parseFixedOffset(s: string): { sign: "+" | "-"; hh: string; mm: string } | null {
123
+ const m = s.match(FIXED_OFFSET_RE);
124
+ if (!m) return null;
125
+ return { sign: m[1] as "+" | "-", hh: m[2]!, mm: m[3]! };
126
+ }
127
+
128
+ /**
129
+ * Convert a UTC ISO timestamp ("2026-05-25T22:30:00.000Z") to the user's local
130
+ * timezone, formatted as ISO 8601 with explicit offset
131
+ * ("2026-05-25T15:30:00-07:00"). Preserves millisecond precision if present.
132
+ *
133
+ * Invalid input is returned unchanged (defensive — we'd rather pass through
134
+ * something funky than throw and break the whole tool call).
135
+ */
136
+ export function toLocalIso(utcIso: string, tz: string = getTimezone()): string {
137
+ if (!isUtcIso(utcIso)) return utcIso;
138
+ const date = new Date(utcIso);
139
+ if (Number.isNaN(date.getTime())) return utcIso;
140
+
141
+ // Fixed-offset TZ ("-0700" or "-07:00") — Whoop's profile.timezone_offset uses
142
+ // this form. Intl.DateTimeFormat only accepts IANA names, so we shift the
143
+ // instant by the offset and format manually.
144
+ const fixedOffset = parseFixedOffset(tz);
145
+ if (fixedOffset) {
146
+ const offsetMin = (Number(fixedOffset.hh) * 60 + Number(fixedOffset.mm))
147
+ * (fixedOffset.sign === "+" ? 1 : -1);
148
+ const shifted = new Date(date.getTime() + offsetMin * 60 * 1000);
149
+ const pad = (n: number, w = 2): string => String(n).padStart(w, "0");
150
+ const Y = shifted.getUTCFullYear();
151
+ const M = pad(shifted.getUTCMonth() + 1);
152
+ const D = pad(shifted.getUTCDate());
153
+ const h = pad(shifted.getUTCHours());
154
+ const m = pad(shifted.getUTCMinutes());
155
+ const s = pad(shifted.getUTCSeconds());
156
+ const msMatch = utcIso.match(/\.(\d+)(?:Z|\+00:?00)$/);
157
+ const fraction = msMatch ? `.${msMatch[1]}` : "";
158
+ return `${Y}-${M}-${D}T${h}:${m}:${s}${fraction}${fixedOffset.sign}${fixedOffset.hh}:${fixedOffset.mm}`;
159
+ }
160
+
161
+ // IANA name path — uses Intl.DateTimeFormat which knows about DST transitions.
162
+ const parts = new Intl.DateTimeFormat("en-US", {
163
+ timeZone: tz,
164
+ year: "numeric",
165
+ month: "2-digit",
166
+ day: "2-digit",
167
+ hour: "2-digit",
168
+ minute: "2-digit",
169
+ second: "2-digit",
170
+ hour12: false,
171
+ timeZoneName: "longOffset",
172
+ }).formatToParts(date);
173
+
174
+ const get = (type: Intl.DateTimeFormatPartTypes): string =>
175
+ parts.find((p) => p.type === type)?.value ?? "";
176
+
177
+ const Y = get("year");
178
+ const M = get("month");
179
+ const D = get("day");
180
+ // Intl.DateTimeFormat with hour12:false sometimes returns "24" for midnight
181
+ // in certain locales — normalize to "00".
182
+ let h = get("hour");
183
+ if (h === "24") h = "00";
184
+ const m = get("minute");
185
+ const s = get("second");
186
+
187
+ // Preserve sub-second precision from the original.
188
+ const msMatch = utcIso.match(/\.(\d+)(?:Z|\+00:?00)$/);
189
+ const fraction = msMatch ? `.${msMatch[1]}` : "";
190
+
191
+ const offsetRaw = get("timeZoneName"); // "GMT-07:00" or "GMT" for UTC
192
+ let offset = "+00:00";
193
+ const match = offsetRaw.match(/GMT([+-])(\d{1,2})(?::?(\d{2}))?/);
194
+ if (match) {
195
+ const sign = match[1];
196
+ const hh = match[2]!.padStart(2, "0");
197
+ const mm = (match[3] ?? "00").padStart(2, "0");
198
+ offset = `${sign}${hh}:${mm}`;
199
+ }
200
+
201
+ return `${Y}-${M}-${D}T${h}:${m}:${s}${fraction}${offset}`;
202
+ }
203
+
204
+ /**
205
+ * Walk an arbitrary JSON-ish value and convert every UTC-ISO string to local
206
+ * time. Non-string values are returned as-is. Object keys are preserved.
207
+ *
208
+ * This is the single function `jsonOut()` calls — it covers every tool's
209
+ * response without each projection needing to know about timezones.
210
+ */
211
+ export function localizeTimestamps<T>(value: T, tz: string = getTimezone()): T {
212
+ if (typeof value === "string") {
213
+ return (isUtcIso(value) ? toLocalIso(value, tz) : value) as T;
214
+ }
215
+ if (Array.isArray(value)) {
216
+ return value.map((item) => localizeTimestamps(item, tz)) as T;
217
+ }
218
+ if (value !== null && typeof value === "object") {
219
+ const out: Record<string, unknown> = {};
220
+ for (const [k, v] of Object.entries(value)) {
221
+ out[k] = localizeTimestamps(v, tz);
222
+ }
223
+ return out as T;
224
+ }
225
+ return value;
226
+ }
227
+
228
+ /**
229
+ * Returns the calendar components of an instant as observed in the user's
230
+ * timezone. Handles both IANA names (`America/Los_Angeles`) and fixed offsets
231
+ * (`-0700` / `-07:00`, the form Whoop's profile returns).
232
+ *
233
+ * This is what makes "today" correct on a UTC server: `new Date()` is the
234
+ * right instant, but `.getDate()` would give the server's (UTC) calendar day.
235
+ * This converts to the user's calendar day instead.
236
+ */
237
+ export function zonedParts(
238
+ date: Date = new Date(),
239
+ tz: string = getTimezone(),
240
+ ): { year: number; month: number; day: number; hour: number; minute: number; second: number } {
241
+ const fixed = parseFixedOffset(tz);
242
+ if (fixed) {
243
+ const offsetMin = (Number(fixed.hh) * 60 + Number(fixed.mm)) * (fixed.sign === "+" ? 1 : -1);
244
+ const shifted = new Date(date.getTime() + offsetMin * 60 * 1000);
245
+ return {
246
+ year: shifted.getUTCFullYear(),
247
+ month: shifted.getUTCMonth() + 1,
248
+ day: shifted.getUTCDate(),
249
+ hour: shifted.getUTCHours(),
250
+ minute: shifted.getUTCMinutes(),
251
+ second: shifted.getUTCSeconds(),
252
+ };
253
+ }
254
+ const parts = new Intl.DateTimeFormat("en-US", {
255
+ timeZone: tz,
256
+ year: "numeric",
257
+ month: "2-digit",
258
+ day: "2-digit",
259
+ hour: "2-digit",
260
+ minute: "2-digit",
261
+ second: "2-digit",
262
+ hour12: false,
263
+ }).formatToParts(date);
264
+ const get = (t: Intl.DateTimeFormatPartTypes): number =>
265
+ Number(parts.find((p) => p.type === t)?.value ?? "0");
266
+ let hour = get("hour");
267
+ if (hour === 24) hour = 0; // some locales render midnight as 24
268
+ return { year: get("year"), month: get("month"), day: get("day"), hour, minute: get("minute"), second: get("second") };
269
+ }