From fcbf3fef77c5347a21fb0d2efd7ae226c08df8f1 Mon Sep 17 00:00:00 2001 From: ebolam Date: Tue, 23 Aug 2022 15:29:29 -0400 Subject: [PATCH] Working W++/SBF for World Info --- aiserver.py | 13 +++---- koboldai_settings.py | 38 ++++++++++++++++++--- static/koboldai.js | 74 +++++++++++++++++++++++++--------------- templates/templates.html | 5 +++ 4 files changed, 90 insertions(+), 40 deletions(-) diff --git a/aiserver.py b/aiserver.py index b9334ff1..1ee492e4 100644 --- a/aiserver.py +++ b/aiserver.py @@ -7240,21 +7240,18 @@ def UI_2_Rename_World_Info_Folder(data): def UI_2_edit_world_info(data): print("edit_world_info") print(data) - if 'wpp' not in data: - wpp = {'name': "", 'type': "", 'attributes': {}} - else: - wpp = data['wpp'] + if data['uid'] == -1: koboldai_vars.worldinfo_v2.add_item(data['title'], data['key'], data['keysecondary'], data['folder'], - data['constant'], data['content'], - data['comment'], wpp=wpp) + data['constant'], data['manual_text'], + data['comment'], wpp=data['wpp'], use_wpp=data['use_wpp']) emit("delete_new_world_info_entry", {}) else: koboldai_vars.worldinfo_v2.edit_item(data['uid'], data['title'], data['key'], data['keysecondary'], data['folder'], - data['constant'], data['content'], - data['comment'], wpp=wpp) + data['constant'], data['manual_text'], + data['comment'], wpp=data['wpp'], use_wpp=data['use_wpp']) #==================================================================# diff --git a/koboldai_settings.py b/koboldai_settings.py index a94562f8..5ee9d3c4 100644 --- a/koboldai_settings.py +++ b/koboldai_settings.py @@ -1166,11 +1166,24 @@ class KoboldWorldInfo(object): self.sync_world_info_to_old_format() self.socketio.emit("world_info_folder", {x: self.world_info_folder[x] for x in self.world_info_folder}, broadcast=True, room="UI_2") - def add_item(self, title, key, keysecondary, folder, constant, content, comment, wpp={'name': "", 'type': "", 'attributes': {}}): + def add_item(self, title, key, keysecondary, folder, constant, manual_text, comment, use_wpp=False, wpp={'name': "", 'type': "", 'format': "W++", 'attributes': {}}): if len(self.world_info) == 0: uid = 0 else: uid = max(self.world_info)+1 + if use_wpp: + if wpp['format'] == "W++": + content = '[{}("{}")\n{{\n'.format(wpp['type'], wpp['name']) + for attribute in wpp['attributes']: + content = "{}{}({})\n".format(content, attribute, " + ".join(['"{}"'.format(x) for x in wpp['attributes'][attribute]])) + content = "{}}}]".format(content) + else: + content = '[ {}: "{}";'.format(wpp['type'], wpp['name']) + for attribute in wpp['attributes']: + content = "{} {}: {};".format(content, attribute, ", ".join(['"{}"'.format(x) for x in wpp['attributes'][attribute]])) + content = "{} ]".format(content[:-1]) + else: + content = manual_text if self.tokenizer is not None: token_length = len(self.tokenizer.encode(content)) else: @@ -1194,12 +1207,14 @@ class KoboldWorldInfo(object): "keysecondary": keysecondary, "folder": folder, "constant": constant, + 'manual_text': manual_text, "content": content, "comment": comment, "token_length": token_length, "selective": len(keysecondary) > 0, "used_in_game": constant, - 'wpp': wpp + 'wpp': wpp, + 'use_wpp': use_wpp } except: print("Error:") @@ -1216,11 +1231,24 @@ class KoboldWorldInfo(object): self.socketio.emit("world_info_entry", self.world_info[uid], broadcast=True, room="UI_2") ignore = self.koboldai_vars.calc_ai_text() - def edit_item(self, uid, title, key, keysecondary, folder, constant, content, comment, before=None, wpp={'name': "", 'type': "", 'attributes': {}}): + def edit_item(self, uid, title, key, keysecondary, folder, constant, manual_text, comment, use_wpp=False, before=None, wpp={'name': "", 'type': "", 'format': "W++", 'attributes': {}}): old_folder = self.world_info[uid]['folder'] #move the world info entry if the folder changed or if there is a new order requested if old_folder != folder or before is not None: self.add_item_to_folder(uid, folder, before=before) + if use_wpp: + if wpp['format'] == "W++": + content = '[{}("{}")\n{{\n'.format(wpp['type'], wpp['name']) + for attribute in wpp['attributes']: + content = "{}{}({})\n".format(content, attribute, " + ".join(['"{}"'.format(x) for x in wpp['attributes'][attribute]])) + content = "{}}}]".format(content) + else: + content = '[ {}: "{}";'.format(wpp['type'], wpp['name']) + for attribute in wpp['attributes']: + content = "{} {}: {};".format(content, attribute, ", ".join(['"{}"'.format(x) for x in wpp['attributes'][attribute]])) + content = "{} ]".format(content[:-1]) + else: + content = manual_text if self.tokenizer is not None: token_length = len(self.tokenizer.encode(content)) else: @@ -1234,12 +1262,14 @@ class KoboldWorldInfo(object): "keysecondary": keysecondary, "folder": folder, "constant": constant, + 'manual_text': manual_text, "content": content, "comment": comment, "token_length": token_length, "selective": len(keysecondary) > 0, "used_in_game": constant, - 'wpp': wpp + 'wpp': wpp, + 'use_wpp': use_wpp } self.story_settings.gamesaved = False diff --git a/static/koboldai.js b/static/koboldai.js index c9f008f1..3c75764c 100644 --- a/static/koboldai.js +++ b/static/koboldai.js @@ -1264,7 +1264,7 @@ function world_info_entry(data) { wpp_toggle.id = "world_info_wpp_toggle_"+data.uid; wpp_toggle.setAttribute("type", "checkbox"); wpp_toggle.setAttribute("uid", data.uid); - wpp_toggle.checked = ((data.wpp.type != "")); + wpp_toggle.checked = data.use_wpp; wpp_toggle.setAttribute("data-size", "mini"); wpp_toggle.setAttribute("data-onstyle", "success"); wpp_toggle.setAttribute("data-toggle", "toggle"); @@ -1276,7 +1276,9 @@ function world_info_entry(data) { document.getElementById("world_info_wpp_area_"+this.getAttribute('uid')).classList.add("hidden"); document.getElementById("world_info_basic_text_"+this.getAttribute('uid')).classList.remove("hidden"); } - //send_world_info(this.getAttribute('uid')); + + world_info_data[this.getAttribute('uid')]['use_wpp'] = this.checked; + send_world_info(this.getAttribute('uid')); this.classList.add("pulse"); } wpp_toggle_area.append(wpp_toggle); @@ -1284,13 +1286,25 @@ function world_info_entry(data) { world_info_wpp_area = world_info_card.querySelector('#world_info_wpp_area_'); world_info_wpp_area.id = "world_info_wpp_area_"+data.uid; world_info_wpp_area.setAttribute("uid", data.uid); + wpp_format = world_info_card.querySelector('#wpp_format_'); + wpp_format.id = "wpp_format_"+data.uid; + wpp_format.setAttribute("uid", data.uid); + wpp_format.setAttribute("data_type", "format"); + wpp_format.onchange = function () { + do_wpp(this.parentElement); + } + console.log(data.wpp['format']); + console.log(data.wpp); + if (data.wpp.format == "W++") { + wpp_format.selectedIndex = 0; + } else { + wpp_format.selectedIndex = 1; + } wpp_type = world_info_card.querySelector('#wpp_type_'); wpp_type.id = "wpp_type_"+data.uid; wpp_type.setAttribute("uid", data.uid); wpp_type.setAttribute("data_type", "type"); - if ("wpp" in data) { - wpp_type.value = data.wpp.type; - } + wpp_type.value = data.wpp.type; wpp_name = world_info_card.querySelector('#wpp_name_'); wpp_name.id = "wpp_name_"+data.uid; wpp_name.setAttribute("uid", data.uid); @@ -1299,11 +1313,11 @@ function world_info_entry(data) { wpp_name.value = data.wpp.name; } if ('attributes' in data.wpp) { - for (const [attribute, value] of Object.entries(data.wpp.attributes)) { + for (const [attribute, values] of Object.entries(data.wpp.attributes)) { if (attribute != '') { attribute_area = document.createElement("div"); label = document.createElement("span"); - label.textContent = "Attribute: "; + label.textContent = "\xa0\xa0\xa0\xa0Attribute: "; attribute_area.append(label); input = document.createElement("input"); input.value = attribute; @@ -1313,10 +1327,10 @@ function world_info_entry(data) { input.onchange = function() {do_wpp(this.parentElement.parentElement)}; attribute_area.append(input); world_info_wpp_area.append(attribute_area); - for (value of value) { + for (value of values) { value_area = document.createElement("div"); label = document.createElement("span"); - label.textContent = " Value: "; + label.textContent = "\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0Value: "; value_area.append(label); input = document.createElement("input"); input.type = "text"; @@ -1329,7 +1343,7 @@ function world_info_entry(data) { } value_area = document.createElement("div"); label = document.createElement("span"); - label.textContent = " Value: "; + label.textContent = "\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0Value: "; value_area.append(label); input = document.createElement("input"); input.type = "text"; @@ -1343,7 +1357,7 @@ function world_info_entry(data) { } attribute_area = document.createElement("div"); label = document.createElement("span"); - label.textContent = "Attribute: "; + label.textContent = "\xa0\xa0\xa0\xa0Attribute: "; attribute_area.append(label); input = document.createElement("input"); input.value = ""; @@ -1359,18 +1373,18 @@ function world_info_entry(data) { //regular data content_area = world_info_card.querySelector('#world_info_basic_text_'); content_area.id = "world_info_basic_text_"+data.uid; - content = world_info_card.querySelector('#world_info_entry_text_'); - content.id = "world_info_entry_text_"+data.uid; - content.setAttribute("uid", data.uid); - content.value = data.content; - content.onchange = function () { - world_info_data[this.getAttribute('uid')]['content'] = this.value; + manual_text = world_info_card.querySelector('#world_info_entry_text_'); + manual_text.id = "world_info_entry_text_"+data.uid; + manual_text.setAttribute("uid", data.uid); + manual_text.value = data.manual_text; + manual_text.onchange = function () { + world_info_data[this.getAttribute('uid')]['manual_text'] = this.value; send_world_info(this.getAttribute('uid')); this.classList.add("pulse"); } comment = world_info_card.querySelector('#world_info_comment_'); comment.id = "world_info_comment_"+data.uid; - content.setAttribute("uid", data.uid); + comment.setAttribute("uid", data.uid); comment.value = data.comment; comment.onchange = function () { world_info_data[this.getAttribute('uid')]['comment'] = this.textContent; @@ -1432,12 +1446,12 @@ function world_info_entry(data) { $('#world_info_wpp_toggle_'+data.uid).bootstrapToggle(); //hide/unhide w++ - if (data.wpp.type != "") { - world_info_wpp_area.classList.remove("hidden"); - content_area.classList.add("hidden"); + if (wpp_toggle.checked) { + document.getElementById("world_info_wpp_area_"+wpp_toggle.getAttribute('uid')).classList.remove("hidden"); + document.getElementById("world_info_basic_text_"+wpp_toggle.getAttribute('uid')).classList.add("hidden"); } else { - world_info_wpp_area.classList.add("hidden"); - content_area.classList.remove("hidden"); + document.getElementById("world_info_wpp_area_"+wpp_toggle.getAttribute('uid')).classList.add("hidden"); + document.getElementById("world_info_basic_text_"+wpp_toggle.getAttribute('uid')).classList.remove("hidden"); } assign_world_info_to_action(null, data.uid); @@ -1599,6 +1613,7 @@ function do_wpp(wpp_area) { wpp['attributes'] = {}; uid = wpp_area.getAttribute("uid"); attribute = ""; + wpp['format'] = document.getElementById("wpp_format_"+uid).value; for (input of wpp_area.querySelectorAll('input')) { if (input.getAttribute("data_type") == "name") { wpp['name'] = input.value; @@ -1606,13 +1621,14 @@ function do_wpp(wpp_area) { wpp['type'] = input.value; } else if (input.getAttribute("data_type") == "attribute") { attribute = input.value; - if (!(input.value in wpp['attributes'])) { - console.log("adding attribute"); + if (!(input.value in wpp['attributes']) && (input.value != "")) { wpp['attributes'][input.value] = []; } - } else if (input.getAttribute("data_type") == "value") { - wpp['attributes'][attribute].push(input.value); + } else if ((input.getAttribute("data_type") == "value") && (attribute != "")) { + if (input.value != "") { + wpp['attributes'][attribute].push(input.value); + } } } world_info_data[uid]['wpp'] = wpp; @@ -2173,10 +2189,12 @@ function create_new_wi_entry(folder) { "folder": folder, "constant": false, "content": "", + "manual_text": "", "comment": "", "token_length": 0, "selective": false, - "wpp": {'name': "", 'type': "", 'attributes': {}} + "wpp": {'name': "", 'type': "", 'format': 'W++', 'attributes': {}}, + 'use_wpp': false, }; card = world_info_entry(data); card.scrollIntoView(false); diff --git a/templates/templates.html b/templates/templates.html index dac6063b..1498fe8f 100644 --- a/templates/templates.html +++ b/templates/templates.html @@ -16,6 +16,11 @@ Use W++