feat(KFC): 更新聊天处理器和回复模块,优化动作名称及上下文构建逻辑
This commit is contained in:
@@ -57,12 +57,40 @@ class ChatterManager:
|
||||
|
||||
self.stats["chatters_registered"] += 1
|
||||
|
||||
def get_chatter_class(self, chat_type: ChatType) -> type | None:
|
||||
"""获取指定聊天类型的聊天处理器类"""
|
||||
if chat_type in self.chatter_classes:
|
||||
return self.chatter_classes[chat_type][0]
|
||||
def get_chatter_class_for_chat_type(self, chat_type: ChatType) -> type | None:
|
||||
"""
|
||||
获取指定聊天类型的最佳聊天处理器类
|
||||
|
||||
优先级规则:
|
||||
1. 优先选择明确匹配当前聊天类型的 Chatter(如 PRIVATE 或 GROUP)
|
||||
2. 如果没有精确匹配,才使用 ALL 类型的 Chatter
|
||||
|
||||
Args:
|
||||
chat_type: 聊天类型
|
||||
|
||||
Returns:
|
||||
最佳匹配的聊天处理器类,如果没有匹配则返回 None
|
||||
"""
|
||||
# 1. 首先尝试精确匹配(排除 ALL 类型)
|
||||
if chat_type != ChatType.ALL and chat_type in self.chatter_classes:
|
||||
chatter_list = self.chatter_classes[chat_type]
|
||||
if chatter_list:
|
||||
logger.debug(f"找到精确匹配的聊天处理器: {chatter_list[0].__name__} for {chat_type.value}")
|
||||
return chatter_list[0]
|
||||
|
||||
# 2. 如果没有精确匹配,回退到 ALL 类型
|
||||
if ChatType.ALL in self.chatter_classes:
|
||||
chatter_list = self.chatter_classes[ChatType.ALL]
|
||||
if chatter_list:
|
||||
logger.debug(f"使用通用聊天处理器: {chatter_list[0].__name__} for {chat_type.value}")
|
||||
return chatter_list[0]
|
||||
|
||||
return None
|
||||
|
||||
def get_chatter_class(self, chat_type: ChatType) -> type | None:
|
||||
"""获取指定聊天类型的聊天处理器类(兼容旧接口)"""
|
||||
return self.get_chatter_class_for_chat_type(chat_type)
|
||||
|
||||
def get_supported_chat_types(self) -> list[ChatType]:
|
||||
"""获取支持的聊天类型列表"""
|
||||
return list(self.chatter_classes.keys())
|
||||
@@ -112,29 +140,29 @@ class ChatterManager:
|
||||
logger.error("schedule unread cleanup failed", stream_id=stream_id, error=runtime_error)
|
||||
|
||||
async def process_stream_context(self, stream_id: str, context: "StreamContext") -> dict:
|
||||
"""处理流上下文"""
|
||||
"""
|
||||
处理流上下文
|
||||
|
||||
每个聊天流只能有一个活跃的 Chatter 组件。
|
||||
选择优先级:明确指定聊天类型的 Chatter > ALL 类型的 Chatter
|
||||
"""
|
||||
chat_type = context.chat_type
|
||||
chat_type_value = chat_type.value
|
||||
logger.debug("处理流上下文", stream_id=stream_id, chat_type=chat_type_value)
|
||||
|
||||
self._ensure_chatter_registry()
|
||||
|
||||
chatter_class = self.get_chatter_class(chat_type)
|
||||
if not chatter_class:
|
||||
all_chatter_class = self.get_chatter_class(ChatType.ALL)
|
||||
if all_chatter_class:
|
||||
chatter_class = all_chatter_class
|
||||
logger.info(
|
||||
"回退到通用聊天处理器",
|
||||
stream_id=stream_id,
|
||||
requested_type=chat_type_value,
|
||||
fallback=ChatType.ALL.value,
|
||||
)
|
||||
else:
|
||||
# 检查是否已有该流的 Chatter 实例
|
||||
stream_instance = self.instances.get(stream_id)
|
||||
|
||||
if stream_instance is None:
|
||||
# 使用新的优先级选择逻辑获取最佳 Chatter 类
|
||||
chatter_class = self.get_chatter_class_for_chat_type(chat_type)
|
||||
|
||||
if not chatter_class:
|
||||
raise ValueError(f"No chatter registered for chat type {chat_type}")
|
||||
|
||||
stream_instance = self.instances.get(stream_id)
|
||||
if stream_instance is None:
|
||||
# 创建新实例
|
||||
stream_instance = chatter_class(stream_id=stream_id, action_manager=self.action_manager)
|
||||
self.instances[stream_id] = stream_instance
|
||||
logger.info(
|
||||
@@ -143,6 +171,13 @@ class ChatterManager:
|
||||
chatter_class=chatter_class.__name__,
|
||||
chat_type=chat_type_value,
|
||||
)
|
||||
else:
|
||||
# 已有实例,直接使用(每个流只有一个活跃的 Chatter)
|
||||
logger.debug(
|
||||
"使用已有聊天处理器实例",
|
||||
stream_id=stream_id,
|
||||
chatter_class=stream_instance.__class__.__name__,
|
||||
)
|
||||
|
||||
self.stats["streams_processed"] += 1
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user