from datetime import datetime from sqlalchemy import DateTime, Integer, String, Text from sqlalchemy.dialects.sqlite import JSON from sqlalchemy.orm import Mapped, mapped_column from core_db import AuditMixin, Base, TenantMixin, VersionMixin from core_shared import JSONValue class MemoryItem(TenantMixin, AuditMixin, VersionMixin, Base): __tablename__ = "memory_item" scope_type: Mapped[str] = mapped_column(String(32), index=True) scope_id: Mapped[str] = mapped_column(String(64), index=True) memory_type: Mapped[str] = mapped_column(String(64), default="fact", index=True) content_text: Mapped[str] = mapped_column(Text) content_json: Mapped[dict[str, JSONValue] | None] = mapped_column(JSON, nullable=True) metadata_json: Mapped[dict[str, JSONValue]] = mapped_column(JSON, default=dict) owner_agent_id: Mapped[str | None] = mapped_column(String(36), nullable=True, index=True) user_id: Mapped[str | None] = mapped_column(String(36), nullable=True, index=True) session_id: Mapped[str | None] = mapped_column(String(36), nullable=True, index=True) source_ref: Mapped[str | None] = mapped_column(String(256), nullable=True) importance_score: Mapped[int] = mapped_column(Integer, default=0) status: Mapped[str] = mapped_column(String(32), default="active", index=True) last_accessed_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) expires_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)