CognxSafeTrack commited on
Commit ·
78e4fe8
1
Parent(s): bad47e7
fix: restore image interception and vision pipeline with [IMAGE-FLOW] logs
Browse files
apps/api/src/routes/whatsapp.ts
CHANGED
|
@@ -222,6 +222,28 @@ export async function whatsappRoutes(fastify: FastifyInstance) {
|
|
| 222 |
phone,
|
| 223 |
text: "⏳ J'analyse ton audio..."
|
| 224 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
}
|
| 226 |
}
|
| 227 |
}
|
|
|
|
| 222 |
phone,
|
| 223 |
text: "⏳ J'analyse ton audio..."
|
| 224 |
});
|
| 225 |
+
} else if (message.type === 'image' && message.image) {
|
| 226 |
+
console.log(`[IMAGE-FLOW] Image detected! ID: ${message.image.id}`);
|
| 227 |
+
const accessToken = process.env.WHATSAPP_ACCESS_TOKEN || undefined;
|
| 228 |
+
const { Queue } = await import('bullmq');
|
| 229 |
+
const Redis = (await import('ioredis')).default;
|
| 230 |
+
const conn = process.env.REDIS_URL
|
| 231 |
+
? new Redis(process.env.REDIS_URL, { maxRetriesPerRequest: null })
|
| 232 |
+
: new Redis({ host: process.env.REDIS_HOST || 'localhost', port: parseInt(process.env.REDIS_PORT || '6379'), maxRetriesPerRequest: null });
|
| 233 |
+
const q = new Queue('whatsapp-queue', { connection: conn as any });
|
| 234 |
+
|
| 235 |
+
console.log(`[IMAGE-FLOW] Enqueuing for download...`);
|
| 236 |
+
await q.add('download-media', {
|
| 237 |
+
mediaId: message.image.id,
|
| 238 |
+
mimeType: 'image/jpeg',
|
| 239 |
+
phone,
|
| 240 |
+
...(accessToken ? { accessToken } : {})
|
| 241 |
+
}, { priority: 1, attempts: 3, backoff: { type: 'exponential', delay: 2000 } });
|
| 242 |
+
|
| 243 |
+
await q.add('send-message-direct', {
|
| 244 |
+
phone,
|
| 245 |
+
text: "⏳ J'analyse ton image..."
|
| 246 |
+
});
|
| 247 |
}
|
| 248 |
}
|
| 249 |
}
|
apps/whatsapp-worker/src/index.ts
CHANGED
|
@@ -725,12 +725,11 @@ const worker = new Worker('whatsapp-queue', async (job: Job) => {
|
|
| 725 |
throw new Error(`Transcription failed HTTP ${transcribeRes.status}`); // throw so BullMQ retries
|
| 726 |
}
|
| 727 |
} else if (mimeType.startsWith('image/')) {
|
| 728 |
-
// 📸
|
| 729 |
-
console.log(`
|
| 730 |
-
// Provide a placeholder text
|
| 731 |
-
// For now, consistent with older logic: text = 'Image reçue'
|
| 732 |
await WhatsAppLogic.handleIncomingMessage(phone, 'Image reçue', undefined, audioUrl);
|
| 733 |
-
console.log(`
|
| 734 |
}
|
| 735 |
} catch (err: unknown) {
|
| 736 |
console.error(`[WORKER] download-media failed:`, err);
|
|
|
|
| 725 |
throw new Error(`Transcription failed HTTP ${transcribeRes.status}`); // throw so BullMQ retries
|
| 726 |
}
|
| 727 |
} else if (mimeType.startsWith('image/')) {
|
| 728 |
+
// 📸 IMAGE-FLOW (Requested Logs)
|
| 729 |
+
console.log(`[IMAGE-FLOW] Image detected for ${phone}. Processing via WhatsAppLogic...`);
|
| 730 |
+
// Provide a placeholder text for logs, image is the primary content.
|
|
|
|
| 731 |
await WhatsAppLogic.handleIncomingMessage(phone, 'Image reçue', undefined, audioUrl);
|
| 732 |
+
console.log(`[IMAGE-FLOW] Inbound image processing complete.`);
|
| 733 |
}
|
| 734 |
} catch (err: unknown) {
|
| 735 |
console.error(`[WORKER] download-media failed:`, err);
|
apps/whatsapp-worker/src/services/whatsapp-logic.ts
CHANGED
|
@@ -319,7 +319,13 @@ export class WhatsAppLogic {
|
|
| 319 |
['whatsapp', 'boutique', 'digital', 'physique', 'tel', 'e-commerce'].includes(normalizedText)
|
| 320 |
);
|
| 321 |
|
| 322 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
const minWordCount = shouldBypassGuardrail ? 1 : 3;
|
| 324 |
|
| 325 |
if (wordCount < minWordCount) {
|
|
|
|
| 319 |
['whatsapp', 'boutique', 'digital', 'physique', 'tel', 'e-commerce'].includes(normalizedText)
|
| 320 |
);
|
| 321 |
|
| 322 |
+
const isDay8Vision = !!imageUrl && activeEnrollment.currentDay === 8;
|
| 323 |
+
const shouldBypassGuardrail = isButtonChoice || isDay7Special || isDay8Vision;
|
| 324 |
+
|
| 325 |
+
if (isDay8Vision) {
|
| 326 |
+
console.log(`[IMAGE-FLOW] 📸 Bypassing wordcount for image response on Day 8 for User ${user.id}`);
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
const minWordCount = shouldBypassGuardrail ? 1 : 3;
|
| 330 |
|
| 331 |
if (wordCount < minWordCount) {
|