Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,24 @@ COPY --chmod=0755 docker/cont-init.d/02-reconcile-profiles /etc/cont-init.d/02-r
# ---------- Runtime ----------
ENV HERMES_WEB_DIST=/opt/hermes/hermes_cli/web_dist
ENV HERMES_HOME=/opt/data
# s6-overlay v3 strips the container environment by default before
# execing the main program. The main-wrapper.sh CMD needs all K8s env
# vars (MATTERMOST_TOKEN, API keys, etc.) to survive into the hermes
# process. S6_KEEP_ENV=1 preserves them. See:
# https://github.com/just-containers/s6-overlay#customizing-s6-overlay-behaviour
ENV S6_KEEP_ENV=1
# Pre-s6 entrypoint.sh did `source .venv/bin/activate` which exported
# the venv bin onto PATH; Architecture B's main-wrapper.sh does the
# same for the container's main process, but `docker exec` and our
# cont-init.d scripts don't pass through the wrapper. Expose the venv
# bin globally so `docker exec <container> hermes ...` and any
# subprocess that doesn't activate the venv first still find hermes.
ENV PATH="/opt/hermes/.venv/bin:/opt/data/.local/bin:${PATH}"
# Login shells (bash -l) reset PATH from /etc/profile, dropping the venv.
# The terminal tool's init_session spawns bash -l to snapshot the env.
# Without this, python3 resolves to /usr/bin/python3 (no google deps, no pip).
RUN echo 'export PATH="/opt/hermes/.venv/bin:/opt/data/.local/bin:${PATH}"' \
> /etc/profile.d/hermes-venv.sh
RUN mkdir -p /opt/data
VOLUME [ "/opt/data" ]

Expand Down
24 changes: 20 additions & 4 deletions plugins/platforms/mattermost/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@ def __init__(self, config: PlatformConfig):
self._reconnect_task: Optional[asyncio.Task] = None
self._closing = False

# Reply mode: "thread" to nest replies, "off" for flat messages.
# Reply mode: "thread" to nest replies, "off" for flat messages, "auto" for smart (flat DMs, thread channels).
self._reply_mode: str = (
config.extra.get("reply_mode", "")
or os.getenv("MATTERMOST_REPLY_MODE", "off")
).lower()

# Dedup cache (prevent reprocessing)
self._dedup = MessageDeduplicator()
self._channel_type_cache: Dict[str, str] = {}

# ------------------------------------------------------------------
# HTTP helpers
Expand Down Expand Up @@ -250,6 +251,19 @@ async def disconnect(self) -> None:
logger.info("Mattermost: disconnected")


async def _should_thread(self, chat_id: str, reply_to: Optional[str]) -> bool:
if not reply_to:
return False
if self._reply_mode == "thread":
return True
if self._reply_mode == "auto":
ch_type = self._channel_type_cache.get(chat_id)
if not ch_type:
info = await self.get_chat_info(chat_id)
ch_type = info["type"]
return ch_type != "dm"
return False

async def _resolve_root_id(self, post_id: str) -> str:
"""Resolve a post_id to the thread root_id for Mattermost.

Expand Down Expand Up @@ -287,7 +301,7 @@ async def send(
"message": chunk,
}
# Thread support: reply_to is the root post ID.
if reply_to and self._reply_mode == "thread":
if await self._should_thread(chat_id, reply_to):
# Ensure root_id points to the thread root, not a reply.
# Mattermost rejects non-root post IDs as root_id.
resolved_root = await self._resolve_root_id(reply_to)
Expand All @@ -308,6 +322,8 @@ async def get_chat_info(self, chat_id: str) -> Dict[str, Any]:

ch_type = _CHANNEL_TYPE_MAP.get(data.get("type", "O"), "channel")
display_name = data.get("display_name") or data.get("name") or chat_id

self._channel_type_cache[chat_id] = ch_type
return {"name": display_name, "type": ch_type}

# ------------------------------------------------------------------
Expand Down Expand Up @@ -470,7 +486,7 @@ async def _send_url_as_file(
"message": caption or "",
"file_ids": [file_id],
}
if reply_to and self._reply_mode == "thread":
if await self._should_thread(chat_id, reply_to):
payload["root_id"] = await self._resolve_root_id(reply_to)

data = await self._api_post("posts", payload)
Expand Down Expand Up @@ -509,7 +525,7 @@ async def _send_local_file(
"message": caption or "",
"file_ids": [file_id],
}
if reply_to and self._reply_mode == "thread":
if await self._should_thread(chat_id, reply_to):
payload["root_id"] = await self._resolve_root_id(reply_to)

data = await self._api_post("posts", payload)
Expand Down