File size: 1,097 Bytes
e92be04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { db } from "../config/gun.js";
import fetch from "node-fetch";

const hiveEventClients = new Set();

export function broadcastHiveEvent(type, data) {
    if (hiveEventClients.size === 0) return;
    const payload = `data: ${JSON.stringify({ type, ts: Date.now(), ...data })}

`;
    for (const client of hiveEventClients) {
        try { client.write(payload); } catch { hiveEventClients.delete(client); }
    }

    // Webhooks (Phase 7)
    db.get("webhooks").map().once((hook, agentId) => {
        if (hook && hook.callbackUrl) {
            try {
                const events = JSON.parse(hook.events || '["*"]');
                if (events.includes("*") || events.includes(type)) {
                   fetch(hook.callbackUrl, {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json' },
                        body: JSON.stringify({ type, data, ts: Date.now() })
                    }).catch(() => {}); // Silent fail for webhooks
                }
            } catch (e) {}
        }
    });
}

export { hiveEventClients };