claude-code-cost 1.3.1 → 1.4.0
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.
- package/README.md +2 -0
- package/hub-app.json +2 -1
- package/lib/app.js +227 -0
- package/lib/cli.js +661 -0
- package/lib/costs.js +1634 -0
- package/package.json +1 -1
- package/public/app.js +82 -26
- package/public/style.css +58 -0
- package/public/vendor/claude-hub-sdk.js +1 -1
- package/server.js +6 -1605
package/package.json
CHANGED
package/public/app.js
CHANGED
|
@@ -15,6 +15,7 @@ let currentProjectName = null;
|
|
|
15
15
|
let scopeProject = null;
|
|
16
16
|
let scopeProjectName = null;
|
|
17
17
|
let currentSessionId = null;
|
|
18
|
+
let currentAgentId = null;
|
|
18
19
|
let lastSelectedProject = null;
|
|
19
20
|
let lastSelectedSession = null;
|
|
20
21
|
const DEFAULT_SORT = {
|
|
@@ -255,6 +256,7 @@ function getUrlState() {
|
|
|
255
256
|
project: p.get('project'),
|
|
256
257
|
projectName: p.get('projectName'),
|
|
257
258
|
session: p.get('session'),
|
|
259
|
+
agent: p.get('agent'),
|
|
258
260
|
};
|
|
259
261
|
}
|
|
260
262
|
|
|
@@ -435,6 +437,7 @@ function updateUrl() {
|
|
|
435
437
|
if (currentProjectPath) p.set('project', currentProjectPath);
|
|
436
438
|
if (currentProjectName) p.set('projectName', currentProjectName);
|
|
437
439
|
if (currentSessionId) p.set('session', currentSessionId);
|
|
440
|
+
if (currentAgentId) p.set('agent', currentAgentId);
|
|
438
441
|
const qs = p.toString();
|
|
439
442
|
history.replaceState(null, '', qs ? `?${qs}` : '/');
|
|
440
443
|
sessionStorage.setItem('cc-cost:nav', qs);
|
|
@@ -568,8 +571,12 @@ async function fetchSessions(encodedPath) {
|
|
|
568
571
|
sessionsModelDistribution = data.modelDistribution || [];
|
|
569
572
|
}
|
|
570
573
|
|
|
571
|
-
async function fetchSessionDetail(sessionId) {
|
|
572
|
-
|
|
574
|
+
async function fetchSessionDetail(sessionId, agentId) {
|
|
575
|
+
const agentPath = agentId ? `/agents/${encodeURIComponent(agentId)}` : '';
|
|
576
|
+
sessionDetailData = await fetchJSON(
|
|
577
|
+
`/api/sessions/${encodeURIComponent(sessionId)}${agentPath}?${rangeParams()}`,
|
|
578
|
+
true,
|
|
579
|
+
);
|
|
573
580
|
// Populate project context when opening via deep link (no project in URL)
|
|
574
581
|
if (sessionDetailData && !currentProjectPath) {
|
|
575
582
|
currentProjectPath = sessionDetailData.encodedProjectPath;
|
|
@@ -1095,9 +1102,16 @@ function renderDetail() {
|
|
|
1095
1102
|
<span class="sep">/</span>
|
|
1096
1103
|
<a onclick="navigateToSessions('${escAttrJs(d.encodedProjectPath)}', '${escAttrJs(d.projectPath)}')">${esc(d.projectPath)}</a>
|
|
1097
1104
|
<span class="sep">/</span>
|
|
1098
|
-
|
|
1105
|
+
${
|
|
1106
|
+
d.agentId
|
|
1107
|
+
? `<a onclick="navigateToDetail('${escAttrJs(d.sessionId)}')">${esc(d.sessionTitle)}</a>
|
|
1108
|
+
<span class="sep">/</span>
|
|
1109
|
+
<span class="current">${esc(d.description || d.agentId)}</span>`
|
|
1110
|
+
: `<span class="current">${esc(d.customTitle || d.firstPrompt || d.sessionId)}</span>`
|
|
1111
|
+
}
|
|
1099
1112
|
</div>
|
|
1100
1113
|
|
|
1114
|
+
${d.agentId ? `<div class="detail-agent-type"><span class="subagent-tag">${esc(d.agentType)}</span> ${esc(d.agentId)}</div>` : ''}
|
|
1101
1115
|
${d.firstPrompt ? `<div style="color:var(--text-tertiary);font-size:12px;margin-bottom:16px;font-style:italic">"${esc(d.firstPrompt)}"</div>` : ''}
|
|
1102
1116
|
|
|
1103
1117
|
<div class="detail-header">
|
|
@@ -1169,8 +1183,9 @@ function renderDetail() {
|
|
|
1169
1183
|
requestAnimationFrame(() => {
|
|
1170
1184
|
destroyChart('cumulative');
|
|
1171
1185
|
destroyChart('tokenBreakdown');
|
|
1172
|
-
|
|
1173
|
-
|
|
1186
|
+
const range = detailRangeFor(d);
|
|
1187
|
+
applyDetailRange(d.messages, range);
|
|
1188
|
+
initRangeBrush(d.messages, d.workflows, range);
|
|
1174
1189
|
});
|
|
1175
1190
|
}
|
|
1176
1191
|
|
|
@@ -1276,9 +1291,12 @@ function buildMessageRow(m) {
|
|
|
1276
1291
|
const sa = m._subagent;
|
|
1277
1292
|
const toolTags = !sa && m.tools ? m.tools.map((t) => `<span class="tool-tag">${esc(t)}</span>`).join(' ') : '';
|
|
1278
1293
|
const modelCol = sa
|
|
1279
|
-
? `<span class="model-badge">${esc(shortModel(m.model))}</span> <span class="subagent-tag">${esc(sa.workflowId ? 'workflow' : sa.agentType)}</span>`
|
|
1294
|
+
? `<span class="model-badge">${esc(shortModel(m.model))}</span> <span class="subagent-tag">${esc(sa.workflowId ? 'workflow' : sa.agentType)}</span>${sa.description ? ` <span class="subagent-desc">${esc(sa.description)}</span>` : ''}`
|
|
1280
1295
|
: `<span class="model-badge">${esc(shortModel(m.model))}</span>${toolTags ? ` ${toolTags}` : ''}`;
|
|
1281
|
-
|
|
1296
|
+
const rowAttrs = sa
|
|
1297
|
+
? ` class="subagent-row" data-clickable onclick="navigateToAgent('${escAttrJs(sa.agentId)}')" title="${esc(`${sa.messageCount} turns · open subagent`)}"`
|
|
1298
|
+
: '';
|
|
1299
|
+
return `<tr${rowAttrs}>
|
|
1282
1300
|
<td class="muted">${m.index}</td>
|
|
1283
1301
|
<td class="muted">${localTime(m.timestamp)}</td>
|
|
1284
1302
|
<td>${modelCol}</td>
|
|
@@ -1700,7 +1718,8 @@ function renderTokenBreakdownChart(messages) {
|
|
|
1700
1718
|
const DETAIL_WINDOW_PRESETS = [25, 50, 100, 200];
|
|
1701
1719
|
const DETAIL_WINDOW = DETAIL_WINDOW_PRESETS[0];
|
|
1702
1720
|
const DETAIL_WINDOW_MIN = 10;
|
|
1703
|
-
|
|
1721
|
+
// Keyed by session or session/agent, so backing out of a subagent returns to the same window.
|
|
1722
|
+
const detailRanges = new Map();
|
|
1704
1723
|
let detailSlice = [];
|
|
1705
1724
|
|
|
1706
1725
|
function median(sorted) {
|
|
@@ -1713,6 +1732,14 @@ function median(sorted) {
|
|
|
1713
1732
|
// surface their own outliers; the absolute floor drops noise in tiny sessions.
|
|
1714
1733
|
const CACHE_BURST_RATIO = 4;
|
|
1715
1734
|
const CACHE_BURST_MIN_TOKENS = 20000;
|
|
1735
|
+
const SUBAGENT_COSTLY_USD = 1;
|
|
1736
|
+
const MARKER_KINDS = {
|
|
1737
|
+
burst: { cls: '', label: 'cache burst' },
|
|
1738
|
+
compaction: { cls: 'compact', label: 'compaction' },
|
|
1739
|
+
agent: { cls: 'agent', label: 'subagent' },
|
|
1740
|
+
costlyAgent: { cls: 'agent costly', label: 'subagent', suffix: ` ≥ ${formatCost(SUBAGENT_COSTLY_USD)}` },
|
|
1741
|
+
workflow: { cls: 'workflow', label: 'workflow' },
|
|
1742
|
+
};
|
|
1716
1743
|
|
|
1717
1744
|
// A compaction explains the burst on the turn that follows it, so that turn is labelled
|
|
1718
1745
|
// `compaction`; `burst` is left for the re-caches with no boundary behind them.
|
|
@@ -1728,7 +1755,12 @@ function notableEvents(messages) {
|
|
|
1728
1755
|
const tip = `#${m.index} · ${c.trigger} compaction ${formatTokens(c.preTokens)} → ${formatTokens(c.postTokens)} · ~${formatCost(c.estCost)}`;
|
|
1729
1756
|
return [{ i, kind: 'compaction', tip }];
|
|
1730
1757
|
}
|
|
1731
|
-
|
|
1758
|
+
const sa = m._subagent;
|
|
1759
|
+
if (sa && !sa.workflowId) {
|
|
1760
|
+
const tip = `#${m.index} · ${sa.agentType}${sa.description ? ` · ${sa.description}` : ''} · ${formatCost(m.cost)}`;
|
|
1761
|
+
return [{ i, kind: m.cost >= SUBAGENT_COSTLY_USD ? 'costlyAgent' : 'agent', tip }];
|
|
1762
|
+
}
|
|
1763
|
+
if (!sa && m.cacheCreationTokens >= threshold) {
|
|
1732
1764
|
return [{ i, kind: 'burst', tip: `#${m.index} · cache write ${formatTokens(m.cacheCreationTokens)}` }];
|
|
1733
1765
|
}
|
|
1734
1766
|
return [];
|
|
@@ -1742,12 +1774,14 @@ function clampRange(start, end, total) {
|
|
|
1742
1774
|
|
|
1743
1775
|
function detailRangeFor(d) {
|
|
1744
1776
|
const total = d.messages.length;
|
|
1745
|
-
const
|
|
1777
|
+
const key = d.agentId ? `${d.sessionId}/${d.agentId}` : d.sessionId;
|
|
1778
|
+
const prev = detailRanges.get(key) || null;
|
|
1746
1779
|
const size = prev ? prev.end - prev.start : DETAIL_WINDOW;
|
|
1747
1780
|
const atTail = !prev || prev.end === prev.total;
|
|
1748
1781
|
const start = atTail ? total - size : prev.start;
|
|
1749
|
-
|
|
1750
|
-
|
|
1782
|
+
const range = { total, ...clampRange(start, start + size, total) };
|
|
1783
|
+
detailRanges.set(key, range);
|
|
1784
|
+
return range;
|
|
1751
1785
|
}
|
|
1752
1786
|
|
|
1753
1787
|
function applyDetailRange(messages, range) {
|
|
@@ -1799,11 +1833,10 @@ function rangeBrushHtml(total) {
|
|
|
1799
1833
|
const rangeOverviewSvg =
|
|
1800
1834
|
'<svg class="rb-overview" viewBox="0 0 1000 100" preserveAspectRatio="none" aria-hidden="true"><polygon /><polyline /></svg>';
|
|
1801
1835
|
|
|
1802
|
-
function initRangeBrush(messages, workflows) {
|
|
1836
|
+
function initRangeBrush(messages, workflows, range) {
|
|
1803
1837
|
const root = document.getElementById('rangeBrush');
|
|
1804
1838
|
if (!root) return;
|
|
1805
1839
|
const total = messages.length;
|
|
1806
|
-
const range = detailRange;
|
|
1807
1840
|
const q = (sel) => root.querySelector(sel);
|
|
1808
1841
|
const track = q('.rb-track');
|
|
1809
1842
|
const win = q('.rb-window');
|
|
@@ -1831,17 +1864,18 @@ function initRangeBrush(messages, workflows) {
|
|
|
1831
1864
|
burstsEl.innerHTML = markers
|
|
1832
1865
|
.map(
|
|
1833
1866
|
(k) =>
|
|
1834
|
-
`<button type="button" class="rb-burst${k.kind
|
|
1867
|
+
`<button type="button" class="rb-burst ${esc(MARKER_KINDS[k.kind].cls)}" data-i="${esc(String(k.i))}" style="left:${((k.i + 0.5) / total) * 100}%" title="${esc(k.tip)}" aria-label="${esc(k.tip)}"></button>`,
|
|
1835
1868
|
)
|
|
1836
1869
|
.join('');
|
|
1837
1870
|
const burstEls = [...burstsEl.children].map((el, k) => ({ el, i: markers[k].i }));
|
|
1838
|
-
const
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
[
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1871
|
+
const tally = { workflow: runs.length };
|
|
1872
|
+
for (const k of markers) tally[k.kind] = (tally[k.kind] || 0) + 1;
|
|
1873
|
+
const counts = Object.entries(MARKER_KINDS)
|
|
1874
|
+
.filter(([kind]) => tally[kind])
|
|
1875
|
+
.map(([kind, { cls, label, suffix = '' }]) => {
|
|
1876
|
+
const n = tally[kind];
|
|
1877
|
+
return `<span class="${esc(cls)}"><i></i>${n} ${label}${n === 1 ? '' : 's'}${suffix}</span>`;
|
|
1878
|
+
});
|
|
1845
1879
|
if (counts.length) {
|
|
1846
1880
|
burstCount.hidden = false;
|
|
1847
1881
|
burstCount.innerHTML = counts.join('<span class="rb-sep">·</span>');
|
|
@@ -2498,11 +2532,18 @@ function showView(viewId) {
|
|
|
2498
2532
|
if (el) el.classList.add('active');
|
|
2499
2533
|
}
|
|
2500
2534
|
|
|
2535
|
+
function agentFor(view, agent) {
|
|
2536
|
+
return view === 'detail' ? agent || null : null;
|
|
2537
|
+
}
|
|
2538
|
+
|
|
2501
2539
|
async function navigate(view, params) {
|
|
2502
2540
|
currentView = view;
|
|
2503
2541
|
if (params?.project) currentProjectPath = params.project;
|
|
2504
2542
|
if (params?.projectName) currentProjectName = params.projectName;
|
|
2505
2543
|
if (params?.session) currentSessionId = params.session;
|
|
2544
|
+
const nextAgent = agentFor(view, params?.agent);
|
|
2545
|
+
if (nextAgent !== currentAgentId) sessionDetailData = null;
|
|
2546
|
+
currentAgentId = nextAgent;
|
|
2506
2547
|
|
|
2507
2548
|
// Clear downstream state
|
|
2508
2549
|
if (view === 'overview' || view === 'insights') {
|
|
@@ -2534,6 +2575,10 @@ async function navigateToDetail(sessionId) {
|
|
|
2534
2575
|
await navigate('detail', { session: sessionId });
|
|
2535
2576
|
}
|
|
2536
2577
|
|
|
2578
|
+
async function navigateToAgent(agentId, sessionId = currentSessionId) {
|
|
2579
|
+
await navigate('detail', { session: sessionId, agent: agentId });
|
|
2580
|
+
}
|
|
2581
|
+
|
|
2537
2582
|
// biome-ignore lint/correctness/noUnusedVariables: called from HTML onclick
|
|
2538
2583
|
function sortBy(field) {
|
|
2539
2584
|
const s = currentSort();
|
|
@@ -2677,7 +2722,7 @@ async function loadAndRender(view) {
|
|
|
2677
2722
|
lastRenderHash.detail = null;
|
|
2678
2723
|
renderDetail();
|
|
2679
2724
|
}
|
|
2680
|
-
await fetchSessionDetail(currentSessionId);
|
|
2725
|
+
await fetchSessionDetail(currentSessionId, currentAgentId);
|
|
2681
2726
|
if (myNav !== navCounter) return;
|
|
2682
2727
|
renderDetail();
|
|
2683
2728
|
}
|
|
@@ -2692,8 +2737,13 @@ async function loadAndRender(view) {
|
|
|
2692
2737
|
// stale cursor, not a broken app: drop it and land on the overview.
|
|
2693
2738
|
if (err.status === 404 && view === 'detail') {
|
|
2694
2739
|
sessionDetailData = null;
|
|
2695
|
-
|
|
2696
|
-
|
|
2740
|
+
if (currentAgentId) {
|
|
2741
|
+
showToast('Subagent not found — showing session');
|
|
2742
|
+
await navigateToDetail(currentSessionId);
|
|
2743
|
+
} else {
|
|
2744
|
+
showToast('Not in this config dir — showing overview');
|
|
2745
|
+
await navigate('overview');
|
|
2746
|
+
}
|
|
2697
2747
|
return;
|
|
2698
2748
|
}
|
|
2699
2749
|
showToast(`Error: ${err.message}`);
|
|
@@ -3222,6 +3272,10 @@ function activateSelectedRow() {
|
|
|
3222
3272
|
|
|
3223
3273
|
async function goBack() {
|
|
3224
3274
|
let target;
|
|
3275
|
+
if (currentView === 'detail' && currentAgentId) {
|
|
3276
|
+
await navigateToDetail(currentSessionId);
|
|
3277
|
+
return;
|
|
3278
|
+
}
|
|
3225
3279
|
if (currentView === 'detail') {
|
|
3226
3280
|
target = currentProjectPath ? 'sessions' : 'overview';
|
|
3227
3281
|
} else if (currentView === 'sessions' || currentView === 'insights') {
|
|
@@ -3427,6 +3481,7 @@ hub.subscribe('project.changed', (p) => {
|
|
|
3427
3481
|
});
|
|
3428
3482
|
|
|
3429
3483
|
hub.handle('session.cost', (p) => p.session && navigateToDetail(p.session));
|
|
3484
|
+
hub.handle('agent.cost', (p) => navigateToAgent(p.agent, p.session));
|
|
3430
3485
|
|
|
3431
3486
|
(function initHubTheme() {
|
|
3432
3487
|
const getTheme = () => (document.body.classList.contains('light') ? 'light' : 'dark');
|
|
@@ -3491,7 +3546,7 @@ document.addEventListener('DOMContentLoaded', async () => {
|
|
|
3491
3546
|
currentSessionId = state.session;
|
|
3492
3547
|
currentProjectPath = state.project;
|
|
3493
3548
|
currentProjectName = state.projectName;
|
|
3494
|
-
await navigate('detail', { session: state.session });
|
|
3549
|
+
await navigate('detail', { session: state.session, agent: state.agent });
|
|
3495
3550
|
} else if (state.project) {
|
|
3496
3551
|
currentProjectPath = state.project;
|
|
3497
3552
|
currentProjectName = state.projectName;
|
|
@@ -3516,6 +3571,7 @@ window.addEventListener('popstate', () => {
|
|
|
3516
3571
|
currentProjectPath = state.project;
|
|
3517
3572
|
currentProjectName = state.projectName;
|
|
3518
3573
|
currentSessionId = state.session;
|
|
3574
|
+
currentAgentId = agentFor(state.view, state.agent);
|
|
3519
3575
|
lastRenderHash = {};
|
|
3520
3576
|
loadAndRender(currentView);
|
|
3521
3577
|
});
|
package/public/style.css
CHANGED
|
@@ -1018,6 +1018,39 @@ body {
|
|
|
1018
1018
|
background: var(--compact);
|
|
1019
1019
|
}
|
|
1020
1020
|
|
|
1021
|
+
.rb-burst.agent::before,
|
|
1022
|
+
.rb-burst.agent::after {
|
|
1023
|
+
background: var(--text-tertiary);
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
.rb-burst.agent::after {
|
|
1027
|
+
top: auto;
|
|
1028
|
+
bottom: 3px;
|
|
1029
|
+
border-radius: 1px;
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
.rb-burst.costly::before,
|
|
1033
|
+
.rb-burst.costly::after {
|
|
1034
|
+
background: var(--error);
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
.rb-burst-count .agent {
|
|
1038
|
+
color: var(--text-tertiary);
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
.rb-burst-count .agent i {
|
|
1042
|
+
border-radius: 1px;
|
|
1043
|
+
background: var(--text-tertiary);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
.rb-burst-count .costly {
|
|
1047
|
+
color: var(--error);
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
.rb-burst-count .costly i {
|
|
1051
|
+
background: var(--error);
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1021
1054
|
/* The shade outside the window is the window's own shadow, clipped by the track. */
|
|
1022
1055
|
.rb-window {
|
|
1023
1056
|
position: absolute;
|
|
@@ -1328,6 +1361,31 @@ body.light .rb-window {
|
|
|
1328
1361
|
background: var(--bg-hover);
|
|
1329
1362
|
}
|
|
1330
1363
|
|
|
1364
|
+
.subagent-desc {
|
|
1365
|
+
font-size: 11px;
|
|
1366
|
+
color: var(--text-secondary);
|
|
1367
|
+
margin-left: 4px;
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
.subagent-row {
|
|
1371
|
+
cursor: pointer;
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
.subagent-row:hover .subagent-desc {
|
|
1375
|
+
color: var(--accent-text);
|
|
1376
|
+
text-decoration: underline;
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
.detail-agent-type {
|
|
1380
|
+
color: var(--text-tertiary);
|
|
1381
|
+
font-size: 12px;
|
|
1382
|
+
margin-bottom: 16px;
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
.detail-agent-type .subagent-tag {
|
|
1386
|
+
margin-left: 0;
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1331
1389
|
.tool-tag {
|
|
1332
1390
|
text-transform: lowercase;
|
|
1333
1391
|
color: var(--chart-2, #60a5fa);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// claude-hub-sdk 1.
|
|
1
|
+
// claude-hub-sdk 1.5.0 (sha256 1505d3d5f8ba). Copied by npm run sdk:sync in claude-code-hub. Do not edit.
|
|
2
2
|
// Claude Code Hub SDK: the one rule that names a key press, shared by the hub page, client.js and
|
|
3
3
|
// stub.js. The hub serves it to its page as /sdk/keys.js; mount() and sdk:sync put it in front of
|
|
4
4
|
// client.js and stub.js, so an app still loads one file.
|