From c6d3c834839f863ece20a9a3fa00d097c2711b62 Mon Sep 17 00:00:00 2001 From: somebody Date: Thu, 15 Sep 2022 21:28:25 -0500 Subject: [PATCH] JS work on new club loading --- aiserver.py | 16 ++++++++++++++- static/koboldai.js | 47 +++++++++++++++++++++++++++++++++++++++++++ templates/popups.html | 25 ++++------------------- 3 files changed, 66 insertions(+), 22 deletions(-) diff --git a/aiserver.py b/aiserver.py index 37e505af..58fdf2e7 100644 --- a/aiserver.py +++ b/aiserver.py @@ -247,6 +247,7 @@ class ImportBuffer: prompt: Optional[str] = None memory: Optional[str] = None authors_note: Optional[str] = None + notes: Optional[str] = None world_infos: Optional[dict] = None @dataclass @@ -284,6 +285,10 @@ class ImportBuffer: if "[" not in ph_text: ph_id = ph_text + # Already have it! + if any([x.id == ph_id for x in placeholders]): + continue + # Apparently, none of these characters are supported: # "${}[]#:@^|", however I have found some prompts using these, # so they will be allowed. @@ -303,6 +308,10 @@ class ImportBuffer: ph_id, _ = ph_text.split("[") ph_text = ph_text.replace(ph_id, "", 1) + # Already have it! + if any([x.id == ph_id for x in placeholders]): + continue + # Match won't match it for some reason (???), so we use finditer and next() try: default_match = next(re.finditer(r"\[(.*?)\]", ph_text)) @@ -330,12 +339,15 @@ class ImportBuffer: def _replace_placeholders(self, text: str, ph_ids: dict): for ph_id, value in ph_ids.items(): - pattern = "\${(?:\d#)?{}.*}".format(ph_id) + print(f"iterating upon {ph_id=}") + pattern = "\${(?:\d#)?%s.*?}" % ph_id for ph_text in re.findall(pattern, text): + print(f"instance of {ph_id} in text, replaceing with {value}") text = text.replace(ph_text, value) return text def replace_placeholders(self, ph_ids: dict): + print(f"Replacing with {ph_ids}") self.prompt = self._replace_placeholders(self.prompt, ph_ids) self.memory = self._replace_placeholders(self.memory, ph_ids) self.authors_note = self._replace_placeholders(self.authors_note, ph_ids) @@ -358,6 +370,7 @@ class ImportBuffer: self.prompt = j["promptContent"] self.memory = j["memory"] self.authors_note = j["authorsNote"] + self.notes = j["description"] self.world_infos = [] @@ -390,6 +403,7 @@ class ImportBuffer: koboldai_vars.prompt = self.prompt koboldai_vars.memory = self.memory or "" koboldai_vars.authornote = self.authors_note or "" + koboldai_vars.notes = self.notes # ???: Was this supposed to increment? num = 0 diff --git a/static/koboldai.js b/static/koboldai.js index 15f838a3..c60a9a50 100644 --- a/static/koboldai.js +++ b/static/koboldai.js @@ -28,6 +28,7 @@ socket.on("error", function(data){show_error_message(data);}); socket.on('load_cookies', function(data){load_cookies(data)}); socket.on('load_tweaks', function(data){load_tweaks(data);}); socket.on("wi_results", updateWISearchListings); +socket.on("request_prompt_config", configurePrompt); //socket.onAny(function(event_name, data) {console.log({"event": event_name, "class": data.classname, "data": data});}); var presets = {}; @@ -3432,6 +3433,52 @@ async function downloadDebugFile(redact=true) { downloadString(JSON.stringify(debug_info, null, 4), "kobold_debug.json"); } +function configurePrompt(placeholderData) { + console.log(placeholderData); + const container = document.querySelector("#prompt-config-container"); + container.classList.remove("hidden"); + + const placeholders = document.querySelector("#prompt-config-placeholders"); + + for (const phData of placeholderData) { + let placeholder = $e("div", placeholders, {classes: ["prompt-config-ph"]}); + + + // ${character.name} is an AI Dungeon thing, although I believe NAI + // supports it as well. Many prompts use it. I think this is the only + // hardcoded thing like this. + let titleText = phData.title || phData.id; + if (titleText === "character.name") titleText = "Character Name"; + + let title = $e("span", placeholder, {classes: ["prompt-config-title"], innerText: titleText}); + + if (phData.description) $e("span", placeholder, { + classes: ["prompt-config-desc", "help_text"], + innerText: phData.description + }); + + let input = $e("input", placeholder, { + classes: ["prompt-config-value"], + value: phData.default || "", + placeholder: phData.default || "", + "placeholder-id": phData.id + }); + } +} + +function sendPromptConfiguration() { + let data = {}; + for (const configInput of document.querySelectorAll(".prompt-config-value")) { + data[configInput.getAttribute("placeholder-id")] = configInput.value; + } + + socket.emit("configure_prompt", data); + + document.querySelector("#popup").classList.add("hidden"); + document.querySelector("#prompt-config-container").classList.add("hidden"); + $(".prompt-config-ph").remove(); +} + function loadNAILorebook(data, filename) { let lorebookVersion = data.lorebookVersion; let wi_data = {folders: {[filename]: []}, entries: {}}; diff --git a/templates/popups.html b/templates/popups.html index e8fefbfd..e8d5c3e1 100644 --- a/templates/popups.html +++ b/templates/popups.html @@ -195,7 +195,7 @@ -
+ - - \ No newline at end of file +
\ No newline at end of file