@@ -69,7 +69,7 @@ function safeStringify(value: unknown): string {
6969}
7070
7171async function updateUserStatsForWand (
72- workflowId : string ,
72+ userId : string ,
7373 usage : {
7474 prompt_tokens ?: number
7575 completion_tokens ?: number
@@ -88,21 +88,6 @@ async function updateUserStatsForWand(
8888 }
8989
9090 try {
91- const [ workflowRecord ] = await db
92- . select ( { userId : workflow . userId , workspaceId : workflow . workspaceId } )
93- . from ( workflow )
94- . where ( eq ( workflow . id , workflowId ) )
95- . limit ( 1 )
96-
97- if ( ! workflowRecord ?. userId ) {
98- logger . warn (
99- `[${ requestId } ] No user found for workflow ${ workflowId } , cannot update user stats`
100- )
101- return
102- }
103-
104- const userId = workflowRecord . userId
105- const workspaceId = workflowRecord . workspaceId
10691 const totalTokens = usage . total_tokens || 0
10792 const promptTokens = usage . prompt_tokens || 0
10893 const completionTokens = usage . completion_tokens || 0
@@ -146,8 +131,6 @@ async function updateUserStatsForWand(
146131 inputTokens : promptTokens ,
147132 outputTokens : completionTokens ,
148133 cost : costToStore ,
149- workspaceId : workspaceId ?? undefined ,
150- workflowId,
151134 } )
152135
153136 await checkAndBillOverageThreshold ( userId )
@@ -325,6 +308,11 @@ export async function POST(req: NextRequest) {
325308
326309 if ( data === '[DONE]' ) {
327310 logger . info ( `[${ requestId } ] Received [DONE] signal` )
311+
312+ if ( finalUsage ) {
313+ await updateUserStatsForWand ( session . user . id , finalUsage , requestId )
314+ }
315+
328316 controller . enqueue (
329317 encoder . encode ( `data: ${ JSON . stringify ( { done : true } ) } \n\n` )
330318 )
@@ -353,10 +341,6 @@ export async function POST(req: NextRequest) {
353341 `[${ requestId } ] Received usage data: ${ JSON . stringify ( parsed . usage ) } `
354342 )
355343 }
356-
357- if ( chunkCount % 10 === 0 ) {
358- logger . debug ( `[${ requestId } ] Processed ${ chunkCount } chunks` )
359- }
360344 } catch ( parseError ) {
361345 logger . debug (
362346 `[${ requestId } ] Skipped non-JSON line: ${ data . substring ( 0 , 100 ) } `
@@ -365,12 +349,6 @@ export async function POST(req: NextRequest) {
365349 }
366350 }
367351 }
368-
369- logger . info ( `[${ requestId } ] Wand generation streaming completed successfully` )
370-
371- if ( finalUsage && workflowId ) {
372- await updateUserStatsForWand ( workflowId , finalUsage , requestId )
373- }
374352 } catch ( streamError : any ) {
375353 logger . error ( `[${ requestId } ] Streaming error` , {
376354 name : streamError ?. name ,
@@ -438,8 +416,8 @@ export async function POST(req: NextRequest) {
438416
439417 logger . info ( `[${ requestId } ] Wand generation successful` )
440418
441- if ( completion . usage && workflowId ) {
442- await updateUserStatsForWand ( workflowId , completion . usage , requestId )
419+ if ( completion . usage ) {
420+ await updateUserStatsForWand ( session . user . id , completion . usage , requestId )
443421 }
444422
445423 return NextResponse . json ( { success : true , content : generatedContent } )
0 commit comments