Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.7 KB
Raw Blame
"""
Contract types for streamlit_apps.

Stable, framework-free frozen dataclasses that define what this product
exposes to the rest of the codebase. No Django imports.

These use ``pydantic.dataclasses.dataclass`` rather than the stdlib variant — same
syntax, same ``is_dataclass()`` compatibility (so ``DataclassSerializer`` keeps
working), but with runtime validation on construction. See
``products/visual_review/backend/facade/contracts.py`` for the pattern this mirrors.
"""

from __future__ import annotations

from dataclasses import field
from datetime import datetime
from uuid import UUID

from pydantic.dataclasses import dataclass


class AppRuntimeError(Exception):
    pass


class AppRuntimeConcurrencyError(AppRuntimeError):
    """Raised when a lifecycle action collides with one already in flight."""

    pass


class SourceEditError(Exception):
    """Raised when a source edit cannot be applied to the base version."""

    def __init__(self, message: str, path: str | None = None, edit_index: int | None = None) -> None:
        self.path = path
        self.edit_index = edit_index
        super().__init__(message)


@dataclass(frozen=True)
class StreamlitAppUserInfo:
    """Lightweight user info for display purposes. Mirrors core UserBasicSerializer."""

    id: int
    uuid: UUID
    distinct_id: str | None
    first_name: str
    last_name: str
    email: str
    is_email_verified: bool | None
    hedgehog_config: dict | None
    role_at_organization: str | None


@dataclass(frozen=True)
class AppVersionContract:
    id: UUID
    version_number: int
    zip_hash: str
    snapshot_id: str | None
    created_by: StreamlitAppUserInfo | None
    created_at: datetime


@dataclass(frozen=True)
class AppSandboxContract:
    status: str
    restart_count: int
    last_error: str
    started_at: datetime | None
    last_activity_at: datetime | None
    version_number: int | None


@dataclass(frozen=True)
class AppContract:
    id: UUID
    short_id: str
    name: str
    description: str
    cpu_cores: float
    memory_gb: float
    status: str
    active_version: AppVersionContract | None
    sandbox: AppSandboxContract | None
    created_by: StreamlitAppUserInfo | None
    created_at: datetime
    updated_at: datetime


@dataclass(frozen=True)
class ConnectInfoContract:
    url: str
    token: str
    expires_at: datetime


@dataclass(frozen=True)
class StreamlitConnectInfo:
    """Iframe connection info returned by the connect_info action."""

    iframe_url: str
    expires_in: int


@dataclass(frozen=True)
class CreateAppInput:
    name: str
    description: str = ""
    cpu_cores: float = 0.5
    memory_gb: float = 1


@dataclass(frozen=True)
class CreateVersionFromSourceInput:
    source: str
    files: dict[str, str] = field(default_factory=dict)
    assets: dict[str, str] = field(default_factory=dict)


@dataclass(frozen=True)
class AppSourceFileContract:
    path: str
    size: int
    sha256: str
    content_type: str
    is_binary: bool
    content: str | None


@dataclass(frozen=True)
class AppVersionSourceContract:
    version_number: int
    files: list[AppSourceFileContract]


@dataclass(frozen=True)
class SourceTextEdit:
    old: str
    new: str


@dataclass(frozen=True)
class SourceFileEdit:
    path: str
    edits: list[SourceTextEdit]


@dataclass(frozen=True)
class EditVersionSourceInput:
    base_version: int
    file_edits: list[SourceFileEdit] = field(default_factory=list)
    create_files: dict[str, str] = field(default_factory=dict)
    delete_files: list[str] = field(default_factory=list)


@dataclass(frozen=True)
class UpdateAppInput:
    name: str | None = None
    description: str | None = None
    cpu_cores: float | None = None
    memory_gb: float | None = None