@usagetap/sdk 0.4.0 → 0.6.0
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/README.md +26 -10
- package/dist/adapters/openai.d.cts +135 -2
- package/dist/adapters/openai.d.ts +135 -2
- package/dist/adapters/{openai.js → openai.mjs} +2 -2
- package/dist/adapters/openai.mjs.map +1 -0
- package/dist/adapters/openrouter.d.cts +2 -1
- package/dist/adapters/openrouter.d.ts +2 -1
- package/dist/adapters/{openrouter.js → openrouter.mjs} +2 -2
- package/dist/adapters/openrouter.mjs.map +1 -0
- package/dist/{openai-Ch7hN2vD.d.cts → client-DAY6cR0C.d.cts} +1 -134
- package/dist/{openai-Ch7hN2vD.d.ts → client-DAY6cR0C.d.ts} +1 -134
- package/dist/express/index.cjs +691 -0
- package/dist/express/index.cjs.map +1 -0
- package/dist/express/index.d.cts +93 -0
- package/dist/express/index.d.ts +93 -0
- package/dist/express/index.mjs +688 -0
- package/dist/express/index.mjs.map +1 -0
- package/dist/index.cjs +1 -778
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -94
- package/dist/index.d.ts +3 -94
- package/dist/{index.js → index.mjs} +4 -773
- package/dist/index.mjs.map +1 -0
- package/dist/openai/index.cjs +775 -0
- package/dist/openai/index.cjs.map +1 -0
- package/dist/openai/index.d.cts +4 -0
- package/dist/openai/index.d.ts +4 -0
- package/dist/openai/index.mjs +768 -0
- package/dist/openai/index.mjs.map +1 -0
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +1 -1
- package/dist/react/index.d.ts +1 -1
- package/dist/react/{index.js → index.mjs} +2 -2
- package/dist/react/index.mjs.map +1 -0
- package/package.json +23 -1
- package/dist/adapters/openai.js.map +0 -1
- package/dist/adapters/openrouter.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/react/index.js.map +0 -1
|
@@ -125,7 +125,7 @@ var IDEMPOTENCY_HEADER = "idempotency-key";
|
|
|
125
125
|
var SDK_HEADER = "x-usage-sdk";
|
|
126
126
|
var USER_AGENT = "UsageTapClient";
|
|
127
127
|
var CANONICAL_MEDIA_TYPE = "application/vnd.usagetap.v1+json";
|
|
128
|
-
var SDK_VERSION = "0.
|
|
128
|
+
var SDK_VERSION = "0.6.0" ;
|
|
129
129
|
var HAS_WINDOW = typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined";
|
|
130
130
|
var UsageTapClient = class {
|
|
131
131
|
apiKey;
|
|
@@ -637,742 +637,6 @@ function wrapEndCallError(error, correlationId) {
|
|
|
637
637
|
);
|
|
638
638
|
}
|
|
639
639
|
|
|
640
|
-
// src/adapters/openai.ts
|
|
641
|
-
function createOpenAIAdapter(init) {
|
|
642
|
-
const { client, usageTap } = init;
|
|
643
|
-
return {
|
|
644
|
-
async invoke(params) {
|
|
645
|
-
const result = await usageTap.withUsage(
|
|
646
|
-
params.begin,
|
|
647
|
-
async (ctx) => {
|
|
648
|
-
const response = await params.call(client, {
|
|
649
|
-
hints: ctx.begin.data.vendorHints,
|
|
650
|
-
begin: ctx.begin
|
|
651
|
-
});
|
|
652
|
-
tryInferUsage(response, ctx.begin.data.vendorHints, params.extractUsage, ctx);
|
|
653
|
-
return {
|
|
654
|
-
data: response,
|
|
655
|
-
begin: ctx.begin
|
|
656
|
-
};
|
|
657
|
-
},
|
|
658
|
-
params.withUsageOptions
|
|
659
|
-
);
|
|
660
|
-
return result;
|
|
661
|
-
},
|
|
662
|
-
async invokeStream(params) {
|
|
663
|
-
const result = await usageTap.withUsage(
|
|
664
|
-
params.begin,
|
|
665
|
-
async (ctx) => {
|
|
666
|
-
const { stream, onComplete } = await params.call(client, {
|
|
667
|
-
hints: ctx.begin.data.vendorHints,
|
|
668
|
-
begin: ctx.begin
|
|
669
|
-
});
|
|
670
|
-
const wrapped = wrapStreamForUsageTap(stream, async () => {
|
|
671
|
-
if (!onComplete) return;
|
|
672
|
-
try {
|
|
673
|
-
const maybeUsage = await onComplete();
|
|
674
|
-
if (maybeUsage) {
|
|
675
|
-
ctx.setUsage(maybeUsage);
|
|
676
|
-
}
|
|
677
|
-
} catch (error) {
|
|
678
|
-
ctx.setError({
|
|
679
|
-
code: "USAGE_FINALIZE_ERROR",
|
|
680
|
-
message: error instanceof Error ? error.message : String(error)
|
|
681
|
-
});
|
|
682
|
-
throw error;
|
|
683
|
-
}
|
|
684
|
-
}, ctx);
|
|
685
|
-
const finalize = async () => {
|
|
686
|
-
await wrapped.__usageTapFinalize?.();
|
|
687
|
-
};
|
|
688
|
-
return {
|
|
689
|
-
stream: wrapped,
|
|
690
|
-
begin: ctx.begin,
|
|
691
|
-
finalize
|
|
692
|
-
};
|
|
693
|
-
},
|
|
694
|
-
params.withUsageOptions
|
|
695
|
-
);
|
|
696
|
-
return result;
|
|
697
|
-
}
|
|
698
|
-
};
|
|
699
|
-
}
|
|
700
|
-
function toNextResponse(stream, options = {}) {
|
|
701
|
-
const mode = options.mode ?? "text";
|
|
702
|
-
const headers = new Headers(options.headers ?? {});
|
|
703
|
-
if (mode === "sse") {
|
|
704
|
-
headers.set("content-type", "text/event-stream; charset=utf-8");
|
|
705
|
-
headers.set("cache-control", "no-cache, no-transform");
|
|
706
|
-
headers.set("connection", "keep-alive");
|
|
707
|
-
headers.set("x-accel-buffering", "no");
|
|
708
|
-
} else {
|
|
709
|
-
headers.set("content-type", options.contentType ?? "text/plain; charset=utf-8");
|
|
710
|
-
}
|
|
711
|
-
const encoder = new TextEncoder();
|
|
712
|
-
let iterator;
|
|
713
|
-
const body = new ReadableStream({
|
|
714
|
-
async start(controller) {
|
|
715
|
-
try {
|
|
716
|
-
const getIterator = stream[Symbol.asyncIterator];
|
|
717
|
-
if (typeof getIterator !== "function") {
|
|
718
|
-
controller.close();
|
|
719
|
-
return;
|
|
720
|
-
}
|
|
721
|
-
iterator = getIterator.call(stream);
|
|
722
|
-
while (true) {
|
|
723
|
-
const result = await iterator.next();
|
|
724
|
-
if (result.done) {
|
|
725
|
-
break;
|
|
726
|
-
}
|
|
727
|
-
const text = chunkToText(result.value);
|
|
728
|
-
if (!text) {
|
|
729
|
-
continue;
|
|
730
|
-
}
|
|
731
|
-
if (mode === "sse") {
|
|
732
|
-
controller.enqueue(encoder.encode(formatSsePayload(text, options.sse)));
|
|
733
|
-
} else {
|
|
734
|
-
controller.enqueue(encoder.encode(text));
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
controller.close();
|
|
738
|
-
} catch (error) {
|
|
739
|
-
controller.error(error);
|
|
740
|
-
} finally {
|
|
741
|
-
await stream.__usageTapFinalize?.();
|
|
742
|
-
}
|
|
743
|
-
},
|
|
744
|
-
async cancel() {
|
|
745
|
-
if (!iterator) {
|
|
746
|
-
const getIterator = stream[Symbol.asyncIterator];
|
|
747
|
-
if (typeof getIterator === "function") {
|
|
748
|
-
iterator = getIterator.call(stream);
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
if (iterator && typeof iterator.return === "function") {
|
|
752
|
-
await iterator.return();
|
|
753
|
-
}
|
|
754
|
-
await stream.__usageTapFinalize?.();
|
|
755
|
-
}
|
|
756
|
-
});
|
|
757
|
-
return new Response(body, { headers });
|
|
758
|
-
}
|
|
759
|
-
async function pipeToResponse(stream, res, options = {}) {
|
|
760
|
-
const mode = options.mode ?? "text";
|
|
761
|
-
if (mode === "sse") {
|
|
762
|
-
setHeaderIfPossible(res, "Content-Type", "text/event-stream; charset=utf-8");
|
|
763
|
-
setHeaderIfPossible(res, "Cache-Control", "no-cache, no-transform");
|
|
764
|
-
setHeaderIfPossible(res, "Connection", "keep-alive");
|
|
765
|
-
setHeaderIfPossible(res, "X-Accel-Buffering", "no");
|
|
766
|
-
} else {
|
|
767
|
-
setHeaderIfPossible(res, "Content-Type", options.contentType ?? "text/plain; charset=utf-8");
|
|
768
|
-
}
|
|
769
|
-
const encoder = new TextEncoder();
|
|
770
|
-
const iterator = stream[Symbol.asyncIterator]();
|
|
771
|
-
try {
|
|
772
|
-
while (true) {
|
|
773
|
-
const result = await iterator.next();
|
|
774
|
-
if (result.done) {
|
|
775
|
-
break;
|
|
776
|
-
}
|
|
777
|
-
const text = chunkToText(result.value);
|
|
778
|
-
if (!text) {
|
|
779
|
-
continue;
|
|
780
|
-
}
|
|
781
|
-
const payload = mode === "sse" ? formatSsePayload(text, options.sse) : text;
|
|
782
|
-
res.write(Buffer.from(encoder.encode(payload)));
|
|
783
|
-
res.flush?.();
|
|
784
|
-
}
|
|
785
|
-
} finally {
|
|
786
|
-
res.end();
|
|
787
|
-
await stream.__usageTapFinalize?.();
|
|
788
|
-
}
|
|
789
|
-
}
|
|
790
|
-
var USAGETAP_CORRELATION_HEADER = "x-usage-correlation-id";
|
|
791
|
-
function wrapOpenAI(client, usageTap, options = {}) {
|
|
792
|
-
if (!client) {
|
|
793
|
-
throw new UsageTapError("USAGETAP_BAD_REQUEST", "wrapOpenAI requires an OpenAI client instance");
|
|
794
|
-
}
|
|
795
|
-
const defaultContext = options.defaultContext;
|
|
796
|
-
const applyVendorHints = options.applyVendorHints !== false;
|
|
797
|
-
const proxiedChat = client.chat ? createChatProxy(client.chat, usageTap, defaultContext, applyVendorHints) : void 0;
|
|
798
|
-
const proxiedResponses = typeof client.responses !== "undefined" ? createResponsesProxy(client.responses, usageTap, defaultContext, applyVendorHints) : void 0;
|
|
799
|
-
const handler = {
|
|
800
|
-
get(target, prop, receiver) {
|
|
801
|
-
if (prop === "chat" && proxiedChat) {
|
|
802
|
-
return proxiedChat;
|
|
803
|
-
}
|
|
804
|
-
if (prop === "responses" && typeof target.responses !== "undefined") {
|
|
805
|
-
return proxiedResponses ?? Reflect.get(target, prop, receiver);
|
|
806
|
-
}
|
|
807
|
-
if (prop === "toNextResponse") {
|
|
808
|
-
return toNextResponse;
|
|
809
|
-
}
|
|
810
|
-
if (prop === "pipeToResponse") {
|
|
811
|
-
return pipeToResponse;
|
|
812
|
-
}
|
|
813
|
-
if (prop === "unwrap") {
|
|
814
|
-
return () => target;
|
|
815
|
-
}
|
|
816
|
-
return Reflect.get(target, prop, receiver);
|
|
817
|
-
}
|
|
818
|
-
};
|
|
819
|
-
return new Proxy(client, handler);
|
|
820
|
-
}
|
|
821
|
-
function streamOpenAIRoute(usageTap, openai, options) {
|
|
822
|
-
if (!options?.getRequest) {
|
|
823
|
-
throw new UsageTapError("USAGETAP_BAD_REQUEST", "streamOpenAIRoute requires a getRequest function");
|
|
824
|
-
}
|
|
825
|
-
const wrapConfig = options.wrapOptions || options.defaultContext ? {
|
|
826
|
-
...options.wrapOptions ?? {},
|
|
827
|
-
defaultContext: options.defaultContext ?? options.wrapOptions?.defaultContext
|
|
828
|
-
} : void 0;
|
|
829
|
-
const wrappedClient = wrapConfig ? wrapOpenAI(openai, usageTap, wrapConfig) : wrapOpenAI(openai, usageTap);
|
|
830
|
-
return async (req) => {
|
|
831
|
-
const requestConfig = await options.getRequest(req);
|
|
832
|
-
const mergedParams = {
|
|
833
|
-
...requestConfig.params,
|
|
834
|
-
stream: true
|
|
835
|
-
};
|
|
836
|
-
const callOptions = {};
|
|
837
|
-
if (requestConfig.usageTap) {
|
|
838
|
-
callOptions.usageTap = requestConfig.usageTap;
|
|
839
|
-
}
|
|
840
|
-
if (requestConfig.withUsage) {
|
|
841
|
-
callOptions.withUsage = requestConfig.withUsage;
|
|
842
|
-
}
|
|
843
|
-
const stream = await wrappedClient.chat.completions.create(
|
|
844
|
-
mergedParams,
|
|
845
|
-
Object.keys(callOptions).length ? callOptions : void 0
|
|
846
|
-
);
|
|
847
|
-
const baseResponse = toNextResponse(stream, {
|
|
848
|
-
mode: options.stream?.mode ?? "sse",
|
|
849
|
-
headers: options.stream?.headers
|
|
850
|
-
});
|
|
851
|
-
const init = options.stream?.responseInit;
|
|
852
|
-
if (!init) {
|
|
853
|
-
return baseResponse;
|
|
854
|
-
}
|
|
855
|
-
const mergedHeaders = new Headers(baseResponse.headers);
|
|
856
|
-
if (init.headers) {
|
|
857
|
-
const extra = normalizeHeaders(init.headers);
|
|
858
|
-
for (const [key, value] of Object.entries(extra)) {
|
|
859
|
-
mergedHeaders.set(key, value);
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
return new Response(baseResponse.body, {
|
|
863
|
-
status: init.status ?? baseResponse.status,
|
|
864
|
-
statusText: init.statusText ?? baseResponse.statusText,
|
|
865
|
-
headers: mergedHeaders
|
|
866
|
-
});
|
|
867
|
-
};
|
|
868
|
-
}
|
|
869
|
-
function createChatProxy(resource, usageTap, defaultContext, applyVendorHints) {
|
|
870
|
-
const completions = createChatCompletionsProxy(
|
|
871
|
-
resource.completions,
|
|
872
|
-
usageTap,
|
|
873
|
-
defaultContext,
|
|
874
|
-
applyVendorHints
|
|
875
|
-
);
|
|
876
|
-
const handler = {
|
|
877
|
-
get(target, prop, receiver) {
|
|
878
|
-
if (prop === "completions") {
|
|
879
|
-
return completions;
|
|
880
|
-
}
|
|
881
|
-
return Reflect.get(target, prop, receiver);
|
|
882
|
-
}
|
|
883
|
-
};
|
|
884
|
-
return new Proxy(resource, handler);
|
|
885
|
-
}
|
|
886
|
-
function createResponsesProxy(resource, usageTap, defaultContext, applyVendorHints) {
|
|
887
|
-
if (!resource || typeof resource !== "object") {
|
|
888
|
-
return void 0;
|
|
889
|
-
}
|
|
890
|
-
if (!("create" in resource) || typeof resource.create !== "function") {
|
|
891
|
-
return resource;
|
|
892
|
-
}
|
|
893
|
-
const originalCreate = resource.create.bind(resource);
|
|
894
|
-
const wrappedCreate = (params, options) => {
|
|
895
|
-
const { requestOptions, usageContext, withUsage: withUsage2 } = splitUsageOptions(options);
|
|
896
|
-
const beginRequest = resolveBeginRequest(defaultContext, usageContext);
|
|
897
|
-
const wantsStream = isStreamingRequest(params);
|
|
898
|
-
return usageTap.withUsage(beginRequest, (ctx) => {
|
|
899
|
-
const finalParams = applyVendorHints ? applyResponsesVendorHints(params, ctx.begin.data.vendorHints) : params;
|
|
900
|
-
const request = attachCorrelationHeader(requestOptions, ctx.begin.correlationId);
|
|
901
|
-
if (wantsStream) {
|
|
902
|
-
const apiPromise2 = originalCreate(finalParams, request);
|
|
903
|
-
const wrappedPromise2 = transformApiPromise(apiPromise2, (rawStream) => {
|
|
904
|
-
ensureAsyncIterable(rawStream, "responses.create");
|
|
905
|
-
const wrappedStream = wrapStreamForUsageTap(rawStream, async () => {
|
|
906
|
-
const usage = await extractUsageFromStream(rawStream, ctx.begin.data.vendorHints);
|
|
907
|
-
if (usage) {
|
|
908
|
-
ctx.setUsage(usage);
|
|
909
|
-
}
|
|
910
|
-
}, ctx);
|
|
911
|
-
return wrappedStream;
|
|
912
|
-
});
|
|
913
|
-
return wrappedPromise2;
|
|
914
|
-
}
|
|
915
|
-
const apiPromise = originalCreate(finalParams, request);
|
|
916
|
-
const wrappedPromise = transformApiPromise(apiPromise, (response) => {
|
|
917
|
-
tryInferUsage(response, ctx.begin.data.vendorHints, void 0, ctx);
|
|
918
|
-
return response;
|
|
919
|
-
});
|
|
920
|
-
return wrappedPromise;
|
|
921
|
-
}, withUsage2);
|
|
922
|
-
};
|
|
923
|
-
const handler = {
|
|
924
|
-
get(target, prop, receiver) {
|
|
925
|
-
if (prop === "create") {
|
|
926
|
-
return wrappedCreate;
|
|
927
|
-
}
|
|
928
|
-
return Reflect.get(target, prop, receiver);
|
|
929
|
-
}
|
|
930
|
-
};
|
|
931
|
-
return new Proxy(resource, handler);
|
|
932
|
-
}
|
|
933
|
-
function createChatCompletionsProxy(resource, usageTap, defaultContext, applyVendorHints) {
|
|
934
|
-
const originalCreate = resource.create.bind(resource);
|
|
935
|
-
const streamCandidate = resource.stream;
|
|
936
|
-
const originalStream = typeof streamCandidate === "function" ? streamCandidate.bind(resource) : void 0;
|
|
937
|
-
const wrappedCreate = (params, options) => {
|
|
938
|
-
const { requestOptions, usageContext, withUsage: withUsage2 } = splitUsageOptions(options);
|
|
939
|
-
const beginRequest = resolveBeginRequest(defaultContext, usageContext);
|
|
940
|
-
const wantsStream = isStreamingRequest(params);
|
|
941
|
-
return usageTap.withUsage(beginRequest, (ctx) => {
|
|
942
|
-
const finalParams = applyVendorHints ? applyChatVendorHints(params, ctx.begin.data.vendorHints) : params;
|
|
943
|
-
const request = attachCorrelationHeader(requestOptions, ctx.begin.correlationId);
|
|
944
|
-
if (wantsStream) {
|
|
945
|
-
const apiPromise2 = originalCreate(finalParams, request);
|
|
946
|
-
const wrappedPromise2 = transformApiPromise(apiPromise2, (rawStream) => {
|
|
947
|
-
ensureAsyncIterable(rawStream, "chat.completions.create");
|
|
948
|
-
const wrappedStream2 = wrapStreamForUsageTap(rawStream, async () => {
|
|
949
|
-
const usage = await extractUsageFromStream(rawStream, ctx.begin.data.vendorHints);
|
|
950
|
-
if (usage) {
|
|
951
|
-
ctx.setUsage(usage);
|
|
952
|
-
}
|
|
953
|
-
}, ctx);
|
|
954
|
-
return wrappedStream2;
|
|
955
|
-
});
|
|
956
|
-
return wrappedPromise2;
|
|
957
|
-
}
|
|
958
|
-
const apiPromise = originalCreate(finalParams, request);
|
|
959
|
-
const wrappedPromise = transformApiPromise(apiPromise, (response) => {
|
|
960
|
-
tryInferUsage(response, ctx.begin.data.vendorHints, void 0, ctx);
|
|
961
|
-
return response;
|
|
962
|
-
});
|
|
963
|
-
return wrappedPromise;
|
|
964
|
-
}, withUsage2);
|
|
965
|
-
};
|
|
966
|
-
const wrappedStream = originalStream ? (params, options) => {
|
|
967
|
-
const { requestOptions, usageContext, withUsage: withUsage2 } = splitUsageOptions(options);
|
|
968
|
-
const beginRequest = resolveBeginRequest(defaultContext, usageContext);
|
|
969
|
-
return usageTap.withUsage(beginRequest, (ctx) => {
|
|
970
|
-
const finalParams = applyVendorHints ? applyChatVendorHints(params, ctx.begin.data.vendorHints) : params;
|
|
971
|
-
const request = attachCorrelationHeader(requestOptions, ctx.begin.correlationId);
|
|
972
|
-
const apiPromise = originalStream(finalParams, request);
|
|
973
|
-
const wrappedPromise = transformApiPromise(apiPromise, (rawStream) => {
|
|
974
|
-
ensureAsyncIterable(rawStream, "chat.completions.stream");
|
|
975
|
-
const wrappedStreamInner = wrapStreamForUsageTap(rawStream, async () => {
|
|
976
|
-
const usage = await extractUsageFromStream(rawStream, ctx.begin.data.vendorHints);
|
|
977
|
-
if (usage) {
|
|
978
|
-
ctx.setUsage(usage);
|
|
979
|
-
}
|
|
980
|
-
}, ctx);
|
|
981
|
-
return wrappedStreamInner;
|
|
982
|
-
});
|
|
983
|
-
return wrappedPromise;
|
|
984
|
-
}, withUsage2);
|
|
985
|
-
} : void 0;
|
|
986
|
-
const handler = {
|
|
987
|
-
get(target, prop, receiver) {
|
|
988
|
-
if (prop === "create") {
|
|
989
|
-
return wrappedCreate;
|
|
990
|
-
}
|
|
991
|
-
if (prop === "stream" && wrappedStream) {
|
|
992
|
-
return wrappedStream;
|
|
993
|
-
}
|
|
994
|
-
return Reflect.get(target, prop, receiver);
|
|
995
|
-
}
|
|
996
|
-
};
|
|
997
|
-
return new Proxy(resource, handler);
|
|
998
|
-
}
|
|
999
|
-
function splitUsageOptions(options) {
|
|
1000
|
-
if (!options || typeof options !== "object") {
|
|
1001
|
-
return {};
|
|
1002
|
-
}
|
|
1003
|
-
const { usageTap, withUsage: withUsage2, ...rest } = options;
|
|
1004
|
-
const requestOptions = Object.keys(rest).length ? cloneRequestOptions(rest) : void 0;
|
|
1005
|
-
return {
|
|
1006
|
-
requestOptions,
|
|
1007
|
-
usageContext: usageTap,
|
|
1008
|
-
withUsage: withUsage2
|
|
1009
|
-
};
|
|
1010
|
-
}
|
|
1011
|
-
function resolveBeginRequest(defaults, override) {
|
|
1012
|
-
const base = defaults ?? {};
|
|
1013
|
-
const current = override ?? {};
|
|
1014
|
-
const customerId = current.customerId ?? base.customerId;
|
|
1015
|
-
if (!customerId) {
|
|
1016
|
-
throw new UsageTapError(
|
|
1017
|
-
"USAGETAP_BAD_REQUEST",
|
|
1018
|
-
"wrapOpenAI requires usageTap.customerId (provide defaultContext or options.usageTap)"
|
|
1019
|
-
);
|
|
1020
|
-
}
|
|
1021
|
-
const tags = mergeTags(base.tags, current.tags);
|
|
1022
|
-
const begin = { customerId };
|
|
1023
|
-
const requested = current.requested ?? base.requested;
|
|
1024
|
-
if (requested) begin.requested = requested;
|
|
1025
|
-
const feature = current.feature ?? base.feature;
|
|
1026
|
-
if (feature) begin.feature = feature;
|
|
1027
|
-
const idempotency = current.idempotency ?? base.idempotency;
|
|
1028
|
-
if (idempotency) begin.idempotency = idempotency;
|
|
1029
|
-
const customerName = current.customerName ?? base.customerName;
|
|
1030
|
-
if (customerName) begin.customerName = customerName;
|
|
1031
|
-
const customerEmail = current.customerEmail ?? base.customerEmail;
|
|
1032
|
-
if (customerEmail) begin.customerEmail = customerEmail;
|
|
1033
|
-
if (tags?.length) {
|
|
1034
|
-
begin.tags = tags;
|
|
1035
|
-
}
|
|
1036
|
-
return begin;
|
|
1037
|
-
}
|
|
1038
|
-
function transformApiPromise(apiPromise, onResolve) {
|
|
1039
|
-
const resolvedPromise = Promise.resolve(apiPromise).then(onResolve);
|
|
1040
|
-
if (isObjectRecord(apiPromise)) {
|
|
1041
|
-
const proto = Object.getPrototypeOf(apiPromise);
|
|
1042
|
-
if (proto) {
|
|
1043
|
-
Object.setPrototypeOf(resolvedPromise, proto);
|
|
1044
|
-
}
|
|
1045
|
-
for (const key of Reflect.ownKeys(apiPromise)) {
|
|
1046
|
-
if (key === "then" || key === "catch" || key === "finally") {
|
|
1047
|
-
continue;
|
|
1048
|
-
}
|
|
1049
|
-
try {
|
|
1050
|
-
const descriptor = Object.getOwnPropertyDescriptor(apiPromise, key);
|
|
1051
|
-
if (descriptor) {
|
|
1052
|
-
Reflect.defineProperty(resolvedPromise, key, descriptor);
|
|
1053
|
-
}
|
|
1054
|
-
} catch {
|
|
1055
|
-
}
|
|
1056
|
-
}
|
|
1057
|
-
}
|
|
1058
|
-
return resolvedPromise;
|
|
1059
|
-
}
|
|
1060
|
-
function isObjectRecord(value) {
|
|
1061
|
-
return typeof value === "object" && value !== null;
|
|
1062
|
-
}
|
|
1063
|
-
function cloneRecord(value) {
|
|
1064
|
-
return isObjectRecord(value) ? { ...value } : {};
|
|
1065
|
-
}
|
|
1066
|
-
function isStringTuple(value) {
|
|
1067
|
-
return Array.isArray(value) && value.length >= 2 && typeof value[0] === "string" && typeof value[1] === "string";
|
|
1068
|
-
}
|
|
1069
|
-
function cloneRequestOptions(source) {
|
|
1070
|
-
const clone = { ...source };
|
|
1071
|
-
if ("headers" in clone) {
|
|
1072
|
-
clone.headers = normalizeHeaders(clone.headers);
|
|
1073
|
-
}
|
|
1074
|
-
return clone;
|
|
1075
|
-
}
|
|
1076
|
-
function attachCorrelationHeader(options, correlationId) {
|
|
1077
|
-
const normalized = normalizeHeaders(options?.headers);
|
|
1078
|
-
if (correlationId && !normalized[USAGETAP_CORRELATION_HEADER]) {
|
|
1079
|
-
normalized[USAGETAP_CORRELATION_HEADER] = correlationId;
|
|
1080
|
-
}
|
|
1081
|
-
if (!options) {
|
|
1082
|
-
return Object.keys(normalized).length ? { headers: normalized } : void 0;
|
|
1083
|
-
}
|
|
1084
|
-
const next = { ...options };
|
|
1085
|
-
if (Object.keys(normalized).length) {
|
|
1086
|
-
next.headers = normalized;
|
|
1087
|
-
}
|
|
1088
|
-
return next;
|
|
1089
|
-
}
|
|
1090
|
-
function normalizeHeaders(headers) {
|
|
1091
|
-
if (!headers) {
|
|
1092
|
-
return {};
|
|
1093
|
-
}
|
|
1094
|
-
if (headers instanceof Headers) {
|
|
1095
|
-
const result = {};
|
|
1096
|
-
headers.forEach((value, key) => {
|
|
1097
|
-
result[key.toLowerCase()] = value;
|
|
1098
|
-
});
|
|
1099
|
-
return result;
|
|
1100
|
-
}
|
|
1101
|
-
if (Array.isArray(headers)) {
|
|
1102
|
-
const result = {};
|
|
1103
|
-
for (const entry of headers) {
|
|
1104
|
-
if (!isStringTuple(entry)) {
|
|
1105
|
-
continue;
|
|
1106
|
-
}
|
|
1107
|
-
const [key, value] = entry;
|
|
1108
|
-
result[key.toLowerCase()] = value;
|
|
1109
|
-
}
|
|
1110
|
-
return result;
|
|
1111
|
-
}
|
|
1112
|
-
if (isObjectRecord(headers)) {
|
|
1113
|
-
const result = {};
|
|
1114
|
-
const record = headers;
|
|
1115
|
-
for (const key of Object.keys(record)) {
|
|
1116
|
-
const value = record[key];
|
|
1117
|
-
if (value !== void 0 && value !== null) {
|
|
1118
|
-
result[key.toLowerCase()] = String(value);
|
|
1119
|
-
}
|
|
1120
|
-
}
|
|
1121
|
-
return result;
|
|
1122
|
-
}
|
|
1123
|
-
return {};
|
|
1124
|
-
}
|
|
1125
|
-
function mergeTags(a, b) {
|
|
1126
|
-
const values = [...a ?? [], ...b ?? []].map((value) => typeof value === "string" ? value.trim() : "").filter(Boolean);
|
|
1127
|
-
if (!values.length) {
|
|
1128
|
-
return void 0;
|
|
1129
|
-
}
|
|
1130
|
-
return dedupeStrings2(values);
|
|
1131
|
-
}
|
|
1132
|
-
function dedupeStrings2(values) {
|
|
1133
|
-
return Array.from(new Set(values));
|
|
1134
|
-
}
|
|
1135
|
-
function isStreamingRequest(params) {
|
|
1136
|
-
if (!params || typeof params !== "object") {
|
|
1137
|
-
return false;
|
|
1138
|
-
}
|
|
1139
|
-
const stream = params.stream;
|
|
1140
|
-
if (typeof stream === "boolean") {
|
|
1141
|
-
return stream;
|
|
1142
|
-
}
|
|
1143
|
-
return stream != null;
|
|
1144
|
-
}
|
|
1145
|
-
function applyChatVendorHints(params, hints) {
|
|
1146
|
-
if (!hints) {
|
|
1147
|
-
return params;
|
|
1148
|
-
}
|
|
1149
|
-
const next = cloneRecord(params);
|
|
1150
|
-
if (hints.preferredModel && (next.model === void 0 || next.model === null)) {
|
|
1151
|
-
next.model = hints.preferredModel;
|
|
1152
|
-
}
|
|
1153
|
-
if (typeof hints.maxResponseTokens === "number" && next.max_tokens == null) {
|
|
1154
|
-
next.max_tokens = hints.maxResponseTokens;
|
|
1155
|
-
}
|
|
1156
|
-
if (typeof hints.maxInputTokens === "number" && next.max_input_tokens == null) {
|
|
1157
|
-
next.max_input_tokens = hints.maxInputTokens;
|
|
1158
|
-
}
|
|
1159
|
-
return next;
|
|
1160
|
-
}
|
|
1161
|
-
function applyResponsesVendorHints(params, hints) {
|
|
1162
|
-
if (!hints) {
|
|
1163
|
-
return params;
|
|
1164
|
-
}
|
|
1165
|
-
const next = cloneRecord(params);
|
|
1166
|
-
if (hints.preferredModel && (next.model === void 0 || next.model === null)) {
|
|
1167
|
-
next.model = hints.preferredModel;
|
|
1168
|
-
}
|
|
1169
|
-
if (typeof hints.maxResponseTokens === "number" && next.max_output_tokens == null) {
|
|
1170
|
-
next.max_output_tokens = hints.maxResponseTokens;
|
|
1171
|
-
}
|
|
1172
|
-
return next;
|
|
1173
|
-
}
|
|
1174
|
-
async function extractUsageFromStream(stream, hints) {
|
|
1175
|
-
const finalPayload = await resolveStreamFinalPayload(stream);
|
|
1176
|
-
if (!finalPayload) {
|
|
1177
|
-
return void 0;
|
|
1178
|
-
}
|
|
1179
|
-
return inferUsageFromResponse(finalPayload, hints);
|
|
1180
|
-
}
|
|
1181
|
-
async function resolveStreamFinalPayload(stream) {
|
|
1182
|
-
if (!stream || typeof stream !== "object") {
|
|
1183
|
-
return void 0;
|
|
1184
|
-
}
|
|
1185
|
-
const candidate = stream;
|
|
1186
|
-
if (typeof candidate.finalChatCompletion === "function") {
|
|
1187
|
-
return candidate.finalChatCompletion();
|
|
1188
|
-
}
|
|
1189
|
-
if (typeof candidate.finalResponse === "function") {
|
|
1190
|
-
return candidate.finalResponse();
|
|
1191
|
-
}
|
|
1192
|
-
if (typeof candidate.finalCompletion === "function") {
|
|
1193
|
-
return candidate.finalCompletion();
|
|
1194
|
-
}
|
|
1195
|
-
if (typeof candidate.finalContent === "function") {
|
|
1196
|
-
return candidate.finalContent();
|
|
1197
|
-
}
|
|
1198
|
-
return void 0;
|
|
1199
|
-
}
|
|
1200
|
-
function ensureAsyncIterable(value, label) {
|
|
1201
|
-
if (!value || typeof value !== "object" || typeof value[Symbol.asyncIterator] !== "function") {
|
|
1202
|
-
throw new UsageTapError(
|
|
1203
|
-
"USAGETAP_BAD_REQUEST",
|
|
1204
|
-
`${label} expected an async iterable stream but received ${typeof value}`
|
|
1205
|
-
);
|
|
1206
|
-
}
|
|
1207
|
-
}
|
|
1208
|
-
function chunkToText(chunk) {
|
|
1209
|
-
if (chunk === void 0 || chunk === null) {
|
|
1210
|
-
return "";
|
|
1211
|
-
}
|
|
1212
|
-
if (typeof chunk === "string") {
|
|
1213
|
-
return chunk;
|
|
1214
|
-
}
|
|
1215
|
-
if (typeof chunk === "object") {
|
|
1216
|
-
const candidate = chunk;
|
|
1217
|
-
const delta = candidate.choices?.[0]?.delta;
|
|
1218
|
-
const content = delta?.content ?? candidate.content;
|
|
1219
|
-
if (typeof content === "string") {
|
|
1220
|
-
return content;
|
|
1221
|
-
}
|
|
1222
|
-
if (Array.isArray(content)) {
|
|
1223
|
-
return content.map((entry) => {
|
|
1224
|
-
if (!entry) return "";
|
|
1225
|
-
if (typeof entry === "string") return entry;
|
|
1226
|
-
if (typeof entry.text === "string") return entry.text;
|
|
1227
|
-
return "";
|
|
1228
|
-
}).join("");
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
return String(chunk);
|
|
1232
|
-
}
|
|
1233
|
-
function formatSsePayload(text, options) {
|
|
1234
|
-
if (!text) {
|
|
1235
|
-
return "";
|
|
1236
|
-
}
|
|
1237
|
-
const lines = text.split(/\r?\n/);
|
|
1238
|
-
const eventLine = options?.event ? `event: ${options.event}
|
|
1239
|
-
` : "";
|
|
1240
|
-
const retryLine = options?.retry ? `retry: ${options.retry}
|
|
1241
|
-
` : "";
|
|
1242
|
-
const dataLines = lines.map((line) => `data: ${line}`).join("\n");
|
|
1243
|
-
return `${eventLine}${retryLine}${dataLines}
|
|
1244
|
-
|
|
1245
|
-
`;
|
|
1246
|
-
}
|
|
1247
|
-
function setHeaderIfPossible(res, key, value) {
|
|
1248
|
-
if (typeof res.setHeader === "function" && res.headersSent !== true) {
|
|
1249
|
-
res.setHeader(key, value);
|
|
1250
|
-
}
|
|
1251
|
-
}
|
|
1252
|
-
function tryInferUsage(response, hints, extractor, ctx) {
|
|
1253
|
-
const explicit = extractor?.(response);
|
|
1254
|
-
const inferred = explicit ?? inferUsageFromResponse(response, hints);
|
|
1255
|
-
if (inferred) {
|
|
1256
|
-
ctx.setUsage(inferred);
|
|
1257
|
-
}
|
|
1258
|
-
}
|
|
1259
|
-
function inferUsageFromResponse(response, hints) {
|
|
1260
|
-
if (!response || typeof response !== "object") {
|
|
1261
|
-
return void 0;
|
|
1262
|
-
}
|
|
1263
|
-
const candidate = response;
|
|
1264
|
-
if (!candidate.usage) {
|
|
1265
|
-
return void 0;
|
|
1266
|
-
}
|
|
1267
|
-
return {
|
|
1268
|
-
modelUsed: candidate.model ?? hints?.preferredModel,
|
|
1269
|
-
inputTokens: candidate.usage.prompt_tokens,
|
|
1270
|
-
responseTokens: candidate.usage.completion_tokens,
|
|
1271
|
-
cachedTokens: candidate.usage.cached_tokens
|
|
1272
|
-
};
|
|
1273
|
-
}
|
|
1274
|
-
function wrapStreamForUsageTap(source, finalize, ctx) {
|
|
1275
|
-
const getIterator = source[Symbol.asyncIterator];
|
|
1276
|
-
if (typeof getIterator !== "function") {
|
|
1277
|
-
throw new TypeError("Stream is not async iterable");
|
|
1278
|
-
}
|
|
1279
|
-
const iterator = getIterator.call(source);
|
|
1280
|
-
let completed = false;
|
|
1281
|
-
const invokeFinalize = async () => {
|
|
1282
|
-
if (completed) return;
|
|
1283
|
-
completed = true;
|
|
1284
|
-
try {
|
|
1285
|
-
await finalize();
|
|
1286
|
-
} catch (error) {
|
|
1287
|
-
ctx.setError({
|
|
1288
|
-
code: "USAGE_FINALIZE_ERROR",
|
|
1289
|
-
message: error instanceof Error ? error.message : String(error)
|
|
1290
|
-
});
|
|
1291
|
-
throw error;
|
|
1292
|
-
}
|
|
1293
|
-
};
|
|
1294
|
-
const prototype = Object.getPrototypeOf(source) ?? Object.prototype;
|
|
1295
|
-
const wrapped = Object.create(prototype);
|
|
1296
|
-
for (const key of Reflect.ownKeys(source)) {
|
|
1297
|
-
try {
|
|
1298
|
-
const descriptor = Object.getOwnPropertyDescriptor(source, key);
|
|
1299
|
-
if (descriptor) {
|
|
1300
|
-
Object.defineProperty(wrapped, key, descriptor);
|
|
1301
|
-
}
|
|
1302
|
-
} catch {
|
|
1303
|
-
}
|
|
1304
|
-
}
|
|
1305
|
-
Object.defineProperty(wrapped, Symbol.asyncIterator, {
|
|
1306
|
-
value() {
|
|
1307
|
-
return this;
|
|
1308
|
-
},
|
|
1309
|
-
configurable: true
|
|
1310
|
-
});
|
|
1311
|
-
Object.defineProperty(wrapped, "next", {
|
|
1312
|
-
value: async (...args) => {
|
|
1313
|
-
try {
|
|
1314
|
-
const result = await iterator.next(...args);
|
|
1315
|
-
if (result.done) {
|
|
1316
|
-
await invokeFinalize();
|
|
1317
|
-
}
|
|
1318
|
-
return result;
|
|
1319
|
-
} catch (error) {
|
|
1320
|
-
await invokeFinalize().catch(() => void 0);
|
|
1321
|
-
throw error;
|
|
1322
|
-
}
|
|
1323
|
-
},
|
|
1324
|
-
configurable: true,
|
|
1325
|
-
writable: true
|
|
1326
|
-
});
|
|
1327
|
-
Object.defineProperty(wrapped, "return", {
|
|
1328
|
-
value: async (value) => {
|
|
1329
|
-
if (typeof iterator.return === "function") {
|
|
1330
|
-
const rawResult = await iterator.return(value);
|
|
1331
|
-
if (!isIteratorResult(rawResult)) {
|
|
1332
|
-
throw new TypeError("Iterator.return() returned an invalid result");
|
|
1333
|
-
}
|
|
1334
|
-
await invokeFinalize();
|
|
1335
|
-
return rawResult;
|
|
1336
|
-
}
|
|
1337
|
-
await invokeFinalize();
|
|
1338
|
-
return { done: true, value };
|
|
1339
|
-
},
|
|
1340
|
-
configurable: true,
|
|
1341
|
-
writable: true
|
|
1342
|
-
});
|
|
1343
|
-
Object.defineProperty(wrapped, "throw", {
|
|
1344
|
-
value: async (error) => {
|
|
1345
|
-
if (typeof iterator.throw === "function") {
|
|
1346
|
-
const rawResult = await iterator.throw(error);
|
|
1347
|
-
if (!isIteratorResult(rawResult)) {
|
|
1348
|
-
throw new TypeError("Iterator.throw() returned an invalid result");
|
|
1349
|
-
}
|
|
1350
|
-
await invokeFinalize();
|
|
1351
|
-
return rawResult;
|
|
1352
|
-
}
|
|
1353
|
-
await invokeFinalize();
|
|
1354
|
-
throw error;
|
|
1355
|
-
},
|
|
1356
|
-
configurable: true,
|
|
1357
|
-
writable: true
|
|
1358
|
-
});
|
|
1359
|
-
Object.defineProperty(wrapped, "__usageTapFinalize", {
|
|
1360
|
-
value: async () => {
|
|
1361
|
-
await invokeFinalize();
|
|
1362
|
-
},
|
|
1363
|
-
configurable: true
|
|
1364
|
-
});
|
|
1365
|
-
return wrapped;
|
|
1366
|
-
}
|
|
1367
|
-
function isIteratorResult(value) {
|
|
1368
|
-
return isObjectRecord(value) && "done" in value;
|
|
1369
|
-
}
|
|
1370
|
-
|
|
1371
|
-
// src/adapters/openrouter.ts
|
|
1372
|
-
function createOpenRouterAdapter(init) {
|
|
1373
|
-
return createOpenAIAdapter(init);
|
|
1374
|
-
}
|
|
1375
|
-
|
|
1376
640
|
// src/adapters/fetch-wrapper.ts
|
|
1377
641
|
function isJsonRecord(value) {
|
|
1378
642
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -1616,39 +880,6 @@ async function finalizeCall(callState, usageTap, error, usage) {
|
|
|
1616
880
|
}
|
|
1617
881
|
}
|
|
1618
882
|
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
try {
|
|
1623
|
-
const customerId = await options.getCustomerId(req);
|
|
1624
|
-
const feature = options.getFeature ? await options.getFeature(req) : void 0;
|
|
1625
|
-
const tags = options.getTags ? await options.getTags(req) : void 0;
|
|
1626
|
-
const baseContext = {
|
|
1627
|
-
customerId,
|
|
1628
|
-
feature,
|
|
1629
|
-
tags,
|
|
1630
|
-
...options.defaultContext
|
|
1631
|
-
};
|
|
1632
|
-
req.usageTap = {
|
|
1633
|
-
openai: (client, contextOverride, wrapOptions) => {
|
|
1634
|
-
const mergedContext = { ...baseContext, ...contextOverride };
|
|
1635
|
-
return wrapOpenAI(client, usageTap, {
|
|
1636
|
-
...wrapOptions,
|
|
1637
|
-
defaultContext: mergedContext
|
|
1638
|
-
});
|
|
1639
|
-
},
|
|
1640
|
-
pipeToResponse
|
|
1641
|
-
};
|
|
1642
|
-
next();
|
|
1643
|
-
} catch (error) {
|
|
1644
|
-
next(error);
|
|
1645
|
-
}
|
|
1646
|
-
};
|
|
1647
|
-
}
|
|
1648
|
-
function withUsage(usageTap, getCustomerId) {
|
|
1649
|
-
return withUsageMiddleware(usageTap, { getCustomerId });
|
|
1650
|
-
}
|
|
1651
|
-
|
|
1652
|
-
export { UsageTapClient, UsageTapError, createIdempotencyKey, createOpenAIAdapter, createOpenRouterAdapter, isUsageTapError, pipeToResponse, streamOpenAIRoute, toNextResponse, withUsage, withUsageMiddleware, wrapFetch, wrapOpenAI };
|
|
1653
|
-
//# sourceMappingURL=index.js.map
|
|
1654
|
-
//# sourceMappingURL=index.js.map
|
|
883
|
+
export { UsageTapClient, UsageTapError, createIdempotencyKey, isUsageTapError, wrapFetch };
|
|
884
|
+
//# sourceMappingURL=index.mjs.map
|
|
885
|
+
//# sourceMappingURL=index.mjs.map
|