mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
- Bug B1-B7: media UI m.kind||m.type, serviceKinds, gemini mediaPriority, schema kind, models/info lookup by kind - Dead code D1-D6: safeParseJSON, drop PROVIDER_ENDPOINTS, orphan fetcher, GITHUB_CONFIG derive, getProviderConfig internal, legacy kiro file - Translator concerns: toOpenAIUsage, toOpenAIFinish (gemini/kiro/ollama + fix kiro tool finish), thinking effort maps - Reorg helpers/ → concerns/ (logic) + formats/ (per-format) + schema/ (pure enums: roles/blocks/finishReasons/defaults) - Wire ~280 hardcoded role/block/finish/default literals to schema enums across 20+ files - collapseTextParts + extractTextContent dedup - Normalize translator fn names to openaiToXRequest / xToOpenAIResponse - Golden tests lock behavior; 0 regression (byte-for-byte providers/alias, 26=26 known fails) Co-authored-by: Cursor <cursoragent@cursor.com>
61 lines
3.0 KiB
JavaScript
61 lines
3.0 KiB
JavaScript
// Build OpenAI usage object. Caller computes prompt/completion/total (provider math).
|
|
// Optional details added only when > 0 (matches existing claude/gemini/codex behavior).
|
|
export function buildUsage({ promptTokens, completionTokens, totalTokens, cachedTokens = 0, cacheCreationTokens = 0, reasoningTokens = 0 }) {
|
|
const usage = { prompt_tokens: promptTokens, completion_tokens: completionTokens, total_tokens: totalTokens };
|
|
if (cachedTokens > 0 || cacheCreationTokens > 0) {
|
|
usage.prompt_tokens_details = {};
|
|
if (cachedTokens > 0) usage.prompt_tokens_details.cached_tokens = cachedTokens;
|
|
if (cacheCreationTokens > 0) usage.prompt_tokens_details.cache_creation_tokens = cacheCreationTokens;
|
|
}
|
|
if (reasoningTokens > 0) {
|
|
usage.completion_tokens_details = { reasoning_tokens: reasoningTokens };
|
|
}
|
|
return usage;
|
|
}
|
|
|
|
const n = (v) => (typeof v === "number" ? v : 0);
|
|
|
|
// Per-provider raw token field-map + math. Returns buildUsage() args (NOT the usage object).
|
|
// Keeps each provider's exact semantics: claude/gemini fold cache+reasoning, others don't.
|
|
const USAGE_EXTRACTORS = {
|
|
claude(raw) {
|
|
const input = n(raw.input_tokens), output = n(raw.output_tokens);
|
|
const cacheRead = n(raw.cache_read_input_tokens), cacheCreate = n(raw.cache_creation_input_tokens);
|
|
const prompt = input + cacheRead + cacheCreate;
|
|
return { promptTokens: prompt, completionTokens: output, totalTokens: prompt + output, cachedTokens: cacheRead, cacheCreationTokens: cacheCreate };
|
|
},
|
|
gemini(raw) {
|
|
const cached = n(raw.cachedContentTokenCount);
|
|
const prompt = n(raw.promptTokenCount);
|
|
const thoughts = n(raw.thoughtsTokenCount);
|
|
const total = n(raw.totalTokenCount);
|
|
let candidates = n(raw.candidatesTokenCount);
|
|
// Fallback: derive candidates from total when upstream omits it
|
|
if (candidates === 0 && total > 0) {
|
|
candidates = total - prompt - thoughts;
|
|
if (candidates < 0) candidates = 0;
|
|
}
|
|
return { promptTokens: prompt, completionTokens: candidates + thoughts, totalTokens: total, cachedTokens: cached, reasoningTokens: thoughts };
|
|
},
|
|
kiro(raw) {
|
|
const input = n(raw.inputTokens), output = n(raw.outputTokens);
|
|
return { promptTokens: input, completionTokens: output, totalTokens: input + output };
|
|
},
|
|
ollama(raw) {
|
|
const input = n(raw.prompt_eval_count), output = n(raw.eval_count);
|
|
return { promptTokens: input, completionTokens: output, totalTokens: input + output };
|
|
},
|
|
commandcode(raw) {
|
|
const input = n(raw.inputTokens), output = n(raw.outputTokens);
|
|
const total = typeof raw.totalTokens === "number" ? raw.totalTokens : input + output;
|
|
return { promptTokens: input, completionTokens: output, totalTokens: total };
|
|
},
|
|
};
|
|
|
|
// Convert provider-native usage object → OpenAI usage. Returns null if no extractor/raw.
|
|
export function toOpenAIUsage(raw, kind) {
|
|
const extract = USAGE_EXTRACTORS[kind];
|
|
if (!extract || !raw || typeof raw !== "object") return null;
|
|
return buildUsage(extract(raw));
|
|
}
|