Skip to content

Commit c30bae4

Browse files
committed
refactor(openai): improve service initialization and streamline audio 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.
1 parent 69b4f1a commit c30bae4

File tree

18 files changed

+967
-1730
lines changed

18 files changed

+967
-1730
lines changed

src/api/integrations/channel/evolution/evolution.channel.service.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,7 @@ export class EvolutionStartupService extends ChannelStartupService {
165165
openAiDefaultSettings.speechToText &&
166166
received?.message?.audioMessage
167167
) {
168-
messageRaw.message.speechToText = await this.openaiService.speechToText(
169-
openAiDefaultSettings.OpenaiCreds,
170-
received,
171-
this.client.updateMediaMessage,
172-
);
168+
messageRaw.message.speechToText = await this.openaiService.speechToText(received);
173169
}
174170
}
175171

src/api/integrations/channel/meta/whatsapp.business.service.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -501,16 +501,12 @@ export class BusinessStartupService extends ChannelStartupService {
501501
openAiDefaultSettings.speechToText &&
502502
audioMessage
503503
) {
504-
messageRaw.message.speechToText = await this.openaiService.speechToText(
505-
openAiDefaultSettings.OpenaiCreds,
506-
{
507-
message: {
508-
mediaUrl: messageRaw.message.mediaUrl,
509-
...messageRaw,
510-
},
504+
messageRaw.message.speechToText = await this.openaiService.speechToText({
505+
message: {
506+
mediaUrl: messageRaw.message.mediaUrl,
507+
...messageRaw,
511508
},
512-
() => {},
513-
);
509+
});
514510
}
515511
}
516512

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,11 +1298,7 @@ export class BaileysStartupService extends ChannelStartupService {
12981298
});
12991299

13001300
if (openAiDefaultSettings && openAiDefaultSettings.openaiCredsId && openAiDefaultSettings.speechToText) {
1301-
messageRaw.message.speechToText = await this.openaiService.speechToText(
1302-
openAiDefaultSettings.OpenaiCreds,
1303-
received,
1304-
this.client.updateMediaMessage,
1305-
);
1301+
messageRaw.message.speechToText = await this.openaiService.speechToText(received);
13061302
}
13071303
}
13081304

@@ -2297,11 +2293,7 @@ export class BaileysStartupService extends ChannelStartupService {
22972293
});
22982294

22992295
if (openAiDefaultSettings && openAiDefaultSettings.openaiCredsId && openAiDefaultSettings.speechToText) {
2300-
messageRaw.message.speechToText = await this.openaiService.speechToText(
2301-
openAiDefaultSettings.OpenaiCreds,
2302-
messageRaw,
2303-
this.client.updateMediaMessage,
2304-
);
2296+
messageRaw.message.speechToText = await this.openaiService.speechToText(messageRaw);
23052297
}
23062298
}
23072299

src/api/integrations/chatbot/base-chatbot.controller.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,21 @@ export interface BaseBotData {
4747
[key: string]: any;
4848
}
4949

50-
export abstract class BaseChatbotController<BotType = any, BotData extends BaseChatbotDto = BaseChatbotDto> extends ChatbotController implements ChatbotControllerInterface {
50+
export abstract class BaseChatbotController<BotType = any, BotData extends BaseChatbotDto = BaseChatbotDto>
51+
extends ChatbotController
52+
implements ChatbotControllerInterface
53+
{
5154
public readonly logger: Logger;
52-
55+
5356
integrationEnabled: boolean;
5457
botRepository: any;
5558
settingsRepository: any;
5659
sessionRepository: any;
5760
userMessageDebounce: { [key: string]: { message: string; timeoutId: NodeJS.Timeout } } = {};
58-
61+
5962
// Name of the integration, to be set by the derived class
6063
protected abstract readonly integrationName: string;
61-
64+
6265
// Method to process bot-specific logic
6366
protected abstract processBot(
6467
waInstance: any,
@@ -70,16 +73,13 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
7073
pushName?: string,
7174
msg?: any,
7275
): Promise<void>;
73-
76+
7477
// Method to get the fallback bot ID from settings
7578
protected abstract getFallbackBotId(settings: any): string | undefined;
76-
77-
constructor(
78-
prismaRepository: PrismaRepository,
79-
waMonitor: WAMonitoringService,
80-
) {
79+
80+
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
8181
super(prismaRepository, waMonitor);
82-
82+
8383
this.sessionRepository = this.prismaRepository.integrationSession;
8484
}
8585

@@ -161,7 +161,9 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
161161
});
162162

163163
if (checkTriggerAll && data.triggerType === 'all') {
164-
throw new Error(`You already have a ${this.integrationName} with an "All" trigger, you cannot have more bots while it is active`);
164+
throw new Error(
165+
`You already have a ${this.integrationName} with an "All" trigger, you cannot have more bots while it is active`,
166+
);
165167
}
166168

167169
// Check for trigger keyword duplicates
@@ -309,7 +311,7 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
309311

310312
// Get the name of the fallback field for this integration type
311313
const fallbackFieldName = this.getFallbackFieldName();
312-
314+
313315
const settingsData = {
314316
expire: data.expire,
315317
keywordFinish: data.keywordFinish,
@@ -336,20 +338,25 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
336338
// Map the specific fallback field to a generic 'fallbackId' in the response
337339
return {
338340
...settings,
339-
fallbackId: settings[fallbackFieldName]
341+
fallbackId: settings[fallbackFieldName],
340342
};
341343
} else {
342344
const settings = await this.settingsRepository.create({
343345
data: {
344346
...settingsData,
345347
instanceId: instanceId,
348+
Instance: {
349+
connect: {
350+
id: instanceId,
351+
},
352+
},
346353
},
347354
});
348355

349356
// Map the specific fallback field to a generic 'fallbackId' in the response
350357
return {
351358
...settings,
352-
fallbackId: settings[fallbackFieldName]
359+
fallbackId: settings[fallbackFieldName],
353360
};
354361
}
355362
} catch (error) {
@@ -631,7 +638,9 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
631638
});
632639

633640
if (checkTriggerAll) {
634-
throw new Error(`You already have a ${this.integrationName} with an "All" trigger, you cannot have more bots while it is active`);
641+
throw new Error(
642+
`You already have a ${this.integrationName} with an "All" trigger, you cannot have more bots while it is active`,
643+
);
635644
}
636645
}
637646

@@ -779,15 +788,15 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
779788
if (this.checkIgnoreJids(settings?.ignoreJids, remoteJid)) return;
780789

781790
const session = await this.getSession(remoteJid, instance);
782-
791+
783792
const content = getConversationMessage(msg);
784-
793+
785794
// Get integration type
786795
const integrationType = this.getIntegrationType();
787-
796+
788797
// Find a bot for this message
789798
let findBot: any = await this.findBotTrigger(this.botRepository, content, instance, session);
790-
799+
791800
// If no bot is found, try to use fallback
792801
if (!findBot) {
793802
const fallback = await this.settingsRepository.findFirst({
@@ -798,7 +807,7 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
798807

799808
// Get the fallback ID for this integration type
800809
const fallbackId = this.getFallbackBotId(fallback);
801-
810+
802811
if (fallbackId) {
803812
const findFallback = await this.botRepository.findFirst({
804813
where: {
@@ -811,7 +820,7 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
811820
return;
812821
}
813822
}
814-
823+
815824
// If we still don't have a bot, return
816825
if (!findBot) {
817826
return;
@@ -918,4 +927,4 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
918927
this.logger.error(error);
919928
}
920929
}
921-
}
930+
}

src/api/integrations/chatbot/base-chatbot.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ export class BaseChatbotSettingDto {
3939
splitMessages?: boolean;
4040
timePerChar?: number;
4141
fallbackId?: string; // Unified fallback ID field for all integrations
42-
}
42+
}

0 commit comments

Comments
 (0)