chore/ 精简展示AI活动的日志

This commit is contained in:
Kevin
2026-04-03 13:49:24 +08:00
parent 43d1689e9c
commit 4cfa3843a7
11 changed files with 194 additions and 24 deletions

View File

@@ -1,5 +1,6 @@
"""聊天 Agent 共享工具:历史获取、格式化、存储"""
import hashlib
from dataclasses import dataclass
from datetime import datetime
from typing import Any, List
@@ -68,12 +69,28 @@ async def get_history_messages(conversation_id: str) -> List[Any]:
return _lc_messages_from_rows(_human_ai_rows(history))
def format_history_string(messages: List[Any]) -> str:
def _sha12_utf8(text: str) -> str:
return hashlib.sha256((text or "").encode("utf-8")).hexdigest()[:12]
def format_history_string(
messages: List[Any], *, omit_system_body: bool = False
) -> str:
"""将 LangChain 消息列表格式化为调试日志用多段文本(含 System不静默跳过"""
history_parts: list[str] = []
for msg in messages:
if isinstance(msg, SystemMessage):
history_parts.append(f"System: {msg.content}")
if omit_system_body:
c = (
(msg.content or "")
if isinstance(msg.content, str)
else str(msg.content)
)
history_parts.append(
f"System: <omitted total_len={len(c)} sha12={_sha12_utf8(c)}>"
)
else:
history_parts.append(f"System: {msg.content}")
elif isinstance(msg, HumanMessage):
history_parts.append(f"Human: {msg.content}")
elif isinstance(msg, AIMessage):