@acorex/platform 0.0.0-ACOREX

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.
Files changed (116) hide show
  1. package/README.md +7 -0
  2. package/auth/README.md +3 -0
  3. package/common/README.md +3 -0
  4. package/core/README.md +4 -0
  5. package/fesm2022/acorex-platform-auth.mjs +1362 -0
  6. package/fesm2022/acorex-platform-auth.mjs.map +1 -0
  7. package/fesm2022/acorex-platform-common-common-settings.provider-G9XcXXOG.mjs +127 -0
  8. package/fesm2022/acorex-platform-common-common-settings.provider-G9XcXXOG.mjs.map +1 -0
  9. package/fesm2022/acorex-platform-common.mjs +4601 -0
  10. package/fesm2022/acorex-platform-common.mjs.map +1 -0
  11. package/fesm2022/acorex-platform-core.mjs +4374 -0
  12. package/fesm2022/acorex-platform-core.mjs.map +1 -0
  13. package/fesm2022/acorex-platform-domain.mjs +3234 -0
  14. package/fesm2022/acorex-platform-domain.mjs.map +1 -0
  15. package/fesm2022/acorex-platform-layout-builder.mjs +2847 -0
  16. package/fesm2022/acorex-platform-layout-builder.mjs.map +1 -0
  17. package/fesm2022/acorex-platform-layout-components-binding-expression-editor-popup.component-CXEdvDTf.mjs +121 -0
  18. package/fesm2022/acorex-platform-layout-components-binding-expression-editor-popup.component-CXEdvDTf.mjs.map +1 -0
  19. package/fesm2022/acorex-platform-layout-components.mjs +8583 -0
  20. package/fesm2022/acorex-platform-layout-components.mjs.map +1 -0
  21. package/fesm2022/acorex-platform-layout-designer.mjs +2474 -0
  22. package/fesm2022/acorex-platform-layout-designer.mjs.map +1 -0
  23. package/fesm2022/acorex-platform-layout-entity.mjs +19150 -0
  24. package/fesm2022/acorex-platform-layout-entity.mjs.map +1 -0
  25. package/fesm2022/acorex-platform-layout-views.mjs +1468 -0
  26. package/fesm2022/acorex-platform-layout-views.mjs.map +1 -0
  27. package/fesm2022/acorex-platform-layout-widget-core.mjs +2950 -0
  28. package/fesm2022/acorex-platform-layout-widget-core.mjs.map +1 -0
  29. package/fesm2022/acorex-platform-layout-widgets-button-widget-designer.component-Dy7jF-oD.mjs +72 -0
  30. package/fesm2022/acorex-platform-layout-widgets-button-widget-designer.component-Dy7jF-oD.mjs.map +1 -0
  31. package/fesm2022/acorex-platform-layout-widgets-file-list-popup.component-9uCkMxcc.mjs +158 -0
  32. package/fesm2022/acorex-platform-layout-widgets-file-list-popup.component-9uCkMxcc.mjs.map +1 -0
  33. package/fesm2022/acorex-platform-layout-widgets-image-preview.popup-C_EPAvCU.mjs +29 -0
  34. package/fesm2022/acorex-platform-layout-widgets-image-preview.popup-C_EPAvCU.mjs.map +1 -0
  35. package/fesm2022/acorex-platform-layout-widgets-page-widget-designer.component-D10yO28c.mjs +172 -0
  36. package/fesm2022/acorex-platform-layout-widgets-page-widget-designer.component-D10yO28c.mjs.map +1 -0
  37. package/fesm2022/acorex-platform-layout-widgets-repeater-widget-column.component-BGQqY5Mw.mjs +111 -0
  38. package/fesm2022/acorex-platform-layout-widgets-repeater-widget-column.component-BGQqY5Mw.mjs.map +1 -0
  39. package/fesm2022/acorex-platform-layout-widgets-tabular-data-edit-popup.component-DmzNTYiS.mjs +274 -0
  40. package/fesm2022/acorex-platform-layout-widgets-tabular-data-edit-popup.component-DmzNTYiS.mjs.map +1 -0
  41. package/fesm2022/acorex-platform-layout-widgets-tabular-data-view-popup.component-BNG_588B.mjs +64 -0
  42. package/fesm2022/acorex-platform-layout-widgets-tabular-data-view-popup.component-BNG_588B.mjs.map +1 -0
  43. package/fesm2022/acorex-platform-layout-widgets-text-block-widget-designer.component-Vo4fWHtX.mjs +34 -0
  44. package/fesm2022/acorex-platform-layout-widgets-text-block-widget-designer.component-Vo4fWHtX.mjs.map +1 -0
  45. package/fesm2022/acorex-platform-layout-widgets.mjs +29791 -0
  46. package/fesm2022/acorex-platform-layout-widgets.mjs.map +1 -0
  47. package/fesm2022/acorex-platform-native.mjs +155 -0
  48. package/fesm2022/acorex-platform-native.mjs.map +1 -0
  49. package/fesm2022/acorex-platform-runtime-catalog-command-definition.mjs +20 -0
  50. package/fesm2022/acorex-platform-runtime-catalog-command-definition.mjs.map +1 -0
  51. package/fesm2022/acorex-platform-runtime-catalog-query-definition.mjs +20 -0
  52. package/fesm2022/acorex-platform-runtime-catalog-query-definition.mjs.map +1 -0
  53. package/fesm2022/acorex-platform-runtime.mjs +899 -0
  54. package/fesm2022/acorex-platform-runtime.mjs.map +1 -0
  55. package/fesm2022/acorex-platform-themes-default-entity-master-create-view.component-Cvvr4HnL.mjs +160 -0
  56. package/fesm2022/acorex-platform-themes-default-entity-master-create-view.component-Cvvr4HnL.mjs.map +1 -0
  57. package/fesm2022/acorex-platform-themes-default-entity-master-modify-view.component-TYoLN1Jq.mjs +120 -0
  58. package/fesm2022/acorex-platform-themes-default-entity-master-modify-view.component-TYoLN1Jq.mjs.map +1 -0
  59. package/fesm2022/acorex-platform-themes-default-entity-master-single-view.component-C2z5Lq9y.mjs +237 -0
  60. package/fesm2022/acorex-platform-themes-default-entity-master-single-view.component-C2z5Lq9y.mjs.map +1 -0
  61. package/fesm2022/acorex-platform-themes-default-error-401.component-C7EYJzSr.mjs +31 -0
  62. package/fesm2022/acorex-platform-themes-default-error-401.component-C7EYJzSr.mjs.map +1 -0
  63. package/fesm2022/acorex-platform-themes-default-error-404.component-7MVLMwIa.mjs +25 -0
  64. package/fesm2022/acorex-platform-themes-default-error-404.component-7MVLMwIa.mjs.map +1 -0
  65. package/fesm2022/acorex-platform-themes-default-error-offline.component-DR6G8gPC.mjs +19 -0
  66. package/fesm2022/acorex-platform-themes-default-error-offline.component-DR6G8gPC.mjs.map +1 -0
  67. package/fesm2022/acorex-platform-themes-default.mjs +2589 -0
  68. package/fesm2022/acorex-platform-themes-default.mjs.map +1 -0
  69. package/fesm2022/acorex-platform-themes-shared-icon-chooser-column.component-CqkWJYdv.mjs +55 -0
  70. package/fesm2022/acorex-platform-themes-shared-icon-chooser-column.component-CqkWJYdv.mjs.map +1 -0
  71. package/fesm2022/acorex-platform-themes-shared-icon-chooser-view.component-BOTuLdWN.mjs +57 -0
  72. package/fesm2022/acorex-platform-themes-shared-icon-chooser-view.component-BOTuLdWN.mjs.map +1 -0
  73. package/fesm2022/acorex-platform-themes-shared-settings.provider-DSs1o1M6.mjs +168 -0
  74. package/fesm2022/acorex-platform-themes-shared-settings.provider-DSs1o1M6.mjs.map +1 -0
  75. package/fesm2022/acorex-platform-themes-shared-theme-color-chooser-column.component-CHfrTtol.mjs +65 -0
  76. package/fesm2022/acorex-platform-themes-shared-theme-color-chooser-column.component-CHfrTtol.mjs.map +1 -0
  77. package/fesm2022/acorex-platform-themes-shared-theme-color-chooser-view.component-BSmvnUVq.mjs +64 -0
  78. package/fesm2022/acorex-platform-themes-shared-theme-color-chooser-view.component-BSmvnUVq.mjs.map +1 -0
  79. package/fesm2022/acorex-platform-themes-shared.mjs +2125 -0
  80. package/fesm2022/acorex-platform-themes-shared.mjs.map +1 -0
  81. package/fesm2022/acorex-platform-workflow.mjs +2501 -0
  82. package/fesm2022/acorex-platform-workflow.mjs.map +1 -0
  83. package/fesm2022/acorex-platform.mjs +6 -0
  84. package/fesm2022/acorex-platform.mjs.map +1 -0
  85. package/layout/builder/README.md +1578 -0
  86. package/layout/components/README.md +3 -0
  87. package/layout/designer/README.md +4 -0
  88. package/layout/entity/README.md +4 -0
  89. package/layout/views/README.md +3 -0
  90. package/layout/widget-core/README.md +4 -0
  91. package/layout/widgets/README.md +3 -0
  92. package/native/README.md +4 -0
  93. package/package.json +103 -0
  94. package/runtime/README.md +3 -0
  95. package/themes/default/README.md +3 -0
  96. package/themes/shared/README.md +3 -0
  97. package/types/acorex-platform-auth.d.ts +680 -0
  98. package/types/acorex-platform-common.d.ts +2926 -0
  99. package/types/acorex-platform-core.d.ts +2896 -0
  100. package/types/acorex-platform-domain.d.ts +2353 -0
  101. package/types/acorex-platform-layout-builder.d.ts +926 -0
  102. package/types/acorex-platform-layout-components.d.ts +2903 -0
  103. package/types/acorex-platform-layout-designer.d.ts +422 -0
  104. package/types/acorex-platform-layout-entity.d.ts +3189 -0
  105. package/types/acorex-platform-layout-views.d.ts +667 -0
  106. package/types/acorex-platform-layout-widget-core.d.ts +1086 -0
  107. package/types/acorex-platform-layout-widgets.d.ts +5478 -0
  108. package/types/acorex-platform-native.d.ts +28 -0
  109. package/types/acorex-platform-runtime-catalog-command-definition.d.ts +137 -0
  110. package/types/acorex-platform-runtime-catalog-query-definition.d.ts +125 -0
  111. package/types/acorex-platform-runtime.d.ts +470 -0
  112. package/types/acorex-platform-themes-default.d.ts +573 -0
  113. package/types/acorex-platform-themes-shared.d.ts +170 -0
  114. package/types/acorex-platform-workflow.d.ts +1806 -0
  115. package/types/acorex-platform.d.ts +2 -0
  116. package/workflow/README.md +4 -0
@@ -0,0 +1,2501 @@
1
+ import * as i0 from '@angular/core';
2
+ import { Injectable, inject, InjectionToken, Optional, Inject, NgModule } from '@angular/core';
3
+ import { Subject, filter } from 'rxjs';
4
+ import { cloneDeep, get, set } from 'lodash-es';
5
+ import { setSmart, AXPExpressionEvaluatorService, AXPDataGenerator } from '@acorex/platform/core';
6
+ import { AXPCommandService } from '@acorex/platform/runtime';
7
+
8
+ class AXPWorkflowError extends Error {
9
+ constructor(message, inner = null) {
10
+ super(message);
11
+ this.inner = inner;
12
+ this.name = 'AXPWorkflowError';
13
+ }
14
+ }
15
+
16
+ class AXPWorkflowEventService {
17
+ constructor() {
18
+ this.eventSubject = new Subject();
19
+ }
20
+ dispatch(event) {
21
+ this.eventSubject.next(event);
22
+ }
23
+ get events$() {
24
+ return this.eventSubject.asObservable();
25
+ }
26
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowEventService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
27
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowEventService, providedIn: 'root' }); }
28
+ }
29
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowEventService, decorators: [{
30
+ type: Injectable,
31
+ args: [{
32
+ providedIn: 'root',
33
+ }]
34
+ }] });
35
+
36
+ class AXPWorkflowRegistryService {
37
+ constructor() {
38
+ this.functionsMap = new Map();
39
+ this.actionsMap = new Map();
40
+ this.workflowsMap = new Map();
41
+ }
42
+ registerWorkflow(name, workflow) {
43
+ this.workflowsMap.set(name, workflow);
44
+ }
45
+ getWorkflow(name) {
46
+ return this.workflowsMap.get(name);
47
+ }
48
+ registerAction(name, action) {
49
+ this.actionsMap.set(name, action);
50
+ }
51
+ getAction(name) {
52
+ return this.actionsMap.get(name);
53
+ }
54
+ registerFunction(name, func) {
55
+ this.functionsMap.set(name, func);
56
+ }
57
+ getFunction(name) {
58
+ return this.functionsMap.get(name);
59
+ }
60
+ getFunctionNames() {
61
+ return Array.from(this.functionsMap.keys());
62
+ }
63
+ getStep(workflowName, stepName) {
64
+ const workflow = this.workflowsMap.get(workflowName);
65
+ if (workflow) {
66
+ const steps = workflow.steps;
67
+ return steps[stepName];
68
+ }
69
+ return undefined;
70
+ }
71
+ updateStepInWorkflow(workflowName, stepName, step) {
72
+ const workflow = this.workflowsMap.get(workflowName);
73
+ if (workflow) {
74
+ const steps = workflow.steps;
75
+ if (steps[stepName]) {
76
+ steps[stepName] = step;
77
+ }
78
+ else {
79
+ throw new Error(`Step with name ${stepName} does not exist in workflow ${workflowName}.`);
80
+ }
81
+ }
82
+ else {
83
+ throw new Error(`Workflow with name ${workflowName} does not exist.`);
84
+ }
85
+ }
86
+ addStepToWorkflow(workflowName, stepName, step) {
87
+ const workflow = this.workflowsMap.get(workflowName);
88
+ if (workflow) {
89
+ const steps = workflow.steps;
90
+ if (!steps[stepName]) {
91
+ steps[stepName] = step;
92
+ }
93
+ else {
94
+ throw new Error(`Step with name ${stepName} already exists in workflow ${workflowName}.`);
95
+ }
96
+ }
97
+ else {
98
+ throw new Error(`Workflow with name ${workflowName} does not exist.`);
99
+ }
100
+ }
101
+ addAfterStep(workflowId, stepId, newStepId, newStepAction, conditionExpression) {
102
+ const step = this.getStep(workflowId, stepId);
103
+ if (step != undefined) {
104
+ this.addStepToWorkflow(workflowId, newStepId, {
105
+ action: newStepAction,
106
+ nextSteps: step.nextSteps,
107
+ });
108
+ this.updateStepInWorkflow(workflowId, stepId, {
109
+ ...step,
110
+ nextSteps: [
111
+ {
112
+ conditions: [{ type: 'SINGLE', expression: conditionExpression }],
113
+ nextStepId: newStepId,
114
+ },
115
+ ],
116
+ });
117
+ }
118
+ }
119
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowRegistryService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
120
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowRegistryService, providedIn: 'root' }); }
121
+ }
122
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowRegistryService, decorators: [{
123
+ type: Injectable,
124
+ args: [{
125
+ providedIn: 'root',
126
+ }]
127
+ }] });
128
+
129
+ class AXPWorkflowContext {
130
+ constructor(initialData = {}) {
131
+ this.variables = {};
132
+ this.stepOutputs = new Map();
133
+ this.variables = cloneDeep(initialData);
134
+ }
135
+ setVariable(key, value) {
136
+ setSmart(this.variables, key, value);
137
+ }
138
+ setVariables(context = {}) {
139
+ this.variables = { ...this.variables, ...context };
140
+ }
141
+ getVariable(key) {
142
+ return !key ? this.variables : get(this.variables, key);
143
+ }
144
+ setOutput(key, output) {
145
+ this.stepOutputs.set(key, output);
146
+ }
147
+ setOutputs(values) {
148
+ this.stepOutputs = new Map([...this.stepOutputs, ...values]);
149
+ }
150
+ getOutput(key) {
151
+ return this.stepOutputs.get(key);
152
+ }
153
+ }
154
+ class AXPWorkflowAction {
155
+ constructor() {
156
+ this.eventService = inject(AXPWorkflowEventService);
157
+ }
158
+ dispatch(event) {
159
+ this.eventService.dispatch(event);
160
+ }
161
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowAction, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
162
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowAction }); }
163
+ }
164
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowAction, decorators: [{
165
+ type: Injectable
166
+ }] });
167
+ class AXPWorkflowFunction {
168
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowFunction, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
169
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowFunction }); }
170
+ }
171
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowFunction, decorators: [{
172
+ type: Injectable
173
+ }] });
174
+ function createWorkFlowEvent(type) {
175
+ const eventCreator = (payload) => ({ type, payload });
176
+ eventCreator.type = type;
177
+ return eventCreator;
178
+ }
179
+ function ofType(...allowedTypes) {
180
+ return filter((event) => allowedTypes.some((type) => event.type === type.type));
181
+ }
182
+
183
+ class AXPWorkflowDecideAction extends AXPWorkflowAction {
184
+ async execute(context) {
185
+ // its a fake action
186
+ }
187
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowDecideAction, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); }
188
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowDecideAction, providedIn: 'root' }); }
189
+ }
190
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowDecideAction, decorators: [{
191
+ type: Injectable,
192
+ args: [{
193
+ providedIn: 'root',
194
+ }]
195
+ }] });
196
+
197
+ class AXPWorkflowService {
198
+ get events$() {
199
+ return this.eventService.events$;
200
+ }
201
+ constructor(registryService, injector) {
202
+ this.registryService = registryService;
203
+ this.injector = injector;
204
+ this.eventService = inject(AXPWorkflowEventService);
205
+ }
206
+ exists(name) {
207
+ return !!this.registryService.getWorkflow(name);
208
+ }
209
+ async execute(workflow, initialContext = null) {
210
+ // Instantiate or retrieve the workflow
211
+ const wk = typeof workflow === 'string' ? this.getWorkflowInstance(workflow) : workflow;
212
+ let currentStepId = wk.startStepId;
213
+ const context = initialContext instanceof AXPWorkflowContext
214
+ ? initialContext
215
+ : new AXPWorkflowContext(this.processData(initialContext) ?? {});
216
+ // Initialize context with initial variables
217
+ while (currentStepId) {
218
+ const currentStep = wk.steps[currentStepId];
219
+ if (!currentStep) {
220
+ throw new AXPWorkflowError(`Step '${currentStepId}' not found in the workflow steps.`);
221
+ }
222
+ // Instantiate or retrieve the action
223
+ const action = typeof currentStep.action === 'string' ? this.getActionInstance(currentStep.action) : currentStep.action;
224
+ //
225
+ if (currentStep.input) {
226
+ let inputs = {};
227
+ await this.processInputs(cloneDeep(currentStep.input), inputs, '', context);
228
+ await new Promise((resolve) => setTimeout(async () => {
229
+ Object.assign(action, inputs);
230
+ resolve(0);
231
+ }));
232
+ }
233
+ try {
234
+ await new Promise((resolve) => setTimeout(async () => {
235
+ await action.execute(context);
236
+ resolve(0);
237
+ }, 0));
238
+ }
239
+ catch (error) {
240
+ console.error(error);
241
+ throw new AXPWorkflowError('Workflow Error', error);
242
+ }
243
+ // Determine the next step based on the conditions
244
+ const nextStepInfo = await this.determineNextStep(currentStep, context);
245
+ if (nextStepInfo) {
246
+ currentStepId = nextStepInfo.nextStepId;
247
+ }
248
+ else {
249
+ break; // Exit the loop if no next step is determined
250
+ }
251
+ }
252
+ return context;
253
+ }
254
+ processData(initialContext) {
255
+ //TODO: update values with expressions and return new object
256
+ return initialContext;
257
+ }
258
+ getActionInstance(actionName) {
259
+ const actionType = this.registryService.getAction(actionName);
260
+ if (!actionType) {
261
+ throw new AXPWorkflowError(`Action type '${actionName}' not found in the registry.`);
262
+ }
263
+ return this.injector.get(actionType);
264
+ }
265
+ getWorkflowInstance(workflowName) {
266
+ const workflowType = this.registryService.getWorkflow(workflowName);
267
+ if (!workflowType) {
268
+ throw new AXPWorkflowError(`Workflow type '${workflowName}' not found in the registry.`);
269
+ }
270
+ return workflowType;
271
+ }
272
+ async determineNextStep(currentStep, context) {
273
+ if (!currentStep.nextSteps) {
274
+ return null;
275
+ }
276
+ for (const nextStep of currentStep.nextSteps) {
277
+ if (await this.evaluateCondition(nextStep.conditions, context)) {
278
+ return nextStep;
279
+ }
280
+ }
281
+ return null;
282
+ }
283
+ async evaluateCondition(conditions, context) {
284
+ if (!conditions || conditions.length === 0) {
285
+ return true; // If no conditions, the step proceeds
286
+ }
287
+ // Map each condition to a promise using evaluateSingleCondition
288
+ const conditionPromises = conditions.map((condition) => this.evaluateSingleCondition(condition, context));
289
+ // Wait for all promises to resolve
290
+ const results = await Promise.all(conditionPromises);
291
+ // Check if every condition is true
292
+ return results.every((result) => result);
293
+ }
294
+ async evaluateSingleCondition(condition, context) {
295
+ switch (condition.type) {
296
+ case 'SINGLE':
297
+ return (!condition.expression ? true : await this.evaluateExpression(condition.expression, context));
298
+ case 'AND':
299
+ // Evaluate all conditions with 'AND' logic
300
+ const andConditions = condition.conditions ?? [];
301
+ const andResults = await Promise.all(andConditions.map((cond) => this.evaluateSingleCondition(cond, context)));
302
+ return andResults.every((result) => result);
303
+ case 'OR':
304
+ // Evaluate all conditions with 'OR' logic
305
+ const orConditions = condition.conditions ?? [];
306
+ const orResults = await Promise.all(orConditions.map((cond) => this.evaluateSingleCondition(cond, context)));
307
+ return orResults.some((result) => result);
308
+ default:
309
+ throw new AXPWorkflowError(`Unsupported condition type: ${condition.type}`);
310
+ }
311
+ }
312
+ //TODO: return any and recursively update object, update scopes
313
+ async evaluateExpression(templateExpression, context) {
314
+ try {
315
+ let expression = '';
316
+ if (typeof templateExpression === 'string' && templateExpression.trim().includes('{{')) {
317
+ const expressionMatch = templateExpression.match(/\{\{\s*(.*?)\s*\}\}/);
318
+ if (!expressionMatch) {
319
+ throw Error(`No valid expression found in "${templateExpression}"`);
320
+ }
321
+ expression = expressionMatch[1];
322
+ }
323
+ else {
324
+ expression = templateExpression;
325
+ }
326
+ // Create a scope that includes context and registered functions
327
+ const scope = { context, ...this.createFunctionScope() };
328
+ // Evaluating the expression within the created scope
329
+ const sandbox = new Function('scope', `return (async function() { with (scope) { return ${expression}; } })();`);
330
+ const result = await sandbox(scope);
331
+ return result;
332
+ }
333
+ catch (error) {
334
+ console.error('Error evaluating expression:', error);
335
+ return false;
336
+ }
337
+ }
338
+ async processInputs(obj, inputs, pathPrefix = '', context = {}) {
339
+ if (!obj) {
340
+ return;
341
+ }
342
+ for await (const i of Object.entries(obj)) {
343
+ const key = i[0];
344
+ const value = i[1];
345
+ const currentPath = pathPrefix ? `${pathPrefix}.${key}` : key;
346
+ if (typeof value === 'string' && value.trim().includes('{{')) {
347
+ const expValue = await this.evaluateExpression(value, context);
348
+ set(inputs, currentPath, expValue);
349
+ }
350
+ else if (typeof value === 'object' &&
351
+ value !== null &&
352
+ (value.constructor === Object || Array.isArray(value))) {
353
+ // Recursively handle nested objects
354
+ this.processInputs(value, inputs, currentPath, context);
355
+ }
356
+ else {
357
+ // Apply static values directly
358
+ set(inputs, currentPath, value);
359
+ }
360
+ }
361
+ }
362
+ createFunctionScope() {
363
+ const scope = {};
364
+ this.registryService.getFunctionNames().forEach((name) => {
365
+ scope[name] = this.getFunctionInstance(name).execute;
366
+ });
367
+ return { methods: { scope } };
368
+ }
369
+ getFunctionInstance(functionName) {
370
+ const functionType = this.registryService.getFunction(functionName);
371
+ if (!functionType) {
372
+ throw new AXPWorkflowError(`Function type '${functionName}' not found in the registry.`);
373
+ }
374
+ return this.injector.get(functionType);
375
+ }
376
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowService, deps: [{ token: AXPWorkflowRegistryService }, { token: i0.Injector }], target: i0.ɵɵFactoryTarget.Injectable }); }
377
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowService, providedIn: 'root' }); }
378
+ }
379
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowService, decorators: [{
380
+ type: Injectable,
381
+ args: [{
382
+ providedIn: 'root',
383
+ }]
384
+ }], ctorParameters: () => [{ type: AXPWorkflowRegistryService }, { type: i0.Injector }] });
385
+
386
+ class AXPStartWorkflowAction extends AXPWorkflowAction {
387
+ constructor() {
388
+ super(...arguments);
389
+ this.workflowService = inject(AXPWorkflowService);
390
+ }
391
+ async execute(context) {
392
+ //console.log("start WK current context", context);
393
+ //console.log("start WK input context", this.context);
394
+ if (this.context)
395
+ context.setVariables(this.context);
396
+ //console.log("start WK merged context", context);
397
+ try {
398
+ await this.workflowService.execute(this.workflow, context);
399
+ }
400
+ catch (e) {
401
+ throw e;
402
+ }
403
+ }
404
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPStartWorkflowAction, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); }
405
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPStartWorkflowAction, providedIn: 'root' }); }
406
+ }
407
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPStartWorkflowAction, decorators: [{
408
+ type: Injectable,
409
+ args: [{
410
+ providedIn: 'root',
411
+ }]
412
+ }] });
413
+
414
+ // ============================================
415
+ // WORKFLOW INSTANCE v3.0.0 TYPES
416
+ // Based on Elsa Workflow Instance schema: https://elsaworkflows.io/schemas/workflow-instance/v3.0.0/schema.json
417
+ // Compatible with Elsa backend while using ACoreX naming conventions
418
+ // ============================================
419
+
420
+ // Note:
421
+ // Previous versions defined dedicated activity result/command types here
422
+ // (AXPActivityResult / AXPActivity) that wrapped output and outcome.
423
+ // Activities are now modeled directly as AXPCommand with outcome stored in
424
+ // AXPExecuteCommandResult.metadata.outcome.
425
+
426
+ const AXP_ACTIVITY_PROVIDER = new InjectionToken('AXP_ACTIVITY_PROVIDER', {
427
+ factory: () => [],
428
+ });
429
+ const AXP_ACTIVITY_CATEGORY_PROVIDER = new InjectionToken('AXP_ACTIVITY_CATEGORY_PROVIDER', {
430
+ factory: () => [],
431
+ });
432
+
433
+ //#region ---- Imports ----
434
+ //#endregion
435
+ /**
436
+ * Optimized Activity Definition Service
437
+ *
438
+ * Manages activity definitions (metadata) for UI and tooling.
439
+ * Similar to AXPReportDefinitionService - only handles metadata, not execution.
440
+ *
441
+ * Performance optimizations:
442
+ * 1. Uses childrenCount to determine if category has children (no query needed)
443
+ * 2. Uses itemsCount to determine if category has activities (no query needed)
444
+ * 3. Aggressive caching prevents duplicate API calls
445
+ * 4. Single pending request per resource prevents race conditions
446
+ * 5. Lazy loading - only loads data when needed
447
+ */
448
+ class AXPActivityDefinitionService {
449
+ constructor() {
450
+ //#region ---- Providers & Caches ----
451
+ this.categoryProviders = inject(AXP_ACTIVITY_CATEGORY_PROVIDER, { optional: true }) || [];
452
+ this.activityProviders = inject(AXP_ACTIVITY_PROVIDER, { optional: true }) || [];
453
+ //#endregion
454
+ //#region ---- Cache Storage ----
455
+ /** Cache for categories by id - O(1) lookup */
456
+ this.categoriesById = new Map();
457
+ /** Cache for categories by parentId - O(1) lookup */
458
+ this.categoriesByParentId = new Map();
459
+ /** Cache for activity definitions by categoryId - O(1) lookup */
460
+ this.activitiesByCategory = new Map();
461
+ /** Cache for individual activity definitions by name - O(1) lookup */
462
+ this.activitiesByName = new Map();
463
+ /** Track which provider index owns each category (by category ID) */
464
+ this.categoryOwnership = new Map(); // Maps categoryId → provider index
465
+ /** Pending API requests to prevent duplicate calls */
466
+ this.pendingCategoriesRequests = new Map();
467
+ this.pendingActivitiesRequests = new Map();
468
+ this.pendingActivityRequests = new Map();
469
+ }
470
+ //#endregion
471
+ //#region ---- Initialization ----
472
+ //#endregion
473
+ //#region ---- Public API: Categories ----
474
+ /**
475
+ * Get categories by parentId with aggressive caching
476
+ *
477
+ * Optimization: Returns cached result immediately if available,
478
+ * preventing unnecessary API calls during navigation
479
+ *
480
+ * @param parentId - Parent category ID (undefined = root categories)
481
+ * @returns Array of categories with count metadata (childrenCount, itemsCount)
482
+ */
483
+ async getCategories(parentId) {
484
+ // ✅ Fast path: Return cached result
485
+ if (this.categoriesByParentId.has(parentId)) {
486
+ return this.categoriesByParentId.get(parentId);
487
+ }
488
+ // ✅ Prevent duplicate requests: Return pending promise
489
+ if (this.pendingCategoriesRequests.has(parentId)) {
490
+ return this.pendingCategoriesRequests.get(parentId);
491
+ }
492
+ // ✅ Create single request and cache it
493
+ const requestPromise = this.loadCategoriesFromProviders(parentId);
494
+ this.pendingCategoriesRequests.set(parentId, requestPromise);
495
+ return requestPromise;
496
+ }
497
+ /**
498
+ * Get single category by ID with O(1) lookup
499
+ *
500
+ * Optimization: Uses Map for instant retrieval, falls back to
501
+ * searching cache, then providers if not found
502
+ */
503
+ async getCategoryById(categoryId) {
504
+ // ✅ Fast path: O(1) lookup in cache
505
+ if (this.categoriesById.has(categoryId)) {
506
+ return this.categoriesById.get(categoryId);
507
+ }
508
+ // ✅ Search in cached parent-child lists
509
+ for (const categories of this.categoriesByParentId.values()) {
510
+ const found = categories.find(cat => cat.id === categoryId);
511
+ if (found) {
512
+ this.categoriesById.set(categoryId, found);
513
+ return found;
514
+ }
515
+ }
516
+ // ✅ Load root categories if not loaded
517
+ if (!this.categoriesByParentId.has(undefined)) {
518
+ await this.getCategories();
519
+ if (this.categoriesById.has(categoryId)) {
520
+ return this.categoriesById.get(categoryId);
521
+ }
522
+ }
523
+ // ✅ Breadth-first search through hierarchy
524
+ return this.searchCategoryInHierarchy(categoryId);
525
+ }
526
+ /**
527
+ * Get category path from root to specified category
528
+ *
529
+ * Optimization: Builds path using cached categories only
530
+ */
531
+ async getCategoriesPathById(categoryId) {
532
+ const path = [];
533
+ let currentCategoryId = categoryId;
534
+ while (currentCategoryId) {
535
+ const category = await this.getCategoryById(currentCategoryId);
536
+ if (!category) {
537
+ throw new Error(`Category '${currentCategoryId}' not found`);
538
+ }
539
+ path.unshift(category);
540
+ currentCategoryId = category.parentId;
541
+ }
542
+ return path;
543
+ }
544
+ //#endregion
545
+ //#region ---- Public API: Activity Definitions ----
546
+ /**
547
+ * Get activity definitions for a category with smart caching
548
+ *
549
+ * Optimization: Checks itemsCount before querying
550
+ * - If itemsCount = 0, returns empty array (no API call)
551
+ * - If itemsCount > 0, loads and caches activity definitions
552
+ * - Returns cached result on subsequent calls
553
+ *
554
+ * @param categoryId - Category ID to get activity definitions from
555
+ * @returns Array of activity definitions
556
+ */
557
+ async getActivitiesByCategoryId(categoryId) {
558
+ // ✅ Fast path: Return cached result
559
+ if (this.activitiesByCategory.has(categoryId)) {
560
+ return this.activitiesByCategory.get(categoryId);
561
+ }
562
+ // ✅ Smart optimization: Check itemsCount before querying
563
+ const category = await this.getCategoryById(categoryId);
564
+ if (category && category.itemsCount !== undefined && category.itemsCount === 0) {
565
+ // Category has no activities - cache empty array and skip API call
566
+ const emptyArray = [];
567
+ this.activitiesByCategory.set(categoryId, emptyArray);
568
+ return emptyArray;
569
+ }
570
+ // ✅ Prevent duplicate requests
571
+ if (this.pendingActivitiesRequests.has(categoryId)) {
572
+ return this.pendingActivitiesRequests.get(categoryId);
573
+ }
574
+ // ✅ Load from providers
575
+ const requestPromise = this.loadActivitiesFromProviders(categoryId);
576
+ this.pendingActivitiesRequests.set(categoryId, requestPromise);
577
+ return requestPromise;
578
+ }
579
+ /**
580
+ * Get single activity definition by name with O(1) lookup
581
+ *
582
+ * Optimization: Uses Map for instant retrieval
583
+ *
584
+ * @param name - Activity name (unique identifier and command key)
585
+ * @returns Activity definition or undefined if not found
586
+ */
587
+ async getActivityByName(name) {
588
+ // ✅ Fast path: O(1) lookup in cache
589
+ if (this.activitiesByName.has(name)) {
590
+ return this.activitiesByName.get(name);
591
+ }
592
+ // ✅ Prevent duplicate requests
593
+ if (this.pendingActivityRequests.has(name)) {
594
+ return this.pendingActivityRequests.get(name);
595
+ }
596
+ // ✅ Load from providers
597
+ const requestPromise = this.loadActivityFromProviders(name);
598
+ this.pendingActivityRequests.set(name, requestPromise);
599
+ return requestPromise;
600
+ }
601
+ /**
602
+ * Get all activity definitions (flat list) by loading root categories and their activities.
603
+ * Used by activity selector UIs (e.g. automation command configurator).
604
+ */
605
+ async getAllActivities() {
606
+ const categories = await this.getCategories(undefined);
607
+ const all = [];
608
+ for (const cat of categories) {
609
+ const activities = await this.getActivitiesByCategoryId(cat.id);
610
+ all.push(...activities);
611
+ }
612
+ return all;
613
+ }
614
+ /**
615
+ * Get category ID containing a specific activity definition
616
+ *
617
+ * Optimization: Searches cache first, loads on-demand if needed
618
+ */
619
+ async getCategoryIdByActivityName(activityName) {
620
+ // ✅ Search in cached activity definitions
621
+ for (const [categoryId, definitions] of this.activitiesByCategory.entries()) {
622
+ if (definitions.some(def => def.name === activityName)) {
623
+ return categoryId;
624
+ }
625
+ }
626
+ // ✅ Try loading the activity definition to find its category
627
+ const definition = await this.getActivityByName(activityName);
628
+ if (definition && definition.category) {
629
+ // Try to find category by name/id
630
+ const categories = await this.getCategories();
631
+ const found = categories.find(cat => cat.id === definition.category || cat.title === definition.category);
632
+ if (found) {
633
+ return found.id;
634
+ }
635
+ }
636
+ return undefined;
637
+ }
638
+ /**
639
+ * Get category path for an activity
640
+ */
641
+ async getCategoriesPathByActivityName(activityName) {
642
+ const categoryId = await this.getCategoryIdByActivityName(activityName);
643
+ if (!categoryId) {
644
+ throw new Error(`Activity '${activityName}' not found in any category`);
645
+ }
646
+ return this.getCategoriesPathById(categoryId);
647
+ }
648
+ //#endregion
649
+ //#region ---- Private: Data Loading ----
650
+ /**
651
+ * Load categories from providers and cache results
652
+ *
653
+ * Optimization: Tracks provider ownership to avoid unnecessary API calls
654
+ * - For root (parentId = undefined): Query ALL providers
655
+ * - For children: Only query the provider that owns the parent
656
+ */
657
+ async loadCategoriesFromProviders(parentId) {
658
+ try {
659
+ const resolvedProviders = await Promise.allSettled(this.categoryProviders);
660
+ const categories = [];
661
+ // Determine which provider(s) to query
662
+ const providerIndicesToQuery = parentId
663
+ ? this.getProviderIndexForCategory(parentId)
664
+ : null; // Root: query all providers
665
+ for (let i = 0; i < resolvedProviders.length; i++) {
666
+ const p = resolvedProviders[i];
667
+ // Skip if we have a specific provider index and this isn't it
668
+ if (providerIndicesToQuery !== null && !providerIndicesToQuery.includes(i)) {
669
+ continue;
670
+ }
671
+ if (p.status === 'fulfilled' && p.value && typeof p.value.getList === 'function') {
672
+ try {
673
+ const cats = await p.value.getList(parentId);
674
+ if (Array.isArray(cats) && cats.length > 0) {
675
+ categories.push(...cats);
676
+ // ✅ Track ownership: This provider INDEX owns these categories
677
+ cats.forEach(cat => this.categoryOwnership.set(cat.id, i));
678
+ }
679
+ }
680
+ catch {
681
+ // Continue on error - try other providers
682
+ }
683
+ }
684
+ }
685
+ // ✅ Cache results for fast subsequent access
686
+ this.categoriesByParentId.set(parentId, categories);
687
+ categories.forEach(cat => this.categoriesById.set(cat.id, cat));
688
+ return categories;
689
+ }
690
+ finally {
691
+ this.pendingCategoriesRequests.delete(parentId);
692
+ }
693
+ }
694
+ /**
695
+ * Get the provider index that owns a specific category
696
+ *
697
+ * @returns Array with provider index, or null if ownership unknown (query all)
698
+ */
699
+ getProviderIndexForCategory(categoryId) {
700
+ const ownerIndex = this.categoryOwnership.get(categoryId);
701
+ if (ownerIndex !== undefined) {
702
+ return [ownerIndex];
703
+ }
704
+ // Ownership unknown - will query all providers (fallback)
705
+ return null;
706
+ }
707
+ /**
708
+ * Load activity definitions from providers and cache results
709
+ *
710
+ * Optimization: Only queries the provider that owns the category
711
+ * Uses provider INDEX to match category provider with activity provider
712
+ */
713
+ async loadActivitiesFromProviders(categoryId) {
714
+ try {
715
+ const resolvedProviders = await Promise.allSettled(this.activityProviders);
716
+ const definitions = [];
717
+ // ✅ Smart routing: Get provider INDEX that owns this category
718
+ const ownerIndex = this.categoryOwnership.get(categoryId);
719
+ const providerIndicesToQuery = ownerIndex !== undefined ? [ownerIndex] : null;
720
+ for (let i = 0; i < resolvedProviders.length; i++) {
721
+ const p = resolvedProviders[i];
722
+ // Skip if we have a specific provider index and this isn't it
723
+ if (providerIndicesToQuery !== null && !providerIndicesToQuery.includes(i)) {
724
+ continue;
725
+ }
726
+ if (p.status === 'fulfilled' && p.value && typeof p.value.getList === 'function') {
727
+ try {
728
+ const defs = await p.value.getList(categoryId);
729
+ if (Array.isArray(defs)) {
730
+ definitions.push(...defs);
731
+ }
732
+ }
733
+ catch {
734
+ // Continue on error - try other providers
735
+ }
736
+ }
737
+ }
738
+ // ✅ Cache results for fast subsequent access
739
+ this.activitiesByCategory.set(categoryId, definitions);
740
+ definitions.forEach(def => {
741
+ if (def.name) {
742
+ this.activitiesByName.set(def.name, def);
743
+ }
744
+ });
745
+ return definitions;
746
+ }
747
+ finally {
748
+ this.pendingActivitiesRequests.delete(categoryId);
749
+ }
750
+ }
751
+ /**
752
+ * Load single activity definition from providers and cache result
753
+ */
754
+ async loadActivityFromProviders(name) {
755
+ try {
756
+ const resolvedProviders = await Promise.allSettled(this.activityProviders);
757
+ // Try providers first
758
+ for (const p of resolvedProviders) {
759
+ if (p.status === 'fulfilled' && p.value && typeof p.value.getById === 'function') {
760
+ try {
761
+ const result = await p.value.getById(name);
762
+ if (result) {
763
+ this.activitiesByName.set(name, result);
764
+ return result;
765
+ }
766
+ }
767
+ catch {
768
+ // Continue on error
769
+ }
770
+ }
771
+ }
772
+ // Fallback: Search in cached activity definitions
773
+ for (const definitions of this.activitiesByCategory.values()) {
774
+ const found = definitions.find(def => def.name === name);
775
+ if (found) {
776
+ this.activitiesByName.set(name, found);
777
+ return found;
778
+ }
779
+ }
780
+ return undefined;
781
+ }
782
+ finally {
783
+ this.pendingActivityRequests.delete(name);
784
+ }
785
+ }
786
+ /**
787
+ * Breadth-first search through category hierarchy
788
+ */
789
+ async searchCategoryInHierarchy(categoryId) {
790
+ const searchQueue = [undefined];
791
+ const searched = new Set();
792
+ while (searchQueue.length > 0) {
793
+ const parentId = searchQueue.shift();
794
+ if (searched.has(parentId))
795
+ continue;
796
+ searched.add(parentId);
797
+ const categories = await this.getCategories(parentId);
798
+ const found = categories.find(cat => cat.id === categoryId);
799
+ if (found) {
800
+ return found;
801
+ }
802
+ // ✅ Optimization: Only search children if childrenCount > 0
803
+ for (const category of categories) {
804
+ if (category.childrenCount > 0 && !searched.has(category.id)) {
805
+ searchQueue.push(category.id);
806
+ }
807
+ }
808
+ }
809
+ return undefined;
810
+ }
811
+ //#endregion
812
+ //#region ---- Cache Management ----
813
+ /**
814
+ * Check if category has children (uses cached count)
815
+ */
816
+ categoryHasChildren(categoryId) {
817
+ const category = this.categoriesById.get(categoryId);
818
+ return category ? category.childrenCount > 0 : false;
819
+ }
820
+ /**
821
+ * Check if category has activities (uses cached count)
822
+ */
823
+ categoryHasActivities(categoryId) {
824
+ const category = this.categoriesById.get(categoryId);
825
+ return category ? (category.itemsCount ?? 0) > 0 : false;
826
+ }
827
+ /**
828
+ * Clear all caches
829
+ */
830
+ clearAllCache() {
831
+ this.categoriesById.clear();
832
+ this.categoriesByParentId.clear();
833
+ this.activitiesByCategory.clear();
834
+ this.activitiesByName.clear();
835
+ this.categoryOwnership.clear();
836
+ this.pendingCategoriesRequests.clear();
837
+ this.pendingActivitiesRequests.clear();
838
+ this.pendingActivityRequests.clear();
839
+ }
840
+ /**
841
+ * Clear categories cache only
842
+ */
843
+ clearCategoriesCache() {
844
+ this.categoriesById.clear();
845
+ this.categoriesByParentId.clear();
846
+ this.categoryOwnership.clear();
847
+ this.pendingCategoriesRequests.clear();
848
+ }
849
+ /**
850
+ * Clear activities cache only
851
+ */
852
+ clearActivitiesCache() {
853
+ this.activitiesByCategory.clear();
854
+ this.activitiesByName.clear();
855
+ this.pendingActivitiesRequests.clear();
856
+ this.pendingActivityRequests.clear();
857
+ }
858
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPActivityDefinitionService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
859
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPActivityDefinitionService, providedIn: 'root' }); }
860
+ }
861
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPActivityDefinitionService, decorators: [{
862
+ type: Injectable,
863
+ args: [{
864
+ providedIn: 'root',
865
+ }]
866
+ }] });
867
+
868
+ /**
869
+ * Injection token for workflow engine.
870
+ * Default implementation is AXPWorkflowLocalEngine.
871
+ */
872
+ const AXP_WORKFLOW_ENGINE = new InjectionToken('AXP_WORKFLOW_ENGINE');
873
+
874
+ //#endregion
875
+ /**
876
+ * Workflow Expression Scope Service
877
+ *
878
+ * Shared service for building expression evaluation scope from workflow data.
879
+ * Used by both Local Engine and Mock Runtime to manage workflow data (inputs, variables, outputs).
880
+ *
881
+ * Responsibilities:
882
+ * - Build expression evaluation scope from workflow state
883
+ * - Provide context.eval() function for accessing workflow data
884
+ * - Manage workflow data structure (inputs, variables, outputs)
885
+ *
886
+ * This service does NOT:
887
+ * - Execute activities
888
+ * - Evaluate expressions (delegates to AXPExpressionEvaluatorService)
889
+ * - Manage workflow state (handled by runtime)
890
+ *
891
+ * @example
892
+ * ```typescript
893
+ * const scopeService = inject(WorkflowExpressionScopeService);
894
+ *
895
+ * // Build scope from workflow state
896
+ * const scope = scopeService.buildScope({
897
+ * inputs: state.input || {},
898
+ * variables: state.variables || {},
899
+ * outputs: activityOutputs
900
+ * });
901
+ *
902
+ * // Or build from state directly
903
+ * const scope = scopeService.buildScopeFromState(state, activityOutputs);
904
+ * ```
905
+ */
906
+ class WorkflowExpressionScopeService {
907
+ //#region ---- Private Helpers (dot-notation input normalization) ----
908
+ /**
909
+ * Collect dot-notation key-value pairs from nested objects so that e.g.
910
+ * { metadata: { "metadata.questionnaire.id": "x" } } becomes { "metadata.questionnaire.id": "x" } at root.
911
+ * Top-level non-dot keys are kept as-is.
912
+ */
913
+ flattenDotKeysFromTree(obj) {
914
+ if (obj === null || typeof obj !== 'object')
915
+ return obj;
916
+ const result = {};
917
+ for (const [key, value] of Object.entries(obj)) {
918
+ if (key.includes('.')) {
919
+ result[key] = value;
920
+ }
921
+ else if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
922
+ for (const [k, v] of Object.entries(value)) {
923
+ if (k.includes('.')) {
924
+ result[k] = v;
925
+ }
926
+ else {
927
+ result[key + '.' + k] = v;
928
+ }
929
+ }
930
+ }
931
+ else {
932
+ result[key] = value;
933
+ }
934
+ }
935
+ return result;
936
+ }
937
+ /**
938
+ * Expand flat keys with dots into nested objects.
939
+ * e.g. { "metadata.questionnaire.id": "x" } -> { metadata: { questionnaire: { id: "x" } } }.
940
+ */
941
+ expandDotKeys(obj) {
942
+ if (obj === null || typeof obj !== 'object')
943
+ return obj;
944
+ const result = {};
945
+ const dotKeys = [];
946
+ const simpleKeys = [];
947
+ for (const [key, value] of Object.entries(obj)) {
948
+ if (key.includes('.')) {
949
+ dotKeys.push([key, value]);
950
+ }
951
+ else {
952
+ simpleKeys.push([key, value]);
953
+ }
954
+ }
955
+ for (const [key, value] of dotKeys) {
956
+ const parts = key.split('.');
957
+ let current = result;
958
+ for (let i = 0; i < parts.length - 1; i++) {
959
+ const part = parts[i];
960
+ if (!(part in current) || typeof current[part] !== 'object' || current[part] === null) {
961
+ current[part] = {};
962
+ }
963
+ current = current[part];
964
+ }
965
+ current[parts[parts.length - 1]] = value;
966
+ }
967
+ for (const [key, value] of simpleKeys) {
968
+ result[key] = value;
969
+ }
970
+ return result;
971
+ }
972
+ /**
973
+ * Normalize workflow input so that flat dot-notation keys (e.g. from form schema
974
+ * "metadata.questionnaire.id") become nested for expression access (inputs.metadata.questionnaire.id).
975
+ */
976
+ normalizeInputs(input) {
977
+ if (!input || typeof input !== 'object')
978
+ return {};
979
+ const flattened = this.flattenDotKeysFromTree(input);
980
+ return this.expandDotKeys(flattened);
981
+ }
982
+ //#endregion
983
+ //#region ---- Public Methods ----
984
+ /**
985
+ * Build expression evaluation scope for workflow activities.
986
+ *
987
+ * Provides workflow-specific data (inputs, variables, outputs) and context.eval() function.
988
+ * Other data (session, current user, etc.) are provided by expression evaluator scope providers.
989
+ *
990
+ * Scope includes:
991
+ * - inputs: Workflow input values (accessible as inputs.propertyName)
992
+ * - variables: Workflow state variables (accessible as variables.propertyName or vars.propertyName)
993
+ * - outputs: Previous activity outputs (accessible as outputs.activityId)
994
+ * - context.eval(path): Function to access nested properties from workflow data
995
+ *
996
+ * Expressions can use:
997
+ * - {{inputs.userName}} - Direct access to workflow input
998
+ * - {{variables.someVar}} or {{vars.someVar}} - Direct access to workflow variable
999
+ * - {{outputs.activityId.property}} - Direct access to previous activity output
1000
+ * - {{context.eval("inputs.userName")}} - Access via context.eval (supports nested paths)
1001
+ * - {{context.eval("variables.someVar")}} - Access variables via context.eval
1002
+ * - {{context.eval("outputs.activityId.property")}} - Access outputs via context.eval
1003
+ * - {{session.currentUser().name}} - Access current user via expression evaluator scope providers
1004
+ *
1005
+ * The context.eval() function provides a unified way to access all workflow data,
1006
+ * similar to how entity-detail-list uses context.eval() to access parent data.
1007
+ *
1008
+ * @param context - Workflow expression context containing inputs, variables, and outputs
1009
+ * @returns Expression evaluator scope with workflow data and context.eval() function
1010
+ */
1011
+ buildScope(context) {
1012
+ // Normalize inputs so flat dot-notation keys (e.g. metadata.questionnaire.id from forms) become nested for expressions
1013
+ const inputs = this.normalizeInputs(context.inputs || {});
1014
+ // Build merged workflow data object for context.eval()
1015
+ // This allows expressions like: context.eval("inputs.userName") or context.eval("variables.count")
1016
+ const workflowData = {
1017
+ inputs,
1018
+ variables: context.variables || {},
1019
+ vars: context.variables || {}, // Alias for convenience
1020
+ outputs: context.outputs || {},
1021
+ };
1022
+ // Build scope object with workflow-specific data and context.eval()
1023
+ // Note: AXPExpressionEvaluatorScope type expects { [namespace]: { [name]: Function } }
1024
+ // but evaluate() method actually accepts flat objects with values too
1025
+ // We'll use 'any' to allow both values and functions
1026
+ const scope = {
1027
+ // Direct access to workflow data
1028
+ inputs: workflowData.inputs,
1029
+ variables: workflowData.variables,
1030
+ vars: workflowData.vars,
1031
+ outputs: workflowData.outputs,
1032
+ // Context object with eval function (similar to entity-detail-list pattern)
1033
+ context: {
1034
+ eval: (path) => {
1035
+ // Use lodash get to access nested properties
1036
+ // Supports paths like: "inputs.userName", "variables.count", "outputs.activityId.property"
1037
+ return get(workflowData, path);
1038
+ },
1039
+ },
1040
+ };
1041
+ return scope;
1042
+ }
1043
+ /**
1044
+ * Build expression evaluation scope from workflow instance state.
1045
+ *
1046
+ * Convenience method that extracts data from AXPWorkflowInstanceState.
1047
+ *
1048
+ * @param state - Workflow instance state
1049
+ * @param activityOutputs - Map of activity outputs (activityId -> output)
1050
+ * @returns Expression evaluator scope with workflow data and context.eval() function
1051
+ */
1052
+ buildScopeFromState(state, activityOutputs) {
1053
+ // Convert activity outputs to record format
1054
+ const outputs = {};
1055
+ if (activityOutputs) {
1056
+ if (activityOutputs instanceof Map) {
1057
+ activityOutputs.forEach((output, activityId) => {
1058
+ outputs[activityId] = output;
1059
+ });
1060
+ }
1061
+ else {
1062
+ Object.assign(outputs, activityOutputs);
1063
+ }
1064
+ }
1065
+ // Convenience alias for the last activity output (if available).
1066
+ // This allows expressions like: {{outputs.last.someField}}
1067
+ if (outputs['last'] === undefined && state.lastActivityOutput !== undefined) {
1068
+ outputs['last'] = state.lastActivityOutput;
1069
+ }
1070
+ return this.buildScope({
1071
+ inputs: this.normalizeInputs((state.input || {})),
1072
+ variables: state.variables || {},
1073
+ outputs: outputs,
1074
+ });
1075
+ }
1076
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: WorkflowExpressionScopeService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
1077
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: WorkflowExpressionScopeService, providedIn: 'root' }); }
1078
+ }
1079
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: WorkflowExpressionScopeService, decorators: [{
1080
+ type: Injectable,
1081
+ args: [{
1082
+ providedIn: 'root'
1083
+ }]
1084
+ }] });
1085
+
1086
+ //#region ---- Constants ----
1087
+ /**
1088
+ * Activity types handled internally by the workflow engine (e.g. mock backend).
1089
+ * When such an activity is not registered as Command on the client, we skip execution
1090
+ * without warning — the engine will run it when executing the workflow.
1091
+ */
1092
+ const ENGINE_BUILTIN_ACTIVITY_TYPES = new Set([
1093
+ 'workflow-activity:set-variable',
1094
+ 'workflow-activity:http-request',
1095
+ ]);
1096
+ //#endregion
1097
+ /**
1098
+ * Activity Executor Service
1099
+ *
1100
+ * Service for executing workflow activities via CommandBus.
1101
+ * Automatically evaluates expressions in activity inputs before execution.
1102
+ *
1103
+ * @example
1104
+ * ```typescript
1105
+ * const executor = inject(ActivityExecutor);
1106
+ *
1107
+ * // Execute activity with task and workflow state (expressions will be evaluated)
1108
+ * const result = await executor.execute(task, workflowState, activityOutputs);
1109
+ * ```
1110
+ */
1111
+ class ActivityExecutor {
1112
+ constructor() {
1113
+ //#region ---- Services & Dependencies ----
1114
+ this.commandService = inject(AXPCommandService);
1115
+ this.expressionEvaluator = inject(AXPExpressionEvaluatorService);
1116
+ this.expressionScopeService = inject(WorkflowExpressionScopeService);
1117
+ }
1118
+ //#endregion
1119
+ //#region ---- Public Methods ----
1120
+ /**
1121
+ * Execute a workflow activity with expression evaluation.
1122
+ *
1123
+ * Evaluates expressions in activity inputs using workflow state,
1124
+ * then executes the activity via CommandBus.
1125
+ *
1126
+ * @param task - Workflow task containing activity information
1127
+ * @param workflowState - Current workflow instance state (for expression evaluation)
1128
+ * @param activityOutputs - Map of previous activity outputs (for expression evaluation)
1129
+ * @returns Execution result with output and outcome
1130
+ */
1131
+ async execute(task, workflowState, activityOutputs) {
1132
+ try {
1133
+ const activityName = task.activityType;
1134
+ // Evaluate inputs if workflow state is provided
1135
+ let evaluatedInputs = task.input || {};
1136
+ if (workflowState) {
1137
+ // Prefer explicit outputs, fallback to outputs stored in state
1138
+ const outputsForScope = activityOutputs ??
1139
+ workflowState.activityOutputs ??
1140
+ undefined;
1141
+ // Build expression scope from workflow state
1142
+ const scope = this.expressionScopeService.buildScopeFromState(workflowState, outputsForScope);
1143
+ // Evaluate all inputs recursively (handles nested objects and arrays)
1144
+ evaluatedInputs = await this.expressionEvaluator.evaluate(task.input || {}, scope);
1145
+ }
1146
+ // Check if command exists
1147
+ const commandExists = this.commandService.exists(activityName);
1148
+ if (!commandExists) {
1149
+ if (!ENGINE_BUILTIN_ACTIVITY_TYPES.has(activityName)) {
1150
+ console.warn(`[ActivityExecutor] ⚠️ Activity '${activityName}' is not registered as Command. ` +
1151
+ `Skipping execution.`);
1152
+ }
1153
+ return {
1154
+ output: null,
1155
+ outcome: 'Done'
1156
+ };
1157
+ }
1158
+ // Flatten properties if nested (workflow-studio format)
1159
+ let commandInput = evaluatedInputs;
1160
+ if (commandInput['properties'] && typeof commandInput['properties'] === 'object') {
1161
+ // Flatten: {properties: {text: "..."}} -> {text: "..."}
1162
+ commandInput = { ...commandInput['properties'] };
1163
+ }
1164
+ // Execute activity via CommandBus
1165
+ // Activities (AXPActivity) return {output, outcome}; legacy may return {output, outcomes}
1166
+ const result = await this.commandService.execute(activityName, commandInput);
1167
+ if (!result) {
1168
+ return {
1169
+ output: null,
1170
+ outcome: 'Failed',
1171
+ };
1172
+ }
1173
+ if (!result.success) {
1174
+ return {
1175
+ output: {
1176
+ error: result.message?.text,
1177
+ },
1178
+ outcome: 'Failed',
1179
+ };
1180
+ }
1181
+ const commandResult = result.data;
1182
+ // Prefer unified outcome in result.metadata; fall back to legacy data.outcome/outcomes.
1183
+ const metadataOutcome = result?.metadata?.['outcome'];
1184
+ let outcome = 'Done';
1185
+ if (typeof metadataOutcome === 'string' && metadataOutcome.length > 0) {
1186
+ outcome = metadataOutcome;
1187
+ }
1188
+ else if (typeof commandResult?.outcome === 'string' && commandResult.outcome.length > 0) {
1189
+ outcome = commandResult.outcome;
1190
+ }
1191
+ else {
1192
+ const outcomes = (commandResult?.outcomes ?? {});
1193
+ if (outcomes && typeof outcomes === 'object' && Object.keys(outcomes).length > 0) {
1194
+ outcome = outcomes['Done'] ? 'Done' : Object.keys(outcomes)[0] || 'Done';
1195
+ }
1196
+ }
1197
+ // Prefer output wrapper when present; otherwise treat data itself as output.
1198
+ const output = commandResult && typeof commandResult === 'object' && 'output' in commandResult
1199
+ ? commandResult.output
1200
+ : commandResult;
1201
+ return {
1202
+ output: output ?? null,
1203
+ outcome,
1204
+ };
1205
+ }
1206
+ catch (error) {
1207
+ console.error(`[ActivityExecutor] ❌ Error evaluating expressions or executing activity:`, error);
1208
+ return {
1209
+ output: { error: error.message || 'Unknown error' },
1210
+ outcome: 'Failed'
1211
+ };
1212
+ }
1213
+ }
1214
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: ActivityExecutor, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
1215
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: ActivityExecutor, providedIn: 'root' }); }
1216
+ }
1217
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: ActivityExecutor, decorators: [{
1218
+ type: Injectable,
1219
+ args: [{
1220
+ providedIn: 'root'
1221
+ }]
1222
+ }] });
1223
+
1224
+ //#endregion
1225
+ /**
1226
+ * Workflow Manager - Facade for workflow lifecycle orchestration.
1227
+ *
1228
+ * This service is the ONLY interface the frontend uses to interact with workflows.
1229
+ * It follows Clean Architecture principles and does NOT contain execution or business logic.
1230
+ *
1231
+ * Responsibilities:
1232
+ * - Orchestrate workflow lifecycle (start, execute, complete, resume)
1233
+ * - Delegate execution to ActivityExecutor
1234
+ * - Cache workflow state in memory
1235
+ * - Expose a stable API for UI
1236
+ *
1237
+ * Rules:
1238
+ * - No HTTP calls (delegates to AXPWorkflowEngine)
1239
+ * - No CommandBus / Command execution (delegates to ActivityExecutor)
1240
+ * - No workflow branching logic (backend decides)
1241
+ * - No business validation (backend validates)
1242
+ * - No backend assumptions (uses abstract runtime service)
1243
+ */
1244
+ class AXPWorkflowManager {
1245
+ constructor() {
1246
+ //#region ---- Services & Dependencies ----
1247
+ this.workflowEngine = inject(AXP_WORKFLOW_ENGINE);
1248
+ this.activityExecutor = inject(ActivityExecutor);
1249
+ //#endregion
1250
+ //#region ---- State Cache ----
1251
+ /**
1252
+ * Cache workflow states in memory for quick access.
1253
+ * Key: instanceId
1254
+ * Value: AXPWorkflowInstanceState
1255
+ */
1256
+ this.stateCache = new Map();
1257
+ /**
1258
+ * Cache TTL in milliseconds (5 minutes).
1259
+ */
1260
+ this.CACHE_TTL = 5 * 60 * 1000;
1261
+ }
1262
+ //#endregion
1263
+ //#region ---- Public Methods ----
1264
+ /**
1265
+ * Execute frontend activities interactively until reaching workflow-activity:human-task or completion.
1266
+ *
1267
+ * Interactive = show form/popup immediately (user sees and acts). Only workflow-activity:human-task
1268
+ * is not interactive (goes to task board). Other frontend activities (show-layout-popup, show-toast, etc.)
1269
+ * even with taskType human-task in definition are executed here.
1270
+ *
1271
+ * @param instanceId - Workflow instance ID
1272
+ * @param task - Current task to execute
1273
+ * @param state - Current workflow state
1274
+ * @param lastActivityOutput - Last activity output (for expression evaluation)
1275
+ * @returns Final result with nextTask (if workflow-activity:human-task) or completion status
1276
+ */
1277
+ async executeInteractiveFlow(instanceId, task, state, activityOutputs) {
1278
+ let currentTask = task;
1279
+ let currentState = state;
1280
+ let currentActivityOutputs = {
1281
+ ...(currentState.activityOutputs || {}),
1282
+ ...(activityOutputs || {}),
1283
+ };
1284
+ const maxIterations = 100; // Prevent infinite loops
1285
+ let iterationCount = 0;
1286
+ while (currentTask && iterationCount < maxIterations) {
1287
+ iterationCount++;
1288
+ // Interactive = frontend executionMode and NOT workflow-activity:human-task (that one goes to task board)
1289
+ const isInteractive = (currentTask.executionMode === 'frontend' || currentTask.executionMode === 'both') &&
1290
+ currentTask.activityType !== 'workflow-activity:human-task';
1291
+ if (isInteractive) {
1292
+ // Execute frontend activity
1293
+ const execResult = await this.activityExecutor.execute(currentTask, currentState, currentActivityOutputs);
1294
+ // Track outputs locally (backend should also persist and return them)
1295
+ currentActivityOutputs = {
1296
+ ...currentActivityOutputs,
1297
+ [currentTask.activityId]: execResult.output,
1298
+ };
1299
+ // Send result to backend
1300
+ const completeResponse = await this.workflowEngine.frontActivtyComplete({
1301
+ instanceId,
1302
+ activityNode: currentTask.activityId,
1303
+ output: execResult.output || {},
1304
+ outcome: execResult.outcome,
1305
+ });
1306
+ // Update state cache
1307
+ if (completeResponse.state) {
1308
+ const normalizedState = { ...completeResponse.state };
1309
+ if (normalizedState.lastUpdated && !(normalizedState.lastUpdated instanceof Date)) {
1310
+ normalizedState.lastUpdated = new Date(normalizedState.lastUpdated);
1311
+ }
1312
+ currentState = normalizedState;
1313
+ // Prefer outputs returned by backend; fallback to local cache
1314
+ currentActivityOutputs = {
1315
+ ...currentActivityOutputs,
1316
+ ...(normalizedState.activityOutputs || {}),
1317
+ };
1318
+ this.stateCache.set(instanceId, normalizedState);
1319
+ }
1320
+ // Backend decides: if no nextTask, workflow is completed
1321
+ if (!completeResponse.nextTask) {
1322
+ return {
1323
+ nextTask: null,
1324
+ state: currentState,
1325
+ output: completeResponse.output,
1326
+ };
1327
+ }
1328
+ // Backend decides: if nextTask is workflow-activity:human-task or not frontend, return it (task board or done)
1329
+ const nextInteractive = (completeResponse.nextTask.executionMode === 'frontend' || completeResponse.nextTask.executionMode === 'both') &&
1330
+ completeResponse.nextTask.activityType !== 'workflow-activity:human-task';
1331
+ if (!nextInteractive) {
1332
+ return {
1333
+ nextTask: completeResponse.nextTask,
1334
+ state: currentState,
1335
+ };
1336
+ }
1337
+ // Continue with next interactive frontend task
1338
+ currentTask = completeResponse.nextTask;
1339
+ }
1340
+ else {
1341
+ // Not interactive (e.g. workflow-activity:human-task) - return as-is for task board
1342
+ return {
1343
+ nextTask: currentTask,
1344
+ state: currentState,
1345
+ };
1346
+ }
1347
+ }
1348
+ // Max iterations reached
1349
+ if (iterationCount >= maxIterations) {
1350
+ console.warn(`[AXPWorkflowManager] ⚠️ Maximum iterations (${maxIterations}) reached`);
1351
+ }
1352
+ return {
1353
+ nextTask: currentTask,
1354
+ state: currentState,
1355
+ };
1356
+ }
1357
+ /**
1358
+ * Start a new workflow instance.
1359
+ *
1360
+ * Creates a new workflow instance in backend and returns instance ID.
1361
+ * Backend decides what to do: returns pendingTask or indicates completion.
1362
+ *
1363
+ * @param workflowId - Workflow ID to start
1364
+ * @param input - Initial input data (optional)
1365
+ * @returns Start result with instanceId, state, and nextTask
1366
+ *
1367
+ * @example
1368
+ * ```typescript
1369
+ * const result = await workflowManager.start('my-workflow', { userId: '123' });
1370
+ *
1371
+ * if (result.success && result.nextTask) {
1372
+ * // Execute task if frontend
1373
+ * if (result.nextTask.executionMode === 'frontend') {
1374
+ * const execResult = await workflowManager.execute(result.nextTask);
1375
+ * await workflowManager.complete(result.instanceId!, result.nextTask, execResult.outcome, execResult.output);
1376
+ * }
1377
+ * }
1378
+ * ```
1379
+ */
1380
+ async start(workflowId, input = {}) {
1381
+ try {
1382
+ const response = await this.workflowEngine.start({
1383
+ workflowId,
1384
+ input,
1385
+ });
1386
+ // Cache state (normalize Date)
1387
+ let startNormalizedState = { ...response.state };
1388
+ if (startNormalizedState.lastUpdated && !(startNormalizedState.lastUpdated instanceof Date)) {
1389
+ startNormalizedState.lastUpdated = new Date(startNormalizedState.lastUpdated);
1390
+ }
1391
+ this.stateCache.set(response.instanceId, startNormalizedState);
1392
+ // 🎯 Interactive flow: Execute frontend activities that are NOT workflow-activity:human-task (those go to task board)
1393
+ let finalNextTask = response.pendingTask || null;
1394
+ let finalOutput = startNormalizedState.output;
1395
+ const pendingTask = response.pendingTask;
1396
+ if (pendingTask &&
1397
+ (pendingTask.executionMode === 'frontend' || pendingTask.executionMode === 'both') &&
1398
+ pendingTask.activityType !== 'workflow-activity:human-task') {
1399
+ const interactiveResult = await this.executeInteractiveFlow(response.instanceId, pendingTask, startNormalizedState, response.activityOutputs || response.state.activityOutputs);
1400
+ finalNextTask = interactiveResult.nextTask;
1401
+ startNormalizedState = interactiveResult.state;
1402
+ if (interactiveResult.output !== undefined) {
1403
+ finalOutput = interactiveResult.output;
1404
+ }
1405
+ // Update cache with final state
1406
+ this.stateCache.set(response.instanceId, startNormalizedState);
1407
+ }
1408
+ // If backend returned null or non-executable task, return it as-is
1409
+ // Backend already decided workflow status (suspended, completed, etc.)
1410
+ return {
1411
+ success: true,
1412
+ instanceId: response.instanceId,
1413
+ state: startNormalizedState,
1414
+ nextTask: finalNextTask,
1415
+ output: finalOutput,
1416
+ };
1417
+ }
1418
+ catch (error) {
1419
+ console.error('[AXPWorkflowManager] ❌ Error starting workflow:', error);
1420
+ return {
1421
+ success: false,
1422
+ error: error.message || 'Failed to start workflow',
1423
+ };
1424
+ }
1425
+ }
1426
+ /**
1427
+ * Resume a suspended workflow (e.g., after user interaction).
1428
+ *
1429
+ * Backend determines nextStep based on outcome and outcomeConnections.
1430
+ * Client only provides instanceId, stepId, outcome, and optional userInput.
1431
+ *
1432
+ * @param instanceId - Workflow instance ID
1433
+ * @param stepId - Step ID that was waiting for user input
1434
+ * @param outcome - User action outcome (e.g., 'Confirmed', 'Cancelled', 'Submitted')
1435
+ * @param userInput - Optional user input data
1436
+ * @param taskToken - Secure task token (required for secure resumption)
1437
+ * @returns Resume result with next task (if any)
1438
+ */
1439
+ async resume(instanceId, stepId, outcome, userInput, taskToken) {
1440
+ try {
1441
+ // Ensure taskToken is provided for secure resumption
1442
+ if (!taskToken) {
1443
+ throw new Error('Missing taskToken for resume operation');
1444
+ }
1445
+ // Backend handles everything: checks outcomeConnections and determines nextStep
1446
+ const response = await this.workflowEngine.resume({
1447
+ instanceId,
1448
+ stepId,
1449
+ taskToken,
1450
+ outcome,
1451
+ userInput,
1452
+ });
1453
+ // Update cache with state from backend
1454
+ let normalizedState = response.state ? { ...response.state } : undefined;
1455
+ if (normalizedState && normalizedState.lastUpdated && !(normalizedState.lastUpdated instanceof Date)) {
1456
+ normalizedState.lastUpdated = new Date(normalizedState.lastUpdated);
1457
+ }
1458
+ if (normalizedState) {
1459
+ this.stateCache.set(instanceId, normalizedState);
1460
+ }
1461
+ // 🎯 Interactive flow: Execute frontend activities that are NOT workflow-activity:human-task (those go to task board)
1462
+ let finalNextTask = response.nextTask || null;
1463
+ let finalOutput = response.output;
1464
+ const nextTask = response.nextTask;
1465
+ if (nextTask &&
1466
+ (nextTask.executionMode === 'frontend' || nextTask.executionMode === 'both') &&
1467
+ nextTask.activityType !== 'workflow-activity:human-task') {
1468
+ const interactiveResult = await this.executeInteractiveFlow(instanceId, nextTask, normalizedState, normalizedState?.activityOutputs);
1469
+ finalNextTask = interactiveResult.nextTask;
1470
+ normalizedState = interactiveResult.state;
1471
+ if (interactiveResult.output !== undefined) {
1472
+ finalOutput = interactiveResult.output;
1473
+ }
1474
+ // Update cache with final state
1475
+ this.stateCache.set(instanceId, normalizedState);
1476
+ }
1477
+ // If backend returned null or non-executable task, return it as-is
1478
+ // Backend already decided workflow status (suspended, completed, etc.)
1479
+ return {
1480
+ success: true,
1481
+ instanceId,
1482
+ state: normalizedState || response.state,
1483
+ nextTask: finalNextTask,
1484
+ output: finalOutput,
1485
+ };
1486
+ }
1487
+ catch (error) {
1488
+ console.error('[AXPWorkflowManager] ❌ Error resuming workflow:', error);
1489
+ return {
1490
+ success: false,
1491
+ instanceId,
1492
+ error: error.message || 'Failed to resume workflow',
1493
+ };
1494
+ }
1495
+ }
1496
+ /**
1497
+ * Get workflow instance state.
1498
+ *
1499
+ * Retrieves state from cache (if valid) or from backend.
1500
+ *
1501
+ * @param instanceId - Workflow instance ID
1502
+ * @returns Workflow instance state or null if not found
1503
+ */
1504
+ async getState(instanceId) {
1505
+ // Check cache first
1506
+ const cached = this.stateCache.get(instanceId);
1507
+ if (cached) {
1508
+ // Normalize lastUpdated to Date (for cache safety)
1509
+ const normalizedCached = { ...cached };
1510
+ if (normalizedCached.lastUpdated && !(normalizedCached.lastUpdated instanceof Date)) {
1511
+ normalizedCached.lastUpdated = new Date(normalizedCached.lastUpdated);
1512
+ }
1513
+ // Validate cache age
1514
+ const cacheAge = Date.now() - normalizedCached.lastUpdated.getTime();
1515
+ if (cacheAge < this.CACHE_TTL) {
1516
+ return normalizedCached;
1517
+ }
1518
+ }
1519
+ // Fetch from backend
1520
+ try {
1521
+ const state = await this.workflowEngine.getState({
1522
+ instanceId,
1523
+ });
1524
+ // Normalize lastUpdated to Date (for cache safety)
1525
+ const normalizedState = { ...state };
1526
+ if (normalizedState.lastUpdated && !(normalizedState.lastUpdated instanceof Date)) {
1527
+ normalizedState.lastUpdated = new Date(normalizedState.lastUpdated);
1528
+ }
1529
+ // Update cache
1530
+ this.stateCache.set(instanceId, normalizedState);
1531
+ return normalizedState;
1532
+ }
1533
+ catch (error) {
1534
+ console.error('[AXPWorkflowManager] ❌ Error getting workflow state:', error);
1535
+ return null;
1536
+ }
1537
+ }
1538
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowManager, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
1539
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowManager, providedIn: 'root' }); }
1540
+ }
1541
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowManager, decorators: [{
1542
+ type: Injectable,
1543
+ args: [{
1544
+ providedIn: 'root',
1545
+ }]
1546
+ }] });
1547
+
1548
+ const AXP_WORKFLOW_PROVIDER = new InjectionToken('AXP_WORKFLOW_PROVIDER', {
1549
+ factory: () => [],
1550
+ });
1551
+ const AXP_WORKFLOW_CATEGORY_PROVIDER = new InjectionToken('AXP_WORKFLOW_CATEGORY_PROVIDER', {
1552
+ factory: () => [],
1553
+ });
1554
+
1555
+ //#endregion
1556
+ /**
1557
+ * Local engine implementation that manages workflow progression and state.
1558
+ *
1559
+ * This engine:
1560
+ * - Returns frontend/both activities as pendingTask (does NOT execute them)
1561
+ * - Skips backend activities (does not error, continues execution)
1562
+ * - Maintains workflow state in memory
1563
+ * - Does not require backend API calls
1564
+ *
1565
+ * Execution of frontend tasks is handled by AXPWorkflowManager via ActivityExecutor.
1566
+ * This engine only manages workflow progression and state storage.
1567
+ *
1568
+ * This is the DEFAULT engine provider. Applications can override it with
1569
+ * an API-based engine implementation.
1570
+ */
1571
+ class AXPWorkflowLocalEngine {
1572
+ constructor() {
1573
+ //#region ---- Services & Dependencies ----
1574
+ this.activityDefinitionService = inject(AXPActivityDefinitionService);
1575
+ this.workflowProviders = inject(AXP_WORKFLOW_PROVIDER, { optional: true }) || [];
1576
+ //#endregion
1577
+ //#region ---- Instance Storage ----
1578
+ /**
1579
+ * In-memory storage for workflow instances.
1580
+ * Key: instanceId
1581
+ * Value: LocalWorkflowState
1582
+ */
1583
+ this.instances = new Map();
1584
+ /**
1585
+ * Task token storage for secure resume operations.
1586
+ * Key: taskToken
1587
+ * Value: { instanceId, activityId }
1588
+ */
1589
+ this.taskTokens = new Map();
1590
+ }
1591
+ //#endregion
1592
+ //#region ---- Public Methods (AXPWorkflowEngine) ----
1593
+ /**
1594
+ * Start a new workflow instance.
1595
+ *
1596
+ * Creates an in-memory workflow instance and progresses it.
1597
+ * Frontend/both activities are returned as pendingTask for external execution.
1598
+ * Backend activities are skipped.
1599
+ */
1600
+ async start(request) {
1601
+ console.log(`[WorkflowLocalEngine] 🚀 Starting workflow: ${request.workflowId}`, request);
1602
+ // Generate instance ID
1603
+ const instanceId = AXPDataGenerator.uuid();
1604
+ const now = new Date();
1605
+ // Load workflow definition
1606
+ console.log(`[WorkflowLocalEngine] 📥 Loading workflow definition: ${request.workflowId}`);
1607
+ const definition = await this.getDefinition(request.workflowId);
1608
+ if (!definition) {
1609
+ console.error(`[WorkflowLocalEngine] ❌ Workflow definition not found: ${request.workflowId}`);
1610
+ throw new Error(`Workflow definition not found: ${request.workflowId}`);
1611
+ }
1612
+ console.log(`[WorkflowLocalEngine] ✅ Definition loaded:`, {
1613
+ name: definition.name,
1614
+ activitiesCount: definition.graph?.activities?.length || 0,
1615
+ connectionsCount: definition.graph?.connections?.length || 0,
1616
+ });
1617
+ // Initialize workflow state
1618
+ const state = {
1619
+ instanceId,
1620
+ workflowId: request.workflowId,
1621
+ status: 'running',
1622
+ variables: {},
1623
+ activityOutputs: {},
1624
+ lastActivityOutput: undefined,
1625
+ input: request.input || {},
1626
+ output: undefined,
1627
+ lastUpdated: now,
1628
+ };
1629
+ // Create local state
1630
+ const localState = {
1631
+ instanceId,
1632
+ workflowId: request.workflowId,
1633
+ definition,
1634
+ state,
1635
+ completedActivities: new Set(),
1636
+ activityResults: new Map(),
1637
+ };
1638
+ // Store instance
1639
+ this.instances.set(instanceId, localState);
1640
+ // Execute workflow steps
1641
+ console.log(`[WorkflowLocalEngine] ⚙️ Executing workflow steps...`);
1642
+ const pendingTask = await this.executeWorkflowSteps(localState);
1643
+ // Update state
1644
+ localState.state.lastUpdated = new Date();
1645
+ console.log(`[WorkflowLocalEngine] ✅ Workflow started:`, {
1646
+ instanceId,
1647
+ status: localState.state.status,
1648
+ hasPendingTask: !!pendingTask,
1649
+ pendingTaskType: pendingTask?.activityType,
1650
+ });
1651
+ return {
1652
+ instanceId,
1653
+ state: localState.state,
1654
+ pendingTask: pendingTask || null,
1655
+ activityOutputs: localState.state.activityOutputs,
1656
+ lastActivityOutput: localState.state.lastActivityOutput,
1657
+ };
1658
+ }
1659
+ /**
1660
+ * Resume a suspended workflow instance.
1661
+ *
1662
+ * Validates task token, applies externally executed result,
1663
+ * and continues progressing workflow steps.
1664
+ */
1665
+ async resume(request) {
1666
+ // Validate task token
1667
+ const tokenInfo = this.taskTokens.get(request.taskToken);
1668
+ if (!tokenInfo || tokenInfo.instanceId !== request.instanceId || tokenInfo.activityId !== request.stepId) {
1669
+ throw new Error('Invalid task token');
1670
+ }
1671
+ // Get instance
1672
+ const localState = this.instances.get(request.instanceId);
1673
+ if (!localState) {
1674
+ throw new Error(`Workflow instance not found: ${request.instanceId}`);
1675
+ }
1676
+ // Store activity result (from external execution)
1677
+ const outcome = request.outcome ?? 'Done';
1678
+ localState.activityResults.set(request.stepId, {
1679
+ output: request.userInput || {},
1680
+ outcome,
1681
+ });
1682
+ localState.completedActivities.add(request.stepId);
1683
+ // Store activity output for expression evaluation
1684
+ localState.state.activityOutputs = {
1685
+ ...(localState.state.activityOutputs || {}),
1686
+ [request.stepId]: request.userInput || {},
1687
+ };
1688
+ localState.state.lastActivityOutput = request.userInput || {};
1689
+ // Merge output/userInput into state variables
1690
+ if (request.userInput) {
1691
+ localState.state.variables = {
1692
+ ...localState.state.variables,
1693
+ ...(request.userInput || {}),
1694
+ };
1695
+ }
1696
+ // Mark activity as completed and continue progression
1697
+ // Continue progressing workflow steps (skipping backend activities)
1698
+ const nextTask = await this.executeWorkflowSteps(localState);
1699
+ // Update state
1700
+ localState.state.lastUpdated = new Date();
1701
+ // Determine final status
1702
+ if (!nextTask && localState.state.status === 'running') {
1703
+ localState.state.status = 'completed';
1704
+ localState.state.output = localState.state.variables;
1705
+ }
1706
+ return {
1707
+ output: request.userInput || {},
1708
+ outcomes: { [outcome]: true },
1709
+ state: localState.state,
1710
+ nextTask: nextTask || null,
1711
+ };
1712
+ }
1713
+ /**
1714
+ * Get current workflow instance state.
1715
+ */
1716
+ async getState(request) {
1717
+ const localState = this.instances.get(request.instanceId);
1718
+ if (!localState) {
1719
+ throw new Error(`Workflow instance not found: ${request.instanceId}`);
1720
+ }
1721
+ // Normalize lastUpdated to Date (for cache safety)
1722
+ const state = { ...localState.state };
1723
+ if (state.lastUpdated && !(state.lastUpdated instanceof Date)) {
1724
+ state.lastUpdated = new Date(state.lastUpdated);
1725
+ }
1726
+ return state;
1727
+ }
1728
+ //#endregion
1729
+ //#region ---- Private Methods ----
1730
+ /**
1731
+ * Get workflow definition from available providers.
1732
+ */
1733
+ async getDefinition(workflowId) {
1734
+ // Try all providers in order
1735
+ const resolvedProviders = await Promise.allSettled(this.workflowProviders);
1736
+ for (const p of resolvedProviders) {
1737
+ if (p.status === 'fulfilled' && p.value) {
1738
+ const provider = p.value;
1739
+ // Check if provider has getByName method
1740
+ if (typeof provider.getByName === 'function') {
1741
+ try {
1742
+ const definition = await provider.getByName(workflowId);
1743
+ if (definition) {
1744
+ return definition;
1745
+ }
1746
+ }
1747
+ catch {
1748
+ // Continue on error - try other providers
1749
+ }
1750
+ }
1751
+ }
1752
+ }
1753
+ return null;
1754
+ }
1755
+ /**
1756
+ * Progress workflow steps starting from the current position.
1757
+ *
1758
+ * For frontend/both activities: returns task immediately (suspends workflow).
1759
+ * For backend activities: skips and continues.
1760
+ *
1761
+ * Returns the next pending task (if frontend activity found) or null (if completed).
1762
+ */
1763
+ async executeWorkflowSteps(localState) {
1764
+ const graph = localState.definition.graph;
1765
+ const activities = graph.activities || [];
1766
+ const connections = graph.connections || [];
1767
+ // Build activity map
1768
+ const activityMap = new Map();
1769
+ activities.forEach(activity => {
1770
+ activityMap.set(activity.id, activity);
1771
+ });
1772
+ // Build connection graph
1773
+ const outgoingConnections = new Map();
1774
+ const incomingConnections = new Map();
1775
+ connections.forEach(conn => {
1776
+ const sourceId = conn.source.activtyName;
1777
+ const targetId = conn.target.activtyName;
1778
+ if (!outgoingConnections.has(sourceId)) {
1779
+ outgoingConnections.set(sourceId, []);
1780
+ }
1781
+ outgoingConnections.get(sourceId).push(conn);
1782
+ if (!incomingConnections.has(targetId)) {
1783
+ incomingConnections.set(targetId, []);
1784
+ }
1785
+ incomingConnections.get(targetId).push(conn);
1786
+ });
1787
+ // Find starting activity (use startActivityId or no incoming connections, or first activity)
1788
+ let currentActivityId = localState.currentActivityId;
1789
+ if (!currentActivityId) {
1790
+ // Use startActivityId if available
1791
+ if (graph.startActivityId) {
1792
+ currentActivityId = graph.startActivityId;
1793
+ }
1794
+ else {
1795
+ // Find root activity (no incoming connections)
1796
+ for (const activity of activities) {
1797
+ if (!incomingConnections.has(activity.id)) {
1798
+ currentActivityId = activity.id;
1799
+ break;
1800
+ }
1801
+ }
1802
+ // If no root found, use first activity
1803
+ if (!currentActivityId && activities.length > 0) {
1804
+ currentActivityId = activities[0].id;
1805
+ }
1806
+ }
1807
+ }
1808
+ // Execute workflow steps
1809
+ while (currentActivityId) {
1810
+ const activity = activityMap.get(currentActivityId);
1811
+ if (!activity) {
1812
+ break;
1813
+ }
1814
+ // Skip if already completed
1815
+ if (localState.completedActivities.has(currentActivityId)) {
1816
+ // Move to next activity
1817
+ const nextId = this.getNextActivityId(currentActivityId, outgoingConnections, localState.activityResults);
1818
+ currentActivityId = nextId ?? undefined;
1819
+ continue;
1820
+ }
1821
+ // Get activity definition to retrieve executionMode and title
1822
+ console.log(`[WorkflowLocalEngine] 🔍 Getting activity definition for: ${activity.name}`);
1823
+ const activityDefinition = await this.activityDefinitionService.getActivityByName(activity.name);
1824
+ console.log(`[WorkflowLocalEngine] 📋 Activity definition:`, {
1825
+ name: activityDefinition?.name,
1826
+ type: activityDefinition?.type,
1827
+ executionMode: activityDefinition?.executionMode,
1828
+ title: activityDefinition?.title,
1829
+ found: !!activityDefinition,
1830
+ });
1831
+ const executionMode = activityDefinition?.executionMode || 'frontend';
1832
+ const activityTitle = activityDefinition?.title;
1833
+ // Handle backend activities: skip
1834
+ if (executionMode === 'backend') {
1835
+ console.log(`[WorkflowLocalEngine] ⏭️ Skipping backend activity: ${activity.name} (${activity.id})`);
1836
+ localState.completedActivities.add(currentActivityId);
1837
+ localState.activityResults.set(currentActivityId, {
1838
+ output: null,
1839
+ outcome: 'Done',
1840
+ });
1841
+ // Move to next activity
1842
+ const nextId = this.getNextActivityId(currentActivityId, outgoingConnections, localState.activityResults);
1843
+ currentActivityId = nextId ?? undefined;
1844
+ continue;
1845
+ }
1846
+ // Handle frontend/both activities: return as pendingTask (do NOT execute)
1847
+ if (executionMode === 'frontend' || executionMode === 'both') {
1848
+ // Create task for external execution
1849
+ // Note: Expression evaluation will be done by ActivityExecutor
1850
+ const task = {
1851
+ taskToken: AXPDataGenerator.uuid(),
1852
+ activityId: activity.id,
1853
+ activityType: activity.name,
1854
+ activityName: activityTitle || undefined,
1855
+ executionMode: executionMode,
1856
+ input: activity.inputs || {}, // Pass raw inputs - executor will evaluate
1857
+ };
1858
+ // Store task token for secure resume
1859
+ this.taskTokens.set(task.taskToken, {
1860
+ instanceId: localState.instanceId,
1861
+ activityId: activity.id,
1862
+ });
1863
+ // Update state to indicate suspension
1864
+ localState.currentActivityId = currentActivityId;
1865
+ localState.state.status = 'suspended';
1866
+ localState.state.currentStepId = currentActivityId;
1867
+ // Return task immediately - AXPWorkflowManager will execute it
1868
+ return task;
1869
+ }
1870
+ // Move to next activity
1871
+ const nextId = this.getNextActivityId(currentActivityId, outgoingConnections, localState.activityResults);
1872
+ currentActivityId = nextId ?? undefined;
1873
+ }
1874
+ // Workflow completed
1875
+ localState.state.status = 'completed';
1876
+ localState.state.output = localState.state.variables;
1877
+ localState.currentActivityId = undefined;
1878
+ return null;
1879
+ }
1880
+ /**
1881
+ * Get next activity ID based on connections and outcomes.
1882
+ */
1883
+ getNextActivityId(currentActivityId, outgoingConnections, activityResults) {
1884
+ const connections = outgoingConnections.get(currentActivityId) || [];
1885
+ if (connections.length === 0) {
1886
+ return null; // No outgoing connections - workflow ends
1887
+ }
1888
+ // Get current activity result
1889
+ const result = activityResults.get(currentActivityId);
1890
+ const outcome = result?.outcome || 'Done';
1891
+ // Find connection matching outcome
1892
+ // Outcome matching is typically done via port name (e.g., "Done", "Failed")
1893
+ for (const conn of connections) {
1894
+ const sourcePort = conn.source.port || 'Done';
1895
+ if (sourcePort === outcome) {
1896
+ return conn.target.activtyName;
1897
+ }
1898
+ }
1899
+ // If no matching outcome, use first connection (default path)
1900
+ if (connections.length > 0) {
1901
+ return connections[0].target.activtyName;
1902
+ }
1903
+ return null;
1904
+ }
1905
+ async frontActivtyComplete(request) {
1906
+ const localState = this.instances.get(request.instanceId);
1907
+ if (!localState) {
1908
+ throw new Error(`Workflow instance not found: ${request.instanceId}`);
1909
+ }
1910
+ // Store activity result
1911
+ const outcome = request.outcome ?? 'Done';
1912
+ localState.activityResults.set(request.activityNode, {
1913
+ output: request.output || {},
1914
+ outcome,
1915
+ });
1916
+ localState.completedActivities.add(request.activityNode);
1917
+ // Store outputs for expression evaluation
1918
+ localState.state.activityOutputs = {
1919
+ ...(localState.state.activityOutputs || {}),
1920
+ [request.activityNode]: request.output || {},
1921
+ };
1922
+ localState.state.lastActivityOutput = request.output || {};
1923
+ // Merge output into workflow variables
1924
+ localState.state.variables = {
1925
+ ...localState.state.variables,
1926
+ ...(request.output || {}),
1927
+ };
1928
+ // Continue workflow progression
1929
+ const nextTask = await this.executeWorkflowSteps(localState);
1930
+ // Update state timestamp
1931
+ localState.state.lastUpdated = new Date();
1932
+ // Determine final status
1933
+ if (!nextTask && localState.state.status === 'running') {
1934
+ localState.state.status = 'completed';
1935
+ localState.state.output = localState.state.variables;
1936
+ }
1937
+ return {
1938
+ output: request.output || {},
1939
+ outcomes: { [outcome]: true },
1940
+ nextTask: nextTask || null,
1941
+ state: localState.state,
1942
+ };
1943
+ }
1944
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowLocalEngine, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
1945
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowLocalEngine }); }
1946
+ }
1947
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowLocalEngine, decorators: [{
1948
+ type: Injectable
1949
+ }] });
1950
+
1951
+ // Workflow Runtime Services
1952
+
1953
+ //#region ---- Imports ----
1954
+ //#endregion
1955
+ /**
1956
+ * Optimized Workflow Definition Service
1957
+ *
1958
+ * Manages workflow definitions (metadata) for UI and tooling.
1959
+ * Similar to AXPActivityDefinitionService - only handles metadata, not execution.
1960
+ *
1961
+ * Performance optimizations:
1962
+ * 1. Uses childrenCount to determine if category has children (no query needed)
1963
+ * 2. Uses itemsCount to determine if category has workflows (no query needed)
1964
+ * 3. Aggressive caching prevents duplicate API calls
1965
+ * 4. Single pending request per resource prevents race conditions
1966
+ * 5. Lazy loading - only loads data when needed
1967
+ */
1968
+ class AXPWorkflowDefinitionService {
1969
+ constructor() {
1970
+ //#region ---- Providers & Caches ----
1971
+ this.categoryProviders = inject(AXP_WORKFLOW_CATEGORY_PROVIDER, { optional: true }) || [];
1972
+ this.workflowProviders = inject(AXP_WORKFLOW_PROVIDER, { optional: true }) || [];
1973
+ //#endregion
1974
+ //#region ---- Cache Storage ----
1975
+ /** Cache for categories by id - O(1) lookup */
1976
+ this.categoriesById = new Map();
1977
+ /** Cache for categories by parentId - O(1) lookup */
1978
+ this.categoriesByParentId = new Map();
1979
+ /** Cache for workflow definitions by categoryId - O(1) lookup */
1980
+ this.workflowsByCategory = new Map();
1981
+ /** Cache for individual workflow definitions by name - O(1) lookup */
1982
+ this.workflowsByName = new Map();
1983
+ /** Track which provider index owns each category (by category ID) */
1984
+ this.categoryOwnership = new Map(); // Maps categoryId → provider index
1985
+ /** Pending API requests to prevent duplicate calls */
1986
+ this.pendingCategoriesRequests = new Map();
1987
+ this.pendingWorkflowsRequests = new Map();
1988
+ this.pendingWorkflowRequests = new Map();
1989
+ }
1990
+ //#endregion
1991
+ //#region ---- Public API: Categories ----
1992
+ /**
1993
+ * Get categories by parentId with aggressive caching
1994
+ *
1995
+ * Optimization: Returns cached result immediately if available,
1996
+ * preventing unnecessary API calls during navigation
1997
+ *
1998
+ * @param parentId - Parent category ID (undefined = root categories)
1999
+ * @returns Array of categories with count metadata (childrenCount, itemsCount)
2000
+ */
2001
+ async getCategories(parentId) {
2002
+ // ✅ Fast path: Return cached result
2003
+ if (this.categoriesByParentId.has(parentId)) {
2004
+ return this.categoriesByParentId.get(parentId);
2005
+ }
2006
+ // ✅ Prevent duplicate requests: Return pending promise
2007
+ if (this.pendingCategoriesRequests.has(parentId)) {
2008
+ return this.pendingCategoriesRequests.get(parentId);
2009
+ }
2010
+ // ✅ Create single request and cache it
2011
+ const requestPromise = this.loadCategoriesFromProviders(parentId);
2012
+ this.pendingCategoriesRequests.set(parentId, requestPromise);
2013
+ return requestPromise;
2014
+ }
2015
+ /**
2016
+ * Get single category by ID with O(1) lookup
2017
+ *
2018
+ * Optimization: Uses Map for instant retrieval, falls back to
2019
+ * searching cache, then providers if not found
2020
+ */
2021
+ async getCategoryById(categoryId) {
2022
+ // ✅ Fast path: O(1) lookup in cache
2023
+ if (this.categoriesById.has(categoryId)) {
2024
+ return this.categoriesById.get(categoryId);
2025
+ }
2026
+ // ✅ Search in cached parent-child lists
2027
+ for (const categories of this.categoriesByParentId.values()) {
2028
+ const found = categories.find(cat => cat.id === categoryId);
2029
+ if (found) {
2030
+ this.categoriesById.set(categoryId, found);
2031
+ return found;
2032
+ }
2033
+ }
2034
+ // ✅ Load root categories if not loaded
2035
+ if (!this.categoriesByParentId.has(undefined)) {
2036
+ await this.getCategories();
2037
+ if (this.categoriesById.has(categoryId)) {
2038
+ return this.categoriesById.get(categoryId);
2039
+ }
2040
+ }
2041
+ // ✅ Breadth-first search through hierarchy
2042
+ return this.searchCategoryInHierarchy(categoryId);
2043
+ }
2044
+ /**
2045
+ * Get category path from root to specified category
2046
+ *
2047
+ * Optimization: Builds path using cached categories only
2048
+ */
2049
+ async getCategoriesPathById(categoryId) {
2050
+ const path = [];
2051
+ let currentCategoryId = categoryId;
2052
+ while (currentCategoryId) {
2053
+ const category = await this.getCategoryById(currentCategoryId);
2054
+ if (!category) {
2055
+ throw new Error(`Category '${currentCategoryId}' not found`);
2056
+ }
2057
+ path.unshift(category);
2058
+ currentCategoryId = category.parentId;
2059
+ }
2060
+ return path;
2061
+ }
2062
+ //#endregion
2063
+ //#region ---- Public API: Workflow Definitions ----
2064
+ /**
2065
+ * Get workflow definitions for a category with smart caching
2066
+ *
2067
+ * Optimization: Checks itemsCount before querying
2068
+ * - If itemsCount = 0, returns empty array (no API call)
2069
+ * - If itemsCount > 0, loads and caches workflow definitions
2070
+ * - Returns cached result on subsequent calls
2071
+ *
2072
+ * @param categoryId - Category ID to get workflow definitions from
2073
+ * @returns Array of workflow definitions
2074
+ */
2075
+ async getWorkflowsByCategoryId(categoryId) {
2076
+ // ✅ Fast path: Return cached result
2077
+ if (this.workflowsByCategory.has(categoryId)) {
2078
+ return this.workflowsByCategory.get(categoryId);
2079
+ }
2080
+ // ✅ Smart optimization: Check itemsCount before querying
2081
+ const category = await this.getCategoryById(categoryId);
2082
+ if (category && category.itemsCount !== undefined && category.itemsCount === 0) {
2083
+ // Category has no workflows - cache empty array and skip API call
2084
+ const emptyArray = [];
2085
+ this.workflowsByCategory.set(categoryId, emptyArray);
2086
+ return emptyArray;
2087
+ }
2088
+ // ✅ Prevent duplicate requests
2089
+ if (this.pendingWorkflowsRequests.has(categoryId)) {
2090
+ return this.pendingWorkflowsRequests.get(categoryId);
2091
+ }
2092
+ // ✅ Load from providers
2093
+ const requestPromise = this.loadWorkflowsFromProviders(categoryId);
2094
+ this.pendingWorkflowsRequests.set(categoryId, requestPromise);
2095
+ return requestPromise;
2096
+ }
2097
+ /**
2098
+ * Get single workflow definition by name with O(1) lookup
2099
+ *
2100
+ * Optimization: Uses Map for instant retrieval
2101
+ *
2102
+ * @param name - Workflow name (unique identifier)
2103
+ * @returns Workflow definition or undefined if not found
2104
+ */
2105
+ async getWorkflowByName(name) {
2106
+ // ✅ Fast path: O(1) lookup in cache
2107
+ if (this.workflowsByName.has(name)) {
2108
+ return this.workflowsByName.get(name);
2109
+ }
2110
+ // ✅ Prevent duplicate requests
2111
+ if (this.pendingWorkflowRequests.has(name)) {
2112
+ return this.pendingWorkflowRequests.get(name);
2113
+ }
2114
+ // ✅ Load from providers
2115
+ const requestPromise = this.loadWorkflowFromProviders(name);
2116
+ this.pendingWorkflowRequests.set(name, requestPromise);
2117
+ return requestPromise;
2118
+ }
2119
+ /**
2120
+ * Get category ID containing a specific workflow definition
2121
+ *
2122
+ * Optimization: Searches cache first, loads on-demand if needed
2123
+ */
2124
+ async getCategoryIdByWorkflowName(workflowName) {
2125
+ // ✅ Search in cached workflow definitions
2126
+ for (const [categoryId, definitions] of this.workflowsByCategory.entries()) {
2127
+ if (definitions.some(def => def.name === workflowName)) {
2128
+ return categoryId;
2129
+ }
2130
+ }
2131
+ // ✅ Try loading the workflow definition to find its category
2132
+ // Note: AXPWorkflowDefinition doesn't have category field
2133
+ // Category is managed separately through category providers
2134
+ // This method searches through cached categories
2135
+ const definition = await this.getWorkflowByName(workflowName);
2136
+ if (definition) {
2137
+ // Search through all categories to find which one contains this workflow
2138
+ const categories = await this.getCategories();
2139
+ for (const category of categories) {
2140
+ const workflows = await this.getWorkflowsByCategoryId(category.id);
2141
+ if (workflows.some(w => w.name === workflowName)) {
2142
+ return category.id;
2143
+ }
2144
+ }
2145
+ }
2146
+ return undefined;
2147
+ }
2148
+ /**
2149
+ * Get category path for a workflow
2150
+ */
2151
+ async getCategoriesPathByWorkflowName(workflowName) {
2152
+ const categoryId = await this.getCategoryIdByWorkflowName(workflowName);
2153
+ if (!categoryId) {
2154
+ throw new Error(`Workflow '${workflowName}' not found in any category`);
2155
+ }
2156
+ return this.getCategoriesPathById(categoryId);
2157
+ }
2158
+ //#endregion
2159
+ //#region ---- Private: Data Loading ----
2160
+ /**
2161
+ * Load categories from providers and cache results
2162
+ *
2163
+ * Optimization: Tracks provider ownership to avoid unnecessary API calls
2164
+ * - For root (parentId = undefined): Query ALL providers
2165
+ * - For children: Only query the provider that owns the parent
2166
+ */
2167
+ async loadCategoriesFromProviders(parentId) {
2168
+ try {
2169
+ const resolvedProviders = await Promise.allSettled(this.categoryProviders);
2170
+ const categories = [];
2171
+ // Determine which provider(s) to query
2172
+ const providerIndicesToQuery = parentId
2173
+ ? this.getProviderIndexForCategory(parentId)
2174
+ : null; // Root: query all providers
2175
+ for (let i = 0; i < resolvedProviders.length; i++) {
2176
+ const p = resolvedProviders[i];
2177
+ // Skip if we have a specific provider index and this isn't it
2178
+ if (providerIndicesToQuery !== null && !providerIndicesToQuery.includes(i)) {
2179
+ continue;
2180
+ }
2181
+ if (p.status === 'fulfilled' && p.value && typeof p.value.getList === 'function') {
2182
+ try {
2183
+ const cats = await p.value.getList(parentId);
2184
+ if (Array.isArray(cats) && cats.length > 0) {
2185
+ categories.push(...cats);
2186
+ // ✅ Track ownership: This provider INDEX owns these categories
2187
+ cats.forEach(cat => this.categoryOwnership.set(cat.id, i));
2188
+ }
2189
+ }
2190
+ catch {
2191
+ // Continue on error - try other providers
2192
+ }
2193
+ }
2194
+ }
2195
+ // ✅ Cache results for fast subsequent access
2196
+ this.categoriesByParentId.set(parentId, categories);
2197
+ categories.forEach(cat => this.categoriesById.set(cat.id, cat));
2198
+ return categories;
2199
+ }
2200
+ finally {
2201
+ this.pendingCategoriesRequests.delete(parentId);
2202
+ }
2203
+ }
2204
+ /**
2205
+ * Get the provider index that owns a specific category
2206
+ *
2207
+ * @returns Array with provider index, or null if ownership unknown (query all)
2208
+ */
2209
+ getProviderIndexForCategory(categoryId) {
2210
+ const ownerIndex = this.categoryOwnership.get(categoryId);
2211
+ if (ownerIndex !== undefined) {
2212
+ return [ownerIndex];
2213
+ }
2214
+ // Ownership unknown - will query all providers (fallback)
2215
+ return null;
2216
+ }
2217
+ /**
2218
+ * Load workflow definitions from providers and cache results
2219
+ *
2220
+ * Optimization: Only queries the provider that owns the category
2221
+ * Uses provider INDEX to match category provider with workflow provider
2222
+ */
2223
+ async loadWorkflowsFromProviders(categoryId) {
2224
+ try {
2225
+ const resolvedProviders = await Promise.allSettled(this.workflowProviders);
2226
+ const definitions = [];
2227
+ // ✅ Smart routing: Get provider INDEX that owns this category
2228
+ const ownerIndex = this.categoryOwnership.get(categoryId);
2229
+ const providerIndicesToQuery = ownerIndex !== undefined ? [ownerIndex] : null;
2230
+ for (let i = 0; i < resolvedProviders.length; i++) {
2231
+ const p = resolvedProviders[i];
2232
+ // Skip if we have a specific provider index and this isn't it
2233
+ if (providerIndicesToQuery !== null && !providerIndicesToQuery.includes(i)) {
2234
+ continue;
2235
+ }
2236
+ if (p.status === 'fulfilled' && p.value && typeof p.value.getList === 'function') {
2237
+ try {
2238
+ const defs = await p.value.getList(categoryId);
2239
+ if (Array.isArray(defs)) {
2240
+ definitions.push(...defs);
2241
+ }
2242
+ }
2243
+ catch {
2244
+ // Continue on error - try other providers
2245
+ }
2246
+ }
2247
+ }
2248
+ // ✅ Cache results for fast subsequent access
2249
+ this.workflowsByCategory.set(categoryId, definitions);
2250
+ definitions.forEach(def => {
2251
+ if (def.name) {
2252
+ this.workflowsByName.set(def.name, def);
2253
+ }
2254
+ });
2255
+ return definitions;
2256
+ }
2257
+ finally {
2258
+ this.pendingWorkflowsRequests.delete(categoryId);
2259
+ }
2260
+ }
2261
+ /**
2262
+ * Load single workflow definition from providers and cache result
2263
+ */
2264
+ async loadWorkflowFromProviders(name) {
2265
+ try {
2266
+ const resolvedProviders = await Promise.allSettled(this.workflowProviders);
2267
+ // Try providers first
2268
+ for (const p of resolvedProviders) {
2269
+ if (p.status === 'fulfilled' && p.value && typeof p.value.getByName === 'function') {
2270
+ try {
2271
+ const definition = await p.value.getByName(name);
2272
+ if (definition) {
2273
+ this.workflowsByName.set(name, definition);
2274
+ return definition;
2275
+ }
2276
+ }
2277
+ catch {
2278
+ // Continue on error
2279
+ }
2280
+ }
2281
+ }
2282
+ // Fallback: Search in cached workflow definitions
2283
+ for (const definitions of this.workflowsByCategory.values()) {
2284
+ const found = definitions.find(def => def.name === name);
2285
+ if (found) {
2286
+ this.workflowsByName.set(name, found);
2287
+ return found;
2288
+ }
2289
+ }
2290
+ return undefined;
2291
+ }
2292
+ finally {
2293
+ this.pendingWorkflowRequests.delete(name);
2294
+ }
2295
+ }
2296
+ /**
2297
+ * Breadth-first search through category hierarchy
2298
+ */
2299
+ async searchCategoryInHierarchy(categoryId) {
2300
+ const searchQueue = [undefined];
2301
+ const searched = new Set();
2302
+ while (searchQueue.length > 0) {
2303
+ const parentId = searchQueue.shift();
2304
+ if (searched.has(parentId))
2305
+ continue;
2306
+ searched.add(parentId);
2307
+ const categories = await this.getCategories(parentId);
2308
+ const found = categories.find(cat => cat.id === categoryId);
2309
+ if (found) {
2310
+ return found;
2311
+ }
2312
+ // ✅ Optimization: Only search children if childrenCount > 0
2313
+ for (const category of categories) {
2314
+ if (category.childrenCount > 0 && !searched.has(category.id)) {
2315
+ searchQueue.push(category.id);
2316
+ }
2317
+ }
2318
+ }
2319
+ return undefined;
2320
+ }
2321
+ //#endregion
2322
+ //#region ---- Cache Management ----
2323
+ /**
2324
+ * Check if category has children (uses cached count)
2325
+ */
2326
+ categoryHasChildren(categoryId) {
2327
+ const category = this.categoriesById.get(categoryId);
2328
+ return category ? category.childrenCount > 0 : false;
2329
+ }
2330
+ /**
2331
+ * Check if category has workflows (uses cached count)
2332
+ */
2333
+ categoryHasWorkflows(categoryId) {
2334
+ const category = this.categoriesById.get(categoryId);
2335
+ return category ? (category.itemsCount ?? 0) > 0 : false;
2336
+ }
2337
+ /**
2338
+ * Clear all caches
2339
+ */
2340
+ clearAllCache() {
2341
+ this.categoriesById.clear();
2342
+ this.categoriesByParentId.clear();
2343
+ this.workflowsByCategory.clear();
2344
+ this.workflowsByName.clear();
2345
+ this.categoryOwnership.clear();
2346
+ this.pendingCategoriesRequests.clear();
2347
+ this.pendingWorkflowsRequests.clear();
2348
+ this.pendingWorkflowRequests.clear();
2349
+ }
2350
+ /**
2351
+ * Clear categories cache only
2352
+ */
2353
+ clearCategoriesCache() {
2354
+ this.categoriesById.clear();
2355
+ this.categoriesByParentId.clear();
2356
+ this.categoryOwnership.clear();
2357
+ this.pendingCategoriesRequests.clear();
2358
+ }
2359
+ /**
2360
+ * Clear workflows cache only
2361
+ */
2362
+ clearWorkflowsCache() {
2363
+ this.workflowsByCategory.clear();
2364
+ this.workflowsByName.clear();
2365
+ this.pendingWorkflowsRequests.clear();
2366
+ this.pendingWorkflowRequests.clear();
2367
+ }
2368
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowDefinitionService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
2369
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowDefinitionService, providedIn: 'root' }); }
2370
+ }
2371
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowDefinitionService, decorators: [{
2372
+ type: Injectable,
2373
+ args: [{
2374
+ providedIn: 'root',
2375
+ }]
2376
+ }] });
2377
+
2378
+ // Workflow Instance Types (Storage/Database)
2379
+
2380
+ class AXPWorkflowModule {
2381
+ static forRoot(config) {
2382
+ return {
2383
+ ngModule: AXPWorkflowModule,
2384
+ providers: [
2385
+ {
2386
+ provide: 'AXPWorkflowModuleFactory',
2387
+ useFactory: (registry) => () => {
2388
+ registry.registerAction('start-workflow', AXPStartWorkflowAction);
2389
+ registry.registerAction('decide', AXPWorkflowDecideAction);
2390
+ //
2391
+ if (config?.functions) {
2392
+ for (const [key, type] of Object.entries(config.functions)) {
2393
+ registry.registerFunction(key, type);
2394
+ }
2395
+ }
2396
+ //
2397
+ if (config?.actions) {
2398
+ for (const [key, type] of Object.entries(config.actions)) {
2399
+ registry.registerAction(key, type);
2400
+ }
2401
+ }
2402
+ //
2403
+ if (config?.workflows) {
2404
+ for (const [key, type] of Object.entries(config.workflows)) {
2405
+ registry.registerWorkflow(key, type);
2406
+ }
2407
+ }
2408
+ },
2409
+ deps: [AXPWorkflowRegistryService],
2410
+ multi: true,
2411
+ },
2412
+ ...Object.values(config?.actions ?? { AXPStartWorkflowAction }),
2413
+ ...Object.values(config?.functions ?? {}),
2414
+ ],
2415
+ };
2416
+ }
2417
+ static forChild(config) {
2418
+ return {
2419
+ ngModule: AXPWorkflowModule,
2420
+ providers: [
2421
+ // Built-in activities are already registered in forRoot via @NgModule providers
2422
+ // No need to register again in forChild
2423
+ {
2424
+ provide: 'AXPWorkflowModuleFactory',
2425
+ useFactory: (registry) => () => {
2426
+ registry.registerAction('start-workflow', AXPStartWorkflowAction);
2427
+ registry.registerAction('decide', AXPWorkflowDecideAction);
2428
+ //
2429
+ if (config?.functions) {
2430
+ for (const [key, type] of Object.entries(config.functions)) {
2431
+ registry.registerFunction(key, type);
2432
+ }
2433
+ }
2434
+ //
2435
+ if (config?.actions) {
2436
+ for (const [key, type] of Object.entries(config.actions)) {
2437
+ registry.registerAction(key, type);
2438
+ }
2439
+ }
2440
+ //
2441
+ if (config?.workflows) {
2442
+ for (const [key, type] of Object.entries(config.workflows)) {
2443
+ registry.registerWorkflow(key, type);
2444
+ }
2445
+ }
2446
+ },
2447
+ deps: [AXPWorkflowRegistryService],
2448
+ multi: true,
2449
+ },
2450
+ ...Object.values(config?.actions ?? {}),
2451
+ ...Object.values(config?.functions ?? {}),
2452
+ ],
2453
+ };
2454
+ }
2455
+ /**
2456
+ * @ignore
2457
+ */
2458
+ constructor(instances) {
2459
+ instances?.forEach((f) => {
2460
+ f();
2461
+ });
2462
+ }
2463
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowModule, deps: [{ token: 'AXPWorkflowModuleFactory', optional: true }], target: i0.ɵɵFactoryTarget.NgModule }); }
2464
+ static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowModule }); }
2465
+ static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowModule, providers: [
2466
+ AXPWorkflowLocalEngine,
2467
+ {
2468
+ provide: AXP_WORKFLOW_ENGINE,
2469
+ useExisting: AXPWorkflowLocalEngine,
2470
+ },
2471
+ AXPWorkflowManager,
2472
+ ] }); }
2473
+ }
2474
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: AXPWorkflowModule, decorators: [{
2475
+ type: NgModule,
2476
+ args: [{
2477
+ imports: [],
2478
+ exports: [],
2479
+ declarations: [],
2480
+ providers: [
2481
+ AXPWorkflowLocalEngine,
2482
+ {
2483
+ provide: AXP_WORKFLOW_ENGINE,
2484
+ useExisting: AXPWorkflowLocalEngine,
2485
+ },
2486
+ AXPWorkflowManager,
2487
+ ],
2488
+ }]
2489
+ }], ctorParameters: () => [{ type: undefined, decorators: [{
2490
+ type: Optional
2491
+ }, {
2492
+ type: Inject,
2493
+ args: ['AXPWorkflowModuleFactory']
2494
+ }] }] });
2495
+
2496
+ /**
2497
+ * Generated bundle index. Do not edit.
2498
+ */
2499
+
2500
+ export { AXPActivityDefinitionService, AXPWorkflowAction, AXPWorkflowContext, AXPWorkflowDefinitionService, AXPWorkflowError, AXPWorkflowEventService, AXPWorkflowFunction, AXPWorkflowLocalEngine, AXPWorkflowManager, AXPWorkflowModule, AXPWorkflowRegistryService, AXPWorkflowService, AXP_ACTIVITY_CATEGORY_PROVIDER, AXP_ACTIVITY_PROVIDER, AXP_WORKFLOW_CATEGORY_PROVIDER, AXP_WORKFLOW_ENGINE, AXP_WORKFLOW_PROVIDER, ActivityExecutor, WorkflowExpressionScopeService, createWorkFlowEvent, ofType };
2501
+ //# sourceMappingURL=acorex-platform-workflow.mjs.map