diff --git a/ui/src/copy/errors/workflows.ts b/ui/src/copy/errors/workflows.ts index ee429e388..13a05470f 100644 --- a/ui/src/copy/errors/workflows.ts +++ b/ui/src/copy/errors/workflows.ts @@ -26,12 +26,17 @@ export const workflows = { triggers: { notLoaded: "Couldn't load triggers", }, + specList: { + nameRequired: "A name is required.", + nameTaken: (name: string) => `${name} is already declared.`, + }, runs: { stagesNotLoaded: "Couldn't load the run's stages", }, editor: { restartFailed: "Restart failed", draftNotSaved: "Draft not saved. Your edit is still in the editor.", + stepIdTaken: (id: string) => `Another step is already called ${id}.`, draftChangedElsewhere: "This draft changed in another tab or by Archie. Your edit is still in the editor.", draftNotRemoved: "Published, but the old draft wasn't removed. Discard it from the menu.", recentRunsNotLoaded: (detail: string) => `Could not load recent runs: ${detail}`, diff --git a/ui/src/workflows/SpecList.vue b/ui/src/workflows/SpecList.vue index e6a0607e5..144c80798 100644 --- a/ui/src/workflows/SpecList.vue +++ b/ui/src/workflows/SpecList.vue @@ -1,11 +1,12 @@ diff --git a/ui/src/workflows/WorkflowEditor.vue b/ui/src/workflows/WorkflowEditor.vue index 79b036fa0..fedbabe89 100644 --- a/ui/src/workflows/WorkflowEditor.vue +++ b/ui/src/workflows/WorkflowEditor.vue @@ -36,7 +36,7 @@ import WorkflowCanvas from "./WorkflowCanvas.vue"; import WorkflowHistory from "./WorkflowHistory.vue"; import { useWorkflowHistory } from "./workflow-history"; import { revisionOfRun, type WorkflowRevision } from "./workflow-revisions"; -import { deleteStep, duplicateStep, insertStep, moveStep, normalizeStepIDs, replaceStep, revertWorkflowChange, SEEDED_AGENT_ID, stepAt, workflowChanges, workflowField, type StepPath } from "./workflow-edit"; +import { deleteStep, duplicateStep, insertStep, moveStep, normalizeStepIDs, replaceStep, revertWorkflowChange, SEEDED_AGENT_ID, stepAt, stepIDs, workflowChanges, workflowField, type StepPath } from "./workflow-edit"; import ChangeList from "@/components/ChangeList.vue"; import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { useWorkflowRuns } from "./workflow-runs"; @@ -169,7 +169,12 @@ function removeAt(path: StepPath): void { open.value = null; } function renameAt(path: StepPath, name: string): void { - const { id: _id, type, ...rest } = stepAt(yaml.value, path) ?? {}; + const { id: current, type, ...rest } = stepAt(yaml.value, path) ?? {}; + // References name steps by id, so two steps sharing one would be ambiguous. + if (name && name !== current && stepIDs(yaml.value).includes(name)) { + localError.value = errors.workflows.editor.stepIdTaken(name); + return; + } yaml.value = replaceStep(yaml.value, path, { id: name || undefined, type, ...rest }); } diff --git a/ui/src/workflows/bind-event.ts b/ui/src/workflows/bind-event.ts index a06616cf4..5a9c8d791 100644 --- a/ui/src/workflows/bind-event.ts +++ b/ui/src/workflows/bind-event.ts @@ -1,4 +1,4 @@ -import { constantValue, type Binding, type InputSource } from "@/bindings/binding-draft"; +import { constantValue, type Binding, type InputSource } from "../bindings/binding-draft.ts"; import type { Capture } from "@/captures/state"; import { errors } from "../copy/errors/index.ts"; import type { FieldType } from "@/mappings/mapping-fields"; @@ -95,7 +95,7 @@ export function rowsFromBinding(binding: Binding, fields: MappingField[]): { row /** Every path in a payload, leaves and containers, in document order. */ export function payloadPaths(payload: unknown, prefix = ""): string[] { if (Array.isArray(payload)) { - return payload.slice(0, 3).flatMap((item, i) => { + return payload.flatMap((item, i) => { const path = `${prefix}[${i}]`; return [path, ...payloadPaths(item, path)]; }); diff --git a/ui/src/workflows/workflow-edit.ts b/ui/src/workflows/workflow-edit.ts index 705dfbec7..405e56db8 100644 --- a/ui/src/workflows/workflow-edit.ts +++ b/ui/src/workflows/workflow-edit.ts @@ -203,6 +203,20 @@ export function normalizeStepIDs(source: string): string { return document.toString(); } +/** Every step id the workflow declares, branches included. */ +export function stepIDs(source: string): string[] { + const value = parseDocument(source).toJS() as { steps?: unknown[] } | null; + 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); + for (const [, branch] of branchingOf(record)?.branches ?? []) branch.forEach(collect); + }; + (Array.isArray(value?.steps) ? value.steps : []).forEach(collect); + return ids; +} + /** 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[] { diff --git a/ui/test/binding-event-types.test.ts b/ui/test/binding-event-types.test.ts index 40ef57ee8..18c2daa9d 100644 --- a/ui/test/binding-event-types.test.ts +++ b/ui/test/binding-event-types.test.ts @@ -143,3 +143,11 @@ test("an edited binding's assignments come back into the draft", () => { c: { param: "", value: "x" }, }); }); + +// Guards a regression where the preview listed only the first three items of +// an array, so a field in a later item could never be bound. +test("an event preview offers a path for every array item", async () => { + const { payloadPaths } = await import("../src/workflows/bind-event.ts"); + const paths = payloadPaths({ alerts: [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }] }); + assert.ok(paths.includes("alerts[3].id"), `got ${JSON.stringify(paths)}`); +});