-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
415 lines (363 loc) · 12 KB
/
extension.js
File metadata and controls
415 lines (363 loc) · 12 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
const vscode = require('vscode');
const https = require('https');
const { execFile } = require('child_process');
const path = require('path');
const os = require('os');
let statusBarItem;
let refreshInterval;
const API_HOST = 'cursor.com';
const USAGE_PATH = '/api/usage';
const INVOICE_PATH = '/api/dashboard/get-monthly-invoice';
function getConfig() {
const cfg = vscode.workspace.getConfiguration('cursorUsage');
return {
sessionToken: cfg.get('sessionToken', ''),
refreshIntervalMinutes: cfg.get('refreshIntervalMinutes', 15),
autoDetectToken: cfg.get('autoDetectToken', true),
};
}
function setSessionToken(token) {
return vscode.workspace.getConfiguration('cursorUsage')
.update('sessionToken', token, vscode.ConfigurationTarget.Global);
}
function getCursorDbPath() {
const home = os.homedir();
switch (process.platform) {
case 'darwin':
return path.join(home, 'Library', 'Application Support', 'Cursor', 'User', 'globalStorage', 'state.vscdb');
case 'linux':
return path.join(home, '.config', 'Cursor', 'User', 'globalStorage', 'state.vscdb');
case 'win32':
return path.join(process.env.APPDATA || '', 'Cursor', 'User', 'globalStorage', 'state.vscdb');
default:
return '';
}
}
function getChromeCookiePath() {
const home = os.homedir();
switch (process.platform) {
case 'darwin':
return path.join(home, 'Library', 'Application Support', 'Google', 'Chrome', 'Default', 'Cookies');
case 'linux':
return path.join(home, '.config', 'google-chrome', 'Default', 'Cookies');
case 'win32':
return path.join(process.env.LOCALAPPDATA || '', 'Google', 'Chrome', 'User Data', 'Default', 'Cookies');
default:
return '';
}
}
function getEdgeCookiePath() {
const home = os.homedir();
switch (process.platform) {
case 'darwin':
return path.join(home, 'Library', 'Application Support', 'Microsoft Edge', 'Default', 'Cookies');
case 'linux':
return path.join(home, '.config', 'microsoft-edge', 'Default', 'Cookies');
case 'win32':
return path.join(process.env.LOCALAPPDATA || '', 'Microsoft', 'Edge', 'User Data', 'Default', 'Cookies');
default:
return '';
}
}
function tryExtractFromBrowserCookies() {
return new Promise((resolve) => {
const cookiePaths = [
{ name: 'Chrome', path: getChromeCookiePath() },
{ name: 'Edge', path: getEdgeCookiePath() },
];
for (const browser of cookiePaths) {
if (browser.path) {
try {
if (require('fs').existsSync(browser.path)) {
console.log(`[Cursor Usage] Found ${browser.name} cookie database at ${browser.path}`);
}
} catch (e) {
}
}
}
resolve(null);
});
}
function decodeJwtPayload(jwt) {
const parts = jwt.split('.');
if (parts.length !== 3) {
throw new Error('Invalid JWT format');
}
let payload = parts[1].replace(/-/g, '+').replace(/_/g, '/');
while (payload.length % 4 !== 0) {
payload += '=';
}
return JSON.parse(Buffer.from(payload, 'base64').toString('utf8'));
}
function extractUserIdFromToken(accessToken) {
const decoded = decodeJwtPayload(accessToken);
const sub = decoded.sub || '';
return sub.includes('|') ? sub.split('|')[1] : sub;
}
function buildCookie(userId, accessToken) {
return `WorkosCursorSessionToken=${userId}%3A%3A${accessToken}`;
}
function extractTokenFromDb() {
return new Promise((resolve, reject) => {
const dbPath = getCursorDbPath();
if (!dbPath) {
reject(new Error('Unsupported platform for auto-detection'));
return;
}
const query = "SELECT value FROM ItemTable WHERE key = 'cursorAuth/accessToken'";
execFile('sqlite3', [dbPath, query], { timeout: 5000 }, (err, stdout) => {
if (err) {
reject(new Error(`sqlite3 error: ${err.message}. Make sure sqlite3 is installed (brew install sqlite3 on macOS) or set token manually via "Cursor Usage: Set Session Token" command.`));
return;
}
const token = stdout.trim();
if (token) {
resolve(token);
} else {
reject(new Error('No token in Cursor database. Open cursor.com, login, then restart Cursor. Or use "Cursor Usage: Set Session Token" to enter manually.'));
}
});
});
}
async function getAuthInfo() {
const config = getConfig();
let accessToken = config.sessionToken;
if (!accessToken && config.autoDetectToken) {
try {
accessToken = await extractTokenFromDb();
} catch (e) {
console.log('[Cursor Usage] Auto-detect failed:', e.message);
return null;
}
}
if (!accessToken) {
return null;
}
try {
const userId = extractUserIdFromToken(accessToken);
return { userId, accessToken, cookie: buildCookie(userId, accessToken) };
} catch (e) {
console.log('[Cursor Usage] Token decode failed:', e.message);
return null;
}
}
function httpRequest(options, body, redirectsLeft) {
if (redirectsLeft === undefined) redirectsLeft = 3;
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location && redirectsLeft > 0) {
const redirectUrl = new URL(res.headers.location, `https://${options.hostname}`);
const redirectOptions = {
...options,
hostname: redirectUrl.hostname,
path: redirectUrl.pathname + redirectUrl.search,
};
httpRequest(redirectOptions, body, redirectsLeft - 1).then(resolve).catch(reject);
return;
}
if (res.statusCode === 200) {
resolve(data);
} else if (res.statusCode === 401 || res.statusCode === 403) {
reject(new Error(`Auth failed (${res.statusCode}). Run "Set Cursor Session Token" or check auto-detect.`));
} else {
reject(new Error(`API returned ${res.statusCode}: ${data.substring(0, 200)}`));
}
});
});
req.on('error', reject);
req.setTimeout(15000, () => {
req.destroy();
reject(new Error('Request timeout'));
});
if (body) {
req.write(body);
}
req.end();
});
}
async function fetchUsage(auth) {
const data = await httpRequest({
hostname: API_HOST,
port: 443,
path: `${USAGE_PATH}?user=${encodeURIComponent(auth.userId)}`,
method: 'GET',
headers: {
'Accept': 'application/json',
'Cookie': auth.cookie,
},
});
console.log('[Cursor Usage] /api/usage response:', data);
const parsed = JSON.parse(data);
let totalRequests = 0;
let maxRequests = null;
const startOfMonth = parsed.startOfMonth || null;
const models = {};
for (const [key, value] of Object.entries(parsed)) {
if (key === 'startOfMonth') continue;
if (typeof value === 'object' && value !== null && value.numRequests !== undefined) {
models[key] = value;
totalRequests += value.numRequests || 0;
if (value.maxRequestUsage != null && (maxRequests === null || value.maxRequestUsage > maxRequests)) {
maxRequests = value.maxRequestUsage;
}
}
}
return { totalRequests, maxRequests, startOfMonth, models };
}
async function fetchInvoice(auth) {
const body = JSON.stringify({ includeUsageEvents: false });
const data = await httpRequest({
hostname: API_HOST,
port: 443,
path: INVOICE_PATH,
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Cookie': auth.cookie,
'Origin': 'https://cursor.com',
},
}, body);
console.log('[Cursor Usage] invoice response:', data);
const parsed = JSON.parse(data);
let totalCents = 0;
const items = parsed.items || [];
for (const item of items) {
totalCents += item.cents || 0;
}
return { totalCents, items };
}
function formatCurrency(cents) {
return `$${(cents / 100).toFixed(2)}`;
}
function ensureStatusBar() {
if (!statusBarItem) {
statusBarItem = vscode.window.createStatusBarItem(
'cursor-usage-status',
vscode.StatusBarAlignment.Left,
100
);
statusBarItem.command = 'cursor-usage.refresh';
}
return statusBarItem;
}
async function updateStatusBar() {
const bar = ensureStatusBar();
const auth = await getAuthInfo();
if (!auth) {
bar.text = '$(key) Cursor: No Token';
bar.tooltip = 'Click to refresh, or run "Cursor Usage: Set Session Token"';
bar.color = undefined;
bar.show();
return;
}
bar.text = '$(sync~spin) Cursor: ...';
bar.tooltip = 'Fetching usage...';
bar.color = undefined;
bar.show();
try {
const [usageResult, invoiceResult] = await Promise.allSettled([
fetchUsage(auth),
fetchInvoice(auth),
]);
const parts = [];
const tooltipLines = [];
if (usageResult.status === 'fulfilled') {
const u = usageResult.value;
if (u.maxRequests != null) {
parts.push(`${u.totalRequests}/${u.maxRequests}`);
} else {
parts.push(`${u.totalRequests} reqs`);
}
tooltipLines.push('--- Request Usage ---');
const now = new Date();
const lastDayOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate();
const daysLeftInMonth = lastDayOfMonth - now.getDate() + 1;
for (const [model, info] of Object.entries(u.models)) {
if (model === 'gpt-4' && info.maxRequestUsage != null) {
const remaining = info.maxRequestUsage - info.numRequests;
const avgPerDay = daysLeftInMonth > 0 ? (remaining / daysLeftInMonth).toFixed(1) : 0;
tooltipLines.push(` ${model}: Remaining ${remaining} reqs (avg ${avgPerDay}/day)`);
} else {
const limit = info.maxRequestUsage != null ? `/${info.maxRequestUsage}` : '';
tooltipLines.push(` ${model}: ${info.numRequests}${limit} reqs`);
}
}
if (u.startOfMonth) {
tooltipLines.push(` Billing cycle start: ${new Date(u.startOfMonth).toLocaleDateString()}`);
}
} else {
parts.push('? reqs');
tooltipLines.push(`Usage error: ${usageResult.reason?.message || 'unknown'}`);
}
if (invoiceResult.status === 'fulfilled' && invoiceResult.value.totalCents > 0) {
parts.push(formatCurrency(invoiceResult.value.totalCents));
tooltipLines.push('');
tooltipLines.push('--- Extra Charges ---');
for (const item of invoiceResult.value.items) {
tooltipLines.push(` ${item.description}: ${formatCurrency(item.cents)}`);
}
}
tooltipLines.push('');
tooltipLines.push(`Updated: ${new Date().toLocaleTimeString()}`);
tooltipLines.push('Click to refresh');
bar.text = `$(dashboard) Cursor: ${parts.join(' | ')}`;
bar.color = undefined;
bar.tooltip = tooltipLines.join('\n');
} catch (error) {
bar.text = '$(error) Cursor: Error';
bar.color = new vscode.ThemeColor('errorForeground');
bar.tooltip = error.message;
}
}
function startRefreshTimer() {
if (refreshInterval) {
clearInterval(refreshInterval);
}
const minutes = getConfig().refreshIntervalMinutes;
refreshInterval = setInterval(() => updateStatusBar(), minutes * 60 * 1000);
}
function activate(context) {
const disposableSetToken = vscode.commands.registerCommand('cursor-usage.setToken', async () => {
const current = getConfig().sessionToken;
const input = await vscode.window.showInputBox({
prompt: 'Paste your WorkosCursorSessionToken or the raw JWT access token from Cursor database',
value: current,
placeHolder: 'eyJhbGciOiJSUzI1NiIs...',
password: true,
});
if (input !== undefined) {
let cleaned = input.replace(/^WorkosCursorSessionToken=/, '').trim();
// If the user pasted the full cookie value containing the user ID prefix (e.g. user_123::eyJ... or user_123%3A%3AeyJ...)
// we need to extract just the JWT part.
if (cleaned.includes('%3A%3A')) {
cleaned = cleaned.split('%3A%3A')[1];
} else if (cleaned.includes('::')) {
cleaned = cleaned.split('::')[1];
}
await setSessionToken(cleaned);
vscode.window.showInformationMessage('Session token saved');
updateStatusBar();
}
});
const disposableRefresh = vscode.commands.registerCommand('cursor-usage.refresh', () => {
updateStatusBar();
});
const disposableClearToken = vscode.commands.registerCommand('cursor-usage.clearToken', async () => {
await setSessionToken('');
vscode.window.showInformationMessage('Session token cleared. Will use auto-detect if enabled.');
updateStatusBar();
});
context.subscriptions.push(disposableSetToken, disposableRefresh, disposableClearToken);
updateStatusBar();
startRefreshTimer();
}
function deactivate() {
if (refreshInterval) {
clearInterval(refreshInterval);
}
}
module.exports = { activate, deactivate };