From 373a531e88a57b652c57d3168b5dda70133557a5 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Fri, 23 May 2025 20:37:01 -0300 Subject: [PATCH] fix: update logging and message handling in EvoaiService This commit modifies the logging messages in the `EvoaiService` to use the correct service name "EvoAI" instead of "Dify" for better clarity. Additionally, it refines the message handling logic by changing the way message IDs are generated and updating the payload structure sent to the API. The extraction of the message from the response artifacts has also been improved to ensure that the correct message is retrieved from the response data. Changes include: - Updated logging statements to reflect the correct service name. - Changed the message ID generation to use a shorter UUID substring. - Adjusted the payload structure to include `contextId` and `messageId`. - Enhanced message extraction logic from the response artifacts. These changes enhance the clarity of logs and improve the robustness of message handling in the service. --- .../chatbot/evoai/services/evoai.service.ts | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/api/integrations/chatbot/evoai/services/evoai.service.ts b/src/api/integrations/chatbot/evoai/services/evoai.service.ts index 952a5b5b..868e3873 100644 --- a/src/api/integrations/chatbot/evoai/services/evoai.service.ts +++ b/src/api/integrations/chatbot/evoai/services/evoai.service.ts @@ -50,7 +50,7 @@ export class EvoaiService extends BaseChatbotService { // Check if this is an audio message that we should try to transcribe if (this.isAudioMessage(content) && msg) { try { - this.logger.debug(`[Dify] Downloading audio for Whisper transcription`); + this.logger.debug(`[EvoAI] Downloading audio for Whisper transcription`); const transcription = await this.openaiService.speechToText(msg); if (transcription) { contentProcessed = transcription; @@ -58,7 +58,7 @@ export class EvoaiService extends BaseChatbotService { contentProcessed = '[Audio message could not be transcribed]'; } } catch (err) { - this.logger.error(`[Dify] Failed to transcribe audio: ${err}`); + this.logger.error(`[EvoAI] Failed to transcribe audio: ${err}`); contentProcessed = '[Audio message could not be transcribed]'; } } @@ -85,8 +85,8 @@ export class EvoaiService extends BaseChatbotService { this.logger.debug(`[EvoAI] Sending message to bot with content: ${content}`); const endpoint: string = evoai.agentUrl; - const callId = `call-${uuidv4()}`; - const taskId = `task-${uuidv4()}`; + const callId = `req-${uuidv4().substring(0, 8)}`; + const messageId = uuidv4(); // Prepare message parts const parts = [ @@ -111,8 +111,8 @@ export class EvoaiService extends BaseChatbotService { type: 'file', file: { name: fileName, - bytes: fileContent, mimeType: 'image/jpeg', + bytes: fileContent, }, } as any); } catch (fileErr) { @@ -122,16 +122,16 @@ export class EvoaiService extends BaseChatbotService { const payload = { jsonrpc: '2.0', - method: 'tasks/send', + id: callId, + method: 'message/send', params: { + contextId: session.sessionId, message: { role: 'user', parts, + messageId: messageId, }, - sessionId: session.sessionId, - id: taskId, }, - id: callId, }; this.logger.debug(`[EvoAI] Sending request to: ${endpoint}`); @@ -159,17 +159,23 @@ export class EvoaiService extends BaseChatbotService { }, }); - this.logger.debug(`[EvoAI] Response: ${JSON.stringify(response.data.status)}`); + this.logger.debug(`[EvoAI] Response: ${JSON.stringify(response.data)}`); if (instance.integration === Integration.WHATSAPP_BAILEYS) await instance.client.sendPresenceUpdate('paused', remoteJid); let message = undefined; const result = response?.data?.result; - if (result?.status?.message?.parts && Array.isArray(result.status.message.parts)) { - const textPart = result.status.message.parts.find((p) => p.type === 'text' && p.text); - if (textPart) message = textPart.text; + + // Extract message from artifacts array + if (result?.artifacts && Array.isArray(result.artifacts) && result.artifacts.length > 0) { + const artifact = result.artifacts[0]; + if (artifact?.parts && Array.isArray(artifact.parts)) { + const textPart = artifact.parts.find((p) => p.type === 'text' && p.text); + if (textPart) message = textPart.text; + } } + this.logger.debug(`[EvoAI] Extracted message to send: ${message}`); const conversationId = session.sessionId;