const api = window.opalineOnboarding;
const card = document.querySelector(".card");
const body = document.querySelector(".card-body");
const dots = document.querySelector(".dots");
const button = document.querySelector(".primary");
const close = document.querySelector(".close");
const STEPS = ["story", "reading", "numbers", "stuck", "ready"];
const state = { step: -1, icons: {}, scan: null, result: null, analysis: null };
const escape = (value) =>
String(value).replace(/[&<>"']/g, (char) => `${char.charCodeAt(0)};`);
const number = (value) => Math.round(value).toLocaleString("en-US");
function icon(agent, className = "app-icon") {
const source = state.icons[agent];
return source ? `` : "";
}
const COUNT_TIMING = { duration: 1100, easing: "cubic-bezier(0.25, 1, 0.5, 1)" };
const WHOLE = { maximumFractionDigits: 0 };
function countUp(element, target) {
let flow = element.querySelector("number-flow");
const fresh = !flow;
if (fresh) {
flow = Object.assign(document.createElement("number-flow"), { locales: "en-US", format: WHOLE });
element.replaceChildren(flow);
}
const from = Number(element.dataset.value ?? 0);
element.dataset.value = String(target);
if (fresh) {
const digits = new Intl.NumberFormat("en-US", WHOLE).formatToParts(target).filter((part) => part.type === "integer").reduce((sum, part) => sum + part.value.length, 0);
flow.format = { ...WHOLE, minimumIntegerDigits: digits };
flow.update(from);
}
requestAnimationFrame(() => {
flow.spinTiming = COUNT_TIMING;
flow.update(target);
setTimeout(() => {
flow.spinTiming = undefined;
flow.format = WHOLE;
}, COUNT_TIMING.duration + 100);
});
}
function totals() {
const claude = state.result?.claude && !state.result.claude.error ? state.result.claude : null;
const codex = state.result?.codex && !state.result.codex.error ? state.result.codex : null;
const gemini = state.result?.gemini && !state.result.gemini.error ? state.result.gemini : null;
const parts = [claude, codex, gemini].filter(Boolean);
const sum = (key) => parts.reduce((total, part) => total + Number(part[key] ?? 0), 0);
const firstSeen = parts
.map((part) => part.first_seen)
.filter(Boolean)
.sort()[0];
const stuck = [
...(claude?.stuck_on ?? []).map(([label, count]) => ({ agent: "claude", label, count })),
...(codex?.stuck_on ?? []).map(([label, count]) => ({ agent: "codex", label, count })),
...(gemini?.stuck_on ?? []).map(([label, count]) => ({ agent: "gemini", label, count })),
]
.filter((item) => item.label)
.sort((a, b) => b.count - a.count)
.slice(0, 4);
return {
ok: parts.length > 0,
sessions: sum("sessions"),
prompts: sum("prompts"),
failed: sum("failed_calls"),
hours: sum("agent_hours"),
firstSeen: firstSeen ? new Date(`${firstSeen.replace(" ", "T")}Z`) : null,
stuck,
error: state.result?.error ?? claude?.error ?? codex?.error ?? gemini?.error,
};
}
function readableLabel(label) {
if (label.startsWith("mcp__")) return label.slice(5).split("__").join(" · ");
return label;
}
const views = {
story: () => `
It reads the sessions already on this Mac and finds the loops, failures and lost time behind them.
Nothing leaves this Mac until you choose to share it.
`, reading: () => `Straight from the files on this Mac, with ClickHouse built in.
${escape(result.error ?? "No Claude Code, Codex or Gemini sessions were found on this Mac.")}
`; const since = result.firstSeen ? result.firstSeen.toLocaleDateString("en-US", { month: "short", day: "numeric" }) : "last month"; return `That's about ${number(result.hours / 8)} workdays of agent time, running side by side on this Mac.
Read in ${(state.result.ms / 1000).toFixed(1)} s on this Mac · nothing was uploaded
`; }, stuck: () => { const result = totals(); const items = result.stuck .map( (item, index) => `${escape(readableLabel(item.label))}
${number(item.count)}×
Each of these failed again and again. Most come from a missing instruction, script or permission.
No repeated failures yetOpaline finds the why behind each one, and what to change.
`; }, ready: () => `Next, choose which repositories Opaline keeps learning from.
`, }; function renderDots() { dots.hidden = state.step < 0; dots.innerHTML = STEPS.map((_, index) => ``).join(""); } function render() { const name = STEPS[state.step]; card.hidden = false; body.innerHTML = views[name](); body.style.animation = "none"; void body.offsetWidth; body.style.animation = ""; for (const element of body.querySelectorAll("[data-count]")) countUp(element, Number(element.dataset.count || 0)); if (name === "reading" && state.scan) countUp(body.querySelector("[data-count]"), state.scan.claude + state.scan.codex + (state.scan.gemini ?? 0)); button.classList.toggle("glow", name === "ready"); button.disabled = name === "reading"; button.textContent = name === "reading" ? "Reading…" : name === "ready" ? "Open Opaline" : "Continue"; renderDots(); if (name === "reading") waitForAnalysis(); } async function waitForAnalysis() { const shownAt = performance.now(); await state.analysis; const wait = Math.max(0, 1400 - (performance.now() - shownAt)); setTimeout(() => { if (STEPS[state.step] === "reading") go(state.step + 1); }, wait); } function go(step) { state.step = step; render(); } function start() { state.analysis = (async () => { state.scan = await api.scan(); const counter = body.querySelector(".reading [data-count]"); if (counter) countUp(counter, state.scan.claude + state.scan.codex + (state.scan.gemini ?? 0)); state.result = await api.analyze(); })(); go(0); } button.addEventListener("click", () => { if (state.step < 0) return start(); if (STEPS[state.step] === "ready") return api.finish(); go(state.step + 1); }); close.addEventListener("click", () => api.finish()); document.addEventListener("keydown", (event) => { if (event.key === "Escape") api.finish(); if (event.key === "Enter" && !button.disabled) button.click(); }); api.icons().then((icons) => { state.icons = icons; });