|
| 1 | +// Maps Durable Core coordination projections into Workboard card patches. |
| 2 | +import type { WorkboardCardPatch } from "./store.js"; |
| 3 | +import type { |
| 4 | + WorkboardDurableChildCounts, |
| 5 | + WorkboardDurableMetadata, |
| 6 | + WorkboardStatus, |
| 7 | +} from "./types.js"; |
| 8 | + |
| 9 | +type DurableProjectionLike = { |
| 10 | + workflowRunId?: unknown; |
| 11 | + workflowId?: unknown; |
| 12 | + workflowVersion?: unknown; |
| 13 | + status?: unknown; |
| 14 | + recoveryState?: unknown; |
| 15 | + waitingReason?: unknown; |
| 16 | + currentStepId?: unknown; |
| 17 | + updatedAt?: unknown; |
| 18 | + external?: unknown; |
| 19 | + children?: unknown; |
| 20 | +}; |
| 21 | + |
| 22 | +function stringValue(value: unknown): string | undefined { |
| 23 | + return typeof value === "string" && value.trim() ? value.trim() : undefined; |
| 24 | +} |
| 25 | + |
| 26 | +function numberValue(value: unknown): number | undefined { |
| 27 | + return typeof value === "number" && Number.isFinite(value) ? value : undefined; |
| 28 | +} |
| 29 | + |
| 30 | +function recordValue(value: unknown): Record<string, unknown> { |
| 31 | + return value && typeof value === "object" && !Array.isArray(value) |
| 32 | + ? (value as Record<string, unknown>) |
| 33 | + : {}; |
| 34 | +} |
| 35 | + |
| 36 | +function childCounts(value: unknown): WorkboardDurableChildCounts | undefined { |
| 37 | + const record = recordValue(value); |
| 38 | + const next: WorkboardDurableChildCounts = {}; |
| 39 | + for (const key of [ |
| 40 | + "total", |
| 41 | + "pending", |
| 42 | + "running", |
| 43 | + "succeeded", |
| 44 | + "failed", |
| 45 | + "cancelled", |
| 46 | + "lost", |
| 47 | + "terminal", |
| 48 | + "open", |
| 49 | + ] as const) { |
| 50 | + const count = numberValue(record[key]); |
| 51 | + if (count !== undefined) { |
| 52 | + next[key] = Math.max(0, Math.trunc(count)); |
| 53 | + } |
| 54 | + } |
| 55 | + return Object.keys(next).length ? next : undefined; |
| 56 | +} |
| 57 | + |
| 58 | +function statusFromDurable(status: string | undefined): WorkboardStatus | undefined { |
| 59 | + switch (status) { |
| 60 | + case "received": |
| 61 | + case "queued": |
| 62 | + case "running": |
| 63 | + case "waiting": |
| 64 | + case "waiting_signal": |
| 65 | + case "waiting_timer": |
| 66 | + case "waiting_child": |
| 67 | + case "retry_scheduled": |
| 68 | + return "running"; |
| 69 | + case "succeeded": |
| 70 | + return "review"; |
| 71 | + case "failed": |
| 72 | + case "cancelled": |
| 73 | + case "lost": |
| 74 | + return "blocked"; |
| 75 | + default: |
| 76 | + return undefined; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +export function buildWorkboardDurableMetadata( |
| 81 | + projection: DurableProjectionLike, |
| 82 | +): WorkboardDurableMetadata | undefined { |
| 83 | + const external = recordValue(projection.external); |
| 84 | + const workflowRunId = stringValue(projection.workflowRunId); |
| 85 | + if (!workflowRunId) { |
| 86 | + return undefined; |
| 87 | + } |
| 88 | + const updatedAt = numberValue(projection.updatedAt) ?? Date.now(); |
| 89 | + const children = childCounts(projection.children); |
| 90 | + const workflowId = stringValue(projection.workflowId); |
| 91 | + const workflowVersion = stringValue(projection.workflowVersion); |
| 92 | + const status = stringValue(projection.status); |
| 93 | + const recoveryState = stringValue(projection.recoveryState); |
| 94 | + const waitingReason = stringValue(projection.waitingReason); |
| 95 | + const currentStepId = stringValue(projection.currentStepId); |
| 96 | + const taskId = stringValue(external.taskId); |
| 97 | + const taskFlowId = stringValue(external.taskFlowId); |
| 98 | + const sessionKey = stringValue(external.sessionKey); |
| 99 | + const childSessionKey = stringValue(external.childSessionKey); |
| 100 | + const runId = stringValue(external.runId); |
| 101 | + const agentId = stringValue(external.agentId); |
| 102 | + const requesterAgentId = stringValue(external.requesterAgentId); |
| 103 | + return { |
| 104 | + workflowRunId, |
| 105 | + ...(workflowId ? { workflowId } : {}), |
| 106 | + ...(workflowVersion ? { workflowVersion } : {}), |
| 107 | + ...(status ? { status } : {}), |
| 108 | + ...(recoveryState ? { recoveryState } : {}), |
| 109 | + ...(waitingReason ? { waitingReason } : {}), |
| 110 | + ...(currentStepId ? { currentStepId } : {}), |
| 111 | + ...(taskId ? { taskId } : {}), |
| 112 | + ...(taskFlowId ? { taskFlowId } : {}), |
| 113 | + ...(sessionKey ? { sessionKey } : {}), |
| 114 | + ...(childSessionKey ? { childSessionKey } : {}), |
| 115 | + ...(runId ? { runId } : {}), |
| 116 | + ...(agentId ? { agentId } : {}), |
| 117 | + ...(requesterAgentId ? { requesterAgentId } : {}), |
| 118 | + timelineCommand: `openclaw durable timeline ${workflowRunId}`, |
| 119 | + ...(children ? { children } : {}), |
| 120 | + updatedAt, |
| 121 | + }; |
| 122 | +} |
| 123 | + |
| 124 | +export function buildWorkboardPatchFromDurableProjection( |
| 125 | + projection: DurableProjectionLike, |
| 126 | +): WorkboardCardPatch { |
| 127 | + const durable = buildWorkboardDurableMetadata(projection); |
| 128 | + if (!durable) { |
| 129 | + return {}; |
| 130 | + } |
| 131 | + const status = statusFromDurable(durable.status); |
| 132 | + return { |
| 133 | + ...(status ? { status } : {}), |
| 134 | + ...((durable.sessionKey ?? durable.childSessionKey) |
| 135 | + ? { sessionKey: durable.sessionKey ?? durable.childSessionKey } |
| 136 | + : {}), |
| 137 | + ...(durable.runId ? { runId: durable.runId } : {}), |
| 138 | + ...(durable.taskId ? { taskId: durable.taskId } : {}), |
| 139 | + metadata: { |
| 140 | + durable, |
| 141 | + lifecycleStatusSourceUpdatedAt: durable.updatedAt, |
| 142 | + }, |
| 143 | + }; |
| 144 | +} |
0 commit comments