MCPcopy Index your code
hub / github.com/coder/mux / flatUsageToV3

Function flatUsageToV3

src/node/utils/gatewayStreamNormalization.ts:39–67  ·  view source on GitHub ↗
(usage: Record<string, unknown>)

Source from the content-addressed store, hash-verified

37 * Convert flat (v2-style) usage to v3 nested format.
38 */
39export function flatUsageToV3(usage: Record<string, unknown>): V3Usage {
40 const inputTokens = typeof usage.inputTokens === "number" ? usage.inputTokens : undefined;
41 const outputTokens = typeof usage.outputTokens === "number" ? usage.outputTokens : undefined;
42 const cachedInputTokens =
43 typeof usage.cachedInputTokens === "number" ? usage.cachedInputTokens : undefined;
44 const reasoningTokens =
45 typeof usage.reasoningTokens === "number" ? usage.reasoningTokens : undefined;
46
47 return {
48 inputTokens: {
49 total: inputTokens,
50 noCache:
51 inputTokens != null && cachedInputTokens != null
52 ? inputTokens - cachedInputTokens
53 : undefined,
54 cacheRead: cachedInputTokens,
55 cacheWrite: undefined,
56 },
57 outputTokens: {
58 total: outputTokens,
59 text:
60 outputTokens != null && reasoningTokens != null
61 ? outputTokens - reasoningTokens
62 : undefined,
63 reasoning: reasoningTokens,
64 },
65 raw: usage,
66 };
67}
68
69/**
70 * Normalize a finish-reason value to v3 format { unified, raw }.

Callers 3

transformFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected