refactor(core): make chat utils async and decouple plugin dependencies
- Convert `get_recent_group_speaker` and `count_messages_between` in `chat/utils` to async functions to align with underlying asynchronous database calls, preventing potential blocking issues. - Implement optional import for `sleep_system` within the `affinity_flow_chatter` plugin using a try-except block. This decouples the hard dependency between the two plugins, enhancing modularity and ensuring the chatter plugin remains operational even if the sleep system is unavailable.
This commit is contained in:
@@ -123,11 +123,11 @@ async def get_embedding(text, request_type="embedding") -> list[float] | None:
|
||||
return embedding
|
||||
|
||||
|
||||
def get_recent_group_speaker(chat_stream_id: str, sender, limit: int = 12) -> list:
|
||||
async def get_recent_group_speaker(chat_stream_id: str, sender, limit: int = 12) -> list:
|
||||
# 获取当前群聊记录内发言的人
|
||||
filter_query = {"chat_id": chat_stream_id}
|
||||
sort_order = [("time", -1)]
|
||||
recent_messages = find_messages(message_filter=filter_query, sort=sort_order, limit=limit)
|
||||
recent_messages = await find_messages(message_filter=filter_query, sort=sort_order, limit=limit)
|
||||
|
||||
if not recent_messages:
|
||||
return []
|
||||
@@ -565,7 +565,7 @@ def cosine_similarity(v1, v2):
|
||||
def text_to_vector(text):
|
||||
"""将文本转换为词频向量"""
|
||||
# 分词
|
||||
words = rjieba.lcut(text)
|
||||
words = rjieba.lcut(text) # type: ignore
|
||||
return Counter(words)
|
||||
|
||||
|
||||
@@ -669,8 +669,7 @@ def get_western_ratio(paragraph):
|
||||
return western_count / len(alnum_chars)
|
||||
|
||||
|
||||
def count_messages_between(start_time: float, end_time: float, stream_id: str) -> tuple[int, int] | tuple[
|
||||
Coroutine[Any, Any, int], int]:
|
||||
async def count_messages_between(start_time: float, end_time: float, stream_id: str) -> tuple[int, int]:
|
||||
"""计算两个时间点之间的消息数量和文本总长度
|
||||
|
||||
Args:
|
||||
@@ -699,10 +698,10 @@ def count_messages_between(start_time: float, end_time: float, stream_id: str) -
|
||||
|
||||
try:
|
||||
# 先获取消息数量
|
||||
count = count_messages(filter_query)
|
||||
count = await count_messages(filter_query)
|
||||
|
||||
# 获取消息内容计算总长度
|
||||
messages = find_messages(message_filter=filter_query)
|
||||
messages = await find_messages(message_filter=filter_query)
|
||||
total_length = sum(len(msg.get("processed_plain_text", "")) for msg in messages)
|
||||
|
||||
return count, total_length
|
||||
|
||||
@@ -159,12 +159,19 @@ class ChatterPlanFilter:
|
||||
from src.chat.message_manager.message_manager import message_manager
|
||||
|
||||
angry_prompt_addition = ""
|
||||
wakeup_mgr = message_manager.wakeup_manager
|
||||
try:
|
||||
from src.plugins.built_in.sleep_system.api import get_wakeup_manager
|
||||
wakeup_mgr = get_wakeup_manager()
|
||||
except ImportError:
|
||||
logger.debug("无法导入睡眠系统API,将跳过相关检查。")
|
||||
wakeup_mgr = None
|
||||
|
||||
if wakeup_mgr:
|
||||
|
||||
# 双重检查确保愤怒状态不会丢失
|
||||
# 检查1: 直接从 wakeup_manager 获取
|
||||
if wakeup_mgr.is_in_angry_state():
|
||||
angry_prompt_addition = wakeup_mgr.get_angry_prompt_addition()
|
||||
if wakeup_mgr.is_in_angry_state():
|
||||
angry_prompt_addition = wakeup_mgr.get_angry_prompt_addition()
|
||||
|
||||
# 检查2: 如果上面没获取到,再从 mood_manager 确认
|
||||
if not angry_prompt_addition:
|
||||
|
||||
Reference in New Issue
Block a user