28 lines
1.0 KiB
Python
28 lines
1.0 KiB
Python
# ============================================================
|
||
# 对话历史规范化(moduleId: core-context, 可重生 ✅)
|
||
# 只做纯数据整形:把网关收到的历史消息收敛成 {role, text}。
|
||
# 多轮语义理解统一交给 Pi Agent,本模块不做任何规则判断。
|
||
# ============================================================
|
||
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
HistoryItem = dict[str, str] # {role: user|agent, text: str}
|
||
|
||
|
||
def normalize_history(raw: list[Any] | None, *, limit: int = 12) -> list[HistoryItem]:
|
||
out: list[HistoryItem] = []
|
||
for m in raw or []:
|
||
if not isinstance(m, dict):
|
||
continue
|
||
role = str(m.get("role") or "")
|
||
if role in ("assistant", "bot", "ai"):
|
||
role = "agent"
|
||
if role not in ("user", "agent"):
|
||
continue
|
||
text = str(m.get("text") or m.get("content") or "").strip()
|
||
if not text:
|
||
continue
|
||
out.append({"role": role, "text": text})
|
||
return out[-limit:]
|