import{toError}from"#shared/errors.js";import{Client}from"#client/client.js";var EveAgentStore=class{#e;#t;#n;#r=new Set;#i;#a={};#o;#s;#c;#l=0;#u;#d;#f;#p;#m=`ready`;constructor(e){this.#e=e.session?void 0:()=>new Client({auth:e.auth,headers:e.headers,host:e.host??``,maxReconnectAttempts:e.maxReconnectAttempts}).session(e.initialSession),this.#c=[...e.initialEvents??[]],this.#d=[...this.#c],this.#t=e.optimistic??!0,this.#n=e.reducer,this.#f=e.session??this.#h(),this.#o=this.#E(this.#d),this.#p=this.#D()}get snapshot(){return this.#p}setCallbacks(e){this.#a=e}subscribe(e){return this.#r.add(e),()=>{this.#r.delete(e)}}async send(t){if(this.#m===`streaming`||this.#m===`submitted`)throw Error(`Eve session is already processing a turn.`);let n=this.#g(),r=new AbortController;this.#i=r,this.#s=void 0,this.#m=`submitted`,this.#O();try{let e=await this.#a.prepareSend?.(t)??t;if(!this.#v(n))return;this.#y(e),this.#b(e),this.#O();let i=await this.#f.send({...e,signal:createAbortSignal(e.signal,r.signal)}),a=!1;for await(let e of i){if(!this.#v(n))return;a||(a=!0,this.#m=`streaming`),this.#c=[...this.#c,e],this.#x(e),this.#a.onEvent?.(e),this.#S(e),this.#O()}if(!this.#v(n))return;this.#m=this.#s===void 0?`ready`:`error`}catch(t){if(!this.#v(n))return;isAbortError(t)?(this.#m=`ready`,this.#C(toError(t))):(this.#s=toError(t),this.#m=`error`,this.#C(this.#s),this.#a.onError?.(this.#s))}finally{this.#v(n)&&(this.#i=void 0,this.#a.onSessionChange?.(this.#f.state),this.#O(),this.#a.onFinish?.(this.#p))}}stop(){this.#i?.abort()}reset(){this.#_(),this.stop(),this.#i=void 0,this.#f=this.#e?.()??this.#f,this.#c=[],this.#u=void 0,this.#d=[],this.#o=this.#n.initial(),this.#s=void 0,this.#m=`ready`,this.#a.onSessionChange?.(this.#f.state),this.#O()}#h(){if(!this.#e)throw Error(`Cannot create an owned Eve session from an external session.`);return this.#e()}#g(){return this.#l+=1,this.#l}#_(){this.#l+=1}#v(e){return this.#l===e}#y(e){if(!this.#t||e.message===void 0)return;let t=createSubmissionId(),n={createdAt:Date.now(),id:t,message:summarizeUserContent(e.message)};this.#u=n,this.#w({data:{createdAt:n.createdAt,message:n.message,submissionId:n.id},type:`client.message.submitted`})}#b(e){e.inputResponses===void 0||e.inputResponses.length===0||this.#w({data:{createdAt:Date.now(),responses:e.inputResponses},type:`client.input.responded`})}#x(e){if(e.type===`message.received`&&this.#u!==void 0){let t=this.#u.id;this.#u=void 0,this.#T(e=>e.type===`client.message.submitted`&&e.data.submissionId===t,e);return}this.#w(e)}#S(e){let t=toTerminalStreamFailureError(e);t!==void 0&&(this.#m=`error`,this.#C(t),this.#s===void 0&&(this.#s=t,this.#a.onError?.(t)))}#C(e){let t=this.#u;t!==void 0&&(this.#u=void 0,this.#T(e=>e.type===`client.message.submitted`&&e.data.submissionId===t.id,{data:{createdAt:t.createdAt,error:{message:e.message},message:t.message,submissionId:t.id},type:`client.message.failed`}))}#w(e){this.#d=[...this.#d,e],this.#o=this.#n.reduce(this.#o,e)}#T(e,t){let n=!1;this.#d=this.#d.map(r=>!n&&e(r)?(n=!0,t):r),n||(this.#d=[...this.#d,t]),this.#o=this.#E(this.#d)}#E(e){let t=this.#n.initial();for(let n of e)t=this.#n.reduce(t,n);return t}#D(){return{data:this.#o,error:this.#s,events:this.#c,session:this.#f.state,status:this.#m}}#O(){this.#p=this.#D();for(let e of this.#r)e()}};let submissionSequence=0;function createSubmissionId(){let e=globalThis.crypto?.randomUUID;return e===void 0?(submissionSequence+=1,`submission_${submissionSequence.toString()}`):e.call(globalThis.crypto)}function createAbortSignal(e,t){return e?AbortSignal.any([e,t]):t}function summarizeUserContent(e){if(typeof e==`string`)return e;let t=[];for(let n of e){if(n.type===`text`){t.push(n.text);continue}n.type===`file`&&t.push(n.filename?`[file: ${n.filename}]`:`[file]`)}return t.join(`
`)}function isAbortError(e){return e instanceof Error&&e.name===`AbortError`}function toTerminalStreamFailureError(e){if(e.type!==`session.failed`)return;let t=Error(e.data.message);return t.name=e.data.code,t}export{EveAgentStore};