Files
9router/open-sse/translator/concerns/thinking.js
Rafli Ahmad Zulfikar d1de324586 perf(usage): bound lastUsed overlay scan to 2-day window; reach max thinking tier
getUsageStats("all") shipped the entire usageHistory table to JS just to
refine lastUsed (~2s on 290K rows, on every statsEmitter update per SSE
listener). Bound the overlay to a 2-day indexed range scan; older entries
keep day-level lastUsed from usageDaily aggregates. Totals unaffected.

budgetToLevel now maps budgets > 80384 (midpoint of 32768/128000) to
"max" instead of clamping to "xhigh", so the top reasoning tier is
reachable from large budget_tokens requests.
2026-09-22 15:08:38 +07:00

57 lines
2.0 KiB
JavaScript

// Concern: reasoning_effort ↔ provider-native thinking config.
// Central source of truth for level↔budget maps (web-standard values).
// Provider-specific application lives in thinkingUnified.js; this file is maps-only.
// Discrete effort levels, ordered low→high.
export const EFFORT_LEVELS = ["minimal", "low", "medium", "high", "xhigh", "max"];
// Web-standard level → budget_tokens (Anthropic/Gemini docs).
export const LEVEL_TO_BUDGET = {
none: 0,
minimal: 512,
low: 1024,
medium: 8192,
high: 24576,
xhigh: 32768,
max: 128000,
};
// Returns budget_tokens for an effort level, or undefined if unknown.
// 0 means "no thinking"; undefined means "effort not recognized".
export function effortToBudget(effort) {
if (!effort) return undefined;
return LEVEL_TO_BUDGET[String(effort).toLowerCase()];
}
// OpenAI reasoning_effort → Gemini thinkingLevel (gemini-3 enum: minimal|low|medium|high).
// Gemini 3 cannot fully disable thinking; "none"/"off" map to "minimal".
export function effortToThinkingLevel(effort) {
const e = String(effort).toLowerCase().trim();
if (e === "none" || e === "off") return "minimal";
if (e === "xhigh" || e === "max") return "high";
return e;
}
// Numeric budget → nearest discrete level (reverse map via thresholds).
// Returns null when budget <= 0 (no reasoning).
// Thresholds are midpoints between LEVEL_TO_BUDGET values: max (128000) is
// reachable, with the xhigh/max boundary at the 32768/128000 midpoint (80384).
export function budgetToLevel(budget) {
const b = Number(budget);
if (!b || b <= 0) return null;
if (b <= 768) return "minimal";
if (b <= 4096) return "low";
if (b <= 16384) return "medium";
if (b <= 28672) return "high";
if (b <= 80384) return "xhigh";
return "max";
}
// Gemini thinkingBudget (numeric) → OpenAI reasoning_effort (antigravity reverse map).
export function budgetToEffort(budget) {
if (!budget || budget <= 0) return null;
if (budget <= 2048) return "low";
if (budget <= 16384) return "medium";
return "high";
}