refactor(ai-review 使用量): 抽出 lowerCaseKeys 簡化 header 正規化
This commit is contained in:
+8
-2
@@ -83,6 +83,13 @@ export function resetRunUsage() {
|
|||||||
/** 最近一次回應的速率配額(rate limit)快照,用來計算「當前視窗剩餘百分比」。 */
|
/** 最近一次回應的速率配額(rate limit)快照,用來計算「當前視窗剩餘百分比」。 */
|
||||||
const rateLimit = { hasData: false, remaining: null, limit: null, kind: null };
|
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 擷取速率配額剩餘量/上限。
|
* 從回應 header 擷取速率配額剩餘量/上限。
|
||||||
* 支援 OpenAI 相容(x-ratelimit-*-tokens)與 Anthropic(anthropic-ratelimit-tokens-*),
|
* 支援 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) {
|
export function recordRateLimit(headers) {
|
||||||
if (!headers || typeof headers !== 'object') return;
|
if (!headers || typeof headers !== 'object') return;
|
||||||
const h = {};
|
const h = lowerCaseKeys(headers);
|
||||||
for (const k of Object.keys(headers)) h[k.toLowerCase()] = headers[k];
|
|
||||||
|
|
||||||
let remaining = h['x-ratelimit-remaining-tokens'] ?? h['anthropic-ratelimit-tokens-remaining'];
|
let remaining = h['x-ratelimit-remaining-tokens'] ?? h['anthropic-ratelimit-tokens-remaining'];
|
||||||
let limit = h['x-ratelimit-limit-tokens'] ?? h['anthropic-ratelimit-tokens-limit'];
|
let limit = h['x-ratelimit-limit-tokens'] ?? h['anthropic-ratelimit-tokens-limit'];
|
||||||
|
|||||||
Reference in New Issue
Block a user