vibezcheck 0.4.0 → 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 +316 -17
  9. package/dist/cli/index.js.map +1 -1
  10. package/dist/cli/index.mjs +316 -17
  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
@@ -650,6 +650,58 @@ function calculateUsageCost(model, usage, optionsOrMarkup) {
650
650
  });
651
651
  }
652
652
 
653
+ // src/customers/helpers.ts
654
+ function normalizeCustomer(customer, extraCustomerId) {
655
+ if (!customer && !extraCustomerId) {
656
+ return {
657
+ customerId: void 0,
658
+ customerEmail: void 0,
659
+ customerObj: void 0,
660
+ customerMetadata: {}
661
+ };
662
+ }
663
+ if (typeof customer === "string") {
664
+ const isEmail = customer.includes("@");
665
+ const customerObj2 = {
666
+ id: customer,
667
+ email: isEmail ? customer : void 0
668
+ };
669
+ return {
670
+ customerId: customer,
671
+ customerEmail: isEmail ? customer : void 0,
672
+ customerObj: customerObj2,
673
+ customerMetadata: {}
674
+ };
675
+ }
676
+ const customerObj = customer || (extraCustomerId ? { id: extraCustomerId } : void 0);
677
+ const customerId = customerObj?.id || customerObj?.userId || extraCustomerId;
678
+ const customerEmail = customerObj?.email;
679
+ const customerMetadata = {};
680
+ if (customerObj) {
681
+ const orgId = customerObj.orgId || customerObj.organizationId;
682
+ if (orgId) customerMetadata.org_id = String(orgId);
683
+ const teamId = customerObj.teamId || customerObj.workspaceId;
684
+ if (teamId) customerMetadata.team_id = String(teamId);
685
+ if (customerObj.plan) customerMetadata.plan = String(customerObj.plan);
686
+ if (customerObj.tier) customerMetadata.tier = String(customerObj.tier);
687
+ if (customerObj.role) customerMetadata.role = String(customerObj.role);
688
+ if (customerObj.orgName) customerMetadata.org_name = String(customerObj.orgName);
689
+ if (customerObj.metadata) {
690
+ for (const [key, val] of Object.entries(customerObj.metadata)) {
691
+ if (val !== null && val !== void 0) {
692
+ customerMetadata[key] = val;
693
+ }
694
+ }
695
+ }
696
+ }
697
+ return {
698
+ customerId,
699
+ customerEmail,
700
+ customerObj,
701
+ customerMetadata
702
+ };
703
+ }
704
+
653
705
  // src/meter/stream.ts
654
706
  function wrapOpenAIStream(stream, options, onComplete) {
655
707
  let detectedModel = options.model || "gpt-4o";
@@ -670,15 +722,20 @@ function wrapOpenAIStream(stream, options, onComplete) {
670
722
  } finally {
671
723
  if (finalUsage) {
672
724
  const cost = calculateUsageCost(detectedModel, finalUsage);
673
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
725
+ const normalized = normalizeCustomer(options.customer, options.customerId);
674
726
  const event = {
675
727
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
676
728
  model: detectedModel,
677
729
  provider: "openai",
678
730
  usage: finalUsage,
679
731
  cost,
680
- customerId,
681
- metadata: options.metadata
732
+ customerId: normalized.customerId,
733
+ customerEmail: normalized.customerEmail,
734
+ customer: normalized.customerObj,
735
+ metadata: {
736
+ ...normalized.customerMetadata,
737
+ ...options.metadata
738
+ }
682
739
  };
683
740
  onComplete(event);
684
741
  if (options.onUsage) {
@@ -703,15 +760,20 @@ function wrapAnthropicStream(stream, options, onComplete) {
703
760
  const extracted = accumulator.getUsage();
704
761
  const model = options.model || extracted.model;
705
762
  const cost = calculateUsageCost(model, extracted.usage);
706
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
763
+ const normalized = normalizeCustomer(options.customer, options.customerId);
707
764
  const usageEvent = {
708
765
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
709
766
  model,
710
767
  provider: "anthropic",
711
768
  usage: extracted.usage,
712
769
  cost,
713
- customerId,
714
- metadata: options.metadata
770
+ customerId: normalized.customerId,
771
+ customerEmail: normalized.customerEmail,
772
+ customer: normalized.customerObj,
773
+ metadata: {
774
+ ...normalized.customerMetadata,
775
+ ...options.metadata
776
+ }
715
777
  };
716
778
  onComplete(usageEvent);
717
779
  if (options.onUsage) {
@@ -740,15 +802,20 @@ function wrapGeminiStream(result, options, onComplete) {
740
802
  const extracted = extractGeminiResponseUsage(lastChunkWithUsage, model);
741
803
  if (extracted) {
742
804
  const cost = calculateUsageCost(extracted.model, extracted.usage);
743
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
805
+ const normalized = normalizeCustomer(options.customer, options.customerId);
744
806
  const event = {
745
807
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
746
808
  model: extracted.model,
747
809
  provider: "google",
748
810
  usage: extracted.usage,
749
811
  cost,
750
- customerId,
751
- metadata: options.metadata
812
+ customerId: normalized.customerId,
813
+ customerEmail: normalized.customerEmail,
814
+ customer: normalized.customerObj,
815
+ metadata: {
816
+ ...normalized.customerMetadata,
817
+ ...options.metadata
818
+ }
752
819
  };
753
820
  onComplete(event);
754
821
  if (options.onUsage) {
@@ -791,7 +858,7 @@ var VibezMeter = class {
791
858
  this.stripeClient = new import_stripe.default(key, {
792
859
  appInfo: {
793
860
  name: "vibezcheck",
794
- version: "0.1.0",
861
+ version: "0.4.2",
795
862
  url: "https://vibezcheck.xyz"
796
863
  }
797
864
  });
@@ -811,15 +878,21 @@ var VibezMeter = class {
811
878
  }
812
879
  const model = options.model || extracted.model;
813
880
  const cost = calculateUsageCost(model, extracted.usage, this.markupMultiplier);
814
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
881
+ const normalized = normalizeCustomer(options.customer, options.customerId);
882
+ const mergedMeta = {
883
+ ...normalized.customerMetadata,
884
+ ...options.metadata
885
+ };
815
886
  const event = {
816
887
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
817
888
  model,
818
889
  provider: extracted.provider,
819
890
  usage: extracted.usage,
820
891
  cost,
821
- customerId,
822
- metadata: options.metadata
892
+ customerId: normalized.customerId,
893
+ customerEmail: normalized.customerEmail,
894
+ customer: normalized.customerObj,
895
+ metadata: mergedMeta
823
896
  };
824
897
  this.batcher.enqueue(event);
825
898
  return event;
@@ -849,15 +922,21 @@ var VibezMeter = class {
849
922
  cachedTokens
850
923
  };
851
924
  const cost = calculateUsageCost(options.model, usage, this.markupMultiplier);
852
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
925
+ const normalized = normalizeCustomer(options.customer, options.customerId);
926
+ const mergedMeta = {
927
+ ...normalized.customerMetadata,
928
+ ...options.metadata
929
+ };
853
930
  const event = {
854
931
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
855
932
  model: options.model,
856
933
  provider: options.provider || "custom",
857
934
  usage,
858
935
  cost,
859
- customerId,
860
- metadata: options.metadata
936
+ customerId: normalized.customerId,
937
+ customerEmail: normalized.customerEmail,
938
+ customer: normalized.customerObj,
939
+ metadata: mergedMeta
861
940
  };
862
941
  this.batcher.enqueue(event);
863
942
  return event;
@@ -894,7 +973,11 @@ function withBilling(model, options = {}) {
894
973
  apiKey: options.stripeApiKey,
895
974
  eventName: options.eventName
896
975
  });
897
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
976
+ const normalized = normalizeCustomer(options.customer, options.customerId);
977
+ const customerId = normalized.customerId;
978
+ const customerEmail = normalized.customerEmail;
979
+ const customerObj = normalized.customerObj;
980
+ const customerMetadata = normalized.customerMetadata;
898
981
  const modelId = model.modelId || "unknown-model";
899
982
  const provider = model.provider?.replace(/^@ai-sdk\//, "") || "ai-sdk";
900
983
  const maxCostPerCall = options.maxCostPerCallUSD !== void 0 ? options.maxCostPerCallUSD : 0.5;
@@ -963,6 +1046,12 @@ function withBilling(model, options = {}) {
963
1046
  throw new VibezCircuitBreakerError(budgetEvent);
964
1047
  }
965
1048
  }
1049
+ const mergedMetadata = {
1050
+ ...customerMetadata,
1051
+ ...options.metadata,
1052
+ ...extraMeta,
1053
+ billingMode: options.billing?.mode || "postpaid"
1054
+ };
966
1055
  const event = {
967
1056
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
968
1057
  model: modelId,
@@ -970,11 +1059,9 @@ function withBilling(model, options = {}) {
970
1059
  usage,
971
1060
  cost,
972
1061
  customerId,
973
- metadata: {
974
- ...options.metadata,
975
- ...extraMeta,
976
- billingMode: options.billing?.mode || "postpaid"
977
- }
1062
+ customerEmail,
1063
+ customer: customerObj,
1064
+ metadata: mergedMetadata
978
1065
  };
979
1066
  meter.recordUsage({
980
1067
  model: modelId,
@@ -984,11 +1071,84 @@ function withBilling(model, options = {}) {
984
1071
  reasoningTokens: usage.reasoningTokens,
985
1072
  cachedTokens: usage.cachedTokens,
986
1073
  customerId,
1074
+ customer: customerObj,
987
1075
  metadata: event.metadata
988
1076
  });
989
1077
  if (options.onUsage) {
990
1078
  options.onUsage(event);
991
1079
  }
1080
+ if (options.database) {
1081
+ const dbTarget = options.database;
1082
+ const executeDbWrite = async () => {
1083
+ try {
1084
+ 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;
1085
+ if (supabaseClient) {
1086
+ const tableName = dbTarget?.table || "vibez_usage";
1087
+ await supabaseClient.from(tableName).insert({
1088
+ customer_id: customerId,
1089
+ user_id: customerObj?.userId || customerId,
1090
+ model: modelId,
1091
+ input_tokens: usage.inputTokens,
1092
+ output_tokens: usage.outputTokens,
1093
+ total_tokens: usage.totalTokens,
1094
+ cached_tokens: usage.cachedTokens,
1095
+ reasoning_tokens: usage.reasoningTokens,
1096
+ cost_usd: cost.totalUSD,
1097
+ created_at: event.timestamp,
1098
+ metadata: event.metadata
1099
+ });
1100
+ return;
1101
+ }
1102
+ if (typeof dbTarget === "object" && dbTarget !== null && typeof dbTarget.save === "function") {
1103
+ await dbTarget.save(event);
1104
+ return;
1105
+ }
1106
+ if (typeof dbTarget === "function") {
1107
+ await dbTarget(event);
1108
+ return;
1109
+ }
1110
+ } catch (err) {
1111
+ console.warn(`[vibezcheck] Database record failed safely:`, err?.message || err);
1112
+ }
1113
+ };
1114
+ if (typeof globalThis !== "undefined" && typeof globalThis.after === "function") {
1115
+ globalThis.after(() => executeDbWrite());
1116
+ } else {
1117
+ executeDbWrite().catch(() => {
1118
+ });
1119
+ }
1120
+ }
1121
+ if (options.billing?.charge && typeof options.billing.charge === "function") {
1122
+ const chargeFn = options.billing.charge;
1123
+ const executeCharge = async () => {
1124
+ try {
1125
+ await chargeFn(cost.totalUSD, event);
1126
+ } catch (err) {
1127
+ console.warn(`[vibezcheck] Custom charge handler failed safely:`, err?.message || err);
1128
+ }
1129
+ };
1130
+ if (typeof globalThis !== "undefined" && typeof globalThis.after === "function") {
1131
+ globalThis.after(() => executeCharge());
1132
+ } else {
1133
+ executeCharge().catch(() => {
1134
+ });
1135
+ }
1136
+ } else if (options.billing?.provider && typeof options.billing.provider === "object" && typeof options.billing.provider.charge === "function") {
1137
+ const providerCharge = options.billing.provider.charge;
1138
+ const executeCharge = async () => {
1139
+ try {
1140
+ await providerCharge(cost.totalUSD, event);
1141
+ } catch (err) {
1142
+ console.warn(`[vibezcheck] Payment provider charge failed safely:`, err?.message || err);
1143
+ }
1144
+ };
1145
+ if (typeof globalThis !== "undefined" && typeof globalThis.after === "function") {
1146
+ globalThis.after(() => executeCharge());
1147
+ } else {
1148
+ executeCharge().catch(() => {
1149
+ });
1150
+ }
1151
+ }
992
1152
  scheduleFlush();
993
1153
  };
994
1154
  const handler = {
@@ -1036,6 +1196,8 @@ function withBilling(model, options = {}) {
1036
1196
  controller.enqueue(chunk);
1037
1197
  if (chunk.type === "text-delta" && chunk.textDelta) {
1038
1198
  accumulatedChars += chunk.textDelta.length;
1199
+ } else if (chunk.type === "reasoning" && (chunk.textDelta || chunk.reasoning)) {
1200
+ accumulatedChars += (chunk.textDelta || chunk.reasoning || "").length;
1039
1201
  }
1040
1202
  if (chunk.type === "finish" && chunk.usage) {
1041
1203
  streamCompleted = true;