vibezcheck 0.4.1 → 0.4.2

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.
Files changed (38) hide show
  1. package/README.md +273 -203
  2. package/dist/ai-sdk/index.d.mts +6 -4
  3. package/dist/ai-sdk/index.d.ts +6 -4
  4. package/dist/ai-sdk/index.js +184 -22
  5. package/dist/ai-sdk/index.js.map +1 -1
  6. package/dist/ai-sdk/index.mjs +184 -22
  7. package/dist/ai-sdk/index.mjs.map +1 -1
  8. package/dist/cli/index.js +2 -2
  9. package/dist/cli/index.js.map +1 -1
  10. package/dist/cli/index.mjs +2 -2
  11. package/dist/cli/index.mjs.map +1 -1
  12. package/dist/{client-kJN_Znan.d.ts → client-w_hWhQ6g.d.mts} +4 -2
  13. package/dist/{client--iq93FQg.d.mts → client-ynfWcHX6.d.ts} +4 -2
  14. package/dist/customers/index.d.mts +30 -4
  15. package/dist/customers/index.d.ts +30 -4
  16. package/dist/customers/index.js +108 -8
  17. package/dist/customers/index.js.map +1 -1
  18. package/dist/customers/index.mjs +106 -7
  19. package/dist/customers/index.mjs.map +1 -1
  20. package/dist/index.d.mts +4 -4
  21. package/dist/index.d.ts +4 -4
  22. package/dist/index.js +238 -28
  23. package/dist/index.js.map +1 -1
  24. package/dist/index.mjs +237 -28
  25. package/dist/index.mjs.map +1 -1
  26. package/dist/meter/index.d.mts +2 -2
  27. package/dist/meter/index.d.ts +2 -2
  28. package/dist/meter/index.js +95 -16
  29. package/dist/meter/index.js.map +1 -1
  30. package/dist/meter/index.mjs +95 -16
  31. package/dist/meter/index.mjs.map +1 -1
  32. package/dist/pricing/index.d.mts +1 -1
  33. package/dist/pricing/index.d.ts +1 -1
  34. package/dist/react/index.d.mts +1 -1
  35. package/dist/react/index.d.ts +1 -1
  36. package/dist/{types-Zbzxg-Ka.d.mts → types-Mozk4-Mr.d.mts} +70 -12
  37. package/dist/{types-Zbzxg-Ka.d.ts → types-Mozk4-Mr.d.ts} +70 -12
  38. package/package.json +161 -153
@@ -617,6 +617,58 @@ function calculateUsageCost(model, usage, optionsOrMarkup) {
617
617
  });
618
618
  }
619
619
 
620
+ // src/customers/helpers.ts
621
+ function normalizeCustomer(customer, extraCustomerId) {
622
+ if (!customer && !extraCustomerId) {
623
+ return {
624
+ customerId: void 0,
625
+ customerEmail: void 0,
626
+ customerObj: void 0,
627
+ customerMetadata: {}
628
+ };
629
+ }
630
+ if (typeof customer === "string") {
631
+ const isEmail = customer.includes("@");
632
+ const customerObj2 = {
633
+ id: customer,
634
+ email: isEmail ? customer : void 0
635
+ };
636
+ return {
637
+ customerId: customer,
638
+ customerEmail: isEmail ? customer : void 0,
639
+ customerObj: customerObj2,
640
+ customerMetadata: {}
641
+ };
642
+ }
643
+ const customerObj = customer || (extraCustomerId ? { id: extraCustomerId } : void 0);
644
+ const customerId = customerObj?.id || customerObj?.userId || extraCustomerId;
645
+ const customerEmail = customerObj?.email;
646
+ const customerMetadata = {};
647
+ if (customerObj) {
648
+ const orgId = customerObj.orgId || customerObj.organizationId;
649
+ if (orgId) customerMetadata.org_id = String(orgId);
650
+ const teamId = customerObj.teamId || customerObj.workspaceId;
651
+ if (teamId) customerMetadata.team_id = String(teamId);
652
+ if (customerObj.plan) customerMetadata.plan = String(customerObj.plan);
653
+ if (customerObj.tier) customerMetadata.tier = String(customerObj.tier);
654
+ if (customerObj.role) customerMetadata.role = String(customerObj.role);
655
+ if (customerObj.orgName) customerMetadata.org_name = String(customerObj.orgName);
656
+ if (customerObj.metadata) {
657
+ for (const [key, val] of Object.entries(customerObj.metadata)) {
658
+ if (val !== null && val !== void 0) {
659
+ customerMetadata[key] = val;
660
+ }
661
+ }
662
+ }
663
+ }
664
+ return {
665
+ customerId,
666
+ customerEmail,
667
+ customerObj,
668
+ customerMetadata
669
+ };
670
+ }
671
+
620
672
  // src/meter/stream.ts
621
673
  function wrapOpenAIStream(stream, options, onComplete) {
622
674
  let detectedModel = options.model || "gpt-4o";
@@ -637,15 +689,20 @@ function wrapOpenAIStream(stream, options, onComplete) {
637
689
  } finally {
638
690
  if (finalUsage) {
639
691
  const cost = calculateUsageCost(detectedModel, finalUsage);
640
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
692
+ const normalized = normalizeCustomer(options.customer, options.customerId);
641
693
  const event = {
642
694
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
643
695
  model: detectedModel,
644
696
  provider: "openai",
645
697
  usage: finalUsage,
646
698
  cost,
647
- customerId,
648
- metadata: options.metadata
699
+ customerId: normalized.customerId,
700
+ customerEmail: normalized.customerEmail,
701
+ customer: normalized.customerObj,
702
+ metadata: {
703
+ ...normalized.customerMetadata,
704
+ ...options.metadata
705
+ }
649
706
  };
650
707
  onComplete(event);
651
708
  if (options.onUsage) {
@@ -670,15 +727,20 @@ function wrapAnthropicStream(stream, options, onComplete) {
670
727
  const extracted = accumulator.getUsage();
671
728
  const model = options.model || extracted.model;
672
729
  const cost = calculateUsageCost(model, extracted.usage);
673
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
730
+ const normalized = normalizeCustomer(options.customer, options.customerId);
674
731
  const usageEvent = {
675
732
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
676
733
  model,
677
734
  provider: "anthropic",
678
735
  usage: extracted.usage,
679
736
  cost,
680
- customerId,
681
- metadata: options.metadata
737
+ customerId: normalized.customerId,
738
+ customerEmail: normalized.customerEmail,
739
+ customer: normalized.customerObj,
740
+ metadata: {
741
+ ...normalized.customerMetadata,
742
+ ...options.metadata
743
+ }
682
744
  };
683
745
  onComplete(usageEvent);
684
746
  if (options.onUsage) {
@@ -707,15 +769,20 @@ function wrapGeminiStream(result, options, onComplete) {
707
769
  const extracted = extractGeminiResponseUsage(lastChunkWithUsage, model);
708
770
  if (extracted) {
709
771
  const cost = calculateUsageCost(extracted.model, extracted.usage);
710
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
772
+ const normalized = normalizeCustomer(options.customer, options.customerId);
711
773
  const event = {
712
774
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
713
775
  model: extracted.model,
714
776
  provider: "google",
715
777
  usage: extracted.usage,
716
778
  cost,
717
- customerId,
718
- metadata: options.metadata
779
+ customerId: normalized.customerId,
780
+ customerEmail: normalized.customerEmail,
781
+ customer: normalized.customerObj,
782
+ metadata: {
783
+ ...normalized.customerMetadata,
784
+ ...options.metadata
785
+ }
719
786
  };
720
787
  onComplete(event);
721
788
  if (options.onUsage) {
@@ -758,7 +825,7 @@ var VibezMeter = class {
758
825
  this.stripeClient = new Stripe(key, {
759
826
  appInfo: {
760
827
  name: "vibezcheck",
761
- version: "0.1.0",
828
+ version: "0.4.2",
762
829
  url: "https://vibezcheck.xyz"
763
830
  }
764
831
  });
@@ -778,15 +845,21 @@ var VibezMeter = class {
778
845
  }
779
846
  const model = options.model || extracted.model;
780
847
  const cost = calculateUsageCost(model, extracted.usage, this.markupMultiplier);
781
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
848
+ const normalized = normalizeCustomer(options.customer, options.customerId);
849
+ const mergedMeta = {
850
+ ...normalized.customerMetadata,
851
+ ...options.metadata
852
+ };
782
853
  const event = {
783
854
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
784
855
  model,
785
856
  provider: extracted.provider,
786
857
  usage: extracted.usage,
787
858
  cost,
788
- customerId,
789
- metadata: options.metadata
859
+ customerId: normalized.customerId,
860
+ customerEmail: normalized.customerEmail,
861
+ customer: normalized.customerObj,
862
+ metadata: mergedMeta
790
863
  };
791
864
  this.batcher.enqueue(event);
792
865
  return event;
@@ -816,15 +889,21 @@ var VibezMeter = class {
816
889
  cachedTokens
817
890
  };
818
891
  const cost = calculateUsageCost(options.model, usage, this.markupMultiplier);
819
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
892
+ const normalized = normalizeCustomer(options.customer, options.customerId);
893
+ const mergedMeta = {
894
+ ...normalized.customerMetadata,
895
+ ...options.metadata
896
+ };
820
897
  const event = {
821
898
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
822
899
  model: options.model,
823
900
  provider: options.provider || "custom",
824
901
  usage,
825
902
  cost,
826
- customerId,
827
- metadata: options.metadata
903
+ customerId: normalized.customerId,
904
+ customerEmail: normalized.customerEmail,
905
+ customer: normalized.customerObj,
906
+ metadata: mergedMeta
828
907
  };
829
908
  this.batcher.enqueue(event);
830
909
  return event;
@@ -861,7 +940,11 @@ function withBilling(model, options = {}) {
861
940
  apiKey: options.stripeApiKey,
862
941
  eventName: options.eventName
863
942
  });
864
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
943
+ const normalized = normalizeCustomer(options.customer, options.customerId);
944
+ const customerId = normalized.customerId;
945
+ const customerEmail = normalized.customerEmail;
946
+ const customerObj = normalized.customerObj;
947
+ const customerMetadata = normalized.customerMetadata;
865
948
  const modelId = model.modelId || "unknown-model";
866
949
  const provider = model.provider?.replace(/^@ai-sdk\//, "") || "ai-sdk";
867
950
  const maxCostPerCall = options.maxCostPerCallUSD !== void 0 ? options.maxCostPerCallUSD : 0.5;
@@ -930,6 +1013,12 @@ function withBilling(model, options = {}) {
930
1013
  throw new VibezCircuitBreakerError(budgetEvent);
931
1014
  }
932
1015
  }
1016
+ const mergedMetadata = {
1017
+ ...customerMetadata,
1018
+ ...options.metadata,
1019
+ ...extraMeta,
1020
+ billingMode: options.billing?.mode || "postpaid"
1021
+ };
933
1022
  const event = {
934
1023
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
935
1024
  model: modelId,
@@ -937,11 +1026,9 @@ function withBilling(model, options = {}) {
937
1026
  usage,
938
1027
  cost,
939
1028
  customerId,
940
- metadata: {
941
- ...options.metadata,
942
- ...extraMeta,
943
- billingMode: options.billing?.mode || "postpaid"
944
- }
1029
+ customerEmail,
1030
+ customer: customerObj,
1031
+ metadata: mergedMetadata
945
1032
  };
946
1033
  meter.recordUsage({
947
1034
  model: modelId,
@@ -951,11 +1038,84 @@ function withBilling(model, options = {}) {
951
1038
  reasoningTokens: usage.reasoningTokens,
952
1039
  cachedTokens: usage.cachedTokens,
953
1040
  customerId,
1041
+ customer: customerObj,
954
1042
  metadata: event.metadata
955
1043
  });
956
1044
  if (options.onUsage) {
957
1045
  options.onUsage(event);
958
1046
  }
1047
+ if (options.database) {
1048
+ const dbTarget = options.database;
1049
+ const executeDbWrite = async () => {
1050
+ try {
1051
+ const supabaseClient = typeof dbTarget === "object" && dbTarget !== null && typeof dbTarget.from === "function" ? dbTarget : typeof dbTarget === "object" && dbTarget !== null && typeof dbTarget.client?.from === "function" ? dbTarget.client : null;
1052
+ if (supabaseClient) {
1053
+ const tableName = dbTarget?.table || "vibez_usage";
1054
+ await supabaseClient.from(tableName).insert({
1055
+ customer_id: customerId,
1056
+ user_id: customerObj?.userId || customerId,
1057
+ model: modelId,
1058
+ input_tokens: usage.inputTokens,
1059
+ output_tokens: usage.outputTokens,
1060
+ total_tokens: usage.totalTokens,
1061
+ cached_tokens: usage.cachedTokens,
1062
+ reasoning_tokens: usage.reasoningTokens,
1063
+ cost_usd: cost.totalUSD,
1064
+ created_at: event.timestamp,
1065
+ metadata: event.metadata
1066
+ });
1067
+ return;
1068
+ }
1069
+ if (typeof dbTarget === "object" && dbTarget !== null && typeof dbTarget.save === "function") {
1070
+ await dbTarget.save(event);
1071
+ return;
1072
+ }
1073
+ if (typeof dbTarget === "function") {
1074
+ await dbTarget(event);
1075
+ return;
1076
+ }
1077
+ } catch (err) {
1078
+ console.warn(`[vibezcheck] Database record failed safely:`, err?.message || err);
1079
+ }
1080
+ };
1081
+ if (typeof globalThis !== "undefined" && typeof globalThis.after === "function") {
1082
+ globalThis.after(() => executeDbWrite());
1083
+ } else {
1084
+ executeDbWrite().catch(() => {
1085
+ });
1086
+ }
1087
+ }
1088
+ if (options.billing?.charge && typeof options.billing.charge === "function") {
1089
+ const chargeFn = options.billing.charge;
1090
+ const executeCharge = async () => {
1091
+ try {
1092
+ await chargeFn(cost.totalUSD, event);
1093
+ } catch (err) {
1094
+ console.warn(`[vibezcheck] Custom charge handler failed safely:`, err?.message || err);
1095
+ }
1096
+ };
1097
+ if (typeof globalThis !== "undefined" && typeof globalThis.after === "function") {
1098
+ globalThis.after(() => executeCharge());
1099
+ } else {
1100
+ executeCharge().catch(() => {
1101
+ });
1102
+ }
1103
+ } else if (options.billing?.provider && typeof options.billing.provider === "object" && typeof options.billing.provider.charge === "function") {
1104
+ const providerCharge = options.billing.provider.charge;
1105
+ const executeCharge = async () => {
1106
+ try {
1107
+ await providerCharge(cost.totalUSD, event);
1108
+ } catch (err) {
1109
+ console.warn(`[vibezcheck] Payment provider charge failed safely:`, err?.message || err);
1110
+ }
1111
+ };
1112
+ if (typeof globalThis !== "undefined" && typeof globalThis.after === "function") {
1113
+ globalThis.after(() => executeCharge());
1114
+ } else {
1115
+ executeCharge().catch(() => {
1116
+ });
1117
+ }
1118
+ }
959
1119
  scheduleFlush();
960
1120
  };
961
1121
  const handler = {
@@ -1003,6 +1163,8 @@ function withBilling(model, options = {}) {
1003
1163
  controller.enqueue(chunk);
1004
1164
  if (chunk.type === "text-delta" && chunk.textDelta) {
1005
1165
  accumulatedChars += chunk.textDelta.length;
1166
+ } else if (chunk.type === "reasoning" && (chunk.textDelta || chunk.reasoning)) {
1167
+ accumulatedChars += (chunk.textDelta || chunk.reasoning || "").length;
1006
1168
  }
1007
1169
  if (chunk.type === "finish" && chunk.usage) {
1008
1170
  streamCompleted = true;