@@ -18,10 +18,7 @@ import type {
1818 LoggerWithContextFn ,
1919} from '@codebuff/common/types/contracts/logger'
2020
21- import type {
22- BlockGrantResult ,
23- SubscriptionRow ,
24- } from '@codebuff/billing/subscription'
21+ import type { BlockGrantResult } from '@codebuff/billing/subscription'
2522import type { NextRequest } from 'next/server'
2623
2724import type { ChatCompletionRequestBody } from '@/llm-api/types'
@@ -83,8 +80,7 @@ export async function postChatCompletions(params: {
8380 getAgentRunFromId : GetAgentRunFromIdFn
8481 fetch : typeof globalThis . fetch
8582 insertMessageBigquery : InsertMessageBigqueryFn
86- getActiveSubscription ?: ( params : { userId : string ; logger : Logger } ) => Promise < SubscriptionRow | null >
87- ensureActiveBlockGrant ?: ( params : { userId : string ; subscription : SubscriptionRow ; logger : Logger } ) => Promise < BlockGrantResult >
83+ ensureSubscriberBlockGrant ?: ( params : { userId : string ; logger : Logger } ) => Promise < BlockGrantResult | null >
8884} ) {
8985 const {
9086 req,
@@ -95,8 +91,7 @@ export async function postChatCompletions(params: {
9591 getAgentRunFromId,
9692 fetch,
9793 insertMessageBigquery,
98- getActiveSubscription,
99- ensureActiveBlockGrant,
94+ ensureSubscriberBlockGrant,
10095 } = params
10196 let { logger } = params
10297
@@ -192,13 +187,10 @@ export async function postChatCompletions(params: {
192187 } )
193188
194189 // For subscribers, ensure a block grant exists before checking balance.
195- // This is done here block grants should only start when the user begins working.
196- if ( getActiveSubscription && ensureActiveBlockGrant ) {
190+ // This is done here because block grants should only start when the user begins working.
191+ if ( ensureSubscriberBlockGrant ) {
197192 try {
198- const activeSub = await getActiveSubscription ( { userId, logger } )
199- if ( activeSub ) {
200- await ensureActiveBlockGrant ( { userId, subscription : activeSub , logger } )
201- }
193+ await ensureSubscriberBlockGrant ( { userId, logger } )
202194 } catch ( error ) {
203195 logger . error (
204196 { error : getErrorObject ( error ) , userId } ,
0 commit comments