fix: dify truncated messages

This commit is contained in:
Toni Moreira 2025-02-01 11:47:50 -03:00 committed by GitHub
parent db9cdbfc38
commit fc84e0f327
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -224,23 +224,13 @@ export class DifyService {
headers: {
Authorization: `Bearer ${dify.apiKey}`,
},
responseType: 'stream',
});
let conversationId;
let answer = '';
const stream = response.data;
const reader = new Readable().wrap(stream);
const data = response.data.replaceAll('data: ', '');
reader.on('data', (chunk) => {
const data = chunk.toString().replace(/data:\s*/g, '');
if (data.trim() === '' || !data.startsWith('{')) {
return;
}
try {
const events = data.split('\n').filter((line) => line.trim() !== '');
for (const eventString of events) {
@ -254,12 +244,7 @@ export class DifyService {
}
}
}
} catch (error) {
console.error('Error parsing stream data:', error);
}
});
reader.on('end', async () => {
if (instance.integration === Integration.WHATSAPP_BAILEYS)
await instance.client.sendPresenceUpdate('paused', remoteJid);
@ -277,11 +262,6 @@ export class DifyService {
sessionId: conversationId,
},
});
});
reader.on('error', (error) => {
console.error('Error reading stream:', error);
});
return;
}