UNPKG

@bernierllc/generic-workflow-ui

Version:

Generic, reusable workflow UI components with linear and graph visualization

105 lines (104 loc) 3.39 kB
"use strict"; /* Copyright (c) 2025 Bernier LLC */ Object.defineProperty(exports, "__esModule", { value: true }); exports.workflowToJSON = workflowToJSON; exports.jsonToWorkflow = jsonToWorkflow; /** * Convert GenericWorkflow to JSON definition */ function workflowToJSON(workflow, options = {}) { const { includeMetadata = true, includeStaticData = false, nodePositions = new Map() } = options; // Convert stages to nodes const nodes = workflow.stages.map((stage, index) => ({ id: stage.id, name: stage.name, type: 'stage', typeVersion: 1, position: nodePositions.get(stage.id) || [250 + (index * 200), 300], parameters: {}, data: includeMetadata ? stage.metadata : undefined, notes: stage.description, disabled: false })); // Convert transitions to connections const connections = {}; workflow.transitions.forEach((transition) => { if (!connections[transition.from]) { connections[transition.from] = { main: [] }; } if (!connections[transition.from].main[0]) { connections[transition.from].main[0] = []; } connections[transition.from].main[0].push({ node: transition.to, type: 'main', index: 0, data: includeMetadata ? transition.metadata : undefined }); }); // Build JSON definition const json = { name: workflow.name, nodes, connections, settings: { executionOrder: 'v1', saveExecutionProgress: true, saveDataErrorExecution: 'all', saveDataSuccessExecution: 'all' } }; if (workflow.description) { json.meta = { ...json.meta, instanceId: workflow.id }; } if (includeStaticData && workflow.metadata) { json.staticData = workflow.metadata; } return json; } /** * Convert JSON definition to GenericWorkflow */ function jsonToWorkflow(json, options = {}) { const { preserveNodeData = true } = options; // Convert nodes to stages const stages = (json.nodes || []).map((node, index) => ({ id: node.id, name: node.name, description: node.notes, order: index, metadata: preserveNodeData ? (node.parameters || node.data) : undefined })); // Convert connections to transitions const transitions = []; let transitionId = 0; Object.entries(json.connections || {}).forEach(([fromNodeId, outputs]) => { Object.entries(outputs).forEach(([outputType, connectionGroups]) => { connectionGroups.forEach((connections) => { connections.forEach((connection) => { transitions.push({ id: `t${transitionId++}`, from: fromNodeId, to: connection.node, name: outputType, metadata: preserveNodeData ? connection.data : undefined }); }); }); }); }); // Build GenericWorkflow const workflow = { id: json.meta?.instanceId || json.versionId || `workflow-${Date.now()}`, name: json.name, stages, transitions, metadata: json.staticData }; return workflow; }