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(json: string | undefined, pick: (value: Record) => 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( () => (state.value.resource?.value as WorkflowDefinitionCollection | undefined) ?? { definitions: [] }, ); /** The shipped definitions the catalog offers as "restore shipped". */ const shipped = computed(() => parse(descriptor.value?.defaults_json, (value) => value, { definitions: [] }), ); /** The registered workflow step types, served in the definitions schema. */ const stepTypes = computed(() => parse(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 { 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), }; });