Merge branch 'dev' of https://github.com/MoFox-Studio/MoFox_Bot into dev
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import asyncio
|
||||||
import time
|
import time
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@@ -15,6 +16,7 @@ class ChatterManager:
|
|||||||
self.action_manager = action_manager
|
self.action_manager = action_manager
|
||||||
self.chatter_classes: dict[ChatType, list[type]] = {}
|
self.chatter_classes: dict[ChatType, list[type]] = {}
|
||||||
self.instances: dict[str, BaseChatter] = {}
|
self.instances: dict[str, BaseChatter] = {}
|
||||||
|
self._processing_tasks: dict[str, asyncio.Task] = {}
|
||||||
|
|
||||||
# 管理器统计
|
# 管理器统计
|
||||||
self.stats = {
|
self.stats = {
|
||||||
@@ -155,3 +157,11 @@ class ChatterManager:
|
|||||||
"successful_executions": 0,
|
"successful_executions": 0,
|
||||||
"failed_executions": 0,
|
"failed_executions": 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def set_processing_task(self, stream_id: str, task: asyncio.Task):
|
||||||
|
"""设置流的处理任务"""
|
||||||
|
self._processing_tasks[stream_id] = task
|
||||||
|
|
||||||
|
def get_processing_task(self, stream_id: str) -> asyncio.Task | None:
|
||||||
|
"""获取流的处理任务"""
|
||||||
|
return self._processing_tasks.get(stream_id)
|
||||||
|
|||||||
@@ -365,7 +365,7 @@ class EnergyManager:
|
|||||||
# 计算与阈值的相对位置
|
# 计算与阈值的相对位置
|
||||||
if energy >= high_threshold:
|
if energy >= high_threshold:
|
||||||
# 高能量区域:指数增强
|
# 高能量区域:指数增强
|
||||||
adjusted = 0.7 + (energy - 0.7) ** 0.8
|
adjusted = 0.7 + max(0, energy - 0.7) ** 0.8
|
||||||
elif energy >= reply_threshold:
|
elif energy >= reply_threshold:
|
||||||
# 中等能量区域:线性保持
|
# 中等能量区域:线性保持
|
||||||
adjusted = energy
|
adjusted = energy
|
||||||
|
|||||||
@@ -8,9 +8,8 @@ from dataclasses import dataclass
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from src.chat.memory_system.integration_layer import IntegrationConfig, IntegrationMode, MemoryIntegrationLayer
|
from src.chat.memory_system.integration_layer import IntegrationConfig, IntegrationMode, MemoryIntegrationLayer
|
||||||
from src.chat.memory_system.memory_formatter import FormatterConfig, format_memories_for_llm
|
|
||||||
|
|
||||||
from src.chat.memory_system.memory_chunk import MemoryChunk, MemoryType
|
from src.chat.memory_system.memory_chunk import MemoryChunk, MemoryType
|
||||||
|
from src.chat.memory_system.memory_formatter import FormatterConfig, format_memories_for_llm
|
||||||
from src.common.logger import get_logger
|
from src.common.logger import get_logger
|
||||||
from src.llm_models.utils_model import LLMRequest
|
from src.llm_models.utils_model import LLMRequest
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ from datetime import datetime
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from src.chat.memory_system.enhanced_memory_manager import enhanced_memory_manager
|
from src.chat.memory_system.enhanced_memory_manager import enhanced_memory_manager
|
||||||
|
|
||||||
from src.common.logger import get_logger
|
from src.common.logger import get_logger
|
||||||
from src.config.config import global_config
|
from src.config.config import global_config
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from src.chat.memory_system.enhanced_memory_hooks import enhanced_memory_hooks
|
from src.chat.memory_system.enhanced_memory_hooks import enhanced_memory_hooks
|
||||||
|
|
||||||
from src.common.logger import get_logger
|
from src.common.logger import get_logger
|
||||||
|
|
||||||
logger = get_logger(__name__)
|
logger = get_logger(__name__)
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ from enum import Enum
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from src.chat.memory_system.enhanced_memory_core import EnhancedMemorySystem
|
from src.chat.memory_system.enhanced_memory_core import EnhancedMemorySystem
|
||||||
|
|
||||||
from src.chat.memory_system.memory_chunk import MemoryChunk
|
from src.chat.memory_system.memory_chunk import MemoryChunk
|
||||||
from src.common.logger import get_logger
|
from src.common.logger import get_logger
|
||||||
from src.llm_models.utils_model import LLMRequest
|
from src.llm_models.utils_model import LLMRequest
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ from src.chat.memory_system.enhanced_memory_adapter import (
|
|||||||
process_conversation_with_enhanced_memory,
|
process_conversation_with_enhanced_memory,
|
||||||
retrieve_memories_with_enhanced_system,
|
retrieve_memories_with_enhanced_system,
|
||||||
)
|
)
|
||||||
|
|
||||||
from src.common.logger import get_logger
|
from src.common.logger import get_logger
|
||||||
|
|
||||||
logger = get_logger(__name__)
|
logger = get_logger(__name__)
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ from enum import Enum
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import orjson
|
import orjson
|
||||||
from src.chat.memory_system.enhanced_reranker import EnhancedReRanker, ReRankingConfig
|
|
||||||
|
|
||||||
|
from src.chat.memory_system.enhanced_reranker import EnhancedReRanker, ReRankingConfig
|
||||||
from src.chat.memory_system.memory_chunk import MemoryChunk, MemoryType
|
from src.chat.memory_system.memory_chunk import MemoryChunk, MemoryType
|
||||||
from src.common.logger import get_logger
|
from src.common.logger import get_logger
|
||||||
|
|
||||||
|
|||||||
@@ -245,6 +245,11 @@ class StreamLoopManager:
|
|||||||
|
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
logger.info(f"流循环被取消: {stream_id}")
|
logger.info(f"流循环被取消: {stream_id}")
|
||||||
|
if self.chatter_manager:
|
||||||
|
task = self.chatter_manager.get_processing_task(stream_id)
|
||||||
|
if task and not task.done():
|
||||||
|
task.cancel()
|
||||||
|
logger.debug(f"已取消 chatter 处理任务: {stream_id}")
|
||||||
break
|
break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"流循环出错 {stream_id}: {e}", exc_info=True)
|
logger.error(f"流循环出错 {stream_id}: {e}", exc_info=True)
|
||||||
@@ -319,8 +324,9 @@ class StreamLoopManager:
|
|||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
|
||||||
# 直接调用chatter_manager处理流上下文
|
# 直接调用chatter_manager处理流上下文
|
||||||
context.processing_task = asyncio.create_task(self.chatter_manager.process_stream_context(stream_id, context))
|
task = asyncio.create_task(self.chatter_manager.process_stream_context(stream_id, context))
|
||||||
results = await context.processing_task
|
self.chatter_manager.set_processing_task(stream_id, task)
|
||||||
|
results = await task
|
||||||
success = results.get("success", False)
|
success = results.get("success", False)
|
||||||
|
|
||||||
if success:
|
if success:
|
||||||
@@ -493,7 +499,7 @@ class StreamLoopManager:
|
|||||||
stream_id: 流ID
|
stream_id: 流ID
|
||||||
"""
|
"""
|
||||||
logger.info(f"强制分发流处理: {stream_id}")
|
logger.info(f"强制分发流处理: {stream_id}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 检查是否有现有的分发循环
|
# 检查是否有现有的分发循环
|
||||||
if stream_id in self.stream_loops:
|
if stream_id in self.stream_loops:
|
||||||
@@ -513,24 +519,24 @@ class StreamLoopManager:
|
|||||||
)
|
)
|
||||||
# 从字典中移除
|
# 从字典中移除
|
||||||
del self.stream_loops[stream_id]
|
del self.stream_loops[stream_id]
|
||||||
|
|
||||||
# 获取聊天管理器和流
|
# 获取聊天管理器和流
|
||||||
chat_manager = get_chat_manager()
|
chat_manager = get_chat_manager()
|
||||||
chat_stream = chat_manager.get_stream(stream_id)
|
chat_stream = chat_manager.get_stream(stream_id)
|
||||||
if not chat_stream:
|
if not chat_stream:
|
||||||
logger.warning(f"强制分发时未找到流: {stream_id}")
|
logger.warning(f"强制分发时未找到流: {stream_id}")
|
||||||
return
|
return
|
||||||
|
|
||||||
# 获取流上下文
|
# 获取流上下文
|
||||||
context = chat_stream.context_manager.context
|
context = chat_stream.context_manager.context
|
||||||
if not context:
|
if not context:
|
||||||
logger.warning(f"强制分发时未找到流上下文: {stream_id}")
|
logger.warning(f"强制分发时未找到流上下文: {stream_id}")
|
||||||
return
|
return
|
||||||
|
|
||||||
# 检查未读消息数量
|
# 检查未读消息数量
|
||||||
unread_count = self._get_unread_count(context)
|
unread_count = self._get_unread_count(context)
|
||||||
logger.info(f"流 {stream_id} 当前未读消息数: {unread_count}")
|
logger.info(f"流 {stream_id} 当前未读消息数: {unread_count}")
|
||||||
|
|
||||||
# 创建新的流循环任务
|
# 创建新的流循环任务
|
||||||
new_task = asyncio.create_task(
|
new_task = asyncio.create_task(
|
||||||
self._stream_loop(stream_id),
|
self._stream_loop(stream_id),
|
||||||
@@ -538,9 +544,9 @@ class StreamLoopManager:
|
|||||||
)
|
)
|
||||||
self.stream_loops[stream_id] = new_task
|
self.stream_loops[stream_id] = new_task
|
||||||
self.stats["total_loops"] += 1
|
self.stats["total_loops"] += 1
|
||||||
|
|
||||||
logger.info(f"已创建强制分发流循环: {stream_id} (当前总数: {len(self.stream_loops)})")
|
logger.info(f"已创建强制分发流循环: {stream_id} (当前总数: {len(self.stream_loops)})")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"强制分发流处理失败 {stream_id}: {e}", exc_info=True)
|
logger.error(f"强制分发流处理失败 {stream_id}: {e}", exc_info=True)
|
||||||
|
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ class MessageManager:
|
|||||||
return
|
return
|
||||||
await self._check_and_handle_interruption(chat_stream)
|
await self._check_and_handle_interruption(chat_stream)
|
||||||
await chat_stream.context_manager.add_message(message)
|
await chat_stream.context_manager.add_message(message)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"添加消息到聊天流 {stream_id} 时发生错误: {e}")
|
logger.error(f"添加消息到聊天流 {stream_id} 时发生错误: {e}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user