← back to Designer Wallcoverings

DW-Agents/dw-agents/integrate-chat.py

123 lines

#!/usr/bin/env python3
"""
Quick chat integration script - adds chat widget to all agents
"""

import os
import re
from pathlib import Path

# Agent files mapping: file_path -> (agent_id, agent_name, port)
AGENTS = {
    '/root/DW-Agents/agent-accounting/accounting-agent.ts': ('agent-accounting', 'Accounting', 9882),
    '/root/DW-Agents/agent-completed-tasks/completed-tasks-agent.ts': ('agent-completed', 'Completed Tasks', 9889),
    '/root/DW-Agents/agent-digital-samples/digital-samples-agent.ts': ('agent-digital-samples', 'Digital Samples', 9879),
    '/root/DW-Agents/agent-executive-ceo/ceo-dashboard-agent.ts': ('agent-ceo', 'CEO Dashboard', 7121),
    '/root/DW-Agents/agent-executive-cfo/cfo-dashboard-agent.ts': ('agent-cfo', 'CFO Dashboard', 7122),
    '/root/DW-Agents/agent-executive-coo/coo-dashboard-agent.ts': ('agent-coo', 'COO Dashboard', 7124),
    '/root/DW-Agents/agent-executive-vp-ops/vp-ops-dashboard-agent.ts': ('agent-vp-ops', 'VP Operations', 7123),
    '/root/DW-Agents/agent-in-parallel/in-parallel-agent.ts': ('agent-in-parallel', 'In Parallel', 9891),
    '/root/DW-Agents/agent-legal-team/legal-agent.ts': ('agent-legal', 'Legal Team', 9878),
    '/root/DW-Agents/agent-log-monitor/log-monitor-agent.ts': ('agent-logs', 'Log Monitor', 7239),
    '/root/DW-Agents/agent-marketing/marketing-agent.ts': ('agent-marketing', 'Marketing', 9881),
    '/root/DW-Agents/agent-needs-attention/needs-attention-agent.ts': ('agent-needs-attention', 'Needs Attention', 9886),
    '/root/DW-Agents/agent-new-client-signup/new-client-signup-agent.ts': ('agent-signups', 'New Client Signups', 9890),
    '/root/DW-Agents/agent-parallel-processes/parallel-processes-agent.ts': ('agent-parallel', 'Parallel Processes', 9887),
    '/root/DW-Agents/agent-purchasing-office/purchasing-office-agent.ts': ('agent-purchasing', 'Purchasing Office', 9880),
    '/root/DW-Agents/agent-restart-analyzer/restart-analyzer-agent.ts': ('agent-restart', 'Restart Analyzer', 7241),
    '/root/DW-Agents/agent-security/security-agent.ts': ('agent-security', 'Security Agent', 9892),
    '/root/DW-Agents/agent-server-uptime/server-uptime-agent.ts': ('agent-uptime', 'Server Uptime', 9888),
    '/root/DW-Agents/agent-shopify-store/shopify-store-agent.ts': ('agent-shopify', 'Shopify Store', 7238),
    '/root/DW-Agents/agent-skills-manager/skills-manager-agent.ts': ('agent-skills', 'Skills Manager', 9894),
    '/root/DW-Agents/agent-task-orchestrator/task-orchestrator-agent.ts': ('agent-orchestrator', 'Task Orchestrator', 9900),
    '/root/DW-Agents/agent-todays-highlights/todays-highlights-agent.ts': ('agent-highlights', 'Todays Highlights', 9885),
    '/root/DW-Agents/agent-trend-research/trend-research-agent.ts': ('agent-trend', 'Trend Research', 9883),
    '/root/DW-Agents/agent-ui-manager/ui-manager-agent.ts': ('agent-ui', 'UI Manager', 7240),
    '/root/DW-Agents/agent-zendesk-chat/zendesk-chat-agent.ts': ('agent-zendesk', 'Zendesk Chat', 9884),
    '/root/DW-Agents/master-hub.ts': ('master-hub', 'Master Hub', 9893),
}

def add_chat_to_file(file_path, agent_id, agent_name, port):
    """Add chat integration to a TypeScript file"""

    if not os.path.exists(file_path):
        print(f"❌ File not found: {file_path}")
        return False

    with open(file_path, 'r') as f:
        content = f.read()

    # Check if already integrated
    if 'chatMiddleware' in content:
        print(f"✅ {agent_name} - Already has chat")
        return True

    # Add import after other imports
    import_line = "import { chatMiddleware } from '../shared-chat-integration';\n"

    # Find the last import statement
    import_pattern = r'(import .+ from .+;)\n'
    imports = list(re.finditer(import_pattern, content))

    if imports:
        last_import = imports[-1]
        insert_pos = last_import.end()
        content = content[:insert_pos] + "\n" + import_line + content[insert_pos:]
    else:
        # No imports found, add at the beginning
        content = import_line + "\n" + content

    # Add middleware after app.use(express.static(...))
    middleware_code = f"""
// Add inter-agent chat widget
app.use(chatMiddleware({{
  agentId: '{agent_id}',
  agentName: '{agent_name}',
  port: {port},
  category: 'agent'
}}));
"""

    # Find app.use(express.static and add after it
    static_pattern = r"(app\.use\(express\.static\([^)]+\)\);)"
    match = re.search(static_pattern, content)

    if match:
        insert_pos = match.end()
        content = content[:insert_pos] + "\n" + middleware_code + content[insert_pos:]
    else:
        # Try to find app.use(cookieParser())
        cookie_pattern = r"(app\.use\(cookieParser\(\)\);)"
        match = re.search(cookie_pattern, content)
        if match:
            insert_pos = match.end()
            content = content[:insert_pos] + "\n" + middleware_code + content[insert_pos:]
        else:
            print(f"⚠️  {agent_name} - Could not find insertion point")
            return False

    # Write back
    with open(file_path, 'w') as f:
        f.write(content)

    print(f"✅ {agent_name} - Chat integration added")
    return True

def main():
    print("🤖 DW-Agents Chat Integration")
    print("━" * 60)

    success_count = 0
    total_count = len(AGENTS)

    for file_path, (agent_id, agent_name, port) in AGENTS.items():
        if add_chat_to_file(file_path, agent_id, agent_name, port):
            success_count += 1

    print("━" * 60)
    print(f"✅ Integration complete: {success_count}/{total_count} agents")
    print("\n🔄 Next step: pm2 restart all")

if __name__ == '__main__':
    main()