diff --git a/sn0w.py b/sn0w.py index 06cc7a9..fb73e13 100644 --- a/sn0w.py +++ b/sn0w.py @@ -391,6 +391,9 @@ async def add_lora_loaders(request): new_unique_loaders = [loader for loader in new_loaders if tuple(loader) not in existing_loaders_set] data["loraLoaders"].extend(new_unique_loaders) + if len(new_unique_loaders) == 0: + return web.json_response({"message": "No new lora loaders added."}) + # Write the updated JSON data back to the file with open(json_path, "w") as file: json.dump(data, file, indent=4) diff --git a/web/js/loraContextMenu.js b/web/js/loraContextMenu.js index 32ff297..5a046d5 100644 --- a/web/js/loraContextMenu.js +++ b/web/js/loraContextMenu.js @@ -2,6 +2,28 @@ import { SettingUtils } from './sn0w.js'; import { app } from '../../../scripts/app.js'; import { api } from '../../../scripts/api.js'; +async function addLoraLoaders(loraLoaders) { + try { + const response = await api.fetchApi(`${SettingUtils.API_PREFIX}/add_lora_loaders`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(loraLoaders), + }) + + if (!response.ok) { + const errorData = await response.json(); + throw new Error(`[sn0w] ${response.status} ${response.statusText} - ${errorData.error}`); + } + + const data = await response.json(); + console.log("[sn0w]", data.message); + } catch (error) { + console.error("[sn0w]", error.message); + } +} + const id = 'sn0w.HighlightFavourite'; const settingDefinition = { id, @@ -45,13 +67,7 @@ app.registerExtension({ // Combine the existing custom loaders with the fetched ones loraLoaders = loraLoaders.concat(...customLoraLoadersArrays); - const apiRequest = await api.fetchApi(`${SettingUtils.API_PREFIX}/add_lora_loaders`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(loraLoaders), - }) + addLoraLoaders(loraLoaders); const original_getNodeMenuOptions = app.canvas.getNodeMenuOptions; app.canvas.getNodeMenuOptions = function (node) {