Merge pull request #825 from yantxs/fix-dify-chunks

Fixed integration with Dify agents
This commit is contained in:
Davidson Gomes 2024-08-27 08:37:28 -03:00 committed by GitHub
commit bb89816ca6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -233,17 +233,25 @@ export class DifyService {
const reader = new Readable().wrap(stream);
reader.on('data', (chunk) => {
const data = chunk.toString();
const data = chunk.toString().replace(/data:\s*/g, '');
if (data.trim() === '' || !data.startsWith('{')) {
return;
}
try {
const cleanedData = data.replace(/^data:\s*/, '');
const event = JSON.parse(cleanedData);
if (event?.event === 'agent_message') {
console.log('event:', event);
conversationId = conversationId ?? event?.conversation_id;
answer += event?.answer;
const events = data.split('\n').filter((line) => line.trim() !== '');
for (const eventString of events) {
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);