import{createParser as E}from"eventsource-parser";var u=class{constructor(e){this.client=e}async createInstance(e){if(!e.handle){let p=await this.client.workspace.create({});e.handle=p.handle}let{version:t,handle:r,fetchIfExists:n=!0,config:a}=e;return(await(await this.client.post("package/instance/create",{packageHandle:e.package,packageVersionHandle:t,handle:r,fetchIfExists:n,config:a})).json())?.packageInstance}async invoke(e){let t=e.base_url;return t[t.length-1]!="/"&&(t=`${t}/`),await this.client.invokePackageMethod(t,e.method,{method:e.verb||"POST",body:JSON.stringify(e.payload||{}),json:!0})}},y=u;var h=class{constructor(e){this.client=e}async respondAsync(e){return await(await this.client.package.invoke({base_url:e.url,method:"async_prompt",payload:e.input,verb:"POST"})).json()}async respond(e){return await(await this.client.package.invoke({base_url:e.url,method:"prompt",payload:e.input,verb:"POST"})).json()}async post(e){return await this.client.package.invoke({base_url:e.url,method:e.path,payload:e.arguments,verb:"POST"})}async get(e){return await this.client.package.invoke({base_url:e.url,method:e.path,payload:e.arguments,verb:"GET"})}},P=h;var f=class{constructor(e){this.client=e}async create(e){let{handle:t,fetch_if_exists:r=!0}=e;return(await(await this.client.post("workspace/create",{handle:t,fetch_if_exists:r})).json())?.workspace}},b=f;var w=class{constructor(e){this.client=e}async stream(e){return await this.client.eventStream(`file/${e.id}/stream`,{})}async raw(e){return await this.client.get(`file/${e.id}/raw`,{})}async get(e){let r=await(await this.client.post("file/get",{id:e.id})).json();return r?.file||r?.data?.file}},S=w;var I=class{constructor(e){this.client=e}async raw(e){return await this.client.get(`block/${e.id}/raw`,{})}async get(e){try{let r=await(await this.client.post("block/get",{id:e.id})).json(),n=r?.block||r?.data?.block;if(!n){await(p=>new Promise(c=>setTimeout(c,p)))(5e3);let o=await(await this.client.post("block/get",{id:e.id})).json();return o?.block||o?.data?.block}return n}catch{await(i=>new Promise(o=>setTimeout(o,i)))(5e3);let a=await(await this.client.post("block/get",{id:e.id})).json();return a?.block||a?.data?.block}}},C=I;var m=class{constructor(){this.package=new y(this),this.workspace=new b(this),this.agent=new P(this),this.block=new C(this),this.file=new S(this)}};var v="https://api.steamship.com/api/v1/";var x={apiBase:v,appBase:v};var d=class s extends m{constructor(t){super();this.config={...x,...t}}workspaceHandleFromBaseUrl(t){try{return t.split("//")[1].split("/")[1]}catch{throw Error(`Error trying to parse workspace handle out of base url: ${t}`)}}makeHeaders(t){let r={};return this.config.apiKey&&(r.Authorization=`Bearer ${this.config.apiKey}`),t.json&&(r["Content-Type"]="application/json"),t.workspace?r["x-workspace-handle"]=t.workspace:t.workspaceId&&(r["x-workspace-id"]=t.workspaceId),{...r,...t.existing}}url(t){return`${this.config.apiBase}${t}`}async invokePackageMethod(t,r,n){let a=this.workspaceHandleFromBaseUrl(t);n.workspace||(n.workspace=a);let i=`${t}${r}`;n.headers=this.makeHeaders({json:!0,existing:n.headers,workspace:n.workspace||this.config.workspace,workspaceId:n.workspaceId||this.config.workspaceId,baseUrl:n.baseUrl});try{return await fetch(i,n)}catch(o){throw console.log(o),o}}async invokeApi(t,r){let n=this.url(t);return r.headers=this.makeHeaders({json:!0,existing:r.headers,workspace:r.workspace||this.config.workspace,workspaceId:r.workspaceId||this.config.workspaceId,baseUrl:r.baseUrl}),await fetch(n,r)}async get(t){return this.invokeApi(t,{method:"GET"})}async post(t,r){return this.invokeApi(t,{method:"POST",body:JSON.stringify(r)})}switchWorkspace({workspace:t,workspaceId:r}){let n={...this.config};return delete n.workspace,delete n.workspaceId,n.workspace=t,n.workspaceId=r,new s(n)}async eventStream(t,r){let n=await this.invokeApi(t,r),a=new TextDecoder;return new ReadableStream({async start(i){function o(c){if(c.type==="event"){let B=c.data;try{let l=JSON.parse(B);l[c.event]&&(l=l[c.event]),c.data=l,i.enqueue(c)}catch(l){i.error(l)}}else console.log("Parser encountered something other than an event")}let p=E(o);for await(let c of n.body)p.feed(a.decode(c));i.close()}})}};var A=new TextDecoder;function k(s){return new TransformStream({transform(e,t){let r=e.id;if(!r){t.error(new Error("Empty Block ID"));return}let n=s.url(`block/${r}/raw`);if(e.mimeType?.startsWith("audio/")){t.enqueue(`[audio](${n})

`);return}else if(e.mimeType?.startsWith("video/")){t.enqueue(`[video](${n})

`);return}else if(e.mimeType?.startsWith("image/")){t.enqueue(`![image](${n})

`);return}else{if(typeof e.text<"u"){e.text&&t.enqueue(e.text+`

`);return}return new Promise(async(a,i)=>{try{let o=await s.block.raw({id:r});if(!o.ok){let p=new Error(`Got back error streaming block: ${await o.text()}`);t.error(p),i(p);return}for await(let p of o.body){let c=A.decode(p);t.enqueue(c)}t.enqueue(`

`),a()}catch(o){t.error(o),i(o)}})}}})}var X=new TextDecoder("utf-8");function g(s){return new TransformStream({transform(e,t){let r=e.data.blockId;if(!r){t.error(new Error("Empty Block ID"));return}try{s.block.get({id:r}).then(n=>new Promise((a,i)=>{if(!n){t.error(new Error(`Block ID did not appear to exist ${r}`)),i();return}t.enqueue(n),a()}))}catch(n){console.log(n),t.error(n);return}}})}function O(s){var e=[];for(var t in s)e.push(encodeURIComponent(t)+"="+encodeURIComponent(s[t]));return e.join("&")}async function T(s,e,t){try{if((s||{}).status?.state=="failed")throw Error(`Exception from server: ${JSON.stringify(s)}`);let r=s?.file?.id,n=s?.task?.requestId||(s?.task||{}).request_id;e=e.switchWorkspace({workspace:void 0,workspaceId:s?.file?.workspaceId||(s?.file||{}).workspace_id});let a={timeoutSeconds:t?.streamTimeoutSeconds||60};n&&(a.tagKindFilter="request-id",a.tagNameFilter=n),t?.minDatetime&&(a.minDatetime=t?.minDatetime);let i=O(a),o=`file/${r}/stream?${i}`;return(await e.eventStream(o,{})).pipeThrough(g(e)).pipeThrough(k(e))}catch(r){throw console.log(r),r}}var R=d;var de=R;export{k as BlockStreamToMarkdownStream,g as FileEventStreamToBlockStream,d as Steamship,T as SteamshipMarkdownStream,de as default};
//# sourceMappingURL=index.mjs.map