Mercurial
comparison love/poppy/db/models.py @ 38:cf9caa4abc3e
[Love] FE and BE. Can chat and render images. Also created MCP for powerpoint generations.
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Mon, 01 Dec 2025 20:35:56 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 37:fb9bcd3145cb | 38:cf9caa4abc3e |
|---|---|
| 1 from sqlmodel import SQLModel, Field, Relationship | |
| 2 from typing import Optional, List | |
| 3 from datetime import datetime | |
| 4 import uuid | |
| 5 from sqlalchemy import text | |
| 6 | |
| 7 | |
| 8 class MessageAsset(SQLModel, table=True): | |
| 9 id: uuid.UUID = Field( | |
| 10 default_factory=uuid.uuid4, | |
| 11 primary_key=True, | |
| 12 index=True, | |
| 13 nullable=False, | |
| 14 sa_column_kwargs={"server_default": text("gen_random_uuid()")}, | |
| 15 ) | |
| 16 message_id: uuid.UUID = Field(foreign_key="message.id", index=True) | |
| 17 asset_type: str = Field(index=True) # "image" | "file" | "audio" | "video" | etc. | |
| 18 url: str = Field() # public or signed URL | |
| 19 # Pretty good suggestions but not needed for now. | |
| 20 # filename: Optional[str] = Field(default=None) | |
| 21 # mime_type: Optional[str] = Field(default=None) | |
| 22 # width: Optional[int] = Field(default=None) # for images/videos | |
| 23 # height: Optional[int] = Field(default=None) | |
| 24 # size_bytes: Optional[int] = Field(default=None) | |
| 25 # alt_text: Optional[str] = Field(default=None) # user-provided description | |
| 26 created_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) | |
| 27 | |
| 28 # Relationship back to message | |
| 29 message: "Message" = Relationship(back_populates="assets") | |
| 30 | |
| 31 class Message(SQLModel, table=True): | |
| 32 id: uuid.UUID = Field( | |
| 33 default_factory=uuid.uuid4, | |
| 34 primary_key=True, | |
| 35 index=True, | |
| 36 nullable=False, | |
| 37 sa_column_kwargs={"server_default": text("gen_random_uuid()")}, | |
| 38 ) | |
| 39 chat_id: uuid.UUID = Field(foreign_key="chat.id") | |
| 40 | |
| 41 role: str = Field( | |
| 42 index=True | |
| 43 ) # "user" | "assistant" TODO: use enum? I don't mind just defining like this tho. | |
| 44 content: str | |
| 45 created_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) | |
| 46 | |
| 47 # Relationship | |
| 48 chat: Optional["Chat"] = Relationship(back_populates="messages") | |
| 49 assets: List[MessageAsset] = Relationship(back_populates="message") | |
| 50 | |
| 51 class Chat(SQLModel, table=True): | |
| 52 id: uuid.UUID = Field( | |
| 53 default_factory=uuid.uuid4, | |
| 54 primary_key=True, | |
| 55 index=True, | |
| 56 nullable=False, | |
| 57 sa_column_kwargs={"server_default": text("gen_random_uuid()")}, | |
| 58 ) | |
| 59 | |
| 60 title: str = Field(default="New Chat") | |
| 61 created_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) | |
| 62 | |
| 63 # Relationship | |
| 64 messages: List[Message] = Relationship( | |
| 65 back_populates="chat", sa_relationship_kwargs={"cascade": "all, delete-orphan"} | |
| 66 ) |