Class: EnhanceSwarm::NotificationManager

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

Constant Summary collapse

NOTIFICATION_TYPES =
{
  agent_completed: { priority: :high, desktop: true, sound: true },
  agent_failed: { priority: :critical, desktop: true, sound: true },
  agent_stuck: { priority: :high, desktop: true, sound: false },
  coordination_complete: { priority: :medium, desktop: true, sound: true },
  intervention_needed: { priority: :critical, desktop: true, sound: true },
  progress_milestone: { priority: :low, desktop: false, sound: false }
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeNotificationManager

Returns a new instance of NotificationManager.



16
17
18
19
20
21
# File 'lib/enhance_swarm/notification_manager.rb', line 16

def initialize
  @enabled = true
  @desktop_notifications = desktop_notifications_available?
  @sound_enabled = sound_available?
  @notification_history = []
end

Instance Method Details

#agent_completed(agent_id, role, duration, details = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/enhance_swarm/notification_manager.rb', line 52

def agent_completed(agent_id, role, duration, details = {})
  message = "๐ŸŽ‰ Agent '#{role}' completed successfully!"
  
  notify(:agent_completed, message, {
    agent_id: agent_id,
    role: role,
    duration: duration,
    **details
  })
  
  if details[:output_path]
    puts "   View: enhance-swarm show #{agent_id}".colorize(:blue)
  end
end

#agent_failed(agent_id, role, error, suggestions = []) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/enhance_swarm/notification_manager.rb', line 67

def agent_failed(agent_id, role, error, suggestions = [])
  message = "โŒ Agent '#{role}' failed: #{error}"
  
  notify(:agent_failed, message, {
    agent_id: agent_id,
    role: role,
    error: error,
    suggestions: suggestions
  })
  
  if suggestions.any?
    puts "\n๐Ÿ’ก Quick fixes:".colorize(:yellow)
    suggestions.each_with_index do |suggestion, index|
      puts "  #{index + 1}. #{suggestion}".colorize(:yellow)
    end
    puts "\nChoose [1-#{suggestions.length}] or [c]ustom command:".colorize(:yellow)
  end
end

#agent_stuck(agent_id, role, last_activity, current_task = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/enhance_swarm/notification_manager.rb', line 86

def agent_stuck(agent_id, role, last_activity, current_task = nil)
  time_stuck = Time.now - last_activity
  time_str = format_duration(time_stuck)
  
  message = "โš ๏ธ Agent '#{role}' stuck for #{time_str}"
  
  notify(:agent_stuck, message, {
    agent_id: agent_id,
    role: role,
    last_activity: last_activity,
    time_stuck: time_stuck,
    current_task: current_task
  })
  
  puts "   Last activity: #{current_task || 'Unknown'}".colorize(:yellow)
  puts "   Action: enhance-swarm restart #{agent_id} [y/N]?".colorize(:blue)
end

#clear_historyObject



180
181
182
# File 'lib/enhance_swarm/notification_manager.rb', line 180

def clear_history
  @notification_history.clear
end

#coordination_complete(summary) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/enhance_swarm/notification_manager.rb', line 104

def coordination_complete(summary)
  total_agents = summary[:completed] + summary[:failed]
  message = "โœ… Coordination complete: #{summary[:completed]}/#{total_agents} agents succeeded"
  
  notify(:coordination_complete, message, summary)
  
  if summary[:failed] > 0
    puts "\nโš ๏ธ #{summary[:failed]} agent(s) failed. Review with: enhance-swarm review".colorize(:yellow)
  end
end

#disable!Object



166
167
168
169
# File 'lib/enhance_swarm/notification_manager.rb', line 166

def disable!
  @enabled = false
  puts "๐Ÿ”‡ Notifications disabled".colorize(:yellow)
end

#enable!Object

Enable/disable notifications



161
162
163
164
# File 'lib/enhance_swarm/notification_manager.rb', line 161

def enable!
  @enabled = true
  puts "โœ… Notifications enabled".colorize(:green)
end

#enabled?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/enhance_swarm/notification_manager.rb', line 171

def enabled?
  @enabled
end

#intervention_needed(reason, agent_id = nil, suggestions = []) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/enhance_swarm/notification_manager.rb', line 115

def intervention_needed(reason, agent_id = nil, suggestions = [])
  message = "๐Ÿšจ Intervention needed: #{reason}"
  
  notify(:intervention_needed, message, {
    reason: reason,
    agent_id: agent_id,
    suggestions: suggestions
  })
  
  if suggestions.any?
    puts "\nRecommended actions:".colorize(:red)
    suggestions.each_with_index do |suggestion, index|
      puts "  #{index + 1}. #{suggestion}".colorize(:red)
    end
  end
end

#notify(type, message, details = {}) ⇒ Object



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
# File 'lib/enhance_swarm/notification_manager.rb', line 23

def notify(type, message, details = {})
  return unless @enabled
  
  notification = build_notification(type, message, details)
  @notification_history << notification
  
  # Console notification (always shown)
  display_console_notification(notification)
  
  # Desktop notification (if available and configured)
  if should_show_desktop?(notification)
    send_desktop_notification(notification)
  end
  
  # Sound notification (if available and configured)
  if should_play_sound?(notification)
    play_notification_sound(notification[:priority])
  end
  
  # Log for automation tools
  Logger.log_operation("notification_#{type}", 'sent', {
    message: message,
    priority: notification[:priority],
    details: details
  })
  
  notification
end

#progress_milestone(milestone, progress_percentage, eta = nil) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/enhance_swarm/notification_manager.rb', line 132

def progress_milestone(milestone, progress_percentage, eta = nil)
  message = "๐Ÿ“ #{milestone} (#{progress_percentage}% complete)"
  
  notify(:progress_milestone, message, {
    milestone: milestone,
    progress: progress_percentage,
    eta: eta
  })
  
  if eta
    puts "   ETA: #{eta.strftime('%H:%M:%S')}".colorize(:blue)
  end
end

#recent_notifications(limit = 10) ⇒ Object

Notification history



176
177
178
# File 'lib/enhance_swarm/notification_manager.rb', line 176

def recent_notifications(limit = 10)
  @notification_history.last(limit)
end

#start_monitoring(agents) ⇒ Object

Background monitoring for stuck agents



147
148
149
150
151
152
153
# File 'lib/enhance_swarm/notification_manager.rb', line 147

def start_monitoring(agents)
  return if @monitoring_thread&.alive?
  
  @monitoring_thread = Thread.new do
    monitor_agents(agents)
  end
end

#stop_monitoringObject



155
156
157
158
# File 'lib/enhance_swarm/notification_manager.rb', line 155

def stop_monitoring
  @monitoring_thread&.kill
  @monitoring_thread = nil
end

#test_notificationsObject

Test notification system



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
# File 'lib/enhance_swarm/notification_manager.rb', line 185

def test_notifications
  puts "๐Ÿ”” Testing notification capabilities...".colorize(:blue)
  
  # Test console notification
  puts "1. Console notifications: โœ… Available"
  
  # Test desktop notifications
  desktop_available = desktop_notifications_available?
  puts "2. Desktop notifications: #{desktop_available ? 'โœ… Available' : 'โŒ Not available'}"
  
  # Test sound notifications  
  sound_available = sound_available?
  puts "3. Sound notifications: #{sound_available ? 'โœ… Available' : 'โŒ Not available'}"
  
  puts "\n๐Ÿงช Running test notifications..."
  sleep(1)
  
  # Test different notification types
  agent_completed('test-123', 'backend', 120, { success: true })
  sleep(1)
  
  progress_milestone('Test milestone reached', 75)
  sleep(1)
  
  puts "\n๐Ÿ“Š Test Results:"
  puts "  Notifications sent: 2"
  puts "  History entries: #{@notification_history.count}"
  puts "  Status: #{@enabled ? 'Enabled' : 'Disabled'}"
  
  if desktop_available || sound_available
    puts "\nNote: Desktop/sound notifications may appear with a delay"
  end
  
  puts "\nโœ… Notification test completed!"
end