UNPKG

@stable-canvas/comfyui-client

Version:

api Client for ComfyUI that supports both NodeJS and Browser environments. It provides full support for all RESTful / WebSocket APIs.

3 lines (2 loc) 50 kB
function t(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=Array(e);n<e;n++)r[n]=t[n];return r}function e(e,n){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(r)return(r=r.call(e)).next.bind(r);if(Array.isArray(e)||(r=function(e,n){if(e){if("string"==typeof e)return t(e,n);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?t(e,n):void 0}}(e))||n&&e&&"number"==typeof e.length){r&&(e=r);var o=0;return function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function n(){return n=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)({}).hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t},n.apply(null,arguments)}function r(t){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},r(t)}function o(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,u(t,e)}function i(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(i=function(){return!!t})()}function s(){s=function(){return e};var t,e={},n=Object.prototype,r=n.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},i=o.iterator||"@@iterator",u=o.asyncIterator||"@@asyncIterator",c=o.toStringTag||"@@toStringTag";function a(t,e,n,r){Object.defineProperty(t,e,{value:n,enumerable:!r,configurable:!r,writable:!r})}try{a({},"")}catch(t){a=function(t,e,n){return t[e]=n}}function l(e,n,r,o){var i=Object.create((n&&n.prototype instanceof p?n:p).prototype);return a(i,"_invoke",function(e,n,r){var o=1;return function(i,s){if(3===o)throw Error("Generator is already running");if(4===o){if("throw"===i)throw s;return{value:t,done:!0}}for(r.method=i,r.arg=s;;){var u=r.delegate;if(u){var c=k(u,r);if(c){if(c===f)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if(1===o)throw o=4,r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);o=3;var a=h(e,n,r);if("normal"===a.type){if(o=r.done?4:2,a.arg===f)continue;return{value:a.arg,done:r.done}}"throw"===a.type&&(o=4,r.method="throw",r.arg=a.arg)}}}(e,r,new S(o||[])),!0),i}function h(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}e.wrap=l;var f={};function p(){}function d(){}function v(){}var m={};a(m,i,function(){return this});var _=Object.getPrototypeOf,g=_&&_(_(j([])));g&&g!==n&&r.call(g,i)&&(m=g);var y=v.prototype=p.prototype=Object.create(m);function w(t){["next","throw","return"].forEach(function(e){a(t,e,function(t){return this._invoke(e,t)})})}function b(t,e){function n(o,i,s,u){var c=h(t[o],t,i);if("throw"!==c.type){var a=c.arg,l=a.value;return l&&"object"==typeof l&&r.call(l,"__await")?e.resolve(l.__await).then(function(t){n("next",t,s,u)},function(t){n("throw",t,s,u)}):e.resolve(l).then(function(t){a.value=t,s(a)},function(t){return n("throw",t,s,u)})}u(c.arg)}var o;a(this,"_invoke",function(t,r){function i(){return new e(function(e,o){n(t,r,e,o)})}return o=o?o.then(i,i):i()},!0)}function k(e,n){var r=n.method,o=e.i[r];if(o===t)return n.delegate=null,"throw"===r&&e.i.return&&(n.method="return",n.arg=t,k(e,n),"throw"===n.method)||"return"!==r&&(n.method="throw",n.arg=new TypeError("The iterator does not provide a '"+r+"' method")),f;var i=h(o,e.i,n.arg);if("throw"===i.type)return n.method="throw",n.arg=i.arg,n.delegate=null,f;var s=i.arg;return s?s.done?(n[e.r]=s.value,n.next=e.n,"return"!==n.method&&(n.method="next",n.arg=t),n.delegate=null,f):s:(n.method="throw",n.arg=new TypeError("iterator result is not an object"),n.delegate=null,f)}function P(t){this.tryEntries.push(t)}function x(e){var n=e[4]||{};n.type="normal",n.arg=t,e[4]=n}function S(t){this.tryEntries=[[-1]],t.forEach(P,this),this.reset(!0)}function j(e){if(null!=e){var n=e[i];if(n)return n.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var o=-1,s=function n(){for(;++o<e.length;)if(r.call(e,o))return n.value=e[o],n.done=!1,n;return n.value=t,n.done=!0,n};return s.next=s}}throw new TypeError(typeof e+" is not iterable")}return d.prototype=v,a(y,"constructor",v),a(v,"constructor",d),a(v,c,d.displayName="GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===d||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,v):(t.__proto__=v,a(t,c,"GeneratorFunction")),t.prototype=Object.create(y),t},e.awrap=function(t){return{__await:t}},w(b.prototype),a(b.prototype,u,function(){return this}),e.AsyncIterator=b,e.async=function(t,n,r,o,i){void 0===i&&(i=Promise);var s=new b(l(t,n,r,o),i);return e.isGeneratorFunction(n)?s:s.next().then(function(t){return t.done?t.value:s.next()})},w(y),a(y,c,"Generator"),a(y,i,function(){return this}),a(y,"toString",function(){return"[object Generator]"}),e.keys=function(t){var e=Object(t),n=[];for(var r in e)n.unshift(r);return function t(){for(;n.length;)if((r=n.pop())in e)return t.value=r,t.done=!1,t;return t.done=!0,t}},e.values=j,S.prototype={constructor:S,reset:function(e){if(this.prev=this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(x),!e)for(var n in this)"t"===n.charAt(0)&&r.call(this,n)&&!isNaN(+n.slice(1))&&(this[n]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0][4];if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var n=this;function r(t){s.type="throw",s.arg=e,n.next=t}for(var o=n.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],s=i[4],u=this.prev,c=i[1],a=i[2];if(-1===i[0])return r("end"),!1;if(!c&&!a)throw Error("try statement without catch or finally");if(null!=i[0]&&i[0]<=u){if(u<c)return this.method="next",this.arg=t,r(c),!0;if(u<a)return r(a),!1}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var r=this.tryEntries[n];if(r[0]>-1&&r[0]<=this.prev&&this.prev<r[2]){var o=r;break}}o&&("break"===t||"continue"===t)&&o[0]<=e&&e<=o[2]&&(o=null);var i=o?o[4]:{};return i.type=t,i.arg=e,o?(this.method="next",this.next=o[2],f):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),f},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n[2]===t)return this.complete(n[4],n[3]),x(n),f}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n[0]===t){var r=n[4];if("throw"===r.type){var o=r.arg;x(n)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,n,r){return this.delegate={i:j(e),r:n,n:r},"next"===this.method&&(this.arg=t),f}},e}function u(t,e){return u=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},u(t,e)}function c(t){var e=function(t){if("object"!=typeof t||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var n=e.call(t,"string");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==typeof e?e:e+""}function a(t){var e="function"==typeof Map?new Map:void 0;return a=function(t){if(null===t||!function(t){try{return-1!==Function.toString.call(t).indexOf("[native code]")}catch(e){return"function"==typeof t}}(t))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,n)}function n(){return function(t,e,n){if(i())return Reflect.construct.apply(null,arguments);var r=[null];r.push.apply(r,e);var o=new(t.bind.apply(t,r));return n&&u(o,n.prototype),o}(t,arguments,r(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),u(n,t)},a(t)}var l,h,f,p=(l=function(t){var e=Object.prototype.hasOwnProperty,n="~";function r(){}function o(t,e,n){this.fn=t,this.context=e,this.once=n||!1}function i(t,e,r,i,s){if("function"!=typeof r)throw new TypeError("The listener must be a function");var u=new o(r,i||t,s),c=n?n+e:e;return t._events[c]?t._events[c].fn?t._events[c]=[t._events[c],u]:t._events[c].push(u):(t._events[c]=u,t._eventsCount++),t}function s(t,e){0===--t._eventsCount?t._events=new r:delete t._events[e]}function u(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),u.prototype.eventNames=function(){var t,r,o=[];if(0===this._eventsCount)return o;for(r in t=this._events)e.call(t,r)&&o.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?o.concat(Object.getOwnPropertySymbols(t)):o},u.prototype.listeners=function(t){var e=this._events[n?n+t:t];if(!e)return[];if(e.fn)return[e.fn];for(var r=0,o=e.length,i=new Array(o);r<o;r++)i[r]=e[r].fn;return i},u.prototype.listenerCount=function(t){var e=this._events[n?n+t:t];return e?e.fn?1:e.length:0},u.prototype.emit=function(t,e,r,o,i,s){var u=n?n+t:t;if(!this._events[u])return!1;var c,a,l=this._events[u],h=arguments.length;if(l.fn){switch(l.once&&this.removeListener(t,l.fn,void 0,!0),h){case 1:return l.fn.call(l.context),!0;case 2:return l.fn.call(l.context,e),!0;case 3:return l.fn.call(l.context,e,r),!0;case 4:return l.fn.call(l.context,e,r,o),!0;case 5:return l.fn.call(l.context,e,r,o,i),!0;case 6:return l.fn.call(l.context,e,r,o,i,s),!0}for(a=1,c=new Array(h-1);a<h;a++)c[a-1]=arguments[a];l.fn.apply(l.context,c)}else{var f,p=l.length;for(a=0;a<p;a++)switch(l[a].once&&this.removeListener(t,l[a].fn,void 0,!0),h){case 1:l[a].fn.call(l[a].context);break;case 2:l[a].fn.call(l[a].context,e);break;case 3:l[a].fn.call(l[a].context,e,r);break;case 4:l[a].fn.call(l[a].context,e,r,o);break;default:if(!c)for(f=1,c=new Array(h-1);f<h;f++)c[f-1]=arguments[f];l[a].fn.apply(l[a].context,c)}}return!0},u.prototype.on=function(t,e,n){return i(this,t,e,n,!1)},u.prototype.once=function(t,e,n){return i(this,t,e,n,!0)},u.prototype.removeListener=function(t,e,r,o){var i=n?n+t:t;if(!this._events[i])return this;if(!e)return s(this,i),this;var u=this._events[i];if(u.fn)u.fn!==e||o&&!u.once||r&&u.context!==r||s(this,i);else{for(var c=0,a=[],l=u.length;c<l;c++)(u[c].fn!==e||o&&!u[c].once||r&&u[c].context!==r)&&a.push(u[c]);a.length?this._events[i]=1===a.length?a[0]:a:s(this,i)}return this},u.prototype.removeAllListeners=function(t){var e;return t?this._events[e=n?n+t:t]&&s(this,e):(this._events=new r,this._eventsCount=0),this},u.prototype.off=u.prototype.removeListener,u.prototype.addListener=u.prototype.on,u.prefixed=n,u.EventEmitter=u,t.exports=u},l(h={exports:{}}),h.exports),d=function(t){return null==t};!function(t){var e=/*#__PURE__*/function(t){function e(e,n,r){var o;return(o=t.call(this,e)||this).status=void 0,o.json=void 0,o.name="HttpError",o.status=n,o.json=r,o}return o(e,t),e}(/*#__PURE__*/a(Error));t.HttpError=e}(f||(f={}));var v=/*#__PURE__*/function(){function t(e){var n,r,o,i,s,u,c;if(this.api_host=void 0,this.api_base=void 0,this.clientId=void 0,this.socket=void 0,this.WebSocket=void 0,this.ssl=void 0,this.user=void 0,this.fetch=void 0,this.events=new p,this.socket_callbacks={},this._polling_timer=null,this._polling_interval=1e3,this.closed=!1,this.api_host=null!=(n=e.api_host)?n:t.DEFAULT_API_HOST,this.api_base=null!=(r=e.api_base)?r:t.DEFAULT_API_BASE,this.clientId=null!=(o=e.clientId)?o:"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var e=16*Math.random()|0;return("x"===t?e:3&e|8).toString(16)}),this.WebSocket=null!=(i=e.WebSocket)?i:globalThis.WebSocket,this.ssl=null!=(s=e.ssl)&&s,this.user=null!=(u=e.user)?u:t.DEFAULT_USER,!globalThis.fetch)throw new Error("fetch is not defined");this.fetch=null!=(c=e.fetch)?c:globalThis.fetch.bind(globalThis),this.WebSocket||console.warn("No WebSocket implementation available, WebSocket disabled")}t.loadImageData=function(t){var e=new DataView(t),n=e.getUint32(0),r=e.getUint32(1);if(1!==n)throw new Error("Unknown binary websocket message of type "+n);var o={1:"image/jpeg",2:"image/png"}[r]||"image/png";return{image:t.slice(8),mime:o}};var e,r,o=t.prototype;return o.apiHeaders=function(t){var e;return n({},this.user?{"Comfy-User":this.user}:{},{Accept:"*/*"},null!=(e=null==t?void 0:t.headers)?e:{})},o.apiURL=function(t){var e=new URL("http"+(this.ssl?"s":"")+"://"+this.api_host),n=(this.api_base+t).split("?"),r=n[1];return e.pathname=n[0],e.pathname=e.pathname.replace(/\/+/g,"/"),r&&(e.search=r),this.clientId&&e.searchParams.set("clientId",this.clientId),e.toString()},o.viewURL=function(t,e,n){var r=new URLSearchParams({filename:t,subfolder:e,type:n}).toString();return"http"+(this.ssl?"s":"")+"://"+this.api_host+this.api_base+"/view?"+r},o.wsURL=function(){var t=new URL("ws"+(this.ssl?"s":"")+"://"+this.api_host);return t.pathname="/ws",this.clientId&&t.searchParams.set("clientId",this.clientId),t.toString()},o.fetchApi=function(t,e){try{var r=this;if(r.closed)throw new Error("Client is closed");var o=r.apiURL(t);return Promise.resolve(r.fetch(o,n({},e,{headers:r.apiHeaders(e)}))).then(function(t){var e=t.status,n=t.statusText,r=function(){if(e<200||e>=400)return Promise.resolve(t.json()).then(function(t){throw new f.HttpError("Endpoint Bad Request ("+e+" "+n+"): "+o,e,t)})}();return r&&r.then?r.then(function(e){return t}):t})}catch(t){return Promise.reject(t)}},o.addEventListener=function(t,e,n){var r=this;return this.events.on(t,e,n),function(){r.events.off(t,e)}},o.on=function(t,e,n){return this.addEventListener(t,e,n)},o.once=function(t,e,n){var r=this;return this.events.once(t,e,n),function(){r.events.off(t,e)}},o.startPollingQueue=function(){var t=this;this._polling_timer||(this._polling_timer=setInterval(function(){try{var e=function(e,n){try{var r=Promise.resolve(t.fetchApi("/prompt")).then(function(e){return Promise.resolve(e.json()).then(function(e){t.events.emit("status",e)})})}catch(t){return n()}return r&&r.then?r.then(void 0,n):r}(0,function(){t.events.emit("status",null)});return Promise.resolve(e&&e.then?e.then(function(){}):void 0)}catch(t){return Promise.reject(t)}},this._polling_interval))},o.addSocketCallback=function(t,e,n,r){var o=this;return this.socket_callbacks[e]=n,t.addEventListener(e,n,r),function(){delete o.socket_callbacks[e],t.removeEventListener(e,n,r)}},o.removeSocketCallbacks=function(){if(this.socket)for(var t in this.socket_callbacks)this.socket.removeEventListener(t,this.socket_callbacks[t]);this.socket_callbacks={}},o.createSocket=function(e){var n=this;if(void 0===e&&(e=!1),!this.socket){if(!this.WebSocket)throw new Error("WebSocket is not defined, please provide a WebSocket implementation");if(!this.closed){var r=!1;this.socket=new this.WebSocket(this.wsURL()),this.socket.binaryType="arraybuffer",this.addSocketCallback(this.socket,"open",function(){r=!0,n.events.emit(e?"reconnected":"connected")}),this.addSocketCallback(this.socket,"error",function(t){var o,i=t,s=null==(o=i.message)?void 0:o.includes("404");n.events.emit("connection_error",{type:"404",message:i.message}),n.socket&&n.socket.close(),s||e||r||n.startPollingQueue()}),this.addSocketCallback(this.socket,"close",function(){setTimeout(function(){n.socket=null,n.createSocket(!0)},300),r&&(n.events.emit("status",null),n.events.emit("reconnecting"))}),this.addSocketCallback(this.socket,"message",function(e){if(n.events.emit("message",e),function(t){return!("string"==typeof t.data||!(ArrayBuffer&&t.data instanceof ArrayBuffer||Buffer&&Buffer.isBuffer(t.data)))}(e)){var r=t.loadImageData(e.data);n.events.emit("image_data",r)}else{var o=JSON.parse(e.data);switch(o.type){case"status":o.data.sid&&(n.clientId=o.data.sid),n.events.emit("status",o.data.status);break;case"progress":n.events.emit("progress",o.data);break;case"executing":n.events.emit("executing",o.data);break;case"executed":n.events.emit("executed",o.data);break;case"execution_start":n.events.emit("execution_start",o.data);break;case"execution_error":n.events.emit("execution_error",o.data);break;case"execution_cached":n.events.emit("execution_cached",o.data);break;case"execution_interrupted":n.events.emit("execution_interrupted",o.data);break;default:n.events.emit(o.type,o.data)}"message"!==o.type&&!1===n.registered.includes(o.type)&&n.events.emit("unhandled",o)}})}}},o.init=function(){this.createSocket()},o.close=function(){this.closed||(this.closed=!0,this.events.emit("close"),this.disconnect(),this.events.removeAllListeners())},o.connect=function(t){var e,n=this,r=this,o=void 0===t?{}:t,i=o.polling,s=void 0===i?{enabled:!1}:i,u=o.websocket,c=void 0===u?{enabled:!0}:u,a=o.timeout_ms,l=void 0===a?15e3:a;if(null!=s&&s.enabled)return this._polling_interval=null!=(e=s.interval)?e:this._polling_interval,this.startPollingQueue(),new Promise(function(t,e){try{var n=setTimeout(function(){e(new Error("Polling connection timed out"))},l);return Promise.resolve(r.fetchApi("/system_stats")).then(function(e){t(e.ok&&200===e.status),clearTimeout(n)})}catch(t){return Promise.reject(t)}});if(null!=c&&c.enabled)return this.createSocket(),new Promise(function(t,e){var r=setTimeout(function(){e(new Error("WebSocket connection timed out"))},l);n.once("connected",function(){t(!0),clearTimeout(r)})});throw new Error("You must enable either polling or websocket")},o.disconnect=function(){this.socket?this._disconnectSocket():process.nextTick(this._disconnectPolling.bind(this)),this._disconnectPolling()},o._disconnectSocket=function(){var t=this.socket;if(t){this.socket=null;try{t.readyState===t.OPEN&&t.close(1e3,"Client closed")}catch(t){}this.removeSocketCallbacks(),"removeAllListeners"in t&&(null==t.removeAllListeners||t.removeAllListeners())}},o._disconnectPolling=function(){null!==this._polling_timer&&(clearInterval(this._polling_timer),this._polling_timer=null)},e=t,(r=[{key:"registered",get:function(){return this.events.eventNames()}}])&&function(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,c(r.key),r)}}(e.prototype,r),Object.defineProperty(e,"prototype",{writable:!1}),e}();v.DEFAULT_API_HOST="127.0.0.1:8188",v.DEFAULT_API_BASE="",v.DEFAULT_USER="",v.IS_BROWSER="undefined"!=typeof window;var m=/*#__PURE__*/function(){function t(){this._cached=void 0,this._cached=globalThis[t.KEY]||new Map,globalThis[t.KEY]=this._cached}var e=t.prototype;return e.clear=function(){this._cached.clear()},e.get=function(t){return this._cached.get(t)},e.set=function(t,e){this._cached.set(t,e)},t}();m.KEY="__COMFY_UI_CLIENT_CACHE__";var _=/*#__PURE__*/function(){function t(e,n){var r,o;this.expire_time_ms=void 0,this.enabled=void 0,this._cached=new m,this.cache_ns="",this.expire_time_ms=null!=(r=null==n?void 0:n.expire_time)?r:t._defaultExpire,this.enabled=null==(o=null==n?void 0:n.enabled)||o,this.cache_ns=e}var e=t.prototype;return e.reset=function(){this._cached.clear()},e._hashArgs=function(t){try{return JSON.stringify(t)}catch(e){return t.toString()}},e.warp=function(t,e){var n=this;return this.enabled?function(){var r=[].slice.call(arguments),o=Date.now(),i=n._hashArgs(r),s=n.cache_ns+":"+t+":"+i,u=n._cached.get(s);if(u&&u.expire>o)return u.result;var c=e.apply(void 0,r);return n._cached.set(s,{result:c,expire:o+n.expire_time_ms}),c}:e},t}();_._defaultExpire=6e4;var g={image:function(t,e,r){var o=r.client;if(null==e)return t;var i=((null==e?void 0:e.images)||[]).map(function(t){var e=t.filename,n=t.subfolder,r=t.type;return d(e)||d(n)||"output"!==r?null:o.viewURL(e,n,r)}).filter(Boolean).map(function(t){return{type:"url",data:t}});return n({},t,{images:[].concat(t.images,i)})}},y={__proto__:null,RESOLVERS:g};function w(t,e){try{var n=t()}catch(t){return e(t)}return n&&n.then?n.then(void 0,e):n}function b(t,e,n){if(!t.s){if(n instanceof k){if(!n.s)return void(n.o=b.bind(null,t,e));1&e&&(e=n.s),n=n.v}if(n&&n.then)return void n.then(b.bind(null,t,e),b.bind(null,t,2));t.s=e,t.v=n;var r=t.o;r&&r(t)}}var k=/*#__PURE__*/function(){function t(){}return t.prototype.then=function(e,n){var r=new t,o=this.s;if(o){var i=1&o?e:n;if(i){try{b(r,1,i(this.v))}catch(t){b(r,2,t)}return r}return this}return this.o=function(t){try{var o=t.v;1&t.s?b(r,1,e?e(o):o):n?b(r,1,n(o)):b(r,2,o)}catch(t){b(r,2,t)}},r},t}();function P(t){return t instanceof k&&1&t.s}var x=/*#__PURE__*/function(t){function e(e){var n;return(n=t.call(this,e)||this)._cached_fn=void 0,n._plugins=[],n._cached_fn=new _(""+e.api_host,e.cache),n}o(e,t);var r=e.prototype;return r.use=function(t){t.install(this),this._plugins.push(t)},r.getExtensions=function(){try{var t=this,e=t._cached_fn.warp("extensions",function(){try{return Promise.resolve(t.fetchApi("/extensions",{cache:"no-store"})).then(function(t){return Promise.resolve(t.json())})}catch(t){return Promise.reject(t)}});return Promise.resolve(e())}catch(t){return Promise.reject(t)}},r.getEmbeddings=function(){try{var t=this,e=t._cached_fn.warp("embeddings",function(){try{return Promise.resolve(t.fetchApi("/embeddings",{cache:"no-store"})).then(function(t){return Promise.resolve(t.json())})}catch(t){return Promise.reject(t)}});return Promise.resolve(e())}catch(t){return Promise.reject(t)}},r.getNodeDefs=function(){try{var t=this,e=t._cached_fn.warp("object_info",function(){try{return Promise.resolve(t.fetchApi("/object_info",{cache:"no-store"})).then(function(t){return Promise.resolve(t.json())})}catch(t){return Promise.reject(t)}});return Promise.resolve(e())}catch(t){return Promise.reject(t)}},r.resetCache=function(){this._cached_fn.reset()},r.queuePrompt=function(t,e){var n=e.prompt,r=e.workflow;try{var o={client_id:this.clientId,prompt:n,extra_data:{extra_pnginfo:{workflow:r}}};return-1===t?o.front=!0:0!==t&&(o.number=t),Promise.resolve(this.fetchApi("/prompt",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)})).then(function(t){function e(e){return Promise.resolve(t.json())}var n=function(){if(200!==t.status)return Promise.resolve(t.text()).then(function(t){try{throw{response:JSON.parse(t)}}catch(e){throw{response:t}}})}();return n&&n.then?n.then(e):e()})}catch(t){return Promise.reject(t)}},r.getItems=function(t){try{return Promise.resolve("queue"===t?this.getQueue():this.getHistory())}catch(t){return Promise.reject(t)}},r.getQueue=function(){try{var t=this;return Promise.resolve(w(function(){return Promise.resolve(t.fetchApi("/queue")).then(function(e){return Promise.resolve(e.json()).then(function(e){return{Running:e.queue_running.map(function(e){return{prompt:e,remove:{name:"Cancel",cb:function(){return t.interrupt()}}}}),Pending:e.queue_pending.map(function(t){return{prompt:t}})}})})},function(t){return console.error(t),{Running:[],Pending:[]}}))}catch(t){return Promise.reject(t)}},r.getHistory=function(t){void 0===t&&(t=200);try{var e=this;return Promise.resolve(w(function(){return Promise.resolve(e.fetchApi("/history?max_items="+t)).then(function(t){return Promise.resolve(t.json()).then(function(t){return{History:Object.values(t)}})})},function(t){return console.error(t),{History:[]}}))}catch(t){return Promise.reject(t)}},r.getSystemStats=function(){try{return Promise.resolve(this.fetchApi("/system_stats")).then(function(t){return t.json()})}catch(t){return Promise.reject(t)}},r.postApi=function(t,e){try{return Promise.resolve(this.fetchApi("/"+t,{method:"POST",headers:{"Content-Type":"application/json"},body:e?JSON.stringify(e):void 0})).then(function(){})}catch(t){return Promise.reject(t)}},r.deleteItem=function(t,e){try{return Promise.resolve(this.postApi(t,{delete:[e]})).then(function(){})}catch(t){return Promise.reject(t)}},r.clearItems=function(t){try{return Promise.resolve(this.postApi(t,{clear:!0})).then(function(){})}catch(t){return Promise.reject(t)}},r.interrupt=function(){try{return Promise.resolve(this.postApi("interrupt",null)).then(function(){})}catch(t){return Promise.reject(t)}},r.free=function(t){try{return Promise.resolve(this.postApi("free",t)).then(function(){})}catch(t){return Promise.reject(t)}},r.getUserConfig=function(){try{return Promise.resolve(this.fetchApi("/users")).then(function(t){return t.json()})}catch(t){return Promise.reject(t)}},r.createUser=function(t){try{return Promise.resolve(this.fetchApi("/users",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({username:t})}))}catch(t){return Promise.reject(t)}},r.getSettings=function(){try{return Promise.resolve(this.fetchApi("/settings")).then(function(t){return t.json()})}catch(t){return Promise.reject(t)}},r.getSetting=function(t){try{return Promise.resolve(this.fetchApi("/settings/"+encodeURIComponent(t))).then(function(t){return t.json()})}catch(t){return Promise.reject(t)}},r.storeSettings=function(t){try{return Promise.resolve(this.fetchApi("/settings",{method:"POST",body:JSON.stringify(t)}))}catch(t){return Promise.reject(t)}},r.storeSetting=function(t,e){try{return Promise.resolve(this.fetchApi("/settings/"+encodeURIComponent(t),{method:"POST",body:JSON.stringify(e)}))}catch(t){return Promise.reject(t)}},r.getUserData=function(t,e){try{return Promise.resolve(this.fetchApi("/userdata/"+encodeURIComponent(t),e))}catch(t){return Promise.reject(t)}},r.storeUserData=function(t,e,r){try{return Promise.resolve(this.fetchApi("/userdata/"+encodeURIComponent(t),n({method:"POST",body:null!=r&&r.stringify?JSON.stringify(e):e},r))).then(function(e){return function(){if(200!==e.status)return Promise.resolve(e.text()).then(function(n){throw new Error("Error storing user data file '"+t+"': "+e.status+" "+n)})}()})}catch(t){return Promise.reject(t)}},r.getSamplers=function(){try{return Promise.resolve(this.getNodeDefs()).then(function(t){var e,n=t.KSampler;return(null==n||null==(e=n.input)||null==(e=e.required)||null==(e=e.sampler_name)?void 0:e[0])||[]})}catch(t){return Promise.reject(t)}},r.getSchedulers=function(){try{return Promise.resolve(this.getNodeDefs()).then(function(t){var e,n=t.KSampler;return(null==n||null==(e=n.input)||null==(e=e.required)||null==(e=e.scheduler)?void 0:e[0])||[]})}catch(t){return Promise.reject(t)}},r.getSDModels=function(){try{return Promise.resolve(this.getNodeDefs()).then(function(t){var e,n=t.CheckpointLoaderSimple;return(null==n||null==(e=n.input)||null==(e=e.required)||null==(e=e.ckpt_name)?void 0:e[0])||[]})}catch(t){return Promise.reject(t)}},r.getCNetModels=function(){try{return Promise.resolve(this.getNodeDefs()).then(function(t){var e,n=t.ControlNetLoader;return(null==n||null==(e=n.input)||null==(e=e.required)||null==(e=e.control_net_name)?void 0:e[0])||[]})}catch(t){return Promise.reject(t)}},r.getUpscaleModels=function(){try{return Promise.resolve(this.getNodeDefs()).then(function(t){var e,n=t.UpscaleModelLoader;return(null==n||null==(e=n.input)||null==(e=e.required)||null==(e=e.model_name)?void 0:e[0])||[]})}catch(t){return Promise.reject(t)}},r.getHyperNetworks=function(){try{return Promise.resolve(this.getNodeDefs()).then(function(t){var e,n=t.HypernetworkLoader;return(null==n||null==(e=n.input)||null==(e=e.required)||null==(e=e.hypernetwork_name)?void 0:e[0])||[]})}catch(t){return Promise.reject(t)}},r.getLoRAs=function(){try{return Promise.resolve(this.getNodeDefs()).then(function(t){var e,n=t.LoraLoader;return(null==n||null==(e=n.input)||null==(e=e.required)||null==(e=e.lora_name)?void 0:e[0])||[]})}catch(t){return Promise.reject(t)}},r.getVAEs=function(){try{return Promise.resolve(this.getNodeDefs()).then(function(t){var e,n=t.VAELoader;return(null==n||null==(e=n.input)||null==(e=e.required)||null==(e=e.vae_name)?void 0:e[0])||[]})}catch(t){return Promise.reject(t)}},r.getPromptStatus=function(t){try{return Promise.resolve(this.getQueue()).then(function(e){var n=e.Pending,r=e.Running.some(function(e){var n;return(null==e||null==(n=e.prompt)?void 0:n[1])===t}),o=n.some(function(e){var n;return(null==e||null==(n=e.prompt)?void 0:n[1])===t});return{running:r,pending:o,done:!r&&!o}})}catch(t){return Promise.reject(t)}},r.getPromptOutputs=function(t){try{return Promise.resolve(this.getHistory()).then(function(e){var n=e.History.find(function(e){return e.prompt[1]===t});if(!n)throw new Error("Prompt ["+t+"] not found in history");var r=n.status.status_str;if("success"!==r)throw new Error("Prompt ["+t+"] failed with status: "+r);return n.outputs})}catch(t){return Promise.reject(t)}},r.getPromptResult=function(t,e){try{var n=this;return Promise.resolve(n.getPromptOutputs(t)).then(function(r){return"function"!=typeof e&&(e=g.image),Object.entries(r).reduce(function(r,o){return e(r,o[1],{client:n,prompt_id:t,node_id:o[0]})},{images:[],prompt_id:t,data:null})})}catch(t){return Promise.reject(t)}},r.waitForPrompt=function(t,e){void 0===e&&(e=1e3);try{var n=this;return Promise.resolve(n.getPromptStatus(t)).then(function(r){var o=function(t,e,n){for(var r;;){var o=t();if(P(o)&&(o=o.v),!o)return i;if(o.then){r=0;break}var i=n();if(i&&i.then){if(!P(i)){r=1;break}i=i.s}}var s=new k,u=b.bind(null,s,2);return(0===r?o.then(a):1===r?i.then(c):(void 0).then(function(){(o=t())?o.then?o.then(a).then(void 0,u):a(o):b(s,1,i)})).then(void 0,u),s;function c(e){i=e;do{if(!(o=t())||P(o)&&!o.v)return void b(s,1,i);if(o.then)return void o.then(a).then(void 0,u);P(i=n())&&(i=i.v)}while(!i||!i.then);i.then(c).then(void 0,u)}function a(t){t?(i=n())&&i.then?i.then(c).then(void 0,u):c(i):b(s,1,i)}}(function(){return!r.done},0,function(){return Promise.resolve(new Promise(function(t){return setTimeout(t,e)})).then(function(){return Promise.resolve(n.getPromptStatus(t)).then(function(t){r=t})})});if(o&&o.then)return o.then(function(){})})}catch(t){return Promise.reject(t)}},r.waitForPromptWebSocket=function(t,e){try{var n=this,r={images:[],prompt_id:t,data:null};return Promise.resolve(new Promise(function(o,i){var s=n.on("image_data",function(t){r.images.push({type:"buff",data:t.image,mime:t.mime})}),u=n.on("executed",function(i){if(i.prompt_id===t){var c=e(r,i.output,{client:n,prompt_id:t,node_id:i.node});o(c),u(),s()}})}))}catch(t){return Promise.reject(t)}},r._enqueue_prompt=function(t,e){try{return Promise.resolve(this.queuePrompt(0,{prompt:t,workflow:null==e?void 0:e.workflow})).then(function(t){if("error"in t)throw new Error(t.error);return t})}catch(t){return Promise.reject(t)}},r.runPrompt=function(t,e){try{var n=this;return Promise.resolve(n._enqueue_prompt(t,e)).then(function(t){var r=t.prompt_id;return Promise.resolve(n.waitForPrompt(r,null==e?void 0:e.polling_ms)).then(function(){return Promise.resolve(n.getPromptResult(r,g.image))})})}catch(t){return Promise.reject(t)}},r.enqueue_polling=function(t,e){try{var n=this;if("function"==typeof(null==e?void 0:e.progress))throw new Error("progress option is not supported in polling mode");return Promise.resolve(n._enqueue_prompt(t,e)).then(function(t){var r=t.prompt_id;return Promise.resolve(n.waitForPrompt(r,null==e?void 0:e.polling_ms)).then(function(){var t;return Promise.resolve(n.getPromptResult(r,null!=(t=null==e?void 0:e.resolver)?t:g.image))})})}catch(t){return Promise.reject(t)}},r.enqueue=function(t,e){try{var n=this;return Promise.resolve(n._enqueue_prompt(t,e)).then(function(t){var r=t.prompt_id,o=n.on_progress(null==e?void 0:e.progress,r);return function(t,o){try{var i=Promise.resolve(n.waitForPromptWebSocket(r,null!=(s=null==e?void 0:e.resolver)?s:g.image))}catch(t){return o(!0,t)}var s;return i&&i.then?i.then(o.bind(null,!1),o.bind(null,!0)):o(!1,i)}(0,function(t,e){if(o(),t)throw e;return e})})}catch(t){return Promise.reject(t)}},r.on_progress=function(t,e){return t?this.on("progress",function(r){var o=n({},"progress"in r?n({},r.progress):{},r);o.prompt_id===e&&t(o)}):function(){}},e}(v),S=/*#__PURE__*/function(){function t(){this._disposed=!1,this._disposed_cbs=[]}var e=t.prototype;return e.dispose=function(){this._disposed||(this._disposed=!0,this._disposed_cbs.forEach(function(t){"function"==typeof t&&t()}))},e._connect=function(t){this._disposed?t():this._disposed_cbs.push(t)},t}();function j(t,e){try{var n=t()}catch(t){return e(t)}return n&&n.then?n.then(void 0,e):n}var E,O=/*#__PURE__*/function(t){function e(e){var n;(n=t.call(this)||this).options=void 0,n.task_id=void 0,n._result={images:[],prompt_id:""},n.is_done=!1,n.enqueued=!1,n.workflow=void 0,n.client=void 0,n.resolver=void 0,n.options=e;var r=e.client,o=e.resolver;return n.workflow=e.workflow,n.client=r,n.resolver=o||g.image,n}o(e,t);var n=e.prototype;return n._enqueue_guard=function(){if(this.enqueued)throw new Error("This workflow is already enqueued");this.enqueued=!0},n._task_id_guard=function(){if(!this.task_id)throw new Error("This workflow is not enqueued and the execution status cannot be interrupt");return this.task_id},n._done_guard=function(){if(this._disposed||this.is_done)throw new Error("This workflow has been disposed")},n._ws_guard=function(){if(null===this.client.socket)throw new Error("WebSocket is not connected")},n.is_owner_event=function(){var t=([].slice.call(arguments)||[])[0],e=this.task_id;return!!e&&"object"==typeof t&&null!==t&&"prompt_id"in t&&t.prompt_id===e},n.on=function(t,e,n){var r=this;this._done_guard();var o=this.client.on(t,function(){var t=[].slice.call(arguments);r.is_owner_event.apply(r,t)&&e.apply(void 0,t)});return this._connect(o),o},n.once=function(t,e,n){var r=this;this._done_guard();var o=this.client.on(t,function(){var t=[].slice.call(arguments);r.is_owner_event.apply(r,t)&&(e.apply(void 0,t),o())});return this._connect(o),o},n.enqueue=function(){try{var t=this;t._enqueue_guard();var e=t.workflow;return Promise.resolve(t.client._enqueue_prompt(e.prompt,{workflow:e.workflow})).then(function(e){t.task_id=e.prompt_id,t.hook_progress(),t.hook_image_data()})}catch(t){return Promise.reject(t)}},n.hook_progress=function(){try{var t=this,e=t.options.progress,n=t.task_id;if(!e)return Promise.resolve();if("function"!=typeof e)throw new Error("progress hook must be a function");if("string"!=typeof n)throw new Error("this workflow is not enqueued");var r=t.client.on_progress(e,n);return t._connect(r),Promise.resolve()}catch(t){return Promise.reject(t)}},n.hook_image_data=function(){try{var t=this,e=t.client.on("image_data",function(e){t.is_done||t._result.images.push({type:"buff",data:e.image,mime:e.mime})});return t._connect(e),Promise.resolve()}catch(t){return Promise.reject(t)}},n.resolve_to_result=function(t){this._result=(0,this.resolver)(this._result,t.output,{client:this.client,prompt_id:t.prompt_id,node_id:t.node})},n.query=function(){try{var t=this._task_id_guard();return Promise.resolve(this.client.getPromptStatus(t))}catch(t){return Promise.reject(t)}},n.interrupt=function(){try{var t=this,e=t._task_id_guard();return Promise.resolve(t.query()).then(function(n){if(!n.done){if(!n.pending){if(n.running)return t.client.interrupt();throw new Error("wrong task status, id: "+e)}t.client.deleteItem("queue",e)}})}catch(t){return Promise.reject(t)}},n.collect_result=function(){try{var t=this,e=t.client,n=t.resolver,r=t._task_id_guard();return Promise.resolve(e.getPromptResult(r,null!=n?n:g.image)).then(function(e){var n;return t._result.images=[].concat(t._result.images,e.images),t._result.data=null!=(n=t._result.data)?n:e.data,t._result})}catch(t){return Promise.reject(t)}},n.when_interrupted=function(t){var e=this._task_id_guard();this._connect(this.client.on("execution_interrupted",function(n){n.prompt_id===e&&t(n)}))},n.wait_polling=function(t){var e=(void 0===t?{}:t).polling_ms;try{var n=this;n._done_guard();var r=n._task_id_guard(),o=n.client;return Promise.resolve(new Promise(function(t,i){try{var s=function(){n.is_done=!0,n.dispose()};n.when_interrupted(function(t){i(new Error("Execution Interrupted")),s()});var u=function(s,u){try{var c=j(function(){return Promise.resolve(o.waitForPrompt(r,null!=e?e:1e3)).then(function(){return Promise.resolve(n.collect_result()).then(function(e){t(e)})})},function(t){i(t)})}catch(t){return u(!0,t)}return c&&c.then?c.then(u.bind(null,!1),u.bind(null,!0)):u(!1,c)}(0,function(t,e){if(s(),t)throw e;return e});return Promise.resolve(u&&u.then?u.then(function(){}):void 0)}catch(t){return Promise.reject(t)}}))}catch(t){return Promise.reject(t)}},n.wait=function(){try{var t=this;t._done_guard(),t._ws_guard();var e=t._task_id_guard();return Promise.resolve(new Promise(function(n,r){var o=function(){t.is_done=!0,t.dispose()},i=function(){try{return Promise.resolve(j(function(){return Promise.resolve(t.query()).then(function(e){e.done&&(n(t._result),o())})},function(t){r(t),o()}))}catch(t){return Promise.reject(t)}};t.when_interrupted(function(t){r(new Error("Execution Interrupted")),o()}),t._connect(i),t._connect(t.client.on("executed",function(n){try{return n.prompt_id!==e||(t.resolve_to_result(n),i()),Promise.resolve()}catch(t){return Promise.reject(t)}})),t._connect(t.client.on("execution_success",function(t){try{return t.prompt_id!==e||i(),Promise.resolve()}catch(t){return Promise.reject(t)}}))}))}catch(t){return Promise.reject(t)}},e}(S),A=globalThis.structuredClone?globalThis.structuredClone:function(t){return JSON.parse(JSON.stringify(t))},L=/*#__PURE__*/function(){function t(){this._workflow={prompt:{}},this._last_node_id=0,this.classes=this._createClassesProxy()}var e=t.prototype;return e._createClassesProxy=function(){var t=this;return new Proxy({},{get:function(e,n,r){return n in e?e[n]:function(e){return t.node(n,e)}}})},e.node=function(t,e){var n=/*#__PURE__*/s().mark(i),r={class_type:t,inputs:e},o=(++this._last_node_id).toString();function i(){var t;return s().wrap(function(e){for(;;)switch(e.prev=e.next){case 0:t=0;case 1:return e.next=4,[o,t++];case 4:e.next=1;break;case 6:case"end":return e.stop()}},n)}this._workflow.prompt[o]=r;for(var u=i(),c=0;c<24;c++)u[c]=[o,c];return u},e.reset=function(){this._workflow.prompt={},this._workflow.workflow=void 0,this._last_node_id=0},e.end=function(){return this.workflow()},e.workflow=function(){return A(this._workflow)},e._ws_connected_guard=function(t){if(!t.socket||t.socket.readyState!==t.WebSocket.OPEN){var e,n,r,o=(e={},e[t.WebSocket.CLOSED]="CLOSED",e[t.WebSocket.CONNECTING]="CONNECTING",e[t.WebSocket.OPEN]="OPEN",e[t.WebSocket.CLOSING]="CLOSING",e[-1]="UNKNOWN",e)[null!=(n=null==(r=t.socket)?void 0:r.readyState)?n:-1];throw new Error("WebSocket is not connected, cannot invoke workflow. readyState="+o)}},e.invoke=function(t,e){try{this._ws_connected_guard(t);var n=this.instance(t,e);return Promise.resolve(n.enqueue()).then(function(){return n.wait()})}catch(t){return Promise.reject(t)}},e.instance=function(t,e){var n=this.workflow();return new O({workflow:n,client:t,resolver:null==e?void 0:e.resolver,progress:null==e?void 0:e.progress})},e.invoke_polling=function(t,e){if("function"==typeof(null==e?void 0:e.progress))throw new Error("progress option is not supported in polling mode");var n=this.workflow();return t.enqueue_polling(n.prompt,{workflow:n.workflow,resolver:null==e?void 0:e.resolver,polling_ms:null==e?void 0:e.polling_ms})},t}(),T=/*#__PURE__*/function(){function t(){this.hooks=[]}var n=t.prototype;return n.install=function(t){for(var n,r=function(){var e=n.value,r=t,o=r[e.name].bind(t);r[e.name]=function(){return e.fn.bind(t).apply(void 0,[o].concat([].slice.call(arguments)))}},o=e(this.hooks);!(n=o()).done;)r()},n.addHook=function(t){this.hooks.push(t)},t}(),C={__proto__:null,LoginAuthPlugin:/*#__PURE__*/function(t){function e(e){var n,r;return(r=t.call(this)||this).options=void 0,n=this,r.options=e,r.addHook({type:"function",name:"apiURL",fn:function(t){var e=t.apply(void 0,[].slice.call(arguments,1)),r=new URL(e);return r.searchParams.set("token",n.options.token),r.toString()}}),r.addHook({type:"function",name:"wsURL",fn:function(t){var e=t.apply(void 0,[].slice.call(arguments,1)),r=new URL(e);return r.searchParams.set("token",n.options.token),r.toString()}}),r}return o(e,t),e}(T)};function N(t,e,n){if(!t.s){if(n instanceof I){if(!n.s)return void(n.o=N.bind(null,t,e));1&e&&(e=n.s),n=n.v}if(n&&n.then)return void n.then(N.bind(null,t,e),N.bind(null,t,2));t.s=e,t.v=n;var r=t.o;r&&r(t)}}!function(t){t.samplers=["euler","euler_cfg_pp","euler_ancestral","euler_ancestral_cfg_pp","heun","heunpp2","dpm_2","dpm_2_ancestral","lms","dpm_fast","dpm_adaptive","dpmpp_2s_ancestral","dpmpp_sde","dpmpp_sde_gpu","dpmpp_2m","dpmpp_2m_sde","dpmpp_2m_sde_gpu","dpmpp_3m_sde","dpmpp_3m_sde_gpu","ddpm","lcm","ipndm","ipndm_v","deis","ddim","uni_pc","uni_pc_bh2"],t.schedulers=["normal","karras","exponential","sgm_uniform","simple","ddim_uniform","beta"]}(E||(E={}));const I=/*#__PURE__*/function(){function t(){}return t.prototype.then=function(e,n){const r=new t,o=this.s;if(o){const t=1&o?e:n;if(t){try{N(r,1,t(this.v))}catch(t){N(r,2,t)}return r}return this}return this.o=function(t){try{const o=t.v;1&t.s?N(r,1,e?e(o):o):n?N(r,1,n(o)):N(r,2,o)}catch(t){N(r,2,t)}},r},t}();var q="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function U(t){return t instanceof I&&1&t.s}var R=/*#__PURE__*/function(t){function e(r){var o;return(o=t.call(this)||this).context=void 0,o._workflow=new L,o._invoked=void 0,o.context=n({},e.defaultContext,r),o}o(e,t),e.nextSeed=function(){return Math.floor(Math.random()*Math.pow(2,32))};var r=e.prototype;return r.update=function(t){Object.assign(this.context,t)},r.image=function(t){return this.update({input_image:t}),this},r.mask=function(t){return this.update({input_mask:t}),this},r.model=function(t){return this.update({ckpt_name:t}),this},r.size=function(t,e){return this.update({width:t,height:e}),this},r.prompt=function(t){return this.update({positive:t}),this},r.negative=function(t){return this.update({negative:t}),this},r.steps=function(t){return this.update({steps:t}),this},r.cfg=function(t){return this.update({cfg:t}),this},r.seed=function(t){return void 0===t&&(t=e.nextSeed()),this.update({seed:t}),this},r.denoise=function(t){return this.update({denoise:t}),this},r.scheduler=function(t){return this.update({scheduler:t}),this},r.sampler=function(t){return this.update({sampler_name:t}),this},r.batch_size=function(t){return this.update({batch_size:t}),this},r.with=function(t){return this.update({client:t}),this},r.on=function(t,e,n){var r=this._invoked;if(!r)throw new Error("workflow not invoked");return r.then(function(r){r.on(t,e,n)}),this},r.once=function(t,e,n){var r=this._invoked;if(!r)throw new Error("workflow not invoked");return r.then(function(r){r.once(t,e,n)}),this},r.build_latent=function(t){var e=this.context,n=this._workflow.classes,r=this.context,o=r.input_image,i=r.input_mask,s=r.grow_mask_by;if(!o)return n.EmptyLatentImage({width:e.width,height:e.height,batch_size:e.batch_size})[0];var u=n.ETN_LoadImageBase64({image:o.toString("base64")})[0];if(!i)return n.VAEEncode({pixels:u,vae:t})[0];var c=n.ETN_LoadMaskBase64({mask:i.toString("base64")})[0];return n.VAEEncodeForInpaint({pixels:u,vae:t,mask:c,grow_mask_by:s})[0]},r.build=function(){var t=this.context,e=this._workflow.classes,n=t.seed,r=t.steps,o=t.cfg,i=t.scheduler,s=t.denoise,u=t.positive,c=t.negative,a=t.sampler_name,l=e.CheckpointLoaderSimple({ckpt_name:t.ckpt_name}),h=l[1],f=l[2],p=function(t){return e.CLIPTextEncode({text:t,clip:h})[0]};return{samples:e.KSampler({seed:n,steps:r,cfg:o,sampler_name:a,scheduler:i,denoise:s,model:l[0],positive:p(u),negative:p(c),latent_image:this.build_latent(f)})[0],vae:f,cls:e}},r._save=function(t){var e=this.build(),n=e.cls,r=n.VAEDecode({samples:e.samples,vae:e.vae})[0];t?n.SaveImage({filename_prefix:t,images:r}):n.SaveImageWebsocket({images:r})},r.read_response=function(t){try{var e=[],n=function(t,e){if("function"==typeof t[q]){var n,r,o,i=function(t){try{for(;!(n=s.next()).done;)if((t=e(n.value))&&t.then){if(!U(t))return void t.then(i,o||(o=N.bind(null,r=new I,2)));t=t.v}r?N(r,1,t):r=t}catch(t){N(r||(r=new I),2,t)}},s=t[q]();if(i(),s.return){var u=function(t){try{n.done||s.return()}catch(t){}return t};if(r&&r.then)return r.then(u,function(t){throw u(t)});u()}return r}if(!("length"in t))throw new TypeError("Object is not iterable");for(var c=[],a=0;a<t.length;a++)c.push(t[a]);return function(t,e){var n,r,o=-1;return function i(s){try{for(;++o<t.length;)if((s=e(o))&&s.then){if(!U(s))return void s.then(i,r||(r=N.bind(null,n=new I,2)));s=s.v}n?N(n,1,s):n=s}catch(t){N(n||(n=new I),2,t)}}(),n}(c,function(t){return e(c[t])})}(t.images,function(t){var n=function(t,e){var n,r=-1;t:{for(var o=0;o<e.length;o++){var i=e[o][0];if(i){var s=i();if(s&&s.then)break t;if(s===t){r=o;break}}else r=o}if(-1!==r){do{for(var u=e[r][1];!u;)r++,u=e[r][1];var c=u();if(c&&c.then){n=!0;break t}var a=e[r][2];r++}while(a&&!a());return c}}var l=new I,h=N.bind(null,l,2);return(n?c.then(f):s.then(function n(s){for(;;){if(s===t){r=o;break}if(++o===e.length){if(-1!==r)break;return void N(l,1,c)}if(i=e[o][0]){if((s=i())&&s.then)return void s.then(n).then(void 0,h)}else r=o}do{for(var u=e[r][1];!u;)r++,u=e[r][1];var c=u();if(c&&c.then)return void c.then(f).then(void 0,h);var a=e[r][2];r++}while(a&&!a());N(l,1,c)})).then(void 0,h),l;function f(t){for(;;){var n=e[r][2];if(!n||n())break;r++;for(var o=e[r][1];!o;)r++,o=e[r][1];if((t=o())&&t.then)return void t.then(f).then(void 0,h)}N(l,1,t)}}(t.type,[[function(){return"buff"},function(){e.push({data:t.data,mime:t.mime})}],[function(){return"url"},function(){return Promise.resolve(fetch(t.data)).then(function(t){var n,r=null!=(n=t.headers.get("content-type"))?n:"image/png";return Promise.resolve(t.blob()).then(function(t){var n=e.push;return Promise.resolve(t.arrayBuffer()).then(function(t){n.call(e,{data:t,mime:r})})})})}]]);if(n&&n.then)return n.then(function(){})});return Promise.resolve(n&&n.then?n.then(function(){return e}):e)}catch(t){return Promise.reject(t)}},r.save=function(t){var e=this,n=this.context.client,r=this._workflow;if(!n)throw new Error("client is not defined");return this._save(t),this._invoked=function(){try{var t=r.instance(n);return t._connect(function(){return e.dispose()}),Promise.resolve(t.enqueue()).then(function(){return t})}catch(t){return Promise.reject(t)}}(),this},r.wait=function(){try{var t=this,e=t._invoked;if(!e)throw new Error("workflow not invoked");return Promise.resolve(e).then(function(e){return Promise.resolve(e.wait()).then(function(e){return Promise.resolve(t.read_response(e)).then(function(t){return{result:e,images:t}})})})}catch(t){return Promise.reject(t)}},r.wait_polling=function(){try{var t=this,e=t._invoked;if(!e)throw new Error("workflow not invoked");return Promise.resolve(e).then(function(e){return Promise.resolve(e.wait_polling()).then(function(e){return Promise.resolve(t.read_response(e)).then(function(t){return{result:e,images:t}})})})}catch(t){return Promise.reject(t)}},e}(S);R.defaultContext={seed:R.nextSeed(),steps:35,cfg:4,sampler_name:"dpmpp_2m_sde_gpu",scheduler:"karras",denoise:1,width:512,height:512,batch_size:1,ckpt_name:"",input_image:null,input_mask:null,grow_mask_by:6,positive:"",negative:"",client:null};var D=/*#__PURE__*/function(t){function r(e){var o;return(o=t.call(this)||this).context=n({},r.defaultContext,e),o}o(r,t);var i=r.prototype;return i.lora=function(t,e){var n=void 0===e?{}:e,r=n.weight,o=n.strength,i=n.clip_strength;return this.context.loras.push({name:t,weight:void 0===r?1:r,model_strength:void 0===o?1:o,clip_strength:void 0===i?1:i}),this},i.cnet=function(t,e,n){var r=void 0===n?{}:n,o=r.strength,i=r.start,s=r.end;return this.context.control_net_blocks.push({image:e,name:t,strength:void 0===o?1:o,start:void 0===i?0:i,end:void 0===s?1:s}),this},i.build_lora_stack=function(){var t=this.context.loras;if(0!==t.length){for(var e=this._workflow.classes,r={lora_count:t.length},o=0;o<50;o++){var i=t[o];if(i){var s=i.weight,u=i.model_strength,c=i.clip_strength;r["lora_name_"+o]=i.name,r["lora_wt_"+o]=s,r["model_str_"+o]=u,r["clip_str_"+o]=c}else r["lora_name_"+o]="None",r["lora_wt_"+o]=1,r["model_str_"+o]=1,r["clip_str_"+o]=1}return e["LoRA Stacker"](n({input_mode:"advanced"},r))[0]}},i.build_cnet_block=function(t,e){var n=t.image,r=t.strength,o=t.start,i=t.end,s=this._workflow,u=s.classes,c=u.ETN_LoadImageBase64,a=s.classes,l=(0,u.ControlNetLoader)({control_net_name:t.name})[0],h=c({image:n.toString("base64")});return a["Control Net Stacker"]({control_net:l,image:h[0],strength:r,start_percent:o,end_percent:i,cnet_stack:e})[0]},i.build_cnet_stack=function(){var t=this.context.control_net_blocks;if(0!==t.length){for(var n,r=void 0,o=e(t);!(n=o()).done;)r=this.build_cnet_block(n.value,r);return r}},i.build=function(){var t=this.context,e=this._workflow.classes,n=t.seed,r=t.steps,o=t.cfg,i=t.scheduler,s=t.denoise,u=t.sampler_name,c=e["Efficient Loader"]({ckpt_name:t.ckpt_name,vae_name:t.vae_name,clip_skip:t.clip_skip,token_normalization:t.token_normalization,weight_interpretation:t.weight_interpretation,empty_latent_height:t.height,empty_latent_width:t.width,batch_size:t.batch_size,positive:t.positive,negative:t.negative,lora_stack:this.build_lora_stack(),cnet_stack:this.build_cnet_stack(),lora_name:"None",lora_model_strength:1,lora_clip_strength:1}),a=c[4];return{samples:e.KSampler({seed:n,steps:r,cfg:o,sampler_name:u,scheduler:i,denoise:s,model:c[0],positive:c[1],negative:c[2],latent_image:this.build_latent(a)})[0],vae:a,cls:e}},r}(R);D.defaultContext=n({},R.defaultContext,{vae_name:"Baked VAE",clip_skip:-2,token_normalization:"none",weight_interpretation:"A1111",loras:[],control_net_blocks:[]});var W=x,F=v,H=L,B=T;export{R as BasePipe,x as Client,B as ClientPlugin,W as ComfyUIApiClient,H as ComfyUIWorkflow,F as ComfyUIWsClient,D as EfficientPipe,E as NSPipeline,T as Plugin,L as Workflow,v as WsClient,y as builtins,C as plugins}; //# sourceMappingURL=main.module.mjs.map