forked from EHxuban11/usage-overlay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite-usage.js
More file actions
56 lines (54 loc) · 2.58 KB
/
Copy pathsqlite-usage.js
File metadata and controls
56 lines (54 loc) · 2.58 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
// Node helper for the two providers that need SQLite, which PowerShell 5.1
// can't read natively. Prints one JSON line for usage-overlay.ps1:
// node sqlite-usage.js cursor -> Cursor plan usage (token from state.vscdb)
// node sqlite-usage.js opencode -> OpenCode local spend from opencode.db
const os = require('os'), path = require('path');
const { DatabaseSync } = require('node:sqlite');
const mode = process.argv[2];
if (mode === 'cursor') {
// Read Cursor's session token from its local state.vscdb and print the plan
// usage summary. Cursor stores no quota locally, so we still hit its API,
// but the SQLite read is what makes this a Node helper.
try {
const db = new DatabaseSync(process.env.APPDATA + '\\Cursor\\User\\globalStorage\\state.vscdb', { readOnly: true });
const token = db.prepare("SELECT value FROM ItemTable WHERE key='cursorAuth/accessToken'").get().value;
const sub = JSON.parse(Buffer.from(token.split('.')[1], 'base64url').toString()).sub;
const userId = sub.includes('|') ? sub.split('|').pop() : sub;
fetch('https://cursor.com/api/usage-summary', {
headers: { Cookie: `WorkosCursorSessionToken=${userId}%3A%3A${token}` },
signal: AbortSignal.timeout(8000),
})
.then((r) => r.json())
.then((j) => {
console.log(JSON.stringify({
api: j.individualUsage.plan.apiPercentUsed,
total: j.individualUsage.plan.totalPercentUsed,
resets: j.billingCycleEnd,
}));
})
.catch((e) => console.log(JSON.stringify({ error: String(e) })));
} catch (e) {
console.log(JSON.stringify({ error: String(e) }));
}
} else if (mode === 'opencode') {
// OpenCode Zen is a prepaid-credit product with no usage/quota API reachable
// from the local API key, so we report locally-tracked spend from opencode.db
// (this calendar month + all-time).
try {
const db = new DatabaseSync(path.join(os.homedir(), '.local/share/opencode/opencode.db'), { readOnly: true });
const now = new Date();
const monthStart = new Date(now.getFullYear(), now.getMonth(), 1).getTime();
const all = db.prepare('SELECT SUM(cost) c, SUM(tokens_input) ti, SUM(tokens_output) to_ FROM session').get();
const mo = db.prepare('SELECT SUM(cost) c FROM session WHERE time_created >= ?').get(monthStart);
console.log(JSON.stringify({
month: mo.c || 0,
total: all.c || 0,
tin: all.ti || 0,
tout: all.to_ || 0,
}));
} catch (e) {
console.log(JSON.stringify({ error: String(e) }));
}
} else {
console.log(JSON.stringify({ error: 'usage: sqlite-usage.js cursor|opencode' }));
}