@serverlessworkflow/sdk 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,11 @@ System.register([], (function (exports) {
3
3
  return {
4
4
  execute: (function () {
5
5
 
6
+ exports({
7
+ buildGraph: buildGraph,
8
+ convertToMermaidCode: convertToMermaidCode
9
+ });
10
+
6
11
  /*
7
12
  * Copyright 2021-Present The Serverless Workflow Specification Authors
8
13
  *
@@ -138,6 +143,17 @@ System.register([], (function (exports) {
138
143
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
139
144
  }
140
145
 
146
+ var __assign = function() {
147
+ __assign = Object.assign || function __assign(t) {
148
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
149
+ s = arguments[i];
150
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
151
+ }
152
+ return t;
153
+ };
154
+ return __assign.apply(this, arguments);
155
+ };
156
+
141
157
  typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
142
158
  var e = new Error(message);
143
159
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
@@ -18094,7 +18110,7 @@ System.register([], (function (exports) {
18094
18110
  * Represents a FlowDirective with methods for validation and normalization.
18095
18111
  * Inherits from ObjectHydrator which provides functionality for hydrating the state based on a model.
18096
18112
  */
18097
- var FlowDirective = /** @class */ (function (_super) {
18113
+ var FlowDirective$1 = /** @class */ (function (_super) {
18098
18114
  __extends(FlowDirective, _super);
18099
18115
  /**
18100
18116
  * Instanciates a new instance of the FlowDirective class.
@@ -18129,7 +18145,7 @@ System.register([], (function (exports) {
18129
18145
  };
18130
18146
  return FlowDirective;
18131
18147
  }(ObjectHydrator));
18132
- var _FlowDirective = FlowDirective;
18148
+ var _FlowDirective = FlowDirective$1;
18133
18149
 
18134
18150
  /*
18135
18151
  * Copyright 2021-Present The Serverless Workflow Specification Authors
@@ -24819,6 +24835,541 @@ System.register([], (function (exports) {
24819
24835
  var load = loader.load;
24820
24836
  var dump = dumper.dump;
24821
24837
 
24838
+ var entrySuffix = '-entry-node';
24839
+ var exitSuffix = '-exit-node';
24840
+ var rooId = 'root';
24841
+ var doReference = '/do';
24842
+ var forReference = '/for';
24843
+ var catchReference = '/catch';
24844
+ var branchReference = '/fork/branches';
24845
+ var tryReference = '/try';
24846
+ /**
24847
+ * Enumeration of possible node types in a graph.
24848
+ */
24849
+ var GraphNodeType; exports('GraphNodeType', GraphNodeType);
24850
+ (function (GraphNodeType) {
24851
+ GraphNodeType["Root"] = "root";
24852
+ GraphNodeType["Start"] = "start";
24853
+ GraphNodeType["End"] = "end";
24854
+ GraphNodeType["Entry"] = "entry";
24855
+ GraphNodeType["Exit"] = "exit";
24856
+ GraphNodeType["Call"] = "call";
24857
+ GraphNodeType["Catch"] = "catch";
24858
+ GraphNodeType["Do"] = "do";
24859
+ GraphNodeType["Emit"] = "emit";
24860
+ GraphNodeType["For"] = "for";
24861
+ GraphNodeType["Fork"] = "fork";
24862
+ GraphNodeType["Listen"] = "listen";
24863
+ GraphNodeType["Raise"] = "raise";
24864
+ GraphNodeType["Run"] = "run";
24865
+ GraphNodeType["Set"] = "set";
24866
+ GraphNodeType["Switch"] = "switch";
24867
+ GraphNodeType["Try"] = "try";
24868
+ GraphNodeType["TryCatch"] = "try-catch";
24869
+ GraphNodeType["Wait"] = "wait";
24870
+ })(GraphNodeType || (exports('GraphNodeType', GraphNodeType = {})));
24871
+ /**
24872
+ * Enumeration of possible workflow flow directives.
24873
+ */
24874
+ var FlowDirective;
24875
+ (function (FlowDirective) {
24876
+ FlowDirective["Exit"] = "exit";
24877
+ FlowDirective["End"] = "end";
24878
+ FlowDirective["Continue"] = "continue";
24879
+ })(FlowDirective || (FlowDirective = {}));
24880
+ /**
24881
+ * Converts an array of TaskItem objects into a Map for easy lookup.
24882
+ *
24883
+ * @param tasksList An array of TaskItem objects.
24884
+ * @returns A map where keys are task names and values are Task objects.
24885
+ */
24886
+ function mapTasks(tasksList) {
24887
+ return (tasksList || []).reduce(function (acc, item) {
24888
+ var _a = Object.entries(item)[0], key = _a[0], task = _a[1];
24889
+ acc.set(key, task);
24890
+ return acc;
24891
+ }, new Map());
24892
+ }
24893
+ /**
24894
+ * Initializes a graph with default entry and exit nodes.
24895
+ *
24896
+ * @param type The type of the graph node.
24897
+ * @param id Unique identifier for the graph.
24898
+ * @param label Optional label for the graph.
24899
+ * @param parent Optional parent graph if this is a subgraph.
24900
+ * @returns A newly created Graph instance.
24901
+ */
24902
+ function initGraph(type, id, label, parent) {
24903
+ if (id === void 0) { id = rooId; }
24904
+ if (label === void 0) { label = undefined; }
24905
+ if (parent === void 0) { parent = undefined; }
24906
+ var entryNode = {
24907
+ type: id === rooId ? GraphNodeType.Start : GraphNodeType.Entry,
24908
+ id: "".concat(id).concat(entrySuffix),
24909
+ };
24910
+ var exitNode = {
24911
+ type: id === rooId ? GraphNodeType.End : GraphNodeType.Exit,
24912
+ id: "".concat(id).concat(exitSuffix),
24913
+ };
24914
+ var graph = {
24915
+ id: id,
24916
+ label: label,
24917
+ type: type,
24918
+ parent: parent,
24919
+ entryNode: entryNode,
24920
+ exitNode: exitNode,
24921
+ nodes: [entryNode, exitNode],
24922
+ edges: [],
24923
+ };
24924
+ if (parent)
24925
+ parent.nodes.push(graph);
24926
+ return graph;
24927
+ }
24928
+ /**
24929
+ * Constructs a graph representation based on the given workflow.
24930
+ *
24931
+ * @param workflow The workflow to be converted into a graph structure.
24932
+ * @returns A graph representation of the workflow.
24933
+ */
24934
+ function buildGraph(workflow) {
24935
+ var graph = initGraph(GraphNodeType.Root);
24936
+ buildTransitions(graph.entryNode, {
24937
+ graph: graph,
24938
+ reference: doReference,
24939
+ taskList: mapTasks(workflow.do),
24940
+ taskReference: doReference,
24941
+ });
24942
+ return graph;
24943
+ }
24944
+ /**
24945
+ * Gets the next task to be executed in the workflow
24946
+ * @param tasksList The list of task to resolve the next task from
24947
+ * @param taskName The current task name, if any
24948
+ * @param transition A specific transition, if any
24949
+ * @returns
24950
+ */
24951
+ function getNextTask(tasksList, taskName, transition) {
24952
+ if (taskName === void 0) { taskName = undefined; }
24953
+ if (transition === void 0) { transition = undefined; }
24954
+ if (!(tasksList === null || tasksList === void 0 ? void 0 : tasksList.size))
24955
+ throw new Error('The task list cannot be empty. No tasks list to get the next task from.');
24956
+ var currentTask = tasksList.get(taskName || '');
24957
+ transition = transition || (currentTask === null || currentTask === void 0 ? void 0 : currentTask.then) || '';
24958
+ if (transition == FlowDirective.End || transition == FlowDirective.Exit) {
24959
+ return {
24960
+ name: transition,
24961
+ index: -1,
24962
+ };
24963
+ }
24964
+ var index = 0;
24965
+ if (transition && transition != FlowDirective.Continue) {
24966
+ index = Array.from(tasksList.keys()).indexOf(transition);
24967
+ }
24968
+ else if (currentTask) {
24969
+ index = Array.from(tasksList.values()).indexOf(currentTask) + 1;
24970
+ if (index >= tasksList.size) {
24971
+ return {
24972
+ name: FlowDirective.End,
24973
+ index: -1,
24974
+ };
24975
+ }
24976
+ }
24977
+ var taskEntry = Array.from(tasksList.entries())[index];
24978
+ return {
24979
+ index: index,
24980
+ name: taskEntry[0],
24981
+ task: taskEntry[1],
24982
+ };
24983
+ }
24984
+ /**
24985
+ * Builds the provided transition from the source node
24986
+ * @param sourceNode The node to build the transition from
24987
+ * @param transition The transition to follow
24988
+ * @param context The context in which the transition is built
24989
+ */
24990
+ function buildTransition(sourceNode, transition, context) {
24991
+ var exitAnchor = sourceNode.exitNode || sourceNode;
24992
+ if (transition.index != -1) {
24993
+ var destinationNode = buildTaskNode(__assign(__assign({}, context), { taskReference: "".concat(context.reference, "/").concat(transition.index, "/").concat(transition.name), taskName: transition.name }));
24994
+ buildEdge(context.graph, exitAnchor, destinationNode.entryNode || destinationNode, transition.label);
24995
+ }
24996
+ else if (transition.name === FlowDirective.Exit) {
24997
+ buildEdge(context.graph, exitAnchor, context.graph.exitNode, transition.label);
24998
+ }
24999
+ else if (transition.name === FlowDirective.End) {
25000
+ buildEdge(context.graph, exitAnchor, context.graph.exitNode, transition.label);
25001
+ }
25002
+ else
25003
+ throw new Error('Invalid transition');
25004
+ }
25005
+ /**
25006
+ * Builds all the possible transitions from the provided node in the provided context
25007
+ * @param sourceNode The node to build the transitions from
25008
+ * @param context The context in which the transitions are built
25009
+ */
25010
+ function buildTransitions(sourceNode, context) {
25011
+ var _a, _b;
25012
+ var transitions = [];
25013
+ var nextTransition = getNextTask(context.taskList, context.taskName);
25014
+ transitions.push(nextTransition);
25015
+ while ((_a = nextTransition === null || nextTransition === void 0 ? void 0 : nextTransition.task) === null || _a === void 0 ? void 0 : _a.if) {
25016
+ nextTransition.label = (_b = nextTransition === null || nextTransition === void 0 ? void 0 : nextTransition.task) === null || _b === void 0 ? void 0 : _b.if;
25017
+ nextTransition = getNextTask(context.taskList, nextTransition.name, FlowDirective.Continue);
25018
+ transitions.push(nextTransition);
25019
+ }
25020
+ transitions
25021
+ .filter(function (transition, index) {
25022
+ return transitions.findIndex(function (t) { return t.index === transition.index && t.name === transition.name && t.task === transition.task; }) === index;
25023
+ })
25024
+ .forEach(function (transition) { return buildTransition(sourceNode, transition, context); });
25025
+ }
25026
+ /**
25027
+ * Builds a graph representation of a task
25028
+ * @param context The context to build the graph/node for
25029
+ * @returns A graph or node for the provided context
25030
+ */
25031
+ function buildTaskNode(context) {
25032
+ var task = context.taskList.get(context.taskName);
25033
+ if (!task)
25034
+ throw new Error("Unabled to find the task '".concat(context.taskName, "' in the current context"));
25035
+ if (task.call)
25036
+ return buildCallTaskNode(task, context);
25037
+ if (task.catch)
25038
+ return buildTryCatchTaskNode(task, context);
25039
+ if (task.emit)
25040
+ return buildEmitTaskNode(task, context);
25041
+ if (task.for)
25042
+ return buildForTaskNode(task, context);
25043
+ if (task.fork)
25044
+ return buildForkTaskNode(task, context);
25045
+ if (task.listen)
25046
+ return buildListenTaskNode(task, context);
25047
+ if (task.raise)
25048
+ return buildRaiseTaskNode(task, context);
25049
+ if (task.run)
25050
+ return buildRunTaskNode(task, context);
25051
+ if (task.set)
25052
+ return buildSetTaskNode(task, context);
25053
+ if (task.switch)
25054
+ return buildSwitchTaskNode(task, context);
25055
+ if (task.wait)
25056
+ return buildWaitTaskNode(task, context);
25057
+ if (task.do)
25058
+ return buildDoTaskNode(task, context);
25059
+ throw new Error("Unable to defined task type of task named '".concat(context.taskName, "'"));
25060
+ }
25061
+ /**
25062
+ * Builds a graph node with the provided type and context
25063
+ * @param type The type of the node
25064
+ * @param context The context to build the graph node for
25065
+ * @returns A graph node for the provided context
25066
+ */
25067
+ function buildGenericTaskNode(type, context) {
25068
+ var node = {
25069
+ type: type,
25070
+ id: context.taskReference,
25071
+ label: context.taskName,
25072
+ };
25073
+ context.graph.nodes.push(node);
25074
+ buildTransitions(node, context);
25075
+ return node;
25076
+ }
25077
+ /**
25078
+ * Builds a graph node for the provided call task
25079
+ * @param task The task to build the graph node for
25080
+ * @param context The context to build the graph node for
25081
+ * @returns A graph node for the provided task
25082
+ */
25083
+ function buildCallTaskNode(task, context) {
25084
+ var node = buildGenericTaskNode(GraphNodeType.Call, context);
25085
+ // TODO: add some details about the task?
25086
+ return node;
25087
+ }
25088
+ /**
25089
+ * Builds a graph for the provided do task
25090
+ * @param task The task to build the graph for
25091
+ * @param context The context to build the graph for
25092
+ * @returns A graph for the provided task
25093
+ */
25094
+ function buildDoTaskNode(task, context) {
25095
+ var subgraph = initGraph(GraphNodeType.Do, context.taskReference, context.taskName, context.graph);
25096
+ var doContext = __assign(__assign({}, context), { graph: subgraph, reference: context.taskReference + doReference, taskList: mapTasks(task.do), taskName: null });
25097
+ buildTransitions(subgraph.entryNode, doContext);
25098
+ buildTransitions(subgraph, context);
25099
+ return subgraph;
25100
+ }
25101
+ /**
25102
+ * Builds a graph node for the provided emit task
25103
+ * @param task The task to build the graph node for
25104
+ * @param context The context to build the graph node for
25105
+ * @returns A graph node for the provided task
25106
+ */
25107
+ function buildEmitTaskNode(task, context) {
25108
+ var node = buildGenericTaskNode(GraphNodeType.Emit, context);
25109
+ // TODO: add some details about the task?
25110
+ return node;
25111
+ }
25112
+ /**
25113
+ * Builds a graph for the provided for task
25114
+ * @param task The task to build the graph for
25115
+ * @param context The context to build the graph for
25116
+ * @returns A graph for the provided task
25117
+ */
25118
+ function buildForTaskNode(task, context) {
25119
+ var subgraph = initGraph(GraphNodeType.For, context.taskReference, context.taskName, context.graph);
25120
+ var forContext = __assign(__assign({}, context), { graph: subgraph, reference: subgraph.id + forReference + doReference, taskList: mapTasks(task.do), taskName: null });
25121
+ buildTransitions(subgraph.entryNode, forContext);
25122
+ buildTransitions(subgraph, context);
25123
+ return subgraph;
25124
+ }
25125
+ /**
25126
+ * Builds a graph for the provided fork task
25127
+ * @param task The task to build the graph for
25128
+ * @param context The context to build the graph for
25129
+ * @returns A graph for the provided task
25130
+ */
25131
+ function buildForkTaskNode(task, context) {
25132
+ var _a, _b;
25133
+ var subgraph = initGraph(GraphNodeType.Fork, context.taskReference, context.taskName, context.graph);
25134
+ for (var i = 0, c = ((_a = task.fork) === null || _a === void 0 ? void 0 : _a.branches.length) || 0; i < c; i++) {
25135
+ var branchItem = (_b = task.fork) === null || _b === void 0 ? void 0 : _b.branches[i];
25136
+ if (!branchItem)
25137
+ continue;
25138
+ var branchName = Object.entries(branchItem)[0][0];
25139
+ var branchContext = __assign(__assign({}, context), { graph: subgraph, reference: "".concat(context.taskReference).concat(branchReference), taskList: mapTasks([branchItem]), taskReference: "".concat(context.taskReference).concat(branchReference, "/").concat(i, "/").concat(branchName), taskName: branchName });
25140
+ var branchNode = buildTaskNode(branchContext);
25141
+ buildEdge(subgraph, subgraph.entryNode, branchNode.entryNode || branchNode);
25142
+ buildEdge(subgraph, branchNode.exitNode || branchNode, subgraph.exitNode);
25143
+ }
25144
+ buildTransitions(subgraph, context);
25145
+ return subgraph;
25146
+ }
25147
+ /**
25148
+ * Builds a graph node for the provided listen task
25149
+ * @param task The task to build the graph node for
25150
+ * @param context The context to build the graph node for
25151
+ * @returns A graph node for the provided task
25152
+ */
25153
+ function buildListenTaskNode(task, context) {
25154
+ var node = buildGenericTaskNode(GraphNodeType.Listen, context);
25155
+ // TODO: add some details about the task?
25156
+ return node;
25157
+ }
25158
+ /**
25159
+ * Builds a graph node for the provided rasie task
25160
+ * @param task The task to build the graph node for
25161
+ * @param context The context to build the graph node for
25162
+ * @returns A graph node for the provided task
25163
+ */
25164
+ function buildRaiseTaskNode(task, context) {
25165
+ var node = buildGenericTaskNode(GraphNodeType.Raise, context);
25166
+ // TODO: add some details about the task?
25167
+ return node;
25168
+ }
25169
+ /**
25170
+ * Builds a graph node for the provided run task
25171
+ * @param task The task to build the graph node for
25172
+ * @param context The context to build the graph node for
25173
+ * @returns A graph node for the provided task
25174
+ */
25175
+ function buildRunTaskNode(task, context) {
25176
+ var node = buildGenericTaskNode(GraphNodeType.Run, context);
25177
+ // TODO: add some details about the task?
25178
+ return node;
25179
+ }
25180
+ /**
25181
+ * Builds a graph node for the provided set task
25182
+ * @param task The task to build the graph node for
25183
+ * @param context The context to build the graph node for
25184
+ * @returns A graph node for the provided task
25185
+ */
25186
+ function buildSetTaskNode(task, context) {
25187
+ var node = buildGenericTaskNode(GraphNodeType.Set, context);
25188
+ // TODO: add some details about the task?
25189
+ return node;
25190
+ }
25191
+ /**
25192
+ * Builds a graph node for the provided switch task
25193
+ * @param task The task to build the graph node for
25194
+ * @param context The context to build the graph node for
25195
+ * @returns A graph node for the provided task
25196
+ */
25197
+ function buildSwitchTaskNode(task, context) {
25198
+ var _a;
25199
+ var node = buildGenericTaskNode(GraphNodeType.Switch, context);
25200
+ var hasDefaultCase = false;
25201
+ (_a = task.switch) === null || _a === void 0 ? void 0 : _a.forEach(function (switchItem) {
25202
+ var _a = Object.entries(switchItem)[0], caseName = _a[0], switchCase = _a[1];
25203
+ if (!switchCase.when)
25204
+ hasDefaultCase = true;
25205
+ var transition = getNextTask(context.taskList, context.taskName, switchCase.then);
25206
+ transition.label = caseName;
25207
+ buildTransition(node, transition, context);
25208
+ });
25209
+ if (!hasDefaultCase) {
25210
+ buildTransitions(node, context);
25211
+ }
25212
+ return node;
25213
+ }
25214
+ /**
25215
+ * Builds a graph for the provided try/catch task
25216
+ * @param task The task to build the graph for
25217
+ * @param context The context to build the graph for
25218
+ * @returns A graph for the provided task
25219
+ */
25220
+ function buildTryCatchTaskNode(task, context) {
25221
+ var _a, _b;
25222
+ var containerSubgraph = initGraph(GraphNodeType.TryCatch, context.taskReference, context.taskName, context.graph);
25223
+ var trySubgraph = initGraph(GraphNodeType.Try, context.taskReference + tryReference, context.taskName + ' (try)', containerSubgraph);
25224
+ buildEdge(containerSubgraph, containerSubgraph.entryNode, trySubgraph.entryNode);
25225
+ var tryContext = __assign(__assign({}, context), { graph: trySubgraph, reference: trySubgraph.id, taskList: mapTasks(task.try), taskName: null });
25226
+ buildTransitions(trySubgraph.entryNode, tryContext);
25227
+ if (!((_b = (_a = task.catch) === null || _a === void 0 ? void 0 : _a.do) === null || _b === void 0 ? void 0 : _b.length)) {
25228
+ var catchNode = {
25229
+ type: GraphNodeType.Catch,
25230
+ id: context.taskReference + catchReference,
25231
+ label: context.taskName + ' (catch)',
25232
+ };
25233
+ containerSubgraph.nodes.push(catchNode);
25234
+ buildEdge(containerSubgraph, trySubgraph.exitNode, catchNode);
25235
+ buildEdge(containerSubgraph, catchNode, containerSubgraph.exitNode);
25236
+ }
25237
+ else {
25238
+ var catchSubgraph = initGraph(GraphNodeType.Catch, context.taskReference + catchReference + doReference, context.taskName + ' (catch)', containerSubgraph);
25239
+ buildEdge(containerSubgraph, trySubgraph.exitNode, catchSubgraph.entryNode);
25240
+ var catchContext = __assign(__assign({}, context), { graph: catchSubgraph, reference: catchSubgraph.id, taskList: mapTasks(task.catch.do), taskName: null });
25241
+ buildTransitions(catchSubgraph.entryNode, catchContext);
25242
+ buildEdge(containerSubgraph, catchSubgraph.exitNode, containerSubgraph.exitNode);
25243
+ }
25244
+ buildTransitions(containerSubgraph, context);
25245
+ return containerSubgraph;
25246
+ }
25247
+ /**
25248
+ * Builds a graph node for the provided wait task
25249
+ * @param task The task to build the graph node for
25250
+ * @param context The context to build the graph node for
25251
+ * @returns A graph node for the provided task
25252
+ */
25253
+ function buildWaitTaskNode(task, context) {
25254
+ var node = buildGenericTaskNode(GraphNodeType.Wait, context);
25255
+ // TODO: add some details about the task?
25256
+ return node;
25257
+ }
25258
+ /**
25259
+ * Builds an edge between two elements
25260
+ * @param graph The graph element containing the nodes
25261
+ * @param source The origin node
25262
+ * @param destination The destination node
25263
+ * @param label The edge label, if any
25264
+ */
25265
+ function buildEdge(graph, source, destination, label) {
25266
+ if (label === void 0) { label = ''; }
25267
+ var edge = graph.edges.find(function (e) { return e.sourceId === source.id && e.destinationId === destination.id; });
25268
+ if (edge) {
25269
+ if (label) {
25270
+ edge.label = edge.label + (edge.label ? ' / ' : '') + label;
25271
+ }
25272
+ return edge;
25273
+ }
25274
+ edge = {
25275
+ label: label,
25276
+ id: "".concat(source.id, "-").concat(destination.id).concat(label ? "-".concat(label) : ''),
25277
+ sourceId: source.id,
25278
+ destinationId: destination.id,
25279
+ };
25280
+ graph.edges.push(edge);
25281
+ }
25282
+
25283
+ /**
25284
+ * Adds indentation to each line of the provided code
25285
+ * @param code The code to indent
25286
+ * @returns The indented code
25287
+ */
25288
+ var indent = function (code) {
25289
+ return code
25290
+ .split('\n')
25291
+ .map(function (line) { return " ".concat(line); })
25292
+ .join('\n');
25293
+ };
25294
+ /**
25295
+ * Converts a graph to Mermaid code
25296
+ * @param graph The graph to convert
25297
+ * @returns The converted graph
25298
+ */
25299
+ function convertGraphToCode(graph) {
25300
+ var isRoot = graph.id === 'root';
25301
+ var code = "".concat(isRoot ? 'flowchart TD' : "subgraph ".concat(graph.id, " [\"").concat(graph.label || graph.id, "\"]"), "\n").concat(indent(graph.nodes.map(function (node) { return convertNodeToCode(node); }).join('\n')), "\n").concat(indent(graph.edges.map(function (edge) { return convertEdgeToCode(edge); }).join('\n')), "\n").concat(isRoot ? '' : 'end');
25302
+ return code;
25303
+ }
25304
+ /**
25305
+ * Converts a node to Mermaid code
25306
+ * @param node The node to convert
25307
+ * @returns The converted node
25308
+ */
25309
+ function convertNodeToCode(node) {
25310
+ var _a;
25311
+ var code = '';
25312
+ if ((_a = node.nodes) === null || _a === void 0 ? void 0 : _a.length) {
25313
+ code = convertGraphToCode(node);
25314
+ }
25315
+ else {
25316
+ code = node.id;
25317
+ switch (node.type) {
25318
+ case GraphNodeType.Entry:
25319
+ case GraphNodeType.Exit:
25320
+ code += ':::hidden';
25321
+ break;
25322
+ case GraphNodeType.Start:
25323
+ code += '(( ))'; // alt '@{ shape: circle, label: " "}';
25324
+ break;
25325
+ case GraphNodeType.End:
25326
+ code += '((( )))'; // alt '@{ shape: dbl-circ, label: " "}';
25327
+ break;
25328
+ default:
25329
+ code += "[\"".concat(node.label, "\"]"); // alt `@{ label: "${node.label}" }`
25330
+ }
25331
+ }
25332
+ return code;
25333
+ }
25334
+ /**
25335
+ * Converts an edge to Mermaid code
25336
+ * @param edge The edge to convert
25337
+ * @returns The converted edge
25338
+ */
25339
+ function convertEdgeToCode(edge) {
25340
+ var ignoreEndArrow = !edge.destinationId.startsWith('root') &&
25341
+ (edge.destinationId.endsWith('-entry-node') || edge.destinationId.endsWith('-exit-node'));
25342
+ var code = "".concat(edge.sourceId, " ").concat(edge.label ? "--\"".concat(edge.label, "\"") : '', "--").concat(ignoreEndArrow ? '-' : '>', " ").concat(edge.destinationId);
25343
+ return code;
25344
+ }
25345
+ /**
25346
+ * Converts the provided workflow to Mermaid code
25347
+ * @param workflow The workflow to convert
25348
+ * @returns The Mermaid diagram
25349
+ */
25350
+ function convertToMermaidCode(workflow) {
25351
+ var graph = buildGraph(workflow);
25352
+ return (convertGraphToCode(graph) +
25353
+ "\n\nclassDef hidden display: none;");
25354
+ }
25355
+ /**
25356
+ * Represents a Mermaid diagram generator for a given workflow.
25357
+ * This class takes a workflow definition and converts it into a Mermaid.js-compatible diagram.
25358
+ */
25359
+ var MermaidDiagram = exports('MermaidDiagram', /** @class */ (function () {
25360
+ function MermaidDiagram(workflow) {
25361
+ this.workflow = workflow;
25362
+ }
25363
+ /**
25364
+ * Generates the Mermaid code representation of the workflow.
25365
+ * @returns The Mermaid diagram source code as a string.
25366
+ */
25367
+ MermaidDiagram.prototype.sourceCode = function () {
25368
+ return convertToMermaidCode(this.workflow);
25369
+ };
25370
+ return MermaidDiagram;
25371
+ }()));
25372
+
24822
25373
  /*
24823
25374
  * Copyright 2021-Present The Serverless Workflow Specification Authors
24824
25375
  *
@@ -24904,6 +25455,12 @@ System.register([], (function (exports) {
24904
25455
  }
24905
25456
  return dump(normalized);
24906
25457
  };
25458
+ Workflow.toGraph = function (model) {
25459
+ return buildGraph(model);
25460
+ };
25461
+ Workflow.toMermaidCode = function (model) {
25462
+ return convertToMermaidCode(model);
25463
+ };
24907
25464
  /**
24908
25465
  * Serializes the workflow to YAML or JSON
24909
25466
  * @param format The format, 'yaml' or 'json', default is 'yaml'
@@ -24915,6 +25472,20 @@ System.register([], (function (exports) {
24915
25472
  if (normalize === void 0) { normalize = true; }
24916
25473
  return Workflow.serialize(this, format, normalize);
24917
25474
  };
25475
+ /**
25476
+ * Creates a directed graph representation of the workflow
25477
+ * @returns A directed graph of the workflow
25478
+ */
25479
+ Workflow.prototype.toGraph = function () {
25480
+ return Workflow.toGraph(this);
25481
+ };
25482
+ /**
25483
+ * Generates the MermaidJS code corresponding to the workflow
25484
+ * @returns The MermaidJS code
25485
+ */
25486
+ Workflow.prototype.toMermaidCode = function () {
25487
+ return Workflow.toMermaidCode(this);
25488
+ };
24918
25489
  return Workflow;
24919
25490
  }(ObjectHydrator));
24920
25491
  var _Workflow = Workflow;