-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·183 lines (153 loc) · 7.33 KB
/
Copy pathindex.js
File metadata and controls
executable file
·183 lines (153 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env node
const { createServer } = require('./src/server');
const args = process.argv.slice(2);
if (args.includes('--help') || args.includes('-h')) {
console.log(`
codex-spend - See where your OpenAI Codex tokens go
Usage:
codex-spend [options]
Options:
--port <port> Port to run dashboard on (default: 4321)
--state-db <path> Override Codex state DB path (advanced)
--no-open Don't auto-open browser
--plan Use if you have a flat subscription plan
--help, -h Show this help message
Examples:
npx codex-spend Open dashboard in browser
codex-spend --port 8080 Use custom port
codex-spend --state-db ~/.codex/state_6.sqlite
`);
process.exit(0);
}
const portIndex = args.indexOf('--port');
const port = portIndex !== -1 ? parseInt(args[portIndex + 1], 10) : 4321;
const stateDbIndex = args.indexOf('--state-db');
const stateDbPath = stateDbIndex !== -1 ? args[stateDbIndex + 1] : null;
const noOpen = args.includes('--no-open');
const isPlan = args.includes('--plan');
if (isNaN(port)) {
console.error('Error: --port must be a number');
process.exit(1);
}
if (stateDbIndex !== -1 && (!stateDbPath || stateDbPath.startsWith('--'))) {
console.error('Error: --state-db must be a file path');
process.exit(1);
}
if (stateDbPath) {
process.env.CODEX_SPEND_STATE_DB = stateDbPath;
}
const app = createServer({ isPlan });
const server = app.listen(port, '127.0.0.1', async () => {
const url = `http://localhost:${port}`;
try {
const { parseAllSessions } = require('./src/parser');
const data = await parseAllSessions();
if (data && data.sessions && data.sessions.length > 0) {
console.log('');
console.log(' \x1b[38;2;0;113;227m ____ _ \x1b[0m');
console.log(' \x1b[38;2;0;113;227m / ___| ___ __| | ___ __ __\x1b[0m');
console.log(' \x1b[38;2;0;113;227m| | / _ \\ / _` | / _ \\\\ \\/ /\x1b[0m');
console.log(' \x1b[38;2;0;113;227m| |___ | (_) | (_| || __/ > < \x1b[0m');
console.log(' \x1b[38;2;0;113;227m \\____| \\___/ \\__,_| \\___|/_/\\_\\\x1b[0m \x1b[38;2;50;173;230m- spend\x1b[0m');
console.log('');
console.log(' \x1b[37mTrack your OpenAI Codex token usage and cost.\x1b[0m');
console.log('');
const colTitle = 28;
const colDate = 10;
const colModel = 15;
const colReasoning = 9;
const colTokens = 12;
const colCost = 8;
const headerSep = '='.repeat(87);
const header =
'Title'.padEnd(colTitle) + ' | ' +
'Date'.padEnd(colDate) + ' | ' +
'Model'.padEnd(colModel) + ' | ' +
'Reasoning'.padEnd(colReasoning) + ' | ' +
'Tokens'.padEnd(colTokens) + ' | ' +
(isPlan ? 'API Value' : 'Cost').padStart(colCost);
console.log(header);
console.log('-'.repeat(header.length));
const recent = [...data.sessions].sort((a, b) => (b.createdAt || 0) - (a.createdAt || 0)).slice(0, 10).reverse(); // show last 10
for (const s of recent) {
const title = s.firstPrompt.length > colTitle ? s.firstPrompt.substring(0, colTitle - 3) + '...' : s.firstPrompt.padEnd(colTitle);
// format date as DD-MM-YY
const dparts = s.date.split('-');
const dateStr = (dparts.length === 3 ? `${dparts[2]}-${dparts[1]}-${dparts[0].slice(2)}` : s.date).padEnd(colDate);
const model = s.model.length > colModel ? s.model.substring(0, colModel) : s.model.padEnd(colModel);
let rsn = s.reasoningLevel || "none";
if (rsn === "very_high") rsn = "Very High";
else rsn = rsn.charAt(0).toUpperCase() + rsn.slice(1);
const reasoning = rsn.padEnd(colReasoning);
const formatK = (n) => n >= 1_000_000 ? (n/1_000_000).toFixed(1) + 'm' : (n >= 1000 ? (n/1000).toFixed(0) + 'k' : n.toString());
const tokensStr = formatK(s.totalTokens).padEnd(colTokens);
let costStr;
if (s.cost === 0 && data.totals.hasUnknownPricing) {
costStr = " N/A".padStart(colCost);
} else if (s.cost > 0 && s.cost < 0.01) {
costStr = "< $0.01".padStart(colCost);
} else {
costStr = ('$' + s.cost.toFixed(2)).padStart(colCost);
}
console.log(`${title} | ${dateStr} | ${model} | ${reasoning} | ${tokensStr} | ${costStr}`);
}
const t = data.totals;
console.log(headerSep);
const inM = (Math.max(0, t.totalInputTokens - (t.totalCacheReadTokens || 0)) / 1_000_000).toFixed(1);
const cacheM = (t.totalCacheReadTokens / 1_000_000).toFixed(1);
const rsnM = (t.totalReasoningTokens / 1_000_000).toFixed(1);
const outM = (t.totalOutputTokens / 1_000_000).toFixed(1);
const cIn = `\x1b[38;2;99;102;241m${inM}M uncached in\x1b[0m`;
const cCache = `\x1b[38;2;245;158;11m${cacheM}M cache\x1b[0m`;
const cRsn = `\x1b[38;2;168;85;247m${rsnM}M rsn\x1b[0m`;
const cOut = `\x1b[38;2;20;184;166m${outM}M out\x1b[0m`;
console.log(`Totals: ${(t.totalTokens / 1_000_000).toFixed(1)}M Tokens (${cIn} / ${cCache} / ${cRsn} / ${cOut}) | Cache Hit: ${((t.cacheHitRate || 0) * 100).toFixed(0)}% | ${isPlan ? 'API Eq. Value' : 'Est. Cost'}: $${(t.totalCost || 0).toFixed(2)}`);
if (t.hasUnknownPricing) {
console.log('⚠️ WARNING: Some models have unknown pricing. Sessions using unknown models show $0.00.');
}
console.log(headerSep);
// Plan usage summary — shown only when --plan is passed
if (isPlan) {
const fmt3h = (n) => n >= 1_000_000 ? (n/1_000_000).toFixed(2) + 'M' : (n >= 1000 ? (n/1000).toFixed(1) + 'K' : n.toString());
const msgs5h = t.estimatedMessagesLast5Hours || 0;
const msgs7d = t.estimatedMessagesLast7Days || 0;
const remaining5h = Math.max(0, 160 - msgs5h);
const remaining7d = Math.max(0, 3000 - msgs7d);
console.log('');
console.log(' \x1b[38;2;99;102;241m── Subscription Usage Estimate (Codex) ──────────────────────\x1b[0m');
console.log(` \x1b[37m Last 5 hours: \x1b[0m\x1b[33m${fmt3h(t.tokensLast5Hours || 0)} tokens\x1b[0m (~${msgs5h} msgs used / ~${remaining5h} remaining of 160)`);
console.log(` \x1b[37m Last 7 days: \x1b[0m\x1b[33m${fmt3h(t.tokensLast7Days || 0)} tokens\x1b[0m (~${msgs7d} msgs used / ~${remaining7d} remaining of 3,000 thinking msgs)`);
console.log(` \x1b[37m Note: Estimates based on your average of ~${t.avgTokensPerQuery || 20000} tokens/message.\x1b[0m`);
console.log(` \x1b[37m Check exact usage: \x1b[0m\x1b[36mhttps://chatgpt.com/codex/cloud/settings/analytics\x1b[0m`);
console.log('');
} else {
console.log('');
}
}
} catch (err) {
// Ignore parsing errors on boot, the UI will surface them
}
console.log(` 🚀 Dashboard running at: ${url}\n`);
if (!noOpen) {
try {
const open = (await import('open')).default;
await open(url);
} catch {
console.log(' Could not auto-open browser. Open the URL manually.');
}
}
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`Port ${port} is already in use. Try --port <other-port>`);
process.exit(1);
}
throw err;
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('');
console.log(' Shutting down...');
server.close();
process.exit(0);
});