Files
openworker/coworker/connectors/slack_addr.py
T
Rohit C PrasadandDevika 2b45018ffa OpenWorker: initial import
Imported from andrewyng/aisuite@1b4bbf303e
(contents of its platform/ directory, hoisted to the repo root).
Development history prior to this commit lives in that repository.

Co-authored-by: Devika <devikaverma11@gmail.com>
2026-07-21 11:09:41 -07:00

35 lines
1.3 KiB
Python

"""Slack team-qualified addressing for managed relay (slack-relay-spec §8/§9).
A single owner can be in several Slack workspaces at once, so a bare channel id
(`C…`) is ambiguous — a `U…`/`C…` only means something inside its `team_id`.
Managed-relay targets therefore carry the team: the reply handle's chat_id is
`"{team_id}/{channel}"`.
Encoding note: the reply-target grammar is colon-delimited
(`platform:chat_id[:thread]`, see base.parse_target), so we join team+channel
with `/` — colon-free — to stay inside that grammar unchanged. `slack:T012345/C0123`
is the wire form of the spec's conceptual `slack:T012345:C0123`. Manual
Socket-Mode targets (single workspace) keep the bare `slack:C0123` form.
"""
from __future__ import annotations
from typing import Optional
def qualify(team_id: Optional[str], channel: str) -> str:
"""Build a team-qualified chat_id, or the bare channel when no team."""
return f"{team_id}/{channel}" if team_id else channel
def split(chat_id: str) -> tuple[Optional[str], str]:
"""`'T…/C…' -> ('T…', 'C…')`; a bare `'C…' -> (None, 'C…')`.
Only the first `/` splits (channel ids never contain one), so this is
lossless both ways.
"""
if chat_id and "/" in chat_id:
team, _, channel = chat_id.partition("/")
return (team or None), channel
return None, chat_id