Openclaw black magic; if you have multiple openclaw agents, paste this to them both. Be sure to edit the prompt for their names and to pick a proper unique topic that’s unique to your swarm;
——-
I need you to join a P2P agent swarm network to collaborate with another agent.
### 1. Create the Swarm Script
Please create a file at `/root/.openclaw/workspace/swarm-node.js` with the following content:
```javascript
const Hyperswarm = require('hyperswarm');
const crypto = require('crypto');
const readline = require('readline');
async function main() {
console.log("🔥 Initializing P2P Agent Swarm Node...");
const swarm = new Hyperswarm();
const topic = crypto.createHash('sha256').update('openclaw-agent-swarm-v1-custom').digest();
const id = crypto.randomUUID().slice(0, 8);
const args = process.argv.slice(2);
const name = args[0] || `Agent-${id}`;
console.log(`🤖 Identity: ${name} (ID: ${id})`);
console.log(`📡 Joining Swarm Topic...`);
swarm.join(topic, { client: true, server: true });
const peers = new Set();
swarm.on('connection', (conn, info) => {
peers.add(conn);
conn.on('data', data => {
try {
const msg = JSON.parse(data.toString());
if (msg.senderId === id) return;
if (msg.type === 'broadcast') {
console.log(`\n💬 [${msg.sender}]: ${msg.content}`);
} else if (msg.type === 'direct' &&
msg.target === name) {
console.log(`\n🔔 [${msg.sender} -> ME]: ${msg.content}`);
if (msg.content.toLowerCase().includes("ping")) {
const reply = { type: 'direct', sender: name, senderId: id, target: msg.sender, content: `Pong! (from ${name})` };
conn.write(JSON.stringify(reply));
}
}
} catch (e) {}
});
conn.on('close', () => peers.delete(conn));
conn.on('error', () => {});
});
console.log(`✅ Swarm Active.`);
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
rl.on('line', (line) => {
const input = line.trim();
if (!input) return;
let msg = { sender: name, senderId: id, timestamp:
Date.now() };
if (input.startsWith('@')) {
const parts = input.split(' ');
msg.type = 'direct';
msg.target = parts[0].substring(1);
msg.content = parts.slice(1).join(' ');
} else {
msg.type = 'broadcast';
msg.content = input;
}
const payload = JSON.stringify(msg);
for (const peer of peers) peer.write(payload);
});
}
main().catch(console.error);
2. Install Dependencies
Run this command to install the required networking package:
npm install hyperswarm
3. Connect to the Swarm
Run the script using exec. Name yourself "Agent-Beta" so the other agent recognizes you.
node /root/.openclaw/workspace/swarm-node.js Agent-Beta
Once running, the script will listen for messages. You can broadcast by typing a message, or message the other agent (Agent-me) directly using
@Agent-me Hello.