数据库的信息重构为dataclass

This commit is contained in:
UnCLAS-Prommer
2025-08-17 17:11:32 +08:00
parent d74beef4b4
commit 3481234d2b
18 changed files with 243 additions and 206 deletions

View File

@@ -285,10 +285,11 @@ class HeartFChatting:
filter_mai=True,
filter_command=True,
)
# TODO: 修复!
temp_recent_messages_dict = [msg.__dict__ for msg in recent_messages_dict]
# 统一的消息处理逻辑
should_process,interest_value = await self._should_process_messages(recent_messages_dict)
should_process,interest_value = await self._should_process_messages(temp_recent_messages_dict)
if should_process:
self.last_read_time = time.time()
await self._observe(interest_value = interest_value)

View File

@@ -346,13 +346,15 @@ class ExpressionLearner:
current_time = time.time()
# 获取上次学习时间
random_msg: Optional[List[Dict[str, Any]]] = get_raw_msg_by_timestamp_with_chat_inclusive(
random_msg_temp = get_raw_msg_by_timestamp_with_chat_inclusive(
chat_id=self.chat_id,
timestamp_start=self.last_learning_time,
timestamp_end=current_time,
limit=num,
)
# TODO: 修复!
random_msg: Optional[List[Dict[str, Any]]] = [msg.__dict__ for msg in random_msg_temp] if random_msg_temp else None
# print(random_msg)
if not random_msg or random_msg == []:
return None

View File

@@ -16,6 +16,7 @@ from rich.traceback import install
from src.llm_models.utils_model import LLMRequest
from src.config.config import global_config, model_config
from src.common.data_models.database_data_model import DatabaseMessages
from src.common.database.database_model import GraphNodes, GraphEdges # Peewee Models导入
from src.common.logger import get_logger
from src.chat.utils.chat_message_builder import (
@@ -1495,13 +1496,13 @@ class MemoryBuilder:
timestamp_end=current_time,
limit=threshold,
)
tmp_msg = [msg.__dict__ for msg in messages] if messages else []
if messages:
# 更新最后处理时间
self.last_processed_time = current_time
self.last_update_time = current_time
return messages or []
return tmp_msg or []

View File

@@ -70,8 +70,10 @@ class ActionModifier:
timestamp=time.time(),
limit=min(int(global_config.chat.max_context_size * 0.33), 10),
)
# TODO: 修复!
temp_msg_list_before_now_half = [msg.__dict__ for msg in message_list_before_now_half]
chat_content = build_readable_messages(
message_list_before_now_half,
temp_msg_list_before_now_half,
replace_bot_name=True,
merge_messages=False,
timestamp_mode="relative",

View File

@@ -95,6 +95,7 @@ class ActionPlanner:
self.max_plan_retries = 3
def find_message_by_id(self, message_id: str, message_id_list: list) -> Optional[Dict[str, Any]]:
# sourcery skip: use-next
"""
根据message_id从message_id_list中查找对应的原始消息
@@ -120,10 +121,7 @@ class ActionPlanner:
Returns:
最新的消息字典如果列表为空则返回None
"""
if not message_id_list:
return None
# 假设消息列表是按时间顺序排列的,最后一个是最新的
return message_id_list[-1].get("message")
return message_id_list[-1].get("message") if message_id_list else None
async def plan(
self,
@@ -208,22 +206,17 @@ class ActionPlanner:
if target_message is None:
self.plan_retry_count += 1
logger.warning(f"{self.log_prefix}无法找到target_message_id '{target_message_id}' 对应的消息,重试次数: {self.plan_retry_count}/{self.max_plan_retries}")
# 如果连续三次plan均为None输出error并选取最新消息
if self.plan_retry_count >= self.max_plan_retries:
logger.error(f"{self.log_prefix}连续{self.max_plan_retries}次plan获取target_message失败选择最新消息作为target_message")
target_message = self.get_latest_message(message_id_list)
self.plan_retry_count = 0 # 重置计数器
else:
# 仍有重试次数
if self.plan_retry_count < self.max_plan_retries:
# 递归重新plan
return await self.plan(mode, loop_start_time, available_actions)
else:
# 成功获取到target_message,重置计数器
self.plan_retry_count = 0
logger.error(f"{self.log_prefix}连续{self.max_plan_retries}次plan获取target_message失败选择最新消息作为target_message")
target_message = self.get_latest_message(message_id_list)
self.plan_retry_count = 0 # 重置计数器
else:
logger.warning(f"{self.log_prefix}动作'{action}'缺少target_message_id")
if action != "no_reply" and action != "reply" and action not in current_available_actions:
logger.warning(
@@ -247,28 +240,27 @@ class ActionPlanner:
is_parallel = False
if mode == ChatMode.NORMAL and action in current_available_actions:
is_parallel = current_available_actions[action].parallel_action
action_data["loop_start_time"] = loop_start_time
actions = []
# 1. 添加Planner取得的动作
actions.append({
"action_type": action,
"reasoning": reasoning,
"action_data": action_data,
"action_message": target_message,
"available_actions": available_actions # 添加这个字段
})
actions = [
{
"action_type": action,
"reasoning": reasoning,
"action_data": action_data,
"action_message": target_message,
"available_actions": available_actions,
}
]
if action != "reply" and is_parallel:
actions.append({
"action_type": "reply",
"action_message": target_message,
"available_actions": available_actions
})
return actions,target_message
@@ -288,9 +280,10 @@ class ActionPlanner:
timestamp=time.time(),
limit=int(global_config.chat.max_context_size * 0.6),
)
# TODO: 修复!
temp_msg_list_before_now = [msg.__dict__ for msg in message_list_before_now]
chat_content_block, message_id_list = build_readable_messages_with_id(
messages=message_list_before_now,
messages=temp_msg_list_before_now,
timestamp_mode="normal_no_YMD",
read_mark=self.last_obs_time_mark,
truncate=True,

View File

@@ -91,7 +91,7 @@ def init_prompt():
""",
"replyer_prompt",
)
Prompt(
"""
{expression_habits_block}{tool_info_block}
@@ -116,7 +116,6 @@ def init_prompt():
""",
"replyer_self_prompt",
)
Prompt(
"""
@@ -179,7 +178,7 @@ class DefaultReplyer:
Returns:
Tuple[bool, Optional[Dict[str, Any]], Optional[str]]: (是否成功, 生成的回复, 使用的prompt)
"""
prompt = None
selected_expressions = None
if available_actions is None:
@@ -187,7 +186,7 @@ class DefaultReplyer:
try:
# 3. 构建 Prompt
with Timer("构建Prompt", {}): # 内部计时器,可选保留
prompt,selected_expressions = await self.build_prompt_reply_context(
prompt, selected_expressions = await self.build_prompt_reply_context(
extra_info=extra_info,
available_actions=available_actions,
choosen_actions=choosen_actions,
@@ -294,12 +293,12 @@ class DefaultReplyer:
async def build_relation_info(self, sender: str, target: str):
if not global_config.relationship.enable_relationship:
return ""
if sender == global_config.bot.nickname:
return ""
# 获取用户ID
person = Person(person_name = sender)
person = Person(person_name=sender)
if not is_person_known(person_name=sender):
logger.warning(f"未找到用户 {sender} 的ID跳过信息提取")
return f"你完全不认识{sender}不理解ta的相关信息。"
@@ -307,6 +306,7 @@ class DefaultReplyer:
return person.build_relationship(points_num=5)
async def build_expression_habits(self, chat_history: str, target: str) -> Tuple[str, List[int]]:
# sourcery skip: for-append-to-extend
"""构建表达习惯块
Args:
@@ -359,7 +359,7 @@ class DefaultReplyer:
Returns:
str: 记忆信息字符串
"""
if not global_config.memory.enable_memory:
return ""
@@ -368,7 +368,6 @@ class DefaultReplyer:
running_memories = await self.memory_activator.activate_memory_with_chat_history(
target_message=target, chat_history_prompt=chat_history
)
if global_config.memory.enable_instant_memory:
asyncio.create_task(self.instant_memory.create_and_store_memory(chat_history))
@@ -379,10 +378,9 @@ class DefaultReplyer:
if not running_memories:
return ""
memory_str = "以下是当前在聊天中,你回忆起的记忆:\n"
for running_memory in running_memories:
keywords,content = running_memory
keywords, content = running_memory
memory_str += f"- {keywords}{content}\n"
if instant_memory:
@@ -405,7 +403,6 @@ class DefaultReplyer:
if not enable_tool:
return ""
try:
# 使用工具执行器获取信息
tool_results, _, _ = await self.tool_executor.execute_from_chat_message(
@@ -559,16 +556,18 @@ class DefaultReplyer:
# 检查最新五条消息中是否包含bot自己说的消息
latest_5_messages = core_dialogue_list[-5:] if len(core_dialogue_list) >= 5 else core_dialogue_list
has_bot_message = any(str(msg.get("user_id")) == bot_id for msg in latest_5_messages)
# logger.info(f"最新五条消息:{latest_5_messages}")
# logger.info(f"最新五条消息中是否包含bot自己说的消息{has_bot_message}")
# 如果最新五条消息中不包含bot的消息则返回空字符串
if not has_bot_message:
core_dialogue_prompt = ""
else:
core_dialogue_list = core_dialogue_list[-int(global_config.chat.max_context_size * 0.6) :] # 限制消息数量
core_dialogue_list = core_dialogue_list[
-int(global_config.chat.max_context_size * 0.6) :
] # 限制消息数量
core_dialogue_prompt_str = build_readable_messages(
core_dialogue_list,
replace_bot_name=True,
@@ -630,12 +629,12 @@ class DefaultReplyer:
mai_think.sender = sender
mai_think.target = target
return mai_think
async def build_actions_prompt(self, available_actions, choosen_actions: Optional[List[Dict[str, Any]]] = None) -> str:
"""构建动作提示
"""
async def build_actions_prompt(
self, available_actions, choosen_actions: Optional[List[Dict[str, Any]]] = None
) -> str:
"""构建动作提示"""
action_descriptions = ""
if available_actions:
action_descriptions = "你可以做以下这些动作:\n"
@@ -643,25 +642,24 @@ class DefaultReplyer:
action_description = action_info.description
action_descriptions += f"- {action_name}: {action_description}\n"
action_descriptions += "\n"
choosen_action_descriptions = ""
if choosen_actions:
for action in choosen_actions:
action_name = action.get('action_type', 'unknown_action')
if action_name =="reply":
action_name = action.get("action_type", "unknown_action")
if action_name == "reply":
continue
action_description = action.get('reason', '无描述')
reasoning = action.get('reasoning', '无原因')
action_description = action.get("reason", "无描述")
reasoning = action.get("reasoning", "无原因")
choosen_action_descriptions += f"- {action_name}: {action_description},原因:{reasoning}\n"
if choosen_action_descriptions:
action_descriptions += "根据聊天情况,你决定在回复的同时做以下这些动作:\n"
action_descriptions += choosen_action_descriptions
return action_descriptions
async def build_prompt_reply_context(
self,
extra_info: str = "",
@@ -691,41 +689,44 @@ class DefaultReplyer:
chat_id = chat_stream.stream_id
is_group_chat = bool(chat_stream.group_info)
platform = chat_stream.platform
if reply_message:
user_id = reply_message.get("user_id","")
user_id = reply_message.get("user_id", "")
person = Person(platform=platform, user_id=user_id)
person_name = person.person_name or user_id
sender = person_name
target = reply_message.get('processed_plain_text')
target = reply_message.get("processed_plain_text")
else:
person_name = "用户"
sender = "用户"
target = "消息"
if global_config.mood.enable_mood:
chat_mood = mood_manager.get_mood_by_chat_id(chat_id)
mood_prompt = chat_mood.mood_state
else:
mood_prompt = ""
target = replace_user_references_sync(target, chat_stream.platform, replace_bot_name=True)
# TODO: 修复!
message_list_before_now_long = get_raw_msg_before_timestamp_with_chat(
chat_id=chat_id,
timestamp=time.time(),
limit=global_config.chat.max_context_size * 1,
)
temp_msg_list_before_long = [msg.__dict__ for msg in message_list_before_now_long]
# TODO: 修复!
message_list_before_short = get_raw_msg_before_timestamp_with_chat(
chat_id=chat_id,
timestamp=time.time(),
limit=int(global_config.chat.max_context_size * 0.33),
)
temp_msg_list_before_short = [msg.__dict__ for msg in message_list_before_short]
chat_talking_prompt_short = build_readable_messages(
message_list_before_short,
temp_msg_list_before_short,
replace_bot_name=True,
merge_messages=False,
timestamp_mode="relative",
@@ -739,12 +740,12 @@ class DefaultReplyer:
self.build_expression_habits(chat_talking_prompt_short, target), "expression_habits"
),
self._time_and_run_task(self.build_relation_info(sender, target), "relation_info"),
self._time_and_run_task(self.build_memory_block(message_list_before_short, target), "memory_block"),
self._time_and_run_task(self.build_memory_block(temp_msg_list_before_short, target), "memory_block"),
self._time_and_run_task(
self.build_tool_info(chat_talking_prompt_short, sender, target, enable_tool=enable_tool), "tool_info"
),
self._time_and_run_task(self.get_prompt_info(chat_talking_prompt_short, sender, target), "prompt_info"),
self._time_and_run_task(self.build_actions_prompt(available_actions,choosen_actions), "actions_info"),
self._time_and_run_task(self.build_actions_prompt(available_actions, choosen_actions), "actions_info"),
)
# 任务名称中英文映射
@@ -760,7 +761,7 @@ class DefaultReplyer:
# 处理结果
timing_logs = []
results_dict = {}
almost_zero_str = ""
for name, result, duration in task_results:
results_dict[name] = result
@@ -768,7 +769,7 @@ class DefaultReplyer:
if duration < 0.01:
almost_zero_str += f"{chinese_name},"
continue
timing_logs.append(f"{chinese_name}: {duration:.1f}s")
if duration > 8:
logger.warning(f"回复生成前信息获取耗时过长: {chinese_name} 耗时: {duration:.1f}s请使用更快的模型")
@@ -791,9 +792,7 @@ class DefaultReplyer:
identity_block = await get_individuality().get_personality_block()
moderation_prompt_block = (
"请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。"
)
moderation_prompt_block = "请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。"
if sender:
if is_group_chat:
@@ -801,7 +800,9 @@ class DefaultReplyer:
f"现在{sender}说的:{target}。引起了你的注意,你想要在群里发言或者回复这条消息。原因是{reply_reason}"
)
else: # private chat
reply_target_block = f"现在{sender}说的:{target}。引起了你的注意,针对这条消息回复。原因是{reply_reason}"
reply_target_block = (
f"现在{sender}说的:{target}。引起了你的注意,针对这条消息回复。原因是{reply_reason}"
)
else:
reply_target_block = ""
@@ -821,10 +822,9 @@ class DefaultReplyer:
# "chat_target_private2", sender_name=chat_target_name
# )
# 构建分离的对话 prompt
core_dialogue_prompt, background_dialogue_prompt = self.build_s4u_chat_history_prompts(
message_list_before_now_long, user_id, sender
temp_msg_list_before_long, user_id, sender
)
if global_config.bot.qq_account == user_id and platform == global_config.bot.platform:
@@ -846,7 +846,7 @@ class DefaultReplyer:
reply_style=global_config.personality.reply_style,
keywords_reaction_prompt=keywords_reaction_prompt,
moderation_prompt=moderation_prompt_block,
),selected_expressions
), selected_expressions
else:
return await global_prompt_manager.format_prompt(
"replyer_prompt",
@@ -867,7 +867,7 @@ class DefaultReplyer:
reply_style=global_config.personality.reply_style,
keywords_reaction_prompt=keywords_reaction_prompt,
moderation_prompt=moderation_prompt_block,
),selected_expressions
), selected_expressions
async def build_prompt_rewrite_context(
self,
@@ -898,8 +898,10 @@ class DefaultReplyer:
timestamp=time.time(),
limit=min(int(global_config.chat.max_context_size * 0.33), 15),
)
# TODO: 修复!
temp_msg_list_before_now_half = [msg.__dict__ for msg in message_list_before_now_half]
chat_talking_prompt_half = build_readable_messages(
message_list_before_now_half,
temp_msg_list_before_now_half,
replace_bot_name=True,
merge_messages=False,
timestamp_mode="relative",
@@ -912,7 +914,6 @@ class DefaultReplyer:
self.build_expression_habits(chat_talking_prompt_half, target),
self.build_relation_info(sender, target),
)
keywords_reaction_prompt = await self.build_keywords_reaction_prompt(target)
@@ -1024,7 +1025,9 @@ class DefaultReplyer:
else:
logger.debug(f"\n{prompt}\n")
content, (reasoning_content, model_name, tool_calls) = await self.express_model.generate_response_async(prompt)
content, (reasoning_content, model_name, tool_calls) = await self.express_model.generate_response_async(
prompt
)
logger.debug(f"replyer生成内容: {content}")
return content, reasoning_content, model_name, tool_calls
@@ -1034,7 +1037,6 @@ class DefaultReplyer:
start_time = time.time()
from src.plugins.built_in.knowledge.lpmm_get_knowledge import SearchKnowledgeFromLPMMTool
logger.debug(f"获取知识库内容,元消息:{message[:30]}...,消息长度: {len(message)}")
# 从LPMM知识库获取知识
try:

View File

@@ -7,9 +7,10 @@ from rich.traceback import install
from src.config.config import global_config
from src.common.message_repository import find_messages, count_messages
from src.common.data_models.database_data_model import DatabaseMessages
from src.common.database.database_model import ActionRecords
from src.common.database.database_model import Images
from src.person_info.person_info import Person,get_person_id
from src.person_info.person_info import Person, get_person_id
from src.chat.utils.utils import translate_timestamp_to_human_readable, assign_message_ids
install(extra_lines=3)
@@ -35,6 +36,7 @@ def replace_user_references_sync(
str: 处理后的内容字符串
"""
if name_resolver is None:
def default_resolver(platform: str, user_id: str) -> str:
# 检查是否是机器人自己
if replace_bot_name and user_id == global_config.bot.qq_account:
@@ -108,6 +110,7 @@ async def replace_user_references_async(
str: 处理后的内容字符串
"""
if name_resolver is None:
async def default_resolver(platform: str, user_id: str) -> str:
# 检查是否是机器人自己
if replace_bot_name and user_id == global_config.bot.qq_account:
@@ -161,9 +164,7 @@ async def replace_user_references_async(
return content
def get_raw_msg_by_timestamp(
timestamp_start: float, timestamp_end: float, limit: int = 0, limit_mode: str = "latest"
) -> List[Dict[str, Any]]:
def get_raw_msg_by_timestamp(timestamp_start: float, timestamp_end: float, limit: int = 0, limit_mode: str = "latest"):
"""
获取从指定时间戳到指定时间戳的消息,按时间升序排序,返回消息列表
limit: 限制返回的消息数量0为不限制
@@ -183,7 +184,7 @@ def get_raw_msg_by_timestamp_with_chat(
limit_mode: str = "latest",
filter_bot=False,
filter_command=False,
) -> List[Dict[str, Any]]:
) -> List[DatabaseMessages]:
"""获取在特定聊天从指定时间戳到指定时间戳的消息,按时间升序排序,返回消息列表
limit: 限制返回的消息数量0为不限制
limit_mode: 当 limit > 0 时生效。 'earliest' 表示获取最早的记录, 'latest' 表示获取最新的记录。默认为 'latest'
@@ -209,7 +210,7 @@ def get_raw_msg_by_timestamp_with_chat_inclusive(
limit: int = 0,
limit_mode: str = "latest",
filter_bot=False,
) -> List[Dict[str, Any]]:
) -> List[DatabaseMessages]:
"""获取在特定聊天从指定时间戳到指定时间戳的消息(包含边界),按时间升序排序,返回消息列表
limit: 限制返回的消息数量0为不限制
limit_mode: 当 limit > 0 时生效。 'earliest' 表示获取最早的记录, 'latest' 表示获取最新的记录。默认为 'latest'
@@ -218,7 +219,6 @@ def get_raw_msg_by_timestamp_with_chat_inclusive(
# 只有当 limit 为 0 时才应用外部 sort
sort_order = [("time", 1)] if limit == 0 else None
# 直接将 limit_mode 传递给 find_messages
return find_messages(
message_filter=filter_query, sort=sort_order, limit=limit, limit_mode=limit_mode, filter_bot=filter_bot
)
@@ -231,7 +231,7 @@ def get_raw_msg_by_timestamp_with_chat_users(
person_ids: List[str],
limit: int = 0,
limit_mode: str = "latest",
) -> List[Dict[str, Any]]:
) -> List[DatabaseMessages]:
"""获取某些特定用户在特定聊天从指定时间戳到指定时间戳的消息,按时间升序排序,返回消息列表
limit: 限制返回的消息数量0为不限制
limit_mode: 当 limit > 0 时生效。 'earliest' 表示获取最早的记录, 'latest' 表示获取最新的记录。默认为 'latest'
@@ -302,7 +302,7 @@ def get_actions_by_timestamp_with_chat_inclusive(
def get_raw_msg_by_timestamp_random(
timestamp_start: float, timestamp_end: float, limit: int = 0, limit_mode: str = "latest"
) -> List[Dict[str, Any]]:
) -> List[DatabaseMessages]:
"""
先在范围时间戳内随机选择一条消息取得消息的chat_id然后根据chat_id获取该聊天在指定时间戳范围内的消息
"""
@@ -312,15 +312,15 @@ def get_raw_msg_by_timestamp_random(
return []
# 随机选一条
msg = random.choice(all_msgs)
chat_id = msg["chat_id"]
timestamp_start = msg["time"]
chat_id = msg.chat_id
timestamp_start = msg.time
# 用 chat_id 获取该聊天在指定时间戳范围内的消息
return get_raw_msg_by_timestamp_with_chat(chat_id, timestamp_start, timestamp_end, limit, "earliest")
def get_raw_msg_by_timestamp_with_users(
timestamp_start: float, timestamp_end: float, person_ids: list, limit: int = 0, limit_mode: str = "latest"
) -> List[Dict[str, Any]]:
) -> List[DatabaseMessages]:
"""获取某些特定用户在 *所有聊天* 中从指定时间戳到指定时间戳的消息,按时间升序排序,返回消息列表
limit: 限制返回的消息数量0为不限制
limit_mode: 当 limit > 0 时生效。 'earliest' 表示获取最早的记录, 'latest' 表示获取最新的记录。默认为 'latest'
@@ -331,7 +331,7 @@ def get_raw_msg_by_timestamp_with_users(
return find_messages(message_filter=filter_query, sort=sort_order, limit=limit, limit_mode=limit_mode)
def get_raw_msg_before_timestamp(timestamp: float, limit: int = 0) -> List[Dict[str, Any]]:
def get_raw_msg_before_timestamp(timestamp: float, limit: int = 0) -> List[DatabaseMessages]:
"""获取指定时间戳之前的消息,按时间升序排序,返回消息列表
limit: 限制返回的消息数量0为不限制
"""
@@ -340,7 +340,7 @@ def get_raw_msg_before_timestamp(timestamp: float, limit: int = 0) -> List[Dict[
return find_messages(message_filter=filter_query, sort=sort_order, limit=limit)
def get_raw_msg_before_timestamp_with_chat(chat_id: str, timestamp: float, limit: int = 0) -> List[Dict[str, Any]]:
def get_raw_msg_before_timestamp_with_chat(chat_id: str, timestamp: float, limit: int = 0) -> List[DatabaseMessages]:
"""获取指定时间戳之前的消息,按时间升序排序,返回消息列表
limit: 限制返回的消息数量0为不限制
"""
@@ -349,7 +349,7 @@ def get_raw_msg_before_timestamp_with_chat(chat_id: str, timestamp: float, limit
return find_messages(message_filter=filter_query, sort=sort_order, limit=limit)
def get_raw_msg_before_timestamp_with_users(timestamp: float, person_ids: list, limit: int = 0) -> List[Dict[str, Any]]:
def get_raw_msg_before_timestamp_with_users(timestamp: float, person_ids: list, limit: int = 0) -> List[DatabaseMessages]:
"""获取指定时间戳之前的消息,按时间升序排序,返回消息列表
limit: 限制返回的消息数量0为不限制
"""

View File

@@ -3,13 +3,15 @@ import re
import string
import time
import jieba
import json
import ast
import numpy as np
from collections import Counter
from maim_message import UserInfo
from typing import Optional, Tuple, Dict, List, Any
from src.common.logger import get_logger
from src.common.data_models.info_data_model import TargetPersonInfo
from src.common.message_repository import find_messages, count_messages
from src.config.config import global_config, model_config
from src.chat.message_receive.message import MessageRecv
@@ -130,22 +132,29 @@ def get_recent_group_speaker(chat_stream_id: str, sender, limit: int = 12) -> li
return []
who_chat_in_group = []
for msg_db_data in recent_messages:
user_info = UserInfo.from_dict(
{
"platform": msg_db_data["user_platform"],
"user_id": msg_db_data["user_id"],
"user_nickname": msg_db_data["user_nickname"],
"user_cardname": msg_db_data.get("user_cardname", ""),
}
)
for db_msg in recent_messages:
# user_info = UserInfo.from_dict(
# {
# "platform": msg_db_data["user_platform"],
# "user_id": msg_db_data["user_id"],
# "user_nickname": msg_db_data["user_nickname"],
# "user_cardname": msg_db_data.get("user_cardname", ""),
# }
# )
# if (
# (user_info.platform, user_info.user_id) != sender
# and user_info.user_id != global_config.bot.qq_account
# and (user_info.platform, user_info.user_id, user_info.user_nickname) not in who_chat_in_group
# and len(who_chat_in_group) < 5
# ): # 排除重复排除消息发送者排除bot限制加载的关系数目
# who_chat_in_group.append((user_info.platform, user_info.user_id, user_info.user_nickname))
if (
(user_info.platform, user_info.user_id) != sender
and user_info.user_id != global_config.bot.qq_account
and (user_info.platform, user_info.user_id, user_info.user_nickname) not in who_chat_in_group
(db_msg.user_info.platform, db_msg.user_info.user_id) != sender
and db_msg.user_info.user_id != global_config.bot.qq_account
and (db_msg.user_info.platform, db_msg.user_info.user_id, db_msg.user_info.user_nickname) not in who_chat_in_group
and len(who_chat_in_group) < 5
): # 排除重复排除消息发送者排除bot限制加载的关系数目
who_chat_in_group.append((user_info.platform, user_info.user_id, user_info.user_nickname))
who_chat_in_group.append((db_msg.user_info.platform, db_msg.user_info.user_id, db_msg.user_info.user_nickname))
return who_chat_in_group
@@ -555,7 +564,7 @@ def count_messages_between(start_time: float, end_time: float, stream_id: str) -
# 获取消息内容计算总长度
messages = find_messages(message_filter=filter_query)
total_length = sum(len(msg.get("processed_plain_text", "")) for msg in messages)
total_length = sum(len(msg.processed_plain_text or "") for msg in messages)
return count, total_length
@@ -628,41 +637,34 @@ def get_chat_type_and_target_info(chat_id: str) -> Tuple[bool, Optional[Dict]]:
user_id: str = user_info.user_id # type: ignore
# Initialize target_info with basic info
target_info = {
"platform": platform,
"user_id": user_id,
"user_nickname": user_info.user_nickname,
"person_id": None,
"person_name": None,
}
target_info = TargetPersonInfo(
platform=platform,
user_id=user_id,
user_nickname=user_info.user_nickname, # type: ignore
person_id=None,
person_name=None
)
# Try to fetch person info
try:
# Assume get_person_id is sync (as per original code), keep using to_thread
person = Person(platform=platform, user_id=user_id)
if not person.is_known:
logger.warning(f"用户 {user_info.user_nickname} 尚未认识")
# 如果用户尚未认识则返回False和None
return False, None
person_id = person.person_id
person_name = None
if person_id:
# get_value is async, so await it directly
person_name = person.person_name
target_info["person_id"] = person_id
target_info["person_name"] = person_name
if person.person_id:
target_info.person_id = person.person_id
target_info.person_name = person.person_name
except Exception as person_e:
logger.warning(
f"获取 person_id 或 person_name 时出错 for {platform}:{user_id} in utils: {person_e}"
)
chat_target_info = target_info
chat_target_info = target_info.__dict__
else:
logger.warning(f"无法获取 chat_stream for {chat_id} in utils")
except Exception as e:
logger.error(f"获取聊天类型和目标信息时出错 for {chat_id}: {e}", exc_info=True)
# Keep defaults on error
return is_group_chat, chat_target_info
@@ -771,6 +773,7 @@ def assign_message_ids_flexible(
# # 结果: [{'id': 'ts123a1b', 'message': 'Hello'}, {'id': 'ts123c2d', 'message': 'World'}, {'id': 'ts123e3f', 'message': 'Test message'}]
def parse_keywords_string(keywords_input) -> list[str]:
# sourcery skip: use-contextlib-suppress
"""
统一的关键词解析函数,支持多种格式的关键词字符串解析
@@ -802,7 +805,6 @@ def parse_keywords_string(keywords_input) -> list[str]:
try:
# 尝试作为JSON对象解析支持 {"keywords": [...]} 格式)
import json
json_data = json.loads(keywords_str)
if isinstance(json_data, dict) and "keywords" in json_data:
keywords_list = json_data["keywords"]
@@ -816,7 +818,6 @@ def parse_keywords_string(keywords_input) -> list[str]:
try:
# 尝试使用 ast.literal_eval 解析支持Python字面量格式
import ast
parsed = ast.literal_eval(keywords_str)
if isinstance(parsed, list):
return [str(k).strip() for k in parsed if str(k).strip()]