mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-10 17:22:17 +03:00
fix(types): normalize stream usage before cost calculation (#9977)
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
logUsage,
|
||||
addBufferToUsage,
|
||||
filterUsageForFormat,
|
||||
normalizeUsage as normalizeTokenUsage,
|
||||
} from "./usageTracking.ts";
|
||||
import {
|
||||
parseSSELine,
|
||||
@@ -1012,7 +1013,9 @@ export function createSSEStream(options: StreamOptions = {}) {
|
||||
// JSON.parse every SSE line will crash on `: x-omniroute-*` comment lines.
|
||||
if (!sseCommentsEnabled()) return;
|
||||
|
||||
const costUsd = finalUsage ? await calculateCost(provider, model, finalUsage) : 0;
|
||||
const costUsd = finalUsage
|
||||
? await calculateCost(provider, model, normalizeTokenUsage(finalUsage))
|
||||
: 0;
|
||||
const comment = buildOmniRouteSseMetadataComment({
|
||||
provider,
|
||||
model,
|
||||
|
||||
@@ -2,7 +2,19 @@ import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
const { extractUsageFromResponse } = await import("../../open-sse/handlers/usageExtractor.ts");
|
||||
const { extractUsage } = await import("../../open-sse/utils/usageTracking.ts");
|
||||
const { extractUsage, normalizeUsage } = await import("../../open-sse/utils/usageTracking.ts");
|
||||
|
||||
test("normalizeUsage keeps only finite numeric fields for stream cost calculation", () => {
|
||||
assert.deepEqual(
|
||||
normalizeUsage({
|
||||
input_tokens: "12",
|
||||
output_tokens: 3,
|
||||
total_tokens: Number.POSITIVE_INFINITY,
|
||||
input_tokens_details: { cached_tokens: 2 },
|
||||
}),
|
||||
{ input_tokens: 12, output_tokens: 3 }
|
||||
);
|
||||
});
|
||||
|
||||
test("extractUsageFromResponse reads OpenAI chat completion usage", () => {
|
||||
const usage = extractUsageFromResponse(
|
||||
|
||||
Reference in New Issue
Block a user