From c43b24042dc3030ad8483a4a0ce2dd09e6ca8d5d Mon Sep 17 00:00:00 2001 From: somebody Date: Fri, 16 Sep 2022 16:16:17 -0500 Subject: [PATCH] Fix type annotations and cleanup import buffer --- aiserver.py | 114 +++++++++++++++++++++++++--------------------------- 1 file changed, 55 insertions(+), 59 deletions(-) diff --git a/aiserver.py b/aiserver.py index 25e4aab8..cefc8f1b 100644 --- a/aiserver.py +++ b/aiserver.py @@ -268,74 +268,74 @@ class ImportBuffer: "description" ]} - #def request_client_configuration(self, placeholders: list[PromptPlaceholder]) -> None: - # emit("request_prompt_config", [x.to_json() for x in placeholders], broadcast=False, room="UI_2") + def request_client_configuration(self, placeholders: List[PromptPlaceholder]) -> None: + emit("request_prompt_config", [x.to_json() for x in placeholders], broadcast=False, room="UI_2") - #def extract_placeholders(self, text: str) -> list[PromptPlaceholder]: - # placeholders = [] + def extract_placeholders(self, text: str) -> List[PromptPlaceholder]: + placeholders = [] - # for match in re.finditer(r"\${(.*?)}", text): - # ph_text = match.group(1) + for match in re.finditer(r"\${(.*?)}", text): + ph_text = match.group(1) - # try: - # ph_order, ph_text = ph_text.split("#") - # except ValueError: - # ph_order = None + try: + ph_order, ph_text = ph_text.split("#") + except ValueError: + ph_order = None - # if "[" not in ph_text: - # ph_id = ph_text + if "[" not in ph_text: + ph_id = ph_text - # Already have it! - # if any([x.id == ph_id for x in placeholders]): - # continue + # 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. - # for char in "${}[]": - # if char in ph_text: - # print("[eph] Weird char") - # print(f"{char=}") - # print(f"{ph_id=}") - # return + # Apparently, none of these characters are supported: + # "${}[]#:@^|", however I have found some prompts using these, + # so they will be allowed. + for char in "${}[]": + if char in ph_text: + print("[eph] Weird char") + print(f"{char=}") + print(f"{ph_id=}") + return - # placeholders.append(self.PromptPlaceholder( - # id=ph_id, - # order=int(ph_order) if ph_order else None, - # )) - # continue + placeholders.append(self.PromptPlaceholder( + id=ph_id, + order=int(ph_order) if ph_order else None, + )) + continue - # ph_id, _ = ph_text.split("[") - # ph_text = ph_text.replace(ph_id, "", 1) + 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 + # 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)) - # except StopIteration: - # print("[eph] Weird brackets") - # return placeholders + # Match won't match it for some reason (???), so we use finditer and next() + try: + default_match = next(re.finditer(r"\[(.*?)\]", ph_text)) + except StopIteration: + print("[eph] Weird brackets") + return placeholders - # ph_default = default_match.group(1) - # ph_text = ph_text.replace(default_match.group(0), "") + ph_default = default_match.group(1) + ph_text = ph_text.replace(default_match.group(0), "") - # try: - # ph_title, ph_desc = ph_text.split(":") - # except ValueError: - # ph_title = ph_text or None - # ph_desc=None + try: + ph_title, ph_desc = ph_text.split(":") + except ValueError: + ph_title = ph_text or None + ph_desc=None - # placeholders.append(self.PromptPlaceholder( - # id=ph_id, - # order=int(ph_order) if ph_order else None, - # default=ph_default, - # title=ph_title, - # description=ph_desc - # )) - # return placeholders + placeholders.append(self.PromptPlaceholder( + id=ph_id, + order=int(ph_order) if ph_order else None, + default=ph_default, + title=ph_title, + description=ph_desc + )) + return placeholders def _replace_placeholders(self, text: str, ph_ids: dict): for ph_id, value in ph_ids.items(): @@ -402,11 +402,7 @@ class ImportBuffer: koboldai_vars.authornote = self.authors_note or "" koboldai_vars.notes = self.notes - # ???: Was this supposed to increment? - num = 0 for wi in self.world_infos: - # koboldai_vars.worldinfo += self.world_infos - koboldai_vars.worldinfo_v2.add_item( wi["key_list"][0], wi["key_list"],