diff --git a/chatmastermind/commands/question.py b/chatmastermind/commands/question.py index 37b62c4..e35bfe5 100644 --- a/chatmastermind/commands/question.py +++ b/chatmastermind/commands/question.py @@ -105,6 +105,25 @@ def make_request(ai: AI, chat: ChatDB, message: Message, args: argparse.Namespac print(response.tokens) +def repeat_messages(messages: list[Message], ai: AI, chat: ChatDB, args: argparse.Namespace) -> None: + """ + Repeat the given messages using the given arguments. + """ + for msg in messages: + print(f"--------- Repeating message '{msg.msg_id()}': ---------") + # overwrite the latest message if requested or empty + # -> but not if it's in the DB! + if ((msg.answer is None or args.overwrite is True) + and (not chat.msg_in_db(msg))): # noqa: W503 + msg.clear_answer() + make_request(ai, chat, msg, args) + # otherwise create a new one + else: + args.ask = [msg.question] + message = create_message(chat, args) + make_request(ai, chat, message, args) + + def question_cmd(args: argparse.Namespace, config: Config) -> None: """ Handler for the 'question' command. @@ -120,7 +139,6 @@ def question_cmd(args: argparse.Namespace, config: Config) -> None: message = create_message(chat, args) if args.create: return - # create the correct AI instance ai: AI = create_ai(args, config) @@ -129,22 +147,18 @@ def question_cmd(args: argparse.Namespace, config: Config) -> None: make_request(ai, chat, message, args) # === REPEAT === elif args.repeat is not None: - lmessage = chat.msg_latest(loc='cache') - if lmessage is None: - print("No message found to repeat!") - sys.exit(1) + repeat_msgs: list[Message] = [] + # repeat latest message + if len(args.repeat) == 0: + lmessage = chat.msg_latest(loc='cache') + if lmessage is None: + print("No message found to repeat!") + sys.exit(1) + repeat_msgs.append(lmessage) + # repeat given message(s) else: - print(f"Repeating message '{lmessage.msg_id()}':") - # overwrite the latest message if requested or empty - if lmessage.answer is None or args.overwrite is True: - lmessage.clear_answer() - make_request(ai, chat, lmessage, args) - # otherwise create a new one - else: - args.ask = [lmessage.question] - message = create_message(chat, args) - make_request(ai, chat, message, args) - + repeat_msgs = chat.msg_find(args.repeat, loc='disk') + repeat_messages(repeat_msgs, ai, chat, args) # === PROCESS === elif args.process is not None: # TODO: process either all questions without an