diff --git a/aiserver.py b/aiserver.py index 03ede98e..775856e1 100644 --- a/aiserver.py +++ b/aiserver.py @@ -7056,11 +7056,11 @@ def load_story_v1(js): if(koboldai_vars.gamestarted): #We set the action count higher so that we don't trigger a scroll in the UI. #Once all but the last is loaded we can bring it back down and do the last one so we scroll to it - koboldai_vars.actions.action_count += 1 - for i in range(len(js["actions"])-1): - koboldai_vars.actions.append(js["actions"][i], action_id_offset=-1, recalc=False) - koboldai_vars.actions.action_count -= 1 - koboldai_vars.actions.append(js["actions"][len(js["actions"])-1], recalc=False) + temp_story_class = koboldai_settings.KoboldStoryRegister(None, None, koboldai_vars, tokenizer=None) + + for i in range(len(js["actions"])): + temp_story_class.append(js["actions"][i], recalc=False) + if "actions_metadata" in js: if type(js["actions_metadata"]) == dict: @@ -7069,7 +7069,10 @@ def load_story_v1(js): data = js["actions_metadata"][key]["Alternative Text"] for i in range(len(js["actions_metadata"][key]["Alternative Text"])): data[i]["text"] = data[i].pop("Text") - koboldai_vars.actions.set_options(data, int(key)) + temp_story_class.set_options(data, int(key)) + print(temp_story_class.to_json()) + koboldai_vars.actions.load_json(temp_story_class.to_json()) + del temp_story_class # Try not to break older save files if("authorsnote" in js): @@ -7119,7 +7122,7 @@ def load_story_v1(js): send_debug() def load_story_v2(js): - logger.debug("Loading V1 Story") + logger.debug("Loading V2 Story") logger.debug("Called from {}".format(inspect.stack()[1].function)) leave_room(session['story']) session['story'] = js['story_name'] diff --git a/koboldai_settings.py b/koboldai_settings.py index 9ebd47ca..f6ea11fd 100644 --- a/koboldai_settings.py +++ b/koboldai_settings.py @@ -56,12 +56,14 @@ def process_variable_changes(socketio, classname, name, value, old_value, debug_ queue.put(["var_changed", {"classname": "story", "name": "actions", "old_value": None, "value":data_to_send}, {"broadcast":True, "room":room}]) else: - socketio.emit("var_changed", {"classname": "actions", "name": "Action Count", "old_value": None, "value":value.action_count}, broadcast=True, room=room) + if socketio is not None: + socketio.emit("var_changed", {"classname": "actions", "name": "Action Count", "old_value": None, "value":value.action_count}, broadcast=True, room=room) data_to_send = [] for i in list(value.actions)[-100:]: data_to_send.append({"id": i, "action": value.actions[i]}) - socketio.emit("var_changed", {"classname": "story", "name": "actions", "old_value": None, "value": data_to_send}, broadcast=True, room=room) + if socketio is not None: + socketio.emit("var_changed", {"classname": "story", "name": "actions", "old_value": None, "value": data_to_send}, broadcast=True, room=room) elif isinstance(value, KoboldWorldInfo): value.send_to_ui() else: @@ -73,7 +75,8 @@ def process_variable_changes(socketio, classname, name, value, old_value, debug_ queue.put(data) else: - socketio.emit("var_changed", {"classname": classname, "name": name, "old_value": clean_var_for_emit(old_value), "value": clean_var_for_emit(value)}, include_self=True, broadcast=True, room=room) + if socketio is not None: + socketio.emit("var_changed", {"classname": classname, "name": name, "old_value": clean_var_for_emit(old_value), "value": clean_var_for_emit(value)}, include_self=True, broadcast=True, room=room) class koboldai_vars(object): def __init__(self, socketio): @@ -1145,19 +1148,24 @@ class KoboldStoryRegister(object): self.action_count = json_data['action_count'] #JSON forces keys to be strings, so let's fix that temp = {} + data_to_send = [] for item in json_data['actions']: temp[int(item)] = json_data['actions'][item] if "WI Search Text" not in temp[int(item)]: temp[int(item)]["WI Search Text"] = re.sub("[^0-9a-z \'\"]", "", temp[int(item)]['Selected Text']) - data_to_send = [] if int(item) >= self.action_count-100: + print("added item {}".format(item)) data_to_send.append({"id": item, 'action': temp[int(item)]}) + else: + print("Not addinig item {}".format(item)) + print("sending {} actions".format(len(data_to_send))) process_variable_changes(self.socketio, "story", 'actions', data_to_send, None) self.actions = temp self.set_game_saved() - self.story_settings.save_story() + if self.story_settings is not None: + self.story_settings.save_story() def append(self, text, action_id_offset=0, recalc=True): self.clear_unused_options() @@ -1370,7 +1378,7 @@ class KoboldStoryRegister(object): return [] def set_game_saved(self): - if 'story_settings' in self.__dict__: + if self.story_settings is not None: self.story_settings.gamesaved = False def recalc_token_length(self, action_id): @@ -1454,9 +1462,12 @@ class KoboldStoryRegister(object): #start_time = time.time() #we're going to split our actions by sentence for better context. We'll add in which actions the sentence covers. Prompt will be added at a -1 ID actions = {i: self.actions[i]['Selected Text'] for i in self.actions} - actions[-1] = self.story_settings.prompt + if self.story_settings is None: + actions[-1] = "" + else: + actions[-1] = self.story_settings.prompt action_text = self.__str__() - action_text = "{}{}".format(self.story_settings.prompt, action_text) + action_text = "{}{}".format("" if self.story_settings is None else self.story_settings.prompt, action_text) ###########action_text_split = [sentence, actions used in sentence, token length, included in AI context]################ action_text_split = [[x+" ", [], 0, False] for x in re.split("(?<=[.!?])\s+", action_text)] #The last action shouldn't have the extra space from the sentence splitting, so let's remove it