From e934a3177854731f203ccb9636cdc3a7e3a64f7a Mon Sep 17 00:00:00 2001 From: operator Date: Sat, 3 Oct 2026 20:00:43 +0000 Subject: [PATCH] dm-listener: auto-route 646 Message commands --- bin/dm-listener.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100755 bin/dm-listener.py diff --git a/bin/dm-listener.py b/bin/dm-listener.py new file mode 100755 index 0000000..69a7d77 --- /dev/null +++ b/bin/dm-listener.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +""" +DM listener: Auto-routes 646's "Message :" requests + +Watches 646's main chat for "Message : " patterns, +routes via dm.py automatically. + +This makes 646's DM "work" — she says who, the system handles how. +""" +import subprocess +import time +import json +import re +from pathlib import Path + +DM_PY = "/home/super/Projects/NetVM/bin/dm.py" +NETVM_EXEC = "/home/super/Projects/NetVM/bin/netvm-exec.sh" +API = "/home/super/Projects/NetVM/bin/muse-chat-api.py" +WATERMARK_FILE = Path("/home/super/Projects/NetVM/bridge/dm-listener-watermark.json") +VALID_AGENTS = ["muse", "pip", "646"] + +def load_watermark(): + if WATERMARK_FILE.exists(): + with open(WATERMARK_FILE) as f: + return json.load(f) + return {"processed": []} + +def save_watermark(data): + WATERMARK_FILE.parent.mkdir(parents=True, exist_ok=True) + with open(WATERMARK_FILE, 'w') as f: + json.dump(data, f, indent=2) + +def run(cmd, timeout=60): + result = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=timeout) + return result.stdout.strip() + +def get_646_messages(): + return run(f"{NETVM_EXEC} 646 -- python3 {API} --account 646 messages 5") + +def poll_once(): + wm = load_watermark() + msgs = get_646_messages() + parts = [p.strip() for p in msgs.split('---') if p.strip()] + + for part in parts: + # Match "Message : " + m = re.match(r'Message\s+(\w+)\s*:\s*(.+)', part, re.DOTALL | re.IGNORECASE) + if m: + agent, text = m.groups() + agent = agent.lower() + text = text.strip() + + if agent not in VALID_AGENTS: + continue + + # Deduplicate + cmd_id = f"{agent}:{text[:40]}" + if cmd_id in wm["processed"]: + continue + + print(f"Routing: 646 -> {agent}: {text[:60]}...") + # Route via dm.py (sends to agent's main chat) + run(f"{DM_PY} send --agent {agent} --target main \"[646] {text[:800]}\"") + + wm["processed"].append(cmd_id) + # Keep only last 20 to avoid unbounded growth + wm["processed"] = wm["processed"][-20:] + save_watermark(wm) + print("Routed.") + +if __name__ == '__main__': + import sys + if '--once' in sys.argv: + poll_once() + elif '--daemon' in sys.argv: + print("DM listener running...") + while True: + try: + poll_once() + except Exception as e: + print(f"Error: {e}") + time.sleep(30) + else: + print("Use --once or --daemon")