在聊天消息构建器中添加日志记录功能,记录函数调用参数和处理过程,优化动作记录的返回格式,确保返回的动作记录包含必要字段。
This commit is contained in:
@@ -259,7 +259,16 @@ def get_actions_by_timestamp_with_chat(
|
|||||||
limit_mode: str = "latest",
|
limit_mode: str = "latest",
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""获取在特定聊天从指定时间戳到指定时间戳的动作记录,按时间升序排序,返回动作记录列表"""
|
"""获取在特定聊天从指定时间戳到指定时间戳的动作记录,按时间升序排序,返回动作记录列表"""
|
||||||
|
from src.common.logger import get_logger
|
||||||
from src.common.database.sqlalchemy_database_api import get_db_session
|
from src.common.database.sqlalchemy_database_api import get_db_session
|
||||||
|
|
||||||
|
logger = get_logger("chat_message_builder")
|
||||||
|
|
||||||
|
# 记录函数调用参数
|
||||||
|
logger.debug(f"[get_actions_by_timestamp_with_chat] 调用参数: chat_id={chat_id}, "
|
||||||
|
f"timestamp_start={timestamp_start}, timestamp_end={timestamp_end}, "
|
||||||
|
f"limit={limit}, limit_mode={limit_mode}")
|
||||||
|
|
||||||
with get_db_session() as session:
|
with get_db_session() as session:
|
||||||
if limit > 0:
|
if limit > 0:
|
||||||
if limit_mode == "latest":
|
if limit_mode == "latest":
|
||||||
@@ -271,7 +280,22 @@ def get_actions_by_timestamp_with_chat(
|
|||||||
)
|
)
|
||||||
).order_by(ActionRecords.time.desc()).limit(limit))
|
).order_by(ActionRecords.time.desc()).limit(limit))
|
||||||
actions = list(query.scalars())
|
actions = list(query.scalars())
|
||||||
return [action.__dict__ for action in reversed(actions)]
|
actions_result = []
|
||||||
|
for action in reversed(actions):
|
||||||
|
action_dict = {
|
||||||
|
'id': action.id,
|
||||||
|
'action_id': action.action_id,
|
||||||
|
'time': action.time,
|
||||||
|
'action_name': action.action_name,
|
||||||
|
'action_data': action.action_data,
|
||||||
|
'action_done': action.action_done,
|
||||||
|
'action_build_into_prompt': action.action_build_into_prompt,
|
||||||
|
'action_prompt_display': action.action_prompt_display,
|
||||||
|
'chat_id': action.chat_id,
|
||||||
|
'chat_info_stream_id': action.chat_info_stream_id,
|
||||||
|
'chat_info_platform': action.chat_info_platform,
|
||||||
|
}
|
||||||
|
actions_result.append(action_dict)
|
||||||
else: # earliest
|
else: # earliest
|
||||||
query = session.execute(select(ActionRecords).where(
|
query = session.execute(select(ActionRecords).where(
|
||||||
and_(
|
and_(
|
||||||
@@ -280,6 +304,23 @@ def get_actions_by_timestamp_with_chat(
|
|||||||
ActionRecords.time < timestamp_end
|
ActionRecords.time < timestamp_end
|
||||||
)
|
)
|
||||||
).order_by(ActionRecords.time.asc()).limit(limit))
|
).order_by(ActionRecords.time.asc()).limit(limit))
|
||||||
|
actions = list(query.scalars())
|
||||||
|
actions_result = []
|
||||||
|
for action in actions:
|
||||||
|
action_dict = {
|
||||||
|
'id': action.id,
|
||||||
|
'action_id': action.action_id,
|
||||||
|
'time': action.time,
|
||||||
|
'action_name': action.action_name,
|
||||||
|
'action_data': action.action_data,
|
||||||
|
'action_done': action.action_done,
|
||||||
|
'action_build_into_prompt': action.action_build_into_prompt,
|
||||||
|
'action_prompt_display': action.action_prompt_display,
|
||||||
|
'chat_id': action.chat_id,
|
||||||
|
'chat_info_stream_id': action.chat_info_stream_id,
|
||||||
|
'chat_info_platform': action.chat_info_platform,
|
||||||
|
}
|
||||||
|
actions_result.append(action_dict)
|
||||||
else:
|
else:
|
||||||
query = session.execute(select(ActionRecords).where(
|
query = session.execute(select(ActionRecords).where(
|
||||||
and_(
|
and_(
|
||||||
@@ -288,9 +329,24 @@ def get_actions_by_timestamp_with_chat(
|
|||||||
ActionRecords.time < timestamp_end
|
ActionRecords.time < timestamp_end
|
||||||
)
|
)
|
||||||
).order_by(ActionRecords.time.asc()))
|
).order_by(ActionRecords.time.asc()))
|
||||||
|
actions = list(query.scalars())
|
||||||
actions = list(query.scalars())
|
actions_result = []
|
||||||
return [action.__dict__ for action in actions]
|
for action in actions:
|
||||||
|
action_dict = {
|
||||||
|
'id': action.id,
|
||||||
|
'action_id': action.action_id,
|
||||||
|
'time': action.time,
|
||||||
|
'action_name': action.action_name,
|
||||||
|
'action_data': action.action_data,
|
||||||
|
'action_done': action.action_done,
|
||||||
|
'action_build_into_prompt': action.action_build_into_prompt,
|
||||||
|
'action_prompt_display': action.action_prompt_display,
|
||||||
|
'chat_id': action.chat_id,
|
||||||
|
'chat_info_stream_id': action.chat_info_stream_id,
|
||||||
|
'chat_info_platform': action.chat_info_platform,
|
||||||
|
}
|
||||||
|
actions_result.append(action_dict)
|
||||||
|
return actions_result
|
||||||
|
|
||||||
|
|
||||||
def get_actions_by_timestamp_with_chat_inclusive(
|
def get_actions_by_timestamp_with_chat_inclusive(
|
||||||
@@ -758,24 +814,50 @@ def build_readable_actions(actions: List[Dict[str, Any]]) -> str:
|
|||||||
Returns:
|
Returns:
|
||||||
格式化的动作字符串。
|
格式化的动作字符串。
|
||||||
"""
|
"""
|
||||||
|
from src.common.logger import get_logger
|
||||||
|
logger = get_logger("chat_message_builder")
|
||||||
|
|
||||||
|
logger.info(f"[build_readable_actions] 开始处理 {len(actions) if actions else 0} 条动作记录")
|
||||||
|
|
||||||
if not actions:
|
if not actions:
|
||||||
|
logger.debug("[build_readable_actions] 动作记录为空,返回空字符串")
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
output_lines = []
|
output_lines = []
|
||||||
current_time = time.time()
|
current_time = time.time()
|
||||||
|
|
||||||
|
logger.info(f"[build_readable_actions] 当前时间戳: {current_time}")
|
||||||
|
|
||||||
# The get functions return actions sorted ascending by time. Let's reverse it to show newest first.
|
# The get functions return actions sorted ascending by time. Let's reverse it to show newest first.
|
||||||
# sorted_actions = sorted(actions, key=lambda x: x.get("time", 0), reverse=True)
|
# sorted_actions = sorted(actions, key=lambda x: x.get("time", 0), reverse=True)
|
||||||
|
|
||||||
for action in actions:
|
for i, action in enumerate(actions):
|
||||||
|
logger.info(f"[build_readable_actions] === 处理第 {i} 条动作记录 ===")
|
||||||
|
logger.info(f"[build_readable_actions] 原始动作数据: {action}")
|
||||||
|
|
||||||
action_time = action.get("time", current_time)
|
action_time = action.get("time", current_time)
|
||||||
action_name = action.get("action_name", "未知动作")
|
action_name = action.get("action_name", "未知动作")
|
||||||
|
|
||||||
|
logger.info(f"[build_readable_actions] 动作时间戳: {action_time}, 动作名称: '{action_name}'")
|
||||||
|
|
||||||
|
# 检查是否是原始的 action_name 值
|
||||||
|
original_action_name = action.get("action_name")
|
||||||
|
if original_action_name is None:
|
||||||
|
logger.error(f"[build_readable_actions] 动作 #{i}: action_name 为 None!")
|
||||||
|
elif original_action_name == "":
|
||||||
|
logger.error(f"[build_readable_actions] 动作 #{i}: action_name 为空字符串!")
|
||||||
|
elif original_action_name == "未知动作":
|
||||||
|
logger.error(f"[build_readable_actions] 动作 #{i}: action_name 已经是'未知动作'!")
|
||||||
|
|
||||||
if action_name in ["no_action", "no_reply"]:
|
if action_name in ["no_action", "no_reply"]:
|
||||||
|
logger.debug(f"[build_readable_actions] 跳过动作 #{i}: {action_name} (在跳过列表中)")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
action_prompt_display = action.get("action_prompt_display", "无具体内容")
|
action_prompt_display = action.get("action_prompt_display", "无具体内容")
|
||||||
|
logger.info(f"[build_readable_actions] 动作提示显示: '{action_prompt_display}'")
|
||||||
|
|
||||||
time_diff_seconds = current_time - action_time
|
time_diff_seconds = current_time - action_time
|
||||||
|
logger.info(f"[build_readable_actions] 时间差: {time_diff_seconds} 秒")
|
||||||
|
|
||||||
if time_diff_seconds < 60:
|
if time_diff_seconds < 60:
|
||||||
time_ago_str = f"在{int(time_diff_seconds)}秒前"
|
time_ago_str = f"在{int(time_diff_seconds)}秒前"
|
||||||
@@ -783,10 +865,15 @@ def build_readable_actions(actions: List[Dict[str, Any]]) -> str:
|
|||||||
time_diff_minutes = round(time_diff_seconds / 60)
|
time_diff_minutes = round(time_diff_seconds / 60)
|
||||||
time_ago_str = f"在{int(time_diff_minutes)}分钟前"
|
time_ago_str = f"在{int(time_diff_minutes)}分钟前"
|
||||||
|
|
||||||
line = f"{time_ago_str},你使用了“{action_name}”,具体内容是:“{action_prompt_display}”"
|
logger.info(f"[build_readable_actions] 时间描述: '{time_ago_str}'")
|
||||||
|
|
||||||
|
line = f"{time_ago_str},你使用了\"{action_name}\",具体内容是:\"{action_prompt_display}\""
|
||||||
|
logger.info(f"[build_readable_actions] 生成的行: '{line}'")
|
||||||
output_lines.append(line)
|
output_lines.append(line)
|
||||||
|
|
||||||
return "\n".join(output_lines)
|
result = "\n".join(output_lines)
|
||||||
|
logger.info(f"[build_readable_actions] 最终结果: '{result}'")
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
async def build_readable_messages_with_list(
|
async def build_readable_messages_with_list(
|
||||||
|
|||||||
Reference in New Issue
Block a user