-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.js
More file actions
163 lines (148 loc) · 4.69 KB
/
usage.js
File metadata and controls
163 lines (148 loc) · 4.69 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
// ─── usage.js ───
// Polls Claude Code's undocumented OAuth usage endpoint to surface
// subscription utilization (5h + 7d windows). Token comes from the
// macOS Keychain entry "Claude Code-credentials" or the Linux
// fallback file ~/.claude/.credentials.json.
//
// Endpoint and beta header are not in Anthropic's public reference;
// they may change without notice. All failures degrade silently —
// the UI just shows no usage bar until the next successful poll.
const { EventEmitter } = require('events');
const { execFile } = require('child_process');
const fs = require('fs');
const path = require('path');
const os = require('os');
const https = require('https');
const ENDPOINT = 'https://api.anthropic.com/api/oauth/usage';
const BETA_HEADER = 'oauth-2025-04-20';
const POLL_MS = 5 * 60 * 1000;
const FIRST_POLL_DELAY_MS = 2000;
// Exponential backoff: double the interval on each consecutive failure
// up to MAX_BACKOFF_MS, then cap. Resets to POLL_MS on first success.
const MAX_BACKOFF_MS = 60 * 60 * 1000;
class UsageMonitor extends EventEmitter {
constructor() {
super();
this.nextTimer = null;
this.latest = null;
this.lastError = null;
this.consecutiveFailures = 0;
this.stopped = false;
}
start() {
if (this.nextTimer || this.stopped) return;
this._scheduleNext(FIRST_POLL_DELAY_MS);
}
stop() {
this.stopped = true;
if (this.nextTimer) clearTimeout(this.nextTimer);
this.nextTimer = null;
}
_scheduleNext(delayMs) {
if (this.stopped) return;
this.nextTimer = setTimeout(async () => {
this.nextTimer = null;
await this.poll();
if (this.stopped) return;
const next = this.consecutiveFailures > 0
? Math.min(POLL_MS * 2 ** Math.min(this.consecutiveFailures - 1, 8), MAX_BACKOFF_MS)
: POLL_MS;
this._scheduleNext(next);
}, delayMs);
}
getLatest() {
return this.latest;
}
async poll() {
try {
const token = await this._readToken();
if (!token) {
this.consecutiveFailures += 1;
this._setError('no-token');
return;
}
const data = await this._fetch(token);
const normalized = this._normalize(data);
this.latest = normalized;
this.lastError = null;
this.consecutiveFailures = 0;
this.emit('update', normalized);
} catch (e) {
this.consecutiveFailures += 1;
this._setError(e.code || e.message || 'unknown');
}
}
_setError(code) {
if (this.lastError === code) return;
this.lastError = code;
this.emit('error', code);
}
_normalize(d) {
const pick = (obj) => obj && typeof obj.utilization === 'number'
? { utilization: obj.utilization, resetsAt: obj.resets_at || null }
: null;
return {
fiveHour: pick(d.five_hour),
sevenDay: pick(d.seven_day),
sevenDaySonnet: pick(d.seven_day_sonnet),
sevenDayOpus: pick(d.seven_day_opus),
fetchedAt: Date.now(),
};
}
_readToken() {
if (process.platform === 'darwin') return this._readTokenKeychain();
return this._readTokenFile();
}
_readTokenKeychain() {
return new Promise((resolve) => {
execFile('security', ['find-generic-password', '-s', 'Claude Code-credentials', '-w'], { timeout: 5000 }, (err, stdout) => {
if (err) return resolve(null);
try {
const json = JSON.parse(stdout.trim());
resolve(json?.claudeAiOauth?.accessToken || null);
} catch {
resolve(null);
}
});
});
}
_readTokenFile() {
try {
const p = path.join(os.homedir(), '.claude', '.credentials.json');
const json = JSON.parse(fs.readFileSync(p, 'utf-8'));
return Promise.resolve(json?.claudeAiOauth?.accessToken || null);
} catch {
return Promise.resolve(null);
}
}
_fetch(token) {
return new Promise((resolve, reject) => {
const req = https.request(ENDPOINT, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`,
'anthropic-beta': BETA_HEADER,
'User-Agent': 'aby-claude-watcher',
},
timeout: 10000,
}, (res) => {
let body = '';
res.on('data', (chunk) => { body += chunk; });
res.on('end', () => {
if (res.statusCode === 200) {
try { resolve(JSON.parse(body)); }
catch (e) { reject(new Error('parse-error')); }
} else {
const err = new Error(`http-${res.statusCode}`);
err.code = `http-${res.statusCode}`;
reject(err);
}
});
});
req.on('timeout', () => { req.destroy(new Error('timeout')); });
req.on('error', reject);
req.end();
});
}
}
module.exports = { UsageMonitor };