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.

5.6 KB
Raw Blame
"""
Contract types for notebooks.

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, but with runtime validation on
construction so structural mistakes from mappers surface at the facade boundary
instead of producing a malformed payload further down the call stack.
"""

from dataclasses import field
from datetime import datetime
from typing import Any
from uuid import UUID

from pydantic.dataclasses import dataclass


class NotebookRunBusy(Exception):
    """Raised when the notebook already has a cell running.

    Surfaced as 409, not 429. It is a conflict with the notebook's state rather than a rate,
    and the MCP client rewrites every 429 into its own rate-limit error after retrying with
    backoff — so a 429 would cost an agent seconds of pointless waiting and then hide the one
    sentence telling it what to do.
    """


class TeamRunCapacityFull(Exception):
    """Raised when the project already has as many notebook cells in flight as it may.

    A rate rather than a state conflict, so this one is a 429: retrying later genuinely helps,
    which is exactly what the MCP client's backoff does.
    """


class NotebookCellLimitExceeded(Exception):
    """Raised when a write would grow a notebook past its cell ceiling.

    Lives here rather than beside the validator because it crosses the product boundary:
    Max writes notebooks through the facade, and its tool has to catch this to tell the
    model the notebook is full instead of retrying the save.
    """


class NotebookContentNotConvertible(Exception):
    """Raised when a create sends rich-text content that the markdown converter cannot read.

    Lives here because it crosses the product boundary: account notebooks are created through
    the facade, and that API has to turn this into a 400 on `content` instead of a 500.
    """


class NotebookMarkdownContentInvalid(Exception):
    pass


class NotebookContentEditNotAllowed(Exception):
    pass


@dataclass(frozen=True)
class NotebookMarkdownComponent:
    tag_name: str
    node_id: str
    props: dict[str, Any]


@dataclass(frozen=True)
class NotebookData:
    """A notebook's persisted state, as other products read it."""

    id: UUID
    short_id: str
    title: str | None
    # ProseMirror/TipTap content: usually a `{type: "doc", content: [...]}` dict, but some
    # notebooks (e.g. group/account templates) persist a bare list of nodes. JSONField allows
    # either, so the contract must too.
    content: dict[str, Any] | list[Any] | None
    text_content: str | None
    deleted: bool
    visibility: str
    version: int
    created_at: datetime
    last_modified_at: datetime
    created_by_id: int | None = None
    last_modified_by_id: int | None = None


@dataclass(frozen=True)
class NotebookRecent:
    """A single entry in a team's recently-modified notebooks list."""

    short_id: str
    title: str | None
    last_modified_at: datetime | None


@dataclass(frozen=True)
class NotebookActivitySummary:
    """Aggregate notebook activity for a team — total count plus the most recent few."""

    total_count: int
    recent: list[NotebookRecent] = field(default_factory=list)


@dataclass(frozen=True)
class NotebookUserInfo:
    """The creator/modifier of a notebook — raw user values, mirroring UserBasicSerializer's input."""

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


@dataclass(frozen=True)
class TeamAccountNote:
    """A row of a team-wide account-notes list: an internal notebook plus the account it's linked to."""

    short_id: str
    title: str | None
    created_at: datetime
    last_modified_at: datetime
    account_id: UUID
    account_name: str
    created_by: NotebookUserInfo | None = None


@dataclass(frozen=True)
class MarkdownNotebookMigrationStats:
    """Markdown migration status for notebooks in an optional team scope."""

    total: int
    converted: int
    pending: int
    team_id: int | None = None


@dataclass(frozen=True)
class MarkdownNotebookMigrationPreview:
    """A dry-run preview of one notebook conversion."""

    short_id: str
    title: str | None
    before_version: int
    markdown_preview: str


@dataclass(frozen=True)
class MarkdownNotebookMigrationResult:
    """Result of a markdown notebook migration run."""

    dry_run: bool
    team_id: int | None
    batch_size: int | None
    total: int
    already_converted: int
    pending_before: int
    pending_after: int
    converted: int
    skipped: int
    errored: int
    previews: list[MarkdownNotebookMigrationPreview] = field(default_factory=list)
    errors: list[str] = field(default_factory=list)


@dataclass(frozen=True)
class AccountNote:
    """An internal notebook linked to a customer-analytics account, for context rendering."""

    title: str | None
    short_id: str


@dataclass(frozen=True)
class AccountNotebook:
    """A full internal notebook linked to an account, with creator/modifier info.

    The richer counterpart to :class:`AccountNote` — backs the account-notebooks CRUD
    endpoints, where consumers render who created/edited each notebook.
    """

    id: UUID
    short_id: str
    title: str | None
    content: dict[str, Any] | list[Any] | None
    text_content: str | None
    created_at: datetime
    last_modified_at: datetime
    created_by: NotebookUserInfo | None = None
    last_modified_by: NotebookUserInfo | None = None