1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
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 tokens = (n) => {
if (n == null) return "—";
if (n >= 1e9) return `${(n / 1e9).toFixed(1)}B`;
if (n >= 1e6) return `${(n / 1e6).toFixed(1)}M`;
if (n >= 1e3) return `${(n / 1e3).toFixed(1)}k`;
return String(n);
};
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.cost)),
line("opencode tokens", `${tokens(o.tokens)} · ${o.calls ?? 0} calls`),
);
for (const m of (o.models ?? []).slice(0, 4)) {
box.append(line(m.model || m.provider, money(m.cost)));
}
},
};
|