Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

13.3 KB
Raw Blame
// The Coach, in the demo build.
//
// The GitHub Pages demo has no backend at all, so there is nothing to run a CLI and nothing
// to poll. Rather than hide the feature there — it is the most interesting thing the app
// does — the demo answers the same calls locally with a canned proposal.
//
// It is built from the demo profile's actual routines rather than hard-coded, so every id
// resolves, the before/after values are real, and applying it exercises exactly the same
// code path as a live instance: validate, snapshot, apply, log, revert. What is faked is the
// provider, not the feature.
//
// Nothing here ships in a self-hosted bundle: every entry point is behind DEMO, which Vite
// replaces at build time.

import { EXIDX, EXDB } from './exercises.js'
import { modeOf, workoutVolume, workoutAt } from './history.js'
import { isWarmupRow } from './workout-model.js'
import { best1RM } from './onerm.js'
import { fmtNum } from './format.js'
import { planHash } from './coach.js'
import { waitingProposal } from '../../../api/coach/core/pending.js'
import { t } from './i18n.js'

const DELAY = 2200      // long enough to see "the Coach is thinking…", short enough to forgive

let pending = null
let job = null
let timer = null
let lastError = null    // the last job that ended without a proposal, in the server's shape

const iso = d => d.toISOString().slice(0, 10)

/** The routine the canned review aims at: the first with two exercises, one to swap and one to cut a set from. */
const reviewable = S => (S.routines || []).find(r => (r.ex || []).length >= 2)

/** A change-set that reads like a real one, aimed at whatever the demo profile actually has. */
function buildReview(S) {
  const routine = reviewable(S)
  if (!routine) return null
  const reps = (routine.ex || []).filter(e => modeOf(e) === 'reps')
  const first = reps[0] || routine.ex[0]
  // Anything but `first`: the two changes below swap `first` away and re-set `second`, so the
  // same entry in both aborts the whole change-set on apply (`missing target`). `reps[1]` was
  // not enough — a routine whose only rep-mode exercise sits at index 1 landed on it twice.
  const second = reps.find(e => e !== first) || routine.ex.find(e => e !== first)
  const other = (S.routines || []).find(r => r.id !== routine.id)

  // Something plausible that is *not* in this routine, from the same body part as the first
  // exercise — a swap the reader can believe rather than a random pick out of 1,324.
  const bp = EXIDX[first.id]?.bp
  const swapTo = EXDB.find(e => e.bp === bp && e.eq === 'dumbbell' && !(routine.ex || []).some(x => x.id === e.id))
    || EXDB.find(e => e.bp === bp && !(routine.ex || []).some(x => x.id === e.id))

  const sessions = (S.workouts || []).slice(-9)
  const changes = []
  if (swapTo) changes.push({
    id: 'd1', type: 'swap-exercise', target: { routineId: routine.id, exId: first.id },
    before: EXIDX[first.id]?.n || first.id, after: { id: swapTo.id, name: swapTo.n },
    why: t('Every top set on this one came in at RPE 9.5 or above for three sessions and the weight has not moved. Swapping the movement for four weeks usually breaks that stall faster than grinding the same one.')
  })
  if (second) changes.push({
    id: 'd2', type: 'sets', target: { routineId: routine.id, exId: second.id },
    before: second.sets ?? 3, after: Math.max(1, (second.sets ?? 3) - 1),
    why: t('Sessions have been running about fifteen minutes over. This is the accessory with the least to lose from one set fewer.')
  })
  if (other) changes.push({
    id: 'd3', type: 'week', target: { weekday: 6 }, before: null, after: other.id,
    why: t('You have moved this session to Saturday three weeks running. Better the plan says so than that you keep overriding it.')
  })

  return {
    id: 'demo-review', kind: 'review', createdAt: Date.now(), expiresAt: Date.now() + 864e5,
    planHash: planHash(S), iteration: 1,
    summary: t('Three things worth changing, and one worth knowing about. Everything else is working: the squat and the pulls are both progressing on schedule.'),
    evidence: { from: sessions[0]?.d || iso(new Date(Date.now() - 28 * 864e5)), to: sessions.at(-1)?.d || iso(new Date()), sessions: sessions.length || 9 },
    changes,
    notes: [t('Body weight has been flat for four weeks while the goal is to gain. That’s a kitchen problem, not a training one. The plan isn’t what’s holding it back.')]
  }
}

/** A small, honest starter plan for the demo's creation flow. */
function buildPlan(S, intake) {
  const pick = (bp, eq) => EXDB.find(e => e.bp === bp && (!eq || e.eq === eq)) || EXDB.find(e => e.bp === bp)
  const days = intake?.preferredDays?.length ? intake.preferredDays.slice(0, 3) : [1, 3, 5]
  const eq = (intake?.equipment || [])[0] || null
  const mk = (id, name, sets, reps, why) => ({ id, sets, reps, mode: 'reps', why })
  const routines = [
    {
      id: 'dr1', name: t('Full body A'), emoji: '💪', prog: 'linear',
      why: t('The two big lower-body and pressing patterns first, while you are fresh.'),
      ex: [
        mk(pick('upper legs', eq)?.id, null, 3, 8, t('The main lower-body driver, where most of the strength comes from.')),
        mk(pick('chest', eq)?.id, null, 3, 10, t('Horizontal pressing, the other half of the session.')),
        mk(pick('back', eq)?.id, null, 3, 10, t('A pull for every press, so the shoulders stay balanced.'))
      ].filter(e => e.id)
    },
    {
      id: 'dr2', name: t('Full body B'), emoji: '🏋️', prog: 'linear',
      why: t('The same patterns, different variations: enough overlap to progress, enough difference to stay fresh.'),
      ex: [
        mk(pick('upper legs', eq)?.id, null, 3, 10, t('Same pattern, higher reps than day A.')),
        mk(pick('shoulders', eq)?.id, null, 3, 10, t('Vertical pressing.')),
        mk(pick('upper arms', eq)?.id, null, 3, 12, t('Direct arm work, since you asked for it.'))
      ].filter(e => e.id)
    }
  ]
  const week = {}
  days.forEach((d, i) => { week[d] = routines[i % routines.length].id })
  return {
    id: 'demo-plan', kind: 'create', createdAt: Date.now(), expiresAt: Date.now() + 864e5, iteration: 1,
    planHash: planHash(S),
    summary: t('A two-day rotation across three sessions a week, built around the equipment you listed. Compounds first, one pull for every press, and enough overlap between the days that nothing goes two weeks without being trained.'),
    bundle: {
      opengym_plan: 1, name: t('Coach plan'),
      summary: t('A two-day rotation across three sessions a week, built around the equipment you listed. Compounds first, one pull for every press, and enough overlap between the days that nothing goes two weeks without being trained.'),
      basedOn: (S.workouts || []).length ? t('Based on the training already in this demo profile.') : t('No training history yet, so starting conservatively.'),
      week, routines, customEx: []
    }
  }
}

/** One session, read back with its own numbers — no plan changes, just what a coach would say after. */
function buildDebrief(S, workoutId) {
  const all = (S.workouts || []).filter(w => w && w.d)
  const w = all.find(x => x.id === workoutId) || all[all.length - 1]
  if (!w) return null
  // workoutVolume() reads `w.entries` and `e.sets` without a guard; every other line here
  // tolerates both being absent. A workout carrying a date and nothing else therefore threw
  // inside the timer below, where at the time nothing caught it — the job stayed 'running'
  // for ever and every later request answered 409.
  const entries = (w.entries || []).map(en => ({ ...en, sets: en.sets || [] }))
  const done = entries.reduce((n, en) => n + en.sets.filter(s => s.done && !isWarmupRow(s)).length, 0)
  const planned = entries.reduce((n, en) => n + en.sets.filter(s => !isWarmupRow(s)).length, 0)
  const vol = Math.round(Number.isFinite(w.vol) ? w.vol : workoutVolume({ ...w, entries }))
  const prs = (w.prs || []).length
  const minutes = w.end && w.start ? Math.round((w.end - w.start) / 60000) : null
  const complete = planned > 0 && done >= planned
  const score = complete ? (prs ? 9 : 8) : 7
  const highlights = [
    complete ? t('Every planned set done: {0} of {1}.', done, planned) : t('{0} of {1} planned sets done.', done, planned),
    t('{0} {1} moved in total.', fmtNum(vol), S.unit)
  ]
  if (prs) highlights.push(t('{0} new personal records.', prs))
  const watch = minutes && minutes > 80 ? [t('{0} minutes is long. Rest periods may be creeping up.', minutes)] : [t('Top sets logged without an effort rating; add RIR so the next review can read how hard they were.')]
  const nextTime = [complete ? t('Add the next load step on the main lift.') : t('Repeat the same loads and get every set.'), t('Keep the session under an hour and a quarter.')]
  return {
    id: 'demo-debrief', kind: 'debrief', createdAt: Date.now(), expiresAt: Date.now() + 864e5,
    planHash: planHash(S), iteration: 1,
    workout: { id: w.id, d: w.d, name: w.name || null, minutes, vol, sets: done, prs },
    summary: complete
      ? t('A clean session: everything on the sheet got done and the loads held. This is exactly what progress looks like from the inside: unremarkable, repeated.')
      : t('Most of the work got done. One or two sets fell short, which is fine once. It only becomes a signal if the same sets miss next time.'),
    score, highlights, watch, nextTime
  }
}

/** What "the room" would say on a busy instance — five people, plausible medians, your real bests. */
export function demoCohort(S) {
  const since = Date.now() - 56 * 864e5
  const you = Math.round((S.workouts || []).filter(w => workoutAt(w) > since).length / 8 * 10) / 10
  const ids = [...new Set((S.routines || []).flatMap(r => (r.ex || []).map(e => e.id)))].slice(0, 5)
  const exercises = ids.map(id => {
    const b = best1RM(S, id)
    const mine = b ? Math.round(b.est * 10) / 10 : null
    return { id, name: EXIDX[id]?.n || id, people: 4, median: mine ? Math.round(mine * 0.92) : 60, you: mine }
  })
  return { ok: true, enabled: true, sharing: true, people: 5, minPeople: 3, unit: S.unit, sessionsPerWeek: { median: 3, you }, exercises, rankPct: 62 }
}

/* ---------------- the API surface the demo stands in for ---------------- */

export const demoStatus = () => ({ job, pending, cap: { used: 0, limit: 0 }, lastError })

function start(kind, make) {
  if (job) throw Object.assign(new Error(t('The Coach is already thinking about your training.')), { status: 409 })
  job = { id: 'demo-' + kind, kind, state: 'running', startedAt: Date.now() }
  lastError = null
  clearTimeout(timer)
  // A builder that throws still has to end the job: left uncaught, one throw kept `job` at
  // 'running' for ever and every later request answered 409 until the page was reloaded. The
  // failure is recorded the way the server and the phone record theirs, so the chat's
  // job-ended effect writes the same error line it would for them. `nostate` because the only
  // thing a builder can trip over is the shape of the profile's own data.
  timer = setTimeout(() => {
    try { pending = make() }
    catch (e) { lastError = { errorClass: 'nostate', detail: String(e && e.message || e) } }
    finally { job = null }
  }, DELAY)
  return { job }
}
export const demoReview = S => {
  // Refused before the job starts, the way demoDebrief refuses with no workout: without a
  // two-exercise routine buildReview has nothing to aim at, and a job that runs the whole wait
  // and ends with neither a proposal nor an error reads as the Coach ignoring you.
  if (!reviewable(S)) throw Object.assign(new Error(t('There’s no routine to review yet. Build one with at least two exercises first.')), { status: 409, code: 'noroutine' })
  return start('review', () => buildReview(S))
}
export const demoPlan = (S, intake) => start('create', () => buildPlan(S, intake))
// A refine re-reads the saved answers, as the server's payload does when a refine carries no
// intake of its own (`opts.intake || coach.profile`). Built from nothing, every revised plan
// went back to Mon/Wed/Fri whatever days were chosen at intake.
export const demoRefine = S => start('create', () => {
  const p = buildPlan(S, S.coach?.profile || null)
  return { ...p, iteration: (pending?.iteration || 1) + 1, summary: t('Revised as you asked. Everything you did not question is exactly as it was.') + ' ' + p.summary }
})
export const demoDebrief = (S, workoutId) => {
  if (!(S.workouts || []).some(w => w && w.d)) throw Object.assign(new Error(t('There’s no workout to look at yet. Log one first.')), { status: 409, code: 'noworkout' })
  return start('debrief', () => buildDebrief(S, workoutId))
}
// The demo has no model behind it: a question gets a sample answer that says so.
export const demoAsk = (S, text) => start('ask', () => ({
  id: 'demo-ask-' + Date.now().toString(36), kind: 'ask', createdAt: Date.now(), expiresAt: Date.now() + 864e5,
  planHash: planHash(S), iteration: 1, prev: waitingProposal(pending),
  answer: t('This is the demo, so there is no AI model behind me. On your own server I answer questions like “{0}” with your plan, your last sessions and the workout you are doing right now.', String(text || '').slice(0, 80)),
}))
export const demoResolve = () => { pending = pending?.kind === 'ask' ? waitingProposal(pending.prev) : null; return { ok: true } }
export const demoDisclosure = () => ({
  provider: 'demo', providerLabel: t('the configured AI provider'),
  categories: ['plan', 'training', 'bodyweight', 'profile', 'prefs'], version: 1
})