import { isMap, isScalar, isSeq, parseDocument } from "yaml"; /** * Canvas edits as edits to the workflow's YAML document. The text stays the * one source of truth, so the canvas and the YAML tab cannot disagree, and * comments and layout the operator wrote survive an edit made on the canvas. */ /** Where a step lives in the document: ["steps", 2], or * ["steps", 1, "parallel", "docs", 0] for a step inside a branch. */ export type StepPath = (string | number)[]; export type StepRecord = Record; /** A step as plain data, or undefined when the path names none. */ export function stepAt(source: string, path: StepPath): StepRecord | undefined { const node = parseDocument(source).getIn(path, true); if (!isMap(node)) return undefined; return node.toJSON() as StepRecord; } /** Replaces one step with a new value. Unset keys are dropped, so a cleared * field leaves no empty key behind. */ export function replaceStep(source: string, path: StepPath, step: StepRecord): string { const document = parseDocument(source); document.setIn(path, document.createNode(prune(step))); return document.toString(); } /** Inserts after a step path; index -1 inserts first in that list. */ export function insertStep(source: string, after: StepPath, type: string): { source: string; path: StepPath } { const document = parseDocument(source); const list = after.slice(0, -1); const inBranch = list.length > 1; if (inBranch && !["agent.run", "workflow.call"].includes(type)) throw new Error("Branches allow only read-only agents and workflow calls."); let steps = document.getIn(list, true); if (!isSeq(steps)) { document.setIn(list, document.createNode([])); steps = document.getIn(list, true); } if (!isSeq(steps)) return { source, path: [] }; const index = Math.min(Math.max(Number(after.at(-1)) + 1, 0), steps.items.length); const step = type === "parallel" ? { parallel: { "branch-1": [], "branch-2": [] } } : { type, ...(inBranch && type === "agent.run" ? { settings: { read_only: true } } : {}) }; steps.items.splice(index, 0, document.createNode(step)); return { source: document.toString(), path: [...list, index] }; } function checkBranchName(name: string): void { if (!/^[a-z][a-z0-9]*(?:[.-][a-z0-9]+)*$/.test(name)) throw new Error("Use a lowercase branch name with letters, digits, dots or dashes."); } export function addBranch(source: string, path: StepPath, name: string): string { checkBranchName(name); const document = parseDocument(source); const branches = document.getIn([...path, "parallel"], true); if (!isMap(branches)) throw new Error("This step has no branches."); if (branches.has(name)) throw new Error(`Branch ${name} already exists.`); branches.set(name, document.createNode([])); return document.toString(); } export function renameBranch(source: string, path: StepPath, name: string, next: string): string { checkBranchName(next); const document = parseDocument(source); const branches = document.getIn([...path, "parallel"], true); if (!isMap(branches)) throw new Error("This step has no branches."); if (name !== next && branches.has(next)) throw new Error(`Branch ${next} already exists.`); const pair = branches.items.find((pair) => isScalar(pair.key) && pair.key.value === name); if (!pair || !isScalar(pair.key)) throw new Error(`Branch ${name} does not exist.`); pair.key.value = next; return document.toString(); } export function deleteBranch(source: string, path: StepPath, name: string): string { const document = parseDocument(source); const branches = document.getIn([...path, "parallel"], true); if (!isMap(branches) || branches.items.length <= 2) throw new Error("Parallel needs at least two branches."); branches.delete(name); return document.toString(); } /** Copies a step to just after itself. The copy drops the id, which must stay * unique. Returns the new source and the copy's path. */ export function duplicateStep(source: string, path: StepPath): { source: string; path: StepPath } { const document = parseDocument(source); const list = document.getIn(path.slice(0, -1), true); const index = path.at(-1); if (!isSeq(list) || typeof index !== "number") return { source, path }; const copy = document.createNode(document.getIn(path)); if (isMap(copy)) copy.delete("id"); list.items.splice(index + 1, 0, copy); return { source: document.toString(), path: [...path.slice(0, -1), index + 1] }; } /** Removes a step, leaving an empty branch available for authoring. */ export function deleteStep(source: string, path: StepPath): string { const document = parseDocument(source); document.deleteIn(path); return document.toString(); } /** Moves a step one place up (-1) or down (+1) within its own list. Returns * the new source and the step's new path. */ export function moveStep(source: string, path: StepPath, delta: -1 | 1): { source: string; path: StepPath } { const document = parseDocument(source); const list = document.getIn(path.slice(0, -1), true); const index = path.at(-1); if (!isSeq(list) || typeof index !== "number") return { source, path }; const target = index + delta; if (target < 0 || target >= list.items.length) return { source, path }; const [item] = list.items.splice(index, 1); list.items.splice(target, 0, item); return { source: document.toString(), path: [...path.slice(0, -1), target] }; } /** The ids every step before the given one declares, which are the steps its * settings may reference. Inside a branch, sibling branches are not visible. */ export function earlierStepIDs(source: string, path: StepPath): string[] { const value = parseDocument(source).toJS() as { steps?: unknown[] } | null; const steps = Array.isArray(value?.steps) ? value.steps : []; const top = typeof path[1] === "number" ? path[1] : steps.length; const ids: string[] = []; const collect = (step: unknown) => { if (typeof step !== "object" || step === null) return; const record = step as StepRecord; if (typeof record.id === "string") ids.push(record.id); if (typeof record.parallel === "object" && record.parallel !== null) for (const branch of Object.values(record.parallel as Record)) if (Array.isArray(branch)) branch.forEach(collect); }; steps.slice(0, top).forEach(collect); if (path.length === 5) { const branch = (steps[top] as StepRecord | undefined)?.parallel as Record | undefined; const own = branch?.[path[3] as string]; if (Array.isArray(own)) own.slice(0, path[4] as number).forEach(collect); } return ids; } function prune(value: unknown): unknown { if (Array.isArray(value)) return value.map(prune); if (typeof value !== "object" || value === null) return value; const out: Record = {}; for (const [key, child] of Object.entries(value)) { if (child === undefined || child === "" || child === null) continue; const pruned = prune(child); if (typeof pruned === "object" && pruned !== null && !Array.isArray(pruned) && Object.keys(pruned).length === 0) continue; out[key] = pruned; } return out; } /** A top-level workflow field (repository, inputs, profile, ...) as plain data. */ export function workflowField(source: string, key: string): unknown { const value = parseDocument(source).get(key, true); return value && typeof value === "object" && "toJSON" in value ? (value as { toJSON(): unknown }).toJSON() : parseDocument(source).get(key); } /** Sets a top-level workflow field, or removes it when the value is empty. */ export function setWorkflowField(source: string, key: string, value: unknown): string { const document = parseDocument(source); const pruned = (prune({ [key]: value }) as Record)[key]; if (pruned === undefined) document.delete(key); else document.set(key, document.createNode(pruned)); return document.toString(); }