@nehorai/credits 1.7.0 → 2.1.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 (47) hide show
  1. package/README.md +384 -153
  2. package/dist/adapters/index.cjs +308 -46
  3. package/dist/adapters/index.cjs.map +1 -1
  4. package/dist/adapters/index.d.cts +3 -2
  5. package/dist/adapters/index.d.ts +3 -2
  6. package/dist/adapters/index.js +308 -46
  7. package/dist/adapters/index.js.map +1 -1
  8. package/dist/auth/index.cjs.map +1 -1
  9. package/dist/auth/index.js.map +1 -1
  10. package/dist/core/index.cjs +700 -76
  11. package/dist/core/index.cjs.map +1 -1
  12. package/dist/core/index.d.cts +583 -5
  13. package/dist/core/index.d.ts +583 -5
  14. package/dist/core/index.js +652 -76
  15. package/dist/core/index.js.map +1 -1
  16. package/dist/index.cjs +1772 -371
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.cts +44 -7
  19. package/dist/index.d.ts +44 -7
  20. package/dist/index.js +1716 -371
  21. package/dist/index.js.map +1 -1
  22. package/dist/repository/index.cjs +1403 -185
  23. package/dist/repository/index.cjs.map +1 -1
  24. package/dist/repository/index.d.cts +141 -15
  25. package/dist/repository/index.d.ts +141 -15
  26. package/dist/repository/index.js +1396 -185
  27. package/dist/repository/index.js.map +1 -1
  28. package/dist/sdk/index.cjs +23 -1
  29. package/dist/sdk/index.cjs.map +1 -1
  30. package/dist/sdk/index.d.cts +2 -2
  31. package/dist/sdk/index.d.ts +2 -2
  32. package/dist/sdk/index.js +23 -1
  33. package/dist/sdk/index.js.map +1 -1
  34. package/dist/service/index.cjs +484 -106
  35. package/dist/service/index.cjs.map +1 -1
  36. package/dist/service/index.d.cts +68 -10
  37. package/dist/service/index.d.ts +68 -10
  38. package/dist/service/index.js +484 -106
  39. package/dist/service/index.js.map +1 -1
  40. package/dist/types-BhCdep_t.d.ts +646 -0
  41. package/dist/{types-CjLj3GW5.d.cts → types-Dzc443vy.d.cts} +44 -2
  42. package/dist/{types-CjLj3GW5.d.ts → types-Dzc443vy.d.ts} +44 -2
  43. package/dist/types-wtksMa4n.d.cts +646 -0
  44. package/package.json +71 -70
  45. package/LICENSE +0 -21
  46. package/dist/types-DFe5BoJY.d.cts +0 -329
  47. package/dist/types-_bSybQnb.d.ts +0 -329
@@ -122,6 +122,20 @@ interface PortableReservation {
122
122
  expiresAt: string;
123
123
  /** When reservation was committed/released - ISO 8601 or undefined */
124
124
  completedAt?: string;
125
+ /**
126
+ * Caller-supplied idempotency key (V2). Absent on legacy reservations.
127
+ * Unique per user; a replay with the same key returns this same reservation.
128
+ */
129
+ idempotencyKey?: string;
130
+ /**
131
+ * When the atomic reserve placed this hold - ISO 8601, or absent.
132
+ *
133
+ * Written by `reserveCreditsV2` inside the same transaction that increases
134
+ * `reserved`, so its presence is proof that the hold exists. `createReservation`
135
+ * never sets it, and the V2 transitions refuse to move a row without it.
136
+ * See `assertHoldPlaced` in `core/reservation-integrity.ts`.
137
+ */
138
+ holdPlacedAt?: string;
125
139
  }
126
140
  /**
127
141
  * Result of a credit check operation
@@ -165,6 +179,15 @@ interface MonthlyResetResult {
165
179
  wasReset: boolean;
166
180
  /** The user credits after the operation */
167
181
  credits: PortableUserCredits;
182
+ /**
183
+ * `true` when the repository wrote the reset's journal entry inside the same
184
+ * atomic step as the balance write. The service then skips its own journal
185
+ * call — a separate call sits outside the reset's atomicity, and if it fails
186
+ * after the CAS was consumed, the line is lost for good (no retry ever sees
187
+ * the reset again). Absent/`false` means the repository did not journal and
188
+ * the service falls back to the legacy non-atomic write.
189
+ */
190
+ journaled?: boolean;
168
191
  }
169
192
  /**
170
193
  * Result of subscription expiry check
@@ -178,6 +201,16 @@ interface SubscriptionExpiryResult {
178
201
  graceDaysRemaining: number;
179
202
  /** The user credits after the operation */
180
203
  credits: PortableUserCredits;
204
+ /**
205
+ * `true` when the repository wrote the downgrade's journal entry inside the
206
+ * same atomic step as the tier write. The service then skips its own journal
207
+ * call — a separate call sits outside the downgrade's atomicity, and if it
208
+ * fails after the tier write committed, the account is downgraded with no
209
+ * audit line and no retry ever fires (the row is no longer eligible).
210
+ * Absent/`false` means the repository did not journal and the service falls
211
+ * back to the legacy non-atomic write.
212
+ */
213
+ journaled?: boolean;
181
214
  }
182
215
  /**
183
216
  * Credit transaction type
@@ -229,6 +262,11 @@ interface PortableJournalEntry {
229
262
  description: string;
230
263
  /** Additional metadata */
231
264
  metadata?: Record<string, unknown>;
265
+ /**
266
+ * Deterministic key that makes this entry unique per user (V2). Absent on
267
+ * legacy entries; present entries cannot be written twice.
268
+ */
269
+ idempotencyKey?: string;
232
270
  /** When this entry was created - ISO 8601 */
233
271
  createdAt: string;
234
272
  }
@@ -300,7 +338,11 @@ interface WithCreditsOptions {
300
338
  operationType: CreditOperationType;
301
339
  /** AI provider (defaults to "gemini") */
302
340
  provider?: AIProviderType;
303
- /** Custom cost override (use with caution) */
341
+ /**
342
+ * Custom cost override (use with caution). Must be a movable amount: only a
343
+ * cost of 0 in `operationCosts` makes an operation free, so a computed
344
+ * override that lands on 0 is refused rather than run for nothing.
345
+ */
304
346
  customCost?: number;
305
347
  /** Resource ID for usage logging */
306
348
  resourceId?: string;
@@ -308,4 +350,4 @@ interface WithCreditsOptions {
308
350
  resourceType?: ResourceType;
309
351
  }
310
352
 
311
- export { type AIProviderType as A, type BuiltinTier as B, type CreditSource 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 CreditOperationType as a, type PortableUserCredits as b, type PortableReservation 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 };
353
+ 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 };
@@ -122,6 +122,20 @@ interface PortableReservation {
122
122
  expiresAt: string;
123
123
  /** When reservation was committed/released - ISO 8601 or undefined */
124
124
  completedAt?: string;
125
+ /**
126
+ * Caller-supplied idempotency key (V2). Absent on legacy reservations.
127
+ * Unique per user; a replay with the same key returns this same reservation.
128
+ */
129
+ idempotencyKey?: string;
130
+ /**
131
+ * When the atomic reserve placed this hold - ISO 8601, or absent.
132
+ *
133
+ * Written by `reserveCreditsV2` inside the same transaction that increases
134
+ * `reserved`, so its presence is proof that the hold exists. `createReservation`
135
+ * never sets it, and the V2 transitions refuse to move a row without it.
136
+ * See `assertHoldPlaced` in `core/reservation-integrity.ts`.
137
+ */
138
+ holdPlacedAt?: string;
125
139
  }
126
140
  /**
127
141
  * Result of a credit check operation
@@ -165,6 +179,15 @@ interface MonthlyResetResult {
165
179
  wasReset: boolean;
166
180
  /** The user credits after the operation */
167
181
  credits: PortableUserCredits;
182
+ /**
183
+ * `true` when the repository wrote the reset's journal entry inside the same
184
+ * atomic step as the balance write. The service then skips its own journal
185
+ * call — a separate call sits outside the reset's atomicity, and if it fails
186
+ * after the CAS was consumed, the line is lost for good (no retry ever sees
187
+ * the reset again). Absent/`false` means the repository did not journal and
188
+ * the service falls back to the legacy non-atomic write.
189
+ */
190
+ journaled?: boolean;
168
191
  }
169
192
  /**
170
193
  * Result of subscription expiry check
@@ -178,6 +201,16 @@ interface SubscriptionExpiryResult {
178
201
  graceDaysRemaining: number;
179
202
  /** The user credits after the operation */
180
203
  credits: PortableUserCredits;
204
+ /**
205
+ * `true` when the repository wrote the downgrade's journal entry inside the
206
+ * same atomic step as the tier write. The service then skips its own journal
207
+ * call — a separate call sits outside the downgrade's atomicity, and if it
208
+ * fails after the tier write committed, the account is downgraded with no
209
+ * audit line and no retry ever fires (the row is no longer eligible).
210
+ * Absent/`false` means the repository did not journal and the service falls
211
+ * back to the legacy non-atomic write.
212
+ */
213
+ journaled?: boolean;
181
214
  }
182
215
  /**
183
216
  * Credit transaction type
@@ -229,6 +262,11 @@ interface PortableJournalEntry {
229
262
  description: string;
230
263
  /** Additional metadata */
231
264
  metadata?: Record<string, unknown>;
265
+ /**
266
+ * Deterministic key that makes this entry unique per user (V2). Absent on
267
+ * legacy entries; present entries cannot be written twice.
268
+ */
269
+ idempotencyKey?: string;
232
270
  /** When this entry was created - ISO 8601 */
233
271
  createdAt: string;
234
272
  }
@@ -300,7 +338,11 @@ interface WithCreditsOptions {
300
338
  operationType: CreditOperationType;
301
339
  /** AI provider (defaults to "gemini") */
302
340
  provider?: AIProviderType;
303
- /** Custom cost override (use with caution) */
341
+ /**
342
+ * Custom cost override (use with caution). Must be a movable amount: only a
343
+ * cost of 0 in `operationCosts` makes an operation free, so a computed
344
+ * override that lands on 0 is refused rather than run for nothing.
345
+ */
304
346
  customCost?: number;
305
347
  /** Resource ID for usage logging */
306
348
  resourceId?: string;
@@ -308,4 +350,4 @@ interface WithCreditsOptions {
308
350
  resourceType?: ResourceType;
309
351
  }
310
352
 
311
- export { type AIProviderType as A, type BuiltinTier as B, type CreditSource 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 CreditOperationType as a, type PortableUserCredits as b, type PortableReservation 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 };
353
+ 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 };