vibezcheck 0.2.0 → 0.4.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 +131 -197
- package/bin/vibezcheck.js +4 -0
- package/dist/ai-sdk/index.d.mts +78 -9
- package/dist/ai-sdk/index.d.ts +78 -9
- package/dist/ai-sdk/index.js +278 -64
- package/dist/ai-sdk/index.js.map +1 -1
- package/dist/ai-sdk/index.mjs +282 -64
- package/dist/ai-sdk/index.mjs.map +1 -1
- package/dist/cli/index.d.mts +1 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +232 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/index.mjs +209 -0
- package/dist/cli/index.mjs.map +1 -0
- package/dist/{client-MJ3tl7bz.d.mts → client--iq93FQg.d.mts} +1 -1
- package/dist/{client-txrE0D_D.d.ts → client-kJN_Znan.d.ts} +1 -1
- package/dist/index.d.mts +50 -26
- package/dist/index.d.ts +50 -26
- package/dist/index.js +299 -104
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +303 -104
- package/dist/index.mjs.map +1 -1
- package/dist/meter/index.d.mts +2 -2
- package/dist/meter/index.d.ts +2 -2
- package/dist/meter/index.js +28 -7
- package/dist/meter/index.js.map +1 -1
- package/dist/meter/index.mjs +28 -7
- package/dist/meter/index.mjs.map +1 -1
- package/dist/pricing/index.d.mts +8 -2
- package/dist/pricing/index.d.ts +8 -2
- package/dist/pricing/index.js +28 -7
- package/dist/pricing/index.js.map +1 -1
- package/dist/pricing/index.mjs +28 -7
- package/dist/pricing/index.mjs.map +1 -1
- package/dist/react/index.d.mts +95 -17
- package/dist/react/index.d.ts +95 -17
- package/dist/react/index.js +468 -7
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +468 -10
- package/dist/react/index.mjs.map +1 -1
- package/dist/types-Zbzxg-Ka.d.mts +261 -0
- package/dist/types-Zbzxg-Ka.d.ts +261 -0
- package/package.json +6 -2
- package/dist/types-CSrSmsd1.d.mts +0 -159
- package/dist/types-CSrSmsd1.d.ts +0 -159
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
+
});
|
|
7
|
+
|
|
1
8
|
// src/index.ts
|
|
2
9
|
import Stripe5 from "stripe";
|
|
3
10
|
|
|
@@ -547,14 +554,26 @@ function registerModelPricing(modelName, rates) {
|
|
|
547
554
|
|
|
548
555
|
// src/pricing/calculator.ts
|
|
549
556
|
function calculateCost(params) {
|
|
550
|
-
|
|
557
|
+
let rates;
|
|
558
|
+
if (params.customRate) {
|
|
559
|
+
const outRate = params.customRate.out ?? params.customRate.output ?? 0;
|
|
560
|
+
rates = {
|
|
561
|
+
inputPer1M: params.customRate.in,
|
|
562
|
+
outputPer1M: outRate,
|
|
563
|
+
reasoningPer1M: params.customRate.reasoning ?? outRate,
|
|
564
|
+
cachedInputPer1M: params.customRate.cached ?? params.customRate.in * 0.15,
|
|
565
|
+
currency: "USD"
|
|
566
|
+
};
|
|
567
|
+
} else {
|
|
568
|
+
rates = getModelPricing(params.model);
|
|
569
|
+
}
|
|
551
570
|
const inputTokens = params.inputTokens ?? 0;
|
|
552
571
|
const outputTokens = params.outputTokens ?? 0;
|
|
553
572
|
const reasoningTokens = params.reasoningTokens ?? 0;
|
|
554
573
|
const cachedTokens = params.cachedTokens ?? 0;
|
|
555
574
|
const regularInputTokens = Math.max(0, inputTokens - cachedTokens);
|
|
556
575
|
const regularInputCost = regularInputTokens / 1e6 * rates.inputPer1M;
|
|
557
|
-
const cachedRate = rates.cachedInputPer1M ?? rates.inputPer1M * 0.
|
|
576
|
+
const cachedRate = rates.cachedInputPer1M ?? rates.inputPer1M * 0.15;
|
|
558
577
|
const cachedInputCost = cachedTokens / 1e6 * cachedRate;
|
|
559
578
|
const inputCostUSD = regularInputCost + cachedInputCost;
|
|
560
579
|
const outputCostUSD = outputTokens / 1e6 * rates.outputPer1M;
|
|
@@ -562,20 +581,27 @@ function calculateCost(params) {
|
|
|
562
581
|
const reasoningCostUSD = reasoningTokens / 1e6 * reasoningRate;
|
|
563
582
|
const standardCacheCost = cachedTokens / 1e6 * rates.inputPer1M;
|
|
564
583
|
const cachedDiscountUSD = Math.max(0, standardCacheCost - cachedInputCost);
|
|
565
|
-
|
|
584
|
+
let totalUSD = inputCostUSD + outputCostUSD;
|
|
566
585
|
const markup = params.markupMultiplier ?? 1;
|
|
567
|
-
|
|
586
|
+
let retailUSD = void 0;
|
|
587
|
+
if (markup !== 1 || params.minimumChargeUSD !== void 0) {
|
|
588
|
+
retailUSD = totalUSD * markup;
|
|
589
|
+
if (params.minimumChargeUSD !== void 0 && retailUSD < params.minimumChargeUSD) {
|
|
590
|
+
retailUSD = params.minimumChargeUSD;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
568
593
|
return {
|
|
569
594
|
inputCostUSD: Number(inputCostUSD.toFixed(8)),
|
|
570
595
|
outputCostUSD: Number(outputCostUSD.toFixed(8)),
|
|
571
596
|
reasoningCostUSD: reasoningTokens > 0 ? Number(reasoningCostUSD.toFixed(8)) : void 0,
|
|
572
597
|
cachedDiscountUSD: cachedTokens > 0 ? Number(cachedDiscountUSD.toFixed(8)) : void 0,
|
|
573
598
|
totalUSD: Number(totalUSD.toFixed(8)),
|
|
574
|
-
retailUSD: retailUSD ? Number(retailUSD.toFixed(8)) : void 0,
|
|
599
|
+
retailUSD: retailUSD !== void 0 ? Number(retailUSD.toFixed(8)) : void 0,
|
|
575
600
|
currency: rates.currency || "USD"
|
|
576
601
|
};
|
|
577
602
|
}
|
|
578
|
-
function calculateUsageCost(model, usage,
|
|
603
|
+
function calculateUsageCost(model, usage, optionsOrMarkup) {
|
|
604
|
+
const options = typeof optionsOrMarkup === "number" ? { markupMultiplier: optionsOrMarkup } : optionsOrMarkup || {};
|
|
579
605
|
return calculateCost({
|
|
580
606
|
model,
|
|
581
607
|
inputTokens: usage.inputTokens,
|
|
@@ -583,7 +609,9 @@ function calculateUsageCost(model, usage, markupMultiplier) {
|
|
|
583
609
|
reasoningTokens: usage.reasoningTokens,
|
|
584
610
|
cachedTokens: usage.cachedTokens,
|
|
585
611
|
cacheWriteTokens: usage.cacheWriteTokens,
|
|
586
|
-
markupMultiplier
|
|
612
|
+
markupMultiplier: options.markupMultiplier,
|
|
613
|
+
minimumChargeUSD: options.minimumChargeUSD,
|
|
614
|
+
customRate: options.customRate
|
|
587
615
|
});
|
|
588
616
|
}
|
|
589
617
|
|
|
@@ -822,6 +850,16 @@ function createMeter(options = {}) {
|
|
|
822
850
|
return new VibezMeter(options);
|
|
823
851
|
}
|
|
824
852
|
|
|
853
|
+
// src/types.ts
|
|
854
|
+
var VibezCircuitBreakerError = class extends Error {
|
|
855
|
+
event;
|
|
856
|
+
constructor(event) {
|
|
857
|
+
super(event.message);
|
|
858
|
+
this.name = "VibezCircuitBreakerError";
|
|
859
|
+
this.event = event;
|
|
860
|
+
}
|
|
861
|
+
};
|
|
862
|
+
|
|
825
863
|
// src/ai-sdk/with-billing.ts
|
|
826
864
|
function withBilling(model, options = {}) {
|
|
827
865
|
if (!model || typeof model !== "object") {
|
|
@@ -834,7 +872,22 @@ function withBilling(model, options = {}) {
|
|
|
834
872
|
const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
|
|
835
873
|
const modelId = model.modelId || "unknown-model";
|
|
836
874
|
const provider = model.provider?.replace(/^@ai-sdk\//, "") || "ai-sdk";
|
|
837
|
-
const
|
|
875
|
+
const maxCostPerCall = options.maxCostPerCallUSD !== void 0 ? options.maxCostPerCallUSD : 0.5;
|
|
876
|
+
const captureOnAbort = options.captureOnAbort !== false;
|
|
877
|
+
const scheduleFlush = () => {
|
|
878
|
+
try {
|
|
879
|
+
const flushPromise = meter.flush();
|
|
880
|
+
if (typeof globalThis !== "undefined") {
|
|
881
|
+
const g = globalThis;
|
|
882
|
+
if (typeof g.after === "function") {
|
|
883
|
+
g.after(() => flushPromise);
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
} catch {
|
|
888
|
+
}
|
|
889
|
+
};
|
|
890
|
+
const handleUsage = (rawUsage, extraMeta) => {
|
|
838
891
|
if (!rawUsage) return;
|
|
839
892
|
const inputTokens = rawUsage.promptTokens ?? rawUsage.inputTokens ?? 0;
|
|
840
893
|
const outputTokens = rawUsage.completionTokens ?? rawUsage.outputTokens ?? 0;
|
|
@@ -848,7 +901,43 @@ function withBilling(model, options = {}) {
|
|
|
848
901
|
visibleOutputTokens: Math.max(0, outputTokens - reasoningTokens),
|
|
849
902
|
cachedTokens: cachedTokens > 0 ? cachedTokens : void 0
|
|
850
903
|
};
|
|
851
|
-
const cost = calculateUsageCost(modelId, usage
|
|
904
|
+
const cost = calculateUsageCost(modelId, usage, {
|
|
905
|
+
markupMultiplier: options.pricing?.margin,
|
|
906
|
+
minimumChargeUSD: options.pricing?.minimumChargeUSD,
|
|
907
|
+
customRate: options.rate
|
|
908
|
+
});
|
|
909
|
+
if (maxCostPerCall > 0 && maxCostPerCall !== Infinity && cost.totalUSD > maxCostPerCall) {
|
|
910
|
+
const budgetEvent = {
|
|
911
|
+
reason: "cost_per_call_exceeded",
|
|
912
|
+
limit: maxCostPerCall,
|
|
913
|
+
current: cost.totalUSD,
|
|
914
|
+
model: modelId,
|
|
915
|
+
customerId,
|
|
916
|
+
message: `[vibezcheck] Circuit Breaker tripped: Call cost ($${cost.totalUSD.toFixed(4)}) exceeded budget ceiling ($${maxCostPerCall.toFixed(4)}).`
|
|
917
|
+
};
|
|
918
|
+
if (options.onBudgetExceeded) {
|
|
919
|
+
options.onBudgetExceeded(budgetEvent);
|
|
920
|
+
}
|
|
921
|
+
if (options.throwOnBudgetExceeded) {
|
|
922
|
+
throw new VibezCircuitBreakerError(budgetEvent);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
if (options.maxTokensPerCall && usage.totalTokens > options.maxTokensPerCall) {
|
|
926
|
+
const budgetEvent = {
|
|
927
|
+
reason: "max_tokens_exceeded",
|
|
928
|
+
limit: options.maxTokensPerCall,
|
|
929
|
+
current: usage.totalTokens,
|
|
930
|
+
model: modelId,
|
|
931
|
+
customerId,
|
|
932
|
+
message: `[vibezcheck] Circuit Breaker tripped: Token count (${usage.totalTokens}) exceeded maxTokensPerCall limit (${options.maxTokensPerCall}).`
|
|
933
|
+
};
|
|
934
|
+
if (options.onBudgetExceeded) {
|
|
935
|
+
options.onBudgetExceeded(budgetEvent);
|
|
936
|
+
}
|
|
937
|
+
if (options.throwOnBudgetExceeded) {
|
|
938
|
+
throw new VibezCircuitBreakerError(budgetEvent);
|
|
939
|
+
}
|
|
940
|
+
}
|
|
852
941
|
const event = {
|
|
853
942
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
854
943
|
model: modelId,
|
|
@@ -856,7 +945,11 @@ function withBilling(model, options = {}) {
|
|
|
856
945
|
usage,
|
|
857
946
|
cost,
|
|
858
947
|
customerId,
|
|
859
|
-
metadata:
|
|
948
|
+
metadata: {
|
|
949
|
+
...options.metadata,
|
|
950
|
+
...extraMeta,
|
|
951
|
+
billingMode: options.billing?.mode || "postpaid"
|
|
952
|
+
}
|
|
860
953
|
};
|
|
861
954
|
meter.recordUsage({
|
|
862
955
|
model: modelId,
|
|
@@ -866,24 +959,16 @@ function withBilling(model, options = {}) {
|
|
|
866
959
|
reasoningTokens: usage.reasoningTokens,
|
|
867
960
|
cachedTokens: usage.cachedTokens,
|
|
868
961
|
customerId,
|
|
869
|
-
metadata:
|
|
962
|
+
metadata: event.metadata
|
|
870
963
|
});
|
|
871
964
|
if (options.onUsage) {
|
|
872
965
|
options.onUsage(event);
|
|
873
966
|
}
|
|
967
|
+
scheduleFlush();
|
|
874
968
|
};
|
|
875
|
-
|
|
969
|
+
const handler = {
|
|
876
970
|
get(target, prop, receiver) {
|
|
877
971
|
const originalValue = Reflect.get(target, prop, receiver);
|
|
878
|
-
if (prop === "doGenerate" && typeof originalValue === "function") {
|
|
879
|
-
return async function(...args) {
|
|
880
|
-
const result = await originalValue.apply(target, args);
|
|
881
|
-
if (result && result.usage) {
|
|
882
|
-
handleUsage(result.usage);
|
|
883
|
-
}
|
|
884
|
-
return result;
|
|
885
|
-
};
|
|
886
|
-
}
|
|
887
972
|
if (prop === "doStream" && typeof originalValue === "function") {
|
|
888
973
|
return async function(...args) {
|
|
889
974
|
const result = await originalValue.apply(target, args);
|
|
@@ -891,61 +976,191 @@ function withBilling(model, options = {}) {
|
|
|
891
976
|
return result;
|
|
892
977
|
}
|
|
893
978
|
const originalStream = result.stream;
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
break;
|
|
904
|
-
}
|
|
905
|
-
if (value && typeof value === "object") {
|
|
906
|
-
if (value.type === "finish" && value.usage) {
|
|
907
|
-
handleUsage(value.usage);
|
|
908
|
-
}
|
|
909
|
-
}
|
|
910
|
-
controller.enqueue(value);
|
|
911
|
-
}
|
|
912
|
-
} catch (err) {
|
|
913
|
-
controller.error(err);
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
});
|
|
917
|
-
return {
|
|
918
|
-
...result,
|
|
919
|
-
stream: transformedStream
|
|
920
|
-
};
|
|
979
|
+
let streamCompleted = false;
|
|
980
|
+
let accumulatedChars = 0;
|
|
981
|
+
let estimatedInputTokens = 0;
|
|
982
|
+
try {
|
|
983
|
+
if (args[0]?.prompt) {
|
|
984
|
+
const str = typeof args[0].prompt === "string" ? args[0].prompt : JSON.stringify(args[0].prompt);
|
|
985
|
+
estimatedInputTokens = Math.ceil(str.length / 4);
|
|
986
|
+
}
|
|
987
|
+
} catch {
|
|
921
988
|
}
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
989
|
+
const abortSignal = args[0]?.abortSignal;
|
|
990
|
+
if (abortSignal && captureOnAbort) {
|
|
991
|
+
abortSignal.addEventListener(
|
|
992
|
+
"abort",
|
|
993
|
+
() => {
|
|
994
|
+
if (!streamCompleted && accumulatedChars > 0) {
|
|
995
|
+
streamCompleted = true;
|
|
996
|
+
const estimatedOutputTokens = Math.ceil(accumulatedChars / 3.8);
|
|
997
|
+
handleUsage(
|
|
998
|
+
{
|
|
999
|
+
inputTokens: estimatedInputTokens,
|
|
1000
|
+
outputTokens: estimatedOutputTokens
|
|
1001
|
+
},
|
|
1002
|
+
{ aborted: true, partial: true }
|
|
1003
|
+
);
|
|
932
1004
|
}
|
|
1005
|
+
},
|
|
1006
|
+
{ once: true }
|
|
1007
|
+
);
|
|
1008
|
+
}
|
|
1009
|
+
const transformStream = new TransformStream({
|
|
1010
|
+
transform(chunk, controller) {
|
|
1011
|
+
controller.enqueue(chunk);
|
|
1012
|
+
if (chunk.type === "text-delta" && chunk.textDelta) {
|
|
1013
|
+
accumulatedChars += chunk.textDelta.length;
|
|
933
1014
|
}
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
}
|
|
1015
|
+
if (chunk.type === "finish" && chunk.usage) {
|
|
1016
|
+
streamCompleted = true;
|
|
1017
|
+
handleUsage(chunk.usage);
|
|
1018
|
+
}
|
|
1019
|
+
},
|
|
1020
|
+
flush() {
|
|
1021
|
+
if (!streamCompleted && captureOnAbort && accumulatedChars > 0) {
|
|
1022
|
+
streamCompleted = true;
|
|
1023
|
+
const estimatedOutputTokens = Math.ceil(accumulatedChars / 3.8);
|
|
1024
|
+
handleUsage(
|
|
1025
|
+
{
|
|
1026
|
+
inputTokens: estimatedInputTokens,
|
|
1027
|
+
outputTokens: estimatedOutputTokens
|
|
1028
|
+
},
|
|
1029
|
+
{ aborted: true, partial: true }
|
|
1030
|
+
);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
});
|
|
1034
|
+
return {
|
|
1035
|
+
...result,
|
|
1036
|
+
stream: originalStream.pipeThrough(transformStream)
|
|
1037
|
+
};
|
|
1038
|
+
};
|
|
1039
|
+
}
|
|
1040
|
+
if (prop === "doGenerate" && typeof originalValue === "function") {
|
|
1041
|
+
return async function(...args) {
|
|
1042
|
+
const result = await originalValue.apply(target, args);
|
|
1043
|
+
if (result && result.usage) {
|
|
1044
|
+
handleUsage(result.usage);
|
|
939
1045
|
}
|
|
940
1046
|
return result;
|
|
941
1047
|
};
|
|
942
1048
|
}
|
|
943
1049
|
return originalValue;
|
|
944
1050
|
}
|
|
945
|
-
}
|
|
1051
|
+
};
|
|
1052
|
+
return new Proxy(model, handler);
|
|
946
1053
|
}
|
|
947
1054
|
var meteredModel = withBilling;
|
|
948
1055
|
|
|
1056
|
+
// src/ai-sdk/declarative.ts
|
|
1057
|
+
function createVibezModel(modelOrId, options = {}) {
|
|
1058
|
+
if (typeof modelOrId === "object" && modelOrId !== null) {
|
|
1059
|
+
return withBilling(modelOrId, options);
|
|
1060
|
+
}
|
|
1061
|
+
if (typeof modelOrId === "string") {
|
|
1062
|
+
const rawId = modelOrId;
|
|
1063
|
+
let providerName = "openai";
|
|
1064
|
+
let cleanModelId = rawId;
|
|
1065
|
+
if (rawId.includes("/")) {
|
|
1066
|
+
const parts = rawId.split("/");
|
|
1067
|
+
providerName = parts[0].toLowerCase();
|
|
1068
|
+
cleanModelId = parts.slice(1).join("/");
|
|
1069
|
+
}
|
|
1070
|
+
const apiKey = options.apiKey || process.env.AI_GATEWAY_API_KEY || process.env.OPENAI_API_KEY || process.env.ANTHROPIC_API_KEY || process.env.GOOGLE_GENERATIVE_AI_API_KEY;
|
|
1071
|
+
const baseURL = options.baseURL || process.env.AI_GATEWAY_BASE_URL || (providerName === "openai" ? "https://api.openai.com/v1" : void 0);
|
|
1072
|
+
let baseModelInstance = null;
|
|
1073
|
+
try {
|
|
1074
|
+
if (providerName === "openai" || providerName === "gateway" || !providerName) {
|
|
1075
|
+
const { createOpenAI } = __require("@ai-sdk/openai");
|
|
1076
|
+
const openaiProvider = createOpenAI({ apiKey, baseURL });
|
|
1077
|
+
baseModelInstance = openaiProvider(cleanModelId);
|
|
1078
|
+
} else if (providerName === "anthropic") {
|
|
1079
|
+
const { createAnthropic } = __require("@ai-sdk/anthropic");
|
|
1080
|
+
const anthropicProvider = createAnthropic({ apiKey, baseURL });
|
|
1081
|
+
baseModelInstance = anthropicProvider(cleanModelId);
|
|
1082
|
+
} else if (providerName === "google") {
|
|
1083
|
+
const { createGoogleGenerativeAI } = __require("@ai-sdk/google");
|
|
1084
|
+
const googleProvider = createGoogleGenerativeAI({ apiKey, baseURL });
|
|
1085
|
+
baseModelInstance = googleProvider(cleanModelId);
|
|
1086
|
+
}
|
|
1087
|
+
} catch {
|
|
1088
|
+
baseModelInstance = {
|
|
1089
|
+
specificationVersion: "v2",
|
|
1090
|
+
provider: providerName,
|
|
1091
|
+
modelId: cleanModelId
|
|
1092
|
+
};
|
|
1093
|
+
}
|
|
1094
|
+
if (!baseModelInstance) {
|
|
1095
|
+
baseModelInstance = {
|
|
1096
|
+
specificationVersion: "v2",
|
|
1097
|
+
provider: providerName,
|
|
1098
|
+
modelId: cleanModelId
|
|
1099
|
+
};
|
|
1100
|
+
}
|
|
1101
|
+
return withBilling(baseModelInstance, options);
|
|
1102
|
+
}
|
|
1103
|
+
return modelOrId;
|
|
1104
|
+
}
|
|
1105
|
+
function createVibezSession(sessionOptions = {}) {
|
|
1106
|
+
const meter = createMeter({
|
|
1107
|
+
apiKey: sessionOptions.stripeApiKey
|
|
1108
|
+
});
|
|
1109
|
+
const customerId = typeof sessionOptions.customer === "string" ? sessionOptions.customer : sessionOptions.customer?.id;
|
|
1110
|
+
return {
|
|
1111
|
+
meter,
|
|
1112
|
+
model: (modelOrId, callOptions = {}) => {
|
|
1113
|
+
return createVibezModel(modelOrId, {
|
|
1114
|
+
customer: sessionOptions.customer,
|
|
1115
|
+
pricing: sessionOptions.pricing,
|
|
1116
|
+
billing: sessionOptions.billing,
|
|
1117
|
+
meter,
|
|
1118
|
+
metadata: {
|
|
1119
|
+
...sessionOptions.metadata,
|
|
1120
|
+
...callOptions.metadata
|
|
1121
|
+
},
|
|
1122
|
+
...callOptions
|
|
1123
|
+
});
|
|
1124
|
+
},
|
|
1125
|
+
trackTool: async (name, { costUSD, metadata }) => {
|
|
1126
|
+
const event = {
|
|
1127
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1128
|
+
model: `tool:${name}`,
|
|
1129
|
+
provider: "tool",
|
|
1130
|
+
usage: {
|
|
1131
|
+
inputTokens: 0,
|
|
1132
|
+
outputTokens: 0,
|
|
1133
|
+
totalTokens: 0
|
|
1134
|
+
},
|
|
1135
|
+
cost: {
|
|
1136
|
+
inputCostUSD: 0,
|
|
1137
|
+
outputCostUSD: costUSD,
|
|
1138
|
+
totalUSD: costUSD,
|
|
1139
|
+
currency: "USD"
|
|
1140
|
+
},
|
|
1141
|
+
customerId,
|
|
1142
|
+
metadata: {
|
|
1143
|
+
...sessionOptions.metadata,
|
|
1144
|
+
...metadata,
|
|
1145
|
+
tool: name
|
|
1146
|
+
}
|
|
1147
|
+
};
|
|
1148
|
+
meter.recordUsage({
|
|
1149
|
+
model: `tool:${name}`,
|
|
1150
|
+
provider: "tool",
|
|
1151
|
+
inputTokens: 0,
|
|
1152
|
+
outputTokens: 0,
|
|
1153
|
+
customerId,
|
|
1154
|
+
metadata: event.metadata
|
|
1155
|
+
});
|
|
1156
|
+
await meter.flush();
|
|
1157
|
+
}
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
1160
|
+
var vibezcheck = Object.assign(createVibezModel, {
|
|
1161
|
+
session: createVibezSession
|
|
1162
|
+
});
|
|
1163
|
+
|
|
949
1164
|
// src/customers/manager.ts
|
|
950
1165
|
import Stripe2 from "stripe";
|
|
951
1166
|
|
|
@@ -1284,43 +1499,12 @@ var VibezCheckClient = class {
|
|
|
1284
1499
|
});
|
|
1285
1500
|
}
|
|
1286
1501
|
/**
|
|
1287
|
-
*
|
|
1502
|
+
* Declarative model resolver for Vercel AI SDK
|
|
1288
1503
|
*/
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
model: params.model,
|
|
1294
|
-
messages: params.messages,
|
|
1295
|
-
stream: true,
|
|
1296
|
-
stream_options: { include_usage: true },
|
|
1297
|
-
temperature: params.temperature
|
|
1298
|
-
});
|
|
1299
|
-
const meteredStream = this.wrapStream(responseStream, {
|
|
1300
|
-
customer: params.customer,
|
|
1301
|
-
model: params.model
|
|
1302
|
-
});
|
|
1303
|
-
const encoder = new TextEncoder();
|
|
1304
|
-
const readable = new ReadableStream({
|
|
1305
|
-
async start(controller) {
|
|
1306
|
-
try {
|
|
1307
|
-
for await (const chunk of meteredStream) {
|
|
1308
|
-
const text = chunk.choices?.[0]?.delta?.content || "";
|
|
1309
|
-
if (text) {
|
|
1310
|
-
controller.enqueue(encoder.encode(text));
|
|
1311
|
-
}
|
|
1312
|
-
}
|
|
1313
|
-
controller.close();
|
|
1314
|
-
} catch (err) {
|
|
1315
|
-
controller.error(err);
|
|
1316
|
-
}
|
|
1317
|
-
}
|
|
1318
|
-
});
|
|
1319
|
-
return new Response(readable, {
|
|
1320
|
-
headers: {
|
|
1321
|
-
"Content-Type": "text/plain; charset=utf-8",
|
|
1322
|
-
"Transfer-Encoding": "chunked"
|
|
1323
|
-
}
|
|
1504
|
+
model(modelOrId, options) {
|
|
1505
|
+
return createVibezModel(modelOrId, {
|
|
1506
|
+
...options,
|
|
1507
|
+
meter: this.meter
|
|
1324
1508
|
});
|
|
1325
1509
|
}
|
|
1326
1510
|
/**
|
|
@@ -1339,9 +1523,21 @@ var VibezCheckClient = class {
|
|
|
1339
1523
|
function createVibezCheck(config = {}) {
|
|
1340
1524
|
return new VibezCheckClient(config);
|
|
1341
1525
|
}
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1526
|
+
function vibezcheck2(firstArg, secondArg) {
|
|
1527
|
+
if (typeof firstArg === "string" || firstArg && (firstArg.specificationVersion || firstArg.doGenerate || firstArg.doStream)) {
|
|
1528
|
+
return createVibezModel(firstArg, secondArg);
|
|
1529
|
+
}
|
|
1530
|
+
return new VibezCheckClient(firstArg || {});
|
|
1531
|
+
}
|
|
1532
|
+
vibezcheck2.calculateCost = calculateCost;
|
|
1533
|
+
vibezcheck2.getModelPricing = getModelPricing;
|
|
1534
|
+
vibezcheck2.registerModelPricing = registerModelPricing;
|
|
1535
|
+
vibezcheck2.create = createVibezCheck;
|
|
1536
|
+
vibezcheck2.withBilling = withBilling;
|
|
1537
|
+
vibezcheck2.createMeter = createMeter;
|
|
1538
|
+
vibezcheck2.session = createVibezSession;
|
|
1539
|
+
var vibez = new VibezCheckClient();
|
|
1540
|
+
var vibescheck = vibezcheck2;
|
|
1345
1541
|
var vibes = vibez;
|
|
1346
1542
|
export {
|
|
1347
1543
|
AnthropicStreamAccumulator,
|
|
@@ -1352,6 +1548,7 @@ export {
|
|
|
1352
1548
|
MODEL_PRICING_TABLE,
|
|
1353
1549
|
MeterBatcher,
|
|
1354
1550
|
VibezCheckClient,
|
|
1551
|
+
VibezCircuitBreakerError,
|
|
1355
1552
|
VibezMeter,
|
|
1356
1553
|
calculateCost,
|
|
1357
1554
|
calculateUsageCost,
|
|
@@ -1360,6 +1557,8 @@ export {
|
|
|
1360
1557
|
createCustomerManager,
|
|
1361
1558
|
createMeter,
|
|
1362
1559
|
createVibezCheck,
|
|
1560
|
+
createVibezModel,
|
|
1561
|
+
createVibezSession,
|
|
1363
1562
|
detectAndExtractUsage,
|
|
1364
1563
|
extractAnthropicResponseUsage,
|
|
1365
1564
|
extractAuthToken,
|
|
@@ -1375,7 +1574,7 @@ export {
|
|
|
1375
1574
|
vibes,
|
|
1376
1575
|
vibescheck,
|
|
1377
1576
|
vibez,
|
|
1378
|
-
vibezcheck,
|
|
1577
|
+
vibezcheck2 as vibezcheck,
|
|
1379
1578
|
withBilling,
|
|
1380
1579
|
wrapAnthropicStream,
|
|
1381
1580
|
wrapGeminiStream,
|