import{EVE_DEV_RUNTIME_ARTIFACTS_ROUTE_PATH}from"#protocol/routes.js";import{isLocalDevelopmentServerUrl,resolveDevelopmentClientHeaders,resolveDevelopmentOidcToken}from"#services/dev-client/request-headers.js";import{Client}from"#client/index.js";var BufferedDevelopmentMessageClient=class{#e;#t;#n;constructor(e){let a=isLocalDevelopmentServerUrl(e.serverUrl);this.#t=createDevelopmentRuntimeArtifactSessionRefresher({serverUrl:e.serverUrl});let o={headers:()=>resolveDevelopmentClientHeaders({serverUrl:e.serverUrl}),host:e.serverUrl};this.#e=new Client(a?o:{...o,auth:{bearer:resolveDevelopmentOidcToken}}),this.#n=this.#e.session()}async clear(){this.#n=this.#e.session(),this.#t.clear()}async close(){}getSession(){return this.#n.state}async send(e){this.#n=await this.#t.refresh({createSession:()=>this.#e.session(),inputResponses:e.inputResponses,message:e.message,session:this.#n});let t=await this.#n.send({clientContext:e.clientContext,inputResponses:e.inputResponses,message:e.message});e.onResponseStart?.({sessionId:t.sessionId});let n=[];for await(let r of t)n.push(r),e.onEvent?.(r);return{events:n,sessionId:t.sessionId,session:this.#n.state}}},LocalDevelopmentRuntimeArtifactSessionRefresher=class{#e;#t;#n;constructor(e){this.#e=isLocalDevelopmentServerUrl(e.serverUrl),this.#t=e.serverUrl}clear(){this.#n=void 0}async refresh(e){if(!this.#e||!shouldRefreshRuntimeArtifactsForTurn(e))return e.session;let t=await this.#r();if(t===void 0)return e.session;let n=e.session,r=this.#n;return r!==void 0&&r!==t&&(n.state.continuationToken!==void 0&&(n=e.createSession()),await e.onRuntimeArtifactsChanged?.({previousRevision:r,revision:t})),this.#n=t,n}async refreshIdle(e){if(!this.#e)return e.session;let t=await this.#r();if(t===void 0)return e.session;let n=e.session,r=this.#n;return r!==void 0&&r!==t&&(n.state.continuationToken!==void 0&&(n=e.createSession()),await e.onRuntimeArtifactsChanged?.({previousRevision:r,revision:t})),this.#n=t,n}async#r(){try{let t=new URL(EVE_DEV_RUNTIME_ARTIFACTS_ROUTE_PATH,this.#t),r=await fetch(t,{headers:await resolveDevelopmentClientHeaders({serverUrl:this.#t})});if(!r.ok)return;let i=await r.json();return typeof i.revision==`string`&&i.revision.length>0?i.revision:void 0}catch{return}}};function shouldRefreshRuntimeArtifactsForTurn(e){return e.message!==void 0&&(e.inputResponses?.length??0)===0}function createDevelopmentRuntimeArtifactSessionRefresher(e){return new LocalDevelopmentRuntimeArtifactSessionRefresher(e)}function createDevClient(e){return new BufferedDevelopmentMessageClient(e)}export{createDevClient,createDevelopmentRuntimeArtifactSessionRefresher};