mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-19 09:53:36 -06:00
Merge pull request #825 from yantxs/fix-dify-chunks
Fixed integration with Dify agents
This commit is contained in:
commit
bb89816ca6
@ -233,17 +233,25 @@ export class DifyService {
|
|||||||
const reader = new Readable().wrap(stream);
|
const reader = new Readable().wrap(stream);
|
||||||
|
|
||||||
reader.on('data', (chunk) => {
|
reader.on('data', (chunk) => {
|
||||||
const data = chunk.toString();
|
const data = chunk.toString().replace(/data:\s*/g, '');
|
||||||
|
|
||||||
|
if (data.trim() === '' || !data.startsWith('{')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const cleanedData = data.replace(/^data:\s*/, '');
|
const events = data.split('\n').filter((line) => line.trim() !== '');
|
||||||
|
|
||||||
const event = JSON.parse(cleanedData);
|
for (const eventString of events) {
|
||||||
|
if (eventString.trim().startsWith('{')) {
|
||||||
if (event?.event === 'agent_message') {
|
const event = JSON.parse(eventString);
|
||||||
console.log('event:', event);
|
|
||||||
conversationId = conversationId ?? event?.conversation_id;
|
if (event?.event === 'agent_message') {
|
||||||
answer += event?.answer;
|
console.log('event:', event);
|
||||||
|
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);
|
||||||
|
Loading…
Reference in New Issue
Block a user