Added dynamic loading of extensions

This commit is contained in:
pythongosssss 2023-03-03 19:05:39 +00:00
parent c23af92baf
commit 592b377ac4
5 changed files with 34 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import nodes
import execution
import uuid
import json
import glob
try:
import aiohttp
@ -52,6 +53,11 @@ class PromptServer():
async def get_root(request):
return web.FileResponse(os.path.join(self.web_root, "index.html"))
@routes.get("/extensions")
async def get_extensions(request):
files = glob.glob(os.path.join(self.web_root, 'extensions/**/*.js'), recursive=True)
return web.json_response(list(map(lambda f: "/" + os.path.relpath(f, self.web_root).replace("\\", "/"), files)))
@routes.get("/view/{file}")
async def view_image(request):
if "file" in request.match_info:

View File

@ -1,6 +1,7 @@
import { app } from "../scripts/app.js";
const ext = {
// Unique name for the extension
name: "Example.LoggingExtension",
async init(app) {
// Any initial setup to run as soon as the page loads

View File

@ -6,9 +6,6 @@
<script type="text/javascript" src="lib/litegraph.core.js"></script>
<script type="module">
import "/extensions/core/dynamicPrompts.js";
import "/extensions/core/rerouteNode.js";
import { app } from "/scripts/app.js";
await app.setup();
window.app = app;

View File

@ -88,6 +88,15 @@ class ComfyApi extends EventTarget {
this.#createSocket();
}
/**
* Gets a list of extension urls
* @returns An array of script urls to import
*/
async getExtensions() {
const resp = await fetch("/extensions", { cache: "no-store" });
return await resp.json();
}
/**
* Loads node object definitions for the graph
* @returns The node definitions

View File

@ -11,14 +11,6 @@ class ComfyApp {
this.nodeOutputs = {};
}
#log(message, ...other) {
console.log("[comfy]", message, ...other);
}
#error(message, ...other) {
console.error("[comfy]", message, ...other);
}
/**
* Invoke an extension callback
* @param {string} method The extension callback to execute
@ -32,7 +24,7 @@ class ComfyApp {
try {
results.push(ext[method](...args, this));
} catch (error) {
this.#error(
console.error(
`Error calling extension '${ext.name}' method '${method}'`,
{ error },
{ extension: ext },
@ -58,7 +50,7 @@ class ComfyApp {
try {
return await ext[method](...args, this);
} catch (error) {
this.#error(
console.error(
`Error calling extension '${ext.name}' method '${method}'`,
{ error },
{ extension: ext },
@ -413,10 +405,26 @@ class ComfyApp {
api.init();
}
/**
* Loads all extensions from the API into the window
*/
async #loadExtensions() {
const extensions = await api.getExtensions();
for (const ext of extensions) {
try {
await import(ext);
} catch (error) {
console.error("Error loading extension", ext, error);
}
}
}
/**
* Set up the app on the page
*/
async setup() {
await this.#loadExtensions();
// Create and mount the LiteGraph in the DOM
const canvasEl = Object.assign(document.createElement("canvas"), { id: "graph-canvas" });
document.body.prepend(canvasEl);