主协调器
| 1482 | |
| 1483 | |
| 1484 | class MasterCoordinator: |
| 1485 | """主协调器""" |
| 1486 | |
| 1487 | def __init__( |
| 1488 | self, |
| 1489 | user_id: Optional[str] = None, |
| 1490 | feishu_user_id: Optional[str] = None, |
| 1491 | chat_id: Optional[str] = None, |
| 1492 | send_to_feishu: bool = True, |
| 1493 | ): |
| 1494 | """ |
| 1495 | 初始化协调器 |
| 1496 | |
| 1497 | Args: |
| 1498 | user_id: 用户 ID(可选,如果为空则从 roles.json 获取) |
| 1499 | feishu_user_id: 飞书用户 ID(用于 open_id 发送) |
| 1500 | chat_id: 聊天 ID(用于 chat_id 发送,优先级更高) |
| 1501 | send_to_feishu: 是否允许发送飞书消息;命令行 dry-run/--no-feishu 会关闭 |
| 1502 | """ |
| 1503 | # 优先使用传入的 user_id,如果没有则从 roles.json 获取 |
| 1504 | if user_id: |
| 1505 | self.user_id = user_id |
| 1506 | else: |
| 1507 | roles_user_id = get_current_user_id() |
| 1508 | self.user_id = roles_user_id if roles_user_id != "user_default" else "user_unknown" |
| 1509 | self.send_to_feishu = send_to_feishu |
| 1510 | self.feishu_user_id = (feishu_user_id or os.environ.get("FEISHU_USER_ID", "")) if send_to_feishu else "" |
| 1511 | self.profile = get_profile(self.user_id) |
| 1512 | self.role_meta = get_role_meta_for_user(self.user_id) or {} |
| 1513 | self.role_name = self.role_meta.get("role_name") |
| 1514 | # 优先使用传入 chat_id,否则回退到角色绑定的 chat_id,避免误发到默认个人账号 |
| 1515 | self.chat_id = (chat_id or resolve_role_chat_id(self.user_id, self.profile)) if send_to_feishu else None |
| 1516 | |
| 1517 | def detect_intent(self, text: str) -> Dict[str, Any]: |
| 1518 | """ |
| 1519 | 检测用户意图 |
| 1520 | |
| 1521 | Args: |
| 1522 | text: 用户输入文本 |
| 1523 | |
| 1524 | Returns: |
| 1525 | 意图字典 {"intent": "...", "confidence": 0.0-1.0, "slots": {}} |
| 1526 | """ |
| 1527 | cleaned = first_meaningful_line(text) |
| 1528 | text_lower = cleaned.lower().strip() |
| 1529 | |
| 1530 | if looks_like_bot_authored_message(text): |
| 1531 | return {"intent": "ignore", "confidence": 1.0, "slots": {}} |
| 1532 | |
| 1533 | explicit_command = detect_explicit_command_intent(text) |
| 1534 | if explicit_command: |
| 1535 | return explicit_command |
| 1536 | |
| 1537 | # 角色管理 |
| 1538 | role_keywords = ["角色", "role", "切换", "create role", "删除角色"] |
| 1539 | if any(kw in text_lower for kw in role_keywords): |
| 1540 | return {"intent": "role_manager", "confidence": 0.85, "slots": {"command": text}} |
| 1541 |
no outgoing calls
no test coverage detected