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