|
| 1 | +import { resolve } from "node:path"; |
| 2 | +import { parseWorkflowEventPayload } from "./workflow-contracts.js"; |
| 3 | +import type { WorkflowStore } from "./workflow-store.js"; |
| 4 | +import type { |
| 5 | + WorkflowAgentCallRecord, |
| 6 | + WorkflowAgentCallStatus, |
| 7 | + WorkflowErrorKind, |
| 8 | + WorkflowEventRecord, |
| 9 | + WorkflowEventType, |
| 10 | + WorkflowRunRecord, |
| 11 | + WorkflowRunSource, |
| 12 | + WorkflowRunStatus, |
| 13 | +} from "./workflow-types.js"; |
| 14 | + |
| 15 | +export const ACTIVE_WORKFLOW_STATUSES = ["starting", "running"] as const satisfies readonly WorkflowRunStatus[]; |
| 16 | + |
| 17 | +export interface WorkflowCallCounts { |
| 18 | + running: number; |
| 19 | + completed: number; |
| 20 | + cached: number; |
| 21 | + failed: number; |
| 22 | + cancelled: number; |
| 23 | + observed: number; |
| 24 | +} |
| 25 | + |
| 26 | +export interface WorkflowCallView { |
| 27 | + callIndex: number; |
| 28 | + status: WorkflowAgentCallStatus; |
| 29 | + provider: string; |
| 30 | + model?: string; |
| 31 | + effort?: string; |
| 32 | + label?: string; |
| 33 | + phase?: string; |
| 34 | + isolation: "shared" | "worktree"; |
| 35 | + worktreePath?: string; |
| 36 | + dirty?: boolean; |
| 37 | + fromCache: boolean; |
| 38 | + replayMatch?: "same_index" | "compatible_key"; |
| 39 | + replayedFromRunId?: string; |
| 40 | + replayedFromCallIndex?: number; |
| 41 | + replayReason?: string; |
| 42 | + error?: string; |
| 43 | + errorKind?: WorkflowErrorKind; |
| 44 | + startedAt?: string; |
| 45 | + completedAt?: string; |
| 46 | + updatedAt: string; |
| 47 | +} |
| 48 | + |
| 49 | +export interface WorkflowPhaseView { |
| 50 | + title: string; |
| 51 | + calls: WorkflowCallView[]; |
| 52 | +} |
| 53 | + |
| 54 | +export interface WorkflowActivityView { |
| 55 | + seq: number; |
| 56 | + type: WorkflowEventType; |
| 57 | + phase?: string; |
| 58 | + label?: string; |
| 59 | + detail?: string; |
| 60 | + createdAt: string; |
| 61 | +} |
| 62 | + |
| 63 | +export interface WorkflowRunView { |
| 64 | + id: string; |
| 65 | + name: string; |
| 66 | + status: WorkflowRunStatus; |
| 67 | + source: WorkflowRunSource; |
| 68 | + scriptPath: string; |
| 69 | + scriptHash: string; |
| 70 | + workspaceRoot: string; |
| 71 | + resumedFromRunId?: string; |
| 72 | + currentPhase?: string; |
| 73 | + calls: WorkflowCallCounts; |
| 74 | + phases: WorkflowPhaseView[]; |
| 75 | + unphasedCalls: WorkflowCallView[]; |
| 76 | + recentActivity: WorkflowActivityView[]; |
| 77 | + latestEventSeq: number; |
| 78 | + version: string; |
| 79 | + error?: string; |
| 80 | + errorKind?: WorkflowErrorKind; |
| 81 | + createdAt: string; |
| 82 | + startedAt?: string; |
| 83 | + completedAt?: string; |
| 84 | + updatedAt: string; |
| 85 | +} |
| 86 | + |
| 87 | +export interface WorkflowProjectView { |
| 88 | + workspaceRoot: string; |
| 89 | + runs: WorkflowRunView[]; |
| 90 | + version: string; |
| 91 | +} |
| 92 | + |
| 93 | +export function loadWorkflowProjectView( |
| 94 | + store: WorkflowStore, |
| 95 | + workspaceRoot: string, |
| 96 | + options: { |
| 97 | + statuses?: WorkflowRunStatus[]; |
| 98 | + limit?: number; |
| 99 | + eventLimit?: number; |
| 100 | + } = {}, |
| 101 | +): WorkflowProjectView { |
| 102 | + const root = resolve(workspaceRoot); |
| 103 | + const runs = store |
| 104 | + .listRunsForWorkspace(root, { |
| 105 | + statuses: options.statuses, |
| 106 | + limit: options.limit, |
| 107 | + }) |
| 108 | + .map((run) => |
| 109 | + buildWorkflowRunView( |
| 110 | + run, |
| 111 | + store.listAgentCalls(run.id), |
| 112 | + store.listEvents(run.id, options.eventLimit ?? 100), |
| 113 | + ), |
| 114 | + ); |
| 115 | + |
| 116 | + return { |
| 117 | + workspaceRoot: root, |
| 118 | + runs, |
| 119 | + version: runs.map((run) => `${run.id}:${run.version}`).join("|"), |
| 120 | + }; |
| 121 | +} |
| 122 | + |
| 123 | +export function buildWorkflowRunView( |
| 124 | + run: WorkflowRunRecord, |
| 125 | + calls: WorkflowAgentCallRecord[], |
| 126 | + events: WorkflowEventRecord[], |
| 127 | +): WorkflowRunView { |
| 128 | + const callViews = calls.map(toCallView); |
| 129 | + const phaseOrder: string[] = []; |
| 130 | + let currentPhase: string | undefined; |
| 131 | + |
| 132 | + for (const event of events) { |
| 133 | + if (event.type !== "phase_started") continue; |
| 134 | + const title = event.phase ?? parsePhaseTitle(event); |
| 135 | + if (!title) continue; |
| 136 | + currentPhase = title; |
| 137 | + if (!phaseOrder.includes(title)) phaseOrder.push(title); |
| 138 | + } |
| 139 | + for (const call of callViews) { |
| 140 | + if (call.phase && !phaseOrder.includes(call.phase)) phaseOrder.push(call.phase); |
| 141 | + } |
| 142 | + |
| 143 | + const phases = phaseOrder.map((title) => ({ |
| 144 | + title, |
| 145 | + calls: callViews.filter((call) => call.phase === title), |
| 146 | + })); |
| 147 | + const latestEventSeq = events.at(-1)?.seq ?? 0; |
| 148 | + const latestCallUpdate = calls.reduce( |
| 149 | + (latest, call) => call.updatedAt > latest ? call.updatedAt : latest, |
| 150 | + run.updatedAt, |
| 151 | + ); |
| 152 | + |
| 153 | + return { |
| 154 | + id: run.id, |
| 155 | + name: run.name, |
| 156 | + status: run.status, |
| 157 | + source: run.source, |
| 158 | + scriptPath: run.scriptPath, |
| 159 | + scriptHash: run.scriptHash, |
| 160 | + workspaceRoot: run.workspaceRoot, |
| 161 | + resumedFromRunId: run.resumedFromRunId, |
| 162 | + currentPhase, |
| 163 | + calls: countCalls(callViews), |
| 164 | + phases, |
| 165 | + unphasedCalls: callViews.filter((call) => !call.phase), |
| 166 | + recentActivity: events.map(toActivityView), |
| 167 | + latestEventSeq, |
| 168 | + version: `${run.updatedAt}:${latestCallUpdate}:${latestEventSeq}`, |
| 169 | + error: run.error, |
| 170 | + errorKind: run.errorKind, |
| 171 | + createdAt: run.createdAt, |
| 172 | + startedAt: run.startedAt, |
| 173 | + completedAt: run.completedAt, |
| 174 | + updatedAt: run.updatedAt, |
| 175 | + }; |
| 176 | +} |
| 177 | + |
| 178 | +function toCallView(call: WorkflowAgentCallRecord): WorkflowCallView { |
| 179 | + return { |
| 180 | + callIndex: call.callIndex, |
| 181 | + status: call.status, |
| 182 | + provider: call.provider, |
| 183 | + model: call.model, |
| 184 | + effort: call.effort, |
| 185 | + label: call.label, |
| 186 | + phase: call.phase, |
| 187 | + isolation: call.isolation, |
| 188 | + worktreePath: call.worktreePath, |
| 189 | + dirty: call.dirty, |
| 190 | + fromCache: call.fromCache, |
| 191 | + replayMatch: call.replayMatch, |
| 192 | + replayedFromRunId: call.replayedFromRunId, |
| 193 | + replayedFromCallIndex: call.replayedFromCallIndex, |
| 194 | + replayReason: call.replayReason, |
| 195 | + error: call.error, |
| 196 | + errorKind: call.errorKind, |
| 197 | + startedAt: call.startedAt, |
| 198 | + completedAt: call.completedAt, |
| 199 | + updatedAt: call.updatedAt, |
| 200 | + }; |
| 201 | +} |
| 202 | + |
| 203 | +function countCalls(calls: WorkflowCallView[]): WorkflowCallCounts { |
| 204 | + const counts: WorkflowCallCounts = { |
| 205 | + running: 0, |
| 206 | + completed: 0, |
| 207 | + cached: 0, |
| 208 | + failed: 0, |
| 209 | + cancelled: 0, |
| 210 | + observed: calls.length, |
| 211 | + }; |
| 212 | + for (const call of calls) { |
| 213 | + if (call.status === "running") counts.running += 1; |
| 214 | + else if (call.status === "completed") counts.completed += 1; |
| 215 | + else if (call.status === "from_cache") counts.cached += 1; |
| 216 | + else if (call.status === "failed") counts.failed += 1; |
| 217 | + else if (call.status === "cancelled") counts.cancelled += 1; |
| 218 | + } |
| 219 | + return counts; |
| 220 | +} |
| 221 | + |
| 222 | +function toActivityView(event: WorkflowEventRecord): WorkflowActivityView { |
| 223 | + return { |
| 224 | + seq: event.seq, |
| 225 | + type: event.type, |
| 226 | + phase: event.phase, |
| 227 | + label: event.label, |
| 228 | + detail: activityDetail(event), |
| 229 | + createdAt: event.createdAt, |
| 230 | + }; |
| 231 | +} |
| 232 | + |
| 233 | +function activityDetail(event: WorkflowEventRecord): string | undefined { |
| 234 | + try { |
| 235 | + if (event.type === "log") { |
| 236 | + return parseWorkflowEventPayload("log", JSON.parse(event.dataJson) as unknown).message; |
| 237 | + } |
| 238 | + if (event.type === "agent_call_failed") { |
| 239 | + return parseWorkflowEventPayload( |
| 240 | + "agent_call_failed", |
| 241 | + JSON.parse(event.dataJson) as unknown, |
| 242 | + ).error; |
| 243 | + } |
| 244 | + } catch { |
| 245 | + return undefined; |
| 246 | + } |
| 247 | + return undefined; |
| 248 | +} |
| 249 | + |
| 250 | +function parsePhaseTitle(event: WorkflowEventRecord): string | undefined { |
| 251 | + try { |
| 252 | + return parseWorkflowEventPayload( |
| 253 | + "phase_started", |
| 254 | + JSON.parse(event.dataJson) as unknown, |
| 255 | + ).title; |
| 256 | + } catch { |
| 257 | + return undefined; |
| 258 | + } |
| 259 | +} |
0 commit comments