@askalf/dario 3.4.5 → 3.5.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/dist/cli.js CHANGED
@@ -37,6 +37,7 @@ import { join } from 'node:path';
37
37
  import { homedir } from 'node:os';
38
38
  import { startAutoOAuthFlow, getStatus, refreshTokens, loadCredentials } from './oauth.js';
39
39
  import { startProxy, sanitizeError } from './proxy.js';
40
+ import { listAccountAliases, loadAllAccounts, addAccountViaOAuth, removeAccount } from './accounts.js';
40
41
  const args = process.argv.slice(2);
41
42
  const command = args[0] ?? 'proxy';
42
43
  async function login() {
@@ -142,6 +143,114 @@ async function proxy() {
142
143
  const model = modelArg ? modelArg.split('=')[1] : undefined;
143
144
  await startProxy({ port, host, verbose, model, passthrough, preserveTools });
144
145
  }
146
+ async function accounts() {
147
+ const sub = args[1];
148
+ if (!sub || sub === 'list') {
149
+ const aliases = await listAccountAliases();
150
+ console.log('');
151
+ console.log(' dario — Accounts');
152
+ console.log(' ────────────────');
153
+ console.log('');
154
+ if (aliases.length === 0) {
155
+ console.log(' No multi-account pool configured.');
156
+ console.log('');
157
+ console.log(' Pool mode activates automatically when ~/.dario/accounts/');
158
+ console.log(' has 2+ entries. Add the first with:');
159
+ console.log(' dario accounts add <alias>');
160
+ console.log('');
161
+ console.log(' Single-account dario (the default) keeps working as-is');
162
+ console.log(' with ~/.dario/credentials.json — you do not need to');
163
+ console.log(' migrate unless you want pool routing across accounts.');
164
+ console.log('');
165
+ return;
166
+ }
167
+ const loaded = await loadAllAccounts();
168
+ const now = Date.now();
169
+ console.log(` ${aliases.length} account${aliases.length === 1 ? '' : 's'} configured`);
170
+ if (aliases.length === 1) {
171
+ console.log(' (Pool mode needs 2+ accounts — single-account mode until another is added.)');
172
+ }
173
+ console.log('');
174
+ for (const a of loaded) {
175
+ const msLeft = Math.max(0, a.expiresAt - now);
176
+ const hours = Math.floor(msLeft / 3600000);
177
+ const mins = Math.floor((msLeft % 3600000) / 60000);
178
+ const expiry = msLeft > 0 ? `${hours}h ${mins}m` : 'expired';
179
+ console.log(` ${a.alias.padEnd(20)} token expires in ${expiry}`);
180
+ }
181
+ console.log('');
182
+ return;
183
+ }
184
+ if (sub === 'add') {
185
+ const alias = args[2];
186
+ if (!alias) {
187
+ console.error('');
188
+ console.error(' Usage: dario accounts add <alias>');
189
+ console.error('');
190
+ console.error(' <alias> is any label you want for the account (e.g. "work", "personal").');
191
+ console.error('');
192
+ process.exit(1);
193
+ }
194
+ if (!/^[a-zA-Z0-9._-]+$/.test(alias)) {
195
+ console.error('[dario] Invalid alias. Use letters, numbers, dot, underscore, dash only.');
196
+ process.exit(1);
197
+ }
198
+ const existing = await listAccountAliases();
199
+ if (existing.includes(alias)) {
200
+ console.error(`[dario] Account "${alias}" already exists. Remove it first with \`dario accounts remove ${alias}\`.`);
201
+ process.exit(1);
202
+ }
203
+ console.log('');
204
+ console.log(` Adding account "${alias}" to the pool...`);
205
+ console.log('');
206
+ try {
207
+ const creds = await addAccountViaOAuth(alias);
208
+ const minutes = Math.round((creds.expiresAt - Date.now()) / 60000);
209
+ console.log('');
210
+ console.log(` Account "${alias}" added.`);
211
+ console.log(` Token expires in ${minutes} minutes (auto-refreshes in the background).`);
212
+ const total = (await listAccountAliases()).length;
213
+ if (total >= 2) {
214
+ console.log('');
215
+ console.log(' Pool mode is now active. Restart `dario proxy` to pick up the new account.');
216
+ }
217
+ else {
218
+ console.log('');
219
+ console.log(' Add at least one more account to activate pool routing:');
220
+ console.log(' dario accounts add <another-alias>');
221
+ }
222
+ console.log('');
223
+ }
224
+ catch (err) {
225
+ console.error('');
226
+ console.error(` Failed to add account: ${sanitizeError(err)}`);
227
+ console.error('');
228
+ process.exit(1);
229
+ }
230
+ return;
231
+ }
232
+ if (sub === 'remove' || sub === 'rm') {
233
+ const alias = args[2];
234
+ if (!alias) {
235
+ console.error('');
236
+ console.error(' Usage: dario accounts remove <alias>');
237
+ console.error('');
238
+ process.exit(1);
239
+ }
240
+ const ok = await removeAccount(alias);
241
+ if (ok) {
242
+ console.log(`[dario] Account "${alias}" removed.`);
243
+ }
244
+ else {
245
+ console.error(`[dario] No account "${alias}" found.`);
246
+ process.exit(1);
247
+ }
248
+ return;
249
+ }
250
+ console.error(`[dario] Unknown accounts subcommand: ${sub}`);
251
+ console.error('Usage: dario accounts [list|add <alias>|remove <alias>]');
252
+ process.exit(1);
253
+ }
145
254
  async function help() {
146
255
  console.log(`
147
256
  dario — Use your Claude subscription as an API.
@@ -152,6 +261,9 @@ async function help() {
152
261
  dario status Check authentication status
153
262
  dario refresh Force token refresh
154
263
  dario logout Remove saved credentials
264
+ dario accounts list List accounts in the multi-account pool
265
+ dario accounts add NAME Add a new account to the pool (runs OAuth flow)
266
+ dario accounts remove N Remove an account from the pool
155
267
 
156
268
  Proxy options:
157
269
  --model=MODEL Force a model for all requests
@@ -206,6 +318,7 @@ const commands = {
206
318
  proxy,
207
319
  refresh,
208
320
  logout,
321
+ accounts,
209
322
  help,
210
323
  version,
211
324
  '--help': help,
package/dist/index.d.ts CHANGED
@@ -7,3 +7,9 @@
7
7
  export { startAutoOAuthFlow, refreshTokens, getAccessToken, getStatus, loadCredentials } from './oauth.js';
8
8
  export type { OAuthTokens, CredentialsFile } from './oauth.js';
9
9
  export { startProxy, sanitizeError } from './proxy.js';
10
+ export { AccountPool, parseRateLimits } from './pool.js';
11
+ export type { PoolAccount, PoolStatus, RateLimitSnapshot, AccountIdentity } from './pool.js';
12
+ export { listAccountAliases, loadAccount, loadAllAccounts, saveAccount, removeAccount, refreshAccountToken, addAccountViaOAuth, getAccountsDir, } from './accounts.js';
13
+ export type { AccountCredentials } from './accounts.js';
14
+ export { Analytics } from './analytics.js';
15
+ export type { RequestRecord, AnalyticsSummary } from './analytics.js';
package/dist/index.js CHANGED
@@ -6,3 +6,9 @@
6
6
  */
7
7
  export { startAutoOAuthFlow, refreshTokens, getAccessToken, getStatus, loadCredentials } from './oauth.js';
8
8
  export { startProxy, sanitizeError } from './proxy.js';
9
+ // Multi-account pool API (pool activates automatically when ~/.dario/accounts/
10
+ // contains 2+ accounts; see README for the progression from single-account
11
+ // mode to pool mode).
12
+ export { AccountPool, parseRateLimits } from './pool.js';
13
+ export { listAccountAliases, loadAccount, loadAllAccounts, saveAccount, removeAccount, refreshAccountToken, addAccountViaOAuth, getAccountsDir, } from './accounts.js';
14
+ export { Analytics } from './analytics.js';
package/dist/pool.d.ts ADDED
@@ -0,0 +1,68 @@
1
+ export interface AccountIdentity {
2
+ deviceId: string;
3
+ accountUuid: string;
4
+ sessionId: string;
5
+ }
6
+ export interface RateLimitSnapshot {
7
+ status: string;
8
+ util5h: number;
9
+ util7d: number;
10
+ overageUtil: number;
11
+ claim: string;
12
+ reset: number;
13
+ fallbackPct: number;
14
+ updatedAt: number;
15
+ }
16
+ export declare const EMPTY_SNAPSHOT: RateLimitSnapshot;
17
+ export interface PoolAccount {
18
+ alias: string;
19
+ accessToken: string;
20
+ refreshToken: string;
21
+ expiresAt: number;
22
+ identity: AccountIdentity;
23
+ rateLimit: RateLimitSnapshot;
24
+ requestCount: number;
25
+ }
26
+ export interface PoolStatus {
27
+ accounts: number;
28
+ healthy: number;
29
+ exhausted: number;
30
+ totalHeadroom: number;
31
+ bestAccount: string;
32
+ queued: number;
33
+ }
34
+ /** Parse an Anthropic response's rate-limit headers into a snapshot. */
35
+ export declare function parseRateLimits(headers: Headers): RateLimitSnapshot;
36
+ export declare class AccountPool {
37
+ private accounts;
38
+ private queue;
39
+ private queueMaxSize;
40
+ private queueTimeoutMs;
41
+ private drainTimer;
42
+ add(alias: string, opts: {
43
+ accessToken: string;
44
+ refreshToken: string;
45
+ expiresAt: number;
46
+ deviceId: string;
47
+ accountUuid: string;
48
+ }): void;
49
+ remove(alias: string): boolean;
50
+ get size(): number;
51
+ /** Select the best account for the next request. */
52
+ select(): PoolAccount | null;
53
+ /** Select the next-best account, excluding the given alias. */
54
+ selectExcluding(excludeAlias: string): PoolAccount | null;
55
+ updateRateLimits(alias: string, snapshot: RateLimitSnapshot): void;
56
+ markRejected(alias: string, snapshot: RateLimitSnapshot): void;
57
+ updateTokens(alias: string, accessToken: string, refreshToken: string, expiresAt: number): void;
58
+ get(alias: string): PoolAccount | undefined;
59
+ all(): PoolAccount[];
60
+ status(): PoolStatus;
61
+ /**
62
+ * Wait for an available account. If all accounts are exhausted, queues
63
+ * the request and resolves when an account becomes available via
64
+ * updateRateLimits reducing utilization below threshold.
65
+ */
66
+ waitForAccount(): Promise<PoolAccount>;
67
+ private drainQueue;
68
+ }
package/dist/pool.js ADDED
@@ -0,0 +1,212 @@
1
+ /**
2
+ * Account pool — rate limit tracking, headroom routing, failover.
3
+ *
4
+ * Activated automatically when `~/.dario/accounts/` contains 2+ accounts.
5
+ * Single-account dario (`~/.dario/credentials.json`) keeps the same code
6
+ * path it has always had; the pool only runs when there are multiple
7
+ * accounts to distribute against.
8
+ */
9
+ import { randomUUID } from 'node:crypto';
10
+ export const EMPTY_SNAPSHOT = {
11
+ status: 'unknown',
12
+ util5h: 0,
13
+ util7d: 0,
14
+ overageUtil: 0,
15
+ claim: 'unknown',
16
+ reset: 0,
17
+ fallbackPct: 0,
18
+ updatedAt: 0,
19
+ };
20
+ /** Parse an Anthropic response's rate-limit headers into a snapshot. */
21
+ export function parseRateLimits(headers) {
22
+ const get = (key) => headers.get(`anthropic-ratelimit-unified-${key}`) ?? '';
23
+ return {
24
+ status: get('status') || 'unknown',
25
+ util5h: parseFloat(get('5h-utilization')) || 0,
26
+ util7d: parseFloat(get('7d-utilization')) || 0,
27
+ overageUtil: parseFloat(get('overage-utilization')) || 0,
28
+ claim: get('representative-claim') || 'unknown',
29
+ reset: parseInt(get('reset')) || 0,
30
+ fallbackPct: parseFloat(get('fallback-percentage')) || 0,
31
+ updatedAt: Date.now(),
32
+ };
33
+ }
34
+ export class AccountPool {
35
+ accounts = new Map();
36
+ queue = [];
37
+ queueMaxSize = 50;
38
+ queueTimeoutMs = 60_000;
39
+ drainTimer = null;
40
+ add(alias, opts) {
41
+ const existing = this.accounts.get(alias);
42
+ this.accounts.set(alias, {
43
+ alias,
44
+ accessToken: opts.accessToken,
45
+ refreshToken: opts.refreshToken,
46
+ expiresAt: opts.expiresAt,
47
+ identity: existing?.identity ?? {
48
+ deviceId: opts.deviceId,
49
+ accountUuid: opts.accountUuid,
50
+ sessionId: randomUUID(),
51
+ },
52
+ rateLimit: existing?.rateLimit ?? { ...EMPTY_SNAPSHOT },
53
+ requestCount: existing?.requestCount ?? 0,
54
+ });
55
+ }
56
+ remove(alias) {
57
+ return this.accounts.delete(alias);
58
+ }
59
+ get size() {
60
+ return this.accounts.size;
61
+ }
62
+ /** Select the best account for the next request. */
63
+ select() {
64
+ if (this.accounts.size === 0)
65
+ return null;
66
+ const now = Date.now();
67
+ const all = [...this.accounts.values()];
68
+ const eligible = all.filter(a => a.rateLimit.status !== 'rejected' &&
69
+ a.expiresAt > now + 30_000);
70
+ if (eligible.length > 0) {
71
+ return eligible.reduce((best, curr) => {
72
+ const bestHeadroom = 1 - Math.max(best.rateLimit.util5h, best.rateLimit.util7d);
73
+ const currHeadroom = 1 - Math.max(curr.rateLimit.util5h, curr.rateLimit.util7d);
74
+ return currHeadroom > bestHeadroom ? curr : best;
75
+ });
76
+ }
77
+ // All accounts exhausted — return the one with the earliest reset
78
+ const withReset = all.filter(a => a.rateLimit.reset > 0);
79
+ if (withReset.length > 0) {
80
+ return withReset.reduce((a, b) => a.rateLimit.reset < b.rateLimit.reset ? a : b);
81
+ }
82
+ // No rate-limit data at all — least-used first
83
+ return all.reduce((a, b) => a.requestCount < b.requestCount ? a : b);
84
+ }
85
+ /** Select the next-best account, excluding the given alias. */
86
+ selectExcluding(excludeAlias) {
87
+ if (this.accounts.size <= 1)
88
+ return null;
89
+ const now = Date.now();
90
+ const candidates = [...this.accounts.values()].filter(a => a.alias !== excludeAlias);
91
+ const eligible = candidates.filter(a => a.rateLimit.status !== 'rejected' &&
92
+ a.expiresAt > now + 30_000);
93
+ if (eligible.length > 0) {
94
+ return eligible.reduce((best, curr) => {
95
+ const bestHeadroom = 1 - Math.max(best.rateLimit.util5h, best.rateLimit.util7d);
96
+ const currHeadroom = 1 - Math.max(curr.rateLimit.util5h, curr.rateLimit.util7d);
97
+ return currHeadroom > bestHeadroom ? curr : best;
98
+ });
99
+ }
100
+ if (candidates.length > 0) {
101
+ return candidates.reduce((a, b) => a.requestCount < b.requestCount ? a : b);
102
+ }
103
+ return null;
104
+ }
105
+ updateRateLimits(alias, snapshot) {
106
+ const account = this.accounts.get(alias);
107
+ if (!account)
108
+ return;
109
+ account.rateLimit = snapshot;
110
+ account.requestCount++;
111
+ }
112
+ markRejected(alias, snapshot) {
113
+ const account = this.accounts.get(alias);
114
+ if (!account)
115
+ return;
116
+ account.rateLimit = { ...snapshot, status: 'rejected' };
117
+ }
118
+ updateTokens(alias, accessToken, refreshToken, expiresAt) {
119
+ const account = this.accounts.get(alias);
120
+ if (!account)
121
+ return;
122
+ account.accessToken = accessToken;
123
+ account.refreshToken = refreshToken;
124
+ account.expiresAt = expiresAt;
125
+ }
126
+ get(alias) {
127
+ return this.accounts.get(alias);
128
+ }
129
+ all() {
130
+ return [...this.accounts.values()];
131
+ }
132
+ status() {
133
+ const all = this.all();
134
+ const now = Date.now();
135
+ const healthy = all.filter(a => a.rateLimit.status !== 'rejected' &&
136
+ a.expiresAt > now + 30_000);
137
+ const headrooms = all.map(a => 1 - Math.max(a.rateLimit.util5h, a.rateLimit.util7d));
138
+ const avgHeadroom = headrooms.length > 0 ? headrooms.reduce((a, b) => a + b, 0) / headrooms.length : 0;
139
+ const best = this.select();
140
+ return {
141
+ accounts: all.length,
142
+ healthy: healthy.length,
143
+ exhausted: all.length - healthy.length,
144
+ totalHeadroom: Math.round(avgHeadroom * 100),
145
+ bestAccount: best?.alias ?? 'none',
146
+ queued: this.queue.length,
147
+ };
148
+ }
149
+ /**
150
+ * Wait for an available account. If all accounts are exhausted, queues
151
+ * the request and resolves when an account becomes available via
152
+ * updateRateLimits reducing utilization below threshold.
153
+ */
154
+ async waitForAccount() {
155
+ const immediate = this.select();
156
+ if (immediate) {
157
+ const headroom = 1 - Math.max(immediate.rateLimit.util5h, immediate.rateLimit.util7d);
158
+ if (headroom > 0.02)
159
+ return immediate;
160
+ }
161
+ if (this.queue.length >= this.queueMaxSize) {
162
+ throw new Error('Queue full — all accounts exhausted');
163
+ }
164
+ if (!this.drainTimer) {
165
+ this.drainTimer = setInterval(() => this.drainQueue(), 5_000);
166
+ this.drainTimer.unref();
167
+ }
168
+ return new Promise((resolve, reject) => {
169
+ const entry = { resolve, reject, enqueuedAt: Date.now() };
170
+ this.queue.push(entry);
171
+ setTimeout(() => {
172
+ const idx = this.queue.indexOf(entry);
173
+ if (idx >= 0) {
174
+ this.queue.splice(idx, 1);
175
+ reject(new Error('Queue timeout — no accounts available within 60s'));
176
+ }
177
+ }, this.queueTimeoutMs);
178
+ });
179
+ }
180
+ drainQueue() {
181
+ if (this.queue.length === 0) {
182
+ if (this.drainTimer) {
183
+ clearInterval(this.drainTimer);
184
+ this.drainTimer = null;
185
+ }
186
+ return;
187
+ }
188
+ const now = Date.now();
189
+ this.queue = this.queue.filter(entry => {
190
+ if (now - entry.enqueuedAt > this.queueTimeoutMs) {
191
+ entry.reject(new Error('Queue timeout — no accounts available within 60s'));
192
+ return false;
193
+ }
194
+ return true;
195
+ });
196
+ while (this.queue.length > 0) {
197
+ const account = this.select();
198
+ if (!account)
199
+ break;
200
+ const headroom = 1 - Math.max(account.rateLimit.util5h, account.rateLimit.util7d);
201
+ if (headroom <= 0.02)
202
+ break;
203
+ const entry = this.queue.shift();
204
+ if (entry)
205
+ entry.resolve(account);
206
+ }
207
+ if (this.queue.length === 0 && this.drainTimer) {
208
+ clearInterval(this.drainTimer);
209
+ this.drainTimer = null;
210
+ }
211
+ }
212
+ }
package/dist/proxy.js CHANGED
@@ -7,6 +7,9 @@ import { homedir } from 'node:os';
7
7
  import { arch, platform } from 'node:process';
8
8
  import { getAccessToken, getStatus } from './oauth.js';
9
9
  import { buildCCRequest, reverseMapResponse } from './cc-template.js';
10
+ import { AccountPool, parseRateLimits } from './pool.js';
11
+ import { Analytics } from './analytics.js';
12
+ import { loadAllAccounts, loadAccount, refreshAccountToken } from './accounts.js';
10
13
  const ANTHROPIC_API = 'https://api.anthropic.com';
11
14
  const DEFAULT_PORT = 3456;
12
15
  const MAX_BODY_BYTES = 10 * 1024 * 1024; // 10 MB — generous for large prompts, prevents abuse
@@ -321,11 +324,59 @@ export async function startProxy(opts = {}) {
321
324
  const host = opts.host ?? process.env.DARIO_HOST ?? DEFAULT_HOST;
322
325
  const verbose = opts.verbose ?? false;
323
326
  const passthrough = opts.passthrough ?? false;
324
- // Verify auth before starting
325
- const status = await getStatus();
326
- if (!status.authenticated) {
327
- console.error('[dario] Not authenticated. Run `dario login` first.');
328
- process.exit(1);
327
+ // Multi-account pool activated when ~/.dario/accounts/ has 2+ entries.
328
+ // Single-account dario keeps its existing code path unchanged.
329
+ const accountsList = await loadAllAccounts();
330
+ const pool = accountsList.length >= 2 ? new AccountPool() : null;
331
+ const analytics = pool ? new Analytics() : null;
332
+ let status;
333
+ if (pool) {
334
+ for (const acc of accountsList) {
335
+ pool.add(acc.alias, {
336
+ accessToken: acc.accessToken,
337
+ refreshToken: acc.refreshToken,
338
+ expiresAt: acc.expiresAt,
339
+ deviceId: acc.deviceId,
340
+ accountUuid: acc.accountUuid,
341
+ });
342
+ }
343
+ console.log(` Pool mode: ${accountsList.length} accounts loaded`);
344
+ // Background refresh — keep every account's token fresh without blocking requests
345
+ const refreshInterval = setInterval(async () => {
346
+ for (const acc of pool.all()) {
347
+ if (acc.expiresAt < Date.now() + 45 * 60 * 1000) {
348
+ try {
349
+ const saved = await loadAccount(acc.alias);
350
+ if (!saved)
351
+ continue;
352
+ const refreshed = await refreshAccountToken(saved);
353
+ pool.updateTokens(acc.alias, refreshed.accessToken, refreshed.refreshToken, refreshed.expiresAt);
354
+ }
355
+ catch (err) {
356
+ console.error(`[dario] Background refresh failed for ${acc.alias}: ${err instanceof Error ? err.message : err}`);
357
+ }
358
+ }
359
+ }
360
+ }, 15 * 60 * 1000);
361
+ refreshInterval.unref();
362
+ // Pool mode doesn't check single-account status — compute a placeholder
363
+ // for the startup banner using the pool's earliest expiry.
364
+ const earliest = Math.min(...pool.all().map(a => a.expiresAt));
365
+ const msLeft = Math.max(0, earliest - Date.now());
366
+ status = {
367
+ authenticated: true,
368
+ status: 'healthy',
369
+ expiresAt: earliest,
370
+ expiresIn: `${Math.floor(msLeft / 3600000)}h ${Math.floor((msLeft % 3600000) / 60000)}m`,
371
+ };
372
+ }
373
+ else {
374
+ // Single-account mode — existing auth check
375
+ status = await getStatus();
376
+ if (!status.authenticated) {
377
+ console.error('[dario] Not authenticated. Run `dario login` first.');
378
+ process.exit(1);
379
+ }
329
380
  }
330
381
  const cliVersion = detectCliVersion();
331
382
  const modelOverride = opts.model ? (MODEL_ALIASES[opts.model] ?? opts.model) : null;
@@ -433,6 +484,39 @@ export async function startProxy(opts = {}) {
433
484
  res.end(JSON.stringify(s));
434
485
  return;
435
486
  }
487
+ // Pool status endpoint — shows loaded accounts, headroom, and the
488
+ // account that would be selected next. Read-only; mutation flows through
489
+ // the `dario accounts` CLI, not HTTP.
490
+ if (urlPath === '/accounts' && req.method === 'GET') {
491
+ if (!pool) {
492
+ res.writeHead(200, JSON_HEADERS);
493
+ res.end(JSON.stringify({ mode: 'single-account', accounts: 0 }));
494
+ return;
495
+ }
496
+ const accounts = pool.all().map(a => ({
497
+ alias: a.alias,
498
+ util5h: a.rateLimit.util5h,
499
+ util7d: a.rateLimit.util7d,
500
+ claim: a.rateLimit.claim,
501
+ status: a.rateLimit.status,
502
+ requestCount: a.requestCount,
503
+ expiresInMs: Math.max(0, a.expiresAt - Date.now()),
504
+ }));
505
+ res.writeHead(200, JSON_HEADERS);
506
+ res.end(JSON.stringify({ mode: 'pool', ...pool.status(), accounts }));
507
+ return;
508
+ }
509
+ // Analytics endpoint — request history + burn-rate summary (pool mode only).
510
+ if (urlPath === '/analytics' && req.method === 'GET') {
511
+ if (!analytics) {
512
+ res.writeHead(200, JSON_HEADERS);
513
+ res.end(JSON.stringify({ mode: 'single-account', note: 'Analytics are only collected in pool mode.' }));
514
+ return;
515
+ }
516
+ res.writeHead(200, JSON_HEADERS);
517
+ res.end(JSON.stringify(analytics.summary()));
518
+ return;
519
+ }
436
520
  if (urlPath === '/v1/models' && req.method === 'GET') {
437
521
  requestCount++;
438
522
  res.writeHead(200, { ...JSON_HEADERS, 'Access-Control-Allow-Origin': corsOrigin });
@@ -465,7 +549,26 @@ export async function startProxy(opts = {}) {
465
549
  let onClientClose = null;
466
550
  let upstreamAbortReason = null;
467
551
  try {
468
- const accessToken = await getAccessToken();
552
+ // Pool mode: select an account by headroom. Single-account mode:
553
+ // fall through to getAccessToken() exactly as before. Request-path
554
+ // 429 failover (retry with the next-best account before returning a
555
+ // rate-limit error to the client) lands in v3.5.1 — this release
556
+ // ships the pool scaffolding and headroom-aware selection across
557
+ // requests, not within a single 429 retry.
558
+ let poolAccount = null;
559
+ let accessToken;
560
+ if (pool) {
561
+ poolAccount = pool.select();
562
+ if (!poolAccount) {
563
+ res.writeHead(503, JSON_HEADERS);
564
+ res.end(JSON.stringify({ error: 'No accounts available in pool' }));
565
+ return;
566
+ }
567
+ accessToken = poolAccount.accessToken;
568
+ }
569
+ else {
570
+ accessToken = await getAccessToken();
571
+ }
469
572
  // Read request body with size limit and timeout (prevents slow-loris)
470
573
  const chunks = [];
471
574
  let totalBytes = 0;
@@ -511,7 +614,10 @@ export async function startProxy(opts = {}) {
511
614
  const fullVersion = `${cliVersion}.${buildTag}`;
512
615
  const billingTag = `x-anthropic-billing-header: cc_version=${fullVersion}; cc_entrypoint=cli; cch=${cch};`;
513
616
  const CACHE_1H = { type: 'ephemeral', ttl: '1h' };
514
- const { body: ccBody, toolMap } = buildCCRequest(r, billingTag, CACHE_1H, { deviceId: identity.deviceId, accountUuid: identity.accountUuid, sessionId: SESSION_ID }, { preserveTools: opts.preserveTools ?? false });
617
+ const bodyIdentity = poolAccount
618
+ ? poolAccount.identity
619
+ : { deviceId: identity.deviceId, accountUuid: identity.accountUuid, sessionId: SESSION_ID };
620
+ const { body: ccBody, toolMap } = buildCCRequest(r, billingTag, CACHE_1H, bodyIdentity, { preserveTools: opts.preserveTools ?? false });
515
621
  // Store tool map for response reverse-mapping
516
622
  ccToolMap = toolMap;
517
623
  // Replace request body entirely with CC template
@@ -555,12 +661,16 @@ export async function startProxy(opts = {}) {
555
661
  await new Promise(r => setTimeout(r, MIN_REQUEST_INTERVAL_MS - elapsed));
556
662
  }
557
663
  lastRequestTime = Date.now();
558
- // Rotate session ID per request — fresh UUID avoids persistent-session fingerprinting
559
- SESSION_ID = randomUUID();
664
+ // Rotate session ID per request — fresh UUID avoids persistent-session fingerprinting.
665
+ // Pool mode uses the per-account identity.sessionId which is stable across
666
+ // a given account's lifetime; single-account mode rotates per request.
667
+ if (!poolAccount)
668
+ SESSION_ID = randomUUID();
669
+ const outboundSessionId = poolAccount ? poolAccount.identity.sessionId : SESSION_ID;
560
670
  const headers = {
561
671
  ...staticHeaders,
562
672
  'Authorization': `Bearer ${accessToken}`,
563
- 'x-claude-code-session-id': SESSION_ID,
673
+ 'x-claude-code-session-id': outboundSessionId,
564
674
  'anthropic-version': passthrough ? (req.headers['anthropic-version'] || '2023-06-01') : '2023-06-01',
565
675
  'anthropic-beta': beta,
566
676
  'x-client-request-id': randomUUID(),
@@ -595,6 +705,18 @@ export async function startProxy(opts = {}) {
595
705
  body: finalBody ? new Uint8Array(finalBody) : undefined,
596
706
  signal: upstreamAbort.signal,
597
707
  });
708
+ // Pool mode: capture rate-limit snapshot from the response. parseRateLimits
709
+ // returns status='rejected' on 429, which makes the next `select()` call
710
+ // route traffic away from this account until it resets.
711
+ if (pool && poolAccount) {
712
+ const snapshot = parseRateLimits(upstream.headers);
713
+ if (upstream.status === 429) {
714
+ pool.markRejected(poolAccount.alias, snapshot);
715
+ }
716
+ else {
717
+ pool.updateRateLimits(poolAccount.alias, snapshot);
718
+ }
719
+ }
598
720
  // Auto-retry without context-1m if it triggers a long-context billing error.
599
721
  // Anthropic returns this as either 400 ("long context beta is not yet available
600
722
  // for this subscription") or 429 ("Extra usage is required for long context
@@ -622,6 +744,16 @@ export async function startProxy(opts = {}) {
622
744
  // Use the retry response from here on — peeked body is now stale
623
745
  upstream = retry;
624
746
  peekedBody = null;
747
+ // Pool mode: re-capture after the context-1m retry as the snapshot may have changed.
748
+ if (pool && poolAccount) {
749
+ const retrySnapshot = parseRateLimits(upstream.headers);
750
+ if (upstream.status === 429) {
751
+ pool.markRejected(poolAccount.alias, retrySnapshot);
752
+ }
753
+ else {
754
+ pool.updateRateLimits(poolAccount.alias, retrySnapshot);
755
+ }
756
+ }
625
757
  }
626
758
  else if (upstream.status === 429) {
627
759
  // Not a context-1m issue — return enriched 429 directly
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@askalf/dario",
3
- "version": "3.4.5",
3
+ "version": "3.5.0",
4
4
  "description": "Use your Claude subscription as an API. No API key needed. Local proxy for Claude Max/Pro subscriptions.",
5
5
  "type": "module",
6
6
  "bin": {