mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 09:51:24 -06:00
fix: dify agent integration
This commit is contained in:
parent
e9f4477d11
commit
2196f65b7a
@ -11,6 +11,7 @@
|
|||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
* Refactor integrations structure for modular system
|
* Refactor integrations structure for modular system
|
||||||
|
* Fixed dify agent integration
|
||||||
|
|
||||||
# 2.0.10 (2024-08-16 16:23)
|
# 2.0.10 (2024-08-16 16:23)
|
||||||
|
|
||||||
|
@ -41,6 +41,15 @@ export class DifyService {
|
|||||||
return content.includes('imageMessage');
|
return content.includes('imageMessage');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private isJSON(str: string): boolean {
|
||||||
|
try {
|
||||||
|
JSON.parse(str);
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async sendMessageToBot(
|
private async sendMessageToBot(
|
||||||
instance: any,
|
instance: any,
|
||||||
session: IntegrationSession,
|
session: IntegrationSession,
|
||||||
@ -50,6 +59,7 @@ export class DifyService {
|
|||||||
pushName: string,
|
pushName: string,
|
||||||
content: string,
|
content: string,
|
||||||
) {
|
) {
|
||||||
|
try {
|
||||||
let endpoint: string = dify.apiUrl;
|
let endpoint: string = dify.apiUrl;
|
||||||
|
|
||||||
if (dify.botType === 'chatBot') {
|
if (dify.botType === 'chatBot') {
|
||||||
@ -94,8 +104,20 @@ export class DifyService {
|
|||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
|
||||||
const message = response?.data?.answer;
|
const message = response?.data?.answer;
|
||||||
|
const conversationId = response?.data?.conversation_id;
|
||||||
|
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, session, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||||
|
|
||||||
|
await this.prismaRepository.integrationSession.update({
|
||||||
|
where: {
|
||||||
|
id: session.id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
status: 'opened',
|
||||||
|
awaitUser: true,
|
||||||
|
sessionId: session.sessionId === remoteJid ? conversationId : session.sessionId,
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dify.botType === 'textGenerator') {
|
if (dify.botType === 'textGenerator') {
|
||||||
@ -140,8 +162,20 @@ export class DifyService {
|
|||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
|
||||||
const message = response?.data?.answer;
|
const message = response?.data?.answer;
|
||||||
|
const conversationId = response?.data?.conversation_id;
|
||||||
|
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, session, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||||
|
|
||||||
|
await this.prismaRepository.integrationSession.update({
|
||||||
|
where: {
|
||||||
|
id: session.id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
status: 'opened',
|
||||||
|
awaitUser: true,
|
||||||
|
sessionId: session.sessionId === remoteJid ? conversationId : session.sessionId,
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dify.botType === 'agent') {
|
if (dify.botType === 'agent') {
|
||||||
@ -185,6 +219,7 @@ export class DifyService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let conversationId;
|
let conversationId;
|
||||||
|
let answer = '';
|
||||||
|
|
||||||
const stream = response.data;
|
const stream = response.data;
|
||||||
const reader = new Readable().wrap(stream);
|
const reader = new Readable().wrap(stream);
|
||||||
@ -193,9 +228,14 @@ export class DifyService {
|
|||||||
const data = chunk.toString();
|
const data = chunk.toString();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const event = JSON.parse(data);
|
const cleanedData = data.replace(/^data:\s*/, '');
|
||||||
if (event.event === 'agent_message') {
|
|
||||||
|
const event = JSON.parse(cleanedData);
|
||||||
|
|
||||||
|
if (event?.event === 'agent_message') {
|
||||||
|
console.log('event:', event);
|
||||||
conversationId = conversationId ?? event?.conversation_id;
|
conversationId = conversationId ?? event?.conversation_id;
|
||||||
|
answer += event?.answer;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error parsing stream data:', error);
|
console.error('Error parsing stream data:', error);
|
||||||
@ -205,9 +245,21 @@ export class DifyService {
|
|||||||
reader.on('end', async () => {
|
reader.on('end', async () => {
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
|
||||||
const message = response?.data?.answer;
|
const message = answer;
|
||||||
|
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, session, settings);
|
console.log('message:', answer);
|
||||||
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||||
|
|
||||||
|
await this.prismaRepository.integrationSession.update({
|
||||||
|
where: {
|
||||||
|
id: session.id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
status: 'opened',
|
||||||
|
awaitUser: true,
|
||||||
|
sessionId: conversationId,
|
||||||
|
},
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
reader.on('error', (error) => {
|
reader.on('error', (error) => {
|
||||||
@ -259,19 +311,27 @@ export class DifyService {
|
|||||||
|
|
||||||
const message = response?.data?.data.outputs.text;
|
const message = response?.data?.data.outputs.text;
|
||||||
|
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, session, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||||
|
|
||||||
|
await this.prismaRepository.integrationSession.update({
|
||||||
|
where: {
|
||||||
|
id: session.id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
status: 'opened',
|
||||||
|
awaitUser: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(error.response?.data || error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async sendMessageWhatsApp(
|
private async sendMessageWhatsApp(instance: any, remoteJid: string, message: string, settings: DifySetting) {
|
||||||
instance: any,
|
|
||||||
remoteJid: string,
|
|
||||||
message: string,
|
|
||||||
session: IntegrationSession,
|
|
||||||
settings: DifySetting,
|
|
||||||
) {
|
|
||||||
const regex = /!?\[(.*?)\]\((.*?)\)/g;
|
const regex = /!?\[(.*?)\]\((.*?)\)/g;
|
||||||
|
|
||||||
const result = [];
|
const result = [];
|
||||||
@ -318,23 +378,6 @@ export class DifyService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (settings.keepOpen) {
|
|
||||||
await this.prismaRepository.integrationSession.update({
|
|
||||||
where: {
|
|
||||||
id: session.id,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
status: 'closed',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
await this.prismaRepository.integrationSession.delete({
|
|
||||||
where: {
|
|
||||||
id: session.id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
sendTelemetry('/message/sendText');
|
sendTelemetry('/message/sendText');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user