openclaw-rs
High-performance sovereign agent runtime in native Rust and Mojo for NVIDIA DGX Spark
Brought by drakestapleton
openclaw-rs / src / main.rs
| 1 | use clap::{Parser, Subcommand}; |
| 2 | use openclaw::{ |
| 3 | start_gateway, AgentEngine, Database, GatewayState, HeartbeatEngine, InferenceEngine, |
| 4 | MojoSimdBridge, SkillRegistry, |
| 5 | }; |
| 6 | use std::path::Path; |
| 7 | use std::sync::Arc; |
| 8 | use std::time::Instant; |
| 9 | use tracing::{info, Level}; |
| 10 | use tracing_subscriber::FmtSubscriber; |
| 11 | |
| 12 | #[derive(Parser)] |
| 13 | #[command(name = "openclaw")] |
| 14 | #[command(version = "0.2.0")] |
| 15 | #[command(about = "Ultra-high-performance sovereign agent runtime in Rust, Mojo, and Modular MAX", long_about = None)] |
| 16 | struct Cli { |
| 17 | #[command(subcommand)] |
| 18 | command: Option<Commands>, |
| 19 | } |
| 20 | |
| 21 | #[derive(Subcommand)] |
| 22 | enum Commands { |
| 23 | /// Start the Axum WebSocket, SSE, and HTTP gateway with autonomous heartbeat |
| 24 | Serve { |
| 25 | #[arg(short, long, default_value = "18096")] |
| 26 | port: u16, |
| 27 | #[arg(long, default_value = "http://127.0.0.1:18006/v1/chat/completions")] |
| 28 | max_url: String, |
| 29 | #[arg(long, default_value = "openclaw.sqlite")] |
| 30 | db_path: String, |
| 31 | #[arg(long, default_value = "60")] |
| 32 | heartbeat_secs: u64, |
| 33 | }, |
| 34 | /// Run a single heartbeat tick immediately |
| 35 | Tick { |
| 36 | #[arg(long, default_value = "openclaw.sqlite")] |
| 37 | db_path: String, |
| 38 | }, |
| 39 | /// Run persistent background heartbeat daemon loop |
| 40 | Heartbeat { |
| 41 | #[arg(long, default_value = "http://127.0.0.1:18006/v1/chat/completions")] |
| 42 | max_url: String, |
| 43 | #[arg(long, default_value = "openclaw.sqlite")] |
| 44 | db_path: String, |
| 45 | #[arg(long, default_value = "30")] |
| 46 | heartbeat_secs: u64, |
| 47 | }, |
| 48 | /// Send a direct prompt to local Modular MAX inference engine |
| 49 | Ask { |
| 50 | prompt: String, |
| 51 | #[arg(long, default_value = "http://127.0.0.1:18006/v1/chat/completions")] |
| 52 | max_url: String, |
| 53 | }, |
| 54 | /// Execute an autonomous agent task with multi-turn tool calling |
| 55 | Agent { |
| 56 | prompt: String, |
| 57 | #[arg(long, default_value = "http://127.0.0.1:18006/v1/chat/completions")] |
| 58 | max_url: String, |
| 59 | #[arg(long, default_value = "openclaw.sqlite")] |
| 60 | db_path: String, |
| 61 | #[arg(long, default_value = "8")] |
| 62 | max_turns: usize, |
| 63 | }, |
| 64 | /// Evaluate Mojo SIMD vector operations |
| 65 | Simd { |
| 66 | #[arg(long, default_value = "10")] |
| 67 | steps: i32, |
| 68 | }, |
| 69 | /// Display runtime status and system health |
| 70 | Status { |
| 71 | #[arg(long, default_value = "http://127.0.0.1:18006/v1/chat/completions")] |
| 72 | max_url: String, |
| 73 | #[arg(long, default_value = "openclaw.sqlite")] |
| 74 | db_path: String, |
| 75 | }, |
| 76 | } |
| 77 | |
| 78 | #[tokio::main] |
| 79 | async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 80 | let subscriber = FmtSubscriber::builder() |
| 81 | .with_max_level(Level::INFO) |
| 82 | .finish(); |
| 83 | tracing::subscriber::set_global_default(subscriber).ok(); |
| 84 | |
| 85 | let cli = Cli::parse(); |
| 86 | |
| 87 | match cli.command.unwrap_or(Commands::Serve { |
| 88 | port: 18096, |
| 89 | max_url: "http://127.0.0.1:18006/v1/chat/completions".to_string(), |
| 90 | db_path: "openclaw.sqlite".to_string(), |
| 91 | heartbeat_secs: 60, |
| 92 | }) { |
| 93 | Commands::Serve { |
| 94 | port, |
| 95 | max_url, |
| 96 | db_path, |
| 97 | heartbeat_secs, |
| 98 | } => { |
| 99 | info!("Initializing OpenClaw engine on Grace Blackwell GB10..."); |
| 100 | let db = Arc::new(Database::open(Path::new(&db_path))?); |
| 101 | let inference = Arc::new(InferenceEngine::new(Some(max_url), None)); |
| 102 | let heartbeat = Arc::new(HeartbeatEngine::with_inference( |
| 103 | heartbeat_secs, |
| 104 | db.clone(), |
| 105 | inference.clone(), |
| 106 | )); |
| 107 | let skills = Arc::new(SkillRegistry::new()); |
| 108 | let agent = Arc::new(AgentEngine::new( |
| 109 | inference.clone(), |
| 110 | skills.clone(), |
| 111 | Some(db.clone()), |
| 112 | )); |
| 113 | |
| 114 | heartbeat.clone().start_loop().await; |
| 115 | |
| 116 | let state = GatewayState { |
| 117 | start_time: Instant::now(), |
| 118 | db: db.clone(), |
| 119 | inference: inference.clone(), |
| 120 | heartbeat: heartbeat.clone(), |
| 121 | skills: skills.clone(), |
| 122 | agent: agent.clone(), |
| 123 | }; |
| 124 | |
| 125 | info!("Starting Axum sub-millisecond gateway on port {}", port); |
| 126 | start_gateway(state, port).await?; |
| 127 | } |
| 128 | Commands::Tick { db_path } => { |
| 129 | info!("Executing single autonomous heartbeat tick..."); |
| 130 | let db = Arc::new(Database::open(Path::new(&db_path))?); |
| 131 | let heartbeat = HeartbeatEngine::new(60, db); |
| 132 | let receipt = heartbeat.pulse_once().await; |
| 133 | info!("Heartbeat tick completed successfully: {:?}", receipt.notes); |
| 134 | } |
| 135 | Commands::Heartbeat { |
| 136 | max_url, |
| 137 | db_path, |
| 138 | heartbeat_secs, |
| 139 | } => { |
| 140 | info!("Initializing OpenClaw persistent heartbeat daemon on Grace Blackwell GB10..."); |
| 141 | let db = Arc::new(Database::open(Path::new(&db_path))?); |
| 142 | let inference = Arc::new(InferenceEngine::new(Some(max_url), None)); |
| 143 | let heartbeat = Arc::new(HeartbeatEngine::with_inference( |
| 144 | heartbeat_secs, |
| 145 | db.clone(), |
| 146 | inference.clone(), |
| 147 | )); |
| 148 | |
| 149 | info!( |
| 150 | "Starting OpenClaw persistent heartbeat daemon loop (interval: {}s)...", |
| 151 | heartbeat_secs |
| 152 | ); |
| 153 | let mut interval = tokio::time::interval(std::time::Duration::from_secs(heartbeat_secs)); |
| 154 | loop { |
| 155 | interval.tick().await; |
| 156 | let receipt = heartbeat.pulse_once().await; |
| 157 | info!( |
| 158 | "Heartbeat pulse #{}: status={}, tasks={}, actions={}", |
| 159 | receipt.tick_id, receipt.status, receipt.tasks_scanned, receipt.actions_dispatched |
| 160 | ); |
| 161 | } |
| 162 | } |
| 163 | Commands::Ask { prompt, max_url } => { |
| 164 | let inference = InferenceEngine::new(Some(max_url), None); |
| 165 | println!("Dispatching query to Modular MAX: {}", prompt); |
| 166 | match inference.generate(&prompt, None, None).await { |
| 167 | Ok(reply) => println!("\n{}", reply), |
| 168 | Err(err) => eprintln!("Inference error: {}", err), |
| 169 | } |
| 170 | } |
| 171 | Commands::Agent { |
| 172 | prompt, |
| 173 | max_url, |
| 174 | db_path, |
| 175 | max_turns, |
| 176 | } => { |
| 177 | println!("=== OpenClaw Sovereign Agent Execution ==="); |
| 178 | println!("Task Goal: {}", prompt); |
| 179 | println!("Target Engine: Modular MAX on Grace Blackwell GB10"); |
| 180 | println!("MAX Endpoint: {}", max_url); |
| 181 | println!("------------------------------------------------------------"); |
| 182 | |
| 183 | let db = Arc::new(Database::open(Path::new(&db_path))?); |
| 184 | let inference = Arc::new(InferenceEngine::new(Some(max_url), None)); |
| 185 | let skills = Arc::new(SkillRegistry::new()); |
| 186 | let agent = AgentEngine::new(inference, skills, Some(db)); |
| 187 | |
| 188 | match agent.execute_task(&prompt, None, max_turns).await { |
| 189 | Ok(result) => { |
| 190 | for step in &result.steps { |
| 191 | println!("\n[Step {}]", step.step_index); |
| 192 | if let Some(ref thought) = step.thought { |
| 193 | let first_line = thought.lines().next().unwrap_or(""); |
| 194 | println!(" Thought: {}", first_line); |
| 195 | } |
| 196 | for tc in &step.tool_calls { |
| 197 | println!(" Tool Call: {}({})", tc.tool_name, tc.arguments); |
| 198 | let preview: String = |
| 199 | tc.output.lines().take(3).collect::<Vec<_>>().join("\n "); |
| 200 | println!(" Result (success={}):\n {}", tc.success, preview); |
| 201 | } |
| 202 | } |
| 203 | println!("\n=================== Terminal Output ==================="); |
| 204 | println!("{}", result.final_response); |
| 205 | println!("======================================================="); |
| 206 | println!( |
| 207 | "Total Turns: {} | Duration: {}ms | Completed: {}", |
| 208 | result.turns_taken, result.duration_ms, result.completed |
| 209 | ); |
| 210 | } |
| 211 | Err(err) => eprintln!("Agent execution error: {}", err), |
| 212 | } |
| 213 | } |
| 214 | Commands::Simd { steps } => { |
| 215 | println!("=== Mojo SIMD Vector Evaluation ==="); |
| 216 | let version = MojoSimdBridge::version(); |
| 217 | let is_accel = MojoSimdBridge::is_mojo_accelerated(); |
| 218 | println!( |
| 219 | "Mojo SIMD Status: {}", |
| 220 | if is_accel { |
| 221 | "ACCELERATED (C-ABI .so)" |
| 222 | } else { |
| 223 | "NATIVE FALLBACK" |
| 224 | } |
| 225 | ); |
| 226 | println!("Kernel Version: {}", version); |
| 227 | |
| 228 | let v1 = [1.0f32, 0.0, 0.0, 0.0]; |
| 229 | let v2 = [0.9f32, 0.1, 0.0, 0.0]; |
| 230 | let sim = MojoSimdBridge::cosine_similarity_4d(v1, v2); |
| 231 | println!("Cosine Similarity ([1,0,0,0] vs [0.9,0.1,0,0]): {:.6}", sim); |
| 232 | |
| 233 | let acc = MojoSimdBridge::simd_accumulate(1.0, 1.05, steps); |
| 234 | println!( |
| 235 | "SIMD Accumulate (base=1.0, scale=1.05, steps={}): {:.6}", |
| 236 | steps, acc |
| 237 | ); |
| 238 | |
| 239 | let entropy = MojoSimdBridge::token_entropy([0.7, 0.2, 0.08, 0.02]); |
| 240 | println!("Token Entropy Proxy: {:.6}", entropy); |
| 241 | |
| 242 | let proj = |
| 243 | MojoSimdBridge::token_projection([1.0, 2.0, 3.0, 4.0], [0.5, 0.5, 0.5, 0.5], 1.0); |
| 244 | println!("Token Projection: {:.6}", proj); |
| 245 | } |
| 246 | Commands::Status { max_url, db_path } => { |
| 247 | println!("=== OpenClaw Sovereign Runtime Status ==="); |
| 248 | println!("Architecture: Pure native Rust + Mojo 1.1 + Modular MAX"); |
| 249 | println!("Hardware Target: Grace Blackwell GB10 (aarch64-unknown-linux-gnu)"); |
| 250 | println!("Database: SQLite WAL ({})", db_path); |
| 251 | println!("MAX Endpoint: {}", max_url); |
| 252 | let inference = InferenceEngine::new(Some(max_url), None); |
| 253 | let healthy = inference.check_health().await; |
| 254 | println!( |
| 255 | "MAX Engine Status: {}", |
| 256 | if healthy { |
| 257 | "ONLINE (GB10 Local)" |
| 258 | } else { |
| 259 | "OFFLINE (Fallback Active)" |
| 260 | } |
| 261 | ); |
| 262 | println!( |
| 263 | "Mojo SIMD Acceleration: {}", |
| 264 | if MojoSimdBridge::is_mojo_accelerated() { |
| 265 | "ACTIVE" |
| 266 | } else { |
| 267 | "NATIVE_FALLBACK" |
| 268 | } |
| 269 | ); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | Ok(()) |
| 274 | } |