From fb2624a215da24d9106fdee012bea79310ee5248 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Tue, 23 Jun 2026 13:51:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ai-review=20=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E9=87=8F):=20=E6=8A=BD=E5=87=BA=20lowerCaseKeys=20=E7=B0=A1?= =?UTF-8?q?=E5=8C=96=20header=20=E6=AD=A3=E8=A6=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/usage.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/usage.js b/app/usage.js index 0dab83a..5c24bba 100644 --- a/app/usage.js +++ b/app/usage.js @@ -83,6 +83,13 @@ export function resetRunUsage() { /** 最近一次回應的速率配額(rate limit)快照,用來計算「當前視窗剩餘百分比」。 */ const rateLimit = { hasData: false, remaining: null, limit: null, kind: null }; +/** 將物件的 key 全部轉小寫,方便對大小寫不敏感的 HTTP header 取值。 */ +function lowerCaseKeys(obj) { + const out = {}; + for (const k of Object.keys(obj)) out[k.toLowerCase()] = obj[k]; + return out; +} + /** * 從回應 header 擷取速率配額剩餘量/上限。 * 支援 OpenAI 相容(x-ratelimit-*-tokens)與 Anthropic(anthropic-ratelimit-tokens-*), @@ -90,8 +97,7 @@ const rateLimit = { hasData: false, remaining: null, limit: null, kind: null }; */ export function recordRateLimit(headers) { if (!headers || typeof headers !== 'object') return; - const h = {}; - for (const k of Object.keys(headers)) h[k.toLowerCase()] = headers[k]; + const h = lowerCaseKeys(headers); let remaining = h['x-ratelimit-remaining-tokens'] ?? h['anthropic-ratelimit-tokens-remaining']; let limit = h['x-ratelimit-limit-tokens'] ?? h['anthropic-ratelimit-tokens-limit'];