chore/ 精简展示AI活动的日志
This commit is contained in:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user