Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(translation): language backstop so misrouted messages are never dropped
dallascyclistb374fa02776a
diff --git a/src/plugins/extensions/translation/core/translation.coordinator.spec.ts b/src/plugins/extensions/translation/core/translation.coordinator.spec.tsindex 65ce0215f..c320926ae 100644--- a/src/plugins/extensions/translation/core/translation.coordinator.spec.ts+++ b/src/plugins/extensions/translation/core/translation.coordinator.spec.ts@@ -264,4 +264,45 @@ describe('TranslationCoordinator', () => {expect.objectContaining({ author: 'x@lid' }),);});++ it('engages the backstop instead of dropping when source != senderLang', async () => {+ const state = freshState({+ announced: true,+ active: true,+ participants: {+ 'liz@lid': { lang: 'es', source: 'pinned', enabled: true, samples: 5, updatedAt: 'x', pushName: 'Lizeth' },+ 'doug@lid': { lang: 'en', source: 'pinned', enabled: true, samples: 5, updatedAt: 'x', pushName: 'Doug' },+ },+ });+ const { store, gateway, translator, logger, mocks } = makeDeps(state);+ // Worst case: misrouted to Doug AND pushName also corrupted -> reconciliation can't help.+ mocks.detect.mockResolvedValue({ lang: 'es', confidence: 0.99 });+ mocks.translate.mockResolvedValue('I feel sick');+ const c = new TranslationCoordinator(translator, store, gateway, OPTS, logger);+ await c.handleMessage('s', msg({ author: 'doug@lid', pushName: 'Doug', body: 'Me siento mal' }));+ expect(mocks.warn).toHaveBeenCalledWith(+ 'target backstop engaged (possible misroute or cross-language write)',+ expect.objectContaining({ source: 'es' }),+ );+ expect(mocks.translate).toHaveBeenCalledWith('Me siento mal', 'es', 'en');+ expect(mocks.sendCombinedReply).toHaveBeenCalled();+ });++ it('does not warn or translate when the group speaks only the source language', async () => {+ const state = freshState({+ announced: true,+ active: true,+ participants: {+ 'a@lid': { lang: 'en', source: 'pinned', enabled: true, samples: 5, updatedAt: 'x', pushName: 'A' },+ 'b@lid': { lang: 'en', source: 'pinned', enabled: true, samples: 5, updatedAt: 'x', pushName: 'B' },+ },+ });+ const { store, gateway, translator, logger, mocks } = makeDeps(state);+ mocks.detect.mockResolvedValue({ lang: 'en', confidence: 0.99 });+ const c = new TranslationCoordinator(translator, store, gateway, OPTS, logger);+ await c.handleMessage('s', msg({ author: 'a@lid', pushName: 'A', body: 'Hello there' }));+ expect(mocks.translate).not.toHaveBeenCalled();+ expect(mocks.warn).not.toHaveBeenCalled();+ expect(mocks.sendCombinedReply).not.toHaveBeenCalled();+ });});diff --git a/src/plugins/extensions/translation/core/translation.coordinator.ts b/src/plugins/extensions/translation/core/translation.coordinator.tsindex 5965e5987..41b6e8acd 100644--- a/src/plugins/extensions/translation/core/translation.coordinator.ts+++ b/src/plugins/extensions/translation/core/translation.coordinator.ts@@ -99,10 +99,29 @@ export class TranslationCoordinator {const knownLangs = this.knownLanguages(state);const source = knownLangs.includes(detected) ? detected : (sender.lang ?? detected);- const targets = this.targetLanguages(state, source, sender.lang);+ let targets = this.targetLanguages(state, source, sender.lang);if (targets.length === 0) {- await this.store.save(state);- return;+ // Backstop: a real message detected in a known language must never be silently dropped due+ // to a sender/source mismatch (e.g. a misrouted @lid author keyed to the wrong participant).+ // Translate into every known language except the source — guarantees delivery.+ const backstop = knownLangs.filter(l => l !== source);+ if (backstop.length === 0) {+ this.logger.debug('no targets; group speaks only the source language', {+ action: 'translation_no_targets',+ source,+ });+ await this.store.save(state);+ return;+ }+ this.logger.warn('target backstop engaged (possible misroute or cross-language write)', {+ action: 'translation_backstop',+ author: msg.author,+ pushName: msg.pushName,+ source,+ senderLang: sender.lang,+ targets: backstop,+ });+ targets = backstop;}const settled = await Promise.allSettled(targets.map(t => this.translator.translate(text, source, t)));