@nehorai/credits 2.2.0 → 3.0.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 (41) hide show
  1. package/dist/adapters/index.cjs +1 -0
  2. package/dist/adapters/index.cjs.map +1 -1
  3. package/dist/adapters/index.d.cts +2 -2
  4. package/dist/adapters/index.d.ts +2 -2
  5. package/dist/adapters/index.js +1 -0
  6. package/dist/adapters/index.js.map +1 -1
  7. package/dist/core/index.cjs +139 -23
  8. package/dist/core/index.cjs.map +1 -1
  9. package/dist/core/index.d.cts +113 -22
  10. package/dist/core/index.d.ts +113 -22
  11. package/dist/core/index.js +128 -21
  12. package/dist/core/index.js.map +1 -1
  13. package/dist/index.cjs +246 -61
  14. package/dist/index.cjs.map +1 -1
  15. package/dist/index.d.cts +4 -4
  16. package/dist/index.d.ts +4 -4
  17. package/dist/index.js +235 -59
  18. package/dist/index.js.map +1 -1
  19. package/dist/repository/index.cjs +183 -41
  20. package/dist/repository/index.cjs.map +1 -1
  21. package/dist/repository/index.d.cts +6 -5
  22. package/dist/repository/index.d.ts +6 -5
  23. package/dist/repository/index.js +183 -41
  24. package/dist/repository/index.js.map +1 -1
  25. package/dist/sdk/index.cjs +1 -0
  26. package/dist/sdk/index.cjs.map +1 -1
  27. package/dist/sdk/index.d.cts +2 -2
  28. package/dist/sdk/index.d.ts +2 -2
  29. package/dist/sdk/index.js +1 -0
  30. package/dist/sdk/index.js.map +1 -1
  31. package/dist/service/index.cjs +25 -16
  32. package/dist/service/index.cjs.map +1 -1
  33. package/dist/service/index.d.cts +2 -2
  34. package/dist/service/index.d.ts +2 -2
  35. package/dist/service/index.js +25 -16
  36. package/dist/service/index.js.map +1 -1
  37. package/dist/{types-MaViK5tD.d.cts → types-C3sxDvNb.d.cts} +29 -6
  38. package/dist/{types-MaViK5tD.d.ts → types-C3sxDvNb.d.ts} +29 -6
  39. package/dist/{types-3c-IAwZI.d.ts → types-CY-oHIMA.d.ts} +19 -3
  40. package/dist/{types-XSjNXO5l.d.cts → types-DOjguhEX.d.cts} +19 -3
  41. package/package.json +1 -1
@@ -99,6 +99,11 @@ interface PortableUserCredits {
99
99
  * Absent means 0. See `planBalanceCap`.
100
100
  */
101
101
  expiredHoldBacking?: number;
102
+ /**
103
+ * Where those credits end among the open holds, so a hold's share needs no write
104
+ * to the hold. Internal to the repositories: `toClientUserCredits` drops it.
105
+ */
106
+ expiredBackingFrontier?: ExpiredBackingFrontier | null;
102
107
  /**
103
108
  * Set when the account was created with a signup-grant claim: the amount
104
109
  * granted, 0 when the claim was already taken. Absent on any other account.
@@ -109,6 +114,21 @@ interface PortableUserCredits {
109
114
  /** Last update timestamp - ISO 8601 */
110
115
  updatedAt: string;
111
116
  }
117
+ /**
118
+ * The newest open hold carrying marked credits (`expiredHoldBacking`).
119
+ *
120
+ * Credits are marked oldest hold first, so a single point describes every share:
121
+ * a hold older than this carries its full amount, this hold carries `share`, and a
122
+ * newer one carries nothing. A marked credit leaves the balance when its hold closes,
123
+ * however it closes. See `allocateExpiredBacking` and `holdExpiredBacking`.
124
+ */
125
+ interface ExpiredBackingFrontier {
126
+ /** The hold's `holdPlacedAt`. */
127
+ holdPlacedAt: string;
128
+ /** The hold's id, which orders holds placed in the same millisecond. */
129
+ id: string;
130
+ share: number;
131
+ }
112
132
  /**
113
133
  * Credit reservation status for two-phase commit
114
134
  */
@@ -148,10 +168,8 @@ interface PortableReservation {
148
168
  */
149
169
  holdPlacedAt?: string;
150
170
  /**
151
- * The part of this hold backed by credits that expired while it was open, and so
152
- * leave the balance when it closes, however it closes. Absent means 0, as on every
153
- * hold placed after the expiry. Open holds' shares sum to the account's
154
- * `expiredHoldBacking`.
171
+ * This hold's share of the marked credits, stored by 2.2.0. A hold placed since carries
172
+ * none and takes its share from the account's frontier. See `holdExpiredBacking`.
155
173
  */
156
174
  expiredBacking?: number;
157
175
  }
@@ -268,8 +286,13 @@ interface PortableJournalEntry {
268
286
  entryType: "debit" | "credit";
269
287
  /** Amount of credits involved */
270
288
  amount: number;
271
- /** Balance after this entry */
289
+ /** `balance + bonusCredits` after this entry */
272
290
  balanceAfter: number;
291
+ /**
292
+ * `balance + bonusCredits` before this entry, read in the same atomic step.
293
+ * Absent on entries written before 3.0.0.
294
+ */
295
+ balanceBefore?: number;
273
296
  /** Source of the credit change */
274
297
  source: CreditSource;
275
298
  /** Reference ID (e.g., reservationId, transactionId) */
@@ -368,4 +391,4 @@ interface WithCreditsOptions {
368
391
  resourceType?: ResourceType;
369
392
  }
370
393
 
371
- export { type AIProviderType as A, type BuiltinTier as B, type CreditOperationType as C, type DeductCreditsResult as D, type JournalReferenceType as J, type MonthlyResetResult as M, type PortableTransaction as P, type ReservationStatus as R, type SubscriptionTier as S, type TierConfig as T, type UsageHistoryEntry as U, type WithCreditsOptions as W, type PortableReservation as a, type CreditSource as b, type PortableUserCredits as c, type PortableUsageLog as d, type SubscriptionExpiryResult as e, type PortableJournalEntry as f, type CreditCheckResult as g, type ResourceType as h, type TransactionType as i, type UsageHistoryResponse as j, calculateAvailableCredits as k, toPortableTimestamp as l, toDate as t };
394
+ export { type AIProviderType as A, type BuiltinTier as B, type CreditOperationType as C, type DeductCreditsResult as D, type ExpiredBackingFrontier as E, type JournalReferenceType as J, type MonthlyResetResult as M, type PortableTransaction as P, type ReservationStatus as R, type SubscriptionTier as S, type TierConfig as T, type UsageHistoryEntry as U, type WithCreditsOptions as W, type PortableReservation as a, type CreditSource as b, type PortableUserCredits as c, type PortableUsageLog as d, type SubscriptionExpiryResult as e, type PortableJournalEntry as f, type CreditCheckResult as g, type ResourceType as h, type TransactionType as i, type UsageHistoryResponse as j, calculateAvailableCredits as k, toPortableTimestamp as l, toDate as t };
@@ -99,6 +99,11 @@ interface PortableUserCredits {
99
99
  * Absent means 0. See `planBalanceCap`.
100
100
  */
101
101
  expiredHoldBacking?: number;
102
+ /**
103
+ * Where those credits end among the open holds, so a hold's share needs no write
104
+ * to the hold. Internal to the repositories: `toClientUserCredits` drops it.
105
+ */
106
+ expiredBackingFrontier?: ExpiredBackingFrontier | null;
102
107
  /**
103
108
  * Set when the account was created with a signup-grant claim: the amount
104
109
  * granted, 0 when the claim was already taken. Absent on any other account.
@@ -109,6 +114,21 @@ interface PortableUserCredits {
109
114
  /** Last update timestamp - ISO 8601 */
110
115
  updatedAt: string;
111
116
  }
117
+ /**
118
+ * The newest open hold carrying marked credits (`expiredHoldBacking`).
119
+ *
120
+ * Credits are marked oldest hold first, so a single point describes every share:
121
+ * a hold older than this carries its full amount, this hold carries `share`, and a
122
+ * newer one carries nothing. A marked credit leaves the balance when its hold closes,
123
+ * however it closes. See `allocateExpiredBacking` and `holdExpiredBacking`.
124
+ */
125
+ interface ExpiredBackingFrontier {
126
+ /** The hold's `holdPlacedAt`. */
127
+ holdPlacedAt: string;
128
+ /** The hold's id, which orders holds placed in the same millisecond. */
129
+ id: string;
130
+ share: number;
131
+ }
112
132
  /**
113
133
  * Credit reservation status for two-phase commit
114
134
  */
@@ -148,10 +168,8 @@ interface PortableReservation {
148
168
  */
149
169
  holdPlacedAt?: string;
150
170
  /**
151
- * The part of this hold backed by credits that expired while it was open, and so
152
- * leave the balance when it closes, however it closes. Absent means 0, as on every
153
- * hold placed after the expiry. Open holds' shares sum to the account's
154
- * `expiredHoldBacking`.
171
+ * This hold's share of the marked credits, stored by 2.2.0. A hold placed since carries
172
+ * none and takes its share from the account's frontier. See `holdExpiredBacking`.
155
173
  */
156
174
  expiredBacking?: number;
157
175
  }
@@ -268,8 +286,13 @@ interface PortableJournalEntry {
268
286
  entryType: "debit" | "credit";
269
287
  /** Amount of credits involved */
270
288
  amount: number;
271
- /** Balance after this entry */
289
+ /** `balance + bonusCredits` after this entry */
272
290
  balanceAfter: number;
291
+ /**
292
+ * `balance + bonusCredits` before this entry, read in the same atomic step.
293
+ * Absent on entries written before 3.0.0.
294
+ */
295
+ balanceBefore?: number;
273
296
  /** Source of the credit change */
274
297
  source: CreditSource;
275
298
  /** Reference ID (e.g., reservationId, transactionId) */
@@ -368,4 +391,4 @@ interface WithCreditsOptions {
368
391
  resourceType?: ResourceType;
369
392
  }
370
393
 
371
- export { type AIProviderType as A, type BuiltinTier as B, type CreditOperationType as C, type DeductCreditsResult as D, type JournalReferenceType as J, type MonthlyResetResult as M, type PortableTransaction as P, type ReservationStatus as R, type SubscriptionTier as S, type TierConfig as T, type UsageHistoryEntry as U, type WithCreditsOptions as W, type PortableReservation as a, type CreditSource as b, type PortableUserCredits as c, type PortableUsageLog as d, type SubscriptionExpiryResult as e, type PortableJournalEntry as f, type CreditCheckResult as g, type ResourceType as h, type TransactionType as i, type UsageHistoryResponse as j, calculateAvailableCredits as k, toPortableTimestamp as l, toDate as t };
394
+ export { type AIProviderType as A, type BuiltinTier as B, type CreditOperationType as C, type DeductCreditsResult as D, type ExpiredBackingFrontier as E, type JournalReferenceType as J, type MonthlyResetResult as M, type PortableTransaction as P, type ReservationStatus as R, type SubscriptionTier as S, type TierConfig as T, type UsageHistoryEntry as U, type WithCreditsOptions as W, type PortableReservation as a, type CreditSource as b, type PortableUserCredits as c, type PortableUsageLog as d, type SubscriptionExpiryResult as e, type PortableJournalEntry as f, type CreditCheckResult as g, type ResourceType as h, type TransactionType as i, type UsageHistoryResponse as j, calculateAvailableCredits as k, toPortableTimestamp as l, toDate as t };
@@ -1,4 +1,4 @@
1
- import { P as PortableTransaction, a as PortableReservation, R as ReservationStatus, C as CreditOperationType, b as CreditSource, J as JournalReferenceType, A as AIProviderType, S as SubscriptionTier, c as PortableUserCredits, d as PortableUsageLog, M as MonthlyResetResult, e as SubscriptionExpiryResult, f as PortableJournalEntry } from './types-MaViK5tD.js';
1
+ import { P as PortableTransaction, a as PortableReservation, R as ReservationStatus, C as CreditOperationType, b as CreditSource, J as JournalReferenceType, A as AIProviderType, S as SubscriptionTier, c as PortableUserCredits, d as PortableUsageLog, M as MonthlyResetResult, e as SubscriptionExpiryResult, f as PortableJournalEntry } from './types-C3sxDvNb.js';
2
2
 
3
3
  /**
4
4
  * Typed outcomes for the V2 reservation boundary.
@@ -326,7 +326,10 @@ interface CreateJournalEntryInput {
326
326
  userId: string;
327
327
  entryType: "debit" | "credit";
328
328
  amount: number;
329
+ /** `balance + bonusCredits` after this entry. */
329
330
  balanceAfter: number;
331
+ /** `balance + bonusCredits` before this entry. */
332
+ balanceBefore?: number;
330
333
  source: CreditSource;
331
334
  referenceId: string;
332
335
  referenceType: JournalReferenceType;
@@ -335,6 +338,18 @@ interface CreateJournalEntryInput {
335
338
  /** Deterministic key making this entry unique per user (V2, optional). */
336
339
  idempotencyKey?: string;
337
340
  }
341
+ /**
342
+ * The journal entry of a one-shot deduct, written in the deduct's own atomic step.
343
+ */
344
+ interface DeductJournalInput {
345
+ referenceId: string;
346
+ description: string;
347
+ /** Defaults to `"operation_commit"`. */
348
+ source?: CreditSource;
349
+ /** Defaults to `"adjustment"`. */
350
+ referenceType?: JournalReferenceType;
351
+ metadata?: Record<string, unknown>;
352
+ }
338
353
  /**
339
354
  * Query options for journal entries
340
355
  */
@@ -556,9 +571,10 @@ interface ICreditRepository extends Partial<ICreditRepositoryV2>, Partial<ICredi
556
571
  * @returns Combined totals (balance + bonusCredits) before and after.
557
572
  * @throws If user has no credits doc or insufficient credits
558
573
  */
559
- deductCreditsAtomic(userId: string, amount: number): Promise<{
574
+ deductCreditsAtomic(userId: string, amount: number, journal?: DeductJournalInput): Promise<{
560
575
  previousBalance: number;
561
576
  newBalance: number;
577
+ journalEntryId?: string;
562
578
  }>;
563
579
  /**
564
580
  * Create a credit transaction *record* — no balance moves.
@@ -678,4 +694,4 @@ declare function supportsIdempotentCredit(repository: ICreditRepository): reposi
678
694
  */
679
695
  declare function toClientUserCredits(credits: PortableUserCredits): PortableUserCredits;
680
696
 
681
- export { type AddCreditsAtomicOptions as A, type CommitOutcome as C, type ExpireOutcome as E, type ICreditRepository as I, type JournalEntryQuery as J, type ReleaseOutcome as R, type TerminalReservationStatus as T, type UsageLogQuery as U, type AddCreditsOutcome as a, type AddCreditsV2Input as b, type CreateJournalEntryInput as c, type CreateReservationInput as d, type CreateTransactionInput as e, type CreateUsageLogInput as f, type CreditBalanceUpdate as g, type CreditPolicyOptions as h, type CreditRepositoryFactory as i, type ExpireReservationV2Options as j, type ICreditRepositoryCreditsV2 as k, type ICreditRepositoryV2 as l, type ReservationTransitionOptions as m, type ReserveCreditsV2Input as n, type ReserveOutcome as o, type TierUpdateInput as p, isCreditedOutcome as q, isReservedOutcome as r, isWinningOutcome as s, reservationJournalKey as t, supportsCreditsV2 as u, supportsIdempotentCredit as v, toClientUserCredits as w };
697
+ export { type AddCreditsAtomicOptions as A, type CommitOutcome as C, type DeductJournalInput as D, type ExpireOutcome as E, type ICreditRepository as I, type JournalEntryQuery as J, type ReleaseOutcome as R, type TerminalReservationStatus as T, type UsageLogQuery as U, type AddCreditsOutcome as a, type AddCreditsV2Input as b, type CreateJournalEntryInput as c, type CreateReservationInput as d, type CreateTransactionInput as e, type CreateUsageLogInput as f, type CreditBalanceUpdate as g, type CreditPolicyOptions as h, type CreditRepositoryFactory as i, type ExpireReservationV2Options as j, type ICreditRepositoryCreditsV2 as k, type ICreditRepositoryV2 as l, type ReservationTransitionOptions as m, type ReserveCreditsV2Input as n, type ReserveOutcome as o, type TierUpdateInput as p, isCreditedOutcome as q, isReservedOutcome as r, isWinningOutcome as s, reservationJournalKey as t, supportsCreditsV2 as u, supportsIdempotentCredit as v, toClientUserCredits as w };
@@ -1,4 +1,4 @@
1
- import { P as PortableTransaction, a as PortableReservation, R as ReservationStatus, C as CreditOperationType, b as CreditSource, J as JournalReferenceType, A as AIProviderType, S as SubscriptionTier, c as PortableUserCredits, d as PortableUsageLog, M as MonthlyResetResult, e as SubscriptionExpiryResult, f as PortableJournalEntry } from './types-MaViK5tD.cjs';
1
+ import { P as PortableTransaction, a as PortableReservation, R as ReservationStatus, C as CreditOperationType, b as CreditSource, J as JournalReferenceType, A as AIProviderType, S as SubscriptionTier, c as PortableUserCredits, d as PortableUsageLog, M as MonthlyResetResult, e as SubscriptionExpiryResult, f as PortableJournalEntry } from './types-C3sxDvNb.cjs';
2
2
 
3
3
  /**
4
4
  * Typed outcomes for the V2 reservation boundary.
@@ -326,7 +326,10 @@ interface CreateJournalEntryInput {
326
326
  userId: string;
327
327
  entryType: "debit" | "credit";
328
328
  amount: number;
329
+ /** `balance + bonusCredits` after this entry. */
329
330
  balanceAfter: number;
331
+ /** `balance + bonusCredits` before this entry. */
332
+ balanceBefore?: number;
330
333
  source: CreditSource;
331
334
  referenceId: string;
332
335
  referenceType: JournalReferenceType;
@@ -335,6 +338,18 @@ interface CreateJournalEntryInput {
335
338
  /** Deterministic key making this entry unique per user (V2, optional). */
336
339
  idempotencyKey?: string;
337
340
  }
341
+ /**
342
+ * The journal entry of a one-shot deduct, written in the deduct's own atomic step.
343
+ */
344
+ interface DeductJournalInput {
345
+ referenceId: string;
346
+ description: string;
347
+ /** Defaults to `"operation_commit"`. */
348
+ source?: CreditSource;
349
+ /** Defaults to `"adjustment"`. */
350
+ referenceType?: JournalReferenceType;
351
+ metadata?: Record<string, unknown>;
352
+ }
338
353
  /**
339
354
  * Query options for journal entries
340
355
  */
@@ -556,9 +571,10 @@ interface ICreditRepository extends Partial<ICreditRepositoryV2>, Partial<ICredi
556
571
  * @returns Combined totals (balance + bonusCredits) before and after.
557
572
  * @throws If user has no credits doc or insufficient credits
558
573
  */
559
- deductCreditsAtomic(userId: string, amount: number): Promise<{
574
+ deductCreditsAtomic(userId: string, amount: number, journal?: DeductJournalInput): Promise<{
560
575
  previousBalance: number;
561
576
  newBalance: number;
577
+ journalEntryId?: string;
562
578
  }>;
563
579
  /**
564
580
  * Create a credit transaction *record* — no balance moves.
@@ -678,4 +694,4 @@ declare function supportsIdempotentCredit(repository: ICreditRepository): reposi
678
694
  */
679
695
  declare function toClientUserCredits(credits: PortableUserCredits): PortableUserCredits;
680
696
 
681
- export { type AddCreditsAtomicOptions as A, type CommitOutcome as C, type ExpireOutcome as E, type ICreditRepository as I, type JournalEntryQuery as J, type ReleaseOutcome as R, type TerminalReservationStatus as T, type UsageLogQuery as U, type AddCreditsOutcome as a, type AddCreditsV2Input as b, type CreateJournalEntryInput as c, type CreateReservationInput as d, type CreateTransactionInput as e, type CreateUsageLogInput as f, type CreditBalanceUpdate as g, type CreditPolicyOptions as h, type CreditRepositoryFactory as i, type ExpireReservationV2Options as j, type ICreditRepositoryCreditsV2 as k, type ICreditRepositoryV2 as l, type ReservationTransitionOptions as m, type ReserveCreditsV2Input as n, type ReserveOutcome as o, type TierUpdateInput as p, isCreditedOutcome as q, isReservedOutcome as r, isWinningOutcome as s, reservationJournalKey as t, supportsCreditsV2 as u, supportsIdempotentCredit as v, toClientUserCredits as w };
697
+ export { type AddCreditsAtomicOptions as A, type CommitOutcome as C, type DeductJournalInput as D, type ExpireOutcome as E, type ICreditRepository as I, type JournalEntryQuery as J, type ReleaseOutcome as R, type TerminalReservationStatus as T, type UsageLogQuery as U, type AddCreditsOutcome as a, type AddCreditsV2Input as b, type CreateJournalEntryInput as c, type CreateReservationInput as d, type CreateTransactionInput as e, type CreateUsageLogInput as f, type CreditBalanceUpdate as g, type CreditPolicyOptions as h, type CreditRepositoryFactory as i, type ExpireReservationV2Options as j, type ICreditRepositoryCreditsV2 as k, type ICreditRepositoryV2 as l, type ReservationTransitionOptions as m, type ReserveCreditsV2Input as n, type ReserveOutcome as o, type TierUpdateInput as p, isCreditedOutcome as q, isReservedOutcome as r, isWinningOutcome as s, reservationJournalKey as t, supportsCreditsV2 as u, supportsIdempotentCredit as v, toClientUserCredits as w };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nehorai/credits",
3
- "version": "2.2.0",
3
+ "version": "3.0.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",