diff --git a/.env.example b/.env.example index c3b57fb..bc16929 100644 --- a/.env.example +++ b/.env.example @@ -148,8 +148,10 @@ RP_NAME=openGym # COACH_DISABLED=1 # # COACH_JOB_TIMEOUT_MS gives a job longer than the default five minutes. Only a -# local model on a CPU needs it (Ollama on a small box: ten minutes is normal); -# a hosted API that needs more has a problem. Never goes below one minute. +# local model on a CPU needs it — and normally only for the first request after +# the model server restarts (the api pre-warms the rules cache after that; a +# warm review on a 3B model takes about a minute). A hosted API that needs +# more has a problem. Never goes below one minute. # # COACH_JOB_TIMEOUT_MS=1500000 # diff --git a/docs/AI_COACH.md b/docs/AI_COACH.md index 1fff4e6..eb0c106 100644 --- a/docs/AI_COACH.md +++ b/docs/AI_COACH.md @@ -75,6 +75,33 @@ A key, a model and the account binding described below belong to the provider th entered for. Switching chips does not clear them: the Anthropic key is still there when you come back from trying Gemini, and each chip shows a mark when it holds one. +#### Picking and changing models + +The model field is never a guess: **List models** asks the endpoint what it actually serves. +For an Ollama box that means the admin decides what exists — `ollama pull` anything the +hardware can hold and it appears in the picker on the next refresh; nothing in openGym pins +you to a blessed list. The choice is remembered **per provider**, so trying a cloud model for +a week and coming back to the local one restores exactly what was set before. + +#### Performance on a small box + +The Coach's rules ride as a byte-identical system prompt, so a llama.cpp/Ollama endpoint can +keep them in its prefix cache and only ever re-process the payload — and the api warms that +cache itself at boot and every half hour, so the expensive first read happens off everyone's +clock. Measured on a 4-core, no-GPU VM with `qwen2.5:3b`: a review costs about **a minute** +warm, versus ~8 minutes before the cache and slimmer payload existed. Three things keep it +that way: + +- `OLLAMA_KEEP_ALIVE=-1` and `OLLAMA_NUM_PARALLEL=1` on the Ollama side — an unloaded model + takes its cache with it, and parallel slots split it. +- Give the container room. A ~3B model at Q4 lives happily in 4 GB; a 4B model squeezed into + the same limit does not fail, it *thrashes* — weights fall out of the page cache and + generation drops to ~1 token/s. On a 4–6 GB box, smaller and comfortable wins. +- The endpoint is sent a **JSON schema** with each request (structured outputs), so the shape + of the answer is enforced while it is generated and the repair round is rarely needed. + Servers that reject schemas get plain JSON mode automatically, then no JSON mode — the + validator is the gate either way. + ### With a runtime in the container (Claude Agent SDK, Codex CLI) **1. Build the image that has an AI runtime in it.** The default image deliberately has none: @@ -231,11 +258,12 @@ is a provider problem rather than a prompting problem, and a retry loop against is a bad way to find that out. A job is given five minutes by default, which is generous for any hosted API. A model running -on a CPU behind the compatible endpoint can need more — a 3B model with an 8k prompt takes ten -minutes on a small VM — so `COACH_JOB_TIMEOUT_MS` in `.env` raises it (never below one -minute); the api's own HTTP client waits as long as the job, and the chat says "this can take a -while" rather than promising minutes when the endpoint is a local one. The phone's BYOK mode -does the same on its own: a local endpoint gets 25 minutes there. +on a CPU behind the compatible endpoint can need more — with a warm cache a 3B model answers +in about a minute, but the first job after the model loads pays the full prompt once — so +`COACH_JOB_TIMEOUT_MS` in `.env` raises the budget (never below one minute); the api's own +HTTP client waits as long as the job, and the chat says "this can take a while" rather than +promising minutes when the endpoint is a local one. The phone's BYOK mode does the same on +its own: a local endpoint gets 25 minutes there. The Coach may only touch **routines and the week**. Your training log, your weigh-ins and your settings are not reachable from any change type that exists. diff --git a/frontend/src/admin.css b/frontend/src/admin.css index 98d307b..a1359da 100644 --- a/frontend/src/admin.css +++ b/frontend/src/admin.css @@ -110,3 +110,34 @@ } .adm-iconbtn{width:44px;height:44px;border-radius:12px;font-size:17px} .adm-empty{color:var(--label-3);font-size:13.5px;padding:10px 2px} + +/* ---------- the Coach before setup: a pitch, not a form ---------- */ +.adm-hero{display:flex;flex-direction:column;align-items:flex-start;gap:4px;padding:6px 2px 2px} +.adm-hero-av{ + width:52px;height:52px;border-radius:16px;display:grid;place-items:center;font-size:26px; + background:linear-gradient(135deg,var(--acc),color-mix(in srgb,var(--acc) 55%,var(--teal))); + color:var(--on-acc);margin-bottom:8px;box-shadow:0 8px 24px -8px color-mix(in srgb,var(--acc) 55%,transparent) +} +.adm-hero h2{margin:0 0 4px;font-size:22px;letter-spacing:-.022em} +.adm-hero p{margin:0 0 14px;font-size:14px;line-height:1.5;color:var(--label-2)} +.adm-hero-feats{display:grid;gap:10px;margin-bottom:18px} +.adm-hero-feats>div{display:flex;gap:11px;align-items:flex-start} +.adm-hero-feats .icn{color:var(--acc);font-size:18px;flex:none;margin-top:2px} +.adm-hero-feats span{font-size:13.5px;line-height:1.45;color:var(--label-2)} +.adm-hero-feats b{color:var(--label);font-weight:600} +.adm-hero .btn{width:100%} + +/* ---------- setup progress ---------- */ +.adm-progress{height:5px;border-radius:99px;background:var(--surface-2);overflow:hidden;margin:0 0 10px} +.adm-progress i{display:block;height:100%;border-radius:99px;background:var(--acc);transition:width var(--med) var(--ease)} + +/* ---------- provider chips: clearly idle vs. clearly chosen ---------- */ +.adm-chips .chip{ + background:var(--surface-2);border:1.5px solid transparent;color:var(--label); + font-weight:500;text-transform:none +} +.adm-chips .chip.on{background:var(--acc-soft);border-color:var(--acc);color:var(--acc);font-weight:600} +.adm-chips .chip:active{transform:scale(.97)} + +/* a finished step's summary reads as a receipt, not a control */ +.adm-step.done>summary .adm-step-t span{color:var(--acc)} diff --git a/frontend/src/index.css b/frontend/src/index.css index 9735d0e..80176b8 100644 --- a/frontend/src/index.css +++ b/frontend/src/index.css @@ -990,3 +990,21 @@ h4.sec{ .exmedia img{height:380px} .hm-wrap{display:flex;flex-direction:column;align-items:center} } + +/* ------------------------------------------------- the Coach entry on Plan -- */ +.coach-cta{ + display:flex;align-items:center;gap:13px;width:100%;text-align:left; + margin-bottom:12px;padding:14px 16px;border-radius:var(--r-card); + background:linear-gradient(135deg,var(--acc-soft),color-mix(in srgb,var(--acc) 6%,var(--surface))); + border:1px solid color-mix(in srgb,var(--acc) 35%,transparent); + transition:transform var(--fast),filter var(--fast) +} +.coach-cta:active{transform:scale(.985);filter:brightness(1.06)} +.coach-cta-av{ + width:42px;height:42px;border-radius:13px;flex:none;display:grid;place-items:center;font-size:21px; + background:var(--acc);color:var(--on-acc);box-shadow:0 6px 18px -6px color-mix(in srgb,var(--acc) 60%,transparent) +} +.coach-cta-t{flex:1;min-width:0;display:flex;flex-direction:column;gap:1px} +.coach-cta-t b{font-size:17px;font-weight:600;letter-spacing:-.015em;color:var(--label)} +.coach-cta-t span{font-size:13px;color:var(--label-2);line-height:1.35} +.coach-cta-chev{color:var(--acc);font-size:15px;flex:none} diff --git a/frontend/src/views/AdminCoach.jsx b/frontend/src/views/AdminCoach.jsx index 247a9e3..1ccb53f 100644 --- a/frontend/src/views/AdminCoach.jsx +++ b/frontend/src/views/AdminCoach.jsx @@ -110,9 +110,30 @@ export default function AdminCoach() { const step3Done = authed const step4Done = !!d.model const step5Done = !!testResult?.ok || !!d.lastSuccess - // Step numbers only count the steps this provider actually shows. + // Step numbers only count the steps this provider actually shows — and like any wizard, + // only the first unfinished step stands open; everything done folds to its summary line. + const flags = [step1Done, ...(needsEndpoint ? [step2Done] : []), ...(hasCredentialStep ? [step3Done] : []), step4Done, step5Done] + const doneCount = flags.filter(Boolean).length + const firstTodo = flags.indexOf(false) let n = 1 + let idx = 0 const num = () => n++ + const stepAt = () => { const i = idx++; return { open: i === (firstTodo === -1 ? -1 : firstTodo), key: i + ':' + (i === firstTodo) } } + + // Off = a quiet, optional feature: one clean pitch and one button, no half-dimmed controls. + if (!d.enabled) return
+
+
+

AI Coach

+

An optional coach that designs training plans and reviews what people actually log. Off right now — nobody sees it anywhere in the app.

+
+
Bring any AI. An API key from Anthropic, OpenAI or Gemini — or a free local model via Ollama.
+
Private by design. A strict allowlist decides what leaves; every change needs the user's yes and can be undone.
+
Each user decides. Turning it on only makes the Coach available; every person consents for themselves.
+
+ +
+
return
@@ -120,16 +141,18 @@ export default function AdminCoach() { patch({ enabled: v })} />
- {!d.enabled ? 'off' : live ? 'ready' : 'not ready'} + {live ? 'ready' : 'not ready'} {status}
+ {!live && }
- The Coach designs a training plan and adjusts it from what people log. This switch is the only place it can be turned off for everyone — users decide for themselves whether to use it, but cannot disable it. + {live ? 'Users find the Coach under Plan → Coach. This switch is the only place it can be turned off for everyone.' + : `${doneCount} of ${flags.length} steps done — finish the open step and the next one unfolds.`}
{d.enabled && <> {/* ---------- provider ---------- */} - +
Pick who answers. A key or token you save stays with its provider, so you can switch back and forth without pasting it again.
{groups.map(g => !!g.items.length &&
{g.title}
@@ -144,7 +167,7 @@ export default function AdminCoach() { {/* ---------- endpoint (compatible only) ---------- */} - {needsEndpoint && + {needsEndpoint &&
The address of any server that speaks OpenAI's chat API: Ollama, LM Studio, vLLM, OpenRouter, or a gateway of your own. Just the base — no /v1, no key in the URL.
@@ -155,14 +178,14 @@ export default function AdminCoach() { } {/* ---------- credential ---------- */} - {hasCredentialStep && + {hasCredentialStep &&
{authState === 'connected' ? <>
Connected{d.auth.account ? ' as ' + d.auth.account : ''} via {credentialLabel(d.auth.type)}{d.auth.connectedAt ? ' · added ' + rel(d.auth.connectedAt) : ''}. The key is stored encrypted and is never shown again.
- {meta.apiKey && }
@@ -185,7 +208,7 @@ export default function AdminCoach() {
} {/* ---------- model ---------- */} - +
{meta.http ? 'Which model the provider should use. "List models" asks the provider for its current list, so nothing here goes stale.' : 'Optional. Leave it empty to use the runtime\'s own default.'}
@@ -201,17 +224,20 @@ export default function AdminCoach() { onBlur={e => e.target.value !== (d.models?.[d.provider] || '') && patch({ model: e.target.value })} />}
{meta.http &&
- + {models && models.length ? {models.length} served by the provider : null}
}
{/* ---------- test ---------- */} - +
Sends one tiny question to the provider and checks the answer. No training data is involved. Do this after every change above.
+ {(!authed || !hasEndpoint) &&
+ {!hasEndpoint ? 'Finish the Endpoint step first.' : 'Finish the Credential step first.'} +
} {testResult &&
{testResult.pending ? 'Asking the provider…' : testResult.ok ? <>Passed{testResult.version ? 'Provider: ' + testResult.version : 'The provider answered as expected.'} @@ -294,8 +320,9 @@ export default function AdminCoach() { /* ---------------------------------- pieces ---------------------------------- */ -function Step({ n, title, hint, done, open, children }) { - return
+function Step({ n, title, hint, done, open, forceOpen, children }) { + // `key` remounts the
when the wizard advances, so the next step unfolds itself. + return
{done ? : n} {title}{hint} @@ -329,7 +356,7 @@ const credentialLabel = type => ({ // The failure classes jobs.js emits, in words an operator can act on. const failureTitle = cls => ({ - timeout: 'The provider took too long (over 5 minutes)', + timeout: 'The provider took longer than the job budget (COACH_JOB_TIMEOUT_MS, default 5 minutes)', missing: 'The provider runtime or key is missing', auth: 'The provider rejected the credential', provider: 'The provider returned an error', diff --git a/frontend/src/views/Plan.jsx b/frontend/src/views/Plan.jsx index a429d24..2594e28 100644 --- a/frontend/src/views/Plan.jsx +++ b/frontend/src/views/Plan.jsx @@ -35,12 +35,14 @@ export default function Plan() {

{t('Plan')}

{t('Your weekly routine')}
- {showCoach &&
- nav('/coach')} /> -
} + {showCoach && }

{t('Week schedule')}

diff --git a/website/docs.html b/website/docs.html index 1194807..2791c92 100644 --- a/website/docs.html +++ b/website/docs.html @@ -139,28 +139,92 @@ docker compose up -d mcp/README.md.

Let an AI coach write your plan

-

Separate from the read-only bridge above, openGym has an optional AI Coach that can - design a training plan and, later, review what you actually logged and propose changes to - it. Answer a few questions — goal, days a week, session length, equipment, anything to work - around — and it builds a week of routines. Every suggestion carries the evidence behind it, - you approve or reject each one, and you can undo the last set of changes.

-

It is off by default. An admin turns it on in Settings → Admin → AI Coach, - picks a provider and pastes a key: Anthropic, OpenAI, Google Gemini, or - any OpenAI-compatible endpoint — Ollama or LM Studio on your own LAN, vLLM, - OpenRouter. These speak plain HTTPS and need nothing beyond the default image. Only the - Claude Agent SDK and the Codex CLI need the bigger build:

-
API_TARGET=coach docker compose up -d --build api
-

On the phone app, Settings → AI Coach offers two ways in: pair the phone with your - self-hosted instance and the Coach runs there, or bring your own API key and the phone calls - the provider directly, key in the platform's secure storage.

-

It runs on your server under your account — there is no openGym - service in the middle, and nothing is sent anywhere until you ask it for something. What - leaves the server is a by-name allowlist (your plan, your logged sets, body weight if you - track it) under a pseudonym; your name, sign-in details and other profiles never do. What - comes back can only touch routines and your weekly schedule, and is applied on your device - after you approve it.

-

The Coach is not a doctor or a physiotherapist. If something hurts, ask a professional. - Full details, including exactly what is sent and how credentials are stored: +

Separate from the read-only bridge above, openGym has an optional AI Coach. Answer a + few questions — goal, days a week, session length, equipment, anything to work around — and + it designs a week of routines. Later it reviews what you actually logged and proposes + changes; you can also ask it to look at one workout (a debrief: a score, what went + well, what to watch, what to do next time) or to improve a single routine and leave + the rest alone. If the admin allows it and at least three people opt in, it can show + anonymous medians so you can see how you sit against the others on your instance.

+ +

How it works

+

The Coach is a chat. The first visit is a short intake, one question per screen; after + that, every request works the same way:

+
    +
  1. The server builds a JSON payload from a by-name allowlist: your plan, the sets + you logged in the review window, weigh-ins, your intake answers, and a few preferences — + under a pseudonym. Your name, e-mail, passkeys and everyone else's data are not in it, + by construction: fields are copied in one by one, nothing is passed through.
  2. +
  3. That payload goes to the provider you configured — there is no openGym service in + the middle.
  4. +
  5. The answer is parsed and validated against a closed list of change types (add, + remove or swap an exercise, sets, reps, schedule, progression policy…). An answer that + names an exercise that doesn't exist, invents a change type, or breaks a rule is + refused — it gets exactly one repair round, then the job fails rather than guessing.
  6. +
  7. What survives is shown to you as a card, one checkbox per change, each with the + evidence behind it. Nothing touches your plan until you accept it, every proposal stays + in the chat history whether you said yes or no, and the last accepted change-set can be + undone in one tap.
  8. +
+ +

Providers

+

It is off by default. An admin turns it on in Settings → Admin → AI Coach and + picks who answers:

+ + + + +
Anthropic, OpenAI, Google GeminiPaste an API key from the provider's console. Plain HTTPS from the api process — runs on the default image.
OpenAI-compatible endpointAny server that speaks the Chat Completions API: Ollama, LM Studio, vLLM, OpenRouter, a gateway of your own. A key only if the endpoint wants one. Also the default image.
Claude Agent SDK / Codex CLIRun a real AI runtime inside the container — the one case that needs the bigger build: API_TARGET=coach docker compose up -d --build api
+ +

Run it fully local — no cloud, no cost

+

A model on your own hardware is just the compatible endpoint with no key. The usual way is + an Ollama container next to openGym:

+
# docker-compose.yml — add next to the openGym services (same file = same network)
+  ollama:
+    image: ollama/ollama
+    restart: unless-stopped
+    volumes:
+      - ./data/ollama:/root/.ollama
+    environment:
+      - OLLAMA_KEEP_ALIVE=-1     # keep the model (and its prompt cache) loaded
+      - OLLAMA_NUM_PARALLEL=1
+    mem_limit: 4g
+
docker exec -it <ollama container> ollama pull qwen2.5:3b
+

Then in Settings → Admin → AI Coach: provider OpenAI-compatible endpoint → + Base URL http://ollama:11434 → List models → pick the model → + Test the Coach. That's the whole setup.

+

Any model you can pull, you can use. ollama pull whatever you like and + it appears under List models — the admin is free to run anything the box can hold, + and each provider remembers its own model choice, so switching around never loses one. + Honest sizing, from measuring exactly this on a small box:

+
    +
  • A ~3B model at Q4 (like qwen2.5:3b) fits a 4–6 GB container and + answers a review in about a minute on a 4-core CPU once its cache is warm.
  • +
  • A 4B model needs ~6 GB+ for its container. Squeezed into less, it doesn't + fail — it thrashes, generating at ~1 token/second. Smaller and comfortable beats bigger + and swapping.
  • +
  • The first request after a restart is slow once: the model loads and reads the + Coach's rules. The api pre-warms that cache at boot and every half hour, so in practice + people hit the warm path.
  • +
  • On a genuinely slow box, raise the job budget with COACH_JOB_TIMEOUT_MS + in .env.
  • +
+

A GPU machine elsewhere on your LAN works the same way: run Ollama or LM Studio there and + point the Base URL at it, e.g. http://192.168.1.50:11434.

+ +

On the phone

+

Settings → AI Coach offers two ways in: pair the phone with your self-hosted + instance and the Coach runs there under the rules above, or bring your own API key and the + phone calls the provider directly — same allowlist, same validator, key in the platform's + secure storage, ten runs a day.

+ +

Privacy and limits

+

Before anyone uses it, a consent screen lists exactly which categories of data leave the + server — generated from the same module that builds the payloads, so it cannot drift from + what actually happens. The admin can cap runs per person and per instance per day, and + COACH_DISABLED=1 in the environment switches the feature off harder than any + toggle. The Coach is not a doctor or a physiotherapist — if something hurts, ask a + professional. Full details, including exactly what is sent and how credentials are stored: docs/AI_COACH.md.

Import your training history