Skip to content

Commit e930f80

Browse files
committed
refactor(session --id): simplify output to total cost only
When --id is provided, return minimal results: - non-JSON: print numeric total cost - JSON: { sessionId, totalCost } (jq-friendly) Remove helper file; keep data-loader unchanged.
1 parent a321886 commit e930f80

2 files changed

Lines changed: 36 additions & 147 deletions

File tree

‎src/commands/_session_id.ts‎

Lines changed: 0 additions & 134 deletions
This file was deleted.

‎src/commands/session.ts‎

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ import {
1111
createTotalsObject,
1212
getTotalTokens,
1313
} from '../calculate-cost.ts';
14-
import { formatDateCompact, loadSessionData } from '../data-loader.ts';
14+
import { formatDateCompact, loadSessionData, loadSessionUsageById } from '../data-loader.ts';
1515
import { detectMismatches, printMismatchReport } from '../debug.ts';
1616
import { log, logger } from '../logger.ts';
17-
import { handleSessionIdLookup } from './_session_id.ts';
1817

1918
// All --id logic moved to ./_session_id.ts
2019

@@ -38,18 +37,42 @@ export const sessionCommand = define({
3837
logger.level = 0;
3938
}
4039

41-
// Handle specific session ID lookup
40+
// Handle specific session ID lookup (simple output)
4241
if (ctx.values.id != null) {
43-
await handleSessionIdLookup({
44-
values: {
45-
id: ctx.values.id,
46-
mode: ctx.values.mode,
47-
offline: ctx.values.offline,
48-
jq: ctx.values.jq,
49-
timezone: ctx.values.timezone,
50-
locale: ctx.values.locale ?? 'en-CA',
51-
},
52-
}, useJson);
42+
const sessionUsage = await loadSessionUsageById(ctx.values.id, {
43+
mode: ctx.values.mode,
44+
offline: ctx.values.offline,
45+
});
46+
47+
if (sessionUsage == null) {
48+
if (useJson) {
49+
log(JSON.stringify(null));
50+
}
51+
else {
52+
logger.warn(`No session found with ID: ${ctx.values.id}`);
53+
}
54+
process.exit(0);
55+
}
56+
57+
if (useJson) {
58+
const payload = { sessionId: ctx.values.id, totalCost: sessionUsage.totalCost };
59+
if (ctx.values.jq != null) {
60+
const jqResult = await processWithJq(payload, ctx.values.jq);
61+
if (Result.isFailure(jqResult)) {
62+
logger.error((jqResult.error).message);
63+
process.exit(1);
64+
}
65+
log(jqResult.value);
66+
}
67+
else {
68+
log(JSON.stringify(payload));
69+
}
70+
}
71+
else {
72+
// Print only the numeric total cost for simplicity
73+
log(String(sessionUsage.totalCost));
74+
}
75+
5376
return;
5477
}
5578

0 commit comments

Comments
 (0)