async function k(e,t){let n=await e.text().catch(()=>"");throw new Error(`[Directive] ${t} streaming error ${e.status}${n?` \u2013 ${n.slice(0,200)}`:""}`)}function w(e){let t=e.body?.getReader();if(!t)throw new Error("[Directive] No response body");return t}function E(e,t){typeof process<"u"&&process.env?.NODE_ENV!=="production"&&!e&&console.warn(`[Directive] ${t}: apiKey is empty. API calls will fail.`);}async function T(e,t,n,o){let s=new TextDecoder,i="",a="",f=0,l=0;try{for(;;){let{done:g,value:m}=await e.read();if(g)break;i+=s.decode(m,{stream:!0});let d=i.split(`
`);i=d.pop()??"";for(let p of d){if(!p.startsWith("data: "))continue;let c=p.slice(6).trim();if(c!=="[DONE]")try{let u=JSON.parse(c),r=n(u);r.text&&(a+=r.text,t?.(r.text)),r.inputTokens!==void 0&&(f=r.inputTokens),r.outputTokens!==void 0&&(l=r.outputTokens);}catch(u){if(u instanceof SyntaxError)typeof process<"u"&&process.env?.NODE_ENV==="development"&&console.warn(`[Directive] Malformed SSE event from ${o}:`,c);else throw u}}}}finally{e.cancel().catch(()=>{});}return {fullText:a,inputTokens:f,outputTokens:l}}function v(e,t,n){let o=Date.now();return e?.onBeforeCall?.({agent:t,input:n,timestamp:o}),o}function y(e,t,n,o,s,i,a){e?.onAfterCall?.({agent:t,input:n,output:o,totalTokens:s,tokenUsage:i,durationMs:Date.now()-a,timestamp:Date.now()});}function b(e,t,n,o,s){o instanceof Error&&e?.onError?.({agent:t,input:n,error:o,durationMs:Date.now()-s,timestamp:Date.now()});}function x(e,t,n,o){return {output:t,messages:[{role:"user",content:e},{role:"assistant",content:t}],toolCalls:[],totalTokens:n,tokenUsage:o}}export{k as a,w as b,E as c,T as d,v as e,y as f,b as g,x as h};//# sourceMappingURL=chunk-265ZKXYE.js.map
//# sourceMappingURL=chunk-265ZKXYE.js.map