@@ -8,6 +8,7 @@ import { getValidationErrorMessage, parseRequest, validationErrorResponse } from
88import { checkInternalAuth } from '@/lib/auth/hybrid'
99import { getMaxExecutionTimeout } from '@/lib/core/execution-limits'
1010import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
11+ import { type FalAICostMetadata , getFalAICostMetadata } from '@/lib/tools/falai-pricing'
1112import { downloadFileFromStorage } from '@/lib/uploads/utils/file-utils.server'
1213import { assertToolFileAccess } from '@/app/api/files/authorization'
1314import type { UserFile } from '@/executor/types'
@@ -102,6 +103,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
102103 let height : number | undefined
103104 let jobId : string | undefined
104105 let actualDuration : number | undefined
106+ let falaiCost : FalAICostMetadata | undefined
105107
106108 if ( body . visualReference ) {
107109 const denied = await assertToolFileAccess (
@@ -200,6 +202,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
200202 resolution ,
201203 body . promptOptimizer ,
202204 body . generateAudio ,
205+ body . useHostedCostTracking === true ,
203206 requestId ,
204207 logger
205208 )
@@ -208,6 +211,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
208211 height = result . height
209212 jobId = result . jobId
210213 actualDuration = result . duration
214+ falaiCost = result . falaiCost
211215 } else {
212216 return NextResponse . json ( { error : `Unknown provider: ${ provider } ` } , { status : 400 } )
213217 }
@@ -262,6 +266,8 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
262266 provider,
263267 model : model || 'default' ,
264268 jobId,
269+ __falaiCostDollars : falaiCost ?. costDollars ,
270+ __falaiBilling : falaiCost ,
265271 } )
266272 }
267273
@@ -294,6 +300,8 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
294300 provider,
295301 model : model || 'default' ,
296302 jobId,
303+ __falaiCostDollars : falaiCost ?. costDollars ,
304+ __falaiBilling : falaiCost ,
297305 } )
298306 } catch ( error ) {
299307 logger . error ( `[${ requestId } ] Video proxy error:` , error )
@@ -1082,9 +1090,17 @@ async function generateWithFalAI(
10821090 resolution : string | undefined ,
10831091 promptOptimizer : boolean | undefined ,
10841092 generateAudio : boolean | undefined ,
1093+ useHostedCostTracking : boolean ,
10851094 requestId : string ,
10861095 logger : ReturnType < typeof createLogger >
1087- ) : Promise < { buffer : Buffer ; width : number ; height : number ; jobId : string ; duration : number } > {
1096+ ) : Promise < {
1097+ buffer : Buffer
1098+ width : number
1099+ height : number
1100+ jobId : string
1101+ duration : number
1102+ falaiCost ?: FalAICostMetadata
1103+ } > {
10881104 logger . info ( `[${ requestId } ] Starting Fal.ai generation with model: ${ model } ` )
10891105
10901106 const modelConfig = FALAI_MODEL_CONFIGS [ model ]
@@ -1229,6 +1245,13 @@ async function generateWithFalAI(
12291245 height,
12301246 jobId : requestIdFal ,
12311247 duration : getNumberProperty ( videoOutput , 'duration' ) || duration || 5 ,
1248+ falaiCost : useHostedCostTracking
1249+ ? await getFalAICostMetadata ( {
1250+ apiKey,
1251+ endpointId : modelConfig . endpoint ,
1252+ requestId : requestIdFal ,
1253+ } )
1254+ : undefined ,
12321255 }
12331256 }
12341257
0 commit comments