MCPcopy
hub / github.com/coder/mux / normalizeGatewayStreamUsage

Function normalizeGatewayStreamUsage

src/node/utils/gatewayStreamNormalization.ts:107–137  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

105 * Only transforms "finish" events; all other chunks pass through unchanged.
106 */
107export function normalizeGatewayStreamUsage(): TransformStream {
108 return new TransformStream({
109 transform(chunk: unknown, controller: TransformStreamDefaultController) {
110 if (typeof chunk !== "object" || chunk == null) {
111 controller.enqueue(chunk);
112 return;
113 }
114
115 const c = chunk as Record<string, unknown>;
116 if (c.type !== "finish") {
117 controller.enqueue(chunk);
118 return;
119 }
120
121 // Normalize usage: convert flat → v3 nested if needed
122 let usage = c.usage;
123 if (usage != null && !isV3Usage(usage)) {
124 usage = flatUsageToV3(usage as Record<string, unknown>);
125 }
126
127 // Normalize finishReason: convert string → { unified, raw } if needed
128 const finishReason = normalizeFinishReason(c.finishReason);
129
130 controller.enqueue({
131 ...c,
132 ...(usage != null && { usage }),
133 ...(finishReason != null && { finishReason }),
134 });
135 },
136 });
137}

Callers 2

collectStreamFunction · 0.90
_createModelCoreMethod · 0.90

Calls

no outgoing calls

Tested by 1

collectStreamFunction · 0.72