mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-20 12:22:21 -06:00
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:
@@ -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';
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user