From c2ed31de2803ae8959007cd1fdaf38273023ffe3 Mon Sep 17 00:00:00 2001 From: Gnome Ann <> Date: Sat, 20 Nov 2021 01:27:57 -0500 Subject: [PATCH 1/8] Add syntax for comments <|...|> --- aiserver.py | 10 +++++++--- static/custom.css | 5 +++++ templates/index.html | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/aiserver.py b/aiserver.py index f392ffb5..6e9ab648 100644 --- a/aiserver.py +++ b/aiserver.py @@ -123,6 +123,8 @@ class vars: regex_sl = re.compile(r'\n*(?<=.) *\n(.|\n)*') # Pattern for limiting the output to a single line acregex_ai = re.compile(r'\n* *>(.|\n)*') # Pattern for matching adventure actions from the AI so we can remove them acregex_ui = re.compile(r'^ *(>.*)$', re.MULTILINE) # Pattern for matching actions in the HTML-escaped story so we can apply colouring, etc (make sure to encase part to format in parentheses) + comregex_ui = re.compile(r'<\|(?:.|\n)*?\|>') # Pattern for matching comments to remove them before sending them to the AI + comregex_ui = re.compile(r'(<\|(?:.|\n)*?\|>)') # Pattern for matching comments in the editor actionmode = 1 adventure = False dynamicscan = False @@ -1270,7 +1272,7 @@ def calcsubmitbudget(actionlen, winfo, mem, anotetxt, actions): lnanote = 0 # Placeholder for Author's Note length # Calculate token budget - prompttkns = tokenizer.encode(vars.prompt) + prompttkns = tokenizer.encode(vars.comregex_ai.sub('', vars.prompt)) lnprompt = len(prompttkns) memtokens = tokenizer.encode(mem) @@ -1292,7 +1294,7 @@ def calcsubmitbudget(actionlen, winfo, mem, anotetxt, actions): if(actionlen == 0): # First/Prompt action - subtxt = vars.memory + winfo + anotetxt + vars.prompt + subtxt = vars.memory + winfo + anotetxt + vars.comregex_ai.sub('', vars.prompt) lnsub = lnsp + lnmem + lnwi + lnprompt + lnanote return subtxt, lnsub+1, lnsub+vars.genamt else: @@ -1305,7 +1307,7 @@ def calcsubmitbudget(actionlen, winfo, mem, anotetxt, actions): # Get most recent action tokens up to our budget n = 0 for key in reversed(actions): - chunk = actions[key] + chunk = vars.comregex_ai.sub('', actions[key]) if(budget <= 0): break @@ -1782,6 +1784,7 @@ def refresh_story(): item = vars.actions[idx] idx += 1 item = html.escape(item) + item = vars.comregex_ui.sub(lambda m: '\n'.join('' + l + '' for l in m.group().split('\n')), item) # Add special formatting to comments item = vars.acregex_ui.sub('\\1', item) # Add special formatting to adventure actions text_parts.extend(('', item, '')) emit('from_server', {'cmd': 'updatescreen', 'gamestarted': vars.gamestarted, 'data': formatforhtml(''.join(text_parts))}, broadcast=True) @@ -1809,6 +1812,7 @@ def update_story_chunk(idx: Union[int, str]): text = vars.actions[idx - 1] item = html.escape(text) + item = vars.comregex_ui.sub(lambda m: '\n'.join('' + l + '' for l in m.group().split('\n')), item) # Add special formatting to comments item = vars.acregex_ui.sub('\\1', item) # Add special formatting to adventure actions chunk_text = f'{formatforhtml(item)}' diff --git a/static/custom.css b/static/custom.css index 3b1ee1dc..ff487def 100644 --- a/static/custom.css +++ b/static/custom.css @@ -11,6 +11,11 @@ chunk { font-weight: bold; } +#gametext comment { + color: #888; + font-style: italic; +} + chunk.editing, chunk.editing * { color: #cdf !important; font-weight: normal !important; diff --git a/templates/index.html b/templates/index.html index f8e091e3..1278f82b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -13,7 +13,7 @@ - + From 8ce8e621ced70c727e0b119a11df1c6fecc14f03 Mon Sep 17 00:00:00 2001 From: Gnome Ann <> Date: Sat, 20 Nov 2021 22:19:12 -0500 Subject: [PATCH 2/8] Fix typo (one of the `comregex_ui` should be `comregex_ai`) --- aiserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiserver.py b/aiserver.py index 6e9ab648..4b59e720 100644 --- a/aiserver.py +++ b/aiserver.py @@ -123,7 +123,7 @@ class vars: regex_sl = re.compile(r'\n*(?<=.) *\n(.|\n)*') # Pattern for limiting the output to a single line acregex_ai = re.compile(r'\n* *>(.|\n)*') # Pattern for matching adventure actions from the AI so we can remove them acregex_ui = re.compile(r'^ *(>.*)$', re.MULTILINE) # Pattern for matching actions in the HTML-escaped story so we can apply colouring, etc (make sure to encase part to format in parentheses) - comregex_ui = re.compile(r'<\|(?:.|\n)*?\|>') # Pattern for matching comments to remove them before sending them to the AI + comregex_ai = re.compile(r'<\|(?:.|\n)*?\|>') # Pattern for matching comments to remove them before sending them to the AI comregex_ui = re.compile(r'(<\|(?:.|\n)*?\|>)') # Pattern for matching comments in the editor actionmode = 1 adventure = False From 1968be82bb71f0f6373ac527c98c3282bff33806 Mon Sep 17 00:00:00 2001 From: Gnome Ann <> Date: Sat, 20 Nov 2021 22:23:06 -0500 Subject: [PATCH 3/8] Remove comments from prompt in WI processor and InferKit mode --- aiserver.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/aiserver.py b/aiserver.py index 4b59e720..56090762 100644 --- a/aiserver.py +++ b/aiserver.py @@ -1391,12 +1391,12 @@ def calcsubmit(txt): forceanote = True if(vars.useprompt): - budget = vars.ikmax - len(vars.prompt) - len(anotetxt) - len(mem) - len(winfo) - 1 + budget = vars.ikmax - len(vars.comregex_ai.sub('', vars.prompt)) - len(anotetxt) - len(mem) - len(winfo) - 1 else: budget = vars.ikmax - len(anotetxt) - len(mem) - len(winfo) - 1 subtxt = "" - prompt = vars.prompt + prompt = vars.comregex_ai.sub('', vars.prompt) n = 0 for key in reversed(vars.actions): chunk = vars.actions[key] @@ -1417,7 +1417,7 @@ def calcsubmit(txt): # add some prompt. if(not vars.useprompt): if(budget > 0): - prompt = vars.prompt[-budget:] + prompt = vars.comregex_ai.sub('', vars.prompt)[-budget:] else: prompt = "" @@ -2092,9 +2092,9 @@ def checkworldinfo(txt, force_use_txt=False): if(ln >= depth): txt = "".join(chunks) elif(ln > 0): - txt = vars.prompt + "".join(chunks) + txt = vars.comregex_ai.sub('', vars.prompt) + "".join(chunks) elif(ln == 0): - txt = vars.prompt + txt = vars.comregex_ai.sub('', vars.prompt) if(force_use_txt): txt += original_txt From a51f88aeb3814fc9b6089cc69efef4b03eb45219 Mon Sep 17 00:00:00 2001 From: Gnome Ann <> Date: Sun, 21 Nov 2021 00:26:45 -0500 Subject: [PATCH 4/8] Also apply comment formatting to prompt in `refresh_story()` --- aiserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiserver.py b/aiserver.py index 56090762..9ebbcf79 100644 --- a/aiserver.py +++ b/aiserver.py @@ -1779,7 +1779,7 @@ def applyoutputformatting(txt): # Sends the current story content to the Game Screen #==================================================================# def refresh_story(): - text_parts = ['', html.escape(vars.prompt), ''] + text_parts = ['', vars.comregex_ui.sub(lambda m: '\n'.join('' + l + '' for l in m.group().split('\n')), html.escape(vars.prompt)), ''] for idx in vars.actions: item = vars.actions[idx] idx += 1 From 624cfbd5a402505d943915c55a6209149ab51438 Mon Sep 17 00:00:00 2001 From: Gnome Ann <> Date: Sun, 21 Nov 2021 00:42:57 -0500 Subject: [PATCH 5/8] Use a smarter regex for comments If the beginning of the comment is at the beginning of a line AND the end of a comment is at the end of a line, an additional newline will now be ignored so that the AI doesn't see a blank line where the comment was. For example, consider the following message: ``` Hello <|This is a comment|> World ``` The AI will now see this: ``` Hello World ``` instead of this: ``` Hello World ``` --- aiserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiserver.py b/aiserver.py index 9ebbcf79..6db56fe4 100644 --- a/aiserver.py +++ b/aiserver.py @@ -123,7 +123,7 @@ class vars: regex_sl = re.compile(r'\n*(?<=.) *\n(.|\n)*') # Pattern for limiting the output to a single line acregex_ai = re.compile(r'\n* *>(.|\n)*') # Pattern for matching adventure actions from the AI so we can remove them acregex_ui = re.compile(r'^ *(>.*)$', re.MULTILINE) # Pattern for matching actions in the HTML-escaped story so we can apply colouring, etc (make sure to encase part to format in parentheses) - comregex_ai = re.compile(r'<\|(?:.|\n)*?\|>') # Pattern for matching comments to remove them before sending them to the AI + comregex_ai = re.compile(r'(?:\n<\|(?:.|\n)*?\|>(?=\n|$))|(?:(?<=\n|^)<\|(?:.|\n)*?\|>\n)|(?:<\|(?:.|\n)*?\|>)') # Pattern for matching comments to remove them before sending them to the AI comregex_ui = re.compile(r'(<\|(?:.|\n)*?\|>)') # Pattern for matching comments in the editor actionmode = 1 adventure = False From a1c378deea0e1f94442a5a1fd0bd539c9fd881dc Mon Sep 17 00:00:00 2001 From: Gnome Ann <> Date: Sun, 21 Nov 2021 00:48:43 -0500 Subject: [PATCH 6/8] Fix CSS issues when editing a chunk that has a comment --- static/custom.css | 1 + 1 file changed, 1 insertion(+) diff --git a/static/custom.css b/static/custom.css index ff487def..9ac2ae65 100644 --- a/static/custom.css +++ b/static/custom.css @@ -19,6 +19,7 @@ chunk { chunk.editing, chunk.editing * { color: #cdf !important; font-weight: normal !important; + font-style: normal !important; } #gametext, chunk, chunk * { From 7ab0d96b8a94d0464b875850e329e773101d9e40 Mon Sep 17 00:00:00 2001 From: Gnome Ann <> Date: Sun, 21 Nov 2021 01:06:31 -0500 Subject: [PATCH 7/8] Change the comment regex again to use fixed-length lookbehind --- aiserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiserver.py b/aiserver.py index 6db56fe4..4b14e345 100644 --- a/aiserver.py +++ b/aiserver.py @@ -123,7 +123,7 @@ class vars: regex_sl = re.compile(r'\n*(?<=.) *\n(.|\n)*') # Pattern for limiting the output to a single line acregex_ai = re.compile(r'\n* *>(.|\n)*') # Pattern for matching adventure actions from the AI so we can remove them acregex_ui = re.compile(r'^ *(>.*)$', re.MULTILINE) # Pattern for matching actions in the HTML-escaped story so we can apply colouring, etc (make sure to encase part to format in parentheses) - comregex_ai = re.compile(r'(?:\n<\|(?:.|\n)*?\|>(?=\n|$))|(?:(?<=\n|^)<\|(?:.|\n)*?\|>\n)|(?:<\|(?:.|\n)*?\|>)') # Pattern for matching comments to remove them before sending them to the AI + comregex_ai = re.compile(r'(?:\n<\|(?:.|\n)*?\|>(?=\n|$))|(?:<\|(?:.|\n)*?\|>\n)|(?:<\|(?:.|\n)*?\|>)') # Pattern for matching comments to remove them before sending them to the AI comregex_ui = re.compile(r'(<\|(?:.|\n)*?\|>)') # Pattern for matching comments in the editor actionmode = 1 adventure = False From df2768b745faa30cbb17dbf37d19396276371882 Mon Sep 17 00:00:00 2001 From: Gnome Ann <> Date: Sun, 21 Nov 2021 01:09:19 -0500 Subject: [PATCH 8/8] Simplify the comment regex --- aiserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiserver.py b/aiserver.py index 4b14e345..1d33eb27 100644 --- a/aiserver.py +++ b/aiserver.py @@ -123,7 +123,7 @@ class vars: regex_sl = re.compile(r'\n*(?<=.) *\n(.|\n)*') # Pattern for limiting the output to a single line acregex_ai = re.compile(r'\n* *>(.|\n)*') # Pattern for matching adventure actions from the AI so we can remove them acregex_ui = re.compile(r'^ *(>.*)$', re.MULTILINE) # Pattern for matching actions in the HTML-escaped story so we can apply colouring, etc (make sure to encase part to format in parentheses) - comregex_ai = re.compile(r'(?:\n<\|(?:.|\n)*?\|>(?=\n|$))|(?:<\|(?:.|\n)*?\|>\n)|(?:<\|(?:.|\n)*?\|>)') # Pattern for matching comments to remove them before sending them to the AI + comregex_ai = re.compile(r'(?:\n<\|(?:.|\n)*?\|>(?=\n|$))|(?:<\|(?:.|\n)*?\|>\n?)') # Pattern for matching comments to remove them before sending them to the AI comregex_ui = re.compile(r'(<\|(?:.|\n)*?\|>)') # Pattern for matching comments in the editor actionmode = 1 adventure = False