@neutrome/open-ai-router 0.1.10 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/app/factory.ts +2 -0
- package/src/router/execute.ts +18 -6
package/package.json
CHANGED
package/src/app/factory.ts
CHANGED
|
@@ -40,6 +40,7 @@ export type RouterAppOptions = {
|
|
|
40
40
|
requestIdFactory?: ExecutionRuntimeOptions["requestIdFactory"];
|
|
41
41
|
executionIdFactory?: ExecutionRuntimeOptions["executionIdFactory"];
|
|
42
42
|
upstreamTimeoutMs?: number;
|
|
43
|
+
suppressUsageToClient?: boolean;
|
|
43
44
|
};
|
|
44
45
|
|
|
45
46
|
export function createRouterApp(options: RouterAppOptions) {
|
|
@@ -127,6 +128,7 @@ export function createRouterApp(options: RouterAppOptions) {
|
|
|
127
128
|
...(options.transforms ? { transforms: options.transforms } : {}),
|
|
128
129
|
...(options.observe ? { observe: options.observe } : {}),
|
|
129
130
|
...(options.upstreamTimeoutMs !== undefined ? { upstreamTimeoutMs: options.upstreamTimeoutMs } : {}),
|
|
131
|
+
...(options.suppressUsageToClient ? { suppressUsageToClient: true } : {}),
|
|
130
132
|
...(onUsage ? { onUsage } : {}),
|
|
131
133
|
...(onProviderCall ? { onProviderCall } : {}),
|
|
132
134
|
...(forceStreaming ? { forceStreaming: true } : {}),
|
package/src/router/execute.ts
CHANGED
|
@@ -50,7 +50,7 @@ export type ProviderErrorReport = {
|
|
|
50
50
|
};
|
|
51
51
|
|
|
52
52
|
export type ProviderCallReport =
|
|
53
|
-
| { kind: "usage"; } & UsageReport
|
|
53
|
+
| { kind: "usage"; requestBytes: number; responseBytes: number } & UsageReport
|
|
54
54
|
| { kind: "error"; } & ProviderErrorReport;
|
|
55
55
|
|
|
56
56
|
export type RouterExecutionOptions = Pick<
|
|
@@ -60,6 +60,7 @@ export type RouterExecutionOptions = Pick<
|
|
|
60
60
|
executors?: Readonly<Record<string, Executor>>;
|
|
61
61
|
transforms?: Readonly<Record<string, ProgramTransform>>;
|
|
62
62
|
upstreamTimeoutMs?: number;
|
|
63
|
+
suppressUsageToClient?: boolean;
|
|
63
64
|
onUsage?: (report: UsageReport) => void;
|
|
64
65
|
onProviderCall?: (report: ProviderCallReport) => void;
|
|
65
66
|
};
|
|
@@ -191,7 +192,7 @@ export async function handleChatCompletions(
|
|
|
191
192
|
|
|
192
193
|
const startTime = Date.now();
|
|
193
194
|
if (isStreaming(request)) {
|
|
194
|
-
const userWantsUsage = requestedStreamUsage(requestBody);
|
|
195
|
+
const userWantsUsage = options.suppressUsageToClient ? false : requestedStreamUsage(requestBody);
|
|
195
196
|
const chunks = execution.stream(request, { target: resolved.target });
|
|
196
197
|
return await streamExecutionResponse(
|
|
197
198
|
"chat-completions",
|
|
@@ -269,6 +270,7 @@ async function handleProviderStyle(
|
|
|
269
270
|
resolved,
|
|
270
271
|
startTime,
|
|
271
272
|
options.onUsage,
|
|
273
|
+
!options.suppressUsageToClient,
|
|
272
274
|
);
|
|
273
275
|
}
|
|
274
276
|
|
|
@@ -319,6 +321,8 @@ export function createFetchProviderInvoker(
|
|
|
319
321
|
const timed = createUpstreamSignal(providerCtx.signal, upstreamTimeoutMs);
|
|
320
322
|
|
|
321
323
|
try {
|
|
324
|
+
const reqBody = emitProviderRequest(provider.style, setModel(request, providerCtx.target.model));
|
|
325
|
+
const reqBytes = reqBody.byteLength;
|
|
322
326
|
const upstream = await fetchProvider(
|
|
323
327
|
runtime,
|
|
324
328
|
ctx,
|
|
@@ -334,7 +338,7 @@ export function createFetchProviderInvoker(
|
|
|
334
338
|
|
|
335
339
|
const bytes = new Uint8Array(await upstream.arrayBuffer());
|
|
336
340
|
const response = parseProviderResponse(provider.style, bytes);
|
|
337
|
-
reportProviderUsage(onProviderCall, response, providerCtx.target);
|
|
341
|
+
reportProviderUsage(onProviderCall, response, providerCtx.target, reqBytes, bytes.byteLength);
|
|
338
342
|
return response;
|
|
339
343
|
} catch (error) {
|
|
340
344
|
throw normalizeUpstreamAbort(error, timed.signal);
|
|
@@ -350,6 +354,8 @@ export function createFetchProviderInvoker(
|
|
|
350
354
|
let buffer = "";
|
|
351
355
|
|
|
352
356
|
try {
|
|
357
|
+
const reqBody = emitProviderRequest(provider.style, setModel(request, providerCtx.target.model));
|
|
358
|
+
const reqBytes = reqBody.byteLength;
|
|
353
359
|
const upstream = await fetchProvider(
|
|
354
360
|
runtime,
|
|
355
361
|
ctx,
|
|
@@ -372,18 +378,20 @@ export function createFetchProviderInvoker(
|
|
|
372
378
|
|
|
373
379
|
reader = upstream.body.getReader();
|
|
374
380
|
let lastChunkWithUsage: Program | null = null;
|
|
381
|
+
let respBytes = 0;
|
|
375
382
|
while (true) {
|
|
376
383
|
const { done, value } = await reader.read();
|
|
377
384
|
if (done) {
|
|
378
385
|
break;
|
|
379
386
|
}
|
|
387
|
+
respBytes += value.byteLength;
|
|
380
388
|
buffer += decoder.decode(value, { stream: true });
|
|
381
389
|
const split = splitSseEvents(buffer);
|
|
382
390
|
buffer = split.remainder;
|
|
383
391
|
for (const event of split.events) {
|
|
384
392
|
for (const data of eventDataLines(event)) {
|
|
385
393
|
if (data === "[DONE]") {
|
|
386
|
-
reportProviderUsage(onProviderCall, lastChunkWithUsage, providerCtx.target);
|
|
394
|
+
reportProviderUsage(onProviderCall, lastChunkWithUsage, providerCtx.target, reqBytes, respBytes);
|
|
387
395
|
return;
|
|
388
396
|
}
|
|
389
397
|
if (data[0] !== "{") continue;
|
|
@@ -400,7 +408,7 @@ export function createFetchProviderInvoker(
|
|
|
400
408
|
if (buffer.length > 0) {
|
|
401
409
|
for (const data of eventDataLines(buffer)) {
|
|
402
410
|
if (data === "[DONE]") {
|
|
403
|
-
reportProviderUsage(onProviderCall, lastChunkWithUsage, providerCtx.target);
|
|
411
|
+
reportProviderUsage(onProviderCall, lastChunkWithUsage, providerCtx.target, reqBytes, respBytes);
|
|
404
412
|
return;
|
|
405
413
|
}
|
|
406
414
|
if (data[0] !== "{") continue;
|
|
@@ -412,7 +420,7 @@ export function createFetchProviderInvoker(
|
|
|
412
420
|
yield chunk;
|
|
413
421
|
}
|
|
414
422
|
}
|
|
415
|
-
reportProviderUsage(onProviderCall, lastChunkWithUsage, providerCtx.target);
|
|
423
|
+
reportProviderUsage(onProviderCall, lastChunkWithUsage, providerCtx.target, reqBytes, respBytes);
|
|
416
424
|
} catch (error) {
|
|
417
425
|
throw normalizeUpstreamAbort(error, timed.signal);
|
|
418
426
|
} finally {
|
|
@@ -766,6 +774,8 @@ function reportProviderUsage(
|
|
|
766
774
|
onProviderCall: ((report: ProviderCallReport) => void) | undefined,
|
|
767
775
|
response: Program | null,
|
|
768
776
|
target: Extract<import("@neutrome/lil-engine").ExecutionTarget, { kind: "provider" }>,
|
|
777
|
+
requestBytes: number,
|
|
778
|
+
responseBytes: number,
|
|
769
779
|
): void {
|
|
770
780
|
if (!onProviderCall) return;
|
|
771
781
|
const usage = response ? usageObject(response) : undefined;
|
|
@@ -782,6 +792,8 @@ function reportProviderUsage(
|
|
|
782
792
|
tokensInput,
|
|
783
793
|
tokensOutput,
|
|
784
794
|
cachedTokensInput,
|
|
795
|
+
requestBytes,
|
|
796
|
+
responseBytes,
|
|
785
797
|
});
|
|
786
798
|
} catch {}
|
|
787
799
|
}
|