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
//! Cliff of Contemplation Line Organization Management
use std::sync::Arc;
use tokio::sync::broadcast;
use crate::domain::{Agent, Group, Message, Organization, Role, LLMConfig, Department, MessageTarget};
use crate::core::store::Store;
/// Cliff of Contemplation Line Manager
pub struct GuiltyLineManager {
store: Arc<dyn Store>,
message_tx: broadcast::Sender<Message>,
}
impl GuiltyLineManager {
pub fn new(store: Arc<dyn Store>, message_tx: broadcast::Sender<Message>) -> Self {
Self { store, message_tx }
}
/// Initialize Cliff of Contemplation Line architecture
/// This will create a special "Cliff of Contemplation Line" department in the organization structure and add corresponding Agents
pub async fn initialize_guilty_line(&self, org: &mut Organization) -> Result<(), Box<dyn std::error::Error>> {
// Add Cliff of Contemplation Line department
let guilty_dept = Department {
id: "guilty_line".to_string(),
name: "Cliff of Contemplation Line".to_string(),
parent_id: None,
leader_id: Some("guilty_chairman".to_string()), // Corporate chairman will become the leader
};
org.add_department(guilty_dept);
// 注意:实际的Agent添加会在用户注册后动态进行
Ok(())
}
/// When a new user registers, add them to the Cliff of Contemplation Line based on their position
pub async fn add_user_to_guilty_line(&self, user_id: &str, position: &crate::domain::user::Position) -> Result<(), Box<dyn std::error::Error>> {
match position {
crate::domain::user::Position::Chairman => {
// Corporate chairman becomes Cliff of Contemplation Line supervisor
self.make_chairman_guilty_leader(user_id).await?;
}
crate::domain::user::Position::Management => {
// Management directly joins the Cliff of Contemplation Line
self.add_management_to_guilty_line(user_id).await?;
}
_ => {
// Regular employees do not join the Cliff of Contemplation Line
}
}
Ok(())
}
/// Make corporate chairman the Cliff of Contemplation Line supervisor
async fn make_chairman_guilty_leader(&self, user_id: &str) -> Result<(), Box<dyn std::error::Error>> {
// Here we will update the organization structure, making the user the leader of the Cliff of Contemplation Line department
// 实现细节取决于具体的组织架构更新机制
// Create or update chairman's Agent information, making them the leader of the Cliff of Contemplation Line
let mut org = self.store.load_organization().await?;
// Find the corresponding Agent and update their department assignment
if let Some(agent) = org.agents.iter_mut().find(|a| a.id == user_id) {
agent.department_id = Some("guilty_line".to_string());
}
self.store.save_organization(&org).await?;
Ok(())
}
/// 将管理层加入思过崖线
async fn add_management_to_guilty_line(&self, user_id: &str) -> Result<(), Box<dyn std::error::Error>> {
let mut org = self.store.load_organization().await?;
// Find the corresponding Agent and update their department assignment
if let Some(agent) = org.agents.iter_mut().find(|a| a.id == user_id) {
agent.department_id = Some("guilty_line".to_string());
}
self.store.save_organization(&org).await?;
Ok(())
}
/// Automatically create "Cliff of Contemplation Line" group chat and add relevant personnel
pub async fn create_guilty_line_group_chat(&self, highest_level_agent_id: &str) -> Result<String, Box<dyn std::error::Error>> {
// Create Cliff of Contemplation Line group chat, including:
// 1. Corporate chairman (00001)
// 2. All management members
// 3. Highest level member of user-defined architecture (highest_level_agent_id)
let users = self.store.load_users().await?;
let mut members = vec![highest_level_agent_id.to_string()];
// Add corporate chairman and all management members
for user in users {
match user.position {
crate::domain::user::Position::Chairman => {
members.push(user.id.clone());
}
crate::domain::user::Position::Management => {
members.push(user.id.clone());
}
_ => {}
}
}
// 去重
members.sort();
members.dedup();
let group = Group {
id: "guilty_line_group".to_string(),
name: "Cliff of Contemplation Line".to_string(),
creator_id: "system".to_string(), // 系统创建
members,
created_at: chrono::Utc::now().timestamp(),
visibility: crate::domain::message::GroupVisibility::Hidden,
};
self.store.save_group(&group).await?;
Ok(group.id)
}
/// Add the highest level member of user-defined architecture to the Cliff of Contemplation Line group chat
pub async fn add_highest_level_to_guilty_group(&self, agent_id: &str) -> Result<(), Box<dyn std::error::Error>> {
let mut groups = self.store.load_groups().await?;
// Find Cliff of Contemplation Line group chat
if let Some(group) = groups.iter_mut().find(|g| g.id == "guilty_line_group") {
if !group.members.contains(&agent_id.to_string()) {
group.members.push(agent_id.to_string());
self.store.save_group(group).await?;
}
}
Ok(())
}
}