codebuff 1.0.247 → 1.0.249

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 (143) hide show
  1. package/dist/code-map/tsconfig.tsbuildinfo +1 -1
  2. package/dist/common/actions.d.ts +32 -32
  3. package/dist/common/billing/auto-topup.d.ts +8 -0
  4. package/dist/common/billing/auto-topup.js +192 -0
  5. package/dist/common/billing/auto-topup.js.map +1 -0
  6. package/dist/common/billing/balance-calculator.d.ts +57 -0
  7. package/dist/common/billing/balance-calculator.js +218 -0
  8. package/dist/common/billing/balance-calculator.js.map +1 -0
  9. package/dist/common/billing/conversion.d.ts +9 -0
  10. package/dist/common/billing/conversion.js +20 -0
  11. package/dist/common/billing/conversion.js.map +1 -0
  12. package/dist/common/billing/credit-conversion.d.ts +24 -0
  13. package/dist/common/billing/credit-conversion.js +48 -0
  14. package/dist/common/billing/credit-conversion.js.map +1 -0
  15. package/dist/common/billing/grant-credits.d.ts +43 -0
  16. package/dist/common/billing/grant-credits.js +266 -0
  17. package/dist/common/billing/grant-credits.js.map +1 -0
  18. package/dist/common/billing/plans.d.ts +13 -0
  19. package/dist/common/billing/plans.js +44 -0
  20. package/dist/common/billing/plans.js.map +1 -0
  21. package/dist/common/json-config/__tests__/__snapshots__/stringify-schema.test.js.snap +66 -0
  22. package/dist/common/types/plan.d.ts +24 -0
  23. package/dist/{types.js → common/types/plan.js} +1 -1
  24. package/dist/common/types/plan.js.map +1 -0
  25. package/dist/common/types/usage.d.ts +2 -2
  26. package/dist/common/util/server/referral.d.ts +14 -0
  27. package/dist/common/util/server/referral.js +85 -0
  28. package/dist/common/util/server/referral.js.map +1 -0
  29. package/dist/common/websockets/websocket-schema.d.ts +68 -68
  30. package/package.json +1 -1
  31. package/dist/background-process-manager.d.ts +0 -50
  32. package/dist/background-process-manager.js +0 -359
  33. package/dist/background-process-manager.js.map +0 -1
  34. package/dist/browser-runner.d.ts +0 -35
  35. package/dist/browser-runner.js +0 -680
  36. package/dist/browser-runner.js.map +0 -1
  37. package/dist/chat-storage.d.ts +0 -2
  38. package/dist/chat-storage.js +0 -93
  39. package/dist/chat-storage.js.map +0 -1
  40. package/dist/checkpoints/checkpoint-manager.d.ts +0 -94
  41. package/dist/checkpoints/checkpoint-manager.js +0 -280
  42. package/dist/checkpoints/checkpoint-manager.js.map +0 -1
  43. package/dist/checkpoints/file-manager.d.ts +0 -72
  44. package/dist/checkpoints/file-manager.js +0 -311
  45. package/dist/checkpoints/file-manager.js.map +0 -1
  46. package/dist/cli-handlers/api-key.d.ts +0 -25
  47. package/dist/cli-handlers/api-key.js +0 -66
  48. package/dist/cli-handlers/api-key.js.map +0 -1
  49. package/dist/cli-handlers/checkpoint.d.ts +0 -18
  50. package/dist/cli-handlers/checkpoint.js +0 -195
  51. package/dist/cli-handlers/checkpoint.js.map +0 -1
  52. package/dist/cli-handlers/diff.d.ts +0 -2
  53. package/dist/cli-handlers/diff.js +0 -31
  54. package/dist/cli-handlers/diff.js.map +0 -1
  55. package/dist/cli-handlers/easter-egg.d.ts +0 -1
  56. package/dist/cli-handlers/easter-egg.js +0 -126
  57. package/dist/cli-handlers/easter-egg.js.map +0 -1
  58. package/dist/cli-handlers/inititalization-flow.d.ts +0 -1
  59. package/dist/cli-handlers/inititalization-flow.js +0 -24
  60. package/dist/cli-handlers/inititalization-flow.js.map +0 -1
  61. package/dist/cli.d.ts +0 -44
  62. package/dist/cli.js +0 -478
  63. package/dist/cli.js.map +0 -1
  64. package/dist/client.d.ts +0 -157
  65. package/dist/client.js +0 -836
  66. package/dist/client.js.map +0 -1
  67. package/dist/config.d.ts +0 -4
  68. package/dist/config.js +0 -12
  69. package/dist/config.js.map +0 -1
  70. package/dist/create-template-project.d.ts +0 -1
  71. package/dist/create-template-project.js +0 -107
  72. package/dist/create-template-project.js.map +0 -1
  73. package/dist/credentials.d.ts +0 -4
  74. package/dist/credentials.js +0 -38
  75. package/dist/credentials.js.map +0 -1
  76. package/dist/dev-process-manager.d.ts +0 -10
  77. package/dist/dev-process-manager.js +0 -54
  78. package/dist/dev-process-manager.js.map +0 -1
  79. package/dist/fingerprint.d.ts +0 -1
  80. package/dist/fingerprint.js +0 -48
  81. package/dist/fingerprint.js.map +0 -1
  82. package/dist/index.d.ts +0 -2
  83. package/dist/index.js +0 -117
  84. package/dist/index.js.map +0 -1
  85. package/dist/menu.d.ts +0 -3
  86. package/dist/menu.js +0 -126
  87. package/dist/menu.js.map +0 -1
  88. package/dist/project-files.d.ts +0 -114
  89. package/dist/project-files.js +0 -513
  90. package/dist/project-files.js.map +0 -1
  91. package/dist/startup-process-handler.d.ts +0 -2
  92. package/dist/startup-process-handler.js +0 -21
  93. package/dist/startup-process-handler.js.map +0 -1
  94. package/dist/tool-handlers.d.ts +0 -28
  95. package/dist/tool-handlers.js +0 -240
  96. package/dist/tool-handlers.js.map +0 -1
  97. package/dist/types.d.ts +0 -15
  98. package/dist/types.js.map +0 -1
  99. package/dist/update-codebuff.d.ts +0 -1
  100. package/dist/update-codebuff.js +0 -160
  101. package/dist/update-codebuff.js.map +0 -1
  102. package/dist/utils/__tests__/background-process-manager.test.d.ts +0 -1
  103. package/dist/utils/__tests__/background-process-manager.test.js +0 -289
  104. package/dist/utils/__tests__/background-process-manager.test.js.map +0 -1
  105. package/dist/utils/__tests__/tool-renderers.test.d.ts +0 -1
  106. package/dist/utils/__tests__/tool-renderers.test.js +0 -51
  107. package/dist/utils/__tests__/tool-renderers.test.js.map +0 -1
  108. package/dist/utils/__tests__/xml-stream-parser.test.d.ts +0 -1
  109. package/dist/utils/__tests__/xml-stream-parser.test.js +0 -229
  110. package/dist/utils/__tests__/xml-stream-parser.test.js.map +0 -1
  111. package/dist/utils/analytics.d.ts +0 -6
  112. package/dist/utils/analytics.js +0 -59
  113. package/dist/utils/analytics.js.map +0 -1
  114. package/dist/utils/detect-shell.d.ts +0 -1
  115. package/dist/utils/detect-shell.js +0 -60
  116. package/dist/utils/detect-shell.js.map +0 -1
  117. package/dist/utils/logger.d.ts +0 -21
  118. package/dist/utils/logger.js +0 -105
  119. package/dist/utils/logger.js.map +0 -1
  120. package/dist/utils/spinner.d.ts +0 -11
  121. package/dist/utils/spinner.js +0 -87
  122. package/dist/utils/spinner.js.map +0 -1
  123. package/dist/utils/system-info.d.ts +0 -8
  124. package/dist/utils/system-info.js +0 -22
  125. package/dist/utils/system-info.js.map +0 -1
  126. package/dist/utils/terminal.d.ts +0 -41
  127. package/dist/utils/terminal.js +0 -475
  128. package/dist/utils/terminal.js.map +0 -1
  129. package/dist/utils/tool-renderers.d.ts +0 -16
  130. package/dist/utils/tool-renderers.js +0 -145
  131. package/dist/utils/tool-renderers.js.map +0 -1
  132. package/dist/utils/xml-stream-parser.d.ts +0 -9
  133. package/dist/utils/xml-stream-parser.js +0 -128
  134. package/dist/utils/xml-stream-parser.js.map +0 -1
  135. package/dist/web-scraper.d.ts +0 -3
  136. package/dist/web-scraper.js +0 -57
  137. package/dist/web-scraper.js.map +0 -1
  138. package/dist/workers/checkpoint-worker.d.ts +0 -1
  139. package/dist/workers/checkpoint-worker.js +0 -48
  140. package/dist/workers/checkpoint-worker.js.map +0 -1
  141. package/dist/workers/project-context.d.ts +0 -1
  142. package/dist/workers/project-context.js +0 -17
  143. package/dist/workers/project-context.js.map +0 -1
@@ -0,0 +1,57 @@
1
+ import db from '../db';
2
+ import { GrantType } from '../db/schema';
3
+ export interface CreditBalance {
4
+ totalRemaining: number;
5
+ totalDebt: number;
6
+ netBalance: number;
7
+ breakdown: Partial<Record<GrantType, number>>;
8
+ principals: Partial<Record<GrantType, number>>;
9
+ }
10
+ export interface CreditUsageAndBalance {
11
+ usageThisCycle: number;
12
+ balance: CreditBalance;
13
+ }
14
+ export interface CreditConsumptionResult {
15
+ consumed: number;
16
+ fromPurchased: number;
17
+ }
18
+ type DbConn = Pick<typeof db, 'select' | 'update'>;
19
+ /**
20
+ * Gets active grants for a user, ordered by expiration (soonest first), then priority, and creation date.
21
+ * Added optional `conn` param so callers inside a transaction can supply their TX object.
22
+ */
23
+ export declare function getOrderedActiveGrants(userId: string, now: Date, conn?: DbConn): Promise<{
24
+ created_at: Date;
25
+ type: import("../types/grant").GrantType;
26
+ expires_at: Date | null;
27
+ operation_id: string;
28
+ user_id: string;
29
+ principal: number;
30
+ balance: number;
31
+ description: string | null;
32
+ priority: number;
33
+ }[]>;
34
+ /**
35
+ * Calculates both the current balance and usage in this cycle in a single query.
36
+ * This is more efficient than calculating them separately.
37
+ */
38
+ export declare function calculateUsageAndBalance(userId: string, quotaResetDate: Date, now?: Date): Promise<CreditUsageAndBalance>;
39
+ /**
40
+ * Updates the remaining amounts in credit grants after consumption.
41
+ * Follows priority order strictly - higher priority grants (lower number) are consumed first.
42
+ * Returns details about credit consumption including how many came from purchased credits.
43
+ *
44
+ * Uses SERIALIZABLE isolation to prevent concurrent modifications that could lead to
45
+ * incorrect credit usage (e.g., "double spending" credits).
46
+ *
47
+ * @param userId The ID of the user
48
+ * @param creditsToConsume Number of credits being consumed
49
+ * @returns Promise resolving to number of credits consumed
50
+ */
51
+ export declare function consumeCredits(userId: string, creditsToConsume: number): Promise<CreditConsumptionResult>;
52
+ /**
53
+ * Calculate the total credits used during the current billing cycle for a user
54
+ * by summing the difference between initial and remaining amounts for all relevant grants.
55
+ */
56
+ export declare function calculateUsageThisCycle(userId: string, quotaResetDate: Date): Promise<number>;
57
+ export {};
@@ -0,0 +1,218 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.getOrderedActiveGrants = getOrderedActiveGrants;
30
+ exports.calculateUsageAndBalance = calculateUsageAndBalance;
31
+ exports.consumeCredits = consumeCredits;
32
+ exports.calculateUsageThisCycle = calculateUsageThisCycle;
33
+ const db_1 = __importDefault(require("../db"));
34
+ const schema = __importStar(require("../db/schema"));
35
+ const drizzle_orm_1 = require("drizzle-orm");
36
+ const logger_1 = require("../util/logger");
37
+ const transaction_1 = require("../db/transaction");
38
+ /**
39
+ * Gets active grants for a user, ordered by expiration (soonest first), then priority, and creation date.
40
+ * Added optional `conn` param so callers inside a transaction can supply their TX object.
41
+ */
42
+ async function getOrderedActiveGrants(userId, now, conn = db_1.default // use DbConn instead of typeof db
43
+ ) {
44
+ return conn
45
+ .select()
46
+ .from(schema.creditLedger)
47
+ .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(schema.creditLedger.user_id, userId), (0, drizzle_orm_1.or)((0, drizzle_orm_1.isNull)(schema.creditLedger.expires_at), (0, drizzle_orm_1.gt)(schema.creditLedger.expires_at, now))))
48
+ .orderBy(
49
+ // Use grants based on priority, then expiration date, then creation date
50
+ (0, drizzle_orm_1.asc)(schema.creditLedger.priority), (0, drizzle_orm_1.asc)(schema.creditLedger.expires_at), (0, drizzle_orm_1.asc)(schema.creditLedger.created_at));
51
+ }
52
+ /**
53
+ * Updates a single grant's balance and logs the change.
54
+ */
55
+ async function updateGrantBalance(userId, grant, consumed, newBalance, tx) {
56
+ await tx
57
+ .update(schema.creditLedger)
58
+ .set({ balance: newBalance })
59
+ .where((0, drizzle_orm_1.eq)(schema.creditLedger.operation_id, grant.operation_id));
60
+ logger_1.logger.debug({
61
+ userId,
62
+ grantId: grant.operation_id,
63
+ grantType: grant.type,
64
+ consumed,
65
+ remaining: newBalance,
66
+ expiresAt: grant.expires_at,
67
+ }, 'Updated grant remaining amount after consumption');
68
+ }
69
+ /**
70
+ * Consumes credits from a list of ordered grants.
71
+ */
72
+ async function consumeFromOrderedGrants(userId, creditsToConsume, grants, tx) {
73
+ let remainingToConsume = creditsToConsume;
74
+ let consumed = 0;
75
+ let fromPurchased = 0;
76
+ // First pass: try to repay any debt
77
+ for (const grant of grants) {
78
+ if (grant.balance < 0 && remainingToConsume > 0) {
79
+ const debtAmount = Math.abs(grant.balance);
80
+ const repayAmount = Math.min(debtAmount, remainingToConsume);
81
+ const newBalance = grant.balance + repayAmount;
82
+ remainingToConsume -= repayAmount;
83
+ consumed += repayAmount;
84
+ await updateGrantBalance(userId, grant, -repayAmount, newBalance, tx);
85
+ logger_1.logger.debug({ userId, grantId: grant.operation_id, repayAmount, newBalance }, 'Repaid debt in grant');
86
+ }
87
+ }
88
+ // Second pass: consume from positive balances
89
+ for (const grant of grants) {
90
+ if (remainingToConsume <= 0)
91
+ break;
92
+ if (grant.balance <= 0)
93
+ continue;
94
+ const consumeFromThisGrant = Math.min(remainingToConsume, grant.balance);
95
+ const newBalance = grant.balance - consumeFromThisGrant;
96
+ remainingToConsume -= consumeFromThisGrant;
97
+ consumed += consumeFromThisGrant;
98
+ // Track consumption from purchased credits
99
+ if (grant.type === 'purchase') {
100
+ fromPurchased += consumeFromThisGrant;
101
+ }
102
+ await updateGrantBalance(userId, grant, consumeFromThisGrant, newBalance, tx);
103
+ }
104
+ // If we still have remaining to consume and no grants left, create debt in the last grant
105
+ if (remainingToConsume > 0 && grants.length > 0) {
106
+ const lastGrant = grants[grants.length - 1];
107
+ if (lastGrant.balance <= 0) {
108
+ const newBalance = lastGrant.balance - remainingToConsume;
109
+ await updateGrantBalance(userId, lastGrant, remainingToConsume, newBalance, tx);
110
+ consumed += remainingToConsume;
111
+ logger_1.logger.warn({
112
+ userId,
113
+ grantId: lastGrant.operation_id,
114
+ requested: remainingToConsume,
115
+ consumed: remainingToConsume,
116
+ newDebt: Math.abs(newBalance),
117
+ }, 'Created new debt in grant');
118
+ }
119
+ }
120
+ return { consumed, fromPurchased };
121
+ }
122
+ /**
123
+ * Calculates both the current balance and usage in this cycle in a single query.
124
+ * This is more efficient than calculating them separately.
125
+ */
126
+ async function calculateUsageAndBalance(userId, quotaResetDate, now = new Date()) {
127
+ // Get all relevant grants in one query
128
+ const grants = await getOrderedActiveGrants(userId, now);
129
+ // Initialize balance structure
130
+ const balance = {
131
+ totalRemaining: 0,
132
+ totalDebt: 0,
133
+ netBalance: 0,
134
+ breakdown: {},
135
+ principals: {},
136
+ };
137
+ // Calculate both metrics in one pass
138
+ let usageThisCycle = 0;
139
+ let totalPositiveBalance = 0;
140
+ let totalDebt = 0;
141
+ // First pass: calculate initial totals and usage
142
+ for (const grant of grants) {
143
+ const grantType = grant.type;
144
+ // Calculate usage if grant was active in this cycle
145
+ if (grant.created_at > quotaResetDate ||
146
+ !grant.expires_at ||
147
+ grant.expires_at > quotaResetDate) {
148
+ usageThisCycle += grant.principal - grant.balance;
149
+ }
150
+ // Add to balance if grant is currently active
151
+ if (!grant.expires_at || grant.expires_at > now) {
152
+ if (grant.balance > 0) {
153
+ totalPositiveBalance += grant.balance;
154
+ balance.breakdown[grantType] =
155
+ (balance.breakdown[grantType] || 0) + grant.balance;
156
+ balance.principals[grantType] =
157
+ (balance.principals[grantType] || 0) + grant.principal;
158
+ }
159
+ else if (grant.balance < 0) {
160
+ totalDebt += Math.abs(grant.balance);
161
+ }
162
+ }
163
+ }
164
+ // Perform in-memory settlement if there's both debt and positive balance
165
+ if (totalDebt > 0 && totalPositiveBalance > 0) {
166
+ const settlementAmount = Math.min(totalDebt, totalPositiveBalance);
167
+ logger_1.logger.debug({ userId, totalDebt, totalPositiveBalance, settlementAmount }, 'Performing in-memory settlement');
168
+ // After settlement:
169
+ totalPositiveBalance -= settlementAmount;
170
+ totalDebt -= settlementAmount;
171
+ }
172
+ // Set final balance values after settlement
173
+ balance.totalRemaining = totalPositiveBalance;
174
+ balance.totalDebt = totalDebt;
175
+ balance.netBalance = totalPositiveBalance - totalDebt;
176
+ logger_1.logger.debug({ userId, balance, usageThisCycle, grantsCount: grants.length }, 'Calculated usage and settled balance');
177
+ return { usageThisCycle, balance };
178
+ }
179
+ /**
180
+ * Updates the remaining amounts in credit grants after consumption.
181
+ * Follows priority order strictly - higher priority grants (lower number) are consumed first.
182
+ * Returns details about credit consumption including how many came from purchased credits.
183
+ *
184
+ * Uses SERIALIZABLE isolation to prevent concurrent modifications that could lead to
185
+ * incorrect credit usage (e.g., "double spending" credits).
186
+ *
187
+ * @param userId The ID of the user
188
+ * @param creditsToConsume Number of credits being consumed
189
+ * @returns Promise resolving to number of credits consumed
190
+ */
191
+ async function consumeCredits(userId, creditsToConsume) {
192
+ return await (0, transaction_1.withSerializableTransaction)(async (tx) => {
193
+ const now = new Date();
194
+ const activeGrants = await getOrderedActiveGrants(userId, now, tx);
195
+ if (activeGrants.length === 0) {
196
+ logger_1.logger.error({ userId, creditsToConsume }, 'No active grants found to consume credits from');
197
+ throw new Error('No active grants found');
198
+ }
199
+ const result = await consumeFromOrderedGrants(userId, creditsToConsume, activeGrants, tx);
200
+ return result;
201
+ }, { userId, creditsToConsume });
202
+ }
203
+ /**
204
+ * Calculate the total credits used during the current billing cycle for a user
205
+ * by summing the difference between initial and remaining amounts for all relevant grants.
206
+ */
207
+ async function calculateUsageThisCycle(userId, quotaResetDate) {
208
+ const usageResult = await db_1.default
209
+ .select({
210
+ totalUsed: (0, drizzle_orm_1.sql) `COALESCE(SUM(${schema.creditLedger.principal} - ${schema.creditLedger.balance}), 0)`,
211
+ })
212
+ .from(schema.creditLedger)
213
+ .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(schema.creditLedger.user_id, userId),
214
+ // Grant was created during this cycle OR expires after this cycle starts (including never expires)
215
+ (0, drizzle_orm_1.or)((0, drizzle_orm_1.gt)(schema.creditLedger.created_at, quotaResetDate), (0, drizzle_orm_1.and)((0, drizzle_orm_1.or)((0, drizzle_orm_1.isNull)(schema.creditLedger.expires_at), (0, drizzle_orm_1.gt)(schema.creditLedger.expires_at, quotaResetDate))))));
216
+ return usageResult[0].totalUsed;
217
+ }
218
+ //# sourceMappingURL=balance-calculator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"balance-calculator.js","sourceRoot":"","sources":["../../src/billing/balance-calculator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,wDAuBC;AAwHD,4DAyEC;AAcD,wCA4BC;AAMD,0DA0BC;AAtUD,+CAAsB;AACtB,qDAAsC;AACtC,6CAA+D;AAE/D,2CAAuC;AAEvC,mDAA+D;AA0B/D;;;GAGG;AACI,KAAK,UAAU,sBAAsB,CAC1C,MAAc,EACd,GAAS,EACT,OAAe,YAAE,CAAC,kCAAkC;;IAEpD,OAAO,IAAI;SACR,MAAM,EAAE;SACR,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC;SACzB,KAAK,CACJ,IAAA,iBAAG,EACD,IAAA,gBAAE,EAAC,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,EACvC,IAAA,gBAAE,EACA,IAAA,oBAAM,EAAC,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,EACtC,IAAA,gBAAE,EAAC,MAAM,CAAC,YAAY,CAAC,UAAU,EAAE,GAAG,CAAC,CACxC,CACF,CACF;SACA,OAAO;IACN,yEAAyE;IACzE,IAAA,iBAAG,EAAC,MAAM,CAAC,YAAY,CAAC,QAAQ,CAAC,EACjC,IAA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E,MAAM,CAAC;IACvC,mGAAmG;IACnG,IAAA,gBAAE,EACA,IAAA,gBAAE,EAAC,MAAM,CAAC,YAAY,CAAC,UAAU,EAAE,cAAc,CAAC,EAClD,IAAA,iBAAG,EACD,IAAA,gBAAE,EACA,IAAA,oBAAM,EAAC,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,EACtC,IAAA,gBAAE,EAAC,MAAM,CAAC,YAAY,CAAC,UAAU,EAAE,cAAc,CAAC,CACnD,CACF,CACF,CACF,CACF,CAAA;IAEH,OAAO,WAAW,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;AACjC,CAAC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Determines the cost of one internal credit in USD cents for a given user.
3
+ * TODO: Enhance this function to fetch user's plan and apply plan-specific rates.
4
+ *
5
+ * @param userId - The ID of the user (currently unused, for future enhancement).
6
+ * @returns The cost of one internal credit in cents.
7
+ */
8
+ export declare function getUserCostPerCredit(userId: string | undefined): Promise<number>;
9
+ export { convertCreditsToUsdCents, convertStripeGrantAmountToCredits } from './credit-conversion';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertStripeGrantAmountToCredits = exports.convertCreditsToUsdCents = void 0;
4
+ exports.getUserCostPerCredit = getUserCostPerCredit;
5
+ /**
6
+ * Determines the cost of one internal credit in USD cents for a given user.
7
+ * TODO: Enhance this function to fetch user's plan and apply plan-specific rates.
8
+ *
9
+ * @param userId - The ID of the user (currently unused, for future enhancement).
10
+ * @returns The cost of one internal credit in cents.
11
+ */
12
+ async function getUserCostPerCredit(userId) {
13
+ // Placeholder: Currently 1 cent per credit for all users. Can adjust in the future based on user
14
+ return 1;
15
+ }
16
+ // Re-export the pure conversion functions
17
+ var credit_conversion_1 = require("./credit-conversion");
18
+ Object.defineProperty(exports, "convertCreditsToUsdCents", { enumerable: true, get: function () { return credit_conversion_1.convertCreditsToUsdCents; } });
19
+ Object.defineProperty(exports, "convertStripeGrantAmountToCredits", { enumerable: true, get: function () { return credit_conversion_1.convertStripeGrantAmountToCredits; } });
20
+ //# sourceMappingURL=conversion.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversion.js","sourceRoot":"","sources":["../../src/billing/conversion.ts"],"names":[],"mappings":";;;AAUA,oDAKC;AAZD;;;;;;GAMG;AACI,KAAK,UAAU,oBAAoB,CACxC,MAA0B;IAE1B,iGAAiG;IACjG,OAAO,CAAC,CAAA;AACV,CAAC;AAED,0CAA0C;AAC1C,yDAAiG;AAAxF,6HAAA,wBAAwB,OAAA;AAAE,sIAAA,iCAAiC,OAAA"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Gets the cost per credit for a user. Currently hardcoded to 1 cent per credit,
3
+ * but could be customized per user in the future.
4
+ */
5
+ export declare function getUserCostPerCredit(userId: string | undefined): Promise<number>;
6
+ /**
7
+ * Converts an internal credit system value to the equivalent monetary amount in USD cents,
8
+ * based on the user's cost per credit.
9
+ * Uses Math.ceil to ensure enough monetary value is calculated if rounding occurs.
10
+ *
11
+ * @param credits - The amount in internal credits.
12
+ * @param centsPerCredit - The user's effective cost per internal credit (in cents).
13
+ * @returns The equivalent amount in USD cents.
14
+ */
15
+ export declare function convertCreditsToUsdCents(credits: number, centsPerCredit: number): number;
16
+ /**
17
+ * Converts a Stripe grant amount (in the smallest currency unit, e.g., cents for USD)
18
+ * to the application's internal credit system units, based on the user's cost per credit.
19
+ *
20
+ * @param amountInSmallestUnit - The grant amount from Stripe, typically in cents.
21
+ * @param centsPerCredit - The user's effective cost per internal credit (in cents).
22
+ * @returns The equivalent amount in application credits. Uses Math.round for conversion.
23
+ */
24
+ export declare function convertStripeGrantAmountToCredits(amountInSmallestUnit: number, centsPerCredit: number): number;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getUserCostPerCredit = getUserCostPerCredit;
4
+ exports.convertCreditsToUsdCents = convertCreditsToUsdCents;
5
+ exports.convertStripeGrantAmountToCredits = convertStripeGrantAmountToCredits;
6
+ /**
7
+ * Gets the cost per credit for a user. Currently hardcoded to 1 cent per credit,
8
+ * but could be customized per user in the future.
9
+ */
10
+ async function getUserCostPerCredit(userId) {
11
+ return 1; // 1 cent per credit
12
+ }
13
+ /**
14
+ * Converts an internal credit system value to the equivalent monetary amount in USD cents,
15
+ * based on the user's cost per credit.
16
+ * Uses Math.ceil to ensure enough monetary value is calculated if rounding occurs.
17
+ *
18
+ * @param credits - The amount in internal credits.
19
+ * @param centsPerCredit - The user's effective cost per internal credit (in cents).
20
+ * @returns The equivalent amount in USD cents.
21
+ */
22
+ function convertCreditsToUsdCents(credits, centsPerCredit) {
23
+ if (credits <= 0 || centsPerCredit <= 0) {
24
+ return 0;
25
+ }
26
+ // Total Cents = Credits * Cents per Credit
27
+ const cents = credits * centsPerCredit;
28
+ // Use Math.ceil for safety, ensuring we calculate enough monetary value.
29
+ return Math.ceil(cents);
30
+ }
31
+ /**
32
+ * Converts a Stripe grant amount (in the smallest currency unit, e.g., cents for USD)
33
+ * to the application's internal credit system units, based on the user's cost per credit.
34
+ *
35
+ * @param amountInSmallestUnit - The grant amount from Stripe, typically in cents.
36
+ * @param centsPerCredit - The user's effective cost per internal credit (in cents).
37
+ * @returns The equivalent amount in application credits. Uses Math.round for conversion.
38
+ */
39
+ function convertStripeGrantAmountToCredits(amountInSmallestUnit, centsPerCredit) {
40
+ if (centsPerCredit <= 0) {
41
+ return 0;
42
+ }
43
+ // Credits = Total Cents / Cents per Credit
44
+ const credits = amountInSmallestUnit / centsPerCredit;
45
+ // Use Math.round for robustness
46
+ return Math.round(credits);
47
+ }
48
+ //# sourceMappingURL=credit-conversion.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"credit-conversion.js","sourceRoot":"","sources":["../../src/billing/credit-conversion.ts"],"names":[],"mappings":";;AAIA,oDAEC;AAWD,4DAWC;AAUD,8EAWC;AAjDD;;;GAGG;AACI,KAAK,UAAU,oBAAoB,CAAC,MAA0B;IACnE,OAAO,CAAC,CAAA,CAAC,oBAAoB;AAC/B,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,wBAAwB,CACtC,OAAe,EACf,cAAsB;IAEtB,IAAI,OAAO,IAAI,CAAC,IAAI,cAAc,IAAI,CAAC,EAAE,CAAC;QACxC,OAAO,CAAC,CAAA;IACV,CAAC;IACD,2CAA2C;IAC3C,MAAM,KAAK,GAAG,OAAO,GAAG,cAAc,CAAA;IACtC,yEAAyE;IACzE,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;AACzB,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,iCAAiC,CAC/C,oBAA4B,EAC5B,cAAsB;IAEtB,IAAI,cAAc,IAAI,CAAC,EAAE,CAAC;QACxB,OAAO,CAAC,CAAA;IACV,CAAC;IACD,2CAA2C;IAC3C,MAAM,OAAO,GAAG,oBAAoB,GAAG,cAAc,CAAA;IACrD,gCAAgC;IAChC,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;AAC5B,CAAC"}
@@ -0,0 +1,43 @@
1
+ import { GrantType } from '../db/schema';
2
+ /**
3
+ * Finds the amount of the most recent expired 'free' grant for a user.
4
+ * Finds the amount of the most recent expired 'free' grant for a user,
5
+ * excluding migration grants (operation_id starting with 'migration-').
6
+ * If there is a previous grant, caps the amount at 2000 credits.
7
+ * If no expired 'free' grant is found, returns the default free limit.
8
+ * @param userId The ID of the user.
9
+ * @returns The amount of the last expired free grant (capped at 2000) or the default.
10
+ */
11
+ export declare function getPreviousFreeGrantAmount(userId: string): Promise<number>;
12
+ /**
13
+ * Calculates the total referral bonus credits a user should receive based on
14
+ * their referral history (both as referrer and referred).
15
+ * @param userId The ID of the user.
16
+ * @returns The total referral bonus credits earned.
17
+ */
18
+ export declare function calculateTotalReferralBonus(userId: string): Promise<number>;
19
+ /**
20
+ * Processes a credit grant request with retries and failure logging.
21
+ */
22
+ export declare function processAndGrantCredit(userId: string, amount: number, type: GrantType, description: string, expiresAt: Date | null, operationId: string): Promise<void>;
23
+ /**
24
+ * Revokes credits from a specific grant by operation ID.
25
+ * This sets the balance to 0 and updates the description to indicate a refund.
26
+ *
27
+ * @param operationId The operation ID of the grant to revoke
28
+ * @param reason The reason for revoking the credits (e.g. refund)
29
+ * @returns true if the grant was found and revoked, false otherwise
30
+ */
31
+ export declare function revokeGrantByOperationId(operationId: string, reason: string): Promise<boolean>;
32
+ /**
33
+ * Checks if a user's quota needs to be reset, and if so:
34
+ * 1. Calculates their new monthly grant amount
35
+ * 2. Issues the grant with the appropriate expiry
36
+ * 3. Updates their next_quota_reset date
37
+ * All of this is done in a single transaction with SERIALIZABLE isolation
38
+ * to prevent concurrent resets from creating duplicate grants.
39
+ *
40
+ * @param userId The ID of the user
41
+ * @returns The effective quota reset date (either existing or new)
42
+ */
43
+ export declare function triggerMonthlyResetAndGrant(userId: string): Promise<Date>;