imitatort 0.0.1-SNAPSHOT-dev.20260302111239

轻量级多Agent公司模拟框架
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
//! Web 服务器模块
//!
//! 提供 HTTP API 和 WebSocket 支持

use std::sync::Arc;

use axum::{
    extract::{Path, Query, State, WebSocketUpgrade},
    http::{HeaderMap, StatusCode},
    response::IntoResponse,
    routing::{delete, get, post},
    Json, Router,
};
use chrono::Utc;
use serde::{Deserialize, Serialize};
use tokio::sync::broadcast;
use tower_http::cors::CorsLayer;
use tracing::{error, info};

use crate::domain::invitation_code::InvitationCode;
use crate::domain::user::User;
use crate::domain::{Agent, LLMConfig, Message, MessageTarget, Organization, Role};
use crate::infrastructure::auth::{JwtService, PasswordService, UserInfo};

// ==================== 错误响应 ====================

#[derive(Serialize)]
struct ErrorResponse {
    error: String,
}

// ==================== 状态 ====================

#[derive(Clone)]
pub struct AppState {
    pub agents: Vec<Agent>,
    pub message_tx: broadcast::Sender<Message>,
    pub store: Arc<dyn crate::core::store::Store>,
    pub jwt_service: JwtService,
}

// ==================== API 响应类型 ====================

#[derive(Serialize)]
pub struct AgentResponse {
    pub id: String,
    pub name: String,
    pub role: String,
    pub department: String,
    pub description: Option<String>,
}

// ==================== 请求类型 ====================

#[derive(Deserialize)]
pub struct SendMessageRequest {
    pub from: String,
    pub to: Option<String>,
    pub content: String,
}

// ==================== 客户端消息类型 ====================

#[derive(Deserialize)]
#[serde(tag = "type")]
pub enum ClientMessage {
    #[serde(rename = "send_message")]
    SendMessage {
        from: String,
        to: String,
        content: String,
    },
    #[serde(rename = "ping")]
    Ping,
}

// 为简化,我们创建一个验证JWT的辅助函数
#[allow(dead_code)]
async fn validate_jwt(state: &AppState, token: &str) -> Option<UserInfo> {
    state.jwt_service.validate_token(token).ok()
}

/// 获取当前用户信息
async fn get_current_user(
    State(state): State<Arc<AppState>>,
    headers: HeaderMap,
) -> impl IntoResponse {
    // 从头部获取授权信息
    let auth_header = headers
        .get("authorization")
        .and_then(|value| value.to_str().ok());

    if let Some(auth_str) = auth_header {
        if auth_str.starts_with("Bearer ") {
            let token = auth_str.strip_prefix("Bearer ").unwrap();

            if let Ok(user_info) = state.jwt_service.validate_token(token) {
                return Json(serde_json::json!({
                    "success": true,
                    "data": {
                        "id": user_info.id,
                        "username": user_info.username,
                        "name": user_info.name,
                        "email": user_info.email,
                        "is_director": user_info.is_director,
                        "employee_id": user_info.employee_id,
                        "position": user_info.position,
                        "department": user_info.department,
                    }
                }))
                .into_response();
            }
        }
    }

    (
        StatusCode::UNAUTHORIZED,
        Json(ErrorResponse {
            error: "Unauthorized".to_string(),
        }),
    )
        .into_response()
}

#[derive(Deserialize)]
pub struct AuthRequest {
    pub username: String,
    pub password: String,
}

#[derive(Deserialize)]
pub struct RegisterRequest {
    pub username: String,
    pub password: String,
    pub name: String,
    pub email: Option<String>,
    pub invite_code: Option<String>, // 邀请码(可选,首位注册不需要)
}

// ==================== 处理器 ====================

/// 健康检查
async fn health_check() -> impl IntoResponse {
    Json(serde_json::json!({
        "status": "ok",
        "timestamp": Utc::now().to_rfc3339(),
    }))
}

/// 获取 Agent 列表
async fn list_agents(State(state): State<Arc<AppState>>) -> impl IntoResponse {
    let agents: Vec<AgentResponse> = state
        .agents
        .iter()
        .map(|a| AgentResponse {
            id: a.id.clone(),
            name: a.name.clone(),
            role: a.role.title.clone(),
            department: a.department_id.clone().unwrap_or_default(),
            description: a.description.clone(),
        })
        .collect();

    Json(agents)
}

/// 获取单个 Agent
async fn get_agent(
    State(state): State<Arc<AppState>>,
    Path(agent_id): Path<String>,
) -> impl IntoResponse {
    match state.agents.iter().find(|a| a.id == agent_id) {
        Some(agent) => Json(serde_json::json!({
            "id": agent.id,
            "name": agent.name,
            "role": agent.role.title,
            "department": agent.department_id,
            "description": agent.description,
        }))
        .into_response(),
        None => (
            StatusCode::NOT_FOUND,
            Json(ErrorResponse {
                error: format!("Agent not found: {}", agent_id),
            }),
        )
            .into_response(),
    }
}

/// 获取公司信息
async fn get_company(State(state): State<Arc<AppState>>) -> impl IntoResponse {
    let departments: Vec<String> = state
        .agents
        .iter()
        .filter_map(|a| a.department_id.clone())
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();

    Json(serde_json::json!({
        "name": "ImitatorT Virtual Company",
        "agent_count": state.agents.len(),
        "departments": departments,
    }))
}

/// 发送消息
async fn send_message(
    State(state): State<Arc<AppState>>,
    Json(req): Json<SendMessageRequest>,
) -> impl IntoResponse {
    let to = if let Some(to_id) = req.to {
        MessageTarget::Direct(to_id)
    } else {
        return (
            StatusCode::BAD_REQUEST,
            Json(ErrorResponse {
                error: "Missing 'to' field".to_string(),
            }),
        )
            .into_response();
    };

    let message = Message {
        id: uuid::Uuid::new_v4().to_string(),
        from: req.from,
        to,
        content: req.content,
        timestamp: Utc::now().timestamp(),
        reply_to: None,
        mentions: Vec::new(),
    };

    // 发送消息
    let _ = state.message_tx.send(message.clone());

    Json(serde_json::json!({
        "id": message.id,
        "status": "sent",
        "timestamp": message.timestamp,
    }))
    .into_response()
}

/// 登录
async fn login(
    State(state): State<Arc<AppState>>,
    Json(req): Json<AuthRequest>,
) -> impl IntoResponse {
    info!("Login attempt: {}", req.username);

    // 从数据库查找用户
    match state.store.load_user_by_username(&req.username).await {
        Ok(Some(user)) => {
            // 验证密码
            match PasswordService::verify_password(&user.password_hash, &req.password) {
                Ok(valid) if valid => {
                    // 生成JWT令牌
                    let token: String = match state.jwt_service.generate_token(&UserInfo {
                        id: user.id.clone(),
                        username: user.username.clone(),
                        name: user.name.clone(),
                        email: user.email.clone(),
                        is_director: matches!(
                            user.position,
                            crate::domain::user::Position::Chairman
                                | crate::domain::user::Position::Management
                        ),
                        employee_id: user.employee_id.clone(),
                        position: format!("{:?}", user.position),
                        department: user.department.clone(),
                    }) {
                        Ok(token) => token,
                        Err(e) => {
                            error!("Failed to generate token: {}", e);
                            return (
                                StatusCode::INTERNAL_SERVER_ERROR,
                                Json(ErrorResponse {
                                    error: "Failed to generate token".to_string(),
                                }),
                            )
                                .into_response();
                        }
                    };

                    Json(serde_json::json!({
                        "success": true,
                        "data": {
                            "token": token,
                            "user": {
                                "id": user.id,
                                "username": user.username,
                                "name": user.name,
                                "email": user.email,
                                "is_director": matches!(user.position, crate::domain::user::Position::Chairman | crate::domain::user::Position::Management),
                                "employee_id": user.employee_id,
                                "position": format!("{:?}", user.position),
                                "department": user.department,
                            }
                        }
                    })).into_response()
                }
                Ok(_) | Err(_) => {
                    // 密码错误
                    (
                        StatusCode::UNAUTHORIZED,
                        Json(ErrorResponse {
                            error: "Invalid username or password".to_string(),
                        }),
                    )
                        .into_response()
                }
            }
        }
        Ok(None) => {
            // 用户不存在
            (
                StatusCode::UNAUTHORIZED,
                Json(ErrorResponse {
                    error: "Invalid username or password".to_string(),
                }),
            )
                .into_response()
        }
        Err(e) => {
            error!("Database error during login: {}", e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: "Database error".to_string(),
                }),
            )
                .into_response()
        }
    }
}

/// 注册
async fn register(
    State(state): State<Arc<AppState>>,
    Json(req): Json<RegisterRequest>,
) -> impl IntoResponse {
    info!("Register attempt: {}", req.username);

    // 检查用户名是否已存在
    match state.store.load_user_by_username(&req.username).await {
        Ok(Some(_)) => {
            // 用户名已存在
            return (
                StatusCode::CONFLICT,
                Json(ErrorResponse {
                    error: "Username already exists".to_string(),
                }),
            )
                .into_response();
        }
        Ok(None) => {
            // 用户名不存在,可以继续
        }
        Err(e) => {
            error!("Database error during registration check: {}", e);
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: "Database error".to_string(),
                }),
            )
                .into_response();
        }
    }

    // 检查是否为首注册用户(检查是否已有用户)
    let existing_users = match state.store.load_users().await {
        Ok(users) => users,
        Err(e) => {
            error!("Database error during user count check: {}", e);
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: "Database error".to_string(),
                }),
            )
                .into_response();
        }
    };

    let user_to_create = if existing_users.is_empty() {
        // The first registered user automatically becomes the corporate chairman
        if req.invite_code.is_some() {
            return (
                StatusCode::BAD_REQUEST,
                Json(ErrorResponse {
                    error: "First user registration does not require an invitation code"
                        .to_string(),
                }),
            )
                .into_response();
        }

        // 哈希密码
        let password_hash = match PasswordService::hash_password(&req.password) {
            Ok(hash) => hash,
            Err(e) => {
                error!("Failed to hash password: {}", e);
                return (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    Json(ErrorResponse {
                        error: "Failed to process password".to_string(),
                    }),
                )
                    .into_response();
            }
        };

        // Create corporate chairman user
        User::new_chairman(
            req.username.clone(),
            req.name.clone(),
            password_hash,
            req.email.clone(),
        )
    } else {
        // 非首位注册,需要邀请码
        if req.invite_code.is_none() {
            return (
                StatusCode::BAD_REQUEST,
                Json(ErrorResponse {
                    error: "Invitation code is required for registration".to_string(),
                }),
            )
                .into_response();
        }

        let invite_code_str = req.invite_code.unwrap();

        // 验证邀请码
        let mut invite_code = match state
            .store
            .load_invitation_code_by_code(&invite_code_str)
            .await
        {
            Ok(Some(code)) => code,
            Ok(None) => {
                return (
                    StatusCode::BAD_REQUEST,
                    Json(ErrorResponse {
                        error: "Invalid invitation code".to_string(),
                    }),
                )
                    .into_response();
            }
            Err(e) => {
                error!("Database error during invitation code check: {}", e);
                return (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    Json(ErrorResponse {
                        error: "Database error".to_string(),
                    }),
                )
                    .into_response();
            }
        };

        // 检查邀请码是否有效
        if !invite_code.is_valid() {
            return (
                StatusCode::BAD_REQUEST,
                Json(ErrorResponse {
                    error: "Invitation code has expired or reached maximum usage".to_string(),
                }),
            )
                .into_response();
        }

        // 哈希密码
        let password_hash = match PasswordService::hash_password(&req.password) {
            Ok(hash) => hash,
            Err(e) => {
                error!("Failed to hash password: {}", e);
                return (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    Json(ErrorResponse {
                        error: "Failed to process password".to_string(),
                    }),
                )
                    .into_response();
            }
        };

        // 使用邀请码(增加使用次数)
        invite_code.use_code();

        // 更新邀请码状态
        if let Err(e) = state.store.update_invitation_code(&invite_code).await {
            error!("Failed to update invitation code: {}", e);
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: "Failed to update invitation code".to_string(),
                }),
            )
                .into_response();
        }

        // 获取当前管理层用户数量,用于生成工号
        let management_users = match state.store.load_users().await {
            Ok(users) => users
                .into_iter()
                .filter(|u| matches!(u.position, crate::domain::user::Position::Management))
                .collect::<Vec<_>>(),
            Err(e) => {
                error!("Database error during management user count: {}", e);
                return (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    Json(ErrorResponse {
                        error: "Database error".to_string(),
                    }),
                )
                    .into_response();
            }
        };

        // 创建管理层用户(目前所有通过邀请码注册的都是管理层)
        User::new_management(
            req.username.clone(),
            req.name.clone(),
            password_hash,
            2 + management_users.len() as u32, // 管理层工号从00002开始
            req.email.clone(),
        )
    };

    // 保存用户到数据库
    if let Err(e) = state.store.save_user(&user_to_create).await {
        error!("Failed to save user: {}", e);
        return (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: "Failed to register user".to_string(),
            }),
        )
            .into_response();
    }

    // If it's corporate chairman or management, add user to Cliff of Contemplation Line
    if matches!(
        user_to_create.position,
        crate::domain::user::Position::Chairman | crate::domain::user::Position::Management
    ) {
        // Create or update organization structure, add user to Cliff of Contemplation Line department
        let mut org = state
            .store
            .load_organization()
            .await
            .unwrap_or_else(|_| Organization::new());

        // Ensure Cliff of Contemplation Line department exists
        let guilty_cliff_dept_id = "guilty-cliff-line";
        let guilty_cliff_dept_name = "Cliff of Contemplation Line";

        // Check if department already exists
        let dept_exists = org.departments.iter().any(|d| d.id == guilty_cliff_dept_id);
        if !dept_exists {
            // Create Cliff of Contemplation Line department
            let dept = crate::domain::Department {
                id: guilty_cliff_dept_id.to_string(),
                name: guilty_cliff_dept_name.to_string(),
                parent_id: None, // Cliff of Contemplation Line is the top-level department
                leader_id: if matches!(
                    user_to_create.position,
                    crate::domain::user::Position::Chairman
                ) {
                    Some(user_to_create.id.clone())
                } else {
                    None
                },
            };
            org.add_department(dept);
        }

        // 检查是否已有对应的Agent,如果没有则创建
        let agent_exists = org.agents.iter().any(|a| a.id == user_to_create.id);

        if !agent_exists {
            // If Agent doesn't exist, create a corresponding Agent and add to Cliff of Contemplation Line
            let new_agent = Agent {
                id: user_to_create.id.clone(),
                name: user_to_create.name.clone(),
                role: if matches!(
                    user_to_create.position,
                    crate::domain::user::Position::Chairman
                ) {
                    Role::simple("Cliff of Contemplation Line Supervisor".to_string(), "You are the supervisor of the Cliff of Contemplation Line, responsible for overseeing and managing senior company affairs.".to_string())
                        .with_responsibilities(vec!["Corporate Chairman".to_string(), "Cliff of Contemplation Line Management".to_string()])
                        .with_expertise(vec!["Corporate Governance".to_string(), "Strategic Planning".to_string()])
                } else {
                    Role::simple("Cliff of Contemplation Line Member".to_string(), "You are a member of the Cliff of Contemplation Line, participating in senior management and decision-making processes.".to_string())
                        .with_responsibilities(vec!["Management Affairs".to_string(), "Collaborative Work".to_string()])
                        .with_expertise(vec!["Team Management".to_string(), "Cross-department Collaboration".to_string()])
                },
                department_id: Some(guilty_cliff_dept_id.to_string()),
                llm_config: LLMConfig::openai("fake-api-key".to_string()),
                description: None,
                watched_tools: vec![],
                trigger_conditions: vec![],
            };
            org.agents.push(new_agent);
        } else {
            // If Agent already exists, update its department information
            if let Some(agent) = org.agents.iter_mut().find(|a| a.id == user_to_create.id) {
                agent.department_id = Some(guilty_cliff_dept_id.to_string());
                if matches!(
                    user_to_create.position,
                    crate::domain::user::Position::Chairman
                ) {
                    // Corporate chairman becomes Cliff of Contemplation Line supervisor
                    agent.role = Role::simple("Cliff of Contemplation Line Supervisor".to_string(), "You are the supervisor of the Cliff of Contemplation Line, responsible for overseeing and managing senior company affairs.".to_string())
                        .with_responsibilities(vec!["Corporate Chairman".to_string(), "Cliff of Contemplation Line Management".to_string()])
                        .with_expertise(vec!["Corporate Governance".to_string(), "Strategic Planning".to_string()]);

                    // Also update department leader
                    if let Some(dept) = org
                        .departments
                        .iter_mut()
                        .find(|d| d.id == guilty_cliff_dept_id)
                    {
                        dept.leader_id = Some(user_to_create.id.clone());
                    }
                } else {
                    // Management member
                    agent.role = Role::simple("Cliff of Contemplation Line Member".to_string(), "You are a member of the Cliff of Contemplation Line, participating in senior management and decision-making processes.".to_string())
                        .with_responsibilities(vec!["Management Affairs".to_string(), "Collaborative Work".to_string()])
                        .with_expertise(vec!["Team Management".to_string(), "Cross-department Collaboration".to_string()]);
                }
            }
        }

        // 保存更新后的组织架构
        if let Err(e) = state.store.save_organization(&org).await {
            error!("Failed to update organization for guilty cliff line: {}", e);
        }

        // Also update user's department information
        let mut updated_user = user_to_create.clone();
        updated_user.department = guilty_cliff_dept_name.to_string(); // Set user department to "Cliff of Contemplation Line"

        // Save updated user information
        if let Err(e) = state.store.save_user(&updated_user).await {
            error!(
                "Failed to update user department for guilty cliff line: {}",
                e
            );
        }
    }

    // Use updated user information (if there was an update)
    let final_user = if matches!(
        user_to_create.position,
        crate::domain::user::Position::Chairman | crate::domain::user::Position::Management
    ) {
        // If it's corporate chairman or management, use updated user information
        let updated_user = match state
            .store
            .load_user_by_username(&user_to_create.username)
            .await
        {
            Ok(Some(user)) => user,
            _ => user_to_create.clone(), // 如果加载失败,使用原始用户
        };
        updated_user
    } else {
        user_to_create.clone()
    };

    // 生成JWT令牌
    let token: String = match state.jwt_service.generate_token(&UserInfo {
        id: final_user.id.clone(),
        username: final_user.username.clone(),
        name: final_user.name.clone(),
        email: final_user.email.clone(),
        is_director: matches!(
            final_user.position,
            crate::domain::user::Position::Chairman | crate::domain::user::Position::Management
        ),
        employee_id: final_user.employee_id.clone(),
        position: format!("{:?}", final_user.position),
        department: final_user.department.clone(),
    }) {
        Ok(token) => token,
        Err(e) => {
            error!("Failed to generate token: {}", e);
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: "Failed to generate token".to_string(),
                }),
            )
                .into_response();
        }
    };

    Json(serde_json::json!({
        "success": true,
        "data": {
            "token": token,
            "user": {
                "id": final_user.id,
                "username": final_user.username,
                "name": final_user.name,
                "email": final_user.email,
                "is_director": matches!(final_user.position, crate::domain::user::Position::Chairman | crate::domain::user::Position::Management),
                "employee_id": final_user.employee_id,
                "position": format!("{:?}", final_user.position),
                "department": final_user.department,
            }
        }
    })).into_response()
}

/// 检查用户名
async fn check_username(
    Query(params): Query<std::collections::HashMap<String, String>>,
) -> impl IntoResponse {
    let username = params.get("username").cloned().unwrap_or_default();
    let exists = username == "admin" || username == "director";

    Json(serde_json::json!({
        "success": true,
        "data": {
            "exists": exists,
            "available": !exists,
        }
    }))
}

/// WebSocket 处理
async fn websocket_handler(
    ws: WebSocketUpgrade,
    State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
    ws.on_upgrade(move |socket| handle_websocket(socket, state))
}

async fn handle_websocket(mut socket: axum::extract::ws::WebSocket, state: Arc<AppState>) {
    let mut rx = state.message_tx.subscribe();

    info!("WebSocket connection established");

    loop {
        tokio::select! {
            // 接收消息
            Ok(message) = rx.recv() => {
                let to_str = match &message.to {
                    MessageTarget::Direct(id) => Some(id.clone()),
                    MessageTarget::Group(id) => Some(format!("group:{}", id)),
                };

                let msg_json = serde_json::json!({
                    "type": "message",
                    "data": {
                        "id": message.id,
                        "from": message.from,
                        "to": to_str,
                        "content": message.content,
                        "timestamp": message.timestamp,
                    }
                });

                if let Err(e) = socket.send(axum::extract::ws::Message::Text(
                    msg_json.to_string().into()
                )).await {
                    error!("WebSocket send error: {}", e);
                    break;
                }
            }

            // 接收客户端消息
            Some(Ok(msg)) = socket.recv() => {
                match msg {
                    axum::extract::ws::Message::Close(_) => {
                        info!("WebSocket connection closed");
                        break;
                    }
                    axum::extract::ws::Message::Ping(ping) => {
                        if let Err(e) = socket.send(axum::extract::ws::Message::Pong(ping)).await {
                            error!("WebSocket pong error: {}", e);
                            break;
                        }
                    }
                    axum::extract::ws::Message::Text(text) => {
                        // 解析客户端发送的消息
                        if let Ok(client_message) = serde_json::from_str::<ClientMessage>(&text) {
                            match client_message {
                                ClientMessage::SendMessage { from, to, content } => {
                                    // 验证消息参数
                                    if from.is_empty() || to.is_empty() || content.is_empty() {
                                        // 发送错误响应
                                        let error_msg = serde_json::json!({
                                            "type": "error",
                                            "message": "Invalid message format: missing required fields"
                                        });

                                        if let Err(e) = socket.send(axum::extract::ws::Message::Text(
                                            error_msg.to_string().into()
                                        )).await {
                                            error!("WebSocket send error: {}", e);
                                            break;
                                        }
                                        continue;
                                    }

                                    // 构造消息目标
                                    let target = if to.starts_with("group:") {
                                        MessageTarget::Group(to.strip_prefix("group:").unwrap_or(&to).to_string())
                                    } else {
                                        MessageTarget::Direct(to)
                                    };

                                    // 创建消息
                                    let message = Message {
                                        id: uuid::Uuid::new_v4().to_string(),
                                        from,
                                        to: target,
                                        content,
                                        timestamp: Utc::now().timestamp(),
                                        reply_to: None,
                                        mentions: Vec::new(),
                                    };

                                    // 发送消息到消息总线
                                    if let Err(e) = state.message_tx.send(message.clone()) {
                                        error!("Failed to send message: {}", e);
                                    }
                                }
                                ClientMessage::Ping => {
                                    // 回复pong消息
                                    let pong_msg = serde_json::json!({
                                        "type": "pong"
                                    });

                                    if let Err(e) = socket.send(axum::extract::ws::Message::Text(
                                        pong_msg.to_string().into()
                                    )).await {
                                        error!("WebSocket send error: {}", e);
                                        break;
                                    }
                                }
                            }
                        } else {
                            // 解析JSON失败,发送错误响应
                            let error_msg = serde_json::json!({
                                "type": "error",
                                "message": "Invalid JSON format"
                            });

                            if let Err(e) = socket.send(axum::extract::ws::Message::Text(
                                error_msg.to_string().into()
                            )).await {
                                error!("WebSocket send error: {}", e);
                                break;
                            }
                        }
                    }
                    _ => {
                        // 忽略其他类型的消息
                        continue;
                    }
                }
            }
        }
    }
}

// ==================== 管理API ====================

#[derive(Deserialize)]
pub struct CreateInviteCodeRequest {
    pub max_usage: Option<u32>,
    pub expires_at: Option<String>, // ISO 8601 format
}

/// 检查用户是否具有管理员权限
async fn check_admin_permission(state: &AppState, token: &str) -> Option<UserInfo> {
    match state.jwt_service.validate_token(token) {
        Ok(user_info) => {
            // 检查是否为董事长或管理层(这里暂时认为所有管理层及以上都是管理员)
            if matches!(user_info.position.as_str(), "Chairman" | "Management") {
                Some(user_info)
            } else {
                None
            }
        }
        Err(_) => None,
    }
}

/// 获取所有邀请码(仅管理员)
async fn get_invite_codes(
    State(state): State<Arc<AppState>>,
    headers: HeaderMap,
) -> impl IntoResponse {
    let auth_header = headers
        .get("authorization")
        .and_then(|value| value.to_str().ok());

    if let Some(auth_str) = auth_header {
        if auth_str.starts_with("Bearer ") {
            let token = auth_str.strip_prefix("Bearer ").unwrap();

            if check_admin_permission(&state, token).await.is_some() {
                match state.store.load_invitation_codes().await {
                    Ok(codes) => {
                        // 转换为前端友好的格式
                        let codes_data: Vec<serde_json::Value> = codes
                            .into_iter()
                            .map(|code| {
                                serde_json::json!({
                                    "id": code.id,
                                    "code": code.code,
                                    "created_by": code.created_by,
                                    "created_at": code.created_at,
                                    "expiry_time": code.expiry_time,
                                    "is_used": !code.is_valid(),
                                    "usage_count": code.current_usage,
                                    "max_usage": code.max_usage,
                                })
                            })
                            .collect();

                        return Json(serde_json::json!({
                            "success": true,
                            "data": codes_data
                        }))
                        .into_response();
                    }
                    Err(e) => {
                        error!("Failed to load invitation codes: {}", e);
                        return (
                            StatusCode::INTERNAL_SERVER_ERROR,
                            Json(ErrorResponse {
                                error: "Failed to load invitation codes".to_string(),
                            }),
                        )
                            .into_response();
                    }
                }
            }
        }
    }

    (
        StatusCode::FORBIDDEN,
        Json(ErrorResponse {
            error: "Insufficient permissions".to_string(),
        }),
    )
        .into_response()
}

/// 创建邀请码(仅管理员)
async fn create_invite_code(
    State(state): State<Arc<AppState>>,
    headers: HeaderMap,
    Json(req): Json<CreateInviteCodeRequest>,
) -> impl IntoResponse {
    let auth_header = headers
        .get("authorization")
        .and_then(|value| value.to_str().ok());

    if let Some(auth_str) = auth_header {
        if auth_str.starts_with("Bearer ") {
            let token = auth_str.strip_prefix("Bearer ").unwrap();

            if let Some(user_info) = check_admin_permission(&state, token).await {
                // 创建邀请码
                let mut new_code = InvitationCode::new(user_info.id.clone(), req.max_usage);

                // 如果指定了过期时间,使用指定的时间,否则使用默认的1天
                if let Some(expires_at_str) = req.expires_at {
                    if let Ok(expires_at) = chrono::DateTime::parse_from_rfc3339(&expires_at_str) {
                        new_code.expiry_time = expires_at.timestamp();
                    }
                }

                match state.store.save_invitation_code(&new_code).await {
                    Ok(_) => {
                        return Json(serde_json::json!({
                            "success": true,
                            "data": {
                                "id": new_code.id,
                                "code": new_code.code,
                                "created_by": new_code.created_by,
                                "created_at": new_code.created_at,
                                "expires_at": new_code.expiry_time,
                                "max_usage": new_code.max_usage,
                                "usage_count": new_code.current_usage,
                                "is_active": new_code.is_valid(),
                            }
                        }))
                        .into_response();
                    }
                    Err(e) => {
                        error!("Failed to save invitation code: {}", e);
                        return (
                            StatusCode::INTERNAL_SERVER_ERROR,
                            Json(ErrorResponse {
                                error: "Failed to save invitation code".to_string(),
                            }),
                        )
                            .into_response();
                    }
                }
            }
        }
    }

    (
        StatusCode::FORBIDDEN,
        Json(ErrorResponse {
            error: "Insufficient permissions".to_string(),
        }),
    )
        .into_response()
}

/// 删除邀请码(仅管理员)
async fn delete_invite_code(
    State(state): State<Arc<AppState>>,
    headers: HeaderMap,
    Path(code_id): Path<String>,
) -> impl IntoResponse {
    let auth_header = headers
        .get("authorization")
        .and_then(|value| value.to_str().ok());

    if let Some(auth_str) = auth_header {
        if auth_str.starts_with("Bearer ") {
            let token = auth_str.strip_prefix("Bearer ").unwrap();

            if check_admin_permission(&state, token).await.is_some() {
                // 由于我们的存储接口没有直接的删除方法,我们需要先加载所有邀请码,找到要删除的,然后不保存它
                match state.store.load_invitation_codes().await {
                    Ok(mut codes) => {
                        // 查找要删除的邀请码
                        if let Some(pos) = codes.iter().position(|c| c.id == code_id) {
                            // 从数据库中移除(通过重新保存其他码)
                            codes.remove(pos);

                            // 重新保存剩余的邀请码
                            for code in codes {
                                if let Err(e) = state.store.save_invitation_code(&code).await {
                                    error!(
                                        "Failed to update invitation codes after deletion: {}",
                                        e
                                    );
                                    return (
                                        StatusCode::INTERNAL_SERVER_ERROR,
                                        Json(ErrorResponse {
                                            error: "Failed to update invitation codes".to_string(),
                                        }),
                                    )
                                        .into_response();
                                }
                            }

                            return Json(serde_json::json!({
                                "success": true,
                                "message": "Invitation code deleted successfully"
                            }))
                            .into_response();
                        } else {
                            return (
                                StatusCode::NOT_FOUND,
                                Json(ErrorResponse {
                                    error: "Invitation code not found".to_string(),
                                }),
                            )
                                .into_response();
                        }
                    }
                    Err(e) => {
                        error!("Failed to load invitation codes: {}", e);
                        return (
                            StatusCode::INTERNAL_SERVER_ERROR,
                            Json(ErrorResponse {
                                error: "Failed to load invitation codes".to_string(),
                            }),
                        )
                            .into_response();
                    }
                }
            }
        }
    }

    (
        StatusCode::FORBIDDEN,
        Json(ErrorResponse {
            error: "Insufficient permissions".to_string(),
        }),
    )
        .into_response()
}

/// 获取聊天会话列表
async fn list_chat_sessions(State(state): State<Arc<AppState>>) -> impl IntoResponse {
    // 从组织架构中获取Agent信息来构建会话列表
    match state.store.load_organization().await {
        Ok(org) => {
            let sessions: Vec<serde_json::Value> = org
                .agents
                .into_iter()
                .map(|agent| {
                    serde_json::json!({
                        "id": agent.id,
                        "name": agent.name,
                        "participants": [{
                            "id": agent.id,
                            "name": agent.name,
                            "isAgent": true,
                            "status": "online",  // 假设Agent始终在线
                            "description": agent.description,
                        }],
                        "lastMessage": null,
                        "unreadCount": 0,
                        "createdAt": chrono::Utc::now().timestamp(),
                        "updatedAt": chrono::Utc::now().timestamp()
                    })
                })
                .collect();

            Json(serde_json::json!({
                "success": true,
                "data": sessions
            }))
            .into_response()
        }
        Err(e) => {
            error!("Failed to load organization for chat sessions: {}", e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: "Failed to load chat sessions".to_string(),
                }),
            )
                .into_response()
        }
    }
}

/// 获取特定会话的消息
async fn get_session_messages(
    State(state): State<Arc<AppState>>,
    Path(session_id): Path<String>,
) -> impl IntoResponse {
    // 获取该Agent相关的所有消息
    let filter = crate::core::store::MessageFilter {
        from: Some(session_id.clone()), // 消息来自该Agent
        target_type: None,
        to: None,
        since: None,
        limit: 50, // 限制返回50条消息
    };

    match state.store.load_messages(filter).await {
        Ok(messages) => {
            // 转换消息格式以匹配前端期望
            let formatted_messages: Vec<serde_json::Value> = messages
                .into_iter()
                .map(|msg| {
                    // 获取发送者信息
                    let sender = if msg.from == session_id {
                        // 如果是Agent发送的
                        serde_json::json!({
                            "id": msg.from,
                            "name": get_agent_name_by_id(&state.agents, &msg.from),
                            "isAgent": true
                        })
                    } else {
                        // 如果是用户发送的
                        serde_json::json!({
                            "id": msg.from,
                            "name": "Unknown User",
                            "isAgent": false
                        })
                    };

                    serde_json::json!({
                        "id": msg.id,
                        "sender": sender,
                        "content": msg.content,
                        "timestamp": msg.timestamp,
                        "replyTo": msg.reply_to,
                        "mentions": msg.mentions
                    })
                })
                .collect();

            Json(serde_json::json!({
                "success": true,
                "data": formatted_messages
            }))
            .into_response()
        }
        Err(e) => {
            error!("Failed to load messages for session {}: {}", session_id, e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: "Failed to load messages".to_string(),
                }),
            )
                .into_response()
        }
    }
}

/// 根据ID获取Agent名称的辅助函数
fn get_agent_name_by_id(agents: &[Agent], agent_id: &str) -> String {
    agents
        .iter()
        .find(|agent| agent.id == agent_id)
        .map(|agent| agent.name.clone())
        .unwrap_or_else(|| "Unknown Agent".to_string())
}

/// 获取组织架构树
async fn get_org_tree(State(state): State<Arc<AppState>>) -> impl IntoResponse {
    match state.store.load_organization().await {
        Ok(org) => {
            // First, convert flat department list to tree structure
            let mut departments_map: std::collections::HashMap<String, serde_json::Value> =
                std::collections::HashMap::new();

            // Step 1: Create basic structure for all departments
            for dept in &org.departments {
                let agent_leaders: Vec<serde_json::Value> = org
                    .agents
                    .iter()
                    .filter(|agent| {
                        agent.department_id.as_ref() == Some(&dept.id)
                            && agent.role.title.contains("主管")
                    })
                    .map(|agent| {
                        serde_json::json!({
                            "id": agent.id,
                            "name": agent.name,
                            "title": agent.role.title,
                            "status": "online",
                            "description": agent.description
                        })
                    })
                    .collect();

                let dept_agents: Vec<serde_json::Value> = org
                    .agents
                    .iter()
                    .filter(|agent| {
                        agent.department_id.as_ref() == Some(&dept.id)
                            && !agent.role.title.contains("主管")
                    })
                    .map(|agent| {
                        serde_json::json!({
                            "id": agent.id,
                            "name": agent.name,
                            "title": agent.role.title,
                            "status": "online",
                            "description": agent.description
                        })
                    })
                    .collect();

                let leader = if !agent_leaders.is_empty() {
                    agent_leaders.first().cloned()
                } else {
                    None
                };

                let users = [&agent_leaders[..], &dept_agents[..]].concat();

                departments_map.insert(
                    dept.id.clone(),
                    serde_json::json!({
                        "id": dept.id,
                        "name": dept.name,
                        "parentId": dept.parent_id,
                        "leader": leader,
                        "users": users,
                        "memberCount": users.len(),
                        "children": Vec::<serde_json::Value>::new() // 初始化为空数组
                    }),
                );
            }

            // Step 2: Build parent-child relationships (add child departments to parent department's children array)
            let mut processed_depts = std::collections::HashMap::new();
            for dept in &org.departments {
                if let Some(mut dept_info) = departments_map.get(&dept.id).cloned() {
                    // Find all child departments with this department as parent
                    let child_depts: Vec<serde_json::Value> = org
                        .departments
                        .iter()
                        .filter(|child| child.parent_id.as_ref() == Some(&dept.id))
                        .filter_map(|child| processed_depts.get(&child.id).cloned())
                        .collect();

                    // Update department information, add child departments
                    if !child_depts.is_empty() {
                        if let serde_json::Value::Object(ref mut obj) = dept_info {
                            obj.insert(
                                "children".to_string(),
                                serde_json::Value::Array(child_depts),
                            );
                        }
                    }

                    processed_depts.insert(dept.id.clone(), dept_info);
                }
            }

            // Finally get root departments (departments without parent)
            let root_depts: Vec<serde_json::Value> = org
                .departments
                .iter()
                .filter(|dept| dept.parent_id.is_none())
                .filter_map(|dept| processed_depts.get(&dept.id).cloned())
                .collect();

            // 同时返回扁平的agents列表用于其他用途(目前未使用)
            let _agents: Vec<serde_json::Value> = org
                .agents
                .iter()
                .map(|agent| {
                    serde_json::json!({
                        "id": agent.id,
                        "name": agent.name,
                        "title": agent.role.title,
                        "departmentId": agent.department_id,
                        "status": "online",  // 假设Agent始终在线
                        "isOnline": true
                    })
                })
                .collect();

            Json(serde_json::json!({
                "success": true,
                "data": root_depts  // Directly return department array, matching frontend expectation
            }))
            .into_response()
        }
        Err(e) => {
            error!("Failed to load organization tree: {}", e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: "Failed to load organization tree".to_string(),
                }),
            )
                .into_response()
        }
    }
}

/// 获取所有用户(仅管理员)
async fn get_users(State(state): State<Arc<AppState>>, headers: HeaderMap) -> impl IntoResponse {
    let auth_header = headers
        .get("authorization")
        .and_then(|value| value.to_str().ok());

    if let Some(auth_str) = auth_header {
        if auth_str.starts_with("Bearer ") {
            let token = auth_str.strip_prefix("Bearer ").unwrap();

            if check_admin_permission(&state, token).await.is_some() {
                match state.store.load_users().await {
                    Ok(users) => {
                        // 转换为前端友好的格式
                        let users_data: Vec<serde_json::Value> = users.into_iter().map(|user| {
                            serde_json::json!({
                                "id": user.id,
                                "username": user.username,
                                "name": user.name,
                                "email": user.email,
                                "employee_id": user.employee_id,
                                "position": format!("{:?}", user.position),
                                "department": user.department,
                                "created_at": user.created_at,
                                "is_director": matches!(user.position, crate::domain::user::Position::Chairman | crate::domain::user::Position::Management),
                            })
                        }).collect();

                        return Json(serde_json::json!({
                            "success": true,
                            "data": users_data
                        }))
                        .into_response();
                    }
                    Err(e) => {
                        error!("Failed to load users: {}", e);
                        return (
                            StatusCode::INTERNAL_SERVER_ERROR,
                            Json(ErrorResponse {
                                error: "Failed to load users".to_string(),
                            }),
                        )
                            .into_response();
                    }
                }
            }
        }
    }

    (
        StatusCode::FORBIDDEN,
        Json(ErrorResponse {
            error: "Insufficient permissions".to_string(),
        }),
    )
        .into_response()
}

// ==================== 路由 ====================

pub fn create_router(state: Arc<AppState>) -> Router {
    let cors = CorsLayer::permissive();

    Router::new()
        .route("/api/health", get(health_check))
        .route("/api/company", get(get_company))
        .route("/api/agents", get(list_agents))
        .route("/api/agents/{id}", get(get_agent))
        .route("/api/messages", post(send_message))
        .route("/api/auth/login", post(login))
        .route("/api/auth/register", post(register))
        .route("/api/auth/check-username", get(check_username))
        .route("/api/auth/current", get(get_current_user))
        .route(
            "/api/admin/invite-codes",
            get(get_invite_codes).post(create_invite_code),
        )
        .route("/api/admin/invite-codes/{id}", delete(delete_invite_code))
        .route("/api/chat/list", get(list_chat_sessions))
        .route("/api/chat/{session_id}/messages", get(get_session_messages))
        .route("/api/org/tree", get(get_org_tree))
        .route("/api/admin/users", get(get_users))
        .route("/ws", get(websocket_handler))
        .layer(cors)
        .with_state(state)
}

// ==================== 服务器启动 ====================

pub async fn start_web_server(
    bind_addr: &str,
    agents: Vec<Agent>,
    message_tx: broadcast::Sender<Message>,
    store: Arc<dyn crate::core::store::Store>,
) -> anyhow::Result<()> {
    // 创建JWT服务
    let jwt_secret =
        std::env::var("JWT_SECRET").unwrap_or_else(|_| "default_secret_key_for_dev".to_string());
    let jwt_service = JwtService::new(&jwt_secret);

    let state = Arc::new(AppState {
        agents,
        message_tx,
        store,
        jwt_service,
    });

    let app = create_router(state);

    let listener = tokio::net::TcpListener::bind(bind_addr).await?;
    info!("Web server started on http://{}", bind_addr);

    axum::serve(listener, app).await?;

    Ok(())
}