-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdashboard_demo.py
More file actions
295 lines (242 loc) · 10.3 KB
/
dashboard_demo.py
File metadata and controls
295 lines (242 loc) · 10.3 KB
1
2
3
4
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
"""Dashboard Demo - Generate Test Data and Run Dashboard.
This script generates sample data for all 6 patterns and runs the dashboard
so you can see it in action.
Copyright 2025 Smart-AI-Memory
Licensed under Fair Source License 0.9
"""
import random
import threading
import time
from pathlib import Path
# Load environment variables from .env file
try:
from dotenv import load_dotenv
# Load .env from project root
env_path = Path(__file__).parent.parent / ".env"
if env_path.exists():
load_dotenv(env_path)
except ImportError:
pass # dotenv not installed, will use system environment
from empathy_os.telemetry import (
ApprovalGate,
CoordinationSignals,
EventStreamer,
FeedbackLoop,
HeartbeatCoordinator,
)
from empathy_os.telemetry.feedback_loop import ModelTier
def keep_agents_alive(memory):
"""Continuously publish heartbeats to keep agents visible in dashboard.
Runs in background thread, updating agent heartbeats every 3 seconds.
"""
agent_configs = {
"Code Analyzer": {"tasks": ["Analyzing code quality", "Running static analysis", "Checking dependencies"]},
"Test Generator": {"tasks": ["Generating unit tests", "Creating test fixtures", "Validating coverage"]},
"Refactoring Agent": {"tasks": ["Refactoring code", "Optimizing performance", "Improving readability"]},
"CI/CD Pipeline": {"tasks": ["Running CI/CD pipeline", "Deploying to staging", "Idle - awaiting tasks"]},
"System Monitor": {"tasks": ["Monitoring system health", "Processing metrics", "Generating reports"]},
}
coordinators = {}
for agent_id, config in agent_configs.items():
coordinator = HeartbeatCoordinator(memory=memory)
coordinator.start_heartbeat(
agent_id=f"agent-{list(agent_configs.keys()).index(agent_id) + 1}", # Internal ID
display_name=agent_id, # Display name for dashboard
metadata={"demo": True, "persistent": True}
)
coordinators[agent_id] = coordinator
while True:
for agent_id, config in agent_configs.items():
coordinator = coordinators[agent_id]
# Randomly vary status and progress for realistic simulation
status = random.choice(["running", "running", "running", "idle"]) # Bias toward "running"
progress = random.random()
current_task = random.choice(config["tasks"])
coordinator.beat(
status=status,
progress=progress,
current_task=current_task
)
time.sleep(3) # Update heartbeats every 3 seconds
def generate_test_data():
"""Generate test data for all dashboard patterns."""
print("=" * 70)
print("GENERATING TEST DATA FOR DASHBOARD")
print("=" * 70)
print()
# Initialize Redis memory backend for all patterns
try:
from empathy_os.memory.short_term import RedisShortTermMemory
memory = RedisShortTermMemory()
print("✅ Connected to Redis memory backend")
print()
except Exception as e:
print(f"❌ Failed to connect to Redis: {e}")
print(" Make sure Redis is running and REDIS_ENABLED=true in .env")
print()
return None
# Pattern 1: Agent Heartbeats (continuous in background)
print("📊 Pattern 1: Setting up continuous agent heartbeats...")
print(" ✓ 5 agents will publish heartbeats every 3 seconds")
print(" ✓ Agents: Code Analyzer, Test Generator, Refactoring Agent, CI/CD Pipeline, System Monitor")
print()
# Pattern 2: Coordination Signals
print("📡 Pattern 2: Creating test coordination signals...")
for i in range(10):
source = f"agent-{random.randint(1, 5)}"
target = f"agent-{random.randint(1, 5)}"
signal_types = ["status_update", "task_complete", "request_help", "acknowledge"]
signals = CoordinationSignals(memory=memory, agent_id=source)
signals.signal(
signal_type=random.choice(signal_types),
source_agent=source,
target_agent=target,
payload={"message": f"Test signal {i+1}", "demo": True},
ttl_seconds=3600, # 1 hour TTL for demo (default is 5 minutes)
)
print(f" ✓ Signal: {source} → {target}")
print()
# Pattern 4: Event Streaming
print("📤 Pattern 4: Creating test events...")
streamer = EventStreamer(memory=memory)
for i in range(15):
event_types = ["workflow_progress", "agent_heartbeat", "coordination_signal"]
workflows = ["code-review", "test-generation", "refactoring"]
streamer.publish_event(
event_type=random.choice(event_types),
data={
"workflow": random.choice(workflows),
"stage": random.choice(["analysis", "generation", "validation"]),
"progress": random.random(),
"demo": True,
},
source=f"agent-{random.randint(1, 5)}",
)
print(f" ✓ Event {i+1} published")
print()
# Pattern 5: Approval Requests (create directly without blocking)
print("✋ Pattern 5: Creating test approval requests...")
# Create approval requests directly in Redis without blocking
gate = ApprovalGate(memory=memory, agent_id="demo-workflow")
# Manually store 2 approval requests without the blocking wait
from uuid import uuid4
from datetime import datetime
import json
for i in range(2):
request_id = f"approval_{uuid4().hex[:8]}"
approval_data = {
"request_id": request_id,
"approval_type": random.choice(["deploy_to_staging", "delete_old_data", "refactor_module"]),
"agent_id": "demo-workflow",
"context": {"version": "1.0.0", "demo": True, "number": i+1},
"timestamp": datetime.utcnow().isoformat(),
"timeout_seconds": 300.0,
"status": "pending"
}
# Store in Redis (without empathy: prefix for approval gates)
request_key = f"approval_request:{request_id}"
memory._client.setex(request_key, 360, json.dumps(approval_data))
print(f" ✓ Approval request {i+1}: {approval_data['approval_type']}")
print()
# Pattern 6: Quality Feedback
print("📊 Pattern 6: Creating test quality feedback...")
feedback = FeedbackLoop(memory=memory)
workflows = ["code-review", "test-generation", "refactoring"]
stages = ["analysis", "generation", "validation"]
tiers = [ModelTier.CHEAP, ModelTier.CAPABLE, ModelTier.PREMIUM]
for workflow in workflows:
for stage in stages:
for tier in tiers:
# Generate 10-15 samples per combination
num_samples = random.randint(10, 15)
for i in range(num_samples):
# Vary quality by tier
if tier == ModelTier.CHEAP:
base_quality = 0.65
elif tier == ModelTier.CAPABLE:
base_quality = 0.80
else: # PREMIUM
base_quality = 0.90
# Add some randomness
quality = base_quality + (random.random() * 0.15 - 0.075)
quality = max(0.0, min(1.0, quality)) # Clamp to 0-1
feedback.record_feedback(
workflow_name=workflow,
stage_name=stage,
tier=tier,
quality_score=quality,
metadata={"demo": True, "tokens": random.randint(50, 300)},
)
print(f" ✓ {workflow}/{stage}/{tier.value}: {num_samples} samples")
print()
print("=" * 70)
print("✅ TEST DATA GENERATION COMPLETE")
print("=" * 70)
print()
return memory
def run_dashboard_demo():
"""Run dashboard demo with test data."""
print("\n")
print("╔" + "=" * 68 + "╗")
print("║" + " " * 17 + "AGENT COORDINATION DASHBOARD DEMO" + " " * 18 + "║")
print("╚" + "=" * 68 + "╝")
print()
print("This demo will:")
print(" 1. Generate test data for all 6 patterns")
print(" 2. Start the dashboard web server")
print(" 3. Open your browser to view the dashboard")
print()
# Generate test data
try:
memory = generate_test_data()
if memory is None:
return # Failed to connect to Redis
except Exception as e:
print(f"\n❌ Failed to generate test data: {e}")
print(" Make sure Redis is running: redis-server")
print(" Or run: empathy memory start")
return
# Start background thread for continuous agent heartbeats
heartbeat_thread = threading.Thread(target=keep_agents_alive, args=(memory,), daemon=True)
heartbeat_thread.start()
print("🔄 Background thread started: Agents will remain active")
print()
# Start dashboard
print()
print("=" * 70)
print("STARTING DASHBOARD SERVER")
print("=" * 70)
print()
print("📊 Dashboard will be available at: http://localhost:8000")
print()
print("💡 What you'll see:")
print(" • 5 active agents with live heartbeats (updating every 3s)")
print(" • 10 coordination signals (Pattern 2)")
print(" • 15 stream events (Pattern 4)")
print(" • 2 pending approval requests (Pattern 5)")
print(" • Quality metrics for 3 workflows × 3 stages × 3 tiers (Pattern 6)")
print()
print("🔄 Dashboard auto-refreshes every 5 seconds")
print("💓 Agent heartbeats update continuously in background")
print()
print("Press Ctrl+C to stop the server")
print("=" * 70)
print()
# Import and run dashboard
try:
# Try simple server first (no dependencies)
from empathy_os.dashboard import run_simple_dashboard
run_simple_dashboard(host="127.0.0.1", port=8000)
except KeyboardInterrupt:
print("\n\n🛑 Dashboard stopped")
print()
except Exception as e:
print(f"\n❌ Failed to start dashboard: {e}")
print()
print("📖 Troubleshooting:")
print(" • Ensure Redis is running: redis-server")
print(" • Check if port 8000 is available")
print(" • For FastAPI version: pip install fastapi uvicorn")
print()
if __name__ == "__main__":
run_dashboard_demo()