import{SSE_EVENTS as i,SSE_DATA_PREFIX as a}from"../../constants";function d(e){return Object.values(i).includes(e)}function p(e){const s=[],t=e.split(`
`);for(const n of t){if(!n.startsWith(a))continue;const r=n.slice(a.length);if(r)try{const c=JSON.parse(r),o=S(c);o&&s.push(o)}catch{}}return s}function S(e){return e.type&&typeof e.type=="string"&&d(e.type)?e:"messageId"in e&&typeof e.messageId=="string"?{type:i.MESSAGE_ID,messageId:e.messageId}:"sources"in e&&Array.isArray(e.sources)?{type:i.SOURCES,sources:e.sources}:"answer"in e&&typeof e.answer=="string"?{type:i.ANSWER,answer:e.answer}:null}async function*y(e,s){const t=e.getReader(),n=new TextDecoder;try{for(;!s?.aborted;){const{value:r,done:c}=await t.read();if(c)break;const o=n.decode(r),u=p(o);for(const f of u)yield f}}finally{t.releaseLock()}}export{p as parseSSEChunk,y as streamSSEMessages};
