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
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import datetime
|
||||
import uuid
|
||||
from sqlalchemy import Column, String, DateTime, ForeignKey, Text, BigInteger
|
||||
|
||||
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
|
||||
from app.db import Base
|
||||
|
||||
|
||||
class EndUser(Base):
|
||||
__tablename__ = "end_users"
|
||||
@@ -18,6 +20,9 @@ class EndUser(Base):
|
||||
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="部门")
|
||||
|
||||
@@ -531,7 +531,9 @@ class MemoryAgentService:
|
||||
# 保存短期记忆到数据库
|
||||
# 只有 search_switch 不为 "2"(快速检索)时才保存
|
||||
try:
|
||||
from app.repositories.memory_short_repository import ShortTermMemoryRepository
|
||||
from app.repositories.memory_short_repository import (
|
||||
ShortTermMemoryRepository,
|
||||
)
|
||||
|
||||
retrieved_content = []
|
||||
repo = ShortTermMemoryRepository(db)
|
||||
@@ -1141,6 +1143,29 @@ class MemoryAgentService:
|
||||
logger.info("Log streaming completed, cleaning up resources")
|
||||
# LogStreamer uses context manager for file handling, so cleanup is automatic
|
||||
|
||||
|
||||
def get_end_user_memory_config_id(end_user_id: str, db: Session) -> Optional[int]:
|
||||
"""
|
||||
快速获取终端用户的 memory_config_id(直接从 end_user 表读取)
|
||||
|
||||
如果 end_user 已有缓存的 memory_config_id,直接返回;
|
||||
否则返回 None,调用方应使用 get_end_user_connected_config 获取完整配置。
|
||||
|
||||
Args:
|
||||
end_user_id: 终端用户ID
|
||||
db: 数据库会话
|
||||
|
||||
Returns:
|
||||
memory_config_id 或 None
|
||||
"""
|
||||
from app.models.end_user_model import EndUser
|
||||
|
||||
end_user = db.query(EndUser).filter(EndUser.id == end_user_id).first()
|
||||
if end_user and end_user.memory_config_id:
|
||||
return end_user.memory_config_id
|
||||
return None
|
||||
|
||||
|
||||
def get_end_user_connected_config(end_user_id: str, db: Session) -> Dict[str, Any]:
|
||||
"""
|
||||
获取终端用户关联的记忆配置
|
||||
@@ -1204,6 +1229,16 @@ def get_end_user_connected_config(end_user_id: str, db: Session) -> Dict[str, An
|
||||
memory_obj = config.get('memory', {})
|
||||
memory_config_id = memory_obj.get('memory_content') if isinstance(memory_obj, dict) else None
|
||||
|
||||
# 4. 更新 end_user 的 memory_config_id(懒更新)
|
||||
if memory_config_id is not None and end_user.memory_config_id != memory_config_id:
|
||||
try:
|
||||
end_user.memory_config_id = memory_config_id
|
||||
db.commit()
|
||||
logger.debug(f"Updated end_user memory_config_id: {end_user_id} -> {memory_config_id}")
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.warning(f"Failed to update end_user memory_config_id: {e}")
|
||||
|
||||
result = {
|
||||
"end_user_id": str(end_user_id),
|
||||
"app_id": str(app_id),
|
||||
|
||||
Reference in New Issue
Block a user