From 36e046e843c7474c25b222b823409e62b2884ca8 Mon Sep 17 00:00:00 2001 From: dlkakbs Date: Sun, 5 Apr 2026 10:48:20 -0700 Subject: [PATCH] fix(gateway): MIME type fallback for Matrix document uploads Cherry-picked run.py portion from PR #3495 by dlkakbs. When Matrix sends non-image files (text, YAML, JSON, etc.), the MIME type may be empty or application/octet-stream. Falls back to extension-based detection so text files are properly injected into agent context. --- gateway/run.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gateway/run.py b/gateway/run.py index 070b77e18..f14713249 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -2650,8 +2650,20 @@ class GatewayRunner: # Enrich document messages with context notes for the agent # ----------------------------------------------------------------- if event.media_urls and event.message_type == MessageType.DOCUMENT: + import mimetypes as _mimetypes + _TEXT_EXTENSIONS = {".txt", ".md", ".csv", ".log", ".json", ".xml", ".yaml", ".yml", ".toml", ".ini", ".cfg"} for i, path in enumerate(event.media_urls): mtype = event.media_types[i] if i < len(event.media_types) else "" + # Fall back to extension-based detection when MIME type is unreliable. + if mtype in ("", "application/octet-stream"): + import os as _os2 + _ext = _os2.path.splitext(path)[1].lower() + if _ext in _TEXT_EXTENSIONS: + mtype = "text/plain" + else: + guessed, _ = _mimetypes.guess_type(path) + if guessed: + mtype = guessed if not (mtype.startswith("application/") or mtype.startswith("text/")): continue # Extract display filename by stripping the doc_{uuid12}_ prefix