diff --git a/aiserver.py b/aiserver.py index 31f03630..e012ab06 100644 --- a/aiserver.py +++ b/aiserver.py @@ -2182,7 +2182,7 @@ def patch_transformers(): if koboldai_vars.chatmode: return False - data = [applyoutputformatting(utils.decodenewlines(tokenizer.decode(x[-1])), no_sentence_trimming=True) for x in input_ids] + data = [applyoutputformatting(utils.decodenewlines(tokenizer.decode(x[-1])), no_sentence_trimming=True, no_single_line=True) for x in input_ids] koboldai_vars.actions.stream_tokens(data) return False @@ -6206,7 +6206,7 @@ def applyinputformatting(txt): #==================================================================# # Applies chosen formatting options to text returned from AI #==================================================================# -def applyoutputformatting(txt, no_sentence_trimming=False): +def applyoutputformatting(txt, no_sentence_trimming=False, no_single_line=False): #remove null ascii character (used to kill chat mode text in multi-generation) txt = txt.replace(chr(0), "") if len(txt) == 0: @@ -6232,7 +6232,7 @@ def applyoutputformatting(txt, no_sentence_trimming=False): if(koboldai_vars.frmtrmspch): txt = utils.removespecialchars(txt, koboldai_vars) # Single Line Mode - if(koboldai_vars.singleline or koboldai_vars.chatmode): + if((koboldai_vars.singleline or koboldai_vars.chatmode) and not no_single_line): txt = utils.singlelineprocessing(txt, koboldai_vars) for sub in koboldai_vars.substitutions: diff --git a/koboldai_settings.py b/koboldai_settings.py index d127c078..97e3e15f 100644 --- a/koboldai_settings.py +++ b/koboldai_settings.py @@ -838,7 +838,7 @@ class story_settings(settings): new_world_info.socketio = self.socketio self.worldinfo_v2 = new_world_info - def assign_world_info_to_actions(self, action_id=None, wuid=None): + def assign_world_info_to_actions(self, action_id=None, wuid=None, no_transmit=False): logger.debug("Calcing WI Assignment for action_id: {} wuid: {}".format(action_id, wuid)) if action_id != -1 and (action_id is None or action_id not in self.actions.actions): actions_to_check = self.actions.actions @@ -853,7 +853,7 @@ class story_settings(settings): for uid, wi in wi_to_check.items(): for key in sorted(wi['key'], key=len, reverse=True): if key in action['Selected Text']: - self.actions.add_wi_to_action(action_id, key, wi['content'], uid) + self.actions.add_wi_to_action(action_id, key, wi['content'], uid, no_transmit=no_transmit) break #Do prompt if no action_id was sent @@ -864,10 +864,10 @@ class story_settings(settings): if wi['keysecondary'] != []: for key2 in wi['keysecondary']: if key2 in self.prompt: - self.actions.add_wi_to_action(-1, key, wi['content'], uid) + self.actions.add_wi_to_action(-1, key, wi['content'], uid, no_transmit=no_transmit) break else: - self.actions.add_wi_to_action(-1, key, wi['content'], uid) + self.actions.add_wi_to_action(-1, key, wi['content'], uid, no_transmit=no_transmit) break def __setattr__(self, name, value): @@ -1138,7 +1138,7 @@ class KoboldStoryRegister(object): def reset(self, sequence=[]): self.__init__(self.socketio, self.story_settings, self.koboldai_vars, sequence=sequence) - def add_wi_to_action(self, action_id, key, content, uid): + def add_wi_to_action(self, action_id, key, content, uid, no_transmit=False): old = self.story_settings.prompt_wi_highlighted_text.copy() if action_id == -1 else self.actions[action_id].copy() #First check to see if we have the wi_highlighted_text variable if action_id != -1: @@ -1174,10 +1174,12 @@ class KoboldStoryRegister(object): i+=1 if action_id != -1: if old != self.actions[action_id]: - process_variable_changes(self.socketio, "story", 'actions', {"id": action_id, 'action': self.actions[action_id]}, old) + if not no_transmit: + process_variable_changes(self.socketio, "story", 'actions', {"id": action_id, 'action': self.actions[action_id]}, old) else: if old != self.story_settings.prompt_wi_highlighted_text: - process_variable_changes(self.socketio, "story", 'prompt_wi_highlighted_text', self.story_settings.prompt_wi_highlighted_text, old) + if not no_transmit: + process_variable_changes(self.socketio, "story", 'prompt_wi_highlighted_text', self.story_settings.prompt_wi_highlighted_text, old) def __str__(self): @@ -1228,7 +1230,7 @@ class KoboldStoryRegister(object): old = None self.actions[i] = {"Selected Text": text, "Probabilities": [], "Options": []} - self.story_settings.assign_world_info_to_actions(action_id=i) + self.story_settings.assign_world_info_to_actions(action_id=i, no_transmit=True) process_variable_changes(self.socketio, "story", 'actions', {"id": i, 'action': self.actions[i]}, old) logger.debug("Calcing AI Text from Action __setitem__") ignore = self.koboldai_vars.calc_ai_text() @@ -1271,7 +1273,7 @@ class KoboldStoryRegister(object): #Check if our json has our new world info highlighting data if len(self.actions) > 0: if 'wi_highlighted_text' not in self.actions[0]: - self.story_settings.assign_world_info_to_actions() + self.story_settings.assign_world_info_to_actions(no_transmit=True) logger.debug("Calcing AI Text from Action load from json") ignore = self.koboldai_vars.calc_ai_text() @@ -1299,7 +1301,7 @@ class KoboldStoryRegister(object): "Options": [], "Probabilities": []} if self.story_settings is not None: - self.story_settings.assign_world_info_to_actions(action_id=action_id) + self.story_settings.assign_world_info_to_actions(action_id=action_id, no_transmit=True) process_variable_changes(self.socketio, "story", 'actions', {"id": action_id, 'action': self.actions[action_id]}, None) self.set_game_saved() if recalc: @@ -1406,9 +1408,9 @@ class KoboldStoryRegister(object): if action_step-1 == self.action_count: self.action_count+=1 self.socketio.emit("var_changed", {"classname": "actions", "name": "Action Count", "old_value": None, "value":self.action_count, "transmit_time": str(datetime.datetime.now())}, broadcast=True, room="UI_2") - self.story_settings.assign_world_info_to_actions(action_id=action_step) - process_variable_changes(self.socketio, "story", 'actions', {"id": action_step, 'action': self.actions[action_step]}, None) + self.story_settings.assign_world_info_to_actions(action_id=action_step, no_transmit=True) self.clear_unused_options(pointer=action_step) + #process_variable_changes(self.socketio, "story", 'actions', {"id": action_step, 'action': self.actions[action_step]}, None) self.set_game_saved() logger.debug("Calcing AI Text from Action Use Option") ignore = self.koboldai_vars.calc_ai_text() diff --git a/static/koboldai.js b/static/koboldai.js index 4fdf80c3..f8489cbd 100644 --- a/static/koboldai.js +++ b/static/koboldai.js @@ -317,6 +317,60 @@ function create_options(action) { //option_chunk.scrollIntoView(); } +function process_actions_data(data) { + start_time = Date.now(); + if (document.getElementById("Selected Text").firstElementChild.id == "story_prompt") { + first_story_element = document.getElementById("Selected Text").firstElementChild.nextElementSibling; + } else { + first_story_element = document.getElementById("Selected Text").firstElementChild; + } + if (Array.isArray(data.value)) { + actions = data.value; + } else { + actions = [data.value]; + } + if (actions.length == 0) {return;} + let action_type = "????"; + let first_action = -100; + //console.log('Chunk Exists: '+(document.getElementById("Selected Text Chunk " + actions[actions.length-1].id))); + //console.log('Passed action has no selected text: '+(actions[actions.length-1].action['Selected Text'] == "")); + //console.log('Old action has no selected text: '+(actions_data[Math.max.apply(null,Object.keys(actions_data).map(Number))]['Selected Text'] == "")); + //console.log(actions_data[Math.max.apply(null,Object.keys(actions_data).map(Number))]['Selected Text']); + //console.log('All action IDs already seen: '+((actions[0].id in actions_data) && (actions[actions.length-1].id in actions_data))); + //we need to figure out if this is changes to existing text, added text at the end, or infinite scroll text at the begining + if (!(document.getElementById("Selected Text Chunk " + actions[actions.length-1].id))) { + //We don't have this item yet, either we're an append or an prepend + if ((Object.keys(actions_data).length > 1) && (actions[actions.length-1].id < Math.min.apply(null,Object.keys(actions_data).map(Number).filter(function(x){return x>0})))) { + //adding to the begining + action_type = "prepend"; + first_action = Math.min.apply(null,Object.keys(actions_data).map(Number).filter(function(x){return x>0})); + } else if (actions[actions.length-1].id > Math.max.apply(null,Object.keys(actions_data).map(Number))) { + action_type = "append"; + } + } else if (actions[actions.length-1].action['Selected Text'] == "") { + action_type = "options text only or deleted chunk"; + } else if (actions_data[Math.max.apply(null,Object.keys(actions_data).map(Number))]['Selected Text'] == "") { + action_type = "append"; + } else if ((actions[0].id in actions_data) && (actions[actions.length-1].id in actions_data)) { + //update + action_type = "update"; + } + for (action of actions) { + actions_data[parseInt(action.id)] = action.action; + do_story_text_updates(action); + create_options(action); + if ('Probabilities' in action.action) { + do_probabilities(action); + } + } + + clearTimeout(game_text_scroll_timeout); + game_text_scroll_timeout = setTimeout(run_infinite_scroll_update.bind(null, action_type, actions, first_action), 200); + + hide_show_prompt(); + //console.log("Took "+((Date.now()-start_time)/1000)+"s to process"); +} + function do_story_text_updates(action) { story_area = document.getElementById('Selected Text'); current_chunk_number = action.id; @@ -574,51 +628,7 @@ function var_changed(data) { } //Special Case for Actions if ((data.classname == "story") && (data.name == "actions")) { - start_time = Date.now(); - //temp_counter += 1; - //if (temp_counter > 10) { - // return; - //} - if (document.getElementById("Selected Text").firstElementChild.id == "story_prompt") { - first_story_element = document.getElementById("Selected Text").firstElementChild.nextElementSibling; - } else { - first_story_element = document.getElementById("Selected Text").firstElementChild; - } - if (Array.isArray(data.value)) { - actions = data.value; - } else { - actions = [data.value]; - } - if (actions.length == 0) {return;} - let action_type = "????"; - let first_action = -100; - //we need to figure out if this is changes to existing text, added text at the end, or infinite scroll text at the begining - if ((actions[0].id in actions_data) && (actions[actions.length-1].id in actions_data)) { - //update - action_type = "update"; - } else if ((Object.keys(actions_data).length > 1) && (actions[actions.length-1].id < Math.min.apply(null,Object.keys(actions_data).map(Number).filter(function(x){return x>0})))) { - //adding to the begining - action_type = "prepend"; - first_action = Math.min.apply(null,Object.keys(actions_data).map(Number).filter(function(x){return x>0})); - } else if (actions[actions.length-1].id > Math.max.apply(null,Object.keys(actions_data).map(Number))) { - action_type = "append"; - } - - for (action of actions) { - actions_data[parseInt(action.id)] = action.action; - do_story_text_updates(action); - create_options(action); - if ('Probabilities' in action.action) { - do_probabilities(action); - } - } - - clearTimeout(game_text_scroll_timeout); - game_text_scroll_timeout = setTimeout(run_infinite_scroll_update.bind(null, action_type, actions, first_action), 200); - - hide_show_prompt(); - //console.log("Took "+((Date.now()-start_time)/1000)+"s to process"); - + process_actions_data(data) //Special Case for Presets } else if ((data.classname == 'model') && (data.name == 'presets')) { do_presets(data); @@ -5290,7 +5300,7 @@ function infinite_scroll() { } function run_infinite_scroll_update(action_type, actions, first_action) { - //console.log("action_type: "+action_type); + console.log("action_type: "+action_type); //console.log("first_action: "+first_action); if (action_type == "append") { if (document.getElementById('Selected Text Chunk '+actions[actions.length-1].id)) {