refactor(chatbot): unify keywordFinish type and enhance session handling

- Changed the type of `keywordFinish` from an array to a string in multiple DTOs and controller interfaces to simplify data handling.
- Updated the `BaseChatbotService` to include logic for updating session status to 'opened' and managing user responses more effectively.
- Refactored the media message handling in the `BaseChatbotService` to streamline the process and improve readability.
- Enhanced error logging across various services to ensure better traceability during operations.

This commit focuses on improving the structure and consistency of chatbot integrations while ensuring that session management is robust and user-friendly.
This commit is contained in:
Davidson Gomes
2025-05-21 17:02:24 -03:00
parent d673c83a93
commit f9567fbeaa
17 changed files with 819 additions and 4139 deletions

View File

@@ -1,4 +1,3 @@
import { IgnoreJidDto } from '@api/dto/chatbot.dto';
import { InstanceDto } from '@api/dto/instance.dto';
import { OpenaiCredsDto, OpenaiDto } from '@api/integrations/chatbot/openai/dto/openai.dto';
import { OpenaiService } from '@api/integrations/chatbot/openai/services/openai.service';

View File

@@ -306,7 +306,24 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
}
// Get thread ID from session or create new thread
const threadId = session.sessionId === remoteJid ? (await this.client.beta.threads.create()).id : session.sessionId;
let threadId = session.sessionId;
// Create a new thread if one doesn't exist or invalid format
if (!threadId || threadId === remoteJid) {
const newThread = await this.client.beta.threads.create();
threadId = newThread.id;
// Save the new thread ID to the session
await this.prismaRepository.integrationSession.update({
where: {
id: session.id,
},
data: {
sessionId: threadId,
},
});
this.logger.log(`Created new thread ID: ${threadId} for session: ${session.id}`);
}
// Add message to thread
await this.client.beta.threads.messages.create(threadId, messageData);
@@ -334,6 +351,7 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
}
// Extract the response text safely with type checking
let responseText = "I couldn't generate a proper response. Please try again.";
try {
const messages = response?.data || [];
if (messages.length > 0) {
@@ -341,7 +359,7 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
if (messageContent.length > 0) {
const textContent = messageContent[0];
if (textContent && 'text' in textContent && textContent.text && 'value' in textContent.text) {
return textContent.text.value;
responseText = textContent.text.value;
}
}
}
@@ -349,8 +367,20 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
this.logger.error(`Error extracting response text: ${error}`);
}
// Update session with the thread ID to ensure continuity
await this.prismaRepository.integrationSession.update({
where: {
id: session.id,
},
data: {
status: 'opened',
awaitUser: true,
sessionId: threadId, // Ensure thread ID is saved consistently
},
});
// Return fallback message if unable to extract text
return "I couldn't generate a proper response. Please try again.";
return responseText;
}
/**