snow-flow 1.1.0

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 (240) hide show
  1. package/.env.example +49 -0
  2. package/.mcp.json +106 -0
  3. package/.mcp.json.template +106 -0
  4. package/LICENSE +21 -0
  5. package/README.md +236 -0
  6. package/bin/snow-flow +4 -0
  7. package/bin/snow-flow.js +3 -0
  8. package/claude-flow +79 -0
  9. package/claude-flow.bat +18 -0
  10. package/claude-flow.config.json +20 -0
  11. package/claude-flow.ps1 +24 -0
  12. package/dist/agents/app-creator.agent.d.ts +55 -0
  13. package/dist/agents/app-creator.agent.d.ts.map +1 -0
  14. package/dist/agents/app-creator.agent.js +1292 -0
  15. package/dist/agents/app-creator.agent.js.map +1 -0
  16. package/dist/agents/base-snow-agent.d.ts +63 -0
  17. package/dist/agents/base-snow-agent.d.ts.map +1 -0
  18. package/dist/agents/base-snow-agent.js +296 -0
  19. package/dist/agents/base-snow-agent.js.map +1 -0
  20. package/dist/agents/script-generator.agent.d.ts +32 -0
  21. package/dist/agents/script-generator.agent.d.ts.map +1 -0
  22. package/dist/agents/script-generator.agent.js +772 -0
  23. package/dist/agents/script-generator.agent.js.map +1 -0
  24. package/dist/agents/ui-builder.agent.d.ts +50 -0
  25. package/dist/agents/ui-builder.agent.d.ts.map +1 -0
  26. package/dist/agents/ui-builder.agent.js +1539 -0
  27. package/dist/agents/ui-builder.agent.js.map +1 -0
  28. package/dist/agents/widget-builder.agent.d.ts +32 -0
  29. package/dist/agents/widget-builder.agent.d.ts.map +1 -0
  30. package/dist/agents/widget-builder.agent.js +439 -0
  31. package/dist/agents/widget-builder.agent.js.map +1 -0
  32. package/dist/agents/workflow-designer.agent.d.ts +31 -0
  33. package/dist/agents/workflow-designer.agent.d.ts.map +1 -0
  34. package/dist/agents/workflow-designer.agent.js +583 -0
  35. package/dist/agents/workflow-designer.agent.js.map +1 -0
  36. package/dist/api/error-handling.d.ts +214 -0
  37. package/dist/api/error-handling.d.ts.map +1 -0
  38. package/dist/api/error-handling.js +594 -0
  39. package/dist/api/error-handling.js.map +1 -0
  40. package/dist/api/flow-designer-api.d.ts +167 -0
  41. package/dist/api/flow-designer-api.d.ts.map +1 -0
  42. package/dist/api/flow-designer-api.js +555 -0
  43. package/dist/api/flow-designer-api.js.map +1 -0
  44. package/dist/api/natural-language-mapper.d.ts +126 -0
  45. package/dist/api/natural-language-mapper.d.ts.map +1 -0
  46. package/dist/api/natural-language-mapper.js +498 -0
  47. package/dist/api/natural-language-mapper.js.map +1 -0
  48. package/dist/api/performance-optimizer.d.ts +240 -0
  49. package/dist/api/performance-optimizer.d.ts.map +1 -0
  50. package/dist/api/performance-optimizer.js +701 -0
  51. package/dist/api/performance-optimizer.js.map +1 -0
  52. package/dist/api/transaction-manager.d.ts +189 -0
  53. package/dist/api/transaction-manager.d.ts.map +1 -0
  54. package/dist/api/transaction-manager.js +576 -0
  55. package/dist/api/transaction-manager.js.map +1 -0
  56. package/dist/cli/deploy-artifact.d.ts +7 -0
  57. package/dist/cli/deploy-artifact.d.ts.map +1 -0
  58. package/dist/cli/deploy-artifact.js +279 -0
  59. package/dist/cli/deploy-artifact.js.map +1 -0
  60. package/dist/cli.d.ts +6 -0
  61. package/dist/cli.d.ts.map +1 -0
  62. package/dist/cli.js +2057 -0
  63. package/dist/cli.js.map +1 -0
  64. package/dist/index.d.ts +10 -0
  65. package/dist/index.d.ts.map +1 -0
  66. package/dist/index.js +35 -0
  67. package/dist/index.js.map +1 -0
  68. package/dist/managers/scope-manager.d.ts +75 -0
  69. package/dist/managers/scope-manager.d.ts.map +1 -0
  70. package/dist/managers/scope-manager.js +448 -0
  71. package/dist/managers/scope-manager.js.map +1 -0
  72. package/dist/mcp/mcp-wrapper.d.ts +8 -0
  73. package/dist/mcp/mcp-wrapper.d.ts.map +1 -0
  74. package/dist/mcp/mcp-wrapper.js +77 -0
  75. package/dist/mcp/mcp-wrapper.js.map +1 -0
  76. package/dist/mcp/servicenow-automation-mcp.d.ts +8 -0
  77. package/dist/mcp/servicenow-automation-mcp.d.ts.map +1 -0
  78. package/dist/mcp/servicenow-automation-mcp.js +683 -0
  79. package/dist/mcp/servicenow-automation-mcp.js.map +1 -0
  80. package/dist/mcp/servicenow-deployment-mcp.d.ts +7 -0
  81. package/dist/mcp/servicenow-deployment-mcp.d.ts.map +1 -0
  82. package/dist/mcp/servicenow-deployment-mcp.js +972 -0
  83. package/dist/mcp/servicenow-deployment-mcp.js.map +1 -0
  84. package/dist/mcp/servicenow-flow-composer-mcp.d.ts +7 -0
  85. package/dist/mcp/servicenow-flow-composer-mcp.d.ts.map +1 -0
  86. package/dist/mcp/servicenow-flow-composer-mcp.js +1121 -0
  87. package/dist/mcp/servicenow-flow-composer-mcp.js.map +1 -0
  88. package/dist/mcp/servicenow-graph-memory-mcp.d.ts +7 -0
  89. package/dist/mcp/servicenow-graph-memory-mcp.d.ts.map +1 -0
  90. package/dist/mcp/servicenow-graph-memory-mcp.js +703 -0
  91. package/dist/mcp/servicenow-graph-memory-mcp.js.map +1 -0
  92. package/dist/mcp/servicenow-integration-mcp.d.ts +8 -0
  93. package/dist/mcp/servicenow-integration-mcp.d.ts.map +1 -0
  94. package/dist/mcp/servicenow-integration-mcp.js +689 -0
  95. package/dist/mcp/servicenow-integration-mcp.js.map +1 -0
  96. package/dist/mcp/servicenow-intelligent-mcp.d.ts +7 -0
  97. package/dist/mcp/servicenow-intelligent-mcp.d.ts.map +1 -0
  98. package/dist/mcp/servicenow-intelligent-mcp.js +1138 -0
  99. package/dist/mcp/servicenow-intelligent-mcp.js.map +1 -0
  100. package/dist/mcp/servicenow-mcp-server.d.ts +37 -0
  101. package/dist/mcp/servicenow-mcp-server.d.ts.map +1 -0
  102. package/dist/mcp/servicenow-mcp-server.js +648 -0
  103. package/dist/mcp/servicenow-mcp-server.js.map +1 -0
  104. package/dist/mcp/servicenow-operations-mcp.d.ts +15 -0
  105. package/dist/mcp/servicenow-operations-mcp.d.ts.map +1 -0
  106. package/dist/mcp/servicenow-operations-mcp.js +1287 -0
  107. package/dist/mcp/servicenow-operations-mcp.js.map +1 -0
  108. package/dist/mcp/servicenow-platform-development-mcp.d.ts +8 -0
  109. package/dist/mcp/servicenow-platform-development-mcp.d.ts.map +1 -0
  110. package/dist/mcp/servicenow-platform-development-mcp.js +557 -0
  111. package/dist/mcp/servicenow-platform-development-mcp.js.map +1 -0
  112. package/dist/mcp/servicenow-progressive-indexer.d.ts +79 -0
  113. package/dist/mcp/servicenow-progressive-indexer.d.ts.map +1 -0
  114. package/dist/mcp/servicenow-progressive-indexer.js +320 -0
  115. package/dist/mcp/servicenow-progressive-indexer.js.map +1 -0
  116. package/dist/mcp/servicenow-reporting-analytics-mcp.d.ts +8 -0
  117. package/dist/mcp/servicenow-reporting-analytics-mcp.d.ts.map +1 -0
  118. package/dist/mcp/servicenow-reporting-analytics-mcp.js +888 -0
  119. package/dist/mcp/servicenow-reporting-analytics-mcp.js.map +1 -0
  120. package/dist/mcp/servicenow-security-compliance-mcp.d.ts +8 -0
  121. package/dist/mcp/servicenow-security-compliance-mcp.d.ts.map +1 -0
  122. package/dist/mcp/servicenow-security-compliance-mcp.js +812 -0
  123. package/dist/mcp/servicenow-security-compliance-mcp.js.map +1 -0
  124. package/dist/mcp/servicenow-update-set-mcp.d.ts +7 -0
  125. package/dist/mcp/servicenow-update-set-mcp.d.ts.map +1 -0
  126. package/dist/mcp/servicenow-update-set-mcp.js +599 -0
  127. package/dist/mcp/servicenow-update-set-mcp.js.map +1 -0
  128. package/dist/mcp/start-all-mcp-servers.d.ts +6 -0
  129. package/dist/mcp/start-all-mcp-servers.d.ts.map +1 -0
  130. package/dist/mcp/start-all-mcp-servers.js +65 -0
  131. package/dist/mcp/start-all-mcp-servers.js.map +1 -0
  132. package/dist/mcp/start-servicenow-mcp.d.ts +7 -0
  133. package/dist/mcp/start-servicenow-mcp.d.ts.map +1 -0
  134. package/dist/mcp/start-servicenow-mcp.js +64 -0
  135. package/dist/mcp/start-servicenow-mcp.js.map +1 -0
  136. package/dist/mcp/test-simple-mcp.d.ts +6 -0
  137. package/dist/mcp/test-simple-mcp.d.ts.map +1 -0
  138. package/dist/mcp/test-simple-mcp.js +64 -0
  139. package/dist/mcp/test-simple-mcp.js.map +1 -0
  140. package/dist/orchestrator/flow-composer.d.ts +269 -0
  141. package/dist/orchestrator/flow-composer.d.ts.map +1 -0
  142. package/dist/orchestrator/flow-composer.js +2221 -0
  143. package/dist/orchestrator/flow-composer.js.map +1 -0
  144. package/dist/orchestrator/flow-pattern-templates.d.ts +260 -0
  145. package/dist/orchestrator/flow-pattern-templates.d.ts.map +1 -0
  146. package/dist/orchestrator/flow-pattern-templates.js +906 -0
  147. package/dist/orchestrator/flow-pattern-templates.js.map +1 -0
  148. package/dist/orchestrator/flow-subflow-decision-engine.d.ts +202 -0
  149. package/dist/orchestrator/flow-subflow-decision-engine.d.ts.map +1 -0
  150. package/dist/orchestrator/flow-subflow-decision-engine.js +769 -0
  151. package/dist/orchestrator/flow-subflow-decision-engine.js.map +1 -0
  152. package/dist/orchestrator/flow-validation-engine.d.ts +145 -0
  153. package/dist/orchestrator/flow-validation-engine.d.ts.map +1 -0
  154. package/dist/orchestrator/flow-validation-engine.js +663 -0
  155. package/dist/orchestrator/flow-validation-engine.js.map +1 -0
  156. package/dist/orchestrator/snow-orchestrator.d.ts +49 -0
  157. package/dist/orchestrator/snow-orchestrator.d.ts.map +1 -0
  158. package/dist/orchestrator/snow-orchestrator.js +235 -0
  159. package/dist/orchestrator/snow-orchestrator.js.map +1 -0
  160. package/dist/orchestrator/subflow-creation-handler.d.ts +230 -0
  161. package/dist/orchestrator/subflow-creation-handler.d.ts.map +1 -0
  162. package/dist/orchestrator/subflow-creation-handler.js +791 -0
  163. package/dist/orchestrator/subflow-creation-handler.js.map +1 -0
  164. package/dist/strategies/global-scope-strategy.d.ts +123 -0
  165. package/dist/strategies/global-scope-strategy.d.ts.map +1 -0
  166. package/dist/strategies/global-scope-strategy.js +585 -0
  167. package/dist/strategies/global-scope-strategy.js.map +1 -0
  168. package/dist/types/index.d.ts +138 -0
  169. package/dist/types/index.d.ts.map +1 -0
  170. package/dist/types/index.js +4 -0
  171. package/dist/types/index.js.map +1 -0
  172. package/dist/types/servicenow.types.d.ts +260 -0
  173. package/dist/types/servicenow.types.d.ts.map +1 -0
  174. package/dist/types/servicenow.types.js +3 -0
  175. package/dist/types/servicenow.types.js.map +1 -0
  176. package/dist/types/snow-flow.types.d.ts +114 -0
  177. package/dist/types/snow-flow.types.d.ts.map +1 -0
  178. package/dist/types/snow-flow.types.js +3 -0
  179. package/dist/types/snow-flow.types.js.map +1 -0
  180. package/dist/utils/action-type-cache.d.ts +66 -0
  181. package/dist/utils/action-type-cache.d.ts.map +1 -0
  182. package/dist/utils/action-type-cache.js +237 -0
  183. package/dist/utils/action-type-cache.js.map +1 -0
  184. package/dist/utils/agent-detector.d.ts +35 -0
  185. package/dist/utils/agent-detector.d.ts.map +1 -0
  186. package/dist/utils/agent-detector.js +353 -0
  187. package/dist/utils/agent-detector.js.map +1 -0
  188. package/dist/utils/flow-api-discovery.d.ts +151 -0
  189. package/dist/utils/flow-api-discovery.d.ts.map +1 -0
  190. package/dist/utils/flow-api-discovery.js +440 -0
  191. package/dist/utils/flow-api-discovery.js.map +1 -0
  192. package/dist/utils/flow-xml-generator.d.ts +41 -0
  193. package/dist/utils/flow-xml-generator.d.ts.map +1 -0
  194. package/dist/utils/flow-xml-generator.js +258 -0
  195. package/dist/utils/flow-xml-generator.js.map +1 -0
  196. package/dist/utils/logger.d.ts +13 -0
  197. package/dist/utils/logger.d.ts.map +1 -0
  198. package/dist/utils/logger.js +51 -0
  199. package/dist/utils/logger.js.map +1 -0
  200. package/dist/utils/mcp-auth-middleware.d.ts +58 -0
  201. package/dist/utils/mcp-auth-middleware.d.ts.map +1 -0
  202. package/dist/utils/mcp-auth-middleware.js +227 -0
  203. package/dist/utils/mcp-auth-middleware.js.map +1 -0
  204. package/dist/utils/mcp-config-manager.d.ts +126 -0
  205. package/dist/utils/mcp-config-manager.d.ts.map +1 -0
  206. package/dist/utils/mcp-config-manager.js +209 -0
  207. package/dist/utils/mcp-config-manager.js.map +1 -0
  208. package/dist/utils/mcp-server-manager.d.ts +91 -0
  209. package/dist/utils/mcp-server-manager.d.ts.map +1 -0
  210. package/dist/utils/mcp-server-manager.js +338 -0
  211. package/dist/utils/mcp-server-manager.js.map +1 -0
  212. package/dist/utils/scope-utils.d.ts +72 -0
  213. package/dist/utils/scope-utils.d.ts.map +1 -0
  214. package/dist/utils/scope-utils.js +386 -0
  215. package/dist/utils/scope-utils.js.map +1 -0
  216. package/dist/utils/servicenow-client.d.ts +233 -0
  217. package/dist/utils/servicenow-client.d.ts.map +1 -0
  218. package/dist/utils/servicenow-client.js +1465 -0
  219. package/dist/utils/servicenow-client.js.map +1 -0
  220. package/dist/utils/snow-oauth-old.d.ts +65 -0
  221. package/dist/utils/snow-oauth-old.d.ts.map +1 -0
  222. package/dist/utils/snow-oauth-old.js +345 -0
  223. package/dist/utils/snow-oauth-old.js.map +1 -0
  224. package/dist/utils/snow-oauth.d.ts +85 -0
  225. package/dist/utils/snow-oauth.d.ts.map +1 -0
  226. package/dist/utils/snow-oauth.js +512 -0
  227. package/dist/utils/snow-oauth.js.map +1 -0
  228. package/dist/utils/template-engine.d.ts +76 -0
  229. package/dist/utils/template-engine.d.ts.map +1 -0
  230. package/dist/utils/template-engine.js +391 -0
  231. package/dist/utils/template-engine.js.map +1 -0
  232. package/dist/version.d.ts +35 -0
  233. package/dist/version.d.ts.map +1 -0
  234. package/dist/version.js +72 -0
  235. package/dist/version.js.map +1 -0
  236. package/mcp-wrapper.sh +12 -0
  237. package/package.json +77 -0
  238. package/reports/swarm-auto-centralized-1752649029776.json +13 -0
  239. package/scripts/postinstall.js +30 -0
  240. package/scripts/setup-mcp.js +89 -0
@@ -0,0 +1,769 @@
1
+ "use strict";
2
+ /**
3
+ * ServiceNow Flow vs Subflow Decision Engine
4
+ *
5
+ * This module implements intelligent decision logic to determine when to create
6
+ * flows versus subflows in ServiceNow. It analyzes natural language requirements
7
+ * and provides recommendations based on complexity, reusability, and best practices.
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.FlowSubflowDecisionEngine = exports.FlowContext = exports.ReusabilityLevel = exports.ComplexityLevel = exports.FlowType = void 0;
11
+ const logger_js_1 = require("../utils/logger.js");
12
+ const servicenow_client_js_1 = require("../utils/servicenow-client.js");
13
+ /**
14
+ * Enumeration of flow types
15
+ */
16
+ var FlowType;
17
+ (function (FlowType) {
18
+ FlowType["MAIN_FLOW"] = "main_flow";
19
+ FlowType["SUBFLOW"] = "subflow";
20
+ })(FlowType || (exports.FlowType = FlowType = {}));
21
+ /**
22
+ * Complexity levels for flow analysis
23
+ */
24
+ var ComplexityLevel;
25
+ (function (ComplexityLevel) {
26
+ ComplexityLevel["LOW"] = "low";
27
+ ComplexityLevel["MEDIUM"] = "medium";
28
+ ComplexityLevel["HIGH"] = "high";
29
+ ComplexityLevel["VERY_HIGH"] = "very_high";
30
+ })(ComplexityLevel || (exports.ComplexityLevel = ComplexityLevel = {}));
31
+ /**
32
+ * Reusability assessment
33
+ */
34
+ var ReusabilityLevel;
35
+ (function (ReusabilityLevel) {
36
+ ReusabilityLevel["NONE"] = "none";
37
+ ReusabilityLevel["LOW"] = "low";
38
+ ReusabilityLevel["MEDIUM"] = "medium";
39
+ ReusabilityLevel["HIGH"] = "high";
40
+ })(ReusabilityLevel || (exports.ReusabilityLevel = ReusabilityLevel = {}));
41
+ /**
42
+ * Flow context types
43
+ */
44
+ var FlowContext;
45
+ (function (FlowContext) {
46
+ FlowContext["APPROVAL_WORKFLOW"] = "approval_workflow";
47
+ FlowContext["FULFILLMENT_WORKFLOW"] = "fulfillment_workflow";
48
+ FlowContext["NOTIFICATION_WORKFLOW"] = "notification_workflow";
49
+ FlowContext["INTEGRATION_WORKFLOW"] = "integration_workflow";
50
+ FlowContext["UTILITY_WORKFLOW"] = "utility_workflow";
51
+ FlowContext["BUSINESS_LOGIC"] = "business_logic";
52
+ })(FlowContext || (exports.FlowContext = FlowContext = {}));
53
+ /**
54
+ * Flow vs Subflow Decision Engine
55
+ */
56
+ class FlowSubflowDecisionEngine {
57
+ constructor() {
58
+ this.patterns = [];
59
+ this.logger = new logger_js_1.Logger('FlowSubflowDecisionEngine');
60
+ this.client = new servicenow_client_js_1.ServiceNowClient();
61
+ this.initializePatterns();
62
+ }
63
+ /**
64
+ * Main decision method - analyzes requirements and recommends flow type
65
+ */
66
+ async analyzeAndRecommend(instruction) {
67
+ this.logger.info('Analyzing instruction for flow vs subflow decision', { instruction });
68
+ // Parse the instruction to extract requirements
69
+ const parsedRequirements = this.parseRequirements(instruction);
70
+ // Analyze complexity
71
+ const complexity = this.analyzeComplexity(parsedRequirements);
72
+ // Analyze reusability
73
+ const reusability = this.analyzeReusability(parsedRequirements);
74
+ // Determine context
75
+ const context = this.determineContext(parsedRequirements);
76
+ // Build decision criteria
77
+ const criteria = this.buildDecisionCriteria(parsedRequirements, complexity, reusability, context);
78
+ // Make the decision
79
+ const decision = this.makeDecision(criteria);
80
+ // Identify subflow candidates
81
+ const subflowCandidates = this.identifySubflowCandidates(parsedRequirements);
82
+ // Match patterns
83
+ const patterns = this.matchPatterns(parsedRequirements, context);
84
+ // Generate alternatives
85
+ const alternatives = this.generateAlternatives(criteria);
86
+ const result = {
87
+ recommendedType: decision.type,
88
+ confidence: decision.confidence,
89
+ rationale: [decision.rationale],
90
+ criteria,
91
+ alternatives,
92
+ patterns,
93
+ subflowCandidates
94
+ };
95
+ this.logger.info('Flow analysis complete', {
96
+ recommendedType: result.recommendedType,
97
+ confidence: result.confidence,
98
+ subflowCandidates: result.subflowCandidates.length
99
+ });
100
+ return result;
101
+ }
102
+ /**
103
+ * Parse natural language requirements into structured data
104
+ */
105
+ parseRequirements(instruction) {
106
+ const lowerInstruction = instruction.toLowerCase();
107
+ return {
108
+ originalInstruction: instruction,
109
+ // Action analysis
110
+ actions: this.extractActions(lowerInstruction),
111
+ // Logical flow analysis
112
+ hasConditionalLogic: this.hasConditionalLogic(lowerInstruction),
113
+ hasLoops: this.hasLoops(lowerInstruction),
114
+ hasParallelActions: this.hasParallelActions(lowerInstruction),
115
+ // Integration analysis
116
+ hasExternalIntegrations: this.hasExternalIntegrations(lowerInstruction),
117
+ hasDataTransformation: this.hasDataTransformation(lowerInstruction),
118
+ // Business logic analysis
119
+ hasBusinessRules: this.hasBusinessRules(lowerInstruction),
120
+ hasApprovalLogic: this.hasApprovalLogic(lowerInstruction),
121
+ // Reusability indicators
122
+ hasReusableComponents: this.hasReusableComponents(lowerInstruction),
123
+ isUtilityFunction: this.isUtilityFunction(lowerInstruction),
124
+ // Error handling
125
+ hasErrorHandling: this.hasErrorHandling(lowerInstruction),
126
+ // Input/Output analysis
127
+ inputParameters: this.extractInputParameters(lowerInstruction),
128
+ outputParameters: this.extractOutputParameters(lowerInstruction),
129
+ // Context indicators
130
+ contextIndicators: this.extractContextIndicators(lowerInstruction)
131
+ };
132
+ }
133
+ /**
134
+ * Analyze complexity based on parsed requirements
135
+ */
136
+ analyzeComplexity(requirements) {
137
+ let complexityScore = 0;
138
+ // Base complexity from action count
139
+ complexityScore += Math.min(requirements.actions.length * 0.5, 5);
140
+ // Logical complexity
141
+ if (requirements.hasConditionalLogic)
142
+ complexityScore += 2;
143
+ if (requirements.hasLoops)
144
+ complexityScore += 3;
145
+ if (requirements.hasParallelActions)
146
+ complexityScore += 2;
147
+ // Integration complexity
148
+ if (requirements.hasExternalIntegrations)
149
+ complexityScore += 3;
150
+ if (requirements.hasDataTransformation)
151
+ complexityScore += 2;
152
+ // Business logic complexity
153
+ if (requirements.hasBusinessRules)
154
+ complexityScore += 2;
155
+ if (requirements.hasApprovalLogic)
156
+ complexityScore += 3;
157
+ // Error handling complexity
158
+ if (requirements.hasErrorHandling)
159
+ complexityScore += 1;
160
+ // Parameter complexity
161
+ complexityScore += Math.min(requirements.inputParameters.length * 0.3, 3);
162
+ complexityScore += Math.min(requirements.outputParameters.length * 0.3, 3);
163
+ // Determine complexity level
164
+ if (complexityScore >= 15)
165
+ return ComplexityLevel.VERY_HIGH;
166
+ if (complexityScore >= 10)
167
+ return ComplexityLevel.HIGH;
168
+ if (complexityScore >= 5)
169
+ return ComplexityLevel.MEDIUM;
170
+ return ComplexityLevel.LOW;
171
+ }
172
+ /**
173
+ * Analyze reusability potential
174
+ */
175
+ analyzeReusability(requirements) {
176
+ let reusabilityScore = 0;
177
+ // Utility function indicators
178
+ if (requirements.isUtilityFunction)
179
+ reusabilityScore += 5;
180
+ // Reusable component indicators
181
+ if (requirements.hasReusableComponents)
182
+ reusabilityScore += 3;
183
+ // Generic patterns
184
+ if (this.isGenericPattern(requirements))
185
+ reusabilityScore += 4;
186
+ // Common business logic
187
+ if (requirements.hasBusinessRules && this.isCommonBusinessLogic(requirements))
188
+ reusabilityScore += 3;
189
+ // Data transformation utilities
190
+ if (requirements.hasDataTransformation)
191
+ reusabilityScore += 2;
192
+ // Multiple context applicability
193
+ if (requirements.contextIndicators.length > 1)
194
+ reusabilityScore += 2;
195
+ // Determine reusability level
196
+ if (reusabilityScore >= 12)
197
+ return ReusabilityLevel.HIGH;
198
+ if (reusabilityScore >= 8)
199
+ return ReusabilityLevel.MEDIUM;
200
+ if (reusabilityScore >= 4)
201
+ return ReusabilityLevel.LOW;
202
+ return ReusabilityLevel.NONE;
203
+ }
204
+ /**
205
+ * Determine the flow context
206
+ */
207
+ determineContext(requirements) {
208
+ const contextIndicators = requirements.contextIndicators;
209
+ if (contextIndicators.includes('approval') || requirements.hasApprovalLogic) {
210
+ return FlowContext.APPROVAL_WORKFLOW;
211
+ }
212
+ if (contextIndicators.includes('fulfillment') || contextIndicators.includes('order')) {
213
+ return FlowContext.FULFILLMENT_WORKFLOW;
214
+ }
215
+ if (contextIndicators.includes('notification') || contextIndicators.includes('email')) {
216
+ return FlowContext.NOTIFICATION_WORKFLOW;
217
+ }
218
+ if (requirements.hasExternalIntegrations) {
219
+ return FlowContext.INTEGRATION_WORKFLOW;
220
+ }
221
+ if (requirements.isUtilityFunction || requirements.hasReusableComponents) {
222
+ return FlowContext.UTILITY_WORKFLOW;
223
+ }
224
+ if (requirements.hasBusinessRules) {
225
+ return FlowContext.BUSINESS_LOGIC;
226
+ }
227
+ return FlowContext.UTILITY_WORKFLOW;
228
+ }
229
+ /**
230
+ * Build comprehensive decision criteria
231
+ */
232
+ buildDecisionCriteria(requirements, complexity, reusability, context) {
233
+ return {
234
+ complexity,
235
+ reusability,
236
+ context,
237
+ actionCount: requirements.actions.length,
238
+ inputParameters: requirements.inputParameters.length,
239
+ outputParameters: requirements.outputParameters.length,
240
+ businessLogicComplexity: this.calculateBusinessLogicComplexity(requirements),
241
+ integrationPoints: this.calculateIntegrationPoints(requirements),
242
+ errorHandlingComplexity: this.calculateErrorHandlingComplexity(requirements),
243
+ testabilityScore: this.calculateTestabilityScore(requirements),
244
+ maintainabilityScore: this.calculateMaintainabilityScore(requirements)
245
+ };
246
+ }
247
+ /**
248
+ * Make the main decision based on criteria
249
+ */
250
+ makeDecision(criteria) {
251
+ const rationale = [];
252
+ let score = 0;
253
+ // Complexity scoring (favors subflows for high complexity reusable components)
254
+ if (criteria.complexity === ComplexityLevel.HIGH || criteria.complexity === ComplexityLevel.VERY_HIGH) {
255
+ if (criteria.reusability === ReusabilityLevel.HIGH || criteria.reusability === ReusabilityLevel.MEDIUM) {
256
+ score += 30;
257
+ rationale.push('High complexity with high reusability favors subflow creation');
258
+ }
259
+ else {
260
+ score -= 10;
261
+ rationale.push('High complexity with low reusability favors main flow');
262
+ }
263
+ }
264
+ // Reusability scoring
265
+ if (criteria.reusability === ReusabilityLevel.HIGH) {
266
+ score += 25;
267
+ rationale.push('High reusability strongly favors subflow');
268
+ }
269
+ else if (criteria.reusability === ReusabilityLevel.MEDIUM) {
270
+ score += 15;
271
+ rationale.push('Medium reusability moderately favors subflow');
272
+ }
273
+ // Context scoring
274
+ if (criteria.context === FlowContext.UTILITY_WORKFLOW) {
275
+ score += 20;
276
+ rationale.push('Utility workflow context favors subflow');
277
+ }
278
+ else if (criteria.context === FlowContext.BUSINESS_LOGIC) {
279
+ score += 15;
280
+ rationale.push('Business logic context favors subflow for reusability');
281
+ }
282
+ // Action count scoring
283
+ if (criteria.actionCount >= 10) {
284
+ score += 10;
285
+ rationale.push('High action count favors subflow for maintainability');
286
+ }
287
+ else if (criteria.actionCount <= 3) {
288
+ score -= 5;
289
+ rationale.push('Low action count favors main flow');
290
+ }
291
+ // Input/Output parameter scoring
292
+ if (criteria.inputParameters >= 5 || criteria.outputParameters >= 3) {
293
+ score += 10;
294
+ rationale.push('High parameter count favors subflow for clarity');
295
+ }
296
+ // Business logic complexity
297
+ if (criteria.businessLogicComplexity >= 8) {
298
+ score += 15;
299
+ rationale.push('Complex business logic favors subflow for separation of concerns');
300
+ }
301
+ // Integration points
302
+ if (criteria.integrationPoints >= 3) {
303
+ score += 10;
304
+ rationale.push('Multiple integration points favor subflow for modularity');
305
+ }
306
+ // Testability score
307
+ if (criteria.testabilityScore >= 8) {
308
+ score += 10;
309
+ rationale.push('High testability requirements favor subflow');
310
+ }
311
+ // Maintainability score
312
+ if (criteria.maintainabilityScore >= 8) {
313
+ score += 10;
314
+ rationale.push('High maintainability requirements favor subflow');
315
+ }
316
+ // Determine recommendation based on score
317
+ const confidence = Math.min(Math.abs(score) / 100, 0.95);
318
+ if (score >= 30) {
319
+ return {
320
+ type: FlowType.SUBFLOW,
321
+ confidence: confidence,
322
+ rationale: rationale.join('; '),
323
+ pros: [
324
+ 'Improved reusability across multiple flows',
325
+ 'Better maintainability and testability',
326
+ 'Clear separation of concerns',
327
+ 'Reduced code duplication'
328
+ ],
329
+ cons: [
330
+ 'Additional complexity in flow design',
331
+ 'Potential performance overhead',
332
+ 'More artifacts to manage'
333
+ ]
334
+ };
335
+ }
336
+ else {
337
+ return {
338
+ type: FlowType.MAIN_FLOW,
339
+ confidence: confidence,
340
+ rationale: rationale.join('; ') || 'Simple workflow without complex reusability requirements',
341
+ pros: [
342
+ 'Simpler flow design',
343
+ 'Fewer artifacts to manage',
344
+ 'Direct execution path',
345
+ 'Easier debugging'
346
+ ],
347
+ cons: [
348
+ 'Potential code duplication',
349
+ 'Limited reusability',
350
+ 'May become complex over time'
351
+ ]
352
+ };
353
+ }
354
+ }
355
+ /**
356
+ * Identify potential subflow candidates within the requirements
357
+ */
358
+ identifySubflowCandidates(requirements) {
359
+ const candidates = [];
360
+ // Approval logic candidate
361
+ if (requirements.hasApprovalLogic) {
362
+ candidates.push({
363
+ name: 'Approval Process',
364
+ description: 'Reusable approval workflow with configurable approvers',
365
+ reason: 'Approval logic is commonly reused across different workflows',
366
+ complexity: ComplexityLevel.MEDIUM,
367
+ reusability: ReusabilityLevel.HIGH,
368
+ inputs: [
369
+ {
370
+ name: 'record',
371
+ type: 'reference',
372
+ required: true,
373
+ description: 'Record requiring approval'
374
+ },
375
+ {
376
+ name: 'approvers',
377
+ type: 'string',
378
+ required: true,
379
+ description: 'Comma-separated list of approvers'
380
+ },
381
+ {
382
+ name: 'timeout_hours',
383
+ type: 'integer',
384
+ required: false,
385
+ description: 'Approval timeout in hours',
386
+ defaultValue: 48
387
+ }
388
+ ],
389
+ outputs: [
390
+ {
391
+ name: 'approval_state',
392
+ type: 'string',
393
+ description: 'Final approval state (approved/rejected/timeout)'
394
+ },
395
+ {
396
+ name: 'approved_by',
397
+ type: 'string',
398
+ description: 'User who approved the request'
399
+ },
400
+ {
401
+ name: 'approval_comments',
402
+ type: 'string',
403
+ description: 'Comments from the approver'
404
+ }
405
+ ],
406
+ actions: ['request_approval', 'check_timeout', 'process_response']
407
+ });
408
+ }
409
+ // Notification candidate
410
+ if (requirements.contextIndicators.includes('notification') || requirements.contextIndicators.includes('email')) {
411
+ candidates.push({
412
+ name: 'Notification Handler',
413
+ description: 'Reusable notification system with multiple delivery methods',
414
+ reason: 'Notification logic is commonly reused across different workflows',
415
+ complexity: ComplexityLevel.LOW,
416
+ reusability: ReusabilityLevel.HIGH,
417
+ inputs: [
418
+ {
419
+ name: 'recipients',
420
+ type: 'string',
421
+ required: true,
422
+ description: 'Recipients for the notification'
423
+ },
424
+ {
425
+ name: 'message_template',
426
+ type: 'string',
427
+ required: true,
428
+ description: 'Message template to use'
429
+ },
430
+ {
431
+ name: 'notification_type',
432
+ type: 'string',
433
+ required: false,
434
+ description: 'Type of notification (email, sms, push)',
435
+ defaultValue: 'email'
436
+ }
437
+ ],
438
+ outputs: [
439
+ {
440
+ name: 'notification_sent',
441
+ type: 'boolean',
442
+ description: 'Whether notification was sent successfully'
443
+ },
444
+ {
445
+ name: 'delivery_status',
446
+ type: 'string',
447
+ description: 'Delivery status details'
448
+ }
449
+ ],
450
+ actions: ['format_message', 'send_notification', 'track_delivery']
451
+ });
452
+ }
453
+ // Data transformation candidate
454
+ if (requirements.hasDataTransformation) {
455
+ candidates.push({
456
+ name: 'Data Transformer',
457
+ description: 'Reusable data transformation utility',
458
+ reason: 'Data transformation logic can be reused across multiple contexts',
459
+ complexity: ComplexityLevel.MEDIUM,
460
+ reusability: ReusabilityLevel.MEDIUM,
461
+ inputs: [
462
+ {
463
+ name: 'source_data',
464
+ type: 'object',
465
+ required: true,
466
+ description: 'Source data to transform'
467
+ },
468
+ {
469
+ name: 'transformation_rules',
470
+ type: 'object',
471
+ required: true,
472
+ description: 'Rules for data transformation'
473
+ }
474
+ ],
475
+ outputs: [
476
+ {
477
+ name: 'transformed_data',
478
+ type: 'object',
479
+ description: 'Transformed data result'
480
+ },
481
+ {
482
+ name: 'transformation_success',
483
+ type: 'boolean',
484
+ description: 'Whether transformation was successful'
485
+ }
486
+ ],
487
+ actions: ['validate_input', 'apply_transformation', 'validate_output']
488
+ });
489
+ }
490
+ // Business rule candidate
491
+ if (requirements.hasBusinessRules) {
492
+ candidates.push({
493
+ name: 'Business Rule Processor',
494
+ description: 'Reusable business rule evaluation engine',
495
+ reason: 'Business rules are often reused across different business processes',
496
+ complexity: ComplexityLevel.HIGH,
497
+ reusability: ReusabilityLevel.HIGH,
498
+ inputs: [
499
+ {
500
+ name: 'business_object',
501
+ type: 'object',
502
+ required: true,
503
+ description: 'Business object to evaluate'
504
+ },
505
+ {
506
+ name: 'rule_set',
507
+ type: 'string',
508
+ required: true,
509
+ description: 'Rule set identifier'
510
+ }
511
+ ],
512
+ outputs: [
513
+ {
514
+ name: 'rule_result',
515
+ type: 'object',
516
+ description: 'Result of rule evaluation'
517
+ },
518
+ {
519
+ name: 'violated_rules',
520
+ type: 'array',
521
+ description: 'List of violated rules'
522
+ }
523
+ ],
524
+ actions: ['load_rules', 'evaluate_rules', 'format_results']
525
+ });
526
+ }
527
+ return candidates;
528
+ }
529
+ /**
530
+ * Match known patterns to the requirements
531
+ */
532
+ matchPatterns(requirements, context) {
533
+ return this.patterns.filter(pattern => {
534
+ // Match by context
535
+ if (pattern.context === context)
536
+ return true;
537
+ // Match by actions
538
+ const actionMatch = pattern.commonActions.some(action => requirements.actions.some((reqAction) => reqAction.includes(action)));
539
+ return actionMatch;
540
+ });
541
+ }
542
+ /**
543
+ * Generate alternative recommendations
544
+ */
545
+ generateAlternatives(criteria) {
546
+ const alternatives = [];
547
+ // Always provide the opposite recommendation as an alternative
548
+ if (criteria.reusability === ReusabilityLevel.HIGH) {
549
+ alternatives.push({
550
+ type: FlowType.MAIN_FLOW,
551
+ confidence: 0.3,
552
+ rationale: 'Direct implementation without subflow abstraction',
553
+ pros: ['Simpler initial implementation', 'Fewer dependencies'],
554
+ cons: ['Limited reusability', 'Potential duplication']
555
+ });
556
+ }
557
+ else {
558
+ alternatives.push({
559
+ type: FlowType.SUBFLOW,
560
+ confidence: 0.4,
561
+ rationale: 'Create subflow for future reusability',
562
+ pros: ['Future-proof design', 'Easier to test'],
563
+ cons: ['Over-engineering for current needs', 'Additional complexity']
564
+ });
565
+ }
566
+ return alternatives;
567
+ }
568
+ /**
569
+ * Initialize common flow patterns
570
+ */
571
+ initializePatterns() {
572
+ this.patterns = [
573
+ {
574
+ name: 'Approval Workflow',
575
+ description: 'Standard approval process with configurable approvers',
576
+ context: FlowContext.APPROVAL_WORKFLOW,
577
+ recommendedType: FlowType.SUBFLOW,
578
+ commonActions: ['approve', 'reject', 'timeout'],
579
+ inputPatterns: ['approvers', 'record', 'timeout'],
580
+ outputPatterns: ['approval_state', 'approved_by'],
581
+ complexity: ComplexityLevel.MEDIUM
582
+ },
583
+ {
584
+ name: 'Notification Dispatcher',
585
+ description: 'Multi-channel notification system',
586
+ context: FlowContext.NOTIFICATION_WORKFLOW,
587
+ recommendedType: FlowType.SUBFLOW,
588
+ commonActions: ['send_email', 'send_sms', 'track_delivery'],
589
+ inputPatterns: ['recipients', 'message', 'channel'],
590
+ outputPatterns: ['delivery_status', 'sent_count'],
591
+ complexity: ComplexityLevel.LOW
592
+ },
593
+ {
594
+ name: 'Order Fulfillment',
595
+ description: 'Complete order processing workflow',
596
+ context: FlowContext.FULFILLMENT_WORKFLOW,
597
+ recommendedType: FlowType.MAIN_FLOW,
598
+ commonActions: ['validate_order', 'process_payment', 'ship_order'],
599
+ inputPatterns: ['order', 'customer', 'payment_method'],
600
+ outputPatterns: ['order_status', 'tracking_number'],
601
+ complexity: ComplexityLevel.HIGH
602
+ },
603
+ {
604
+ name: 'Data Validation',
605
+ description: 'Reusable data validation utility',
606
+ context: FlowContext.UTILITY_WORKFLOW,
607
+ recommendedType: FlowType.SUBFLOW,
608
+ commonActions: ['validate_format', 'check_constraints', 'sanitize'],
609
+ inputPatterns: ['data', 'validation_rules'],
610
+ outputPatterns: ['is_valid', 'errors'],
611
+ complexity: ComplexityLevel.LOW
612
+ },
613
+ {
614
+ name: 'Integration Connector',
615
+ description: 'External system integration pattern',
616
+ context: FlowContext.INTEGRATION_WORKFLOW,
617
+ recommendedType: FlowType.SUBFLOW,
618
+ commonActions: ['authenticate', 'transform_data', 'call_api'],
619
+ inputPatterns: ['endpoint', 'credentials', 'payload'],
620
+ outputPatterns: ['response', 'status'],
621
+ complexity: ComplexityLevel.MEDIUM
622
+ }
623
+ ];
624
+ }
625
+ // Helper methods for requirement analysis
626
+ extractActions(instruction) {
627
+ const actions = [];
628
+ const actionKeywords = [
629
+ 'approve', 'reject', 'notify', 'send', 'create', 'update', 'delete',
630
+ 'validate', 'process', 'transform', 'integrate', 'schedule', 'assign',
631
+ 'escalate', 'complete', 'cancel', 'retry', 'log', 'track', 'monitor'
632
+ ];
633
+ actionKeywords.forEach(keyword => {
634
+ if (instruction.includes(keyword)) {
635
+ actions.push(keyword);
636
+ }
637
+ });
638
+ return actions;
639
+ }
640
+ hasConditionalLogic(instruction) {
641
+ return /\b(if|when|unless|condition|check|validate|approve|reject)\b/.test(instruction);
642
+ }
643
+ hasLoops(instruction) {
644
+ return /\b(loop|repeat|iterate|foreach|while|until|retry)\b/.test(instruction);
645
+ }
646
+ hasParallelActions(instruction) {
647
+ return /\b(parallel|concurrent|simultaneously|at the same time)\b/.test(instruction);
648
+ }
649
+ hasExternalIntegrations(instruction) {
650
+ return /\b(api|integration|external|third-party|webhook|service)\b/.test(instruction);
651
+ }
652
+ hasDataTransformation(instruction) {
653
+ return /\b(transform|convert|map|format|parse|serialize)\b/.test(instruction);
654
+ }
655
+ hasBusinessRules(instruction) {
656
+ return /\b(business rule|policy|constraint|validation|compliance)\b/.test(instruction);
657
+ }
658
+ hasApprovalLogic(instruction) {
659
+ return /\b(approval|approve|reject|escalate|manager|supervisor)\b/.test(instruction);
660
+ }
661
+ hasReusableComponents(instruction) {
662
+ return /\b(reuse|common|shared|utility|helper|generic)\b/.test(instruction);
663
+ }
664
+ isUtilityFunction(instruction) {
665
+ return /\b(utility|helper|common|shared|library|function)\b/.test(instruction);
666
+ }
667
+ hasErrorHandling(instruction) {
668
+ return /\b(error|exception|fail|retry|fallback|recovery)\b/.test(instruction);
669
+ }
670
+ extractInputParameters(instruction) {
671
+ const parameters = [];
672
+ const parameterPatterns = [
673
+ /with\s+([a-zA-Z_][a-zA-Z0-9_]*)/g,
674
+ /using\s+([a-zA-Z_][a-zA-Z0-9_]*)/g,
675
+ /input\s+([a-zA-Z_][a-zA-Z0-9_]*)/g
676
+ ];
677
+ parameterPatterns.forEach(pattern => {
678
+ let match;
679
+ while ((match = pattern.exec(instruction)) !== null) {
680
+ parameters.push(match[1]);
681
+ }
682
+ });
683
+ return parameters;
684
+ }
685
+ extractOutputParameters(instruction) {
686
+ const parameters = [];
687
+ const parameterPatterns = [
688
+ /returns?\s+([a-zA-Z_][a-zA-Z0-9_]*)/g,
689
+ /output\s+([a-zA-Z_][a-zA-Z0-9_]*)/g,
690
+ /result\s+([a-zA-Z_][a-zA-Z0-9_]*)/g
691
+ ];
692
+ parameterPatterns.forEach(pattern => {
693
+ let match;
694
+ while ((match = pattern.exec(instruction)) !== null) {
695
+ parameters.push(match[1]);
696
+ }
697
+ });
698
+ return parameters;
699
+ }
700
+ extractContextIndicators(instruction) {
701
+ const indicators = [];
702
+ const contextKeywords = [
703
+ 'approval', 'fulfillment', 'notification', 'integration', 'utility',
704
+ 'business', 'order', 'catalog', 'incident', 'problem', 'change'
705
+ ];
706
+ contextKeywords.forEach(keyword => {
707
+ if (instruction.includes(keyword)) {
708
+ indicators.push(keyword);
709
+ }
710
+ });
711
+ return indicators;
712
+ }
713
+ isGenericPattern(requirements) {
714
+ return requirements.isUtilityFunction ||
715
+ requirements.hasReusableComponents ||
716
+ requirements.contextIndicators.length > 2;
717
+ }
718
+ isCommonBusinessLogic(requirements) {
719
+ const commonLogicPatterns = ['approval', 'validation', 'notification', 'assignment'];
720
+ return commonLogicPatterns.some(pattern => requirements.contextIndicators.includes(pattern));
721
+ }
722
+ calculateBusinessLogicComplexity(requirements) {
723
+ let complexity = 0;
724
+ if (requirements.hasBusinessRules)
725
+ complexity += 3;
726
+ if (requirements.hasApprovalLogic)
727
+ complexity += 2;
728
+ if (requirements.hasConditionalLogic)
729
+ complexity += 2;
730
+ if (requirements.hasLoops)
731
+ complexity += 3;
732
+ return complexity;
733
+ }
734
+ calculateIntegrationPoints(requirements) {
735
+ let points = 0;
736
+ if (requirements.hasExternalIntegrations)
737
+ points += 2;
738
+ if (requirements.hasDataTransformation)
739
+ points += 1;
740
+ return points;
741
+ }
742
+ calculateErrorHandlingComplexity(requirements) {
743
+ return requirements.hasErrorHandling ? 3 : 0;
744
+ }
745
+ calculateTestabilityScore(requirements) {
746
+ let score = 5; // Base score
747
+ if (requirements.inputParameters.length > 0)
748
+ score += 2;
749
+ if (requirements.outputParameters.length > 0)
750
+ score += 2;
751
+ if (requirements.hasBusinessRules)
752
+ score += 1;
753
+ return Math.min(score, 10);
754
+ }
755
+ calculateMaintainabilityScore(requirements) {
756
+ let score = 5; // Base score
757
+ if (requirements.actions.length > 5)
758
+ score += 2;
759
+ if (requirements.hasBusinessRules)
760
+ score += 1;
761
+ if (requirements.hasErrorHandling)
762
+ score += 1;
763
+ if (requirements.hasReusableComponents)
764
+ score += 1;
765
+ return Math.min(score, 10);
766
+ }
767
+ }
768
+ exports.FlowSubflowDecisionEngine = FlowSubflowDecisionEngine;
769
+ //# sourceMappingURL=flow-subflow-decision-engine.js.map