var p=class d{constructor(){this.buffer="",this.extractedResult=null,this.rawText="",this.zodSchema=null,this.lastOutputLength=0,this.onToolCall=null,this._lastToolEmit=null}processChunk(t){if(!t)return null;this.buffer+=t;let s=this.buffer.split(`
`);this.buffer=s.pop()||"";let r="";for(let l of s)if(l.trim())try{let n=JSON.parse(l);this._emitToolCalls(n);let e=this.extractText(n);if(e){if(this.rawText&&e.startsWith(this.rawText)){let i=e.substring(this.rawText.length);this.rawText=e,r+=i}else(!this.rawText.includes(e)||e.length<20)&&(this.rawText+=e,r+=e);this.tryExtractResult(this.rawText)}else this.isValidResult(n)&&(this.rawText+=`${l}
`,r+=`${l}
`,this.extractedResult=n)}catch{if(l.includes('"text"')||l.includes('"content"')){let e=l.match(/"text"\s*:\s*"([^"]*)/),i=l.match(/"content"\s*:\s*"([^"]*)/),a=e?e[1]:i?i[1]:null;a&&!this.rawText.includes(a)&&(r+=a,this.rawText+=a)}}return r||null}flush(){if(!this.buffer.trim())return null;let t="";try{let s=JSON.parse(this.buffer);this._emitToolCalls(s);let r=this.extractText(s);r&&(this.rawText+=r,t+=r,this.tryExtractResult(r))}catch{this.rawText+=this.buffer,t+=this.buffer,this.tryExtractResult(this.buffer)}return this.buffer="",t||null}_emitToolCalls(t){if(!this.onToolCall)return;let s=(e,i)=>{if(!e)return;let a=`${e}:${JSON.stringify(i??{})}`;this._lastToolEmit!==a&&(this._lastToolEmit=a,this.onToolCall(e,i??void 0))},r=e=>{if(e!=null){if(typeof e=="object"&&!Array.isArray(e))return e;if(typeof e=="string")try{return JSON.parse(e)}catch{return}}};if(t.type==="tool_use"||t.type==="tool_call"){if(t.name){s(t.name,r(t.input??t.arguments));return}let e=t.tool_call;if(e&&typeof e=="object"&&!Array.isArray(e)){let i=Object.keys(e);if(i.length===1){let a=i[0],o=e[a],h=o&&typeof o=="object"?o.args??o.input??o:void 0;s(a,r(h))}return}return}if(Array.isArray(t.tool_calls)){for(let e of t.tool_calls)s(e.name,r(e.input??e.arguments));return}let l=t.message??t;if(Array.isArray(l?.tool_calls)){for(let e of l.tool_calls)s(e.name,r(e.input??e.arguments));return}let n=l?.content??t.content;if(Array.isArray(n))for(let e of n)(e.type==="tool_use"||e.type==="tool_call")&&e.name&&s(e.name,r(e.input??e.arguments))}extractText(t){if(t.type==="assistant"&&t.message?.content){let s=t.message.content;if(Array.isArray(s))return s.filter(r=>r.type==="text"&&r.text).map(r=>r.text).join("")}return t.type==="thinking"&&t.text||t.text?t.text:t.content&&typeof t.content=="string"?t.content:t.delta?t.delta:null}tryExtractResult(t){if(!t||typeof t!="string")return;let s=[],r=/```json\s*\n?([\s\S]*?)\n?```/g,l;for(;(l=r.exec(t))!==null;){let c=l[1].trim();try{JSON.parse(c),s.push({text:c,source:"markdown"})}catch{}}let n=0,e=0;for(;n<t.length&&(n=t.indexOf("{",n),n!==-1);){let c=0,f=n;for(let u=n;u<t.length;u++)if(t[u]==="{")c++;else if(t[u]==="}"&&(c--,c===0)){f=u,s.push({text:t.substring(n,f+1),source:"brace"}),e++;break}n=f+1}let i=this.extractedResult,a=i?JSON.stringify(i).length:0,o=0,h=-1;for(let c=0;c<s.length;c++){let f=s[c];try{let u=f.text.replace(/,(\s*[}\]])/g,"$1"),y=JSON.parse(u);this.isValidResult(y)&&(o++,a=JSON.stringify(y).length,i=y,h=c)}catch{}}i&&(this.extractedResult=i)}isValidResult(t){if(!t||typeof t!="object"||Array.isArray(t)||t.session_id||t.timestamp_ms||t.type||t.call_id||t.tool_call||t.result&&typeof t.result=="object"&&(t.result.success&&typeof t.result.success=="object"||t.result.error&&typeof t.result.error=="object")||t.args&&typeof t.args=="object")return!1;if(this.zodSchema)try{return this.zodSchema.parse(t),!0}catch{return!1}return!0}getResult(){return this.extractedResult}getRawText(){return this.rawText}static extractResult(t,s=null){let r=new d;r.zodSchema=s,r.processChunk(t),r.flush();let l=r.getResult();return!l&&process.env.LOG_LEVEL==="debug"&&console.error("[StreamingParser] No result extracted from",t?.length||0,"chars"),l}};export{p as StreamingParser};
