All files / web/src/stores executionStore.ts

0% Statements 0/148
0% Branches 0/1
0% Functions 0/1
0% Lines 0/148

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
import { defineStore } from 'pinia'
import type { TaskStatus } from '@/types/generated/TaskStatus'
 
export type NodeExecutionStatus = TaskStatus | 'skipped'
 
type Json = any
 
export interface NodeExecutionResult {
  nodeId: string
  status: NodeExecutionStatus
  startTime?: number
  endTime?: number
  executionTime?: number
  input?: Json
  output?: Json
  error?: string
  logs?: string[]
}
 
export interface ExecutionSummary {
  totalNodes: number
  success: number
  failed: number
  skipped: number
  running: number
  totalTime?: number
}
 
interface ExecutionState {
  currentExecutionId: string | null
  isExecuting: boolean
  nodeResults: Map<string, NodeExecutionResult>
  nodeResultsVersion: number
  selectedNodeId: string | null
  isEditingNode: boolean
}
 
export const useExecutionStore = defineStore('execution', {
  state: (): ExecutionState => ({
    currentExecutionId: null,
    isExecuting: false,
    nodeResults: new Map(),
    nodeResultsVersion: 0,
    selectedNodeId: null,
    isEditingNode: false,
  }),
 
  getters: {
    executionSummary(): ExecutionSummary | null {
      if (this.nodeResults.size === 0) return null
 
      const results = Array.from(this.nodeResults.values())
      const summary: ExecutionSummary = {
        totalNodes: results.length,
        success: results.filter(r => r.status === 'Completed').length,
        failed: results.filter(r => r.status === 'Failed').length,
        skipped: results.filter(r => r.status === 'skipped').length,
        running: results.filter(r => r.status === 'Running').length,
        totalTime: undefined,
      }
 
      // Calculate total execution time
      const completedResults = results.filter(r => r.startTime && r.endTime)
      if (completedResults.length > 0) {
        const minStart = Math.min(...completedResults.map(r => r.startTime!))
        const maxEnd = Math.max(...completedResults.map(r => r.endTime!))
        summary.totalTime = maxEnd - minStart
      }
 
      return summary
    },
 
    sortedNodeResults(): NodeExecutionResult[] {
      return Array.from(this.nodeResults.values()).sort((a, b) => {
        // Sort by start time, then by node ID
        if (a.startTime && b.startTime) {
          return a.startTime - b.startTime
        }
        return a.nodeId.localeCompare(b.nodeId)
      })
    },
 
    selectedNodeResult(): NodeExecutionResult | null {
      if (!this.selectedNodeId) return null
      return this.nodeResults.get(this.selectedNodeId) || null
    },
 
    hasResults(): boolean {
      return this.nodeResults.size > 0
    },
  },
 
  actions: {
    // Execution management
    startExecution(executionId: string) {
      this.currentExecutionId = executionId
      this.isExecuting = true
      this.nodeResults.clear()
      this.nodeResultsVersion++
      this.selectedNodeId = null
    },
 
    endExecution() {
      this.isExecuting = false
      // Auto-select first error node if any
      const errorNode = this.sortedNodeResults.find(r => r.status === 'Failed')
      if (errorNode) {
        this.selectedNodeId = errorNode.nodeId
      }
    },
 
    clearExecution() {
      this.currentExecutionId = null
      this.isExecuting = false
      this.nodeResults.clear()
      this.nodeResultsVersion++
      this.selectedNodeId = null
    },
 
    // Node results management
    setNodeResult(nodeId: string, result: Partial<NodeExecutionResult>) {
      const existing = this.nodeResults.get(nodeId) || { nodeId, status: 'Pending' as NodeExecutionStatus }
      const updated = { ...existing, ...result }
 
      // Preserve startTime if not provided
      if (!updated.startTime && 'startTime' in existing) {
        updated.startTime = existing.startTime
      }
 
      // Preserve input if not provided
      if (!updated.input && existing.input) {
        updated.input = existing.input
      }
 
      // Preserve output if not provided
      if (!updated.output && existing.output) {
        updated.output = existing.output
      }
 
      // Calculate execution time if both timestamps exist
      if (updated.startTime && updated.endTime) {
        updated.executionTime = updated.endTime - updated.startTime
      }
 
      this.nodeResults.set(nodeId, updated as NodeExecutionResult)
      this.nodeResultsVersion++
    },
 
    updateNodeStatus(nodeId: string, status: NodeExecutionStatus) {
      const result: NodeExecutionResult = this.nodeResults.get(nodeId) || { nodeId, status: 'Pending' as NodeExecutionStatus }
 
      if (status === 'Running') {
        result.startTime = Date.now()
      } else if (status === 'Completed' || status === 'Failed') {
        result.endTime = Date.now()
      }
 
      result.status = status
      this.nodeResults.set(nodeId, result)
      this.nodeResultsVersion++
    },
 
    // Parse execution context and populate results
    parseExecutionContext(context: any) {
      if (!context) return
 
      // Parse node outputs from execution context
      // Handle both formats: node_outputs or results
      const outputs = context.node_outputs || context.results
      if (!outputs) return
 
      Object.entries(outputs).forEach(([nodeId, output]) => {
        // Check if node was skipped
        if (output && typeof output === 'object' && (output as any).skipped) {
          this.setNodeResult(nodeId, {
            nodeId,
            status: 'skipped' as const,
            output,
            endTime: Date.now(),
          })
        } else {
          this.setNodeResult(nodeId, {
            nodeId,
            status: 'Completed',
            output,
            endTime: Date.now(),
          })
        }
      })
    },
 
    selectNode(nodeId: string | null) {
      this.selectedNodeId = nodeId
    },
 
    setEditingNode(editing: boolean) {
      this.isEditingNode = editing
    },
 
    getNodeStatus(nodeId: string): NodeExecutionStatus | null {
      const result = this.nodeResults.get(nodeId)
      return result?.status || null
    },
 
    getNodeResult(nodeId: string): NodeExecutionResult | null {
      return this.nodeResults.get(nodeId) || null
    },
 
    // Update from async execution tasks (the only way to update execution status)
    updateFromTasks(tasks: Array<{
      node_id: string
      status: NodeExecutionStatus
      input?: Json
      output?: Json
      error?: string | null
      started_at?: bigint | null
      completed_at?: bigint | null
    }>) {
      tasks.forEach(task => {
        this.setNodeResult(task.node_id, {
          nodeId: task.node_id,
          status: task.status,
          input: task.input,
          output: task.output,
          error: task.error || undefined,
          startTime: task.started_at ? Number(task.started_at) : undefined,
          endTime: task.completed_at ? Number(task.completed_at) : undefined,
        })
      })
    },
  },
})