diff --git a/javascript/__globals.js b/javascript/__globals.js index 7e62311..af572be 100644 --- a/javascript/__globals.js +++ b/javascript/__globals.js @@ -42,6 +42,7 @@ var umiPreviousTags = []; // Queues const QUEUE_AFTER_INSERT = []; const QUEUE_AFTER_SETUP = []; +const QUEUE_FILE_LOAD = []; const QUEUE_AFTER_CONFIG_CHANGE = []; // List of parsers to try diff --git a/javascript/_utils.js b/javascript/_utils.js index a8251b9..06810db 100644 --- a/javascript/_utils.js +++ b/javascript/_utils.js @@ -96,9 +96,9 @@ function escapeHTML(unsafeText) { } // Queue calling function to process global queues -function processQueue(queue, context, ...args) { +async function processQueue(queue, context, ...args) { for (let i = 0; i < queue.length; i++) { - queue[i].call(context, ...args); + await queue[i].call(context, ...args); } } // The same but with return values diff --git a/javascript/ext_embeddings.js b/javascript/ext_embeddings.js index 2d952a2..ca83c9e 100644 --- a/javascript/ext_embeddings.js +++ b/javascript/ext_embeddings.js @@ -32,4 +32,19 @@ class EmbeddingParser extends BaseTagParser { } } -PARSERS.push(new EmbeddingParser(EMB_TRIGGER)); \ No newline at end of file +async function load() { + if (embeddings.length === 0) { + try { + embeddings = (await readFile(`${tagBasePath}/temp/emb.txt?${new Date().getTime()}`)).split("\n") + .filter(x => x.trim().length > 0) // Remove empty lines + .map(x => x.trim().split(",")); // Split into name, version type pairs + } catch (e) { + console.error("Error loading embeddings.txt: " + e); + } + } +} + +PARSERS.push(new EmbeddingParser(EMB_TRIGGER)); + +// Add load function to the queue +QUEUE_FILE_LOAD.push(load); \ No newline at end of file diff --git a/javascript/ext_hypernets.js b/javascript/ext_hypernets.js index 221dcb3..8e011cf 100644 --- a/javascript/ext_hypernets.js +++ b/javascript/ext_hypernets.js @@ -24,4 +24,19 @@ class HypernetParser extends BaseTagParser { } } -PARSERS.push(new HypernetParser(HYP_TRIGGER)); \ No newline at end of file +async function load() { + if (hypernetworks.length === 0) { + try { + hypernetworks = (await readFile(`${tagBasePath}/temp/hyp.txt?${new Date().getTime()}`)).split("\n") + .filter(x => x.trim().length > 0) //Remove empty lines + .map(x => x.trim()); // Remove carriage returns and padding if it exists + } catch (e) { + console.error("Error loading hypernetworks.txt: " + e); + } + } +} + +PARSERS.push(new HypernetParser(HYP_TRIGGER)); + +// Add load function to the queue +QUEUE_FILE_LOAD.push(load); \ No newline at end of file diff --git a/javascript/ext_loras.js b/javascript/ext_loras.js index 82c4012..9279185 100644 --- a/javascript/ext_loras.js +++ b/javascript/ext_loras.js @@ -24,4 +24,19 @@ class LoraParser extends BaseTagParser { } } -PARSERS.push(new LoraParser(LORA_TRIGGER)); \ No newline at end of file +async function load() { + if (loras.length === 0) { + try { + loras = (await readFile(`${tagBasePath}/temp/lora.txt?${new Date().getTime()}`)).split("\n") + .filter(x => x.trim().length > 0) // Remove empty lines + .map(x => x.trim()); // Remove carriage returns and padding if it exists + } catch (e) { + console.error("Error loading lora.txt: " + e); + } + } +} + +PARSERS.push(new LoraParser(LORA_TRIGGER)); + +// Add load function to the queue +QUEUE_FILE_LOAD.push(load); \ No newline at end of file diff --git a/javascript/ext_umi.js b/javascript/ext_umi.js index 5a4ed97..c476e7b 100644 --- a/javascript/ext_umi.js +++ b/javascript/ext_umi.js @@ -202,7 +202,29 @@ function updateUmiTags( tagType, sanitizedText, newPrompt, textArea) { return false; } +async function load() { + if (yamlWildcards.length === 0) { + try { + let yamlTags = (await readFile(`${tagBasePath}/temp/wcet.txt?${new Date().getTime()}`)).split("\n"); + // Split into tag, count pairs + yamlWildcards = yamlTags.map(x => x + .trim() + .split(",")) + .map(([i, ...rest]) => [ + i, + rest.reduce((a, b) => { + a[b.toLowerCase()] = true; + return a; + }, {}), + ]); + } catch (e) { + console.error("Error loading yaml wildcards: " + e); + } + } +} + // Add UMI parser PARSERS.push(new UmiParser(UMI_TRIGGER)); // Add tag update after insert -QUEUE_AFTER_INSERT.push(updateUmiTags); \ No newline at end of file +QUEUE_AFTER_INSERT.push(updateUmiTags); +QUEUE_FILE_LOAD.push(load); \ No newline at end of file diff --git a/javascript/ext_wildcards.js b/javascript/ext_wildcards.js index 8a2f205..f976449 100644 --- a/javascript/ext_wildcards.js +++ b/javascript/ext_wildcards.js @@ -55,6 +55,44 @@ class WildcardFileParser extends BaseTagParser { } } +async function load() { + if (wildcardFiles.length === 0 && wildcardExtFiles.length === 0) { + try { + let wcFileArr = (await readFile(`${tagBasePath}/temp/wc.txt?${new Date().getTime()}`)).split("\n"); + let wcBasePath = wcFileArr[0].trim(); // First line should be the base path + wildcardFiles = wcFileArr.slice(1) + .filter(x => x.trim().length > 0) // Remove empty lines + .map(x => [wcBasePath, x.trim().replace(".txt", "")]); // Remove file extension & newlines + + // To support multiple sources, we need to separate them using the provided "-----" strings + let wcExtFileArr = (await readFile(`${tagBasePath}/temp/wce.txt?${new Date().getTime()}`)).split("\n"); + let splitIndices = []; + for (let index = 0; index < wcExtFileArr.length; index++) { + if (wcExtFileArr[index].trim() === "-----") { + splitIndices.push(index); + } + } + // For each group, add them to the wildcardFiles array with the base path as the first element + for (let i = 0; i < splitIndices.length; i++) { + let start = splitIndices[i - 1] || 0; + if (i > 0) start++; // Skip the "-----" line + let end = splitIndices[i]; + + let wcExtFile = wcExtFileArr.slice(start, end); + let base = wcExtFile[0].trim() + "/"; + wcExtFile = wcExtFile.slice(1) + .filter(x => x.trim().length > 0) // Remove empty lines + .map(x => x.trim().replace(base, "").replace(".txt", "")); // Remove file extension & newlines; + + wcExtFile = wcExtFile.map(x => [base, x]); + wildcardExtFiles.push(...wcExtFile); + } + } catch (e) { + console.error("Error loading wildcards: " + e); + } + } +} + function keepOpenIfWildcard(tagType, sanitizedText, newPrompt, textArea) { // If it's a wildcard, we want to keep the results open so the user can select another wildcard if (tagType === ResultType.wildcardFile) { @@ -71,4 +109,6 @@ PARSERS.push(new WildcardParser(WC_TRIGGER)); PARSERS.push(new WildcardFileParser(WC_FILE_TRIGGER)); // Add the keep open function to the queue -QUEUE_AFTER_INSERT.push(keepOpenIfWildcard); \ No newline at end of file +QUEUE_AFTER_INSERT.push(keepOpenIfWildcard); +// Add the load function to the queue +QUEUE_FILE_LOAD.push(load); \ No newline at end of file diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index cacb048..89007b2 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -226,7 +226,7 @@ async function syncOptions() { CFG = newCFG; // Callback - processQueue(QUEUE_AFTER_CONFIG_CHANGE, null); + await processQueue(QUEUE_AFTER_CONFIG_CHANGE, null); } // Create the result list div and necessary styling @@ -783,91 +783,8 @@ async function setup() { // Load colors CFG["colors"] = (await readFile(`${tagBasePath}/colors.json?${new Date().getTime()}`, true)); - // Load wildcards - if (wildcardFiles.length === 0 && wildcardExtFiles.length === 0) { - try { - let wcFileArr = (await readFile(`${tagBasePath}/temp/wc.txt?${new Date().getTime()}`)).split("\n"); - let wcBasePath = wcFileArr[0].trim(); // First line should be the base path - wildcardFiles = wcFileArr.slice(1) - .filter(x => x.trim().length > 0) // Remove empty lines - .map(x => [wcBasePath, x.trim().replace(".txt", "")]); // Remove file extension & newlines - - // To support multiple sources, we need to separate them using the provided "-----" strings - let wcExtFileArr = (await readFile(`${tagBasePath}/temp/wce.txt?${new Date().getTime()}`)).split("\n"); - let splitIndices = []; - for (let index = 0; index < wcExtFileArr.length; index++) { - if (wcExtFileArr[index].trim() === "-----") { - splitIndices.push(index); - } - } - // For each group, add them to the wildcardFiles array with the base path as the first element - for (let i = 0; i < splitIndices.length; i++) { - let start = splitIndices[i - 1] || 0; - if (i > 0) start++; // Skip the "-----" line - let end = splitIndices[i]; - - let wcExtFile = wcExtFileArr.slice(start, end); - let base = wcExtFile[0].trim() + "/"; - wcExtFile = wcExtFile.slice(1) - .filter(x => x.trim().length > 0) // Remove empty lines - .map(x => x.trim().replace(base, "").replace(".txt", "")); // Remove file extension & newlines; - - wcExtFile = wcExtFile.map(x => [base, x]); - wildcardExtFiles.push(...wcExtFile); - } - } catch (e) { - console.error("Error loading wildcards: " + e); - } - } - // Load yaml wildcards - if (yamlWildcards.length === 0) { - try { - let yamlTags = (await readFile(`${tagBasePath}/temp/wcet.txt?${new Date().getTime()}`)).split("\n"); - // Split into tag, count pairs - yamlWildcards = yamlTags.map(x => x - .trim() - .split(",")) - .map(([i, ...rest]) => [ - i, - rest.reduce((a, b) => { - a[b.toLowerCase()] = true; - return a; - }, {}), - ]); - } catch (e) { - console.error("Error loading yaml wildcards: " + e); - } - } - // Load embeddings - if (embeddings.length === 0) { - try { - embeddings = (await readFile(`${tagBasePath}/temp/emb.txt?${new Date().getTime()}`)).split("\n") - .filter(x => x.trim().length > 0) // Remove empty lines - .map(x => x.trim().split(",")); // Split into name, version type pairs - } catch (e) { - console.error("Error loading embeddings.txt: " + e); - } - } - // Load hypernetworks - if (hypernetworks.length === 0) { - try { - hypernetworks = (await readFile(`${tagBasePath}/temp/hyp.txt?${new Date().getTime()}`)).split("\n") - .filter(x => x.trim().length > 0) //Remove empty lines - .map(x => x.trim()); // Remove carriage returns and padding if it exists - } catch (e) { - console.error("Error loading hypernetworks.txt: " + e); - } - } - // Load lora - if (loras.length === 0) { - try { - loras = (await readFile(`${tagBasePath}/temp/lora.txt?${new Date().getTime()}`)).split("\n") - .filter(x => x.trim().length > 0) // Remove empty lines - .map(x => x.trim()); // Remove carriage returns and padding if it exists - } catch (e) { - console.error("Error loading lora.txt: " + e); - } - } + // Load external files needed by completion extensions + await processQueue(QUEUE_FILE_LOAD, null); // Find all textareas let textAreas = getTextAreas(); @@ -984,7 +901,7 @@ async function setup() { gradioApp().appendChild(acStyle); // Callback - processQueue(QUEUE_AFTER_SETUP, null); + await processQueue(QUEUE_AFTER_SETUP, null); } onUiUpdate(async () => {