@chatarmin/os 1.5.9 → 1.8.0

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 (69) hide show
  1. package/README.md +417 -8
  2. package/dist/billing/client.d.ts +8 -0
  3. package/dist/billing/client.d.ts.map +1 -0
  4. package/dist/billing/client.js +46 -0
  5. package/dist/billing/index.d.ts +3 -0
  6. package/dist/billing/index.d.ts.map +1 -0
  7. package/dist/billing/index.js +1 -0
  8. package/dist/billing/types.d.ts +201 -0
  9. package/dist/billing/types.d.ts.map +1 -0
  10. package/dist/billing/usage-breakdown-types.d.ts +132 -0
  11. package/dist/billing/usage-breakdown-types.d.ts.map +1 -0
  12. package/dist/companies/client.d.ts +1 -1
  13. package/dist/companies/client.d.ts.map +1 -1
  14. package/dist/features/client.d.ts +8 -0
  15. package/dist/features/client.d.ts.map +1 -0
  16. package/dist/features/client.js +18 -0
  17. package/dist/features/index.d.ts +3 -0
  18. package/dist/features/index.d.ts.map +1 -0
  19. package/dist/features/index.js +1 -0
  20. package/dist/features/types.d.ts +184 -0
  21. package/dist/features/types.d.ts.map +1 -0
  22. package/dist/index.d.ts +29 -760
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +8 -361
  25. package/dist/webhooks/constants.d.ts +5 -0
  26. package/dist/webhooks/constants.d.ts.map +1 -0
  27. package/dist/webhooks/constants.js +4 -0
  28. package/dist/webhooks/construct-event.d.ts +3 -0
  29. package/dist/webhooks/construct-event.d.ts.map +1 -0
  30. package/dist/webhooks/construct-event.js +64 -0
  31. package/dist/webhooks/errors.d.ts +4 -0
  32. package/dist/webhooks/errors.d.ts.map +1 -0
  33. package/dist/webhooks/errors.js +6 -0
  34. package/dist/webhooks/generate-test-headers.d.ts +3 -0
  35. package/dist/webhooks/generate-test-headers.d.ts.map +1 -0
  36. package/dist/webhooks/generate-test-headers.js +11 -0
  37. package/dist/webhooks/index.d.ts +33 -0
  38. package/dist/webhooks/index.d.ts.map +1 -0
  39. package/dist/webhooks/index.js +31 -0
  40. package/dist/webhooks/read-header.d.ts +3 -0
  41. package/dist/webhooks/read-header.d.ts.map +1 -0
  42. package/dist/webhooks/read-header.js +16 -0
  43. package/dist/webhooks/signing.d.ts +4 -0
  44. package/dist/webhooks/signing.d.ts.map +1 -0
  45. package/dist/webhooks/signing.js +16 -0
  46. package/dist/webhooks/types.d.ts +30 -0
  47. package/dist/webhooks/types.d.ts.map +1 -0
  48. package/package.json +7 -1
  49. package/dist/billing-get-dashboard-context.d.ts +0 -16
  50. package/dist/billing-get-dashboard-context.d.ts.map +0 -1
  51. package/dist/billing-get-dashboard-context.js +0 -7
  52. package/dist/chatarmin-os-billing-namespace.d.ts +0 -122
  53. package/dist/chatarmin-os-billing-namespace.d.ts.map +0 -1
  54. package/dist/chatarmin-os-billing-namespace.js +0 -24
  55. package/dist/features-list-for-product.d.ts +0 -26
  56. package/dist/features-list-for-product.d.ts.map +0 -1
  57. package/dist/features-list-for-product.js +0 -17
  58. package/dist/types/billing-dashboard-context.d.ts +0 -33
  59. package/dist/types/billing-dashboard-context.d.ts.map +0 -1
  60. package/dist/types/chatarmin-os-billing-inputs.d.ts +0 -90
  61. package/dist/types/chatarmin-os-billing-inputs.d.ts.map +0 -1
  62. package/dist/types/feature-access-list-item.d.ts +0 -7
  63. package/dist/types/feature-access-list-item.d.ts.map +0 -1
  64. package/dist/types/product-feature-catalog.d.ts +0 -24
  65. package/dist/types/product-feature-catalog.d.ts.map +0 -1
  66. /package/dist/{types/billing-dashboard-context.js → billing/types.js} +0 -0
  67. /package/dist/{types/chatarmin-os-billing-inputs.js → billing/usage-breakdown-types.js} +0 -0
  68. /package/dist/{types/feature-access-list-item.js → features/types.js} +0 -0
  69. /package/dist/{types/product-feature-catalog.js → webhooks/types.js} +0 -0
package/dist/index.d.ts CHANGED
@@ -1,6 +1,12 @@
1
- import type { AppRouter } from "@chatarmin/api";
1
+ import type { AppRouter } from "@chatarmin/api/trpc-types";
2
2
  import { type CompaniesClient } from "./companies";
3
+ import { type BillingClient } from "./billing";
4
+ import { type FeaturesClient } from "./features";
5
+ export { webhooks, constructProductWebhookEvent, generateTestWebhookHeaders, OsWebhookSignatureVerificationError, WEBHOOK_ID_HEADER, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_TIMESTAMP_HEADER, } from "./webhooks";
6
+ export type { ConstructProductWebhookEventOptions, ProductWebhookEventPayload, ProductWebhookHeaders, VerifiedProductWebhookEvent, } from "./webhooks";
3
7
  export type { CompaniesClient, CompanyAssignee, CompanyAssignees, CreateCompanyInput, CreateCompanyResult, GetCompanyInput, LinkedCompany, ListCompaniesInput, ListCompaniesResult, ListLinkedCompaniesInput, ListLinkedCompaniesResult, ProductCompany, UpdateCompanyResult, } from "./companies";
8
+ export type { BillingClient, BillingInvoiceSummary, BillingOverview, BillingProfileSummary, BillingSubscriptionSummary, ClaimCheckoutInput, ClaimCheckoutResult, CreateBillingPortalSessionInput, EnsureFeatureSubscriptionInput, EnsureFeatureSubscriptionResult, GetBillingOverviewInput, GetUsageInput, LinkSubscriptionInput, LinkSubscriptionResult, ListBillingInvoicesOptions, PeriodOffsetRange, RelinkBillingProfileInput, ResolveInvoicePaymentUrlInput, SetUsageInput, SyncCompanyFromStripeInput, SyncCompanyFromStripeResult, UsageAggregate, UsageCycleBucket, UsageCycleSummary, UsagePoint, UsageQueryResult, UsageRecord, UsageSubscriptionItem, UsageResult, } from "./billing";
9
+ export type { ExternalOrgFeatureAccessItem, FeatureAccessByExternalOrgIdResult, FeatureAccessItem, FeatureBillingDetails, FeatureCheckByExternalOrgIdResult, FeatureCheckInput, FeatureCheckResult, FeatureSetAccessInput, FeatureSetAccessResult, FeaturesClient, ListFeatureAccessInput, } from "./features";
4
10
  /**
5
11
  * Configuration options for the ChatarminOS SDK client.
6
12
  */
@@ -40,202 +46,6 @@ export declare class ChatarminOSError extends Error {
40
46
  details: ChatarminOSErrorDetails;
41
47
  };
42
48
  }
43
- /**
44
- * Input for checking feature access.
45
- */
46
- export interface FeatureCheckInput {
47
- /** Company ID to check */
48
- companyId: string;
49
- /** Feature code (e.g., "ai_credit", "whatsapp_messages") */
50
- featureCode: string;
51
- /**
52
- * Verify Stripe subscription is active for this feature.
53
- * When true, the response includes subscription validation fields
54
- * and `canUse` will be false if no active subscription exists.
55
- * @default false
56
- */
57
- verifySubscription?: boolean;
58
- }
59
- /**
60
- * Billing details for a feature (when includeBillingDetails: true)
61
- */
62
- export interface FeatureBillingDetails {
63
- /** Stripe subscription item ID */
64
- stripeSubscriptionItemId: string | null;
65
- /** Current billing period start */
66
- currentPeriodStart: Date | null;
67
- /** Current billing period end */
68
- currentPeriodEnd: Date | null;
69
- /** Unit amount in cents (e.g., 100 = $1.00) */
70
- unitAmount: number | null;
71
- /** Currency code (e.g., "usd") */
72
- currency: string | null;
73
- /** Billing interval (e.g., "month", "year") */
74
- intervalUnit: string | null;
75
- /** Usage type (e.g., "metered", "licensed") */
76
- usageType: string | null;
77
- /** Subscription status (e.g., "active", "trialing") */
78
- subscriptionStatus: string | null;
79
- }
80
- /**
81
- * Input for listing feature access.
82
- */
83
- export interface ListFeatureAccessInput {
84
- /** Company ID */
85
- companyId: string;
86
- /**
87
- * Include billing details (Stripe price, usage, billing period).
88
- * Adds a `billing` field to each feature with subscription linkage info.
89
- * @default false
90
- */
91
- includeBillingDetails?: boolean;
92
- }
93
- /**
94
- * Result of a feature access check.
95
- */
96
- export interface FeatureCheckResult {
97
- /** Feature code */
98
- featureCode: string;
99
- /** Whether the feature is enabled for this company */
100
- isEnabled: boolean;
101
- /** Whether this feature tracks quantity/usage */
102
- hasQuantity: boolean;
103
- /** Current usage count (for quantity-based features) */
104
- currentUsage: number;
105
- /** Maximum allowed quantity (null = unlimited) */
106
- quantityLimit: number | null;
107
- /** Included/free quantity before overage charges */
108
- includedQuantity: number;
109
- /** Remaining quantity before limit (null = unlimited) */
110
- remaining: number | null;
111
- /** Whether usage limit has been reached */
112
- isAtLimit: boolean;
113
- /** Whether the company can use this feature (enabled + not at limit + subscription if verified) */
114
- canUse: boolean;
115
- /** Whether an active Stripe subscription exists for this feature */
116
- hasActiveSubscription?: boolean;
117
- /** Stripe subscription status (active, trialing, canceled, etc.) */
118
- subscriptionStatus?: string | null;
119
- /** Stripe price ID from the subscription item */
120
- stripePriceId?: string | null;
121
- /** Stripe subscription ID */
122
- stripeSubscriptionId?: string | null;
123
- }
124
- /**
125
- * Input for updating feature access with partial config merge.
126
- */
127
- export interface FeatureSetAccessInput {
128
- /** Company ID to update (either this or externalOrgId required) */
129
- companyId?: string;
130
- /** Your product's external org ID (either this or companyId required) */
131
- externalOrgId?: string;
132
- /** Feature code to update */
133
- featureCode: string;
134
- /** Enable or disable the feature */
135
- isEnabled?: boolean;
136
- /** Maximum quantity allowed (for quantity-based features) */
137
- quantityLimit?: number | null;
138
- /** Included quantity before billing kicks in */
139
- includedQuantity?: number | null;
140
- /**
141
- * Custom config values to merge with existing config.
142
- * Only specified fields will be updated - other fields remain unchanged.
143
- */
144
- config?: Record<string, unknown>;
145
- /** Source of the access change */
146
- source?: "subscription" | "manual" | "trial" | "api";
147
- /** When the access expires (null for no expiration) */
148
- validUntil?: Date | null;
149
- }
150
- export interface SyncCompanyFromStripeInput {
151
- companyId: string;
152
- stripeCustomerId?: string;
153
- forceReassign?: boolean;
154
- }
155
- export interface SyncCompanyFromStripeResult {
156
- billingProfileId: string;
157
- stripeCustomerId: string;
158
- subscriptionsSynced: number;
159
- subscriptionItemsSynced: number;
160
- featuresLinked: number;
161
- matchedMeters: number;
162
- }
163
- export interface EnsureFeatureSubscriptionInput {
164
- companyId: string;
165
- featureCode: string;
166
- stripeSubscriptionId?: string;
167
- }
168
- export interface RelinkBillingProfileInput {
169
- companyId: string;
170
- stripeCustomerId: string;
171
- }
172
- export interface EnsureFeatureSubscriptionResult {
173
- success: boolean;
174
- subscriptionItemId: string | null;
175
- stripePriceId: string | null;
176
- stripeSubscriptionId: string | null;
177
- action: "already_linked" | "linked_existing" | "created_new" | "failed";
178
- error?: string;
179
- }
180
- /**
181
- * Input for setting usage to an absolute value.
182
- * Use for corrections when you know what the total usage SHOULD be.
183
- */
184
- export interface SetUsageInput {
185
- /** Company ID */
186
- companyId: string;
187
- /** Feature code */
188
- featureCode: string;
189
- /** The absolute value to set usage to (must be >= 0) */
190
- absoluteValue: number;
191
- /** Reason for the set operation (required for audit trail) */
192
- reason: string;
193
- /** Admin user who performed the operation */
194
- performedBy?: string;
195
- /** Additional metadata (e.g., { ticketId: "SUPPORT-123" }) */
196
- metadata?: Record<string, unknown>;
197
- }
198
- /**
199
- * Result from usage operations.
200
- */
201
- export interface UsageResult {
202
- success: boolean;
203
- featureCode: string;
204
- previousUsage: number;
205
- currentUsage: number;
206
- delta: number;
207
- quantityLimit: number | null;
208
- remaining: number | null;
209
- isAtLimit: boolean;
210
- includedQuantity: number;
211
- billableQuantity: number;
212
- meterId: string;
213
- eventId: string | null;
214
- }
215
- /**
216
- * Input for claiming a Stripe checkout subscription.
217
- */
218
- export interface ClaimCheckoutInput {
219
- /** Company ID to link the subscription to */
220
- companyId: string;
221
- /** Stripe checkout session ID (cs_xxx) */
222
- checkoutSessionId: string;
223
- }
224
- /**
225
- * Input for linking an existing Stripe subscription.
226
- */
227
- export interface LinkSubscriptionInput {
228
- /** Company ID to link the subscription to */
229
- companyId: string;
230
- /** Stripe subscription ID (sub_xxx) */
231
- stripeSubscriptionId: string;
232
- /** Stripe customer ID (cus_xxx) - fetched from subscription if not provided */
233
- stripeCustomerId?: string;
234
- /** Tier code to apply features from (e.g., "free", "pro") */
235
- tierCode?: string;
236
- /** Display name for the subscription */
237
- displayName?: string;
238
- }
239
49
  /**
240
50
  * Input for applying a tier to a company.
241
51
  */
@@ -413,6 +223,10 @@ export interface OnboardResult {
413
223
  * @see https://os.chatarmin.com/docs for full documentation
414
224
  */
415
225
  export declare class ChatarminOS {
226
+ static webhooks: {
227
+ constructEvent: typeof import("./webhooks").constructProductWebhookEvent;
228
+ generateTestHeaders: typeof import("./webhooks").generateTestWebhookHeaders;
229
+ };
416
230
  private client;
417
231
  private call;
418
232
  /**
@@ -518,12 +332,12 @@ export declare class ChatarminOS {
518
332
  } & ContactInput) => Promise<{
519
333
  isNew: boolean;
520
334
  id: string;
521
- role: string | null;
335
+ metadata: unknown;
522
336
  created_at: Date;
523
337
  updated_at: Date;
338
+ role: string | null;
524
339
  short_id: string | null;
525
340
  name: string | null;
526
- metadata: unknown;
527
341
  is_primary: boolean;
528
342
  avatar_url: string | null;
529
343
  }>;
@@ -572,12 +386,12 @@ export declare class ChatarminOS {
572
386
  */
573
387
  list: (companyId: string, primaryOnly?: boolean) => Promise<{
574
388
  id: string;
575
- role: string | null;
389
+ metadata: unknown;
576
390
  created_at: Date;
577
391
  updated_at: Date;
392
+ role: string | null;
578
393
  short_id: string | null;
579
394
  name: string | null;
580
- metadata: unknown;
581
395
  is_primary: boolean;
582
396
  avatar_url: string | null;
583
397
  }[]>;
@@ -605,554 +419,9 @@ export declare class ChatarminOS {
605
419
  * }
606
420
  * ```
607
421
  */
608
- get features(): {
609
- /**
610
- * Check if a company has access to a specific feature.
611
- *
612
- * Returns detailed access information including whether the feature
613
- * is enabled, current usage, limits, and whether they can use more.
614
- *
615
- * @param input - Check parameters
616
- * @param input.companyId - Company ID to check
617
- * @param input.featureCode - Feature code (e.g., "ai_credit")
618
- * @returns Detailed feature access information
619
- *
620
- * @example
621
- * ```typescript
622
- * const access = await os.features.check({
623
- * companyId: 'comp_xxx',
624
- * featureCode: 'ai_credit'
625
- * })
626
- *
627
- * console.log({
628
- * enabled: access.isEnabled,
629
- * canUse: access.canUse,
630
- * usage: access.currentUsage,
631
- * limit: access.quantityLimit,
632
- * remaining: access.remaining,
633
- * included: access.includedQuantity
634
- * })
635
- * ```
636
- */
637
- check: (input: FeatureCheckInput) => Promise<FeatureCheckResult>;
638
- /**
639
- * List all features and their access status for a company.
640
- *
641
- * Returns a complete list of features with their current status,
642
- * useful for displaying a company's full feature set or building
643
- * a feature comparison view.
644
- *
645
- * @param input - Query parameters
646
- * @returns Array of feature access records
647
- *
648
- * @example Basic usage
649
- * ```typescript
650
- * const features = await os.features.listAccess({ companyId: company.id })
651
- *
652
- * for (const f of features) {
653
- * console.log(`${f.featureName}: ${f.isEnabled ? '✓' : '✗'}`)
654
- * if (f.quantityLimit) {
655
- * console.log(` Usage: ${f.currentUsage} / ${f.quantityLimit}`)
656
- * }
657
- * }
658
- * ```
659
- *
660
- * @example With billing details
661
- * ```typescript
662
- * const features = await os.features.listAccess({
663
- * companyId: company.id,
664
- * includeBillingDetails: true
665
- * })
666
- *
667
- * for (const f of features) {
668
- * console.log(`${f.featureName}:`)
669
- * console.log(` Usage: ${f.currentUsage} / ${f.quantityLimit}`)
670
- *
671
- * if (f.billing) {
672
- * console.log(` Price: ${f.billing.unitAmount / 100} ${f.billing.currency}`)
673
- * console.log(` Period: ${f.billing.currentPeriodStart} to ${f.billing.currentPeriodEnd}`)
674
- * console.log(` Status: ${f.billing.subscriptionStatus}`)
675
- * }
676
- * }
677
- * ```
678
- */
679
- listAccess: (input: ListFeatureAccessInput | string) => Promise<({
680
- featureCode: string;
681
- featureName: string;
682
- description: string | null;
683
- hasQuantity: boolean;
684
- isEnabled: boolean;
685
- quantityLimit: number | null;
686
- includedQuantity: number;
687
- currentUsage: number;
688
- source: string;
689
- validUntil: Date | null;
690
- stripeSubscriptionItemId: string | null;
691
- stripePriceId: string | null;
692
- stripeSubscriptionId: string | null;
693
- meterId: string | null;
694
- configValues: Record<string, unknown>;
695
- } | {
696
- billing: {
697
- stripeSubscriptionItemId: string | null;
698
- currentPeriodStart: Date | null;
699
- currentPeriodEnd: Date | null;
700
- unitAmount: number | null;
701
- currency: string | null;
702
- intervalUnit: string | null;
703
- usageType: string | null;
704
- subscriptionStatus: string | null;
705
- };
706
- featureCode: string;
707
- featureName: string;
708
- description: string | null;
709
- hasQuantity: boolean;
710
- isEnabled: boolean;
711
- quantityLimit: number | null;
712
- includedQuantity: number;
713
- currentUsage: number;
714
- source: string;
715
- validUntil: Date | null;
716
- stripeSubscriptionItemId: string | null;
717
- stripePriceId: string | null;
718
- stripeSubscriptionId: string | null;
719
- meterId: string | null;
720
- configValues: Record<string, unknown>;
721
- })[]>;
722
- /**
723
- * Update feature access for a company with partial config merge.
724
- *
725
- * This allows you to update individual feature configs without
726
- * replacing all config values. Only the fields you specify in
727
- * `config` will be updated - other fields remain unchanged.
728
- *
729
- * @param input - Update parameters
730
- * @returns Updated feature access information
731
- *
732
- * @example Update by company ID
733
- * ```typescript
734
- * await os.features.setAccess({
735
- * companyId: 'comp_xxx',
736
- * featureCode: 'ai_credit',
737
- * isEnabled: true,
738
- * config: {
739
- * included_quantity: 500,
740
- * max_quantity: 1000
741
- * }
742
- * })
743
- * ```
744
- *
745
- * @example Update by your product's external org ID
746
- * ```typescript
747
- * await os.features.setAccess({
748
- * externalOrgId: 'org_abc123',
749
- * featureCode: 'ai_credit',
750
- * config: { tier_level: 'premium' }
751
- * })
752
- * ```
753
- *
754
- * @example Partial config update (only updates specified fields)
755
- * ```typescript
756
- * // Existing config: { tier_level: 'basic', max_seats: 5 }
757
- * await os.features.setAccess({
758
- * companyId: 'comp_xxx',
759
- * featureCode: 'team_seats',
760
- * config: { max_seats: 10 } // tier_level remains 'basic'
761
- * })
762
- * // New config: { tier_level: 'basic', max_seats: 10 }
763
- * ```
764
- */
765
- setAccess: (input: FeatureSetAccessInput) => Promise<{
766
- success: boolean;
767
- featureCode: string;
768
- companyId: string;
769
- isEnabled: boolean;
770
- quantityLimit: number | null;
771
- includedQuantity: number | null;
772
- configValues: {
773
- [x: string]: unknown;
774
- };
775
- source: string;
776
- validUntil: Date | null;
777
- created: boolean;
778
- updated: boolean;
779
- }>;
780
- /**
781
- * Get all feature access using your product's external org ID.
782
- *
783
- * Convenience method when you know the external org ID but not
784
- * the ChatarminOS company ID.
785
- *
786
- * @param externalOrgId - Your product's organization ID
787
- * @returns All features with their access status and config values
788
- *
789
- * @example
790
- * ```typescript
791
- * const result = await os.features.getAccessByExternalOrgId('org_abc123')
792
- *
793
- * console.log(`Company: ${result.companyId}`)
794
- * for (const f of result.features) {
795
- * console.log(`${f.featureCode}: ${f.isEnabled}`)
796
- * console.log(` Config:`, f.configValues)
797
- * }
798
- * ```
799
- */
800
- getAccessByExternalOrgId: (externalOrgId: string) => Promise<{
801
- companyId: string;
802
- productLinkId: string;
803
- externalOrgId: string;
804
- features: {
805
- featureCode: string;
806
- featureName: string;
807
- description: string | null;
808
- hasQuantity: boolean;
809
- isEnabled: boolean;
810
- quantityLimit: number | null;
811
- includedQuantity: number;
812
- configValues: Record<string, unknown>;
813
- source: string;
814
- validUntil: Date | null;
815
- }[];
816
- }>;
817
- /**
818
- * Check specific feature access using your product's external org ID.
819
- *
820
- * @param externalOrgId - Your product's organization ID
821
- * @param featureCode - Feature code to check
822
- * @returns Detailed feature access including config values
823
- *
824
- * @example
825
- * ```typescript
826
- * const access = await os.features.checkByExternalOrgId(
827
- * 'org_abc123',
828
- * 'ai_credit'
829
- * )
830
- *
831
- * if (access.canUse) {
832
- * console.log(`Remaining: ${access.remaining}`)
833
- * console.log(`Config:`, access.configValues)
834
- * }
835
- * ```
836
- */
837
- checkByExternalOrgId: (externalOrgId: string, featureCode: string) => Promise<{
838
- featureCode: string;
839
- companyId: string;
840
- externalOrgId: string;
841
- isEnabled: boolean;
842
- hasQuantity: boolean;
843
- currentUsage: number;
844
- quantityLimit: number | null;
845
- includedQuantity: number;
846
- remaining: number | null;
847
- isAtLimit: boolean;
848
- canUse: boolean;
849
- configValues: {};
850
- } | {
851
- configValues: Record<string, unknown>;
852
- featureCode: string;
853
- isEnabled: boolean;
854
- hasQuantity: boolean;
855
- currentUsage: number;
856
- quantityLimit: number | null;
857
- includedQuantity: number;
858
- remaining: number | null;
859
- isAtLimit: boolean;
860
- canUse: boolean;
861
- companyId: string;
862
- productLinkId: string;
863
- externalOrgId: string;
864
- }>;
865
- };
866
- /**
867
- * Billing, usage tracking, and subscription management.
868
- *
869
- * Set feature usage for billing, claim subscriptions from Stripe checkout,
870
- * or link existing Stripe subscriptions to companies.
871
- *
872
- * @example Set Usage
873
- * ```typescript
874
- * await os.billing.setUsage({
875
- * companyId: company.id,
876
- * featureCode: 'ai_credit',
877
- * absoluteValue: 42,
878
- * reason: 'Synced current usage'
879
- * })
880
- * ```
881
- *
882
- * @example Claim Checkout Subscription
883
- * ```typescript
884
- * // After Stripe checkout completes
885
- * const result = await os.billing.claimCheckout({
886
- * companyId: company.id,
887
- * checkoutSessionId: 'cs_xxx'
888
- * })
889
- * ```
890
- *
891
- * @example Link Existing Subscription
892
- * ```typescript
893
- * // When you create subscriptions on your Stripe account
894
- * const result = await os.billing.linkSubscription({
895
- * companyId: company.id,
896
- * stripeSubscriptionId: 'sub_xxx',
897
- * tierCode: 'pro' // Apply tier features
898
- * })
899
- * ```
900
- */
901
- get billing(): {
902
- /**
903
- * Set usage to an absolute value.
904
- *
905
- * Use this for corrections when you know what the total usage SHOULD be.
906
- * Creates a snapshot for audit trail. The delta (difference) is calculated
907
- * automatically and synced to Stripe.
908
- *
909
- * @param input - Set usage parameters
910
- * @returns Result with previous and new usage values
911
- *
912
- * @example Correct usage to 100 (was 150)
913
- * ```typescript
914
- * const result = await os.billing.setUsage({
915
- * companyId: 'comp_xxx',
916
- * featureCode: 'ai_credit',
917
- * absoluteValue: 100,
918
- * reason: 'Corrected after customer support review',
919
- * performedBy: 'admin@company.com'
920
- * })
921
- *
922
- * console.log({
923
- * previousUsage: result.previousUsage, // 150
924
- * currentUsage: result.currentUsage, // 100
925
- * delta: result.delta // -50
926
- * })
927
- * ```
928
- */
929
- setUsage: (input: SetUsageInput) => Promise<UsageResult>;
930
- /**
931
- * Get usage event history for a feature.
932
- *
933
- * Returns the history of usage events including increments, sets,
934
- * and adjustments. Useful for auditing and debugging.
935
- *
936
- * @param companyId - Company ID
937
- * @param featureCode - Feature code to get history for
938
- * @param options - Query options
939
- * @returns Array of usage events
940
- *
941
- * @example
942
- * ```typescript
943
- * const history = await os.billing.getUsageHistory(
944
- * 'comp_xxx',
945
- * 'ai_credit',
946
- * { limit: 20, eventType: 'adjustment' }
947
- * )
948
- *
949
- * for (const event of history) {
950
- * console.log(`${event.event_type}: ${event.quantity} at ${event.event_time}`)
951
- * if (event.adjustment_reason) {
952
- * console.log(` Reason: ${event.adjustment_reason}`)
953
- * }
954
- * }
955
- * ```
956
- */
957
- getUsageHistory: (companyId: string, featureCode: string, options?: {
958
- limit?: number;
959
- includeDelivered?: boolean;
960
- eventType?: "increment" | "set" | "adjustment";
961
- }) => Promise<{
962
- id: string;
963
- created_at: Date;
964
- metadata: unknown;
965
- billing_profile_id: string | null;
966
- source: string;
967
- idempotency_key: string;
968
- event_type: string;
969
- subscription_item_id: string | null;
970
- feature_access_id: string | null;
971
- meter_code: string;
972
- quantity: number;
973
- stripe_price_id: string | null;
974
- event_time: Date;
975
- previous_value: number | null;
976
- adjustment_reason: string | null;
977
- adjusts_event_id: string | null;
978
- stripe_delivery_status: string;
979
- stripe_delivery_attempts: number;
980
- stripe_last_delivery_attempt_at: Date | null;
981
- stripe_event_id: string | null;
982
- stripe_delivery_error: string | null;
983
- delivered_at: Date | null;
984
- reconcile_target_key: string | null;
985
- reconcile_period_start: Date | null;
986
- reconcile_period_end: Date | null;
987
- reconcile_current_stripe_quantity: number | null;
988
- }[]>;
989
- /**
990
- * Claim a subscription from a completed Stripe Checkout Session.
991
- *
992
- * Use this after a user completes Stripe checkout in your product.
993
- * This links the subscription and customer to the company in OS.
994
- *
995
- * @param input - Claim parameters
996
- * @returns Result with subscription ID and claim status
997
- *
998
- * @example
999
- * ```typescript
1000
- * // In your Stripe checkout success handler:
1001
- * const result = await os.billing.claimCheckout({
1002
- * companyId: company.id,
1003
- * checkoutSessionId: session.id // From Stripe callback
1004
- * })
1005
- *
1006
- * if (result.alreadyClaimed) {
1007
- * console.log('Subscription was already claimed')
1008
- * } else {
1009
- * console.log(`Claimed subscription: ${result.subscriptionId}`)
1010
- * }
1011
- * ```
1012
- */
1013
- claimCheckout: (input: ClaimCheckoutInput) => Promise<{
1014
- success: boolean;
1015
- subscriptionId: string;
1016
- alreadyClaimed: boolean;
1017
- status?: undefined;
1018
- } | {
1019
- success: boolean;
1020
- subscriptionId: string;
1021
- alreadyClaimed: boolean;
1022
- status: import("stripe").Stripe.Subscription.Status;
1023
- }>;
1024
- /**
1025
- * Link an existing Stripe subscription to a company.
1026
- *
1027
- * Use this when you create subscriptions on your product's Stripe
1028
- * account and want OS to track them. Optionally applies tier features.
1029
- *
1030
- * @param input - Link parameters
1031
- * @returns Result with subscription ID, link status, and features applied
1032
- *
1033
- * @example
1034
- * ```typescript
1035
- * // After creating subscription on your Stripe account:
1036
- * const result = await os.billing.linkSubscription({
1037
- * companyId: company.id,
1038
- * stripeSubscriptionId: 'sub_xxx',
1039
- * stripeCustomerId: 'cus_xxx', // Optional
1040
- * tierCode: 'pro', // Apply tier features
1041
- * displayName: 'Pro Plan'
1042
- * })
1043
- *
1044
- * console.log({
1045
- * subscriptionId: result.subscriptionId,
1046
- * isNew: !result.alreadyLinked,
1047
- * featuresApplied: result.featuresApplied
1048
- * })
1049
- * ```
1050
- */
1051
- linkSubscription: (input: LinkSubscriptionInput) => Promise<{
1052
- success: boolean;
1053
- subscriptionId: string;
1054
- alreadyLinked: boolean;
1055
- status?: undefined;
1056
- tierApplied?: undefined;
1057
- featuresApplied?: undefined;
1058
- } | {
1059
- success: boolean;
1060
- subscriptionId: string;
1061
- alreadyLinked: boolean;
1062
- status: import("stripe").Stripe.Subscription.Status;
1063
- tierApplied: boolean;
1064
- featuresApplied: number;
1065
- }>;
1066
- /**
1067
- * Get billing status for a company.
1068
- *
1069
- * Quick check for subscription status, active features, and
1070
- * billing profile information.
1071
- *
1072
- * @param companyId - Company ID to check
1073
- * @returns Billing status overview
1074
- *
1075
- * @example
1076
- * ```typescript
1077
- * const status = await os.billing.getStatus(company.id)
1078
- *
1079
- * console.log({
1080
- * hasBilling: status.hasBillingProfile,
1081
- * subscriptions: status.activeSubscriptions.length,
1082
- * features: status.enabledFeaturesCount
1083
- * })
1084
- *
1085
- * // Check active tier
1086
- * if (status.activeSubscriptions[0]?.tier) {
1087
- * console.log(`Current tier: ${status.activeSubscriptions[0].tier.name}`)
1088
- * }
1089
- * ```
1090
- */
1091
- getStatus: (companyId: string) => Promise<{
1092
- hasBillingProfile: boolean;
1093
- stripeCustomerId: string | null;
1094
- billingProfiles: {
1095
- id: string;
1096
- organization_id: string;
1097
- created_at: Date;
1098
- updated_at: Date;
1099
- short_id: string | null;
1100
- name: string;
1101
- metadata: unknown;
1102
- status: string;
1103
- company_id: string | null;
1104
- stripe_customer_id: string;
1105
- link_status: string;
1106
- default_currency: string;
1107
- has_payment_method: boolean;
1108
- subscription_status: string;
1109
- contract_start_at: Date | null;
1110
- contract_end_at: Date | null;
1111
- mrr_cents: number;
1112
- mrr_committed_cents: number;
1113
- mrr_usage_cents: number;
1114
- open_invoices_count: number;
1115
- open_amount_cents: number;
1116
- past_due_invoices_count: number;
1117
- past_due_amount_cents: number;
1118
- max_days_overdue: number;
1119
- last_synced_at: Date | null;
1120
- company_group_id: string | null;
1121
- }[];
1122
- activeSubscriptions: {
1123
- id: string;
1124
- billingProfileId: string;
1125
- status: string;
1126
- stripeSubscriptionId: string | null;
1127
- currentPeriodEnd: Date | null;
1128
- trialEnd: Date | null;
1129
- tier: {
1130
- code: string;
1131
- name: string | null;
1132
- } | null;
1133
- }[];
1134
- enabledFeaturesCount: number;
1135
- }>;
1136
- /**
1137
- * Sync billing profile, subscriptions, and feature access from Stripe.
1138
- */
1139
- syncCompanyFromStripe: (input: SyncCompanyFromStripeInput) => Promise<SyncCompanyFromStripeResult>;
1140
- /**
1141
- * Relink billing profile to a new Stripe customer (destructive).
1142
- */
1143
- relinkBillingProfile: (input: RelinkBillingProfileInput) => Promise<{
1144
- billingProfileId: string;
1145
- stripeCustomerId: string;
1146
- subscriptionsSynced: number;
1147
- subscriptionItemsSynced: number;
1148
- featuresLinked: number;
1149
- matchedMeters: number;
1150
- }>;
1151
- /**
1152
- * Ensure a feature is linked to an active Stripe subscription item.
1153
- */
1154
- ensureFeatureSubscription: (input: EnsureFeatureSubscriptionInput) => Promise<EnsureFeatureSubscriptionResult>;
1155
- };
422
+ get features(): FeaturesClient;
423
+ /** Billing, usage tracking, subscriptions, and customer billing surfaces. */
424
+ get billing(): BillingClient;
1156
425
  /**
1157
426
  * Manual link operations for associating external IDs with companies.
1158
427
  *
@@ -1206,12 +475,12 @@ export declare class ChatarminOS {
1206
475
  externalUserId?: string;
1207
476
  }) => Promise<{
1208
477
  id: string;
478
+ company_id: string | null;
479
+ status: string;
480
+ metadata: unknown;
1209
481
  created_at: Date;
1210
482
  updated_at: Date;
1211
- metadata: unknown;
1212
- status: string;
1213
483
  product_id: string;
1214
- company_id: string | null;
1215
484
  external_org_id: string;
1216
485
  external_user_id: string | null;
1217
486
  confidence: number | null;
@@ -1342,15 +611,15 @@ export declare class ChatarminOS {
1342
611
  configValues: unknown;
1343
612
  }[];
1344
613
  id: string;
614
+ metadata: unknown;
1345
615
  created_at: Date;
1346
616
  updated_at: Date;
1347
617
  name: string;
1348
- metadata: unknown;
1349
- product_id: string;
1350
- code: string;
1351
- is_active: boolean;
1352
618
  description: string | null;
1353
619
  display_order: number;
620
+ code: string;
621
+ is_active: boolean;
622
+ product_id: string;
1354
623
  }[]>;
1355
624
  /**
1356
625
  * Get detailed information about a specific tier.
@@ -1460,15 +729,15 @@ export declare class ChatarminOS {
1460
729
  configValues: unknown;
1461
730
  }[];
1462
731
  id: string;
732
+ metadata: unknown;
1463
733
  created_at: Date;
1464
734
  updated_at: Date;
1465
735
  name: string;
1466
- metadata: unknown;
1467
- product_id: string;
1468
- code: string;
1469
- is_active: boolean;
1470
736
  description: string | null;
1471
737
  display_order: number;
738
+ code: string;
739
+ is_active: boolean;
740
+ product_id: string;
1472
741
  }[]>;
1473
742
  /**
1474
743
  * Get detailed tier information by code.