fix: 修复merge错误
This commit is contained in:
70
src/main.py
70
src/main.py
@@ -8,10 +8,13 @@ from .plugins.chat.emoji_manager import emoji_manager
|
|||||||
from .plugins.chat.relationship_manager import relationship_manager
|
from .plugins.chat.relationship_manager import relationship_manager
|
||||||
from .plugins.willing.willing_manager import willing_manager
|
from .plugins.willing.willing_manager import willing_manager
|
||||||
from .plugins.chat.chat_stream import chat_manager
|
from .plugins.chat.chat_stream import chat_manager
|
||||||
from .plugins.memory_system.memory import hippocampus
|
from .plugins.memory_system.Hippocampus import HippocampusManager
|
||||||
|
from .plugins.chat import auto_speak_manager
|
||||||
|
from .think_flow_demo.heartflow import subheartflow_manager
|
||||||
|
from .think_flow_demo.outer_world import outer_world
|
||||||
from .plugins.chat.message_sender import message_manager
|
from .plugins.chat.message_sender import message_manager
|
||||||
from .plugins.chat.storage import MessageStorage
|
from .plugins.chat.storage import MessageStorage
|
||||||
from .plugins.chat.config import global_config
|
from .plugins.config.config import global_config
|
||||||
from .plugins.chat.bot import chat_bot
|
from .plugins.chat.bot import chat_bot
|
||||||
from .common.logger import get_module_logger
|
from .common.logger import get_module_logger
|
||||||
|
|
||||||
@@ -22,6 +25,7 @@ class MainSystem:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.llm_stats = LLMStatistics("llm_statistics.txt")
|
self.llm_stats = LLMStatistics("llm_statistics.txt")
|
||||||
self.mood_manager = MoodManager.get_instance()
|
self.mood_manager = MoodManager.get_instance()
|
||||||
|
self.hippocampus_manager = HippocampusManager.get_instance()
|
||||||
self._message_manager_started = False
|
self._message_manager_started = False
|
||||||
|
|
||||||
# 使用消息API替代直接的FastAPI实例
|
# 使用消息API替代直接的FastAPI实例
|
||||||
@@ -34,10 +38,7 @@ class MainSystem:
|
|||||||
logger.debug(f"正在唤醒{global_config.BOT_NICKNAME}......")
|
logger.debug(f"正在唤醒{global_config.BOT_NICKNAME}......")
|
||||||
|
|
||||||
# 其他初始化任务
|
# 其他初始化任务
|
||||||
await asyncio.gather(
|
await asyncio.gather(self._init_components())
|
||||||
self._init_components(), # 将原有的初始化代码移到这个新方法中
|
|
||||||
# api_task,
|
|
||||||
)
|
|
||||||
|
|
||||||
logger.success("系统初始化完成")
|
logger.success("系统初始化完成")
|
||||||
|
|
||||||
@@ -70,24 +71,43 @@ class MainSystem:
|
|||||||
await chat_manager._initialize()
|
await chat_manager._initialize()
|
||||||
asyncio.create_task(chat_manager._auto_save_task())
|
asyncio.create_task(chat_manager._auto_save_task())
|
||||||
|
|
||||||
|
# 使用HippocampusManager初始化海马体
|
||||||
|
|
||||||
|
self.hippocampus_manager.initialize(global_config=global_config)
|
||||||
|
|
||||||
# 初始化日程
|
# 初始化日程
|
||||||
await bot_schedule.initialize()
|
bot_schedule.initialize(
|
||||||
bot_schedule.print_schedule()
|
name=global_config.BOT_NICKNAME,
|
||||||
|
personality=global_config.PROMPT_PERSONALITY,
|
||||||
|
behavior=global_config.PROMPT_SCHEDULE_GEN,
|
||||||
|
interval=global_config.SCHEDULE_DOING_UPDATE_INTERVAL,
|
||||||
|
)
|
||||||
|
asyncio.create_task(bot_schedule.mai_schedule_start())
|
||||||
|
|
||||||
# 启动FastAPI服务器
|
# 启动FastAPI服务器
|
||||||
self.app.register_message_handler(chat_bot.message_process)
|
self.app.register_message_handler(chat_bot.message_process)
|
||||||
|
|
||||||
|
try:
|
||||||
|
asyncio.create_task(outer_world.open_eyes())
|
||||||
|
logger.success("大脑和外部世界启动成功")
|
||||||
|
# 启动心流系统
|
||||||
|
asyncio.create_task(subheartflow_manager.heartflow_start_working())
|
||||||
|
logger.success("心流系统启动成功")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"启动大脑和外部世界失败: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
async def schedule_tasks(self):
|
async def schedule_tasks(self):
|
||||||
"""调度定时任务"""
|
"""调度定时任务"""
|
||||||
while True:
|
while True:
|
||||||
tasks = [
|
tasks = [
|
||||||
self.build_memory_task(),
|
self.build_memory_task(),
|
||||||
self.forget_memory_task(),
|
self.forget_memory_task(),
|
||||||
self.merge_memory_task(),
|
# self.merge_memory_task(),
|
||||||
self.print_mood_task(),
|
self.print_mood_task(),
|
||||||
self.generate_schedule_task(),
|
# self.generate_schedule_task(),
|
||||||
self.remove_recalled_message_task(),
|
self.remove_recalled_message_task(),
|
||||||
emoji_manager.start_periodic_check(interval_MINS=global_config.EMOJI_CHECK_INTERVAL),
|
emoji_manager.start_periodic_check(),
|
||||||
self.app.run(),
|
self.app.run(),
|
||||||
]
|
]
|
||||||
await asyncio.gather(*tasks)
|
await asyncio.gather(*tasks)
|
||||||
@@ -96,22 +116,22 @@ class MainSystem:
|
|||||||
"""记忆构建任务"""
|
"""记忆构建任务"""
|
||||||
while True:
|
while True:
|
||||||
logger.info("正在进行记忆构建")
|
logger.info("正在进行记忆构建")
|
||||||
await hippocampus.operation_build_memory()
|
await HippocampusManager.get_instance().build_memory()
|
||||||
await asyncio.sleep(global_config.build_memory_interval)
|
await asyncio.sleep(global_config.build_memory_interval)
|
||||||
|
|
||||||
async def forget_memory_task(self):
|
async def forget_memory_task(self):
|
||||||
"""记忆遗忘任务"""
|
"""记忆遗忘任务"""
|
||||||
while True:
|
while True:
|
||||||
print("\033[1;32m[记忆遗忘]\033[0m 开始遗忘记忆...")
|
print("\033[1;32m[记忆遗忘]\033[0m 开始遗忘记忆...")
|
||||||
await hippocampus.operation_forget_topic(percentage=global_config.memory_forget_percentage)
|
await HippocampusManager.get_instance().forget_memory(percentage=global_config.memory_forget_percentage)
|
||||||
print("\033[1;32m[记忆遗忘]\033[0m 记忆遗忘完成")
|
print("\033[1;32m[记忆遗忘]\033[0m 记忆遗忘完成")
|
||||||
await asyncio.sleep(global_config.forget_memory_interval)
|
await asyncio.sleep(global_config.forget_memory_interval)
|
||||||
|
|
||||||
async def merge_memory_task(self):
|
# async def merge_memory_task(self):
|
||||||
"""记忆整合任务"""
|
# """记忆整合任务"""
|
||||||
while True:
|
# while True:
|
||||||
logger.info("正在进行记忆整合")
|
# logger.info("正在进行记忆整合")
|
||||||
await asyncio.sleep(global_config.build_memory_interval + 10)
|
# await asyncio.sleep(global_config.build_memory_interval + 10)
|
||||||
|
|
||||||
async def print_mood_task(self):
|
async def print_mood_task(self):
|
||||||
"""打印情绪状态"""
|
"""打印情绪状态"""
|
||||||
@@ -119,13 +139,13 @@ class MainSystem:
|
|||||||
self.mood_manager.print_mood_status()
|
self.mood_manager.print_mood_status()
|
||||||
await asyncio.sleep(30)
|
await asyncio.sleep(30)
|
||||||
|
|
||||||
async def generate_schedule_task(self):
|
# async def generate_schedule_task(self):
|
||||||
"""生成日程任务"""
|
# """生成日程任务"""
|
||||||
while True:
|
# while True:
|
||||||
await bot_schedule.initialize()
|
# await bot_schedule.initialize()
|
||||||
if not bot_schedule.enable_output:
|
# if not bot_schedule.enable_output:
|
||||||
bot_schedule.print_schedule()
|
# bot_schedule.print_schedule()
|
||||||
await asyncio.sleep(7200)
|
# await asyncio.sleep(7200)
|
||||||
|
|
||||||
async def remove_recalled_message_task(self):
|
async def remove_recalled_message_task(self):
|
||||||
"""删除撤回消息任务"""
|
"""删除撤回消息任务"""
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ from .chat.emoji_manager import emoji_manager
|
|||||||
from .chat.relationship_manager import relationship_manager
|
from .chat.relationship_manager import relationship_manager
|
||||||
from .moods.moods import MoodManager
|
from .moods.moods import MoodManager
|
||||||
from .willing.willing_manager import willing_manager
|
from .willing.willing_manager import willing_manager
|
||||||
from .memory_system.Hippocampus import HippocampusManager
|
|
||||||
from .schedule.schedule_generator import bot_schedule
|
from .schedule.schedule_generator import bot_schedule
|
||||||
|
|
||||||
# 导出主要组件供外部使用
|
# 导出主要组件供外部使用
|
||||||
|
|||||||
@@ -5,15 +5,17 @@ from random import random as random_float
|
|||||||
from typing import Dict
|
from typing import Dict
|
||||||
from ..config.config import global_config
|
from ..config.config import global_config
|
||||||
from .message import MessageSending, MessageThinking, MessageSet, MessageRecv
|
from .message import MessageSending, MessageThinking, MessageSet, MessageRecv
|
||||||
from .message_base import UserInfo, Seg
|
from ..message.message_base import UserInfo, Seg
|
||||||
from .message_sender import message_manager
|
from .message_sender import message_manager
|
||||||
from ..moods.moods import MoodManager
|
from ..moods.moods import MoodManager
|
||||||
from .llm_generator import ResponseGenerator
|
from .llm_generator import ResponseGenerator
|
||||||
from src.common.logger import get_module_logger
|
from src.common.logger import get_module_logger
|
||||||
from src.think_flow_demo.heartflow import subheartflow_manager
|
from src.think_flow_demo.heartflow import subheartflow_manager
|
||||||
from ...common.database import db
|
from ...common.database import db
|
||||||
|
|
||||||
logger = get_module_logger("auto_speak")
|
logger = get_module_logger("auto_speak")
|
||||||
|
|
||||||
|
|
||||||
class AutoSpeakManager:
|
class AutoSpeakManager:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._last_auto_speak_time: Dict[str, float] = {} # 记录每个聊天流上次自主发言的时间
|
self._last_auto_speak_time: Dict[str, float] = {} # 记录每个聊天流上次自主发言的时间
|
||||||
@@ -38,13 +40,16 @@ class AutoSpeakManager:
|
|||||||
async def _periodic_check(self):
|
async def _periodic_check(self):
|
||||||
"""定期检查是否需要自主发言"""
|
"""定期检查是否需要自主发言"""
|
||||||
while True and global_config.enable_think_flow:
|
while True and global_config.enable_think_flow:
|
||||||
|
|
||||||
# 获取所有活跃的子心流
|
# 获取所有活跃的子心流
|
||||||
active_subheartflows = []
|
active_subheartflows = []
|
||||||
for chat_id, subheartflow in subheartflow_manager._subheartflows.items():
|
for chat_id, subheartflow in subheartflow_manager._subheartflows.items():
|
||||||
if subheartflow.is_active and subheartflow.current_state.willing > 0: # 只考虑活跃且意愿值大于0.5的子心流
|
if (
|
||||||
|
subheartflow.is_active and subheartflow.current_state.willing > 0
|
||||||
|
): # 只考虑活跃且意愿值大于0.5的子心流
|
||||||
active_subheartflows.append((chat_id, subheartflow))
|
active_subheartflows.append((chat_id, subheartflow))
|
||||||
logger.debug(f"发现活跃子心流 - 聊天ID: {chat_id}, 意愿值: {subheartflow.current_state.willing:.2f}")
|
logger.debug(
|
||||||
|
f"发现活跃子心流 - 聊天ID: {chat_id}, 意愿值: {subheartflow.current_state.willing:.2f}"
|
||||||
|
)
|
||||||
|
|
||||||
if not active_subheartflows:
|
if not active_subheartflows:
|
||||||
logger.debug("当前没有活跃的子心流")
|
logger.debug("当前没有活跃的子心流")
|
||||||
@@ -54,7 +59,7 @@ class AutoSpeakManager:
|
|||||||
# 随机选择一个活跃的子心流
|
# 随机选择一个活跃的子心流
|
||||||
chat_id, subheartflow = random.choice(active_subheartflows)
|
chat_id, subheartflow = random.choice(active_subheartflows)
|
||||||
logger.info(f"随机选择子心流 - 聊天ID: {chat_id}, 意愿值: {subheartflow.current_state.willing:.2f}")
|
logger.info(f"随机选择子心流 - 聊天ID: {chat_id}, 意愿值: {subheartflow.current_state.willing:.2f}")
|
||||||
|
|
||||||
# 检查是否应该自主发言
|
# 检查是否应该自主发言
|
||||||
if await self.check_auto_speak(subheartflow):
|
if await self.check_auto_speak(subheartflow):
|
||||||
logger.info(f"准备自主发言 - 聊天ID: {chat_id}")
|
logger.info(f"准备自主发言 - 聊天ID: {chat_id}")
|
||||||
@@ -64,59 +69,61 @@ class AutoSpeakManager:
|
|||||||
user_nickname=global_config.BOT_NICKNAME,
|
user_nickname=global_config.BOT_NICKNAME,
|
||||||
platform="qq", # 默认使用qq平台
|
platform="qq", # 默认使用qq平台
|
||||||
)
|
)
|
||||||
|
|
||||||
# 创建一个空的MessageRecv对象作为上下文
|
# 创建一个空的MessageRecv对象作为上下文
|
||||||
message = MessageRecv({
|
message = MessageRecv(
|
||||||
"message_info": {
|
{
|
||||||
"user_info": {
|
"message_info": {
|
||||||
"user_id": chat_id,
|
"user_info": {"user_id": chat_id, "user_nickname": "", "platform": "qq"},
|
||||||
"user_nickname": "",
|
"group_info": None,
|
||||||
"platform": "qq"
|
"platform": "qq",
|
||||||
|
"time": time.time(),
|
||||||
},
|
},
|
||||||
"group_info": None,
|
"processed_plain_text": "",
|
||||||
"platform": "qq",
|
"raw_message": "",
|
||||||
"time": time.time()
|
"is_emoji": False,
|
||||||
},
|
}
|
||||||
"processed_plain_text": "",
|
)
|
||||||
"raw_message": "",
|
|
||||||
"is_emoji": False
|
await self.generate_auto_speak(
|
||||||
})
|
subheartflow, message, bot_user_info, message.message_info["user_info"], message.message_info
|
||||||
|
)
|
||||||
await self.generate_auto_speak(subheartflow, message, bot_user_info, message.message_info["user_info"], message.message_info)
|
|
||||||
else:
|
else:
|
||||||
logger.debug(f"不满足自主发言条件 - 聊天ID: {chat_id}")
|
logger.debug(f"不满足自主发言条件 - 聊天ID: {chat_id}")
|
||||||
|
|
||||||
# 每分钟检查一次
|
# 每分钟检查一次
|
||||||
await asyncio.sleep(20)
|
await asyncio.sleep(20)
|
||||||
|
|
||||||
# await asyncio.sleep(5) # 发生错误时等待5秒再继续
|
# await asyncio.sleep(5) # 发生错误时等待5秒再继续
|
||||||
|
|
||||||
async def check_auto_speak(self, subheartflow) -> bool:
|
async def check_auto_speak(self, subheartflow) -> bool:
|
||||||
"""检查是否应该自主发言"""
|
"""检查是否应该自主发言"""
|
||||||
if not subheartflow:
|
if not subheartflow:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
current_time = time.time()
|
current_time = time.time()
|
||||||
chat_id = subheartflow.observe_chat_id
|
chat_id = subheartflow.observe_chat_id
|
||||||
|
|
||||||
# 获取上次自主发言时间
|
# 获取上次自主发言时间
|
||||||
if chat_id not in self._last_auto_speak_time:
|
if chat_id not in self._last_auto_speak_time:
|
||||||
self._last_auto_speak_time[chat_id] = 0
|
self._last_auto_speak_time[chat_id] = 0
|
||||||
last_speak_time = self._last_auto_speak_time.get(chat_id, 0)
|
last_speak_time = self._last_auto_speak_time.get(chat_id, 0)
|
||||||
|
|
||||||
# 如果距离上次自主发言不到5分钟,不发言
|
# 如果距离上次自主发言不到5分钟,不发言
|
||||||
if current_time - last_speak_time < 30:
|
if current_time - last_speak_time < 30:
|
||||||
logger.debug(f"距离上次发言时间太短 - 聊天ID: {chat_id}, 剩余时间: {30 - (current_time - last_speak_time):.1f}秒")
|
logger.debug(
|
||||||
|
f"距离上次发言时间太短 - 聊天ID: {chat_id}, 剩余时间: {30 - (current_time - last_speak_time):.1f}秒"
|
||||||
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# 获取当前意愿值
|
# 获取当前意愿值
|
||||||
current_willing = subheartflow.current_state.willing
|
current_willing = subheartflow.current_state.willing
|
||||||
|
|
||||||
if current_willing > 0.1 and random_float() < 0.5:
|
if current_willing > 0.1 and random_float() < 0.5:
|
||||||
self._last_auto_speak_time[chat_id] = current_time
|
self._last_auto_speak_time[chat_id] = current_time
|
||||||
logger.info(f"满足自主发言条件 - 聊天ID: {chat_id}, 意愿值: {current_willing:.2f}")
|
logger.info(f"满足自主发言条件 - 聊天ID: {chat_id}, 意愿值: {current_willing:.2f}")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
logger.debug(f"不满足自主发言条件 - 聊天ID: {chat_id}, 意愿值: {current_willing:.2f}")
|
logger.debug(f"不满足自主发言条件 - 聊天ID: {chat_id}, 意愿值: {current_willing:.2f}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -131,16 +138,16 @@ class AutoSpeakManager:
|
|||||||
reply=message,
|
reply=message,
|
||||||
thinking_start_time=thinking_time_point,
|
thinking_start_time=thinking_time_point,
|
||||||
)
|
)
|
||||||
|
|
||||||
message_manager.add_message(thinking_message)
|
message_manager.add_message(thinking_message)
|
||||||
|
|
||||||
# 生成自主发言内容
|
# 生成自主发言内容
|
||||||
response, raw_content = await self.gpt.generate_response(message)
|
response, raw_content = await self.gpt.generate_response(message)
|
||||||
|
|
||||||
if response:
|
if response:
|
||||||
message_set = MessageSet(None, think_id) # 不需要chat_stream
|
message_set = MessageSet(None, think_id) # 不需要chat_stream
|
||||||
mark_head = False
|
mark_head = False
|
||||||
|
|
||||||
for msg in response:
|
for msg in response:
|
||||||
message_segment = Seg(type="text", data=msg)
|
message_segment = Seg(type="text", data=msg)
|
||||||
bot_message = MessageSending(
|
bot_message = MessageSending(
|
||||||
@@ -157,16 +164,17 @@ class AutoSpeakManager:
|
|||||||
if not mark_head:
|
if not mark_head:
|
||||||
mark_head = True
|
mark_head = True
|
||||||
message_set.add_message(bot_message)
|
message_set.add_message(bot_message)
|
||||||
|
|
||||||
message_manager.add_message(message_set)
|
message_manager.add_message(message_set)
|
||||||
|
|
||||||
# 更新情绪和关系
|
# 更新情绪和关系
|
||||||
stance, emotion = await self.gpt._get_emotion_tags(raw_content, message.processed_plain_text)
|
stance, emotion = await self.gpt._get_emotion_tags(raw_content, message.processed_plain_text)
|
||||||
self.mood_manager.update_mood_from_emotion(emotion, global_config.mood_intensity_factor)
|
self.mood_manager.update_mood_from_emotion(emotion, global_config.mood_intensity_factor)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
# 创建全局AutoSpeakManager实例
|
# 创建全局AutoSpeakManager实例
|
||||||
auto_speak_manager = AutoSpeakManager()
|
auto_speak_manager = AutoSpeakManager()
|
||||||
|
|||||||
Reference in New Issue
Block a user