@claude-flow/cli 3.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. package/.agentic-flow/intelligence.json +16 -0
  2. package/.claude-flow/metrics/agent-metrics.json +1 -0
  3. package/.claude-flow/metrics/performance.json +87 -0
  4. package/.claude-flow/metrics/task-metrics.json +10 -0
  5. package/README.md +1186 -0
  6. package/__tests__/README.md +140 -0
  7. package/__tests__/TEST_SUMMARY.md +144 -0
  8. package/__tests__/cli.test.ts +558 -0
  9. package/__tests__/commands.test.ts +726 -0
  10. package/__tests__/config-adapter.test.ts +362 -0
  11. package/__tests__/config-loading.test.ts +106 -0
  12. package/__tests__/coverage/.tmp/coverage-0.json +1 -0
  13. package/__tests__/coverage/.tmp/coverage-1.json +1 -0
  14. package/__tests__/coverage/.tmp/coverage-2.json +1 -0
  15. package/__tests__/coverage/.tmp/coverage-3.json +1 -0
  16. package/__tests__/coverage/.tmp/coverage-4.json +1 -0
  17. package/__tests__/coverage/.tmp/coverage-5.json +1 -0
  18. package/__tests__/mcp-client.test.ts +480 -0
  19. package/__tests__/p1-commands.test.ts +1064 -0
  20. package/bin/cli.js +14 -0
  21. package/dist/src/commands/agent.d.ts +8 -0
  22. package/dist/src/commands/agent.d.ts.map +1 -0
  23. package/dist/src/commands/agent.js +803 -0
  24. package/dist/src/commands/agent.js.map +1 -0
  25. package/dist/src/commands/config.d.ts +8 -0
  26. package/dist/src/commands/config.d.ts.map +1 -0
  27. package/dist/src/commands/config.js +406 -0
  28. package/dist/src/commands/config.js.map +1 -0
  29. package/dist/src/commands/hive-mind.d.ts +8 -0
  30. package/dist/src/commands/hive-mind.d.ts.map +1 -0
  31. package/dist/src/commands/hive-mind.js +627 -0
  32. package/dist/src/commands/hive-mind.js.map +1 -0
  33. package/dist/src/commands/hooks.d.ts +8 -0
  34. package/dist/src/commands/hooks.d.ts.map +1 -0
  35. package/dist/src/commands/hooks.js +2098 -0
  36. package/dist/src/commands/hooks.js.map +1 -0
  37. package/dist/src/commands/index.d.ts +51 -0
  38. package/dist/src/commands/index.d.ts.map +1 -0
  39. package/dist/src/commands/index.js +105 -0
  40. package/dist/src/commands/index.js.map +1 -0
  41. package/dist/src/commands/init.d.ts +8 -0
  42. package/dist/src/commands/init.d.ts.map +1 -0
  43. package/dist/src/commands/init.js +532 -0
  44. package/dist/src/commands/init.js.map +1 -0
  45. package/dist/src/commands/mcp.d.ts +11 -0
  46. package/dist/src/commands/mcp.d.ts.map +1 -0
  47. package/dist/src/commands/mcp.js +662 -0
  48. package/dist/src/commands/mcp.js.map +1 -0
  49. package/dist/src/commands/memory.d.ts +8 -0
  50. package/dist/src/commands/memory.d.ts.map +1 -0
  51. package/dist/src/commands/memory.js +911 -0
  52. package/dist/src/commands/memory.js.map +1 -0
  53. package/dist/src/commands/migrate.d.ts +8 -0
  54. package/dist/src/commands/migrate.d.ts.map +1 -0
  55. package/dist/src/commands/migrate.js +398 -0
  56. package/dist/src/commands/migrate.js.map +1 -0
  57. package/dist/src/commands/process.d.ts +10 -0
  58. package/dist/src/commands/process.d.ts.map +1 -0
  59. package/dist/src/commands/process.js +566 -0
  60. package/dist/src/commands/process.js.map +1 -0
  61. package/dist/src/commands/session.d.ts +8 -0
  62. package/dist/src/commands/session.d.ts.map +1 -0
  63. package/dist/src/commands/session.js +750 -0
  64. package/dist/src/commands/session.js.map +1 -0
  65. package/dist/src/commands/start.d.ts +8 -0
  66. package/dist/src/commands/start.d.ts.map +1 -0
  67. package/dist/src/commands/start.js +398 -0
  68. package/dist/src/commands/start.js.map +1 -0
  69. package/dist/src/commands/status.d.ts +8 -0
  70. package/dist/src/commands/status.d.ts.map +1 -0
  71. package/dist/src/commands/status.js +560 -0
  72. package/dist/src/commands/status.js.map +1 -0
  73. package/dist/src/commands/swarm.d.ts +8 -0
  74. package/dist/src/commands/swarm.d.ts.map +1 -0
  75. package/dist/src/commands/swarm.js +573 -0
  76. package/dist/src/commands/swarm.js.map +1 -0
  77. package/dist/src/commands/task.d.ts +8 -0
  78. package/dist/src/commands/task.d.ts.map +1 -0
  79. package/dist/src/commands/task.js +671 -0
  80. package/dist/src/commands/task.js.map +1 -0
  81. package/dist/src/commands/workflow.d.ts +8 -0
  82. package/dist/src/commands/workflow.d.ts.map +1 -0
  83. package/dist/src/commands/workflow.js +617 -0
  84. package/dist/src/commands/workflow.js.map +1 -0
  85. package/dist/src/config-adapter.d.ts +15 -0
  86. package/dist/src/config-adapter.d.ts.map +1 -0
  87. package/dist/src/config-adapter.js +185 -0
  88. package/dist/src/config-adapter.js.map +1 -0
  89. package/dist/src/index.d.ts +55 -0
  90. package/dist/src/index.d.ts.map +1 -0
  91. package/dist/src/index.js +312 -0
  92. package/dist/src/index.js.map +1 -0
  93. package/dist/src/infrastructure/in-memory-repositories.d.ts +68 -0
  94. package/dist/src/infrastructure/in-memory-repositories.d.ts.map +1 -0
  95. package/dist/src/infrastructure/in-memory-repositories.js +264 -0
  96. package/dist/src/infrastructure/in-memory-repositories.js.map +1 -0
  97. package/dist/src/init/claudemd-generator.d.ts +15 -0
  98. package/dist/src/init/claudemd-generator.d.ts.map +1 -0
  99. package/dist/src/init/claudemd-generator.js +626 -0
  100. package/dist/src/init/claudemd-generator.js.map +1 -0
  101. package/dist/src/init/executor.d.ts +11 -0
  102. package/dist/src/init/executor.d.ts.map +1 -0
  103. package/dist/src/init/executor.js +647 -0
  104. package/dist/src/init/executor.js.map +1 -0
  105. package/dist/src/init/helpers-generator.d.ts +42 -0
  106. package/dist/src/init/helpers-generator.d.ts.map +1 -0
  107. package/dist/src/init/helpers-generator.js +613 -0
  108. package/dist/src/init/helpers-generator.js.map +1 -0
  109. package/dist/src/init/index.d.ts +12 -0
  110. package/dist/src/init/index.d.ts.map +1 -0
  111. package/dist/src/init/index.js +15 -0
  112. package/dist/src/init/index.js.map +1 -0
  113. package/dist/src/init/mcp-generator.d.ts +18 -0
  114. package/dist/src/init/mcp-generator.d.ts.map +1 -0
  115. package/dist/src/init/mcp-generator.js +71 -0
  116. package/dist/src/init/mcp-generator.js.map +1 -0
  117. package/dist/src/init/settings-generator.d.ts +14 -0
  118. package/dist/src/init/settings-generator.d.ts.map +1 -0
  119. package/dist/src/init/settings-generator.js +257 -0
  120. package/dist/src/init/settings-generator.js.map +1 -0
  121. package/dist/src/init/statusline-generator.d.ts +14 -0
  122. package/dist/src/init/statusline-generator.d.ts.map +1 -0
  123. package/dist/src/init/statusline-generator.js +206 -0
  124. package/dist/src/init/statusline-generator.js.map +1 -0
  125. package/dist/src/init/types.d.ts +240 -0
  126. package/dist/src/init/types.d.ts.map +1 -0
  127. package/dist/src/init/types.js +210 -0
  128. package/dist/src/init/types.js.map +1 -0
  129. package/dist/src/mcp-client.d.ts +92 -0
  130. package/dist/src/mcp-client.d.ts.map +1 -0
  131. package/dist/src/mcp-client.js +189 -0
  132. package/dist/src/mcp-client.js.map +1 -0
  133. package/dist/src/mcp-server.d.ts +153 -0
  134. package/dist/src/mcp-server.d.ts.map +1 -0
  135. package/dist/src/mcp-server.js +448 -0
  136. package/dist/src/mcp-server.js.map +1 -0
  137. package/dist/src/mcp-tools/agent-tools.d.ts +8 -0
  138. package/dist/src/mcp-tools/agent-tools.d.ts.map +1 -0
  139. package/dist/src/mcp-tools/agent-tools.js +90 -0
  140. package/dist/src/mcp-tools/agent-tools.js.map +1 -0
  141. package/dist/src/mcp-tools/config-tools.d.ts +8 -0
  142. package/dist/src/mcp-tools/config-tools.d.ts.map +1 -0
  143. package/dist/src/mcp-tools/config-tools.js +86 -0
  144. package/dist/src/mcp-tools/config-tools.js.map +1 -0
  145. package/dist/src/mcp-tools/hooks-tools.d.ts +41 -0
  146. package/dist/src/mcp-tools/hooks-tools.d.ts.map +1 -0
  147. package/dist/src/mcp-tools/hooks-tools.js +1646 -0
  148. package/dist/src/mcp-tools/hooks-tools.js.map +1 -0
  149. package/dist/src/mcp-tools/index.d.ts +12 -0
  150. package/dist/src/mcp-tools/index.d.ts.map +1 -0
  151. package/dist/src/mcp-tools/index.js +11 -0
  152. package/dist/src/mcp-tools/index.js.map +1 -0
  153. package/dist/src/mcp-tools/memory-tools.d.ts +8 -0
  154. package/dist/src/mcp-tools/memory-tools.d.ts.map +1 -0
  155. package/dist/src/mcp-tools/memory-tools.js +87 -0
  156. package/dist/src/mcp-tools/memory-tools.js.map +1 -0
  157. package/dist/src/mcp-tools/swarm-tools.d.ts +8 -0
  158. package/dist/src/mcp-tools/swarm-tools.d.ts.map +1 -0
  159. package/dist/src/mcp-tools/swarm-tools.js +67 -0
  160. package/dist/src/mcp-tools/swarm-tools.js.map +1 -0
  161. package/dist/src/mcp-tools/types.d.ts +31 -0
  162. package/dist/src/mcp-tools/types.d.ts.map +1 -0
  163. package/dist/src/mcp-tools/types.js +7 -0
  164. package/dist/src/mcp-tools/types.js.map +1 -0
  165. package/dist/src/output.d.ts +117 -0
  166. package/dist/src/output.d.ts.map +1 -0
  167. package/dist/src/output.js +471 -0
  168. package/dist/src/output.js.map +1 -0
  169. package/dist/src/parser.d.ts +41 -0
  170. package/dist/src/parser.d.ts.map +1 -0
  171. package/dist/src/parser.js +353 -0
  172. package/dist/src/parser.js.map +1 -0
  173. package/dist/src/prompt.d.ts +44 -0
  174. package/dist/src/prompt.d.ts.map +1 -0
  175. package/dist/src/prompt.js +501 -0
  176. package/dist/src/prompt.js.map +1 -0
  177. package/dist/src/types.d.ts +198 -0
  178. package/dist/src/types.d.ts.map +1 -0
  179. package/dist/src/types.js +38 -0
  180. package/dist/src/types.js.map +1 -0
  181. package/dist/tsconfig.tsbuildinfo +1 -0
  182. package/docs/CONFIG_LOADING.md +236 -0
  183. package/docs/IMPLEMENTATION_COMPLETE.md +421 -0
  184. package/docs/MCP_CLIENT_GUIDE.md +620 -0
  185. package/docs/REFACTORING_SUMMARY.md +247 -0
  186. package/package.json +29 -0
  187. package/src/commands/agent.ts +941 -0
  188. package/src/commands/config.ts +452 -0
  189. package/src/commands/hive-mind.ts +762 -0
  190. package/src/commands/hooks.ts +2603 -0
  191. package/src/commands/index.ts +115 -0
  192. package/src/commands/init.ts +597 -0
  193. package/src/commands/mcp.ts +753 -0
  194. package/src/commands/memory.ts +1063 -0
  195. package/src/commands/migrate.ts +447 -0
  196. package/src/commands/process.ts +617 -0
  197. package/src/commands/session.ts +891 -0
  198. package/src/commands/start.ts +457 -0
  199. package/src/commands/status.ts +705 -0
  200. package/src/commands/swarm.ts +648 -0
  201. package/src/commands/task.ts +792 -0
  202. package/src/commands/workflow.ts +742 -0
  203. package/src/config-adapter.ts +210 -0
  204. package/src/index.ts +383 -0
  205. package/src/infrastructure/in-memory-repositories.ts +310 -0
  206. package/src/init/claudemd-generator.ts +631 -0
  207. package/src/init/executor.ts +756 -0
  208. package/src/init/helpers-generator.ts +628 -0
  209. package/src/init/index.ts +60 -0
  210. package/src/init/mcp-generator.ts +83 -0
  211. package/src/init/settings-generator.ts +274 -0
  212. package/src/init/statusline-generator.ts +211 -0
  213. package/src/init/types.ts +447 -0
  214. package/src/mcp-client.ts +227 -0
  215. package/src/mcp-server.ts +571 -0
  216. package/src/mcp-tools/agent-tools.ts +92 -0
  217. package/src/mcp-tools/config-tools.ts +88 -0
  218. package/src/mcp-tools/hooks-tools.ts +1819 -0
  219. package/src/mcp-tools/index.ts +12 -0
  220. package/src/mcp-tools/memory-tools.ts +89 -0
  221. package/src/mcp-tools/swarm-tools.ts +69 -0
  222. package/src/mcp-tools/types.ts +33 -0
  223. package/src/output.ts +593 -0
  224. package/src/parser.ts +417 -0
  225. package/src/prompt.ts +619 -0
  226. package/src/types.ts +287 -0
  227. package/tsconfig.json +16 -0
  228. package/tsconfig.tsbuildinfo +1 -0
  229. package/vitest.config.ts +13 -0
@@ -0,0 +1,210 @@
1
+ /**
2
+ * Configuration Adapter
3
+ * Converts between SystemConfig and V3Config types
4
+ */
5
+
6
+ import type { SystemConfig } from '@claude-flow/shared';
7
+ import type { V3Config } from './types.js';
8
+
9
+ /**
10
+ * Convert SystemConfig to V3Config (CLI-specific format)
11
+ */
12
+ export function systemConfigToV3Config(systemConfig: SystemConfig): V3Config {
13
+ return {
14
+ version: '3.0.0',
15
+ projectRoot: systemConfig.orchestrator?.session?.dataDir || process.cwd(),
16
+
17
+ // Agent configuration
18
+ agents: {
19
+ defaultType: 'coder',
20
+ autoSpawn: false, // Not in SystemConfig
21
+ maxConcurrent: systemConfig.orchestrator?.lifecycle?.maxConcurrentAgents ?? 15,
22
+ timeout: systemConfig.orchestrator?.lifecycle?.spawnTimeout ?? 300000,
23
+ providers: [],
24
+ },
25
+
26
+ // Swarm configuration
27
+ swarm: {
28
+ topology: normalizeTopology(systemConfig.swarm?.topology),
29
+ maxAgents: systemConfig.swarm?.maxAgents ?? 15,
30
+ autoScale: systemConfig.swarm?.autoScale?.enabled ?? false,
31
+ coordinationStrategy: systemConfig.swarm?.coordination?.consensusRequired ? 'consensus' : 'leader',
32
+ healthCheckInterval: systemConfig.swarm?.coordination?.timeoutMs ?? 10000,
33
+ },
34
+
35
+ // Memory configuration
36
+ memory: {
37
+ backend: normalizeMemoryBackend(systemConfig.memory?.type),
38
+ persistPath: systemConfig.memory?.path || './data/memory',
39
+ cacheSize: systemConfig.memory?.maxSize ?? 1000000,
40
+ enableHNSW: systemConfig.memory?.agentdb?.indexType === 'hnsw',
41
+ vectorDimension: systemConfig.memory?.agentdb?.dimensions ?? 1536,
42
+ },
43
+
44
+ // MCP configuration
45
+ mcp: {
46
+ serverHost: systemConfig.mcp?.transport?.host || 'localhost',
47
+ serverPort: systemConfig.mcp?.transport?.port ?? 3000,
48
+ autoStart: false, // Not in SystemConfig
49
+ transportType: systemConfig.mcp?.transport?.type || 'stdio',
50
+ tools: [], // Not in SystemConfig
51
+ },
52
+
53
+ // CLI preferences
54
+ cli: {
55
+ colorOutput: true,
56
+ interactive: true,
57
+ verbosity: 'normal',
58
+ outputFormat: 'text',
59
+ progressStyle: 'spinner',
60
+ },
61
+
62
+ // Hooks configuration
63
+ hooks: {
64
+ enabled: false,
65
+ autoExecute: false,
66
+ hooks: [],
67
+ },
68
+ };
69
+ }
70
+
71
+ /**
72
+ * Convert V3Config to SystemConfig
73
+ */
74
+ export function v3ConfigToSystemConfig(v3Config: V3Config): Partial<SystemConfig> {
75
+ return {
76
+ orchestrator: {
77
+ lifecycle: {
78
+ maxConcurrentAgents: v3Config.agents.maxConcurrent,
79
+ spawnTimeout: v3Config.agents.timeout,
80
+ terminateTimeout: 10000,
81
+ maxSpawnRetries: 3,
82
+ },
83
+ session: {
84
+ dataDir: v3Config.projectRoot,
85
+ persistSessions: true,
86
+ sessionRetentionMs: 3600000,
87
+ },
88
+ health: {
89
+ checkInterval: v3Config.swarm.healthCheckInterval,
90
+ historyLimit: 100,
91
+ degradedThreshold: 1,
92
+ unhealthyThreshold: 2,
93
+ },
94
+ },
95
+
96
+ swarm: {
97
+ topology: denormalizeTopology(v3Config.swarm.topology),
98
+ maxAgents: v3Config.swarm.maxAgents,
99
+ autoScale: {
100
+ enabled: v3Config.swarm.autoScale,
101
+ minAgents: 1,
102
+ maxAgents: v3Config.swarm.maxAgents,
103
+ scaleUpThreshold: 0.8,
104
+ scaleDownThreshold: 0.3,
105
+ },
106
+ coordination: {
107
+ consensusRequired: v3Config.swarm.coordinationStrategy === 'consensus',
108
+ timeoutMs: v3Config.swarm.healthCheckInterval,
109
+ retryPolicy: {
110
+ maxRetries: 3,
111
+ backoffMs: 500,
112
+ },
113
+ },
114
+ communication: {
115
+ protocol: 'events',
116
+ batchSize: 10,
117
+ flushIntervalMs: 100,
118
+ },
119
+ },
120
+
121
+ memory: {
122
+ type: denormalizeMemoryBackend(v3Config.memory.backend),
123
+ path: v3Config.memory.persistPath,
124
+ maxSize: v3Config.memory.cacheSize,
125
+ agentdb: {
126
+ dimensions: v3Config.memory.vectorDimension,
127
+ indexType: v3Config.memory.enableHNSW ? 'hnsw' : 'flat',
128
+ efConstruction: 200,
129
+ m: 16,
130
+ quantization: 'none',
131
+ },
132
+ },
133
+
134
+ mcp: {
135
+ name: 'claude-flow',
136
+ version: '3.0.0',
137
+ transport: {
138
+ type: v3Config.mcp.transportType as 'stdio' | 'http' | 'websocket',
139
+ host: v3Config.mcp.serverHost,
140
+ port: v3Config.mcp.serverPort,
141
+ },
142
+ capabilities: {
143
+ tools: true,
144
+ resources: true,
145
+ prompts: true,
146
+ logging: true,
147
+ },
148
+ },
149
+ };
150
+ }
151
+
152
+ /**
153
+ * Normalize topology from SystemConfig to V3Config
154
+ */
155
+ function normalizeTopology(
156
+ topology: string | undefined
157
+ ): 'hierarchical' | 'mesh' | 'ring' | 'star' | 'hybrid' {
158
+ switch (topology) {
159
+ case 'hierarchical':
160
+ case 'mesh':
161
+ case 'ring':
162
+ case 'star':
163
+ return topology;
164
+ case 'hierarchical-mesh':
165
+ case 'adaptive':
166
+ return 'hybrid';
167
+ default:
168
+ return 'hierarchical';
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Denormalize topology from V3Config to SystemConfig
174
+ */
175
+ function denormalizeTopology(
176
+ topology: 'hierarchical' | 'mesh' | 'ring' | 'star' | 'hybrid'
177
+ ): 'hierarchical' | 'mesh' | 'ring' | 'star' | 'adaptive' | 'hierarchical-mesh' {
178
+ if (topology === 'hybrid') {
179
+ return 'hierarchical-mesh';
180
+ }
181
+ return topology;
182
+ }
183
+
184
+ /**
185
+ * Normalize memory backend from SystemConfig to V3Config
186
+ */
187
+ function normalizeMemoryBackend(
188
+ backend: string | undefined
189
+ ): 'memory' | 'sqlite' | 'agentdb' | 'hybrid' {
190
+ switch (backend) {
191
+ case 'memory':
192
+ case 'sqlite':
193
+ case 'agentdb':
194
+ case 'hybrid':
195
+ return backend;
196
+ case 'redis':
197
+ return 'memory'; // Redis maps to memory for CLI purposes
198
+ default:
199
+ return 'hybrid';
200
+ }
201
+ }
202
+
203
+ /**
204
+ * Denormalize memory backend from V3Config to SystemConfig
205
+ */
206
+ function denormalizeMemoryBackend(
207
+ backend: 'memory' | 'sqlite' | 'agentdb' | 'hybrid'
208
+ ): 'memory' | 'sqlite' | 'agentdb' | 'hybrid' | 'redis' {
209
+ return backend;
210
+ }
package/src/index.ts ADDED
@@ -0,0 +1,383 @@
1
+ /**
2
+ * V3 CLI Main Entry Point
3
+ * Modernized CLI for Claude Flow V3
4
+ */
5
+
6
+ import type { Command, CommandContext, CommandResult, V3Config, CLIError } from './types.js';
7
+ import { CommandParser, commandParser } from './parser.js';
8
+ import { OutputFormatter, output } from './output.js';
9
+ import { commands, commandRegistry, getCommand } from './commands/index.js';
10
+
11
+ export const VERSION = '3.0.0-alpha.1';
12
+
13
+ export interface CLIOptions {
14
+ name?: string;
15
+ description?: string;
16
+ version?: string;
17
+ interactive?: boolean;
18
+ }
19
+
20
+ /**
21
+ * V3 CLI Application
22
+ */
23
+ export class CLI {
24
+ private name: string;
25
+ private description: string;
26
+ private version: string;
27
+ private parser: CommandParser;
28
+ private output: OutputFormatter;
29
+ private interactive: boolean;
30
+
31
+ constructor(options: CLIOptions = {}) {
32
+ this.name = options.name || 'claude-flow';
33
+ this.description = options.description || 'Claude Flow V3 - AI Agent Orchestration Platform';
34
+ this.version = options.version || VERSION;
35
+ this.parser = commandParser;
36
+ this.output = output;
37
+ this.interactive = options.interactive ?? process.stdin.isTTY ?? false;
38
+
39
+ // Register all commands
40
+ for (const cmd of commands) {
41
+ this.parser.registerCommand(cmd);
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Run the CLI with given arguments
47
+ */
48
+ async run(args: string[] = process.argv.slice(2)): Promise<void> {
49
+ try {
50
+ // Parse arguments
51
+ const parseResult = this.parser.parse(args);
52
+ const { command: commandPath, flags, positional } = parseResult;
53
+
54
+ // Handle global flags
55
+ if (flags.version || flags.V) {
56
+ this.showVersion();
57
+ return;
58
+ }
59
+
60
+ if (flags.noColor) {
61
+ this.output.setColorEnabled(false);
62
+ }
63
+
64
+ // No command - show help
65
+ if (commandPath.length === 0 || flags.help || flags.h) {
66
+ if (commandPath.length > 0) {
67
+ // Show command-specific help
68
+ this.showCommandHelp(commandPath[0]);
69
+ } else {
70
+ this.showHelp();
71
+ }
72
+ return;
73
+ }
74
+
75
+ // Find and execute command
76
+ const commandName = commandPath[0];
77
+ // First check the parser's registry (for dynamically registered commands)
78
+ // Then fall back to the static registry
79
+ const command = this.parser.getCommand(commandName) || getCommand(commandName);
80
+
81
+ if (!command) {
82
+ this.output.printError(`Unknown command: ${commandName}`);
83
+ this.output.writeln(`Run "${this.name} --help" for available commands`);
84
+ process.exit(1);
85
+ }
86
+
87
+ // Handle subcommand (supports nested subcommands)
88
+ let targetCommand = command;
89
+ let subcommandArgs = positional;
90
+
91
+ // Process command path (e.g., ['hooks', 'worker', 'list'])
92
+ if (commandPath.length > 1 && command.subcommands) {
93
+ const subcommandName = commandPath[1];
94
+ const subcommand = command.subcommands.find(
95
+ sc => sc.name === subcommandName || sc.aliases?.includes(subcommandName)
96
+ );
97
+
98
+ if (subcommand) {
99
+ targetCommand = subcommand;
100
+ subcommandArgs = positional.slice(1);
101
+
102
+ // Check for nested subcommand (level 2)
103
+ if (commandPath.length > 2 && subcommand.subcommands) {
104
+ const nestedName = commandPath[2];
105
+ const nestedSubcommand = subcommand.subcommands.find(
106
+ sc => sc.name === nestedName || sc.aliases?.includes(nestedName)
107
+ );
108
+ if (nestedSubcommand) {
109
+ targetCommand = nestedSubcommand;
110
+ subcommandArgs = positional.slice(2);
111
+ }
112
+ }
113
+ }
114
+ } else if (positional.length > 0 && command.subcommands) {
115
+ // Check if first positional is a subcommand
116
+ const subcommandName = positional[0];
117
+ const subcommand = command.subcommands.find(
118
+ sc => sc.name === subcommandName || sc.aliases?.includes(subcommandName)
119
+ );
120
+
121
+ if (subcommand) {
122
+ targetCommand = subcommand;
123
+ subcommandArgs = positional.slice(1);
124
+
125
+ // Check for nested subcommand (level 2 from positional)
126
+ if (subcommandArgs.length > 0 && subcommand.subcommands) {
127
+ const nestedName = subcommandArgs[0];
128
+ const nestedSubcommand = subcommand.subcommands.find(
129
+ sc => sc.name === nestedName || sc.aliases?.includes(nestedName)
130
+ );
131
+ if (nestedSubcommand) {
132
+ targetCommand = nestedSubcommand;
133
+ subcommandArgs = subcommandArgs.slice(1);
134
+ }
135
+ }
136
+ }
137
+ }
138
+
139
+ // Validate flags
140
+ const validationErrors = this.parser.validateFlags(flags, targetCommand);
141
+ if (validationErrors.length > 0) {
142
+ for (const error of validationErrors) {
143
+ this.output.printError(error);
144
+ }
145
+ process.exit(1);
146
+ }
147
+
148
+ // Build context
149
+ const ctx: CommandContext = {
150
+ args: subcommandArgs,
151
+ flags,
152
+ config: await this.loadConfig(flags.config as string),
153
+ cwd: process.cwd(),
154
+ interactive: this.interactive && !flags.quiet
155
+ };
156
+
157
+ // Execute command
158
+ if (targetCommand.action) {
159
+ const result = await targetCommand.action(ctx);
160
+
161
+ if (result && !result.success) {
162
+ process.exit(result.exitCode || 1);
163
+ }
164
+ } else {
165
+ // No action - show command help
166
+ this.showCommandHelp(commandName);
167
+ }
168
+ } catch (error) {
169
+ // Don't re-handle if this is a process.exit error (from mocked tests)
170
+ const errorMessage = (error as Error).message;
171
+ if (errorMessage && errorMessage.startsWith('process.exit:')) {
172
+ throw error; // Re-throw so tests can capture the exit code
173
+ }
174
+ this.handleError(error as Error);
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Show main help
180
+ */
181
+ private showHelp(): void {
182
+ this.output.writeln();
183
+ this.output.writeln(this.output.bold(`${this.name} v${this.version}`));
184
+ this.output.writeln(this.output.dim(this.description));
185
+ this.output.writeln();
186
+
187
+ this.output.writeln(this.output.bold('USAGE:'));
188
+ this.output.writeln(` ${this.name} <command> [subcommand] [options]`);
189
+ this.output.writeln();
190
+
191
+ this.output.writeln(this.output.bold('COMMANDS:'));
192
+ for (const cmd of commands) {
193
+ if (cmd.hidden) continue;
194
+ const name = cmd.name.padEnd(12);
195
+ this.output.writeln(` ${this.output.highlight(name)} ${cmd.description}`);
196
+ }
197
+ this.output.writeln();
198
+
199
+ this.output.writeln(this.output.bold('GLOBAL OPTIONS:'));
200
+ for (const opt of this.parser.getGlobalOptions()) {
201
+ const flags = opt.short ? `-${opt.short}, --${opt.name}` : ` --${opt.name}`;
202
+ this.output.writeln(` ${flags.padEnd(25)} ${opt.description}`);
203
+ }
204
+ this.output.writeln();
205
+
206
+ this.output.writeln(this.output.bold('V3 FEATURES:'));
207
+ this.output.printList([
208
+ '15-agent hierarchical mesh coordination',
209
+ 'AgentDB with HNSW indexing (150x-12,500x faster)',
210
+ 'Flash Attention (2.49x-7.47x speedup)',
211
+ 'Unified SwarmCoordinator engine',
212
+ 'Event-sourced state management',
213
+ 'Domain-Driven Design architecture'
214
+ ]);
215
+ this.output.writeln();
216
+
217
+ this.output.writeln(this.output.bold('EXAMPLES:'));
218
+ this.output.writeln(` ${this.name} agent spawn -t coder # Spawn a coder agent`);
219
+ this.output.writeln(` ${this.name} swarm init --v3-mode # Initialize V3 swarm`);
220
+ this.output.writeln(` ${this.name} memory search -q "auth patterns" # Semantic search`);
221
+ this.output.writeln(` ${this.name} mcp start # Start MCP server`);
222
+ this.output.writeln();
223
+
224
+ this.output.writeln(this.output.dim('Run "claude-flow <command> --help" for command help'));
225
+ this.output.writeln();
226
+ }
227
+
228
+ /**
229
+ * Show command-specific help
230
+ */
231
+ private showCommandHelp(commandName: string): void {
232
+ const command = getCommand(commandName);
233
+
234
+ if (!command) {
235
+ this.output.printError(`Unknown command: ${commandName}`);
236
+ return;
237
+ }
238
+
239
+ this.output.writeln();
240
+ this.output.writeln(this.output.bold(`${this.name} ${command.name}`));
241
+ this.output.writeln(command.description);
242
+ this.output.writeln();
243
+
244
+ // Subcommands
245
+ if (command.subcommands && command.subcommands.length > 0) {
246
+ this.output.writeln(this.output.bold('SUBCOMMANDS:'));
247
+ for (const sub of command.subcommands) {
248
+ if (sub.hidden) continue;
249
+ const name = sub.name.padEnd(15);
250
+ const aliases = sub.aliases ? this.output.dim(` (${sub.aliases.join(', ')})`) : '';
251
+ this.output.writeln(` ${this.output.highlight(name)} ${sub.description}${aliases}`);
252
+ }
253
+ this.output.writeln();
254
+ }
255
+
256
+ // Options
257
+ if (command.options && command.options.length > 0) {
258
+ this.output.writeln(this.output.bold('OPTIONS:'));
259
+ for (const opt of command.options) {
260
+ const flags = opt.short ? `-${opt.short}, --${opt.name}` : ` --${opt.name}`;
261
+ const required = opt.required ? this.output.error(' (required)') : '';
262
+ const defaultVal = opt.default !== undefined ? this.output.dim(` [default: ${opt.default}]`) : '';
263
+ this.output.writeln(` ${flags.padEnd(25)} ${opt.description}${required}${defaultVal}`);
264
+ }
265
+ this.output.writeln();
266
+ }
267
+
268
+ // Examples
269
+ if (command.examples && command.examples.length > 0) {
270
+ this.output.writeln(this.output.bold('EXAMPLES:'));
271
+ for (const example of command.examples) {
272
+ this.output.writeln(` ${this.output.dim('$')} ${example.command}`);
273
+ this.output.writeln(` ${this.output.dim(example.description)}`);
274
+ }
275
+ this.output.writeln();
276
+ }
277
+ }
278
+
279
+ /**
280
+ * Show version
281
+ */
282
+ private showVersion(): void {
283
+ this.output.writeln(`${this.name} v${this.version}`);
284
+ }
285
+
286
+ /**
287
+ * Load configuration file
288
+ */
289
+ private async loadConfig(configPath?: string): Promise<V3Config | undefined> {
290
+ try {
291
+ // Import config utilities
292
+ const { loadConfig: loadSystemConfig } = await import('@claude-flow/shared');
293
+ const { systemConfigToV3Config } = await import('./config-adapter.js');
294
+
295
+ // Load configuration
296
+ const loaded = await loadSystemConfig({
297
+ file: configPath,
298
+ paths: configPath ? undefined : [process.cwd()],
299
+ });
300
+
301
+ // Convert to V3Config format
302
+ const v3Config = systemConfigToV3Config(loaded.config);
303
+
304
+ // Log warnings if any
305
+ if (loaded.warnings && loaded.warnings.length > 0) {
306
+ for (const warning of loaded.warnings) {
307
+ this.output.printWarning(warning);
308
+ }
309
+ }
310
+
311
+ return v3Config;
312
+ } catch (error) {
313
+ // Config loading is optional - don't fail if it doesn't exist
314
+ if (process.env.DEBUG) {
315
+ this.output.writeln(
316
+ this.output.dim(`Config loading failed: ${(error as Error).message}`)
317
+ );
318
+ }
319
+ return undefined;
320
+ }
321
+ }
322
+
323
+ /**
324
+ * Handle errors
325
+ */
326
+ private handleError(error: Error): void {
327
+ if ('code' in error) {
328
+ // CLIError
329
+ const cliError = error as CLIError;
330
+ this.output.printError(cliError.message);
331
+
332
+ if (cliError.details) {
333
+ this.output.writeln(this.output.dim(JSON.stringify(cliError.details, null, 2)));
334
+ }
335
+
336
+ process.exit(cliError.exitCode);
337
+ } else {
338
+ // Generic error
339
+ this.output.printError(error.message);
340
+
341
+ if (process.env.DEBUG) {
342
+ this.output.writeln();
343
+ this.output.writeln(this.output.dim(error.stack || ''));
344
+ }
345
+
346
+ process.exit(1);
347
+ }
348
+ }
349
+ }
350
+
351
+ // =============================================================================
352
+ // Module Exports
353
+ // =============================================================================
354
+
355
+ // Types
356
+ export * from './types.js';
357
+
358
+ // Parser
359
+ export { CommandParser, commandParser } from './parser.js';
360
+
361
+ // Output
362
+ export { OutputFormatter, output, Progress, Spinner } from './output.js';
363
+
364
+ // Prompt
365
+ export * from './prompt.js';
366
+
367
+ // Commands (internal use)
368
+ export * from './commands/index.js';
369
+
370
+ // MCP Server management
371
+ export {
372
+ MCPServerManager,
373
+ createMCPServerManager,
374
+ getServerManager,
375
+ startMCPServer,
376
+ stopMCPServer,
377
+ getMCPServerStatus,
378
+ type MCPServerOptions,
379
+ type MCPServerStatus,
380
+ } from './mcp-server.js';
381
+
382
+ // Default export
383
+ export default CLI;