puck / frontend /src /lib /traces.ts
vu1n's picture
Puck — desktop fairy familiar (HF Build Small)
3c124f3
Raw
History Blame Contribute Delete
1.05 kB
// Ship trace records to the daemon at Night Bloom. Daemon down is normal
// (Space/demo mode) — buffer in localStorage and retry at the next sleep,
// so no day's labels are lost to a napping daemon.
import type { TraceRecord } from "../engine";
const BUFFER_KEY = "puck:trace-buffer";
export async function exportTraces(records: TraceRecord[]): Promise<void> {
let buffered: TraceRecord[] = [];
try {
buffered = JSON.parse(localStorage.getItem(BUFFER_KEY) ?? "[]");
} catch (e) {
console.error("puck: corrupt trace buffer, dropping it", e);
}
const all = [...buffered, ...records];
if (!all.length) return;
try {
const res = await fetch("/api/traces", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ records: all }),
signal: AbortSignal.timeout(5000),
});
if (!res.ok) throw new Error(`daemon rejected traces: ${res.status}`);
localStorage.removeItem(BUFFER_KEY);
} catch {
localStorage.setItem(BUFFER_KEY, JSON.stringify(all));
}
}