use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;
pub trait SimpleAgent: Send + Sync {
fn id(&self) -> &str;
fn specialization(&self) -> AgentSpecialization;
fn execute_sync(&self, context: &AgentContext) -> AgentResult;
fn coordinate_sync(&self, message: CoordinationMessage) -> AgentResult;
fn status(&self) -> AgentStatus;
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AgentSpecialization {
LondonBdd, Byzantene, MarketIntelligence, SemanticAnalyst, SecuritySentinel, PerformanceGuardian, }
#[derive(Debug, Clone)]
pub struct AgentContext {
pub request_id: Uuid,
pub project_context: ProjectContext,
pub execution_environment: ExecutionEnvironment,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectContext {
pub project_id: String,
pub project_type: String,
pub technology_stack: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionEnvironment {
pub available_resources: ResourceAllocation,
pub network_status: NetworkStatus,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceAllocation {
pub cpu_cores: u32,
pub memory_mb: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum NetworkStatus {
Online,
Limited,
Offline,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentResult {
pub success: bool,
pub output: serde_json::Value,
pub confidence: f64,
pub execution_time_ms: u64,
pub recommendations: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoordinationMessage {
pub from_agent: String,
pub to_agent: String,
pub message_type: MessageType,
pub payload: serde_json::Value,
pub priority: Priority,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MessageType {
StatusUpdate,
TaskDelegation,
KnowledgeShare,
Coordination,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Priority {
Low,
Medium,
High,
Critical,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentStatus {
pub agent_id: String,
pub specialization: AgentSpecialization,
pub current_load: f64,
pub health_score: f64,
pub last_activity: chrono::DateTime<chrono::Utc>,
pub active_tasks: Vec<String>,
}
pub struct AgentRegistry {
agents: HashMap<String, Box<dyn SimpleAgent>>,
}
impl Default for AgentRegistry {
fn default() -> Self {
Self::new()
}
}
impl AgentRegistry {
pub fn new() -> Self {
Self {
agents: HashMap::new(),
}
}
pub fn register(&mut self, agent: Box<dyn SimpleAgent>) {
self.agents.insert(agent.id().to_string(), agent);
}
pub fn get(&self, id: &str) -> Option<&dyn SimpleAgent> {
self.agents.get(id).map(|v| &**v)
}
pub fn list(&self) -> Vec<&str> {
self.agents.keys().map(|s| s.as_str()).collect()
}
pub fn get_by_specialization(
&self, specialization: &AgentSpecialization,
) -> Vec<&dyn SimpleAgent> {
self.agents
.values()
.filter(|agent| agent.specialization() == *specialization)
.map(|agent| &**agent)
.collect()
}
}
pub struct MessageRouter {
message_queue: Vec<CoordinationMessage>,
}
impl Default for MessageRouter {
fn default() -> Self {
Self::new()
}
}
impl MessageRouter {
pub fn new() -> Self {
Self {
message_queue: Vec::new(),
}
}
pub fn route_message(&mut self, message: CoordinationMessage) -> RoutingResult {
self.message_queue.push(message.clone());
RoutingResult {
message_id: Uuid::new_v4().to_string(),
delivered_to: vec![message.to_agent.clone()],
delivery_successful: true,
response_time_ms: 50,
}
}
pub fn process_messages(&mut self, agents: &AgentRegistry) -> Vec<AgentResult> {
let mut results = Vec::new();
while let Some(message) = self.message_queue.pop() {
if let Some(agent) = agents.get(&message.to_agent) {
let result = agent.coordinate_sync(message);
results.push(result);
}
}
results
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoutingResult {
pub message_id: String,
pub delivered_to: Vec<String>,
pub delivery_successful: bool,
pub response_time_ms: u64,
}
pub struct TaskCoordinator {
tasks: Vec<CoordinationTask>,
#[allow(dead_code)]
completed_tasks: Vec<String>,
}
impl Default for TaskCoordinator {
fn default() -> Self {
Self::new()
}
}
impl TaskCoordinator {
pub fn new() -> Self {
Self {
tasks: Vec::new(),
completed_tasks: Vec::new(),
}
}
pub fn add_task(&mut self, task: CoordinationTask) {
self.tasks.push(task);
}
pub fn coordinate_tasks(&mut self, agents: &AgentRegistry) -> Vec<TaskResult> {
let mut results = Vec::new();
for task in &self.tasks {
if let Some(agent) = self.select_agent_for_task(task, agents) {
let context = AgentContext {
request_id: Uuid::new_v4(),
project_context: ProjectContext {
project_id: "ggen-marketplace".to_string(),
project_type: "library".to_string(),
technology_stack: vec!["rust".to_string()],
},
execution_environment: ExecutionEnvironment {
available_resources: ResourceAllocation {
cpu_cores: 4,
memory_mb: 8192,
},
network_status: NetworkStatus::Online,
},
};
let result = agent.execute_sync(&context);
results.push(TaskResult {
task_id: task.id.clone(),
success: result.success,
execution_time_ms: result.execution_time_ms,
agent_used: agent.id().to_string(),
});
}
}
results
}
fn select_agent_for_task<'a>(
&self, task: &CoordinationTask, agents: &'a AgentRegistry,
) -> Option<&'a dyn SimpleAgent> {
agents
.get_by_specialization(&task.required_specialization)
.first()
.copied()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoordinationTask {
pub id: String,
pub task_type: String,
pub priority: Priority,
pub required_specialization: AgentSpecialization,
pub input_data: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskResult {
pub task_id: String,
pub success: bool,
pub execution_time_ms: u64,
pub agent_used: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemStatus {
pub total_agents: usize,
pub active_tasks: usize,
pub system_health: f64,
pub last_updated: chrono::DateTime<chrono::Utc>,
}
pub struct AgentSystem {
registry: AgentRegistry,
message_router: MessageRouter,
task_coordinator: TaskCoordinator,
}
impl Default for AgentSystem {
fn default() -> Self {
Self::new()
}
}
impl AgentSystem {
pub fn new() -> Self {
Self {
registry: AgentRegistry::new(),
message_router: MessageRouter::new(),
task_coordinator: TaskCoordinator::new(),
}
}
pub fn register_agent(&mut self, agent: Box<dyn SimpleAgent>) {
self.registry.register(agent);
}
pub fn get_status(&self) -> SystemStatus {
SystemStatus {
total_agents: self.registry.list().len(),
active_tasks: self.task_coordinator.tasks.len(),
system_health: 0.95, last_updated: chrono::Utc::now(),
}
}
pub fn coordinate_task(&mut self, task: CoordinationTask) -> TaskResult {
self.task_coordinator.add_task(task);
let results = self.task_coordinator.coordinate_tasks(&self.registry);
results.into_iter().next().unwrap_or_else(|| TaskResult {
task_id: "unknown".to_string(),
success: false,
execution_time_ms: 0,
agent_used: "none".to_string(),
})
}
pub fn process_messages(&mut self) -> Vec<AgentResult> {
self.message_router.process_messages(&self.registry)
}
}
pub struct LondonBddAgent {
id: String,
}
impl Default for LondonBddAgent {
fn default() -> Self {
Self::new()
}
}
impl LondonBddAgent {
pub fn new() -> Self {
Self {
id: "london-bdd-001".to_string(),
}
}
}
impl SimpleAgent for LondonBddAgent {
fn id(&self) -> &str {
&self.id
}
fn specialization(&self) -> AgentSpecialization {
AgentSpecialization::LondonBdd
}
fn execute_sync(&self, _context: &AgentContext) -> AgentResult {
AgentResult {
success: true,
output: serde_json::json!({"bdd_analysis": "completed"}),
confidence: 0.95,
execution_time_ms: 200,
recommendations: vec!["Add more BDD scenarios".to_string()],
}
}
fn coordinate_sync(&self, _message: CoordinationMessage) -> AgentResult {
AgentResult {
success: true,
output: serde_json::json!({"coordination_acknowledged": true}),
confidence: 1.0,
execution_time_ms: 50,
recommendations: vec![],
}
}
fn status(&self) -> AgentStatus {
AgentStatus {
agent_id: self.id.clone(),
specialization: AgentSpecialization::LondonBdd,
current_load: 0.3,
health_score: 0.95,
last_activity: chrono::Utc::now(),
active_tasks: vec!["bdd_orchestration".to_string()],
}
}
}
pub struct ByzanteneAgent {
id: String,
}
impl Default for ByzanteneAgent {
fn default() -> Self {
Self::new()
}
}
impl ByzanteneAgent {
pub fn new() -> Self {
Self {
id: "byzantene-001".to_string(),
}
}
}
impl SimpleAgent for ByzanteneAgent {
fn id(&self) -> &str {
&self.id
}
fn specialization(&self) -> AgentSpecialization {
AgentSpecialization::Byzantene
}
fn execute_sync(&self, _context: &AgentContext) -> AgentResult {
AgentResult {
success: true,
output: serde_json::json!({"fault_tolerance": "active"}),
confidence: 0.98,
execution_time_ms: 100,
recommendations: vec!["Monitor agent health".to_string()],
}
}
fn coordinate_sync(&self, _message: CoordinationMessage) -> AgentResult {
AgentResult {
success: true,
output: serde_json::json!({"coordination_acknowledged": true}),
confidence: 1.0,
execution_time_ms: 50,
recommendations: vec![],
}
}
fn status(&self) -> AgentStatus {
AgentStatus {
agent_id: self.id.clone(),
specialization: AgentSpecialization::Byzantene,
current_load: 0.2,
health_score: 0.98,
last_activity: chrono::Utc::now(),
active_tasks: vec!["fault_detection".to_string()],
}
}
}