-
Notifications
You must be signed in to change notification settings - Fork 3.9k
refactor: improve chatbot integrations #1508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This commit refactors the TypebotController and TypebotService to streamline the processTypebot method, aligning it with the base class pattern. It reduces complexity by consolidating parameters and improving readability. Additionally, it updates the TypebotDto and TypebotSettingDto to remove unused properties, enhancing code clarity and maintainability.
… management This commit refactors the OpenAIController and OpenAIService to improve credential handling and error management. Key changes include: - Added checks to prevent duplicate API keys and names during bot creation. - Updated the getModels method to accept an optional credential ID for more flexible credential usage. - Enhanced error handling with specific BadRequestException messages for better clarity. - Removed unused methods and streamlined the speech-to-text functionality to utilize instance-specific settings. These improvements enhance the maintainability and usability of the OpenAI integration.
This commit enhances the N8n integration by refining session management and validation logic. Key changes include: - Added error handling for session creation failures in the BaseChatbotService. - Removed unused methods and properties in N8nService and N8nDto to streamline the codebase. - Updated N8n schema to enforce required fields and improve validation checks. - Simplified message processing logic to utilize base class methods, enhancing maintainability. These improvements contribute to a more robust and efficient N8n integration.
…alidation This commit refines the Flowise integration by enhancing configuration management and validation logic. Key changes include: - Reordered parameters in the FlowiseService constructor for consistency. - Updated FlowiseController to utilize the configService for integration enablement checks. - Simplified FlowiseDto and FlowiseSettingDto by removing unused properties. - Enhanced validation logic in flowise.schema.ts to include new fields. - Improved error handling in the createBot method to prevent duplicate entries. These updates contribute to a more robust and maintainable Flowise integration.
…age handling This commit refines the Evoai integration by updating the service and controller logic for better functionality and maintainability. Key changes include: - Added the `openaiService` as a parameter in the EvoaiService constructor for improved dependency management. - Enhanced the createBot method in EvoaiController to include EvoAI-specific validation and duplicate checks. - Updated EvoaiDto and EvoaiSettingDto to remove unnecessary comments and add a fallback property. - Refined the message processing logic in EvoaiService to handle audio messages more effectively and improve logging clarity. - Adjusted the schema for Evoai settings to rename `evoaiIdFallback` to `botIdFallback` for better clarity. These updates contribute to a more robust and maintainable Evoai integration.
…ge processing This commit refines the Dify integration by updating the controller and service logic for better functionality and maintainability. Key changes include: - Added Dify-specific validation in the createBot method to prevent duplicate entries. - Simplified comments for clarity and removed unused methods in DifyController. - Enhanced message processing in DifyService to handle audio messages more effectively and improve error handling. - Updated DifyDto and DifySettingDto to streamline properties and improve clarity. These updates contribute to a more robust and maintainable Dify integration.
Reviewer's GuideThis PR refactors each chatbot integration to leverage the shared BaseChatbotService and consolidates repetitive logic—especially around audio transcription, message sending, session creation, controllers, DTOs, and validation—into unified implementations, improving code reuse and reducing boilerplate. Sequence Diagram for Audio Message Processing via DifyService and OpenaiServicesequenceDiagram
actor User
participant ChatPlatform
participant DifyService
participant OpenaiService
participant DifyAPI
User->>ChatPlatform: Sends audio message
ChatPlatform->>DifyService: Forwards audio message (msg, instance)
DifyService->>OpenaiService: speechToText(msg, instance)
OpenaiService->>OpenaiService: Download/process audio buffer
OpenaiService->>DifyAPI: Request transcription (OpenAI Whisper)
DifyAPI-->>OpenaiService: Return transcription
OpenaiService-->>DifyService: Return transcription text
DifyService->>DifyService: processedContent = "[audio] " + transcription
DifyService->>DifyAPI: POST /chat-messages (query: processedContent)
DifyAPI-->>DifyService: Bot response
DifyService->>ChatPlatform: Send reply message
ChatPlatform-->>User: Delivers reply
Sequence Diagram for Message Processing and Session HandlingsequenceDiagram
actor User
participant ChatPlatform
participant ChatbotController
participant BaseChatbotService
participant SpecificBotService
participant PrismaRepository
participant ExternalBotAPI
User->>ChatPlatform: Sends message
ChatPlatform->>ChatbotController: routeMessage(instance, ...)
ChatbotController->>BaseChatbotService: process(instance, remoteJid, bot, session, settings, content, ...)
BaseChatbotService->>BaseChatbotService: Check/Get session
alt No existing session
BaseChatbotService->>BaseChatbotService: createNewSession(instanceInfo, sessionData, botType)
BaseChatbotService->>PrismaRepository: Save new session
PrismaRepository-->>BaseChatbotService: New session created
end
BaseChatbotService->>SpecificBotService: sendMessageToBot(instance, session, settings, bot, ...)
SpecificBotService->>SpecificBotService: Prepare payload (e.g., transcribe audio if needed)
SpecificBotService->>ExternalBotAPI: Send processed message
ExternalBotAPI-->>SpecificBotService: Receive bot response
SpecificBotService->>BaseChatbotService: sendMessageWhatsApp(instance, remoteJid, responseText, settings)
BaseChatbotService->>ChatPlatform: Send reply to user
ChatPlatform-->>User: Deliver reply
Entity Relationship Diagram for N8N Schema UpdateserDiagram
n8nSchema {
boolean enabled "Required"
string description
string webhookUrl "Required"
string basicAuthUser
string basicAuthPassword "Renamed from basicAuthPass"
string triggerType "New, Required, Enum: all, keyword, none, advanced"
string triggerOperator "New, Enum: equals, contains, ..."
string triggerValue "New"
integer expire "New"
string keywordFinish "New"
integer delayMessage "New"
string unknownMessage "New"
boolean listeningFromMe "New"
boolean stopBotFromMe "New"
boolean keepOpen "New"
integer debounceTime "New"
array ignoreJids "New"
boolean splitMessages "New"
integer timePerChar "New"
}
n8nSettingSchema {
integer expire "Required"
string keywordFinish "Required"
integer delayMessage "Required"
string unknownMessage "Required"
boolean listeningFromMe "Required"
boolean stopBotFromMe "Required"
boolean keepOpen "Required"
integer debounceTime "Required"
array ignoreJids "Required"
string botIdFallback "Renamed from n8nIdFallback"
boolean splitMessages "Required"
integer timePerChar "Required"
}
n8nStatusSchema {
string remoteJid "Required"
string status "Required, Enum: opened, closed, paused, delete (order changed)"
}
n8nIgnoreJidSchema {
string remoteJid "Required"
string action "Required, Enum: add, remove"
}
note "Shows key field changes and additions to N8N JSON schemas based on the diff."
Class Diagram for Chatbot Service RefactoringclassDiagram
class BaseChatbotService {
+String serviceName
+ConfigService configService
+PrismaRepository prismaRepository
+WAMonitoringService waMonitor
+process(instance, remoteJid, bot, session, settings, content, pushName, msg)
+createNewSession(instanceInfo, sessionData, botType): IntegrationSession
+sendMessageWhatsApp(instance, remoteJid, message, settings)
+isAudioMessage(content): boolean
+isImageMessage(content): boolean
#abstract getBotType(): string
#abstract sendMessageToBot(instance, session, settings, bot, remoteJid, pushName, content, msg): Promise~void~
}
class DifyService {
+OpenaiService openaiService
+constructor(waMonitor, prisma, config, openai)
+getBotType(): string
+sendMessageToBot(instance, session, settings, dify, remoteJid, pushName, content, msg): Promise~void~
-createNewSession(instance, data) "Removed"
}
class N8nService {
+OpenaiService openaiService
+constructor(waMonitor, prisma, config, openai)
+getBotType(): string
+sendMessageToBot(instance, session, settings, n8n, remoteJid, pushName, content, msg): Promise~void~
-createBot() "Removed"
-findBots() "Removed"
-fetchBot() "Removed"
-updateBot() "Removed"
-deleteBot() "Removed"
-createNewSession() "Removed"
-sendMessageWhatsApp() "Removed"
}
class OpenaiService {
+constructor(waMonitor, prisma, config)
+getBotType(): string
+speechToText(msg, instance): Promise~string~ "Refactored"
+sendMessageToBot(instance, session, settings, bot, remoteJid, pushName, content, msg): Promise~void~
-createNewSession() "Removed"
}
class FlowiseService {
+OpenaiService openaiService
+constructor(waMonitor, prisma, config, openai) "Updated signature"
+getBotType(): string
+processBot(instance, remoteJid, bot, session, settings, content, pushName, msg) "Added"
+sendMessageToBot(instance, session, settings, bot, remoteJid, pushName, content, msg): Promise~void~
}
class EvoaiService {
+OpenaiService openaiService
+constructor(waMonitor, prisma, config, openai) "Updated signature"
+getBotType(): string
+sendMessageToBot(instance, session, settings, evoai, remoteJid, pushName, content, msg): Promise~void~
-process() "Removed"
-createNewSession() "Removed"
}
BaseChatbotService <|-- DifyService
BaseChatbotService <|-- N8nService
BaseChatbotService <|-- OpenaiService
BaseChatbotService <|-- FlowiseService
BaseChatbotService <|-- EvoaiService
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
…tbot controllers This commit refines the Flowise and Typebot integrations by simplifying the integration enablement checks in their respective controllers. Key changes include: - Consolidated the integration checks in the createBot method of FlowiseController and startBot method of TypebotController for improved readability. - Removed unnecessary line breaks to enhance code clarity. These updates contribute to a cleaner and more maintainable codebase for chatbot integrations.
This commit updates the asset references in the index.html file to point to new CSS and JS files. Key changes include: - Replaced the old JavaScript file `index-mxi8bQ4k.js` with `index-D-oOjDYe.js`. - Updated the CSS file reference from `index-DNOCacL_.css` to the new `index-CXH2BdD4.css`. - Removed the old CSS and JS files to clean up the codebase. These updates ensure that the application uses the latest styles and scripts, contributing to improved performance and maintainability.
…ot services - Add YAML file loader to tsup.config.ts to fix build compilation errors - Fix OpenAI speechToText method signature across all chatbot services - Correct DifyService constructor parameter order in server.module.ts and channel.service.ts - Add missing OpenAI service dependency injection to EvoaiService - Standardize audio transcription logic in FlowiseService to match N8N implementation - Fix speechToText calls in WhatsApp Baileys and Evolution channel services - Ensure consistent error handling and parameter passing for audio processing This resolves the "Cannot read properties of undefined" errors and ensures all chatbot integrations (OpenAI, N8N, Flowise, EvoAI, Dify, EvolutionBot) properly handle audio message transcription using OpenAI Whisper.
This commit refines the EvolutionBot integration by reordering constructor parameters for consistency and removing unused properties from the EvolutionBotDto and EvolutionBotSettingDto classes. Key changes include: - Adjusted the parameter order in the EvolutionBotService constructor for improved clarity. - Streamlined the EvolutionBotDto and EvolutionBotSettingDto by eliminating unnecessary fields. These updates enhance the maintainability and readability of the EvolutionBot integration.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Refactored chatbot integrations to better leverage base classes and improve code reuse.
Summary by Sourcery
Refactor chatbot integrations to consolidate common logic in BaseChatbotService for session management, message sending, audio transcription and media handling, remove duplication across dify, n8n, openai, flowise, evoai and typebot modules, clean up DTOs and controllers to use base class implementations, update validation schemas, and introduce Flowise enablement toggle in configuration.
Bug Fixes:
Enhancements:
Chores: