Files
archie/ui/src/stores/workflows.ts
T
sam 41f44c43a1 feat(workflow): issue routing is a control-plane setting edited in the dashboard (#1959)
Label to workflow rules and a default workflow are the intake-routes
resource: ordered, case-insensitive, first rule wins, seeded with bug to
tdd, feature to feasibility, default triage. A write naming an undefined
workflow or an empty label is refused. The daemon reads it through the
workflow definitions client when pinning a task and at intake.

The kind indirection only carried this routing, so it is removed: the
per-kind NATS subjects (every consumer already bound archie.task.>) are
one subject, the envelope and playbook triggers lose their kind, and
x-intake-routes is dropped. The dashboard gets an Issue routing page
under Automation; the trigger card reads the same setting.
2026-11-13 20:28:52 +11:00

105 lines
3.7 KiB
TypeScript

import { defineStore } from "pinia";
import { computed } from "vue";
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;
yaml: string;
}
export interface WorkflowDefinitionCollection {
definitions: WorkflowDefinitionEntry[];
}
export function upsertWorkflowDefinition(
collection: WorkflowDefinitionCollection,
entry: WorkflowDefinitionEntry,
): WorkflowDefinitionCollection {
const found = collection.definitions.some(({ id }) => id === entry.id);
return {
definitions: found
? collection.definitions.map((definition) =>
definition.id === entry.id ? entry : definition,
)
: [...collection.definitions, entry],
};
}
// restoreShippedDefinitions resets every shipped workflow to what archie ships
// and keeps the operator's custom ones, which archie has no copy of to restore.
export function restoreShippedDefinitions(
collection: WorkflowDefinitionCollection,
shipped: WorkflowDefinitionCollection,
): WorkflowDefinitionCollection {
return shipped.definitions.reduce(upsertWorkflowDefinition, collection);
}
export function removeWorkflowDefinition(
collection: WorkflowDefinitionCollection,
id: string,
): WorkflowDefinitionCollection {
return {
definitions: collection.definitions.filter(
(definition) => definition.id !== id,
),
};
}
function parse<T>(json: string | undefined, pick: (value: Record<string, unknown>) => unknown, fallback: T): T {
if (!json) return fallback;
try {
return (pick(JSON.parse(json)) as T | undefined) ?? fallback;
} catch {
return fallback;
}
}
/** The workflow definitions: one control-plane resource, with the shipped
* copies and step vocabulary its catalog entry serves. */
export const useWorkflowsStore = defineStore("workflows", () => {
const settings = useSettingsStore();
const descriptor = computed(() => settings.catalog.find((resource) => resource.kind === KIND));
const state = computed(() => settings.stateFor(KIND));
const collection = computed<WorkflowDefinitionCollection>(
() => (state.value.resource?.value as WorkflowDefinitionCollection | undefined) ?? { definitions: [] },
);
/** The shipped definitions the catalog offers as "restore shipped". */
const shipped = computed(() =>
parse<WorkflowDefinitionCollection>(descriptor.value?.defaults_json, (value) => value, { definitions: [] }),
);
/** The registered workflow step types, served in the definitions schema. */
const stepTypes = computed(() =>
parse<StepTypeInfo[]>(descriptor.value?.schema_json, (value) => value["x-step-types"], []),
);
/** 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,
reloadDrafts: () => settings.loadResource(DRAFTS),
error: computed(() => settings.catalogError || state.value.error),
collection,
shipped,
stepTypes,
load: settings.load,
save: (next: WorkflowDefinitionCollection) => settings.replace(KIND, next),
};
});