@@ -4,7 +4,7 @@ import { z } from 'zod'
44import { getSession } from '@/lib/auth'
55import { createLogger } from '@/lib/logs/console-logger'
66import { db } from '@/db'
7- import { permissions , workflow , workflowExecutionBlocks , workflowExecutionLogs } from '@/db/schema'
7+ import { permissions , workflow , workflowExecutionLogs } from '@/db/schema'
88
99const logger = createLogger ( 'EnhancedLogsAPI' )
1010
@@ -183,56 +183,8 @@ export async function GET(request: NextRequest) {
183183
184184 const count = countResult [ 0 ] ?. count || 0
185185
186- // Get block executions for all workflow executions
187- const executionIds = logs . map ( ( log ) => log . executionId )
188- let blockExecutionsByExecution : Record < string , any [ ] > = { }
189-
190- if ( executionIds . length > 0 ) {
191- const blockLogs = await db
192- . select ( )
193- . from ( workflowExecutionBlocks )
194- . where ( inArray ( workflowExecutionBlocks . executionId , executionIds ) )
195- . orderBy ( workflowExecutionBlocks . startedAt )
196-
197- // Group block logs by execution ID
198- blockExecutionsByExecution = blockLogs . reduce (
199- ( acc , blockLog ) => {
200- if ( ! acc [ blockLog . executionId ] ) {
201- acc [ blockLog . executionId ] = [ ]
202- }
203- acc [ blockLog . executionId ] . push ( {
204- id : blockLog . id ,
205- blockId : blockLog . blockId ,
206- blockName : blockLog . blockName || '' ,
207- blockType : blockLog . blockType ,
208- startedAt : blockLog . startedAt . toISOString ( ) ,
209- endedAt : blockLog . endedAt ?. toISOString ( ) || blockLog . startedAt . toISOString ( ) ,
210- durationMs : blockLog . durationMs || 0 ,
211- status : blockLog . status ,
212- errorMessage : blockLog . errorMessage || undefined ,
213- errorStackTrace : blockLog . errorStackTrace || undefined ,
214- inputData : blockLog . inputData ,
215- outputData : blockLog . outputData ,
216- cost : blockLog . costTotal
217- ? {
218- input : Number ( blockLog . costInput ) || 0 ,
219- output : Number ( blockLog . costOutput ) || 0 ,
220- total : Number ( blockLog . costTotal ) || 0 ,
221- tokens : {
222- prompt : blockLog . tokensPrompt || 0 ,
223- completion : blockLog . tokensCompletion || 0 ,
224- total : blockLog . tokensTotal || 0 ,
225- } ,
226- model : blockLog . modelUsed || '' ,
227- }
228- : undefined ,
229- metadata : blockLog . metadata || { } ,
230- } )
231- return acc
232- } ,
233- { } as Record < string , any [ ] >
234- )
235- }
186+ // Block executions are now extracted from trace spans instead of separate table
187+ const blockExecutionsByExecution : Record < string , any [ ] > = { }
236188
237189 // Create clean trace spans from block executions
238190 const createTraceSpans = ( blockExecutions : any [ ] ) => {
@@ -397,87 +349,38 @@ export async function GET(request: NextRequest) {
397349
398350 // Include block execution data if requested
399351 if ( params . includeBlocks ) {
400- const executionIds = logs . map ( ( log ) => log . executionId )
401-
402- if ( executionIds . length > 0 ) {
403- const blockLogs = await db
404- . select ( )
405- . from ( workflowExecutionBlocks )
406- . where ( inArray ( workflowExecutionBlocks . executionId , executionIds ) )
407- . orderBy ( workflowExecutionBlocks . startedAt )
408-
409- // Group block logs by execution ID
410- const blockLogsByExecution = blockLogs . reduce (
411- ( acc , blockLog ) => {
412- if ( ! acc [ blockLog . executionId ] ) {
413- acc [ blockLog . executionId ] = [ ]
414- }
415- acc [ blockLog . executionId ] . push ( {
416- id : blockLog . id ,
417- blockId : blockLog . blockId ,
418- blockName : blockLog . blockName || '' ,
419- blockType : blockLog . blockType ,
420- startedAt : blockLog . startedAt . toISOString ( ) ,
421- endedAt : blockLog . endedAt ?. toISOString ( ) || blockLog . startedAt . toISOString ( ) ,
422- durationMs : blockLog . durationMs || 0 ,
423- status : blockLog . status ,
424- errorMessage : blockLog . errorMessage || undefined ,
425- inputData : blockLog . inputData ,
426- outputData : blockLog . outputData ,
427- cost : blockLog . costTotal
428- ? {
429- input : Number ( blockLog . costInput ) || 0 ,
430- output : Number ( blockLog . costOutput ) || 0 ,
431- total : Number ( blockLog . costTotal ) || 0 ,
432- tokens : {
433- prompt : blockLog . tokensPrompt || 0 ,
434- completion : blockLog . tokensCompletion || 0 ,
435- total : blockLog . tokensTotal || 0 ,
436- } ,
437- model : blockLog . modelUsed || '' ,
438- }
439- : undefined ,
440- } )
441- return acc
442- } ,
443- { } as Record < string , any [ ] >
444- )
352+ // Block executions are now extracted from stored trace spans in metadata
353+ const blockLogsByExecution : Record < string , any [ ] > = { }
354+
355+ logs . forEach ( ( log ) => {
356+ const storedTraceSpans = ( log . metadata as any ) ?. traceSpans
357+ if ( storedTraceSpans && Array . isArray ( storedTraceSpans ) ) {
358+ blockLogsByExecution [ log . executionId ] =
359+ extractBlockExecutionsFromTraceSpans ( storedTraceSpans )
360+ } else {
361+ blockLogsByExecution [ log . executionId ] = [ ]
362+ }
363+ } )
445364
446- // For executions with no block logs in the database,
447- // extract block executions from stored trace spans in metadata
448- logs . forEach ( ( log ) => {
449- if (
450- ! blockLogsByExecution [ log . executionId ] ||
451- blockLogsByExecution [ log . executionId ] . length === 0
452- ) {
453- const storedTraceSpans = ( log . metadata as any ) ?. traceSpans
454- if ( storedTraceSpans && Array . isArray ( storedTraceSpans ) ) {
455- blockLogsByExecution [ log . executionId ] =
456- extractBlockExecutionsFromTraceSpans ( storedTraceSpans )
457- }
458- }
459- } )
460-
461- // Add block logs to metadata
462- const logsWithBlocks = enhancedLogs . map ( ( log ) => ( {
463- ...log ,
464- metadata : {
465- ...log . metadata ,
466- blockExecutions : blockLogsByExecution [ log . executionId ] || [ ] ,
467- } ,
468- } ) )
469-
470- return NextResponse . json (
471- {
472- data : logsWithBlocks ,
473- total : Number ( count ) ,
474- page : Math . floor ( params . offset / params . limit ) + 1 ,
475- pageSize : params . limit ,
476- totalPages : Math . ceil ( Number ( count ) / params . limit ) ,
477- } ,
478- { status : 200 }
479- )
480- }
365+ // Add block logs to metadata
366+ const logsWithBlocks = enhancedLogs . map ( ( log ) => ( {
367+ ...log ,
368+ metadata : {
369+ ...log . metadata ,
370+ blockExecutions : blockLogsByExecution [ log . executionId ] || [ ] ,
371+ } ,
372+ } ) )
373+
374+ return NextResponse . json (
375+ {
376+ data : logsWithBlocks ,
377+ total : Number ( count ) ,
378+ page : Math . floor ( params . offset / params . limit ) + 1 ,
379+ pageSize : params . limit ,
380+ totalPages : Math . ceil ( Number ( count ) / params . limit ) ,
381+ } ,
382+ { status : 200 }
383+ )
481384 }
482385
483386 // Return basic logs
0 commit comments