Merge branch 'dev' of https://github.com/MaiM-with-u/MaiBot into dev
This commit is contained in:
@@ -22,7 +22,6 @@ from src.plugin_system.apis import generator_api, send_api, message_api
|
|||||||
from src.chat.willing.willing_manager import get_willing_manager
|
from src.chat.willing.willing_manager import get_willing_manager
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ERROR_LOOP_INFO = {
|
ERROR_LOOP_INFO = {
|
||||||
"loop_plan_info": {
|
"loop_plan_info": {
|
||||||
"action_result": {
|
"action_result": {
|
||||||
@@ -167,15 +166,14 @@ class HeartFChatting:
|
|||||||
self.history_loop.append(self._current_cycle_detail)
|
self.history_loop.append(self._current_cycle_detail)
|
||||||
self._current_cycle_detail.timers = cycle_timers
|
self._current_cycle_detail.timers = cycle_timers
|
||||||
self._current_cycle_detail.end_time = time.time()
|
self._current_cycle_detail.end_time = time.time()
|
||||||
|
|
||||||
|
|
||||||
def _handle_energy_completion(self, task: asyncio.Task):
|
def _handle_energy_completion(self, task: asyncio.Task):
|
||||||
if exception := task.exception():
|
if exception := task.exception():
|
||||||
logger.error(f"{self.log_prefix} HeartFChatting: 能量循环异常: {exception}")
|
logger.error(f"{self.log_prefix} HeartFChatting: 能量循环异常: {exception}")
|
||||||
logger.error(traceback.format_exc())
|
logger.error(traceback.format_exc())
|
||||||
else:
|
else:
|
||||||
logger.info(f"{self.log_prefix} HeartFChatting: 能量循环完成")
|
logger.info(f"{self.log_prefix} HeartFChatting: 能量循环完成")
|
||||||
|
|
||||||
async def _energy_loop(self):
|
async def _energy_loop(self):
|
||||||
while self.running:
|
while self.running:
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
@@ -222,9 +220,9 @@ class HeartFChatting:
|
|||||||
|
|
||||||
if len(new_messages_data) > 3 * global_config.chat.focus_value:
|
if len(new_messages_data) > 3 * global_config.chat.focus_value:
|
||||||
self.loop_mode = ChatMode.FOCUS
|
self.loop_mode = ChatMode.FOCUS
|
||||||
self.energy_value = 10 + (new_messages_data / (3 * global_config.chat.focus_value)) * 10
|
self.energy_value = 10 + (len(new_messages_data) / (3 * global_config.chat.focus_value)) * 10
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if self.energy_value >= 30 * global_config.chat.focus_value:
|
if self.energy_value >= 30 * global_config.chat.focus_value:
|
||||||
self.loop_mode = ChatMode.FOCUS
|
self.loop_mode = ChatMode.FOCUS
|
||||||
return True
|
return True
|
||||||
@@ -332,20 +330,13 @@ class HeartFChatting:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
action_message: Dict[str, Any] = message_data or target_message # type: ignore
|
||||||
if message_data:
|
|
||||||
action_message = message_data
|
|
||||||
else:
|
|
||||||
action_message = target_message
|
|
||||||
# 动作执行计时
|
# 动作执行计时
|
||||||
|
|
||||||
|
|
||||||
with Timer("动作执行", cycle_timers):
|
with Timer("动作执行", cycle_timers):
|
||||||
success, reply_text, command = await self._handle_action(
|
success, reply_text, command = await self._handle_action(
|
||||||
action_type, reasoning, action_data, cycle_timers, thinking_id, action_message
|
action_type, reasoning, action_data, cycle_timers, thinking_id, action_message
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
loop_info = {
|
loop_info = {
|
||||||
"loop_plan_info": {
|
"loop_plan_info": {
|
||||||
@@ -372,7 +363,7 @@ class HeartFChatting:
|
|||||||
|
|
||||||
if action_type != "no_reply" and action_type != "no_action":
|
if action_type != "no_reply" and action_type != "no_action":
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def _main_chat_loop(self):
|
async def _main_chat_loop(self):
|
||||||
@@ -459,7 +450,7 @@ class HeartFChatting:
|
|||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return False, "", ""
|
return False, "", ""
|
||||||
|
|
||||||
async def normal_response(self, message_data: dict) -> None:
|
async def normal_response(self, message_data: dict) -> bool:
|
||||||
"""
|
"""
|
||||||
处理接收到的消息。
|
处理接收到的消息。
|
||||||
在"兴趣"模式下,判断是否回复并生成内容。
|
在"兴趣"模式下,判断是否回复并生成内容。
|
||||||
@@ -498,7 +489,7 @@ class HeartFChatting:
|
|||||||
f"{message_data.get('user_nickname')}:"
|
f"{message_data.get('user_nickname')}:"
|
||||||
f"{message_data.get('processed_plain_text')}[兴趣:{interested_rate:.2f}][回复概率:{reply_probability * 100:.1f}%]"
|
f"{message_data.get('processed_plain_text')}[兴趣:{interested_rate:.2f}][回复概率:{reply_probability * 100:.1f}%]"
|
||||||
)
|
)
|
||||||
|
|
||||||
talk_frequency = global_config.chat.get_current_talk_frequency(self.stream_id)
|
talk_frequency = global_config.chat.get_current_talk_frequency(self.stream_id)
|
||||||
reply_probability = talk_frequency * reply_probability
|
reply_probability = talk_frequency * reply_probability
|
||||||
|
|
||||||
@@ -506,11 +497,11 @@ class HeartFChatting:
|
|||||||
await self.willing_manager.before_generate_reply_handle(message_data.get("message_id", ""))
|
await self.willing_manager.before_generate_reply_handle(message_data.get("message_id", ""))
|
||||||
await self._observe(message_data=message_data)
|
await self._observe(message_data=message_data)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
# 意愿管理器:注销当前message信息 (无论是否回复,只要处理过就删除)
|
# 意愿管理器:注销当前message信息 (无论是否回复,只要处理过就删除)
|
||||||
return False
|
|
||||||
self.willing_manager.delete(message_data.get("message_id", ""))
|
self.willing_manager.delete(message_data.get("message_id", ""))
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def _generate_response(
|
async def _generate_response(
|
||||||
self, message_data: dict, available_actions: Optional[Dict[str, ActionInfo]], reply_to: str
|
self, message_data: dict, available_actions: Optional[Dict[str, ActionInfo]], reply_to: str
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ def _extract_json_from_text(text: str) -> dict:
|
|||||||
def _entity_extract(llm_req: LLMRequest, paragraph: str) -> List[str]:
|
def _entity_extract(llm_req: LLMRequest, paragraph: str) -> List[str]:
|
||||||
"""对段落进行实体提取,返回提取出的实体列表(JSON格式)"""
|
"""对段落进行实体提取,返回提取出的实体列表(JSON格式)"""
|
||||||
entity_extract_context = prompt_template.build_entity_extract_context(paragraph)
|
entity_extract_context = prompt_template.build_entity_extract_context(paragraph)
|
||||||
response, (reasoning_content, model_name) = llm_req.generate_response_sync(entity_extract_context)
|
response, (reasoning_content, model_name) = llm_req.generate_response_async(entity_extract_context)
|
||||||
|
|
||||||
entity_extract_result = _extract_json_from_text(response)
|
entity_extract_result = _extract_json_from_text(response)
|
||||||
# 尝试load JSON数据
|
# 尝试load JSON数据
|
||||||
@@ -50,7 +50,7 @@ def _rdf_triple_extract(llm_req: LLMRequest, paragraph: str, entities: list) ->
|
|||||||
rdf_extract_context = prompt_template.build_rdf_triple_extract_context(
|
rdf_extract_context = prompt_template.build_rdf_triple_extract_context(
|
||||||
paragraph, entities=json.dumps(entities, ensure_ascii=False)
|
paragraph, entities=json.dumps(entities, ensure_ascii=False)
|
||||||
)
|
)
|
||||||
response, (reasoning_content, model_name) = llm_req.generate_response_sync(rdf_extract_context)
|
response, (reasoning_content, model_name) = llm_req.generate_response_async(rdf_extract_context)
|
||||||
|
|
||||||
entity_extract_result = _extract_json_from_text(response)
|
entity_extract_result = _extract_json_from_text(response)
|
||||||
# 尝试load JSON数据
|
# 尝试load JSON数据
|
||||||
|
|||||||
@@ -23,7 +23,20 @@ def register_plugin(cls):
|
|||||||
# 只是注册插件类,不立即实例化
|
# 只是注册插件类,不立即实例化
|
||||||
# 插件管理器会负责实例化和注册
|
# 插件管理器会负责实例化和注册
|
||||||
plugin_name = cls.plugin_name or cls.__name__
|
plugin_name = cls.plugin_name or cls.__name__
|
||||||
plugin_manager.plugin_classes[plugin_name] = cls
|
plugin_manager.plugin_classes[plugin_name] = cls # type: ignore
|
||||||
logger.debug(f"插件类已注册: {plugin_name}")
|
logger.debug(f"插件类已注册: {plugin_name}")
|
||||||
|
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
|
def register_event_plugin(cls, *args, **kwargs):
|
||||||
|
from src.plugin_system.core.events_manager import events_manager
|
||||||
|
from src.plugin_system.base.component_types import EventType
|
||||||
|
|
||||||
|
"""事件插件注册装饰器
|
||||||
|
|
||||||
|
用法:
|
||||||
|
@register_event_plugin
|
||||||
|
class MyEventPlugin:
|
||||||
|
event_type = EventType.MESSAGE_RECEIVED
|
||||||
|
...
|
||||||
|
"""
|
||||||
@@ -7,3 +7,5 @@ class EventsManager:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
# 有权重的 events 订阅者注册表
|
# 有权重的 events 订阅者注册表
|
||||||
self.events_subscribers: Dict[EventType, List[Dict[int, Type]]] = {event: [] for event in EventType}
|
self.events_subscribers: Dict[EventType, List[Dict[int, Type]]] = {event: [] for event in EventType}
|
||||||
|
|
||||||
|
events_manager = EventsManager()
|
||||||
Reference in New Issue
Block a user