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:
decolua
2026-06-13 17:18:15 +07:00
parent 34ea763d80
commit 39278e9613
5 changed files with 27 additions and 45 deletions

View File

@@ -1,6 +1,7 @@
import { register } from "../index.js";
import { FORMATS } from "../formats.js";
import { buildChunk } from "../helpers/chunkBuilder.js";
import { buildUsage } from "../helpers/usageHelper.js";
// Build chunk meta for current gemini state
function chunkMeta(state) {
@@ -144,25 +145,7 @@ export function geminiToOpenAIResponse(chunk, state) {
// completion_tokens = candidatesTokenCount + thoughtsTokenCount (match Go code)
const completionTokens = candidatesTokens + thoughtsTokens;
state.usage = {
prompt_tokens: promptTokens,
completion_tokens: completionTokens,
total_tokens: totalTokens
};
// Add prompt_tokens_details if cached tokens exist
if (cachedTokens > 0) {
state.usage.prompt_tokens_details = {
cached_tokens: cachedTokens
};
}
// Add completion_tokens_details if reasoning tokens exist
if (thoughtsTokens > 0) {
state.usage.completion_tokens_details = {
reasoning_tokens: thoughtsTokens
};
}
state.usage = buildUsage({ promptTokens, completionTokens, totalTokens, cachedTokens, reasoningTokens: thoughtsTokens });
}
// Finish reason - include usage in final chunk