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

@@ -5,6 +5,7 @@
import { register } from "../index.js";
import { FORMATS } from "../formats.js";
import { buildChunk } from "../helpers/chunkBuilder.js";
import { buildUsage } from "../helpers/usageHelper.js";
/**
* Translate OpenAI chunk to Responses API events
@@ -466,18 +467,7 @@ export function openaiResponsesToOpenAIResponse(chunk, state) {
// Cache info is in input_tokens_details.cached_tokens
const cacheReadTokens = responseUsage.input_tokens_details?.cached_tokens || responseUsage.cache_read_input_tokens || 0;
state.usage = {
prompt_tokens: inputTokens,
completion_tokens: outputTokens,
total_tokens: inputTokens + outputTokens
};
// Add prompt_tokens_details if cache tokens exist
if (cacheReadTokens > 0) {
state.usage.prompt_tokens_details = {
cached_tokens: cacheReadTokens
};
}
state.usage = buildUsage({ promptTokens: inputTokens, completionTokens: outputTokens, totalTokens: inputTokens + outputTokens, cachedTokens: cacheReadTokens });
}
if (!state.finishReasonSent) {