llmtop 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/README.md +70 -67
  2. package/dashboard/dist/assets/be-vietnam-pro-latin-400-normal-PpnXBOrz.woff2 +0 -0
  3. package/dashboard/dist/assets/be-vietnam-pro-latin-500-normal-B6LVzGNe.woff2 +0 -0
  4. package/dashboard/dist/assets/be-vietnam-pro-latin-600-normal-BZDkUTrt.woff2 +0 -0
  5. package/dashboard/dist/assets/be-vietnam-pro-latin-700-normal-DlW1Zbsh.woff2 +0 -0
  6. package/dashboard/dist/assets/be-vietnam-pro-latin-800-normal-BpHZASpI.woff2 +0 -0
  7. package/dashboard/dist/assets/be-vietnam-pro-latin-ext-400-normal-CiZNW1ec.woff2 +0 -0
  8. package/dashboard/dist/assets/be-vietnam-pro-latin-ext-500-normal-h0Fp6aX0.woff2 +0 -0
  9. package/dashboard/dist/assets/be-vietnam-pro-latin-ext-600-normal-BNd8euf0.woff2 +0 -0
  10. package/dashboard/dist/assets/be-vietnam-pro-latin-ext-700-normal-C8_gqRu2.woff2 +0 -0
  11. package/dashboard/dist/assets/be-vietnam-pro-latin-ext-800-normal-GEpNgheG.woff2 +0 -0
  12. package/dashboard/dist/assets/be-vietnam-pro-vietnamese-400-normal-CRcqvyg1.woff2 +0 -0
  13. package/dashboard/dist/assets/be-vietnam-pro-vietnamese-500-normal-DREgrEoJ.woff2 +0 -0
  14. package/dashboard/dist/assets/be-vietnam-pro-vietnamese-600-normal-nyU-ZL2p.woff2 +0 -0
  15. package/dashboard/dist/assets/be-vietnam-pro-vietnamese-700-normal-Csr0PCuG.woff2 +0 -0
  16. package/dashboard/dist/assets/be-vietnam-pro-vietnamese-800-normal-D4LFh2W4.woff2 +0 -0
  17. package/dashboard/dist/assets/index-C6JlgSDz.js +9 -0
  18. package/dashboard/dist/assets/index-CeDllzpB.css +1 -0
  19. package/dashboard/dist/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
  20. package/dashboard/dist/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
  21. package/dashboard/dist/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
  22. package/dashboard/dist/index.html +2 -2
  23. package/dashboard/src/App.tsx +224 -88
  24. package/dashboard/src/controls/DateRange.tsx +3 -3
  25. package/dashboard/src/controls/Search.tsx +2 -2
  26. package/dashboard/src/controls/popover.tsx +24 -4
  27. package/dashboard/src/format.ts +1 -1
  28. package/dashboard/src/i18n.tsx +32 -4
  29. package/dashboard/src/main.tsx +11 -2
  30. package/dashboard/src/model.ts +1 -1
  31. package/dashboard/src/overview-flow.css +24 -0
  32. package/dashboard/src/overviewFlow.tsx +76 -0
  33. package/dashboard/src/sessionDetail.test.ts +19 -0
  34. package/dashboard/src/sessionScope.tsx +28 -0
  35. package/dashboard/src/shell/ConsoleFilters.tsx +203 -0
  36. package/dashboard/src/shell/Header.tsx +29 -0
  37. package/dashboard/src/shell/MobileSheet.tsx +29 -0
  38. package/dashboard/src/shell/Palette.tsx +114 -0
  39. package/dashboard/src/shell/Settings.tsx +37 -0
  40. package/dashboard/src/shell/SpecialStates.tsx +145 -0
  41. package/dashboard/src/shell/preferences.ts +71 -0
  42. package/dashboard/src/shell/styleColors.ts +5 -0
  43. package/dashboard/src/shell/useShortcuts.ts +67 -0
  44. package/dashboard/src/state.test.ts +9 -0
  45. package/dashboard/src/state.ts +13 -2
  46. package/dashboard/src/styles/commands.css +61 -0
  47. package/dashboard/src/styles/console.css +149 -0
  48. package/dashboard/src/styles/donut.css +83 -0
  49. package/dashboard/src/styles/fonts/be-vietnam-pro-LICENSE +93 -0
  50. package/dashboard/src/styles/fonts/be-vietnam-pro-latin-400-normal.woff2 +0 -0
  51. package/dashboard/src/styles/fonts/be-vietnam-pro-latin-500-normal.woff2 +0 -0
  52. package/dashboard/src/styles/fonts/be-vietnam-pro-latin-600-normal.woff2 +0 -0
  53. package/dashboard/src/styles/fonts/be-vietnam-pro-latin-700-normal.woff2 +0 -0
  54. package/dashboard/src/styles/fonts/be-vietnam-pro-latin-800-normal.woff2 +0 -0
  55. package/dashboard/src/styles/fonts/be-vietnam-pro-latin-ext-400-normal.woff2 +0 -0
  56. package/dashboard/src/styles/fonts/be-vietnam-pro-latin-ext-500-normal.woff2 +0 -0
  57. package/dashboard/src/styles/fonts/be-vietnam-pro-latin-ext-600-normal.woff2 +0 -0
  58. package/dashboard/src/styles/fonts/be-vietnam-pro-latin-ext-700-normal.woff2 +0 -0
  59. package/dashboard/src/styles/fonts/be-vietnam-pro-latin-ext-800-normal.woff2 +0 -0
  60. package/dashboard/src/styles/fonts/be-vietnam-pro-vietnamese-400-normal.woff2 +0 -0
  61. package/dashboard/src/styles/fonts/be-vietnam-pro-vietnamese-500-normal.woff2 +0 -0
  62. package/dashboard/src/styles/fonts/be-vietnam-pro-vietnamese-600-normal.woff2 +0 -0
  63. package/dashboard/src/styles/fonts/be-vietnam-pro-vietnamese-700-normal.woff2 +0 -0
  64. package/dashboard/src/styles/fonts/be-vietnam-pro-vietnamese-800-normal.woff2 +0 -0
  65. package/dashboard/src/styles/fonts/jetbrains-mono-LICENSE +93 -0
  66. package/dashboard/src/styles/fonts/jetbrains-mono-latin-ext-wght-normal.woff2 +0 -0
  67. package/dashboard/src/styles/fonts/jetbrains-mono-latin-wght-normal.woff2 +0 -0
  68. package/dashboard/src/styles/fonts/jetbrains-mono-vietnamese-wght-normal.woff2 +0 -0
  69. package/dashboard/src/styles/fonts.css +240 -0
  70. package/dashboard/src/styles/shell.css +151 -0
  71. package/dashboard/src/styles/states.css +32 -0
  72. package/dashboard/src/styles/tokens.css +159 -0
  73. package/dashboard/src/styles.css +13 -44
  74. package/dashboard/src/tip.ts +3 -1
  75. package/dashboard/src/treemap.ts +3 -0
  76. package/dashboard/src/ui.tsx +21 -32
  77. package/dashboard/src/views/Breakdown.tsx +46 -0
  78. package/dashboard/src/views/SessionDetail.tsx +67 -0
  79. package/dashboard/src/views/breakdown.css +33 -0
  80. package/dashboard/src/views/{Activity.tsx → classic/Activity.tsx} +31 -50
  81. package/dashboard/src/views/classic/Context.tsx +140 -0
  82. package/dashboard/src/views/{Models.tsx → classic/Models.tsx} +18 -35
  83. package/dashboard/src/views/classic/Overview.tsx +80 -0
  84. package/dashboard/src/views/classic/Projects.tsx +98 -0
  85. package/dashboard/src/views/{Sessions.tsx → classic/Sessions.tsx} +14 -17
  86. package/dashboard/src/views/{Subagents.tsx → classic/Subagents.tsx} +10 -34
  87. package/dashboard/src/views/console/Activity.tsx +241 -0
  88. package/dashboard/src/views/console/ConsoleBoot.tsx +73 -0
  89. package/dashboard/src/views/console/Context.tsx +162 -0
  90. package/dashboard/src/views/console/Models.tsx +187 -0
  91. package/dashboard/src/views/console/Overview.tsx +91 -0
  92. package/dashboard/src/views/console/Projects.tsx +6 -0
  93. package/dashboard/src/views/console/ProviderMeters.tsx +35 -0
  94. package/dashboard/src/views/console/Sessions.tsx +6 -0
  95. package/dashboard/src/views/console/Subagents.tsx +137 -0
  96. package/dashboard/src/views/console/activity-context.css +92 -0
  97. package/dashboard/src/views/console/console-boot.css +137 -0
  98. package/dashboard/src/views/console/modelVisuals.tsx +29 -0
  99. package/dashboard/src/views/console/models-subagents.css +203 -0
  100. package/dashboard/src/views/donut/Activity.tsx +247 -0
  101. package/dashboard/src/views/donut/Context.tsx +169 -0
  102. package/dashboard/src/views/donut/DonutBoot.tsx +68 -0
  103. package/dashboard/src/views/donut/Models.tsx +58 -0
  104. package/dashboard/src/views/donut/Overview.tsx +49 -0
  105. package/dashboard/src/views/donut/Projects.tsx +6 -0
  106. package/dashboard/src/views/donut/Ring.tsx +30 -0
  107. package/dashboard/src/views/donut/Sessions.tsx +6 -0
  108. package/dashboard/src/views/donut/Subagents.tsx +131 -0
  109. package/dashboard/src/views/donut/activity-context.css +154 -0
  110. package/dashboard/src/views/donut/donut-boot.css +165 -0
  111. package/dashboard/src/views/donut/models-subagents.css +157 -0
  112. package/dashboard/src/views/records/Projects.tsx +140 -0
  113. package/dashboard/src/views/records/Sessions.tsx +125 -0
  114. package/dashboard/src/views/records/records.css +119 -0
  115. package/dashboard/src/views/session-detail.css +44 -0
  116. package/dashboard/src/vm/activity.ts +72 -0
  117. package/dashboard/src/vm/colors.ts +57 -0
  118. package/dashboard/src/vm/context.ts +81 -0
  119. package/dashboard/src/vm/filterChoices.ts +27 -0
  120. package/dashboard/src/vm/models.ts +50 -0
  121. package/dashboard/src/vm/overview.ts +46 -0
  122. package/dashboard/src/vm/palette.ts +63 -0
  123. package/dashboard/src/vm/projects.ts +63 -0
  124. package/dashboard/src/vm/providerBreakdown.ts +19 -0
  125. package/dashboard/src/vm/sessionDetail.ts +16 -0
  126. package/dashboard/src/vm/sessions.ts +36 -0
  127. package/dashboard/src/vm/specialStates.ts +64 -0
  128. package/dashboard/src/vm/subagents.ts +36 -0
  129. package/dashboard/src/vm.test.ts +183 -0
  130. package/package.json +1 -1
  131. package/src/api.mjs +6 -1
  132. package/src/dashboard.mjs +4 -1
  133. package/src/db.mjs +5 -2
  134. package/src/providers/codex.mjs +121 -19
  135. package/src/serve.mjs +20 -10
  136. package/dashboard/dist/assets/index-Ba0kF2tG.js +0 -9
  137. package/dashboard/dist/assets/index-NQzaBVIY.css +0 -1
  138. package/dashboard/dist/assets/montserrat-cyrillic-ext-wght-normal-CO5hGrJv.woff2 +0 -0
  139. package/dashboard/dist/assets/montserrat-cyrillic-wght-normal-EAA9jha_.woff2 +0 -0
  140. package/dashboard/dist/assets/roboto-mono-cyrillic-ext-wght-normal-BUDPrIko.woff2 +0 -0
  141. package/dashboard/dist/assets/roboto-mono-cyrillic-wght-normal-HUlVHixE.woff2 +0 -0
  142. package/dashboard/dist/assets/roboto-mono-greek-wght-normal-BJJTbwTT.woff2 +0 -0
  143. package/dashboard/src/views/Context.tsx +0 -183
  144. package/dashboard/src/views/Overview.tsx +0 -83
  145. package/dashboard/src/views/Projects.tsx +0 -116
@@ -1,32 +1,19 @@
1
+ import { RelatedSessions } from "../../sessionScope.tsx";
1
2
  import { Fragment, useState, type ReactNode } from "react";
2
- import { useDash } from "../dash.ts";
3
- import { dayAdd, rebuilds, skillUses, slice, slotsOf, toolCalls, type Fact, type Model, type Session } from "../model.ts";
4
- import { ChartSeg, Columns, Delta, Empty, Heat, HeatSeg, Info, Kpi, Stats, tip } from "../ui.tsx";
3
+ import { useDash } from "../../dash.ts";
4
+ import { type Fact } from "../../model.ts";
5
+ import { ACTIVITY_PREVIEW_SIZE as SHOWN, buildActivityVM, type ActivityVM } from "../../vm/activity.ts";
6
+ import { ChartSeg, Columns, Delta, Empty, Heat, HeatSeg, Info, Kpi, Stats, tip } from "../../ui.tsx";
5
7
 
6
8
  // What the agent did: time on the model and on tools, owner turns, lines, compactions and agent time per owner turn
7
9
  // against the window before; working time per day beside the hours of the week it fell in; lines per day; where the cache was rebuilt;
8
10
  // tool calls with their failed part beside the skills used.
9
- const work = (M: Model, F: Fact[]) => {
10
- const ids = [...new Set(F.map((x) => x.s))], ss = ids.map((s) => M.sessions[s]), add = (k: (s: Session) => number) => ss.reduce((n, s) => n + k(s), 0);
11
- return { ids, ss, api: add((s) => s.api), tool: add((s) => s.tool), idle: add((s) => Math.max(0, s.dur * 60 - s.api - s.tool)), owner: add((s) => s.owner), compact: add((s) => s.compact), la: add((s) => s.la), lr: add((s) => s.lr) };
12
- };
13
-
14
- // How long the agent worked, waiting for the model or running tools, for each owner turn.
15
- const perTurn = (w: { api: number; tool: number; owner: number }) => (w.owner ? (w.api + w.tool) / w.owner : 0);
16
-
17
11
  export function Activity({ F, list }: { F: Fact[]; list: string[] }) {
18
12
  const { M, S, filter, t, f } = useDash();
19
- const w = work(M, F), pf = dayAdd(filter.from, -list.length);
20
- const p = pf >= M.first ? work(M, slice(M, filter, pf, dayAdd(filter.from, -1))) : null;
13
+ const data = buildActivityVM(M, F, list, filter, S.heat);
14
+ const { w, p, slots, timeRows, lineRows, idle, series, perTurnNow, perTurnPrior } = data;
21
15
  if (!w.ss.length) return <section className="card"><Empty /></section>;
22
16
 
23
- // Per day by session start, or per hour in a one-day window.
24
- const slots = slotsOf(list), hourly = slots !== list;
25
- const at = (s: Session) => slots.indexOf(hourly ? s.hour : s.day);
26
- const per = (k: (s: Session) => number) => { const out = slots.map(() => 0); w.ss.forEach((s) => { const i = at(s); if (i >= 0) out[i] += k(s); }); return out; };
27
- const api = per((s) => s.api / 3600), tool = per((s) => s.tool / 3600), idle = per((s) => Math.max(0, s.dur * 60 - s.api - s.tool) / 3600);
28
- const la = per((s) => s.la), lr = per((s) => s.lr);
29
- const daily = (k: (s: Session) => number) => list.map((d) => w.ss.reduce((n, s) => n + (s.day === d ? k(s) : 0), 0));
30
17
  const hours = (h: number) => (h >= 1 ? `${f.dec(h)}h` : `${Math.round(h * 60)}m`);
31
18
  const kpi = (label: string, value: ReactNode, a: number, b: number | undefined, series: number[]) => (
32
19
  <Kpi label={label} value={value} d={<Delta a={a} b={p ? b : undefined} side="none" />} series={series} />
@@ -35,46 +22,43 @@ export function Activity({ F, list }: { F: Fact[]; list: string[] }) {
35
22
  return (
36
23
  <div className="stack">
37
24
  <div className="grid kpis">
38
- {kpi(t("api"), f.dur(w.api / 60), w.api, p?.api, daily((s) => s.api))}
39
- {kpi(t("tool"), f.dur(w.tool / 60), w.tool, p?.tool, daily((s) => s.tool))}
40
- {kpi(t("ownerTurns"), f.int(w.owner), w.owner, p?.owner, daily((s) => s.owner))}
41
- {kpi(t("linesAR"), <>{f.cmp(w.la)} <small>/ {f.cmp(w.lr)}</small></>, w.la + w.lr, p ? p.la + p.lr : undefined, daily((s) => s.la + s.lr))}
42
- {kpi(t("compactions"), f.int(w.compact), w.compact, p?.compact, daily((s) => s.compact))}
43
- {kpi(t("perTurn"), f.dur(perTurn(w) / 60), perTurn(w), p ? perTurn(p) : undefined, list.map((d) => { const ss = w.ss.filter((s) => s.day === d), o = ss.reduce((n, s) => n + s.owner, 0); return o ? ss.reduce((n, s) => n + s.api + s.tool, 0) / o : 0; }))}
25
+ {kpi(t("api"), f.dur(w.api / 60), w.api, p?.api, series.api)}
26
+ {kpi(t("tool"), f.dur(w.tool / 60), w.tool, p?.tool, series.tool)}
27
+ {kpi(t("ownerTurns"), f.int(w.owner), w.owner, p?.owner, series.owner)}
28
+ {kpi(t("linesAR"), <>{f.cmp(w.la)} <small>/ {f.cmp(w.lr)}</small></>, w.la + w.lr, p ? p.la + p.lr : undefined, series.lines)}
29
+ {kpi(t("compactions"), f.int(w.compact), w.compact, p?.compact, series.compact)}
30
+ {kpi(t("perTurn"), f.dur(perTurnNow / 60), perTurnNow, perTurnPrior, series.perTurn)}
44
31
  </div>
45
32
  <div className="grid half">
46
33
  <section className="card">
47
34
  <header><h2>{t("time")}</h2><div className="segs"><span className="hint">{t("idle")}: {f.dur(w.idle / 60)}</span><ChartSeg /></div></header>
48
35
  <ul className="legend"><li><i className="c1" />{t("api")}<b>{f.dur(w.api / 60)}</b></li><li><i className="c3" />{t("tool")}<b>{f.dur(w.tool / 60)}</b></li></ul>
49
- <Columns list={slots} rows={slots.map((_, i) => [api[i], tool[i]])} groups={[{ n: t("api"), c: "c1" }, { n: t("tool"), c: "c3" }]} metric="out" fmt={hours}
36
+ <Columns list={slots} rows={timeRows} groups={[{ n: t("api"), c: "c1" }, { n: t("tool"), c: "c3" }]} metric="out" fmt={hours}
50
37
  extra={(i) => (idle[i] ? [["c0", t("idle"), hours(idle[i])]] : [])} />
51
38
  </section>
52
- <section className="card"><header><h2>{t("hours")}</h2><HeatSeg /></header><Heat F={F} by={S.heat} /></section>
39
+ <section className="card"><header><h2>{t("hours")}</h2><HeatSeg /></header><Heat data={data.heat} by={S.heat} /></section>
53
40
  </div>
54
41
  <section className="card">
55
42
  <header><h2>{t("lines")}</h2><ChartSeg /></header>
56
43
  <ul className="legend"><li><i className="c1" />{t("added")}<b>{f.cmp(w.la)}</b></li><li><i className="c4" />{t("removed")}<b>{f.cmp(w.lr)}</b></li></ul>
57
- <Columns list={slots} rows={slots.map((_, i) => [la[i], lr[i]])} groups={[{ n: t("added"), c: "c1" }, { n: t("removed"), c: "c4" }]} metric="out" fmt={(v) => f.cmp(v)} />
44
+ <Columns list={slots} rows={lineRows} groups={[{ n: t("added"), c: "c1" }, { n: t("removed"), c: "c4" }]} metric="out" fmt={(v) => f.cmp(v)} />
58
45
  </section>
59
- <Rebuilds F={F} />
46
+ <Rebuilds data={data.rebuild} sessionsLink={<RelatedSessions F={F} scope="rebuild" />} />
60
47
  <div className="grid half">
61
- <Tools ids={w.ids} />
62
- <Skills ids={w.ids} />
48
+ <Tools data={data.tools} />
49
+ <Skills data={data.skills} />
63
50
  </div>
64
51
  </div>
65
52
  );
66
53
  }
67
54
 
68
- const SHOWN = 12;
69
-
70
55
  // Tool calls, most used first, each with its failed part; the lead says how many failed and which tool most of them came
71
56
  // from. Past SHOWN rows the rest opens below.
72
- function Tools({ ids }: { ids: number[] }) {
73
- const { M, t, L, f } = useDash();
57
+ function Tools({ data }: { data: ActivityVM["tools"] }) {
58
+ const { t, L, f } = useDash();
74
59
  const [all, setAll] = useState(false);
75
- const rows = toolCalls(M, ids), shown = all ? rows : rows.slice(0, SHOWN);
76
- const calls = rows.reduce((n, [, [c]]) => n + c, 0), errs = rows.reduce((n, [, [, e]]) => n + e, 0), tmax = Math.max(1, ...rows.map(([, [c]]) => c));
77
- const worst = [...rows].sort((a, b) => b[1][1] - a[1][1])[0];
60
+ const { rows, calls, errs, tmax, worst } = data;
61
+ const shown = all ? rows : rows.slice(0, SHOWN);
78
62
  return (
79
63
  <section className="card">
80
64
  <header><h2>{t("tools")}</h2><span className="hint">{f.int(calls)} {t("calls")} · {f.int(rows.length)} {t("toolCount")}</span></header>
@@ -97,13 +81,11 @@ const SOURCE = { project: "srcProject", personal: "srcPersonal", plugin: "srcPlu
97
81
 
98
82
  // Skills by where they were loaded from, most used first: each bar splits what the owner typed as a command from what the
99
83
  // agent called on its own, with its failed calls at the end. Past SHOWN rows the rest opens below.
100
- function Skills({ ids }: { ids: number[] }) {
101
- const { M, t, f } = useDash();
84
+ function Skills({ data }: { data: ActivityVM["skills"] }) {
85
+ const { t, f } = useDash();
102
86
  const [all, setAll] = useState(false);
103
- const rows = skillUses(M, ids), uses = rows.reduce((n, r) => n + r.command + r.agent, 0);
104
- const top = new Set([...rows].sort((a, b) => b.command + b.agent - (a.command + a.agent)).slice(0, SHOWN).map((r) => r.name));
105
- const shown = all ? rows : rows.filter((r) => top.has(r.name));
106
- const max = Math.max(1, ...rows.map((r) => r.command + r.agent));
87
+ const { rows, uses, preview, max } = data;
88
+ const shown = all ? rows : preview;
107
89
  return (
108
90
  <section className="card">
109
91
  <header><h2>{t("skills")}</h2><span className="hint">{f.int(uses)} {t("uses")} · {f.int(rows.length)} {t("skillCount")}</span></header>
@@ -132,17 +114,16 @@ function Skills({ ids }: { ids: number[] }) {
132
114
 
133
115
  // Where the prompt cache had to be written again: the four causes with their count, rewritten tokens and extra cost
134
116
  // over reading it; beside it, the idle ones by the time since the previous request.
135
- function Rebuilds({ F }: { F: Fact[] }) {
117
+ function Rebuilds({ data, sessionsLink }: { data: ActivityVM["rebuild"]; sessionsLink: ReactNode }) {
136
118
  const { t, L, f } = useDash();
137
- const r = rebuilds(F);
119
+ const { r, count, gmax } = data;
138
120
  const rows = [["idle", t("rbIdle"), t("rbIdleTip")], ["compaction", t("rbCompaction"), t("rbCompactionTip")], ["other", t("rbOther"), t("rbOtherTip")], ["start", t("rbStart"), t("rbStartTip")]] as const;
139
- const count = r.start.n + r.compaction.n + r.idle.n + r.other.n;
140
- const gaps = [t("gapShort"), t("gapMid"), t("gapLong")], gmax = Math.max(1, ...r.gaps);
121
+ const gaps = [t("gapShort"), t("gapMid"), t("gapLong")];
141
122
  return (
142
123
  <div className="grid half">
143
124
  <section className="card">
144
125
  <header>
145
- <div className="ttl"><h2>{t("rebuilt")}</h2><Info label={t("howToRead")}>
126
+ <div className="ttl"><h2>{t("rebuilt")}</h2>{sessionsLink}<Info label={t("howToRead")}>
146
127
  <p>{t("rbInfo")}</p>
147
128
  <ul>{rows.map(([c, label, def]) => <li key={c}><b>{label}</b>: {def}</li>)}</ul>
148
129
  </Info></div>
@@ -0,0 +1,140 @@
1
+ import { useState } from "react";
2
+ import { RelatedSessions } from "../../sessionScope.tsx";
3
+ import { ReplayNotes } from "../Breakdown.tsx";
4
+ import { useDash } from "../../dash.ts";
5
+ import { type Fact } from "../../model.ts";
6
+ import { buildContextVM, round5, TAIL, type ContextVM, type ReplayScope } from "../../vm/context.ts";
7
+ import { Delta, Empty, Kpi, Stats, tip, type TipRow } from "../../ui.tsx";
8
+
9
+ // Whether the auto-compact window suits the sessions: how compactions went, what reading the context cost by its size, and
10
+ // the same requests replayed with compaction at other points.
11
+
12
+ export function Context({ F, list }: { F: Fact[]; list: string[] }) {
13
+ return F.length ? <ContextContent F={F} list={list} /> : <section className="card"><Empty /></section>;
14
+ }
15
+
16
+ function ContextContent({ F, list }: { F: Fact[]; list: string[] }) {
17
+ const { M, filter, t, L, f } = useDash();
18
+ const [scope, setScope] = useState<ReplayScope>("all");
19
+ const data = buildContextVM(M, F, list, filter, scope);
20
+ const { total, current, prior, series } = data;
21
+ return (
22
+ <div className="stack ctx">
23
+ <div className="grid kpis">
24
+ <Kpi label={t("compactions")} value={f.int(current.count)} d={<Delta a={current.count} b={prior.count} side="none" />} series={series.count} />
25
+ <Kpi label={t("compacting")} value={<>{f.usd(current.compacting)} <small>{L.ofCost(f.pct(total ? current.compacting / total : 0))}</small></>}
26
+ d={<Delta a={current.compacting} b={prior.compacting} side="none" />} series={series.compacting} />
27
+ <Kpi label={t("readingCtx")} value={<>{f.usd(current.reading)} <small>{L.ofCost(f.pct(total ? current.reading / total : 0))}</small></>}
28
+ d={<Delta a={current.reading} b={prior.reading} side="none" />} series={series.reading} />
29
+ </div>
30
+ <RelatedSessions F={F} scope="compaction" />
31
+ <div className="grid half">
32
+ <Compactions data={data.compactions} />
33
+ <Reading data={data.reading} />
34
+ </div>
35
+ <Replay data={data.replay} scope={scope} setScope={setScope} />
36
+ </div>
37
+ );
38
+ }
39
+
40
+ // How compactions went: what each kept and left to work with, the wait, what one cost, the sessions that ended right after
41
+ // one, and the stretch from each to the next by its order in the session.
42
+ function Compactions({ data }: { data: ContextVM["compactions"] }) {
43
+ const { t, L, f } = useDash();
44
+ const { parts: counts, fire, kept, wait, totalWait, sum, rb, soon, intervals } = data;
45
+ const parts = [[counts[0], L.nAuto], [counts[1], L.nManual], [counts[2], L.nInSubs]] as const;
46
+ const head = <header><h2>{t("compactions")}</h2><span className="hint">{parts.filter(([n]) => n).map(([n, l]) => l(f.int(n))).join(" · ")}</span></header>;
47
+ if (!data.count) return <section className="card">{head}<Empty /></section>;
48
+ return (
49
+ <section className="card">
50
+ {head}
51
+ {fire && kept && kept < fire ? <>
52
+ <ul className="legend"><li><i className="c0" />{t("keptAfter")}<b>{f.cmp(kept)}</b></li><li><i className="c1" />{t("roomToWork")}<b>{f.cmp(fire - kept)}</b></li></ul>
53
+ <div className="gauge">
54
+ <div className="track"><i className="c0" style={{ width: `${(kept / fire) * 100}%` }} /><i className="c1" style={{ flex: 1 }} /></div>
55
+ <div className="ticks"><span style={{ left: 0 }}>0</span><span style={{ left: `${(kept / fire) * 100}%` }}>{f.cmp(kept)}</span><span style={{ left: "100%" }}>{L.compactsAt(f.cmp(fire))}</span></div>
56
+ </div>
57
+ </> : null}
58
+ <Stats items={[
59
+ { label: t("wait"), value: wait !== null ? `${Math.round(wait / 1000)} s` : "—", sub: wait !== null ? L.inAll(f.dur(totalWait / 60000)) : undefined },
60
+ { label: t("eachCosts"), value: f.usd(sum + rb), sub: L.eachSplit(f.usd(sum), f.usd(rb)).map((s) => <span key={s} className="line">{s}</span>) },
61
+ { label: t("endedSoon"), value: f.int(soon), sub: L.endedWithin(TAIL) },
62
+ ]} />
63
+ {data.mainCount ? <div className="scroll"><table>
64
+ <thead><tr><th>{t("inSession")}</th><th className="num">{t("compactions")}</th><th className="num o1">{t("keptCol")}</th><th className="num">{t("reqsToNext")}</th><th className="num">{t("turnsToNext")}</th></tr></thead>
65
+ <tbody>{intervals.map((g, k) => g.count ? <tr key={k}>
66
+ <td className="name">{L.nth[k]}</td><td className="num">{f.int(g.count)}</td><td className="num o1">{cell(g.kept, f.cmp)}</td>
67
+ <td className="num">{cell(g.next, f.int)}</td><td className="num">{cell(g.turns, f.int)}</td>
68
+ </tr> : null)}</tbody>
69
+ </table></div> : null}
70
+ </section>
71
+ );
72
+ }
73
+ const cell = (v: number | null, fmt: (n: number) => string) => (v === null ? "—" : fmt(v));
74
+
75
+ // The cost of reading the context by the context each request carried: rows add up to the overview's cost of reading the
76
+ // context, a part the host charged with no request included.
77
+ function Reading({ data }: { data: ContextVM["reading"] }) {
78
+ const { t, L, f } = useDash();
79
+ const { E, n, b, q, cost, reqs, above, aboveQ, per, top, base, ctx, max } = data;
80
+ const head = <header><h2>{t("readingCtx")}</h2><span className="hint">{t("byCtx")}</span></header>;
81
+ if (!reqs) return <section className="card">{head}<Empty /></section>;
82
+ const k = (v: number) => f.cmp(v).replace(/K$/, "");
83
+ const label = (j: number) => (j === 0 ? L.under(f.cmp(E[0])) : j === n - 1 ? L.andOver(f.cmp(E[n - 2])) : `${f.cmp(E[j - 1]).endsWith("K") && f.cmp(E[j]).endsWith("K") ? k(E[j - 1]) : f.cmp(E[j - 1])}–${f.cmp(E[j])}`);
84
+ return (
85
+ <section className="card">
86
+ {head}
87
+ <Stats items={[
88
+ { label: t("ctxPerReq"), value: f.cmp(ctx), sub: t("onAverage") },
89
+ { label: L.above(f.cmp(300e3)), value: f.pct(cost ? above / cost : 0), sub: L.ofCostReqs(f.pct(aboveQ / reqs)) },
90
+ ...(top >= 0 && base >= 0 && top !== base && per[base] ? [{ label: L.perReqAt(label(top)), value: f.times(per[top] / per[base]), sub: L.whatAt(label(base)) }] : []),
91
+ ]} />
92
+ <div className="scroll"><table>
93
+ <thead><tr><th>{t("ctxCol")}</th><th className="cell-bar" /><th className="num o1">{t("requests")}</th><th className="num">{t("cost")}</th><th className="num">{t("perReqCol")}</th></tr></thead>
94
+ <tbody>
95
+ {q.map((v, j) => (v ? <tr key={j}>
96
+ <td className="name">{label(j)}</td><td className="cell-bar"><span className="bar1" style={{ width: `${max ? (b[j] / max) * 100 : 0}%` }} /></td>
97
+ <td className="num o1">{f.int(v)}</td><td className="num">{f.usd(b[j])}</td><td className="num"><b>{f.usd3(per[j])}</b></td>
98
+ </tr> : null))}
99
+ {b[n] >= cost * 0.005 ? <tr><td className="name">{t("noRequest")}</td><td className="cell-bar" /><td className="num o1">—</td><td className="num">{f.usd(b[n])}</td><td className="num">—</td></tr> : null}
100
+ </tbody>
101
+ <tfoot><tr><td>{t("all")}</td><td /><td className="num o1">{f.int(reqs)}</td><td className="num">{f.usd(cost)}</td><td className="num">{f.usd3(cost / reqs)}</td></tr></tfoot>
102
+ </table></div>
103
+ </section>
104
+ );
105
+ }
106
+
107
+ // The main threads replayed with compaction at each point, against the point it fires at now. A point within NEAR of now
108
+ // gives way to it; when the models in the filter fire at different points now, now has a row of its own.
109
+ function Replay({ data, scope, setScope }: { data: ContextVM["replay"]; scope: ReplayScope; setScope: (scope: ReplayScope) => void }) {
110
+ const { t, f } = useDash();
111
+ const { hasRows, recorded, recordedCost, P, shown, one, levels, base, max } = data;
112
+ const kinds: [string, string][] = [["ck0", t("readingCtx")], ["ck1", t("compacting")], ["ck2", t("rebuildIdle")]];
113
+ const name = (x: (typeof shown)[number]) => (x.now ? (one ? f.cmp(round5(one)) : levels.map((v) => f.cmp(round5(v))).join(" · ")) : f.cmp(x.at));
114
+ return (
115
+ <section className="card">
116
+ <header>
117
+ <h2>{t("otherPoints")}</h2>
118
+ <div className="segs"><div className="seg" role="group">{(["all", "long"] as const).map((k) => <button key={k} aria-pressed={scope === k} onClick={() => setScope(k)}>{t(k === "all" ? "allSessions" : "long4")}</button>)}</div></div>
119
+ </header>
120
+ {hasRows ? <>
121
+ <ReplayNotes count={recorded} cost={recordedCost} />
122
+ <ul className="legend">{kinds.map(([c, n]) => <li key={c}><i className={c} />{n}</li>)}</ul>
123
+ <div className="scroll"><table>
124
+ <thead><tr><th>{t("compactsAtCol")}</th><th className="num">{t("compactions")}</th><th className="num o2">{t("sessCompacted")}</th><th className="cell-bar o1" /><th className="num">{t("estimatedCost")}</th>{base ? <th className="num">{t("againstNow")}</th> : null}</tr></thead>
125
+ <tbody>{shown.map((x) => {
126
+ const d = base ? x.total - base.total : 0;
127
+ const rowsTip: TipRow[] = kinds.map(([c, n], k) => [c, n, f.usd(x.parts[k])]);
128
+ return <tr key={`${x.j}`} className={x.now ? "now" : ""} data-tip={tip(`${t("compactsAtCol")} ${name(x)}`, rowsTip)}>
129
+ <td className="lbl">{name(x)}{x.now ? <span className="chip">{t("now")}</span> : null}{!x.now && x.at === P.at(-1) ? <span className="sub">{t("byDefault")}</span> : null}</td>
130
+ <td className="num">{f.int(x.n)}</td><td className="num o2">{f.int(x.sess)}</td>
131
+ <td className="cell-bar o1"><div className="split kinds" style={{ width: `${(x.total / max) * 100}%` }}>{x.parts.map((v, k) => <i key={k} className={kinds[k][0]} style={{ flex: v }} />)}</div></td>
132
+ <td className="num">{f.usd(x.total)}</td>
133
+ {base ? <td className="num">{x.now ? "—" : <span className={d < 0 ? "better" : d > 0 ? "worse" : ""}>{d < 0 ? "−" : "+"}{f.usd(Math.abs(d))}</span>}</td> : null}
134
+ </tr>;
135
+ })}</tbody>
136
+ </table></div>
137
+ </> : <Empty />}
138
+ </section>
139
+ );
140
+ }
@@ -1,7 +1,9 @@
1
- import { useDash } from "../dash.ts";
2
- import { behavior, costParts, mainLevel, totals, vsHigh, type Behavior, type Fact } from "../model.ts";
3
- import { Disclosure } from "../controls/Disclosure.tsx";
4
- import { Empty, Info, Legend, Rank, Swatch, tip, useCostKinds, type TipRow } from "../ui.tsx";
1
+ import { BreakdownSessions } from "../Breakdown.tsx";
2
+ import { useDash } from "../../dash.ts";
3
+ import { type Behavior, type Fact } from "../../model.ts";
4
+ import { buildModelsVM, type ModelsVM } from "../../vm/models.ts";
5
+ import { Disclosure } from "../../controls/Disclosure.tsx";
6
+ import { Empty, Info, Legend, Rank, Swatch, tip, useCostKinds, type TipRow } from "../../ui.tsx";
5
7
 
6
8
  // The model and effort story, top to bottom: which model the money goes to against the work it does, how each
7
9
  // model works, what share each effort level takes, and what one request writes at each level.
@@ -9,22 +11,20 @@ import { Empty, Info, Legend, Rank, Swatch, tip, useCostKinds, type TipRow } fro
9
11
  export function Models({ F }: { F: Fact[] }) {
10
12
  const { M, t, f, set } = useDash();
11
13
  const open = (n: string) => set({ view: "overview", model: n });
12
- const all = totals(M, F);
13
- const per = M.models.map((n, m) => ({ n, m, c: M.mColor(m), tt: totals(M, F.filter((y) => y.m === m)) })).filter((r) => r.tt.q);
14
- // Each model's cost split by what it paid for; web search and the part the table cannot split are named only when some model has them.
15
- const kinds = useCostKinds(), parts = (r: (typeof per)[number]) => costParts(r.tt.c, r.tt.k);
16
- const shown = kinds.filter((_, j) => j < 3 || per.some((r) => parts(r)[j]));
14
+ const { all, per: rows, behaviors, efforts } = buildModelsVM(M, F);
15
+ const per = rows.map((r) => ({ ...r, c: M.mColor(r.m) }));
16
+ const kinds = useCostKinds();
17
+ const shown = kinds.filter((_, j) => j < 3 || per.some((r) => r.parts[j]));
17
18
 
18
19
  if (!per.length) return <section className="card"><Empty /></section>;
19
20
  // The model's whole cost as one bar, its parts labelled with their share when there is room; amounts in the tooltip.
20
21
  const kindBar = (name: string, c: number, v: number[]) => {
21
22
  const sum = v.reduce((a, b) => a + b, 0);
22
- if (!sum) return "—";
23
23
  const about = tip(`${name} · ${f.usd(c)}`, v.map((x, j): TipRow => [kinds[j].c, kinds[j].n, `${f.usd(x)} · ${f.pct(x / sum)}`]).filter((_, j) => v[j]));
24
24
  return (
25
- <span className="split kinds" tabIndex={0} data-tip={about}>
25
+ <BreakdownSessions model={name} detail={sum ? about : undefined}><span className="split kinds">
26
26
  {v.map((x, j) => (x ? <i key={j} className={kinds[j].c} style={{ width: `${(x / sum) * 100}%` }}>{x / sum >= 0.12 ? f.pct(x / sum) : ""}</i> : null))}
27
- </span>
27
+ </span></BreakdownSessions>
28
28
  );
29
29
  };
30
30
  return (
@@ -32,7 +32,7 @@ export function Models({ F }: { F: Fact[] }) {
32
32
  <section className="card">
33
33
  <header><h2>{t("whereMoney")}</h2><Legend groups={shown} metric="sess" style={{ margin: 0 }} /></header>
34
34
  <div className="scroll"><table className="money">
35
- <thead><tr><th>{t("model")}</th><th className="num">{t("cost")}</th><th className="num">{t("share")}</th><th className="num o2">{t("perM")}</th><th className="num o2">{t("cache")}</th><th className="num o1">{t("ctxPerReq")}</th><th className="wide">{t("costKinds")}</th></tr></thead>
35
+ <thead><tr><th>{t("model")}</th><th className="num">{t("cost")}</th><th className="num">{t("shareTotalCost")}</th><th className="num o2">{t("perM")}</th><th className="num o2">{t("cache")}</th><th className="num o1">{t("ctxPerReq")}</th><th className="wide">{t("costKinds")}</th></tr></thead>
36
36
  <tbody>{per.map((r) => {
37
37
  const tokens = r.tt.i + r.tt.cr + r.tt.cw + r.tt.o;
38
38
  return (
@@ -43,7 +43,7 @@ export function Models({ F }: { F: Fact[] }) {
43
43
  <td className="num o2">{tokens ? f.usd((r.tt.c / tokens) * 1e6) : "—"}</td>
44
44
  <td className="num o2">{r.tt.q ? f.pct(r.tt.hit) : "—"}</td>
45
45
  <td className="num o1">{r.tt.q ? f.cmp(r.tt.ctx) : "—"}</td>
46
- <td className="wide" onClick={(e) => e.stopPropagation()}>{kindBar(r.n, r.tt.c, parts(r))}</td>
46
+ <td className="wide" onClick={(e) => e.stopPropagation()}>{kindBar(r.n, r.tt.c, r.parts)}</td>
47
47
  </tr>
48
48
  );
49
49
  })}</tbody>
@@ -61,8 +61,8 @@ export function Models({ F }: { F: Fact[] }) {
61
61
  </table></div>
62
62
  </Disclosure>
63
63
  </section>
64
- <Behaviors B={behavior(M, F)} />
65
- <Efforts F={F} />
64
+ <Behaviors B={behaviors} />
65
+ <Efforts data={efforts} />
66
66
  </div>
67
67
  );
68
68
  }
@@ -100,32 +100,15 @@ function Behaviors({ B }: { B: Behavior[] }) {
100
100
 
101
101
  // Each effort level's share of sessions, requests, output, thinking and cost, with the level whose cost departs most from its requests named
102
102
  // above; beside it, what one request writes at each level, model by model.
103
- function Efforts({ F }: { F: Fact[] }) {
103
+ function Efforts({ data }: { data: ModelsVM["efforts"] }) {
104
104
  const { M, t, L, f } = useDash();
105
- const lv = M.levels.filter((e) => F.some((x) => x.e === e));
105
+ const { lv, q, o, th, c, sq, so, sth, sc, k, ss, sw, n, sn, price, allC } = data;
106
106
  if (!lv.length) return null;
107
- const total = (pick: (x: Fact) => number) => lv.map((e) => F.reduce((n, x) => n + (x.e === e ? pick(x) : 0), 0));
108
- const share = (arr: number[]) => { const sum = arr.reduce((a, b) => a + b, 0) || 1; return arr.map((v) => v / sum); };
109
- const q = total((x) => x.q), o = total((x) => x.o), th = total((x) => x.th), c = total((x) => x.c), sq = share(q), so = share(o), sth = share(th), sc = share(c);
110
- let k = 0;
111
- lv.forEach((_, i) => { if (Math.abs(sc[i] - sq[i]) > Math.abs(sc[k] - sq[k])) k = i; });
112
107
  const lead = lv.length > 1 ? <p className="lead accent" style={{ borderColor: `var(--v${lv[k]}, var(--s0))` }}>{(sc[k] >= sq[k] ? L.leadMore : L.leadLess)(M.efforts[lv[k]], f.pct(sq[k]), f.pct(sc[k]))}</p> : null;
113
108
  const bar = (label: string, sh: number[], raw: number[], fm: (n: number) => string) => [
114
109
  <span key={`l${label}`}>{label}</span>,
115
110
  <div key={`b${label}`} className="split">{lv.map((e, i) => (sh[i] ? <i key={e} className={`v${e}`} style={{ width: `${sh[i] * 100}%` }} data-tip={tip(M.efforts[e], [["v" + e, label, fm(raw[i]) + " · " + f.pct(sh[i])]])}>{sh[i] >= 0.14 ? f.pct(sh[i]) : ""}</i> : null))}</div>,
116
111
  ];
117
- // Sessions whose main thread changed level; the Sessions table draws their levels.
118
- const ss = [...new Set(F.filter((x) => !x.sub).map((x) => x.s))].map((s) => M.sessions[s]), sw = ss.filter((x) => x.runs.length > 1);
119
- // Each session once, under the level its main thread ran most.
120
- const n = lv.map((e) => ss.filter((x) => x.runs.length && mainLevel(x.runs) === e).length), sn = share(n);
121
- // What effort controls: how much a request writes (thinking included) at each level, model by model, and against
122
- // the same model's high within the sessions that ran both, since most sessions keep one level and the levels would
123
- // otherwise compare different work. Cost per request follows the context more than the effort, so it stays in the tooltip.
124
- const price = M.models.map((n, m) => {
125
- const cells = lv.map((e) => { const x = F.filter((y) => y.m === m && y.e === e); return { e, q: x.reduce((a, y) => a + y.q, 0), o: x.reduce((a, y) => a + y.o, 0), th: x.reduce((a, y) => a + y.th, 0), c: x.reduce((a, y) => a + y.c, 0) }; });
126
- return { n, m, cells: cells.map((r) => ({ ...r, vs: r.e !== 2 && r.q ? vsHigh(F, m, r.e, 2) : null })) };
127
- }).filter((r) => r.cells.some((x) => x.q));
128
- const allC = c.reduce((a, b) => a + b, 0) || 1;
129
112
  return (
130
113
  <div className="grid half">
131
114
  <section className="card">
@@ -0,0 +1,80 @@
1
+ import { SessionLink } from "../SessionDetail.tsx";
2
+ import type { ReactNode } from "react";
3
+ import { useDash } from "../../dash.ts";
4
+ import { type Fact } from "../../model.ts";
5
+ import { buildOverviewVM } from "../../vm/overview.ts";
6
+ import { OverviewLayout, OverviewDaySessions, OverviewSessionsLink } from "../../overviewFlow.tsx";
7
+ import { Disclosure } from "../../controls/Disclosure.tsx";
8
+ import { ChartSeg, Columns, CostCell, Delta, Empty, Kpi, Legend, MetricSeg, Rank, SessionMix, SplitSeg, tip, useCostFlags, useCostKinds, type Side } from "../../ui.tsx";
9
+
10
+ export function Overview({ F, list }: { F: Fact[]; list: string[] }) {
11
+ const { M, S, filter, t, L, f, set } = useDash();
12
+ const { now, prev, rereadNow, rereadPrev, series, slots, hourly, split, groupIds, rows, sums, activeCols: active, projects: ranked, top } = buildOverviewVM(M, F, list, filter, S);
13
+ const delta = (a: number, b: number | undefined, side: Side, points = false) => <Delta a={a} b={prev ? b : undefined} side={side} points={points} />;
14
+ const tile = (label: string, value: ReactNode, d: ReactNode, series: number[]) => <Kpi label={label} value={value} d={d} series={series} />;
15
+ const metric = S.metric, flags = useCostFlags(F);
16
+ const kinds = useCostKinds();
17
+ const groups = groupIds.map((id) => {
18
+ switch (split) {
19
+ case "kind": return kinds[id];
20
+ case "model": return { n: M.models[id], c: M.mColor(id) };
21
+ case "provider": return { n: M.providers[id], c: M.prColor(id) };
22
+ case "project": return id < 0 ? { n: t("other"), c: "c0" } : { n: M.projects[id], c: M.pColor(id) };
23
+ case "effort": return { n: M.efforts[id], c: "v" + id };
24
+ }
25
+ });
26
+ const activeCols = active.map((k) => ({ g: groups[k], k }));
27
+ const projects = ranked.map(({ p, v }) => ({ n: M.projects[p], v, onClick: () => set({ project: M.projects[p] }) }));
28
+ return (
29
+ <div className="stack">
30
+ <OverviewLayout kpiClass="grid kpis overview-kpis"
31
+ primary={<>
32
+ {tile(t("out"), f.cmp(now.o), delta(now.o, prev?.o, "none"), series.out)}
33
+ {tile(t("usageCost"), <span className="overview-money">{f.usd(now.c)}{flags.length ? <sup className="flag" tabIndex={0} data-tip={tip(t("costNotes"), flags)}>*</sup> : null}</span>, delta(now.c, prev?.c, "none"), series.cost)}
34
+ {tile(t("sess"), f.int(now.n), delta(now.n, prev?.n, "none"), series.sessions)}
35
+ </>}
36
+ secondary={<>
37
+ {tile(t("cache"), f.pct(now.hit), delta(now.hit, prev?.hit, "up", true), series.hit)}
38
+ {tile(t("saved"), <span className="overview-money" tabIndex={0} data-tip={tip(t("savedTip"), [])}>{f.usd(now.sv)}</span>, <>{L.reread(f.pct(rereadNow))} <Delta a={rereadNow} b={rereadPrev} side="down" points bare /></>, series.saved)}
39
+ {tile(t("speed"), <>{f.int(now.tps)} <small>{t("tps")}</small></>, delta(now.tps, prev?.tps, "up"), series.speed)}
40
+ </>}
41
+ trend={(
42
+ <section className="card">
43
+ <header><h2>{hourly ? t("perHour") : t("perDay")}</h2><div className="segs"><MetricSeg /><SplitSeg split={split} /><ChartSeg /></div></header>
44
+ <Legend groups={groups} sums={sums} metric={metric} />
45
+ <Columns list={slots} rows={rows} groups={groups} metric={metric} onSlot={!hourly ? (slot) => set({ range: "", from: String(slot), to: String(slot) }) : undefined} />
46
+ <OverviewDaySessions F={F} />
47
+ <Disclosure label={hourly ? t("hourlyTable") : t("dailyTable")}>
48
+ <div className="scroll"><table>
49
+ <thead><tr><th>{hourly ? t("hour") : t("day")}</th>{activeCols.map(({ g }) => <th key={g.n} className="num">{g.n}</th>)}{activeCols.length > 1 ? <th className="num">{t("total")}</th> : null}</tr></thead>
50
+ <tbody>{slots.map((d, i) => (
51
+ <tr key={d}><th>{hourly ? d + ":00" : f.dshort(d as string)}</th>{activeCols.map(({ k }) => <td key={k} className="num">{rows[i][k] ? f.fmt(metric, rows[i][k]) : "—"}</td>)}{activeCols.length > 1 ? <td className="num name">{f.fmt(metric, activeCols.reduce((a, { k }) => a + rows[i][k], 0))}</td> : null}</tr>
52
+ ))}</tbody>
53
+ </table></div>
54
+ </Disclosure>
55
+ </section>
56
+ )}
57
+ />
58
+ <div className="grid two">
59
+ <section className="card">
60
+ <header><h2>{t("topSessions")}</h2><OverviewSessionsLink /></header>
61
+ <div className="scroll"><table>
62
+ <thead><tr><th className="o1">{t("project")}</th><th>{t("title")}</th><th className="o1">{t("start")}</th><th className="num o1">{t("dur")}</th><th className="o2">{t("model")}</th><th className="num">{t("out")}</th><th className="num">{t("cost")}</th></tr></thead>
63
+ <tbody>{top.length ? top.map((r) => (
64
+ <tr key={r.s}>
65
+ <th className="name o1">{M.projects[r.x.p]}</th>
66
+ <td><SessionLink session={r.x} /><span className="sub ph">{M.projects[r.x.p]} · {f.dur(r.x.dur)}</span></td>
67
+ <td className="o1">{f.startText(r.x)}</td>
68
+ <td className="num o1">{f.dur(r.x.dur)}</td>
69
+ <td className="o2">{M.models[r.x.parent] ?? ""}<SessionMix r={r} /></td>
70
+ <td className="num">{f.cmp(r.o)}</td>
71
+ <td className="num"><CostCell r={r} /></td>
72
+ </tr>
73
+ )) : <tr><td colSpan={7} className="empty">{t("nodata")}</td></tr>}</tbody>
74
+ </table></div>
75
+ </section>
76
+ <section className="card"><header><h2>{t("topProjects")}</h2><button className="link" onClick={() => set({ view: "projects" })}>{t("seeAll")} →</button></header>{projects.length ? <Rank rows={projects} metric={metric} /> : <Empty />}</section>
77
+ </div>
78
+ </div>
79
+ );
80
+ }
@@ -0,0 +1,98 @@
1
+ import { useLayoutEffect, useRef, useState, type ReactNode } from "react";
2
+ import { BreakdownSessions, ProjectComparison } from "../Breakdown.tsx";
3
+ import { useDash } from "../../dash.ts";
4
+ import { type Fact } from "../../model.ts";
5
+ import { buildProjectsVM, projectValues, type ProjectColumn, type ProjectOrder, type ProjectSum, type ProjectsVM } from "../../vm/projects.ts";
6
+ import { projectMapHeight, squarify } from "../../treemap.ts";
7
+ import { Mag, MetricSeg, Swatch, tip } from "../../ui.tsx";
8
+
9
+ // Projects side by side: the share each takes of the chosen metric and how they compare.
10
+ // The project filter does not hide the others here; picking a project opens it in the overview.
11
+ export function Projects({ F }: { F: Fact[] }) {
12
+ const { M, S, t, f, set, filter } = useDash();
13
+ const [order, setOrder] = useState<ProjectOrder>({ k: "cost", dir: -1 });
14
+ const open = (p: number) => set({ view: "overview", project: M.projects[p] });
15
+
16
+ const { rows, all, most, items, total } = buildProjectsVM(M, F, S.metric, order);
17
+
18
+ type Col = { k: ProjectColumn; label: string; cls: string; get: (r: ProjectSum) => number | string; show: (v: number) => ReactNode };
19
+ const columns: Col[] = [
20
+ { k: "last", label: t("lastActive"), cls: "o1", get: projectValues.last, show: () => null },
21
+ { k: "sess", label: t("sess"), cls: "num", get: projectValues.sess, show: f.int },
22
+ { k: "cost", label: t("cost"), cls: "num", get: projectValues.cost, show: f.usd },
23
+ { k: "cps", label: t("costPerSess"), cls: "num o1", get: projectValues.cps, show: f.usd },
24
+ { k: "dur", label: t("avgDur"), cls: "num o2", get: projectValues.dur, show: f.dur },
25
+ { k: "turns", label: t("turnsPerSess"), cls: "num o2", get: projectValues.turns, show: f.dec },
26
+ { k: "compact", label: t("compactPerSess"), cls: "num o2", get: projectValues.compact, show: f.dec },
27
+ { k: "hit", label: t("cache"), cls: "num o2", get: projectValues.hit, show: f.pct },
28
+ { k: "kloc", label: t("costPerKLines"), cls: "num o2", get: projectValues.kloc, show: f.usd },
29
+ ];
30
+ const sortBy = (k: ProjectOrder["k"]) => setOrder(order.k === k ? { k, dir: order.dir === 1 ? -1 : 1 } : { k, dir: k === "project" ? 1 : -1 });
31
+ const th = (k: ProjectOrder["k"], label: string, cls: string) => (
32
+ <th key={k} className={cls} aria-sort={order.k === k ? (order.dir > 0 ? "ascending" : "descending") : undefined}>
33
+ <button onClick={() => sortBy(k)}>{label}{order.k === k ? (order.dir > 0 ? "\u00a0↑" : "\u00a0↓") : ""}</button>
34
+ </th>
35
+ );
36
+ const bar = (c: Col, r: ProjectSum) => (c.k === "sess" || c.k === "cost" ? <Mag v={c.get(r) as number} max={most[c.k]}>{cell(c, r)}</Mag> : cell(c, r));
37
+ const cell = (c: Col, r: ProjectSum) => {
38
+ if (c.k === "last") return r.last ? f.startText(r.last) : "—";
39
+ const v = c.get(r) as number;
40
+ return Number.isFinite(v) ? c.show(v) : "—";
41
+ };
42
+
43
+ return (
44
+ <div className="stack">
45
+ <section className="card">
46
+ <header><h2>{t("shareBy")}</h2><MetricSeg /></header>
47
+ <ProjectComparison />
48
+ <Treemap items={items} total={total} open={open} />
49
+ </section>
50
+ <section className="card">
51
+ <div className="scroll"><table className="compare">
52
+ <thead><tr>{th("project", t("project"), "")}{columns.map((c) => th(c.k, c.label, c.cls))}</tr></thead>
53
+ <tbody>
54
+ {rows.length ? rows.map((r) => (
55
+ <tr key={r.p} className="go" aria-current={filter.project === r.p || undefined} onClick={() => open(r.p)}>
56
+ <th className="name"><Swatch c={M.pColor(r.p)} /><button className="rowlink" onClick={(e) => { e.stopPropagation(); open(r.p); }}>{M.projects[r.p]}</button></th>
57
+ {columns.map((c) => <td key={c.k} className={c.cls}>{c.k === "sess"
58
+ ? <BreakdownSessions project={M.projects[r.p]} count={f.int(r.n)}>{bar(c, r)}</BreakdownSessions>
59
+ : bar(c, r)}</td>)}
60
+ </tr>
61
+ )) : <tr><td colSpan={columns.length + 1} className="empty">{t("nodata")}</td></tr>}
62
+ </tbody>
63
+ {rows.length > 1 ? <tfoot><tr><th>{t("allProj")}</th>{columns.map((c) => <td key={c.k} className={c.cls}>{cell(c, all)}</td>)}</tr></tfoot> : null}
64
+ </table></div>
65
+ </section>
66
+ </div>
67
+ );
68
+ }
69
+
70
+ // Each project as a rectangle sized by the chosen metric; small ones keep their name in the tooltip.
71
+ function Treemap({ items, total, open }: Pick<ProjectsVM, "items" | "total"> & { open: (p: number) => void }) {
72
+ const { M, S, t, f } = useDash();
73
+ const box = useRef<HTMLDivElement>(null);
74
+ const [w, setW] = useState(0);
75
+ useLayoutEffect(() => {
76
+ const el = box.current!, ro = new ResizeObserver(() => setW(el.clientWidth));
77
+ ro.observe(el); setW(el.clientWidth);
78
+ return () => ro.disconnect();
79
+ }, []);
80
+ const h = projectMapHeight(w);
81
+ const rects = squarify(items.map((x) => x.v), { x: 0, y: 0, w, h });
82
+ return (
83
+ <div className="tree" ref={box} style={{ height: items.length ? h : "auto" }}>
84
+ {!items.length ? <div className="empty">{t("nodata")}</div> : w ? items.map((x, k) => {
85
+ const r = rects[k], c = M.pColor(x.p), name = M.projects[x.p], text = `${f.fmt(S.metric, x.v)} · ${f.pct(x.v / total)}`;
86
+ const label = r.w > 84 && r.h > 46;
87
+ // A tile thinner than its own border cannot be seen or clicked; the table below still lists the project.
88
+ if (r.w < 4 || r.h < 4) return null;
89
+ return (
90
+ <button key={x.p} className={`tile ${c}`} style={{ left: r.x, top: r.y, width: r.w, height: r.h, borderRadius: Math.min(10, r.w / 3, r.h / 3), padding: label ? undefined : 0 }}
91
+ aria-label={`${name}: ${text}`} data-tip={label ? undefined : tip(name, [[c, t(S.metric), text]])} onClick={() => open(x.p)}>
92
+ {label ? <><b>{name}</b><span>{text}</span></> : null}
93
+ </button>
94
+ );
95
+ }) : null}
96
+ </div>
97
+ );
98
+ }