vibezcheck 0.4.2 → 0.5.3
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/LICENSE +21 -21
- package/README.md +249 -273
- package/bin/vibezcheck.js +4 -4
- package/dist/ai-sdk/index.d.mts +28 -59
- package/dist/ai-sdk/index.d.ts +28 -59
- package/dist/ai-sdk/index.js +307 -69
- package/dist/ai-sdk/index.js.map +1 -1
- package/dist/ai-sdk/index.mjs +307 -69
- package/dist/ai-sdk/index.mjs.map +1 -1
- package/dist/ai-sdk/middleware.d.mts +15 -0
- package/dist/ai-sdk/middleware.d.ts +15 -0
- package/dist/ai-sdk/middleware.js +1433 -0
- package/dist/ai-sdk/middleware.js.map +1 -0
- package/dist/ai-sdk/middleware.mjs +1396 -0
- package/dist/ai-sdk/middleware.mjs.map +1 -0
- package/dist/auth/index.js.map +1 -1
- package/dist/auth/index.mjs.map +1 -1
- package/dist/billing/index.d.mts +90 -1
- package/dist/billing/index.d.ts +90 -1
- package/dist/billing/index.js +1542 -2
- package/dist/billing/index.js.map +1 -1
- package/dist/billing/index.mjs +1537 -1
- package/dist/billing/index.mjs.map +1 -1
- package/dist/cli/index.js +59 -7
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/index.mjs +59 -7
- package/dist/cli/index.mjs.map +1 -1
- package/dist/{client-ynfWcHX6.d.ts → client-BORmJy8s.d.ts} +1 -1
- package/dist/{client-w_hWhQ6g.d.mts → client-CbuXmoXz.d.mts} +1 -1
- package/dist/compat/stripe-meter.d.mts +21 -0
- package/dist/compat/stripe-meter.d.ts +21 -0
- package/dist/compat/stripe-meter.js +1451 -0
- package/dist/compat/stripe-meter.js.map +1 -0
- package/dist/compat/stripe-meter.mjs +1414 -0
- package/dist/compat/stripe-meter.mjs.map +1 -0
- package/dist/compat/stripe-provider.d.mts +34 -0
- package/dist/compat/stripe-provider.d.ts +34 -0
- package/dist/compat/stripe-provider.js +1620 -0
- package/dist/compat/stripe-provider.js.map +1 -0
- package/dist/compat/stripe-provider.mjs +1587 -0
- package/dist/compat/stripe-provider.mjs.map +1 -0
- package/dist/compat/token-meter.d.mts +28 -0
- package/dist/compat/token-meter.d.ts +28 -0
- package/dist/compat/token-meter.js +1320 -0
- package/dist/compat/token-meter.js.map +1 -0
- package/dist/compat/token-meter.mjs +1283 -0
- package/dist/compat/token-meter.mjs.map +1 -0
- package/dist/customers/index.d.mts +1 -1
- package/dist/customers/index.d.ts +1 -1
- package/dist/customers/index.js.map +1 -1
- package/dist/customers/index.mjs.map +1 -1
- package/dist/index.d.mts +25 -11
- package/dist/index.d.ts +25 -11
- package/dist/index.js +810 -72
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +798 -72
- 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 +176 -42
- package/dist/meter/index.js.map +1 -1
- package/dist/meter/index.mjs +176 -42
- package/dist/meter/index.mjs.map +1 -1
- package/dist/pricing/index.d.mts +15 -4
- package/dist/pricing/index.d.ts +15 -4
- package/dist/pricing/index.js +165 -29
- package/dist/pricing/index.js.map +1 -1
- package/dist/pricing/index.mjs +164 -29
- package/dist/pricing/index.mjs.map +1 -1
- package/dist/react/index.d.mts +46 -2
- package/dist/react/index.d.ts +46 -2
- package/dist/react/index.js +471 -29
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +469 -29
- package/dist/react/index.mjs.map +1 -1
- package/dist/{types-Mozk4-Mr.d.mts → types-6P71CXAD.d.mts} +16 -6
- package/dist/{types-Mozk4-Mr.d.ts → types-6P71CXAD.d.ts} +16 -6
- package/dist/with-billing-Bj6Ya_Iy.d.ts +49 -0
- package/dist/with-billing-DndWGxL8.d.mts +49 -0
- package/package.json +35 -10
package/dist/index.js
CHANGED
|
@@ -30,11 +30,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
|
+
AgentSession: () => AgentSession,
|
|
33
34
|
AnthropicStreamAccumulator: () => AnthropicStreamAccumulator,
|
|
34
35
|
ApiKeyAuth: () => ApiKeyAuth,
|
|
35
36
|
BillingHelper: () => BillingHelper,
|
|
36
37
|
CustomerCache: () => CustomerCache,
|
|
37
38
|
CustomerManager: () => CustomerManager,
|
|
39
|
+
MODEL_ALIASES: () => MODEL_ALIASES,
|
|
38
40
|
MODEL_PRICING_TABLE: () => MODEL_PRICING_TABLE,
|
|
39
41
|
MeterBatcher: () => MeterBatcher,
|
|
40
42
|
VibezCheckClient: () => VibezCheckClient,
|
|
@@ -42,10 +44,14 @@ __export(src_exports, {
|
|
|
42
44
|
VibezMeter: () => VibezMeter,
|
|
43
45
|
calculateCost: () => calculateCost,
|
|
44
46
|
calculateUsageCost: () => calculateUsageCost,
|
|
47
|
+
createAgentSession: () => createAgentSession,
|
|
45
48
|
createApiKeyAuth: () => createApiKeyAuth,
|
|
46
49
|
createBillingHelper: () => createBillingHelper,
|
|
47
50
|
createCustomerManager: () => createCustomerManager,
|
|
48
51
|
createMeter: () => createMeter,
|
|
52
|
+
createStripe: () => createStripe,
|
|
53
|
+
createStripeV3: () => createStripeV3,
|
|
54
|
+
createTokenMeter: () => createTokenMeter,
|
|
49
55
|
createVibezCheck: () => createVibezCheck,
|
|
50
56
|
createVibezModel: () => createVibezModel,
|
|
51
57
|
createVibezSession: () => createVibezSession,
|
|
@@ -57,20 +63,26 @@ __export(src_exports, {
|
|
|
57
63
|
extractOpenAIResponseUsage: () => extractOpenAIResponseUsage,
|
|
58
64
|
getModelPricing: () => getModelPricing,
|
|
59
65
|
inspectOpenAIStreamChunk: () => inspectOpenAIStreamChunk,
|
|
66
|
+
instrumentToolKit: () => instrumentToolKit,
|
|
60
67
|
meteredModel: () => meteredModel,
|
|
61
68
|
normalizeCustomer: () => normalizeCustomer,
|
|
62
69
|
normalizeModelKey: () => normalizeModelKey,
|
|
63
70
|
registerModelPricing: () => registerModelPricing,
|
|
71
|
+
stripe: () => stripe,
|
|
72
|
+
stripeMeteredModel: () => meteredModel2,
|
|
73
|
+
stripeV3: () => stripeV3,
|
|
64
74
|
trackTokens: () => trackTokens,
|
|
65
75
|
vibes: () => vibes,
|
|
66
76
|
vibescheck: () => vibescheck,
|
|
67
77
|
vibez: () => vibez,
|
|
68
78
|
vibezcheck: () => vibezcheck2,
|
|
79
|
+
vibezcheckMiddleware: () => vibezcheckMiddleware,
|
|
69
80
|
withBilling: () => withBilling,
|
|
70
81
|
wrapAnthropicStream: () => wrapAnthropicStream,
|
|
71
82
|
wrapGeminiStream: () => wrapGeminiStream,
|
|
72
83
|
wrapOpenAIStream: () => wrapOpenAIStream,
|
|
73
84
|
wrapStream: () => wrapStream,
|
|
85
|
+
wrapTool: () => wrapTool,
|
|
74
86
|
wrapUniversalStream: () => wrapUniversalStream
|
|
75
87
|
});
|
|
76
88
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -337,12 +349,12 @@ function extractOpenAIResponseUsage(response) {
|
|
|
337
349
|
function inspectOpenAIStreamChunk(chunk) {
|
|
338
350
|
if (!chunk || typeof chunk !== "object") return {};
|
|
339
351
|
const model = chunk.model;
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
const inputTokens = rawUsage.prompt_tokens ?? rawUsage.input_tokens ?? 0;
|
|
343
|
-
const outputTokens = rawUsage.completion_tokens ?? rawUsage.output_tokens ?? 0;
|
|
344
|
-
const reasoningTokens = rawUsage.completion_tokens_details?.reasoning_tokens ?? rawUsage.output_token_details?.reasoning_tokens ?? 0;
|
|
345
|
-
const cachedTokens = rawUsage.prompt_tokens_details?.cached_tokens ?? rawUsage.input_token_details?.cached_tokens ?? 0;
|
|
352
|
+
const rawUsage = chunk.usage || chunk.token_usage || chunk.x_groq?.usage || chunk.usageMetadata;
|
|
353
|
+
if (rawUsage) {
|
|
354
|
+
const inputTokens = rawUsage.prompt_tokens ?? rawUsage.input_tokens ?? rawUsage.promptTokens ?? rawUsage.inputTokens ?? rawUsage.promptTokenCount ?? 0;
|
|
355
|
+
const outputTokens = rawUsage.completion_tokens ?? rawUsage.output_tokens ?? rawUsage.completionTokens ?? rawUsage.outputTokens ?? rawUsage.candidatesTokenCount ?? 0;
|
|
356
|
+
const reasoningTokens = rawUsage.completion_tokens_details?.reasoning_tokens ?? rawUsage.output_token_details?.reasoning_tokens ?? rawUsage.reasoning_tokens ?? rawUsage.reasoningTokens ?? rawUsage.thoughtsTokenCount ?? 0;
|
|
357
|
+
const cachedTokens = rawUsage.prompt_tokens_details?.cached_tokens ?? rawUsage.input_token_details?.cached_tokens ?? rawUsage.prompt_cache_hit_tokens ?? rawUsage.cached_tokens ?? rawUsage.cachedTokens ?? rawUsage.cachedContentTokenCount ?? 0;
|
|
346
358
|
return {
|
|
347
359
|
model,
|
|
348
360
|
usage: {
|
|
@@ -357,11 +369,11 @@ function inspectOpenAIStreamChunk(chunk) {
|
|
|
357
369
|
}
|
|
358
370
|
if (chunk.type === "response.completed" || chunk.type === "response.done") {
|
|
359
371
|
if (chunk.response?.usage) {
|
|
360
|
-
const
|
|
361
|
-
const inputTokens =
|
|
362
|
-
const outputTokens =
|
|
363
|
-
const reasoningTokens =
|
|
364
|
-
const cachedTokens =
|
|
372
|
+
const rawUsage2 = chunk.response.usage;
|
|
373
|
+
const inputTokens = rawUsage2.input_tokens ?? 0;
|
|
374
|
+
const outputTokens = rawUsage2.output_tokens ?? 0;
|
|
375
|
+
const reasoningTokens = rawUsage2.output_token_details?.reasoning_tokens ?? 0;
|
|
376
|
+
const cachedTokens = rawUsage2.input_token_details?.cached_tokens ?? 0;
|
|
365
377
|
return {
|
|
366
378
|
model: chunk.response.model || model,
|
|
367
379
|
usage: {
|
|
@@ -526,7 +538,7 @@ function detectAndExtractUsage(response, fallbackModel, fallbackProvider) {
|
|
|
526
538
|
|
|
527
539
|
// src/pricing/table.ts
|
|
528
540
|
var MODEL_PRICING_TABLE = {
|
|
529
|
-
// --- OpenAI ---
|
|
541
|
+
// --- OpenAI (Modern & Reasoning) ---
|
|
530
542
|
"gpt-5.6-sol": { inputPer1M: 4, outputPer1M: 20, cachedInputPer1M: 0.4 },
|
|
531
543
|
"gpt-5.6-terra": { inputPer1M: 2, outputPer1M: 12, cachedInputPer1M: 0.2 },
|
|
532
544
|
"gpt-5.6-luna": { inputPer1M: 0.2, outputPer1M: 1.2, cachedInputPer1M: 0.02 },
|
|
@@ -538,11 +550,22 @@ var MODEL_PRICING_TABLE = {
|
|
|
538
550
|
"o3-mini": { inputPer1M: 1.1, outputPer1M: 4.4, cachedInputPer1M: 0.55 },
|
|
539
551
|
"gpt-4o": { inputPer1M: 2.5, outputPer1M: 10, cachedInputPer1M: 1.25 },
|
|
540
552
|
"gpt-4o-mini": { inputPer1M: 0.15, outputPer1M: 0.6, cachedInputPer1M: 0.075 },
|
|
553
|
+
"gpt-4.5": { inputPer1M: 75, outputPer1M: 150, cachedInputPer1M: 37.5 },
|
|
554
|
+
"gpt-4.5-preview": { inputPer1M: 75, outputPer1M: 150, cachedInputPer1M: 37.5 },
|
|
555
|
+
"chatgpt-4o-latest": { inputPer1M: 5, outputPer1M: 15 },
|
|
541
556
|
"gpt-4.1": { inputPer1M: 2, outputPer1M: 8, cachedInputPer1M: 1 },
|
|
542
557
|
"gpt-4.1-nano": { inputPer1M: 0.1, outputPer1M: 0.4, cachedInputPer1M: 0.05 },
|
|
558
|
+
// --- OpenAI (Legacy & Backward Compatibility) ---
|
|
559
|
+
"gpt-4-turbo": { inputPer1M: 10, outputPer1M: 30, cachedInputPer1M: 5 },
|
|
560
|
+
"gpt-4-turbo-preview": { inputPer1M: 10, outputPer1M: 30, cachedInputPer1M: 5 },
|
|
561
|
+
"gpt-4": { inputPer1M: 30, outputPer1M: 60 },
|
|
562
|
+
"gpt-4-32k": { inputPer1M: 60, outputPer1M: 120 },
|
|
563
|
+
"gpt-3.5-turbo": { inputPer1M: 0.5, outputPer1M: 1.5 },
|
|
564
|
+
"gpt-3.5-turbo-16k": { inputPer1M: 3, outputPer1M: 4 },
|
|
543
565
|
"text-embedding-3-small": { inputPer1M: 0.02, outputPer1M: 0 },
|
|
544
566
|
"text-embedding-3-large": { inputPer1M: 0.13, outputPer1M: 0 },
|
|
545
|
-
|
|
567
|
+
"text-embedding-ada-002": { inputPer1M: 0.1, outputPer1M: 0 },
|
|
568
|
+
// --- Anthropic (Modern & Extended Thinking) ---
|
|
546
569
|
"claude-3-7-sonnet": { inputPer1M: 0.59, outputPer1M: 2.93, cachedInputPer1M: 0.3 },
|
|
547
570
|
"claude-sonnet-5": { inputPer1M: 2, outputPer1M: 10, cachedInputPer1M: 0.3 },
|
|
548
571
|
"claude-3-5-sonnet": { inputPer1M: 3, outputPer1M: 15, cachedInputPer1M: 0.3 },
|
|
@@ -550,48 +573,145 @@ var MODEL_PRICING_TABLE = {
|
|
|
550
573
|
"haiku-4.5": { inputPer1M: 1, outputPer1M: 5, cachedInputPer1M: 0.1 },
|
|
551
574
|
"claude-opus-5": { inputPer1M: 5, outputPer1M: 25, cachedInputPer1M: 1.5 },
|
|
552
575
|
"claude-3-opus": { inputPer1M: 15, outputPer1M: 75, cachedInputPer1M: 1.5 },
|
|
553
|
-
// ---
|
|
576
|
+
// --- Anthropic (Legacy & Backward Compatibility) ---
|
|
577
|
+
"claude-3-sonnet": { inputPer1M: 3, outputPer1M: 15, cachedInputPer1M: 0.3 },
|
|
578
|
+
"claude-3-haiku": { inputPer1M: 0.25, outputPer1M: 1.25, cachedInputPer1M: 0.025 },
|
|
579
|
+
"claude-2.1": { inputPer1M: 8, outputPer1M: 24 },
|
|
580
|
+
"claude-2.0": { inputPer1M: 8, outputPer1M: 24 },
|
|
581
|
+
"claude-instant-1.2": { inputPer1M: 1.63, outputPer1M: 5.51 },
|
|
582
|
+
// --- Google Gemini (Modern & Thoughts) ---
|
|
554
583
|
"gemini-3.7-flash": { inputPer1M: 0.75, outputPer1M: 3.75, cachedInputPer1M: 0.18 },
|
|
555
584
|
"gemini-3.1-pro": { inputPer1M: 2, outputPer1M: 12, cachedInputPer1M: 0.5 },
|
|
556
585
|
"gemini-3.5-flash": { inputPer1M: 1.5, outputPer1M: 9, cachedInputPer1M: 0.38 },
|
|
557
586
|
"gemini-3.1-flash-lite": { inputPer1M: 0.25, outputPer1M: 1.5, cachedInputPer1M: 0.06 },
|
|
587
|
+
"gemini-2.5-pro": { inputPer1M: 1.25, outputPer1M: 5, cachedInputPer1M: 0.3125 },
|
|
588
|
+
"gemini-2.5-flash": { inputPer1M: 0.15, outputPer1M: 0.6, cachedInputPer1M: 0.0375 },
|
|
558
589
|
"gemini-2.0-flash": { inputPer1M: 0.1, outputPer1M: 0.4, cachedInputPer1M: 0.025 },
|
|
590
|
+
"gemini-2.0-flash-lite": { inputPer1M: 0.075, outputPer1M: 0.3, cachedInputPer1M: 0.01875 },
|
|
591
|
+
"gemini-2.0-pro": { inputPer1M: 1.25, outputPer1M: 5, cachedInputPer1M: 0.3125 },
|
|
592
|
+
// --- Google Gemini (Legacy & Backward Compatibility) ---
|
|
559
593
|
"gemini-1.5-pro": { inputPer1M: 1.25, outputPer1M: 5, cachedInputPer1M: 0.3125 },
|
|
560
594
|
"gemini-1.5-flash": { inputPer1M: 0.075, outputPer1M: 0.3, cachedInputPer1M: 0.01875 },
|
|
595
|
+
"gemini-1.5-flash-8b": { inputPer1M: 0.0375, outputPer1M: 0.15, cachedInputPer1M: 9375e-6 },
|
|
596
|
+
"gemini-1.0-pro": { inputPer1M: 0.5, outputPer1M: 1.5 },
|
|
561
597
|
// --- xAI Grok ---
|
|
562
598
|
"grok-4.6": { inputPer1M: 3, outputPer1M: 15 },
|
|
599
|
+
"grok-3": { inputPer1M: 3, outputPer1M: 15 },
|
|
600
|
+
"grok-3-mini": { inputPer1M: 0.3, outputPer1M: 1.5 },
|
|
563
601
|
"grok-2": { inputPer1M: 2, outputPer1M: 10 },
|
|
564
602
|
"grok-2-vision": { inputPer1M: 2, outputPer1M: 10 },
|
|
565
603
|
"grok-beta": { inputPer1M: 5, outputPer1M: 15 },
|
|
566
604
|
// --- Mistral ---
|
|
567
605
|
"mistral-large-3": { inputPer1M: 2, outputPer1M: 6 },
|
|
568
606
|
"mistral-large-latest": { inputPer1M: 2, outputPer1M: 6 },
|
|
607
|
+
"mistral-large-2411": { inputPer1M: 2, outputPer1M: 6 },
|
|
569
608
|
"codestral-latest": { inputPer1M: 0.3, outputPer1M: 0.9 },
|
|
609
|
+
"mistral-medium-latest": { inputPer1M: 2.7, outputPer1M: 8.1 },
|
|
570
610
|
"mistral-small-latest": { inputPer1M: 0.2, outputPer1M: 0.6 },
|
|
571
611
|
"ministral-8b-latest": { inputPer1M: 0.1, outputPer1M: 0.1 },
|
|
572
|
-
|
|
612
|
+
"ministral-3b-latest": { inputPer1M: 0.04, outputPer1M: 0.04 },
|
|
613
|
+
"open-mistral-7b": { inputPer1M: 0.2, outputPer1M: 0.2 },
|
|
614
|
+
"open-mixtral-8x7b": { inputPer1M: 0.7, outputPer1M: 0.7 },
|
|
615
|
+
"open-mixtral-8x22b": { inputPer1M: 2, outputPer1M: 6 },
|
|
616
|
+
// --- Groq & Meta Llama LPUs ---
|
|
573
617
|
"llama-3.3-70b-versatile": { inputPer1M: 0.59, outputPer1M: 0.79 },
|
|
618
|
+
"llama-3.1-405b": { inputPer1M: 3, outputPer1M: 3 },
|
|
619
|
+
"llama-3.1-70b-versatile": { inputPer1M: 0.59, outputPer1M: 0.79 },
|
|
574
620
|
"llama-3.1-8b-instant": { inputPer1M: 0.05, outputPer1M: 0.08 },
|
|
621
|
+
"llama-3.2-1b-preview": { inputPer1M: 0.04, outputPer1M: 0.04 },
|
|
622
|
+
"llama-3.2-3b-preview": { inputPer1M: 0.06, outputPer1M: 0.06 },
|
|
623
|
+
"llama-3.2-11b-vision": { inputPer1M: 0.18, outputPer1M: 0.18 },
|
|
624
|
+
"llama-3.2-90b-vision": { inputPer1M: 0.9, outputPer1M: 0.9 },
|
|
575
625
|
"deepseek-r1-distill-llama-70b": { inputPer1M: 0.75, outputPer1M: 0.99 },
|
|
626
|
+
"deepseek-r1-distill-qwen-32b": { inputPer1M: 0.49, outputPer1M: 0.49 },
|
|
576
627
|
"qwen-2.5-32b": { inputPer1M: 0.29, outputPer1M: 0.39 },
|
|
628
|
+
"qwen-2.5-72b": { inputPer1M: 0.35, outputPer1M: 0.4 },
|
|
629
|
+
"mixtral-8x7b-32768": { inputPer1M: 0.24, outputPer1M: 0.24 },
|
|
630
|
+
"gemma2-9b-it": { inputPer1M: 0.2, outputPer1M: 0.2 },
|
|
577
631
|
// --- DeepSeek ---
|
|
578
632
|
"deepseek-v4-pro": { inputPer1M: 0.66, outputPer1M: 1.98, cachedInputPer1M: 0.15 },
|
|
579
633
|
"deepseek-v4-flash": { inputPer1M: 0.22, outputPer1M: 0.66, cachedInputPer1M: 0.05 },
|
|
580
|
-
"deepseek-
|
|
581
|
-
"deepseek-
|
|
634
|
+
"deepseek-v3": { inputPer1M: 0.14, outputPer1M: 0.28, cachedInputPer1M: 0.014 },
|
|
635
|
+
"deepseek-chat": { inputPer1M: 0.14, outputPer1M: 0.28, cachedInputPer1M: 0.014 },
|
|
636
|
+
"deepseek-r1": { inputPer1M: 0.55, outputPer1M: 2.19, cachedInputPer1M: 0.14 },
|
|
637
|
+
"deepseek-reasoner": { inputPer1M: 0.55, outputPer1M: 2.19, cachedInputPer1M: 0.14 },
|
|
582
638
|
// --- Cohere ---
|
|
583
639
|
"command-r-plus": { inputPer1M: 2.5, outputPer1M: 10 },
|
|
584
|
-
"command-r": { inputPer1M: 0.15, outputPer1M: 0.6 }
|
|
640
|
+
"command-r": { inputPer1M: 0.15, outputPer1M: 0.6 },
|
|
641
|
+
"command": { inputPer1M: 1, outputPer1M: 2 },
|
|
642
|
+
"command-light": { inputPer1M: 0.3, outputPer1M: 0.6 },
|
|
643
|
+
"embed-english-v3.0": { inputPer1M: 0.1, outputPer1M: 0 },
|
|
644
|
+
// --- Perplexity ---
|
|
645
|
+
"sonar": { inputPer1M: 1, outputPer1M: 1 },
|
|
646
|
+
"sonar-pro": { inputPer1M: 3, outputPer1M: 15 },
|
|
647
|
+
"sonar-reasoning": { inputPer1M: 1, outputPer1M: 5 },
|
|
648
|
+
"sonar-reasoning-pro": { inputPer1M: 2, outputPer1M: 8 }
|
|
649
|
+
};
|
|
650
|
+
var MODEL_ALIASES = {
|
|
651
|
+
// OpenAI Shorthands & Aliases
|
|
652
|
+
"gpt4": "gpt-4",
|
|
653
|
+
"gpt4o": "gpt-4o",
|
|
654
|
+
"gpt-4-preview": "gpt-4-turbo",
|
|
655
|
+
"gpt-4-0125-preview": "gpt-4-turbo",
|
|
656
|
+
"gpt-4-1106-preview": "gpt-4-turbo",
|
|
657
|
+
"gpt-3.5": "gpt-3.5-turbo",
|
|
658
|
+
"gpt-3.5-turbo-0125": "gpt-3.5-turbo",
|
|
659
|
+
"gpt-3.5-turbo-1106": "gpt-3.5-turbo",
|
|
660
|
+
"gpt-3.5-turbo-16k-0613": "gpt-3.5-turbo-16k",
|
|
661
|
+
// Anthropic Shorthands
|
|
662
|
+
"sonnet": "claude-3-5-sonnet",
|
|
663
|
+
"sonnet-3.7": "claude-3-7-sonnet",
|
|
664
|
+
"sonnet-3.5": "claude-3-5-sonnet",
|
|
665
|
+
"haiku": "claude-3-5-haiku",
|
|
666
|
+
"haiku-3.5": "claude-3-5-haiku",
|
|
667
|
+
"opus": "claude-3-opus",
|
|
668
|
+
"claude-2": "claude-2.0",
|
|
669
|
+
"claude-instant": "claude-instant-1.2",
|
|
670
|
+
// DeepSeek Shorthands
|
|
671
|
+
"r1": "deepseek-r1",
|
|
672
|
+
"v3": "deepseek-v3",
|
|
673
|
+
// Gemini Shorthands
|
|
674
|
+
"flash": "gemini-2.0-flash",
|
|
675
|
+
"pro": "gemini-1.5-pro",
|
|
676
|
+
// Meta / Groq Shorthands
|
|
677
|
+
"llama-3.3-70b": "llama-3.3-70b-versatile",
|
|
678
|
+
"llama-3.1-70b": "llama-3.1-70b-versatile",
|
|
679
|
+
"llama-3.1-8b": "llama-3.1-8b-instant",
|
|
680
|
+
"llama-3-70b": "llama-3.1-70b-versatile",
|
|
681
|
+
"llama-3-8b": "llama-3.1-8b-instant",
|
|
682
|
+
// Mistral Shorthands
|
|
683
|
+
"codestral": "codestral-latest",
|
|
684
|
+
"mistral-large": "mistral-large-latest",
|
|
685
|
+
"mistral-small": "mistral-small-latest"
|
|
585
686
|
};
|
|
586
687
|
var customPricingRegistry = {};
|
|
587
688
|
function normalizeModelKey(rawModel) {
|
|
588
689
|
if (!rawModel) return "unknown";
|
|
589
690
|
let model = rawModel.toLowerCase().trim();
|
|
691
|
+
model = model.replace(/^[a-z0-9_-]+\.(anthropic|meta|amazon|cohere|mistral|ai21)\./i, "");
|
|
692
|
+
model = model.replace(/^(anthropic|meta|amazon|cohere|mistral|ai21)\./i, "");
|
|
693
|
+
model = model.replace(/-v\d+(:\d+)?$/, "");
|
|
694
|
+
model = model.replace(/:\d+$/, "");
|
|
590
695
|
if (model.includes("/")) {
|
|
591
|
-
model = model.split("/")
|
|
696
|
+
model = model.split("/").slice(1).join("/");
|
|
592
697
|
}
|
|
698
|
+
model = model.replace(/:(latest|free|beta)$/, "");
|
|
593
699
|
model = model.replace(/-\d{8}$/, "");
|
|
594
700
|
model = model.replace(/-\d{4}-\d{2}-\d{2}$/, "");
|
|
701
|
+
model = model.replace(/llama(\d+)-(\d+)-/g, "llama-$1.$2-");
|
|
702
|
+
model = model.replace(/llama(\d+)\.(\d+)-/g, "llama-$1.$2-");
|
|
703
|
+
if (MODEL_ALIASES[model]) {
|
|
704
|
+
return MODEL_ALIASES[model];
|
|
705
|
+
}
|
|
706
|
+
if (!MODEL_PRICING_TABLE[model]) {
|
|
707
|
+
const withoutInstruct = model.replace(/-(instruct|chat|preview)$/, "");
|
|
708
|
+
if (MODEL_PRICING_TABLE[withoutInstruct]) {
|
|
709
|
+
return withoutInstruct;
|
|
710
|
+
}
|
|
711
|
+
if (MODEL_ALIASES[withoutInstruct]) {
|
|
712
|
+
return MODEL_ALIASES[withoutInstruct];
|
|
713
|
+
}
|
|
714
|
+
}
|
|
595
715
|
return model;
|
|
596
716
|
}
|
|
597
717
|
function getModelPricing(modelName) {
|
|
@@ -608,6 +728,10 @@ function getModelPricing(modelName) {
|
|
|
608
728
|
if (MODEL_PRICING_TABLE[modelName]) {
|
|
609
729
|
return MODEL_PRICING_TABLE[modelName];
|
|
610
730
|
}
|
|
731
|
+
const alias = MODEL_ALIASES[modelName.toLowerCase().trim()];
|
|
732
|
+
if (alias && MODEL_PRICING_TABLE[alias]) {
|
|
733
|
+
return MODEL_PRICING_TABLE[alias];
|
|
734
|
+
}
|
|
611
735
|
return {
|
|
612
736
|
inputPer1M: 1,
|
|
613
737
|
outputPer1M: 3,
|
|
@@ -635,35 +759,57 @@ function calculateCost(params) {
|
|
|
635
759
|
} else {
|
|
636
760
|
rates = getModelPricing(params.model);
|
|
637
761
|
}
|
|
638
|
-
const inputTokens = params.inputTokens ?? 0;
|
|
639
|
-
const outputTokens = params.outputTokens ?? 0;
|
|
640
|
-
const reasoningTokens = params.reasoningTokens ?? 0;
|
|
641
|
-
const cachedTokens = params.cachedTokens ?? 0;
|
|
642
|
-
const regularInputTokens =
|
|
643
|
-
const
|
|
644
|
-
const
|
|
645
|
-
const
|
|
646
|
-
const
|
|
647
|
-
const
|
|
648
|
-
const
|
|
649
|
-
const
|
|
650
|
-
const
|
|
651
|
-
const
|
|
652
|
-
|
|
762
|
+
const inputTokens = BigInt(Math.max(0, params.inputTokens ?? 0));
|
|
763
|
+
const outputTokens = BigInt(Math.max(0, params.outputTokens ?? 0));
|
|
764
|
+
const reasoningTokens = BigInt(Math.max(0, params.reasoningTokens ?? 0));
|
|
765
|
+
const cachedTokens = BigInt(Math.max(0, params.cachedTokens ?? 0));
|
|
766
|
+
const regularInputTokens = inputTokens > cachedTokens ? inputTokens - cachedTokens : 0n;
|
|
767
|
+
const rateInputNano = BigInt(Math.round(rates.inputPer1M * 1e3));
|
|
768
|
+
const cachedPer1M = rates.cachedInputPer1M ?? rates.inputPer1M * 0.15;
|
|
769
|
+
const rateCachedNano = BigInt(Math.round(cachedPer1M * 1e3));
|
|
770
|
+
const rateOutputNano = BigInt(Math.round(rates.outputPer1M * 1e3));
|
|
771
|
+
const reasoningPer1M = rates.reasoningPer1M ?? rates.outputPer1M;
|
|
772
|
+
const rateReasoningNano = BigInt(Math.round(reasoningPer1M * 1e3));
|
|
773
|
+
const regularInputCostNano = regularInputTokens * rateInputNano;
|
|
774
|
+
const cachedInputCostNano = cachedTokens * rateCachedNano;
|
|
775
|
+
const inputCostNano = regularInputCostNano + cachedInputCostNano;
|
|
776
|
+
const outputCostNano = outputTokens * rateOutputNano;
|
|
777
|
+
const reasoningCostNano = reasoningTokens * rateReasoningNano;
|
|
778
|
+
const standardCacheCostNano = cachedTokens * rateInputNano;
|
|
779
|
+
const cachedDiscountNano = standardCacheCostNano > cachedInputCostNano ? standardCacheCostNano - cachedInputCostNano : 0n;
|
|
780
|
+
const wholesaleNano = inputCostNano + outputCostNano;
|
|
653
781
|
const markup = params.markupMultiplier ?? 1;
|
|
654
|
-
let
|
|
782
|
+
let billedNano = wholesaleNano;
|
|
783
|
+
let hasRetail = false;
|
|
655
784
|
if (markup !== 1 || params.minimumChargeUSD !== void 0) {
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
785
|
+
hasRetail = true;
|
|
786
|
+
const markupMultiplierNano = BigInt(Math.round(markup * 1e3));
|
|
787
|
+
billedNano = wholesaleNano * markupMultiplierNano / 1000n;
|
|
788
|
+
if (params.minimumChargeUSD !== void 0 && params.minimumChargeUSD > 0) {
|
|
789
|
+
const minChargeNano = BigInt(Math.round(params.minimumChargeUSD * 1e9));
|
|
790
|
+
if (billedNano < minChargeNano) {
|
|
791
|
+
billedNano = minChargeNano;
|
|
792
|
+
}
|
|
659
793
|
}
|
|
660
794
|
}
|
|
795
|
+
const inputCostUSD = Number(inputCostNano) / 1e9;
|
|
796
|
+
const outputCostUSD = Number(outputCostNano) / 1e9;
|
|
797
|
+
const reasoningCostUSD = Number(reasoningCostNano) / 1e9;
|
|
798
|
+
const cachedDiscountUSD = Number(cachedDiscountNano) / 1e9;
|
|
799
|
+
const totalUSD = Number(wholesaleNano) / 1e9;
|
|
800
|
+
const wholesaleTotalUSD = totalUSD;
|
|
801
|
+
const billedUSD = Number(billedNano) / 1e9;
|
|
802
|
+
const profitUSD = Math.max(0, billedUSD - wholesaleTotalUSD);
|
|
803
|
+
const retailUSD = hasRetail ? billedUSD : void 0;
|
|
661
804
|
return {
|
|
662
805
|
inputCostUSD: Number(inputCostUSD.toFixed(8)),
|
|
663
806
|
outputCostUSD: Number(outputCostUSD.toFixed(8)),
|
|
664
|
-
reasoningCostUSD: reasoningTokens >
|
|
665
|
-
cachedDiscountUSD: cachedTokens >
|
|
807
|
+
reasoningCostUSD: reasoningTokens > 0n ? Number(reasoningCostUSD.toFixed(8)) : void 0,
|
|
808
|
+
cachedDiscountUSD: cachedTokens > 0n ? Number(cachedDiscountUSD.toFixed(8)) : void 0,
|
|
666
809
|
totalUSD: Number(totalUSD.toFixed(8)),
|
|
810
|
+
wholesaleTotalUSD: Number(wholesaleTotalUSD.toFixed(8)),
|
|
811
|
+
billedUSD: Number(billedUSD.toFixed(8)),
|
|
812
|
+
profitUSD: Number(profitUSD.toFixed(8)),
|
|
667
813
|
retailUSD: retailUSD !== void 0 ? Number(retailUSD.toFixed(8)) : void 0,
|
|
668
814
|
currency: rates.currency || "USD"
|
|
669
815
|
};
|
|
@@ -869,7 +1015,7 @@ function wrapUniversalStream(stream, options = {}, onComplete) {
|
|
|
869
1015
|
return wrapGeminiStream(stream, options, onComplete);
|
|
870
1016
|
}
|
|
871
1017
|
if (Symbol.asyncIterator in stream) {
|
|
872
|
-
if (options.provider === "anthropic") {
|
|
1018
|
+
if (options.provider === "anthropic" || options.model?.toLowerCase().includes("claude")) {
|
|
873
1019
|
return wrapAnthropicStream(stream, options, onComplete);
|
|
874
1020
|
}
|
|
875
1021
|
return wrapOpenAIStream(stream, options, onComplete);
|
|
@@ -891,7 +1037,7 @@ var VibezMeter = class {
|
|
|
891
1037
|
this.stripeClient = new import_stripe.default(key, {
|
|
892
1038
|
appInfo: {
|
|
893
1039
|
name: "vibezcheck",
|
|
894
|
-
version: "0.
|
|
1040
|
+
version: "0.5.3",
|
|
895
1041
|
url: "https://vibezcheck.xyz"
|
|
896
1042
|
}
|
|
897
1043
|
});
|
|
@@ -1008,10 +1154,20 @@ var VibezCircuitBreakerError = class extends Error {
|
|
|
1008
1154
|
};
|
|
1009
1155
|
|
|
1010
1156
|
// src/ai-sdk/with-billing.ts
|
|
1011
|
-
function withBilling(model,
|
|
1157
|
+
function withBilling(model, optionsOrCustomer, extraOptions = {}) {
|
|
1012
1158
|
if (!model || typeof model !== "object") {
|
|
1013
1159
|
return model;
|
|
1014
1160
|
}
|
|
1161
|
+
let options = {};
|
|
1162
|
+
if (typeof optionsOrCustomer === "string") {
|
|
1163
|
+
options = { ...extraOptions, customer: optionsOrCustomer };
|
|
1164
|
+
} else if (typeof optionsOrCustomer === "object" && optionsOrCustomer !== null) {
|
|
1165
|
+
if ("userId" in optionsOrCustomer || "email" in optionsOrCustomer || "id" in optionsOrCustomer) {
|
|
1166
|
+
options = { ...extraOptions, customer: optionsOrCustomer };
|
|
1167
|
+
} else {
|
|
1168
|
+
options = { ...optionsOrCustomer, ...extraOptions };
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1015
1171
|
const meter = options.meter || createMeter({
|
|
1016
1172
|
apiKey: options.stripeApiKey,
|
|
1017
1173
|
eventName: options.eventName
|
|
@@ -1040,17 +1196,19 @@ function withBilling(model, options = {}) {
|
|
|
1040
1196
|
};
|
|
1041
1197
|
const handleUsage = (rawUsage, extraMeta) => {
|
|
1042
1198
|
if (!rawUsage) return;
|
|
1043
|
-
const inputTokens = rawUsage.promptTokens ?? rawUsage.inputTokens ?? 0;
|
|
1044
|
-
const outputTokens = rawUsage.completionTokens ?? rawUsage.outputTokens ?? 0;
|
|
1045
|
-
const reasoningTokens = rawUsage.reasoningTokens ?? rawUsage.completionTokensDetails?.reasoningTokens ?? rawUsage.outputTokenDetails?.reasoningTokens ?? 0;
|
|
1046
|
-
const cachedTokens = rawUsage.promptTokensDetails?.cachedTokens ?? rawUsage.inputTokenDetails?.cachedTokens ?? 0;
|
|
1199
|
+
const inputTokens = rawUsage.promptTokens ?? rawUsage.inputTokens ?? rawUsage.prompt_tokens ?? rawUsage.input_tokens ?? rawUsage.promptTokenCount ?? 0;
|
|
1200
|
+
const outputTokens = rawUsage.completionTokens ?? rawUsage.outputTokens ?? rawUsage.completion_tokens ?? rawUsage.output_tokens ?? rawUsage.candidatesTokenCount ?? 0;
|
|
1201
|
+
const reasoningTokens = rawUsage.reasoningTokens ?? rawUsage.completionTokensDetails?.reasoningTokens ?? rawUsage.outputTokenDetails?.reasoningTokens ?? rawUsage.reasoning_tokens ?? rawUsage.completion_tokens_details?.reasoning_tokens ?? rawUsage.output_token_details?.reasoning_tokens ?? rawUsage.thoughtsTokenCount ?? 0;
|
|
1202
|
+
const cachedTokens = rawUsage.promptTokensDetails?.cachedTokens ?? rawUsage.inputTokenDetails?.cachedTokens ?? rawUsage.cachedTokens ?? rawUsage.cached_tokens ?? rawUsage.prompt_tokens_details?.cached_tokens ?? rawUsage.input_token_details?.cached_tokens ?? rawUsage.cachedContentTokenCount ?? rawUsage.cache_read_input_tokens ?? 0;
|
|
1203
|
+
const cacheWriteTokens = rawUsage.cacheWriteTokens ?? rawUsage.cache_creation_input_tokens ?? rawUsage.cacheCreationTokens ?? 0;
|
|
1047
1204
|
const usage = {
|
|
1048
1205
|
inputTokens,
|
|
1049
1206
|
outputTokens,
|
|
1050
1207
|
totalTokens: inputTokens + outputTokens,
|
|
1051
1208
|
reasoningTokens: reasoningTokens > 0 ? reasoningTokens : void 0,
|
|
1052
1209
|
visibleOutputTokens: Math.max(0, outputTokens - reasoningTokens),
|
|
1053
|
-
cachedTokens: cachedTokens > 0 ? cachedTokens : void 0
|
|
1210
|
+
cachedTokens: cachedTokens > 0 ? cachedTokens : void 0,
|
|
1211
|
+
cacheWriteTokens: cacheWriteTokens > 0 ? cacheWriteTokens : void 0
|
|
1054
1212
|
};
|
|
1055
1213
|
const cost = calculateUsageCost(modelId, usage, {
|
|
1056
1214
|
markupMultiplier: options.pricing?.margin,
|
|
@@ -1151,7 +1309,9 @@ function withBilling(model, options = {}) {
|
|
|
1151
1309
|
return;
|
|
1152
1310
|
}
|
|
1153
1311
|
} catch (err) {
|
|
1154
|
-
|
|
1312
|
+
if (process.env.NODE_ENV !== "test" && !options.silent) {
|
|
1313
|
+
console.warn(`[vibezcheck] Database record failed safely:`, err?.message || err);
|
|
1314
|
+
}
|
|
1155
1315
|
}
|
|
1156
1316
|
};
|
|
1157
1317
|
if (typeof globalThis !== "undefined" && typeof globalThis.after === "function") {
|
|
@@ -1167,7 +1327,9 @@ function withBilling(model, options = {}) {
|
|
|
1167
1327
|
try {
|
|
1168
1328
|
await chargeFn(cost.totalUSD, event);
|
|
1169
1329
|
} catch (err) {
|
|
1170
|
-
|
|
1330
|
+
if (process.env.NODE_ENV !== "test" && !options.silent) {
|
|
1331
|
+
console.warn(`[vibezcheck] Custom charge handler failed safely:`, err?.message || err);
|
|
1332
|
+
}
|
|
1171
1333
|
}
|
|
1172
1334
|
};
|
|
1173
1335
|
if (typeof globalThis !== "undefined" && typeof globalThis.after === "function") {
|
|
@@ -1182,7 +1344,9 @@ function withBilling(model, options = {}) {
|
|
|
1182
1344
|
try {
|
|
1183
1345
|
await providerCharge(cost.totalUSD, event);
|
|
1184
1346
|
} catch (err) {
|
|
1185
|
-
|
|
1347
|
+
if (process.env.NODE_ENV !== "test" && !options.silent) {
|
|
1348
|
+
console.warn(`[vibezcheck] Payment provider charge failed safely:`, err?.message || err);
|
|
1349
|
+
}
|
|
1186
1350
|
}
|
|
1187
1351
|
};
|
|
1188
1352
|
if (typeof globalThis !== "undefined" && typeof globalThis.after === "function") {
|
|
@@ -1237,14 +1401,19 @@ function withBilling(model, options = {}) {
|
|
|
1237
1401
|
const transformStream = new TransformStream({
|
|
1238
1402
|
transform(chunk, controller) {
|
|
1239
1403
|
controller.enqueue(chunk);
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1404
|
+
const delta = chunk.textDelta || chunk.text || chunk.delta;
|
|
1405
|
+
if (delta && typeof delta === "string") {
|
|
1406
|
+
accumulatedChars += delta.length;
|
|
1407
|
+
} else if (chunk.type === "reasoning" || chunk.type === "reasoning-delta") {
|
|
1408
|
+
const reasoning = chunk.reasoning || chunk.textDelta || chunk.text || chunk.delta || "";
|
|
1409
|
+
if (typeof reasoning === "string") {
|
|
1410
|
+
accumulatedChars += reasoning.length;
|
|
1411
|
+
}
|
|
1244
1412
|
}
|
|
1245
|
-
|
|
1413
|
+
const chunkUsage = chunk.usage || chunk.token_usage || chunk.usageMetadata;
|
|
1414
|
+
if (chunkUsage) {
|
|
1246
1415
|
streamCompleted = true;
|
|
1247
|
-
handleUsage(
|
|
1416
|
+
handleUsage(chunkUsage);
|
|
1248
1417
|
}
|
|
1249
1418
|
},
|
|
1250
1419
|
flush() {
|
|
@@ -1270,13 +1439,29 @@ function withBilling(model, options = {}) {
|
|
|
1270
1439
|
if (prop === "doGenerate" && typeof originalValue === "function") {
|
|
1271
1440
|
return async function(...args) {
|
|
1272
1441
|
const result = await originalValue.apply(target, args);
|
|
1273
|
-
|
|
1274
|
-
|
|
1442
|
+
const usage = result?.usage || result?.tokenUsage || result?.usageMetadata;
|
|
1443
|
+
if (usage) {
|
|
1444
|
+
handleUsage(usage);
|
|
1275
1445
|
}
|
|
1276
1446
|
return result;
|
|
1277
1447
|
};
|
|
1278
1448
|
}
|
|
1279
1449
|
return originalValue;
|
|
1450
|
+
},
|
|
1451
|
+
apply(target, thisArg, argArray) {
|
|
1452
|
+
if (typeof target === "function") {
|
|
1453
|
+
return Reflect.apply(target, thisArg, argArray);
|
|
1454
|
+
}
|
|
1455
|
+
return target;
|
|
1456
|
+
},
|
|
1457
|
+
has(target, prop) {
|
|
1458
|
+
return Reflect.has(target, prop);
|
|
1459
|
+
},
|
|
1460
|
+
ownKeys(target) {
|
|
1461
|
+
return Reflect.ownKeys(target);
|
|
1462
|
+
},
|
|
1463
|
+
getPrototypeOf(target) {
|
|
1464
|
+
return Reflect.getPrototypeOf(target);
|
|
1280
1465
|
}
|
|
1281
1466
|
};
|
|
1282
1467
|
return new Proxy(model, handler);
|
|
@@ -1296,23 +1481,88 @@ function createVibezModel(modelOrId, options = {}) {
|
|
|
1296
1481
|
const parts = rawId.split("/");
|
|
1297
1482
|
providerName = parts[0].toLowerCase();
|
|
1298
1483
|
cleanModelId = parts.slice(1).join("/");
|
|
1484
|
+
} else if (rawId.startsWith("claude-")) {
|
|
1485
|
+
providerName = "anthropic";
|
|
1486
|
+
} else if (rawId.startsWith("gemini-")) {
|
|
1487
|
+
providerName = "google";
|
|
1488
|
+
} else if (rawId.startsWith("deepseek-")) {
|
|
1489
|
+
providerName = "deepseek";
|
|
1490
|
+
} else if (rawId.startsWith("grok-")) {
|
|
1491
|
+
providerName = "xai";
|
|
1492
|
+
} else if (rawId.startsWith("mistral-") || rawId.startsWith("codestral-") || rawId.startsWith("ministral-") || rawId.startsWith("open-mistral") || rawId.startsWith("open-mixtral")) {
|
|
1493
|
+
providerName = "mistral";
|
|
1494
|
+
} else if (rawId.startsWith("llama-") || rawId.startsWith("mixtral-") || rawId.startsWith("gemma") || rawId.startsWith("qwen-")) {
|
|
1495
|
+
providerName = "groq";
|
|
1496
|
+
} else if (rawId.startsWith("command-")) {
|
|
1497
|
+
providerName = "cohere";
|
|
1299
1498
|
}
|
|
1300
|
-
const apiKey = options.apiKey || process.env.AI_GATEWAY_API_KEY || process.env.
|
|
1499
|
+
const apiKey = options.apiKey || process.env.AI_GATEWAY_API_KEY || (providerName === "anthropic" ? process.env.ANTHROPIC_API_KEY : void 0) || (providerName === "google" ? process.env.GOOGLE_GENERATIVE_AI_API_KEY : void 0) || (providerName === "mistral" ? process.env.MISTRAL_API_KEY : void 0) || (providerName === "groq" ? process.env.GROQ_API_KEY : void 0) || (providerName === "deepseek" ? process.env.DEEPSEEK_API_KEY : void 0) || (providerName === "xai" ? process.env.XAI_API_KEY : void 0) || (providerName === "cohere" ? process.env.COHERE_API_KEY : void 0) || process.env.OPENAI_API_KEY;
|
|
1301
1500
|
const baseURL = options.baseURL || process.env.AI_GATEWAY_BASE_URL || (providerName === "openai" ? "https://api.openai.com/v1" : void 0);
|
|
1302
1501
|
let baseModelInstance = null;
|
|
1303
1502
|
try {
|
|
1304
1503
|
if (providerName === "openai" || providerName === "gateway" || !providerName) {
|
|
1305
|
-
const
|
|
1306
|
-
const
|
|
1307
|
-
|
|
1504
|
+
const mod = require("@ai-sdk/openai");
|
|
1505
|
+
const factory = mod.createOpenAI || mod.openai || mod.default?.createOpenAI || mod.default?.openai;
|
|
1506
|
+
if (typeof factory === "function") {
|
|
1507
|
+
if (mod.createOpenAI) {
|
|
1508
|
+
const provider = factory({ apiKey, baseURL });
|
|
1509
|
+
baseModelInstance = provider(cleanModelId);
|
|
1510
|
+
} else {
|
|
1511
|
+
baseModelInstance = factory(cleanModelId);
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1308
1514
|
} else if (providerName === "anthropic") {
|
|
1309
|
-
const
|
|
1310
|
-
const
|
|
1311
|
-
|
|
1515
|
+
const mod = require("@ai-sdk/anthropic");
|
|
1516
|
+
const factory = mod.createAnthropic || mod.anthropic || mod.default?.createAnthropic || mod.default?.anthropic;
|
|
1517
|
+
if (typeof factory === "function") {
|
|
1518
|
+
if (mod.createAnthropic) {
|
|
1519
|
+
const provider = factory({ apiKey, baseURL });
|
|
1520
|
+
baseModelInstance = provider(cleanModelId);
|
|
1521
|
+
} else {
|
|
1522
|
+
baseModelInstance = factory(cleanModelId);
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1312
1525
|
} else if (providerName === "google") {
|
|
1313
|
-
const
|
|
1314
|
-
const
|
|
1315
|
-
|
|
1526
|
+
const mod = require("@ai-sdk/google");
|
|
1527
|
+
const factory = mod.createGoogleGenerativeAI || mod.google || mod.default?.createGoogleGenerativeAI || mod.default?.google;
|
|
1528
|
+
if (typeof factory === "function") {
|
|
1529
|
+
if (mod.createGoogleGenerativeAI) {
|
|
1530
|
+
const provider = factory({ apiKey, baseURL });
|
|
1531
|
+
baseModelInstance = provider(cleanModelId);
|
|
1532
|
+
} else {
|
|
1533
|
+
baseModelInstance = factory(cleanModelId);
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
} else if (providerName === "mistral") {
|
|
1537
|
+
const mod = require("@ai-sdk/mistral");
|
|
1538
|
+
const factory = mod.createMistral || mod.mistral || mod.default?.createMistral || mod.default?.mistral;
|
|
1539
|
+
if (typeof factory === "function") {
|
|
1540
|
+
baseModelInstance = mod.createMistral ? factory({ apiKey, baseURL })(cleanModelId) : factory(cleanModelId);
|
|
1541
|
+
}
|
|
1542
|
+
} else if (providerName === "groq") {
|
|
1543
|
+
const mod = require("@ai-sdk/groq");
|
|
1544
|
+
const factory = mod.createGroq || mod.groq || mod.default?.createGroq || mod.default?.groq;
|
|
1545
|
+
if (typeof factory === "function") {
|
|
1546
|
+
baseModelInstance = mod.createGroq ? factory({ apiKey, baseURL })(cleanModelId) : factory(cleanModelId);
|
|
1547
|
+
}
|
|
1548
|
+
} else if (providerName === "deepseek") {
|
|
1549
|
+
const mod = require("@ai-sdk/deepseek");
|
|
1550
|
+
const factory = mod.createDeepSeek || mod.deepseek || mod.default?.createDeepSeek || mod.default?.deepseek;
|
|
1551
|
+
if (typeof factory === "function") {
|
|
1552
|
+
baseModelInstance = mod.createDeepSeek ? factory({ apiKey, baseURL })(cleanModelId) : factory(cleanModelId);
|
|
1553
|
+
}
|
|
1554
|
+
} else if (providerName === "xai") {
|
|
1555
|
+
const mod = require("@ai-sdk/xai");
|
|
1556
|
+
const factory = mod.createXai || mod.xai || mod.default?.createXai || mod.default?.xai;
|
|
1557
|
+
if (typeof factory === "function") {
|
|
1558
|
+
baseModelInstance = mod.createXai ? factory({ apiKey, baseURL })(cleanModelId) : factory(cleanModelId);
|
|
1559
|
+
}
|
|
1560
|
+
} else if (providerName === "cohere") {
|
|
1561
|
+
const mod = require("@ai-sdk/cohere");
|
|
1562
|
+
const factory = mod.createCohere || mod.cohere || mod.default?.createCohere || mod.default?.cohere;
|
|
1563
|
+
if (typeof factory === "function") {
|
|
1564
|
+
baseModelInstance = mod.createCohere ? factory({ apiKey, baseURL })(cleanModelId) : factory(cleanModelId);
|
|
1565
|
+
}
|
|
1316
1566
|
}
|
|
1317
1567
|
} catch {
|
|
1318
1568
|
baseModelInstance = {
|
|
@@ -1391,6 +1641,23 @@ var vibezcheck = Object.assign(createVibezModel, {
|
|
|
1391
1641
|
session: createVibezSession
|
|
1392
1642
|
});
|
|
1393
1643
|
|
|
1644
|
+
// src/ai-sdk/middleware.ts
|
|
1645
|
+
function vibezcheckMiddleware(options = {}) {
|
|
1646
|
+
return {
|
|
1647
|
+
specificationVersion: "v1",
|
|
1648
|
+
wrapGenerate: async ({ doGenerate, params, model }) => {
|
|
1649
|
+
const target = { ...model, doGenerate };
|
|
1650
|
+
const metered = withBilling(target, options);
|
|
1651
|
+
return metered.doGenerate(params);
|
|
1652
|
+
},
|
|
1653
|
+
wrapStream: async ({ doStream, params, model }) => {
|
|
1654
|
+
const target = { ...model, doStream };
|
|
1655
|
+
const metered = withBilling(target, options);
|
|
1656
|
+
return metered.doStream(params);
|
|
1657
|
+
}
|
|
1658
|
+
};
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1394
1661
|
// src/customers/manager.ts
|
|
1395
1662
|
var import_stripe2 = __toESM(require("stripe"));
|
|
1396
1663
|
|
|
@@ -1556,8 +1823,8 @@ var crypto = __toESM(require("crypto"));
|
|
|
1556
1823
|
var import_stripe3 = __toESM(require("stripe"));
|
|
1557
1824
|
var ApiKeyAuth = class {
|
|
1558
1825
|
stripe;
|
|
1559
|
-
constructor(
|
|
1560
|
-
this.stripe =
|
|
1826
|
+
constructor(stripe2) {
|
|
1827
|
+
this.stripe = stripe2;
|
|
1561
1828
|
}
|
|
1562
1829
|
/**
|
|
1563
1830
|
* Hashes a raw API key using SHA-256
|
|
@@ -1722,6 +1989,164 @@ function createBillingHelper(options = {}) {
|
|
|
1722
1989
|
return new BillingHelper(options);
|
|
1723
1990
|
}
|
|
1724
1991
|
|
|
1992
|
+
// src/billing/session.ts
|
|
1993
|
+
var AgentSession = class {
|
|
1994
|
+
sessionId;
|
|
1995
|
+
customer;
|
|
1996
|
+
maxCostUSD;
|
|
1997
|
+
totalCostUSD = 0;
|
|
1998
|
+
totalTokens = 0;
|
|
1999
|
+
toolCalls = [];
|
|
2000
|
+
options;
|
|
2001
|
+
constructor(options = {}) {
|
|
2002
|
+
this.sessionId = `vibez_sess_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
2003
|
+
this.customer = options.customer;
|
|
2004
|
+
this.maxCostUSD = options.sessionBudgetUSD ?? options.maxCostUSD ?? 1;
|
|
2005
|
+
this.options = options;
|
|
2006
|
+
}
|
|
2007
|
+
/**
|
|
2008
|
+
* Returns an AI model instance bound to this agent session with cumulative budget enforcement
|
|
2009
|
+
*/
|
|
2010
|
+
model(modelOrId, modelOptions = {}) {
|
|
2011
|
+
const session = this;
|
|
2012
|
+
return withBilling(modelOrId, {
|
|
2013
|
+
...modelOptions,
|
|
2014
|
+
customer: this.customer,
|
|
2015
|
+
pricing: modelOptions.pricing || this.options.pricing,
|
|
2016
|
+
billing: modelOptions.billing || this.options.billing,
|
|
2017
|
+
onUsage: async (event) => {
|
|
2018
|
+
const callCost = event.cost.retailUSD ?? event.cost.billedUSD ?? event.cost.totalUSD;
|
|
2019
|
+
session.totalCostUSD = Number((session.totalCostUSD + callCost).toFixed(8));
|
|
2020
|
+
session.totalTokens += event.usage.totalTokens;
|
|
2021
|
+
if (session.totalCostUSD > session.maxCostUSD) {
|
|
2022
|
+
throw new VibezCircuitBreakerError({
|
|
2023
|
+
reason: "cost_per_call_exceeded",
|
|
2024
|
+
limit: session.maxCostUSD,
|
|
2025
|
+
current: session.totalCostUSD,
|
|
2026
|
+
model: event.model,
|
|
2027
|
+
customerId: event.customerId,
|
|
2028
|
+
message: `[vibezcheck] Agent Session Budget exceeded: Cumulative cost ($${session.totalCostUSD.toFixed(4)}) exceeded limit ($${session.maxCostUSD.toFixed(4)}).`
|
|
2029
|
+
});
|
|
2030
|
+
}
|
|
2031
|
+
if (session.options.onUsage) {
|
|
2032
|
+
await session.options.onUsage(event);
|
|
2033
|
+
}
|
|
2034
|
+
}
|
|
2035
|
+
});
|
|
2036
|
+
}
|
|
2037
|
+
/**
|
|
2038
|
+
* Tracks an external tool invocation (e.g. web search, Python code sandbox, vector query, API call)
|
|
2039
|
+
*/
|
|
2040
|
+
async trackTool(toolName, costOrOptions, fn) {
|
|
2041
|
+
const costUSD = typeof costOrOptions === "number" ? costOrOptions : costOrOptions.costUSD;
|
|
2042
|
+
const latencyMs = typeof costOrOptions === "object" ? costOrOptions.latencyMs : void 0;
|
|
2043
|
+
const metadata = typeof costOrOptions === "object" ? costOrOptions.metadata : void 0;
|
|
2044
|
+
if (this.totalCostUSD + costUSD > this.maxCostUSD) {
|
|
2045
|
+
throw new VibezCircuitBreakerError({
|
|
2046
|
+
reason: "cost_per_call_exceeded",
|
|
2047
|
+
limit: this.maxCostUSD,
|
|
2048
|
+
current: this.totalCostUSD + costUSD,
|
|
2049
|
+
model: `tool:${toolName}`,
|
|
2050
|
+
message: `[vibezcheck] Agent Session Budget exceeded on tool '${toolName}': ($${(this.totalCostUSD + costUSD).toFixed(4)}) exceeded limit ($${this.maxCostUSD.toFixed(4)}).`
|
|
2051
|
+
});
|
|
2052
|
+
}
|
|
2053
|
+
let result = void 0;
|
|
2054
|
+
const start = performance.now();
|
|
2055
|
+
if (fn) {
|
|
2056
|
+
result = await fn();
|
|
2057
|
+
}
|
|
2058
|
+
const elapsed = latencyMs ?? Math.round(performance.now() - start);
|
|
2059
|
+
this.totalCostUSD = Number((this.totalCostUSD + costUSD).toFixed(8));
|
|
2060
|
+
this.toolCalls.push({
|
|
2061
|
+
name: toolName,
|
|
2062
|
+
costUSD,
|
|
2063
|
+
latencyMs: elapsed,
|
|
2064
|
+
metadata
|
|
2065
|
+
});
|
|
2066
|
+
return result;
|
|
2067
|
+
}
|
|
2068
|
+
getCurrentCostUSD() {
|
|
2069
|
+
return this.totalCostUSD;
|
|
2070
|
+
}
|
|
2071
|
+
getTotalTokens() {
|
|
2072
|
+
return this.totalTokens;
|
|
2073
|
+
}
|
|
2074
|
+
/**
|
|
2075
|
+
* Concludes session and returns final financial summary
|
|
2076
|
+
*/
|
|
2077
|
+
async conclude() {
|
|
2078
|
+
return this.getSummary();
|
|
2079
|
+
}
|
|
2080
|
+
/**
|
|
2081
|
+
* Returns current session summary
|
|
2082
|
+
*/
|
|
2083
|
+
getSummary() {
|
|
2084
|
+
return {
|
|
2085
|
+
sessionId: this.sessionId,
|
|
2086
|
+
customer: this.customer,
|
|
2087
|
+
totalCostUSD: this.totalCostUSD,
|
|
2088
|
+
totalTokens: this.totalTokens,
|
|
2089
|
+
toolCallCount: this.toolCalls.length,
|
|
2090
|
+
toolCalls: [...this.toolCalls]
|
|
2091
|
+
};
|
|
2092
|
+
}
|
|
2093
|
+
};
|
|
2094
|
+
function createAgentSession(options = {}) {
|
|
2095
|
+
return new AgentSession(options);
|
|
2096
|
+
}
|
|
2097
|
+
|
|
2098
|
+
// src/billing/tools.ts
|
|
2099
|
+
function wrapTool(options) {
|
|
2100
|
+
const { name, costUSD, tool, execute } = options;
|
|
2101
|
+
const targetFn = execute || tool?.execute;
|
|
2102
|
+
if (!targetFn && !tool) {
|
|
2103
|
+
return options;
|
|
2104
|
+
}
|
|
2105
|
+
const base = tool || {};
|
|
2106
|
+
return {
|
|
2107
|
+
...base,
|
|
2108
|
+
name,
|
|
2109
|
+
costUSD,
|
|
2110
|
+
execute: async (...args) => {
|
|
2111
|
+
const startTime = performance.now();
|
|
2112
|
+
try {
|
|
2113
|
+
const result = await targetFn(...args);
|
|
2114
|
+
const latencyMs = Math.round(performance.now() - startTime);
|
|
2115
|
+
if (typeof globalThis.__vibezCurrentSession?.trackTool === "function") {
|
|
2116
|
+
await globalThis.__vibezCurrentSession.trackTool(name, {
|
|
2117
|
+
costUSD,
|
|
2118
|
+
latencyMs,
|
|
2119
|
+
metadata: { args: args[0] }
|
|
2120
|
+
});
|
|
2121
|
+
}
|
|
2122
|
+
return result;
|
|
2123
|
+
} catch (error) {
|
|
2124
|
+
const latencyMs = Math.round(performance.now() - startTime);
|
|
2125
|
+
if (typeof globalThis.__vibezCurrentSession?.trackTool === "function") {
|
|
2126
|
+
await globalThis.__vibezCurrentSession.trackTool(name, {
|
|
2127
|
+
costUSD,
|
|
2128
|
+
latencyMs,
|
|
2129
|
+
metadata: { error: String(error) }
|
|
2130
|
+
});
|
|
2131
|
+
}
|
|
2132
|
+
throw error;
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
};
|
|
2136
|
+
}
|
|
2137
|
+
function instrumentToolKit(tools, options = {}) {
|
|
2138
|
+
const instrumented = {};
|
|
2139
|
+
const cost = options.costPerActionUSD ?? 5e-3;
|
|
2140
|
+
for (const [toolName, toolDef] of Object.entries(tools)) {
|
|
2141
|
+
instrumented[toolName] = wrapTool({
|
|
2142
|
+
name: toolName,
|
|
2143
|
+
costUSD: cost,
|
|
2144
|
+
tool: toolDef
|
|
2145
|
+
});
|
|
2146
|
+
}
|
|
2147
|
+
return instrumented;
|
|
2148
|
+
}
|
|
2149
|
+
|
|
1725
2150
|
// src/meter/index.ts
|
|
1726
2151
|
var defaultMeter = createMeter();
|
|
1727
2152
|
function wrapStream(stream, options) {
|
|
@@ -1731,6 +2156,299 @@ function trackTokens(response, options) {
|
|
|
1731
2156
|
return defaultMeter.trackUsage(response, options);
|
|
1732
2157
|
}
|
|
1733
2158
|
|
|
2159
|
+
// src/compat/token-meter.ts
|
|
2160
|
+
function createTokenMeter(stripeApiKey, config = {}) {
|
|
2161
|
+
const apiKey = stripeApiKey || (typeof process !== "undefined" ? process.env?.STRIPE_SECRET_KEY || process.env?.STRIPE_API_KEY : void 0);
|
|
2162
|
+
const meter = new VibezMeter({
|
|
2163
|
+
apiKey,
|
|
2164
|
+
eventName: config.eventName
|
|
2165
|
+
});
|
|
2166
|
+
const resolveCustomer = (cust) => {
|
|
2167
|
+
if (typeof cust === "string") return cust;
|
|
2168
|
+
return cust.customerId || cust.customer || "unknown-customer";
|
|
2169
|
+
};
|
|
2170
|
+
return {
|
|
2171
|
+
trackUsage(response, customer) {
|
|
2172
|
+
if (!response) return;
|
|
2173
|
+
const customerId = resolveCustomer(customer);
|
|
2174
|
+
meter.trackUsage(response, { customer: customerId });
|
|
2175
|
+
},
|
|
2176
|
+
track(response, customer) {
|
|
2177
|
+
if (!response) return;
|
|
2178
|
+
const customerId = resolveCustomer(customer);
|
|
2179
|
+
meter.trackUsage(response, { customer: customerId });
|
|
2180
|
+
},
|
|
2181
|
+
trackUsageStreamOpenAI(stream, customer) {
|
|
2182
|
+
if (!stream || typeof stream[Symbol.asyncIterator] !== "function") {
|
|
2183
|
+
return stream;
|
|
2184
|
+
}
|
|
2185
|
+
const stripeCustomerId = resolveCustomer(customer);
|
|
2186
|
+
const originalIterator = stream[Symbol.asyncIterator].bind(stream);
|
|
2187
|
+
let capturedUsage = null;
|
|
2188
|
+
let capturedModel = "gpt-4o";
|
|
2189
|
+
const wrappedAsyncGenerator = async function* () {
|
|
2190
|
+
try {
|
|
2191
|
+
for await (const chunk of originalIterator()) {
|
|
2192
|
+
if (chunk.model) {
|
|
2193
|
+
capturedModel = chunk.model;
|
|
2194
|
+
}
|
|
2195
|
+
if (chunk.usage || chunk.token_usage) {
|
|
2196
|
+
capturedUsage = chunk.usage || chunk.token_usage;
|
|
2197
|
+
}
|
|
2198
|
+
yield chunk;
|
|
2199
|
+
}
|
|
2200
|
+
} finally {
|
|
2201
|
+
if (capturedUsage) {
|
|
2202
|
+
meter.recordUsage({
|
|
2203
|
+
model: capturedModel,
|
|
2204
|
+
provider: "openai",
|
|
2205
|
+
inputTokens: capturedUsage.prompt_tokens ?? capturedUsage.inputTokens ?? 0,
|
|
2206
|
+
outputTokens: capturedUsage.completion_tokens ?? capturedUsage.outputTokens ?? 0,
|
|
2207
|
+
reasoningTokens: capturedUsage.completion_tokens_details?.reasoning_tokens ?? capturedUsage.output_token_details?.reasoning_tokens ?? 0,
|
|
2208
|
+
cachedTokens: capturedUsage.prompt_tokens_details?.cached_tokens ?? capturedUsage.input_token_details?.cached_tokens ?? 0,
|
|
2209
|
+
customer: stripeCustomerId
|
|
2210
|
+
});
|
|
2211
|
+
}
|
|
2212
|
+
}
|
|
2213
|
+
};
|
|
2214
|
+
return wrappedAsyncGenerator();
|
|
2215
|
+
},
|
|
2216
|
+
trackUsageStreamAnthropic(stream, customer) {
|
|
2217
|
+
if (!stream || typeof stream[Symbol.asyncIterator] !== "function") {
|
|
2218
|
+
return stream;
|
|
2219
|
+
}
|
|
2220
|
+
const stripeCustomerId = resolveCustomer(customer);
|
|
2221
|
+
const originalIterator = stream[Symbol.asyncIterator].bind(stream);
|
|
2222
|
+
let capturedUsage = null;
|
|
2223
|
+
let capturedModel = "claude-3-5-sonnet";
|
|
2224
|
+
const wrappedAsyncGenerator = async function* () {
|
|
2225
|
+
try {
|
|
2226
|
+
for await (const chunk of originalIterator()) {
|
|
2227
|
+
if (chunk.type === "message_start" && chunk.message?.model) {
|
|
2228
|
+
capturedModel = chunk.message.model;
|
|
2229
|
+
if (chunk.message.usage) {
|
|
2230
|
+
capturedUsage = { ...capturedUsage, ...chunk.message.usage };
|
|
2231
|
+
}
|
|
2232
|
+
} else if (chunk.type === "message_delta" && chunk.usage) {
|
|
2233
|
+
capturedUsage = { ...capturedUsage, ...chunk.usage };
|
|
2234
|
+
}
|
|
2235
|
+
yield chunk;
|
|
2236
|
+
}
|
|
2237
|
+
} finally {
|
|
2238
|
+
if (capturedUsage) {
|
|
2239
|
+
meter.recordUsage({
|
|
2240
|
+
model: capturedModel,
|
|
2241
|
+
provider: "anthropic",
|
|
2242
|
+
inputTokens: capturedUsage.input_tokens ?? 0,
|
|
2243
|
+
outputTokens: capturedUsage.output_tokens ?? 0,
|
|
2244
|
+
cachedTokens: capturedUsage.cache_read_input_tokens ?? 0,
|
|
2245
|
+
customer: stripeCustomerId
|
|
2246
|
+
});
|
|
2247
|
+
}
|
|
2248
|
+
}
|
|
2249
|
+
};
|
|
2250
|
+
return wrappedAsyncGenerator();
|
|
2251
|
+
},
|
|
2252
|
+
trackUsageStreamGemini(streamResult, customer, modelName = "gemini-2.0-flash") {
|
|
2253
|
+
const stripeCustomerId = resolveCustomer(customer);
|
|
2254
|
+
const originalStream = streamResult?.stream || streamResult;
|
|
2255
|
+
if (!originalStream || typeof originalStream[Symbol.asyncIterator] !== "function") {
|
|
2256
|
+
return streamResult;
|
|
2257
|
+
}
|
|
2258
|
+
const wrappedAsyncGenerator = async function* () {
|
|
2259
|
+
let lastUsageMetadata = null;
|
|
2260
|
+
try {
|
|
2261
|
+
for await (const chunk of originalStream) {
|
|
2262
|
+
if (chunk.usageMetadata) {
|
|
2263
|
+
lastUsageMetadata = chunk.usageMetadata;
|
|
2264
|
+
}
|
|
2265
|
+
yield chunk;
|
|
2266
|
+
}
|
|
2267
|
+
} finally {
|
|
2268
|
+
if (lastUsageMetadata) {
|
|
2269
|
+
meter.recordUsage({
|
|
2270
|
+
model: modelName,
|
|
2271
|
+
provider: "google",
|
|
2272
|
+
inputTokens: lastUsageMetadata.promptTokenCount ?? 0,
|
|
2273
|
+
outputTokens: (lastUsageMetadata.candidatesTokenCount ?? 0) + (lastUsageMetadata.thoughtsTokenCount ?? 0),
|
|
2274
|
+
reasoningTokens: lastUsageMetadata.thoughtsTokenCount ?? 0,
|
|
2275
|
+
customer: stripeCustomerId
|
|
2276
|
+
});
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2279
|
+
};
|
|
2280
|
+
if (streamResult && streamResult.stream) {
|
|
2281
|
+
return {
|
|
2282
|
+
...streamResult,
|
|
2283
|
+
stream: wrappedAsyncGenerator()
|
|
2284
|
+
};
|
|
2285
|
+
}
|
|
2286
|
+
return wrappedAsyncGenerator();
|
|
2287
|
+
},
|
|
2288
|
+
trackUsageStreamDeepSeek(stream, customer, modelName = "deepseek-chat") {
|
|
2289
|
+
const stripeCustomerId = resolveCustomer(customer);
|
|
2290
|
+
if (!stream || typeof stream[Symbol.asyncIterator] !== "function") {
|
|
2291
|
+
return stream;
|
|
2292
|
+
}
|
|
2293
|
+
const originalIterator = stream[Symbol.asyncIterator].bind(stream);
|
|
2294
|
+
let capturedUsage = null;
|
|
2295
|
+
let capturedModel = modelName;
|
|
2296
|
+
const wrappedAsyncGenerator = async function* () {
|
|
2297
|
+
try {
|
|
2298
|
+
for await (const chunk of originalIterator()) {
|
|
2299
|
+
if (chunk.model) capturedModel = chunk.model;
|
|
2300
|
+
if (chunk.usage) capturedUsage = chunk.usage;
|
|
2301
|
+
yield chunk;
|
|
2302
|
+
}
|
|
2303
|
+
} finally {
|
|
2304
|
+
if (capturedUsage) {
|
|
2305
|
+
meter.recordUsage({
|
|
2306
|
+
model: capturedModel,
|
|
2307
|
+
provider: "deepseek",
|
|
2308
|
+
inputTokens: capturedUsage.prompt_tokens ?? capturedUsage.inputTokens ?? 0,
|
|
2309
|
+
outputTokens: capturedUsage.completion_tokens ?? capturedUsage.outputTokens ?? 0,
|
|
2310
|
+
reasoningTokens: capturedUsage.completion_tokens_details?.reasoning_tokens ?? 0,
|
|
2311
|
+
cachedTokens: capturedUsage.prompt_cache_hit_tokens ?? capturedUsage.prompt_tokens_details?.cached_tokens ?? 0,
|
|
2312
|
+
customer: stripeCustomerId
|
|
2313
|
+
});
|
|
2314
|
+
}
|
|
2315
|
+
}
|
|
2316
|
+
};
|
|
2317
|
+
return wrappedAsyncGenerator();
|
|
2318
|
+
},
|
|
2319
|
+
trackUsageStreamGroq(stream, customer, modelName = "llama-3.3-70b-versatile") {
|
|
2320
|
+
const stripeCustomerId = resolveCustomer(customer);
|
|
2321
|
+
if (!stream || typeof stream[Symbol.asyncIterator] !== "function") {
|
|
2322
|
+
return stream;
|
|
2323
|
+
}
|
|
2324
|
+
const originalIterator = stream[Symbol.asyncIterator].bind(stream);
|
|
2325
|
+
let capturedUsage = null;
|
|
2326
|
+
let capturedModel = modelName;
|
|
2327
|
+
const wrappedAsyncGenerator = async function* () {
|
|
2328
|
+
try {
|
|
2329
|
+
for await (const chunk of originalIterator()) {
|
|
2330
|
+
if (chunk.model) capturedModel = chunk.model;
|
|
2331
|
+
if (chunk.usage || chunk.x_groq?.usage) {
|
|
2332
|
+
capturedUsage = chunk.usage || chunk.x_groq?.usage;
|
|
2333
|
+
}
|
|
2334
|
+
yield chunk;
|
|
2335
|
+
}
|
|
2336
|
+
} finally {
|
|
2337
|
+
if (capturedUsage) {
|
|
2338
|
+
meter.recordUsage({
|
|
2339
|
+
model: capturedModel,
|
|
2340
|
+
provider: "groq",
|
|
2341
|
+
inputTokens: capturedUsage.prompt_tokens ?? 0,
|
|
2342
|
+
outputTokens: capturedUsage.completion_tokens ?? 0,
|
|
2343
|
+
customer: stripeCustomerId
|
|
2344
|
+
});
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
};
|
|
2348
|
+
return wrappedAsyncGenerator();
|
|
2349
|
+
},
|
|
2350
|
+
trackUsageStreamMistral(stream, customer, modelName = "mistral-large-latest") {
|
|
2351
|
+
const stripeCustomerId = resolveCustomer(customer);
|
|
2352
|
+
if (!stream || typeof stream[Symbol.asyncIterator] !== "function") {
|
|
2353
|
+
return stream;
|
|
2354
|
+
}
|
|
2355
|
+
const originalIterator = stream[Symbol.asyncIterator].bind(stream);
|
|
2356
|
+
let capturedUsage = null;
|
|
2357
|
+
let capturedModel = modelName;
|
|
2358
|
+
const wrappedAsyncGenerator = async function* () {
|
|
2359
|
+
try {
|
|
2360
|
+
for await (const chunk of originalIterator()) {
|
|
2361
|
+
if (chunk.model) capturedModel = chunk.model;
|
|
2362
|
+
if (chunk.usage) capturedUsage = chunk.usage;
|
|
2363
|
+
yield chunk;
|
|
2364
|
+
}
|
|
2365
|
+
} finally {
|
|
2366
|
+
if (capturedUsage) {
|
|
2367
|
+
meter.recordUsage({
|
|
2368
|
+
model: capturedModel,
|
|
2369
|
+
provider: "mistral",
|
|
2370
|
+
inputTokens: capturedUsage.prompt_tokens ?? 0,
|
|
2371
|
+
outputTokens: capturedUsage.completion_tokens ?? 0,
|
|
2372
|
+
customer: stripeCustomerId
|
|
2373
|
+
});
|
|
2374
|
+
}
|
|
2375
|
+
}
|
|
2376
|
+
};
|
|
2377
|
+
return wrappedAsyncGenerator();
|
|
2378
|
+
},
|
|
2379
|
+
trackUsageStream(stream, customer, options = {}) {
|
|
2380
|
+
const stripeCustomerId = resolveCustomer(customer);
|
|
2381
|
+
return meter.wrapStream(stream, {
|
|
2382
|
+
customer: stripeCustomerId,
|
|
2383
|
+
model: options.model,
|
|
2384
|
+
provider: options.provider
|
|
2385
|
+
});
|
|
2386
|
+
}
|
|
2387
|
+
};
|
|
2388
|
+
}
|
|
2389
|
+
|
|
2390
|
+
// src/compat/stripe-provider.ts
|
|
2391
|
+
function createStripe(config = {}) {
|
|
2392
|
+
const apiKey = config.apiKey || (typeof process !== "undefined" ? process.env?.STRIPE_API_KEY || process.env?.STRIPE_SECRET_KEY : void 0);
|
|
2393
|
+
const provider = function(modelId, settings = {}) {
|
|
2394
|
+
const customerId = settings.customerId || config.customerId || "anonymous";
|
|
2395
|
+
const options = {
|
|
2396
|
+
customer: customerId,
|
|
2397
|
+
stripeApiKey: apiKey
|
|
2398
|
+
};
|
|
2399
|
+
if (settings.model && typeof settings.model === "object") {
|
|
2400
|
+
return withBilling(settings.model, options);
|
|
2401
|
+
}
|
|
2402
|
+
return createVibezModel(modelId, {
|
|
2403
|
+
...settings,
|
|
2404
|
+
customer: customerId,
|
|
2405
|
+
stripeApiKey: apiKey,
|
|
2406
|
+
baseURL: config.baseURL
|
|
2407
|
+
});
|
|
2408
|
+
};
|
|
2409
|
+
provider.languageModel = provider;
|
|
2410
|
+
provider.specificationVersion = "v3";
|
|
2411
|
+
return provider;
|
|
2412
|
+
}
|
|
2413
|
+
var stripe = createStripe();
|
|
2414
|
+
var createStripeV3 = createStripe;
|
|
2415
|
+
var stripeV3 = stripe;
|
|
2416
|
+
|
|
2417
|
+
// src/compat/stripe-meter.ts
|
|
2418
|
+
function meteredModel2(model, arg2, arg3, arg4) {
|
|
2419
|
+
if (!model || typeof model !== "object") {
|
|
2420
|
+
throw new Error("[vibezcheck / Stripe AI] Invalid model provided to meteredModel().");
|
|
2421
|
+
}
|
|
2422
|
+
let stripeApiKey;
|
|
2423
|
+
let stripeCustomerId;
|
|
2424
|
+
let options = {};
|
|
2425
|
+
if (typeof arg2 === "object" && arg2 !== null) {
|
|
2426
|
+
options = { ...arg2 };
|
|
2427
|
+
stripeCustomerId = options.customerId || (typeof options.customer === "string" ? options.customer : options.customer?.id);
|
|
2428
|
+
stripeApiKey = options.stripeApiKey;
|
|
2429
|
+
} else if (typeof arg2 === "string") {
|
|
2430
|
+
if (typeof arg3 === "string") {
|
|
2431
|
+
stripeApiKey = arg2;
|
|
2432
|
+
stripeCustomerId = arg3;
|
|
2433
|
+
options = { ...arg4 || {} };
|
|
2434
|
+
} else {
|
|
2435
|
+
stripeCustomerId = arg2;
|
|
2436
|
+
options = { ...typeof arg3 === "object" ? arg3 : {} };
|
|
2437
|
+
}
|
|
2438
|
+
}
|
|
2439
|
+
stripeApiKey = stripeApiKey || options.stripeApiKey || (typeof process !== "undefined" ? process.env?.STRIPE_API_KEY || process.env?.STRIPE_SECRET_KEY : void 0);
|
|
2440
|
+
if (typeof process !== "undefined" && stripeApiKey && !process.env.STRIPE_API_KEY && !process.env.STRIPE_SECRET_KEY) {
|
|
2441
|
+
process.env.STRIPE_API_KEY = stripeApiKey;
|
|
2442
|
+
}
|
|
2443
|
+
const mergedOptions = {
|
|
2444
|
+
...options,
|
|
2445
|
+
customer: stripeCustomerId || options.customer,
|
|
2446
|
+
customerId: stripeCustomerId,
|
|
2447
|
+
stripeApiKey
|
|
2448
|
+
};
|
|
2449
|
+
return withBilling(model, mergedOptions);
|
|
2450
|
+
}
|
|
2451
|
+
|
|
1734
2452
|
// src/index.ts
|
|
1735
2453
|
var VibezCheckClient = class {
|
|
1736
2454
|
meter;
|
|
@@ -1806,22 +2524,32 @@ function vibezcheck2(firstArg, secondArg) {
|
|
|
1806
2524
|
return new VibezCheckClient(firstArg || {});
|
|
1807
2525
|
}
|
|
1808
2526
|
vibezcheck2.calculateCost = calculateCost;
|
|
2527
|
+
vibezcheck2.calculateUsageCost = calculateUsageCost;
|
|
1809
2528
|
vibezcheck2.getModelPricing = getModelPricing;
|
|
1810
2529
|
vibezcheck2.registerModelPricing = registerModelPricing;
|
|
1811
2530
|
vibezcheck2.create = createVibezCheck;
|
|
1812
2531
|
vibezcheck2.withBilling = withBilling;
|
|
1813
2532
|
vibezcheck2.createMeter = createMeter;
|
|
1814
|
-
vibezcheck2.session =
|
|
2533
|
+
vibezcheck2.session = createAgentSession;
|
|
2534
|
+
vibezcheck2.Session = AgentSession;
|
|
2535
|
+
vibezcheck2.wrapTool = wrapTool;
|
|
2536
|
+
vibezcheck2.instrumentToolKit = instrumentToolKit;
|
|
2537
|
+
vibezcheck2.middleware = vibezcheckMiddleware;
|
|
2538
|
+
vibezcheck2.Billing = BillingHelper;
|
|
2539
|
+
vibezcheck2.Auth = ApiKeyAuth;
|
|
2540
|
+
vibezcheck2.Customers = CustomerManager;
|
|
1815
2541
|
var vibez = new VibezCheckClient();
|
|
1816
2542
|
var vibescheck = vibezcheck2;
|
|
1817
2543
|
var vibes = vibez;
|
|
1818
2544
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1819
2545
|
0 && (module.exports = {
|
|
2546
|
+
AgentSession,
|
|
1820
2547
|
AnthropicStreamAccumulator,
|
|
1821
2548
|
ApiKeyAuth,
|
|
1822
2549
|
BillingHelper,
|
|
1823
2550
|
CustomerCache,
|
|
1824
2551
|
CustomerManager,
|
|
2552
|
+
MODEL_ALIASES,
|
|
1825
2553
|
MODEL_PRICING_TABLE,
|
|
1826
2554
|
MeterBatcher,
|
|
1827
2555
|
VibezCheckClient,
|
|
@@ -1829,10 +2557,14 @@ var vibes = vibez;
|
|
|
1829
2557
|
VibezMeter,
|
|
1830
2558
|
calculateCost,
|
|
1831
2559
|
calculateUsageCost,
|
|
2560
|
+
createAgentSession,
|
|
1832
2561
|
createApiKeyAuth,
|
|
1833
2562
|
createBillingHelper,
|
|
1834
2563
|
createCustomerManager,
|
|
1835
2564
|
createMeter,
|
|
2565
|
+
createStripe,
|
|
2566
|
+
createStripeV3,
|
|
2567
|
+
createTokenMeter,
|
|
1836
2568
|
createVibezCheck,
|
|
1837
2569
|
createVibezModel,
|
|
1838
2570
|
createVibezSession,
|
|
@@ -1844,20 +2576,26 @@ var vibes = vibez;
|
|
|
1844
2576
|
extractOpenAIResponseUsage,
|
|
1845
2577
|
getModelPricing,
|
|
1846
2578
|
inspectOpenAIStreamChunk,
|
|
2579
|
+
instrumentToolKit,
|
|
1847
2580
|
meteredModel,
|
|
1848
2581
|
normalizeCustomer,
|
|
1849
2582
|
normalizeModelKey,
|
|
1850
2583
|
registerModelPricing,
|
|
2584
|
+
stripe,
|
|
2585
|
+
stripeMeteredModel,
|
|
2586
|
+
stripeV3,
|
|
1851
2587
|
trackTokens,
|
|
1852
2588
|
vibes,
|
|
1853
2589
|
vibescheck,
|
|
1854
2590
|
vibez,
|
|
1855
2591
|
vibezcheck,
|
|
2592
|
+
vibezcheckMiddleware,
|
|
1856
2593
|
withBilling,
|
|
1857
2594
|
wrapAnthropicStream,
|
|
1858
2595
|
wrapGeminiStream,
|
|
1859
2596
|
wrapOpenAIStream,
|
|
1860
2597
|
wrapStream,
|
|
2598
|
+
wrapTool,
|
|
1861
2599
|
wrapUniversalStream
|
|
1862
2600
|
});
|
|
1863
2601
|
//# sourceMappingURL=index.js.map
|