From 221ed0e5a54c9b1c4e94be0ff2459faec306ee25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E6=A2=93=E6=9F=92?= <1787882683@qq.com> Date: Wed, 16 Jul 2025 11:35:08 +0800 Subject: [PATCH 1/2] fix function name error --- src/chat/knowledge/ie_process.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/chat/knowledge/ie_process.py b/src/chat/knowledge/ie_process.py index a6f72eb52..bd0e17684 100644 --- a/src/chat/knowledge/ie_process.py +++ b/src/chat/knowledge/ie_process.py @@ -28,7 +28,7 @@ def _extract_json_from_text(text: str) -> dict: def _entity_extract(llm_req: LLMRequest, paragraph: str) -> List[str]: """对段落进行实体提取,返回提取出的实体列表(JSON格式)""" 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) # 尝试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( 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) # 尝试load JSON数据 From 1a17fa20f7a62191da95f84fe5b313e6d95ecc95 Mon Sep 17 00:00:00 2001 From: UnCLAS-Prommer Date: Wed, 16 Jul 2025 11:38:01 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=BD=9C=E5=9C=A8=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D,=20events=20sys?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/focus_chat/heartFC_chat.py | 31 +++++++------------ src/plugin_system/apis/plugin_register_api.py | 15 ++++++++- src/plugin_system/core/events_manager.py | 2 ++ 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/chat/focus_chat/heartFC_chat.py b/src/chat/focus_chat/heartFC_chat.py index 180b29690..879428fd9 100644 --- a/src/chat/focus_chat/heartFC_chat.py +++ b/src/chat/focus_chat/heartFC_chat.py @@ -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 - ERROR_LOOP_INFO = { "loop_plan_info": { "action_result": { @@ -167,15 +166,14 @@ class HeartFChatting: self.history_loop.append(self._current_cycle_detail) self._current_cycle_detail.timers = cycle_timers self._current_cycle_detail.end_time = time.time() - - + def _handle_energy_completion(self, task: asyncio.Task): if exception := task.exception(): logger.error(f"{self.log_prefix} HeartFChatting: 能量循环异常: {exception}") logger.error(traceback.format_exc()) else: logger.info(f"{self.log_prefix} HeartFChatting: 能量循环完成") - + async def _energy_loop(self): while self.running: await asyncio.sleep(1) @@ -222,9 +220,9 @@ class HeartFChatting: if len(new_messages_data) > 3 * global_config.chat.focus_value: 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 - + if self.energy_value >= 30 * global_config.chat.focus_value: self.loop_mode = ChatMode.FOCUS return True @@ -332,20 +330,13 @@ class HeartFChatting: return True else: - - if message_data: - action_message = message_data - else: - action_message = target_message + action_message: Dict[str, Any] = message_data or target_message # type: ignore + # 动作执行计时 - - with Timer("动作执行", cycle_timers): success, reply_text, command = await self._handle_action( action_type, reasoning, action_data, cycle_timers, thinking_id, action_message ) - - loop_info = { "loop_plan_info": { @@ -372,7 +363,7 @@ class HeartFChatting: if action_type != "no_reply" and action_type != "no_action": return True - + return True async def _main_chat_loop(self): @@ -459,7 +450,7 @@ class HeartFChatting: traceback.print_exc() 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('processed_plain_text')}[兴趣:{interested_rate:.2f}][回复概率:{reply_probability * 100:.1f}%]" ) - + talk_frequency = global_config.chat.get_current_talk_frequency(self.stream_id) 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._observe(message_data=message_data) return True - # 意愿管理器:注销当前message信息 (无论是否回复,只要处理过就删除) - return False self.willing_manager.delete(message_data.get("message_id", "")) + return False + async def _generate_response( self, message_data: dict, available_actions: Optional[Dict[str, ActionInfo]], reply_to: str diff --git a/src/plugin_system/apis/plugin_register_api.py b/src/plugin_system/apis/plugin_register_api.py index d6e7f1f53..8291a314f 100644 --- a/src/plugin_system/apis/plugin_register_api.py +++ b/src/plugin_system/apis/plugin_register_api.py @@ -23,7 +23,20 @@ def register_plugin(cls): # 只是注册插件类,不立即实例化 # 插件管理器会负责实例化和注册 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}") 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 + ... + """ \ No newline at end of file diff --git a/src/plugin_system/core/events_manager.py b/src/plugin_system/core/events_manager.py index 92ef350e2..1b96da44c 100644 --- a/src/plugin_system/core/events_manager.py +++ b/src/plugin_system/core/events_manager.py @@ -7,3 +7,5 @@ class EventsManager: def __init__(self): # 有权重的 events 订阅者注册表 self.events_subscribers: Dict[EventType, List[Dict[int, Type]]] = {event: [] for event in EventType} + +events_manager = EventsManager() \ No newline at end of file