import{createParser as R}from"eventsource-parser";var d=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:s,fetchIfExists:r=!0,config:a}=e;return(await(await this.client.post("package/instance/create",{packageHandle:e.package,packageVersionHandle:t,handle:s,fetchIfExists:r,config:a})).json())?.packageInstance}async invoke(e){return await this.client.invokePackageMethod(e.base_url,e.method,{method:e.verb||"POST",body:JSON.stringify(e.payload||{}),json:!0})}},f=d;var g=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=g;var u=class{constructor(e){this.client=e}async create(e){let{handle:t,fetch_if_exists:s=!0}=e;return(await(await this.client.post("workspace/create",{handle:t,fetch_if_exists:s})).json())?.workspace}},I=u;var h=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 s=await(await this.client.post("file/get",{id:e.id})).json();return s?.file||s?.data?.file}},b=h;var w=class{constructor(e){this.client=e}async raw(e){return await this.client.get(`block/${e.id}/raw`,{})}async get(e){try{let s=await(await this.client.post("block/get",{id:e.id})).json(),r=s?.block||s?.data?.block;if(!r){await(p=>new Promise(c=>setTimeout(c,p)))(5e3);let n=await(await this.client.post("block/get",{id:e.id})).json();return n?.block||n?.data?.block}return r}catch{await(o=>new Promise(n=>setTimeout(n,o)))(5e3);let a=await(await this.client.post("block/get",{id:e.id})).json();return a?.block||a?.data?.block}}},y=w;var m=class{constructor(){this.package=new f(this),this.workspace=new I(this),this.agent=new P(this),this.block=new y(this),this.file=new b(this)}};var C="https://api.steamship.com/api/v1/";var v={apiBase:C,appBase:C};var k=class i extends m{constructor(t){super();this.config={...v,...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 s={};return this.config.apiKey&&(s.Authorization=`Bearer ${this.config.apiKey}`),t.json&&(s["Content-Type"]="application/json"),t.workspace?s["x-workspace-handle"]=t.workspace:t.workspaceId&&(s["x-workspace-id"]=t.workspaceId),{...s,...t.existing}}url(t){return`${this.config.apiBase}${t}`}async invokePackageMethod(t,s,r){let a=this.workspaceHandleFromBaseUrl(t);r.workspace||(r.workspace=a);let o=`${t}${s}`;r.headers=this.makeHeaders({json:!0,existing:r.headers,workspace:r.workspace||this.config.workspace,workspaceId:r.workspaceId||this.config.workspaceId,baseUrl:r.baseUrl});try{return await fetch(o,r)}catch(n){throw console.log(n),n}}async invokeApi(t,s){let r=this.url(t);return s.headers=this.makeHeaders({json:!0,existing:s.headers,workspace:s.workspace||this.config.workspace,workspaceId:s.workspaceId||this.config.workspaceId,baseUrl:s.baseUrl}),await fetch(r,s)}async get(t){return this.invokeApi(t,{method:"GET"})}async post(t,s){return this.invokeApi(t,{method:"POST",body:JSON.stringify(s)})}switchWorkspace({workspace:t,workspaceId:s}){let r={...this.config};return delete r.workspace,delete r.workspaceId,r.workspace=t,r.workspaceId=s,new i(r)}async eventStream(t,s){let r=await this.invokeApi(t,s),a=new TextDecoder;return new ReadableStream({async start(o){function n(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,o.enqueue(c)}catch(l){o.error(l)}}}let p=R(n);for await(let c of r.body)p.feed(a.decode(c))}})}};var A=new TextDecoder;function x(i){return new TransformStream({transform(e,t){let s=e.id;if(!s){t.error(new Error("Empty Block ID"));return}let r=i.url(`block/${s}/raw`);if(e.mimeType?.startsWith("audio/")){t.enqueue(`[audio](${r})

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

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

`);return}else{if(e.text){t.enqueue(e.text+`

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

`),a()}catch(n){t.error(n),o(n)}})}}})}var z=new TextDecoder("utf-8");function S(i){return new TransformStream({transform(e,t){let s=e.data.blockId;if(!s){t.error(new Error("Empty Block ID"));return}i.block.get({id:s}).then(r=>new Promise((a,o)=>{if(!r){t.error(new Error(`Block ID did not appear to exist ${s}`)),o();return}t.enqueue(r),a()}))}})}var T=k;var ne=T;export{x as BlockStreamToMarkdownStream,S as FileEventStreamToBlockStream,k as Steamship,ne as default};
//# sourceMappingURL=index.mjs.map