gitdone-agent 0.6.9 → 0.6.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +48 -1
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -27,7 +27,7 @@ import { randomUUID } from 'node:crypto'
27
27
  // Reported to the server on every sync so the web UI can flag outdated agents.
28
28
  // Keep in lockstep with packages/agent/package.json "version" AND
29
29
  // src/lib/agentVersion.ts LATEST_AGENT_VERSION.
30
- const AGENT_VERSION = '0.6.9'
30
+ const AGENT_VERSION = '0.6.10'
31
31
 
32
32
  const AGENT_DIR = join(homedir(), '.gitdone-agent')
33
33
  const CONFIG_PATH = join(AGENT_DIR, 'config.json')
@@ -1260,13 +1260,60 @@ async function executeCommand(cfg, cmd, repoPath) {
1260
1260
  }
1261
1261
 
1262
1262
  // Report discovered repos + machine info, get back which repos to track.
1263
+ // ─── Claude plan usage (gd-355) ────────────────────────────────────────────
1264
+ // Claude Code persists an OAuth token at ~/.claude/.credentials.json and keeps
1265
+ // it fresh. We reuse it to ask Anthropic for the SAME plan-usage numbers the
1266
+ // /usage command shows (account-level: a 5-hour "session" window + a weekly
1267
+ // limit). The АИ Конзола then renders these live. Best-effort: on a missing or
1268
+ // expired token, being offline, or any error we return null and the console
1269
+ // simply keeps the last snapshot. Cached ~60s so the 30s sync loop doesn't hit
1270
+ // the endpoint twice as often as it changes.
1271
+ let usageCache = { at: 0, data: null }
1272
+ async function readClaudeUsage() {
1273
+ const now = Date.now()
1274
+ if (usageCache.data && now - usageCache.at < 60_000) return usageCache.data
1275
+ try {
1276
+ const creds = JSON.parse(readFileSync(join(homedir(), '.claude', '.credentials.json'), 'utf8'))
1277
+ const token = creds?.claudeAiOauth?.accessToken
1278
+ if (!token) return null
1279
+ const ctrl = new AbortController()
1280
+ const to = setTimeout(() => ctrl.abort(), 8000)
1281
+ let res
1282
+ try {
1283
+ res = await fetch('https://api.anthropic.com/api/oauth/usage', {
1284
+ headers: {
1285
+ Authorization: `Bearer ${token}`,
1286
+ 'Content-Type': 'application/json',
1287
+ 'anthropic-beta': 'oauth-2025-04-20',
1288
+ 'User-Agent': 'claude-cli',
1289
+ },
1290
+ signal: ctrl.signal,
1291
+ })
1292
+ } finally { clearTimeout(to) }
1293
+ if (!res.ok) return null // 401 = token expired; Claude Code refreshes it — skip this tick
1294
+ const j = await res.json()
1295
+ const data = {
1296
+ sessionPct: Math.round(j?.five_hour?.utilization ?? 0),
1297
+ sessionResetsAt: j?.five_hour?.resets_at ?? null,
1298
+ weeklyPct: Math.round(j?.seven_day?.utilization ?? 0),
1299
+ weeklyResetsAt: j?.seven_day?.resets_at ?? null,
1300
+ }
1301
+ usageCache = { at: now, data }
1302
+ return data
1303
+ } catch {
1304
+ return null // no creds file / malformed / offline — stay quiet
1305
+ }
1306
+ }
1307
+
1263
1308
  async function sync(cfg, discovered) {
1309
+ const usage = await readClaudeUsage()
1264
1310
  const data = await api(cfg, '/api/v1/agent/sync', {
1265
1311
  machineId: cfg.machineId,
1266
1312
  hostname: cfg.hostname,
1267
1313
  agentVersion: AGENT_VERSION,
1268
1314
  roots: cfg.roots,
1269
1315
  repos: discovered,
1316
+ ...(usage ? { usage } : {}),
1270
1317
  })
1271
1318
  return data.tracked ?? []
1272
1319
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gitdone-agent",
3
- "version": "0.6.9",
3
+ "version": "0.6.10",
4
4
  "description": "Local git agent for gitdone — watches a local repo and sends snapshots to gitdone.eu",
5
5
  "type": "module",
6
6
  "bin": {