Skip to content

Commit 3500fbe

Browse files
Merge pull request #1508 from gomessguii/develop
refactor: improve chatbot integrations
2 parents dd0dfd4 + cb76381 commit 3500fbe

File tree

35 files changed

+873
-1870
lines changed

35 files changed

+873
-1870
lines changed

manager/dist/assets/index-CXH2BdD4.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manager/dist/assets/index-D-oOjDYe.js

Lines changed: 381 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manager/dist/assets/index-DNOCacL_.css

Lines changed: 0 additions & 1 deletion
This file was deleted.

manager/dist/assets/index-mxi8bQ4k.js

Lines changed: 0 additions & 381 deletions
This file was deleted.

manager/dist/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<link rel="icon" type="image/png" href="https://evolution-api.com/files/evo/favicon.svg" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>Evolution Manager</title>
8-
<script type="module" crossorigin src="/assets/index-mxi8bQ4k.js"></script>
9-
<link rel="stylesheet" crossorigin href="/assets/index-DNOCacL_.css">
8+
<script type="module" crossorigin src="/assets/index-D-oOjDYe.js"></script>
9+
<link rel="stylesheet" crossorigin href="/assets/index-CXH2BdD4.css">
1010
</head>
1111
<body>
1212
<div id="root"></div>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export class EvolutionStartupService extends ChannelStartupService {
165165
openAiDefaultSettings.speechToText &&
166166
received?.message?.audioMessage
167167
) {
168-
messageRaw.message.speechToText = await this.openaiService.speechToText(received);
168+
messageRaw.message.speechToText = await this.openaiService.speechToText(received, this);
169169
}
170170
}
171171

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

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

13001300
if (openAiDefaultSettings && openAiDefaultSettings.openaiCredsId && openAiDefaultSettings.speechToText) {
1301-
messageRaw.message.speechToText = await this.openaiService.speechToText(received);
1301+
messageRaw.message.speechToText = await this.openaiService.speechToText(received, this);
13021302
}
13031303
}
13041304

@@ -2324,7 +2324,7 @@ export class BaileysStartupService extends ChannelStartupService {
23242324
});
23252325

23262326
if (openAiDefaultSettings && openAiDefaultSettings.openaiCredsId && openAiDefaultSettings.speechToText) {
2327-
messageRaw.message.speechToText = await this.openaiService.speechToText(messageRaw);
2327+
messageRaw.message.speechToText = await this.openaiService.speechToText(messageRaw, this);
23282328
}
23292329
}
23302330

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -353,20 +353,25 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
353353
? pushName
354354
: null;
355355

356-
session = (
357-
await this.createNewSession(
358-
{
359-
instanceName: instance.instanceName,
360-
instanceId: instance.instanceId,
361-
},
362-
{
363-
remoteJid,
364-
pushName: pushNameValue,
365-
botId: (bot as any).id,
366-
},
367-
this.getBotType(),
368-
)
369-
)?.session;
356+
const sessionResult = await this.createNewSession(
357+
{
358+
instanceName: instance.instanceName,
359+
instanceId: instance.instanceId,
360+
},
361+
{
362+
remoteJid,
363+
pushName: pushNameValue,
364+
botId: (bot as any).id,
365+
},
366+
this.getBotType(),
367+
);
368+
369+
if (!sessionResult || !sessionResult.session) {
370+
this.logger.error('Failed to create new session');
371+
return;
372+
}
373+
374+
session = sessionResult.session;
370375
}
371376

372377
// Update session status to opened

src/api/integrations/chatbot/dify/controllers/dify.controller.ts

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class DifyController extends BaseChatbotController<DifyModel, DifyDto> {
8080
}
8181
}
8282

83-
// Bots
83+
// Override createBot to add Dify-specific validation
8484
public async createBot(instance: InstanceDto, data: DifyDto) {
8585
if (!this.integrationEnabled) throw new BadRequestException('Dify is disabled');
8686

@@ -92,7 +92,7 @@ export class DifyController extends BaseChatbotController<DifyModel, DifyDto> {
9292
})
9393
.then((instance) => instance.id);
9494

95-
// Check for Dify-specific duplicate
95+
// Dify-specific duplicate check
9696
const checkDuplicate = await this.botRepository.findFirst({
9797
where: {
9898
instanceId: instanceId,
@@ -106,62 +106,10 @@ export class DifyController extends BaseChatbotController<DifyModel, DifyDto> {
106106
throw new Error('Dify already exists');
107107
}
108108

109-
// Let the base class handle the rest of the bot creation process
109+
// Let the base class handle the rest
110110
return super.createBot(instance, data);
111111
}
112112

113-
public async findBot(instance: InstanceDto) {
114-
if (!this.integrationEnabled) throw new BadRequestException('Dify is disabled');
115-
116-
const instanceId = await this.prismaRepository.instance
117-
.findFirst({
118-
where: {
119-
name: instance.instanceName,
120-
},
121-
})
122-
.then((instance) => instance.id);
123-
124-
const bots = await this.botRepository.findMany({
125-
where: {
126-
instanceId: instanceId,
127-
},
128-
});
129-
130-
if (!bots.length) {
131-
return null;
132-
}
133-
134-
return bots;
135-
}
136-
137-
public async fetchBot(instance: InstanceDto, botId: string) {
138-
if (!this.integrationEnabled) throw new BadRequestException('Dify is disabled');
139-
140-
const instanceId = await this.prismaRepository.instance
141-
.findFirst({
142-
where: {
143-
name: instance.instanceName,
144-
},
145-
})
146-
.then((instance) => instance.id);
147-
148-
const bot = await this.botRepository.findFirst({
149-
where: {
150-
id: botId,
151-
},
152-
});
153-
154-
if (!bot) {
155-
throw new Error('Dify not found');
156-
}
157-
158-
if (bot.instanceId !== instanceId) {
159-
throw new Error('Dify not found');
160-
}
161-
162-
return bot;
163-
}
164-
165113
// Process Dify-specific bot logic
166114
protected async processBot(
167115
instance: any,
@@ -173,6 +121,6 @@ export class DifyController extends BaseChatbotController<DifyModel, DifyDto> {
173121
pushName?: string,
174122
msg?: any,
175123
) {
176-
this.difyService.process(instance, remoteJid, bot, session, settings, content, pushName, msg);
124+
await this.difyService.process(instance, remoteJid, bot, session, settings, content, pushName, msg);
177125
}
178126
}

src/api/integrations/chatbot/dify/dto/dify.dto.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import { $Enums } from '@prisma/client';
33
import { BaseChatbotDto, BaseChatbotSettingDto } from '../../base-chatbot.dto';
44

55
export class DifyDto extends BaseChatbotDto {
6-
// Dify specific fields
76
botType?: $Enums.DifyBotType;
87
apiUrl?: string;
98
apiKey?: string;
109
}
1110

1211
export class DifySettingDto extends BaseChatbotSettingDto {
13-
// Dify specific fields
12+
difyIdFallback?: string;
1413
}

0 commit comments

Comments
 (0)