fix: guard workflow names against blanks and collisions
- refuse blank or duplicate spec names and duplicate step ids, with inline errors - offer a payload path for every array item in event previews, not just the first three - trim multiline step settings and apply them on change
This commit is contained in:
8 files changed
+65
-9
No files matched your search
@@ -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}`,
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from "vue";
|
||||
import { computed, ref } from "vue";
|
||||
import { Plus, Trash2 } from "@lucide/vue";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import OptionSelect from "./OptionSelect.vue";
|
||||
import { errors } from "../copy/errors/index.ts";
|
||||
import { setWorkflowField, workflowField } from "./workflow-edit";
|
||||
|
||||
// A workflow's declared inputs or outputs: name, type and whether required.
|
||||
@@ -22,6 +23,25 @@ function write(entries: [string, Spec][]): void {
|
||||
function edit(index: number, name: string, spec: Spec): void {
|
||||
write(list.value.map((entry, i) => (i === index ? [name, spec] : entry)));
|
||||
}
|
||||
// A rename to blank or onto another entry's name would drop an entry, so it is
|
||||
// refused: the field goes back to the old name and says why.
|
||||
const nameError = ref<{ index: number; message: string } | null>(null);
|
||||
function rename(index: number, input: HTMLInputElement, spec: Spec): void {
|
||||
const name = input.value.trim();
|
||||
const current = list.value[index]?.[0] ?? "";
|
||||
const message = !name
|
||||
? errors.workflows.specList.nameRequired
|
||||
: list.value.some(([other], i) => i !== index && other === name)
|
||||
? errors.workflows.specList.nameTaken(name)
|
||||
: "";
|
||||
if (message) {
|
||||
nameError.value = { index, message };
|
||||
input.value = current;
|
||||
return;
|
||||
}
|
||||
nameError.value = null;
|
||||
if (name !== current) edit(index, name, spec);
|
||||
}
|
||||
function add(): void {
|
||||
const stem = props.field.slice(0, -1);
|
||||
let n = list.value.length + 1;
|
||||
@@ -37,11 +57,14 @@ function add(): void {
|
||||
<Button type="button" variant="ghost" size="xs" class="ml-auto" @click="add"><Plus data-icon="inline-start" />Add</Button>
|
||||
</div>
|
||||
<div v-if="list.length" class="divide-y divide-border rounded-md border border-border">
|
||||
<div v-for="([name, spec], i) in list" :key="i" class="flex items-center gap-2 px-2 py-1.5">
|
||||
<Input :model-value="name" class="h-8 min-w-0 flex-1 font-mono text-caption" :aria-label="`${field} name`" @change="edit(i, ($event.target as HTMLInputElement).value.trim(), spec)" />
|
||||
<div v-for="([name, spec], i) in list" :key="i" class="px-2 py-1.5">
|
||||
<div class="flex items-center gap-2">
|
||||
<Input :model-value="name" class="h-8 min-w-0 flex-1 font-mono text-caption" :aria-label="`${field} name`" :aria-invalid="nameError?.index === i || undefined" @change="rename(i, $event.target as HTMLInputElement, spec)" />
|
||||
<OptionSelect :model-value="spec.type ?? 'string'" :options="TYPES" class="w-24" :aria-label="`${name} type`" @update:model-value="edit(i, name, { ...spec, type: $event })" />
|
||||
<Switch :model-value="spec.required === true" :title="spec.required ? 'Required' : 'Optional'" :aria-label="`${name} required`" @update:model-value="edit(i, name, { ...spec, required: $event || undefined })" />
|
||||
<Button type="button" variant="ghost" size="icon-xs" :aria-label="`Remove ${name}`" @click="write(list.filter((_, j) => j !== i))"><Trash2 /></Button>
|
||||
</div>
|
||||
<p v-if="nameError?.index === i" class="mt-1 text-caption text-danger" role="alert">{{ nameError.message }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@@ -273,7 +273,7 @@ const ON_FAILURE = [{ value: "", label: "Park the task" }, { value: "continue",
|
||||
:aria-label="label(field.key, field.schema)"
|
||||
class="min-h-16 font-mono text-caption"
|
||||
placeholder="One per line"
|
||||
@update:model-value="setSetting(field.key, String($event).split('\n').filter(Boolean))"
|
||||
@change="setSetting(field.key, ($event.target as HTMLTextAreaElement).value.split('\n').map((line) => line.trim()).filter(Boolean))"
|
||||
/>
|
||||
<template v-else>
|
||||
<Textarea :model-value="asYaml(settings[field.key])" :aria-label="label(field.key, field.schema)" class="min-h-24 font-mono text-caption" placeholder="YAML" @change="setYamlSetting(field.key, ($event.target as HTMLTextAreaElement).value)" />
|
||||
|
||||
@@ -99,7 +99,8 @@ function commitRename(event: Event): void {
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^[^a-z]+|-+$/g, "");
|
||||
if (name !== (props.data.name ?? "")) props.data.onRename?.(name);
|
||||
// Nothing usable typed keeps the current id rather than dropping it.
|
||||
if (name && name !== (props.data.name ?? "")) props.data.onRename?.(name);
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
|
||||
|
||||
@@ -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)];
|
||||
});
|
||||
|
||||
@@ -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[] {
|
||||
|
||||
@@ -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)}`);
|
||||
});
|
||||
Reference in new issue
Block a user