This commit is contained in:
SengokuCola
2025-07-16 11:38:54 +08:00
4 changed files with 29 additions and 23 deletions

View File

@@ -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": {
@@ -168,7 +167,6 @@ class HeartFChatting:
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}")
@@ -222,7 +220,7 @@ 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:
@@ -332,21 +330,14 @@ class HeartFChatting:
return True
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):
success, reply_text, command = await self._handle_action(
action_type, reasoning, action_data, cycle_timers, thinking_id, action_message
)
loop_info = {
"loop_plan_info": {
"action_result": plan_result.get("action_result", {}),
@@ -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:
"""
处理接收到的消息。
"兴趣"模式下,判断是否回复并生成内容。
@@ -507,10 +498,10 @@ class HeartFChatting:
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

View File

@@ -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数据

View File

@@ -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
...
"""

View File

@@ -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()