Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
Connect app: the third tab and strata connect give the server root (no /v1); the server also accepts /v1/v1/messages
Niko1221323a11e72183
diff --git a/apps/host/server/api-paths.mjs b/apps/host/server/api-paths.mjsnew file mode 100644index 000000000..77d4d49e9--- /dev/null+++ b/apps/host/server/api-paths.mjs@@ -0,0 +1,12 @@+/**+ * Anthropic clients add `/v1/messages` to the base URL themselves, so the base URL they are given is the server root (no `/v1`).+ * A base URL copied from an OpenAI-style setup ends in `/v1`, and the client then posts `/v1/v1/messages`. That one path means the+ * same as the real one; nothing else is aliased (an OpenAI client never adds `/v1` on its own).+ */+const ALIASES = new Map([['/v1/v1/messages', '/v1/messages'], ['/v1/v1/messages/count_tokens', '/v1/messages/count_tokens']]);++export function apiPath(path) {+ const clean = path.length > 1 ? path.replace(/\/+$/, '') : path;+ return ALIASES.get(clean) ?? path;+}+diff --git a/apps/host/server/index.mjs b/apps/host/server/index.mjsindex ede15db88..139ffd104 100644--- a/apps/host/server/index.mjs+++ b/apps/host/server/index.mjs@@ -68,6 +68,7 @@ import { forbidden as acquisitionForbidden } from './acquisition.mjs';import { randomBytes, timingSafeEqual } from 'node:crypto';import { nativeGateway } from './native-gateway.mjs';import { proxyCompletion } from './inference.mjs';+import { apiPath } from './api-paths.mjs';import { validateChatImages } from './chat-images.mjs';import { createTimeSkip, measureFolder, timeskipRevisionMessage, imageTypeOf } from './timeskip.mjs';@@ -925,7 +926,7 @@ const server = http.createServer(async (req, res) => {}if (path.startsWith('/v1/')) {if (storageChanging && method !== 'GET') return error(res, 409, 'Wait for the model folder check to finish.');- return await openai(req, res, path, method);+ return await openai(req, res, apiPath(path), method);}return await staticFile(req, res, path);} catch (err) {diff --git a/apps/host/server/local-share.mjs b/apps/host/server/local-share.mjsindex 34cc5a359..788932f97 100644--- a/apps/host/server/local-share.mjs+++ b/apps/host/server/local-share.mjs@@ -1,6 +1,7 @@/** A separate, key-protected listener for inference only. Never serves the UI or host APIs. */import http from 'node:http';import os from 'node:os';+import { apiPath } from './api-paths.mjs';export function createLocalShare({ handle, hasKey, changed = () => {}, host = '0.0.0.0', port = 8792 }) {let listener = null, active = false;const address = () => Object.values(os.networkInterfaces()).flat().find(n => n?.family === 'IPv4' && !n.internal)?.address ?? '127.0.0.1';@@ -10,7 +11,7 @@ export function createLocalShare({ handle, hasKey, changed = () => {}, host = '0if (listener) throw new Error('Sharing is already starting.');if (!hasKey()) throw new Error('Set an API key before sharing with other devices.');listener = http.createServer(async (req, res) => {- const path = new URL(req.url ?? '/', 'http://localhost').pathname;+ const path = apiPath(new URL(req.url ?? '/', 'http://localhost').pathname);if (!['/v1/models','/v1/chat/completions','/v1/messages'].includes(path)) { res.writeHead(404); res.end('Not found'); return; }try { await handle(req, res, path, req.method ?? 'GET'); }catch { if (!res.headersSent) res.writeHead(500); res.end('Request failed'); }diff --git a/apps/host/src/lib/connect-urls.ts b/apps/host/src/lib/connect-urls.tsnew file mode 100644index 000000000..9e71a1472--- /dev/null+++ b/apps/host/src/lib/connect-urls.ts@@ -0,0 +1,18 @@+/**+ * The address an app is given depends on who builds the final request URL.+ *+ * OpenAI-style clients (Chat Completions, Responses) are given the base that ends in /v1 and add /chat/completions or /responses.+ * Anthropic-style clients (Claude Code, the Anthropic SDKs) add /v1/messages themselves, so they are given the server root. With+ * the /v1 base they post /v1/v1/messages.+ */+export type ConnectProtocol = 'openai' | 'responses' | 'anthropic';++/** The server root: the base address without its trailing /v1. */+export function serverRoot(baseUrl: string): string {+ return String(baseUrl ?? '').trim().replace(/\/+$/, '').replace(/\/v1$/i, '');+}++/** The address to type into an app that speaks this protocol. */+export function appBaseUrl(baseUrl: string, protocol: ConnectProtocol): string {+ return protocol === 'anthropic' ? serverRoot(baseUrl) : String(baseUrl ?? '').trim().replace(/\/+$/, '');+}diff --git a/apps/host/src/locales/de.json b/apps/host/src/locales/de.jsonindex 935736b7f..6f5f19805 100644--- a/apps/host/src/locales/de.json+++ b/apps/host/src/locales/de.json@@ -1,5 +1,6 @@{"s0001": "% PCIe",+ "s7311": "Anthropic-Apps hängen /v1/messages selbst an, deshalb steht am Ende dieser Adresse kein /v1. In Claude Code setzen Sie ANTHROPIC_BASE_URL darauf.","s0002": "(optional)","s0003": ", Kontextcache passt nicht in den Speicher","s0004": "-y @modelcontextprotocol/server-filesystem /your/folder",diff --git a/apps/host/src/locales/en.json b/apps/host/src/locales/en.jsonindex 08edd6ba9..4b752d647 100644--- a/apps/host/src/locales/en.json+++ b/apps/host/src/locales/en.json@@ -1,5 +1,6 @@{"s0001": "% PCIe",+ "s7311": "Anthropic apps add /v1/messages themselves, so this address has no /v1 at the end. In Claude Code, set ANTHROPIC_BASE_URL to it.","s0002": "(optional)","s0003": ", context cache does not fit","s0004": "-y @modelcontextprotocol/server-filesystem /your/folder",diff --git a/apps/host/src/locales/es.json b/apps/host/src/locales/es.jsonindex 4a00e2186..ce7d7f381 100644--- a/apps/host/src/locales/es.json+++ b/apps/host/src/locales/es.json@@ -1,5 +1,6 @@{"s0001": "% PCIe",+ "s7311": "Las apps de Anthropic añaden /v1/messages por sí mismas, así que esta dirección no termina en /v1. En Claude Code, pon esta dirección en ANTHROPIC_BASE_URL.","s0002": "(opcional)","s0003": ", la caché de contexto no cabe","s0004": "-y @modelcontextprotocol/server-filesystem /your/folder",diff --git a/apps/host/src/locales/ja.json b/apps/host/src/locales/ja.jsonindex ec8bdf4c3..68cb27cce 100644--- a/apps/host/src/locales/ja.json+++ b/apps/host/src/locales/ja.json@@ -1,5 +1,6 @@{"s0001": "% PCIe",+ "s7311": "Anthropic 系のアプリは /v1/messages を自分で付け足すため、このアドレスの末尾に /v1 はありません。Claude Code では ANTHROPIC_BASE_URL にこのアドレスを設定します。","s0002": "(任意)","s0003": "、コンテキストキャッシュが収まりません","s0004": "-y @modelcontextprotocol/server-filesystem /your/folder",diff --git a/apps/host/src/locales/ko.json b/apps/host/src/locales/ko.jsonindex 40dc9433a..427895f89 100644--- a/apps/host/src/locales/ko.json+++ b/apps/host/src/locales/ko.json@@ -1,5 +1,6 @@{"s0001": "% PCIe",+ "s7311": "Anthropic 앱은 /v1/messages를 스스로 붙이므로 이 주소 끝에는 /v1이 없습니다. Claude Code에서는 ANTHROPIC_BASE_URL에 이 주소를 넣으세요.","s0002": "(선택 사항)","s0003": ", 컨텍스트 캐시를 저장할 공간이 부족함","s0004": "-y @modelcontextprotocol/server-filesystem /your/folder",diff --git a/apps/host/src/locales/pt.json b/apps/host/src/locales/pt.jsonindex 53292b3d9..05ec2fa59 100644--- a/apps/host/src/locales/pt.json+++ b/apps/host/src/locales/pt.json@@ -1,5 +1,6 @@{"s0001": "% PCIe",+ "s7311": "Os apps da Anthropic acrescentam /v1/messages sozinhos, por isso este endereço não termina em /v1. No Claude Code, defina ANTHROPIC_BASE_URL com ele.","s0002": "(opcional)","s0003": ", o cache de contexto não cabe","s0004": "-y @modelcontextprotocol/server-filesystem /your/folder",diff --git a/apps/host/src/locales/ru.json b/apps/host/src/locales/ru.jsonindex d810a8453..d0f94a958 100644--- a/apps/host/src/locales/ru.json+++ b/apps/host/src/locales/ru.json@@ -1,5 +1,6 @@{"s0001": "% PCIe",+ "s7311": "Приложения Anthropic сами добавляют /v1/messages, поэтому в конце этого адреса нет /v1. В Claude Code укажите его в ANTHROPIC_BASE_URL.","s0002": "(необязательно)","s0003": ", кеш контекста не помещается","s0004": "-y @modelcontextprotocol/server-filesystem /your/folder",diff --git a/apps/host/src/locales/zh.json b/apps/host/src/locales/zh.jsonindex 358312ccb..de4f4c02c 100644--- a/apps/host/src/locales/zh.json+++ b/apps/host/src/locales/zh.json@@ -1,5 +1,6 @@{"s0001": "% PCIe",+ "s7311": "Anthropic 类应用会自己加上 /v1/messages,所以这个地址末尾没有 /v1。在 Claude Code 中,把 ANTHROPIC_BASE_URL 设为这个地址。","s0002": "(可选)","s0003": ",上下文缓存无法容纳","s0004": "-y @modelcontextprotocol/server-filesystem /your/folder",diff --git a/apps/host/src/screens/Serving.tsx b/apps/host/src/screens/Serving.tsxindex 49205a70c..e2642b900 100644--- a/apps/host/src/screens/Serving.tsx+++ b/apps/host/src/screens/Serving.tsx@@ -1,4 +1,5 @@import { tr } from '../lib/i18n';+import { appBaseUrl } from '../lib/connect-urls';import { deviceLabel, instanceLabel } from '../lib/runtime-models';import { nativeRouting, useNative } from '../lib/native';/** Connection facts first. Instructions and request details open only on demand. */@@ -94,7 +95,7 @@ export function ServingScreen() {{tr(requests.length ? <div className="st-tablewrap"><table className="st-table"><thead><tr><th aria-sort={sort === 'at' ? 'descending' : 'none'}><button className="st-btn st-btn--ghost st-btn--sm" onClick={() => setSort('at')}>{tr("Time")}</button></th><th>{tr("Client / endpoint")}</th>{tr(multipleModels ? <th>{tr("Model")}</th> : null)}<th>{tr("Status")}</th><th>{tr("Tokens in / out")}</th><th aria-sort={sort === 'durationMs' ? 'descending' : 'none'}><button className="st-btn st-btn--ghost st-btn--sm" onClick={() => setSort('durationMs')}>{tr("Duration")}</button></th></tr></thead><tbody>{tr(requests.map(r => <tr key={r.id}><td><button className="st-btn st-btn--ghost st-btn--sm" aria-label={tr("View request {0}", [r.id])} onClick={() => setSelectedId(r.id)}>{tr(clockTime(r.at))}</button></td><td>{tr(r.client)}<div className="xs muted">{tr(r.endpoint)}</div></td>{tr(multipleModels ? <td><code className="xs">{r.model}</code></td> : null)}<td>{tr(<span className="request-status" data-state={r.state}>{tr(r.cancelRequested ? r.cancellable ? 'Cancelling…' : 'Cancelled' : r.state.charAt(0).toUpperCase() + r.state.slice(1))}</span>)}</td><td className="num">{tr(tokens(r.tokensIn))} / {tr(tokens(r.tokensOut))}<div className="xs muted">{tr(r.usageSource === 'demo' ? 'Simulated' : r.usageSource === 'reported' ? 'Reported by model' : 'Usage unavailable')}</div></td><td className="num">{tr(r.durationMs ? duration(r.durationMs) : '—')}</td></tr>))}</tbody></table></div> : <Empty icon="link" title={tr(serving.requests.length ? 'No matching requests' : 'No requests yet')} body={serving.requests.length ? 'Try another filter or client name.' : 'Requests appear here when a client or Strata chat uses the API.'} action={tr(serving.requests.length ? <Btn onClick={() => { setQuery(''); setFilter('all'); setModelFilter('all'); }}>{tr("Clear filters")}</Btn> : <Btn onClick={() => setGuide(true)}>{tr("Connect an app")}</Btn>)}/>)}</section>{tr(error ? <ErrorNotice message={error}/> : null)}- </div><Dialog open={guide} title={tr("Connect Strata to an app")} onClose={() => setGuide(false)} headerActions={<div className="protocol-switch"><Seg panelId="api-protocol-panel" value={protocol} ariaLabel={tr("API protocol")} options={[{ value: 'openai', label: 'Chat Completions' }, { value: 'responses', label: 'Responses' }, { value: 'anthropic', label: 'Anthropic' }]} onChange={setProtocol}/></div>} sheet><div className="stack">{tr(multipleModels ? <label className="input-field"><span>{tr("Model for this app")}</span><Select ariaLabel={tr("Connection guide model")} value={loaded?.apiModelId ?? ''} options={modelOptions} disabled={!modelOptions.length} onChange={setGuideModel}/></label> : null)}<ol><li>{tr("In your client, choose a compatible provider.")}</li><li>{tr("Set its base URL to ")}<code>{serving.baseUrl}</code>.</li><li>{tr("Use model ID ")}<code>{loaded?.apiModelId ?? 'from the Models screen after loading'}</code>.</li><li>{tr(serving.access.apiKeySet ? 'Enter your existing API key in the client.' : 'This local API has no key configured.')}</li></ol>{tr(protocol === 'responses' && (!native?.instances.some(i => i.status === 'ready') || native.capabilities.responses === false) ? <p className="small muted">{tr("Responses requires a running native model and a Strata version that supports this API. Update the native installation in Settings \u2192 About if needed.")}</p> : null)}<div id="api-protocol-panel" role="tabpanel" aria-labelledby={`api-protocol-panel-tab-${protocol}`} tabIndex={0}><div className="st-code"><div className="st-code__head"><span>{tr("curl")}</span><IconBtn name="copy" title={tr("Copy request template")} onClick={() => void navigator.clipboard.writeText(code)}/></div><pre>{code}</pre></div><p className="xs muted">{tr("Model list: ")}{tr(serving.baseUrl)}{tr(serving.endpoints.models)}{tr(". Network access depends on the host\u2019s listen address and your firewall.")}</p></div></div></Dialog>+ </div><Dialog open={guide} title={tr("Connect Strata to an app")} onClose={() => setGuide(false)} headerActions={<div className="protocol-switch"><Seg panelId="api-protocol-panel" value={protocol} ariaLabel={tr("API protocol")} options={[{ value: 'openai', label: 'Chat Completions' }, { value: 'responses', label: 'Responses' }, { value: 'anthropic', label: 'Anthropic' }]} onChange={setProtocol}/></div>} sheet><div className="stack">{tr(multipleModels ? <label className="input-field"><span>{tr("Model for this app")}</span><Select ariaLabel={tr("Connection guide model")} value={loaded?.apiModelId ?? ''} options={modelOptions} disabled={!modelOptions.length} onChange={setGuideModel}/></label> : null)}<ol><li>{tr("In your client, choose a compatible provider.")}</li><li>{tr("Set its base URL to ")}<code>{appBaseUrl(serving.baseUrl, protocol)}</code>.</li><li>{tr("Use model ID ")}<code>{loaded?.apiModelId ?? 'from the Models screen after loading'}</code>.</li><li>{tr(serving.access.apiKeySet ? 'Enter your existing API key in the client.' : 'This local API has no key configured.')}</li></ol>{tr(protocol === 'anthropic' ? <p className="small muted">{tr("Anthropic apps add /v1/messages themselves, so this address has no /v1 at the end. In Claude Code, set ANTHROPIC_BASE_URL to it.")}</p> : null)}{tr(protocol === 'responses' && (!native?.instances.some(i => i.status === 'ready') || native.capabilities.responses === false) ? <p className="small muted">{tr("Responses requires a running native model and a Strata version that supports this API. Update the native installation in Settings \u2192 About if needed.")}</p> : null)}<div id="api-protocol-panel" role="tabpanel" aria-labelledby={`api-protocol-panel-tab-${protocol}`} tabIndex={0}><div className="st-code"><div className="st-code__head"><span>{tr("curl")}</span><IconBtn name="copy" title={tr("Copy request template")} onClick={() => void navigator.clipboard.writeText(code)}/></div><pre>{code}</pre></div><p className="xs muted">{tr("Model list: ")}{tr(serving.baseUrl)}{tr(serving.endpoints.models)}{tr(". Network access depends on the host\u2019s listen address and your firewall.")}</p></div></div></Dialog><Dialog open={!!selectedId} title={tr("Request details")} onClose={() => setSelectedId(null)} sheet>{tr(selected ? <><dl className="summary-list"><div><dt>{tr("ID")}</dt><dd className="mono">{tr(selected.id)}</dd></div><div><dt>{tr("State")}</dt><dd>{tr(selected.cancelRequested ? selected.cancellable ? 'Cancelling…' : 'Cancelled' : selected.state)}</dd></div><div><dt>{tr("Model")}</dt><dd>{tr(selected.model)}</dd></div><div><dt>{tr("Tokens in / out")}</dt><dd>{tr(tokens(selected.tokensIn))} / {tr(tokens(selected.tokensOut))}</dd></div><div><dt>{tr("Usage source")}</dt><dd>{tr(selected.usageSource === 'demo' ? 'Simulated' : selected.usageSource === 'reported' ? 'Reported by model' : 'Unavailable')}</dd></div><div><dt>{tr("Recorded duration")}</dt><dd>{tr(selected.durationMs ? duration(selected.durationMs) : 'Not yet available')}</dd></div></dl>{tr(selected.error ? <div ref={cancelResult} tabIndex={-1}><ErrorNotice message={selected.error} next={tr("Check the client connection and try the request again.")}/></div> : null)}<p className="small muted">{tr("Prompt content and historical queue position are not retained. This view updates while the request runs.")}</p>{tr(selected.cancellable !== false && ['queued', 'generating'].includes(selected.state) ? <Btn disabled={pending || selected.cancelRequested} onClick={() => { requestedCancel.current = selected.id; void operation(() => cancelRequest(selected.id)); }}>{tr(selected.cancelRequested ? 'Cancelling…' : 'Cancel request')}</Btn> : null)}{tr(error ? <ErrorNotice message={error}/> : null)}</> : <p>{tr("This request is no longer retained in the session log.")}</p>)}</Dialog><Dialog open={sharingOpen} title={tr("Local sharing")} onClose={() => !pending && setSharingOpen(false)} actions={<><Btn disabled={pending} onClick={() => setSharingOpen(false)}>{tr("Close")}</Btn><Btn variant="primary" disabled={pending || !serving.access.apiKeySet} onClick={() => void operation(async () => { await setLocalSharing(!serving.sharing?.active); setSharingOpen(false); })}>{tr(pending ? 'Updating…' : serving.sharing?.active ? 'Stop sharing' : 'Start sharing')}</Btn></>}><div className="form-stack"><span className="dialog-feature-icon"><Icon name="link" size={28}/></span><p>{tr(serving.sharing?.active ? 'Your inference API is available to devices on this network.' : 'Let another device on your network use your loaded models.')}</p><p className="small muted">{tr("Only the inference API is shared. Clients need your API key. Use a trusted local network; this connection uses HTTP.")}</p>{tr(serving.sharing?.active ? <div className="row"><code className="key-url">{serving.sharing.url}</code><IconBtn name="copy" title={tr("Copy local network URL")} onClick={() => void navigator.clipboard.writeText(serving.sharing!.url!)}/></div> : null)}{tr(!serving.access.apiKeySet ? <Btn onClick={() => { generateKey(); setSharingOpen(false); setKeyOpen(true); }}>{tr("Create an API key first")}</Btn> : <p className="small muted"><Icon name="shield" size={16}/>{tr(" API key required")}</p>)}{tr(error ? <ErrorNotice message={error}/> : null)}</div></Dialog><Dialog open={keyOpen} className="serving-key-dialog" title={tr(serving.access.apiKeySet ? 'Change API key' : 'Create an API key')} onClose={() => {diff --git a/apps/host/tests/connect-urls.test.mjs b/apps/host/tests/connect-urls.test.mjsnew file mode 100644index 000000000..00960fdd0--- /dev/null+++ b/apps/host/tests/connect-urls.test.mjs@@ -0,0 +1,51 @@+// Connect an app: Anthropic-style apps get the server root (they add /v1/messages themselves), OpenAI-style apps keep /v1,+// and the host accepts /v1/v1/messages as the same request for people who pasted the old address.+import test from 'node:test';+import assert from 'node:assert/strict';+import { readFile } from 'node:fs/promises';+import http from 'node:http';+import { build } from 'esbuild';+import { apiPath } from '../server/api-paths.mjs';+import { createLocalShare } from '../server/local-share.mjs';++const bundled = await build({ entryPoints: ['src/lib/connect-urls.ts'], bundle: true, write: false, format: 'esm', platform: 'node' });+const { serverRoot, appBaseUrl } = await import(`data:text/javascript;base64,${Buffer.from(bundled.outputFiles[0].text).toString('base64')}`);++test('the Anthropic tab shows the address without /v1, the other tabs keep it', () => {+ const base = 'http://127.0.0.1:8792/v1';+ assert.equal(appBaseUrl(base, 'anthropic'), 'http://127.0.0.1:8792');+ assert.equal(appBaseUrl(base, 'openai'), base);+ assert.equal(appBaseUrl(base, 'responses'), base);+ assert.equal(serverRoot('https://abc.trycloudflare.com/v1/'), 'https://abc.trycloudflare.com');+ assert.equal(serverRoot('http://192.168.1.5:8792'), 'http://192.168.1.5:8792');+ assert.equal(serverRoot('http://host/api/v1'), 'http://host/api');+});++test('the Connect dialog gives the Anthropic tab the root address and says why in plain words', async () => {+ const screen = await readFile('src/screens/Serving.tsx', 'utf8');+ assert.match(screen, /appBaseUrl\(serving\.baseUrl, protocol\)/);+ assert.match(screen, /ANTHROPIC_BASE_URL/);+});++test('/v1/v1/messages is read as /v1/messages, and nothing else is aliased', () => {+ assert.equal(apiPath('/v1/v1/messages'), '/v1/messages');+ assert.equal(apiPath('/v1/v1/messages/'), '/v1/messages');+ assert.equal(apiPath('/v1/v1/messages/count_tokens'), '/v1/messages/count_tokens');+ assert.equal(apiPath('/v1/messages'), '/v1/messages');+ for (const other of ['/v1/v1/chat/completions', '/v1/v1/models', '/v1/v1/responses', '/v1/v1/v1/messages']) assert.equal(apiPath(other), other);+});++test('the shared (key-protected) listener serves /v1/v1/messages as /v1/messages', async () => {+ const seen = [];+ const share = createLocalShare({ handle: async (req, res, path) => { seen.push(path); res.writeHead(200); res.end('ok'); }, hasKey: () => true, host: '127.0.0.1', port: 0 });+ const { url } = await share.start();+ try {+ const root = url.replace(/\/v1$/, '');+ for (const path of ['/v1/messages', '/v1/v1/messages']) {+ const res = await fetch(root + path, { method: 'POST', body: '{}' });+ assert.equal(res.status, 200, path);+ }+ assert.deepEqual(seen, ['/v1/messages', '/v1/messages']);+ assert.equal((await fetch(root + '/v1/v1/chat/completions', { method: 'POST', body: '{}' })).status, 404);+ } finally { await share.stop(); }+});diff --git a/serve/server.py b/serve/server.pyindex 1b82c1023..ca222a04d 100644--- a/serve/server.py+++ b/serve/server.py@@ -240,6 +240,9 @@ def focused_recovery_prompt(tok, ids, generated):LOOPBACK_NAMES = ("localhost", "127.0.0.1", "::1")QUEUE_BEAT_S = 10.0 # a request waiting for the single turn sends a keep-alive this often (#1619)KEEPALIVE_S = 10.0 # a held-back tool argument leaves the stream quiet: ping at least this often+# Anthropic clients add /v1/messages to their base URL themselves. A base URL that already ends in /v1 (copied from an OpenAI-style+# setup) makes them post /v1/v1/messages; these two paths mean the same as the real ones. Nothing else is aliased.+V1_V1_ALIASES = {"/v1/v1/messages": "/v1/messages", "/v1/v1/messages/count_tokens": "/v1/messages/count_tokens"}CTX_SLACK = 8 # `strata --serve` rejects prompt + max_new + 8 > context: keep the same margin here# The live tok/s is a rate over a window, not a mean since the first token: a mean reads ~1/elapsed at the first# token (the Monitor showed five-digit numbers) and then undershoots for the first second of every answer.@@ -6116,6 +6119,7 @@ def make_handler(svc: Service):if not self._authorized():returnpath = self.path.split("?")[0].rstrip("/") # issue #55: Claude Code posts /v1/messages?beta=true+ path = V1_V1_ALIASES.get(path, path) # an Anthropic client given ".../v1" as its base posts /v1/v1/messagesif path.startswith("/v1/") and self._foreign_page():returnif path == "/settings":diff --git a/serve/test_anthropic_base_url.py b/serve/test_anthropic_base_url.pynew file mode 100644index 000000000..7170747e1--- /dev/null+++ b/serve/test_anthropic_base_url.py@@ -0,0 +1,94 @@+"""serve/test_anthropic_base_url.py - an Anthropic client reaches the server by the root address, and by the "/v1" one too.++The Anthropic SDKs (and Claude Code) add /v1/messages to the base URL themselves. The app used to show the OpenAI-style base+(.../v1) in its Anthropic tab, so those clients posted /v1/v1/messages and got a 404. The address is now shown without /v1, and+the server accepts /v1/v1/messages (and /v1/v1/messages/count_tokens) as the same request, for people who already pasted the old one.++Uses the real `anthropic` Python client against the mock-engine server (no network, no key). Skipped when the package is missing.++ python -m unittest serve.test_anthropic_base_url -v+"""+from __future__ import annotations++import http.client+import json+import sys+import unittest+from pathlib import Path++sys.path.insert(0, str(Path(__file__).resolve().parents[1]))+from serve.frontend import ChatTemplate # noqa: E402+from serve.server import ByteTokenizer, MockEngine, Service, serve # noqa: E402++ROOT = Path(__file__).resolve().parents[1]++try:+ import anthropic+except ImportError: # pragma: no cover+ anthropic = None+++REPLY = "<think>\nhm\n</think>\n\nhello from the mock"+++class AnthropicBase(unittest.TestCase):+ @classmethod+ def setUpClass(cls):+ tok = ByteTokenizer()+ cls.engine = MockEngine(tok, REPLY, max_context=4096)+ cls.svc = Service(cls.engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja"))+ cls.httpd = serve(cls.svc, port=0)+ cls.port = cls.httpd.server_address[1]+ cls.root = f"http://127.0.0.1:{cls.port}"++ @classmethod+ def tearDownClass(cls):+ cls.httpd.shutdown()+ cls.httpd.server_close()++ def raw(self, path, body):+ c = http.client.HTTPConnection("127.0.0.1", self.port, timeout=20)+ try:+ c.request("POST", path, body=json.dumps(body).encode(), headers={"Content-Type": "application/json"})+ r = c.getresponse()+ return r.status, json.loads(r.read() or b"{}")+ finally:+ c.close()++ @unittest.skipIf(anthropic is None, "the anthropic package is not installed")+ def test_sdk_with_the_root_address_works(self):+ client = anthropic.Anthropic(base_url=self.root, api_key="not-needed", max_retries=0)+ msg = client.messages.create(model="strata", max_tokens=32, messages=[{"role": "user", "content": "hi"}])+ self.assertEqual(msg.type, "message")+ self.assertIn("hello", "".join(b.text for b in msg.content if b.type == "text"))++ @unittest.skipIf(anthropic is None, "the anthropic package is not installed")+ def test_sdk_with_the_old_v1_address_works_through_the_alias(self):+ client = anthropic.Anthropic(base_url=self.root + "/v1", api_key="not-needed", max_retries=0)+ msg = client.messages.create(model="strata", max_tokens=32, messages=[{"role": "user", "content": "hi"}])+ self.assertEqual(msg.type, "message")+ self.assertIn("hello", "".join(b.text for b in msg.content if b.type == "text"))++ @unittest.skipIf(anthropic is None, "the anthropic package is not installed")+ def test_sdk_streaming_through_the_alias(self):+ client = anthropic.Anthropic(base_url=self.root + "/v1", api_key="not-needed", max_retries=0)+ with client.messages.stream(model="strata", max_tokens=32, messages=[{"role": "user", "content": "hi"}]) as stream:+ text = "".join(stream.text_stream)+ self.assertIn("hello", text)++ def test_count_tokens_alias(self):+ body = {"model": "strata", "messages": [{"role": "user", "content": "hi"}]}+ code, real = self.raw("/v1/messages/count_tokens", body)+ code2, aliased = self.raw("/v1/v1/messages/count_tokens", body)+ self.assertEqual((code, code2), (200, 200))+ self.assertEqual(real, aliased)++ def test_only_the_two_messages_paths_are_aliased(self):+ body = {"model": "strata", "messages": [{"role": "user", "content": "hi"}], "max_tokens": 8}+ for path in ("/v1/v1/chat/completions", "/v1/v1/responses", "/v1/v1/v1/messages", "/v1/v2/messages"):+ code, _ = self.raw(path, body)+ self.assertEqual(code, 404, path)+++if __name__ == "__main__":+ unittest.main()diff --git a/strata_cli/launch/cmd.py b/strata_cli/launch/cmd.pyindex 0412429b2..fb810705f 100644--- a/strata_cli/launch/cmd.py+++ b/strata_cli/launch/cmd.py@@ -494,6 +494,8 @@ def run_launch(a: argparse.Namespace) -> int:label = ("This computer" if "127.0.0.1" in u else "Cloudflare" if "trycloudflare" in u else"Tailscale" if ("ts.net" in u or u.split("//")[1].startswith("100.")) else "Network")_say(f" {label:<14} {u}/v1")+ if result.urls:+ _say(f" {'Anthropic apps':<14} {result.urls[0]} (Claude Code: ANTHROPIC_BASE_URL, no /v1 at the end)")if result.key:from strata_cli.launch import access_say(f" {'API key':<14} {access.mask_key(result.key)} (show it: strata launch --show-key --preset \"{s.name}\")")diff --git a/strata_cli/launch/harness.py b/strata_cli/launch/harness.pyindex 29ebdc315..5f9379c10 100644--- a/strata_cli/launch/harness.py+++ b/strata_cli/launch/harness.py@@ -164,7 +164,8 @@ def _limits_codex(t: Target) -> list[str]:def _limits_claude(t: Target) -> list[str]:- out = ["Set CLAUDE_CODE_MAX_CONTEXT_TOKENS to the served context (%d here) and CLAUDE_CODE_MAX_OUTPUT_TOKENS=4096: without them Claude Code "+ out = ["ANTHROPIC_BASE_URL is the server address %s without /v1: Claude Code adds /v1/messages itself (with /v1 in it the requests go to /v1/v1/messages)." % t.url,+ "Set CLAUDE_CODE_MAX_CONTEXT_TOKENS to the served context (%d here) and CLAUDE_CODE_MAX_OUTPUT_TOKENS=4096: without them Claude Code ""assumes a 100K window and never compacts, or compacts on every turn." % t.context,"No Anthropic login or credential is used; the token is only checked by your own server.","The notice [claude-code:unrecognized_model] on stderr is harmless."]@@ -249,8 +250,14 @@ def closest_name(name: str) -> str | None:# ------------------------------------------------------------------------------------------------ rendering+def server_root(url: str) -> str:+ """The server root: the address without a trailing /v1 (people paste the OpenAI-style base). Anthropic apps add /v1/messages themselves."""+ root = url.strip().rstrip("/")+ return root[:-3].rstrip("/") if root.lower().endswith("/v1") else root++def target(url: str, key: str | None, model: str, context: int | None = None) -> Target:- return Target(url=url.rstrip("/"), key=key or PLACEHOLDER_KEY, model=model or "strata", context=int(context or 32768))+ return Target(url=server_root(url), key=key or PLACEHOLDER_KEY, model=model or "strata", context=int(context or 32768))def render(h: Harness, t: Target) -> dict:diff --git a/strata_cli/launch/monitor_ext.py b/strata_cli/launch/monitor_ext.pyindex c13224243..2239c0ace 100644--- a/strata_cli/launch/monitor_ext.py+++ b/strata_cli/launch/monitor_ext.py@@ -223,6 +223,7 @@ class LaunchMonitor(MonitorApp):t.append(f" URL {self.info['url']}/v1 model {self.info.get('model') or 'strata'} key {shown} ")t.append("(k hide)" if self.show_key else "(k show)", style=MUTE)t.append("\n")+ t.append(f" Claude Code and other Anthropic apps use {self.info['url']} (no /v1)\n", style=MUTE)for u in self.info.get("urls", [])[1:]:t.append(f" also reachable at {u}/v1\n", style=MUTE)t.append("\n " + "App".ljust(20) + "Works with".ljust(22) + "Status\n", style="bold")diff --git a/tests/terminal/test_launch.py b/tests/terminal/test_launch.pyindex 66d3d7277..819f8548c 100644--- a/tests/terminal/test_launch.py+++ b/tests/terminal/test_launch.py@@ -804,6 +804,20 @@ class ConnectCommand(unittest.TestCase):self.assertIn("ANTHROPIC_AUTH_TOKEN", out)self.assertNotIn("/v1", out.split("ANTHROPIC_BASE_URL")[1].splitlines()[0]) # the Anthropic client adds /v1 itself+ def test_a_pasted_v1_address_is_cut_back_to_the_server_root(self):+ """The Anthropic client adds /v1/messages itself, so ANTHROPIC_BASE_URL must never end in /v1 (that gave /v1/v1/messages, a 404);+ the OpenAI-style apps get exactly one /v1."""+ code, out, _ = run_cli(["connect", "claude-code", "--url", "http://127.0.0.1:8080/v1/", "--key", "sk-x1234567890abc", "--show-key", "--output", "json"])+ data = json.loads(out)+ self.assertEqual(data["env"]["ANTHROPIC_BASE_URL"], "http://127.0.0.1:8080")+ self.assertIn('"ANTHROPIC_BASE_URL": "http://127.0.0.1:8080"', data["files"][0]["text"])+ code, out, _ = run_cli(["connect", "opencode", "--url", "http://127.0.0.1:8080/v1", "--output", "json"])+ text = json.loads(out)["files"][0]["text"]+ self.assertIn("http://127.0.0.1:8080/v1", text)+ self.assertNotIn("/v1/v1", text)+ code, out, _ = run_cli(["connect", "claude-code", "--url", "http://127.0.0.1:8080"])+ self.assertIn("without /v1", out)+def test_write_keeps_a_backup_and_merges(self):target = Path(os.environ["HOME"]) / ".config" / "opencode" / "opencode.json"target.parent.mkdir(parents=True)