feat(messenger): hook自动将脚本stdout写入broadcast表(消费者接管消息归档)
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user