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
package/dist/index.mjs CHANGED
@@ -615,6 +615,58 @@ function calculateUsageCost(model, usage, optionsOrMarkup) {
615
615
  });
616
616
  }
617
617
 
618
+ // src/customers/helpers.ts
619
+ function normalizeCustomer(customer, extraCustomerId) {
620
+ if (!customer && !extraCustomerId) {
621
+ return {
622
+ customerId: void 0,
623
+ customerEmail: void 0,
624
+ customerObj: void 0,
625
+ customerMetadata: {}
626
+ };
627
+ }
628
+ if (typeof customer === "string") {
629
+ const isEmail = customer.includes("@");
630
+ const customerObj2 = {
631
+ id: customer,
632
+ email: isEmail ? customer : void 0
633
+ };
634
+ return {
635
+ customerId: customer,
636
+ customerEmail: isEmail ? customer : void 0,
637
+ customerObj: customerObj2,
638
+ customerMetadata: {}
639
+ };
640
+ }
641
+ const customerObj = customer || (extraCustomerId ? { id: extraCustomerId } : void 0);
642
+ const customerId = customerObj?.id || customerObj?.userId || extraCustomerId;
643
+ const customerEmail = customerObj?.email;
644
+ const customerMetadata = {};
645
+ if (customerObj) {
646
+ const orgId = customerObj.orgId || customerObj.organizationId;
647
+ if (orgId) customerMetadata.org_id = String(orgId);
648
+ const teamId = customerObj.teamId || customerObj.workspaceId;
649
+ if (teamId) customerMetadata.team_id = String(teamId);
650
+ if (customerObj.plan) customerMetadata.plan = String(customerObj.plan);
651
+ if (customerObj.tier) customerMetadata.tier = String(customerObj.tier);
652
+ if (customerObj.role) customerMetadata.role = String(customerObj.role);
653
+ if (customerObj.orgName) customerMetadata.org_name = String(customerObj.orgName);
654
+ if (customerObj.metadata) {
655
+ for (const [key, val] of Object.entries(customerObj.metadata)) {
656
+ if (val !== null && val !== void 0) {
657
+ customerMetadata[key] = val;
658
+ }
659
+ }
660
+ }
661
+ }
662
+ return {
663
+ customerId,
664
+ customerEmail,
665
+ customerObj,
666
+ customerMetadata
667
+ };
668
+ }
669
+
618
670
  // src/meter/stream.ts
619
671
  function wrapOpenAIStream(stream, options, onComplete) {
620
672
  let detectedModel = options.model || "gpt-4o";
@@ -635,15 +687,20 @@ function wrapOpenAIStream(stream, options, onComplete) {
635
687
  } finally {
636
688
  if (finalUsage) {
637
689
  const cost = calculateUsageCost(detectedModel, finalUsage);
638
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
690
+ const normalized = normalizeCustomer(options.customer, options.customerId);
639
691
  const event = {
640
692
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
641
693
  model: detectedModel,
642
694
  provider: "openai",
643
695
  usage: finalUsage,
644
696
  cost,
645
- customerId,
646
- metadata: options.metadata
697
+ customerId: normalized.customerId,
698
+ customerEmail: normalized.customerEmail,
699
+ customer: normalized.customerObj,
700
+ metadata: {
701
+ ...normalized.customerMetadata,
702
+ ...options.metadata
703
+ }
647
704
  };
648
705
  onComplete(event);
649
706
  if (options.onUsage) {
@@ -668,15 +725,20 @@ function wrapAnthropicStream(stream, options, onComplete) {
668
725
  const extracted = accumulator.getUsage();
669
726
  const model = options.model || extracted.model;
670
727
  const cost = calculateUsageCost(model, extracted.usage);
671
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
728
+ const normalized = normalizeCustomer(options.customer, options.customerId);
672
729
  const usageEvent = {
673
730
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
674
731
  model,
675
732
  provider: "anthropic",
676
733
  usage: extracted.usage,
677
734
  cost,
678
- customerId,
679
- metadata: options.metadata
735
+ customerId: normalized.customerId,
736
+ customerEmail: normalized.customerEmail,
737
+ customer: normalized.customerObj,
738
+ metadata: {
739
+ ...normalized.customerMetadata,
740
+ ...options.metadata
741
+ }
680
742
  };
681
743
  onComplete(usageEvent);
682
744
  if (options.onUsage) {
@@ -705,15 +767,20 @@ function wrapGeminiStream(result, options, onComplete) {
705
767
  const extracted = extractGeminiResponseUsage(lastChunkWithUsage, model);
706
768
  if (extracted) {
707
769
  const cost = calculateUsageCost(extracted.model, extracted.usage);
708
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
770
+ const normalized = normalizeCustomer(options.customer, options.customerId);
709
771
  const event = {
710
772
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
711
773
  model: extracted.model,
712
774
  provider: "google",
713
775
  usage: extracted.usage,
714
776
  cost,
715
- customerId,
716
- metadata: options.metadata
777
+ customerId: normalized.customerId,
778
+ customerEmail: normalized.customerEmail,
779
+ customer: normalized.customerObj,
780
+ metadata: {
781
+ ...normalized.customerMetadata,
782
+ ...options.metadata
783
+ }
717
784
  };
718
785
  onComplete(event);
719
786
  if (options.onUsage) {
@@ -756,7 +823,7 @@ var VibezMeter = class {
756
823
  this.stripeClient = new Stripe(key, {
757
824
  appInfo: {
758
825
  name: "vibezcheck",
759
- version: "0.1.0",
826
+ version: "0.4.2",
760
827
  url: "https://vibezcheck.xyz"
761
828
  }
762
829
  });
@@ -776,15 +843,21 @@ var VibezMeter = class {
776
843
  }
777
844
  const model = options.model || extracted.model;
778
845
  const cost = calculateUsageCost(model, extracted.usage, this.markupMultiplier);
779
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
846
+ const normalized = normalizeCustomer(options.customer, options.customerId);
847
+ const mergedMeta = {
848
+ ...normalized.customerMetadata,
849
+ ...options.metadata
850
+ };
780
851
  const event = {
781
852
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
782
853
  model,
783
854
  provider: extracted.provider,
784
855
  usage: extracted.usage,
785
856
  cost,
786
- customerId,
787
- metadata: options.metadata
857
+ customerId: normalized.customerId,
858
+ customerEmail: normalized.customerEmail,
859
+ customer: normalized.customerObj,
860
+ metadata: mergedMeta
788
861
  };
789
862
  this.batcher.enqueue(event);
790
863
  return event;
@@ -814,15 +887,21 @@ var VibezMeter = class {
814
887
  cachedTokens
815
888
  };
816
889
  const cost = calculateUsageCost(options.model, usage, this.markupMultiplier);
817
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
890
+ const normalized = normalizeCustomer(options.customer, options.customerId);
891
+ const mergedMeta = {
892
+ ...normalized.customerMetadata,
893
+ ...options.metadata
894
+ };
818
895
  const event = {
819
896
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
820
897
  model: options.model,
821
898
  provider: options.provider || "custom",
822
899
  usage,
823
900
  cost,
824
- customerId,
825
- metadata: options.metadata
901
+ customerId: normalized.customerId,
902
+ customerEmail: normalized.customerEmail,
903
+ customer: normalized.customerObj,
904
+ metadata: mergedMeta
826
905
  };
827
906
  this.batcher.enqueue(event);
828
907
  return event;
@@ -869,7 +948,11 @@ function withBilling(model, options = {}) {
869
948
  apiKey: options.stripeApiKey,
870
949
  eventName: options.eventName
871
950
  });
872
- const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
951
+ const normalized = normalizeCustomer(options.customer, options.customerId);
952
+ const customerId = normalized.customerId;
953
+ const customerEmail = normalized.customerEmail;
954
+ const customerObj = normalized.customerObj;
955
+ const customerMetadata = normalized.customerMetadata;
873
956
  const modelId = model.modelId || "unknown-model";
874
957
  const provider = model.provider?.replace(/^@ai-sdk\//, "") || "ai-sdk";
875
958
  const maxCostPerCall = options.maxCostPerCallUSD !== void 0 ? options.maxCostPerCallUSD : 0.5;
@@ -938,6 +1021,12 @@ function withBilling(model, options = {}) {
938
1021
  throw new VibezCircuitBreakerError(budgetEvent);
939
1022
  }
940
1023
  }
1024
+ const mergedMetadata = {
1025
+ ...customerMetadata,
1026
+ ...options.metadata,
1027
+ ...extraMeta,
1028
+ billingMode: options.billing?.mode || "postpaid"
1029
+ };
941
1030
  const event = {
942
1031
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
943
1032
  model: modelId,
@@ -945,11 +1034,9 @@ function withBilling(model, options = {}) {
945
1034
  usage,
946
1035
  cost,
947
1036
  customerId,
948
- metadata: {
949
- ...options.metadata,
950
- ...extraMeta,
951
- billingMode: options.billing?.mode || "postpaid"
952
- }
1037
+ customerEmail,
1038
+ customer: customerObj,
1039
+ metadata: mergedMetadata
953
1040
  };
954
1041
  meter.recordUsage({
955
1042
  model: modelId,
@@ -959,11 +1046,84 @@ function withBilling(model, options = {}) {
959
1046
  reasoningTokens: usage.reasoningTokens,
960
1047
  cachedTokens: usage.cachedTokens,
961
1048
  customerId,
1049
+ customer: customerObj,
962
1050
  metadata: event.metadata
963
1051
  });
964
1052
  if (options.onUsage) {
965
1053
  options.onUsage(event);
966
1054
  }
1055
+ if (options.database) {
1056
+ const dbTarget = options.database;
1057
+ const executeDbWrite = async () => {
1058
+ try {
1059
+ 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;
1060
+ if (supabaseClient) {
1061
+ const tableName = dbTarget?.table || "vibez_usage";
1062
+ await supabaseClient.from(tableName).insert({
1063
+ customer_id: customerId,
1064
+ user_id: customerObj?.userId || customerId,
1065
+ model: modelId,
1066
+ input_tokens: usage.inputTokens,
1067
+ output_tokens: usage.outputTokens,
1068
+ total_tokens: usage.totalTokens,
1069
+ cached_tokens: usage.cachedTokens,
1070
+ reasoning_tokens: usage.reasoningTokens,
1071
+ cost_usd: cost.totalUSD,
1072
+ created_at: event.timestamp,
1073
+ metadata: event.metadata
1074
+ });
1075
+ return;
1076
+ }
1077
+ if (typeof dbTarget === "object" && dbTarget !== null && typeof dbTarget.save === "function") {
1078
+ await dbTarget.save(event);
1079
+ return;
1080
+ }
1081
+ if (typeof dbTarget === "function") {
1082
+ await dbTarget(event);
1083
+ return;
1084
+ }
1085
+ } catch (err) {
1086
+ console.warn(`[vibezcheck] Database record failed safely:`, err?.message || err);
1087
+ }
1088
+ };
1089
+ if (typeof globalThis !== "undefined" && typeof globalThis.after === "function") {
1090
+ globalThis.after(() => executeDbWrite());
1091
+ } else {
1092
+ executeDbWrite().catch(() => {
1093
+ });
1094
+ }
1095
+ }
1096
+ if (options.billing?.charge && typeof options.billing.charge === "function") {
1097
+ const chargeFn = options.billing.charge;
1098
+ const executeCharge = async () => {
1099
+ try {
1100
+ await chargeFn(cost.totalUSD, event);
1101
+ } catch (err) {
1102
+ console.warn(`[vibezcheck] Custom charge handler failed safely:`, err?.message || err);
1103
+ }
1104
+ };
1105
+ if (typeof globalThis !== "undefined" && typeof globalThis.after === "function") {
1106
+ globalThis.after(() => executeCharge());
1107
+ } else {
1108
+ executeCharge().catch(() => {
1109
+ });
1110
+ }
1111
+ } else if (options.billing?.provider && typeof options.billing.provider === "object" && typeof options.billing.provider.charge === "function") {
1112
+ const providerCharge = options.billing.provider.charge;
1113
+ const executeCharge = async () => {
1114
+ try {
1115
+ await providerCharge(cost.totalUSD, event);
1116
+ } catch (err) {
1117
+ console.warn(`[vibezcheck] Payment provider charge failed safely:`, err?.message || err);
1118
+ }
1119
+ };
1120
+ if (typeof globalThis !== "undefined" && typeof globalThis.after === "function") {
1121
+ globalThis.after(() => executeCharge());
1122
+ } else {
1123
+ executeCharge().catch(() => {
1124
+ });
1125
+ }
1126
+ }
967
1127
  scheduleFlush();
968
1128
  };
969
1129
  const handler = {
@@ -1011,6 +1171,8 @@ function withBilling(model, options = {}) {
1011
1171
  controller.enqueue(chunk);
1012
1172
  if (chunk.type === "text-delta" && chunk.textDelta) {
1013
1173
  accumulatedChars += chunk.textDelta.length;
1174
+ } else if (chunk.type === "reasoning" && (chunk.textDelta || chunk.reasoning)) {
1175
+ accumulatedChars += (chunk.textDelta || chunk.reasoning || "").length;
1014
1176
  }
1015
1177
  if (chunk.type === "finish" && chunk.usage) {
1016
1178
  streamCompleted = true;
@@ -1209,7 +1371,7 @@ var CustomerManager = class {
1209
1371
  this.cache = new CustomerCache(options.cacheTtlMs);
1210
1372
  }
1211
1373
  /**
1212
- * Retrieves existing Stripe Customer or automatically provisions a new one
1374
+ * Retrieves existing Stripe Customer or automatically provisions a new one with full metadata
1213
1375
  */
1214
1376
  async getOrCreate(params) {
1215
1377
  const cacheKey = params.userId || params.email;
@@ -1250,20 +1412,66 @@ var CustomerManager = class {
1250
1412
  return { id: customer.id, isNew: false, customer };
1251
1413
  }
1252
1414
  }
1415
+ const orgId = params.orgId || params.organizationId;
1416
+ const teamId = params.teamId || params.workspaceId;
1417
+ const stripeMetadata = {
1418
+ vibez_user_id: params.userId || "",
1419
+ created_by: "vibezcheck"
1420
+ };
1421
+ if (orgId) stripeMetadata.org_id = String(orgId);
1422
+ if (params.orgName) stripeMetadata.org_name = String(params.orgName);
1423
+ if (teamId) stripeMetadata.team_id = String(teamId);
1424
+ if (params.plan) stripeMetadata.plan = String(params.plan);
1425
+ if (params.tier) stripeMetadata.tier = String(params.tier);
1426
+ if (params.role) stripeMetadata.role = String(params.role);
1427
+ if (params.metadata) {
1428
+ for (const [k, v] of Object.entries(params.metadata)) {
1429
+ if (v !== void 0 && v !== null) {
1430
+ stripeMetadata[k] = String(v);
1431
+ }
1432
+ }
1433
+ }
1253
1434
  const newCustomer = await this.stripe.customers.create({
1254
1435
  email: params.email,
1255
1436
  name: params.name,
1256
- metadata: {
1257
- vibez_user_id: params.userId,
1258
- created_by: "vibezcheck",
1259
- ...params.metadata || {}
1260
- }
1437
+ phone: params.phone,
1438
+ metadata: stripeMetadata
1261
1439
  });
1262
1440
  if (cacheKey) this.cache.set(cacheKey, newCustomer.id);
1263
1441
  if (params.userId) this.cache.set(params.userId, newCustomer.id);
1264
1442
  if (params.email) this.cache.set(params.email, newCustomer.id);
1265
1443
  return { id: newCustomer.id, isNew: true, customer: newCustomer };
1266
1444
  }
1445
+ /**
1446
+ * Updates an existing Stripe Customer's information and metadata
1447
+ */
1448
+ async updateCustomer(customerId, params) {
1449
+ const updatePayload = {};
1450
+ if (params.email) updatePayload.email = params.email;
1451
+ if (params.name) updatePayload.name = params.name;
1452
+ if (params.phone) updatePayload.phone = params.phone;
1453
+ const metadataToUpdate = {};
1454
+ if (params.userId) metadataToUpdate.vibez_user_id = params.userId;
1455
+ const orgId = params.orgId || params.organizationId;
1456
+ if (orgId) metadataToUpdate.org_id = String(orgId);
1457
+ if (params.orgName) metadataToUpdate.org_name = String(params.orgName);
1458
+ const teamId = params.teamId || params.workspaceId;
1459
+ if (teamId) metadataToUpdate.team_id = String(teamId);
1460
+ if (params.plan) metadataToUpdate.plan = String(params.plan);
1461
+ if (params.tier) metadataToUpdate.tier = String(params.tier);
1462
+ if (params.role) metadataToUpdate.role = String(params.role);
1463
+ if (params.metadata) {
1464
+ for (const [k, v] of Object.entries(params.metadata)) {
1465
+ if (v !== void 0 && v !== null) {
1466
+ metadataToUpdate[k] = String(v);
1467
+ }
1468
+ }
1469
+ }
1470
+ if (Object.keys(metadataToUpdate).length > 0) {
1471
+ updatePayload.metadata = metadataToUpdate;
1472
+ }
1473
+ return await this.stripe.customers.update(customerId, updatePayload);
1474
+ }
1267
1475
  /**
1268
1476
  * Clears in-memory resolution cache
1269
1477
  */
@@ -1568,6 +1776,7 @@ export {
1568
1776
  getModelPricing,
1569
1777
  inspectOpenAIStreamChunk,
1570
1778
  meteredModel,
1779
+ normalizeCustomer,
1571
1780
  normalizeModelKey,
1572
1781
  registerModelPricing,
1573
1782
  trackTokens,