Files
aiagent/backend/app/models/persistent_user_memory.py

35 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
按 Agent/工作流 + 会话键持久化的用户记忆MySQL与 Redis 热缓存配合实现长期记忆。
"""
import uuid
from sqlalchemy import Column, DateTime, String, UniqueConstraint, func
from sqlalchemy.dialects.mysql import CHAR, JSON
from app.core.database import Base
class PersistentUserMemory(Base):
"""会话级记忆快照:同一 scope 下 session_key 唯一。"""
__tablename__ = "persistent_user_memories"
__table_args__ = (
UniqueConstraint("scope_kind", "scope_id", "session_key", name="uq_persistent_mem_scope_session"),
)
id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="主键")
scope_kind = Column(String(16), nullable=False, comment="agent 或 workflow")
scope_id = Column(CHAR(36), nullable=False, comment="Agent ID 或 Workflow ID")
session_key = Column(String(512), nullable=False, comment="调用方传入的 user_id 等会话键")
payload = Column(JSON, nullable=False, comment="与 Redis 中 user_memory_* 结构一致的记忆 JSON")
updated_at = Column(
DateTime,
server_default=func.now(),
onupdate=func.now(),
nullable=True,
comment="更新时间",
)
def __repr__(self):
return f"<PersistentUserMemory(scope={self.scope_kind}:{self.scope_id}, session={self.session_key[:32]})>"