Skip to content

Commit 7682a67

Browse files
committed
refactor: enhance Dify integration with improved validation and message 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.
1 parent 97ca23a commit 7682a67

File tree

3 files changed

+38
-408
lines changed

3 files changed

+38
-408
lines changed

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)