mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-16 04:02:54 -06:00
fix: dify truncated messages
This commit is contained in:
parent
db9cdbfc38
commit
fc84e0f327
@ -224,63 +224,43 @@ export class DifyService {
|
|||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${dify.apiKey}`,
|
Authorization: `Bearer ${dify.apiKey}`,
|
||||||
},
|
},
|
||||||
responseType: 'stream',
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let conversationId;
|
let conversationId;
|
||||||
let answer = '';
|
let answer = '';
|
||||||
|
|
||||||
const stream = response.data;
|
const data = response.data.replaceAll('data: ', '');
|
||||||
const reader = new Readable().wrap(stream);
|
|
||||||
|
|
||||||
reader.on('data', (chunk) => {
|
const events = data.split('\n').filter((line) => line.trim() !== '');
|
||||||
const data = chunk.toString().replace(/data:\s*/g, '');
|
|
||||||
|
|
||||||
if (data.trim() === '' || !data.startsWith('{')) {
|
for (const eventString of events) {
|
||||||
return;
|
if (eventString.trim().startsWith('{')) {
|
||||||
}
|
const event = JSON.parse(eventString);
|
||||||
|
|
||||||
try {
|
if (event?.event === 'agent_message') {
|
||||||
const events = data.split('\n').filter((line) => line.trim() !== '');
|
console.log('event:', event);
|
||||||
|
conversationId = conversationId ?? event?.conversation_id;
|
||||||
for (const eventString of events) {
|
answer += event?.answer;
|
||||||
if (eventString.trim().startsWith('{')) {
|
|
||||||
const event = JSON.parse(eventString);
|
|
||||||
|
|
||||||
if (event?.event === 'agent_message') {
|
|
||||||
console.log('event:', event);
|
|
||||||
conversationId = conversationId ?? event?.conversation_id;
|
|
||||||
answer += event?.answer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
console.error('Error parsing stream data:', error);
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
reader.on('end', async () => {
|
if (instance.integration === Integration.WHATSAPP_BAILEYS)
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS)
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
|
||||||
|
|
||||||
const message = answer;
|
const message = answer;
|
||||||
|
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||||
|
|
||||||
await this.prismaRepository.integrationSession.update({
|
await this.prismaRepository.integrationSession.update({
|
||||||
where: {
|
where: {
|
||||||
id: session.id,
|
id: session.id,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
status: 'opened',
|
status: 'opened',
|
||||||
awaitUser: true,
|
awaitUser: true,
|
||||||
sessionId: conversationId,
|
sessionId: conversationId,
|
||||||
},
|
},
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
reader.on('error', (error) => {
|
|
||||||
console.error('Error reading stream:', error);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user