@@ -17,6 +17,7 @@ import {
1717 readResponseToBufferWithLimit ,
1818} from '@/lib/core/utils/stream-limits'
1919import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
20+ import { type FalAICostMetadata , getFalAICostMetadata } from '@/lib/tools/falai-pricing'
2021import { downloadFileFromStorage } from '@/lib/uploads/utils/file-utils.server'
2122import { assertToolFileAccess } from '@/app/api/files/authorization'
2223import type { UserFile } from '@/executor/types'
@@ -138,6 +139,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
138139 let height : number | undefined
139140 let jobId : string | undefined
140141 let actualDuration : number | undefined
142+ let falaiCost : FalAICostMetadata | undefined
141143
142144 if ( body . visualReference ) {
143145 const denied = await assertToolFileAccess (
@@ -236,6 +238,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
236238 resolution ,
237239 body . promptOptimizer ,
238240 body . generateAudio ,
241+ body . useHostedCostTracking === true ,
239242 requestId ,
240243 logger
241244 )
@@ -244,6 +247,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
244247 height = result . height
245248 jobId = result . jobId
246249 actualDuration = result . duration
250+ falaiCost = result . falaiCost
247251 } else {
248252 return NextResponse . json ( { error : `Unknown provider: ${ provider } ` } , { status : 400 } )
249253 }
@@ -301,6 +305,8 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
301305 provider,
302306 model : model || 'default' ,
303307 jobId,
308+ __falaiCostDollars : falaiCost ?. costDollars ,
309+ __falaiBilling : falaiCost ,
304310 } )
305311 }
306312
@@ -333,6 +339,8 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
333339 provider,
334340 model : model || 'default' ,
335341 jobId,
342+ __falaiCostDollars : falaiCost ?. costDollars ,
343+ __falaiBilling : falaiCost ,
336344 } )
337345 } catch ( error ) {
338346 logger . error ( `[${ requestId } ] Video proxy error:` , error )
@@ -1167,9 +1175,17 @@ async function generateWithFalAI(
11671175 resolution : string | undefined ,
11681176 promptOptimizer : boolean | undefined ,
11691177 generateAudio : boolean | undefined ,
1178+ useHostedCostTracking : boolean ,
11701179 requestId : string ,
11711180 logger : ReturnType < typeof createLogger >
1172- ) : Promise < { buffer : Buffer ; width : number ; height : number ; jobId : string ; duration : number } > {
1181+ ) : Promise < {
1182+ buffer : Buffer
1183+ width : number
1184+ height : number
1185+ jobId : string
1186+ duration : number
1187+ falaiCost ?: FalAICostMetadata
1188+ } > {
11731189 logger . info ( `[${ requestId } ] Starting Fal.ai generation with model: ${ model } ` )
11741190
11751191 const modelConfig = FALAI_MODEL_CONFIGS [ model ]
@@ -1312,6 +1328,13 @@ async function generateWithFalAI(
13121328 height,
13131329 jobId : requestIdFal ,
13141330 duration : getNumberProperty ( videoOutput , 'duration' ) || duration || 5 ,
1331+ falaiCost : useHostedCostTracking
1332+ ? await getFalAICostMetadata ( {
1333+ apiKey,
1334+ endpointId : modelConfig . endpoint ,
1335+ requestId : requestIdFal ,
1336+ } )
1337+ : undefined ,
13151338 }
13161339 }
13171340
0 commit comments