perf(methods): 通过移除不必要的 self 参数优化方法签名
在包括 chat、plugin_system、schedule 和 mais4u 在内的多个模块中,消除冗余的实例引用。此次改动将无需访问实例状态的实用函数转换为静态方法,从而提升了内存效率,并使方法依赖关系更加清晰。
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
import copy
|
||||
import hashlib
|
||||
import datetime
|
||||
import asyncio
|
||||
import orjson
|
||||
import hashlib
|
||||
import time
|
||||
|
||||
from json_repair import repair_json
|
||||
from typing import Any, Callable, Dict, Union, Optional
|
||||
|
||||
import orjson
|
||||
from json_repair import repair_json
|
||||
from sqlalchemy import select
|
||||
from src.common.logger import get_logger
|
||||
from src.common.database.sqlalchemy_models import PersonInfo
|
||||
|
||||
from src.common.database.sqlalchemy_database_api import get_db_session
|
||||
from src.llm_models.utils_model import LLMRequest
|
||||
from src.common.database.sqlalchemy_models import PersonInfo
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config import global_config, model_config
|
||||
from src.llm_models.utils_model import LLMRequest
|
||||
|
||||
"""
|
||||
PersonInfoManager 类方法功能摘要:
|
||||
@@ -116,7 +116,8 @@ class PersonInfoManager:
|
||||
logger.error(f"检查用户 {person_id} 是否已知时出错 (SQLAlchemy): {e}")
|
||||
return False
|
||||
|
||||
async def get_person_id_by_person_name(self, person_name: str) -> str:
|
||||
@staticmethod
|
||||
async def get_person_id_by_person_name(person_name: str) -> str:
|
||||
"""根据用户名获取用户ID"""
|
||||
try:
|
||||
# 在需要时获取会话
|
||||
@@ -188,7 +189,8 @@ class PersonInfoManager:
|
||||
|
||||
await _db_create_async(final_data)
|
||||
|
||||
async def _safe_create_person_info(self, person_id: str, data: Optional[dict] = None):
|
||||
@staticmethod
|
||||
async def _safe_create_person_info(person_id: str, data: Optional[dict] = None):
|
||||
"""安全地创建用户信息,处理竞态条件"""
|
||||
if not person_id:
|
||||
logger.debug("创建失败,person_id不存在")
|
||||
|
||||
@@ -3,7 +3,7 @@ import traceback
|
||||
import os
|
||||
import pickle
|
||||
import random
|
||||
from typing import List, Dict, Any
|
||||
from typing import List, Dict, Any, Coroutine
|
||||
from src.config.config import global_config
|
||||
from src.common.logger import get_logger
|
||||
from src.person_info.relationship_manager import get_relationship_manager
|
||||
@@ -201,7 +201,7 @@ class RelationshipBuilder:
|
||||
messages = get_raw_msg_by_timestamp_with_chat_inclusive(self.chat_id, start_time, end_time)
|
||||
return len(messages)
|
||||
|
||||
def _count_messages_between(self, start_time: float, end_time: float) -> int:
|
||||
def _count_messages_between(self, start_time: float, end_time: float) -> Coroutine[Any, Any, int]:
|
||||
"""计算两个时间点之间的消息数量(不包含边界),用于间隔检查"""
|
||||
return num_new_messages_since(self.chat_id, start_time, end_time)
|
||||
|
||||
@@ -314,18 +314,12 @@ class RelationshipBuilder:
|
||||
if not self.person_engaged_cache:
|
||||
return f"{self.log_prefix} 关系缓存为空"
|
||||
|
||||
status_lines = [f"{self.log_prefix} 关系缓存状态:"]
|
||||
status_lines.append(
|
||||
f"最后处理消息时间:{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(self.last_processed_message_time)) if self.last_processed_message_time > 0 else '未设置'}"
|
||||
)
|
||||
status_lines.append(
|
||||
f"最后清理时间:{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(self.last_cleanup_time)) if self.last_cleanup_time > 0 else '未执行'}"
|
||||
)
|
||||
status_lines.append(f"总用户数:{len(self.person_engaged_cache)}")
|
||||
status_lines.append(
|
||||
f"清理配置:{'启用' if SEGMENT_CLEANUP_CONFIG['enable_cleanup'] else '禁用'} (最大保存{SEGMENT_CLEANUP_CONFIG['max_segment_age_days']}天, 每用户最多{SEGMENT_CLEANUP_CONFIG['max_segments_per_user']}段)"
|
||||
)
|
||||
status_lines.append("")
|
||||
status_lines = [f"{self.log_prefix} 关系缓存状态:",
|
||||
f"最后处理消息时间:{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(self.last_processed_message_time)) if self.last_processed_message_time > 0 else '未设置'}",
|
||||
f"最后清理时间:{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(self.last_cleanup_time)) if self.last_cleanup_time > 0 else '未执行'}",
|
||||
f"总用户数:{len(self.person_engaged_cache)}",
|
||||
f"清理配置:{'启用' if SEGMENT_CLEANUP_CONFIG['enable_cleanup'] else '禁用'} (最大保存{SEGMENT_CLEANUP_CONFIG['max_segment_age_days']}天, 每用户最多{SEGMENT_CLEANUP_CONFIG['max_segments_per_user']}段)",
|
||||
""]
|
||||
|
||||
for person_id, segments in self.person_engaged_cache.items():
|
||||
total_count = self._get_total_message_count(person_id)
|
||||
|
||||
@@ -492,7 +492,8 @@ class RelationshipManager:
|
||||
|
||||
return current_points
|
||||
|
||||
def calculate_time_weight(self, point_time: str, current_time: str) -> float:
|
||||
@staticmethod
|
||||
def calculate_time_weight(point_time: str, current_time: str) -> float:
|
||||
"""计算基于时间的权重系数"""
|
||||
try:
|
||||
point_timestamp = datetime.strptime(point_time, "%Y-%m-%d %H:%M:%S")
|
||||
@@ -516,7 +517,8 @@ class RelationshipManager:
|
||||
logger.error(f"计算时间权重失败: {e}")
|
||||
return 0.5 # 发生错误时返回中等权重
|
||||
|
||||
def tfidf_similarity(self, s1, s2):
|
||||
@staticmethod
|
||||
def tfidf_similarity(s1, s2):
|
||||
"""
|
||||
使用 TF-IDF 和余弦相似度计算两个句子的相似性。
|
||||
"""
|
||||
@@ -551,7 +553,8 @@ class RelationshipManager:
|
||||
# 返回 s1 和 s2 的相似度
|
||||
return similarity_matrix[0, 1]
|
||||
|
||||
def sequence_similarity(self, s1, s2):
|
||||
@staticmethod
|
||||
def sequence_similarity(s1, s2):
|
||||
"""
|
||||
使用 SequenceMatcher 计算两个句子的相似性。
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user