Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(translation): log swallowed translate failures + per-message decision
dallascyclist54c6c1a1e2c9
diff --git a/src/plugins/extensions/translation/core/translation.coordinator.spec.ts b/src/plugins/extensions/translation/core/translation.coordinator.spec.tsindex c320926ae..15b5adb2f 100644--- a/src/plugins/extensions/translation/core/translation.coordinator.spec.ts+++ b/src/plugins/extensions/translation/core/translation.coordinator.spec.ts@@ -305,4 +305,45 @@ describe('TranslationCoordinator', () => {expect(mocks.warn).not.toHaveBeenCalled();expect(mocks.sendCombinedReply).not.toHaveBeenCalled();});++ it('warns on a failed translate call and still delivers the successful targets', async () => {+ const state = freshState({+ announced: true,+ active: true,+ participants: {+ 's1@lid': { lang: 'en', source: 'pinned', enabled: true, samples: 5, updatedAt: 'x', pushName: 'S1' },+ 's2@lid': { lang: 'es', source: 'pinned', enabled: true, samples: 5, updatedAt: 'x', pushName: 'S2' },+ 's3@lid': { lang: 'fr', source: 'pinned', enabled: true, samples: 5, updatedAt: 'x', pushName: 'S3' },+ },+ });+ const { store, gateway, translator, logger, mocks } = makeDeps(state);+ mocks.detect.mockResolvedValue({ lang: 'en', confidence: 0.99 });+ mocks.translate.mockImplementation((_t: string, _s: string, target: string) =>+ target === 'fr' ? Promise.reject(new Error('boom')) : Promise.resolve('Hola'),+ );+ const c = new TranslationCoordinator(translator, store, gateway, OPTS, logger);+ await c.handleMessage('s', msg({ author: 's1@lid', pushName: 'S1', body: 'Hello everyone' }));+ expect(mocks.warn).toHaveBeenCalledWith('translate call failed', expect.objectContaining({ target: 'fr' }));+ expect(mocks.sendCombinedReply).toHaveBeenCalledWith('s', '[email protected]', 'M1', expect.stringContaining('Hola'));+ });++ it('emits a decision debug log for each translated message', async () => {+ const state = freshState({+ announced: true,+ active: true,+ participants: {+ '[email protected]': { lang: 'en', source: 'pinned', enabled: true, samples: 2, updatedAt: 'x', pushName: 'D' },+ '[email protected]': { lang: 'es', source: 'pinned', enabled: true, samples: 2, updatedAt: 'x', pushName: 'L' },+ },+ });+ const { store, gateway, translator, logger, mocks } = makeDeps(state);+ mocks.detect.mockResolvedValue({ lang: 'en', confidence: 0.99 });+ mocks.translate.mockResolvedValue('Hola');+ const c = new TranslationCoordinator(translator, store, gateway, OPTS, logger);+ await c.handleMessage('s', msg({ author: '[email protected]', pushName: 'D', body: 'Hello' }));+ expect(mocks.debug).toHaveBeenCalledWith(+ 'translate decision',+ expect.objectContaining({ detected: 'en', source: 'en', sent: 1 }),+ );+ });});diff --git a/src/plugins/extensions/translation/core/translation.coordinator.ts b/src/plugins/extensions/translation/core/translation.coordinator.tsindex 41b6e8acd..82f695b61 100644--- a/src/plugins/extensions/translation/core/translation.coordinator.ts+++ b/src/plugins/extensions/translation/core/translation.coordinator.ts@@ -127,7 +127,29 @@ export class TranslationCoordinator {const settled = await Promise.allSettled(targets.map(t => this.translator.translate(text, source, t)));const translations: Translation[] = [];settled.forEach((r, i) => {- if (r.status === 'fulfilled') translations.push({ lang: targets[i], text: r.value });+ if (r.status === 'fulfilled') {+ translations.push({ lang: targets[i], text: r.value });+ } else {+ this.logger.warn('translate call failed', {+ action: 'translation_translate_failed',+ source,+ target: targets[i],+ error: String(r.reason),+ });+ }+ });++ this.logger.debug('translate decision', {+ action: 'translation_decision',+ author: msg.author,+ resolvedKey: senderKey,+ pushName: msg.pushName,+ detected,+ source,+ senderLang: sender.lang,+ knownLangs,+ targets,+ sent: translations.length,});if (translations.length > 0) {