How z-ghazanfar/fitos works
1. Overall shape
FitOS is a Next.js App Router application that acts as a private fitness “OS” on top of Postgres/Prisma. The app has both interactive pages and a set of API routes that ingest, sync, and expose fitness data to external systems.
2. Primary UI surfaces
The user-facing app is centered on /, /training, /nutrition, /recovery, and /settings, with auth pages for /login and /signup. The UI is mainly a dashboard over stored data, not a data-entry-heavy app; most write flows happen through API endpoints, integrations, or GPT actions.
3. Core data store and boundaries
Prisma is the persistence boundary for users, fitness records, integration state, OAuth clients/tokens, and coach profile history. The schema is explicitly designed for multi-user operation with user_id on core tables, while the app currently behaves like a private invite-only system.
4. Authentication and user identity
The app uses its own signed HTTP-only session cookie rather than relying on a third-party session provider. It supports email/password login plus Google sign-in, and Google accounts are linked to existing invited users by matching email. Public signup is optional and controlled by configuration.
5. Encrypted secrets and integration credentials
Sensitive integration material is stored encrypted at rest with AES-256-GCM. This matters architecturally because WHOOP OAuth tokens, Hevy API keys, and similar credentials are not treated as plain application data; the app has a dedicated security layer for them.
6. WHOOP integration pipeline
WHOOP is connected through OAuth, then used in two ways: scheduled daily syncs and webhook-driven event capture. Webhooks are stored and de-duplicated, but the scheduled reconciliation job is the authoritative backstop that re-fetches recent WHOOP windows and updates daily recovery/body measurement context.
7. Hevy integration pipeline
Hevy is integrated as a read-heavy source for workout history and progression analysis. The app supports full workout backfill plus incremental event sync, stores workouts at set-level granularity, and then uses that stored history for training dashboards and recommendation generation. The recommendation engine is intentionally read-only and does not write back to Hevy.
8. Nutrition ingestion and daily totals
Nutrition is centered on a daily aggregate record rather than individual meals. The key write path is the GPT Action endpoint PUT /api/actions/nutrition/day, which overwrites a user’s daily calories and macros for a date. Historical nutrition can also be imported from CSV, and the /nutrition page is for review/trends rather than detailed meal tracking.
9. Weight logging
Weight history comes from a dedicated ingestion endpoint for Apple Health / Shortcuts, POST /api/actions/weight/log. This exists because Apple Health is not directly web-accessible, so the system expects weight samples to arrive from an iPhone Shortcut or CSV import and then uses them for trend views and AI context.
10. Shared GPT OAuth and AI coach endpoints
The app exposes an OAuth-secured GPT action surface so a shared Custom GPT can act on behalf of the signed-in user. Separate AI endpoints provide structured context for snapshots, progression, recovery/training, workouts, and a coach report. The GPT can also update the persistent coach profile, which stores goal, phase, priorities, notes, and revision history.
11. Sync architecture
Sync is split between user-triggered and scheduled paths. There are per-source sync routes, a combined /api/sync/all, and cron-oriented endpoints for WHOOP daily sync, WHOOP reconcile, Hevy events, and recommendation generation. The sync layer pulls from external APIs, normalizes data into the database, and then powers both dashboards and AI context.
12. External services and trust boundaries
The architecture crosses several external boundaries: Google for login, WHOOP for recovery/body metrics and webhooks, Hevy for workout data, ChatGPT for GPT Actions/OAuth, and Apple Health via Shortcuts for weight. The app’s main trust boundaries are its session cookie, its OAuth-protected GPT action API, and its internal-secret-protected scheduler/webhook routes.
13. Key product flow
The main end-to-end flow is: external fitness data is synchronized into Postgres, dashboards read it back for trends and status, and the shared GPT consumes the same stored data through OAuth APIs to generate coaching context or update nutrition/coach profile state. This makes the database the central source of truth for both UI and AI-assisted workflows.
Main entry points: app-frame.tsx Shared UI · schema.prisma Prisma DB · session.ts Auth core · route.ts GPT actions
Components 22
UI surfaces
App Router pages and shared dashboard components
- App shell · Root layout, global styles, and shared frame for the dashboard
app/layout.tsx - Home · Main overview surface for the private fitness OS
app/page.tsx - Training · Training trends and workout-focused UI
app/training/page.tsx - Nutrition · Daily nutrition review and trends
app/nutrition/page.tsx - Recovery · Recovery and body metric views
app/recovery/page.tsx - Settings · Account and integration controls
app/settings/page.tsx - Auth pages · Sign-in and account creation entry points
app/login/page.tsx - Shared UI · Dashboard frame, charts, and command-center widgets
app/components/app-frame.tsx
API boundary
Authentication, GPT actions, sync, and integration routes
- Auth API · Password auth, Google callback, and session handling endpoints
app/api/auth/google/callback/route.ts - GPT actions · OAuth-protected action surface for nutrition, weight, and shared GPT access
app/api/oauth/token/route.ts - AI endpoints · Structured coach, snapshot, progression, and workout context APIs
app/api/ai/snapshot/route.ts - Sync routes · Combined and source-specific sync endpoints plus cron backstops
app/api/sync/all/route.ts - WHOOP API · OAuth connect/callback, webhook intake, and reconciliation support
app/api/whoop/connect/route.ts - Hevy sync · Workout backfill and incremental event ingestion from Hevy
app/api/sync/hevy/backfill/route.ts
Core logic
Auth, AI, sync, security, and domain helpers
- Crypto · AES-GCM encryption for stored integration secrets and tokens
lib/security/crypto.ts - Auth core · Signed cookie sessions, password auth, Google linking, and redirect logic
lib/auth/session.ts - Coach AI · Coach profile state, structured context, and report generation logic
lib/ai/coach-profile.ts - Sync engine · Normalization and orchestration across fitness sources and recommendations
lib/sync.ts - Integration clients · Hevy and WHOOP client wrappers and webhook handling helpers
lib/integrations/whoop.ts - Nutrition/weight · Daily nutrition totals and weight trend helpers
lib/nutrition.ts
Persistence
Prisma schema and database boundary
- Prisma DB · Primary persistence boundary for users, fitness records, integrations, and coach history
prisma/schema.prisma - External services · Google, WHOOP, Hevy, ChatGPT, and Apple Health/Shortcuts
Connections 28
- App shell → Shared UI: uses · Shared shell composes reusable dashboard widgets
- Home → Shared UI: renders · Home page uses the shared frame and charts
- Training → Shared UI: renders · Training UI is built from shared dashboard components
- Nutrition → Shared UI: renders · Nutrition UI uses shared primitives and charts
- Recovery → Shared UI: renders · Recovery UI reuses the common app frame
- Settings → Shared UI: renders · Settings surface is part of the same shell
- Auth pages → Auth core: uses · Login and signup pages delegate authentication behavior
- Auth API → Auth core: uses · Auth endpoints mint and validate signed sessions
- Auth API → Prisma DB: reads/writes · Accounts and session-linked user records are persisted
- Auth API → External services: Google sign-in · OAuth callback links Google accounts to invited users
- GPT actions → Auth core: authenticates · Shared GPT action surface is tied to the signed-in user
- GPT actions → Nutrition/weight: writes · Nutrition and weight actions update daily history
- GPT actions → Coach AI: updates · GPT can revise persistent coach profile state
- AI endpoints → Coach AI: reads · Coach report and context endpoints assemble AI-ready views
- AI endpoints → Sync engine: reads · AI endpoints consume normalized fitness data
- Sync routes → Sync engine: orchestrates · User-triggered and scheduled sync entry points drive the engine
- Sync routes → Integration clients: pulls · Sync routes fetch and normalize external fitness data
- WHOOP API → Integration clients: webhooks/OAuth · WHOOP connect and webhook routes bridge the external service
- Hevy sync → Integration clients: pulls · Hevy backfill and events routes ingest workout history
- Integration clients → External services: connects · WHOOP and Hevy are source systems outside the app boundary
- Crypto → Prisma DB: protects · Encrypted-at-rest secret handling guards stored credentials
- Sync engine → Prisma DB: persists · Normalized workouts, recovery, nutrition, and integration state land in Postgres
- Coach AI → Prisma DB: reads/writes · Coach profile history and AI context are stored centrally
- Nutrition/weight → Prisma DB: reads/writes · Daily nutrition totals and weight samples are stored for trends
- Auth core → Prisma DB: reads/writes · Users, invitation state, and linked identities are persisted
- Auth core → Crypto: protects · Session and credential handling rely on encrypted secrets
- Coach AI → External services: ChatGPT · Shared GPT and AI endpoints are served through OpenAI/ChatGPT
- GPT actions → External services: GPT OAuth · Custom GPT accesses the app through an OAuth-secured boundary