Files
group_fqcd_jr/app/model/memory.py
T
张胜宇 e239eb778b docs: 品牌全量口径统一为「南方基金」+ 作废文档清理
1) 客服 Agent 四份交付文档 + 构建脚手架:品牌由包装占位 XX科技 / 旧名 南方财富
   统一为南方基金(热线 400-889-8899 / 官网 nffund.com),系统名改为「智能服务系统」;
   同步追加 §0.4 修订记录行,工程记录行保留原占位字面以支撑硬编码扫描验收。
2) 开发文档:清理 28 份已作废/残留文档(14 份移出归档 + 14 份仓库副本),
   新增《文档规整方案与开发前待决事项-2026-09-17》。
3) 客服agent 四份交付文档首次纳入本分支。
2026-09-17 15:15:22 +08:00

99 lines
5.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.
from datetime import datetime
from typing import Any
from sqlalchemy import JSON, BigInteger, DateTime, Integer, Numeric, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from app.model.base import Base
from app.model.profile import ProfileSnapshot
__all__ = [
"MemoryUnit", "MemoryEvidence", "MemorySyncOutbox", "MemoryConflict",
"ProfileSnapshot",
]
class MemoryUnit(Base):
"""中期记忆单元;字段与 docs/00 第 669-696 行基线一致。
生成列 `active_memory_key` 由数据库计算(仅 status='active' 时返回 memory_key),
ORM 不映射该列,唯一键 `(customer_id, active_memory_key)` 负责"每客户每键仅一条有效记忆"。
"""
__tablename__ = "memory_unit"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
memory_uuid: Mapped[str] = mapped_column(String(36), unique=True, nullable=False)
customer_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
memory_key: Mapped[str] = mapped_column(String(128), nullable=False)
content: Mapped[str] = mapped_column(Text, nullable=False)
structured_value: Mapped[dict[str, Any] | None] = mapped_column(JSON)
memory_type: Mapped[str] = mapped_column(String(24), nullable=False)
source_type: Mapped[str] = mapped_column(String(24), nullable=False)
source_confidence: Mapped[float] = mapped_column(Numeric(5, 4), nullable=False)
confidence: Mapped[float] = mapped_column(Numeric(5, 4), nullable=False)
evidence_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
conflict_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
recall_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
status: Mapped[str] = mapped_column(String(16), nullable=False)
valid_from: Mapped[datetime] = mapped_column(DateTime, nullable=False)
valid_until: Mapped[datetime | None] = mapped_column(DateTime)
last_evidenced_at: Mapped[datetime | None] = mapped_column(DateTime)
last_recall_at: Mapped[datetime | None] = mapped_column(DateTime)
promoted_at: Mapped[datetime | None] = mapped_column(DateTime)
version: Mapped[int] = mapped_column(BigInteger, nullable=False, default=1)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)
class MemoryEvidence(Base):
"""记忆证据;`idempotency_key` 唯一键是重复消费的幂等边界。"""
__tablename__ = "memory_evidence"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
memory_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
evidence_type: Mapped[str] = mapped_column(String(24), nullable=False)
source_table: Mapped[str | None] = mapped_column(String(64))
source_record_id: Mapped[str | None] = mapped_column(String(64))
source_episode_id: Mapped[int | None] = mapped_column(BigInteger)
evidence_excerpt: Mapped[str | None] = mapped_column(Text)
evidence_snapshot: Mapped[dict[str, Any] | None] = mapped_column(JSON)
weight: Mapped[float] = mapped_column(Numeric(5, 4), nullable=False)
idempotency_key: Mapped[str] = mapped_column(String(128), unique=True, nullable=False)
occurred_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)
class MemorySyncOutbox(Base):
__tablename__ = "memory_sync_outbox"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
event_uuid: Mapped[str] = mapped_column(String(36), nullable=False)
aggregate_type: Mapped[str] = mapped_column(String(24), nullable=False)
aggregate_uuid: Mapped[str] = mapped_column(String(36), nullable=False)
aggregate_version: Mapped[int] = mapped_column(BigInteger, nullable=False)
target_store: Mapped[str] = mapped_column(String(16), nullable=False)
operation: Mapped[str] = mapped_column(String(16), nullable=False)
payload: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False)
status: Mapped[str] = mapped_column(String(16), nullable=False)
retry_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0)
next_retry_at: Mapped[datetime | None] = mapped_column(DateTime)
last_error: Mapped[str | None] = mapped_column(Text)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)
processed_at: Mapped[datetime | None] = mapped_column(DateTime)
class MemoryConflict(Base):
"""记忆冲突;库列名为 `status`,`resolution`/`severity`/`resolved_by` 必须映射。"""
__tablename__ = "memory_conflict"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
left_memory_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
right_memory_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
conflict_type: Mapped[str] = mapped_column(String(24), nullable=False)
severity: Mapped[str] = mapped_column(String(8), nullable=False)
status: Mapped[str] = mapped_column(String(16), nullable=False)
resolution: Mapped[str | None] = mapped_column(Text)
winner_memory_id: Mapped[int | None] = mapped_column(BigInteger)
resolved_by: Mapped[int | None] = mapped_column(BigInteger)
resolved_at: Mapped[datetime | None] = mapped_column(DateTime)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)