fix:间隔过长的消息在回复器中会特殊处理

This commit is contained in:
SengokuCola
2025-12-27 11:54:31 +08:00
parent 6acb1ff0ed
commit ba9b9d26a2
10 changed files with 3653 additions and 1783 deletions

View File

@@ -370,6 +370,7 @@ def _build_readable_messages_internal(
show_pic: bool = True,
message_id_list: Optional[List[Tuple[str, DatabaseMessages]]] = None,
pic_single: bool = False,
long_time_notice: bool = False,
) -> Tuple[str, List[Tuple[float, str, str]], Dict[str, str], int]:
# sourcery skip: use-getitem-for-re-match-groups
"""
@@ -523,7 +524,30 @@ def _build_readable_messages_internal(
# 3: 格式化为字符串
output_lines: List[str] = []
prev_timestamp: Optional[float] = None
for timestamp, name, content, is_action in detailed_message:
# 检查是否需要插入长时间间隔提示
if long_time_notice and prev_timestamp is not None:
time_diff = timestamp - prev_timestamp
time_diff_hours = time_diff / 3600
# 检查是否跨天
prev_date = time.strftime("%Y-%m-%d", time.localtime(prev_timestamp))
current_date = time.strftime("%Y-%m-%d", time.localtime(timestamp))
is_cross_day = prev_date != current_date
# 如果间隔大于8小时或跨天插入提示
if time_diff_hours > 8 or is_cross_day:
# 格式化日期为中文格式xxxx年xx月xx日去掉前导零
current_time_struct = time.localtime(timestamp)
year = current_time_struct.tm_year
month = current_time_struct.tm_mon
day = current_time_struct.tm_mday
date_str = f"{year}{month}{day}"
hours_str = f"{int(time_diff_hours)}h"
notice = f"以下聊天开始时间:{date_str}。距离上一条消息过去了{hours_str}\n"
output_lines.append(notice)
readable_time = translate_timestamp_to_human_readable(timestamp, mode=timestamp_mode)
# 查找消息id如果有并构建id_prefix
@@ -536,6 +560,8 @@ def _build_readable_messages_internal(
else:
output_lines.append(f"{id_prefix}{readable_time}, {name}: {content}")
output_lines.append("\n") # 在每个消息块后添加换行,保持可读性
prev_timestamp = timestamp
formatted_string = "".join(output_lines).strip()
@@ -651,6 +677,7 @@ async def build_readable_messages_with_list(
show_pic=True,
message_id_list=None,
pic_single=pic_single,
long_time_notice=False,
)
if not pic_single:
@@ -704,6 +731,7 @@ def build_readable_messages(
message_id_list: Optional[List[Tuple[str, DatabaseMessages]]] = None,
remove_emoji_stickers: bool = False,
pic_single: bool = False,
long_time_notice: bool = False,
) -> str: # sourcery skip: extract-method
"""
将消息列表转换为可读的文本格式。
@@ -719,6 +747,7 @@ def build_readable_messages(
truncate: 是否截断长消息
show_actions: 是否显示动作记录
remove_emoji_stickers: 是否移除表情包并过滤空消息
long_time_notice: 是否在消息间隔过长(>8小时或跨天时插入时间提示
"""
# WIP HERE and BELOW ----------------------------------------------
# 创建messages的深拷贝避免修改原始列表
@@ -812,6 +841,7 @@ def build_readable_messages(
show_pic=show_pic,
message_id_list=message_id_list,
pic_single=pic_single,
long_time_notice=long_time_notice,
)
if not pic_single:
@@ -839,6 +869,7 @@ def build_readable_messages(
show_pic=show_pic,
message_id_list=message_id_list,
pic_single=pic_single,
long_time_notice=long_time_notice,
)
formatted_after, _, pic_id_mapping, _ = _build_readable_messages_internal(
messages_after_mark,
@@ -850,6 +881,7 @@ def build_readable_messages(
show_pic=show_pic,
message_id_list=message_id_list,
pic_single=pic_single,
long_time_notice=long_time_notice,
)
read_mark_line = "\n--- 以上消息是你已经看过,请关注以下未读的新消息---\n"