"use strict";var d=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var _=Object.prototype.hasOwnProperty;var F=(n,e)=>{for(var t in e)d(n,t,{get:e[t],enumerable:!0})},j=(n,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of O(e))!_.call(n,r)&&r!==t&&d(n,r,{get:()=>e[r],enumerable:!(s=E(e,r))||s.enumerable});return n};var W=n=>j(d({},"__esModule",{value:!0}),n);var $={};F($,{BlockStreamToMarkdownStream:()=>P,FileEventStreamToBlockStream:()=>I,Steamship:()=>m,default:()=>U});module.exports=W($);var B=require("eventsource-parser");var g=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:o}=e;return(await(await this.client.post("package/instance/create",{packageHandle:e.package,packageVersionHandle:t,handle:s,fetchIfExists:r,config:o})).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})}},b=g;var u=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"})}},y=u;var h=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}},C=h;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 s=await(await this.client.post("file/get",{id:e.id})).json();return s?.file||s?.data?.file}},v=w;var f=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 a=await(await this.client.post("block/get",{id:e.id})).json();return a?.block||a?.data?.block}return r}catch{await(i=>new Promise(a=>setTimeout(a,i)))(5e3);let o=await(await this.client.post("block/get",{id:e.id})).json();return o?.block||o?.data?.block}}},x=f;var k=class{constructor(){this.package=new b(this),this.workspace=new C(this),this.agent=new y(this),this.block=new x(this),this.file=new v(this)}};var S="https://api.steamship.com/api/v1/";var T={apiBase:S,appBase:S};var m=class n extends k{constructor(t){super();this.config={...T,...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 o=this.workspaceHandleFromBaseUrl(t);r.workspace||(r.workspace=o);let i=`${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(i,r)}catch(a){throw console.log(a),a}}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 n(r)}async eventStream(t,s){let r=await this.invokeApi(t,s),o=new TextDecoder;return new ReadableStream({async start(i){function a(c){if(c.type==="event"){let A=c.data;try{let l=JSON.parse(A);l[c.event]&&(l=l[c.event]),c.data=l,i.enqueue(c)}catch(l){i.error(l)}}}let p=(0,B.createParser)(a);for await(let c of r.body)p.feed(o.decode(c))}})}};var q=new TextDecoder;function P(n){return new TransformStream({transform(e,t){let s=e.id;if(!s){t.error(new Error("Empty Block ID"));return}let r=n.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(o,i)=>{try{let a=await n.block.raw({id:s});if(!a.ok){let p=new Error(`Got back error streaming block: ${await a.text()}`);t.error(p),i(p);return}for await(let p of a.body){let c=q.decode(p);t.enqueue(c)}t.enqueue(`

`),o()}catch(a){t.error(a),i(a)}})}}})}var ne=new TextDecoder("utf-8");function I(n){return new TransformStream({transform(e,t){let s=e.data.blockId;if(!s){t.error(new Error("Empty Block ID"));return}n.block.get({id:s}).then(r=>new Promise((o,i)=>{if(!r){t.error(new Error(`Block ID did not appear to exist ${s}`)),i();return}t.enqueue(r),o()}))}})}var R=m;var U=R;0&&(module.exports={BlockStreamToMarkdownStream,FileEventStreamToBlockStream,Steamship});
//# sourceMappingURL=index.js.map