Crates.io | ruv-swarm-core |
lib.rs | ruv-swarm-core |
version | 1.0.6 |
created_at | 2025-06-30 04:36:26.138197+00 |
updated_at | 2025-07-03 18:18:22.301549+00 |
description | Core orchestration and agent traits for RUV Swarm |
homepage | |
repository | https://github.com/ruvnet/ruv-FANN |
max_upload_size | |
id | 1731411 |
size | 144,687 |
Core orchestration and agent traits for RUV Swarm - the foundational building blocks for creating distributed AI agent swarms with cognitive diversity patterns.
ruv-swarm-core is the foundational orchestration crate that powers the RUV Swarm ecosystem. It provides the core traits, abstractions, and coordination primitives needed to build distributed AI agent systems with cognitive diversity patterns and advanced swarm behaviors.
This crate serves as the bedrock for all swarm operations, defining how agents communicate, coordinate, and execute tasks across different topologies and distribution strategies.
Add ruv-swarm-core to your Cargo.toml
:
[dependencies]
ruv-swarm-core = "0.1.0"
Enable optional features based on your deployment needs:
[dependencies]
ruv-swarm-core = { version = "0.1.0", features = ["std", "wasm"] }
Available features:
std
(default) - Standard library support with full functionalityno_std
- No standard library support for embedded environmentswasm
- WebAssembly support with JavaScript interopminimal
- Minimal feature set for size optimizationuse ruv_swarm_core::prelude::*;
use async_trait::async_trait;
// Define a compute agent
struct ComputeAgent {
id: String,
capabilities: Vec<String>,
}
#[async_trait]
impl Agent for ComputeAgent {
type Input = f64;
type Output = f64;
type Error = std::io::Error;
async fn process(&mut self, input: Self::Input) -> Result<Self::Output, Self::Error> {
// Simulate computational work
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
Ok(input * 2.0)
}
fn capabilities(&self) -> &[String] {
&self.capabilities
}
fn id(&self) -> &str {
&self.id
}
fn cognitive_pattern(&self) -> CognitivePattern {
CognitivePattern::Convergent
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create an agent
let agent = ComputeAgent {
id: "compute-001".to_string(),
capabilities: vec!["mathematics".to_string(), "computation".to_string()],
};
// Process input
let mut agent = agent;
let result = agent.process(42.0).await?;
println!("Agent processed 42.0 -> {}", result);
Ok(())
}
use ruv_swarm_core::{Swarm, SwarmConfig, Task, Priority, TopologyType};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Configure swarm with mesh topology
let config = SwarmConfig {
max_agents: 10,
topology: TopologyType::Mesh,
distribution_strategy: DistributionStrategy::Balanced,
enable_monitoring: true,
..Default::default()
};
// Create swarm
let mut swarm = Swarm::new(config).await?;
// Add agents to swarm
for i in 0..5 {
let agent = ComputeAgent {
id: format!("agent-{:03}", i),
capabilities: vec!["computation".to_string()],
};
swarm.add_agent(Box::new(agent)).await?;
}
// Create and submit tasks
for i in 0..20 {
let task = Task::new(
format!("task-{}", i),
Priority::Medium,
i as f64,
);
swarm.submit_task(task).await?;
}
// Process tasks
swarm.start().await?;
// Wait for completion
while swarm.has_pending_tasks().await {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
println!("All tasks completed!");
Ok(())
}
use ruv_swarm_core::{Agent, CognitivePattern};
struct AdaptiveAgent {
id: String,
current_pattern: CognitivePattern,
}
impl AdaptiveAgent {
fn switch_pattern(&mut self, task_type: &str) {
self.current_pattern = match task_type {
"creative" => CognitivePattern::Divergent,
"analytical" => CognitivePattern::Convergent,
"innovative" => CognitivePattern::Lateral,
"systematic" => CognitivePattern::Systems,
_ => CognitivePattern::Critical,
};
}
}
#[async_trait]
impl Agent for AdaptiveAgent {
type Input = (String, f64); // (task_type, data)
type Output = f64;
type Error = std::io::Error;
async fn process(&mut self, input: Self::Input) -> Result<Self::Output, Self::Error> {
let (task_type, data) = input;
// Switch cognitive pattern based on task
self.switch_pattern(&task_type);
// Process differently based on cognitive pattern
let result = match self.current_pattern {
CognitivePattern::Convergent => data * 1.1,
CognitivePattern::Divergent => data * 1.5,
CognitivePattern::Lateral => data.sqrt() * 2.0,
CognitivePattern::Systems => data.ln() + 1.0,
_ => data,
};
Ok(result)
}
fn id(&self) -> &str {
&self.id
}
fn cognitive_pattern(&self) -> CognitivePattern {
self.current_pattern
}
fn capabilities(&self) -> &[String] {
static CAPS: &[String] = &[];
CAPS
}
}
use ruv_swarm_core::{Topology, TopologyType};
async fn create_hierarchical_swarm() -> Result<(), Box<dyn std::error::Error>> {
// Create hierarchical topology with coordinators and workers
let topology = Topology::new(TopologyType::Hierarchical);
let config = SwarmConfig {
topology_type: TopologyType::Hierarchical,
coordinator_count: 2,
worker_count: 8,
enable_fault_tolerance: true,
..Default::default()
};
let mut swarm = Swarm::with_topology(config, topology).await?;
// Add coordinator agents
for i in 0..2 {
let coordinator = CoordinatorAgent::new(format!("coord-{}", i));
swarm.add_coordinator(Box::new(coordinator)).await?;
}
// Add worker agents
for i in 0..8 {
let worker = WorkerAgent::new(format!("worker-{}", i));
swarm.add_worker(Box::new(worker)).await?;
}
// Start coordinated processing
swarm.start_coordinated().await?;
Ok(())
}
The foundational trait that all swarm agents must implement:
#[async_trait]
pub trait Agent: Send + Sync {
type Input: Send;
type Output: Send;
type Error: Send;
async fn process(&mut self, input: Self::Input) -> Result<Self::Output, Self::Error>;
fn id(&self) -> &str;
fn capabilities(&self) -> &[String];
fn cognitive_pattern(&self) -> CognitivePattern;
fn health_status(&self) -> HealthStatus;
}
Seven distinct patterns for diverse problem-solving approaches:
Priority-based task orchestration with sophisticated scheduling:
pub struct Task<T> {
pub id: TaskId,
pub priority: Priority,
pub data: T,
pub requirements: Vec<String>,
pub timeout: Option<Duration>,
}
pub enum Priority {
Low = 1,
Medium = 2,
High = 3,
Critical = 4,
}
Full connectivity between all agents for maximum redundancy:
Coordinator-worker structure for organized task flow:
Agents connected in a circular pattern:
Central hub with spoke connections to all agents:
Complete API documentation is available on docs.rs:
ruv-swarm-core integrates seamlessly with the broader RUV ecosystem:
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ ruv-FANN │ │ ruv-swarm │ │ neuro-divergent │
│ Neural Networks │◄──►│ Agent Swarms │◄──►│ Forecasting │
└─────────────────┘ └──────────────────┘ └─────────────────┘
▲
│
┌─────────────────┐
│ ruv-swarm-core │
│ Core Traits & │
│ Orchestration │
└─────────────────┘
We welcome contributions to ruv-swarm-core! Please see the main repository's Contributing Guide for details.
# Clone the main repository
git clone https://github.com/ruvnet/ruv-FANN.git
cd ruv-FANN/ruv-swarm/crates/ruv-swarm-core
# Run tests
cargo test --all-features
# Run benchmarks
cargo bench
# Check no-std compatibility
cargo check --no-default-features --features no_std
# Test WASM compatibility
cargo check --target wasm32-unknown-unknown --features wasm
Licensed under either of:
at your option.
Created by rUv
Building the future of distributed AI agent orchestration - one cognitive pattern at a time.
Part of the RUV-FANN ecosystem for neural networks, agent swarms, and AI forecasting.