// Mirror the overlay's console to the daemon (/api/debug/log → /tmp/puck-frontend.log) // because the WKWebView inspector is unusable behind a fullscreen always-on-top // transparent window. Tauri-only so the browser sim isn't noisy. import { inTauri } from "./tauri"; const stringify = (a: unknown): string => { if (typeof a === "string") return a; try { return JSON.stringify(a); } catch { return String(a); } }; export function installDebugLog(): void { if (!inTauri()) return; const send = (level: string, args: unknown[]) => { void fetch("/api/debug/log", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ level, msg: args.map(stringify).join(" ") }), }).catch(() => {}); }; for (const level of ["log", "warn", "error"] as const) { const orig = console[level].bind(console); console[level] = (...args: unknown[]) => { orig(...args); send(level, args); }; } window.addEventListener("error", (e) => send("error", [`onerror: ${e.message} @ ${e.filename}:${e.lineno}`]), ); window.addEventListener("unhandledrejection", (e) => send("error", [`unhandledrejection: ${String(e.reason)}`]), ); console.log("puck: debug-log forwarding on"); }