view love/poppy/apis/schemas.py @ 71:75de5903355c

Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
author June Park <parkjune1995@gmail.com>
date Sun, 28 Dec 2025 20:34:22 -0800
parents cf9caa4abc3e
children
line wrap: on
line source

from uuid import UUID
from pydantic import BaseModel, ConfigDict, field_serializer
from datetime import datetime
from typing import Literal, Optional


class MessageBase(BaseModel):
    role: Literal["user", "assistant"]
    content: str

    model_config = ConfigDict(from_attributes=True)


class MessageCreate(MessageBase):
    pass


class MessageOut(MessageBase):
    id: UUID
    image_url: Optional[str] = None
    created_at: datetime

    @field_serializer("id")
    def serialize_id(self, id: UUID):
        return str(id)

    # Automatically populate image_url / image_urls from relationship
    @classmethod
    def from_orm_with_assets(cls, obj):
        data = cls.model_validate(obj)

        image_assets = [
            a for a in getattr(obj, "assets", [])
            if a.asset_type == "image"
        ]

        image_urls = [a.url for a in image_assets if a.url]
        data.image_url = image_urls[0] if image_urls else None

        return data


class ChatOut(BaseModel):
    id: UUID
    title: str
    created_at: datetime

    model_config = ConfigDict(from_attributes=True)

    @field_serializer("id")
    def serialize_id(self, id: UUID):
        return str(id)


class ChatWithMessages(ChatOut):
    messages: list[MessageOut] = []

class Chats(BaseModel):
    chats: list[ChatOut] = []