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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Merge remote-tracking branch 'pr/386'
Duarte Santos540d361123c7
diff --cc api/coach/core/adapters/http.jsindex 3517d3514,a24ccee7d..f105e3a46--- a/api/coach/core/adapters/http.js+++ b/api/coach/core/adapters/http.js@@@ -111,7 -101,7 +111,7 @@@ export function httpAdapter(spec)if (!key && !meta.keyOptional) return { ok: false, error: 'no API key configured', models: [] };let res;try {- res = await call(fetchImpl, endpointUrl(base, spec.modelsPath), { method: 'GET', headers: spec.headers(key) }, timeoutMs, signal);- res = await call(fetchImpl, base + spec.modelsPath, { method: 'GET', headers: { ...extraHeadersFor(id, cfg), ...spec.headers(key) } }, timeoutMs, signal);++ res = await call(fetchImpl, endpointUrl(base, spec.modelsPath), { method: 'GET', headers: { ...extraHeadersFor(id, cfg), ...spec.headers(key) } }, timeoutMs, signal);} catch (e) {return { ok: false, error: e.name === 'AbortError' ? 'timed out' : `could not reach ${hostOf(base)}: ${trim(e.message, 120)}`, models: [] };}@@@ -143,9 -133,9 +143,9 @@@for (;;) {let res;try {- res = await call(fetchImpl, base + spec.path(chosen), {+ res = await call(fetchImpl, endpointUrl(base, spec.path(chosen)), {method: 'POST',- headers: { 'content-type': 'application/json', ...spec.headers(key) },+ headers: { ...extraHeadersFor(id, cfg), 'content-type': 'application/json', ...spec.headers(key) },body: JSON.stringify(body)}, timeoutMs, signal);} catch (e) {diff --cc api/test/adapters-http.test.jsindex f58cf998b,79e827769..d2629e947--- a/api/test/adapters-http.test.js+++ b/api/test/adapters-http.test.js@@@ -331,24 -306,35 +331,57 @@@ test('models(): OpenAI’s list is cutassert.deepEqual((await compatible.models({ providerOptions: { compatible: { baseUrl: 'http://ollama:11434' } } }, {}, { fetch: co })).models, ['llama3.2', 'qwen2.5:3b']);});+test('invoke sends the configured maxTokens override, and the adapter default when none is given', async () => {+ const { MAX_OUTPUT_TOKENS } = await import('../coach/core/adapters/http.js');++ // No override: every caller that configures nothing (the phone, warmup) still gets the default.+ const d = fakeFetch([ok({ choices: [{ message: { content: ANSWER }, finish_reason: 'stop' }] })]);+ await compatible.invoke({ cfg: cfgCompat, prompt: 'P', env: {}, model: 'm', fetch: d });+ assert.equal(d.calls[0].body.max_tokens, MAX_OUTPUT_TOKENS);++ // An override, on each wire shape that carries the ceiling.+ const o = fakeFetch([ok({ choices: [{ message: { content: ANSWER }, finish_reason: 'stop' }] })]);+ await openai.invoke({ cfg: {}, prompt: 'P', env, model: 'gpt-x', fetch: o, maxTokens: 48000 });+ assert.equal(o.calls[0].body.max_completion_tokens, 48000);++ const a = fakeFetch([ok({ content: [{ type: 'text', text: ANSWER }], stop_reason: 'end_turn' })]);+ await anthropic.invoke({ cfg: {}, prompt: 'P', env, fetch: a, maxTokens: 48000 });+ assert.equal(a.calls[0].body.max_tokens, 48000);++ const g = fakeFetch([ok({ candidates: [{ content: { parts: [{ text: ANSWER }] }, finishReason: 'STOP' }] })]);+ await gemini.invoke({ cfg: {}, prompt: 'P', env, model: 'gemini-z', fetch: g, maxTokens: 48000 });+ assert.equal(g.calls[0].body.generationConfig.maxOutputTokens, 48000);+});+++ test('compatible: configured extra headers ride on models, check and jobs — auth framing always wins', async () => {+ const cfg = { provider: 'compatible', providerOptions: { compatible: { baseUrl: 'http://ollama.lan:11434', headers: { 'x-opencode-session': 'sess-1' } } } };+ const m = fakeFetch([ok({ data: [] })]);+ await compatible.models(cfg, env, { fetch: m });+ assert.equal(m.calls[0].headers['x-opencode-session'], 'sess-1');+ assert.equal(m.calls[0].headers.authorization, 'Bearer compat-1');+ const j = fakeFetch([ok({ choices: [{ message: { content: ANSWER }, finish_reason: 'stop' }] })]);+ await compatible.invoke({ cfg, prompt: 'P', env, model: 'llama3', fetch: j });+ assert.equal(j.calls[0].headers['x-opencode-session'], 'sess-1');++ // A stored map from before a name was reserved must not override auth framing.+ const evil = { provider: 'compatible', providerOptions: { compatible: { baseUrl: 'http://x', headers: { authorization: 'Bearer evil', 'content-type': 'text/plain' } } } };+ const e = fakeFetch([ok({ choices: [{ message: { content: ANSWER }, finish_reason: 'stop' }] })]);+ await compatible.invoke({ cfg: evil, prompt: 'P', env, model: 'llama3', fetch: e });+ assert.equal(e.calls[0].headers.authorization, 'Bearer compat-1');+ assert.equal(e.calls[0].headers['content-type'], 'application/json');+ });++ test('validateHeaders: small maps pass, framing names and junk do not', () => {+ assert.deepEqual(validateHeaders(null).value, null);+ assert.deepEqual(validateHeaders({}).value, null);+ assert.deepEqual(validateHeaders({ 'X-Opencode-Session': ' abc ' }).value, { 'x-opencode-session': 'abc' });+ for (const bad of ['authorization', 'Content-Type', 'content-length', 'Host', 'cookie']) {+ assert.equal(validateHeaders({ [bad]: 'x' }).ok, false, bad);+ }+ assert.equal(validateHeaders({ 'not a name!': 'x' }).ok, false);+ assert.equal(validateHeaders({ a: '' }).ok, false);+ assert.equal(validateHeaders('x-opencode-session: abc').ok, false);+ assert.equal(validateHeaders([['a', 'b']]).ok, false);+ const many = {}; for (let i = 0; i < 9; i++) many['x-h-' + i] = 'v';+ assert.equal(validateHeaders(many).ok, false);+ });diff --cc api/test/routes.test.jsindex 6b28b45fc,38d4b20ce..6070b19f0--- a/api/test/routes.test.js+++ b/api/test/routes.test.js@@@ -235,42 -235,28 +235,68 @@@ test('an admin can raise the Coach messassert.equal(status.body.maxMessageLen, 1800);});+// The guard in front of every user route answers 503 while the Coach is switched off or has no+// provider connected. It sent the words with no class, so the app could not pick its own,+// translated line for it and a German lifter read "the Coach is not set up on this instance". It+// names the class now: 'off', the class enqueue's own refusal carries for the same state.+test('the Coach-off guard names its class beside the words, on every guarded user route', async () => {+ const guarded = ['GET /api/coach/status', 'POST /api/coach/plan', 'POST /api/coach/review', 'POST /api/coach/debrief',+ 'GET /api/coach/cohort', 'POST /api/coach/cohort/share', 'POST /api/coach/pending/resolve'];+ const refusal = { error: 'the Coach is not set up on this instance', code: 'off' };+ const { call } = harness();+ for (const [state, patch] of [['switched off', { enabled: false }], ['no provider connected', { provider: 'anthropic' }]]) {+ fresh(patch);+ assert.equal(cfg.isConnected(), false, state);+ for (const key of guarded) {+ const r = await call(key, {});+ assert.equal(r.status, 503, `${state}: ${key}`);+ assert.deepEqual(r.body, refusal, `${state}: ${key}`);+ }+ }+});++/* ---------- configurable max output tokens (reasoning models) ---------- */+test('an admin can raise the Coach output cap for a reasoning model, clamped to a sane range', async () => {+ fresh();+ const { call } = harness();++ // The old hard-coded constant, for an instance that has never touched the setting.+ assert.equal((await call('GET /api/admin/coach')).body.maxOutputTokens, 16000);++ const set = await call('POST /api/admin/coach/config', { maxOutputTokens: 48000 });+ assert.equal(set.status, 200);+ assert.equal((await call('GET /api/admin/coach')).body.maxOutputTokens, 48000);+ assert.equal(cfg.load().maxOutputTokens, 48000, 'persists with the rest of the Coach config');++ // Clamped, not trusted outright — a reasoning model needs more than 16000, but not a million.+ await call('POST /api/admin/coach/config', { maxOutputTokens: 999999 });+ assert.equal((await call('GET /api/admin/coach')).body.maxOutputTokens, 65536, 'ceiling');+ await call('POST /api/admin/coach/config', { maxOutputTokens: 1 });+ assert.equal((await call('GET /api/admin/coach')).body.maxOutputTokens, 1024, 'floor');+});+++ /* ---------- compatible extra headers (issue #385) ---------- */+ test('an admin can store extra headers for the compatible endpoint; framing names are refused', async () => {+ fresh();+ const { call } = harness();++ // Empty by default.+ assert.equal((await call('GET /api/admin/coach')).body.headers, null);++ let r = await call('POST /api/admin/coach/config', { provider: 'compatible', baseUrl: 'http://127.0.0.1:9', headers: { 'x-opencode-session': 'sess-1' } });+ assert.equal(r.status, 200);+ assert.deepEqual((await call('GET /api/admin/coach')).body.headers, { 'x-opencode-session': 'sess-1' });+ assert.deepEqual(cfg.load().providerOptions.compatible.headers, { 'x-opencode-session': 'sess-1' }, 'persists with the rest of the Coach config');++ // Reserved framing names are refused, and a fixed-endpoint provider takes none.+ r = await call('POST /api/admin/coach/config', { headers: { authorization: 'Bearer x' } });+ assert.equal(r.status, 400);+ r = await call('POST /api/admin/coach/config', { provider: 'openai', headers: { 'x-a': 'b' } });+ assert.equal(r.status, 400);++ // Clearing with null drops the key rather than storing an empty map.+ r = await call('POST /api/admin/coach/config', { provider: 'compatible', headers: null });+ assert.equal(r.status, 200);+ assert.equal((await call('GET /api/admin/coach')).body.headers, null);+ assert.equal('headers' in cfg.load().providerOptions.compatible, false);+ });diff --cc docs/AI_COACH.mdindex 66c4450a9,dfedacd5b..1804fb259--- a/docs/AI_COACH.md+++ b/docs/AI_COACH.md@@@ -73,10 -73,10 +73,13 @@@ answers on instead, and a key only if i- Pick the provider chip.- For a compatible endpoint, enter the **Endpoint** — `http://` or `https://`, no username orpassword in it, no query string. The host is written into the job log so you can see where- jobs went.+ jobs went. Paste the base the way the provider documents it: a bare host+ (`http://ollama.lan:11434`) gets `/v1` added, and a base that already carries its version+ (`https://openrouter.ai/api/v1`, Zhipu's `…/paas/v4`, Gemini's `…/v1beta/openai`) is used+ as it is.+ - A gateway that demands extra headers gets them under **Extra headers**: one `Name: value`+ per line (e.g. opencode Go's `x-opencode-session`), sent with the list, test and job+ calls. `Authorization` and `Content-Type` are refused there — auth framing always wins.- **Use an API key** → paste it. It is encrypted into `./data/coach.json` and is never shownagain.- **List models** asks the endpoint what it serves and turns the model field into a picker.diff --cc frontend/src/views/AdminCoach.jsxindex 8f44f81a3,355445c32..05b2f05bc--- a/frontend/src/views/AdminCoach.jsx+++ b/frontend/src/views/AdminCoach.jsx@@@ -3,8 -3,7 +3,8 @@@ import { useUI } from '../store/useUI.jimport { useStore } from '../store/useStore.js'import { api } from '../lib/api.js'import Icon from '../components/Icon.jsx'- import { Button, Switch, TextField } from '../components/ui.jsx'+ import { Button, Switch, TextArea, TextField } from '../components/ui.jsx'+import { confirmSheet } from '../sheets.jsx'/* The operator's side of the Coach, laid out as a guided setup: one master switch, numberedsteps that each say what they are for, and everything an owner rarely needs folded away