synaptic-mesh-cli 0.1.1

CLI library for Synaptic Neural Mesh - complete integration with Synaptic Market
Documentation
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//! Synaptic Mesh CLI Library - Complete integration of all components
//!
//! This library provides the command-line interface and programmatic API
//! for the entire Synaptic Neural Mesh ecosystem.

use clap::{Parser, Subcommand};
use anyhow::Result;
use serde::{Serialize, Deserialize};
use synaptic_qudag_core::QuDAGNetwork;
use synaptic_neural_wasm::{NeuralNetwork, Layer};
use synaptic_neural_mesh::{NeuralMesh, Agent};
use synaptic_daa_swarm::{Swarm, SwarmBehavior};
use claude_market::{ClaudeMarket, MarketConfig};

/// Synaptic Mesh CLI
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,
}

/// Available commands
#[derive(Subcommand, Debug)]
pub enum Commands {
    /// Node operations
    Node {
        #[command(subcommand)]
        action: NodeAction,
    },
    /// Swarm operations
    Swarm {
        #[command(subcommand)]
        action: SwarmAction,
    },
    /// Neural network operations
    Neural {
        #[command(subcommand)]
        action: NeuralAction,
    },
    /// Mesh operations
    Mesh {
        #[command(subcommand)]
        action: MeshAction,
    },
    /// Market operations
    Market {
        #[command(subcommand)]
        action: MarketAction,
    },
    /// Wallet operations
    Wallet {
        #[command(subcommand)]
        action: WalletAction,
    },
    /// Show status
    Status,
}

/// Node actions
#[derive(Subcommand, Debug)]
pub enum NodeAction {
    /// Start a node
    Start {
        #[arg(short, long, default_value = "8080")]
        port: u16,
    },
    /// Stop a node
    Stop,
    /// List nodes
    List,
}

/// Swarm actions
#[derive(Subcommand, Debug)]
pub enum SwarmAction {
    /// Create a swarm
    Create {
        #[arg(short, long, default_value = "10")]
        agents: usize,
        #[arg(short, long)]
        behavior: Option<String>,
    },
    /// Run swarm
    Run {
        #[arg(short, long)]
        id: Option<String>,
    },
    /// List swarms
    List,
}

/// Neural network actions
#[derive(Subcommand, Debug)]
pub enum NeuralAction {
    /// Create a neural network
    Create {
        #[arg(short, long)]
        layers: Vec<usize>,
        #[arg(short, long)]
        output: String,
    },
    /// Train a model
    Train {
        #[arg(short, long)]
        model: String,
        #[arg(short, long)]
        data: String,
    },
    /// Predict with a model
    Predict {
        #[arg(short, long)]
        model: String,
        #[arg(short, long)]
        input: Vec<f32>,
    },
}

/// Mesh actions
#[derive(Subcommand, Debug)]
pub enum MeshAction {
    /// Show mesh info
    Info,
    /// Add agent
    AddAgent {
        #[arg(short, long)]
        name: String,
    },
    /// Submit task
    SubmitTask {
        #[arg(short, long)]
        name: String,
        #[arg(short, long)]
        compute: f64,
    },
}

/// Market actions
#[derive(Subcommand, Debug)]
pub enum MarketAction {
    /// Initialize market
    Init {
        #[arg(short, long)]
        db_path: Option<String>,
    },
    /// Create capacity offer
    Offer {
        #[arg(short, long)]
        slots: u64,
        #[arg(short, long)]
        price: u64,
        #[arg(long)]
        opt_in: bool,
    },
    /// Submit capacity bid
    Bid {
        #[arg(short, long)]
        task: String,
        #[arg(short, long)]
        max_price: u64,
    },
    /// Show market status
    Status {
        #[arg(short, long)]
        detailed: bool,
    },
    /// View terms and compliance
    Terms,
}

/// Wallet actions
#[derive(Subcommand, Debug)]
pub enum WalletAction {
    /// Show balance
    Balance,
    /// Transfer tokens
    Transfer {
        #[arg(short, long)]
        to: String,
        #[arg(short, long)]
        amount: u64,
        #[arg(short, long)]
        memo: Option<String>,
    },
    /// Show transaction history
    History {
        #[arg(short, long, default_value = "10")]
        limit: usize,
    },
}

/// Mesh command for programmatic use
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MeshCommand {
    NodeStart { port: u16 },
    NodeStop,
    NodeList,
    SwarmCreate { agents: usize, behavior: Option<SwarmBehavior> },
    SwarmRun { id: Option<String> },
    SwarmList,
    NeuralCreate { layers: Vec<usize>, output: String },
    NeuralTrain { model: String, data: String },
    NeuralPredict { model: String, input: Vec<f32> },
    MeshInfo,
    MeshAddAgent { name: String },
    MeshSubmitTask { name: String, compute: f64 },
    MarketInit { db_path: Option<String> },
    MarketOffer { slots: u64, price: u64, opt_in: bool },
    MarketBid { task: String, max_price: u64 },
    MarketStatus { detailed: bool },
    MarketTerms,
    WalletBalance,
    WalletTransfer { to: String, amount: u64, memo: Option<String> },
    WalletHistory { limit: usize },
    Status,
}

/// Execute a mesh command
pub async fn execute_command(command: MeshCommand) -> Result<CommandResult> {
    match command {
        MeshCommand::NodeStart { port } => {
            // Start a QuDAG node
            let _network = QuDAGNetwork::new();
            Ok(CommandResult::NodeStarted { port, id: "node-1".to_string() })
        }
        
        MeshCommand::NodeStop => {
            Ok(CommandResult::NodeStopped)
        }
        
        MeshCommand::NodeList => {
            Ok(CommandResult::NodeList { nodes: vec![] })
        }
        
        MeshCommand::SwarmCreate { agents, behavior } => {
            let swarm = Swarm::new();
            if let Some(b) = behavior {
                swarm.add_behavior(b);
            }
            swarm.initialize(agents).await;
            Ok(CommandResult::SwarmCreated { id: "swarm-1".to_string(), agents })
        }
        
        MeshCommand::SwarmRun { id } => {
            // In real implementation, would look up swarm by ID
            let swarm = Swarm::new();
            swarm.initialize(10).await;
            // Don't actually run the infinite loop in the library
            Ok(CommandResult::SwarmRunning { id: id.unwrap_or("swarm-1".to_string()) })
        }
        
        MeshCommand::SwarmList => {
            Ok(CommandResult::SwarmList { swarms: vec![] })
        }
        
        MeshCommand::NeuralCreate { layers, output } => {
            let mut network = NeuralNetwork::new();
            
            // Create layers
            for i in 0..layers.len() - 1 {
                let layer = Layer::dense(layers[i], layers[i + 1]);
                network.add_layer(layer);
            }
            
            // Save to file
            let json = network.to_json()?;
            std::fs::write(&output, json)?;
            
            Ok(CommandResult::NeuralCreated { path: output })
        }
        
        MeshCommand::NeuralTrain { model, data: _ } => {
            // Load model and data
            let model_json = std::fs::read_to_string(&model)?;
            let _network = NeuralNetwork::from_json(&model_json)?;
            
            // Training would happen here
            Ok(CommandResult::NeuralTrained { model, epochs: 100 })
        }
        
        MeshCommand::NeuralPredict { model, input } => {
            let model_json = std::fs::read_to_string(&model)?;
            let network = NeuralNetwork::from_json(&model_json)?;
            
            // Predict returns JSON string in WASM version
            let output_json = network.predict(&input)
                .map_err(|e| anyhow::anyhow!("Prediction failed: {:?}", e))?;
            let output: Vec<f32> = serde_json::from_str(&output_json)?;
            
            Ok(CommandResult::NeuralPrediction { output })
        }
        
        MeshCommand::MeshInfo => {
            let mesh = NeuralMesh::new();
            let stats = mesh.get_stats();
            Ok(CommandResult::MeshInfo { 
                agents: stats.total_agents,
                tasks: stats.total_tasks,
            })
        }
        
        MeshCommand::MeshAddAgent { name } => {
            let mesh = NeuralMesh::new();
            let agent = Agent::new(&name);
            let id = mesh.add_agent(agent).await?;
            Ok(CommandResult::AgentAdded { id: id.to_string(), name })
        }
        
        MeshCommand::MeshSubmitTask { name, compute } => {
            let mesh = NeuralMesh::new();
            let requirements = synaptic_neural_mesh::TaskRequirements {
                min_compute_power: compute,
                min_memory: 1024 * 1024,
                required_specializations: vec!["general".to_string()],
                max_latency_ms: 100.0,
            };
            let id = mesh.submit_task(&name, requirements).await?;
            Ok(CommandResult::TaskSubmitted { id: id.to_string(), name })
        }
        
        MeshCommand::MarketInit { db_path } => {
            let config = MarketConfig {
                db_path: db_path.clone(),
                ..Default::default()
            };
            let _market = ClaudeMarket::new(config).await?;
            Ok(CommandResult::MarketInitialized { 
                db_path: db_path.unwrap_or("claude_market.db".to_string()) 
            })
        }
        
        MeshCommand::MarketOffer { slots, price, opt_in } => {
            if !opt_in {
                return Err(anyhow::anyhow!("Market participation requires explicit opt-in with --opt-in flag"));
            }
            // In real implementation, would create actual offer
            Ok(CommandResult::MarketOfferCreated { slots, price })
        }
        
        MeshCommand::MarketBid { task, max_price } => {
            // In real implementation, would submit actual bid
            Ok(CommandResult::MarketBidSubmitted { task, max_price })
        }
        
        MeshCommand::MarketStatus { detailed: _ } => {
            // In real implementation, would query actual market state
            Ok(CommandResult::MarketStatus { 
                active_offers: 3,
                active_bids: 7,
            })
        }
        
        MeshCommand::MarketTerms => {
            let terms = r#"
SYNAPTIC MARKET TERMS OF SERVICE

Synaptic Market facilitates peer compute federation, not API access resale. 

KEY COMPLIANCE REQUIREMENTS:
✅ NO shared API keys - Each participant uses their own Claude subscription
✅ LOCAL execution - Tasks run locally on provider's Claude account
✅ VOLUNTARY participation - Full user control with opt-in mechanisms  
✅ TOKEN rewards - RUV tokens reward contribution, not access purchase

LEGAL FRAMEWORK:
• Each node maintains individual Claude subscriptions
• Tasks are routed, not account access shared
• Participation is voluntary and contribution-based
• API keys are never shared or transmitted
• This is peer compute federation, not resale

By using Synaptic Market, you agree to maintain your own Claude subscription
and comply with Anthropic's Terms of Service.
"#;
            Ok(CommandResult::MarketTerms { terms: terms.to_string() })
        }
        
        MeshCommand::WalletBalance => {
            // In real implementation, would query actual wallet
            Ok(CommandResult::WalletBalance { balance: 1000 })
        }
        
        MeshCommand::WalletTransfer { to, amount, memo: _ } => {
            // In real implementation, would perform actual transfer
            Ok(CommandResult::WalletTransferCompleted { to, amount })
        }
        
        MeshCommand::WalletHistory { limit: _ } => {
            // In real implementation, would query actual transaction history
            Ok(CommandResult::WalletHistory { 
                transactions: vec![
                    "Transfer: 100 RUV to peer-123 (market_payment)".to_string(),
                    "Received: 50 RUV from peer-456 (task_completion)".to_string(),
                ]
            })
        }

        MeshCommand::Status => {
            Ok(CommandResult::Status {
                mesh_active: true,
                nodes: 1,
                agents: 0,
                swarms: 0,
            })
        }
    }
}

/// Command execution result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CommandResult {
    NodeStarted { port: u16, id: String },
    NodeStopped,
    NodeList { nodes: Vec<String> },
    SwarmCreated { id: String, agents: usize },
    SwarmRunning { id: String },
    SwarmList { swarms: Vec<String> },
    NeuralCreated { path: String },
    NeuralTrained { model: String, epochs: usize },
    NeuralPrediction { output: Vec<f32> },
    MeshInfo { agents: usize, tasks: usize },
    AgentAdded { id: String, name: String },
    TaskSubmitted { id: String, name: String },
    MarketInitialized { db_path: String },
    MarketOfferCreated { slots: u64, price: u64 },
    MarketBidSubmitted { task: String, max_price: u64 },
    MarketStatus { active_offers: usize, active_bids: usize },
    MarketTerms { terms: String },
    WalletBalance { balance: u64 },
    WalletTransferCompleted { to: String, amount: u64 },
    WalletHistory { transactions: Vec<String> },
    Status { mesh_active: bool, nodes: usize, agents: usize, swarms: usize },
}

/// Convert CLI commands to mesh commands
pub fn cli_to_command(cli: Cli) -> MeshCommand {
    match cli.command {
        Commands::Node { action } => match action {
            NodeAction::Start { port } => MeshCommand::NodeStart { port },
            NodeAction::Stop => MeshCommand::NodeStop,
            NodeAction::List => MeshCommand::NodeList,
        },
        Commands::Swarm { action } => match action {
            SwarmAction::Create { agents, behavior } => {
                let b = behavior.and_then(|s| match s.as_str() {
                    "flocking" => Some(SwarmBehavior::Flocking),
                    "foraging" => Some(SwarmBehavior::Foraging),
                    "exploration" => Some(SwarmBehavior::Exploration),
                    "consensus" => Some(SwarmBehavior::Consensus),
                    "optimization" => Some(SwarmBehavior::Optimization),
                    _ => None,
                });
                MeshCommand::SwarmCreate { agents, behavior: b }
            },
            SwarmAction::Run { id } => MeshCommand::SwarmRun { id },
            SwarmAction::List => MeshCommand::SwarmList,
        },
        Commands::Neural { action } => match action {
            NeuralAction::Create { layers, output } => MeshCommand::NeuralCreate { layers, output },
            NeuralAction::Train { model, data } => MeshCommand::NeuralTrain { model, data },
            NeuralAction::Predict { model, input } => MeshCommand::NeuralPredict { model, input },
        },
        Commands::Mesh { action } => match action {
            MeshAction::Info => MeshCommand::MeshInfo,
            MeshAction::AddAgent { name } => MeshCommand::MeshAddAgent { name },
            MeshAction::SubmitTask { name, compute } => MeshCommand::MeshSubmitTask { name, compute },
        },
        Commands::Market { action } => match action {
            MarketAction::Init { db_path } => MeshCommand::MarketInit { db_path },
            MarketAction::Offer { slots, price, opt_in } => MeshCommand::MarketOffer { slots, price, opt_in },
            MarketAction::Bid { task, max_price } => MeshCommand::MarketBid { task, max_price },
            MarketAction::Status { detailed } => MeshCommand::MarketStatus { detailed },
            MarketAction::Terms => MeshCommand::MarketTerms,
        },
        Commands::Wallet { action } => match action {
            WalletAction::Balance => MeshCommand::WalletBalance,
            WalletAction::Transfer { to, amount, memo } => MeshCommand::WalletTransfer { to, amount, memo },
            WalletAction::History { limit } => MeshCommand::WalletHistory { limit },
        },
        Commands::Status => MeshCommand::Status,
    }
}

/// Initialize tracing
pub fn init_tracing() {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::DEBUG)
        .init();
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[tokio::test]
    async fn test_node_start() {
        let cmd = MeshCommand::NodeStart { port: 8080 };
        let result = execute_command(cmd).await.unwrap();
        
        match result {
            CommandResult::NodeStarted { port, .. } => assert_eq!(port, 8080),
            _ => panic!("Unexpected result"),
        }
    }
    
    #[tokio::test]
    async fn test_swarm_create() {
        let cmd = MeshCommand::SwarmCreate { 
            agents: 5, 
            behavior: Some(SwarmBehavior::Flocking) 
        };
        let result = execute_command(cmd).await.unwrap();
        
        match result {
            CommandResult::SwarmCreated { agents, .. } => assert_eq!(agents, 5),
            _ => panic!("Unexpected result"),
        }
    }
    
    #[tokio::test]
    async fn test_neural_create() {
        let cmd = MeshCommand::NeuralCreate {
            layers: vec![10, 5, 2],
            output: "/tmp/test_model.json".to_string(),
        };
        let result = execute_command(cmd).await.unwrap();
        
        match result {
            CommandResult::NeuralCreated { path } => {
                assert_eq!(path, "/tmp/test_model.json");
                // Clean up
                std::fs::remove_file(path).ok();
            },
            _ => panic!("Unexpected result"),
        }
    }
}