bloby-bot 0.48.3 → 0.49.1
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
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();
|
|
@@ -85,6 +85,15 @@ export interface WaStreamState {
|
|
|
85
85
|
chunkBuf: string;
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
/** Agent-turn events that carry per-turn content. Broadcast only for dashboard surfaces
|
|
89
|
+
* ('workspace' / 'chat'); suppressed for WhatsApp/Alexa turns so their replies don't
|
|
90
|
+
* bleed into the chat-bubble UI. Non-turn events (bot:idle, bot:error, channel:*) are
|
|
91
|
+
* always broadcast. */
|
|
92
|
+
const CHAT_TURN_EVENTS = new Set([
|
|
93
|
+
'bot:token', 'bot:response', 'bot:tool',
|
|
94
|
+
'bot:task-created', 'bot:task-progress', 'bot:task-done',
|
|
95
|
+
]);
|
|
96
|
+
|
|
88
97
|
export class ChannelManager {
|
|
89
98
|
private providers = new Map<ChannelType, ChannelProvider>();
|
|
90
99
|
private opts: ChannelManagerOpts;
|
|
@@ -377,7 +386,7 @@ export class ChannelManager {
|
|
|
377
386
|
q = [];
|
|
378
387
|
this.routingQueues.set(convId, q);
|
|
379
388
|
}
|
|
380
|
-
q.push(target);
|
|
389
|
+
q.push({ ...target, pushedAt: Date.now() });
|
|
381
390
|
pushMessage(convId, content, attachments, savedFiles);
|
|
382
391
|
}
|
|
383
392
|
|
|
@@ -395,9 +404,18 @@ export class ChannelManager {
|
|
|
395
404
|
if (!q || q.length === 0) return undefined;
|
|
396
405
|
const target = q.shift();
|
|
397
406
|
if (q.length === 0) this.routingQueues.delete(convId);
|
|
407
|
+
if (target?.pushedAt && Date.now() - target.pushedAt > 30_000) {
|
|
408
|
+
log.warn(`[channels] Stale route popped: surface=${target.surface}, age=${Math.round((Date.now() - target.pushedAt) / 1000)}s, to=${target.waSendTo || 'none'}, queueDepth=${q?.length ?? 0}`);
|
|
409
|
+
}
|
|
398
410
|
return target;
|
|
399
411
|
}
|
|
400
412
|
|
|
413
|
+
/** Return the surface of the current turn's routing target without consuming it.
|
|
414
|
+
* Used by broadcast guards to suppress chat-bubble events for non-dashboard turns. */
|
|
415
|
+
peekCurrentSurface(convId: string): RoutingTarget['surface'] | undefined {
|
|
416
|
+
return this.routingQueues.get(convId)?.[0]?.surface;
|
|
417
|
+
}
|
|
418
|
+
|
|
401
419
|
/** Drop all pending routes for a conversation — used when the live conversation ends.
|
|
402
420
|
* Accepts undefined for ergonomics in callers that hold a possibly-undefined convId. */
|
|
403
421
|
clearRoutes(convId: string | undefined): void {
|
|
@@ -883,8 +901,9 @@ export class ChannelManager {
|
|
|
883
901
|
return;
|
|
884
902
|
}
|
|
885
903
|
|
|
886
|
-
// Mirror
|
|
887
|
-
|
|
904
|
+
// Mirror non-turn events (bot:idle, bot:error, channel:*) to chat clients.
|
|
905
|
+
// Turn events (tokens, response, tools) go to WhatsApp only via the routing FIFO.
|
|
906
|
+
if (!CHAT_TURN_EVENTS.has(type)) broadcastBloby(type, eventData);
|
|
888
907
|
}, { botName, humanName }, recentMessages);
|
|
889
908
|
}
|
|
890
909
|
|
|
@@ -1006,7 +1025,8 @@ export class ChannelManager {
|
|
|
1006
1025
|
return;
|
|
1007
1026
|
}
|
|
1008
1027
|
|
|
1009
|
-
|
|
1028
|
+
// Turn events go to Alexa only via the routing FIFO; suppress from chat-bubble.
|
|
1029
|
+
if (!CHAT_TURN_EVENTS.has(type)) broadcastBloby(type, eventData);
|
|
1010
1030
|
}, { botName, humanName }, recentMessages);
|
|
1011
1031
|
}
|
|
1012
1032
|
|
|
@@ -86,6 +86,8 @@ export interface RoutingTarget {
|
|
|
86
86
|
assistantBufferKey?: string;
|
|
87
87
|
/** Original inbound WA message key — kept opaque here, used by the channel to react/quote. */
|
|
88
88
|
inboundKey?: unknown;
|
|
89
|
+
/** Unix ms when this target was pushed — used to detect stale queue entries. */
|
|
90
|
+
pushedAt?: number;
|
|
89
91
|
}
|
|
90
92
|
|
|
91
93
|
export interface ChannelProvider {
|
|
@@ -197,6 +197,7 @@ export function useBlobyChat(ws: WsClient | null, triggerReload?: number, enable
|
|
|
197
197
|
});
|
|
198
198
|
}),
|
|
199
199
|
ws.on('bot:response', (data: { conversationId: string; messageId?: string; content: string }) => {
|
|
200
|
+
if (conversationIdRef.current && data.conversationId !== conversationIdRef.current) return;
|
|
200
201
|
setConversationId(data.conversationId);
|
|
201
202
|
|
|
202
203
|
// Strip text that was already committed as a partial message
|
package/supervisor/index.ts
CHANGED
|
@@ -1451,10 +1451,13 @@ mint();
|
|
|
1451
1451
|
const ownPhone = waStatus?.connected ? (waStatus.info?.phoneNumber as string | undefined) : undefined;
|
|
1452
1452
|
const waMirrorTo = ownPhone ? `${ownPhone}@s.whatsapp.net` : undefined;
|
|
1453
1453
|
|
|
1454
|
+
// Channel tag so the agent knows the message came from the dashboard workspace
|
|
1455
|
+
// (parallel to [WhatsApp ...] and [Alexa ...] tags). DB + chat:sync use the raw
|
|
1456
|
+
// content so user-facing UIs show what was typed.
|
|
1454
1457
|
channelManager.pushWithRouting(
|
|
1455
1458
|
convId,
|
|
1456
1459
|
{ surface: 'workspace', waSendTo: waMirrorTo, isSelfChat: true },
|
|
1457
|
-
content
|
|
1460
|
+
`[workspace]\n${content}`,
|
|
1458
1461
|
agentAttachments,
|
|
1459
1462
|
savedFiles,
|
|
1460
1463
|
);
|
|
@@ -1660,6 +1663,12 @@ mint();
|
|
|
1660
1663
|
appWss.on('connection', (ws) => {
|
|
1661
1664
|
console.log('[supervisor] App API WS client connected');
|
|
1662
1665
|
|
|
1666
|
+
// Per-WS chat subscription: when the client opts in, this WS joins chatSubscribers
|
|
1667
|
+
// and receives every bot:* / chat:* event the dashboard widget does. SSE through the
|
|
1668
|
+
// Cloudflare tunnel buffers chunks; WebSocket frames flow through reliably (same
|
|
1669
|
+
// reason /app/api fetch is routed through this WS to begin with).
|
|
1670
|
+
let chatSub: ChatSubscriber | null = null;
|
|
1671
|
+
|
|
1663
1672
|
ws.on('message', (raw) => {
|
|
1664
1673
|
const rawStr = raw.toString();
|
|
1665
1674
|
|
|
@@ -1675,6 +1684,44 @@ mint();
|
|
|
1675
1684
|
return;
|
|
1676
1685
|
}
|
|
1677
1686
|
|
|
1687
|
+
if (msg.type === 'chat:subscribe') {
|
|
1688
|
+
if (chatSub) chatSubscribers.delete(chatSub);
|
|
1689
|
+
const clientId = msg.data?.clientId;
|
|
1690
|
+
const subId = crypto.randomBytes(8).toString('hex');
|
|
1691
|
+
chatSub = {
|
|
1692
|
+
id: subId,
|
|
1693
|
+
clientId,
|
|
1694
|
+
send: (type, data) => {
|
|
1695
|
+
if (ws.readyState !== WebSocket.OPEN) return;
|
|
1696
|
+
ws.send(JSON.stringify({ type: 'chat:event', data: { eventType: type, eventData: data } }));
|
|
1697
|
+
},
|
|
1698
|
+
close: () => {},
|
|
1699
|
+
};
|
|
1700
|
+
chatSubscribers.add(chatSub);
|
|
1701
|
+
console.log(`[app-ws-chat] subscribe sub=${subId} clientId=${clientId} total=${chatSubscribers.size}`);
|
|
1702
|
+
|
|
1703
|
+
if (agentQueryActive && currentStreamConvId) {
|
|
1704
|
+
chatSub.send('chat:state', {
|
|
1705
|
+
streaming: true,
|
|
1706
|
+
conversationId: currentStreamConvId,
|
|
1707
|
+
buffer: currentStreamBuffer,
|
|
1708
|
+
});
|
|
1709
|
+
}
|
|
1710
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
1711
|
+
ws.send(JSON.stringify({ type: 'chat:subscribed', data: { clientId, subId } }));
|
|
1712
|
+
}
|
|
1713
|
+
return;
|
|
1714
|
+
}
|
|
1715
|
+
|
|
1716
|
+
if (msg.type === 'chat:unsubscribe') {
|
|
1717
|
+
if (chatSub) {
|
|
1718
|
+
chatSubscribers.delete(chatSub);
|
|
1719
|
+
console.log(`[app-ws-chat] unsubscribe sub=${chatSub.id} total=${chatSubscribers.size}`);
|
|
1720
|
+
chatSub = null;
|
|
1721
|
+
}
|
|
1722
|
+
return;
|
|
1723
|
+
}
|
|
1724
|
+
|
|
1678
1725
|
if (msg.type !== 'app:api' || !msg.data) return;
|
|
1679
1726
|
|
|
1680
1727
|
const { id, method, path: reqPath, headers: reqHeaders, body } = msg.data;
|
|
@@ -1738,6 +1785,11 @@ mint();
|
|
|
1738
1785
|
});
|
|
1739
1786
|
|
|
1740
1787
|
ws.on('close', () => {
|
|
1788
|
+
if (chatSub) {
|
|
1789
|
+
chatSubscribers.delete(chatSub);
|
|
1790
|
+
console.log(`[app-ws-chat] auto-unsubscribe on close sub=${chatSub.id} total=${chatSubscribers.size}`);
|
|
1791
|
+
chatSub = null;
|
|
1792
|
+
}
|
|
1741
1793
|
console.log('[supervisor] App API WS client disconnected');
|
|
1742
1794
|
});
|
|
1743
1795
|
});
|
|
@@ -1783,6 +1835,8 @@ mint();
|
|
|
1783
1835
|
*
|
|
1784
1836
|
* Factored so adding a new surface (here: workspace) cannot drift from the chat WS
|
|
1785
1837
|
* behaviour — same buffer, same persistence, same restart timing. */
|
|
1838
|
+
const CHAT_TURN_EVENTS = new Set(['bot:token', 'bot:response', 'bot:tool', 'bot:task-created', 'bot:task-progress', 'bot:task-done']);
|
|
1839
|
+
|
|
1786
1840
|
function createSharedChatOnMessage(
|
|
1787
1841
|
convId: string,
|
|
1788
1842
|
model: string,
|
|
@@ -1790,12 +1844,17 @@ mint();
|
|
|
1790
1844
|
waState: ReturnType<typeof channelManager.createWaStreamState>,
|
|
1791
1845
|
) {
|
|
1792
1846
|
return async (type: string, eventData: any) => {
|
|
1847
|
+
// Capture surface BEFORE routeWaStreamEvent consumes the routing target on bot:response.
|
|
1848
|
+
// Used below to suppress chat-bubble broadcasts for non-dashboard turns.
|
|
1849
|
+
const triggerSurface = channelManager.peekCurrentSurface(convId);
|
|
1850
|
+
const isDashboardTurn = !triggerSurface || triggerSurface === 'workspace' || triggerSurface === 'chat';
|
|
1851
|
+
|
|
1793
1852
|
if (type === 'bot:typing') {
|
|
1794
1853
|
currentStreamConvId = convId;
|
|
1795
1854
|
currentStreamBuffer = '';
|
|
1796
1855
|
agentQueryActive = true;
|
|
1797
1856
|
}
|
|
1798
|
-
if (type === 'bot:token' && eventData.token) {
|
|
1857
|
+
if (type === 'bot:token' && eventData.token && isDashboardTurn) {
|
|
1799
1858
|
currentStreamBuffer += eventData.token;
|
|
1800
1859
|
}
|
|
1801
1860
|
|
|
@@ -1846,6 +1905,11 @@ mint();
|
|
|
1846
1905
|
}
|
|
1847
1906
|
}
|
|
1848
1907
|
|
|
1908
|
+
// Suppress agent-turn events from non-dashboard surfaces. WhatsApp/Alexa replies
|
|
1909
|
+
// are already delivered via the routing FIFO; broadcasting them would bleed
|
|
1910
|
+
// content into chat-bubble clients that weren't part of that conversation.
|
|
1911
|
+
if (CHAT_TURN_EVENTS.has(type) && !isDashboardTurn) return;
|
|
1912
|
+
|
|
1849
1913
|
broadcastBloby(type, eventData);
|
|
1850
1914
|
};
|
|
1851
1915
|
}
|
|
@@ -2192,7 +2256,7 @@ mint();
|
|
|
2192
2256
|
channelManager.pushWithRouting(
|
|
2193
2257
|
convId,
|
|
2194
2258
|
{ surface: 'chat', waSendTo: waMirrorTo, isSelfChat: true },
|
|
2195
|
-
content
|
|
2259
|
+
`[chat]\n${content}`,
|
|
2196
2260
|
data.attachments,
|
|
2197
2261
|
savedFiles,
|
|
2198
2262
|
);
|
|
@@ -274,11 +274,16 @@ If your human asks you to update a skill's behavior, edit the files INSIDE `skil
|
|
|
274
274
|
- Correct: `skills/my-skill/SCRIPT.md`
|
|
275
275
|
- Wrong: `SCRIPT.md` (this writes to workspace root!)
|
|
276
276
|
|
|
277
|
-
## Channels (WhatsApp, Telegram, Discord, etc.)
|
|
277
|
+
## Channels (chat, workspace, WhatsApp, Telegram, Discord, etc.)
|
|
278
278
|
|
|
279
|
-
You can communicate through
|
|
279
|
+
You can communicate through several surfaces at once. The two built-in ones are:
|
|
280
280
|
|
|
281
|
-
|
|
281
|
+
- **`[chat]`** — the chat bubble in the dashboard. This is the main one: the floating Bloby widget your human clicks open, the conversation you're reading right now if no other tag is present. Treat it as your home base.
|
|
282
|
+
- **`[workspace]`** — a chat-shaped widget your human placed somewhere inside their dashboard *workspace*. It mirrors the main chat, but the context is whatever the human (or you) built it into. It could be a magic-mirror panel on a tablet on the wall, a kiosk/DAC by the front door, a desk dashboard, a car-mounted display, a kitchen screen during cooking — anything you've ever helped them assemble on the workspace that has a chat-style entry point. **Check `MEMORY.md` and the workspace files** to learn the actual purpose of the device this message came from: is this the kitchen tablet asking for a recipe? The hallway mirror asking what's on today's schedule? The garage panel asking about the car? Tailor tone, brevity, and content to that role. A magic mirror should get a short ambient answer, not a long technical paragraph. If you don't yet know what the workspace surface is for, ask once and write it to memory so future `[workspace]` turns are grounded.
|
|
283
|
+
|
|
284
|
+
Beyond those, your human can install additional channels (WhatsApp, Telegram, Discord, Alexa…) as **skills** from the Bloby Marketplace. Each channel skill teaches you the conventions for that surface.
|
|
285
|
+
|
|
286
|
+
**Channel discipline.** Every incoming message is tagged with a surface (e.g. `[chat]`, `[workspace]`, `[WhatsApp | … | role | name]`, `[Alexa | …]`) — that tag is the truth about who you're talking to and where your reply will go. The supervisor pins each turn's reply to the surface that triggered it; concurrent inbounds from another channel cannot redirect this turn. **Don't infer the channel from prior messages, conversation drift, or what feels right** — read the tag on the current turn and respond accordingly. Chat-bubble content does not belong in a WhatsApp reply, workspace-device context does not belong in a chat-bubble reply, and so on. If a tag isn't present, you're on the chat bubble. If you ever feel the urge to mention a different channel's content in your reply, stop and re-check the tag.
|
|
282
287
|
|
|
283
288
|
## Marketplace — Getting New Skills
|
|
284
289
|
|