slash_commands_branch_thread.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """``/branch`` destination on thread-capable platforms (#66023).
  2. On Discord, Telegram, Slack and Matrix a plain ``/branch`` opens a NEW sibling thread for the
  3. clone and leaves the current chat bound to the original session, so the user can keep both paths
  4. alive. ``/branch --here`` keeps the legacy in-place switch; platforms without threads (and the CLI)
  5. always branch in place. Pure helpers only — the handler lives in ``slash_commands_session.py``.
  6. """
  7. from __future__ import annotations
  8. import dataclasses
  9. from typing import Optional
  10. from gateway.config import Platform
  11. from gateway.session import SessionSource
  12. # Adapters that override ``BasePlatformAdapter.create_handoff_thread`` (the base returns None).
  13. BRANCH_THREAD_PLATFORMS = frozenset({Platform.DISCORD, Platform.TELEGRAM, Platform.SLACK, Platform.MATRIX})
  14. BRANCH_HERE_FLAG = "--here"
  15. def parse_branch_args(raw: str) -> tuple[bool, str]:
  16. """``(stay_here, title)`` from the text after ``/branch``.
  17. Only a leading ``--here`` is a flag; everything else is the optional title. The CLI strips the
  18. flag through the same parser so ``/branch --here` never becomes a session titled ``--here``.
  19. """
  20. text = (raw or "").strip()
  21. head, _, rest = text.partition(" ")
  22. if head.lower() == BRANCH_HERE_FLAG:
  23. return True, rest.strip()
  24. return False, text
  25. def branch_thread_parent(source: SessionSource) -> Optional[str]:
  26. """Chat that can host a sibling thread for *source*, or None when nothing can (Discord DMs,
  27. a Discord thread whose parent channel is unknown)."""
  28. if source.platform not in BRANCH_THREAD_PLATFORMS:
  29. return None
  30. if source.platform == Platform.DISCORD:
  31. if source.chat_type == "dm":
  32. return None
  33. # Inbound Discord threads carry ``chat_id == thread_id``; the sibling goes under the real
  34. # text channel, which only ``parent_chat_id`` names.
  35. if source.thread_id or source.chat_type == "thread":
  36. return str(source.parent_chat_id) if source.parent_chat_id else None
  37. # Telegram forum topics, Slack threads and Matrix threads all key on the parent chat/room id.
  38. return str(source.chat_id) if source.chat_id else None
  39. def branch_dest_source(source: SessionSource, *, parent_id: str, thread_id: str, title: str) -> SessionSource:
  40. """The ``SessionSource`` a follow-up typed in the new thread will arrive on — the shape must
  41. match each adapter's inbound source or the clone is bound to a key nobody ever reads (the
  42. CLI→platform handoff in ``run_startup.py::_handoff_resolve_destination`` mirrors the same
  43. rules). Per-message fields are dropped; identity/scope fields travel with the copy."""
  44. common = dict(thread_id=str(thread_id), message_id=None, prospective_thread_id=None,
  45. auto_thread_created=False, auto_thread_initial_name=None)
  46. if source.platform == Platform.DISCORD:
  47. # Discord keys an in-thread message on the thread's OWN id as chat_id.
  48. return dataclasses.replace(source, chat_id=str(thread_id), chat_name=title or source.chat_name,
  49. chat_type="thread", parent_chat_id=str(parent_id), **common)
  50. # Telegram (``group:<chat>:<topic>``, private-chat topics stay ``dm``), Slack (parent channel's
  51. # dm/group + workspace scope) and Matrix (room type) key a thread reply on the PARENT chat's type.
  52. chat_type = "group" if source.chat_type == "thread" else source.chat_type
  53. return dataclasses.replace(source, chat_id=str(parent_id), chat_type=chat_type, **common)
  54. def format_thread_ref(platform: Optional[Platform], thread_id: str) -> str:
  55. """Clickable pointer where the platform has one (Discord ``<#id>`` mentions); id otherwise."""
  56. if platform == Platform.DISCORD:
  57. return f"<#{thread_id}>"
  58. return f"`{thread_id}`"