@dataclass
class WorkspaceState:
"""Live workspace projection for the Telegram-style thread sidebar."""
projects: dict[str, ProjectShell] = field(default_factory=dict)
threads: dict[str, ThreadShell] = field(default_factory=dict)
archived_threads: dict[str, ThreadShell] = field(default_factory=dict)
active_thread_id: str | None = None
unread_ids: set[str] = field(default_factory=set)
sequence: int = 0
synchronized: bool = False
def select_thread(self, thread_id: str | None) -> None:
"""Set the currently open thread and clear its unread badge."""
self.active_thread_id = thread_id
if thread_id is not None:
self.unread_ids.discard(thread_id)
def project_title(self, project_id: str) -> str:
"""Human-readable project name for `project_id`."""
project = self.projects.get(project_id)
return project.title if project is not None else project_id
def apply_shell(self, item: ShellStreamItem) -> ShellChange:
"""Apply one `subscribeShell` stream item to workspace state."""
result = ShellChange(changed=False)
match item:
case ShellSnapshot():
snap = item.snapshot
self.sequence = snap.snapshot_sequence
self.projects = {p.id: p for p in snap.projects if p.archived_at is None}
self.threads = {t.id: t for t in snap.threads if t.deleted_at is None}
self.archived_threads = {
t.id: t for t in snap.archived_threads if t.deleted_at is None
}
result = ShellChange(changed=True)
case ShellProjectUpdated():
self.sequence = max(self.sequence, item.sequence)
if item.project.archived_at is None:
self.projects[item.project.id] = item.project
else:
self.projects.pop(item.project.id, None)
result = ShellChange(changed=True)
case ShellProjectRemoved():
self.sequence = max(self.sequence, item.sequence)
self.projects.pop(item.project_id, None)
result = ShellChange(changed=True)
case ShellThreadUpdated():
result = self._apply_thread_updated(item)
case ShellThreadRemoved():
self.sequence = max(self.sequence, item.sequence)
target = self.threads if item.location == "active" else self.archived_threads
target.pop(item.thread_id, None)
self.unread_ids.discard(item.thread_id)
result = ShellChange(changed=True)
case ShellSynchronized():
self.synchronized = True
case ShellSnapshotRequired():
result = ShellChange(changed=False, needs_resubscribe=True)
case _:
pass
return result
def _apply_thread_updated(self, item: ShellThreadUpdated) -> ShellChange:
self.sequence = max(self.sequence, item.sequence)
thread = item.thread
if item.location == "archive":
self.threads.pop(thread.id, None)
self.archived_threads[thread.id] = thread
self.unread_ids.discard(thread.id)
return ShellChange(changed=True)
previous = self.threads.get(thread.id)
self.archived_threads.pop(thread.id, None)
if thread.deleted_at is not None:
self.threads.pop(thread.id, None)
self.unread_ids.discard(thread.id)
return ShellChange(changed=True)
self.threads[thread.id] = thread
reason = _detect_attention(previous, thread)
if reason is not None and thread.id != self.active_thread_id:
self.unread_ids.add(thread.id)
return ShellChange(changed=True, attention_thread_id=thread.id, attention_reason=reason)
return ShellChange(changed=True)
def sorted_threads(
self, filter_text: str = "", *, project_id: str | None = None
) -> list[ThreadShell]:
"""Return active threads in Telegram-like priority order.
Waiting threads and pinned threads bubble to the top, followed by running
threads, then most recently updated threads.
"""
query = filter_text.strip().lower()
rows: list[ThreadShell] = []
for thread in self.threads.values():
if project_id is not None and thread.project_id != project_id:
continue
if query:
proj = self.project_title(thread.project_id).lower()
preview = (
thread.latest_visible_message.text.lower()
if thread.latest_visible_message and thread.latest_visible_message.text
else ""
)
if query not in thread.title.lower() and query not in proj and query not in preview:
continue
rows.append(thread)
def sort_key(t: ThreadShell) -> tuple[int, int, int, str]:
waiting = 0 if thread_needs_attention(t) else 1
pinned = 0 if t.pinned_at else 1
running = 0 if thread_is_running(t) else 1
# Reverse lexicographical ISO timestamp via negative ordinal isn't needed if we
# sort in two passes; instead we sort descending by timestamp first.
return (waiting, pinned, running, t.id)
rows.sort(key=lambda t: t.updated_at or t.created_at or "", reverse=True)
rows.sort(key=sort_key)
return rows