Files
weval-l99/enterprise-model-sync.py

134 lines
4.8 KiB
Python
Executable File

#!/usr/bin/env python3
"""
enterprise-model-sync.py — Pipeline: Paperclip → enterprise-model.html
Source: Paperclip API (127.0.0.1:3100)
Target: /var/www/html/enterprise-model.html (AG array)
Runs on-demand or via cron. Maps Paperclip agents to enterprise-model rooms.
Preserves existing agent properties (colors, actions, deliverables).
Only ADDS new agents from Paperclip, never removes existing ones.
"""
import json, re, subprocess, urllib.request, sys, os
from datetime import datetime
PAPERCLIP_URL = "http://127.0.0.1:3100/api/companies/dd12987b-c774-45e7-95fd-d34003f91650/agents"
EM_FILE = "/var/www/html/enterprise-model.html"
WIKI_API = "https://weval-consulting.com/api/l99-wiki.php"
# Room mapping: Paperclip role → enterprise-model room
ROLE_TO_ROOM = {
'orchestrator': 'ops', 'general': 'sal', 'wevia': 'wevia', 'dev': 'dev',
'qa': 'qa', 'security': 'sec', 'devops': 'srv', 'data': 'ai',
'research': 'con', 'marketing': 'sal', 'sales': 'sal', 'admin': 'ceo',
'scraping': 'cron', 'monitoring': 'ops', 'testing': 'qa', 'infra': 'srv',
'email': 'mta', 'ethica': 'pha', 'saas': 'saas', 'integration': 'intg',
'docker': 'dock', 'platform': 'plat', 'wire': 'wire', 'dead': 'dead',
}
def log(msg):
print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}")
def get_paperclip_agents():
try:
req = urllib.request.Request(PAPERCLIP_URL, headers={"Accept": "application/json"})
with urllib.request.urlopen(req, timeout=10) as r:
return json.loads(r.read())
except Exception as e:
log(f"ERROR fetching Paperclip: {e}")
return []
def get_existing_names(content):
return set(re.findall(r"n:'([^']+)'", content))
def map_role(role):
return ROLE_TO_ROOM.get(role, 'ops')
def build_agent_entry(agent):
name = (agent.get('name') or '?').replace("'", "")[:20]
role = agent.get('role', 'general')
rm = map_role(role)
desc = (agent.get('description') or role)[:15].replace("'", "")
return f"{{n:'{name}',rm:'{rm}',d:'{desc}',p:'Paperclip',sk:'#b0b0b0',hc:'#666',F:0,re:'🤖',act:['Sync','Execute','Report','Monitor'],si:'sit'}}"
def main():
log("Enterprise-Model Sync Pipeline v1.0")
# 1. Fetch from Paperclip
pc_agents = get_paperclip_agents()
log(f"Paperclip: {len(pc_agents)} agents")
if not pc_agents:
log("No agents from Paperclip, aborting")
return
# 2. Read current enterprise-model.html
subprocess.run(["sudo", "chattr", "-i", EM_FILE], capture_output=True)
with open(EM_FILE, "r") as f:
content = f.read()
existing = get_existing_names(content)
log(f"Existing in HTML: {len(existing)} agents")
# 3. Find new agents
new_agents = []
for a in pc_agents:
name = (a.get('name') or '?').replace("'", "")[:20]
if name not in existing and name != '?':
new_agents.append(a)
log(f"New agents to add: {len(new_agents)}")
if not new_agents:
log("No new agents. Already synced.")
subprocess.run(["sudo", "chattr", "+i", EM_FILE], capture_output=True)
return
# 4. Build injection entries
entries = [build_agent_entry(a) for a in new_agents]
injection = ",\n".join(entries)
# 5. Find injection point (before ];...AG=AG.filter)
guard = "AG=AG.filter(function(a){return a&&a.n;});"
guard_pos = content.find(guard)
if guard_pos < 0:
log("ERROR: null guard not found")
subprocess.run(["sudo", "chattr", "+i", EM_FILE], capture_output=True)
return
area = content[:guard_pos]
bracket = area.rfind("];")
if bracket < 0:
log("ERROR: ]; not found")
subprocess.run(["sudo", "chattr", "+i", EM_FILE], capture_output=True)
return
# 6. Inject
new_content = content[:bracket] + ",\n" + injection + "\n" + content[bracket:]
# 7. Validate
old_count = len(re.findall(r"\{n:", content))
new_count = len(re.findall(r"\{n:", new_content))
log(f"Agents: {old_count} -> {new_count} (+{new_count - old_count})")
# 8. Write
with open(EM_FILE, "w") as f:
f.write(new_content)
subprocess.run(["sudo", "chattr", "+i", EM_FILE], capture_output=True)
log(f"DONE: {new_count} agents in enterprise-model.html")
# 9. Git commit - ALERT-ONLY MODE since 2026-04-24
# DISABLED-OPUS-20260424: os.system git commit replaced by alert log
from datetime import datetime as _dt
alert = "[%s] enterprise-model-sync WOULD HAVE COMMITTED +%d agents (alert-only mode)" % (_dt.now().isoformat(), len(new_agents))
log(alert)
try:
with open("/var/log/weval-alerts.log","a") as af:
af.write(alert+"\n")
except Exception as e:
log(" alert-log err: %s" % e)
log("Pipeline complete")
if __name__ == "__main__":
main()