-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Refatoração da funcionalidade de chatbots (em andamento) #1482
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
DavidsonGomes
merged 2 commits into
EvolutionAPI:develop
from
gomessguii:fix/message-query
May 21, 2025
Merged
Refatoração da funcionalidade de chatbots (em andamento) #1482
DavidsonGomes
merged 2 commits into
EvolutionAPI:develop
from
gomessguii:fix/message-query
May 21, 2025
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
…on capabilities - Introduced a base structure for chatbot integrations, including BaseChatbotController and BaseChatbotService. - Added common DTOs for chatbot settings and data to streamline integration processes. - Updated existing chatbot controllers (Dify, Evoai, N8n) to extend from the new base classes, improving code reusability and maintainability. - Enhanced media message handling across integrations, including audio transcription capabilities using OpenAI's Whisper API. - Refactored service methods to accommodate new message structures and improve error handling.
… transcription handling - Updated OpenaiService and related classes to enhance the initialization process by ensuring the correct order of parameters. - Simplified audio message handling by consolidating transcription logic and improving error handling. - Refactored the OpenaiController to utilize the new structure, ensuring better integration with the base chatbot framework. - Enhanced logging for better traceability during audio processing and API interactions.
Reviewer's GuideThis PR centralizes duplicated chatbot logic into reusable base classes, refactors each integration’s controllers and services to extend these abstractions, and updates DTOs and channel code to conform to the new unified architecture. Sequence Diagram for OpenaiService Audio Transcription (speechToText)sequenceDiagram
participant CS as ChannelService (e.g., BaileysStartupService)
participant OS as OpenaiService
participant ConfS as ConfigService
participant Axios as HTTP Client (axios)
participant OAI as OpenAI API
CS->>OS: speechToText(msgOrBuffer, updateMediaMessage?)
activate OS
OS->>OS: getAudioBufferFromMsg(msgOrBuffer, updateMediaMessage?)
OS-->>OS: audioBuffer
OS->>OS: processAudioTranscription(audioBuffer)
activate OS
alt client already initialized with API key
note right of OS: Use existing API key from client
else
OS->>ConfS: get("OPENAI").API_KEY
ConfS-->>OS: apiKey
end
OS->>Axios: POST /v1/audio/transcriptions (formData with audioBuffer, model, lang)
activate Axios
Axios->>OAI: Transcribe Audio Request
OAI-->>Axios: Transcription Response
Axios-->>OS: { text: "transcribed_text" }
deactivate Axios
OS-->>OS: "transcribed_text"
deactivate OS
OS-->>CS: "transcribed_text"
deactivate OS
Class Diagram for Chatbot RefactoringclassDiagram
class BaseChatbotDto {
+enabled: boolean
+description: string
+expire: number
+keywordFinish: string[]
+delayMessage: number
+unknownMessage: string
+listeningFromMe: boolean
+stopBotFromMe: boolean
+keepOpen: boolean
+debounceTime: number
+triggerType: TriggerType
+triggerOperator: TriggerOperator
+triggerValue: string
+ignoreJids: string[]
+splitMessages: boolean
+timePerChar: number
}
class OpenaiDto {
+openaiCredsId: string
+botType: string
+assistantId: string
+functionUrl: string
+model: string
+systemMessages: string[]
+assistantMessages: string[]
+userMessages: string[]
+maxTokens: number
}
class DifyDto {
+botType: $Enums.DifyBotType
+apiUrl: string
+apiKey: string
}
BaseChatbotDto <|-- OpenaiDto
BaseChatbotDto <|-- DifyDto
class BaseChatbotSettingDto {
+expire: number
+keywordFinish: string[]
+delayMessage: number
+unknownMessage: string
+listeningFromMe: boolean
+stopBotFromMe: boolean
+keepOpen: boolean
+debounceTime: number
+ignoreJids: string[]
+splitMessages: boolean
+timePerChar: number
+fallbackId: string
}
class OpenaiSettingDto {
+openaiCredsId: string
+openaiIdFallback: string
+speechToText: boolean
}
BaseChatbotSettingDto <|-- OpenaiSettingDto
class BaseChatbotService {
<<abstract>>
#prismaRepository: PrismaRepository
#configService: ConfigService
#logger: Logger
+constructor(waMonitor, prismaRepository, loggerName, configService)
#getBotType(): string
+createNewSession(instance, data, type)
#initClient(apiKey: string)
+sendMessageWhatsApp(instance, remoteJid, message, settings)
+speechToText(msgOrBuffer, updateMediaMessage) : Promise~string~
#getAudioBufferFromMsg(msg, updateMediaMessage) : Promise~Buffer~
#processAudioTranscription(audioBuffer: Buffer) : Promise~string~
}
class OpenaiService {
#client: OpenAI
+constructor(waMonitor, prismaRepository, configService)
#getBotType(): string
+createNewSession(instance, data)
#initClient(apiKey: string) : OpenAI
+process(instance, remoteJid, bot, session, settings, content, pushName, msg) : Promise~void~
#sendMessageToBot(instance, session, settings, bot, remoteJid, pushName, content, msg) : Promise~void~
#processAssistantMessage(instance, session, openaiBot, remoteJid, pushName, fromMe, content, msg) : Promise~string~
#processChatCompletionMessage(instance, openaiBot, remoteJid, content, msg) : Promise~string~
+speechToText(msgOrBuffer, updateMediaMessage) : Promise~string~
}
BaseChatbotService <|-- OpenaiService
class BaseChatbotController {
<<abstract>>
#service: BaseChatbotService
#prismaRepository: PrismaRepository
#integrationName: string
+constructor(service, prismaRepository, waMonitor, integrationName)
#getFallbackBotId(settings: any): string
#getIntegrationType(): string
#getAdditionalBotData(data: BaseChatbotDto): Record~string, any~
#getAdditionalUpdateFields(data: BaseChatbotDto): Record~string, any~
+createBot(instance, data: BaseChatbotDto)
+updateBot(instance, botId, data: BaseChatbotDto)
+emit(data: any)
#processBot(instance, remoteJid, bot, session, settings, content, pushName, msg) : Promise~void~
}
class OpenaiController {
#openaiService: OpenaiService
+constructor(openaiService, prismaRepository, waMonitor)
#getIntegrationType(): string
#getAdditionalBotData(data: OpenaiDto): Record~string, any~
#processBot(instance, remoteJid, bot, session, settings, content, pushName, msg) : Promise~void~
+createOpenaiCreds(instance, data: OpenaiCredsDto)
+getModels(instance)
}
class DifyController {
#difyService: DifyService
+constructor(difyService, prismaRepository, waMonitor)
#getIntegrationType(): string
#getAdditionalBotData(data: DifyDto): Record~string, any~
#processBot(instance, remoteJid, bot, session, settings, content, pushName, msg) : Promise~void~
}
BaseChatbotController <|-- OpenaiController
BaseChatbotController <|-- DifyController
class ChannelStartupService {
+openaiService: OpenaiService
}
class BaileysStartupService {
+onMessage(received)
}
ChannelStartupService <|-- BaileysStartupService
BaileysStartupService ..> OpenaiService : uses speechToText()
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
Summary by Sourcery
Refactor chatbot integrations to remove duplication and centralize common logic.
Enhancements: