|
| 1 | +"""Composer (conversation) and Bubble (message) typed models.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass, field |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from models.errors import SchemaError |
| 9 | + |
| 10 | + |
| 11 | +@dataclass(frozen=True) |
| 12 | +class Composer: |
| 13 | + """A Cursor conversation (a.k.a. "composer") row. |
| 14 | +
|
| 15 | + Required fields per the schema-validation contract: |
| 16 | + - ``fullConversationHeadersOnly`` — without this, a composer cannot be |
| 17 | + rendered (no message order is recoverable). This is the only hard |
| 18 | + requirement: real Cursor data legitimately omits ``createdAt`` for |
| 19 | + older composers (the existing call sites already fall back to |
| 20 | + ``lastUpdatedAt`` and then to epoch zero), so it is captured but |
| 21 | + not gated on. |
| 22 | +
|
| 23 | + The composer ID is intentionally passed in as a constructor argument |
| 24 | + rather than read from ``raw`` because Cursor stores it in the row key |
| 25 | + (``composerData:<id>``) rather than in the JSON value. |
| 26 | + """ |
| 27 | + |
| 28 | + composer_id: str |
| 29 | + full_conversation_headers_only: list[dict[str, Any]] |
| 30 | + created_at: Any |
| 31 | + name: str | None = None |
| 32 | + last_updated_at: Any = None |
| 33 | + model_config: dict[str, Any] = field(default_factory=dict) |
| 34 | + raw: dict[str, Any] = field(default_factory=dict) |
| 35 | + |
| 36 | + @classmethod |
| 37 | + def from_dict(cls, raw: dict[str, Any], *, composer_id: str) -> "Composer": |
| 38 | + if not composer_id: |
| 39 | + raise SchemaError("Composer", "composerId", hint="empty composer ID") |
| 40 | + if "fullConversationHeadersOnly" not in raw: |
| 41 | + raise SchemaError("Composer", "fullConversationHeadersOnly") |
| 42 | + |
| 43 | + headers = raw.get("fullConversationHeadersOnly") or [] |
| 44 | + if not isinstance(headers, list): |
| 45 | + raise SchemaError( |
| 46 | + "Composer", |
| 47 | + "fullConversationHeadersOnly", |
| 48 | + hint=f"expected list, got {type(headers).__name__}", |
| 49 | + ) |
| 50 | + |
| 51 | + model_config = raw.get("modelConfig") or {} |
| 52 | + if not isinstance(model_config, dict): |
| 53 | + model_config = {} |
| 54 | + |
| 55 | + return cls( |
| 56 | + composer_id=composer_id, |
| 57 | + full_conversation_headers_only=headers, |
| 58 | + created_at=raw.get("createdAt"), |
| 59 | + name=raw.get("name"), |
| 60 | + last_updated_at=raw.get("lastUpdatedAt"), |
| 61 | + model_config=model_config, |
| 62 | + raw=raw, |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +@dataclass(frozen=True) |
| 67 | +class Bubble: |
| 68 | + """A single message bubble within a composer. |
| 69 | +
|
| 70 | + The bubble ID lives in the row key (``bubbleId:<composer_id>:<bubble_id>``) |
| 71 | + rather than the JSON value, so it is passed in explicitly. The raw dict |
| 72 | + is preserved to keep downstream rendering code (which still walks the |
| 73 | + untyped shape) working without modification. |
| 74 | + """ |
| 75 | + |
| 76 | + bubble_id: str |
| 77 | + raw: dict[str, Any] = field(default_factory=dict) |
| 78 | + |
| 79 | + @classmethod |
| 80 | + def from_dict(cls, raw: dict[str, Any], *, bubble_id: str) -> "Bubble": |
| 81 | + if not bubble_id: |
| 82 | + raise SchemaError("Bubble", "bubbleId", hint="empty bubble ID") |
| 83 | + return cls(bubble_id=bubble_id, raw=raw) |
0 commit comments