const num = (v) => (v == null ? null : Number(v)); const pct = (v) => (num(v) == null ? "—" : `${Math.round(num(v))}%`); const money = (v, cur) => (num(v) == null ? "—" : `${num(v).toFixed(2)}${cur ? " " + cur : ""}`); const clock = (sec) => (sec ? new Date(sec * 1000).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }) : ""); function line(label, value, percent) { const row = document.createElement("div"); row.className = "row"; const name = document.createElement("span"); name.textContent = label; const val = document.createElement("span"); val.className = "num"; val.textContent = value; row.append(name, val); const wrap = document.createElement("div"); wrap.append(row); if (percent != null) { const meter = document.createElement("div"); meter.className = "meter"; const fill = document.createElement("i"); fill.style.width = `${Math.max(0, Math.min(100, Number(percent)))}%`; meter.append(fill); wrap.append(meter); } return wrap; } export default { title: "LLM usage", interval: 5, mount(el) { const box = document.createElement("div"); box.className = "llm"; el.append(box); }, update(state, el) { const box = el.querySelector(".llm"); box.textContent = ""; if (state.offline) { box.textContent = "llm-cost-dashboard offline"; return; } const c = state.claude ?? {}; const o = state.opencode ?? {}; box.append( line("Context", pct(c.context_pct), c.context_pct), line(`5h window ${clock(c.five_hour_resets_at)}`, pct(c.five_hour_pct), c.five_hour_pct), line(`7d window ${clock(c.seven_day_resets_at)}`, pct(c.seven_day_pct), c.seven_day_pct), line("Month", money(c.extra_used, c.extra_currency)), line("opencode spend", money(o.total_cost)), ); }, };