Module: EnhanceSwarm::AdditionalCommands

Defined in:
lib/enhance_swarm/additional_commands.rb

Class Method Summary collapse

Class Method Details

.add_commands_to(cli_class) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
84
85
86
87
88
89
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
123
124
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
157
158
159
160
161
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/enhance_swarm/additional_commands.rb', line 5

def self.add_commands_to(cli_class)
  cli_class.class_eval do
    desc 'dashboard', 'Start visual agent dashboard'
    option :agents, type: :array, desc: 'Specific agent IDs to monitor'
    option :refresh, type: :numeric, default: 2, desc: 'Refresh rate in seconds'
    option :snapshot, type: :boolean, desc: 'Take dashboard snapshot and exit'
    def dashboard
      if options[:snapshot]
        agents = discover_running_agents
        VisualDashboard.instance.display_snapshot(agents)
        return
      end

      say "๐Ÿ–ฅ๏ธ  Starting Visual Agent Dashboard...", :green
      
      # Create demo agents if none running
      agents = options[:agents] ? 
                 load_specific_agents(options[:agents]) : 
                 discover_running_agents
      
      if agents.empty?
        say "No agents found, creating demo agents for dashboard...", :yellow
        agents = create_demo_agents
      end
      
      dashboard = VisualDashboard.instance
      dashboard.instance_variable_set(:@refresh_rate, options[:refresh])
      
      begin
        dashboard.start_dashboard(agents)
      rescue Interrupt
        say "\n๐Ÿ–ฅ๏ธ  Dashboard stopped by user", :yellow
      end
    end

    desc 'notifications', 'Manage notification settings'
    option :enable, type: :boolean, desc: 'Enable notifications'
    option :disable, type: :boolean, desc: 'Disable notifications'
    option :test, type: :boolean, desc: 'Test notification system'
    option :status, type: :boolean, desc: 'Show notification status'
    def notifications
      notification_manager = NotificationManager.instance
      
      if options[:enable]
        notification_manager.enable!
        say "โœ… Notifications enabled", :green
      elsif options[:disable]
        notification_manager.disable!
        say "๐Ÿ”• Notifications disabled", :yellow
      elsif options[:test]
        say "๐Ÿ”” Testing notification system...", :blue
        notification_manager.test_notifications
        say "โœ… Notification test completed", :green
      else
        # Show status by default
        enabled = notification_manager.enabled?
        say "\n๐Ÿ’ฌ Notification Status:", :blue
        say "  Enabled: #{enabled ? 'โœ… Yes' : 'โŒ No'}"
        say "  Platform: #{RUBY_PLATFORM}"
        
        if enabled
          say "\n๐Ÿ”” Testing notifications..."
          notification_manager.agent_completed('demo-123', 'backend', 120, { success: true })
        end
      end
    end

    desc 'communicate', 'Manage agent communication and messages'
    option :status, type: :boolean, desc: 'Show communication status'
    option :demo, type: :boolean, desc: 'Demo communication features'
    def communicate
      communicator = AgentCommunicator.instance
      
      if options[:demo]
        say "๐Ÿ’ฌ Demo Agent Communication", :green
        say "Creating demo messages...", :blue
        
        # Create demo messages
        communicator.agent_question('demo-backend', 'Should I use PostgreSQL or MySQL?', 
                                  ['PostgreSQL', 'MySQL', 'SQLite'])
        communicator.agent_status('demo-frontend', 'UI components 60% complete')
        
        say "โœ… Demo messages created", :green
        say "Use 'enhance-swarm communicate --status' to see them"
      else
        # Show status
        pending = communicator.pending_messages
        recent = communicator.recent_messages(5)
        
        say "\n๐Ÿ’ฌ Agent Communication Status:", :blue
        say "  Pending messages: #{pending.count}"
        say "  Recent messages: #{recent.count}"
        
        if pending.any?
          say "\n๐Ÿ“‹ Recent Messages:", :yellow
          pending.first(3).each_with_index do |message, index|
            say "  #{index + 1}. #{message[:type]} from #{message[:role]}"
            say "     #{message[:content][0..60]}..."
          end
        else
          say "  No messages currently"
          say "\nTry: enhance-swarm communicate --demo"
        end
      end
    end

    desc 'suggest', 'Get smart suggestions for next actions'
    option :context, type: :string, desc: 'Additional context for suggestions'
    def suggest
      say "๐Ÿง  Analyzing project and generating smart suggestions...", :blue
      
      context = {
        git_status: { modified_files: 2, untracked_files: 1 },
        project_files: { ruby_files: 15, test_files: 8 },
        user_context: options[:context]
      }
      
      suggestions = SmartDefaults.get_suggestions(context)
      
      if suggestions.empty?
        say "โœ… No suggestions at this time. Your project looks good!", :green
        return
      end
      
      say "\n๐Ÿ’ก Smart Suggestions:\n", :yellow
      
      suggestions.each_with_index do |suggestion, i|
        priority_color = case suggestion[:priority]
                        when :high then :red
                        when :medium then :yellow  
                        when :low then :blue
                        else :white
                        end
        
        say "#{i + 1}. [#{suggestion[:priority].to_s.upcase}] #{suggestion[:description]}", priority_color
        say "   Command: #{suggestion[:command]}", :light_black if suggestion[:command]
        say ""
      end
    end

    desc 'recover', 'Intelligent error recovery and analysis'
    option :analyze, type: :string, desc: 'Analyze specific error message'
    option :stats, type: :boolean, desc: 'Show error recovery statistics'
    option :demo, type: :boolean, desc: 'Demo error recovery features'
    def recover
      error_recovery = ErrorRecovery.instance
      
      if options[:analyze]
        say "๐Ÿ” Analyzing error: #{options[:analyze]}", :blue
        
        test_error = StandardError.new(options[:analyze])
        analysis = error_recovery.analyze_error(test_error, { context: 'cli_demo' })
        
        say "\n๐Ÿ“Š Error Analysis:", :yellow
        say "  Type: #{analysis[:error][:type]}"
        say "  Auto-recoverable: #{analysis[:auto_recoverable] ? 'Yes' : 'No'}"
        say "  Suggestions: #{analysis[:suggestions].count}"
        
        if analysis[:suggestions].any?
          say "\n๐Ÿ’ก Recovery Suggestions:", :green
          analysis[:suggestions].first(3).each_with_index do |suggestion, i|
            description = suggestion['description'] || suggestion[:description]
            say "  #{i + 1}. #{description}"
          end
        end
        
      elsif options[:stats]
        stats = error_recovery.recovery_statistics
        
        say "\n๐Ÿ“Š Error Recovery Statistics:", :blue
        say "  Total errors processed: #{stats[:total_errors_processed]}"
        say "  Successful recoveries: #{stats[:successful_automatic_recoveries]}"
        say "  Success rate: #{stats[:recovery_success_rate]}%"
        say "  Patterns learned: #{stats[:recovery_patterns_learned]}"
        
      elsif options[:demo]
        say "๐Ÿ”ง Demo Error Recovery", :green
        
        # Demo different error types
        errors = [
          'Connection timeout after 30 seconds',
          'No such file or directory - missing.rb',
          'Permission denied accessing /etc/hosts'
        ]
        
        errors.each do |error_msg|
          say "\n๐Ÿ” Analyzing: #{error_msg}", :blue
          test_error = StandardError.new(error_msg)
          analysis = error_recovery.analyze_error(test_error)
          
          say "  Auto-recoverable: #{analysis[:auto_recoverable] ? 'โœ… Yes' : 'โŒ No'}"
          say "  Suggestions: #{analysis[:suggestions].count}"
        end
        
      else
        say "Please specify an action:", :yellow
        say "  --analyze 'error message'  - Analyze specific error"
        say "  --stats                    - Show recovery statistics" 
        say "  --demo                     - Demo error recovery features"
      end
    end

    desc 'troubleshoot', 'Interactive troubleshooting assistant'
    def troubleshoot
      say "๐Ÿ”ง EnhanceSwarm Troubleshooting Assistant", :green
      say "โ”€" * 50, :light_black
      
      # Quick system check
      say "\n๐Ÿ” Quick System Check:", :blue
      
      # Check dependencies
      begin
        require 'thor'
        say "  โœ… Thor gem available"
      rescue LoadError
        say "  โŒ Thor gem missing"
      end
      
      begin
        require 'colorize'
        say "  โœ… Colorize gem available"
      rescue LoadError
        say "  โŒ Colorize gem missing"
      end
      
      # Check git
      git_available = system('git --version > /dev/null 2>&1')
      say "  #{git_available ? 'โœ…' : 'โŒ'} Git #{git_available ? 'available' : 'not found'}"
      
      # Check project structure
      enhance_config = File.exist?('.enhance_swarm.yml')
      say "  #{enhance_config ? 'โœ…' : 'โŒ'} Project config #{enhance_config ? 'found' : 'missing'}"
      
      # Test core classes
      say "\n๐Ÿงช Testing Core Classes:", :blue
      
      classes = %w[NotificationManager VisualDashboard SmartDefaults ErrorRecovery AgentCommunicator]
      classes.each do |cls|
        begin
          klass = EnhanceSwarm.const_get(cls)
          klass.instance if klass.respond_to?(:instance)
          say "  โœ… #{cls} working"
        rescue => e
          say "  โŒ #{cls} error: #{e.message}"
        end
      end
      
      say "\nโœ… Troubleshooting completed!", :green
      say "If issues persist, check the README for setup instructions."
    end

    private

    def discover_running_agents
      # Return demo agents for now since we don't have real agent discovery
      []
    end

    def load_specific_agents(agent_ids)
      agent_ids.map do |id|
        {
          id: id,
          role: 'backend', 
          status: 'running',
          progress: rand(10..90),
          start_time: Time.now - rand(60..3600),
          pid: rand(1000..9999)
        }
      end
    end

    def create_demo_agents
      [
        {
          id: 'backend-auth-123',
          role: 'backend',
          status: 'running', 
          progress: 75,
          start_time: Time.now - 300,
          pid: 1234
        },
        {
          id: 'frontend-ui-456',
          role: 'frontend',
          status: 'completed',
          progress: 100,
          start_time: Time.now - 600,
          pid: 5678
        }
      ]
    end
  end
end