refactor: 重构心流聊天模块,简化频率控制; 删除 frequency_control.py 和 hfc_utils_old.py 旧文件; 新增 heartflow_manager.py 统一管理心流聊天; 将 HeartFChatting.adjust_talk_frequency 改为同步方法; 更新消息处理器使用新的 heartflow_manager;
This commit is contained in:
committed by
DrSmoothl
parent
e303fbeb6b
commit
021463b9f9
@@ -1,50 +0,0 @@
|
||||
from typing import Dict
|
||||
|
||||
from src.common.logger import get_logger
|
||||
|
||||
logger = get_logger("frequency_control")
|
||||
|
||||
|
||||
class FrequencyControl:
|
||||
"""简化的频率控制类,仅管理不同chat_id的频率值"""
|
||||
|
||||
def __init__(self, chat_id: str):
|
||||
self.chat_id = chat_id
|
||||
# 发言频率调整值
|
||||
self.talk_frequency_adjust: float = 1.0
|
||||
|
||||
def get_talk_frequency_adjust(self) -> float:
|
||||
"""获取发言频率调整值"""
|
||||
return self.talk_frequency_adjust
|
||||
|
||||
def set_talk_frequency_adjust(self, value: float) -> None:
|
||||
"""设置发言频率调整值"""
|
||||
self.talk_frequency_adjust = max(0.1, min(5.0, value))
|
||||
|
||||
|
||||
class FrequencyControlManager:
|
||||
"""频率控制管理器,管理多个聊天流的频率控制实例"""
|
||||
|
||||
def __init__(self):
|
||||
self.frequency_control_dict: Dict[str, FrequencyControl] = {}
|
||||
|
||||
def get_or_create_frequency_control(self, chat_id: str) -> FrequencyControl:
|
||||
"""获取或创建指定聊天流的频率控制实例"""
|
||||
if chat_id not in self.frequency_control_dict:
|
||||
self.frequency_control_dict[chat_id] = FrequencyControl(chat_id)
|
||||
return self.frequency_control_dict[chat_id]
|
||||
|
||||
def remove_frequency_control(self, chat_id: str) -> bool:
|
||||
"""移除指定聊天流的频率控制实例"""
|
||||
if chat_id in self.frequency_control_dict:
|
||||
del self.frequency_control_dict[chat_id]
|
||||
return True
|
||||
return False
|
||||
|
||||
def get_all_chat_ids(self) -> list[str]:
|
||||
"""获取所有有频率控制的聊天ID"""
|
||||
return list(self.frequency_control_dict.keys())
|
||||
|
||||
|
||||
# 创建全局实例
|
||||
frequency_control_manager = FrequencyControlManager()
|
||||
@@ -119,7 +119,7 @@ class HeartFChatting:
|
||||
|
||||
logger.info(f"{self.log_prefix} HeartFChatting 已停止")
|
||||
|
||||
async def adjust_talk_frequency(self, new_value: float):
|
||||
def adjust_talk_frequency(self, new_value: float):
|
||||
"""调整发言频率的调整值
|
||||
|
||||
Args:
|
||||
|
||||
@@ -10,8 +10,8 @@ from src.chat.brain_chat.brain_chat import BrainChatting
|
||||
logger = get_logger("heartflow")
|
||||
|
||||
|
||||
class Heartflow:
|
||||
"""主心流协调器,负责初始化并协调聊天"""
|
||||
class HeartflowManager:
|
||||
"""主心流协调器,负责初始化并协调聊天,控制聊天属性"""
|
||||
|
||||
def __init__(self):
|
||||
self.heartflow_chat_list: Dict[str, HeartFChatting | BrainChatting] = {}
|
||||
@@ -35,5 +35,14 @@ class Heartflow:
|
||||
traceback.print_exc()
|
||||
return None
|
||||
|
||||
def adjust_talk_frequency(self, session_id: str, frequency: float):
|
||||
"""调整指定聊天流的说话频率"""
|
||||
chat = self.heartflow_chat_list.get(session_id)
|
||||
if chat and isinstance(chat, HeartFChatting):
|
||||
chat.adjust_talk_frequency(frequency)
|
||||
logger.info(f"已调整聊天 {session_id} 的说话频率为 {frequency}")
|
||||
else:
|
||||
logger.warning(f"无法调整频率,未找到 session_id={session_id} 的聊天流")
|
||||
|
||||
heartflow = Heartflow()
|
||||
|
||||
heartflow_manager = HeartflowManager()
|
||||
@@ -2,7 +2,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
import traceback
|
||||
|
||||
from src.chat.heart_flow.heartflow import heartflow
|
||||
from src.chat.heart_flow.heartflow_manager import heartflow_manager
|
||||
|
||||
# from src.chat.utils.chat_message_builder import replace_user_references
|
||||
from src.common.utils.utils_message import MessageUtils
|
||||
@@ -58,7 +58,7 @@ class HeartFCMessageReceiver:
|
||||
|
||||
MessageUtils.store_message_to_db(message) # 存储消息到数据库
|
||||
|
||||
await heartflow.get_or_create_heartflow_chat(message.session_id)
|
||||
await heartflow_manager.get_or_create_heartflow_chat(message.session_id)
|
||||
|
||||
# 3. 日志记录
|
||||
mes_name = group_info.group_name if group_info else "私聊"
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, Dict, Any
|
||||
|
||||
import time
|
||||
|
||||
from src.config.config import global_config
|
||||
from src.common.logger import get_logger
|
||||
from src.chat.message_receive.chat_manager import chat_manager as _chat_manager
|
||||
from src.services import send_service as send_api
|
||||
|
||||
from src.common.message_repository import count_messages
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class CyclePlanInfo: ...
|
||||
|
||||
|
||||
@dataclass
|
||||
class CycleActionInfo: ...
|
||||
|
||||
|
||||
class CycleDetail:
|
||||
"""循环信息记录类"""
|
||||
|
||||
def __init__(self, cycle_id: int):
|
||||
self.cycle_id = cycle_id
|
||||
self.thinking_id = ""
|
||||
self.start_time = time.time()
|
||||
self.end_time: Optional[float] = None
|
||||
self.timers: Dict[str, float] = {}
|
||||
|
||||
|
||||
def set_loop_info(self, loop_info: Dict[str, Any]):
|
||||
"""设置循环信息"""
|
||||
self.loop_plan_info = loop_info["loop_plan_info"]
|
||||
self.loop_action_info = loop_info["loop_action_info"]
|
||||
|
||||
|
||||
def get_recent_message_stats(minutes: float = 30, chat_id: Optional[str] = None) -> dict:
|
||||
"""
|
||||
Args:
|
||||
minutes (float): 检索的分钟数,默认30分钟
|
||||
chat_id (str, optional): 指定的chat_id,仅统计该chat下的消息。为None时统计全部。
|
||||
Returns:
|
||||
dict: {"bot_reply_count": int, "total_message_count": int}
|
||||
"""
|
||||
|
||||
now = time.time()
|
||||
start_time = now - minutes * 60
|
||||
bot_id = global_config.bot.qq_account
|
||||
|
||||
filter_base: Dict[str, Any] = {"time": {"$gte": start_time}}
|
||||
if chat_id is not None:
|
||||
filter_base["chat_id"] = chat_id
|
||||
|
||||
# 总消息数
|
||||
total_message_count = count_messages(filter_base)
|
||||
# bot自身回复数
|
||||
bot_filter = filter_base.copy()
|
||||
bot_filter["user_id"] = bot_id
|
||||
bot_reply_count = count_messages(bot_filter)
|
||||
|
||||
return {"bot_reply_count": bot_reply_count, "total_message_count": total_message_count}
|
||||
|
||||
|
||||
async def send_typing():
|
||||
chat = await _chat_manager.get_or_create_session(
|
||||
platform="amaidesu_default",
|
||||
user_id="114514",
|
||||
group_id="114514",
|
||||
)
|
||||
|
||||
await send_api.custom_to_stream(
|
||||
message_type="state", content="typing", stream_id=chat.session_id, storage_message=False
|
||||
)
|
||||
|
||||
|
||||
async def stop_typing():
|
||||
chat = await _chat_manager.get_or_create_session(
|
||||
platform="amaidesu_default",
|
||||
user_id="114514",
|
||||
group_id="114514",
|
||||
)
|
||||
|
||||
await send_api.custom_to_stream(
|
||||
message_type="state", content="stop_typing", stream_id=chat.session_id, storage_message=False
|
||||
)
|
||||
Reference in New Issue
Block a user