feat(profile):对用户关系和分析系统进行重构,采用结构化数据和异步更新

此提交完全重写了用户关系和分析系统,创建了一个更强大、详细和响应式的框架。旧系统已被弃用,取而代之的是一个集中式的`UserRelationships`模型。

主要变更:

1.  ‌**增强数据库模型(`UserRelationships`):**‌
    - 添加`impression_text`用于长期、叙述式印象。
    - 引入`key_facts`(JSON)存储结构化数据如生日、工作和位置。
    - 添加`relationship_stage`跟踪关系进展(如陌生人、朋友、挚友)。
    - 添加`first_met_time`和`last_impression_update`的时间戳。

2.  ‌**重设计`UserProfileTool`:**‌
    - 工具的用途被限定为仅捕捉重要新信息,防止用于小聊。
    - 更新现在在后台异步处理,确保机器人回复不被延迟。
    - 引入`key_info_type`和`key_info_value`参数供LLM提交结构化事实。

3.  ‌**复杂的印象和情感逻辑:**‌
    - 关系追踪LLM现在分析最近聊天历史生成更丰富、更上下文的印象。
    - 用渐进的`affection_change`(最大±0.03)取代直接情感分数设置,使关系发展更真实。

4.  ‌**数据源整合:**‌
    - `RelationshipFetcher`重构为仅依赖`UserRelationships`表作为唯一数据源。
    - 简化`get_user_relationship` API并移除其缓存,确保分析的实时数据访问。

破坏性变更:`UserProfileTool`已重设计,新增参数(`key_info_type`、`key_info_value`)并改变用途。移除`affection_score`参数。此外,`get_user_relationship`数据库API签名简化为仅接受`user_id`。
This commit is contained in:
tt-P607
2025-12-03 16:53:40 +08:00
parent 39c52490d9
commit 2671a6e7e5
9 changed files with 1233 additions and 202 deletions

View File

@@ -655,7 +655,16 @@ class UserPermissions(Base):
class UserRelationships(Base):
"""用户关系模型 - 存储用户与bot的关系数据"""
"""用户关系模型 - 存储用户与bot的关系数据
核心字段:
- relationship_text: 当前印象描述(用于兼容旧系统,逐步迁移到 impression_text
- impression_text: 长期印象(新字段,自然叙事风格)
- preference_keywords: 用户偏好关键词
- relationship_score: 好感度分数(0-1)
- key_facts: 关键信息JSON生日、职业、理想等
- relationship_stage: 关系阶段stranger/acquaintance/friend/close_friend/bestie
"""
__tablename__ = "user_relationships"
@@ -663,9 +672,22 @@ class UserRelationships(Base):
user_id: Mapped[str] = mapped_column(get_string_field(100), nullable=False, unique=True, index=True)
user_name: Mapped[str | None] = mapped_column(get_string_field(100), nullable=True)
user_aliases: Mapped[str | None] = mapped_column(Text, nullable=True) # 用户别名,逗号分隔
relationship_text: Mapped[str | None] = mapped_column(Text, nullable=True)
# 印象相关(新旧兼容)
relationship_text: Mapped[str | None] = mapped_column(Text, nullable=True) # 旧字段,保持兼容
impression_text: Mapped[str | None] = mapped_column(Text, nullable=True) # 新字段:长期印象(自然叙事)
# 用户信息
preference_keywords: Mapped[str | None] = mapped_column(Text, nullable=True) # 用户偏好关键词,逗号分隔
relationship_score: Mapped[float] = mapped_column(Float, nullable=False, default=0.3) # 关系分数(0-1)
key_facts: Mapped[str | None] = mapped_column(Text, nullable=True) # 关键信息JSON生日、职业等
# 关系状态
relationship_score: Mapped[float] = mapped_column(Float, nullable=False, default=0.3) # 好感度(0-1)
relationship_stage: Mapped[str | None] = mapped_column(get_string_field(50), nullable=True, default="stranger") # 关系阶段
# 时间记录
first_met_time: Mapped[float | None] = mapped_column(Float, nullable=True) # 首次认识时间戳
last_impression_update: Mapped[float | None] = mapped_column(Float, nullable=True) # 上次更新印象时间
last_updated: Mapped[float] = mapped_column(Float, nullable=False, default=time.time)
created_at: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False)
@@ -673,4 +695,5 @@ class UserRelationships(Base):
Index("idx_user_relationship_id", "user_id"),
Index("idx_relationship_score", "relationship_score"),
Index("idx_relationship_updated", "last_updated"),
Index("idx_relationship_stage", "relationship_stage"),
)