Files
MemoryBear/api/app/models/end_user_model.py
Ke Sun d9fa9039bb feat(memory): add memory config caching to end_user model
- Add memory_config_id field to EndUser model for lazy caching of memory configuration
- Create get_end_user_memory_config_id() function for fast retrieval of cached config ID
- Implement lazy update mechanism in get_end_user_connected_config() to cache memory_config_id
- Optimize memory config lookup by storing config ID directly on end_user record
- Improve import organization and formatting in memory_agent_service.py
- Add indexed foreign key relationship to data_config table for efficient queries
2026-01-29 12:05:50 +08:00

52 lines
2.7 KiB
Python

import datetime
import uuid
from app.db import Base
from sqlalchemy import BigInteger, Column, DateTime, ForeignKey, Integer, String, Text
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
class EndUser(Base):
__tablename__ = "end_users"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, nullable=False, index=True)
app_id = Column(UUID(as_uuid=True), ForeignKey("apps.id"), nullable=False)
# end_user_id = Column(String, nullable=False, index=True)
other_id = Column(String, nullable=True) # Store original user_id
other_name = Column(String, default="", nullable=False)
other_address = Column(String, default="", nullable=False)
reflection_time = Column(DateTime, nullable=True)
created_at = Column(DateTime, default=datetime.datetime.now)
updated_at = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)
# Memory config association - updated lazily during conversation
memory_config_id = Column(Integer, ForeignKey("data_config.config_id"), nullable=True, index=True, comment="关联的记忆配置ID")
# 用户基本信息字段
position = Column(String, nullable=True, comment="职位")
department = Column(String, nullable=True, comment="部门")
contact = Column(String, nullable=True, comment="联系方式")
phone = Column(String, nullable=True, comment="电话")
hire_date = Column(DateTime, nullable=True, comment="入职日期")
updatetime_profile = Column(DateTime, nullable=True, comment="核心档案信息最后更新时间")
# 用户摘要四个维度 - User Summary Four Dimensions
user_summary = Column(Text, nullable=True, comment="缓存的用户摘要(基本介绍)")
personality_traits = Column(Text, nullable=True, comment="性格特点")
core_values = Column(Text, nullable=True, comment="核心价值观")
one_sentence_summary = Column(Text, nullable=True, comment="一句话总结")
user_summary_updated_at = Column(DateTime, nullable=True, comment="用户摘要最后更新时间")
# 记忆洞察四个维度 - Memory Insight Four Dimensions
memory_insight = Column(Text, nullable=True, comment="缓存的记忆洞察报告(总体概述)")
behavior_pattern = Column(Text, nullable=True, comment="行为模式")
key_findings = Column(Text, nullable=True, comment="关键发现")
growth_trajectory = Column(Text, nullable=True, comment="成长轨迹")
memory_insight_updated_at = Column(DateTime, nullable=True, comment="洞察报告最后更新时间")
# 与 App 的反向关系
app = relationship(
"App",
back_populates="end_users"
)