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.

Coach: a compatible base that already carries its API version is used as it is (#437)

iamtechnoanafd18a951eb9b
diff --git a/api/coach/core/adapters/http.js b/api/coach/core/adapters/http.js
index 93c043a0a..511a6a6c2 100644
--- a/api/coach/core/adapters/http.js
+++ b/api/coach/core/adapters/http.js
@@ -41,6 +41,16 @@ const hostOf = url => { try { return new URL(url).host; } catch { return url; }
const schemeOf = url => { try { return new URL(url).protocol === 'http:' ? 'HTTP' : 'HTTPS'; } catch { return 'HTTPS'; } };
const trim = (s, n = 300) => String(s == null ? '' : s).slice(0, n);
+// A base that already names its API version (OpenRouter's …/api/v1, Zhipu's …/paas/v4, Gemini's
+// …/v1beta/openai) is the whole prefix, so the spec's own leading version is dropped instead of
+// doubled into …/v4/v1/chat/completions (#437). A bare host still gets the spec's version.
+const VERSION_SEGMENT = /\/v\d+(?:(?:alpha|beta)\d*)?(?=\/|$)/i;
+export function endpointUrl(base, path) {
+ const basePath = base.replace(/^[a-z][a-z0-9+.-]*:\/\/[^/]*/i, '');
+ if (!VERSION_SEGMENT.test(basePath)) return base + path;
+ return base + path.replace(new RegExp('^' + VERSION_SEGMENT.source, 'i'), '');
+}
+
/** A fetch, bounded by AbortController. Never throws for HTTP status; throws for transport. */
async function call(fetchImpl, url, init, timeoutMs, signal) {
const ctl = new AbortController();
@@ -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, base + spec.modelsPath, { method: 'GET', headers: spec.headers(key) }, timeoutMs, signal);
+ res = await call(fetchImpl, endpointUrl(base, spec.modelsPath), { method: 'GET', headers: 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: [] };
}
@@ -133,7 +143,7 @@ export function httpAdapter(spec) {
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) },
body: JSON.stringify(body)
diff --git a/api/test/adapters-http.test.js b/api/test/adapters-http.test.js
index 05261579e..44deb44d0 100644
--- a/api/test/adapters-http.test.js
+++ b/api/test/adapters-http.test.js
@@ -107,6 +107,31 @@ test('compatible: the configured base URL, max_tokens, and no Authorization head
assert.equal(g.calls[0].headers.authorization, 'Bearer compat-1');
});
+test('compatible: a base that already names its API version is used as the full prefix (#437)', async () => {
+ const answer = ok({ choices: [{ message: { content: ANSWER }, finish_reason: 'stop' }] });
+ const at = baseUrl => ({ provider: 'compatible', providerOptions: { compatible: { baseUrl } } });
+ const cases = [
+ // Zhipu GLM serves Chat Completions only under v4; there is no /v1 alias.
+ ['https://open.bigmodel.cn/api/coding/paas/v4', 'https://open.bigmodel.cn/api/coding/paas/v4'],
+ // OpenRouter, Groq and most gateways document their base with /v1 on it.
+ ['https://openrouter.ai/api/v1', 'https://openrouter.ai/api/v1'],
+ // Gemini's OpenAI-compatible endpoint has its version mid-path.
+ ['https://generativelanguage.googleapis.com/v1beta/openai/', 'https://generativelanguage.googleapis.com/v1beta/openai'],
+ // A bare host keeps getting /v1, as before.
+ ['http://ollama.lan:11434', 'http://ollama.lan:11434/v1'],
+ // A version-looking word that is not a version segment is left alone.
+ ['http://gw.lan/v2ray', 'http://gw.lan/v2ray/v1']
+ ];
+ for (const [base, prefix] of cases) {
+ const f = fakeFetch([answer]);
+ await compatible.invoke({ cfg: at(base), prompt: 'P', env: {}, model: 'm', fetch: f });
+ assert.equal(f.calls[0].url, prefix + '/chat/completions', base);
+ const m = fakeFetch([ok({ data: [] })]);
+ await compatible.models(at(base), {}, { fetch: m });
+ assert.equal(m.calls[0].url, prefix + '/models', base);
+ }
+});
+
test('compatible: no endpoint configured, or no model chosen, is a clean failure rather than a request', async () => {
const f = fakeFetch([]);
const none = await compatible.invoke({ cfg: {}, prompt: 'P', env: {}, model: 'x', fetch: f });
diff --git a/docs/AI_COACH.md b/docs/AI_COACH.md
index 20c92ebee..8b94814d7 100644
--- a/docs/AI_COACH.md
+++ b/docs/AI_COACH.md
@@ -73,7 +73,10 @@ answers on instead, and a key only if it wants one.
- Pick the provider chip.
- For a compatible endpoint, enter the **Endpoint** — `http://` or `https://`, no username or
password 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.
- **Use an API key** → paste it. It is encrypted into `./data/coach.json` and is never shown
again.
- **List models** asks the endpoint what it serves and turns the model field into a picker.
diff --git a/frontend/src/views/AdminCoach.jsx b/frontend/src/views/AdminCoach.jsx
index 9e69872ce..d5c367ab4 100644
--- a/frontend/src/views/AdminCoach.jsx
+++ b/frontend/src/views/AdminCoach.jsx
@@ -188,7 +188,7 @@ export default function AdminCoach() {
{/* ---------- endpoint (compatible only) ---------- */}
{needsEndpoint && <Step n={num()} title="Endpoint" hint={d.baseUrl || 'Where the model runs'} done={step2Done} {...stepAt()}>
- <div className="adm-hint">The address of any server that speaks OpenAI's chat API: <b>Ollama</b>, <b>LM Studio</b>, <b>vLLM</b>, <b>OpenRouter</b>, or a gateway of your own. Just the base: no <code>/v1</code>, no key in the URL.</div>
+ <div className="adm-hint">The address of any server that speaks OpenAI's chat API: <b>Ollama</b>, <b>LM Studio</b>, <b>vLLM</b>, <b>OpenRouter</b>, or a gateway of your own. The base as your provider documents it, with or without its version (<code>/v1</code>, <code>/v4</code>), and no key in the URL.</div>
<div className="adm-field">
<label>Base URL</label>
<TextField key={d.baseUrl || ''} defaultValue={d.baseUrl || ''} placeholder="http://ollama:11434 or https://openrouter.ai/api" inputMode="url" autoCapitalize="none" autoCorrect="off"