vibezcheck 0.5.3 → 0.5.5
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/bin/examples.js +8 -0
- package/dist/ai-sdk/index.d.mts +4 -4
- package/dist/ai-sdk/index.d.ts +4 -4
- package/dist/ai-sdk/index.js +151 -26
- package/dist/ai-sdk/index.js.map +1 -1
- package/dist/ai-sdk/index.mjs +151 -26
- package/dist/ai-sdk/index.mjs.map +1 -1
- package/dist/ai-sdk/middleware.d.mts +3 -3
- package/dist/ai-sdk/middleware.d.ts +3 -3
- package/dist/ai-sdk/middleware.js +146 -25
- package/dist/ai-sdk/middleware.js.map +1 -1
- package/dist/ai-sdk/middleware.mjs +146 -25
- package/dist/ai-sdk/middleware.mjs.map +1 -1
- package/dist/billing/index.d.mts +3 -3
- package/dist/billing/index.d.ts +3 -3
- package/dist/billing/index.js +146 -25
- package/dist/billing/index.js.map +1 -1
- package/dist/billing/index.mjs +146 -25
- package/dist/billing/index.mjs.map +1 -1
- package/dist/cli/index.js +272 -43
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/index.mjs +296 -60
- package/dist/cli/index.mjs.map +1 -1
- package/dist/{client-CbuXmoXz.d.mts → client-BzwEHQlC.d.mts} +1 -1
- package/dist/{client-BORmJy8s.d.ts → client-CbO6vLUe.d.ts} +1 -1
- package/dist/compat/stripe-meter.d.mts +3 -3
- package/dist/compat/stripe-meter.d.ts +3 -3
- package/dist/compat/stripe-meter.js +146 -25
- package/dist/compat/stripe-meter.js.map +1 -1
- package/dist/compat/stripe-meter.mjs +146 -25
- package/dist/compat/stripe-meter.mjs.map +1 -1
- package/dist/compat/stripe-provider.js +151 -26
- package/dist/compat/stripe-provider.js.map +1 -1
- package/dist/compat/stripe-provider.mjs +151 -26
- package/dist/compat/stripe-provider.mjs.map +1 -1
- package/dist/compat/token-meter.js +97 -12
- package/dist/compat/token-meter.js.map +1 -1
- package/dist/compat/token-meter.mjs +97 -12
- package/dist/compat/token-meter.mjs.map +1 -1
- package/dist/customers/index.d.mts +1 -1
- package/dist/customers/index.d.ts +1 -1
- package/dist/index.d.mts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.js +151 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +151 -26
- 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 +97 -12
- package/dist/meter/index.js.map +1 -1
- package/dist/meter/index.mjs +97 -12
- package/dist/meter/index.mjs.map +1 -1
- package/dist/pricing/index.d.mts +1 -1
- package/dist/pricing/index.d.ts +1 -1
- package/dist/pricing/index.js +34 -1
- package/dist/pricing/index.js.map +1 -1
- package/dist/pricing/index.mjs +34 -1
- package/dist/pricing/index.mjs.map +1 -1
- package/dist/react/index.d.mts +6 -4
- package/dist/react/index.d.ts +6 -4
- package/dist/react/index.js +315 -193
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +315 -193
- package/dist/react/index.mjs.map +1 -1
- package/dist/{types-6P71CXAD.d.mts → types-Btf2OhIr.d.mts} +6 -0
- package/dist/{types-6P71CXAD.d.ts → types-Btf2OhIr.d.ts} +6 -0
- package/dist/{with-billing-DndWGxL8.d.mts → with-billing-BEqiJtCR.d.mts} +2 -2
- package/dist/{with-billing-Bj6Ya_Iy.d.ts → with-billing-D7QRoNHM.d.ts} +2 -2
- package/examples/01-local-cost-meter.ts +63 -0
- package/examples/02-vercel-ai-sdk.ts +43 -0
- package/examples/03-native-openai-stream.ts +40 -0
- package/examples/04-claude-thinking.ts +41 -0
- package/examples/05-reasoning-agent-stream.ts +54 -0
- package/examples/nextjs-app-router-openai/.env.example +5 -0
- package/examples/nextjs-app-router-openai/README.md +29 -0
- package/examples/nextjs-app-router-openai/app/api/chat/route.ts +22 -0
- package/examples/nextjs-app-router-openai/app/globals.css +21 -0
- package/examples/nextjs-app-router-openai/app/layout.tsx +21 -0
- package/examples/nextjs-app-router-openai/app/page.tsx +43 -0
- package/examples/nextjs-app-router-openai/next.config.mjs +7 -0
- package/examples/nextjs-app-router-openai/package.json +31 -0
- package/examples/nextjs-app-router-openai/postcss.config.mjs +9 -0
- package/examples/nextjs-app-router-openai/tailwind.config.ts +13 -0
- package/examples/nextjs-app-router-openai/tsconfig.json +27 -0
- package/examples/nextjs-saas-starter/README.md +29 -0
- package/examples/nextjs-saas-starter/app/api/chat/route.ts +65 -0
- package/examples/nextjs-saas-starter/app/api/checkout/route.ts +51 -0
- package/examples/nextjs-saas-starter/app/globals.css +14 -0
- package/examples/nextjs-saas-starter/app/layout.tsx +24 -0
- package/examples/nextjs-saas-starter/app/page.tsx +326 -0
- package/examples/nextjs-saas-starter/next.config.mjs +7 -0
- package/examples/nextjs-saas-starter/package.json +34 -0
- package/examples/nextjs-saas-starter/postcss.config.mjs +9 -0
- package/examples/nextjs-saas-starter/tailwind.config.ts +20 -0
- package/examples/nextjs-saas-starter/tsconfig.json +27 -0
- package/package.json +5 -3
package/bin/examples.js
ADDED
package/dist/ai-sdk/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { W as WithBillingOptions } from '../with-billing-
|
|
2
|
-
export { m as meteredModel, w as withBilling } from '../with-billing-
|
|
3
|
-
import { C as CustomerParam } from '../types-
|
|
4
|
-
import { V as VibezMeter } from '../client-
|
|
1
|
+
import { W as WithBillingOptions } from '../with-billing-BEqiJtCR.mjs';
|
|
2
|
+
export { m as meteredModel, w as withBilling } from '../with-billing-BEqiJtCR.mjs';
|
|
3
|
+
import { C as CustomerParam } from '../types-Btf2OhIr.mjs';
|
|
4
|
+
import { V as VibezMeter } from '../client-BzwEHQlC.mjs';
|
|
5
5
|
import 'stripe';
|
|
6
6
|
|
|
7
7
|
interface VibezCheckModelOptions extends WithBillingOptions {
|
package/dist/ai-sdk/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { W as WithBillingOptions } from '../with-billing-
|
|
2
|
-
export { m as meteredModel, w as withBilling } from '../with-billing-
|
|
3
|
-
import { C as CustomerParam } from '../types-
|
|
4
|
-
import { V as VibezMeter } from '../client-
|
|
1
|
+
import { W as WithBillingOptions } from '../with-billing-D7QRoNHM.js';
|
|
2
|
+
export { m as meteredModel, w as withBilling } from '../with-billing-D7QRoNHM.js';
|
|
3
|
+
import { C as CustomerParam } from '../types-Btf2OhIr.js';
|
|
4
|
+
import { V as VibezMeter } from '../client-CbO6vLUe.js';
|
|
5
5
|
import 'stripe';
|
|
6
6
|
|
|
7
7
|
interface VibezCheckModelOptions extends WithBillingOptions {
|
package/dist/ai-sdk/index.js
CHANGED
|
@@ -309,12 +309,38 @@ function extractOpenAIResponseUsage(response) {
|
|
|
309
309
|
function inspectOpenAIStreamChunk(chunk) {
|
|
310
310
|
if (!chunk || typeof chunk !== "object") return {};
|
|
311
311
|
const model = chunk.model;
|
|
312
|
-
const rawUsage = chunk.usage || chunk.token_usage || chunk.x_groq?.usage || chunk.usageMetadata;
|
|
312
|
+
const rawUsage = (chunk.type === "finish" ? chunk.usage : null) || chunk.usage || chunk.token_usage || chunk.x_groq?.usage || chunk.usageMetadata;
|
|
313
313
|
if (rawUsage) {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
314
|
+
let inputTokens = 0;
|
|
315
|
+
let outputTokens = 0;
|
|
316
|
+
let reasoningTokens = 0;
|
|
317
|
+
let cachedTokens = 0;
|
|
318
|
+
if (typeof rawUsage.inputTokens === "object" && rawUsage.inputTokens !== null) {
|
|
319
|
+
inputTokens = rawUsage.inputTokens.total ?? rawUsage.inputTokens.noCache ?? 0;
|
|
320
|
+
cachedTokens = rawUsage.inputTokens.cacheRead ?? 0;
|
|
321
|
+
} else if (typeof rawUsage.promptTokens === "number") {
|
|
322
|
+
inputTokens = rawUsage.promptTokens;
|
|
323
|
+
} else if (typeof rawUsage.inputTokens === "number") {
|
|
324
|
+
inputTokens = rawUsage.inputTokens;
|
|
325
|
+
} else {
|
|
326
|
+
inputTokens = rawUsage.prompt_tokens ?? rawUsage.input_tokens ?? rawUsage.promptTokenCount ?? 0;
|
|
327
|
+
}
|
|
328
|
+
if (typeof rawUsage.outputTokens === "object" && rawUsage.outputTokens !== null) {
|
|
329
|
+
outputTokens = rawUsage.outputTokens.total ?? rawUsage.outputTokens.text ?? 0;
|
|
330
|
+
reasoningTokens = rawUsage.outputTokens.reasoning ?? 0;
|
|
331
|
+
} else if (typeof rawUsage.completionTokens === "number") {
|
|
332
|
+
outputTokens = rawUsage.completionTokens;
|
|
333
|
+
} else if (typeof rawUsage.outputTokens === "number") {
|
|
334
|
+
outputTokens = rawUsage.outputTokens;
|
|
335
|
+
} else {
|
|
336
|
+
outputTokens = rawUsage.completion_tokens ?? rawUsage.output_tokens ?? rawUsage.candidatesTokenCount ?? 0;
|
|
337
|
+
}
|
|
338
|
+
if (!reasoningTokens) {
|
|
339
|
+
reasoningTokens = rawUsage.completion_tokens_details?.reasoning_tokens ?? rawUsage.output_token_details?.reasoning_tokens ?? rawUsage.reasoning_tokens ?? rawUsage.reasoningTokens ?? rawUsage.thoughtsTokenCount ?? 0;
|
|
340
|
+
}
|
|
341
|
+
if (!cachedTokens) {
|
|
342
|
+
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;
|
|
343
|
+
}
|
|
318
344
|
return {
|
|
319
345
|
model,
|
|
320
346
|
usage: {
|
|
@@ -460,10 +486,36 @@ function extractGenericResponseUsage(response, fallbackModel = "generic-llm", fa
|
|
|
460
486
|
if (!response || typeof response !== "object") return null;
|
|
461
487
|
const usage = response.usage || response.token_usage || response.usageMetadata;
|
|
462
488
|
if (usage) {
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
489
|
+
let inputTokens = 0;
|
|
490
|
+
let outputTokens = 0;
|
|
491
|
+
let reasoningTokens = 0;
|
|
492
|
+
let cachedTokens = 0;
|
|
493
|
+
if (typeof usage.inputTokens === "object" && usage.inputTokens !== null) {
|
|
494
|
+
inputTokens = usage.inputTokens.total ?? usage.inputTokens.noCache ?? 0;
|
|
495
|
+
cachedTokens = usage.inputTokens.cacheRead ?? 0;
|
|
496
|
+
} else if (typeof usage.promptTokens === "number") {
|
|
497
|
+
inputTokens = usage.promptTokens;
|
|
498
|
+
} else if (typeof usage.inputTokens === "number") {
|
|
499
|
+
inputTokens = usage.inputTokens;
|
|
500
|
+
} else {
|
|
501
|
+
inputTokens = usage.prompt_tokens ?? usage.input_tokens ?? usage.promptTokenCount ?? usage.prompt_eval_count ?? 0;
|
|
502
|
+
}
|
|
503
|
+
if (typeof usage.outputTokens === "object" && usage.outputTokens !== null) {
|
|
504
|
+
outputTokens = usage.outputTokens.total ?? usage.outputTokens.text ?? 0;
|
|
505
|
+
reasoningTokens = usage.outputTokens.reasoning ?? 0;
|
|
506
|
+
} else if (typeof usage.completionTokens === "number") {
|
|
507
|
+
outputTokens = usage.completionTokens;
|
|
508
|
+
} else if (typeof usage.outputTokens === "number") {
|
|
509
|
+
outputTokens = usage.outputTokens;
|
|
510
|
+
} else {
|
|
511
|
+
outputTokens = usage.completion_tokens ?? usage.output_tokens ?? usage.candidatesTokenCount ?? usage.eval_count ?? 0;
|
|
512
|
+
}
|
|
513
|
+
if (!reasoningTokens) {
|
|
514
|
+
reasoningTokens = usage.reasoning_tokens ?? usage.thoughtsTokenCount ?? usage.completion_tokens_details?.reasoning_tokens ?? 0;
|
|
515
|
+
}
|
|
516
|
+
if (!cachedTokens) {
|
|
517
|
+
cachedTokens = usage.prompt_tokens_details?.cached_tokens ?? usage.cached_tokens ?? usage.cachedContentTokenCount ?? 0;
|
|
518
|
+
}
|
|
467
519
|
const model = response.model || fallbackModel;
|
|
468
520
|
const provider = response.provider || fallbackProvider;
|
|
469
521
|
return {
|
|
@@ -515,6 +567,10 @@ var MODEL_PRICING_TABLE = {
|
|
|
515
567
|
"chatgpt-4o-latest": { inputPer1M: 5, outputPer1M: 15 },
|
|
516
568
|
"gpt-4.1": { inputPer1M: 2, outputPer1M: 8, cachedInputPer1M: 1 },
|
|
517
569
|
"gpt-4.1-nano": { inputPer1M: 0.1, outputPer1M: 0.4, cachedInputPer1M: 0.05 },
|
|
570
|
+
"gpt-6-astra": { inputPer1M: 5, outputPer1M: 25, cachedInputPer1M: 0.5 },
|
|
571
|
+
"gpt-6-astra-fast": { inputPer1M: 3, outputPer1M: 15, cachedInputPer1M: 0.3 },
|
|
572
|
+
"gpt-image-2.5-flare": { inputPer1M: 0, outputPer1M: 0.04 },
|
|
573
|
+
"gpt-image-2.5-sunburst": { inputPer1M: 0, outputPer1M: 0.08 },
|
|
518
574
|
// --- OpenAI (Legacy & Backward Compatibility) ---
|
|
519
575
|
"gpt-4-turbo": { inputPer1M: 10, outputPer1M: 30, cachedInputPer1M: 5 },
|
|
520
576
|
"gpt-4-turbo-preview": { inputPer1M: 10, outputPer1M: 30, cachedInputPer1M: 5 },
|
|
@@ -567,7 +623,20 @@ var MODEL_PRICING_TABLE = {
|
|
|
567
623
|
"mistral-large-2411": { inputPer1M: 2, outputPer1M: 6 },
|
|
568
624
|
"codestral-latest": { inputPer1M: 0.3, outputPer1M: 0.9 },
|
|
569
625
|
"mistral-medium-latest": { inputPer1M: 2.7, outputPer1M: 8.1 },
|
|
626
|
+
"mistral-medium-3-5": { inputPer1M: 2, outputPer1M: 6, cachedInputPer1M: 0.4 },
|
|
627
|
+
"mistral-medium-2604": { inputPer1M: 2, outputPer1M: 6, cachedInputPer1M: 0.4 },
|
|
628
|
+
"magistral-small-latest": { inputPer1M: 0.5, outputPer1M: 1.5, cachedInputPer1M: 0.1 },
|
|
629
|
+
"magistral-medium-latest": { inputPer1M: 2, outputPer1M: 6, cachedInputPer1M: 0.4 },
|
|
570
630
|
"mistral-small-latest": { inputPer1M: 0.2, outputPer1M: 0.6 },
|
|
631
|
+
"mistral-small-2603": { inputPer1M: 0.2, outputPer1M: 0.6, cachedInputPer1M: 0.05 },
|
|
632
|
+
"mistral-vibe-cli-fast": { inputPer1M: 0.2, outputPer1M: 0.6, cachedInputPer1M: 0.05 },
|
|
633
|
+
"mistral-vibe-cli-latest": { inputPer1M: 0.2, outputPer1M: 0.6, cachedInputPer1M: 0.05 },
|
|
634
|
+
"codestral-2508": { inputPer1M: 0.3, outputPer1M: 0.9, cachedInputPer1M: 0.08 },
|
|
635
|
+
"codestral-embed": { inputPer1M: 0.1, outputPer1M: 0 },
|
|
636
|
+
"mistral-embed-2312": { inputPer1M: 0.1, outputPer1M: 0 },
|
|
637
|
+
"zai-glm-5-2": { inputPer1M: 0.6, outputPer1M: 2.2, cachedInputPer1M: 0.15 },
|
|
638
|
+
"glm-5-2": { inputPer1M: 0.6, outputPer1M: 2.2, cachedInputPer1M: 0.15 },
|
|
639
|
+
"labs-leanstral-1-5": { inputPer1M: 0.2, outputPer1M: 0.6, cachedInputPer1M: 0.05 },
|
|
571
640
|
"ministral-8b-latest": { inputPer1M: 0.1, outputPer1M: 0.1 },
|
|
572
641
|
"ministral-3b-latest": { inputPer1M: 0.04, outputPer1M: 0.04 },
|
|
573
642
|
"open-mistral-7b": { inputPer1M: 0.2, outputPer1M: 0.2 },
|
|
@@ -590,11 +659,17 @@ var MODEL_PRICING_TABLE = {
|
|
|
590
659
|
"gemma2-9b-it": { inputPer1M: 0.2, outputPer1M: 0.2 },
|
|
591
660
|
// --- DeepSeek ---
|
|
592
661
|
"deepseek-v4-pro": { inputPer1M: 0.66, outputPer1M: 1.98, cachedInputPer1M: 0.15 },
|
|
662
|
+
"deepseek-v4.1-flash": { inputPer1M: 0.2, outputPer1M: 0.6, cachedInputPer1M: 0.05 },
|
|
593
663
|
"deepseek-v4-flash": { inputPer1M: 0.22, outputPer1M: 0.66, cachedInputPer1M: 0.05 },
|
|
594
664
|
"deepseek-v3": { inputPer1M: 0.14, outputPer1M: 0.28, cachedInputPer1M: 0.014 },
|
|
595
665
|
"deepseek-chat": { inputPer1M: 0.14, outputPer1M: 0.28, cachedInputPer1M: 0.014 },
|
|
596
666
|
"deepseek-r1": { inputPer1M: 0.55, outputPer1M: 2.19, cachedInputPer1M: 0.14 },
|
|
597
667
|
"deepseek-reasoner": { inputPer1M: 0.55, outputPer1M: 2.19, cachedInputPer1M: 0.14 },
|
|
668
|
+
// --- Gateway, Inception & InclusionAI Models ---
|
|
669
|
+
"mercury-2.5": { inputPer1M: 0.25, outputPer1M: 0.75, cachedInputPer1M: 0.05 },
|
|
670
|
+
"mercury-2": { inputPer1M: 0.25, outputPer1M: 0.75, cachedInputPer1M: 0.05 },
|
|
671
|
+
"ling-3.0-flash-sante": { inputPer1M: 0.15, outputPer1M: 0.45, cachedInputPer1M: 0.03 },
|
|
672
|
+
"ling-3.0-flash": { inputPer1M: 0.15, outputPer1M: 0.45, cachedInputPer1M: 0.03 },
|
|
598
673
|
// --- Cohere ---
|
|
599
674
|
"command-r-plus": { inputPer1M: 2.5, outputPer1M: 10 },
|
|
600
675
|
"command-r": { inputPer1M: 0.15, outputPer1M: 0.6 },
|
|
@@ -642,7 +717,15 @@ var MODEL_ALIASES = {
|
|
|
642
717
|
// Mistral Shorthands
|
|
643
718
|
"codestral": "codestral-latest",
|
|
644
719
|
"mistral-large": "mistral-large-latest",
|
|
645
|
-
"mistral-small": "mistral-small-latest"
|
|
720
|
+
"mistral-small": "mistral-small-latest",
|
|
721
|
+
"magistral": "magistral-small-latest",
|
|
722
|
+
"magistral-small": "magistral-small-latest",
|
|
723
|
+
"magistral-medium": "magistral-medium-latest",
|
|
724
|
+
"mistral-3.5": "mistral-medium-3-5",
|
|
725
|
+
"mistral-medium-3.5": "mistral-medium-3-5",
|
|
726
|
+
"gpt-image-2.5": "gpt-image-2.5-flare",
|
|
727
|
+
"gpt-image-sunburst": "gpt-image-2.5-sunburst",
|
|
728
|
+
"gpt-image-flare": "gpt-image-2.5-flare"
|
|
646
729
|
};
|
|
647
730
|
var customPricingRegistry = {};
|
|
648
731
|
function normalizeModelKey(rawModel) {
|
|
@@ -761,8 +844,10 @@ function calculateCost(params) {
|
|
|
761
844
|
outputCostUSD: Number(outputCostUSD.toFixed(8)),
|
|
762
845
|
reasoningCostUSD: reasoningTokens > 0n ? Number(reasoningCostUSD.toFixed(8)) : void 0,
|
|
763
846
|
cachedDiscountUSD: cachedTokens > 0n ? Number(cachedDiscountUSD.toFixed(8)) : void 0,
|
|
847
|
+
savingsUSD: cachedTokens > 0n ? Number(cachedDiscountUSD.toFixed(8)) : void 0,
|
|
764
848
|
totalUSD: Number(totalUSD.toFixed(8)),
|
|
765
849
|
wholesaleTotalUSD: Number(wholesaleTotalUSD.toFixed(8)),
|
|
850
|
+
wholesaleUSD: Number(wholesaleTotalUSD.toFixed(8)),
|
|
766
851
|
billedUSD: Number(billedUSD.toFixed(8)),
|
|
767
852
|
profitUSD: Number(profitUSD.toFixed(8)),
|
|
768
853
|
retailUSD: retailUSD !== void 0 ? Number(retailUSD.toFixed(8)) : void 0,
|
|
@@ -984,7 +1069,7 @@ var VibezMeter = class {
|
|
|
984
1069
|
stripeClient;
|
|
985
1070
|
markupMultiplier;
|
|
986
1071
|
constructor(options = {}) {
|
|
987
|
-
this.markupMultiplier = options.markupMultiplier;
|
|
1072
|
+
this.markupMultiplier = options.pricing?.margin ?? options.markupMultiplier;
|
|
988
1073
|
if (options.stripe) {
|
|
989
1074
|
this.stripeClient = options.stripe;
|
|
990
1075
|
} else if (options.apiKey || process.env.STRIPE_SECRET_KEY) {
|
|
@@ -992,7 +1077,7 @@ var VibezMeter = class {
|
|
|
992
1077
|
this.stripeClient = new import_stripe.default(key, {
|
|
993
1078
|
appInfo: {
|
|
994
1079
|
name: "vibezcheck",
|
|
995
|
-
version: "0.5.
|
|
1080
|
+
version: "0.5.5",
|
|
996
1081
|
url: "https://vibezcheck.xyz"
|
|
997
1082
|
}
|
|
998
1083
|
});
|
|
@@ -1141,11 +1226,41 @@ function withBilling(model, optionsOrCustomer, extraOptions = {}) {
|
|
|
1141
1226
|
};
|
|
1142
1227
|
const handleUsage = (rawUsage, extraMeta) => {
|
|
1143
1228
|
if (!rawUsage) return;
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1229
|
+
let inputTokens = 0;
|
|
1230
|
+
let outputTokens = 0;
|
|
1231
|
+
let reasoningTokens = 0;
|
|
1232
|
+
let cachedTokens = 0;
|
|
1233
|
+
let cacheWriteTokens = 0;
|
|
1234
|
+
if (typeof rawUsage.inputTokens === "object" && rawUsage.inputTokens !== null) {
|
|
1235
|
+
inputTokens = rawUsage.inputTokens.total ?? rawUsage.inputTokens.noCache ?? 0;
|
|
1236
|
+
cachedTokens = rawUsage.inputTokens.cacheRead ?? 0;
|
|
1237
|
+
cacheWriteTokens = rawUsage.inputTokens.cacheWrite ?? 0;
|
|
1238
|
+
} else if (typeof rawUsage.promptTokens === "number") {
|
|
1239
|
+
inputTokens = rawUsage.promptTokens;
|
|
1240
|
+
} else if (typeof rawUsage.inputTokens === "number") {
|
|
1241
|
+
inputTokens = rawUsage.inputTokens;
|
|
1242
|
+
} else {
|
|
1243
|
+
inputTokens = rawUsage.prompt_tokens ?? rawUsage.input_tokens ?? rawUsage.promptTokenCount ?? 0;
|
|
1244
|
+
}
|
|
1245
|
+
if (typeof rawUsage.outputTokens === "object" && rawUsage.outputTokens !== null) {
|
|
1246
|
+
outputTokens = rawUsage.outputTokens.total ?? rawUsage.outputTokens.text ?? 0;
|
|
1247
|
+
reasoningTokens = rawUsage.outputTokens.reasoning ?? 0;
|
|
1248
|
+
} else if (typeof rawUsage.completionTokens === "number") {
|
|
1249
|
+
outputTokens = rawUsage.completionTokens;
|
|
1250
|
+
} else if (typeof rawUsage.outputTokens === "number") {
|
|
1251
|
+
outputTokens = rawUsage.outputTokens;
|
|
1252
|
+
} else {
|
|
1253
|
+
outputTokens = rawUsage.completion_tokens ?? rawUsage.output_tokens ?? rawUsage.candidatesTokenCount ?? 0;
|
|
1254
|
+
}
|
|
1255
|
+
if (!reasoningTokens) {
|
|
1256
|
+
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;
|
|
1257
|
+
}
|
|
1258
|
+
if (!cachedTokens) {
|
|
1259
|
+
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;
|
|
1260
|
+
}
|
|
1261
|
+
if (!cacheWriteTokens) {
|
|
1262
|
+
cacheWriteTokens = rawUsage.cacheWriteTokens ?? rawUsage.cache_creation_input_tokens ?? rawUsage.cacheCreationTokens ?? 0;
|
|
1263
|
+
}
|
|
1149
1264
|
const usage = {
|
|
1150
1265
|
inputTokens,
|
|
1151
1266
|
outputTokens,
|
|
@@ -1346,16 +1461,22 @@ function withBilling(model, optionsOrCustomer, extraOptions = {}) {
|
|
|
1346
1461
|
const transformStream = new TransformStream({
|
|
1347
1462
|
transform(chunk, controller) {
|
|
1348
1463
|
controller.enqueue(chunk);
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1464
|
+
if (chunk.type === "text-delta" && typeof chunk.delta === "string") {
|
|
1465
|
+
accumulatedChars += chunk.delta.length;
|
|
1466
|
+
} else if (chunk.type === "reasoning-delta" && typeof chunk.delta === "string") {
|
|
1467
|
+
accumulatedChars += chunk.delta.length;
|
|
1468
|
+
} else {
|
|
1469
|
+
const delta = chunk.textDelta || chunk.text || chunk.delta;
|
|
1470
|
+
if (delta && typeof delta === "string") {
|
|
1471
|
+
accumulatedChars += delta.length;
|
|
1472
|
+
} else if (chunk.type === "reasoning" || chunk.type === "reasoning-delta") {
|
|
1473
|
+
const reasoning = chunk.reasoning || chunk.textDelta || chunk.text || chunk.delta || "";
|
|
1474
|
+
if (typeof reasoning === "string") {
|
|
1475
|
+
accumulatedChars += reasoning.length;
|
|
1476
|
+
}
|
|
1356
1477
|
}
|
|
1357
1478
|
}
|
|
1358
|
-
const chunkUsage = chunk.usage || chunk.token_usage || chunk.usageMetadata;
|
|
1479
|
+
const chunkUsage = (chunk.type === "finish" ? chunk.usage : null) || chunk.usage || chunk.token_usage || chunk.usageMetadata;
|
|
1359
1480
|
if (chunkUsage) {
|
|
1360
1481
|
streamCompleted = true;
|
|
1361
1482
|
handleUsage(chunkUsage);
|
|
@@ -1434,12 +1555,16 @@ function createVibezModel(modelOrId, options = {}) {
|
|
|
1434
1555
|
providerName = "deepseek";
|
|
1435
1556
|
} else if (rawId.startsWith("grok-")) {
|
|
1436
1557
|
providerName = "xai";
|
|
1437
|
-
} else if (rawId.startsWith("mistral-") || rawId.startsWith("codestral-") || rawId.startsWith("ministral-") || rawId.startsWith("open-mistral") || rawId.startsWith("open-mixtral")) {
|
|
1558
|
+
} else if (rawId.startsWith("mistral-") || rawId.startsWith("codestral-") || rawId.startsWith("ministral-") || rawId.startsWith("magistral-") || rawId.startsWith("voxtral-") || rawId.startsWith("devstral-") || rawId.startsWith("open-mistral") || rawId.startsWith("open-mixtral")) {
|
|
1438
1559
|
providerName = "mistral";
|
|
1439
1560
|
} else if (rawId.startsWith("llama-") || rawId.startsWith("mixtral-") || rawId.startsWith("gemma") || rawId.startsWith("qwen-")) {
|
|
1440
1561
|
providerName = "groq";
|
|
1441
1562
|
} else if (rawId.startsWith("command-")) {
|
|
1442
1563
|
providerName = "cohere";
|
|
1564
|
+
} else if (rawId.startsWith("mercury-") || rawId.startsWith("ling-")) {
|
|
1565
|
+
providerName = "gateway";
|
|
1566
|
+
} else if (rawId.startsWith("gpt-image-") || rawId.startsWith("gpt-6-")) {
|
|
1567
|
+
providerName = "openai";
|
|
1443
1568
|
}
|
|
1444
1569
|
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;
|
|
1445
1570
|
const baseURL = options.baseURL || process.env.AI_GATEWAY_BASE_URL || (providerName === "openai" ? "https://api.openai.com/v1" : void 0);
|