feat(memory): sync user entity aliases and metadata to PostgreSQL

- Add `aliases` and `end_user_id` fields to user entity dicts in
  `collect_user_entities_for_metadata` so downstream tasks can write
  them to PostgreSQL
- Add `update_aliases_and_metadata` method to `EndUserInfoRepository`
  for incremental, case-insensitive dedup merge of aliases and
  structured metadata fields
- Add `_sync_end_user_info_pg` helper in tasks.py that writes aliases
  and extracted metadata to `end_user_info`, and back-fills
  `end_user.other_name` when empty
- Call `_sync_end_user_info_pg` from `extract_metadata_batch_task`
  after Neo4j write, and also when no new metadata but aliases exist
- Filter `meta_data` response in `UserMemoryService.get_end_user_info`
  to expose only four core fields: goals, traits, interests, core_facts
This commit is contained in:
lanceyq
2026-05-07 18:07:32 +08:00
parent d255f33f1f
commit e3ab19dd4f
4 changed files with 159 additions and 16 deletions

View File

@@ -399,25 +399,17 @@ class UserMemoryService:
}
# 构建响应数据(转换时间为毫秒时间戳)
# meta_data 中的 profile、knowledge_tags、behavioral_hints 平铺到顶层
meta = end_user_info_record.meta_data or {}
# profile 列表字段截断:只返回前 MAX_PROFILE_LIST_SIZE 条(按时间从新到旧)
MAX_PROFILE_LIST_SIZE = 5
profile = meta.get("profile")
if isinstance(profile, dict):
for key in ("role", "domain", "expertise", "interests"):
if isinstance(profile.get(key), list):
profile[key] = profile[key][:MAX_PROFILE_LIST_SIZE]
# meta_data 只暴露四个核心字段
_META_FIELDS = ("goals", "traits", "interests", "core_facts")
raw_meta = end_user_info_record.meta_data or {}
filtered_meta = {k: raw_meta[k] for k in _META_FIELDS if k in raw_meta}
response_data = {
"end_user_info_id": str(end_user_info_record.id),
"end_user_id": str(end_user_info_record.end_user_id),
"other_name": end_user_info_record.other_name,
"aliases": end_user_info_record.aliases,
"profile": profile,
"knowledge_tags": meta.get("knowledge_tags"),
"behavioral_hints": meta.get("behavioral_hints"),
"meta_data": filtered_meta,
"created_at": datetime_to_timestamp(end_user_info_record.created_at),
"updated_at": datetime_to_timestamp(end_user_info_record.updated_at)
}