bloby-bot 0.48.3 → 0.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/supervisor/app-ws.js +53 -0
- package/supervisor/index.ts +49 -0
package/package.json
CHANGED
package/supervisor/app-ws.js
CHANGED
|
@@ -16,6 +16,25 @@
|
|
|
16
16
|
var intentionalClose = false;
|
|
17
17
|
var originalFetch = window.fetch;
|
|
18
18
|
|
|
19
|
+
// Chat pub/sub state — bot:* / chat:* events forwarded from the supervisor's chat
|
|
20
|
+
// broadcaster. Listeners are independent of WS lifecycle: when the WS reconnects we
|
|
21
|
+
// re-send the subscribe message automatically.
|
|
22
|
+
var chatListeners = [];
|
|
23
|
+
var chatSubscribed = false;
|
|
24
|
+
var chatClientId = null;
|
|
25
|
+
|
|
26
|
+
function emitChatEvent(eventType, data) {
|
|
27
|
+
for (var i = 0; i < chatListeners.length; i++) {
|
|
28
|
+
try { chatListeners[i](eventType, data); } catch (err) { console.error('[app-ws] chat listener error', err); }
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function sendChatSubscribe() {
|
|
33
|
+
if (!ws || ws.readyState !== WebSocket.OPEN) return;
|
|
34
|
+
ws.send(JSON.stringify({ type: 'chat:subscribe', data: { clientId: chatClientId } }));
|
|
35
|
+
console.log('[app-ws] chat:subscribe sent clientId=' + chatClientId);
|
|
36
|
+
}
|
|
37
|
+
|
|
19
38
|
function buildWsUrl() {
|
|
20
39
|
var proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
21
40
|
return proto + '//' + location.host + '/app/ws';
|
|
@@ -56,6 +75,7 @@
|
|
|
56
75
|
reconnectDelay = RECONNECT_BASE;
|
|
57
76
|
startHeartbeat();
|
|
58
77
|
console.log('[app-ws] Connected to /app/ws');
|
|
78
|
+
if (chatSubscribed) sendChatSubscribe();
|
|
59
79
|
};
|
|
60
80
|
|
|
61
81
|
ws.onmessage = function (e) {
|
|
@@ -68,6 +88,16 @@
|
|
|
68
88
|
return;
|
|
69
89
|
}
|
|
70
90
|
|
|
91
|
+
if (msg.type === 'chat:event' && msg.data) {
|
|
92
|
+
emitChatEvent(msg.data.eventType, msg.data.eventData);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (msg.type === 'chat:subscribed') {
|
|
97
|
+
console.log('[app-ws] chat:subscribed ack clientId=' + (msg.data && msg.data.clientId));
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
71
101
|
if (msg.type === 'app:api:response' && msg.data && msg.data.id) {
|
|
72
102
|
var pending = pendingRequests[msg.data.id];
|
|
73
103
|
if (pending) {
|
|
@@ -194,6 +224,29 @@
|
|
|
194
224
|
connected: function () {
|
|
195
225
|
return connected;
|
|
196
226
|
},
|
|
227
|
+
// Subscribe to chat events (bot:typing, bot:token, bot:response, chat:sync, …).
|
|
228
|
+
// Returns an unsubscribe function. Multiple listeners are supported. The underlying
|
|
229
|
+
// subscribe message is sent on first listener registration and re-sent on reconnect.
|
|
230
|
+
onChatEvent: function (handler) {
|
|
231
|
+
chatListeners.push(handler);
|
|
232
|
+
if (!chatSubscribed) {
|
|
233
|
+
chatSubscribed = true;
|
|
234
|
+
chatClientId =
|
|
235
|
+
(typeof crypto !== 'undefined' && crypto.randomUUID)
|
|
236
|
+
? crypto.randomUUID()
|
|
237
|
+
: 'ws-' + Math.random().toString(36).slice(2);
|
|
238
|
+
if (ws && ws.readyState === WebSocket.OPEN) sendChatSubscribe();
|
|
239
|
+
}
|
|
240
|
+
return function unsubscribe() {
|
|
241
|
+
var i = chatListeners.indexOf(handler);
|
|
242
|
+
if (i >= 0) chatListeners.splice(i, 1);
|
|
243
|
+
};
|
|
244
|
+
},
|
|
245
|
+
// Stable per-tab clientId used to identify this subscriber to the server. Useful
|
|
246
|
+
// when calling /api/agent/chat/message so the server can skip echoing chat:sync back.
|
|
247
|
+
clientId: function () {
|
|
248
|
+
return chatClientId;
|
|
249
|
+
},
|
|
197
250
|
};
|
|
198
251
|
|
|
199
252
|
connect();
|
package/supervisor/index.ts
CHANGED
|
@@ -1660,6 +1660,12 @@ mint();
|
|
|
1660
1660
|
appWss.on('connection', (ws) => {
|
|
1661
1661
|
console.log('[supervisor] App API WS client connected');
|
|
1662
1662
|
|
|
1663
|
+
// Per-WS chat subscription: when the client opts in, this WS joins chatSubscribers
|
|
1664
|
+
// and receives every bot:* / chat:* event the dashboard widget does. SSE through the
|
|
1665
|
+
// Cloudflare tunnel buffers chunks; WebSocket frames flow through reliably (same
|
|
1666
|
+
// reason /app/api fetch is routed through this WS to begin with).
|
|
1667
|
+
let chatSub: ChatSubscriber | null = null;
|
|
1668
|
+
|
|
1663
1669
|
ws.on('message', (raw) => {
|
|
1664
1670
|
const rawStr = raw.toString();
|
|
1665
1671
|
|
|
@@ -1675,6 +1681,44 @@ mint();
|
|
|
1675
1681
|
return;
|
|
1676
1682
|
}
|
|
1677
1683
|
|
|
1684
|
+
if (msg.type === 'chat:subscribe') {
|
|
1685
|
+
if (chatSub) chatSubscribers.delete(chatSub);
|
|
1686
|
+
const clientId = msg.data?.clientId;
|
|
1687
|
+
const subId = crypto.randomBytes(8).toString('hex');
|
|
1688
|
+
chatSub = {
|
|
1689
|
+
id: subId,
|
|
1690
|
+
clientId,
|
|
1691
|
+
send: (type, data) => {
|
|
1692
|
+
if (ws.readyState !== WebSocket.OPEN) return;
|
|
1693
|
+
ws.send(JSON.stringify({ type: 'chat:event', data: { eventType: type, eventData: data } }));
|
|
1694
|
+
},
|
|
1695
|
+
close: () => {},
|
|
1696
|
+
};
|
|
1697
|
+
chatSubscribers.add(chatSub);
|
|
1698
|
+
console.log(`[app-ws-chat] subscribe sub=${subId} clientId=${clientId} total=${chatSubscribers.size}`);
|
|
1699
|
+
|
|
1700
|
+
if (agentQueryActive && currentStreamConvId) {
|
|
1701
|
+
chatSub.send('chat:state', {
|
|
1702
|
+
streaming: true,
|
|
1703
|
+
conversationId: currentStreamConvId,
|
|
1704
|
+
buffer: currentStreamBuffer,
|
|
1705
|
+
});
|
|
1706
|
+
}
|
|
1707
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
1708
|
+
ws.send(JSON.stringify({ type: 'chat:subscribed', data: { clientId, subId } }));
|
|
1709
|
+
}
|
|
1710
|
+
return;
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
if (msg.type === 'chat:unsubscribe') {
|
|
1714
|
+
if (chatSub) {
|
|
1715
|
+
chatSubscribers.delete(chatSub);
|
|
1716
|
+
console.log(`[app-ws-chat] unsubscribe sub=${chatSub.id} total=${chatSubscribers.size}`);
|
|
1717
|
+
chatSub = null;
|
|
1718
|
+
}
|
|
1719
|
+
return;
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1678
1722
|
if (msg.type !== 'app:api' || !msg.data) return;
|
|
1679
1723
|
|
|
1680
1724
|
const { id, method, path: reqPath, headers: reqHeaders, body } = msg.data;
|
|
@@ -1738,6 +1782,11 @@ mint();
|
|
|
1738
1782
|
});
|
|
1739
1783
|
|
|
1740
1784
|
ws.on('close', () => {
|
|
1785
|
+
if (chatSub) {
|
|
1786
|
+
chatSubscribers.delete(chatSub);
|
|
1787
|
+
console.log(`[app-ws-chat] auto-unsubscribe on close sub=${chatSub.id} total=${chatSubscribers.size}`);
|
|
1788
|
+
chatSub = null;
|
|
1789
|
+
}
|
|
1741
1790
|
console.log('[supervisor] App API WS client disconnected');
|
|
1742
1791
|
});
|
|
1743
1792
|
});
|