Files
life-echo/api/app/features/conversation/ws/profile_collector.py
Sully 53e0065e3e refactor(api): TOML 配置 SSOT、统一错误契约、Auth/事务加固与可观测性 (#33)
配置 SSOT(TOML + .env)
统一错误契约
Auth 与事务边界
Redis / Celery 可靠性:业务 Redis(DB/0)与 Celery broker/backend(DB/1)显式拆分;连接池、sync client
可观测性(OpenTelemetry + LGTM)
2026-05-22 13:44:50 +08:00

59 lines
2.1 KiB
Python

"""用户资料收集:缺失字段检测、提取与应用"""
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.db import transactional
from app.features.user.models import User
def get_missing_profile_fields(user: User) -> list:
"""检查用户缺失的资料字段"""
from app.agents.chat.prompts_profile import (
get_missing_profile_fields as _get_missing,
)
return _get_missing(
birth_year=user.birth_year,
birth_place=user.birth_place,
grew_up_place=user.grew_up_place,
occupation=user.occupation,
)
def get_filled_profile_fields(user: User) -> dict:
"""获取用户已有的资料字段(中文展示)"""
filled = {}
if user.birth_year:
filled["birth_year"] = str(user.birth_year)
if user.birth_place:
filled["birth_place"] = user.birth_place
if user.grew_up_place:
filled["grew_up_place"] = user.grew_up_place
if user.occupation:
filled["occupation"] = user.occupation
return filled
async def apply_extracted_profile(user: User, extracted: dict, db: AsyncSession):
"""将提取到的资料信息保存到用户模型"""
changed = False
if "birth_year" in extracted and not user.birth_year:
changed = True
if "birth_place" in extracted and not user.birth_place:
changed = True
if "grew_up_place" in extracted and not user.grew_up_place:
changed = True
if "occupation" in extracted and not user.occupation:
changed = True
if changed:
async with transactional(db):
if "birth_year" in extracted and not user.birth_year:
user.birth_year = extracted["birth_year"]
if "birth_place" in extracted and not user.birth_place:
user.birth_place = extracted["birth_place"]
if "grew_up_place" in extracted and not user.grew_up_place:
user.grew_up_place = extracted["grew_up_place"]
if "occupation" in extracted and not user.occupation:
user.occupation = extracted["occupation"]
await db.refresh(user)