How to handle stream response with SSE and assemble into JSON

For the API endpoint of Send a message to a conversation (SSE) with stream https://api.agentx.so/api/v1/access/conversations/{id}/jsonmessagesse

The response will return in stream way (chunk by chunk).

And chunks can be accumulate into a JSON.

e.g. {"cot": "I am thinking", "text": "Hello"}

The response will stream back chunk by chunk, in this case, you will receive {, ", c, o, t .... one by one. We will need to accumulate them and try to parse it into JSON.

Here is the sample code of how to handle it in JavaScript:

const response = await axios.post(url, { message }, {
  headers: {
    'Accept': 'text/event-stream',
    'Content-Type': 'application/json',
    'x-api-key': apiKey,
    'Connection': 'close'
  },
  responseType: 'stream'
});

let buffer = "";
let completeCot = "";
let completeText = "";

response.data.on('data', async (chunk) => {
  buffer += chunk.toString();;

  try {
    const jsonChunk = JSON.parse(buffer);
    if (jsonChunk.cot || jsonChunk.text){
      completeCot += jsonChunk.cot
      completeText += jsonChunk.text
      buffer = "" // reset buffer
    }
  } catch (err) {
    // JSON parse failed, incomplete, continue
    null;
  }
});

You may change the code accordingly.