Skip to content

Commit 7ccf217

Browse files
committed
refactor: extract runtime account state helpers
1 parent 1bccc76 commit 7ccf217

2 files changed

Lines changed: 57 additions & 50 deletions

File tree

index.ts

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ import {
165165
resolveAccountSelection,
166166
type TokenSuccessWithAccount,
167167
} from "./lib/runtime/account-selection.js";
168+
import {
169+
formatRateLimitEntry,
170+
getRateLimitResetTimeForFamily,
171+
resolveActiveIndex,
172+
} from "./lib/runtime/account-state.js";
168173
import { buildManualOAuthFlow } from "./lib/runtime/manual-oauth-flow.js";
169174
import {
170175
createRuntimeMetrics,
@@ -483,22 +488,6 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
483488
}
484489
};
485490

486-
const resolveActiveIndex = (
487-
storage: {
488-
activeIndex: number;
489-
activeIndexByFamily?: Partial<Record<ModelFamily, number>>;
490-
accounts: unknown[];
491-
},
492-
family: ModelFamily = "codex",
493-
): number => {
494-
const total = storage.accounts.length;
495-
if (total === 0) return 0;
496-
const rawCandidate =
497-
storage.activeIndexByFamily?.[family] ?? storage.activeIndex;
498-
const raw = Number.isFinite(rawCandidate) ? rawCandidate : 0;
499-
return Math.max(0, Math.min(raw, total - 1));
500-
};
501-
502491
const hydrateEmails = async (
503492
storage: AccountStorageV3 | null,
504493
): Promise<AccountStorageV3 | null> => {
@@ -571,40 +560,6 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
571560
return storage;
572561
};
573562

574-
const getRateLimitResetTimeForFamily = (
575-
account: { rateLimitResetTimes?: Record<string, number | undefined> },
576-
now: number,
577-
family: ModelFamily,
578-
): number | null => {
579-
const times = account.rateLimitResetTimes;
580-
if (!times) return null;
581-
582-
let minReset: number | null = null;
583-
const prefix = `${family}:`;
584-
for (const [key, value] of Object.entries(times)) {
585-
if (typeof value !== "number") continue;
586-
if (value <= now) continue;
587-
if (key !== family && !key.startsWith(prefix)) continue;
588-
if (minReset === null || value < minReset) {
589-
minReset = value;
590-
}
591-
}
592-
593-
return minReset;
594-
};
595-
596-
const formatRateLimitEntry = (
597-
account: { rateLimitResetTimes?: Record<string, number | undefined> },
598-
now: number,
599-
family: ModelFamily = "codex",
600-
): string | null => {
601-
const resetAt = getRateLimitResetTimeForFamily(account, now, family);
602-
if (typeof resetAt !== "number") return null;
603-
const remaining = resetAt - now;
604-
if (remaining <= 0) return null;
605-
return `resets in ${formatWaitTime(remaining)}`;
606-
};
607-
608563
const applyUiRuntimeFromConfig = (
609564
pluginConfig: ReturnType<typeof loadPluginConfig>,
610565
): UiRuntimeOptions => {

lib/runtime/account-state.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { formatWaitTime } from "../accounts.js";
2+
import type { ModelFamily } from "../prompts/codex.js";
3+
4+
export function resolveActiveIndex(
5+
storage: {
6+
activeIndex: number;
7+
activeIndexByFamily?: Partial<Record<ModelFamily, number>>;
8+
accounts: unknown[];
9+
},
10+
family: ModelFamily = "codex",
11+
): number {
12+
const total = storage.accounts.length;
13+
if (total === 0) return 0;
14+
const rawCandidate =
15+
storage.activeIndexByFamily?.[family] ?? storage.activeIndex;
16+
const raw = Number.isFinite(rawCandidate) ? rawCandidate : 0;
17+
return Math.max(0, Math.min(raw, total - 1));
18+
}
19+
20+
export function getRateLimitResetTimeForFamily(
21+
account: { rateLimitResetTimes?: Record<string, number | undefined> },
22+
now: number,
23+
family: ModelFamily,
24+
): number | null {
25+
const times = account.rateLimitResetTimes;
26+
if (!times) return null;
27+
28+
let minReset: number | null = null;
29+
const prefix = `${family}:`;
30+
for (const [key, value] of Object.entries(times)) {
31+
if (typeof value !== "number") continue;
32+
if (value <= now) continue;
33+
if (key !== family && !key.startsWith(prefix)) continue;
34+
if (minReset === null || value < minReset) {
35+
minReset = value;
36+
}
37+
}
38+
39+
return minReset;
40+
}
41+
42+
export function formatRateLimitEntry(
43+
account: { rateLimitResetTimes?: Record<string, number | undefined> },
44+
now: number,
45+
family: ModelFamily = "codex",
46+
): string | null {
47+
const resetAt = getRateLimitResetTimeForFamily(account, now, family);
48+
if (typeof resetAt !== "number") return null;
49+
const remaining = resetAt - now;
50+
if (remaining <= 0) return null;
51+
return `resets in ${formatWaitTime(remaining)}`;
52+
}

0 commit comments

Comments
 (0)