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