← back to Designer Wallcoverings

DW-Agents/dw-agents/integrate-chat-all-agents.sh

136 lines

#!/bin/bash

###############################################################################
# DW-Agents Chat Integration Script
# Adds inter-agent chat functionality to all agents
###############################################################################

echo "🤖 DW-Agents Chat Integration"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Backup timestamp
BACKUP_DIR="/root/DW-Agents-backup-chat-$(date +%Y%m%d-%H%M%S)"

echo "📦 Creating backup at: $BACKUP_DIR"
mkdir -p "$BACKUP_DIR"

# Agents to integrate (name:port:id)
declare -a AGENTS=(
  "agent-accounting:9882:agent-accounting"
  "agent-ceo-dashboard:7121:agent-ceo"
  "agent-cfoo-dashboard:7122:agent-cfo"
  "agent-coo-dashboard:7124:agent-coo"
  "agent-vp-operations:7123:agent-vp-ops"
  "agent-security:9892:agent-security"
  "agent-needs-attention:9886:agent-needs-attention"
  "agent-shopify-store:7238:agent-shopify"
  "agent-marketing:9881:agent-marketing"
  "agent-digital-samples:9879:agent-digital-samples"
  "agent-skills-manager:9894:agent-skills"
  "agent-task-orchestrator:9900:agent-orchestrator"
  "agent-zendesk-chat:9884:agent-zendesk"
  "agent-todays-highlights:9885:agent-highlights"
  "agent-parallel-processes:9887:agent-parallel"
  "agent-in-parallel:9891:agent-in-parallel"
  "agent-completed-tasks:9889:agent-completed"
  "agent-new-client-signups:9890:agent-signups"
  "agent-server-uptime:9888:agent-uptime"
  "agent-log-monitor:7239:agent-logs"
  "agent-ui-manager:7240:agent-ui"
  "agent-restart-analyzer:7241:agent-restart"
  "agent-all-agents-viewer:9111:agent-viewer"
)

# Function to add chat to an agent
add_chat_to_agent() {
  local agent_dir=$1
  local port=$2
  local agent_id=$3
  local agent_name=$4

  echo ""
  echo "📝 Processing: $agent_name"

  # Find the main TypeScript file
  local ts_file=""
  if [ -f "$agent_dir/${agent_dir##*/}.ts" ]; then
    ts_file="$agent_dir/${agent_dir##*/}.ts"
  elif [ -f "$agent_dir/server.ts" ]; then
    ts_file="$agent_dir/server.ts"
  elif [ -f "$agent_dir/index.ts" ]; then
    ts_file="$agent_dir/index.ts"
  else
    echo "   ⚠️  No TypeScript file found in $agent_dir"
    return 1
  fi

  echo "   Found: $ts_file"

  # Backup
  cp "$ts_file" "$BACKUP_DIR/${agent_dir##*/}.ts.bak"

  # Check if already has chat
  if grep -q "chatMiddleware" "$ts_file"; then
    echo "   ✅ Chat already integrated"
    return 0
  fi

  # Add import after other imports
  awk -v agent_id="$agent_id" -v agent_name="$agent_name" -v port="$port" '
    /^import/ && !done {
      print
      next
    }
    !done && !/^import/ && !/^$/ && !/^\/\// {
      print "import { chatMiddleware } from '\''../shared-chat-integration'\'';"
      print ""
      done=1
    }
    /^app\.use\(express\.static/ && !chat_added {
      print
      print ""
      print "// Add inter-agent chat"
      print "app.use(chatMiddleware({"
      print "  agentId: '\''" agent_id "'\'',"
      print "  agentName: '\''" agent_name "'\'',"
      print "  port: " port ","
      print "  category: '\''agent'\''"
      print "}));"
      chat_added=1
      next
    }
    { print }
  ' "$ts_file" > "$ts_file.tmp" && mv "$ts_file.tmp" "$ts_file"

  echo "   ✅ Chat integration added"
}

# Process each agent
for agent_info in "${AGENTS[@]}"; do
  IFS=':' read -r agent_dir port agent_id <<< "$agent_info"

  # Extract readable name from directory
  agent_name=$(echo "$agent_dir" | sed 's/agent-//; s/-/ /g; s/\b\(.\)/\u\1/g')

  if [ -d "/root/DW-Agents/$agent_dir" ]; then
    add_chat_to_agent "/root/DW-Agents/$agent_dir" "$port" "$agent_id" "$agent_name"
  else
    echo "⚠️  Directory not found: $agent_dir"
  fi
done

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✅ Chat integration complete!"
echo ""
echo "📊 Summary:"
echo "   • Backup: $BACKUP_DIR"
echo "   • Chat Server: http://45.61.58.125:9998"
echo "   • Integrated: ${#AGENTS[@]} agents"
echo ""
echo "🔄 Next steps:"
echo "   1. pm2 restart all"
echo "   2. Visit any agent and look for 💬 chat button"
echo "   3. Test inter-agent messaging"
echo ""