claude-code-cost 0.1.0 → 0.2.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-code-cost",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Cost dashboard for Claude Code usage",
5
5
  "main": "server.js",
6
6
  "bin": {
package/public/app.js CHANGED
@@ -10,7 +10,7 @@ let currentProjectName = null;
10
10
  let currentSessionId = null;
11
11
  let sortField = 'totalCost';
12
12
  let sortOrder = 'desc';
13
- let dateRange = 7;
13
+ let dateRange = 3;
14
14
  const charts = {};
15
15
  let lastRenderHash = {};
16
16
  let navCounter = 0;
@@ -107,13 +107,28 @@ function getUrlState() {
107
107
 
108
108
  function loadDateRange() {
109
109
  const v = localStorage.getItem('cc-cost:range');
110
- return v ? parseInt(v, 10) || 7 : 7;
110
+ return v ? parseInt(v, 10) || 3 : 3;
111
111
  }
112
112
 
113
113
  function saveDateRange(val) {
114
114
  localStorage.setItem('cc-cost:range', String(val));
115
115
  }
116
116
 
117
+ function loadSort(view) {
118
+ const raw = localStorage.getItem(`cc-cost:sort:${view}`);
119
+ if (raw) {
120
+ try {
121
+ const { field, order } = JSON.parse(raw);
122
+ if (field) sortField = field;
123
+ if (order) sortOrder = order;
124
+ } catch { /* ignore */ }
125
+ }
126
+ }
127
+
128
+ function saveSort(view) {
129
+ localStorage.setItem(`cc-cost:sort:${view}`, JSON.stringify({ field: sortField, order: sortOrder }));
130
+ }
131
+
117
132
  function updateUrl() {
118
133
  const p = new URLSearchParams();
119
134
  if (currentView !== 'overview') p.set('view', currentView);
@@ -210,6 +225,11 @@ async function fetchSessions(encodedPath) {
210
225
 
211
226
  async function fetchSessionDetail(sessionId) {
212
227
  sessionDetailData = await fetchJSON(`/api/sessions/${encodeURIComponent(sessionId)}`, true);
228
+ // Populate project context when opening via deep link (no project in URL)
229
+ if (sessionDetailData && !currentProjectPath) {
230
+ currentProjectPath = sessionDetailData.encodedProjectPath;
231
+ currentProjectName = sessionDetailData.projectPath;
232
+ }
213
233
  }
214
234
 
215
235
  // #endregion
@@ -375,7 +395,7 @@ function renderSessions() {
375
395
  return;
376
396
  }
377
397
 
378
- const h = JSON.stringify(sessionsData);
398
+ const h = JSON.stringify(sessionsData) + sortField + sortOrder;
379
399
  if (lastRenderHash.sessions === h) return;
380
400
  lastRenderHash.sessions = h;
381
401
 
@@ -404,20 +424,20 @@ function renderSessions() {
404
424
  </div>
405
425
  <table class="data-table">
406
426
  <thead><tr>
407
- <th>Session</th>
408
- <th>Cost</th>
409
- <th>Tokens</th>
410
- <th>Messages</th>
411
- <th>Duration</th>
427
+ <th class="${thClass('firstPrompt')}" onclick="sortBy('firstPrompt')">Session ${sortArrow('firstPrompt')}</th>
428
+ <th class="${thClass('totalCost')}" onclick="sortBy('totalCost')">Cost ${sortArrow('totalCost')}</th>
429
+ <th class="${thClass('totalTokens')}" onclick="sortBy('totalTokens')">Tokens ${sortArrow('totalTokens')}</th>
430
+ <th class="${thClass('messageCount')}" onclick="sortBy('messageCount')">Messages ${sortArrow('messageCount')}</th>
431
+ <th class="${thClass('durationMinutes')}" onclick="sortBy('durationMinutes')">Duration ${sortArrow('durationMinutes')}</th>
412
432
  <th>Model</th>
413
- <th>Last Active</th>
433
+ <th class="${thClass('lastTimestamp')}" onclick="sortBy('lastTimestamp')">Last Active ${sortArrow('lastTimestamp')}</th>
414
434
  </tr></thead>
415
435
  <tbody>
416
- ${sessionsData
436
+ ${[...sessionsData].sort((a, b) => sortCompare(a, b, sortField, sortOrder))
417
437
  .map(
418
438
  (s) => `
419
439
  <tr data-clickable onclick="navigateToDetail('${esc(s.sessionId)}')">
420
- <td class="truncate" title="${esc(s.firstPrompt || s.sessionId)}">${esc(s.firstPrompt || `${s.sessionId.slice(0, 8)}...`)}</td>
440
+ <td class="truncate" title="${esc(s.customTitle || s.firstPrompt || s.sessionId)}">${esc(s.customTitle || s.firstPrompt || `${s.sessionId.slice(0, 8)}...`)}</td>
421
441
  <td class="cost-cell">${formatCost(s.totalCost)}</td>
422
442
  <td>${formatTokens(s.totalTokens)}</td>
423
443
  <td>${s.messageCount}</td>
@@ -458,7 +478,7 @@ function renderDetail() {
458
478
  <span class="sep">/</span>
459
479
  <a onclick="navigateToSessions('${esc(d.encodedProjectPath)}', '${esc(d.projectPath)}')">${esc(d.projectPath)}</a>
460
480
  <span class="sep">/</span>
461
- <span class="current">${esc(d.sessionId.slice(0, 12))}...</span>
481
+ <span class="current">${esc(d.customTitle || d.firstPrompt || `${d.sessionId.slice(0, 12)}...`)}</span>
462
482
  </div>
463
483
 
464
484
  ${d.firstPrompt ? `<div style="color:var(--text-tertiary);font-size:12px;margin-bottom:16px;font-style:italic">"${esc(d.firstPrompt)}"</div>` : ''}
@@ -839,6 +859,7 @@ function showView(viewId) {
839
859
 
840
860
  async function navigate(view, params) {
841
861
  currentView = view;
862
+ loadSort(view);
842
863
  if (params?.project) currentProjectPath = params.project;
843
864
  if (params?.projectName) currentProjectName = params.projectName;
844
865
  if (params?.session) currentSessionId = params.session;
@@ -880,10 +901,11 @@ function sortBy(field) {
880
901
  sortField = field;
881
902
  sortOrder = 'desc';
882
903
  }
883
- lastRenderHash.projects = null;
884
- lastRenderHash.overview = null;
904
+ lastRenderHash[currentView] = null;
905
+ saveSort(currentView);
885
906
  updateUrl();
886
907
  if (currentView === 'overview') renderOverview();
908
+ else if (currentView === 'sessions') renderSessions();
887
909
  else renderProjects();
888
910
  }
889
911
 
@@ -945,6 +967,7 @@ async function loadAndRender(view) {
945
967
  case 'sessions':
946
968
  if (currentProjectPath) {
947
969
  sessionsData = null;
970
+ lastRenderHash.sessions = null;
948
971
  renderSessions();
949
972
  await fetchSessions(currentProjectPath);
950
973
  if (myNav !== navCounter) return;
@@ -954,6 +977,7 @@ async function loadAndRender(view) {
954
977
  case 'detail':
955
978
  if (currentSessionId) {
956
979
  sessionDetailData = null;
980
+ lastRenderHash.detail = null;
957
981
  renderDetail();
958
982
  await fetchSessionDetail(currentSessionId);
959
983
  if (myNav !== navCounter) return;
@@ -1032,7 +1056,11 @@ function activateSelectedRow() {
1032
1056
 
1033
1057
  function goBack() {
1034
1058
  if (currentView === 'detail') {
1035
- navigate('sessions', { project: currentProjectPath, projectName: currentProjectName });
1059
+ if (currentProjectPath) {
1060
+ navigate('sessions', { project: currentProjectPath, projectName: currentProjectName });
1061
+ } else {
1062
+ navigate('projects');
1063
+ }
1036
1064
  } else if (currentView === 'sessions') {
1037
1065
  navigate('projects');
1038
1066
  } else if (currentView === 'projects') {
@@ -1199,6 +1227,7 @@ document.addEventListener('DOMContentLoaded', async () => {
1199
1227
  window.addEventListener('popstate', () => {
1200
1228
  const state = getUrlState();
1201
1229
  currentView = state.view || 'overview';
1230
+ loadSort(currentView);
1202
1231
  currentProjectPath = state.project;
1203
1232
  currentProjectName = state.projectName;
1204
1233
  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" selected>7 days</option>
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;
@@ -311,6 +303,7 @@ async function loadProjectData(files, pricing) {
311
303
  messages: [],
312
304
  models: new Set(),
313
305
  firstPrompt: '',
306
+ customTitle: null,
314
307
  firstTimestamp: null,
315
308
  lastTimestamp: null,
316
309
  });
@@ -318,17 +311,25 @@ async function loadProjectData(files, pricing) {
318
311
  const session = sessions.get(sessionId);
319
312
 
320
313
  await processJSONLFile(file, async (line) => {
321
- const data = parseUsageLine(line);
322
- if (!data) return;
314
+ let parsed;
315
+ try { parsed = JSON.parse(line); } catch { return; }
316
+
317
+ if (parsed.type === 'custom-title' && parsed.customTitle) {
318
+ session.customTitle = parsed.customTitle;
319
+ return;
320
+ }
321
+
322
+ if (!parsed.message?.usage?.input_tokens && parsed.message?.usage?.input_tokens !== 0) return;
323
+ if (!parsed.timestamp) return;
323
324
 
324
- const hash = createDedupeHash(data);
325
+ const hash = createDedupeHash(parsed);
325
326
  if (hash && seen.has(hash)) return;
326
327
  if (hash) seen.add(hash);
327
328
 
328
- const cost = calculateEntryCost(data, pricing);
329
- const usage = data.message.usage;
330
- const model = data.message?.model || 'unknown';
331
- const ts = data.timestamp;
329
+ const cost = calculateEntryCost(parsed, pricing);
330
+ const usage = parsed.message.usage;
331
+ const model = parsed.message?.model || 'unknown';
332
+ const ts = parsed.timestamp;
332
333
 
333
334
  session.totalCost += cost;
334
335
  session.inputTokens += usage.input_tokens || 0;
@@ -352,10 +353,10 @@ async function loadProjectData(files, pricing) {
352
353
  });
353
354
 
354
355
  // Capture first user prompt from the JSONL (look for human/user messages)
355
- if (!session.firstPrompt && data.type === 'human' && data.message?.content) {
356
- const content = Array.isArray(data.message.content)
357
- ? data.message.content.map(c => c.text || '').join(' ')
358
- : (typeof data.message.content === 'string' ? data.message.content : '');
356
+ if (!session.firstPrompt && parsed.type === 'human' && parsed.message?.content) {
357
+ const content = Array.isArray(parsed.message.content)
358
+ ? parsed.message.content.map(c => c.text || '').join(' ')
359
+ : (typeof parsed.message.content === 'string' ? parsed.message.content : '');
359
360
  if (content.trim()) {
360
361
  session.firstPrompt = content.trim().slice(0, 120);
361
362
  }
@@ -553,6 +554,7 @@ async function getProjectSessionsData(encodedPath, days) {
553
554
 
554
555
  result.push({
555
556
  sessionId: session.sessionId,
557
+ customTitle: session.customTitle,
556
558
  totalCost: cost,
557
559
  inputTokens: input,
558
560
  outputTokens: output,
@@ -602,6 +604,7 @@ async function getSessionDetailData(sessionId) {
602
604
 
603
605
  const result = {
604
606
  sessionId,
607
+ customTitle: session.customTitle,
605
608
  projectPath: decodeProjectPath(encodedPath),
606
609
  encodedProjectPath: encodedPath,
607
610
  totalCost: session.totalCost,