@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.
- package/.dockerignore +23 -0
- package/.env.example +66 -0
- package/CHANGELOG.md +283 -0
- package/CONTRIBUTING.md +149 -0
- package/Dockerfile +53 -0
- package/LICENSE +37 -0
- package/README.md +587 -0
- package/SECURITY.md +79 -0
- package/TOOLS.md +429 -0
- package/WHOOP.md +2673 -0
- package/assets/banner.svg +38 -0
- package/dist/cli/index.js +589 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/setup.js +1127 -0
- package/dist/cli/setup.js.map +1 -0
- package/dist/cli/ui.js +512 -0
- package/dist/cli/ui.js.map +1 -0
- package/dist/cli/update.js +465 -0
- package/dist/cli/update.js.map +1 -0
- package/dist/data/behaviors.js +2777 -0
- package/dist/data/behaviors.js.map +1 -0
- package/dist/data/endpoints.js +320 -0
- package/dist/data/endpoints.js.map +1 -0
- package/dist/data/exercises.js +4863 -0
- package/dist/data/exercises.js.map +1 -0
- package/dist/data/sports.js +212 -0
- package/dist/data/sports.js.map +1 -0
- package/dist/lib/concurrency.js +27 -0
- package/dist/lib/concurrency.js.map +1 -0
- package/dist/lib/dates.js +29 -0
- package/dist/lib/dates.js.map +1 -0
- package/dist/lib/format.js +22 -0
- package/dist/lib/format.js.map +1 -0
- package/dist/lib/stats.js +32 -0
- package/dist/lib/stats.js.map +1 -0
- package/dist/lib/timezone.js +249 -0
- package/dist/lib/timezone.js.map +1 -0
- package/dist/lib/walk.js +396 -0
- package/dist/lib/walk.js.map +1 -0
- package/dist/projections/behavior_impact.js +96 -0
- package/dist/projections/behavior_impact.js.map +1 -0
- package/dist/projections/calendar.js +33 -0
- package/dist/projections/calendar.js.map +1 -0
- package/dist/projections/communities.js +105 -0
- package/dist/projections/communities.js.map +1 -0
- package/dist/projections/cycle.js +81 -0
- package/dist/projections/cycle.js.map +1 -0
- package/dist/projections/hr_zones.js +35 -0
- package/dist/projections/hr_zones.js.map +1 -0
- package/dist/projections/journal.js +56 -0
- package/dist/projections/journal.js.map +1 -0
- package/dist/projections/leaderboard.js +37 -0
- package/dist/projections/leaderboard.js.map +1 -0
- package/dist/projections/lift_exercise.js +22 -0
- package/dist/projections/lift_exercise.js.map +1 -0
- package/dist/projections/lift_history.js +133 -0
- package/dist/projections/lift_history.js.map +1 -0
- package/dist/projections/lift_library.js +71 -0
- package/dist/projections/lift_library.js.map +1 -0
- package/dist/projections/lift_progression.js +138 -0
- package/dist/projections/lift_progression.js.map +1 -0
- package/dist/projections/lift_prs.js +20 -0
- package/dist/projections/lift_prs.js.map +1 -0
- package/dist/projections/live_hr.js +19 -0
- package/dist/projections/live_hr.js.map +1 -0
- package/dist/projections/live_state.js +23 -0
- package/dist/projections/live_state.js.map +1 -0
- package/dist/projections/live_stress.js +11 -0
- package/dist/projections/live_stress.js.map +1 -0
- package/dist/projections/performance_assessment.js +15 -0
- package/dist/projections/performance_assessment.js.map +1 -0
- package/dist/projections/profile.js +59 -0
- package/dist/projections/profile.js.map +1 -0
- package/dist/projections/recovery.js +131 -0
- package/dist/projections/recovery.js.map +1 -0
- package/dist/projections/sleep.js +256 -0
- package/dist/projections/sleep.js.map +1 -0
- package/dist/projections/sleep_need.js +32 -0
- package/dist/projections/sleep_need.js.map +1 -0
- package/dist/projections/smart_alarm.js +45 -0
- package/dist/projections/smart_alarm.js.map +1 -0
- package/dist/projections/strain.js +128 -0
- package/dist/projections/strain.js.map +1 -0
- package/dist/projections/stress.js +86 -0
- package/dist/projections/stress.js.map +1 -0
- package/dist/projections/today.js +129 -0
- package/dist/projections/today.js.map +1 -0
- package/dist/projections/trend.js +149 -0
- package/dist/projections/trend.js.map +1 -0
- package/dist/projections/workout.js +161 -0
- package/dist/projections/workout.js.map +1 -0
- package/dist/projections/workouts.js +37 -0
- package/dist/projections/workouts.js.map +1 -0
- package/dist/schemas/calendar.js +12 -0
- package/dist/schemas/calendar.js.map +1 -0
- package/dist/schemas/coach.js +11 -0
- package/dist/schemas/coach.js.map +1 -0
- package/dist/schemas/communities.js +51 -0
- package/dist/schemas/communities.js.map +1 -0
- package/dist/schemas/compare.js +15 -0
- package/dist/schemas/compare.js.map +1 -0
- package/dist/schemas/escape.js +18 -0
- package/dist/schemas/escape.js.map +1 -0
- package/dist/schemas/journal.js +69 -0
- package/dist/schemas/journal.js.map +1 -0
- package/dist/schemas/leaderboard.js +25 -0
- package/dist/schemas/leaderboard.js.map +1 -0
- package/dist/schemas/live.js +26 -0
- package/dist/schemas/live.js.map +1 -0
- package/dist/schemas/performance.js +12 -0
- package/dist/schemas/performance.js.map +1 -0
- package/dist/schemas/primitives.js +29 -0
- package/dist/schemas/primitives.js.map +1 -0
- package/dist/schemas/profile.js +39 -0
- package/dist/schemas/profile.js.map +1 -0
- package/dist/schemas/recovery.js +27 -0
- package/dist/schemas/recovery.js.map +1 -0
- package/dist/schemas/settings.js +26 -0
- package/dist/schemas/settings.js.map +1 -0
- package/dist/schemas/sleep.js +38 -0
- package/dist/schemas/sleep.js.map +1 -0
- package/dist/schemas/sleep_need.js +15 -0
- package/dist/schemas/sleep_need.js.map +1 -0
- package/dist/schemas/smart_alarm.js +36 -0
- package/dist/schemas/smart_alarm.js.map +1 -0
- package/dist/schemas/strain.js +24 -0
- package/dist/schemas/strain.js.map +1 -0
- package/dist/schemas/strength.js +170 -0
- package/dist/schemas/strength.js.map +1 -0
- package/dist/schemas/stress.js +16 -0
- package/dist/schemas/stress.js.map +1 -0
- package/dist/schemas/today.js +38 -0
- package/dist/schemas/today.js.map +1 -0
- package/dist/schemas/trend.js +50 -0
- package/dist/schemas/trend.js.map +1 -0
- package/dist/schemas/womens_health.js +23 -0
- package/dist/schemas/womens_health.js.map +1 -0
- package/dist/schemas/workouts.js +50 -0
- package/dist/schemas/workouts.js.map +1 -0
- package/dist/scripts/cognito_bootstrap.js +278 -0
- package/dist/scripts/cognito_bootstrap.js.map +1 -0
- package/dist/server-http.js +221 -0
- package/dist/server-http.js.map +1 -0
- package/dist/server.js +74 -0
- package/dist/server.js.map +1 -0
- package/dist/tools/register.js +117 -0
- package/dist/tools/register.js.map +1 -0
- package/dist/tools/v2/activity_create.js +64 -0
- package/dist/tools/v2/activity_create.js.map +1 -0
- package/dist/tools/v2/activity_delete.js +26 -0
- package/dist/tools/v2/activity_delete.js.map +1 -0
- package/dist/tools/v2/behavior_impact.js +36 -0
- package/dist/tools/v2/behavior_impact.js.map +1 -0
- package/dist/tools/v2/calendar.js +28 -0
- package/dist/tools/v2/calendar.js.map +1 -0
- package/dist/tools/v2/coach_ask.js +117 -0
- package/dist/tools/v2/coach_ask.js.map +1 -0
- package/dist/tools/v2/communities.js +60 -0
- package/dist/tools/v2/communities.js.map +1 -0
- package/dist/tools/v2/compare.js +67 -0
- package/dist/tools/v2/compare.js.map +1 -0
- package/dist/tools/v2/cycle.js +23 -0
- package/dist/tools/v2/cycle.js.map +1 -0
- package/dist/tools/v2/cycle_log.js +43 -0
- package/dist/tools/v2/cycle_log.js.map +1 -0
- package/dist/tools/v2/day.js +32 -0
- package/dist/tools/v2/day.js.map +1 -0
- package/dist/tools/v2/endpoints.js +28 -0
- package/dist/tools/v2/endpoints.js.map +1 -0
- package/dist/tools/v2/hidden_metric.js +28 -0
- package/dist/tools/v2/hidden_metric.js.map +1 -0
- package/dist/tools/v2/hr_zones.js +24 -0
- package/dist/tools/v2/hr_zones.js.map +1 -0
- package/dist/tools/v2/hr_zones_set.js +51 -0
- package/dist/tools/v2/hr_zones_set.js.map +1 -0
- package/dist/tools/v2/journal.js +26 -0
- package/dist/tools/v2/journal.js.map +1 -0
- package/dist/tools/v2/journal_autopop.js +19 -0
- package/dist/tools/v2/journal_autopop.js.map +1 -0
- package/dist/tools/v2/journal_catalog.js +44 -0
- package/dist/tools/v2/journal_catalog.js.map +1 -0
- package/dist/tools/v2/journal_log.js +78 -0
- package/dist/tools/v2/journal_log.js.map +1 -0
- package/dist/tools/v2/leaderboard.js +79 -0
- package/dist/tools/v2/leaderboard.js.map +1 -0
- package/dist/tools/v2/lift_catalog.js +47 -0
- package/dist/tools/v2/lift_catalog.js.map +1 -0
- package/dist/tools/v2/lift_custom_exercise.js +108 -0
- package/dist/tools/v2/lift_custom_exercise.js.map +1 -0
- package/dist/tools/v2/lift_exercise.js +31 -0
- package/dist/tools/v2/lift_exercise.js.map +1 -0
- package/dist/tools/v2/lift_history.js +39 -0
- package/dist/tools/v2/lift_history.js.map +1 -0
- package/dist/tools/v2/lift_library.js +22 -0
- package/dist/tools/v2/lift_library.js.map +1 -0
- package/dist/tools/v2/lift_log.js +95 -0
- package/dist/tools/v2/lift_log.js.map +1 -0
- package/dist/tools/v2/lift_progression.js +30 -0
- package/dist/tools/v2/lift_progression.js.map +1 -0
- package/dist/tools/v2/lift_prs.js +21 -0
- package/dist/tools/v2/lift_prs.js.map +1 -0
- package/dist/tools/v2/lift_template_save.js +83 -0
- package/dist/tools/v2/lift_template_save.js.map +1 -0
- package/dist/tools/v2/live_hr.js +21 -0
- package/dist/tools/v2/live_hr.js.map +1 -0
- package/dist/tools/v2/live_state.js +21 -0
- package/dist/tools/v2/live_state.js.map +1 -0
- package/dist/tools/v2/live_stress.js +22 -0
- package/dist/tools/v2/live_stress.js.map +1 -0
- package/dist/tools/v2/performance_assessment.js +38 -0
- package/dist/tools/v2/performance_assessment.js.map +1 -0
- package/dist/tools/v2/profile.js +28 -0
- package/dist/tools/v2/profile.js.map +1 -0
- package/dist/tools/v2/profile_update.js +66 -0
- package/dist/tools/v2/profile_update.js.map +1 -0
- package/dist/tools/v2/raw.js +43 -0
- package/dist/tools/v2/raw.js.map +1 -0
- package/dist/tools/v2/recovery.js +31 -0
- package/dist/tools/v2/recovery.js.map +1 -0
- package/dist/tools/v2/sleep.js +23 -0
- package/dist/tools/v2/sleep.js.map +1 -0
- package/dist/tools/v2/sleep_need.js +21 -0
- package/dist/tools/v2/sleep_need.js.map +1 -0
- package/dist/tools/v2/smart_alarm.js +24 -0
- package/dist/tools/v2/smart_alarm.js.map +1 -0
- package/dist/tools/v2/smart_alarm_set.js +94 -0
- package/dist/tools/v2/smart_alarm_set.js.map +1 -0
- package/dist/tools/v2/sports_catalog.js +26 -0
- package/dist/tools/v2/sports_catalog.js.map +1 -0
- package/dist/tools/v2/strain.js +23 -0
- package/dist/tools/v2/strain.js.map +1 -0
- package/dist/tools/v2/stress.js +23 -0
- package/dist/tools/v2/stress.js.map +1 -0
- package/dist/tools/v2/symptom_log.js +72 -0
- package/dist/tools/v2/symptom_log.js.map +1 -0
- package/dist/tools/v2/today.js +30 -0
- package/dist/tools/v2/today.js.map +1 -0
- package/dist/tools/v2/trend.js +26 -0
- package/dist/tools/v2/trend.js.map +1 -0
- package/dist/tools/v2/workout.js +23 -0
- package/dist/tools/v2/workout.js.map +1 -0
- package/dist/tools/v2/workouts.js +32 -0
- package/dist/tools/v2/workouts.js.map +1 -0
- package/dist/whoop/build_lift_body.js +67 -0
- package/dist/whoop/build_lift_body.js.map +1 -0
- package/dist/whoop/client.js +84 -0
- package/dist/whoop/client.js.map +1 -0
- package/dist/whoop/cognito.js +110 -0
- package/dist/whoop/cognito.js.map +1 -0
- package/dist/whoop/constants.js +4 -0
- package/dist/whoop/constants.js.map +1 -0
- package/dist/whoop/device.js +103 -0
- package/dist/whoop/device.js.map +1 -0
- package/dist/whoop/errors.js +42 -0
- package/dist/whoop/errors.js.map +1 -0
- package/dist/whoop/init_timezone.js +62 -0
- package/dist/whoop/init_timezone.js.map +1 -0
- package/dist/whoop/installation.js +62 -0
- package/dist/whoop/installation.js.map +1 -0
- package/dist/whoop/json_out.js +9 -0
- package/dist/whoop/json_out.js.map +1 -0
- package/dist/whoop/oauth_provider.js +297 -0
- package/dist/whoop/oauth_provider.js.map +1 -0
- package/dist/whoop/session_state.js +34 -0
- package/dist/whoop/session_state.js.map +1 -0
- package/dist/whoop/token_manager.js +60 -0
- package/dist/whoop/token_manager.js.map +1 -0
- package/dist/whoop/token_store.js +47 -0
- package/dist/whoop/token_store.js.map +1 -0
- package/dist/whoop/types.js +82 -0
- package/dist/whoop/types.js.map +1 -0
- package/dist/whoop/write_safety.js +23 -0
- package/dist/whoop/write_safety.js.map +1 -0
- package/package.json +66 -0
- package/src/cli/index.ts +597 -0
- package/src/cli/setup.ts +1106 -0
- package/src/cli/ui.ts +505 -0
- package/src/cli/update.ts +442 -0
- package/src/data/behaviors.ts +2784 -0
- package/src/data/endpoints.ts +319 -0
- package/src/data/exercises.ts +4868 -0
- package/src/data/sports.ts +218 -0
- package/src/lib/concurrency.ts +28 -0
- package/src/lib/dates.ts +35 -0
- package/src/lib/format.ts +27 -0
- package/src/lib/stats.ts +35 -0
- package/src/lib/timezone.ts +269 -0
- package/src/lib/walk.ts +418 -0
- package/src/projections/behavior_impact.ts +98 -0
- package/src/projections/calendar.ts +46 -0
- package/src/projections/communities.ts +135 -0
- package/src/projections/cycle.ts +85 -0
- package/src/projections/hr_zones.ts +43 -0
- package/src/projections/journal.ts +55 -0
- package/src/projections/leaderboard.ts +49 -0
- package/src/projections/lift_exercise.ts +29 -0
- package/src/projections/lift_history.ts +140 -0
- package/src/projections/lift_library.ts +70 -0
- package/src/projections/lift_progression.ts +138 -0
- package/src/projections/lift_prs.ts +23 -0
- package/src/projections/live_hr.ts +21 -0
- package/src/projections/live_state.ts +28 -0
- package/src/projections/live_stress.ts +13 -0
- package/src/projections/performance_assessment.ts +19 -0
- package/src/projections/profile.ts +71 -0
- package/src/projections/recovery.ts +133 -0
- package/src/projections/sleep.ts +276 -0
- package/src/projections/sleep_need.ts +36 -0
- package/src/projections/smart_alarm.ts +70 -0
- package/src/projections/strain.ts +131 -0
- package/src/projections/stress.ts +95 -0
- package/src/projections/today.ts +166 -0
- package/src/projections/trend.ts +149 -0
- package/src/projections/workout.ts +168 -0
- package/src/projections/workouts.ts +36 -0
- package/src/schemas/calendar.ts +13 -0
- package/src/schemas/coach.ts +11 -0
- package/src/schemas/communities.ts +56 -0
- package/src/schemas/compare.ts +17 -0
- package/src/schemas/escape.ts +21 -0
- package/src/schemas/journal.ts +80 -0
- package/src/schemas/leaderboard.ts +26 -0
- package/src/schemas/live.ts +31 -0
- package/src/schemas/performance.ts +13 -0
- package/src/schemas/primitives.ts +35 -0
- package/src/schemas/profile.ts +40 -0
- package/src/schemas/recovery.ts +28 -0
- package/src/schemas/settings.ts +30 -0
- package/src/schemas/sleep.ts +40 -0
- package/src/schemas/sleep_need.ts +16 -0
- package/src/schemas/smart_alarm.ts +40 -0
- package/src/schemas/strain.ts +25 -0
- package/src/schemas/strength.ts +189 -0
- package/src/schemas/stress.ts +17 -0
- package/src/schemas/today.ts +39 -0
- package/src/schemas/trend.ts +53 -0
- package/src/schemas/womens_health.ts +26 -0
- package/src/schemas/workouts.ts +55 -0
- package/src/scripts/cognito_bootstrap.ts +249 -0
- package/src/server-http.ts +241 -0
- package/src/server.ts +85 -0
- package/src/tools/register.ts +120 -0
- package/src/tools/v2/activity_create.ts +74 -0
- package/src/tools/v2/activity_delete.ts +33 -0
- package/src/tools/v2/behavior_impact.ts +39 -0
- package/src/tools/v2/calendar.ts +33 -0
- package/src/tools/v2/coach_ask.ts +131 -0
- package/src/tools/v2/communities.ts +65 -0
- package/src/tools/v2/compare.ts +83 -0
- package/src/tools/v2/cycle.ts +28 -0
- package/src/tools/v2/cycle_log.ts +51 -0
- package/src/tools/v2/day.ts +37 -0
- package/src/tools/v2/endpoints.ts +32 -0
- package/src/tools/v2/hidden_metric.ts +33 -0
- package/src/tools/v2/hr_zones.ts +29 -0
- package/src/tools/v2/hr_zones_set.ts +57 -0
- package/src/tools/v2/journal.ts +31 -0
- package/src/tools/v2/journal_autopop.ts +26 -0
- package/src/tools/v2/journal_catalog.ts +48 -0
- package/src/tools/v2/journal_log.ts +84 -0
- package/src/tools/v2/leaderboard.ts +92 -0
- package/src/tools/v2/lift_catalog.ts +48 -0
- package/src/tools/v2/lift_custom_exercise.ts +117 -0
- package/src/tools/v2/lift_exercise.ts +35 -0
- package/src/tools/v2/lift_history.ts +52 -0
- package/src/tools/v2/lift_library.ts +28 -0
- package/src/tools/v2/lift_log.ts +102 -0
- package/src/tools/v2/lift_progression.ts +34 -0
- package/src/tools/v2/lift_prs.ts +26 -0
- package/src/tools/v2/lift_template_save.ts +89 -0
- package/src/tools/v2/live_hr.ts +26 -0
- package/src/tools/v2/live_state.ts +26 -0
- package/src/tools/v2/live_stress.ts +27 -0
- package/src/tools/v2/performance_assessment.ts +44 -0
- package/src/tools/v2/profile.ts +33 -0
- package/src/tools/v2/profile_update.ts +76 -0
- package/src/tools/v2/raw.ts +50 -0
- package/src/tools/v2/recovery.ts +36 -0
- package/src/tools/v2/sleep.ts +28 -0
- package/src/tools/v2/sleep_need.ts +26 -0
- package/src/tools/v2/smart_alarm.ts +29 -0
- package/src/tools/v2/smart_alarm_set.ts +103 -0
- package/src/tools/v2/sports_catalog.ts +32 -0
- package/src/tools/v2/strain.ts +28 -0
- package/src/tools/v2/stress.ts +28 -0
- package/src/tools/v2/symptom_log.ts +78 -0
- package/src/tools/v2/today.ts +35 -0
- package/src/tools/v2/trend.ts +31 -0
- package/src/tools/v2/workout.ts +28 -0
- package/src/tools/v2/workouts.ts +37 -0
- package/src/whoop/build_lift_body.ts +84 -0
- package/src/whoop/client.ts +118 -0
- package/src/whoop/cognito.ts +150 -0
- package/src/whoop/constants.ts +3 -0
- package/src/whoop/device.ts +106 -0
- package/src/whoop/errors.ts +41 -0
- package/src/whoop/init_timezone.ts +64 -0
- package/src/whoop/installation.ts +58 -0
- package/src/whoop/json_out.ts +9 -0
- package/src/whoop/oauth_provider.ts +377 -0
- package/src/whoop/session_state.ts +41 -0
- package/src/whoop/token_manager.ts +76 -0
- package/src/whoop/token_store.ts +45 -0
- package/src/whoop/types.ts +89 -0
- package/src/whoop/write_safety.ts +30 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import type { SleepOutT } from "../schemas/sleep.js";
|
|
2
|
+
import {
|
|
3
|
+
isObject,
|
|
4
|
+
asArray,
|
|
5
|
+
asNumber,
|
|
6
|
+
asString,
|
|
7
|
+
findByType,
|
|
8
|
+
findDetailsCardByTitle,
|
|
9
|
+
labelToNumber,
|
|
10
|
+
timeLabelToMs,
|
|
11
|
+
} from "../lib/walk.js";
|
|
12
|
+
|
|
13
|
+
// Whoop's deep-dive/sleep/last-night returns:
|
|
14
|
+
// header_section.destination.parameters.{start_time, end_time, activity_id}
|
|
15
|
+
// DETAILS_GRAPHING_CARDs by card_title:
|
|
16
|
+
// "HOURS OF SLEEP" → arrow_stat[0].current_stat_text = "7:24"
|
|
17
|
+
// "HOURS VS. NEEDED" → arrow_stat[0].current_stat_text = "85%"
|
|
18
|
+
// "SLEEP CONSISTENCY" → "73%"
|
|
19
|
+
// "SLEEP EFFICIENCY" → "93%"
|
|
20
|
+
// BAR_GRAPH_CARD (first one):
|
|
21
|
+
// content.duration_display = "7:59" (total time in bed)
|
|
22
|
+
// content.heart_rate_zones[] (misnamed — actually sleep stages):
|
|
23
|
+
// {id, bar_graph_tile_title_display, bar_graph_tile_percentage_display, bar_graph_tile_time_display}
|
|
24
|
+
// DETAILS_METRIC_TILES "WAKE EVENTS" → disturbances count
|
|
25
|
+
//
|
|
26
|
+
// Hypnogram + in-sleep HR (avg/min) are reconstructed from the per-stage HR-curve
|
|
27
|
+
// points — see buildSleepTimeline below. Sleep HRV / respiratory rate / debt /
|
|
28
|
+
// latency aren't exposed by this endpoint as named fields and stay null.
|
|
29
|
+
|
|
30
|
+
function arrowStat(card: Record<string, unknown> | null): string | null {
|
|
31
|
+
if (!card) return null;
|
|
32
|
+
const content = isObject(card.content) ? (card.content as Record<string, unknown>) : {};
|
|
33
|
+
const arr = asArray(content.arrow_stat);
|
|
34
|
+
const first = arr[0];
|
|
35
|
+
if (!isObject(first)) return null;
|
|
36
|
+
return asString(first.current_stat_text);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function findStageBar(raw: unknown, stageId: string): Record<string, unknown> | null {
|
|
40
|
+
// BAR_GRAPH_CARD with content.duration_display non-empty is the stages card.
|
|
41
|
+
// (The other BAR_GRAPH_CARD has empty duration_display — that one is stress.)
|
|
42
|
+
let stageCard: Record<string, unknown> | null = null;
|
|
43
|
+
let allBars: Record<string, unknown>[] = [];
|
|
44
|
+
function walk(n: unknown): void {
|
|
45
|
+
if (Array.isArray(n)) {
|
|
46
|
+
for (const x of n) walk(x);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (!isObject(n)) return;
|
|
50
|
+
if (n.type === "BAR_GRAPH_CARD") {
|
|
51
|
+
const c = isObject(n.content) ? (n.content as Record<string, unknown>) : {};
|
|
52
|
+
if (asString(c.duration_display)) {
|
|
53
|
+
stageCard = n;
|
|
54
|
+
const zones = asArray(c.heart_rate_zones);
|
|
55
|
+
allBars = zones.filter(isObject) as Record<string, unknown>[];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (!stageCard) for (const v of Object.values(n)) walk(v);
|
|
59
|
+
}
|
|
60
|
+
walk(raw);
|
|
61
|
+
return allBars.find((b) => asString(b.id) === stageId) ?? null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function stageTime(raw: unknown, stageId: string): { ms: number | null; pct: number | null } {
|
|
65
|
+
const bar = findStageBar(raw, stageId);
|
|
66
|
+
if (!bar) return { ms: null, pct: null };
|
|
67
|
+
const timeDisplay = asString(bar.bar_graph_tile_time_display);
|
|
68
|
+
const pctDisplay = asString(bar.bar_graph_tile_percentage_display);
|
|
69
|
+
return { ms: timeLabelToMs(timeDisplay), pct: labelToNumber(pctDisplay) };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ---- Hypnogram + in-sleep HR ------------------------------------------------
|
|
73
|
+
// Whoop draws the in-sleep HR curve as four LINE_PLOTs (one per stage, so each
|
|
74
|
+
// can be colored differently). Every point carries data_scrubber_details with:
|
|
75
|
+
// scrubber_style → the sleep stage at that instant
|
|
76
|
+
// value_display (+ "bpm") → heart rate
|
|
77
|
+
// secondary_contextual_display → a local clock label, e.g. "1:24 AM"
|
|
78
|
+
// The four plots share one X axis, so merging + sorting by position_x yields the
|
|
79
|
+
// chronological timeline. We time the segments from the *clock labels*, not
|
|
80
|
+
// position_x — the graph axis has ~40min of padding that skews position_x.
|
|
81
|
+
|
|
82
|
+
const STAGE_MAP: Record<string, "AWAKE" | "LIGHT" | "REM" | "SWS"> = {
|
|
83
|
+
AWAKE: "AWAKE",
|
|
84
|
+
LIGHT_SLEEP: "LIGHT",
|
|
85
|
+
REM_SLEEP: "REM",
|
|
86
|
+
SWS_SLEEP: "SWS",
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
interface StagePoint {
|
|
90
|
+
x: number;
|
|
91
|
+
stage: "AWAKE" | "LIGHT" | "REM" | "SWS";
|
|
92
|
+
bpm: number | null;
|
|
93
|
+
clockMin: number | null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// "1:24 AM" / "11:52 PM" → minutes since midnight (0..1439), or null.
|
|
97
|
+
function parseClockMinutes(s: string | null): number | null {
|
|
98
|
+
if (!s) return null;
|
|
99
|
+
const m = /^(\d{1,2}):(\d{2})\s*([AP]M)$/i.exec(s.trim());
|
|
100
|
+
if (!m) return null;
|
|
101
|
+
let h = parseInt(m[1]!, 10);
|
|
102
|
+
const min = parseInt(m[2]!, 10);
|
|
103
|
+
const pm = m[3]!.toUpperCase() === "PM";
|
|
104
|
+
if (pm && h !== 12) h += 12;
|
|
105
|
+
if (!pm && h === 12) h = 0;
|
|
106
|
+
return h * 60 + min;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function collectStagePoints(raw: unknown): StagePoint[] {
|
|
110
|
+
const out: StagePoint[] = [];
|
|
111
|
+
function walk(n: unknown): void {
|
|
112
|
+
if (Array.isArray(n)) {
|
|
113
|
+
for (const v of n) walk(v);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (!isObject(n)) return;
|
|
117
|
+
const dsd = isObject(n.data_scrubber_details) ? (n.data_scrubber_details as Record<string, unknown>) : null;
|
|
118
|
+
const style = dsd ? asString(dsd.scrubber_style) : null;
|
|
119
|
+
const mapped = style ? STAGE_MAP[style] : undefined;
|
|
120
|
+
const x = asNumber(n.position_x);
|
|
121
|
+
if (dsd && mapped && x !== null) {
|
|
122
|
+
out.push({
|
|
123
|
+
x,
|
|
124
|
+
stage: mapped,
|
|
125
|
+
bpm: asNumber(dsd.value_display),
|
|
126
|
+
clockMin: parseClockMinutes(asString(dsd.secondary_contextual_display)),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
for (const v of Object.values(n)) walk(v);
|
|
130
|
+
}
|
|
131
|
+
walk(raw);
|
|
132
|
+
out.sort((a, b) => a.x - b.x);
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Build the stage timeline (hypnogram) + in-sleep HR (avg/min) from the points.
|
|
137
|
+
// Timestamps: clock labels give wall-clock minutes; we make them monotonic
|
|
138
|
+
// (handling the midnight wrap), then map them onto the UTC sleep window anchored
|
|
139
|
+
// at the *midpoint*. The data is inset ~symmetrically at both ends, so the
|
|
140
|
+
// midpoint of the clock span lines up with the midpoint of [start, end] — which
|
|
141
|
+
// sidesteps both the axis padding and any timezone knowledge. We emit UTC and
|
|
142
|
+
// let json_out localize for display.
|
|
143
|
+
function buildSleepTimeline(
|
|
144
|
+
points: StagePoint[],
|
|
145
|
+
startIso: string | null,
|
|
146
|
+
endIso: string | null,
|
|
147
|
+
): { hypnogram: SleepOutT["hypnogram"]; sleep_hr: SleepOutT["sleep_hr"] } {
|
|
148
|
+
const bpms = points.map((p) => p.bpm).filter((b): b is number => b !== null);
|
|
149
|
+
const sleep_hr = bpms.length
|
|
150
|
+
? { avg_bpm: Math.round(bpms.reduce((a, b) => a + b, 0) / bpms.length), min_bpm: Math.min(...bpms) }
|
|
151
|
+
: { avg_bpm: null, min_bpm: null };
|
|
152
|
+
|
|
153
|
+
const startMs = startIso ? Date.parse(startIso) : NaN;
|
|
154
|
+
const endMs = endIso ? Date.parse(endIso) : NaN;
|
|
155
|
+
const clocked = points.filter((p) => p.clockMin !== null);
|
|
156
|
+
if (clocked.length < 2 || Number.isNaN(startMs) || Number.isNaN(endMs)) {
|
|
157
|
+
return { hypnogram: [], sleep_hr };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Monotonic elapsed minutes from the first point's clock label.
|
|
161
|
+
const rel: number[] = [0];
|
|
162
|
+
for (let i = 1; i < clocked.length; i++) {
|
|
163
|
+
let delta = clocked[i]!.clockMin! - clocked[i - 1]!.clockMin!;
|
|
164
|
+
if (delta < -720) delta += 1440; // crossed midnight
|
|
165
|
+
rel.push(rel[i - 1]! + delta);
|
|
166
|
+
}
|
|
167
|
+
const relMid = (rel[0]! + rel[rel.length - 1]!) / 2;
|
|
168
|
+
const utcMid = (startMs + endMs) / 2;
|
|
169
|
+
const tsAt = (i: number): string => new Date(utcMid + (rel[i]! - relMid) * 60000).toISOString();
|
|
170
|
+
|
|
171
|
+
// Group consecutive same-stage runs into [started_at, ended_at, stage] segments.
|
|
172
|
+
const hypnogram: SleepOutT["hypnogram"] = [];
|
|
173
|
+
let runStart = 0;
|
|
174
|
+
for (let i = 1; i <= clocked.length; i++) {
|
|
175
|
+
if (i === clocked.length || clocked[i]!.stage !== clocked[runStart]!.stage) {
|
|
176
|
+
hypnogram.push({
|
|
177
|
+
started_at: tsAt(runStart),
|
|
178
|
+
ended_at: tsAt(Math.min(i, clocked.length - 1)),
|
|
179
|
+
stage: clocked[runStart]!.stage,
|
|
180
|
+
});
|
|
181
|
+
runStart = i;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return { hypnogram, sleep_hr };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function projectSleep(raw: unknown, date: string): SleepOutT {
|
|
188
|
+
const root = isObject(raw) ? raw : {};
|
|
189
|
+
const headerSection = isObject(root.header_section) ? (root.header_section as Record<string, unknown>) : {};
|
|
190
|
+
const dest = isObject(headerSection.destination) ? (headerSection.destination as Record<string, unknown>) : null;
|
|
191
|
+
const params = dest && isObject(dest.parameters) ? (dest.parameters as Record<string, unknown>) : null;
|
|
192
|
+
|
|
193
|
+
const hoursOfSleepCard = findDetailsCardByTitle(raw, "HOURS OF SLEEP");
|
|
194
|
+
const hoursVsNeededCard = findDetailsCardByTitle(raw, "HOURS VS");
|
|
195
|
+
const consistencyCard = findDetailsCardByTitle(raw, "SLEEP CONSISTENCY");
|
|
196
|
+
const efficiencyCard = findDetailsCardByTitle(raw, "SLEEP EFFICIENCY");
|
|
197
|
+
|
|
198
|
+
const totalSleepMs = timeLabelToMs(arrowStat(hoursOfSleepCard));
|
|
199
|
+
const performancePct = labelToNumber(arrowStat(hoursVsNeededCard));
|
|
200
|
+
const consistencyPct = labelToNumber(arrowStat(consistencyCard));
|
|
201
|
+
const efficiencyPct = labelToNumber(arrowStat(efficiencyCard));
|
|
202
|
+
|
|
203
|
+
// Time in bed from BAR_GRAPH_CARD duration_display
|
|
204
|
+
let timeInBedMs: number | null = null;
|
|
205
|
+
function walkForTib(n: unknown): void {
|
|
206
|
+
if (Array.isArray(n)) {
|
|
207
|
+
for (const x of n) walkForTib(x);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (!isObject(n)) return;
|
|
211
|
+
if (n.type === "BAR_GRAPH_CARD") {
|
|
212
|
+
const c = isObject(n.content) ? (n.content as Record<string, unknown>) : {};
|
|
213
|
+
const dur = asString(c.duration_display);
|
|
214
|
+
if (dur) timeInBedMs = timeLabelToMs(dur);
|
|
215
|
+
}
|
|
216
|
+
if (timeInBedMs === null) for (const v of Object.values(n)) walkForTib(v);
|
|
217
|
+
}
|
|
218
|
+
walkForTib(raw);
|
|
219
|
+
|
|
220
|
+
const rem = stageTime(raw, "REM_SLEEP");
|
|
221
|
+
const light = stageTime(raw, "LIGHT_SLEEP");
|
|
222
|
+
const sws = stageTime(raw, "SWS_SLEEP");
|
|
223
|
+
const wake = stageTime(raw, "AWAKE");
|
|
224
|
+
|
|
225
|
+
// Wake events tile
|
|
226
|
+
const wakeTile = findByType(raw, "DETAILS_METRIC_TILES");
|
|
227
|
+
let disturbances: number | null = null;
|
|
228
|
+
if (wakeTile) {
|
|
229
|
+
const content = isObject(wakeTile.content) ? (wakeTile.content as Record<string, unknown>) : {};
|
|
230
|
+
if (asString(content.title) === "WAKE EVENTS") {
|
|
231
|
+
// Find the numeric stat inside the tile
|
|
232
|
+
const tiles = asArray(content.metric_tiles ?? content.tiles ?? content.items);
|
|
233
|
+
for (const t of tiles) {
|
|
234
|
+
if (isObject(t)) {
|
|
235
|
+
const v = asNumber(t.value ?? t.metric_value);
|
|
236
|
+
if (v !== null) {
|
|
237
|
+
disturbances = v;
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const startedAt = params ? asString(params.start_time) : null;
|
|
246
|
+
const endedAt = params ? asString(params.end_time) : null;
|
|
247
|
+
const { hypnogram, sleep_hr } = buildSleepTimeline(collectStagePoints(raw), startedAt, endedAt);
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
date,
|
|
251
|
+
started_at: startedAt,
|
|
252
|
+
ended_at: endedAt,
|
|
253
|
+
total_sleep_ms: totalSleepMs,
|
|
254
|
+
time_in_bed_ms: timeInBedMs,
|
|
255
|
+
efficiency_pct: efficiencyPct,
|
|
256
|
+
performance_pct: performancePct,
|
|
257
|
+
consistency_pct: consistencyPct,
|
|
258
|
+
debt_ms: null,
|
|
259
|
+
latency_ms: null,
|
|
260
|
+
stages: {
|
|
261
|
+
rem_ms: rem.ms,
|
|
262
|
+
rem_pct: rem.pct,
|
|
263
|
+
light_ms: light.ms,
|
|
264
|
+
light_pct: light.pct,
|
|
265
|
+
sws_ms: sws.ms,
|
|
266
|
+
sws_pct: sws.pct,
|
|
267
|
+
wake_ms: wake.ms,
|
|
268
|
+
wake_pct: wake.pct,
|
|
269
|
+
},
|
|
270
|
+
hypnogram,
|
|
271
|
+
disturbances,
|
|
272
|
+
sleep_hr,
|
|
273
|
+
sleep_hrv_ms: null,
|
|
274
|
+
respiratory_rate: null,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { SleepNeedOutT } from "../schemas/sleep_need.js";
|
|
2
|
+
import { isObject, asNumber, asString, asBool } from "../lib/walk.js";
|
|
3
|
+
|
|
4
|
+
// /coaching-service/v2/sleepneed shape:
|
|
5
|
+
// need_breakdown: { total, baseline, naps, strain, debt } <- all MILLISECONDS
|
|
6
|
+
// recommended_time_in_bed_formatted: { "70": {...}, "85": {...}, "100": {...},
|
|
7
|
+
// weekly_plan: {...}, optimize_sleep: {...} }
|
|
8
|
+
// each tier: { recommended_time_in_bed (ms), recommended_time_in_bed_time_string ("8:45"),
|
|
9
|
+
// optimal_endpoints_formatted: { start, end }, ... }
|
|
10
|
+
// alarm_schedule_state ("OFF"), next_schedule_day_label, eligible_for_smart_alarms
|
|
11
|
+
// The "85" tier is Whoop's standard (85% sleep-performance) recommendation.
|
|
12
|
+
|
|
13
|
+
const msToMin = (ms: number | null): number | null => (ms === null ? null : Math.round(ms / 60000));
|
|
14
|
+
|
|
15
|
+
export function projectSleepNeed(raw: unknown): SleepNeedOutT {
|
|
16
|
+
const root = isObject(raw) ? raw : {};
|
|
17
|
+
const need = isObject(root.need_breakdown) ? (root.need_breakdown as Record<string, unknown>) : {};
|
|
18
|
+
const recFmt = isObject(root.recommended_time_in_bed_formatted)
|
|
19
|
+
? (root.recommended_time_in_bed_formatted as Record<string, unknown>)
|
|
20
|
+
: {};
|
|
21
|
+
const tier = isObject(recFmt["85"]) ? (recFmt["85"] as Record<string, unknown>) : {};
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
recommended_time_in_bed: asString(tier.recommended_time_in_bed_time_string),
|
|
25
|
+
recommended_time_in_bed_minutes: msToMin(asNumber(tier.recommended_time_in_bed)),
|
|
26
|
+
need_breakdown: {
|
|
27
|
+
baseline_minutes: msToMin(asNumber(need.baseline)),
|
|
28
|
+
debt_minutes: msToMin(asNumber(need.debt)),
|
|
29
|
+
strain_minutes: msToMin(asNumber(need.strain)),
|
|
30
|
+
nap_credit_minutes: msToMin(asNumber(need.naps)),
|
|
31
|
+
},
|
|
32
|
+
next_schedule_day: asString(root.next_schedule_day_label),
|
|
33
|
+
smart_alarm_eligible: asBool(root.eligible_for_smart_alarms) ?? false,
|
|
34
|
+
schedule_state: asString(root.alarm_schedule_state),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { SmartAlarmOutT } from "../schemas/smart_alarm.js";
|
|
2
|
+
import { isObject, asArray, asBool, asNumber, asString } from "../lib/walk.js";
|
|
3
|
+
|
|
4
|
+
// Schedule list from /smart-alarm-bff/v1/schedule/all:
|
|
5
|
+
// alarm_schedule_list[], schedule_enabled
|
|
6
|
+
//
|
|
7
|
+
// Preferences from /smart-alarm-service/v1/smartalarm/preferences (documented):
|
|
8
|
+
// lower_time_bound, recovery_score_goal, sleep_score_goal, weekly_plan_goal,
|
|
9
|
+
// weekly_plan_sleep_hours_goal_in_minutes, weekly_plan_sleep_hours_goal,
|
|
10
|
+
// weekly_plan_goal_info, alarm_bounds, last_triggered_at, created_at
|
|
11
|
+
//
|
|
12
|
+
// The UPPER bound + goal mode are nested in alarm_bounds (not at top level).
|
|
13
|
+
// The PUT body shape (from captures) is {default, enabled, goal, lower_time_bound,
|
|
14
|
+
// schedule_enabled, time_zone_offset, upper_time_bound, weekly_plan_goal} —
|
|
15
|
+
// so on GET they're likely inside alarm_bounds: {goal, upper, lower, enabled}.
|
|
16
|
+
|
|
17
|
+
interface ProjectSmartAlarmInput {
|
|
18
|
+
schedules: unknown;
|
|
19
|
+
preferences: unknown;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const VALID_DAYS = ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"] as const;
|
|
23
|
+
type Day = typeof VALID_DAYS[number];
|
|
24
|
+
const VALID_MODES = ["IN_THE_GREEN", "EXACT_TIME_PEAK", "EXACT_TIME_OPTIMIZE_SLEEP"] as const;
|
|
25
|
+
type Mode = typeof VALID_MODES[number];
|
|
26
|
+
|
|
27
|
+
export function projectSmartAlarm(input: ProjectSmartAlarmInput): SmartAlarmOutT {
|
|
28
|
+
const sched = isObject(input.schedules) ? (input.schedules as Record<string, unknown>) : {};
|
|
29
|
+
const prefs = isObject(input.preferences) ? (input.preferences as Record<string, unknown>) : {};
|
|
30
|
+
const bounds = isObject(prefs.alarm_bounds) ? (prefs.alarm_bounds as Record<string, unknown>) : {};
|
|
31
|
+
|
|
32
|
+
const list = asArray(sched.alarm_schedule_list)
|
|
33
|
+
.map((s) => {
|
|
34
|
+
if (!isObject(s)) return null;
|
|
35
|
+
const days = asArray(s.day_of_week_list)
|
|
36
|
+
.filter((d): d is string => typeof d === "string")
|
|
37
|
+
.filter((d): d is Day => (VALID_DAYS as readonly string[]).includes(d));
|
|
38
|
+
const mode = asString(s.alarm_mode);
|
|
39
|
+
return {
|
|
40
|
+
schedule_id: asString(s.schedule_id ?? s.id) ?? "",
|
|
41
|
+
enabled: asBool(s.enabled) ?? false,
|
|
42
|
+
days_of_week: days,
|
|
43
|
+
latest_wake_time: asString(s.latest_wake_time) ?? "",
|
|
44
|
+
alarm_mode: ((VALID_MODES as readonly string[]).includes(mode ?? "") ? mode : "IN_THE_GREEN") as Mode,
|
|
45
|
+
sleep_goal: asString(s.sleep_goal),
|
|
46
|
+
timezone_offset: asString(s.time_zone_offset) ?? "",
|
|
47
|
+
};
|
|
48
|
+
})
|
|
49
|
+
.filter((x): x is NonNullable<typeof x> => x !== null);
|
|
50
|
+
|
|
51
|
+
// goal + upper_time_bound: try alarm_bounds first, fall back to top-level
|
|
52
|
+
const goal = asString(bounds.goal) ?? asString(prefs.goal);
|
|
53
|
+
const upperTimeBound = asString(bounds.upper) ?? asString(prefs.upper_time_bound);
|
|
54
|
+
const lowerTimeBound = asString(prefs.lower_time_bound) ?? asString(bounds.lower);
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
enabled: asBool(sched.schedule_enabled) ?? asBool(prefs.schedule_enabled) ?? false,
|
|
58
|
+
preferences: {
|
|
59
|
+
lower_time_bound: lowerTimeBound,
|
|
60
|
+
upper_time_bound: upperTimeBound,
|
|
61
|
+
goal: (VALID_MODES as readonly string[]).includes(goal ?? "")
|
|
62
|
+
? (goal as Mode)
|
|
63
|
+
: null,
|
|
64
|
+
weekly_plan_goal_minutes:
|
|
65
|
+
asNumber(prefs.weekly_plan_sleep_hours_goal_in_minutes) ?? asNumber(prefs.weekly_plan_goal),
|
|
66
|
+
last_triggered_at: asString(prefs.last_triggered_at),
|
|
67
|
+
},
|
|
68
|
+
schedules: list,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type { StrainOutT } from "../schemas/strain.js";
|
|
2
|
+
import { isObject, asArray, asString } from "../lib/walk.js";
|
|
3
|
+
|
|
4
|
+
// Whoop migrated /home-service/v1/deep-dive/strain in May 2026 from
|
|
5
|
+
// GRAPHING_CARD tiles to a SCORE_GAUGE + CONTRIBUTORS_TILE + ACTIVITY structure:
|
|
6
|
+
//
|
|
7
|
+
// sections[].items[].type === "SCORE_GAUGE"
|
|
8
|
+
// content.id === "STRAIN_SCORE_GAUGE"
|
|
9
|
+
// content.score_display: "18.9"
|
|
10
|
+
// content.score_target: 0.6299... ← daily strain target (0-1 of max=21)
|
|
11
|
+
// content.lower_optimal_percentage: 0.534... ← lower bound of optimal range
|
|
12
|
+
// content.higher_optimal_percentage: 0.749... ← upper bound of optimal range
|
|
13
|
+
//
|
|
14
|
+
// sections[].items[].type === "CONTRIBUTORS_TILE"
|
|
15
|
+
// content.id === "STRAIN_CONTRIBUTORS_TILE"
|
|
16
|
+
// content.metrics[] each:
|
|
17
|
+
// id: "CONTRIBUTORS_TILE_HR_ZONES_1_3" | "CONTRIBUTORS_TILE_HR_ZONES_4_5" |
|
|
18
|
+
// "CONTRIBUTORS_TILE_STRENGTH_TRAINING_TIME" | "CONTRIBUTORS_TILE_STEPS"
|
|
19
|
+
// status: "2:18" (h:mm or "10,616" for steps)
|
|
20
|
+
//
|
|
21
|
+
// sections[].items[].type === "ACTIVITY"
|
|
22
|
+
// score: "17.7", title: "STRENGTH TRAINER"
|
|
23
|
+
//
|
|
24
|
+
// Calories, avg_hr_bpm, max_hr_bpm, and per-zone (zone_0..zone_5) granularity
|
|
25
|
+
// are NOT in this endpoint anymore. They live in per-workout /cardio-details.
|
|
26
|
+
// We populate zone_1 with the 1-3 aggregate and zone_4 with the 4-5 aggregate.
|
|
27
|
+
|
|
28
|
+
// Whoop's strain scale is logarithmic 0–21. The deep-dive strain endpoint stores
|
|
29
|
+
// the daily target as a 0–1 fraction of max; we multiply by 21 to convert back
|
|
30
|
+
// to a strain value the AI can compare against the actual score.
|
|
31
|
+
const MAX_STRAIN = 21;
|
|
32
|
+
|
|
33
|
+
function targetFraction(content: Record<string, unknown>, key: string): number | null {
|
|
34
|
+
const v = content[key];
|
|
35
|
+
return typeof v === "number" && Number.isFinite(v) ? v * MAX_STRAIN : null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function parseNumberWithCommas(s: string | null): number | null {
|
|
39
|
+
if (!s) return null;
|
|
40
|
+
const cleaned = s.replace(/[,%]/g, "").trim();
|
|
41
|
+
const n = parseFloat(cleaned);
|
|
42
|
+
return Number.isFinite(n) ? n : null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Parse "2:35" → 2h35m → ms. Also handles "2:35:12" (h:m:s).
|
|
46
|
+
function timeToMs(s: string | null): number | null {
|
|
47
|
+
if (!s) return null;
|
|
48
|
+
const parts = s.trim().split(":").map((p) => parseInt(p, 10));
|
|
49
|
+
if (parts.some((p) => !Number.isFinite(p))) return null;
|
|
50
|
+
if (parts.length === 2) return ((parts[0]! * 60) + parts[1]!) * 60 * 1000;
|
|
51
|
+
if (parts.length === 3) return ((parts[0]! * 3600) + (parts[1]! * 60) + parts[2]!) * 1000;
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function collectItems(node: unknown, out: Array<{ type: string; content: Record<string, unknown> }>): void {
|
|
56
|
+
if (!node || typeof node !== "object") return;
|
|
57
|
+
if (Array.isArray(node)) { for (const n of node) collectItems(n, out); return; }
|
|
58
|
+
const obj = node as Record<string, unknown>;
|
|
59
|
+
if (typeof obj.type === "string" && isObject(obj.content)) {
|
|
60
|
+
out.push({ type: obj.type, content: obj.content as Record<string, unknown> });
|
|
61
|
+
}
|
|
62
|
+
for (const k of Object.keys(obj)) collectItems(obj[k], out);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function findContributor(metrics: unknown[], idSuffix: string): Record<string, unknown> | null {
|
|
66
|
+
for (const m of metrics) {
|
|
67
|
+
if (!isObject(m)) continue;
|
|
68
|
+
const id = asString((m as Record<string, unknown>).id);
|
|
69
|
+
if (id && id.toUpperCase().endsWith(idSuffix.toUpperCase())) return m as Record<string, unknown>;
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function projectStrain(raw: unknown, date: string): StrainOutT {
|
|
75
|
+
const items: Array<{ type: string; content: Record<string, unknown> }> = [];
|
|
76
|
+
collectItems(raw, items);
|
|
77
|
+
|
|
78
|
+
const gauge = items.find((it) =>
|
|
79
|
+
it.type === "SCORE_GAUGE" && asString(it.content.id) === "STRAIN_SCORE_GAUGE",
|
|
80
|
+
);
|
|
81
|
+
const score = gauge ? parseNumberWithCommas(asString(gauge.content.score_display)) : null;
|
|
82
|
+
const target = {
|
|
83
|
+
value: gauge ? targetFraction(gauge.content, "score_target") : null,
|
|
84
|
+
optimal_lower: gauge ? targetFraction(gauge.content, "lower_optimal_percentage") : null,
|
|
85
|
+
optimal_upper: gauge ? targetFraction(gauge.content, "higher_optimal_percentage") : null,
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const contributors = items.find((it) =>
|
|
89
|
+
it.type === "CONTRIBUTORS_TILE" && asString(it.content.id) === "STRAIN_CONTRIBUTORS_TILE",
|
|
90
|
+
);
|
|
91
|
+
const metrics = contributors ? asArray(contributors.content.metrics) : [];
|
|
92
|
+
|
|
93
|
+
function readTimeMetric(idSuffix: string): number | null {
|
|
94
|
+
const m = findContributor(metrics, idSuffix);
|
|
95
|
+
if (!m) return null;
|
|
96
|
+
return timeToMs(asString(m.status));
|
|
97
|
+
}
|
|
98
|
+
function readNumericMetric(idSuffix: string): number | null {
|
|
99
|
+
const m = findContributor(metrics, idSuffix);
|
|
100
|
+
if (!m) return null;
|
|
101
|
+
return parseNumberWithCommas(asString(m.status));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const zones13Ms = readTimeMetric("HR_ZONES_1_3");
|
|
105
|
+
const zones45Ms = readTimeMetric("HR_ZONES_4_5");
|
|
106
|
+
const strengthTimeMs = readTimeMetric("STRENGTH_TRAINING_TIME");
|
|
107
|
+
const steps = readNumericMetric("STEPS");
|
|
108
|
+
|
|
109
|
+
// Count today's ACTIVITY items
|
|
110
|
+
const workoutsCount = items.filter((it) => it.type === "ACTIVITY").length;
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
date,
|
|
114
|
+
score,
|
|
115
|
+
target,
|
|
116
|
+
calories: null,
|
|
117
|
+
avg_hr_bpm: null,
|
|
118
|
+
max_hr_bpm: null,
|
|
119
|
+
zone_durations: {
|
|
120
|
+
zone_0_ms: null,
|
|
121
|
+
zone_1_ms: zones13Ms,
|
|
122
|
+
zone_2_ms: null,
|
|
123
|
+
zone_3_ms: null,
|
|
124
|
+
zone_4_ms: zones45Ms,
|
|
125
|
+
zone_5_ms: null,
|
|
126
|
+
},
|
|
127
|
+
workouts_count: workoutsCount,
|
|
128
|
+
steps,
|
|
129
|
+
strength_activity_time_ms: strengthTimeMs,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { StressOutT } from "../schemas/stress.js";
|
|
2
|
+
import { isObject, asArray, asNumber, asString, labelToNumber } from "../lib/walk.js";
|
|
3
|
+
import { clockLabelToMinutes, wallClockIso } from "../lib/timezone.js";
|
|
4
|
+
|
|
5
|
+
// /health-service/v2/stress-bff returns (note: NOT the older `stress_state.timeline`
|
|
6
|
+
// shape — `stress_state` is a STRING like "RELAXED"/"CALIBRATING"):
|
|
7
|
+
// stress_state → headline state string ("RELAXED", "STRESSED", …)
|
|
8
|
+
// calibration_text_display → non-null while the metric is still calibrating
|
|
9
|
+
// gauge: {
|
|
10
|
+
// gauge_score_display "0.6" → current stress level (0–3 scale)
|
|
11
|
+
// gauge_subtext_display "LOW"
|
|
12
|
+
// gauge_min_display "0.0", gauge_max_display "3.0"
|
|
13
|
+
// }
|
|
14
|
+
// stress_graph.graph.plots[].plot.segments[].points[].data_scrubber_details:
|
|
15
|
+
// primary_contextual_display "9:38 AM" → local clock label
|
|
16
|
+
// value_display "1.0" → stress level at that point
|
|
17
|
+
// secondary_contextual_display "LOW"
|
|
18
|
+
|
|
19
|
+
// Cap on emitted timeline points. The raw graph holds ~700 intraday samples,
|
|
20
|
+
// which is useless noise in an LLM context; we evenly downsample to this many.
|
|
21
|
+
const MAX_TIMELINE_POINTS = 48;
|
|
22
|
+
|
|
23
|
+
interface RawPoint {
|
|
24
|
+
clockMin: number;
|
|
25
|
+
level: number | null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function collectPoints(graph: unknown): RawPoint[] {
|
|
29
|
+
const g = isObject(graph) ? graph : {};
|
|
30
|
+
const out: RawPoint[] = [];
|
|
31
|
+
for (const p of asArray(g.plots)) {
|
|
32
|
+
if (!isObject(p)) continue;
|
|
33
|
+
const plot = isObject(p.plot) ? (p.plot as Record<string, unknown>) : null;
|
|
34
|
+
if (!plot) continue;
|
|
35
|
+
for (const seg of asArray(plot.segments)) {
|
|
36
|
+
if (!isObject(seg)) continue;
|
|
37
|
+
for (const pt of asArray(seg.points)) {
|
|
38
|
+
if (!isObject(pt)) continue;
|
|
39
|
+
const dsd = isObject(pt.data_scrubber_details) ? (pt.data_scrubber_details as Record<string, unknown>) : {};
|
|
40
|
+
const clockMin = clockLabelToMinutes(asString(dsd.primary_contextual_display));
|
|
41
|
+
if (clockMin === null) continue;
|
|
42
|
+
out.push({ clockMin, level: labelToNumber(asString(dsd.value_display)) });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function downsample<T>(arr: T[], max: number): T[] {
|
|
50
|
+
if (arr.length <= max) return arr;
|
|
51
|
+
const step = arr.length / max;
|
|
52
|
+
const out: T[] = [];
|
|
53
|
+
for (let i = 0; i < max; i++) out.push(arr[Math.floor(i * step)]!);
|
|
54
|
+
// Always include the final point so the timeline ends at the latest reading.
|
|
55
|
+
if (out[out.length - 1] !== arr[arr.length - 1]) out.push(arr[arr.length - 1]!);
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function projectStress(raw: unknown, date: string): StressOutT {
|
|
60
|
+
const root = isObject(raw) ? raw : {};
|
|
61
|
+
const gauge = isObject(root.gauge) ? (root.gauge as Record<string, unknown>) : {};
|
|
62
|
+
|
|
63
|
+
// stress_graph wraps the plottable data under `.graph`.
|
|
64
|
+
const stressGraph = isObject(root.stress_graph) ? (root.stress_graph as Record<string, unknown>) : {};
|
|
65
|
+
const points = collectPoints(stressGraph.graph);
|
|
66
|
+
const levels = points.map((p) => p.level).filter((v): v is number => v !== null);
|
|
67
|
+
|
|
68
|
+
// Current level from the gauge (the headline number the app shows); fall back
|
|
69
|
+
// to the last graphed sample.
|
|
70
|
+
const currentLevel =
|
|
71
|
+
labelToNumber(asString(gauge.gauge_score_display)) ??
|
|
72
|
+
(levels.length > 0 ? levels[levels.length - 1]! : null);
|
|
73
|
+
|
|
74
|
+
const calibrating =
|
|
75
|
+
asString(root.calibration_text_display) !== null ||
|
|
76
|
+
asString(root.stress_state) === "CALIBRATING";
|
|
77
|
+
|
|
78
|
+
// Pair consecutive downsampled samples into [started_at, ended_at) intervals.
|
|
79
|
+
const sampled = downsample(points, MAX_TIMELINE_POINTS);
|
|
80
|
+
const timeline = sampled.map((p, i) => {
|
|
81
|
+
const next = sampled[i + 1] ?? p;
|
|
82
|
+
const toIso = (min: number): string => wallClockIso(date, Math.floor(min / 60), min % 60);
|
|
83
|
+
return { started_at: toIso(p.clockMin), ended_at: toIso(next.clockMin), level: p.level };
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
date,
|
|
88
|
+
current_level: currentLevel,
|
|
89
|
+
baseline_level: null,
|
|
90
|
+
peak_level: levels.length > 0 ? Math.max(...levels) : null,
|
|
91
|
+
min_level: levels.length > 0 ? Math.min(...levels) : null,
|
|
92
|
+
calibration_state: calibrating ? "CALIBRATING" : "CALIBRATED",
|
|
93
|
+
timeline,
|
|
94
|
+
};
|
|
95
|
+
}
|