From d56981d38fdf67831fd68082506aab1d59d127e8 Mon Sep 17 00:00:00 2001 From: xxm Date: Fri, 21 Aug 2026 22:24:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(messenger):=20hook=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=B0=86=E8=84=9A=E6=9C=ACstdout=E5=86=99=E5=85=A5broadcast?= =?UTF-8?q?=E8=A1=A8(=E6=B6=88=E8=B4=B9=E8=80=85=E6=8E=A5=E7=AE=A1?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=BD=92=E6=A1=A3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/profile-scripts/messenger.py | 31 +++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/deploy/profile-scripts/messenger.py b/deploy/profile-scripts/messenger.py index 95101e89..26898803 100644 --- a/deploy/profile-scripts/messenger.py +++ b/deploy/profile-scripts/messenger.py @@ -133,18 +133,45 @@ class _MessengerStdout: self.orig = orig self.script = script self.channel = get_channel(script) - self._buf = "" def write(self, s): if self.channel in ("xmpp", "both"): + # xmpp 通道: 输出给 hermes(推xmpp) + 写入 broadcast 表 self.orig.write(s); self.orig.flush() + _write_broadcast_from_stdout(s, self.script) else: - self._buf += s + # broadcast 通道: 不输出 stdout(不推 xmpp), 只写入 broadcast 表 + _write_broadcast_from_stdout(s, self.script) return len(s) def flush(self): if self.channel in ("xmpp", "both"): self.orig.flush() +def _write_broadcast_from_stdout(text, script): + """把 stdout 内容写入 broadcast_messages(仅包含有意义的内容行)""" + if not text or not text.strip(): + return + # 过滤纯日志/进度(如 [DB]/[SYNC]/百分比), 只保留有信息量的行 + t = text.strip() + skip_prefix = ("[DB]", "[SYNC", "[guard", "[regime] ", "⏱", "📊 ", "📥 ", "💾 ", "🔄 ") + for line in t.split("\n"): + line = line.strip() + if not line or len(line) < 3: + continue + if line.startswith(skip_prefix): + continue + # 有意义的内容行 → 写广播表 + try: + send(title=f"MoFin·{_job_title(script)}", content=line, source=script) + except Exception: + pass + + +def _job_title(script): + return script.replace(".py", "") + + + def install_stdio_hook(script_name=None): """在脚本入口调用:重定向 stdout 到 messenger 通道控制""" if script_name is None: