MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / consumeCreditsWithFallback

Function consumeCreditsWithFallback

packages/billing/src/credit-delegation.ts:235–290  ·  view source on GitHub ↗
(
  params: ParamsOf<ConsumeCreditsWithFallbackFn>,
)

Source from the content-addressed store, hash-verified

233 * Tries organization delegation first if a repo URL is available, falls back to personal credits.
234 */
235export async function consumeCreditsWithFallback(
236 params: ParamsOf<ConsumeCreditsWithFallbackFn>,
237): ReturnType<ConsumeCreditsWithFallbackFn> {
238 const { userId, creditsToCharge, repoUrl, context, logger } = params
239
240 try {
241 // Try organization delegation first if repo URL is available
242 if (repoUrl) {
243 const delegationResult = await consumeCreditsWithDelegation({
244 ...params,
245 repositoryUrl: repoUrl,
246 creditsToConsume: creditsToCharge,
247 })
248
249 if (delegationResult.success) {
250 logger.debug(
251 {
252 userId,
253 creditsToCharge,
254 organizationId: delegationResult.organizationId,
255 context,
256 },
257 `Charged organization credits for ${context}`,
258 )
259
260 return success({
261 organizationId: delegationResult.organizationId,
262 organizationName: delegationResult.organizationName,
263 chargedToOrganization: true,
264 })
265 }
266 }
267
268 // Fall back to personal credits
269 await consumeCredits({
270 userId,
271 creditsToConsume: creditsToCharge,
272 logger,
273 })
274 logger.debug(
275 { userId, creditsToCharge, context },
276 `Charged personal credits for ${context}`,
277 )
278
279 return success({
280 chargedToOrganization: false,
281 })
282 } catch (error) {
283 logger.error(
284 { error, userId, creditsToCharge, context },
285 `Failed to charge credits for ${context}`,
286 )
287
288 return failure(error)
289 }
290}

Callers 1

checkCreditsAndChargeFunction · 0.85

Calls 4

successFunction · 0.90
consumeCreditsFunction · 0.90
failureFunction · 0.90

Tested by

no test coverage detected