Class: EnhanceSwarm::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/enhance_swarm/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean



13
14
15
# File 'lib/enhance_swarm/cli.rb', line 13

def self.exit_on_failure?
  true
end

Instance Method Details

#configObject



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/enhance_swarm/cli.rb', line 268

def config
  config = EnhanceSwarm.configuration

  say "\n⚙️  EnhanceSwarm Configuration:", :green
  say "\nProject:"
  say "  Name: #{config.project_name}"
  say "  Description: #{config.project_description}"
  say "  Stack: #{config.technology_stack}"

  say "\nCommands:"
  say "  Test: #{config.test_command}"
  say "  Task: #{config.task_command}"

  say "\nOrchestration:"
  say "  Max agents: #{config.max_concurrent_agents}"
  say "  Monitor interval: #{config.monitor_interval}s"
  say "  Monitor timeout: #{config.monitor_timeout}s"

  say "\nMCP Tools:"
  config.mcp_tools.each do |tool, enabled|
    status = enabled ? '✓'.green : '✗'.red
    say "  #{tool}: #{status}"
  end
end

#doctorObject



296
297
298
299
300
301
302
# File 'lib/enhance_swarm/cli.rb', line 296

def doctor
  if options[:json]
    run_detailed_doctor_json
  else
    run_basic_doctor(options[:detailed])
  end
end

#enhanceObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/enhance_swarm/cli.rb', line 41

def enhance
  say '🎯 ENHANCE Protocol Activated!', :green

  # Auto-cleanup if enabled
  if options[:auto_cleanup]
    cleanup_count = SmartDefaults.auto_cleanup_if_needed
    say "🧹 Auto-cleaned #{cleanup_count} stale resources", :blue if cleanup_count > 0
  end

  # Setup notifications and interrupts
  setup_notifications_and_interrupts if options[:notifications]

  # Learn from this action
  SmartDefaults.learn_from_action('enhance', {
    control_agent: options[:control_agent],
    follow: options[:follow],
    notifications: options[:notifications]
  })

  # Get task description from user
  print "Enter task description: "
  task_description = $stdin.gets.chomp
  
  # Use smart orchestration by default
  begin
    SmartOrchestration.enhance_with_coordination(task_description)
    say "✅ Smart orchestration completed successfully!", :green
  rescue StandardError => e
    say "❌ Smart orchestration failed, falling back to control agent", :yellow
    Logger.error("Smart orchestration error: #{e.message}")
    
    if options[:control_agent] && !options[:dry_run]
      enhance_with_control_agent_manual(task_description)
    else
      orchestrator = Orchestrator.new
      orchestrator.enhance(
        task_id: options[:task],
        dry_run: options[:dry_run],
        follow: options[:follow]
      )
    end
  end
end

#generate(generator_type) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/enhance_swarm/cli.rb', line 250

def generate(generator_type)
  case generator_type
  when 'claude'
    Generator.new.generate_claude_files
    say '✅ Generated Claude configuration files', :green
  when 'mcp'
    Generator.new.generate_mcp_config
    say '✅ Generated MCP configuration', :green
  when 'hooks'
    Generator.new.generate_git_hooks
    say '✅ Generated Git hooks', :green
  else
    say "❌ Unknown generator: #{generator_type}", :red
    say 'Available generators: claude, mcp, hooks'
  end
end

#initObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/enhance_swarm/cli.rb', line 21

def init
  say '🚀 Initializing EnhanceSwarm...', :green

  generator = Generator.new
  generator.init_project

  say "\n✅ EnhanceSwarm initialized successfully!", :green
  say "\nNext steps:", :yellow
  say '  1. Review and customize .enhance_swarm.yml'
  say '  2. Check the generated .claude/ directory'
  say "  3. Run 'enhance-swarm enhance' to start orchestration"
end

#monitorObject



209
210
211
212
213
214
215
216
217
# File 'lib/enhance_swarm/cli.rb', line 209

def monitor
  say '👀 Monitoring swarm agents...', :yellow

  monitor = Monitor.new
  monitor.watch(
    interval: options[:interval],
    timeout: options[:timeout]
  )
end

#orchestrate(task_desc) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/enhance_swarm/cli.rb', line 90

def orchestrate(task_desc)
  say "🎯 Starting intelligent multi-agent orchestration", :blue
  say "Task: #{task_desc}", :white
  
  begin
    coordinator = TaskCoordinator.new
    
    if options[:detached]
      # Run completely detached - spawn background process and return immediately
      spawn_detached_orchestration(task_desc, coordinator)
      say "🚀 Orchestration started in detached mode", :green
      say "📁 Check .enhance_swarm/logs/ for progress updates", :blue
      return
    elsif options[:background]
      # Run in background with periodic status updates
      run_background_orchestration(task_desc, coordinator)
    else
      # Run normally (blocking)
      coordinator.coordinate_task(task_desc)
    end
    
    say "✅ Multi-agent orchestration completed successfully!", :green
  rescue Timeout::Error => e
    say "⏰ Orchestration timed out: #{e.message}", :red
    say "Consider breaking down the task into smaller parts", :yellow
  rescue SystemExit, Interrupt
    say "🛑 Orchestration interrupted by user", :yellow
  rescue StandardError => e
    say "❌ Orchestration failed: #{e.message}", :red
    say "Debug info: #{e.backtrace.first(3).join("\n")}", :yellow
    say "Run 'enhance-swarm doctor' to check system health", :blue
  end
end

#spawn(task_desc) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/enhance_swarm/cli.rb', line 162

def spawn(task_desc)
  # Use smart role detection if no role specified
  role = options[:role] || SmartDefaults.suggest_role_for_task(task_desc)
  
  if role != options[:role]
    say "🤖 Auto-detected role: #{role} (use --role to override)", :blue
  end

  # Learn from this action
  SmartDefaults.learn_from_action('spawn', {
    role: role,
    worktree: options[:worktree],
    follow: options[:follow]
  })

  if options[:follow]
    say "🤖 Spawning #{role} agent with live output for: #{task_desc}", :yellow
    spawn_with_streaming(task_desc, role)
  else
    say "🤖 Spawning #{role} agent for: #{task_desc}", :yellow
    
    orchestrator = Orchestrator.new
    
    # Ensure session exists before spawning
    session_manager = SessionManager.new
    unless session_manager.session_exists?
      session_manager.create_session(task_desc)
      say "📁 Created session for agent spawn", :blue
    end
    
    result = orchestrator.spawn_single(
      task: task_desc,
      role: role,
      worktree: options[:worktree]
    )
    
    if result
      say "✅ Agent spawned successfully with PID: #{result}", :green
    else
      say "❌ Failed to spawn agent", :red
    end
  end
end

#statusObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/enhance_swarm/cli.rb', line 125

def status
  if File.exist?('.enhance_swarm/logs/orchestration.pid')
    pid = File.read('.enhance_swarm/logs/orchestration.pid').strip
    
    begin
      Process.getpgid(pid.to_i)
      say "🔄 Orchestration running (PID: #{pid})", :blue
      
      if File.exist?('.enhance_swarm/logs/orchestration_status.txt')
        status = File.read('.enhance_swarm/logs/orchestration_status.txt').strip
        say "Status: #{status}", :yellow
      end
      
      # Show recent log entries
      if File.exist?('.enhance_swarm/logs/orchestration.log')
        logs = File.readlines('.enhance_swarm/logs/orchestration.log').last(5)
        say "\nRecent activity:", :white
        logs.each { |log| say "  #{log.strip}", :light_black }
      end
      
    rescue Errno::ESRCH
      say "💤 No orchestration currently running", :yellow
      
      if File.exist?('.enhance_swarm/logs/orchestration_status.txt')
        status = File.read('.enhance_swarm/logs/orchestration_status.txt').strip
        say "Last status: #{status}", :light_black
      end
    end
  else
    say "💤 No orchestration currently running", :yellow
  end
end