refactor(open-sse): extract buildUsage helper, dedup token-details (B2)
Add helpers/usageHelper.js for conditional prompt/completion token details. Apply to gemini/codex/claude response translators; keep each provider's token math intact. No behavior change; golden + gate: no regression. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
34ea763d80
commit
39278e9613
5 files changed
+27
-45
No files matched your search
@@ -0,0 +1,14 @@
|
||||
// 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;
|
||||
}
|
||||
Reference in new issue
Block a user