bloby-bot 0.40.0 → 0.42.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/channels/manager.ts +60 -7
- package/supervisor/channels/whatsapp.ts +43 -22
- package/supervisor/index.ts +30 -7
- package/worker/db.ts +3 -0
- package/worker/index.ts +8 -1
package/package.json
CHANGED
|
@@ -116,6 +116,7 @@ export class ChannelManager {
|
|
|
116
116
|
},
|
|
117
117
|
(status) => this.handleStatusChange(status),
|
|
118
118
|
(audioBase64) => this.transcribeAudio(audioBase64),
|
|
119
|
+
(fromMe, isSelfChat, isGroup) => this.shouldProcessWhatsAppAudio(fromMe, isSelfChat, isGroup),
|
|
119
120
|
);
|
|
120
121
|
this.providers.set('whatsapp', whatsapp);
|
|
121
122
|
|
|
@@ -140,6 +141,7 @@ export class ChannelManager {
|
|
|
140
141
|
},
|
|
141
142
|
(status) => this.handleStatusChange(status),
|
|
142
143
|
(audioBase64) => this.transcribeAudio(audioBase64),
|
|
144
|
+
(fromMe, isSelfChat, isGroup) => this.shouldProcessWhatsAppAudio(fromMe, isSelfChat, isGroup),
|
|
143
145
|
);
|
|
144
146
|
this.providers.set('whatsapp', whatsapp);
|
|
145
147
|
provider = whatsapp;
|
|
@@ -419,6 +421,35 @@ export class ChannelManager {
|
|
|
419
421
|
return config.channels?.[channel];
|
|
420
422
|
}
|
|
421
423
|
|
|
424
|
+
/** Decide whether an inbound WhatsApp audio is worth transcribing.
|
|
425
|
+
* Mirrors the gates in handleInboundMessage so we don't burn Whisper calls
|
|
426
|
+
* (or, worse, leak the bot via "Whisper not enabled" replies) on messages
|
|
427
|
+
* that would be filtered out anyway.
|
|
428
|
+
*
|
|
429
|
+
* Audio carries no `@bloby` text trigger, so in assistant mode we only
|
|
430
|
+
* transcribe when the audio is admin's self-chat command. */
|
|
431
|
+
private shouldProcessWhatsAppAudio(fromMe: boolean, isSelfChat: boolean, isGroup: boolean): boolean {
|
|
432
|
+
const channelConfig = this.getChannelConfig('whatsapp');
|
|
433
|
+
if (!channelConfig) return false;
|
|
434
|
+
|
|
435
|
+
const mode = channelConfig.mode || 'channel';
|
|
436
|
+
|
|
437
|
+
// Group gating mirrors handleInboundMessage.
|
|
438
|
+
if (isGroup) {
|
|
439
|
+
if (mode === 'channel') return false;
|
|
440
|
+
if (!channelConfig.allowGroups) return false;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (mode === 'channel') return fromMe && isSelfChat;
|
|
444
|
+
if (mode === 'assistant') return fromMe && isSelfChat;
|
|
445
|
+
if (mode === 'business') {
|
|
446
|
+
// Outbound non-self-chat messages are filtered out — same as handleInboundMessage.
|
|
447
|
+
if (fromMe && !isSelfChat) return false;
|
|
448
|
+
return true;
|
|
449
|
+
}
|
|
450
|
+
return false;
|
|
451
|
+
}
|
|
452
|
+
|
|
422
453
|
/** Handle an incoming message from any channel — debounces rapid messages from the same sender.
|
|
423
454
|
*
|
|
424
455
|
* Per-mode behavior is decided here. To add a new mode: extend the gating block below
|
|
@@ -638,14 +669,25 @@ export class ChannelManager {
|
|
|
638
669
|
const { workerApi, broadcastBloby, getModel } = this.opts;
|
|
639
670
|
const model = getModel();
|
|
640
671
|
|
|
641
|
-
// Get or create conversation (shared with chat for mirroring)
|
|
672
|
+
// Get or create conversation (shared with chat for mirroring).
|
|
673
|
+
// The current_conversation setting can desync from the DB (e.g. a chat-UI
|
|
674
|
+
// re-mount writing back a stale cached id, or a conv that was never
|
|
675
|
+
// persisted in the first place). Verify it actually exists before using it,
|
|
676
|
+
// otherwise we'd push messages into a ghost conv and FK-fail on every write.
|
|
642
677
|
let convId: string | undefined;
|
|
643
678
|
try {
|
|
644
679
|
const ctx = await workerApi('/api/context/current');
|
|
645
680
|
if (ctx.conversationId) {
|
|
646
|
-
|
|
647
|
-
|
|
681
|
+
const verify = await workerApi(`/api/conversations/${ctx.conversationId}/exists`);
|
|
682
|
+
if (verify?.exists) {
|
|
683
|
+
convId = ctx.conversationId;
|
|
684
|
+
} else {
|
|
685
|
+
log.warn(`[channels] current_conversation=${ctx.conversationId} is stale (no DB row) — creating fresh`);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
if (!convId) {
|
|
648
689
|
const conv = await workerApi('/api/conversations', 'POST', { title: 'WhatsApp', model });
|
|
690
|
+
if (!conv?.id) throw new Error(`POST /api/conversations returned no id: ${JSON.stringify(conv)}`);
|
|
649
691
|
convId = conv.id;
|
|
650
692
|
await workerApi('/api/context/set', 'POST', { conversationId: convId });
|
|
651
693
|
}
|
|
@@ -657,15 +699,20 @@ export class ChannelManager {
|
|
|
657
699
|
// Use display text for DB/chat (hides enriched agent context from the UI)
|
|
658
700
|
const displayContent = msg.displayText || msg.text;
|
|
659
701
|
|
|
660
|
-
// Save user message to DB
|
|
702
|
+
// Save user message to DB. The worker returns {error,...} on failure
|
|
703
|
+
// without throwing, so check the response body — silent drops here are
|
|
704
|
+
// the original cause of "messages not appearing in UI" reports.
|
|
661
705
|
try {
|
|
662
|
-
await workerApi(`/api/conversations/${convId}/messages`, 'POST', {
|
|
706
|
+
const result = await workerApi(`/api/conversations/${convId}/messages`, 'POST', {
|
|
663
707
|
role: 'user',
|
|
664
708
|
content: displayContent,
|
|
665
709
|
meta: { model, channel: msg.channel },
|
|
666
710
|
});
|
|
711
|
+
if (result?.error) {
|
|
712
|
+
log.warn(`[channels] CRITICAL: user message NOT persisted (convId=${convId}): ${result.error}`);
|
|
713
|
+
}
|
|
667
714
|
} catch (err: any) {
|
|
668
|
-
log.warn(`[channels]
|
|
715
|
+
log.warn(`[channels] CRITICAL: user message NOT persisted (convId=${convId}): ${err.message}`);
|
|
669
716
|
}
|
|
670
717
|
|
|
671
718
|
// Broadcast to chat clients (mirroring)
|
|
@@ -733,7 +780,13 @@ export class ChannelManager {
|
|
|
733
780
|
role: 'assistant',
|
|
734
781
|
content: eventData.content,
|
|
735
782
|
meta: { model },
|
|
736
|
-
}).
|
|
783
|
+
}).then((result: any) => {
|
|
784
|
+
if (result?.error) {
|
|
785
|
+
log.warn(`[channels] CRITICAL: assistant reply NOT persisted (convId=${convId}): ${result.error}`);
|
|
786
|
+
}
|
|
787
|
+
}).catch((err: any) => {
|
|
788
|
+
log.warn(`[channels] CRITICAL: assistant reply NOT persisted (convId=${convId}): ${err.message}`);
|
|
789
|
+
});
|
|
737
790
|
}
|
|
738
791
|
|
|
739
792
|
// Handle turn completion — restart backend if file tools were used,
|
|
@@ -48,6 +48,16 @@ export type OnWhatsAppMessage = (
|
|
|
48
48
|
/** Callback to transcribe audio via whisper */
|
|
49
49
|
export type TranscribeFn = (audioBase64: string) => Promise<string | null>;
|
|
50
50
|
|
|
51
|
+
/** Callback that decides whether an audio message warrants transcription.
|
|
52
|
+
* Returning false makes the channel silently skip the audio (no Whisper call,
|
|
53
|
+
* no "Whisper not enabled" reply) — used to avoid leaking the bot in modes
|
|
54
|
+
* where the message would be filtered out downstream anyway. */
|
|
55
|
+
export type ShouldTranscribeAudioFn = (
|
|
56
|
+
fromMe: boolean,
|
|
57
|
+
isSelfChat: boolean,
|
|
58
|
+
isGroup: boolean,
|
|
59
|
+
) => boolean;
|
|
60
|
+
|
|
51
61
|
export class WhatsAppChannel implements ChannelProvider {
|
|
52
62
|
readonly type: ChannelType = 'whatsapp';
|
|
53
63
|
|
|
@@ -58,6 +68,7 @@ export class WhatsAppChannel implements ChannelProvider {
|
|
|
58
68
|
private onMessage: OnWhatsAppMessage;
|
|
59
69
|
private onStatusChange: (status: ChannelStatus) => void;
|
|
60
70
|
private transcribe: TranscribeFn | null = null;
|
|
71
|
+
private shouldTranscribeAudio: ShouldTranscribeAudioFn | null = null;
|
|
61
72
|
private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
|
62
73
|
private intentionalDisconnect = false;
|
|
63
74
|
|
|
@@ -76,10 +87,12 @@ export class WhatsAppChannel implements ChannelProvider {
|
|
|
76
87
|
onMessage: OnWhatsAppMessage,
|
|
77
88
|
onStatusChange: (status: ChannelStatus) => void,
|
|
78
89
|
transcribe?: TranscribeFn,
|
|
90
|
+
shouldTranscribeAudio?: ShouldTranscribeAudioFn,
|
|
79
91
|
) {
|
|
80
92
|
this.onMessage = onMessage;
|
|
81
93
|
this.onStatusChange = onStatusChange;
|
|
82
94
|
this.transcribe = transcribe || null;
|
|
95
|
+
this.shouldTranscribeAudio = shouldTranscribeAudio || null;
|
|
83
96
|
}
|
|
84
97
|
|
|
85
98
|
async connect(): Promise<void> {
|
|
@@ -441,6 +454,29 @@ export class WhatsAppChannel implements ChannelProvider {
|
|
|
441
454
|
continue;
|
|
442
455
|
}
|
|
443
456
|
|
|
457
|
+
// Resolve sender/chat identity up front so audio gating can consult mode/role.
|
|
458
|
+
const fromMe = msg.key.fromMe || false;
|
|
459
|
+
const rawSender = msg.key.remoteJid || '';
|
|
460
|
+
const participant = msg.key.participant || '';
|
|
461
|
+
const isGroup = rawSender.endsWith('@g.us');
|
|
462
|
+
|
|
463
|
+
// chatJid: where to reply (group JID for groups, peer JID otherwise).
|
|
464
|
+
const chatJid = rawSender;
|
|
465
|
+
|
|
466
|
+
// The actual sender JID:
|
|
467
|
+
// - groups: always `participant` (remoteJid is the group)
|
|
468
|
+
// - 1:1: `participant` if Baileys provided one (newer protocol), else remoteJid
|
|
469
|
+
const actualSender = isGroup
|
|
470
|
+
? participant || rawSender
|
|
471
|
+
: (participant || rawSender);
|
|
472
|
+
|
|
473
|
+
// Translate LID JIDs to phone JIDs (only handles our own LID)
|
|
474
|
+
const sender = this.translateJid(actualSender);
|
|
475
|
+
const pushName = msg.pushName || undefined;
|
|
476
|
+
|
|
477
|
+
// Self-chat: only meaningful for 1:1 — remoteJid is our own number AND no participant.
|
|
478
|
+
const isSelfChat = !isGroup && !participant && this.ownPhoneJid !== null && this.translateJid(rawSender) === this.ownPhoneJid;
|
|
479
|
+
|
|
444
480
|
// Extract text — or transcribe audio if it's a voice note
|
|
445
481
|
let rawText = this.extractText(msg.message);
|
|
446
482
|
const images: WhatsAppImageAttachment[] = [];
|
|
@@ -459,6 +495,13 @@ export class WhatsAppChannel implements ChannelProvider {
|
|
|
459
495
|
}
|
|
460
496
|
|
|
461
497
|
if (!rawText && this.isAudioMessage(msg.message)) {
|
|
498
|
+
// Mode-aware gate: don't transcribe (and don't reveal the bot with a
|
|
499
|
+
// "Whisper not enabled" reply) when the message would be filtered out
|
|
500
|
+
// downstream — e.g. a friend's voice note in assistant mode.
|
|
501
|
+
if (this.shouldTranscribeAudio && !this.shouldTranscribeAudio(fromMe, isSelfChat, isGroup)) {
|
|
502
|
+
log.info(`[whatsapp] Audio skipped by mode gate (fromMe=${fromMe}, selfChat=${isSelfChat}, group=${isGroup})`);
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
462
505
|
// Voice note / audio — download and transcribe
|
|
463
506
|
if (!this.transcribe) {
|
|
464
507
|
log.info('[whatsapp] Audio message received but no transcribe function configured — skipping');
|
|
@@ -494,28 +537,6 @@ export class WhatsAppChannel implements ChannelProvider {
|
|
|
494
537
|
// Escape special characters to prevent prompt injection via message content
|
|
495
538
|
const text = this.escapeMessageText(rawText);
|
|
496
539
|
|
|
497
|
-
const fromMe = msg.key.fromMe || false;
|
|
498
|
-
const rawSender = msg.key.remoteJid || '';
|
|
499
|
-
const participant = msg.key.participant || '';
|
|
500
|
-
const isGroup = rawSender.endsWith('@g.us');
|
|
501
|
-
|
|
502
|
-
// chatJid: where to reply (group JID for groups, peer JID otherwise).
|
|
503
|
-
const chatJid = rawSender;
|
|
504
|
-
|
|
505
|
-
// The actual sender JID:
|
|
506
|
-
// - groups: always `participant` (remoteJid is the group)
|
|
507
|
-
// - 1:1: `participant` if Baileys provided one (newer protocol), else remoteJid
|
|
508
|
-
const actualSender = isGroup
|
|
509
|
-
? participant || rawSender
|
|
510
|
-
: (participant || rawSender);
|
|
511
|
-
|
|
512
|
-
// Translate LID JIDs to phone JIDs (only handles our own LID)
|
|
513
|
-
const sender = this.translateJid(actualSender);
|
|
514
|
-
const pushName = msg.pushName || undefined;
|
|
515
|
-
|
|
516
|
-
// Self-chat: only meaningful for 1:1 — remoteJid is our own number AND no participant.
|
|
517
|
-
const isSelfChat = !isGroup && !participant && this.ownPhoneJid !== null && this.translateJid(rawSender) === this.ownPhoneJid;
|
|
518
|
-
|
|
519
540
|
log.info(`[whatsapp] Message from ${sender} (chat=${chatJid}, group=${isGroup}, fromMe=${fromMe}, selfChat=${isSelfChat}, images=${images.length}): ${text.slice(0, 80)}`);
|
|
520
541
|
|
|
521
542
|
this.onMessage(sender, pushName, text, fromMe, isSelfChat, chatJid, isGroup, images.length > 0 ? images : undefined);
|
package/supervisor/index.ts
CHANGED
|
@@ -1325,16 +1325,33 @@ ${!connected ? `<script>
|
|
|
1325
1325
|
}
|
|
1326
1326
|
|
|
1327
1327
|
try {
|
|
1328
|
-
//
|
|
1328
|
+
// Resolve the conversation id, but verify it exists in the DB
|
|
1329
|
+
// before reusing it. clientConvs and current_conversation can both
|
|
1330
|
+
// hold stale ids (e.g. after a manual DB swap or a chat-UI
|
|
1331
|
+
// re-mount writing back a cached id) — pushing into a ghost conv
|
|
1332
|
+
// FK-fails silently and loses every message.
|
|
1329
1333
|
let dbConvId = clientConvs.get(ws);
|
|
1334
|
+
if (dbConvId) {
|
|
1335
|
+
const verify = await workerApi(`/api/conversations/${dbConvId}/exists`);
|
|
1336
|
+
if (!verify?.exists) {
|
|
1337
|
+
log.warn(`[bloby] cached convId ${dbConvId} is stale (no DB row) — discarding`);
|
|
1338
|
+
dbConvId = undefined;
|
|
1339
|
+
clientConvs.delete(ws);
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1330
1342
|
if (!dbConvId) {
|
|
1331
|
-
// Check if there's a current conversation set in settings
|
|
1332
1343
|
const ctx = await workerApi('/api/context/current');
|
|
1333
1344
|
if (ctx.conversationId) {
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1345
|
+
const verify = await workerApi(`/api/conversations/${ctx.conversationId}/exists`);
|
|
1346
|
+
if (verify?.exists) {
|
|
1347
|
+
dbConvId = ctx.conversationId;
|
|
1348
|
+
} else {
|
|
1349
|
+
log.warn(`[bloby] current_conversation=${ctx.conversationId} is stale (no DB row) — creating fresh`);
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
if (!dbConvId) {
|
|
1337
1353
|
const conv = await workerApi('/api/conversations', 'POST', { title: content.slice(0, 80), model: freshConfig.ai.model });
|
|
1354
|
+
if (!conv?.id) throw new Error(`POST /api/conversations returned no id: ${JSON.stringify(conv)}`);
|
|
1338
1355
|
dbConvId = conv.id;
|
|
1339
1356
|
await workerApi('/api/context/set', 'POST', { conversationId: dbConvId });
|
|
1340
1357
|
}
|
|
@@ -1353,9 +1370,12 @@ ${!connected ? `<script>
|
|
|
1353
1370
|
type: f.type, name: f.name, mediaType: f.mediaType, filePath: f.relPath,
|
|
1354
1371
|
})));
|
|
1355
1372
|
}
|
|
1356
|
-
await workerApi(`/api/conversations/${convId}/messages`, 'POST', {
|
|
1373
|
+
const result = await workerApi(`/api/conversations/${convId}/messages`, 'POST', {
|
|
1357
1374
|
role: 'user', content, meta,
|
|
1358
1375
|
});
|
|
1376
|
+
if (result?.error) {
|
|
1377
|
+
log.warn(`[bloby] CRITICAL: user message NOT persisted (convId=${convId}): ${result.error}`);
|
|
1378
|
+
}
|
|
1359
1379
|
|
|
1360
1380
|
// Broadcast user message to other clients
|
|
1361
1381
|
broadcastBlobyExcept(ws, 'chat:sync', {
|
|
@@ -1467,9 +1487,12 @@ ${!connected ? `<script>
|
|
|
1467
1487
|
|
|
1468
1488
|
(async () => {
|
|
1469
1489
|
try {
|
|
1470
|
-
await workerApi(`/api/conversations/${convId}/messages`, 'POST', {
|
|
1490
|
+
const result = await workerApi(`/api/conversations/${convId}/messages`, 'POST', {
|
|
1471
1491
|
role: 'assistant', content: eventData.content, meta: { model: freshConfig.ai.model },
|
|
1472
1492
|
});
|
|
1493
|
+
if (result?.error) {
|
|
1494
|
+
log.warn(`[bloby] CRITICAL: assistant reply NOT persisted (convId=${convId}): ${result.error}`);
|
|
1495
|
+
}
|
|
1473
1496
|
} catch (err: any) {
|
|
1474
1497
|
log.warn(`[bloby] DB persist bot response error: ${err.message}`);
|
|
1475
1498
|
}
|
package/worker/db.ts
CHANGED
|
@@ -90,6 +90,9 @@ export function listConversations(limit = 50) {
|
|
|
90
90
|
export function deleteConversation(id: string) {
|
|
91
91
|
db.prepare('DELETE FROM conversations WHERE id = ?').run(id);
|
|
92
92
|
}
|
|
93
|
+
export function conversationExists(id: string): boolean {
|
|
94
|
+
return !!db.prepare('SELECT 1 FROM conversations WHERE id = ?').get(id);
|
|
95
|
+
}
|
|
93
96
|
|
|
94
97
|
// Messages
|
|
95
98
|
export function addMessage(convId: string, role: string, content: string, meta?: { tokens_in?: number; tokens_out?: number; model?: string; audio_data?: string; attachments?: string }) {
|
package/worker/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ import path from 'path';
|
|
|
5
5
|
import { loadConfig, saveConfig } from '../shared/config.js';
|
|
6
6
|
import { paths, WORKSPACE_DIR } from '../shared/paths.js';
|
|
7
7
|
import { log } from '../shared/logger.js';
|
|
8
|
-
import { initDb, closeDb, listConversations, createConversation, deleteConversation, getMessages, addMessage, getSetting, getAllSettings, setSetting, createSession, getSession, deleteExpiredSessions, getRecentMessages, getMessagesBefore, addPushSubscription, removePushSubscription, getAllPushSubscriptions, getPushSubscriptionByEndpoint, createTrustedDevice, getTrustedDevice, updateDeviceLastSeen, listTrustedDevices, deleteTrustedDevice, deleteExpiredDevices, deleteAllTrustedDevices } from './db.js';
|
|
8
|
+
import { initDb, closeDb, listConversations, createConversation, deleteConversation, conversationExists, getMessages, addMessage, getSetting, getAllSettings, setSetting, createSession, getSession, deleteExpiredSessions, getRecentMessages, getMessagesBefore, addPushSubscription, removePushSubscription, getAllPushSubscriptions, getPushSubscriptionByEndpoint, createTrustedDevice, getTrustedDevice, updateDeviceLastSeen, listTrustedDevices, deleteTrustedDevice, deleteExpiredDevices, deleteAllTrustedDevices } from './db.js';
|
|
9
9
|
import webpush from 'web-push';
|
|
10
10
|
import { TOTP } from 'otpauth';
|
|
11
11
|
import QRCode from 'qrcode';
|
|
@@ -120,6 +120,9 @@ app.get('/api/conversations/:id', (req, res) => {
|
|
|
120
120
|
const msgs = getMessages(req.params.id);
|
|
121
121
|
res.json({ id: req.params.id, messages: msgs });
|
|
122
122
|
});
|
|
123
|
+
app.get('/api/conversations/:id/exists', (req, res) => {
|
|
124
|
+
res.json({ exists: conversationExists(req.params.id) });
|
|
125
|
+
});
|
|
123
126
|
app.post('/api/conversations', (req, res) => {
|
|
124
127
|
const { title, model } = req.body || {};
|
|
125
128
|
const conv = createConversation(title, model);
|
|
@@ -128,6 +131,10 @@ app.post('/api/conversations', (req, res) => {
|
|
|
128
131
|
app.post('/api/conversations/:id/messages', (req, res) => {
|
|
129
132
|
const { role, content, meta } = req.body || {};
|
|
130
133
|
if (!role || !content) { res.status(400).json({ error: 'Missing role or content' }); return; }
|
|
134
|
+
if (!conversationExists(req.params.id)) {
|
|
135
|
+
res.status(404).json({ error: 'conversation_not_found', conversationId: req.params.id });
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
131
138
|
const msg = addMessage(req.params.id, role, content, meta);
|
|
132
139
|
res.json(msg);
|
|
133
140
|
});
|