UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines 2.33 kB
import{EVE_HEALTH_ROUTE_PATH,EVE_INFO_ROUTE_PATH}from"#protocol/routes.js";import{ClientError}from"#client/client-error.js";import{ClientSession}from"#client/session.js";import{createInitialSessionState}from"#client/session-utils.js";import{createClientUrl}from"#client/url.js";var Client=class{#e;#t;#n;#r;#i;#a;constructor(e){this.#n=e.host,this.#e=e.auth,this.#t=e.headers,this.#r=e.maxReconnectAttempts??3,this.#i=e.preserveCompletedSessions??!1,this.#a=e.redirect}async health(){let t=createClientUrl(this.#n,EVE_HEALTH_ROUTE_PATH),r=await this.#o(),i=await fetch(t,withRedirectPolicy({headers:r},this.#a));if(!i.ok){let e=await i.text();throw new ClientError(i.status,e)}return await i.json()}async info(){let e=createClientUrl(this.#n,EVE_INFO_ROUTE_PATH),r=await this.#o(),i=await fetch(e,withRedirectPolicy({headers:r},this.#a));if(!i.ok){let e=await i.text();throw new ClientError(i.status,e)}return await i.json()}async fetch(e,t={}){let n=createClientUrl(this.#n,e),r=await this.#o(headersInitToRecord(t.headers));return await fetch(n,withRedirectPolicy({...t,headers:r},this.#a))}session(e){let t;return t=typeof e==`string`?{continuationToken:e,streamIndex:0}:e||createInitialSessionState(),new ClientSession({host:this.#n,maxReconnectAttempts:this.#r,preserveCompletedSessions:this.#i,redirect:this.#a,resolveHeaders:e=>this.#o(e)},t)}async#o(e){let t=new Headers,n=await resolveHeadersValue(this.#t);for(let[e,r]of Object.entries(n))t.set(e,r);if(e)for(let[n,r]of Object.entries(e))t.set(n,r);let r=await this.#s();return r&&t.set(`authorization`,r),t}async#s(){let e=this.#e;if(e){if(`bearer`in e){let t=(await resolveTokenValue(e.bearer)).trim();return t.length===0?void 0:`Bearer ${t}`}if(`basic`in e){let t=await resolveTokenValue(e.basic.password);return`Basic ${encodeBasicCredentials(e.basic.username,t)}`}}}};async function resolveTokenValue(e){return typeof e==`function`?e():e}async function resolveHeadersValue(e){return e===void 0?{}:typeof e==`function`?await e():e}function headersInitToRecord(e){return e===void 0?{}:Object.fromEntries(new Headers(e).entries())}function withRedirectPolicy(e,t){return t===void 0?e:{...e,redirect:t}}function encodeBasicCredentials(e,t){let n=new TextEncoder().encode(`${e}:${t}`),r=Array.from(n,e=>String.fromCodePoint(e)).join(``);return btoa(r)}export{Client};