Fixed error in redo action when editing previous entries and/or editing right after a redo

This commit is contained in:
ebolam 2022-02-28 08:31:26 -05:00
parent fd7ba9f70e
commit 7803fbb137
1 changed files with 66 additions and 57 deletions

View File

@ -122,7 +122,7 @@ class vars:
setauthornotetemplate = authornotetemplate # Saved author's note template in settings setauthornotetemplate = authornotetemplate # Saved author's note template in settings
andepth = 3 # How far back in history to append author's note andepth = 3 # How far back in history to append author's note
actions = structures.KoboldStoryRegister() # Actions submitted by user and AI actions = structures.KoboldStoryRegister() # Actions submitted by user and AI
actions_metadata = [] # List of dictonaries, one dictonary for every action that contains information about the action like alternative options. actions_metadata = {} # List of dictonaries, one dictonary for every action that contains information about the action like alternative options.
# Contains at least the same number of items as actions. Back action will remove an item from actions, but not actions_metadata # Contains at least the same number of items as actions. Back action will remove an item from actions, but not actions_metadata
# Dictonary keys are: # Dictonary keys are:
# Selected Text: (text the user had selected. None when this is a newly generated action) # Selected Text: (text the user had selected. None when this is a newly generated action)
@ -2569,7 +2569,7 @@ def actionsubmit(data, actionmode=0, force_submit=False, force_prompt_gen=False,
# we now need to update the actions_metadata # we now need to update the actions_metadata
# we'll have two conditions. # we'll have two conditions.
# 1. This is totally new (user entered) # 1. This is totally new (user entered)
if len(vars.actions_metadata) < len(vars.actions): if vars.actions.get_next_id()-1 not in vars.actions_metadata:
vars.actions_metadata.append({"Selected Text": data, "Alternative Text": []}) vars.actions_metadata.append({"Selected Text": data, "Alternative Text": []})
else: else:
# 2. We've selected a chunk of text that is was presented previously # 2. We've selected a chunk of text that is was presented previously
@ -2580,9 +2580,9 @@ def actionsubmit(data, actionmode=0, force_submit=False, force_prompt_gen=False,
print(vars.actions_metadata) print(vars.actions_metadata)
raise raise
if data in alternatives: if data in alternatives:
alternatives = [item for item in vars.actions_metadata[len(vars.actions)-1]["Alternative Text"] if item['Text'] != data] alternatives = [item for item in vars.actions_metadata[vars.actions.get_next_id()-1 ]["Alternative Text"] if item['Text'] != data]
vars.actions_metadata[len(vars.actions)-1]["Alternative Text"] = alternatives vars.actions_metadata[vars.actions.get_next_id()-1]["Alternative Text"] = alternatives
vars.actions_metadata[len(vars.actions)-1]["Selected Text"] = data vars.actions_metadata[vars.actions.get_next_id()-1]["Selected Text"] = data
update_story_chunk('last') update_story_chunk('last')
send_debug() send_debug()
@ -2640,11 +2640,11 @@ def actionretry(data):
if(vars.gamestarted if vars.useprompt else len(vars.actions) > 0): if(vars.gamestarted if vars.useprompt else len(vars.actions) > 0):
if(not vars.recentback and len(vars.actions) != 0 and len(vars.genseqs) == 0): # Don't pop if we're in the "Select sequence to keep" menu or if there are no non-prompt actions if(not vars.recentback and len(vars.actions) != 0 and len(vars.genseqs) == 0): # Don't pop if we're in the "Select sequence to keep" menu or if there are no non-prompt actions
# We are going to move the selected text to alternative text in the actions_metadata variable so we can redo this action # We are going to move the selected text to alternative text in the actions_metadata variable so we can redo this action
vars.actions_metadata[len(vars.actions)-1]['Alternative Text'] = [{'Text': vars.actions_metadata[len(vars.actions)-1]['Selected Text'], vars.actions_metadata[vars.actions.get_next_id()-1 ]['Alternative Text'] = [{'Text': vars.actions_metadata[vars.actions.get_next_id()-1 ]['Selected Text'],
'Pinned': False, 'Pinned': False,
"Previous Selection": True, "Previous Selection": True,
"Edited": False}] + vars.actions_metadata[len(vars.actions)-1]['Alternative Text'] "Edited": False}] + vars.actions_metadata[vars.actions.get_next_id()-1 ]['Alternative Text']
vars.actions_metadata[len(vars.actions)-1]['Selected Text'] = "" vars.actions_metadata[vars.actions.get_next_id()-1]['Selected Text'] = ""
@ -2668,16 +2668,18 @@ def actionback():
# Remove last index of actions and refresh game screen # Remove last index of actions and refresh game screen
if(len(vars.genseqs) == 0 and len(vars.actions) > 0): if(len(vars.genseqs) == 0 and len(vars.actions) > 0):
# We are going to move the selected text to alternative text in the actions_metadata variable so we can redo this action # We are going to move the selected text to alternative text in the actions_metadata variable so we can redo this action
vars.actions_metadata[len(vars.actions)-1]['Alternative Text'] = [{'Text': vars.actions_metadata[len(vars.actions)-1]['Selected Text'], vars.actions_metadata[vars.actions.get_next_id()-1 ]['Alternative Text'] = [{'Text': vars.actions_metadata[vars.actions.get_next_id()-1 ]['Selected Text'],
'Pinned': False, 'Pinned': False,
"Previous Selection": True, "Previous Selection": True,
"Edited": False}] + vars.actions_metadata[len(vars.actions)-1]['Alternative Text'] "Edited": False}] + vars.actions_metadata[vars.actions.get_next_id()-1 ]['Alternative Text']
vars.actions_metadata[len(vars.actions)-1]['Selected Text'] = "" vars.actions_metadata[vars.actions.get_next_id()-1 ]['Selected Text'] = ""
last_key = vars.actions.get_last_key() last_key = vars.actions.get_last_key()
vars.actions.pop() vars.actions.pop()
vars.recentback = True vars.recentback = True
remove_story_chunk(last_key + 1) remove_story_chunk(last_key + 1)
#for the redo to not get out of whack, need to reset the max # in the actions sequence
vars.actions.set_next_id(vars.actions.get_next_id()-1)
elif(len(vars.genseqs) == 0): elif(len(vars.genseqs) == 0):
emit('from_server', {'cmd': 'errmsg', 'data': "Cannot delete the prompt."}) emit('from_server', {'cmd': 'errmsg', 'data': "Cannot delete the prompt."})
else: else:
@ -2686,21 +2688,22 @@ def actionback():
def actionredo(): def actionredo():
i = 0 i = 0
if len(vars.actions) < len(vars.actions_metadata): if vars.actions.get_last_key()+1 in vars.actions_metadata:
genout = [{"generated_text": item['Text']} for item in vars.actions_metadata[len(vars.actions)]['Alternative Text'] if (item["Previous Selection"]==True)] genout = [{"generated_text": item['Text']} for item in vars.actions_metadata[vars.actions.get_last_key()+1]['Alternative Text'] if (item["Previous Selection"]==True)]
genout = genout + [{"generated_text": item['Text']} for item in vars.actions_metadata[len(vars.actions)]['Alternative Text'] if (item["Pinned"]==True) and (item["Previous Selection"]==False)] if len(genout) > 0:
genout = genout + [{"generated_text": item['Text']} for item in vars.actions_metadata[vars.actions.get_last_key()+1]['Alternative Text'] if (item["Pinned"]==True) and (item["Previous Selection"]==False)]
if len(genout) == 1:
vars.actions_metadata[len(vars.actions)]['Alternative Text'] = [item for item in vars.actions_metadata[len(vars.actions)]['Alternative Text'] if (item["Previous Selection"]!=True)]
genresult(genout[0]['generated_text'], flash=True)
else:
# Store sequences in memory until selection is made
vars.genseqs = genout
if len(genout) == 1:
# Send sequences to UI for selection vars.actions_metadata[vars.actions.get_last_key()+1]['Alternative Text'] = [item for item in vars.actions_metadata[vars.actions.get_last_key()+1]['Alternative Text'] if (item["Previous Selection"]!=True)]
genout = [[item['Text'], "redo"] for item in vars.actions_metadata[len(vars.actions)]['Alternative Text'] if (item["Previous Selection"]==True)] genresult(genout[0]['generated_text'], flash=True)
emit('from_server', {'cmd': 'genseqs', 'data': genout}, broadcast=True) else:
# Store sequences in memory until selection is made
vars.genseqs = genout
# Send sequences to UI for selection
genout = [[item['Text'], "redo"] for item in vars.actions_metadata[vars.actions.get_last_key()+1]['Alternative Text'] if (item["Previous Selection"]==True)]
emit('from_server', {'cmd': 'genseqs', 'data': genout}, broadcast=True)
else: else:
emit('from_server', {'cmd': 'popuperror', 'data': "There's nothing to undo"}, broadcast=True) emit('from_server', {'cmd': 'popuperror', 'data': "There's nothing to undo"}, broadcast=True)
send_debug() send_debug()
@ -3097,10 +3100,10 @@ def genresult(genout, flash=True):
vars.prompt = genout vars.prompt = genout
else: else:
vars.actions.append(genout) vars.actions.append(genout)
if len(vars.actions) > len(vars.actions_metadata): if vars.actions.get_next_id()-1 not in vars.actions_metadata:
vars.actions_metadata.append({'Selected Text': genout, 'Alternative Text': []}) vars.actions_metadata[vars.actions.get_next_id()-1] = {'Selected Text': genout, 'Alternative Text': []}
else: else:
vars.actions_metadata[len(vars.actions)-1]['Selected Text'] = genout vars.actions_metadata[vars.actions.get_next_id()-1]['Selected Text'] = genout
update_story_chunk('last') update_story_chunk('last')
if(flash): if(flash):
emit('from_server', {'cmd': 'texteffect', 'data': vars.actions.get_last_key() + 1 if len(vars.actions) else 0}, broadcast=True) emit('from_server', {'cmd': 'texteffect', 'data': vars.actions.get_last_key() + 1 if len(vars.actions) else 0}, broadcast=True)
@ -3120,24 +3123,24 @@ def genselect(genout):
# Add the options to the actions metadata # Add the options to the actions metadata
# If we've already generated text for this action but haven't selected one we'll want to kill all non-pinned, non-previous selection, and non-edited options then add the new ones # If we've already generated text for this action but haven't selected one we'll want to kill all non-pinned, non-previous selection, and non-edited options then add the new ones
if (len(vars.actions_metadata) > len(vars.actions)): if vars.actions.get_next_id() in vars.actions_metadata:
if (vars.actions_metadata[len(vars.actions)]['Selected Text'] == ""): if (vars.actions_metadata[vars.actions.get_next_id()]['Selected Text'] == ""):
vars.actions_metadata[len(vars.actions)]['Alternative Text'] = [{"Text": item['Text'], "Pinned": item['Pinned'], vars.actions_metadata[vars.actions.get_next_id()]['Alternative Text'] = [{"Text": item['Text'], "Pinned": item['Pinned'],
"Previous Selection": item["Previous Selection"], "Previous Selection": item["Previous Selection"],
"Edited": item["Edited"]} for item in vars.actions_metadata[len(vars.actions)]['Alternative Text'] "Edited": item["Edited"]} for item in vars.actions_metadata[vars.actions.get_next_id()]['Alternative Text']
if item['Pinned'] or item["Previous Selection"] or item["Edited"]] + [{"Text": text["generated_text"], if item['Pinned'] or item["Previous Selection"] or item["Edited"]] + [{"Text": text["generated_text"],
"Pinned": False, "Previous Selection": False, "Edited": False} for text in genout] "Pinned": False, "Previous Selection": False, "Edited": False} for text in genout]
else: else:
vars.actions_metadata.append({'Selected Text': '', 'Alternative Text': [{"Text": text["generated_text"], "Pinned": False, "Previous Selection": False, "Edited": False} for text in genout]}) vars.actions_metadata[vars.actions.get_next_id()] = {'Selected Text': '', 'Alternative Text': [{"Text": text["generated_text"], "Pinned": False, "Previous Selection": False, "Edited": False} for text in genout]}
else: else:
vars.actions_metadata.append({'Selected Text': '', 'Alternative Text': [{"Text": text["generated_text"], "Pinned": False, "Previous Selection": False, "Edited": False} for text in genout]}) vars.actions_metadata[vars.actions.get_next_id()] = {'Selected Text': '', 'Alternative Text': [{"Text": text["generated_text"], "Pinned": False, "Previous Selection": False, "Edited": False} for text in genout]}
genout = [{"generated_text": item['Text']} for item in vars.actions_metadata[len(vars.actions)]['Alternative Text'] if (item["Previous Selection"]==False) and (item["Edited"]==False)] genout = [{"generated_text": item['Text']} for item in vars.actions_metadata[vars.actions.get_next_id()]['Alternative Text'] if (item["Previous Selection"]==False) and (item["Edited"]==False)]
# Store sequences in memory until selection is made # Store sequences in memory until selection is made
vars.genseqs = genout vars.genseqs = genout
genout = [[item['Text'], "pinned" if item['Pinned'] else "normal"] for item in vars.actions_metadata[len(vars.actions)]['Alternative Text'] if (item["Previous Selection"]==False) and (item["Edited"]==False)] genout = [[item['Text'], "pinned" if item['Pinned'] else "normal"] for item in vars.actions_metadata[vars.actions.get_next_id()]['Alternative Text'] if (item["Previous Selection"]==False) and (item["Edited"]==False)]
# Send sequences to UI for selection # Send sequences to UI for selection
emit('from_server', {'cmd': 'genseqs', 'data': genout}, broadcast=True) emit('from_server', {'cmd': 'genseqs', 'data': genout}, broadcast=True)
@ -3153,8 +3156,8 @@ def selectsequence(n):
if(len(vars.lua_koboldbridge.feedback) != 0): if(len(vars.lua_koboldbridge.feedback) != 0):
vars.actions.append(vars.lua_koboldbridge.feedback) vars.actions.append(vars.lua_koboldbridge.feedback)
#We'll want to remove the option from the alternative text and put it in selected text #We'll want to remove the option from the alternative text and put it in selected text
vars.actions_metadata[len(vars.actions)-1]['Alternative Text'] = [item for item in vars.actions_metadata[len(vars.actions)-1]['Alternative Text'] if item['Text'] != vars.lua_koboldbridge.feedback] vars.actions_metadata[vars.actions.get_next_id()-1 ]['Alternative Text'] = [item for item in vars.actions_metadata[vars.actions.get_next_id()-1]['Alternative Text'] if item['Text'] != vars.lua_koboldbridge.feedback]
vars.actions_metadata[len(vars.actions)-1]['Selected Text'] = vars.lua_koboldbridge.feedback vars.actions_metadata[vars.actions.get_next_id()-1 ]['Selected Text'] = vars.lua_koboldbridge.feedback
update_story_chunk('last') update_story_chunk('last')
emit('from_server', {'cmd': 'texteffect', 'data': vars.actions.get_last_key() + 1 if len(vars.actions) else 0}, broadcast=True) emit('from_server', {'cmd': 'texteffect', 'data': vars.actions.get_last_key() + 1 if len(vars.actions) else 0}, broadcast=True)
emit('from_server', {'cmd': 'hidegenseqs', 'data': ''}, broadcast=True) emit('from_server', {'cmd': 'hidegenseqs', 'data': ''}, broadcast=True)
@ -3170,13 +3173,13 @@ def selectsequence(n):
def pinsequence(n): def pinsequence(n):
if n.isnumeric(): if n.isnumeric():
text = vars.genseqs[int(n)]['generated_text'] text = vars.genseqs[int(n)]['generated_text']
if text in [item['Text'] for item in vars.actions_metadata[len(vars.actions)]['Alternative Text']]: if text in [item['Text'] for item in vars.actions_metadata[vars.actions.get_next_id()]['Alternative Text']]:
alternatives = vars.actions_metadata[len(vars.actions)]['Alternative Text'] alternatives = vars.actions_metadata[vars.actions.get_next_id()]['Alternative Text']
for i in range(len(alternatives)): for i in range(len(alternatives)):
if alternatives[i]['Text'] == text: if alternatives[i]['Text'] == text:
alternatives[i]['Pinned'] = not alternatives[i]['Pinned'] alternatives[i]['Pinned'] = not alternatives[i]['Pinned']
break break
vars.actions_metadata[len(vars.actions)]['Alternative Text'] = alternatives vars.actions_metadata[vars.actions.get_next_id()]['Alternative Text'] = alternatives
send_debug() send_debug()
@ -4057,15 +4060,15 @@ def ikrequest(txt):
if not vars.quiet: if not vars.quiet:
print("{0}{1}{2}".format(colors.CYAN, genout, colors.END)) print("{0}{1}{2}".format(colors.CYAN, genout, colors.END))
vars.actions.append(genout) vars.actions.append(genout)
if len(vars.actions_metadata) < len(vars.actions): if vars.actions.get_next_id()-1 in vars.actions_metadata:
vars.actions_metadata.append({"Selected Text": genout, "Alternative Text": []}) vars.actions_metadata[vars.actions.get_next_id()-1] = {"Selected Text": genout, "Alternative Text": []}
else: else:
# 2. We've selected a chunk of text that is was presented previously # 2. We've selected a chunk of text that is was presented previously
alternatives = [item['Text'] for item in vars.actions_metadata[len(vars.actions)]["Alternative Text"]] alternatives = [item['Text'] for item in vars.actions_metadata[vars.actions.get_next_id()-1]["Alternative Text"]]
if genout in alternatives: if genout in alternatives:
alternatives = [item for item in vars.actions_metadata[len(vars.actions)]["Alternative Text"] if item['Text'] != genout] alternatives = [item for item in vars.actions_metadata[vars.actions.get_next_id()-1]["Alternative Text"] if item['Text'] != genout]
vars.actions_metadata[len(vars.actions)]["Alternative Text"] = alternatives vars.actions_metadata[vars.actions.get_next_id()-1]["Alternative Text"] = alternatives
vars.actions_metadata[len(vars.actions)]["Selected Text"] = genout vars.actions_metadata[vars.actions.get_next_id()-1]["Selected Text"] = genout
update_story_chunk('last') update_story_chunk('last')
emit('from_server', {'cmd': 'texteffect', 'data': vars.actions.get_last_key() + 1 if len(vars.actions) else 0}, broadcast=True) emit('from_server', {'cmd': 'texteffect', 'data': vars.actions.get_last_key() + 1 if len(vars.actions) else 0}, broadcast=True)
send_debug() send_debug()
@ -4127,15 +4130,15 @@ def oairequest(txt, min, max):
if not vars.quiet: if not vars.quiet:
print("{0}{1}{2}".format(colors.CYAN, genout, colors.END)) print("{0}{1}{2}".format(colors.CYAN, genout, colors.END))
vars.actions.append(genout) vars.actions.append(genout)
if len(vars.actions_metadata) < len(vars.actions): if vars.actions.get_next_id()-1 in vars.actions_metadata:
vars.actions_metadata.append({"Selected Text": genout, "Alternative Text": []}) vars.actions_metadata[vars.actions.get_next_id()-1] = {"Selected Text": genout, "Alternative Text": []}
else: else:
# 2. We've selected a chunk of text that is was presented previously # 2. We've selected a chunk of text that is was presented previously
alternatives = [item['Text'] for item in vars.actions_metadata[len(vars.actions)]["Alternative Text"]] alternatives = [item['Text'] for item in vars.actions_metadata[vars.actions.get_next_id()-1]["Alternative Text"]]
if genout in alternatives: if genout in alternatives:
alternatives = [item for item in vars.actions_metadata[len(vars.actions)]["Alternative Text"] if item['Text'] != genout] alternatives = [item for item in vars.actions_metadata[vars.actions.get_next_id()-1 ]["Alternative Text"] if item['Text'] != genout]
vars.actions_metadata[len(vars.actions)]["Alternative Text"] = alternatives vars.actions_metadata[vars.actions.get_next_id()-1 ]["Alternative Text"] = alternatives
vars.actions_metadata[len(vars.actions)]["Selected Text"] = genout vars.actions_metadata[vars.actions.get_next_id()-1 ]["Selected Text"] = genout
update_story_chunk('last') update_story_chunk('last')
emit('from_server', {'cmd': 'texteffect', 'data': vars.actions.get_last_key() + 1 if len(vars.actions) else 0}, broadcast=True) emit('from_server', {'cmd': 'texteffect', 'data': vars.actions.get_last_key() + 1 if len(vars.actions) else 0}, broadcast=True)
send_debug() send_debug()
@ -4394,9 +4397,15 @@ def loadRequest(loadpath, filename=None):
actions = collections.deque(js["actions"]) actions = collections.deque(js["actions"])
if "actions_metadata" in js: if "actions_metadata" in js:
vars.actions_metadata = js["actions_metadata"] if type(js["actions_metadata"]) == dict:
vars.actions_metadata = js["actions_metadata"]
else: else:
vars.actions_metadata = [{'Selected Text': text, 'Alternative Text': []} for text in js["actions"]] vars.actions_metadata = {}
i = 0
for text in js['actions']:
vars.actions_metadata[i] = {'Selected Text': text, 'Alternative Text': []}
i+=1
if(len(vars.prompt.strip()) == 0): if(len(vars.prompt.strip()) == 0):
@ -4834,7 +4843,7 @@ def newGameRequest():
vars.prompt = "" vars.prompt = ""
vars.memory = "" vars.memory = ""
vars.actions = structures.KoboldStoryRegister() vars.actions = structures.KoboldStoryRegister()
vars.actions_metadata = [] vars.actions_metadata = {}
vars.authornote = "" vars.authornote = ""
vars.authornotetemplate = vars.setauthornotetemplate vars.authornotetemplate = vars.setauthornotetemplate
@ -4921,7 +4930,7 @@ if(vars.model in ("TPUMeshTransformerGPTJ",)):
def send_debug(): def send_debug():
if vars.debug: if vars.debug:
debug_info = "" debug_info = ""
for variable in [["Action Length", len(vars.actions)], ["Actions Metadata Length", len(vars.actions_metadata)], ["Actions Metadata", vars.actions_metadata], ["Newline Mode", vars.newlinemode]]: for variable in [["Action Length", vars.actions.get_last_key()], ["Actions Metadata Length", max(vars.actions_metadata)], ["Actions Metadata", vars.actions_metadata], ["Newline Mode", vars.newlinemode]]:
debug_info = "{}{}: {}\n".format(debug_info, variable[0], variable[1]) debug_info = "{}{}: {}\n".format(debug_info, variable[0], variable[1])
emit('from_server', {'cmd': 'debug_info', 'data': debug_info}, broadcast=True) emit('from_server', {'cmd': 'debug_info', 'data': debug_info}, broadcast=True)