dm-listener: auto-route 646 Message commands

This commit is contained in:
operator
2026-10-03 20:00:43 +00:00
parent 9fde2232fd
commit e934a31778
+84
View File
@@ -0,0 +1,84 @@
#!/usr/bin/env python3
"""
DM listener: Auto-routes 646's "Message <agent>:" requests
Watches 646's main chat for "Message <agent>: <text>" 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 <agent>: <text>"
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")