Files
strata/apps/host/server/serving.mjs
T
Niko1221 fccfd343d7 One definition of tok/s: produced tokens over decode time, from the engine's own figures
The agent footer reads the local engine's /metrics for the turn (no empty number on short replies, prompt reading speed kept apart), model APIs are timed from the first token, the monitor average and the request list use answer speeds, the footer shows the last reply's own speed, and the server's done line no longer prints a million tok/s for a one-token reply.
2026-10-20 14:03:26 +02:00

170 lines
5.7 KiB
JavaScript

/** Serving surface: API keys, access policy, and the request log. */
import { randomBytes } from 'node:crypto';
import os from 'node:os';
import { get as getStore, update } from './store.mjs';
import { nowIso, round, uid } from './util.mjs';
const requests = [];
const MAX_REQUESTS = 60;
const cancellations = new Map();
let revision = Date.now();
let hubRef = null;
let tunnel = { status: 'idle', mode: null, url: null, error: null, installed: false, originUrl: null };
export function setTunnel(value) { tunnel = value; emit(); }
const tunnelActive = () => ['starting', 'connected', 'stopping'].includes(tunnel.status);
let sharing = { active: false, url: null };
export function setSharing(value) { sharing = value; emit(); }
let port = 8080;
let running = false;
let bindHost = '127.0.0.1';
const isLocal = () => ['127.0.0.1', 'localhost', '::1'].includes(bindHost);
export function init(hub, opts = {}) {
hubRef = hub;
port = opts.port ?? port;
running = true;
bindHost = opts.host ?? '127.0.0.1';
}
export function baseUrl() {
const host = isLocal() ? '127.0.0.1' : localIp();
return `http://${host}:${port}/v1`;
}
export function state() {
const s = getStore();
const active = requests.filter((r) => r.state === 'generating' || r.state === 'queued');
return {
revision,
running,
baseUrl: sharing.active ? sharing.url : baseUrl(),
sharing,
tunnel,
port,
endpoints: { openai: '/chat/completions', anthropic: '/messages', models: '/models' },
access: {
otherDevices: !isLocal() || sharing.active || tunnelActive(),
apiKeySet: !!s.serving?.apiKey,
apiKeyHint: s.serving?.apiKey ? `Ends in ${s.serving.apiKey.slice(-4)}` : null,
allowOrigin: '*',
},
stats: {
running: active.filter((r) => r.state === 'generating').length,
waiting: active.filter((r) => r.state === 'queued').length,
completed: requests.filter((r) => r.state === 'done').length,
failed: requests.filter((r) => r.state === 'error').length,
tokensOut: requests.reduce((sum, r) => sum + (r.tokensOut || 0), 0),
},
requests: [...requests].reverse(),
pauseServing: !!s.serving?.pauseServing,
};
}
function emit() {
revision = Math.max(Date.now(), revision + 1);
hubRef?.send('serving', state());
}
export function patch(body) {
const s = getStore();
const serving = { ...(s.serving ?? {}) };
if (typeof body.otherDevices === 'boolean' && body.otherDevices !== !isLocal()) throw new Error('Change the host listen address and restart to update network access.');
if (body.apiKey === null && (!isLocal() || sharing.active || tunnelActive())) throw new Error('Stop local sharing and Cloudflare before removing the API key.');
if (body.apiKey === null) serving.apiKey = null;
else if (typeof body.apiKey === 'string' && body.apiKey.trim()) serving.apiKey = body.apiKey.trim();
else if (body.generateKey) serving.apiKey = `sk-strata-${randomBytes(12).toString('hex')}`;
if (typeof body.pauseServing === 'boolean') serving.pauseServing = body.pauseServing;
update({ serving });
emit();
return state();
}
export function authorized(req) {
const key = getStore().serving?.apiKey;
if (!key) return true;
const header = req.headers.authorization ?? '';
const bearer = header.startsWith('Bearer ') ? header.slice(7) : '';
const alt = req.headers['x-api-key'];
return bearer === key || alt === key;
}
export function open({ client, endpoint, tokensIn = null, model, instanceId, gpuIds, cancel }) {
const entry = {
id: uid('req'),
at: nowIso(),
client,
endpoint,
state: 'queued',
tokensIn,
tokensOut: null,
durationMs: 0,
model: model ?? 'local',
instanceId, gpuIds,
cancellable: typeof cancel === 'function',
cancelRequested: false,
usageSource: 'unavailable',
};
if (entry.cancellable) cancellations.set(entry.id, cancel);
requests.push(entry);
if (requests.length > MAX_REQUESTS) {
const oldestCompleted = requests.findIndex(r => !['generating', 'queued'].includes(r.state));
if (oldestCompleted >= 0) requests.splice(oldestCompleted, 1);
}
emit();
return entry;
}
export function mark(id, patchObj) {
const entry = requests.find((r) => r.id === id);
if (!entry) return;
Object.assign(entry, patchObj);
emit();
return entry;
}
export function finish(id, { tokensOut, durationMs, error }) {
const entry = requests.find((r) => r.id === id);
if (!entry) return;
entry.state = error ? 'error' : 'done';
entry.tokensOut = tokensOut ?? entry.tokensOut;
entry.durationMs = durationMs ?? entry.durationMs;
if (error) entry.error = error;
entry.cancellable = false;
cancellations.delete(id);
emit();
}
export function cancel(id) {
const entry = requests.find((r) => r.id === id);
if (!entry) throw new Error('This request is no longer retained.');
if (!['generating', 'queued'].includes(entry.state)) return state();
const abort = cancellations.get(id);
if (!abort) throw new Error('This request cannot be cancelled by this host.');
if (!entry.cancelRequested) { entry.cancelRequested = true; abort(); }
emit();
return state();
}
export function rateSample() {
const done = requests.filter((r) => r.state === 'done' && r.tokensOut > 0);
if (!done.length) return 0;
const tps = done.filter((r) => r.tps > 0).map((r) => r.tps); // answer speeds (decode time), not tokens over the whole request
if (!tps.length) return 0;
return round(tps.reduce((a, b) => a + b, 0) / tps.length, 1);
}
function localIp() {
try {
const nets = os.networkInterfaces();
for (const list of Object.values(nets)) {
for (const net of list ?? []) {
if (net.family === 'IPv4' && !net.internal) return net.address;
}
}
} catch {
/* fall through */
}
return '127.0.0.1';
}