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.js
CHANGED
|
@@ -38,6 +38,7 @@ __export(src_exports, {
|
|
|
38
38
|
MODEL_PRICING_TABLE: () => MODEL_PRICING_TABLE,
|
|
39
39
|
MeterBatcher: () => MeterBatcher,
|
|
40
40
|
VibezCheckClient: () => VibezCheckClient,
|
|
41
|
+
VibezCircuitBreakerError: () => VibezCircuitBreakerError,
|
|
41
42
|
VibezMeter: () => VibezMeter,
|
|
42
43
|
calculateCost: () => calculateCost,
|
|
43
44
|
calculateUsageCost: () => calculateUsageCost,
|
|
@@ -46,6 +47,8 @@ __export(src_exports, {
|
|
|
46
47
|
createCustomerManager: () => createCustomerManager,
|
|
47
48
|
createMeter: () => createMeter,
|
|
48
49
|
createVibezCheck: () => createVibezCheck,
|
|
50
|
+
createVibezModel: () => createVibezModel,
|
|
51
|
+
createVibezSession: () => createVibezSession,
|
|
49
52
|
detectAndExtractUsage: () => detectAndExtractUsage,
|
|
50
53
|
extractAnthropicResponseUsage: () => extractAnthropicResponseUsage,
|
|
51
54
|
extractAuthToken: () => extractAuthToken,
|
|
@@ -61,7 +64,7 @@ __export(src_exports, {
|
|
|
61
64
|
vibes: () => vibes,
|
|
62
65
|
vibescheck: () => vibescheck,
|
|
63
66
|
vibez: () => vibez,
|
|
64
|
-
vibezcheck: () =>
|
|
67
|
+
vibezcheck: () => vibezcheck2,
|
|
65
68
|
withBilling: () => withBilling,
|
|
66
69
|
wrapAnthropicStream: () => wrapAnthropicStream,
|
|
67
70
|
wrapGeminiStream: () => wrapGeminiStream,
|
|
@@ -618,14 +621,26 @@ function registerModelPricing(modelName, rates) {
|
|
|
618
621
|
|
|
619
622
|
// src/pricing/calculator.ts
|
|
620
623
|
function calculateCost(params) {
|
|
621
|
-
|
|
624
|
+
let rates;
|
|
625
|
+
if (params.customRate) {
|
|
626
|
+
const outRate = params.customRate.out ?? params.customRate.output ?? 0;
|
|
627
|
+
rates = {
|
|
628
|
+
inputPer1M: params.customRate.in,
|
|
629
|
+
outputPer1M: outRate,
|
|
630
|
+
reasoningPer1M: params.customRate.reasoning ?? outRate,
|
|
631
|
+
cachedInputPer1M: params.customRate.cached ?? params.customRate.in * 0.15,
|
|
632
|
+
currency: "USD"
|
|
633
|
+
};
|
|
634
|
+
} else {
|
|
635
|
+
rates = getModelPricing(params.model);
|
|
636
|
+
}
|
|
622
637
|
const inputTokens = params.inputTokens ?? 0;
|
|
623
638
|
const outputTokens = params.outputTokens ?? 0;
|
|
624
639
|
const reasoningTokens = params.reasoningTokens ?? 0;
|
|
625
640
|
const cachedTokens = params.cachedTokens ?? 0;
|
|
626
641
|
const regularInputTokens = Math.max(0, inputTokens - cachedTokens);
|
|
627
642
|
const regularInputCost = regularInputTokens / 1e6 * rates.inputPer1M;
|
|
628
|
-
const cachedRate = rates.cachedInputPer1M ?? rates.inputPer1M * 0.
|
|
643
|
+
const cachedRate = rates.cachedInputPer1M ?? rates.inputPer1M * 0.15;
|
|
629
644
|
const cachedInputCost = cachedTokens / 1e6 * cachedRate;
|
|
630
645
|
const inputCostUSD = regularInputCost + cachedInputCost;
|
|
631
646
|
const outputCostUSD = outputTokens / 1e6 * rates.outputPer1M;
|
|
@@ -633,20 +648,27 @@ function calculateCost(params) {
|
|
|
633
648
|
const reasoningCostUSD = reasoningTokens / 1e6 * reasoningRate;
|
|
634
649
|
const standardCacheCost = cachedTokens / 1e6 * rates.inputPer1M;
|
|
635
650
|
const cachedDiscountUSD = Math.max(0, standardCacheCost - cachedInputCost);
|
|
636
|
-
|
|
651
|
+
let totalUSD = inputCostUSD + outputCostUSD;
|
|
637
652
|
const markup = params.markupMultiplier ?? 1;
|
|
638
|
-
|
|
653
|
+
let retailUSD = void 0;
|
|
654
|
+
if (markup !== 1 || params.minimumChargeUSD !== void 0) {
|
|
655
|
+
retailUSD = totalUSD * markup;
|
|
656
|
+
if (params.minimumChargeUSD !== void 0 && retailUSD < params.minimumChargeUSD) {
|
|
657
|
+
retailUSD = params.minimumChargeUSD;
|
|
658
|
+
}
|
|
659
|
+
}
|
|
639
660
|
return {
|
|
640
661
|
inputCostUSD: Number(inputCostUSD.toFixed(8)),
|
|
641
662
|
outputCostUSD: Number(outputCostUSD.toFixed(8)),
|
|
642
663
|
reasoningCostUSD: reasoningTokens > 0 ? Number(reasoningCostUSD.toFixed(8)) : void 0,
|
|
643
664
|
cachedDiscountUSD: cachedTokens > 0 ? Number(cachedDiscountUSD.toFixed(8)) : void 0,
|
|
644
665
|
totalUSD: Number(totalUSD.toFixed(8)),
|
|
645
|
-
retailUSD: retailUSD ? Number(retailUSD.toFixed(8)) : void 0,
|
|
666
|
+
retailUSD: retailUSD !== void 0 ? Number(retailUSD.toFixed(8)) : void 0,
|
|
646
667
|
currency: rates.currency || "USD"
|
|
647
668
|
};
|
|
648
669
|
}
|
|
649
|
-
function calculateUsageCost(model, usage,
|
|
670
|
+
function calculateUsageCost(model, usage, optionsOrMarkup) {
|
|
671
|
+
const options = typeof optionsOrMarkup === "number" ? { markupMultiplier: optionsOrMarkup } : optionsOrMarkup || {};
|
|
650
672
|
return calculateCost({
|
|
651
673
|
model,
|
|
652
674
|
inputTokens: usage.inputTokens,
|
|
@@ -654,7 +676,9 @@ function calculateUsageCost(model, usage, markupMultiplier) {
|
|
|
654
676
|
reasoningTokens: usage.reasoningTokens,
|
|
655
677
|
cachedTokens: usage.cachedTokens,
|
|
656
678
|
cacheWriteTokens: usage.cacheWriteTokens,
|
|
657
|
-
markupMultiplier
|
|
679
|
+
markupMultiplier: options.markupMultiplier,
|
|
680
|
+
minimumChargeUSD: options.minimumChargeUSD,
|
|
681
|
+
customRate: options.customRate
|
|
658
682
|
});
|
|
659
683
|
}
|
|
660
684
|
|
|
@@ -893,6 +917,16 @@ function createMeter(options = {}) {
|
|
|
893
917
|
return new VibezMeter(options);
|
|
894
918
|
}
|
|
895
919
|
|
|
920
|
+
// src/types.ts
|
|
921
|
+
var VibezCircuitBreakerError = class extends Error {
|
|
922
|
+
event;
|
|
923
|
+
constructor(event) {
|
|
924
|
+
super(event.message);
|
|
925
|
+
this.name = "VibezCircuitBreakerError";
|
|
926
|
+
this.event = event;
|
|
927
|
+
}
|
|
928
|
+
};
|
|
929
|
+
|
|
896
930
|
// src/ai-sdk/with-billing.ts
|
|
897
931
|
function withBilling(model, options = {}) {
|
|
898
932
|
if (!model || typeof model !== "object") {
|
|
@@ -905,7 +939,22 @@ function withBilling(model, options = {}) {
|
|
|
905
939
|
const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
|
|
906
940
|
const modelId = model.modelId || "unknown-model";
|
|
907
941
|
const provider = model.provider?.replace(/^@ai-sdk\//, "") || "ai-sdk";
|
|
908
|
-
const
|
|
942
|
+
const maxCostPerCall = options.maxCostPerCallUSD !== void 0 ? options.maxCostPerCallUSD : 0.5;
|
|
943
|
+
const captureOnAbort = options.captureOnAbort !== false;
|
|
944
|
+
const scheduleFlush = () => {
|
|
945
|
+
try {
|
|
946
|
+
const flushPromise = meter.flush();
|
|
947
|
+
if (typeof globalThis !== "undefined") {
|
|
948
|
+
const g = globalThis;
|
|
949
|
+
if (typeof g.after === "function") {
|
|
950
|
+
g.after(() => flushPromise);
|
|
951
|
+
return;
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
} catch {
|
|
955
|
+
}
|
|
956
|
+
};
|
|
957
|
+
const handleUsage = (rawUsage, extraMeta) => {
|
|
909
958
|
if (!rawUsage) return;
|
|
910
959
|
const inputTokens = rawUsage.promptTokens ?? rawUsage.inputTokens ?? 0;
|
|
911
960
|
const outputTokens = rawUsage.completionTokens ?? rawUsage.outputTokens ?? 0;
|
|
@@ -919,7 +968,43 @@ function withBilling(model, options = {}) {
|
|
|
919
968
|
visibleOutputTokens: Math.max(0, outputTokens - reasoningTokens),
|
|
920
969
|
cachedTokens: cachedTokens > 0 ? cachedTokens : void 0
|
|
921
970
|
};
|
|
922
|
-
const cost = calculateUsageCost(modelId, usage
|
|
971
|
+
const cost = calculateUsageCost(modelId, usage, {
|
|
972
|
+
markupMultiplier: options.pricing?.margin,
|
|
973
|
+
minimumChargeUSD: options.pricing?.minimumChargeUSD,
|
|
974
|
+
customRate: options.rate
|
|
975
|
+
});
|
|
976
|
+
if (maxCostPerCall > 0 && maxCostPerCall !== Infinity && cost.totalUSD > maxCostPerCall) {
|
|
977
|
+
const budgetEvent = {
|
|
978
|
+
reason: "cost_per_call_exceeded",
|
|
979
|
+
limit: maxCostPerCall,
|
|
980
|
+
current: cost.totalUSD,
|
|
981
|
+
model: modelId,
|
|
982
|
+
customerId,
|
|
983
|
+
message: `[vibezcheck] Circuit Breaker tripped: Call cost ($${cost.totalUSD.toFixed(4)}) exceeded budget ceiling ($${maxCostPerCall.toFixed(4)}).`
|
|
984
|
+
};
|
|
985
|
+
if (options.onBudgetExceeded) {
|
|
986
|
+
options.onBudgetExceeded(budgetEvent);
|
|
987
|
+
}
|
|
988
|
+
if (options.throwOnBudgetExceeded) {
|
|
989
|
+
throw new VibezCircuitBreakerError(budgetEvent);
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
if (options.maxTokensPerCall && usage.totalTokens > options.maxTokensPerCall) {
|
|
993
|
+
const budgetEvent = {
|
|
994
|
+
reason: "max_tokens_exceeded",
|
|
995
|
+
limit: options.maxTokensPerCall,
|
|
996
|
+
current: usage.totalTokens,
|
|
997
|
+
model: modelId,
|
|
998
|
+
customerId,
|
|
999
|
+
message: `[vibezcheck] Circuit Breaker tripped: Token count (${usage.totalTokens}) exceeded maxTokensPerCall limit (${options.maxTokensPerCall}).`
|
|
1000
|
+
};
|
|
1001
|
+
if (options.onBudgetExceeded) {
|
|
1002
|
+
options.onBudgetExceeded(budgetEvent);
|
|
1003
|
+
}
|
|
1004
|
+
if (options.throwOnBudgetExceeded) {
|
|
1005
|
+
throw new VibezCircuitBreakerError(budgetEvent);
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
923
1008
|
const event = {
|
|
924
1009
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
925
1010
|
model: modelId,
|
|
@@ -927,7 +1012,11 @@ function withBilling(model, options = {}) {
|
|
|
927
1012
|
usage,
|
|
928
1013
|
cost,
|
|
929
1014
|
customerId,
|
|
930
|
-
metadata:
|
|
1015
|
+
metadata: {
|
|
1016
|
+
...options.metadata,
|
|
1017
|
+
...extraMeta,
|
|
1018
|
+
billingMode: options.billing?.mode || "postpaid"
|
|
1019
|
+
}
|
|
931
1020
|
};
|
|
932
1021
|
meter.recordUsage({
|
|
933
1022
|
model: modelId,
|
|
@@ -937,24 +1026,16 @@ function withBilling(model, options = {}) {
|
|
|
937
1026
|
reasoningTokens: usage.reasoningTokens,
|
|
938
1027
|
cachedTokens: usage.cachedTokens,
|
|
939
1028
|
customerId,
|
|
940
|
-
metadata:
|
|
1029
|
+
metadata: event.metadata
|
|
941
1030
|
});
|
|
942
1031
|
if (options.onUsage) {
|
|
943
1032
|
options.onUsage(event);
|
|
944
1033
|
}
|
|
1034
|
+
scheduleFlush();
|
|
945
1035
|
};
|
|
946
|
-
|
|
1036
|
+
const handler = {
|
|
947
1037
|
get(target, prop, receiver) {
|
|
948
1038
|
const originalValue = Reflect.get(target, prop, receiver);
|
|
949
|
-
if (prop === "doGenerate" && typeof originalValue === "function") {
|
|
950
|
-
return async function(...args) {
|
|
951
|
-
const result = await originalValue.apply(target, args);
|
|
952
|
-
if (result && result.usage) {
|
|
953
|
-
handleUsage(result.usage);
|
|
954
|
-
}
|
|
955
|
-
return result;
|
|
956
|
-
};
|
|
957
|
-
}
|
|
958
1039
|
if (prop === "doStream" && typeof originalValue === "function") {
|
|
959
1040
|
return async function(...args) {
|
|
960
1041
|
const result = await originalValue.apply(target, args);
|
|
@@ -962,61 +1043,191 @@ function withBilling(model, options = {}) {
|
|
|
962
1043
|
return result;
|
|
963
1044
|
}
|
|
964
1045
|
const originalStream = result.stream;
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
break;
|
|
975
|
-
}
|
|
976
|
-
if (value && typeof value === "object") {
|
|
977
|
-
if (value.type === "finish" && value.usage) {
|
|
978
|
-
handleUsage(value.usage);
|
|
979
|
-
}
|
|
980
|
-
}
|
|
981
|
-
controller.enqueue(value);
|
|
982
|
-
}
|
|
983
|
-
} catch (err) {
|
|
984
|
-
controller.error(err);
|
|
985
|
-
}
|
|
986
|
-
}
|
|
987
|
-
});
|
|
988
|
-
return {
|
|
989
|
-
...result,
|
|
990
|
-
stream: transformedStream
|
|
991
|
-
};
|
|
1046
|
+
let streamCompleted = false;
|
|
1047
|
+
let accumulatedChars = 0;
|
|
1048
|
+
let estimatedInputTokens = 0;
|
|
1049
|
+
try {
|
|
1050
|
+
if (args[0]?.prompt) {
|
|
1051
|
+
const str = typeof args[0].prompt === "string" ? args[0].prompt : JSON.stringify(args[0].prompt);
|
|
1052
|
+
estimatedInputTokens = Math.ceil(str.length / 4);
|
|
1053
|
+
}
|
|
1054
|
+
} catch {
|
|
992
1055
|
}
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1056
|
+
const abortSignal = args[0]?.abortSignal;
|
|
1057
|
+
if (abortSignal && captureOnAbort) {
|
|
1058
|
+
abortSignal.addEventListener(
|
|
1059
|
+
"abort",
|
|
1060
|
+
() => {
|
|
1061
|
+
if (!streamCompleted && accumulatedChars > 0) {
|
|
1062
|
+
streamCompleted = true;
|
|
1063
|
+
const estimatedOutputTokens = Math.ceil(accumulatedChars / 3.8);
|
|
1064
|
+
handleUsage(
|
|
1065
|
+
{
|
|
1066
|
+
inputTokens: estimatedInputTokens,
|
|
1067
|
+
outputTokens: estimatedOutputTokens
|
|
1068
|
+
},
|
|
1069
|
+
{ aborted: true, partial: true }
|
|
1070
|
+
);
|
|
1003
1071
|
}
|
|
1072
|
+
},
|
|
1073
|
+
{ once: true }
|
|
1074
|
+
);
|
|
1075
|
+
}
|
|
1076
|
+
const transformStream = new TransformStream({
|
|
1077
|
+
transform(chunk, controller) {
|
|
1078
|
+
controller.enqueue(chunk);
|
|
1079
|
+
if (chunk.type === "text-delta" && chunk.textDelta) {
|
|
1080
|
+
accumulatedChars += chunk.textDelta.length;
|
|
1081
|
+
}
|
|
1082
|
+
if (chunk.type === "finish" && chunk.usage) {
|
|
1083
|
+
streamCompleted = true;
|
|
1084
|
+
handleUsage(chunk.usage);
|
|
1004
1085
|
}
|
|
1005
|
-
}
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1086
|
+
},
|
|
1087
|
+
flush() {
|
|
1088
|
+
if (!streamCompleted && captureOnAbort && accumulatedChars > 0) {
|
|
1089
|
+
streamCompleted = true;
|
|
1090
|
+
const estimatedOutputTokens = Math.ceil(accumulatedChars / 3.8);
|
|
1091
|
+
handleUsage(
|
|
1092
|
+
{
|
|
1093
|
+
inputTokens: estimatedInputTokens,
|
|
1094
|
+
outputTokens: estimatedOutputTokens
|
|
1095
|
+
},
|
|
1096
|
+
{ aborted: true, partial: true }
|
|
1097
|
+
);
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
});
|
|
1101
|
+
return {
|
|
1102
|
+
...result,
|
|
1103
|
+
stream: originalStream.pipeThrough(transformStream)
|
|
1104
|
+
};
|
|
1105
|
+
};
|
|
1106
|
+
}
|
|
1107
|
+
if (prop === "doGenerate" && typeof originalValue === "function") {
|
|
1108
|
+
return async function(...args) {
|
|
1109
|
+
const result = await originalValue.apply(target, args);
|
|
1110
|
+
if (result && result.usage) {
|
|
1111
|
+
handleUsage(result.usage);
|
|
1010
1112
|
}
|
|
1011
1113
|
return result;
|
|
1012
1114
|
};
|
|
1013
1115
|
}
|
|
1014
1116
|
return originalValue;
|
|
1015
1117
|
}
|
|
1016
|
-
}
|
|
1118
|
+
};
|
|
1119
|
+
return new Proxy(model, handler);
|
|
1017
1120
|
}
|
|
1018
1121
|
var meteredModel = withBilling;
|
|
1019
1122
|
|
|
1123
|
+
// src/ai-sdk/declarative.ts
|
|
1124
|
+
function createVibezModel(modelOrId, options = {}) {
|
|
1125
|
+
if (typeof modelOrId === "object" && modelOrId !== null) {
|
|
1126
|
+
return withBilling(modelOrId, options);
|
|
1127
|
+
}
|
|
1128
|
+
if (typeof modelOrId === "string") {
|
|
1129
|
+
const rawId = modelOrId;
|
|
1130
|
+
let providerName = "openai";
|
|
1131
|
+
let cleanModelId = rawId;
|
|
1132
|
+
if (rawId.includes("/")) {
|
|
1133
|
+
const parts = rawId.split("/");
|
|
1134
|
+
providerName = parts[0].toLowerCase();
|
|
1135
|
+
cleanModelId = parts.slice(1).join("/");
|
|
1136
|
+
}
|
|
1137
|
+
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;
|
|
1138
|
+
const baseURL = options.baseURL || process.env.AI_GATEWAY_BASE_URL || (providerName === "openai" ? "https://api.openai.com/v1" : void 0);
|
|
1139
|
+
let baseModelInstance = null;
|
|
1140
|
+
try {
|
|
1141
|
+
if (providerName === "openai" || providerName === "gateway" || !providerName) {
|
|
1142
|
+
const { createOpenAI } = require("@ai-sdk/openai");
|
|
1143
|
+
const openaiProvider = createOpenAI({ apiKey, baseURL });
|
|
1144
|
+
baseModelInstance = openaiProvider(cleanModelId);
|
|
1145
|
+
} else if (providerName === "anthropic") {
|
|
1146
|
+
const { createAnthropic } = require("@ai-sdk/anthropic");
|
|
1147
|
+
const anthropicProvider = createAnthropic({ apiKey, baseURL });
|
|
1148
|
+
baseModelInstance = anthropicProvider(cleanModelId);
|
|
1149
|
+
} else if (providerName === "google") {
|
|
1150
|
+
const { createGoogleGenerativeAI } = require("@ai-sdk/google");
|
|
1151
|
+
const googleProvider = createGoogleGenerativeAI({ apiKey, baseURL });
|
|
1152
|
+
baseModelInstance = googleProvider(cleanModelId);
|
|
1153
|
+
}
|
|
1154
|
+
} catch {
|
|
1155
|
+
baseModelInstance = {
|
|
1156
|
+
specificationVersion: "v2",
|
|
1157
|
+
provider: providerName,
|
|
1158
|
+
modelId: cleanModelId
|
|
1159
|
+
};
|
|
1160
|
+
}
|
|
1161
|
+
if (!baseModelInstance) {
|
|
1162
|
+
baseModelInstance = {
|
|
1163
|
+
specificationVersion: "v2",
|
|
1164
|
+
provider: providerName,
|
|
1165
|
+
modelId: cleanModelId
|
|
1166
|
+
};
|
|
1167
|
+
}
|
|
1168
|
+
return withBilling(baseModelInstance, options);
|
|
1169
|
+
}
|
|
1170
|
+
return modelOrId;
|
|
1171
|
+
}
|
|
1172
|
+
function createVibezSession(sessionOptions = {}) {
|
|
1173
|
+
const meter = createMeter({
|
|
1174
|
+
apiKey: sessionOptions.stripeApiKey
|
|
1175
|
+
});
|
|
1176
|
+
const customerId = typeof sessionOptions.customer === "string" ? sessionOptions.customer : sessionOptions.customer?.id;
|
|
1177
|
+
return {
|
|
1178
|
+
meter,
|
|
1179
|
+
model: (modelOrId, callOptions = {}) => {
|
|
1180
|
+
return createVibezModel(modelOrId, {
|
|
1181
|
+
customer: sessionOptions.customer,
|
|
1182
|
+
pricing: sessionOptions.pricing,
|
|
1183
|
+
billing: sessionOptions.billing,
|
|
1184
|
+
meter,
|
|
1185
|
+
metadata: {
|
|
1186
|
+
...sessionOptions.metadata,
|
|
1187
|
+
...callOptions.metadata
|
|
1188
|
+
},
|
|
1189
|
+
...callOptions
|
|
1190
|
+
});
|
|
1191
|
+
},
|
|
1192
|
+
trackTool: async (name, { costUSD, metadata }) => {
|
|
1193
|
+
const event = {
|
|
1194
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1195
|
+
model: `tool:${name}`,
|
|
1196
|
+
provider: "tool",
|
|
1197
|
+
usage: {
|
|
1198
|
+
inputTokens: 0,
|
|
1199
|
+
outputTokens: 0,
|
|
1200
|
+
totalTokens: 0
|
|
1201
|
+
},
|
|
1202
|
+
cost: {
|
|
1203
|
+
inputCostUSD: 0,
|
|
1204
|
+
outputCostUSD: costUSD,
|
|
1205
|
+
totalUSD: costUSD,
|
|
1206
|
+
currency: "USD"
|
|
1207
|
+
},
|
|
1208
|
+
customerId,
|
|
1209
|
+
metadata: {
|
|
1210
|
+
...sessionOptions.metadata,
|
|
1211
|
+
...metadata,
|
|
1212
|
+
tool: name
|
|
1213
|
+
}
|
|
1214
|
+
};
|
|
1215
|
+
meter.recordUsage({
|
|
1216
|
+
model: `tool:${name}`,
|
|
1217
|
+
provider: "tool",
|
|
1218
|
+
inputTokens: 0,
|
|
1219
|
+
outputTokens: 0,
|
|
1220
|
+
customerId,
|
|
1221
|
+
metadata: event.metadata
|
|
1222
|
+
});
|
|
1223
|
+
await meter.flush();
|
|
1224
|
+
}
|
|
1225
|
+
};
|
|
1226
|
+
}
|
|
1227
|
+
var vibezcheck = Object.assign(createVibezModel, {
|
|
1228
|
+
session: createVibezSession
|
|
1229
|
+
});
|
|
1230
|
+
|
|
1020
1231
|
// src/customers/manager.ts
|
|
1021
1232
|
var import_stripe2 = __toESM(require("stripe"));
|
|
1022
1233
|
|
|
@@ -1355,43 +1566,12 @@ var VibezCheckClient = class {
|
|
|
1355
1566
|
});
|
|
1356
1567
|
}
|
|
1357
1568
|
/**
|
|
1358
|
-
*
|
|
1569
|
+
* Declarative model resolver for Vercel AI SDK
|
|
1359
1570
|
*/
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
model: params.model,
|
|
1365
|
-
messages: params.messages,
|
|
1366
|
-
stream: true,
|
|
1367
|
-
stream_options: { include_usage: true },
|
|
1368
|
-
temperature: params.temperature
|
|
1369
|
-
});
|
|
1370
|
-
const meteredStream = this.wrapStream(responseStream, {
|
|
1371
|
-
customer: params.customer,
|
|
1372
|
-
model: params.model
|
|
1373
|
-
});
|
|
1374
|
-
const encoder = new TextEncoder();
|
|
1375
|
-
const readable = new ReadableStream({
|
|
1376
|
-
async start(controller) {
|
|
1377
|
-
try {
|
|
1378
|
-
for await (const chunk of meteredStream) {
|
|
1379
|
-
const text = chunk.choices?.[0]?.delta?.content || "";
|
|
1380
|
-
if (text) {
|
|
1381
|
-
controller.enqueue(encoder.encode(text));
|
|
1382
|
-
}
|
|
1383
|
-
}
|
|
1384
|
-
controller.close();
|
|
1385
|
-
} catch (err) {
|
|
1386
|
-
controller.error(err);
|
|
1387
|
-
}
|
|
1388
|
-
}
|
|
1389
|
-
});
|
|
1390
|
-
return new Response(readable, {
|
|
1391
|
-
headers: {
|
|
1392
|
-
"Content-Type": "text/plain; charset=utf-8",
|
|
1393
|
-
"Transfer-Encoding": "chunked"
|
|
1394
|
-
}
|
|
1571
|
+
model(modelOrId, options) {
|
|
1572
|
+
return createVibezModel(modelOrId, {
|
|
1573
|
+
...options,
|
|
1574
|
+
meter: this.meter
|
|
1395
1575
|
});
|
|
1396
1576
|
}
|
|
1397
1577
|
/**
|
|
@@ -1410,9 +1590,21 @@ var VibezCheckClient = class {
|
|
|
1410
1590
|
function createVibezCheck(config = {}) {
|
|
1411
1591
|
return new VibezCheckClient(config);
|
|
1412
1592
|
}
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1593
|
+
function vibezcheck2(firstArg, secondArg) {
|
|
1594
|
+
if (typeof firstArg === "string" || firstArg && (firstArg.specificationVersion || firstArg.doGenerate || firstArg.doStream)) {
|
|
1595
|
+
return createVibezModel(firstArg, secondArg);
|
|
1596
|
+
}
|
|
1597
|
+
return new VibezCheckClient(firstArg || {});
|
|
1598
|
+
}
|
|
1599
|
+
vibezcheck2.calculateCost = calculateCost;
|
|
1600
|
+
vibezcheck2.getModelPricing = getModelPricing;
|
|
1601
|
+
vibezcheck2.registerModelPricing = registerModelPricing;
|
|
1602
|
+
vibezcheck2.create = createVibezCheck;
|
|
1603
|
+
vibezcheck2.withBilling = withBilling;
|
|
1604
|
+
vibezcheck2.createMeter = createMeter;
|
|
1605
|
+
vibezcheck2.session = createVibezSession;
|
|
1606
|
+
var vibez = new VibezCheckClient();
|
|
1607
|
+
var vibescheck = vibezcheck2;
|
|
1416
1608
|
var vibes = vibez;
|
|
1417
1609
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1418
1610
|
0 && (module.exports = {
|
|
@@ -1424,6 +1616,7 @@ var vibes = vibez;
|
|
|
1424
1616
|
MODEL_PRICING_TABLE,
|
|
1425
1617
|
MeterBatcher,
|
|
1426
1618
|
VibezCheckClient,
|
|
1619
|
+
VibezCircuitBreakerError,
|
|
1427
1620
|
VibezMeter,
|
|
1428
1621
|
calculateCost,
|
|
1429
1622
|
calculateUsageCost,
|
|
@@ -1432,6 +1625,8 @@ var vibes = vibez;
|
|
|
1432
1625
|
createCustomerManager,
|
|
1433
1626
|
createMeter,
|
|
1434
1627
|
createVibezCheck,
|
|
1628
|
+
createVibezModel,
|
|
1629
|
+
createVibezSession,
|
|
1435
1630
|
detectAndExtractUsage,
|
|
1436
1631
|
extractAnthropicResponseUsage,
|
|
1437
1632
|
extractAuthToken,
|