opencode-multi-account-core 0.2.7 → 0.2.9

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/index.d.ts CHANGED
@@ -1,20 +1,551 @@
1
- export * from "./account-manager";
2
- export * from "./account-store";
3
- export * from "./claims";
4
- export * from "./config";
5
- export * from "./constants";
6
- export * from "./executor";
7
- export * from "./proactive-refresh";
8
- export * from "./rate-limit";
9
- export * from "./storage";
10
- export * from "./types";
11
- export * from "./utils";
12
- export * from "./auth-migration";
13
- export * from "./ui/ansi";
14
- export * from "./ui/confirm";
15
- export * from "./ui/select";
16
- export * from "./adapters";
17
- export * from "./pool-types";
18
- export * from "./pool-config-store";
19
- export * from "./pool-manager";
20
- export * from "./cascade-state";
1
+ import * as v from 'valibot';
2
+
3
+ declare const OAuthCredentialsSchema: v.ObjectSchema<{
4
+ readonly type: v.LiteralSchema<"oauth", undefined>;
5
+ readonly refresh: v.StringSchema<undefined>;
6
+ readonly access: v.StringSchema<undefined>;
7
+ readonly expires: v.NumberSchema<undefined>;
8
+ }, undefined>;
9
+ declare const UsageLimitEntrySchema: v.ObjectSchema<{
10
+ readonly utilization: v.NumberSchema<undefined>;
11
+ readonly resets_at: v.NullableSchema<v.StringSchema<undefined>, undefined>;
12
+ }, undefined>;
13
+ declare const UsageLimitsSchema: v.ObjectSchema<{
14
+ readonly five_hour: v.OptionalSchema<v.NullableSchema<v.ObjectSchema<{
15
+ readonly utilization: v.NumberSchema<undefined>;
16
+ readonly resets_at: v.NullableSchema<v.StringSchema<undefined>, undefined>;
17
+ }, undefined>, undefined>, null>;
18
+ readonly seven_day: v.OptionalSchema<v.NullableSchema<v.ObjectSchema<{
19
+ readonly utilization: v.NumberSchema<undefined>;
20
+ readonly resets_at: v.NullableSchema<v.StringSchema<undefined>, undefined>;
21
+ }, undefined>, undefined>, null>;
22
+ readonly seven_day_sonnet: v.OptionalSchema<v.NullableSchema<v.ObjectSchema<{
23
+ readonly utilization: v.NumberSchema<undefined>;
24
+ readonly resets_at: v.NullableSchema<v.StringSchema<undefined>, undefined>;
25
+ }, undefined>, undefined>, null>;
26
+ }, undefined>;
27
+ declare const CredentialRefreshPatchSchema: v.ObjectSchema<{
28
+ readonly accessToken: v.StringSchema<undefined>;
29
+ readonly expiresAt: v.NumberSchema<undefined>;
30
+ readonly refreshToken: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
31
+ readonly uuid: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
32
+ readonly accountId: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
33
+ readonly email: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
34
+ }, undefined>;
35
+ declare const StoredAccountSchema: v.ObjectSchema<{
36
+ readonly uuid: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
37
+ readonly accountId: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
38
+ readonly label: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
39
+ readonly email: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
40
+ readonly planTier: v.OptionalSchema<v.StringSchema<undefined>, "">;
41
+ readonly refreshToken: v.StringSchema<undefined>;
42
+ readonly accessToken: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
43
+ readonly expiresAt: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
44
+ readonly addedAt: v.NumberSchema<undefined>;
45
+ readonly lastUsed: v.NumberSchema<undefined>;
46
+ readonly enabled: v.OptionalSchema<v.BooleanSchema<undefined>, true>;
47
+ readonly rateLimitResetAt: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
48
+ readonly cachedUsage: v.OptionalSchema<v.ObjectSchema<{
49
+ readonly five_hour: v.OptionalSchema<v.NullableSchema<v.ObjectSchema<{
50
+ readonly utilization: v.NumberSchema<undefined>;
51
+ readonly resets_at: v.NullableSchema<v.StringSchema<undefined>, undefined>;
52
+ }, undefined>, undefined>, null>;
53
+ readonly seven_day: v.OptionalSchema<v.NullableSchema<v.ObjectSchema<{
54
+ readonly utilization: v.NumberSchema<undefined>;
55
+ readonly resets_at: v.NullableSchema<v.StringSchema<undefined>, undefined>;
56
+ }, undefined>, undefined>, null>;
57
+ readonly seven_day_sonnet: v.OptionalSchema<v.NullableSchema<v.ObjectSchema<{
58
+ readonly utilization: v.NumberSchema<undefined>;
59
+ readonly resets_at: v.NullableSchema<v.StringSchema<undefined>, undefined>;
60
+ }, undefined>, undefined>, null>;
61
+ }, undefined>, undefined>;
62
+ readonly cachedUsageAt: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
63
+ readonly consecutiveAuthFailures: v.OptionalSchema<v.NumberSchema<undefined>, 0>;
64
+ readonly isAuthDisabled: v.OptionalSchema<v.BooleanSchema<undefined>, false>;
65
+ readonly authDisabledReason: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
66
+ }, undefined>;
67
+ declare const AccountStorageSchema: v.ObjectSchema<{
68
+ readonly version: v.LiteralSchema<1, undefined>;
69
+ readonly accounts: v.OptionalSchema<v.ArraySchema<v.ObjectSchema<{
70
+ readonly uuid: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
71
+ readonly accountId: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
72
+ readonly label: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
73
+ readonly email: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
74
+ readonly planTier: v.OptionalSchema<v.StringSchema<undefined>, "">;
75
+ readonly refreshToken: v.StringSchema<undefined>;
76
+ readonly accessToken: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
77
+ readonly expiresAt: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
78
+ readonly addedAt: v.NumberSchema<undefined>;
79
+ readonly lastUsed: v.NumberSchema<undefined>;
80
+ readonly enabled: v.OptionalSchema<v.BooleanSchema<undefined>, true>;
81
+ readonly rateLimitResetAt: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
82
+ readonly cachedUsage: v.OptionalSchema<v.ObjectSchema<{
83
+ readonly five_hour: v.OptionalSchema<v.NullableSchema<v.ObjectSchema<{
84
+ readonly utilization: v.NumberSchema<undefined>;
85
+ readonly resets_at: v.NullableSchema<v.StringSchema<undefined>, undefined>;
86
+ }, undefined>, undefined>, null>;
87
+ readonly seven_day: v.OptionalSchema<v.NullableSchema<v.ObjectSchema<{
88
+ readonly utilization: v.NumberSchema<undefined>;
89
+ readonly resets_at: v.NullableSchema<v.StringSchema<undefined>, undefined>;
90
+ }, undefined>, undefined>, null>;
91
+ readonly seven_day_sonnet: v.OptionalSchema<v.NullableSchema<v.ObjectSchema<{
92
+ readonly utilization: v.NumberSchema<undefined>;
93
+ readonly resets_at: v.NullableSchema<v.StringSchema<undefined>, undefined>;
94
+ }, undefined>, undefined>, null>;
95
+ }, undefined>, undefined>;
96
+ readonly cachedUsageAt: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
97
+ readonly consecutiveAuthFailures: v.OptionalSchema<v.NumberSchema<undefined>, 0>;
98
+ readonly isAuthDisabled: v.OptionalSchema<v.BooleanSchema<undefined>, false>;
99
+ readonly authDisabledReason: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
100
+ }, undefined>, undefined>, readonly []>;
101
+ readonly activeAccountUuid: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
102
+ }, undefined>;
103
+ declare const AccountSelectionStrategySchema: v.PicklistSchema<["sticky", "round-robin", "hybrid"], undefined>;
104
+ declare const PluginConfigSchema: v.ObjectSchema<{
105
+ readonly account_selection_strategy: v.OptionalSchema<v.PicklistSchema<["sticky", "round-robin", "hybrid"], undefined>, "sticky">;
106
+ readonly cross_process_claims: v.OptionalSchema<v.BooleanSchema<undefined>, true>;
107
+ readonly soft_quota_threshold_percent: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.MinValueAction<number, 0, undefined>, v.MaxValueAction<number, 100, undefined>]>, 100>;
108
+ readonly rate_limit_min_backoff_ms: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.MinValueAction<number, 0, undefined>]>, 30000>;
109
+ readonly default_retry_after_ms: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.MinValueAction<number, 0, undefined>]>, 60000>;
110
+ readonly max_consecutive_auth_failures: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>]>, 3>;
111
+ readonly token_failure_backoff_ms: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.MinValueAction<number, 0, undefined>]>, 30000>;
112
+ readonly proactive_refresh: v.OptionalSchema<v.BooleanSchema<undefined>, true>;
113
+ readonly proactive_refresh_buffer_seconds: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.MinValueAction<number, 60, undefined>]>, 1800>;
114
+ readonly proactive_refresh_interval_seconds: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.MinValueAction<number, 30, undefined>]>, 300>;
115
+ readonly quiet_mode: v.OptionalSchema<v.BooleanSchema<undefined>, false>;
116
+ readonly debug: v.OptionalSchema<v.BooleanSchema<undefined>, false>;
117
+ }, undefined>;
118
+ type OAuthCredentials = v.InferOutput<typeof OAuthCredentialsSchema>;
119
+ type UsageLimitEntry = v.InferOutput<typeof UsageLimitEntrySchema>;
120
+ type UsageLimits = v.InferOutput<typeof UsageLimitsSchema>;
121
+ type CredentialRefreshPatch = v.InferOutput<typeof CredentialRefreshPatchSchema>;
122
+ type StoredAccount = v.InferOutput<typeof StoredAccountSchema>;
123
+ type AccountStorage = v.InferOutput<typeof AccountStorageSchema>;
124
+ type AccountSelectionStrategy = v.InferOutput<typeof AccountSelectionStrategySchema>;
125
+ type PluginConfig = v.InferOutput<typeof PluginConfigSchema>;
126
+ type TokenRefreshResult = {
127
+ ok: true;
128
+ patch: CredentialRefreshPatch;
129
+ } | {
130
+ ok: false;
131
+ permanent: boolean;
132
+ status?: number;
133
+ };
134
+ declare class TokenRefreshError extends Error {
135
+ readonly status?: number;
136
+ readonly permanent: boolean;
137
+ constructor(permanent: boolean, status?: number);
138
+ }
139
+ declare function isTokenRefreshError(error: unknown): error is TokenRefreshError;
140
+ interface ManagedAccount {
141
+ index: number;
142
+ uuid?: string;
143
+ accountId?: string;
144
+ label?: string;
145
+ email?: string;
146
+ planTier?: string;
147
+ refreshToken: string;
148
+ accessToken?: string;
149
+ expiresAt?: number;
150
+ addedAt: number;
151
+ lastUsed: number;
152
+ enabled: boolean;
153
+ rateLimitResetAt?: number;
154
+ last429At?: number;
155
+ cachedUsage?: UsageLimits;
156
+ cachedUsageAt?: number;
157
+ consecutiveAuthFailures: number;
158
+ isAuthDisabled: boolean;
159
+ authDisabledReason?: string;
160
+ }
161
+ interface PluginClient {
162
+ auth: {
163
+ set: (params: {
164
+ path: {
165
+ id: string;
166
+ };
167
+ body: {
168
+ type: string;
169
+ refresh: string;
170
+ access: string;
171
+ expires: number;
172
+ };
173
+ }) => Promise<void>;
174
+ };
175
+ tui: {
176
+ showToast: (params: {
177
+ body: {
178
+ title?: string;
179
+ message: string;
180
+ variant: "info" | "warning" | "success" | "error";
181
+ };
182
+ }) => Promise<void>;
183
+ };
184
+ app: {
185
+ log: (params: {
186
+ body: {
187
+ service: string;
188
+ level: "debug" | "info" | "warn" | "error";
189
+ message: string;
190
+ extra?: Record<string, unknown>;
191
+ };
192
+ }) => Promise<void>;
193
+ };
194
+ }
195
+
196
+ interface DiskCredentials {
197
+ refreshToken: string;
198
+ accessToken?: string;
199
+ expiresAt?: number;
200
+ accountId?: string;
201
+ }
202
+ declare class AccountStore {
203
+ load(): Promise<AccountStorage>;
204
+ readCredentials(uuid: string): Promise<DiskCredentials | null>;
205
+ mutateAccount(uuid: string, fn: (account: StoredAccount) => void): Promise<StoredAccount | null>;
206
+ mutateStorage(fn: (storage: AccountStorage) => void): Promise<void>;
207
+ addAccount(account: StoredAccount): Promise<void>;
208
+ removeAccount(uuid: string): Promise<boolean>;
209
+ setActiveUuid(uuid: string | undefined): Promise<void>;
210
+ clear(): Promise<void>;
211
+ }
212
+
213
+ interface ProfileData {
214
+ email?: string;
215
+ planTier: string;
216
+ }
217
+ interface RuntimeFactoryLike {
218
+ invalidate(uuid: string): void;
219
+ }
220
+ interface AccountManagerDependencies {
221
+ providerAuthId: string;
222
+ isTokenExpired: (account: Pick<ManagedAccount, "accessToken" | "expiresAt">) => boolean;
223
+ refreshToken: (currentRefreshToken: string, accountId: string, client: PluginClient) => Promise<TokenRefreshResult>;
224
+ }
225
+ interface AccountManagerInstance {
226
+ initialize(currentAuth: OAuthCredentials, client?: PluginClient): Promise<void>;
227
+ refresh(): Promise<void>;
228
+ getAccountCount(): number;
229
+ getAccounts(): ManagedAccount[];
230
+ getActiveAccount(): ManagedAccount | null;
231
+ setClient(client: PluginClient): void;
232
+ setRuntimeFactory(factory: RuntimeFactoryLike): void;
233
+ hasAnyUsableAccount(): boolean;
234
+ isRateLimited(account: ManagedAccount): boolean;
235
+ clearExpiredRateLimits(): void;
236
+ getMinWaitTime(): number;
237
+ selectAccount(): Promise<ManagedAccount | null>;
238
+ markRateLimited(uuid: string, backoffMs?: number): Promise<void>;
239
+ markRevoked(uuid: string): Promise<void>;
240
+ markSuccess(uuid: string): Promise<void>;
241
+ markAuthFailure(uuid: string, result: TokenRefreshResult): Promise<void>;
242
+ applyUsageCache(uuid: string, usage: UsageLimits): Promise<void>;
243
+ applyProfileCache(uuid: string, profile: ProfileData): Promise<void>;
244
+ ensureValidToken(uuid: string, client: PluginClient): Promise<TokenRefreshResult>;
245
+ validateNonActiveTokens(client: PluginClient): Promise<void>;
246
+ removeAccount(index: number): Promise<boolean>;
247
+ clearAllAccounts(): Promise<void>;
248
+ addAccount(auth: OAuthCredentials, email?: string): Promise<void>;
249
+ toggleEnabled(uuid: string): Promise<void>;
250
+ replaceAccountCredentials(uuid: string, auth: OAuthCredentials): Promise<void>;
251
+ retryAuth(uuid: string, client: PluginClient): Promise<TokenRefreshResult>;
252
+ }
253
+ interface AccountManagerClass {
254
+ new (store: AccountStore): AccountManagerInstance;
255
+ create(store: AccountStore, currentAuth: OAuthCredentials, client?: PluginClient): Promise<AccountManagerInstance>;
256
+ }
257
+ declare function createAccountManagerForProvider(dependencies: AccountManagerDependencies): AccountManagerClass;
258
+
259
+ type ClaimsMap = Record<string, {
260
+ pid: number;
261
+ at: number;
262
+ }>;
263
+ declare function readClaims(): Promise<ClaimsMap>;
264
+ declare function writeClaim(accountId: string): Promise<void>;
265
+ declare function releaseClaim(accountId: string): Promise<void>;
266
+ declare function isClaimedByOther(claims: ClaimsMap, accountId: string | undefined): boolean;
267
+
268
+ type CoreConfig = Pick<PluginConfig, "quiet_mode" | "debug">;
269
+ declare function initCoreConfig(filename: string): void;
270
+ declare function loadConfig(): Promise<PluginConfig>;
271
+ declare function getConfig(): PluginConfig;
272
+ declare function resetConfigCache(): void;
273
+ declare function setConfigGetter(getter: () => PluginConfig): void;
274
+ declare function updateConfigField<K extends keyof PluginConfig>(key: K, value: PluginConfig[K]): Promise<void>;
275
+
276
+ declare let ACCOUNTS_FILENAME: string;
277
+ declare function setAccountsFilename(filename: string): void;
278
+
279
+ interface ExecutorAccountManager {
280
+ getAccountCount(): number;
281
+ refresh(): Promise<void>;
282
+ selectAccount(): Promise<ManagedAccount | null>;
283
+ markSuccess(uuid: string): Promise<void>;
284
+ markAuthFailure(uuid: string, result: TokenRefreshResult): Promise<void>;
285
+ markRevoked(uuid: string): Promise<void>;
286
+ hasAnyUsableAccount(): boolean;
287
+ getMinWaitTime(): number;
288
+ }
289
+ interface ExecutorRuntimeFactory {
290
+ getRuntime(uuid: string): Promise<{
291
+ fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
292
+ }>;
293
+ invalidate(uuid: string): void;
294
+ }
295
+ interface ExecutorDependencies {
296
+ handleRateLimitResponse: (manager: unknown, client: PluginClient, account: ManagedAccount, response: Response) => Promise<void>;
297
+ formatWaitTime: (ms: number) => string;
298
+ sleep: (ms: number) => Promise<void>;
299
+ showToast: (client: PluginClient, message: string, variant: "info" | "warning" | "success" | "error") => Promise<void>;
300
+ getAccountLabel: (account: ManagedAccount) => string;
301
+ }
302
+ declare function createExecutorForProvider(providerName: string, dependencies: ExecutorDependencies): {
303
+ executeWithAccountRotation: (manager: ExecutorAccountManager, runtimeFactory: ExecutorRuntimeFactory, client: PluginClient, input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
304
+ };
305
+
306
+ interface ProactiveRefreshDependencies {
307
+ providerAuthId: string;
308
+ getConfig: () => PluginConfig;
309
+ refreshToken: (currentRefreshToken: string, accountId: string, client: PluginClient) => Promise<TokenRefreshResult>;
310
+ isTokenExpired: (account: Pick<StoredAccount, "accessToken" | "expiresAt">) => boolean;
311
+ debugLog: (client: PluginClient, message: string, extra?: Record<string, unknown>) => void;
312
+ }
313
+ interface ProactiveRefreshQueueInstance {
314
+ start(): void;
315
+ stop(): Promise<void>;
316
+ }
317
+ interface ProactiveRefreshQueueClass {
318
+ new (client: PluginClient, store: AccountStore, onInvalidate?: (uuid: string) => void): ProactiveRefreshQueueInstance;
319
+ }
320
+ declare function createProactiveRefreshQueueForProvider(dependencies: ProactiveRefreshDependencies): ProactiveRefreshQueueClass;
321
+
322
+ interface RateLimitDependencies {
323
+ fetchUsage: (accessToken: string, accountId?: string) => Promise<{
324
+ ok: true;
325
+ data: UsageLimits;
326
+ } | {
327
+ ok: false;
328
+ reason: string;
329
+ }>;
330
+ getConfig: () => Pick<PluginConfig, "default_retry_after_ms">;
331
+ formatWaitTime: (ms: number) => string;
332
+ getAccountLabel: (account: ManagedAccount) => string;
333
+ showToast: (client: PluginClient, message: string, variant: "info" | "warning" | "success" | "error") => Promise<void>;
334
+ }
335
+ interface RateLimitAccountManager {
336
+ markRateLimited(uuid: string, backoffMs?: number): Promise<void>;
337
+ applyUsageCache(uuid: string, usage: UsageLimits): Promise<void>;
338
+ getAccountCount(): number;
339
+ }
340
+ declare function createRateLimitHandlers(dependencies: RateLimitDependencies): {
341
+ retryAfterMsFromResponse: (response: Response) => number;
342
+ getResetMsFromUsage: (account: ManagedAccount) => number | null;
343
+ fetchUsageLimits: (accessToken: string, accountId?: string) => Promise<UsageLimits | null>;
344
+ handleRateLimitResponse: (manager: RateLimitAccountManager, client: PluginClient, account: ManagedAccount, response: Response) => Promise<void>;
345
+ };
346
+
347
+ declare function readStorageFromDisk(targetPath: string, backupOnCorrupt: boolean): Promise<AccountStorage | null>;
348
+ declare function deduplicateAccounts(accounts: StoredAccount[]): StoredAccount[];
349
+ declare function loadAccounts(): Promise<AccountStorage | null>;
350
+
351
+ declare function getConfigDir(): string;
352
+ declare function getErrorCode(error: unknown): string | undefined;
353
+ declare function formatWaitTime(ms: number): string;
354
+ declare function getAccountLabel(account: ManagedAccount): string;
355
+ declare function sleep(ms: number): Promise<void>;
356
+ declare function showToast(client: PluginClient, message: string, variant: "info" | "warning" | "success" | "error"): Promise<void>;
357
+ declare function debugLog(client: PluginClient, message: string, extra?: Record<string, unknown>): void;
358
+ declare function createMinimalClient(): PluginClient;
359
+ declare function getClearedOAuthBody(): {
360
+ type: "oauth";
361
+ refresh: string;
362
+ access: string;
363
+ expires: number;
364
+ };
365
+
366
+ /**
367
+ * Imports an existing OAuth credential from OpenCode's auth.json
368
+ * into the multi-account storage on first use.
369
+ *
370
+ * Only runs when storage has zero accounts. Does not modify auth.json.
371
+ *
372
+ * @param providerKey - The key in auth.json ("anthropic" or "openai")
373
+ * @param store - The AccountStore instance to import into
374
+ * @returns true if a credential was imported, false otherwise
375
+ */
376
+ declare function migrateFromAuthJson(providerKey: string, store: AccountStore): Promise<boolean>;
377
+
378
+ declare const ANSI: {
379
+ readonly hide: "\u001B[?25l";
380
+ readonly show: "\u001B[?25h";
381
+ readonly up: (n?: number) => string;
382
+ readonly down: (n?: number) => string;
383
+ readonly clearLine: "\u001B[2K";
384
+ readonly cyan: "\u001B[36m";
385
+ readonly green: "\u001B[32m";
386
+ readonly red: "\u001B[31m";
387
+ readonly yellow: "\u001B[33m";
388
+ readonly dim: "\u001B[2m";
389
+ readonly bold: "\u001B[1m";
390
+ readonly reset: "\u001B[0m";
391
+ };
392
+ type KeyAction = "up" | "down" | "enter" | "escape" | "escape-start" | null;
393
+ declare function parseKey(data: Buffer): KeyAction;
394
+ declare function isTTY(): boolean;
395
+
396
+ declare function confirm(message: string, defaultYes?: boolean): Promise<boolean>;
397
+
398
+ interface MenuItem<T = string> {
399
+ label: string;
400
+ value: T;
401
+ hint?: string;
402
+ disabled?: boolean;
403
+ separator?: boolean;
404
+ color?: "red" | "green" | "yellow" | "cyan";
405
+ }
406
+ interface SelectOptions {
407
+ message: string;
408
+ subtitle?: string;
409
+ }
410
+ declare function select<T>(items: MenuItem<T>[], options: SelectOptions): Promise<T | null>;
411
+
412
+ interface OAuthAdapterTransformConfig {
413
+ rewriteOpenCodeBranding: boolean;
414
+ addToolPrefix: boolean;
415
+ stripToolPrefixInResponse: boolean;
416
+ enableMessagesBetaQuery: boolean;
417
+ }
418
+ type OAuthAdapterPlanLabels = Record<string, string>;
419
+ interface OAuthAdapter {
420
+ id: string;
421
+ authProviderId: string;
422
+ modelDisplayName: string;
423
+ statusToolName: string;
424
+ authMethodLabel: string;
425
+ serviceLogName: string;
426
+ oauthClientId: string;
427
+ tokenEndpoint: string;
428
+ usageEndpoint: string;
429
+ profileEndpoint: string;
430
+ oauthBetaHeader: string;
431
+ requestBetaHeader: string;
432
+ cliUserAgent: string;
433
+ cliVersion: string;
434
+ billingSalt: string;
435
+ toolPrefix: string;
436
+ accountStorageFilename: string;
437
+ transform: OAuthAdapterTransformConfig;
438
+ planLabels: OAuthAdapterPlanLabels;
439
+ supported: boolean;
440
+ unsupportedReason?: string;
441
+ }
442
+
443
+ declare const anthropicOAuthAdapter: OAuthAdapter;
444
+
445
+ declare const openAIOAuthAdapter: OAuthAdapter;
446
+
447
+ declare const PoolConfigSchema: v.ObjectSchema<{
448
+ readonly name: v.StringSchema<undefined>;
449
+ readonly baseProvider: v.StringSchema<undefined>;
450
+ readonly members: v.ArraySchema<v.StringSchema<undefined>, undefined>;
451
+ readonly enabled: v.BooleanSchema<undefined>;
452
+ }, undefined>;
453
+ declare const ChainEntryConfigSchema: v.ObjectSchema<{
454
+ readonly pool: v.StringSchema<undefined>;
455
+ readonly model: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
456
+ readonly enabled: v.BooleanSchema<undefined>;
457
+ }, undefined>;
458
+ declare const ChainConfigSchema: v.ObjectSchema<{
459
+ readonly name: v.StringSchema<undefined>;
460
+ readonly entries: v.ArraySchema<v.ObjectSchema<{
461
+ readonly pool: v.StringSchema<undefined>;
462
+ readonly model: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
463
+ readonly enabled: v.BooleanSchema<undefined>;
464
+ }, undefined>, undefined>;
465
+ readonly enabled: v.BooleanSchema<undefined>;
466
+ }, undefined>;
467
+ declare const PoolChainConfigSchema: v.ObjectSchema<{
468
+ readonly pools: v.OptionalSchema<v.ArraySchema<v.ObjectSchema<{
469
+ readonly name: v.StringSchema<undefined>;
470
+ readonly baseProvider: v.StringSchema<undefined>;
471
+ readonly members: v.ArraySchema<v.StringSchema<undefined>, undefined>;
472
+ readonly enabled: v.BooleanSchema<undefined>;
473
+ }, undefined>, undefined>, readonly []>;
474
+ readonly chains: v.OptionalSchema<v.ArraySchema<v.ObjectSchema<{
475
+ readonly name: v.StringSchema<undefined>;
476
+ readonly entries: v.ArraySchema<v.ObjectSchema<{
477
+ readonly pool: v.StringSchema<undefined>;
478
+ readonly model: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
479
+ readonly enabled: v.BooleanSchema<undefined>;
480
+ }, undefined>, undefined>;
481
+ readonly enabled: v.BooleanSchema<undefined>;
482
+ }, undefined>, undefined>, readonly []>;
483
+ }, undefined>;
484
+ type PoolConfig = v.InferOutput<typeof PoolConfigSchema>;
485
+ type ChainEntryConfig = v.InferOutput<typeof ChainEntryConfigSchema>;
486
+ type ChainConfig = v.InferOutput<typeof ChainConfigSchema>;
487
+ type PoolChainConfig = v.InferOutput<typeof PoolChainConfigSchema>;
488
+ interface CascadeState {
489
+ prompt: string;
490
+ attemptedAccounts: Set<string>;
491
+ visitedChainIndexes: Set<number>;
492
+ }
493
+ interface FailoverCandidate {
494
+ poolName: string;
495
+ accountUuid: string;
496
+ source: "pool" | "chain";
497
+ chainIndex?: number;
498
+ }
499
+ interface FailoverSkip {
500
+ type: "pool_exhausted" | "chain_disabled" | "account_attempted" | "account_unavailable";
501
+ poolName: string;
502
+ reason: string;
503
+ detail?: string;
504
+ }
505
+
506
+ declare function loadPoolChainConfig(): Promise<PoolChainConfig>;
507
+ declare function savePoolChainConfig(config: PoolChainConfig): Promise<void>;
508
+
509
+ interface PoolAwareAccountManager {
510
+ getAccounts(): ManagedAccount[];
511
+ isRateLimited(account: ManagedAccount): boolean;
512
+ selectAccount(): Promise<ManagedAccount | null>;
513
+ }
514
+ interface BuildFailoverPlanOptions {
515
+ attemptedAccounts?: Set<string>;
516
+ visitedChainIndexes?: Set<number>;
517
+ }
518
+ interface FailoverPlan {
519
+ candidates: FailoverCandidate[];
520
+ skips: FailoverSkip[];
521
+ }
522
+ declare class PoolManager {
523
+ private poolsByName;
524
+ private exhaustedUntilByAccount;
525
+ private exhaustedCooldownMs;
526
+ constructor(options?: {
527
+ exhaustedCooldownMs?: number;
528
+ });
529
+ loadPools(configs: PoolConfig[]): void;
530
+ getPoolForAccount(accountUuid: string): PoolConfig | null;
531
+ getAvailableMembers(pool: PoolConfig, accountManager: PoolAwareAccountManager): string[];
532
+ markExhausted(accountUuid: string): void;
533
+ getNextMember(pool: PoolConfig, currentUuid: string | undefined, accountManager: PoolAwareAccountManager): Promise<string | null>;
534
+ buildFailoverPlan(currentAccount: Pick<ManagedAccount, "uuid" | "accountId"> | null, config: PoolChainConfig, accountManager: PoolAwareAccountManager, options?: BuildFailoverPlanOptions): Promise<FailoverPlan>;
535
+ private isExhausted;
536
+ private clearExpiredExhausted;
537
+ private selectPreferredMember;
538
+ }
539
+
540
+ declare class CascadeStateManager {
541
+ suppressNextStartTurn: boolean;
542
+ private cascadeState;
543
+ startTurn(prompt: string, currentAccountUuid?: string): CascadeState;
544
+ ensureCascadeState(prompt: string, currentAccountUuid?: string): CascadeState;
545
+ markAttempted(accountUuid: string): void;
546
+ markVisitedChainIndex(index: number): void;
547
+ clearCascadeState(): void;
548
+ getSnapshot(): CascadeState | null;
549
+ }
550
+
551
+ export { ACCOUNTS_FILENAME, ANSI, type AccountManagerClass, type AccountManagerDependencies, type AccountManagerInstance, type AccountSelectionStrategy, AccountSelectionStrategySchema, type AccountStorage, AccountStorageSchema, AccountStore, type BuildFailoverPlanOptions, type CascadeState, CascadeStateManager, type ChainConfig, ChainConfigSchema, type ChainEntryConfig, ChainEntryConfigSchema, type ClaimsMap, type CoreConfig, type CredentialRefreshPatch, CredentialRefreshPatchSchema, type DiskCredentials, type ExecutorAccountManager, type ExecutorDependencies, type ExecutorRuntimeFactory, type FailoverCandidate, type FailoverPlan, type FailoverSkip, type KeyAction, type ManagedAccount, type MenuItem, type OAuthAdapter, type OAuthAdapterPlanLabels, type OAuthAdapterTransformConfig, type OAuthCredentials, OAuthCredentialsSchema, type PluginClient, type PluginConfig, PluginConfigSchema, type PoolChainConfig, PoolChainConfigSchema, type PoolConfig, PoolConfigSchema, PoolManager, type ProactiveRefreshDependencies, type ProactiveRefreshQueueClass, type ProactiveRefreshQueueInstance, type ProfileData, type RateLimitAccountManager, type RateLimitDependencies, type RuntimeFactoryLike, type SelectOptions, type StoredAccount, StoredAccountSchema, TokenRefreshError, type TokenRefreshResult, type UsageLimitEntry, UsageLimitEntrySchema, type UsageLimits, UsageLimitsSchema, anthropicOAuthAdapter, confirm, createAccountManagerForProvider, createExecutorForProvider, createMinimalClient, createProactiveRefreshQueueForProvider, createRateLimitHandlers, debugLog, deduplicateAccounts, formatWaitTime, getAccountLabel, getClearedOAuthBody, getConfig, getConfigDir, getErrorCode, initCoreConfig, isClaimedByOther, isTTY, isTokenRefreshError, loadAccounts, loadConfig, loadPoolChainConfig, migrateFromAuthJson, openAIOAuthAdapter, parseKey, readClaims, readStorageFromDisk, releaseClaim, resetConfigCache, savePoolChainConfig, select, setAccountsFilename, setConfigGetter, showToast, sleep, updateConfigField, writeClaim };
package/dist/index.js CHANGED
@@ -1,20 +1,20 @@
1
1
  // src/account-manager.ts
2
- import { randomUUID } from "node:crypto";
2
+ import { randomUUID } from "crypto";
3
3
 
4
4
  // src/claims.ts
5
- import { promises as fs2 } from "node:fs";
6
- import { randomBytes as randomBytes2 } from "node:crypto";
7
- import { dirname as dirname2, join as join3 } from "node:path";
5
+ import { promises as fs2 } from "fs";
6
+ import { randomBytes as randomBytes2 } from "crypto";
7
+ import { dirname as dirname2, join as join3 } from "path";
8
8
 
9
9
  // src/utils.ts
10
- import { join as join2 } from "node:path";
11
- import { homedir as homedir2 } from "node:os";
10
+ import { join as join2 } from "path";
11
+ import { homedir as homedir2 } from "os";
12
12
 
13
13
  // src/config.ts
14
- import { promises as fs } from "node:fs";
15
- import { randomBytes } from "node:crypto";
16
- import { dirname, join } from "node:path";
17
- import { homedir } from "node:os";
14
+ import { promises as fs } from "fs";
15
+ import { randomBytes } from "crypto";
16
+ import { dirname, join } from "path";
17
+ import { homedir } from "os";
18
18
  import * as v2 from "valibot";
19
19
 
20
20
  // src/types.ts
@@ -380,6 +380,7 @@ function createAccountManagerForProvider(dependencies) {
380
380
  constructor(store) {
381
381
  this.store = store;
382
382
  }
383
+ store;
383
384
  cached = [];
384
385
  activeAccountUuid;
385
386
  client = null;
@@ -928,15 +929,15 @@ function createAccountManagerForProvider(dependencies) {
928
929
  }
929
930
 
930
931
  // src/account-store.ts
931
- import { promises as fs4 } from "node:fs";
932
- import { randomBytes as randomBytes3 } from "node:crypto";
933
- import { dirname as dirname4, join as join5 } from "node:path";
932
+ import { promises as fs4 } from "fs";
933
+ import { randomBytes as randomBytes3 } from "crypto";
934
+ import { dirname as dirname4, join as join5 } from "path";
934
935
  import lockfile from "proper-lockfile";
935
936
  import * as v4 from "valibot";
936
937
 
937
938
  // src/storage.ts
938
- import { promises as fs3 } from "node:fs";
939
- import { dirname as dirname3, join as join4 } from "node:path";
939
+ import { promises as fs3 } from "fs";
940
+ import { dirname as dirname3, join as join4 } from "path";
940
941
  import * as v3 from "valibot";
941
942
 
942
943
  // src/constants.ts
@@ -1380,6 +1381,9 @@ function createProactiveRefreshQueueForProvider(dependencies) {
1380
1381
  this.store = store;
1381
1382
  this.onInvalidate = onInvalidate;
1382
1383
  }
1384
+ client;
1385
+ store;
1386
+ onInvalidate;
1383
1387
  timeoutHandle = null;
1384
1388
  runToken = 0;
1385
1389
  inFlight = null;
@@ -1578,8 +1582,8 @@ function createRateLimitHandlers(dependencies) {
1578
1582
  }
1579
1583
 
1580
1584
  // src/auth-migration.ts
1581
- import { promises as fs5 } from "node:fs";
1582
- import { join as join6 } from "node:path";
1585
+ import { promises as fs5 } from "fs";
1586
+ import { join as join6 } from "path";
1583
1587
  var AUTH_JSON_FILENAME = "auth.json";
1584
1588
  function isValidOAuthCredential(value) {
1585
1589
  if (typeof value !== "object" || value === null) return false;
@@ -1925,9 +1929,9 @@ var PoolChainConfigSchema = v5.object({
1925
1929
  });
1926
1930
 
1927
1931
  // src/pool-config-store.ts
1928
- import { promises as fs6 } from "node:fs";
1929
- import { randomBytes as randomBytes4 } from "node:crypto";
1930
- import { dirname as dirname5, join as join7 } from "node:path";
1932
+ import { promises as fs6 } from "fs";
1933
+ import { randomBytes as randomBytes4 } from "crypto";
1934
+ import { dirname as dirname5, join as join7 } from "path";
1931
1935
  import lockfile2 from "proper-lockfile";
1932
1936
  import * as v6 from "valibot";
1933
1937
  var POOL_CONFIG_FILENAME = "multiauth-pools.json";
@@ -2348,3 +2352,4 @@ export {
2348
2352
  updateConfigField,
2349
2353
  writeClaim
2350
2354
  };
2355
+ //# sourceMappingURL=index.js.map