aboutsummaryrefslogtreecommitdiffstats
path: root/static/modules/llm.js
blob: 9d05f29cbb440bc60bdb1b9b5d46fa9e4a723c19 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const num = (v) => (v == null ? null : Number(v));
const money = (v) => (num(v) == null ? "—" : `$${num(v).toFixed(2)}`);
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 CLAUDE_ICON = '<svg class="ico" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m12 3 1.9 5.8L20 11l-6.1 2.2L12 19l-1.9-5.8L4 11l6.1-2.2L12 3Z"/></svg>';
const OPENCODE_ICON = '<svg class="ico" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2"/><path d="m7 10 2 2-2 2"/><path d="M12.5 14H17"/></svg>';

function section(icon, title, model) {
  const sec = document.createElement("section");
  sec.className = "llm-sec";
  const head = document.createElement("header");
  head.className = "llm-head";
  head.innerHTML = `${icon}<h3></h3><span class="model"></span>`;
  head.querySelector("h3").textContent = title;
  head.querySelector(".model").textContent = model || "—";
  sec.append(head);
  return sec;
}

function gauge(label, value) {
  const d = document.createElement("div");
  d.className = "gauge";
  const l = document.createElement("span");
  l.className = "glabel";
  l.textContent = label;
  const v = document.createElement("span");
  v.className = "gvalue num";
  v.textContent = value == null ? "—" : `${Math.round(value)}%`;
  const bar = document.createElement("div");
  bar.className = "gbar";
  const fill = document.createElement("i");
  fill.style.width = `${Math.max(0, Math.min(100, value || 0))}%`;
  bar.append(fill);
  d.append(l, v, bar);
  return d;
}

function stat(label, value, sub) {
  const d = document.createElement("div");
  d.className = "lbig";
  const l = document.createElement("span");
  l.className = "mlabel";
  l.textContent = label;
  const v = document.createElement("span");
  v.className = "mvalue num";
  v.textContent = value;
  const s = document.createElement("span");
  s.className = "msub";
  s.textContent = sub || "";
  d.append(l, v, s);
  return d;
}

export default {
  title: "LLM usage",
  interval: 5,

  mount(el) {
    el.innerHTML = `<div class="llm"></div>`;
  },

  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 ?? {};

    const claude = section(CLAUDE_ICON, "Claude Code", c.model);
    const gauges = document.createElement("div");
    gauges.className = "gauges";
    gauges.append(
      gauge("Context", c.context_pct),
      gauge("5h", c.five_hour_pct),
      gauge("7d", c.seven_day_pct),
    );
    claude.append(gauges);
    box.append(claude);

    const opencode = section(OPENCODE_ICON, "opencode", o.model);
    opencode.append(stat("spend", money(o.cost), `${tokens(o.tokens)} · ${o.calls ?? 0} calls`));
    box.append(opencode);
  },
};