@@ -11,15 +11,23 @@ export type WhamUsageResponse = {
1111 email ?: string | null ;
1212 plan_type ?: string | null ;
1313 rate_limit ?: {
14- primary_window ?: { used_percent ?: number ; reset_at ?: number } ;
15- secondary_window ?: { used_percent ?: number ; reset_at ?: number } ;
16- tertiary_window ?: { used_percent ?: number ; reset_at ?: number } ;
14+ primary_window ?: WhamUsageWindow ;
15+ secondary_window ?: WhamUsageWindow ;
16+ tertiary_window ?: WhamUsageWindow ;
1717 } ;
1818 rate_limit_reset_credits ?: {
1919 available_count : number ;
2020 } | null ;
2121} ;
2222
23+ type WhamUsageWindow = {
24+ used_percent ?: number ;
25+ reset_at ?: number ;
26+ limit_window_seconds ?: number ;
27+ } ;
28+
29+ const MONTHLY_WINDOW_MIN_SECONDS = 28 * 24 * 60 * 60 ;
30+
2331const accountQuota = new Map < string , StoredAccountQuota > ( ) ;
2432
2533export const CODEX_UNKNOWN_USAGE_SCORE = 100 ;
@@ -49,6 +57,13 @@ function hasKnownQuotaValue(quota: Omit<StoredAccountQuota, "updatedAt">): boole
4957 . some ( value => typeof value === "number" && Number . isFinite ( value ) ) ;
5058}
5159
60+ function isExplicitMonthlyWindow ( window : WhamUsageWindow | undefined ) : boolean {
61+ const seconds = window ?. limit_window_seconds ;
62+ return typeof seconds === "number"
63+ && Number . isFinite ( seconds )
64+ && seconds >= MONTHLY_WINDOW_MIN_SECONDS ;
65+ }
66+
5267export function updateAccountQuota (
5368 accountId : string ,
5469 weekly : unknown ,
@@ -110,16 +125,30 @@ export function parseUsageQuota(data: WhamUsageResponse): Omit<StoredAccountQuot
110125
111126 const quota : Omit < StoredAccountQuota , "updatedAt" > = { } ;
112127 const thirtyDayOnly = data . plan_type ?. trim ( ) . toLowerCase ( ) === "go" || data . plan_type ?. trim ( ) . toLowerCase ( ) === "free" ;
113- // primary_window was the 5h window; it now carries weekly data for GPT plans.
114- // secondary_window is the legacy weekly source; prefer primary when present.
115- const primaryPercent = normalizeUsagePercent ( data . rate_limit . primary_window ?. used_percent ) ;
116- const secondaryPercent = normalizeUsagePercent ( data . rate_limit . secondary_window ?. used_percent ) ;
117- const weeklyPercent = primaryPercent ?? secondaryPercent ;
118- const monthlyPercent = normalizeUsagePercent ( data . rate_limit . tertiary_window ?. used_percent ) ;
119- const primaryResetAt = normalizeResetAt ( data . rate_limit . primary_window ?. reset_at ) ;
120- const secondaryResetAt = normalizeResetAt ( data . rate_limit . secondary_window ?. reset_at ) ;
121- const weeklyResetAt = primaryPercent !== undefined ? primaryResetAt : secondaryResetAt ;
122- const monthlyResetAt = normalizeResetAt ( data . rate_limit . tertiary_window ?. reset_at ) ;
128+ const primaryWindow = data . rate_limit . primary_window ;
129+ const secondaryWindow = data . rate_limit . secondary_window ;
130+ const tertiaryWindow = data . rate_limit . tertiary_window ;
131+ const primaryPercent = normalizeUsagePercent ( primaryWindow ?. used_percent ) ;
132+ const secondaryPercent = normalizeUsagePercent ( secondaryWindow ?. used_percent ) ;
133+ const tertiaryPercent = normalizeUsagePercent ( tertiaryWindow ?. used_percent ) ;
134+ const primaryResetAt = normalizeResetAt ( primaryWindow ?. reset_at ) ;
135+ const secondaryResetAt = normalizeResetAt ( secondaryWindow ?. reset_at ) ;
136+ const tertiaryResetAt = normalizeResetAt ( tertiaryWindow ?. reset_at ) ;
137+ const primaryIsMonthly = isExplicitMonthlyWindow ( primaryWindow ) ;
138+
139+ // [Decision Log]
140+ // - 목적과 의도: distinguish weekly and roughly monthly WHAM primary windows without plan-name guesses.
141+ // - 기존 구현 및 제약 조건: primary meant weekly, and older responses omit limit_window_seconds.
142+ // - 검토한 주요 대안: exact-duration matching, plan-specific mapping, and a duration lower bound.
143+ // - 선택한 방식: only an explicit primary duration of at least 28 days changes it to monthly.
144+ // - 다른 대안 대신 이 방식을 선택한 이유: it accepts calendar-month variance and preserves legacy payloads.
145+ // - 장점, 단점 및 영향: Team monthly quotas classify correctly; unknown durations remain weekly by design.
146+ const weeklyPercent = primaryIsMonthly ? secondaryPercent : primaryPercent ?? secondaryPercent ;
147+ const weeklyResetAt = primaryIsMonthly
148+ ? secondaryResetAt
149+ : primaryPercent !== undefined ? primaryResetAt : secondaryResetAt ;
150+ const monthlyPercent = primaryIsMonthly ? primaryPercent ?? tertiaryPercent : tertiaryPercent ;
151+ const monthlyResetAt = primaryIsMonthly && primaryPercent !== undefined ? primaryResetAt : tertiaryResetAt ;
123152 if ( thirtyDayOnly ) {
124153 if ( monthlyPercent !== undefined ) {
125154 quota . monthlyPercent = monthlyPercent ;
0 commit comments