la transcripcion de audios funciona incluso cuando se reinicia el objeto conversation
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { WhatsAppMessage, Msg, Conversation } from "../types";
|
import { WhatsAppMessage, Msg, Conversation } from "./types";
|
||||||
import { transcribeAudioMessage } from "../transcribeAudioMessage";
|
import { transcribeAudioMessage } from "./transcribeAudioMessage";
|
||||||
import axios from 'axios'; // Needed for sending error messages
|
import axios from 'axios'; // Needed for sending error messages
|
||||||
|
|
||||||
export const mapWhatsAppMessageToMsg = (m: WhatsAppMessage): Msg => {
|
export const mapWhatsAppMessageToMsg = (m: WhatsAppMessage): Msg => {
|
||||||
@@ -30,9 +30,9 @@ export const processIncomingMessageForConversation = (
|
|||||||
// Avoid duplicates if multiple webhook events deliver the same message
|
// Avoid duplicates if multiple webhook events deliver the same message
|
||||||
if (!conversation.messages.some((m) => m.id === mappedMsg.id)) {
|
if (!conversation.messages.some((m) => m.id === mappedMsg.id)) {
|
||||||
conversation.messages.push(mappedMsg);
|
conversation.messages.push(mappedMsg);
|
||||||
if (conversation.messages.length > 20) {
|
// if (conversation.messages.length > 20) {
|
||||||
conversation.messages.shift(); // Keep only the last 20 messages
|
// conversation.messages.shift(); // Keep only the last 20 messages
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add new participants if necessary
|
// Add new participants if necessary
|
||||||
@@ -59,12 +59,8 @@ export const handleAudioMessageTranscription = async (
|
|||||||
const audioUrl = message.clientUrl || message.deprecatedMms3Url;
|
const audioUrl = message.clientUrl || message.deprecatedMms3Url;
|
||||||
if (!audioUrl) {
|
if (!audioUrl) {
|
||||||
console.error('[MessageProcessor] No audio URL found for PTT message');
|
console.error('[MessageProcessor] No audio URL found for PTT message');
|
||||||
// Not sending a message back here as webhook.ts will continue processing
|
throw new Error('No audio URL found for PTT message');
|
||||||
// and potentially send a generic error or a handler-specific message.
|
|
||||||
// Alternatively, we could throw an error to be caught by the webhook.
|
|
||||||
return message; // Return original message
|
|
||||||
}
|
}
|
||||||
console.log('[MessageProcessor] 🎤 Audio message detected', audioUrl);
|
|
||||||
try {
|
try {
|
||||||
const transcript = await transcribeAudioMessage(message);
|
const transcript = await transcribeAudioMessage(message);
|
||||||
console.log('[MessageProcessor] 📝 Transcription:', transcript);
|
console.log('[MessageProcessor] 📝 Transcription:', transcript);
|
||||||
@@ -73,7 +69,7 @@ export const handleAudioMessageTranscription = async (
|
|||||||
} catch (transcriptionError: any) {
|
} catch (transcriptionError: any) {
|
||||||
console.error('[MessageProcessor] Error in transcription:', transcriptionError.message);
|
console.error('[MessageProcessor] Error in transcription:', transcriptionError.message);
|
||||||
const reply =
|
const reply =
|
||||||
"I received an audio message, but I couldn't transcribe it. Please send the transcript manually.";
|
"I received an audio message, but I couldn't transcribe it. Please send the transcript manually. ⚠️⚠️";
|
||||||
// Need from/chatId to send reply
|
// Need from/chatId to send reply
|
||||||
const chatId = message.chatId || message.from;
|
const chatId = message.chatId || message.from;
|
||||||
if (chatId && openWaUrl) {
|
if (chatId && openWaUrl) {
|
||||||
|
|||||||
@@ -22,12 +22,13 @@ async function loadMessages(
|
|||||||
|
|
||||||
export async function getConversation(
|
export async function getConversation(
|
||||||
chatId: string,
|
chatId: string,
|
||||||
openWaUrl: string
|
openWaUrl: string,
|
||||||
|
incommingMessage?: WhatsAppMessage
|
||||||
): Promise<Conversation> {
|
): Promise<Conversation> {
|
||||||
console.log(`[conversationStore] Retrieving conversation for ${chatId}`);
|
console.log(`[conversationStore] Retrieving conversation for ${chatId}`);
|
||||||
let conv = conversations.get(chatId);
|
let conv = conversations.get(chatId);
|
||||||
if (!conv) {
|
if (!conv) {
|
||||||
conv = await buildConversation(chatId, openWaUrl);
|
conv = await buildConversation(chatId, openWaUrl, incommingMessage);
|
||||||
}
|
}
|
||||||
return conv;
|
return conv;
|
||||||
}
|
}
|
||||||
@@ -39,13 +40,14 @@ export function listConversations(): Conversation[] {
|
|||||||
|
|
||||||
export async function buildConversation(
|
export async function buildConversation(
|
||||||
chatId: string,
|
chatId: string,
|
||||||
openWaUrl: string
|
openWaUrl: string,
|
||||||
|
incommingMessage?: WhatsAppMessage
|
||||||
): Promise<Conversation> {
|
): Promise<Conversation> {
|
||||||
console.log(`[conversationStore] Building conversation for ${chatId}`);
|
console.log(`[conversationStore] Building conversation for ${chatId}`);
|
||||||
const rawMessages = await loadMessages(chatId, openWaUrl);
|
const rawMessages = await loadMessages(chatId, openWaUrl);
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|
||||||
const first = rawMessages[0];
|
const first = rawMessages[0] = incommingMessage || rawMessages[0];
|
||||||
const chat = first?.chat;
|
const chat = first?.chat;
|
||||||
const title = chat?.formattedTitle || chat?.name || chatId;
|
const title = chat?.formattedTitle || chat?.name || chatId;
|
||||||
const isGroup = chat?.isGroup || false;
|
const isGroup = chat?.isGroup || false;
|
||||||
@@ -111,14 +113,14 @@ export async function addMessageToConversation(
|
|||||||
msg: WhatsAppMessage,
|
msg: WhatsAppMessage,
|
||||||
openWaUrl: string
|
openWaUrl: string
|
||||||
): Promise<Conversation> {
|
): Promise<Conversation> {
|
||||||
console.log(`[conversationStore] Adding message to ${chatId}`);
|
const conv = await getConversation(chatId, openWaUrl, msg);
|
||||||
const conv = await getConversation(chatId, openWaUrl);
|
|
||||||
|
|
||||||
// Delegate message processing to the new function
|
// Delegate message processing to the new function
|
||||||
// processIncomingMessageForConversation modifies `conv` directly
|
// processIncomingMessageForConversation modifies `conv` directly
|
||||||
processIncomingMessageForConversation(conv, msg);
|
processIncomingMessageForConversation(conv, msg);
|
||||||
|
|
||||||
// Ensure the conversation is updated in the map (though it's by reference as conv is an object)
|
// Ensure the conversation is updated in the map (though it's by reference as conv is an object)
|
||||||
|
|
||||||
conversations.set(chatId, conv);
|
conversations.set(chatId, conv);
|
||||||
|
|
||||||
return conv;
|
return conv;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { getHandler } from './chatHandlers';
|
|||||||
import { addMessageToConversation } from './store/conversation';
|
import { addMessageToConversation } from './store/conversation';
|
||||||
import { WhatsAppMessage, Conversation } from './types';
|
import { WhatsAppMessage, Conversation } from './types';
|
||||||
// import { transcribeAudioMessage } from './transcribeAudioMessage'; // Moved to messageProcessor
|
// import { transcribeAudioMessage } from './transcribeAudioMessage'; // Moved to messageProcessor
|
||||||
import { handleAudioMessageTranscription } from '../messageProcessor'; // Added import
|
import { handleAudioMessageTranscription } from './messageProcessor'; // Added import
|
||||||
|
|
||||||
export interface WebhookConfig {
|
export interface WebhookConfig {
|
||||||
API_URL: string;
|
API_URL: string;
|
||||||
@@ -50,8 +50,7 @@ export function registerWebhookRoutes(
|
|||||||
|
|
||||||
// Audio message handling
|
// Audio message handling
|
||||||
try {
|
try {
|
||||||
// message object will be updated by reference if transcription occurs
|
message = await handleAudioMessageTranscription(message, openWaUrl);
|
||||||
await handleAudioMessageTranscription(message, openWaUrl);
|
|
||||||
} catch (transcriptionError: any) {
|
} catch (transcriptionError: any) {
|
||||||
// Log the error already handled by handleAudioMessageTranscription (which also sends a message to user)
|
// Log the error already handled by handleAudioMessageTranscription (which also sends a message to user)
|
||||||
console.error('[Webhook] Transcription failed, stopping further processing for this message.', transcriptionError.message);
|
console.error('[Webhook] Transcription failed, stopping further processing for this message.', transcriptionError.message);
|
||||||
@@ -59,7 +58,6 @@ export function registerWebhookRoutes(
|
|||||||
return res.sendStatus(200);
|
return res.sendStatus(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log(message); // For debugging, check if message.body is updated
|
|
||||||
let conv: Conversation | undefined;
|
let conv: Conversation | undefined;
|
||||||
if (chatId) {
|
if (chatId) {
|
||||||
try {
|
try {
|
||||||
@@ -73,6 +71,8 @@ export function registerWebhookRoutes(
|
|||||||
if (!handler) throw new Error('No handler configured');
|
if (!handler) throw new Error('No handler configured');
|
||||||
let reply: string;
|
let reply: string;
|
||||||
if (typeof handler === 'string') {
|
if (typeof handler === 'string') {
|
||||||
|
console.log(`🔗 Calling agent at ${handler} for conversation ${chatId}\n`);
|
||||||
|
|
||||||
const agentRes = await axios.post(handler, { conversation: conv });
|
const agentRes = await axios.post(handler, { conversation: conv });
|
||||||
reply = agentRes.data.reply || agentRes.data;
|
reply = agentRes.data.reply || agentRes.data;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user