Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree (570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base = the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main). Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details, conflict table and left-out files are in docs/upstream.md. Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61), docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out. scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run"). Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR macros on. No GPU run, no quality gate. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
125 lines
4.3 KiB
TypeScript
125 lines
4.3 KiB
TypeScript
import { userError } from './format';
|
|
/** Thin HTTP client for the Strata host. Zero deps, works in browser and Electron renderer. */
|
|
|
|
const BASE = (globalThis as { __STRATA_HOST__?: string }).__STRATA_HOST__ ?? '';
|
|
|
|
export class ApiError extends Error {
|
|
constructor(
|
|
message: string,
|
|
readonly status: number,
|
|
readonly body?: unknown,
|
|
) {
|
|
super(userError(message));
|
|
this.name = 'ApiError';
|
|
}
|
|
}
|
|
|
|
async function request<T>(method: string, path: string, body?: unknown): Promise<T> {
|
|
const res = await fetch(`${BASE}${path}`, {
|
|
method,
|
|
headers: body === undefined ? undefined : { 'content-type': 'application/json' },
|
|
body: body === undefined ? undefined : JSON.stringify(body),
|
|
});
|
|
const text = await res.text();
|
|
const data = text ? (JSON.parse(text) as unknown) : undefined;
|
|
if (!res.ok) {
|
|
const message =
|
|
(data as { error?: string } | undefined)?.error ?? `Request failed (${res.status} ${res.statusText})`;
|
|
throw new ApiError(message, res.status, data);
|
|
}
|
|
return data as T;
|
|
}
|
|
|
|
export const api = {
|
|
get: <T>(path: string) => request<T>('GET', path),
|
|
post: <T>(path: string, body?: unknown) => request<T>('POST', path, body),
|
|
patch: <T>(path: string, body?: unknown) => request<T>('PATCH', path, body),
|
|
put: <T>(path: string, body?: unknown) => request<T>('PUT', path, body),
|
|
del: <T>(path: string) => request<T>('DELETE', path),
|
|
};
|
|
|
|
export interface StreamEvent {
|
|
event: string;
|
|
data: Record<string, unknown>;
|
|
}
|
|
|
|
/** Subscribe to the host event stream. Returns an unsubscribe function. */
|
|
export function onEvent(handler: (e: StreamEvent) => void, onStatus?: (s: 'live' | 'offline') => void) {
|
|
let source: EventSource;
|
|
let seenAt = Date.now();
|
|
const names = ['engine', 'monitor', 'models', 'native', 'image-models', 'runtime', 'providers', 'serving', 'download', 'log', 'setup', 'skills', 'task', 'goal', 'timeskip', 'plan', 'queue'];
|
|
const connect = () => {
|
|
source?.close();
|
|
seenAt = Date.now();
|
|
source = new EventSource(`${BASE}/api/events`);
|
|
for (const name of names) {
|
|
source.addEventListener(name, ((ev: MessageEvent) => {
|
|
seenAt = Date.now();
|
|
try { handler({ event: name, data: JSON.parse(ev.data) as Record<string, unknown> }); }
|
|
catch { /* Ignore malformed frames without losing valid state. */ }
|
|
}) as EventListener);
|
|
}
|
|
source.addEventListener('open', () => { seenAt = Date.now(); onStatus?.('live'); });
|
|
source.addEventListener('error', () => onStatus?.('offline'));
|
|
source.addEventListener('message', ev => {
|
|
seenAt = Date.now();
|
|
try { handler({ event: 'message', data: JSON.parse((ev as MessageEvent).data) as Record<string, unknown> }); }
|
|
catch { /* Ignore malformed frames. */ }
|
|
});
|
|
};
|
|
connect();
|
|
// The host sends monitor frames each second. Some proxies leave a dead SSE
|
|
// socket open, so silence must also invalidate readings and retry the stream.
|
|
const watchdog = setInterval(() => {
|
|
if (Date.now() - seenAt <= 10000) return;
|
|
onStatus?.('offline');
|
|
connect();
|
|
}, 5000);
|
|
return () => { clearInterval(watchdog); source.close(); };
|
|
}
|
|
|
|
/** POST a JSON body and consume an NDJSON/SSE-ish stream of JSON frames. */
|
|
export async function streamPost(
|
|
path: string,
|
|
body: unknown,
|
|
onFrame: (frame: Record<string, unknown>) => void,
|
|
signal?: AbortSignal,
|
|
): Promise<void> {
|
|
const res = await fetch(`${BASE}${path}`, {
|
|
method: 'POST',
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
signal,
|
|
});
|
|
if (!res.ok || !res.body) {
|
|
const text = await res.text().catch(() => '');
|
|
throw new ApiError(text || `Stream failed (${res.status})`, res.status);
|
|
}
|
|
const reader = res.body.getReader();
|
|
const decoder = new TextDecoder();
|
|
let buffer = '';
|
|
for (;;) {
|
|
const { done, value } = await reader.read();
|
|
if (done) break;
|
|
buffer += decoder.decode(value, { stream: true });
|
|
let idx: number;
|
|
while ((idx = buffer.indexOf('\n')) >= 0) {
|
|
const line = buffer.slice(0, idx).trim();
|
|
buffer = buffer.slice(idx + 1);
|
|
if (!line) continue;
|
|
try {
|
|
onFrame(JSON.parse(line) as Record<string, unknown>);
|
|
} catch {
|
|
/* ignore partial frame */
|
|
}
|
|
}
|
|
}
|
|
if (buffer.trim()) {
|
|
try {
|
|
onFrame(JSON.parse(buffer.trim()) as Record<string, unknown>);
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
}
|