feat(ui): workflow canvas renders each definition as a step graph with data edges, parallel branches and conditions

This commit is contained in:
sam committed 2026-10-05 00:27:32 +11:00
1 parent 876d25c548
commit fd6094541d
8 files changed
+679 -103

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
<script setup lang="ts">
import { Handle, Position } from "@vue-flow/core";
import { Flag, GitBranch, Play, Repeat, SkipForward } from "@lucide/vue";
import type { StepNodeData } from "./workflow-graph";
defineProps<{ data: StepNodeData }>();
</script>
<template>
<div
class="w-60 rounded-lg border bg-card px-3 py-2 text-left shadow-sm"
:class="data.kind === 'start' ? 'border-primary/60' : 'border-border'"
>
<Handle v-if="data.kind !== 'start'" type="target" :position="Position.Top" />
<Handle id="data-in" type="target" :position="Position.Right" class="!opacity-0" />
<div class="flex items-center gap-1.5">
<Play v-if="data.kind === 'start'" class="size-3.5 text-primary" aria-hidden="true" />
<span class="truncate text-sm font-medium" :title="data.title">{{ data.title }}</span>
<span v-if="data.branch" class="ml-auto flex items-center gap-0.5 text-[11px] text-fg-subtle">
<GitBranch class="size-3" aria-hidden="true" />{{ data.branch }}
</span>
</div>
<div v-if="data.type && data.type !== data.title" class="truncate font-mono text-[11px] text-fg-subtle">{{ data.type }}</div>
<div v-if="data.detail" class="mt-0.5 truncate text-xs text-muted-foreground" :title="data.detail">{{ data.detail }}</div>
<div v-if="data.when || data.retry || data.continues" class="mt-1 flex flex-wrap gap-1 text-[11px]">
<span v-if="data.when" class="flex max-w-full min-w-0 items-center gap-0.5 rounded bg-secondary px-1 font-mono" :title="`Runs only when ${data.when}`">
<Flag class="size-3 shrink-0" aria-hidden="true" /><span class="truncate">{{ data.when }}</span>
</span>
<span v-if="data.retry" class="flex items-center gap-0.5 rounded bg-secondary px-1" :title="`Up to ${data.retry} attempts`">
<Repeat class="size-3" aria-hidden="true" />×{{ data.retry }}
</span>
<span v-if="data.continues" class="flex items-center gap-0.5 rounded bg-secondary px-1" title="A failure does not stop the run">
<SkipForward class="size-3" aria-hidden="true" />continues
</span>
</div>
<Handle type="source" :position="Position.Bottom" />
<Handle id="data-out" type="source" :position="Position.Right" class="!opacity-0" />
</div>
</template>
+82
View File
@@ -0,0 +1,82 @@
<script setup lang="ts">
import { computed, ref } from "vue";
import { VueFlow, type VueFlowStore } from "@vue-flow/core";
import { Background } from "@vue-flow/background";
import { Controls } from "@vue-flow/controls";
import "@vue-flow/core/dist/style.css";
import "@vue-flow/controls/dist/style.css";
import StepNode from "./StepNode.vue";
import { workflowGraph } from "./workflow-graph";
const props = defineProps<{ yaml: string }>();
const NODE_WIDTH = 240;
const container = ref<HTMLElement | null>(null);
const graph = computed(() => workflowGraph(props.yaml));
const nodes = computed(() => graph.value.nodes);
const edges = computed(() =>
graph.value.edges.map(({ label, offset, ...edge }) => ({
...edge,
type: "smoothstep",
pathOptions: { offset: offset ?? 20, borderRadius: 8 },
class: edge.kind === "data" ? "workflow-data-edge" : "workflow-flow-edge",
// A data edge names the result it carries on hover, not on the canvas.
data: { label },
})),
);
// Start at the top at a readable size; a long workflow scrolls down rather
// than shrinking until nothing on it can be read.
function place(flow: VueFlowStore): void {
const width = container.value?.clientWidth ?? 800;
void flow.setViewport({ x: width / 2 - NODE_WIDTH / 2, y: 24, zoom: 0.9 });
}
</script>
<template>
<div ref="container" class="workflow-canvas h-[560px] overflow-hidden rounded-lg border border-border bg-background">
<VueFlow
:key="yaml"
:nodes="nodes"
:edges="edges"
:nodes-draggable="false"
:nodes-connectable="false"
:elements-selectable="false"
:min-zoom="0.3"
:max-zoom="1.5"
pan-on-scroll
@pane-ready="place"
>
<template #node-step="nodeProps">
<StepNode v-bind="nodeProps" />
</template>
<Background :gap="20" />
<Controls :show-interactive="false" />
</VueFlow>
</div>
</template>
<style>
.workflow-flow-edge path {
stroke: var(--color-muted-foreground);
stroke-width: 1.5;
}
.workflow-data-edge path {
stroke: var(--color-primary);
stroke-dasharray: 5 4;
stroke-width: 1.25;
}
.workflow-canvas .vue-flow__controls {
box-shadow: none;
}
.workflow-canvas .vue-flow__controls-button {
background: var(--color-card);
border-color: var(--color-border);
fill: var(--color-foreground);
}
.workflow-canvas .vue-flow__controls-button:hover {
background: var(--color-secondary);
}
</style>
+17 -33
View File
@@ -1,12 +1,14 @@
<script setup lang="ts">
import { computed, ref, watch } from "vue";
import { ArrowRight, RotateCcw, Trash2 } from "@lucide/vue";
import { RotateCcw, Trash2 } from "@lucide/vue";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Spinner } from "@/components/ui/spinner";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Textarea } from "@/components/ui/textarea";
import WorkflowCanvas from "./WorkflowCanvas.vue";
import {
cloneControlPlaneValue,
removeWorkflowDefinition,
@@ -105,7 +107,7 @@ async function restoreOne(): Promise<void> {
// not served, so a save the server refuses still reports its own reason below.
const vocabulary = computed(() => store.stepTypes());
const parsed = computed(() => parseWorkflowYaml(yaml.value, vocabulary.value));
const steps = computed(() => (parsed.value.ok ? parsed.value.steps : []));
const view = ref("canvas");
const lines = computed(() => yamlLines(yaml.value));
// The highlight layer sits behind the textarea and never scrolls by itself, so
@@ -123,36 +125,12 @@ function syncScroll(event: Event): void {
<Label for="workflow-id">ID</Label>
<Input id="workflow-id" v-model="id" name="workflow-id" required autocomplete="off" class="max-w-sm font-mono" />
</div>
<div class="space-y-1.5">
<!-- The steps the YAML actually declares, as the server would read them:
a parser's answer, never a scan for lines that look like steps. -->
<span class="text-sm leading-none font-medium">Steps</span>
<div
class="flex h-9 flex-nowrap items-center gap-1.5 overflow-hidden"
role="list"
aria-label="Steps"
>
<template v-if="steps.length">
<template v-for="(step, i) in steps" :key="step.index">
<span
role="listitem"
class="shrink-0 rounded border border-border bg-secondary px-2 py-0.5 font-mono text-xs"
:title="step.type"
>{{ step.type }}</span
>
<ArrowRight
v-if="i < steps.length - 1"
class="size-3.5 shrink-0 text-fg-subtle"
aria-hidden="true"
/>
</template>
</template>
<span v-else class="text-xs text-fg-subtle">No steps yet</span>
</div>
</div>
<div class="space-y-1.5">
<div class="flex items-baseline gap-2">
<Label for="workflow-yaml">YAML</Label>
<Tabs v-model="view" class="space-y-1.5">
<div class="flex items-center gap-2">
<TabsList>
<TabsTrigger value="canvas">Canvas</TabsTrigger>
<TabsTrigger value="yaml">YAML</TabsTrigger>
</TabsList>
<!-- Step settings are the server's to check, on save. -->
<span
class="ml-auto text-xs"
@@ -161,6 +139,10 @@ function syncScroll(event: Event): void {
>{{ parsed.ok ? "✓" : "✕" }} {{ validationLabel(parsed) }}</span
>
</div>
<TabsContent value="canvas">
<WorkflowCanvas :yaml="yaml" />
</TabsContent>
<TabsContent value="yaml">
<!--
A gutter and a highlighted layer behind the live textarea, so the YAML
is numbered and coloured where it is edited rather than in a second,
@@ -201,6 +183,7 @@ function syncScroll(event: Event): void {
</div>
<Textarea
id="workflow-yaml"
aria-label="Workflow YAML"
v-model="yaml"
name="workflow-yaml"
required
@@ -209,7 +192,8 @@ function syncScroll(event: Event): void {
@scroll="syncScroll"
/>
</div>
</div>
</TabsContent>
</Tabs>
<details v-if="vocabulary.length" class="text-sm">
<summary class="cursor-pointer text-fg-subtle">Step types ({{ vocabulary.length }})</summary>
<ul class="mt-2 flex flex-wrap gap-1.5" aria-label="Step types">
+205
View File
@@ -0,0 +1,205 @@
import { parseDocument } from "yaml";
/**
* A workflow definition laid out as a graph: a start node, each step as a
* node in run order, parallel branches side by side, and a dashed edge from
* every step whose result a later step reads.
*/
export interface StepNodeData {
kind: "start" | "step";
/** The step's id, or its 1-based position when it has none. */
key: string;
title: string;
type: string;
detail: string;
when?: string;
retry?: number;
continues?: boolean;
branch?: string;
}
export interface GraphNode {
id: string;
type: "step";
position: { x: number; y: number };
data: StepNodeData;
}
export interface GraphEdge {
id: string;
source: string;
target: string;
/** "flow" is run order; "data" is a reference to an earlier result. */
kind: "flow" | "data";
label?: string;
sourceHandle?: string;
targetHandle?: string;
offset?: number;
}
export interface WorkflowGraph {
nodes: GraphNode[];
edges: GraphEdge[];
}
const ROW = 120;
const COLUMN = 280;
const TYPE_TITLES: Record<string, string> = {
"agent.run": "Agent",
"command.run": "Run commands",
"repo.prepare": "Prepare worktree",
"repo.commit": "Commit",
"repo.open-pr": "Open pull request",
"gate.repository": "Repository gate",
"gate.diff-size": "Diff size gate",
"gate.diff-rules": "Diff rules gate",
"forge.comment": "Comment",
"forge.close-issue": "Close issue",
"human.approve": "Human approval",
"workflow.call": "Call workflow",
"workflow.handoff": "Hand off",
"workflow.finish": "Finish",
"review.pr": "Review pull request",
"review.start-round": "Start review round",
"review.reply": "Reply to review",
};
/** A step type's display name: a known one's title, else the type itself. */
export function stepTitle(type: string): string {
return TYPE_TITLES[type] ?? type;
}
const REFERENCE = /\{\{\s*steps\.([A-Za-z0-9_-]+)\.[^}]*\}\}/g;
type Mapping = Record<string, unknown>;
function isMapping(value: unknown): value is Mapping {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
/** One line saying what a step does, from the settings that matter most. */
function stepDetail(type: string, settings: Mapping): string {
const pick = (key: string) => (typeof settings[key] === "string" ? (settings[key] as string) : "");
switch (type) {
case "agent.run": {
const role = pick("role") || "builder";
const mode = settings.read_only ? "read-only" : pick("gate") ? `gated: ${pick("gate")}` : "";
return [role, mode].filter(Boolean).join(" · ");
}
case "repo.commit":
return [pick("message"), settings.push ? "push" : ""].filter(Boolean).join(" · ");
case "workflow.call":
case "workflow.handoff":
return pick("workflow");
case "workflow.finish":
return pick("status") || "completed";
case "human.approve":
return pick("then") ? `then ${pick("then")}` : "";
case "gate.repository":
return pick("expect") === "test-failure" ? "tests must fail" : settings.repair ? "repair if red" : "";
}
return "";
}
/** Every step id a step's settings reference. */
function referencedSteps(settings: unknown, when: unknown): string[] {
const text = JSON.stringify(settings ?? {}) + " " + (typeof when === "string" ? `{{ ${when.replace(/^!/, "")} }}` : "");
return [...new Set([...text.matchAll(REFERENCE)].map((match) => match[1]))];
}
export function workflowGraph(source: string): WorkflowGraph {
const value: unknown = parseDocument(source).toJS();
const steps = isMapping(value) && Array.isArray(value.steps) ? value.steps : [];
const nodes: GraphNode[] = [];
const edges: GraphEdge[] = [];
const byStepID = new Map<string, string>();
const pendingData: { from: string; to: string }[] = [];
const start = "start";
nodes.push({
id: start,
type: "step",
position: { x: 0, y: 0 },
data: { kind: "start", key: "start", title: "Start", type: "", detail: "an event or a task starts the run" },
});
let previous = [start];
let row = 1;
const addStep = (step: unknown, key: string, x: number, y: number, branch?: string): string => {
const record = isMapping(step) ? step : {};
const type = typeof record.type === "string" ? record.type : "";
const settings = isMapping(record.settings) ? record.settings : {};
const stepID = typeof record.id === "string" ? record.id : "";
const id = `step-${key}`;
nodes.push({
id,
type: "step",
position: { x, y },
data: {
kind: "step",
key: stepID || key,
title: stepID || stepTitle(type),
type,
detail: stepDetail(type, settings),
when: typeof record.when === "string" ? record.when : undefined,
retry: isMapping(record.retry) && typeof record.retry.attempts === "number" ? record.retry.attempts : undefined,
continues: record.on_failure === "continue",
branch,
},
});
if (stepID) byStepID.set(stepID, id);
for (const ref of referencedSteps(record.settings, record.when)) pendingData.push({ from: ref, to: id });
return id;
};
const link = (from: string[], to: string) => {
for (const source of from) edges.push({ id: `flow-${source}-${to}`, source, target: to, kind: "flow" });
};
for (const [i, step] of steps.entries()) {
if (isMapping(step) && isMapping(step.parallel)) {
const branches = Object.entries(step.parallel);
const ends: string[] = [];
let depth = 0;
branches.forEach(([name, branchSteps], column) => {
const x = (column - (branches.length - 1) / 2) * COLUMN;
let tail = previous;
(Array.isArray(branchSteps) ? branchSteps : []).forEach((branchStep, j) => {
const id = addStep(branchStep, `${i + 1}-${name}-${j + 1}`, x, (row + j) * ROW, name);
link(tail, id);
tail = [id];
depth = Math.max(depth, j + 1);
});
ends.push(...tail);
});
previous = ends;
row += Math.max(depth, 1);
continue;
}
const id = addStep(step, String(i + 1), 0, row * ROW);
link(previous, id);
previous = [id];
row++;
}
for (const { from, to } of pendingData) {
const source = byStepID.get(from);
if (!source) continue;
// A data edge that only restates the run-order edge adds nothing.
if (edges.some((edge) => edge.kind === "flow" && edge.source === source && edge.target === to)) continue;
edges.push({
id: `data-${source}-${to}`,
source,
target: to,
kind: "data",
label: from,
// Each data edge loops a little further out, so several from one step
// stay apart instead of drawing one line.
offset: 24 + 14 * edges.filter((edge) => edge.kind === "data").length,
sourceHandle: "data-out",
targetHandle: "data-in",
});
}
return { nodes, edges };
}