(params: SessionCompleteParams = {})
| 374 | } |
| 375 | |
| 376 | async complete(params: SessionCompleteParams = {}): Promise<void> { |
| 377 | if (this.completed || this.completing) { |
| 378 | return |
| 379 | } |
| 380 | this.completing = true |
| 381 | |
| 382 | const { endedAt, totalDurationMs, finalOutput, traceSpans, workflowInput, executionState } = |
| 383 | params |
| 384 | |
| 385 | try { |
| 386 | const costSummary = calculateCostSummary(traceSpans || []) |
| 387 | const endTime = endedAt || new Date().toISOString() |
| 388 | const duration = totalDurationMs || 0 |
| 389 | |
| 390 | await this.completeExecutionWithFinalization({ |
| 391 | endedAt: endTime, |
| 392 | totalDurationMs: duration, |
| 393 | costSummary, |
| 394 | finalOutput: finalOutput || {}, |
| 395 | traceSpans: traceSpans || [], |
| 396 | workflowInput, |
| 397 | executionState, |
| 398 | finalizationPath: 'completed', |
| 399 | }) |
| 400 | |
| 401 | this.completed = true |
| 402 | |
| 403 | if (traceSpans && traceSpans.length > 0) { |
| 404 | try { |
| 405 | const { PlatformEvents, createOTelSpansForWorkflowExecution } = await import( |
| 406 | '@/lib/core/telemetry' |
| 407 | ) |
| 408 | |
| 409 | const hasErrors = traceSpans.some((span: any) => { |
| 410 | const checkForErrors = (s: any): boolean => { |
| 411 | if (s.status === 'error' && !s.errorHandled) return true |
| 412 | if (s.children && Array.isArray(s.children)) { |
| 413 | return s.children.some(checkForErrors) |
| 414 | } |
| 415 | return false |
| 416 | } |
| 417 | return checkForErrors(span) |
| 418 | }) |
| 419 | |
| 420 | PlatformEvents.workflowExecuted({ |
| 421 | workflowId: this.workflowId, |
| 422 | durationMs: duration, |
| 423 | status: hasErrors ? 'error' : 'success', |
| 424 | trigger: this.triggerType, |
| 425 | blocksExecuted: traceSpans.length, |
| 426 | hasErrors, |
| 427 | totalCost: costSummary.totalCost || 0, |
| 428 | }) |
| 429 | |
| 430 | const startTime = new Date(new Date(endTime).getTime() - duration).toISOString() |
| 431 | createOTelSpansForWorkflowExecution({ |
| 432 | workflowId: this.workflowId, |
| 433 | workflowName: this.workflowState?.metadata?.name, |
no test coverage detected