feat: 优化日报总结与延续性跟踪

This commit is contained in:
Benature
2026-11-08 23:45:44 +08:00
parent c8658ad064
commit e1d222a3fd
8 changed files with 1036 additions and 167 deletions

View File

@ -22,9 +22,12 @@ def _names(payload: dict[str, Any]) -> dict[str, str]:
return result
def _sender_names(client: Any, sender_ids: Iterable[str]) -> dict[str, str]:
def _sender_metadata(
client: Any, sender_ids: Iterable[str]
) -> tuple[dict[str, str], dict[str, int]]:
ids = sorted({str(user_id) for user_id in sender_ids if user_id})
names = {}
types = {}
for offset in range(0, len(ids), 50):
response = client.api(
"chatters.PullChattersByIdsRequest",
@ -33,8 +36,23 @@ def _sender_names(client: Any, sender_ids: Iterable[str]) -> dict[str, str]:
"notSubscribeChatters": True,
},
)
names.update(_names((response or {}).get("chatters") or {}))
return names
chatters = (response or {}).get("chatters") or {}
names.update(_names(chatters))
for key, person in chatters.items():
if not isinstance(person, dict):
continue
user_id = str(person.get("id") or key or "")
try:
sender_type = int(person.get("type") or 0)
except (TypeError, ValueError):
sender_type = 0
if user_id:
types[user_id] = sender_type
return names, types
def _sender_names(client: Any, sender_ids: Iterable[str]) -> dict[str, str]:
return _sender_metadata(client, sender_ids)[0]
def _chat_member_names(client: Any, chat_id: str) -> dict[str, str]:
@ -55,8 +73,10 @@ def _chat_member_names(client: Any, chat_id: str) -> dict[str, str]:
raise ContactsError("通讯录分页超过安全页数上限。")
def fetch_contacts(client: Any, rows: list[Any]) -> dict[str, dict[str, str]]:
"""Return full names per relevant chat, never the unrelated enterprise directory."""
def fetch_contacts(
client: Any, rows: list[Any], sender_types: dict[str, int] | None = None
) -> dict[str, dict[str, str]]:
"""Return scoped full names and optionally sender type metadata (1=human, 2=bot)."""
chats = {}
for row in rows:
chat_id = str(row.chat_id)
@ -65,7 +85,7 @@ def fetch_contacts(client: Any, rows: list[Any]) -> dict[str, dict[str, str]]:
chats[chat_id]["sender_ids"].add(str(row.sender_id))
try:
senders = _sender_names(
senders, types = _sender_metadata(
client,
{
sender_id
@ -73,6 +93,8 @@ def fetch_contacts(client: Any, rows: list[Any]) -> dict[str, dict[str, str]]:
for sender_id in chat["sender_ids"]
},
)
if sender_types is not None:
sender_types.update(types)
result = {}
for chat_id, chat in chats.items():
names = {