diff --git a/chatmastermind/message.py b/chatmastermind/message.py index 04def6d..d88ac5c 100644 --- a/chatmastermind/message.py +++ b/chatmastermind/message.py @@ -372,10 +372,6 @@ class Message(): tags = TagLine(fd.readline()).tags() except TagError: fd.seek(pos) - if tags_or or tags_and or tags_not: - # match with an empty set if the file has no tags - if not match_tags(tags, tags_or, tags_and, tags_not): - return None # AILine (Optional) try: pos = fd.tell() @@ -390,17 +386,23 @@ class Message(): fd.seek(pos) # Question and Answer text = fd.read().strip().split('\n') - try: - question_idx = text.index(Question.txt_header) + 1 - except ValueError: - raise MessageError(f"'{file_path}' does not contain a valid message") - try: - answer_idx = text.index(Answer.txt_header) - question = Question.from_list(text[question_idx:answer_idx]) - answer = Answer.from_list(text[answer_idx + 1:]) - except ValueError: - question = Question.from_list(text[question_idx:]) - return cls(question, answer, tags, ai, model, file_path) + try: + question_idx = text.index(Question.txt_header) + 1 + except ValueError: + raise MessageError(f"'{file_path}' does not contain a valid message") + try: + answer_idx = text.index(Answer.txt_header) + question = Question.from_list(text[question_idx:answer_idx]) + answer = Answer.from_list(text[answer_idx + 1:]) + except ValueError: + question = Question.from_list(text[question_idx:]) + # match tags AFTER reading the whole file + # -> make sure it's a valid 'txt' file format + if tags_or or tags_and or tags_not: + # match with an empty set if the file has no tags + if not match_tags(tags, tags_or, tags_and, tags_not): + return None + return cls(question, answer, tags, ai, model, file_path) @classmethod def __from_file_yaml(cls: Type[MessageInst], file_path: pathlib.Path) -> MessageInst: