Merge pull request #86 from ebolam/united

Fixed error in redo action
This commit is contained in:
henk717 2022-02-28 14:43:53 +01:00 committed by GitHub
commit 261981da45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 66 additions and 57 deletions

View File

@ -158,7 +158,7 @@ class vars:
setauthornotetemplate = authornotetemplate # Saved author's note template in settings
andepth = 3 # How far back in history to append author's note
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
# Dictonary keys are:
# Selected Text: (text the user had selected. None when this is a newly generated action)
@ -2675,7 +2675,7 @@ def actionsubmit(data, actionmode=0, force_submit=False, force_prompt_gen=False,
# we now need to update the actions_metadata
# we'll have two conditions.
# 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": []})
else:
# 2. We've selected a chunk of text that is was presented previously
@ -2686,9 +2686,9 @@ def actionsubmit(data, actionmode=0, force_submit=False, force_prompt_gen=False,
print(vars.actions_metadata)
raise
if data in alternatives:
alternatives = [item for item in vars.actions_metadata[len(vars.actions)-1]["Alternative Text"] if item['Text'] != data]
vars.actions_metadata[len(vars.actions)-1]["Alternative Text"] = alternatives
vars.actions_metadata[len(vars.actions)-1]["Selected 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[vars.actions.get_next_id()-1]["Alternative Text"] = alternatives
vars.actions_metadata[vars.actions.get_next_id()-1]["Selected Text"] = data
update_story_chunk('last')
send_debug()
@ -2746,11 +2746,11 @@ def actionretry(data):
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
# 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,
"Previous Selection": True,
"Edited": False}] + vars.actions_metadata[len(vars.actions)-1]['Alternative Text']
vars.actions_metadata[len(vars.actions)-1]['Selected Text'] = ""
"Edited": False}] + vars.actions_metadata[vars.actions.get_next_id()-1 ]['Alternative Text']
vars.actions_metadata[vars.actions.get_next_id()-1]['Selected Text'] = ""
@ -2774,16 +2774,18 @@ def actionback():
# Remove last index of actions and refresh game screen
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
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,
"Previous Selection": True,
"Edited": False}] + vars.actions_metadata[len(vars.actions)-1]['Alternative Text']
vars.actions_metadata[len(vars.actions)-1]['Selected Text'] = ""
"Edited": False}] + vars.actions_metadata[vars.actions.get_next_id()-1 ]['Alternative Text']
vars.actions_metadata[vars.actions.get_next_id()-1 ]['Selected Text'] = ""
last_key = vars.actions.get_last_key()
vars.actions.pop()
vars.recentback = True
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):
emit('from_server', {'cmd': 'errmsg', 'data': "Cannot delete the prompt."})
else:
@ -2792,21 +2794,22 @@ def actionback():
def actionredo():
i = 0
if len(vars.actions) < len(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 = 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 vars.actions.get_last_key()+1 in vars.actions_metadata:
genout = [{"generated_text": item['Text']} for item in vars.actions_metadata[vars.actions.get_last_key()+1]['Alternative Text'] if (item["Previous Selection"]==True)]
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:
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)]
genresult(genout[0]['generated_text'], flash=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[len(vars.actions)]['Alternative Text'] if (item["Previous Selection"]==True)]
emit('from_server', {'cmd': 'genseqs', 'data': genout}, broadcast=True)
# 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:
emit('from_server', {'cmd': 'popuperror', 'data': "There's nothing to undo"}, broadcast=True)
send_debug()
@ -3203,10 +3206,10 @@ def genresult(genout, flash=True):
vars.prompt = genout
else:
vars.actions.append(genout)
if len(vars.actions) > len(vars.actions_metadata):
vars.actions_metadata.append({'Selected Text': genout, 'Alternative Text': []})
if vars.actions.get_next_id()-1 not in vars.actions_metadata:
vars.actions_metadata[vars.actions.get_next_id()-1] = {'Selected Text': genout, 'Alternative Text': []}
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')
if(flash):
emit('from_server', {'cmd': 'texteffect', 'data': vars.actions.get_last_key() + 1 if len(vars.actions) else 0}, broadcast=True)
@ -3226,24 +3229,24 @@ def genselect(genout):
# 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 (len(vars.actions_metadata) > len(vars.actions)):
if (vars.actions_metadata[len(vars.actions)]['Selected Text'] == ""):
vars.actions_metadata[len(vars.actions)]['Alternative Text'] = [{"Text": item['Text'], "Pinned": item['Pinned'],
if vars.actions.get_next_id() in vars.actions_metadata:
if (vars.actions_metadata[vars.actions.get_next_id()]['Selected Text'] == ""):
vars.actions_metadata[vars.actions.get_next_id()]['Alternative Text'] = [{"Text": item['Text'], "Pinned": item['Pinned'],
"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"],
"Pinned": False, "Previous Selection": False, "Edited": False} for text in genout]
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:
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
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
emit('from_server', {'cmd': 'genseqs', 'data': genout}, broadcast=True)
@ -3259,8 +3262,8 @@ def selectsequence(n):
if(len(vars.lua_koboldbridge.feedback) != 0):
vars.actions.append(vars.lua_koboldbridge.feedback)
#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[len(vars.actions)-1]['Selected 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[vars.actions.get_next_id()-1 ]['Selected Text'] = vars.lua_koboldbridge.feedback
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': 'hidegenseqs', 'data': ''}, broadcast=True)
@ -3276,13 +3279,13 @@ def selectsequence(n):
def pinsequence(n):
if n.isnumeric():
text = vars.genseqs[int(n)]['generated_text']
if text in [item['Text'] for item in vars.actions_metadata[len(vars.actions)]['Alternative Text']]:
alternatives = 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[vars.actions.get_next_id()]['Alternative Text']
for i in range(len(alternatives)):
if alternatives[i]['Text'] == text:
alternatives[i]['Pinned'] = not alternatives[i]['Pinned']
break
vars.actions_metadata[len(vars.actions)]['Alternative Text'] = alternatives
vars.actions_metadata[vars.actions.get_next_id()]['Alternative Text'] = alternatives
send_debug()
@ -4163,15 +4166,15 @@ def ikrequest(txt):
if not vars.quiet:
print("{0}{1}{2}".format(colors.CYAN, genout, colors.END))
vars.actions.append(genout)
if len(vars.actions_metadata) < len(vars.actions):
vars.actions_metadata.append({"Selected Text": genout, "Alternative Text": []})
if vars.actions.get_next_id()-1 in vars.actions_metadata:
vars.actions_metadata[vars.actions.get_next_id()-1] = {"Selected Text": genout, "Alternative Text": []}
else:
# 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:
alternatives = [item for item in vars.actions_metadata[len(vars.actions)]["Alternative Text"] if item['Text'] != genout]
vars.actions_metadata[len(vars.actions)]["Alternative Text"] = alternatives
vars.actions_metadata[len(vars.actions)]["Selected 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[vars.actions.get_next_id()-1]["Alternative Text"] = alternatives
vars.actions_metadata[vars.actions.get_next_id()-1]["Selected Text"] = genout
update_story_chunk('last')
emit('from_server', {'cmd': 'texteffect', 'data': vars.actions.get_last_key() + 1 if len(vars.actions) else 0}, broadcast=True)
send_debug()
@ -4233,15 +4236,15 @@ def oairequest(txt, min, max):
if not vars.quiet:
print("{0}{1}{2}".format(colors.CYAN, genout, colors.END))
vars.actions.append(genout)
if len(vars.actions_metadata) < len(vars.actions):
vars.actions_metadata.append({"Selected Text": genout, "Alternative Text": []})
if vars.actions.get_next_id()-1 in vars.actions_metadata:
vars.actions_metadata[vars.actions.get_next_id()-1] = {"Selected Text": genout, "Alternative Text": []}
else:
# 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:
alternatives = [item for item in vars.actions_metadata[len(vars.actions)]["Alternative Text"] if item['Text'] != genout]
vars.actions_metadata[len(vars.actions)]["Alternative Text"] = alternatives
vars.actions_metadata[len(vars.actions)]["Selected 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[vars.actions.get_next_id()-1 ]["Alternative Text"] = alternatives
vars.actions_metadata[vars.actions.get_next_id()-1 ]["Selected Text"] = genout
update_story_chunk('last')
emit('from_server', {'cmd': 'texteffect', 'data': vars.actions.get_last_key() + 1 if len(vars.actions) else 0}, broadcast=True)
send_debug()
@ -4500,9 +4503,15 @@ def loadRequest(loadpath, filename=None):
actions = collections.deque(js["actions"])
if "actions_metadata" in js:
vars.actions_metadata = js["actions_metadata"]
if type(js["actions_metadata"]) == dict:
vars.actions_metadata = js["actions_metadata"]
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):
@ -4886,7 +4895,7 @@ def newGameRequest():
vars.prompt = ""
vars.memory = ""
vars.actions = structures.KoboldStoryRegister()
vars.actions_metadata = []
vars.actions_metadata = {}
vars.authornote = ""
vars.authornotetemplate = vars.setauthornotetemplate
@ -4980,7 +4989,7 @@ if(vars.model in ("TPUMeshTransformerGPTJ",)):
def send_debug():
if vars.debug:
debug_info = ""
for variable in [["Newline Mode", vars.newlinemode], ["Action Length", len(vars.actions)], ["Actions Metadata Length", len(vars.actions_metadata)], ["Actions Metadata", vars.actions_metadata]]:
for variable in [["Newline Mode", vars.newlinemode], ["Action Length", vars.actions.get_last_key()], ["Actions Metadata Length", max(vars.actions_metadata)], ["Actions Metadata", vars.actions_metadata]]:
debug_info = "{}{}: {}\n".format(debug_info, variable[0], variable[1])
emit('from_server', {'cmd': 'debug_info', 'data': debug_info}, broadcast=True)