feat(workflows): edits save as a draft and reach the running workflow only on publish

This commit is contained in:
sam committed 2026-10-18 02:21:54 +11:00
1 parent 0b8532d985
commit a97877e50b
6 files changed
+189 -79

No files matched your search

+15
View File
@@ -5,6 +5,7 @@ import type { StepTypeInfo } from "../workflows/workflow-yaml.ts";
import { useSettingsStore } from "./settings.ts";
const KIND = "workflow-definitions";
const DRAFTS = "workflow-drafts";
export interface WorkflowDefinitionEntry {
id: string;
@@ -80,8 +81,22 @@ export const useWorkflowsStore = defineStore("workflows", () => {
parse<Record<string, string>>(descriptor.value?.schema_json, (value) => value["x-intake-routes"], {}),
);
/** Unpublished edits by workflow id; only a test run runs a draft. */
const drafts = computed(
() => ((settings.stateFor(DRAFTS).resource?.value as { drafts?: WorkflowDefinitionEntry[] } | undefined)?.drafts ?? []),
);
/** Writes a workflow's draft, or removes it when yaml is undefined. */
function putDraft(id: string, yaml: string | undefined): Promise<boolean> {
const rest = drafts.value.filter((draft) => draft.id !== id);
if (yaml === undefined && rest.length === drafts.value.length) return Promise.resolve(true);
return settings.replace(DRAFTS, { drafts: yaml === undefined ? rest : [...rest, { id, yaml }] });
}
return {
state,
drafts,
draftState: computed(() => settings.stateFor(DRAFTS)),
putDraft,
error: computed(() => settings.catalogError || state.value.error),
collection,
shipped,
+60 -11
View File
@@ -8,7 +8,7 @@ import {
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { computed, ref, watch } from "vue";
import { computed, onBeforeUnmount, ref, watch } from "vue";
import { Ellipsis, RotateCcw, Trash2 } from "@lucide/vue";
import { Button } from "@/components/ui/button";
@@ -123,7 +123,7 @@ function openRevision(version: number): void {
historyRevision.value = version;
view.value = "history";
}
// A restored version is an unsaved edit: nothing is written until Save.
// A restored version becomes the draft: nothing runs it until Publish.
function restoreRevision(restored: string): void {
yaml.value = restored;
open.value = null;
@@ -167,7 +167,7 @@ function load(value: string): void {
(candidate) => candidate.id === value,
);
id.value = entry?.id ?? value;
yaml.value = entry?.yaml ?? `id: ${value}\nsteps: []\n`;
yaml.value = workflows.drafts.find((draft) => draft.id === value)?.yaml ?? entry?.yaml ?? `id: ${value}\nsteps: []\n`;
loadedYaml.value = yaml.value;
open.value = null;
localError.value = "";
@@ -176,17 +176,61 @@ function load(value: string): void {
watch(selected, load, { immediate: true });
// The collection arrives after the first render; a later live update must not
// overwrite what is being typed, so it only refreshes an editor with no local edits.
watch(collection, () => {
const incoming = collection.value.definitions.find((entry) => entry.id === selected.value);
watch([collection, () => workflows.drafts], () => {
const incoming =
workflows.drafts.find((draft) => draft.id === selected.value) ??
collection.value.definitions.find((entry) => entry.id === selected.value);
if (incoming && yaml.value === loadedYaml.value && incoming.yaml !== loadedYaml.value) load(selected.value);
});
// Every edit lands in the workflow's draft shortly after it is made, so work
// survives a reload and nothing reaches the running workflow until Publish.
// A draft equal to the published definition is removed.
let pending: ReturnType<typeof setTimeout> | undefined;
async function persistDraft(target: string, value: string): Promise<void> {
if (!target) return;
const published = collection.value.definitions.find((entry) => entry.id === target)?.yaml ?? "";
const current = workflows.drafts.find((draft) => draft.id === target)?.yaml;
const next = value === published ? undefined : value;
if (next === current) return;
if (await workflows.putDraft(target, next)) {
if (target === selected.value && value === yaml.value) loadedYaml.value = value;
} else if (workflows.draftState.saving) {
pending = setTimeout(() => void persistDraft(target, value), 500);
}
}
// The last edit not yet written, flushed at once when the editor switches
// workflow so the switch cannot drop it.
let unsaved: { target: string; value: string } | undefined;
watch(yaml, (value) => {
clearTimeout(pending);
if (unsaved && unsaved.target !== selected.value) void persistDraft(unsaved.target, unsaved.value);
const edit = (unsaved = { target: selected.value, value });
pending = setTimeout(() => {
unsaved = undefined;
void persistDraft(edit.target, edit.value);
}, 800);
});
onBeforeUnmount(() => {
clearTimeout(pending);
void persistDraft(selected.value, yaml.value);
});
async function dropDraft(target: string): Promise<void> {
clearTimeout(pending);
unsaved = undefined;
await workflows.putDraft(target, undefined);
}
async function discard(): Promise<void> {
await dropDraft(selected.value);
load(selected.value);
}
async function save(next: WorkflowDefinitionCollection): Promise<boolean> {
localError.value = "";
return workflows.save(next);
}
async function saveDraft(): Promise<void> {
async function publish(): Promise<void> {
// The id is the one the YAML declares; renaming is editing it there.
const nextID = parsed.value.ok ? parsed.value.id : "";
if (!nextID) return;
@@ -203,6 +247,8 @@ async function saveDraft(): Promise<void> {
if (
await save(upsertWorkflowDefinition(next, { id: nextID, yaml: yaml.value }))
) {
await dropDraft(previous);
if (previous !== nextID) await dropDraft(nextID);
selected.value = nextID;
loadedYaml.value = yaml.value;
}
@@ -215,6 +261,7 @@ async function remove(): Promise<void> {
!(await save(removeWorkflowDefinition(collection.value, selected.value)))
)
return;
await dropDraft(selected.value);
selected.value = "";
id.value = "";
yaml.value = "";
@@ -230,8 +277,10 @@ async function restoreOne(): Promise<void> {
cloneControlPlaneValue(shippedEntry.value),
),
)
)
) {
await dropDraft(selected.value);
load(selected.value);
}
}
// What is typed, read against the served step vocabulary (workflow-yaml.ts).
@@ -281,7 +330,7 @@ function syncScroll(event: Event): void {
</script>
<template>
<form class="space-y-4" @submit.prevent="saveDraft">
<form class="space-y-4" @submit.prevent="publish">
<Tabs v-model="view" class="space-y-1.5">
<div class="flex flex-wrap items-center gap-2">
<TabsList class="max-w-full justify-start overflow-x-auto">
@@ -306,7 +355,7 @@ function syncScroll(event: Event): void {
<Popover v-else-if="dirty">
<PopoverTrigger as-child>
<Button type="button" variant="ghost" size="sm" class="ml-auto text-xs text-fg-subtle">
{{ changes.length || "" }} Unsaved {{ changes.length === 1 ? "change" : "changes" }}</Button>
Draft · {{ changes.length }} {{ changes.length === 1 ? "change" : "changes" }}</Button>
</PopoverTrigger>
<PopoverContent align="end" class="max-h-96 w-96 overflow-y-auto p-0">
<ChangeList :changes="changes" @revert="(change) => (yaml = revertWorkflowChange(yaml, change))" />
@@ -318,7 +367,7 @@ function syncScroll(event: Event): void {
<Button type="button" variant="ghost" size="icon-sm" aria-label="More"><Ellipsis /></Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" class="w-48">
<DropdownMenuItem v-if="dirty" @select="load(selected)"><RotateCcw />Discard changes</DropdownMenuItem>
<DropdownMenuItem v-if="dirty" @select="discard"><RotateCcw />Discard draft</DropdownMenuItem>
<DropdownMenuItem v-if="shippedEntry" @select="restoreOne"><RotateCcw />Restore Defaults</DropdownMenuItem>
<DropdownMenuSeparator v-if="dirty || shippedEntry" />
<DropdownMenuItem variant="destructive" @select="confirmDelete = true"><Trash2 />Delete workflow</DropdownMenuItem>
@@ -327,7 +376,7 @@ function syncScroll(event: Event): void {
<RunWorkflowButton v-if="stored !== undefined" :workflow="selected" :draft="yaml" :disabled="!parsed.ok" @started="(id) => { view = 'canvas'; void follow(id); }" />
<RunWorkflowButton v-if="stored !== undefined" :workflow="selected" :disabled="dirty" @started="(id) => { view = 'canvas'; void follow(id); }" />
<Button type="submit" size="sm" :disabled="state.saving || !parsed.ok || !dirty">
<Spinner v-if="state.saving" data-icon="inline-start" />Save</Button>
<Spinner v-if="state.saving" data-icon="inline-start" />Publish</Button>
</template>
</div>
<p v-if="runsLoading && !runs.length" class="text-xs text-fg-muted" role="status">Loading recent runs…</p>
+2 -1
View File
@@ -82,7 +82,7 @@ const stored = computed(() => store.collection.definitions);
const rows = computed(() =>
workflowRows(
data.value.workflows,
stored.value.map((d) => ({
[...stored.value, ...store.drafts.filter((draft) => !stored.value.some((d) => d.id === draft.id))].map((d) => ({
id: d.id,
name: d.id,
origin: data.value.definitions?.find((x) => x.id === d.id)?.origin,
@@ -224,6 +224,7 @@ const rate = (deliveredRuns = 0, total = 0) => (total ? deliveredRuns / total :
>
<span class="flex items-baseline gap-2">
<span class="min-w-0 flex-1 truncate font-mono text-[13px]" :class="row.id === selected && 'font-medium'">{{ row.id }}</span>
<Badge v-if="store.drafts.some((draft) => draft.id === row.id)" variant="outline">Draft</Badge>
<span class="font-mono text-xs text-fg-subtle">{{ row.avg_tokens ? compact(row.avg_tokens) : "–" }}</span>
</span>
<span v-if="row.runs" class="mt-1.5 flex items-center gap-2 text-xs text-fg-subtle">