claude-code-cost 0.1.0 → 0.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/package.json +1 -1
- package/public/app.js +139 -43
- package/public/index.html +2 -2
- package/server.js +67 -36
package/package.json
CHANGED
package/public/app.js
CHANGED
|
@@ -4,13 +4,18 @@ let currentView = 'overview';
|
|
|
4
4
|
let overviewData = null;
|
|
5
5
|
let projectsData = null;
|
|
6
6
|
let sessionsData = null;
|
|
7
|
+
let sessionsDaily = null;
|
|
8
|
+
let sessionsModelDistribution = null;
|
|
7
9
|
let sessionDetailData = null;
|
|
8
10
|
let currentProjectPath = null;
|
|
9
11
|
let currentProjectName = null;
|
|
10
12
|
let currentSessionId = null;
|
|
13
|
+
let parentView = 'projects';
|
|
14
|
+
let lastSelectedProject = null;
|
|
15
|
+
let lastSelectedSession = null;
|
|
11
16
|
let sortField = 'totalCost';
|
|
12
17
|
let sortOrder = 'desc';
|
|
13
|
-
let dateRange =
|
|
18
|
+
let dateRange = 3;
|
|
14
19
|
const charts = {};
|
|
15
20
|
let lastRenderHash = {};
|
|
16
21
|
let navCounter = 0;
|
|
@@ -89,6 +94,28 @@ function thClass(field) {
|
|
|
89
94
|
return sortField === field ? 'sorted' : '';
|
|
90
95
|
}
|
|
91
96
|
|
|
97
|
+
function parentBreadcrumb() {
|
|
98
|
+
const label = parentView === 'overview' ? 'Overview' : 'Projects';
|
|
99
|
+
return `<a class="parent-breadcrumb">${label}</a>`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function focusPreviousRow(view) {
|
|
103
|
+
let selector;
|
|
104
|
+
if (view === 'overview' || view === 'projects') {
|
|
105
|
+
if (!lastSelectedProject) return;
|
|
106
|
+
selector = `tr[onclick*="'${lastSelectedProject}'"]`;
|
|
107
|
+
} else if (view === 'sessions') {
|
|
108
|
+
if (!lastSelectedSession) return;
|
|
109
|
+
selector = `tr[onclick*="'${lastSelectedSession}'"]`;
|
|
110
|
+
}
|
|
111
|
+
if (!selector) return;
|
|
112
|
+
requestAnimationFrame(() => {
|
|
113
|
+
const rows = getVisibleRows();
|
|
114
|
+
const idx = rows.findIndex(r => r.matches(selector));
|
|
115
|
+
selectRow(idx >= 0 ? idx : 0);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
92
119
|
// #endregion
|
|
93
120
|
|
|
94
121
|
// #region URL_STATE
|
|
@@ -102,24 +129,41 @@ function getUrlState() {
|
|
|
102
129
|
session: p.get('session'),
|
|
103
130
|
sort: p.get('sort'),
|
|
104
131
|
order: p.get('order'),
|
|
132
|
+
parentView: p.get('parentView') || 'projects',
|
|
105
133
|
};
|
|
106
134
|
}
|
|
107
135
|
|
|
108
136
|
function loadDateRange() {
|
|
109
137
|
const v = localStorage.getItem('cc-cost:range');
|
|
110
|
-
return v ? parseInt(v, 10) ||
|
|
138
|
+
return v ? parseInt(v, 10) || 3 : 3;
|
|
111
139
|
}
|
|
112
140
|
|
|
113
141
|
function saveDateRange(val) {
|
|
114
142
|
localStorage.setItem('cc-cost:range', String(val));
|
|
115
143
|
}
|
|
116
144
|
|
|
145
|
+
function loadSort(view) {
|
|
146
|
+
const raw = localStorage.getItem(`cc-cost:sort:${view}`);
|
|
147
|
+
if (raw) {
|
|
148
|
+
try {
|
|
149
|
+
const { field, order } = JSON.parse(raw);
|
|
150
|
+
if (field) sortField = field;
|
|
151
|
+
if (order) sortOrder = order;
|
|
152
|
+
} catch { /* ignore */ }
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function saveSort(view) {
|
|
157
|
+
localStorage.setItem(`cc-cost:sort:${view}`, JSON.stringify({ field: sortField, order: sortOrder }));
|
|
158
|
+
}
|
|
159
|
+
|
|
117
160
|
function updateUrl() {
|
|
118
161
|
const p = new URLSearchParams();
|
|
119
162
|
if (currentView !== 'overview') p.set('view', currentView);
|
|
120
163
|
if (currentProjectPath) p.set('project', currentProjectPath);
|
|
121
164
|
if (currentProjectName) p.set('projectName', currentProjectName);
|
|
122
165
|
if (currentSessionId) p.set('session', currentSessionId);
|
|
166
|
+
if (parentView !== 'projects') p.set('parentView', parentView);
|
|
123
167
|
if (sortField !== 'totalCost') p.set('sort', sortField);
|
|
124
168
|
if (sortOrder !== 'desc') p.set('order', sortOrder);
|
|
125
169
|
const qs = p.toString();
|
|
@@ -205,11 +249,19 @@ async function fetchProjects() {
|
|
|
205
249
|
}
|
|
206
250
|
|
|
207
251
|
async function fetchSessions(encodedPath) {
|
|
208
|
-
|
|
252
|
+
const data = await fetchJSON(`/api/projects/${encodeURIComponent(encodedPath)}/sessions?range=${dateRange}`, true);
|
|
253
|
+
sessionsData = data.sessions;
|
|
254
|
+
sessionsDaily = data.daily || [];
|
|
255
|
+
sessionsModelDistribution = data.modelDistribution || [];
|
|
209
256
|
}
|
|
210
257
|
|
|
211
258
|
async function fetchSessionDetail(sessionId) {
|
|
212
259
|
sessionDetailData = await fetchJSON(`/api/sessions/${encodeURIComponent(sessionId)}`, true);
|
|
260
|
+
// Populate project context when opening via deep link (no project in URL)
|
|
261
|
+
if (sessionDetailData && !currentProjectPath) {
|
|
262
|
+
currentProjectPath = sessionDetailData.encodedProjectPath;
|
|
263
|
+
currentProjectName = sessionDetailData.projectPath;
|
|
264
|
+
}
|
|
213
265
|
}
|
|
214
266
|
|
|
215
267
|
// #endregion
|
|
@@ -375,16 +427,14 @@ function renderSessions() {
|
|
|
375
427
|
return;
|
|
376
428
|
}
|
|
377
429
|
|
|
378
|
-
const h = JSON.stringify(sessionsData);
|
|
430
|
+
const h = JSON.stringify({ sessionsData, sessionsDaily, sessionsModelDistribution, sortField, sortOrder });
|
|
379
431
|
if (lastRenderHash.sessions === h) return;
|
|
380
432
|
lastRenderHash.sessions = h;
|
|
381
433
|
|
|
382
434
|
if (sessionsData.length === 0) {
|
|
383
435
|
el.innerHTML = `<div class="dashboard-content">
|
|
384
436
|
<div class="breadcrumb">
|
|
385
|
-
|
|
386
|
-
<span class="sep">/</span>
|
|
387
|
-
<a onclick="navigate('projects')">Projects</a>
|
|
437
|
+
${parentBreadcrumb()}
|
|
388
438
|
<span class="sep">/</span>
|
|
389
439
|
<span class="current">${esc(currentProjectName || 'Project')}</span>
|
|
390
440
|
</div>
|
|
@@ -396,28 +446,42 @@ function renderSessions() {
|
|
|
396
446
|
el.innerHTML = `
|
|
397
447
|
<div class="dashboard-content">
|
|
398
448
|
<div class="breadcrumb">
|
|
399
|
-
|
|
400
|
-
<span class="sep">/</span>
|
|
401
|
-
<a onclick="navigate('projects')">Projects</a>
|
|
449
|
+
${parentBreadcrumb()}
|
|
402
450
|
<span class="sep">/</span>
|
|
403
451
|
<span class="current">${esc(currentProjectName || 'Project')}</span>
|
|
404
452
|
</div>
|
|
453
|
+
<div class="charts-row" style="grid-template-columns:3fr 2fr auto">
|
|
454
|
+
<div class="chart-box">
|
|
455
|
+
<div class="chart-title">Daily Cost</div>
|
|
456
|
+
<canvas id="sessionsDailyChart"></canvas>
|
|
457
|
+
</div>
|
|
458
|
+
<div class="chart-box">
|
|
459
|
+
<div class="chart-title">Cost by Model</div>
|
|
460
|
+
<canvas id="sessionsModelChart"></canvas>
|
|
461
|
+
</div>
|
|
462
|
+
<div class="chart-box" style="display:flex;flex-direction:column;justify-content:center;align-items:center;min-width:120px">
|
|
463
|
+
<div class="chart-title">Total Cost</div>
|
|
464
|
+
<div style="font-size:22px;font-weight:700;color:var(--accent)">${formatCost(sessionsData.reduce((s, x) => s + x.totalCost, 0))}</div>
|
|
465
|
+
<div style="color:var(--text-tertiary);font-size:12px;margin-top:4px">${sessionsData.length} sessions</div>
|
|
466
|
+
</div>
|
|
467
|
+
</div>
|
|
468
|
+
|
|
405
469
|
<table class="data-table">
|
|
406
470
|
<thead><tr>
|
|
407
|
-
<th>Session</th>
|
|
408
|
-
<th>Cost</th>
|
|
409
|
-
<th>Tokens</th>
|
|
410
|
-
<th>Messages</th>
|
|
411
|
-
<th>Duration</th>
|
|
471
|
+
<th class="${thClass('firstPrompt')}" onclick="sortBy('firstPrompt')">Session ${sortArrow('firstPrompt')}</th>
|
|
472
|
+
<th class="${thClass('totalCost')}" onclick="sortBy('totalCost')">Cost ${sortArrow('totalCost')}</th>
|
|
473
|
+
<th class="${thClass('totalTokens')}" onclick="sortBy('totalTokens')">Tokens ${sortArrow('totalTokens')}</th>
|
|
474
|
+
<th class="${thClass('messageCount')}" onclick="sortBy('messageCount')">Messages ${sortArrow('messageCount')}</th>
|
|
475
|
+
<th class="${thClass('durationMinutes')}" onclick="sortBy('durationMinutes')">Duration ${sortArrow('durationMinutes')}</th>
|
|
412
476
|
<th>Model</th>
|
|
413
|
-
<th>Last Active</th>
|
|
477
|
+
<th class="${thClass('lastTimestamp')}" onclick="sortBy('lastTimestamp')">Last Active ${sortArrow('lastTimestamp')}</th>
|
|
414
478
|
</tr></thead>
|
|
415
479
|
<tbody>
|
|
416
|
-
${sessionsData
|
|
480
|
+
${[...sessionsData].sort((a, b) => sortCompare(a, b, sortField, sortOrder))
|
|
417
481
|
.map(
|
|
418
482
|
(s) => `
|
|
419
483
|
<tr data-clickable onclick="navigateToDetail('${esc(s.sessionId)}')">
|
|
420
|
-
<td class="truncate" title="${esc(s.firstPrompt || s.sessionId)}">${esc(s.firstPrompt ||
|
|
484
|
+
<td class="truncate" title="${esc(s.customTitle || s.firstPrompt || s.sessionId)}">${esc(s.customTitle || s.firstPrompt || s.sessionId)}</td>
|
|
421
485
|
<td class="cost-cell">${formatCost(s.totalCost)}</td>
|
|
422
486
|
<td>${formatTokens(s.totalTokens)}</td>
|
|
423
487
|
<td>${s.messageCount}</td>
|
|
@@ -430,6 +494,11 @@ function renderSessions() {
|
|
|
430
494
|
</tbody>
|
|
431
495
|
</table>
|
|
432
496
|
</div>`;
|
|
497
|
+
|
|
498
|
+
requestAnimationFrame(() => {
|
|
499
|
+
renderDailyChart(sessionsDaily || [], 'sessionsDailyChart', 'sessionsDaily');
|
|
500
|
+
renderModelChart(sessionsModelDistribution || [], 'sessionsModelChart', 'sessionsModel');
|
|
501
|
+
});
|
|
433
502
|
}
|
|
434
503
|
|
|
435
504
|
// #endregion
|
|
@@ -452,13 +521,11 @@ function renderDetail() {
|
|
|
452
521
|
el.innerHTML = `
|
|
453
522
|
<div class="dashboard-content">
|
|
454
523
|
<div class="breadcrumb">
|
|
455
|
-
|
|
456
|
-
<span class="sep">/</span>
|
|
457
|
-
<a onclick="navigate('projects')">Projects</a>
|
|
524
|
+
${parentBreadcrumb()}
|
|
458
525
|
<span class="sep">/</span>
|
|
459
526
|
<a onclick="navigateToSessions('${esc(d.encodedProjectPath)}', '${esc(d.projectPath)}')">${esc(d.projectPath)}</a>
|
|
460
527
|
<span class="sep">/</span>
|
|
461
|
-
<span class="current">${esc(d.
|
|
528
|
+
<span class="current">${esc(d.customTitle || d.firstPrompt || d.sessionId)}</span>
|
|
462
529
|
</div>
|
|
463
530
|
|
|
464
531
|
${d.firstPrompt ? `<div style="color:var(--text-tertiary);font-size:12px;margin-bottom:16px;font-style:italic">"${esc(d.firstPrompt)}"</div>` : ''}
|
|
@@ -602,15 +669,15 @@ function destroyChart(id) {
|
|
|
602
669
|
}
|
|
603
670
|
}
|
|
604
671
|
|
|
605
|
-
function renderDailyChart(daily) {
|
|
606
|
-
const canvas = document.getElementById(
|
|
672
|
+
function renderDailyChart(daily, canvasId = 'dailyChart', chartKey = 'daily') {
|
|
673
|
+
const canvas = document.getElementById(canvasId);
|
|
607
674
|
if (!canvas || !daily?.length) return;
|
|
608
|
-
destroyChart(
|
|
675
|
+
destroyChart(chartKey);
|
|
609
676
|
|
|
610
677
|
const c = getChartColors();
|
|
611
678
|
const defaults = chartDefaults();
|
|
612
679
|
|
|
613
|
-
charts
|
|
680
|
+
charts[chartKey] = new Chart(canvas, {
|
|
614
681
|
type: 'bar',
|
|
615
682
|
data: {
|
|
616
683
|
labels: daily.map((d) => shortDate(d.date)),
|
|
@@ -647,17 +714,17 @@ function renderDailyChart(daily) {
|
|
|
647
714
|
});
|
|
648
715
|
}
|
|
649
716
|
|
|
650
|
-
function renderModelChart(models) {
|
|
651
|
-
const canvas = document.getElementById(
|
|
717
|
+
function renderModelChart(models, canvasId = 'modelChart', chartKey = 'model') {
|
|
718
|
+
const canvas = document.getElementById(canvasId);
|
|
652
719
|
if (!canvas || !models?.length) return;
|
|
653
|
-
destroyChart(
|
|
720
|
+
destroyChart(chartKey);
|
|
654
721
|
|
|
655
722
|
const colors = getChartColors();
|
|
656
723
|
const palette = [colors.chart1, colors.chart2, colors.chart3, colors.chart4, colors.chart5, colors.chart6];
|
|
657
724
|
const defaults = chartDefaults();
|
|
658
725
|
const total = models.reduce((s, m) => s + m.cost, 0);
|
|
659
726
|
|
|
660
|
-
charts
|
|
727
|
+
charts[chartKey] = new Chart(canvas, {
|
|
661
728
|
type: 'bar',
|
|
662
729
|
data: {
|
|
663
730
|
labels: models.map((m) => shortModel(m.model)),
|
|
@@ -839,6 +906,7 @@ function showView(viewId) {
|
|
|
839
906
|
|
|
840
907
|
async function navigate(view, params) {
|
|
841
908
|
currentView = view;
|
|
909
|
+
loadSort(view);
|
|
842
910
|
if (params?.project) currentProjectPath = params.project;
|
|
843
911
|
if (params?.projectName) currentProjectName = params.projectName;
|
|
844
912
|
if (params?.session) currentSessionId = params.session;
|
|
@@ -853,14 +921,19 @@ async function navigate(view, params) {
|
|
|
853
921
|
currentSessionId = null;
|
|
854
922
|
}
|
|
855
923
|
|
|
856
|
-
const navView = view === 'sessions' || view === 'detail' ?
|
|
924
|
+
const navView = view === 'sessions' || view === 'detail' ? parentView : view;
|
|
857
925
|
setActiveNav(navView);
|
|
858
926
|
updateUrl();
|
|
859
927
|
await loadAndRender(view);
|
|
928
|
+
if (view !== 'detail') selectRow(0);
|
|
860
929
|
}
|
|
861
930
|
|
|
862
931
|
// biome-ignore lint/correctness/noUnusedVariables: called from HTML onclick
|
|
863
932
|
async function navigateToSessions(encodedPath, name) {
|
|
933
|
+
if (currentView === 'overview' || currentView === 'projects') {
|
|
934
|
+
parentView = currentView;
|
|
935
|
+
}
|
|
936
|
+
lastSelectedProject = encodedPath;
|
|
864
937
|
currentProjectPath = encodedPath;
|
|
865
938
|
currentProjectName = name;
|
|
866
939
|
await navigate('sessions', { project: encodedPath, projectName: name });
|
|
@@ -868,6 +941,7 @@ async function navigateToSessions(encodedPath, name) {
|
|
|
868
941
|
|
|
869
942
|
// biome-ignore lint/correctness/noUnusedVariables: called from HTML onclick
|
|
870
943
|
async function navigateToDetail(sessionId) {
|
|
944
|
+
lastSelectedSession = sessionId;
|
|
871
945
|
currentSessionId = sessionId;
|
|
872
946
|
await navigate('detail', { session: sessionId });
|
|
873
947
|
}
|
|
@@ -880,10 +954,11 @@ function sortBy(field) {
|
|
|
880
954
|
sortField = field;
|
|
881
955
|
sortOrder = 'desc';
|
|
882
956
|
}
|
|
883
|
-
lastRenderHash
|
|
884
|
-
|
|
957
|
+
lastRenderHash[currentView] = null;
|
|
958
|
+
saveSort(currentView);
|
|
885
959
|
updateUrl();
|
|
886
960
|
if (currentView === 'overview') renderOverview();
|
|
961
|
+
else if (currentView === 'sessions') renderSessions();
|
|
887
962
|
else renderProjects();
|
|
888
963
|
}
|
|
889
964
|
|
|
@@ -945,6 +1020,9 @@ async function loadAndRender(view) {
|
|
|
945
1020
|
case 'sessions':
|
|
946
1021
|
if (currentProjectPath) {
|
|
947
1022
|
sessionsData = null;
|
|
1023
|
+
sessionsDaily = null;
|
|
1024
|
+
sessionsModelDistribution = null;
|
|
1025
|
+
lastRenderHash.sessions = null;
|
|
948
1026
|
renderSessions();
|
|
949
1027
|
await fetchSessions(currentProjectPath);
|
|
950
1028
|
if (myNav !== navCounter) return;
|
|
@@ -954,6 +1032,7 @@ async function loadAndRender(view) {
|
|
|
954
1032
|
case 'detail':
|
|
955
1033
|
if (currentSessionId) {
|
|
956
1034
|
sessionDetailData = null;
|
|
1035
|
+
lastRenderHash.detail = null;
|
|
957
1036
|
renderDetail();
|
|
958
1037
|
await fetchSessionDetail(currentSessionId);
|
|
959
1038
|
if (myNav !== navCounter) return;
|
|
@@ -1030,14 +1109,24 @@ function activateSelectedRow() {
|
|
|
1030
1109
|
}
|
|
1031
1110
|
}
|
|
1032
1111
|
|
|
1033
|
-
function goBack() {
|
|
1112
|
+
async function goBack() {
|
|
1113
|
+
let target;
|
|
1034
1114
|
if (currentView === 'detail') {
|
|
1035
|
-
|
|
1115
|
+
if (currentProjectPath) {
|
|
1116
|
+
target = 'sessions';
|
|
1117
|
+
await navigate('sessions', { project: currentProjectPath, projectName: currentProjectName });
|
|
1118
|
+
} else {
|
|
1119
|
+
target = parentView;
|
|
1120
|
+
await navigate(parentView);
|
|
1121
|
+
}
|
|
1036
1122
|
} else if (currentView === 'sessions') {
|
|
1037
|
-
|
|
1123
|
+
target = parentView;
|
|
1124
|
+
await navigate(parentView);
|
|
1038
1125
|
} else if (currentView === 'projects') {
|
|
1039
|
-
|
|
1126
|
+
target = 'overview';
|
|
1127
|
+
await navigate('overview');
|
|
1040
1128
|
}
|
|
1129
|
+
if (target) focusPreviousRow(target);
|
|
1041
1130
|
}
|
|
1042
1131
|
|
|
1043
1132
|
document.addEventListener('keydown', (e) => {
|
|
@@ -1063,13 +1152,11 @@ document.addEventListener('keydown', (e) => {
|
|
|
1063
1152
|
if (e.key === '1') {
|
|
1064
1153
|
e.preventDefault();
|
|
1065
1154
|
navigate('overview');
|
|
1066
|
-
selectedRowIdx = -1;
|
|
1067
1155
|
return;
|
|
1068
1156
|
}
|
|
1069
1157
|
if (e.key === '2') {
|
|
1070
1158
|
e.preventDefault();
|
|
1071
1159
|
navigate('projects');
|
|
1072
|
-
selectedRowIdx = -1;
|
|
1073
1160
|
return;
|
|
1074
1161
|
}
|
|
1075
1162
|
|
|
@@ -1087,13 +1174,11 @@ document.addEventListener('keydown', (e) => {
|
|
|
1087
1174
|
if (e.key === 'Escape') {
|
|
1088
1175
|
e.preventDefault();
|
|
1089
1176
|
goBack();
|
|
1090
|
-
selectedRowIdx = -1;
|
|
1091
1177
|
return;
|
|
1092
1178
|
}
|
|
1093
1179
|
if (e.key === 'Backspace') {
|
|
1094
1180
|
e.preventDefault();
|
|
1095
1181
|
goBack();
|
|
1096
|
-
selectedRowIdx = -1;
|
|
1097
1182
|
return;
|
|
1098
1183
|
}
|
|
1099
1184
|
|
|
@@ -1110,10 +1195,9 @@ document.addEventListener('keydown', (e) => {
|
|
|
1110
1195
|
if (rows.length) selectRow(Math.max(selectedRowIdx - 1, 0));
|
|
1111
1196
|
return;
|
|
1112
1197
|
}
|
|
1113
|
-
if (e.key === 'Enter') {
|
|
1198
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
1114
1199
|
e.preventDefault();
|
|
1115
1200
|
activateSelectedRow();
|
|
1116
|
-
selectedRowIdx = -1;
|
|
1117
1201
|
return;
|
|
1118
1202
|
}
|
|
1119
1203
|
});
|
|
@@ -1172,6 +1256,14 @@ window.hubNavigate = function hubNavigate(app, url) {
|
|
|
1172
1256
|
|
|
1173
1257
|
loadTheme();
|
|
1174
1258
|
|
|
1259
|
+
document.addEventListener('click', async (e) => {
|
|
1260
|
+
if (e.target.matches('.parent-breadcrumb')) {
|
|
1261
|
+
const target = parentView;
|
|
1262
|
+
await navigate(target);
|
|
1263
|
+
focusPreviousRow(target);
|
|
1264
|
+
}
|
|
1265
|
+
});
|
|
1266
|
+
|
|
1175
1267
|
document.addEventListener('DOMContentLoaded', async () => {
|
|
1176
1268
|
const state = getUrlState();
|
|
1177
1269
|
dateRange = loadDateRange();
|
|
@@ -1182,10 +1274,12 @@ document.addEventListener('DOMContentLoaded', async () => {
|
|
|
1182
1274
|
|
|
1183
1275
|
if (state.session) {
|
|
1184
1276
|
currentSessionId = state.session;
|
|
1277
|
+
parentView = state.parentView;
|
|
1185
1278
|
currentProjectPath = state.project;
|
|
1186
1279
|
currentProjectName = state.projectName;
|
|
1187
1280
|
await navigate('detail', { session: state.session });
|
|
1188
1281
|
} else if (state.project) {
|
|
1282
|
+
parentView = state.parentView;
|
|
1189
1283
|
currentProjectPath = state.project;
|
|
1190
1284
|
currentProjectName = state.projectName;
|
|
1191
1285
|
await navigate('sessions', { project: state.project, projectName: state.projectName });
|
|
@@ -1199,6 +1293,8 @@ document.addEventListener('DOMContentLoaded', async () => {
|
|
|
1199
1293
|
window.addEventListener('popstate', () => {
|
|
1200
1294
|
const state = getUrlState();
|
|
1201
1295
|
currentView = state.view || 'overview';
|
|
1296
|
+
loadSort(currentView);
|
|
1297
|
+
parentView = state.parentView;
|
|
1202
1298
|
currentProjectPath = state.project;
|
|
1203
1299
|
currentProjectName = state.projectName;
|
|
1204
1300
|
currentSessionId = state.session;
|
package/public/index.html
CHANGED
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
</div>
|
|
36
36
|
<select class="topbar-select" id="rangeSelect" onchange="onRangeChange(this.value)">
|
|
37
37
|
<option value="1">1 day</option>
|
|
38
|
-
<option value="3">3 days</option>
|
|
39
|
-
<option value="7"
|
|
38
|
+
<option value="3" selected>3 days</option>
|
|
39
|
+
<option value="7">7 days</option>
|
|
40
40
|
<option value="30">30 days</option>
|
|
41
41
|
<option value="90">90 days</option>
|
|
42
42
|
<option value="365">1 year</option>
|
package/server.js
CHANGED
|
@@ -183,14 +183,6 @@ async function processJSONLFile(filePath, processLine) {
|
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
-
function parseUsageLine(line) {
|
|
187
|
-
try {
|
|
188
|
-
const data = JSON.parse(line);
|
|
189
|
-
if (!data.message?.usage?.input_tokens && data.message?.usage?.input_tokens !== 0) return null;
|
|
190
|
-
if (!data.timestamp) return null;
|
|
191
|
-
return data;
|
|
192
|
-
} catch { return null; }
|
|
193
|
-
}
|
|
194
186
|
|
|
195
187
|
function createDedupeHash(data) {
|
|
196
188
|
const mid = data.message?.id;
|
|
@@ -237,6 +229,24 @@ function localDateStr(d) {
|
|
|
237
229
|
return `${y}-${m}-${day}`;
|
|
238
230
|
}
|
|
239
231
|
|
|
232
|
+
function buildFilledDaily(dailyCosts, start, end) {
|
|
233
|
+
const filled = [];
|
|
234
|
+
if (Object.keys(dailyCosts).length > 0) {
|
|
235
|
+
for (let d = new Date(start); d <= end; d.setDate(d.getDate() + 1)) {
|
|
236
|
+
const ds = localDateStr(d);
|
|
237
|
+
filled.push({ date: ds, cost: dailyCosts[ds] || 0 });
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return filled;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function buildModelDistribution(modelCosts) {
|
|
244
|
+
return Object.entries(modelCosts)
|
|
245
|
+
.filter(([model, cost]) => cost > 0 && !model.startsWith('<'))
|
|
246
|
+
.map(([model, cost]) => ({ model, cost }))
|
|
247
|
+
.sort((a, b) => b.cost - a.cost);
|
|
248
|
+
}
|
|
249
|
+
|
|
240
250
|
function scanProjectDirs(cutoffDate) {
|
|
241
251
|
const projects = new Map();
|
|
242
252
|
const cutoffMs = cutoffDate ? cutoffDate.getTime() : 0;
|
|
@@ -311,6 +321,7 @@ async function loadProjectData(files, pricing) {
|
|
|
311
321
|
messages: [],
|
|
312
322
|
models: new Set(),
|
|
313
323
|
firstPrompt: '',
|
|
324
|
+
customTitle: null,
|
|
314
325
|
firstTimestamp: null,
|
|
315
326
|
lastTimestamp: null,
|
|
316
327
|
});
|
|
@@ -318,17 +329,25 @@ async function loadProjectData(files, pricing) {
|
|
|
318
329
|
const session = sessions.get(sessionId);
|
|
319
330
|
|
|
320
331
|
await processJSONLFile(file, async (line) => {
|
|
321
|
-
|
|
322
|
-
|
|
332
|
+
let parsed;
|
|
333
|
+
try { parsed = JSON.parse(line); } catch { return; }
|
|
334
|
+
|
|
335
|
+
if (parsed.type === 'custom-title' && parsed.customTitle) {
|
|
336
|
+
session.customTitle = parsed.customTitle;
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (!parsed.message?.usage?.input_tokens && parsed.message?.usage?.input_tokens !== 0) return;
|
|
341
|
+
if (!parsed.timestamp) return;
|
|
323
342
|
|
|
324
|
-
const hash = createDedupeHash(
|
|
343
|
+
const hash = createDedupeHash(parsed);
|
|
325
344
|
if (hash && seen.has(hash)) return;
|
|
326
345
|
if (hash) seen.add(hash);
|
|
327
346
|
|
|
328
|
-
const cost = calculateEntryCost(
|
|
329
|
-
const usage =
|
|
330
|
-
const model =
|
|
331
|
-
const ts =
|
|
347
|
+
const cost = calculateEntryCost(parsed, pricing);
|
|
348
|
+
const usage = parsed.message.usage;
|
|
349
|
+
const model = parsed.message?.model || 'unknown';
|
|
350
|
+
const ts = parsed.timestamp;
|
|
332
351
|
|
|
333
352
|
session.totalCost += cost;
|
|
334
353
|
session.inputTokens += usage.input_tokens || 0;
|
|
@@ -352,10 +371,10 @@ async function loadProjectData(files, pricing) {
|
|
|
352
371
|
});
|
|
353
372
|
|
|
354
373
|
// Capture first user prompt from the JSONL (look for human/user messages)
|
|
355
|
-
if (!session.firstPrompt &&
|
|
356
|
-
const content = Array.isArray(
|
|
357
|
-
?
|
|
358
|
-
: (typeof
|
|
374
|
+
if (!session.firstPrompt && parsed.type === 'human' && parsed.message?.content) {
|
|
375
|
+
const content = Array.isArray(parsed.message.content)
|
|
376
|
+
? parsed.message.content.map(c => c.text || '').join(' ')
|
|
377
|
+
: (typeof parsed.message.content === 'string' ? parsed.message.content : '');
|
|
359
378
|
if (content.trim()) {
|
|
360
379
|
session.firstPrompt = content.trim().slice(0, 120);
|
|
361
380
|
}
|
|
@@ -431,16 +450,9 @@ async function getOverviewData(days) {
|
|
|
431
450
|
}
|
|
432
451
|
}
|
|
433
452
|
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
const start = new Date(cutoff);
|
|
438
|
-
start.setHours(0, 0, 0, 0);
|
|
439
|
-
for (let d = new Date(start); d <= now; d.setDate(d.getDate() + 1)) {
|
|
440
|
-
const ds = localDateStr(d);
|
|
441
|
-
filledDaily.push({ date: ds, cost: dailyCosts[ds] || 0 });
|
|
442
|
-
}
|
|
443
|
-
}
|
|
453
|
+
const dailyStart = new Date(cutoff);
|
|
454
|
+
dailyStart.setHours(0, 0, 0, 0);
|
|
455
|
+
const filledDaily = buildFilledDaily(dailyCosts, dailyStart, now);
|
|
444
456
|
|
|
445
457
|
const todayStr = localDateStr(now);
|
|
446
458
|
const todayCost = dailyCosts[todayStr] || 0;
|
|
@@ -448,11 +460,7 @@ async function getOverviewData(days) {
|
|
|
448
460
|
const totalInputAll = totalInput + totalCacheCreation + totalCacheRead;
|
|
449
461
|
const cacheEfficiency = totalInputAll > 0 ? totalCacheRead / totalInputAll : 0;
|
|
450
462
|
|
|
451
|
-
|
|
452
|
-
const modelDistribution = Object.entries(modelCosts)
|
|
453
|
-
.filter(([model, cost]) => cost > 0 && !model.startsWith('<'))
|
|
454
|
-
.map(([model, cost]) => ({ model, cost }))
|
|
455
|
-
.sort((a, b) => b.cost - a.cost);
|
|
463
|
+
const modelDistribution = buildModelDistribution(modelCosts);
|
|
456
464
|
|
|
457
465
|
const result = {
|
|
458
466
|
summary: {
|
|
@@ -535,6 +543,8 @@ async function getProjectSessionsData(encodedPath, days) {
|
|
|
535
543
|
const sessions = await loadProjectData(proj.files, pricing);
|
|
536
544
|
const cutoffStr = cutoff ? cutoff.toISOString() : null;
|
|
537
545
|
const result = [];
|
|
546
|
+
const dailyCosts = {};
|
|
547
|
+
const modelCosts = {};
|
|
538
548
|
|
|
539
549
|
for (const [, session] of sessions) {
|
|
540
550
|
const msgs = cutoffStr
|
|
@@ -546,6 +556,11 @@ async function getProjectSessionsData(encodedPath, days) {
|
|
|
546
556
|
for (const m of msgs) {
|
|
547
557
|
cost += m.cost; input += m.inputTokens; output += m.outputTokens;
|
|
548
558
|
cacheCreation += m.cacheCreationTokens; cacheRead += m.cacheReadTokens;
|
|
559
|
+
const day = localDateStr(new Date(m.timestamp));
|
|
560
|
+
dailyCosts[day] = (dailyCosts[day] || 0) + m.cost;
|
|
561
|
+
if (m.model && !m.model.startsWith('<')) {
|
|
562
|
+
modelCosts[m.model] = (modelCosts[m.model] || 0) + m.cost;
|
|
563
|
+
}
|
|
549
564
|
}
|
|
550
565
|
const durationMs = session.firstTimestamp && session.lastTimestamp
|
|
551
566
|
? new Date(session.lastTimestamp) - new Date(session.firstTimestamp)
|
|
@@ -553,6 +568,7 @@ async function getProjectSessionsData(encodedPath, days) {
|
|
|
553
568
|
|
|
554
569
|
result.push({
|
|
555
570
|
sessionId: session.sessionId,
|
|
571
|
+
customTitle: session.customTitle,
|
|
556
572
|
totalCost: cost,
|
|
557
573
|
inputTokens: input,
|
|
558
574
|
outputTokens: output,
|
|
@@ -569,9 +585,23 @@ async function getProjectSessionsData(encodedPath, days) {
|
|
|
569
585
|
});
|
|
570
586
|
}
|
|
571
587
|
|
|
588
|
+
let dailyStart;
|
|
589
|
+
if (cutoff) {
|
|
590
|
+
dailyStart = new Date(cutoff);
|
|
591
|
+
dailyStart.setHours(0, 0, 0, 0);
|
|
592
|
+
} else {
|
|
593
|
+
const dates = Object.keys(dailyCosts).sort();
|
|
594
|
+
dailyStart = dates.length ? new Date(dates[0] + 'T00:00:00') : now;
|
|
595
|
+
}
|
|
596
|
+
const dailyEnd = now;
|
|
597
|
+
const filledDaily = buildFilledDaily(dailyCosts, dailyStart, dailyEnd);
|
|
598
|
+
|
|
599
|
+
const modelDistribution = buildModelDistribution(modelCosts);
|
|
600
|
+
|
|
572
601
|
const sorted = result.sort((a, b) => (b.lastTimestamp || '').localeCompare(a.lastTimestamp || ''));
|
|
573
|
-
|
|
574
|
-
|
|
602
|
+
const response = { sessions: sorted, daily: filledDaily, modelDistribution };
|
|
603
|
+
setCache(cacheKey, response);
|
|
604
|
+
return response;
|
|
575
605
|
}
|
|
576
606
|
|
|
577
607
|
async function getSessionDetailData(sessionId) {
|
|
@@ -602,6 +632,7 @@ async function getSessionDetailData(sessionId) {
|
|
|
602
632
|
|
|
603
633
|
const result = {
|
|
604
634
|
sessionId,
|
|
635
|
+
customTitle: session.customTitle,
|
|
605
636
|
projectPath: decodeProjectPath(encodedPath),
|
|
606
637
|
encodedProjectPath: encodedPath,
|
|
607
638
|
totalCost: session.totalCost,
|