CognxSafeTrack commited on
Commit Β·
70901bb
1
Parent(s): aa52c48
fix(stt): resolve post-transcription silence and harden gateway detection
Browse files
apps/api/src/routes/internal.ts
CHANGED
|
@@ -142,15 +142,21 @@ export async function internalRoutes(fastify: FastifyInstance) {
|
|
| 142 |
}
|
| 143 |
}, async (request, reply) => {
|
| 144 |
const { phone, text, audioUrl } = request.body;
|
|
|
|
|
|
|
| 145 |
if (!phone || text === undefined) {
|
|
|
|
| 146 |
return reply.code(400).send({ error: 'phone and text are required' });
|
| 147 |
}
|
| 148 |
|
|
|
|
|
|
|
| 149 |
// Fire and await - ensuring the worker knows if it failed
|
| 150 |
try {
|
| 151 |
await WhatsAppService.handleIncomingMessage(phone, text, audioUrl);
|
|
|
|
| 152 |
} catch (err: any) {
|
| 153 |
-
request.log.error(`
|
| 154 |
return reply.code(500).send({ error: err.message });
|
| 155 |
}
|
| 156 |
|
|
|
|
| 142 |
}
|
| 143 |
}, async (request, reply) => {
|
| 144 |
const { phone, text, audioUrl } = request.body;
|
| 145 |
+
const traceId = `[INTERNAL-TX-${phone.slice(-4)}]`;
|
| 146 |
+
|
| 147 |
if (!phone || text === undefined) {
|
| 148 |
+
request.log.warn(`${traceId} Missing phone or text in handle-message request`);
|
| 149 |
return reply.code(400).send({ error: 'phone and text are required' });
|
| 150 |
}
|
| 151 |
|
| 152 |
+
request.log.info(`${traceId} Received transcribed text: "${text.substring(0, 100)}..."`);
|
| 153 |
+
|
| 154 |
// Fire and await - ensuring the worker knows if it failed
|
| 155 |
try {
|
| 156 |
await WhatsAppService.handleIncomingMessage(phone, text, audioUrl);
|
| 157 |
+
request.log.info(`${traceId} Successfully processed message`);
|
| 158 |
} catch (err: any) {
|
| 159 |
+
request.log.error(`${traceId} handleIncomingMessage error: ${err.message}`);
|
| 160 |
return reply.code(500).send({ error: err.message });
|
| 161 |
}
|
| 162 |
|
apps/api/src/routes/whatsapp.ts
CHANGED
|
@@ -113,7 +113,16 @@ export async function whatsappRoutes(fastify: FastifyInstance) {
|
|
| 113 |
|
| 114 |
// ββ 2. Forward to Railway Internal Worker (if configured as Gateway) ββ
|
| 115 |
const railwayInternalUrl = process.env.RAILWAY_INTERNAL_URL;
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
if (railwayInternalUrl && isGateway) {
|
| 119 |
// Loop protection: don't forward to yourself
|
|
@@ -122,7 +131,7 @@ export async function whatsappRoutes(fastify: FastifyInstance) {
|
|
| 122 |
request.log.warn('[WEBHOOK] Loop detected: RAILWAY_INTERNAL_URL matches self. Skipping forward.');
|
| 123 |
} else {
|
| 124 |
const targetUrl = `${railwayInternalUrl.replace(/\/$/, '')}/v1/internal/whatsapp/inbound`;
|
| 125 |
-
request.log.info(`[WEBHOOK]
|
| 126 |
|
| 127 |
try {
|
| 128 |
// Fire and forget (don't await) to ensure fast 200 response to Meta
|
|
@@ -139,7 +148,7 @@ export async function whatsappRoutes(fastify: FastifyInstance) {
|
|
| 139 |
request.log.error(`[WEBHOOK] Forward to Railway failed: ${err.message}`);
|
| 140 |
});
|
| 141 |
|
| 142 |
-
// π¨ CRITICAL: CRUCIAL EXIT POINT
|
| 143 |
return reply.code(200).send('EVENT_RECEIVED');
|
| 144 |
} catch (error: any) {
|
| 145 |
request.log.error(`[WEBHOOK] Forward throwing error: ${error?.message}`);
|
|
|
|
| 113 |
|
| 114 |
// ββ 2. Forward to Railway Internal Worker (if configured as Gateway) ββ
|
| 115 |
const railwayInternalUrl = process.env.RAILWAY_INTERNAL_URL;
|
| 116 |
+
|
| 117 |
+
// Hardened Gateway Detection:
|
| 118 |
+
// We are a gateway if:
|
| 119 |
+
// 1. IS_GATEWAY is explicitly true
|
| 120 |
+
// 2. We are running on Hugging Face (HF_SPACE_ID exists)
|
| 121 |
+
// 3. We have a RAILWAY_INTERNAL_URL but NOT a RAILWAY_STATIC_URL (not on Railway)
|
| 122 |
+
const isGateway =
|
| 123 |
+
process.env.IS_GATEWAY === 'true' ||
|
| 124 |
+
process.env.HF_SPACE_ID !== undefined ||
|
| 125 |
+
(!!railwayInternalUrl && !process.env.RAILWAY_STATIC_URL);
|
| 126 |
|
| 127 |
if (railwayInternalUrl && isGateway) {
|
| 128 |
// Loop protection: don't forward to yourself
|
|
|
|
| 131 |
request.log.warn('[WEBHOOK] Loop detected: RAILWAY_INTERNAL_URL matches self. Skipping forward.');
|
| 132 |
} else {
|
| 133 |
const targetUrl = `${railwayInternalUrl.replace(/\/$/, '')}/v1/internal/whatsapp/inbound`;
|
| 134 |
+
request.log.info(`[WEBHOOK] GATEWAY MODE: Forwarding payload to ${targetUrl}`);
|
| 135 |
|
| 136 |
try {
|
| 137 |
// Fire and forget (don't await) to ensure fast 200 response to Meta
|
|
|
|
| 148 |
request.log.error(`[WEBHOOK] Forward to Railway failed: ${err.message}`);
|
| 149 |
});
|
| 150 |
|
| 151 |
+
// π¨ CRITICAL: CRUCIAL EXIT POINT FOR GATEWAY
|
| 152 |
return reply.code(200).send('EVENT_RECEIVED');
|
| 153 |
} catch (error: any) {
|
| 154 |
request.log.error(`[WEBHOOK] Forward throwing error: ${error?.message}`);
|
apps/whatsapp-worker/src/index.ts
CHANGED
|
@@ -510,7 +510,22 @@ const worker = new Worker('whatsapp-queue', async (job: Job) => {
|
|
| 510 |
const traceId = `[STT-FLOW-${phone.slice(-4)}]`;
|
| 511 |
console.log(`${traceId} Forwarding to API handle-message...`);
|
| 512 |
|
| 513 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 514 |
method: 'POST',
|
| 515 |
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}` },
|
| 516 |
body: JSON.stringify({ phone, text: transcribedText, audioUrl })
|
|
@@ -518,7 +533,7 @@ const worker = new Worker('whatsapp-queue', async (job: Job) => {
|
|
| 518 |
|
| 519 |
if (!handleRes.ok) {
|
| 520 |
const errText = await handleRes.text();
|
| 521 |
-
console.error(`${traceId} API handle-message failed: ${errText}`);
|
| 522 |
throw new Error(`API handle-message failed: ${handleRes.status}`);
|
| 523 |
}
|
| 524 |
console.log(`${traceId} API handle-message success.`);
|
|
|
|
| 510 |
const traceId = `[STT-FLOW-${phone.slice(-4)}]`;
|
| 511 |
console.log(`${traceId} Forwarding to API handle-message...`);
|
| 512 |
|
| 513 |
+
// βββ Port Hardening: Handle HF Space 7860 vs Local 8080 βββββββββ
|
| 514 |
+
let finalApiUrl = AI_API_BASE_URL.replace(/\/$/, "");
|
| 515 |
+
if (finalApiUrl.includes('localhost:8080')) {
|
| 516 |
+
try {
|
| 517 |
+
// Quick check if 8080 is up, if not assume 7860 (Hugging Face)
|
| 518 |
+
const pingRes = await fetch('http://localhost:8080/health').catch(() => null);
|
| 519 |
+
if (!pingRes || !pingRes.ok) {
|
| 520 |
+
console.log(`${traceId} Local port 8080 not responding, trying 7860 (HF Default)...`);
|
| 521 |
+
finalApiUrl = finalApiUrl.replace('8080', '7860');
|
| 522 |
+
}
|
| 523 |
+
} catch (e) {
|
| 524 |
+
finalApiUrl = finalApiUrl.replace('8080', '7860');
|
| 525 |
+
}
|
| 526 |
+
}
|
| 527 |
+
|
| 528 |
+
const handleRes = await fetch(`${finalApiUrl}/v1/internal/handle-message`, {
|
| 529 |
method: 'POST',
|
| 530 |
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}` },
|
| 531 |
body: JSON.stringify({ phone, text: transcribedText, audioUrl })
|
|
|
|
| 533 |
|
| 534 |
if (!handleRes.ok) {
|
| 535 |
const errText = await handleRes.text();
|
| 536 |
+
console.error(`${traceId} API handle-message failed (HTTP ${handleRes.status}): ${errText}`);
|
| 537 |
throw new Error(`API handle-message failed: ${handleRes.status}`);
|
| 538 |
}
|
| 539 |
console.log(`${traceId} API handle-message success.`);
|