""" ContextSnippet model for storing reusable context snippets. Stores small, highly compressed pieces of information like technical decisions, configurations, patterns, and lessons learned for quick retrieval. """ from typing import TYPE_CHECKING, Optional from sqlalchemy import Float, ForeignKey, Index, Integer, String, Text from sqlalchemy.orm import Mapped, mapped_column, relationship from .base import Base, TimestampMixin, UUIDMixin if TYPE_CHECKING: from .client import Client from .project import Project class ContextSnippet(Base, UUIDMixin, TimestampMixin): """ ContextSnippet model for storing reusable context snippets. Stores small, highly compressed pieces of information like technical decisions, configurations, patterns, and lessons learned. These snippets are designed for quick retrieval and reuse across conversations. Attributes: category: Category of snippet (tech_decision, configuration, pattern, lesson_learned) title: Brief title describing the snippet dense_content: Highly compressed information content structured_data: JSON object for optional structured representation tags: JSON array of tags for retrieval and categorization project_id: Foreign key to projects (optional) client_id: Foreign key to clients (optional) relevance_score: Float score for ranking relevance (default 1.0) usage_count: Integer count of how many times this snippet was retrieved (default 0) project: Relationship to Project model client: Relationship to Client model """ __tablename__ = "context_snippets" # Foreign keys project_id: Mapped[Optional[str]] = mapped_column( String(36), ForeignKey("projects.id", ondelete="SET NULL"), doc="Foreign key to projects (optional)" ) client_id: Mapped[Optional[str]] = mapped_column( String(36), ForeignKey("clients.id", ondelete="SET NULL"), doc="Foreign key to clients (optional)" ) # Snippet metadata category: Mapped[str] = mapped_column( String(100), nullable=False, doc="Category: tech_decision, configuration, pattern, lesson_learned" ) title: Mapped[str] = mapped_column( String(200), nullable=False, doc="Brief title describing the snippet" ) # Content dense_content: Mapped[str] = mapped_column( Text, nullable=False, doc="Highly compressed information content" ) structured_data: Mapped[Optional[str]] = mapped_column( Text, doc="JSON object for optional structured representation" ) # Retrieval metadata tags: Mapped[Optional[str]] = mapped_column( Text, doc="JSON array of tags for retrieval and categorization" ) relevance_score: Mapped[float] = mapped_column( Float, default=1.0, server_default="1.0", doc="Float score for ranking relevance (default 1.0)" ) usage_count: Mapped[int] = mapped_column( Integer, default=0, server_default="0", doc="Integer count of how many times this snippet was retrieved" ) # Relationships project: Mapped[Optional["Project"]] = relationship( "Project", doc="Relationship to Project model" ) client: Mapped[Optional["Client"]] = relationship( "Client", doc="Relationship to Client model" ) # Indexes __table_args__ = ( Index("idx_context_snippets_project", "project_id"), Index("idx_context_snippets_client", "client_id"), Index("idx_context_snippets_category", "category"), Index("idx_context_snippets_relevance", "relevance_score"), Index("idx_context_snippets_usage", "usage_count"), ) def __repr__(self) -> str: """String representation of the context snippet.""" return f""