-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_monitor.rs
More file actions
173 lines (154 loc) · 5.17 KB
/
web_monitor.rs
File metadata and controls
173 lines (154 loc) · 5.17 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
//! Web monitor for offline‑first multi‑agent autonomy SDK.
//!
//! This example runs a simple web server that displays connected agents and their state.
use warp::Filter;
use std::sync::Arc;
use tokio::sync::RwLock;
use serde_json::json;
use std::collections::HashMap;
use common::types::AgentId;
use mesh_transport::{MeshTransport, MeshTransportConfig};
use state_sync::StateSync;
use agent_core::Agent;
use std::net::SocketAddr;
/// Shared state for the web server.
#[derive(Clone)]
struct AppState {
/// Map from agent ID to agent info.
agents: Arc<RwLock<HashMap<u64, AgentInfo>>>,
}
#[derive(Clone, serde::Serialize)]
struct AgentInfo {
id: u64,
peers: Vec<u64>,
key_count: usize,
last_seen: std::time::SystemTime,
}
impl AppState {
fn new() -> Self {
Self {
agents: Arc::new(RwLock::new(HashMap::new())),
}
}
async fn update_agent(&self, id: u64, peers: Vec<u64>, key_count: usize) {
let mut map = self.agents.write().await;
map.insert(id, AgentInfo {
id,
peers,
key_count,
last_seen: std::time::SystemTime::now(),
});
}
}
/// Start a simple agent that periodically broadcasts changes.
async fn run_agent(agent_id: u64, state: AppState) -> anyhow::Result<()> {
let config = MeshTransportConfig {
local_agent_id: AgentId(agent_id),
use_in_memory: true, // Use in‑memory backend for demo
..Default::default()
};
let mut agent = Agent::new(AgentId(agent_id), config)?;
agent.start()?;
// Simulate some changes
agent.set_value("demo_key", json!("demo_value"))?;
loop {
// Update web state
let peers = agent.transport().peers();
let key_count = agent.state().map().len();
state.update_agent(agent_id, peers.iter().map(|p| p.agent_id.0).collect(), key_count).await;
// Broadcast changes every 5 seconds
agent.broadcast_changes().await?;
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let state = AppState::new();
// Start a few demo agents in the background
let state1 = state.clone();
tokio::spawn(async move {
if let Err(e) = run_agent(1, state1).await {
eprintln!("Agent 1 error: {}", e);
}
});
let state2 = state.clone();
tokio::spawn(async move {
if let Err(e) = run_agent(2, state2).await {
eprintln!("Agent 2 error: {}", e);
}
});
let state3 = state.clone();
tokio::spawn(async move {
if let Err(e) = run_agent(3, state3).await {
eprintln!("Agent 3 error: {}", e);
}
});
// Define web routes
let state_filter = warp::any().map(move || state.clone());
// GET /agents returns JSON list of agents
let agents_route = warp::path("agents")
.and(warp::get())
.and(state_filter.clone())
.and_then(|state: AppState| async move {
let agents = state.agents.read().await;
let list: Vec<AgentInfo> = agents.values().cloned().collect();
Ok::<_, warp::Rejection>(warp::reply::json(&list))
});
// GET / serves a simple HTML page
let index_route = warp::path::end()
.and(warp::get())
.map(|| {
warp::reply::html(INDEX_HTML)
});
// Combine routes
let routes = index_route
.or(agents_route)
.with(warp::cors().allow_any_origin());
let addr: SocketAddr = "127.0.0.1:3030".parse()?;
println!("Web monitor listening on http://{}", addr);
warp::serve(routes).run(addr).await;
Ok(())
}
const INDEX_HTML: &str = r#"
<!DOCTYPE html>
<html>
<head>
<title>Offline‑First Multi‑Agent Autonomy SDK Monitor</title>
<style>
body { font-family: sans-serif; margin: 2em; }
h1 { color: #333; }
.agent { border: 1px solid #ccc; border-radius: 5px; padding: 1em; margin: 1em 0; }
.agent-id { font-weight: bold; font-size: 1.2em; }
.peers { margin-left: 1em; }
.key-count { color: green; }
.last-seen { color: gray; font-size: 0.9em; }
</style>
</head>
<body>
<h1>Agent Monitor</h1>
<div id="agents"></div>
<script>
async function fetchAgents() {
const resp = await fetch('/agents');
const agents = await resp.json();
const container = document.getElementById('agents');
container.innerHTML = '';
agents.forEach(agent => {
const div = document.createElement('div');
div.className = 'agent';
div.innerHTML = `
<div class="agent-id">Agent ${agent.id}</div>
<div class="peers">Peers: ${agent.peers.join(', ') || 'none'}</div>
<div class="key-count">Keys in CRDT: ${agent.key_count}</div>
<div class="last-seen">Last seen: ${new Date(agent.last_seen).toLocaleTimeString()}</div>
`;
container.appendChild(div);
});
}
setInterval(fetchAgents, 2000);
fetchAgents();
</script>
</body>
</html>
"#;