agy-bar 1.0.0 → 1.1.0
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.
- package/CHANGELOG.md +27 -0
- package/README.md +149 -55
- package/README.zh-CN.md +145 -51
- package/bin/agy-bar.mjs +625 -9
- package/llms.txt +47 -0
- package/package.json +3 -2
- package/skills/agy-bar/SKILL.md +11 -4
- package/src/account-import.mjs +435 -0
- package/src/account-quota.mjs +466 -0
- package/src/account-store.mjs +1140 -0
- package/src/agent-state.mjs +2 -1
- package/src/ansi.mjs +7 -0
- package/src/config.mjs +24 -0
- package/src/fs-atomic.mjs +16 -5
- package/src/handoff.mjs +424 -0
- package/src/installer.mjs +16 -0
- package/src/keyring.mjs +604 -0
- package/src/proxy.mjs +189 -48
- package/src/quota-sync.mjs +100 -20
- package/src/render.mjs +11 -3
- package/src/statusline.mjs +83 -12
- package/src/step-timing.mjs +129 -3
- package/src/telemetry.mjs +17 -3
- package/src/title.mjs +50 -8
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { bold, dim, fg, padDisplayEnd } from './ansi.mjs';
|
|
3
|
+
import { configDir } from './config.mjs';
|
|
4
|
+
import { readAccountIndex, readActiveAccountState, switchToAccount } from './account-store.mjs';
|
|
5
|
+
import { createKeyringBackend } from './keyring.mjs';
|
|
6
|
+
import {
|
|
7
|
+
isBucketResetDue,
|
|
8
|
+
isWeeklyBucketBlocking,
|
|
9
|
+
parseRealResetTs,
|
|
10
|
+
readAllAccountQuotas,
|
|
11
|
+
} from './quota-sync.mjs';
|
|
12
|
+
import { resolvePalette } from './themes.mjs';
|
|
13
|
+
|
|
14
|
+
export const ACCOUNT_FAMILIES = Object.freeze(['auto', 'gemini', 'claude']);
|
|
15
|
+
|
|
16
|
+
const STATUS_RANK = Object.freeze({
|
|
17
|
+
READY: 3,
|
|
18
|
+
LOW: 2,
|
|
19
|
+
UNKNOWN: 1,
|
|
20
|
+
LIMITED: 0,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
function formatDurationCompact(ms) {
|
|
24
|
+
if (!Number.isFinite(ms) || ms <= 0) return 'now';
|
|
25
|
+
const totalMin = Math.ceil(ms / 60_000);
|
|
26
|
+
if (totalMin < 60) return `${totalMin}m`;
|
|
27
|
+
const hours = Math.floor(totalMin / 60);
|
|
28
|
+
const mins = totalMin % 60;
|
|
29
|
+
if (hours < 24) return mins > 0 ? `${hours}h${mins}m` : `${hours}h`;
|
|
30
|
+
const days = Math.floor(hours / 24);
|
|
31
|
+
const remHours = hours % 24;
|
|
32
|
+
return remHours > 0 ? `${days}d${remHours}h` : `${days}d`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function formatRelativeAge(ts, now) {
|
|
36
|
+
if (!Number.isFinite(ts) || ts <= 0) return '--';
|
|
37
|
+
const diffMs = Math.max(0, now - ts);
|
|
38
|
+
if (diffMs < 60_000) return 'just now';
|
|
39
|
+
if (diffMs < 3_600_000) return `${Math.floor(diffMs / 60_000)}m ago`;
|
|
40
|
+
if (diffMs < 86_400_000) return `${Math.floor(diffMs / 3_600_000)}h ago`;
|
|
41
|
+
return `${Math.floor(diffMs / 86_400_000)}d ago`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function evaluateFamilyQuota(buckets, fiveHourId, weeklyId, now = Date.now()) {
|
|
45
|
+
if (!buckets || typeof buckets !== 'object') {
|
|
46
|
+
return {
|
|
47
|
+
status: 'UNKNOWN',
|
|
48
|
+
fiveHourPct: null,
|
|
49
|
+
weeklyPct: null,
|
|
50
|
+
resetDue: false,
|
|
51
|
+
nextResetTs: null,
|
|
52
|
+
nextResetLabel: null,
|
|
53
|
+
observedAt: null,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const b5 = buckets[fiveHourId] || null;
|
|
58
|
+
const bw = buckets[weeklyId] || null;
|
|
59
|
+
if (!b5 && !bw) {
|
|
60
|
+
return {
|
|
61
|
+
status: 'UNKNOWN',
|
|
62
|
+
fiveHourPct: null,
|
|
63
|
+
weeklyPct: null,
|
|
64
|
+
resetDue: false,
|
|
65
|
+
nextResetTs: null,
|
|
66
|
+
nextResetLabel: null,
|
|
67
|
+
observedAt: null,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const weeklyResetDue = bw ? isBucketResetDue(bw, now) : false;
|
|
72
|
+
const weeklyEffectiveFrac =
|
|
73
|
+
bw && typeof bw.remaining_fraction === 'number'
|
|
74
|
+
? weeklyResetDue
|
|
75
|
+
? 1
|
|
76
|
+
: bw.remaining_fraction
|
|
77
|
+
: null;
|
|
78
|
+
|
|
79
|
+
const weeklyBlocked = isWeeklyBucketBlocking(fiveHourId, buckets, now);
|
|
80
|
+
const fiveHourResetDue = b5 ? isBucketResetDue(b5, now) && !weeklyBlocked : false;
|
|
81
|
+
const fiveHourEffectiveFrac =
|
|
82
|
+
b5 && typeof b5.remaining_fraction === 'number'
|
|
83
|
+
? fiveHourResetDue
|
|
84
|
+
? 1
|
|
85
|
+
: b5.remaining_fraction
|
|
86
|
+
: null;
|
|
87
|
+
|
|
88
|
+
const fiveHourResetTs = b5 ? parseRealResetTs(b5) : null;
|
|
89
|
+
const weeklyResetTs = bw ? parseRealResetTs(bw) : null;
|
|
90
|
+
|
|
91
|
+
let status = 'UNKNOWN';
|
|
92
|
+
let nextResetTs = null;
|
|
93
|
+
|
|
94
|
+
if (weeklyBlocked || (weeklyEffectiveFrac !== null && weeklyEffectiveFrac <= 0.005)) {
|
|
95
|
+
status = 'LIMITED';
|
|
96
|
+
nextResetTs = weeklyResetTs && weeklyResetTs > now ? weeklyResetTs : null;
|
|
97
|
+
} else if (fiveHourEffectiveFrac !== null && fiveHourEffectiveFrac <= 0.005) {
|
|
98
|
+
status = 'LIMITED';
|
|
99
|
+
nextResetTs = fiveHourResetTs && fiveHourResetTs > now ? fiveHourResetTs : null;
|
|
100
|
+
} else if (
|
|
101
|
+
(fiveHourEffectiveFrac !== null && fiveHourEffectiveFrac <= 0.2) ||
|
|
102
|
+
(weeklyEffectiveFrac !== null && weeklyEffectiveFrac <= 0.2)
|
|
103
|
+
) {
|
|
104
|
+
status = 'LOW';
|
|
105
|
+
nextResetTs = fiveHourResetTs && fiveHourResetTs > now ? fiveHourResetTs : weeklyResetTs && weeklyResetTs > now ? weeklyResetTs : null;
|
|
106
|
+
} else if (fiveHourEffectiveFrac !== null || weeklyEffectiveFrac !== null) {
|
|
107
|
+
status = 'READY';
|
|
108
|
+
nextResetTs = fiveHourResetTs && fiveHourResetTs > now ? fiveHourResetTs : null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const observedAt = Math.max(Number(b5?.observedAt || 0), Number(bw?.observedAt || 0)) || null;
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
status,
|
|
115
|
+
fiveHourPct: fiveHourEffectiveFrac !== null ? Math.round(fiveHourEffectiveFrac * 100) : null,
|
|
116
|
+
weeklyPct: weeklyEffectiveFrac !== null ? Math.round(weeklyEffectiveFrac * 100) : null,
|
|
117
|
+
resetDue: fiveHourResetDue || weeklyResetDue,
|
|
118
|
+
nextResetTs,
|
|
119
|
+
nextResetLabel: nextResetTs ? formatDurationCompact(nextResetTs - now) : null,
|
|
120
|
+
observedAt,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function combineFamilyStatus(gemini, claude) {
|
|
125
|
+
const rankG = STATUS_RANK[gemini.status] ?? 0;
|
|
126
|
+
const rankC = STATUS_RANK[claude.status] ?? 0;
|
|
127
|
+
if (rankG >= rankC) return gemini.status;
|
|
128
|
+
return claude.status;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function listAccountsWithQuota({
|
|
132
|
+
baseDir = configDir(homedir()),
|
|
133
|
+
now = Date.now(),
|
|
134
|
+
} = {}) {
|
|
135
|
+
const index = readAccountIndex({ baseDir });
|
|
136
|
+
const state = readActiveAccountState({ baseDir });
|
|
137
|
+
const allQuotas = readAllAccountQuotas({ baseDir, now });
|
|
138
|
+
|
|
139
|
+
return index.accounts.map((slot) => {
|
|
140
|
+
const isActive = Boolean(state?.activeAccountId && state.activeAccountId === slot.id);
|
|
141
|
+
const quotaEntry =
|
|
142
|
+
allQuotas[slot.quotaKey] ||
|
|
143
|
+
(slot.emailHash ? allQuotas[slot.emailHash] : null) ||
|
|
144
|
+
(isActive ? allQuotas.default || null : null);
|
|
145
|
+
const buckets = quotaEntry?.buckets || null;
|
|
146
|
+
|
|
147
|
+
const gemini = evaluateFamilyQuota(buckets, 'gemini-5h', 'gemini-weekly', now);
|
|
148
|
+
const claude = evaluateFamilyQuota(buckets, '3p-5h', '3p-weekly', now);
|
|
149
|
+
const overallStatus = combineFamilyStatus(gemini, claude);
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
...slot,
|
|
153
|
+
isActive,
|
|
154
|
+
quotaUpdatedAt: quotaEntry?.updatedAt || null,
|
|
155
|
+
gemini,
|
|
156
|
+
claude,
|
|
157
|
+
overallStatus,
|
|
158
|
+
};
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function familyScore(row, family) {
|
|
163
|
+
if (family === 'gemini') {
|
|
164
|
+
return {
|
|
165
|
+
statusRank: STATUS_RANK[row.gemini.status] ?? 0,
|
|
166
|
+
fiveHour: row.gemini.fiveHourPct ?? -1,
|
|
167
|
+
weekly: row.gemini.weeklyPct ?? -1,
|
|
168
|
+
nextResetTs: row.gemini.nextResetTs ?? Number.MAX_SAFE_INTEGER,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
if (family === 'claude') {
|
|
172
|
+
return {
|
|
173
|
+
statusRank: STATUS_RANK[row.claude.status] ?? 0,
|
|
174
|
+
fiveHour: row.claude.fiveHourPct ?? -1,
|
|
175
|
+
weekly: row.claude.weeklyPct ?? -1,
|
|
176
|
+
nextResetTs: row.claude.nextResetTs ?? Number.MAX_SAFE_INTEGER,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
// auto: prefer best available family across Gemini and Claude.
|
|
180
|
+
const bestStatusRank = Math.max(STATUS_RANK[row.gemini.status] ?? 0, STATUS_RANK[row.claude.status] ?? 0);
|
|
181
|
+
const fiveHour = Math.max(row.gemini.fiveHourPct ?? -1, row.claude.fiveHourPct ?? -1);
|
|
182
|
+
const weekly = Math.max(row.gemini.weeklyPct ?? -1, row.claude.weeklyPct ?? -1);
|
|
183
|
+
const nextResetTs = Math.min(
|
|
184
|
+
row.gemini.nextResetTs ?? Number.MAX_SAFE_INTEGER,
|
|
185
|
+
row.claude.nextResetTs ?? Number.MAX_SAFE_INTEGER,
|
|
186
|
+
);
|
|
187
|
+
return { statusRank: bestStatusRank, fiveHour, weekly, nextResetTs };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function pickNextBestAccount(rows, { currentId = null, family = 'auto' } = {}) {
|
|
191
|
+
const normalizedFamily = String(family || 'auto').toLowerCase();
|
|
192
|
+
if (!ACCOUNT_FAMILIES.includes(normalizedFamily)) {
|
|
193
|
+
throw new Error(`Invalid --family "${family}". Choose one of: ${ACCOUNT_FAMILIES.join(', ')}`);
|
|
194
|
+
}
|
|
195
|
+
if (!Array.isArray(rows) || rows.length === 0) return null;
|
|
196
|
+
|
|
197
|
+
const candidates = rows.length > 1 && currentId ? rows.filter((r) => r.id !== currentId) : [...rows];
|
|
198
|
+
candidates.sort((a, b) => {
|
|
199
|
+
const sa = familyScore(a, normalizedFamily);
|
|
200
|
+
const sb = familyScore(b, normalizedFamily);
|
|
201
|
+
if (sb.statusRank !== sa.statusRank) return sb.statusRank - sa.statusRank;
|
|
202
|
+
if (sb.statusRank === STATUS_RANK.LIMITED) {
|
|
203
|
+
// All remaining accounts limited -> pick whichever resets soonest.
|
|
204
|
+
if (sa.nextResetTs !== sb.nextResetTs) return sa.nextResetTs - sb.nextResetTs;
|
|
205
|
+
}
|
|
206
|
+
if (sb.fiveHour !== sa.fiveHour) return sb.fiveHour - sa.fiveHour;
|
|
207
|
+
if (sb.weekly !== sa.weekly) return sb.weekly - sa.weekly;
|
|
208
|
+
// Natural round-robin tie-breaker: least recently used first.
|
|
209
|
+
return Number(a.lastUsedAt || 0) - Number(b.lastUsedAt || 0);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
const winner = candidates[0];
|
|
213
|
+
const score = familyScore(winner, normalizedFamily);
|
|
214
|
+
return {
|
|
215
|
+
account: winner,
|
|
216
|
+
family: normalizedFamily,
|
|
217
|
+
allLimited: score.statusRank === STATUS_RANK.LIMITED,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function detectQuotaFamilyFromModel(model) {
|
|
222
|
+
const raw =
|
|
223
|
+
typeof model === 'string'
|
|
224
|
+
? model
|
|
225
|
+
: `${model?.id || ''} ${model?.display_name || ''}`;
|
|
226
|
+
const lower = raw.toLowerCase();
|
|
227
|
+
if (/claude|sonnet|opus|haiku|anthropic|3p/.test(lower)) return 'claude';
|
|
228
|
+
if (/gemini|neon|carbon|argon|hydrogen|flash|pro/.test(lower)) return 'gemini';
|
|
229
|
+
return 'auto';
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function isAccountLimitedForFamily(row, family = 'auto') {
|
|
233
|
+
if (!row) return false;
|
|
234
|
+
if (family === 'gemini') return row.gemini?.status === 'LIMITED';
|
|
235
|
+
if (family === 'claude') return row.claude?.status === 'LIMITED';
|
|
236
|
+
return row.overallStatus === 'LIMITED';
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function computeSwapHint({
|
|
240
|
+
payload = null,
|
|
241
|
+
activeAccountId = null,
|
|
242
|
+
baseDir = configDir(homedir()),
|
|
243
|
+
now = Date.now(),
|
|
244
|
+
} = {}) {
|
|
245
|
+
if (!activeAccountId) return null;
|
|
246
|
+
const family = detectQuotaFamilyFromModel(payload?.model);
|
|
247
|
+
const liveQuota = payload?.quota && typeof payload.quota === 'object' ? payload.quota : null;
|
|
248
|
+
|
|
249
|
+
// Ultra-fast check against live statusline payload before reading accounts index.
|
|
250
|
+
if (liveQuota) {
|
|
251
|
+
const liveGemini = evaluateFamilyQuota(liveQuota, 'gemini-5h', 'gemini-weekly', now);
|
|
252
|
+
const liveClaude = evaluateFamilyQuota(liveQuota, '3p-5h', '3p-weekly', now);
|
|
253
|
+
const liveLimited =
|
|
254
|
+
family === 'gemini'
|
|
255
|
+
? liveGemini.status === 'LIMITED'
|
|
256
|
+
: family === 'claude'
|
|
257
|
+
? liveClaude.status === 'LIMITED'
|
|
258
|
+
: combineFamilyStatus(liveGemini, liveClaude) === 'LIMITED';
|
|
259
|
+
if (!liveLimited) return null;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const rows = listAccountsWithQuota({ baseDir, now });
|
|
263
|
+
if (rows.length < 2) return null;
|
|
264
|
+
const activeRow = rows.find((r) => r.id === activeAccountId);
|
|
265
|
+
if (!activeRow) return null;
|
|
266
|
+
if (!liveQuota && !isAccountLimitedForFamily(activeRow, family)) return null;
|
|
267
|
+
|
|
268
|
+
const pick = pickNextBestAccount(rows, { currentId: activeAccountId, family });
|
|
269
|
+
if (!pick || pick.allLimited || pick.account.id === activeAccountId) return null;
|
|
270
|
+
const famInfo = family === 'claude' ? pick.account.claude : family === 'gemini' ? pick.account.gemini : pick.account.gemini.status === 'READY' ? pick.account.gemini : pick.account.claude;
|
|
271
|
+
|
|
272
|
+
return {
|
|
273
|
+
alias: pick.account.alias,
|
|
274
|
+
family,
|
|
275
|
+
status: pick.account.overallStatus,
|
|
276
|
+
fiveHourPct: famInfo?.fiveHourPct ?? null,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function findAutoSwitchCandidate({
|
|
281
|
+
family = 'auto',
|
|
282
|
+
excludeIds = [],
|
|
283
|
+
baseDir = configDir(homedir()),
|
|
284
|
+
now = Date.now(),
|
|
285
|
+
} = {}) {
|
|
286
|
+
const state = readActiveAccountState({ baseDir });
|
|
287
|
+
if (!state?.activeAccountId) {
|
|
288
|
+
return { shouldSwitch: false, reason: 'unused' };
|
|
289
|
+
}
|
|
290
|
+
const rows = listAccountsWithQuota({ baseDir, now });
|
|
291
|
+
if (rows.length < 2) {
|
|
292
|
+
return { shouldSwitch: false, reason: 'single-account' };
|
|
293
|
+
}
|
|
294
|
+
const activeRow = rows.find((r) => r.id === state.activeAccountId);
|
|
295
|
+
if (!activeRow) {
|
|
296
|
+
return { shouldSwitch: false, reason: 'missing-active' };
|
|
297
|
+
}
|
|
298
|
+
if (!isAccountLimitedForFamily(activeRow, family)) {
|
|
299
|
+
return { shouldSwitch: false, reason: 'not-limited', activeAlias: activeRow.alias, family };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const excluded = new Set([activeRow.id, ...excludeIds]);
|
|
303
|
+
const remaining = rows.filter((r) => !excluded.has(r.id));
|
|
304
|
+
if (remaining.length === 0) {
|
|
305
|
+
return { shouldSwitch: false, reason: 'no-remaining-accounts', activeAlias: activeRow.alias, family };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const pick = pickNextBestAccount(remaining, { currentId: null, family });
|
|
309
|
+
if (!pick || pick.allLimited) {
|
|
310
|
+
return {
|
|
311
|
+
shouldSwitch: false,
|
|
312
|
+
reason: 'all-limited',
|
|
313
|
+
activeAlias: activeRow.alias,
|
|
314
|
+
family,
|
|
315
|
+
soonestAlias: pick?.account?.alias || null,
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return {
|
|
320
|
+
shouldSwitch: true,
|
|
321
|
+
reason: 'active-limited',
|
|
322
|
+
family,
|
|
323
|
+
fromId: activeRow.id,
|
|
324
|
+
fromAlias: activeRow.alias,
|
|
325
|
+
toId: pick.account.id,
|
|
326
|
+
toAlias: pick.account.alias,
|
|
327
|
+
toAccount: pick.account,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export function maybeAutoSwitchAccount({
|
|
332
|
+
family = 'auto',
|
|
333
|
+
excludeIds = [],
|
|
334
|
+
baseDir = configDir(homedir()),
|
|
335
|
+
home = homedir(),
|
|
336
|
+
keyring = createKeyringBackend(),
|
|
337
|
+
now = Date.now(),
|
|
338
|
+
} = {}) {
|
|
339
|
+
const decision = findAutoSwitchCandidate({ family, excludeIds, baseDir, now });
|
|
340
|
+
if (!decision.shouldSwitch) {
|
|
341
|
+
return { switched: false, ...decision };
|
|
342
|
+
}
|
|
343
|
+
const res = switchToAccount(decision.toAlias, { baseDir, home, keyring, now });
|
|
344
|
+
return {
|
|
345
|
+
switched: true,
|
|
346
|
+
reason: decision.reason,
|
|
347
|
+
family: decision.family,
|
|
348
|
+
fromId: decision.fromId,
|
|
349
|
+
fromAlias: decision.fromAlias,
|
|
350
|
+
toId: res.account.id,
|
|
351
|
+
toAlias: res.account.alias,
|
|
352
|
+
wroteBackOutgoing: res.wroteBackOutgoing,
|
|
353
|
+
account: res.account,
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export function sanitizeAccountForJson(row) {
|
|
358
|
+
return {
|
|
359
|
+
id: row.id,
|
|
360
|
+
alias: row.alias,
|
|
361
|
+
active: Boolean(row.isActive),
|
|
362
|
+
maskedEmail: row.maskedEmail || null,
|
|
363
|
+
tier: row.tier || null,
|
|
364
|
+
authMethod: row.authMethod || 'oauth',
|
|
365
|
+
hasRefreshToken: Boolean(row.hasRefreshToken),
|
|
366
|
+
storageBackend: row.storageBackend || 'file',
|
|
367
|
+
status: row.overallStatus,
|
|
368
|
+
gemini: {
|
|
369
|
+
status: row.gemini.status,
|
|
370
|
+
fiveHourPct: row.gemini.fiveHourPct,
|
|
371
|
+
weeklyPct: row.gemini.weeklyPct,
|
|
372
|
+
resetDue: row.gemini.resetDue,
|
|
373
|
+
nextResetIn: row.gemini.nextResetLabel,
|
|
374
|
+
},
|
|
375
|
+
claude: {
|
|
376
|
+
status: row.claude.status,
|
|
377
|
+
fiveHourPct: row.claude.fiveHourPct,
|
|
378
|
+
weeklyPct: row.claude.weeklyPct,
|
|
379
|
+
resetDue: row.claude.resetDue,
|
|
380
|
+
nextResetIn: row.claude.nextResetLabel,
|
|
381
|
+
},
|
|
382
|
+
createdAt: row.createdAt ? new Date(row.createdAt).toISOString() : null,
|
|
383
|
+
lastUsedAt: row.lastUsedAt ? new Date(row.lastUsedAt).toISOString() : null,
|
|
384
|
+
quotaUpdatedAt: row.quotaUpdatedAt ? new Date(row.quotaUpdatedAt).toISOString() : null,
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function renderMiniGauge(pct, palette, color, width = 5) {
|
|
389
|
+
if (pct === null || pct === undefined) return dim('·'.repeat(width), color);
|
|
390
|
+
const clamped = Math.max(0, Math.min(100, pct));
|
|
391
|
+
const filled = Math.round((clamped / 100) * width);
|
|
392
|
+
const empty = Math.max(0, width - filled);
|
|
393
|
+
const tone = clamped <= 5 ? palette.danger : clamped <= 20 ? palette.warn : palette.good;
|
|
394
|
+
return `${fg('█'.repeat(filled), tone, color)}${fg('░'.repeat(empty), palette.track, color)}`;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function renderFamilyCell(fam, palette, color) {
|
|
398
|
+
if (fam.status === 'UNKNOWN') return dim('--', color);
|
|
399
|
+
const bar = renderMiniGauge(fam.fiveHourPct, palette, color);
|
|
400
|
+
const p5 = fam.fiveHourPct !== null ? `${String(fam.fiveHourPct).padStart(3)}%` : ' --';
|
|
401
|
+
const pw = fam.weeklyPct !== null ? ` 7d:${fam.weeklyPct}%` : '';
|
|
402
|
+
const suffix = fam.status === 'LIMITED' && fam.nextResetLabel
|
|
403
|
+
? fg(` ↻${fam.nextResetLabel}`, palette.warn, color)
|
|
404
|
+
: fam.resetDue
|
|
405
|
+
? fg(' ↻ready', palette.good, color)
|
|
406
|
+
: '';
|
|
407
|
+
return `${bar} ${p5}${dim(pw, color)}${suffix}`;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function renderStatusBadge(status, palette, color) {
|
|
411
|
+
if (status === 'READY') return fg('READY ', palette.good, color);
|
|
412
|
+
if (status === 'LOW') return fg('LOW ', palette.warn, color);
|
|
413
|
+
if (status === 'LIMITED') return fg('LIMITED', palette.danger, color);
|
|
414
|
+
return dim('UNKNOWN', color);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export function formatAccountTable(rows, { color = true, theme = 'auto', now = Date.now() } = {}) {
|
|
418
|
+
const { palette, color: themeAllowsColor } = resolvePalette(theme);
|
|
419
|
+
const useColor = Boolean(color && themeAllowsColor);
|
|
420
|
+
if (!rows || rows.length === 0) {
|
|
421
|
+
return [
|
|
422
|
+
'No saved Antigravity accounts yet.',
|
|
423
|
+
' Save current login: agy-bar account save work',
|
|
424
|
+
' Add another account: agy-bar account add alt1',
|
|
425
|
+
].join('\n');
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const lines = [
|
|
429
|
+
bold('agy-bar accounts', useColor) + dim(` (${rows.length} saved · passive quota cache, zero private API)`, useColor),
|
|
430
|
+
'',
|
|
431
|
+
` ${padDisplayEnd('Account', 16)} ${padDisplayEnd('Status', 8)} ${padDisplayEnd('Gemini (5h / 7d)', 24)} ${padDisplayEnd('Claude (5h / 7d)', 24)} ${padDisplayEnd('Store', 9)} Last Used`,
|
|
432
|
+
dim(` ${'─'.repeat(94)}`, useColor),
|
|
433
|
+
];
|
|
434
|
+
|
|
435
|
+
for (const row of rows) {
|
|
436
|
+
const marker = row.isActive ? fg('● ', palette.good, useColor) : ' ';
|
|
437
|
+
const aliasText = row.isActive ? bold(row.alias, useColor) : row.alias;
|
|
438
|
+
const tierBadge = row.tier
|
|
439
|
+
? ` ${fg(`[${row.tier}]`, row.tier === 'ULTRA' ? palette.warn : row.tier === 'PRO' ? palette.accent : palette.muted, useColor)}`
|
|
440
|
+
: '';
|
|
441
|
+
const emailPart = row.maskedEmail ? dim(`(${row.maskedEmail})`, useColor) : '';
|
|
442
|
+
const subline = [emailPart, tierBadge.trim()].filter(Boolean).join(' ');
|
|
443
|
+
const accountCell = `${marker}${aliasText}`;
|
|
444
|
+
const statusCell = renderStatusBadge(row.overallStatus, palette, useColor);
|
|
445
|
+
const geminiCell = renderFamilyCell(row.gemini, palette, useColor);
|
|
446
|
+
const claudeCell = renderFamilyCell(row.claude, palette, useColor);
|
|
447
|
+
const storeCell = dim(row.storageBackend || 'file', useColor);
|
|
448
|
+
const ageCell = dim(formatRelativeAge(row.lastUsedAt, now), useColor);
|
|
449
|
+
|
|
450
|
+
lines.push(
|
|
451
|
+
` ${padDisplayEnd(accountCell, 16)} ${padDisplayEnd(statusCell, 8)} ${padDisplayEnd(geminiCell, 24)} ${padDisplayEnd(claudeCell, 24)} ${padDisplayEnd(storeCell, 9)} ${ageCell}`,
|
|
452
|
+
);
|
|
453
|
+
if (subline) {
|
|
454
|
+
lines.push(` ${subline}`);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
lines.push('');
|
|
459
|
+
lines.push(
|
|
460
|
+
dim(
|
|
461
|
+
' Switch: agy-bar switch <alias> | Auto-pick best: agy-bar switch next | With context: agy-bar switch next --handoff',
|
|
462
|
+
useColor,
|
|
463
|
+
),
|
|
464
|
+
);
|
|
465
|
+
return lines.join('\n');
|
|
466
|
+
}
|