Coverage for /Users/les/Projects/crackerjack/crackerjack/cli/handlers.py: 0%

146 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-03 15:39 -0700

1import asyncio 

2import os 

3import sys 

4from pathlib import Path 

5 

6from rich.console import Console 

7 

8from .options import Options 

9 

10 

11def setup_ai_agent_env(ai_agent: bool, verbose: bool = False) -> None: 

12 if ai_agent: 

13 os.environ["AI_AGENT"] = "1" 

14 os.environ["AI_AGENT_DEBUG"] = "1" 

15 

16 if verbose: 

17 os.environ["AI_AGENT_VERBOSE"] = "1" 

18 

19 if verbose: 

20 console = Console() 

21 console.print( 

22 "[bold cyan]🐛 AI Agent Debug Mode Configuration: [/ bold cyan]", 

23 ) 

24 console.print(f" • AI Agent: {'✅ Enabled' if ai_agent else '❌ Disabled'}") 

25 console.print( 

26 f" • Debug Mode: {'✅ Enabled' if os.environ.get('AI_AGENT_DEBUG') == '1' else '❌ Disabled'}", 

27 ) 

28 console.print( 

29 f" • Verbose Mode: {'✅ Enabled' if os.environ.get('AI_AGENT_VERBOSE') == '1' else '❌ Disabled'}", 

30 ) 

31 console.print(" • Enhanced logging will be available during execution") 

32 

33 

34def handle_mcp_server(websocket_port: int | None = None) -> None: 

35 from crackerjack.mcp.server import main as start_mcp_main 

36 

37 project_path = str(Path.cwd()) 

38 

39 if websocket_port: 

40 start_mcp_main(project_path, websocket_port) 

41 else: 

42 start_mcp_main(project_path) 

43 

44 

45def handle_monitor_mode(dev_mode: bool = False) -> None: 

46 from crackerjack.mcp.progress_monitor import run_progress_monitor 

47 

48 console = Console() 

49 console.print("[bold cyan]🌟 Starting Multi-Project Progress Monitor[/ bold cyan]") 

50 console.print( 

51 "[bold yellow]🐕 With integrated Service Watchdog and WebSocket polling[/ bold yellow]", 

52 ) 

53 

54 try: 

55 asyncio.run(run_progress_monitor(dev_mode=dev_mode)) 

56 except KeyboardInterrupt: 

57 console.print("\n[yellow]🛑 Monitor stopped[/ yellow]") 

58 

59 

60def handle_enhanced_monitor_mode(dev_mode: bool = False) -> None: 

61 from crackerjack.mcp.enhanced_progress_monitor import run_enhanced_progress_monitor 

62 

63 console = Console() 

64 console.print("[bold magenta]✨ Starting Enhanced Progress Monitor[/ bold magenta]") 

65 console.print( 

66 "[bold cyan]📊 With advanced MetricCard widgets and modern web UI patterns[/ bold cyan]", 

67 ) 

68 

69 try: 

70 asyncio.run(run_enhanced_progress_monitor(dev_mode=dev_mode)) 

71 except KeyboardInterrupt: 

72 console.print("\n[yellow]🛑 Enhanced Monitor stopped[/ yellow]") 

73 

74 

75def handle_dashboard_mode(dev_mode: bool = False) -> None: 

76 from crackerjack.mcp.dashboard import run_dashboard 

77 

78 console = Console() 

79 console.print("[bold green]🎯 Starting Comprehensive Dashboard[/ bold green]") 

80 console.print( 

81 "[bold cyan]📈 With system metrics, job tracking, and performance monitoring[/ bold cyan]", 

82 ) 

83 

84 try: 

85 run_dashboard() 

86 except KeyboardInterrupt: 

87 console.print("\n[yellow]🛑 Dashboard stopped[/ yellow]") 

88 

89 

90def handle_watchdog_mode() -> None: 

91 from crackerjack.mcp.service_watchdog import main as start_watchdog 

92 

93 console = Console() 

94 try: 

95 asyncio.run(start_watchdog()) 

96 except KeyboardInterrupt: 

97 console.print("\n[yellow]🛑 Watchdog stopped[/ yellow]") 

98 

99 

100def handle_start_websocket_server(port: int = 8675) -> None: 

101 from crackerjack.mcp.websocket.server import handle_websocket_server_command 

102 

103 handle_websocket_server_command(start=True, port=port) 

104 

105 

106def handle_stop_websocket_server() -> None: 

107 from crackerjack.mcp.websocket.server import handle_websocket_server_command 

108 

109 handle_websocket_server_command(stop=True) 

110 

111 

112def handle_restart_websocket_server(port: int = 8675) -> None: 

113 from crackerjack.mcp.websocket.server import handle_websocket_server_command 

114 

115 handle_websocket_server_command(restart=True, port=port) 

116 

117 

118def handle_stop_mcp_server() -> None: 

119 from crackerjack.services.server_manager import list_server_status, stop_all_servers 

120 

121 console = Console() 

122 console.print("[bold red]🛑 Stopping MCP Servers[/ bold red]") 

123 

124 list_server_status(console) 

125 

126 if stop_all_servers(console): 

127 console.print("\n[bold green]✅ All servers stopped successfully[/ bold green]") 

128 else: 

129 console.print("\n[bold red]❌ Some servers failed to stop[/ bold red]") 

130 raise SystemExit(1) 

131 

132 

133def handle_restart_mcp_server(websocket_port: int | None = None) -> None: 

134 from crackerjack.services.server_manager import restart_mcp_server 

135 

136 console = Console() 

137 if restart_mcp_server(websocket_port, console): 

138 console.print("\n[bold green]✅ MCP server restart completed[/ bold green]") 

139 else: 

140 console.print("\n[bold red]❌ MCP server restart failed[/ bold red]") 

141 raise SystemExit(1) 

142 

143 

144def handle_interactive_mode(options: Options) -> None: 

145 from crackerjack.cli.utils import get_package_version 

146 

147 from .interactive import launch_interactive_cli 

148 

149 pkg_version = get_package_version() 

150 launch_interactive_cli(pkg_version, options) 

151 

152 

153def handle_standard_mode( 

154 options: Options, 

155 async_mode: bool, 

156 job_id: str | None = None, 

157 orchestrated: bool = False, 

158) -> None: 

159 from rich.console import Console 

160 

161 console = Console() 

162 

163 if orchestrated: 

164 handle_orchestrated_mode(options, job_id) 

165 else: 

166 from crackerjack.core.async_workflow_orchestrator import ( 

167 AsyncWorkflowOrchestrator, 

168 ) 

169 from crackerjack.core.workflow_orchestrator import WorkflowOrchestrator 

170 

171 pkg_path = Path.cwd() 

172 

173 if async_mode: 

174 orchestrator = AsyncWorkflowOrchestrator( 

175 console=console, 

176 pkg_path=pkg_path, 

177 web_job_id=job_id, 

178 ) 

179 success = asyncio.run(orchestrator.run_complete_workflow_async(options)) 

180 else: 

181 orchestrator = WorkflowOrchestrator( 

182 console=console, 

183 pkg_path=pkg_path, 

184 web_job_id=job_id, 

185 verbose=options.verbose, 

186 ) 

187 success = asyncio.run(orchestrator.run_complete_workflow(options)) 

188 

189 if not success: 

190 raise SystemExit(1) 

191 

192 

193def handle_orchestrated_mode(options: Options, job_id: str | None = None) -> None: 

194 from rich.console import Console 

195 

196 console = Console() 

197 console.print("[bold bright_blue]🚀 ORCHESTRATED MODE ENABLED[/ bold bright_blue]") 

198 

199 try: 

200 from crackerjack.core.session_coordinator import SessionCoordinator 

201 from crackerjack.orchestration.advanced_orchestrator import ( 

202 AdvancedWorkflowOrchestrator, 

203 ) 

204 from crackerjack.orchestration.execution_strategies import ( 

205 AICoordinationMode, 

206 ExecutionStrategy, 

207 OrchestrationConfig, 

208 ProgressLevel, 

209 ) 

210 except ImportError as e: 

211 console.print(f"[red]Orchestrated mode not available: {e}[/ red]") 

212 console.print("[yellow]Falling back to standard mode[/ yellow]") 

213 handle_standard_mode(options, False, job_id) 

214 return 

215 

216 try: 

217 strategy = ExecutionStrategy(options.orchestration_strategy) 

218 except ValueError: 

219 console.print( 

220 f"[red]Invalid orchestration strategy: {options.orchestration_strategy}[/ red]", 

221 ) 

222 strategy = ExecutionStrategy.ADAPTIVE 

223 

224 try: 

225 progress = ProgressLevel(options.orchestration_progress) 

226 except ValueError: 

227 console.print( 

228 f"[red]Invalid progress level: {options.orchestration_progress}[/ red]", 

229 ) 

230 progress = ProgressLevel.GRANULAR 

231 

232 try: 

233 ai_mode = AICoordinationMode(options.orchestration_ai_mode) 

234 except ValueError: 

235 console.print(f"[red]Invalid AI mode: {options.orchestration_ai_mode}[/ red]") 

236 ai_mode = AICoordinationMode.SINGLE_AGENT 

237 

238 config = OrchestrationConfig( 

239 execution_strategy=strategy, 

240 progress_level=progress, 

241 ai_coordination_mode=ai_mode, 

242 ) 

243 

244 console.print(f"[cyan]Execution Strategy: [/ cyan] {strategy.value}") 

245 console.print(f"[cyan]Progress Level: [/ cyan] {progress.value}") 

246 console.print(f"[cyan]AI Coordination: [/ cyan] {ai_mode.value}") 

247 

248 pkg_path = Path.cwd() 

249 session = SessionCoordinator(console, pkg_path, web_job_id=job_id) 

250 orchestrator = AdvancedWorkflowOrchestrator(console, pkg_path, session, config) 

251 

252 try: 

253 success = asyncio.run(orchestrator.execute_orchestrated_workflow(options)) 

254 if success: 

255 console.print( 

256 "\n[bold green]🎉 ORCHESTRATED WORKFLOW COMPLETED SUCCESSFULLY ![/ bold green]", 

257 ) 

258 else: 

259 console.print("\n[bold red]❌ ORCHESTRATED WORKFLOW FAILED[/ bold red]") 

260 sys.exit(1) 

261 except KeyboardInterrupt: 

262 console.print("\n[yellow]🛑 Orchestrated workflow interrupted[/ yellow]") 

263 sys.exit(130) 

264 except Exception as e: 

265 console.print(f"\n[red]💥 Orchestrated workflow error: {e}[/ red]") 

266 sys.exit(1)