| |
| |
| |
| |
| |
| |
| |
|
|
| use crate::config::AgentRole; |
| use std::collections::{HashMap, HashSet}; |
| use std::time::Instant; |
|
|
| |
| |
| |
|
|
| |
| pub struct Project { |
| pub id: String, |
| pub name: String, |
| pub description: String, |
| pub status: String, |
| pub assigned_workers: HashSet<String>, |
| pub assigned_thinkers: HashSet<String>, |
| pub tasks: Vec<ProjectTask>, |
| pub context: String, |
| pub results: Vec<String>, |
| } |
|
|
| |
| pub struct ProjectTask { |
| pub id: String, |
| pub title: String, |
| pub tool: String, |
| pub args: serde_json::Value, |
| pub assigned_to: Option<String>, |
| pub status: String, |
| } |
|
|
| |
| pub struct ProjectRegistry { |
| pub projects: HashMap<String, Project>, |
| } |
|
|
| impl ProjectRegistry { |
| pub fn new() -> Self { |
| Self { projects: HashMap::new() } |
| } |
|
|
| pub fn add_project(&mut self, project: Project) { |
| self.projects.insert(project.id.clone(), project); |
| } |
|
|
| pub fn get_project(&self, id: &str) -> Option<&Project> { |
| self.projects.get(id) |
| } |
|
|
| pub fn get_project_mut(&mut self, id: &str) -> Option<&mut Project> { |
| self.projects.get_mut(id) |
| } |
|
|
| pub fn remove_project(&mut self, id: &str) -> Option<Project> { |
| self.projects.remove(id) |
| } |
|
|
| pub fn list_projects(&self) -> Vec<String> { |
| self.projects.keys().cloned().collect() |
| } |
| } |
|
|
| impl Default for ProjectRegistry { |
| fn default() -> Self { Self::new() } |
| } |
|
|
| |
| |
| |
|
|
| |
| pub struct WorkerState { |
| pub pub_key: String, |
| pub status: String, |
| pub current_project: Option<String>, |
| pub current_task: Option<String>, |
| pub last_heartbeat: Instant, |
| } |
|
|
| impl WorkerState { |
| pub fn new(pub_key: String) -> Self { |
| Self { |
| pub_key, |
| status: "Idle".to_string(), |
| current_project: None, |
| current_task: None, |
| last_heartbeat: Instant::now(), |
| } |
| } |
| } |
|
|
| |
| |
| pub struct ThinkerState { |
| pub pub_key: String, |
| pub status: String, |
| pub current_project: Option<String>, |
| pub last_heartbeat: Instant, |
| } |
|
|
| impl ThinkerState { |
| pub fn new(pub_key: String) -> Self { |
| Self { |
| pub_key, |
| status: "Idle".to_string(), |
| current_project: None, |
| last_heartbeat: Instant::now(), |
| } |
| } |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| pub struct OrchestratorState { |
| pub my_role: AgentRole, |
| pub my_pub_key: String, |
| pub projects: ProjectRegistry, |
| pub workers: HashMap<String, WorkerState>, |
| pub thinkers: HashMap<String, ThinkerState>, |
| pub next_project_id: u64, |
| } |
|
|
| impl OrchestratorState { |
| pub fn new(my_role: AgentRole, my_pub_key: String) -> Self { |
| Self { |
| my_role, |
| my_pub_key, |
| projects: ProjectRegistry::new(), |
| workers: HashMap::new(), |
| thinkers: HashMap::new(), |
| next_project_id: 1, |
| } |
| } |
|
|
| |
| pub fn next_project_id(&mut self) -> String { |
| let id = format!("proj-{}", self.next_project_id); |
| self.next_project_id += 1; |
| id |
| } |
|
|
| |
| pub fn heartbeat_worker(&mut self, pub_key: &str) { |
| if let Some(worker) = self.workers.get_mut(pub_key) { |
| worker.last_heartbeat = Instant::now(); |
| if worker.status == "Offline" { |
| worker.status = "Idle".to_string(); |
| } |
| } |
| } |
|
|
| |
| pub fn heartbeat_thinker(&mut self, pub_key: &str) { |
| if let Some(thinker) = self.thinkers.get_mut(pub_key) { |
| thinker.last_heartbeat = Instant::now(); |
| if thinker.status == "Offline" { |
| thinker.status = "Idle".to_string(); |
| } |
| } |
| } |
|
|
| |
| pub fn assign_worker(&mut self, pub_key: &str, project_id: &str) { |
| self.workers |
| .entry(pub_key.to_string()) |
| .or_insert_with(|| WorkerState::new(pub_key.to_string())); |
|
|
| if let Some(worker) = self.workers.get_mut(pub_key) { |
| worker.status = "Idle".to_string(); |
| worker.current_project = Some(project_id.to_string()); |
| worker.current_task = None; |
| } |
| } |
|
|
| |
| pub fn assign_thinker(&mut self, pub_key: &str, project_id: &str) { |
| self.thinkers |
| .entry(pub_key.to_string()) |
| .or_insert_with(|| ThinkerState::new(pub_key.to_string())); |
|
|
| if let Some(thinker) = self.thinkers.get_mut(pub_key) { |
| thinker.status = "Idle".to_string(); |
| thinker.current_project = Some(project_id.to_string()); |
| } |
| } |
|
|
| |
| pub fn worker_offline(&mut self, pub_key: &str) { |
| if let Some(worker) = self.workers.get_mut(pub_key) { |
| worker.status = "Offline".to_string(); |
| } |
| } |
|
|
| |
| pub fn thinker_offline(&mut self, pub_key: &str) { |
| if let Some(thinker) = self.thinkers.get_mut(pub_key) { |
| thinker.status = "Offline".to_string(); |
| } |
| } |
|
|
| |
| pub fn summary(&self) -> String { |
| format!( |
| "Orchestrator | role={} | projects={} | workers={} idle / {} total | thinkers={} idle / {} total", |
| self.my_role, |
| self.projects.projects.len(), |
| self.workers.values().filter(|w| w.status == "Idle").count(), |
| self.workers.len(), |
| self.thinkers.values().filter(|t| t.status == "Idle").count(), |
| self.thinkers.len(), |
| ) |
| } |
| } |
|
|