translation: some small required refactoring
This commit is contained in:
@@ -3,9 +3,10 @@ import argparse
|
||||
from pathlib import Path
|
||||
from itertools import zip_longest
|
||||
from copy import deepcopy
|
||||
from .common import invert_input_tag_args, add_file_as_code, add_file_as_text
|
||||
from ..configuration import Config
|
||||
from ..chat import ChatDB, msg_location
|
||||
from ..message import Message, MessageFilter, MessageError, Question, source_code
|
||||
from ..message import Message, MessageFilter, Question
|
||||
from ..ai_factory import create_ai
|
||||
from ..ai import AI, AIResponse
|
||||
|
||||
@@ -14,47 +15,6 @@ class QuestionCmdError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def add_file_as_text(question_parts: list[str], file: str) -> None:
|
||||
"""
|
||||
Add the given file as plain text to the question part list.
|
||||
If the file is a Message, add the answer.
|
||||
"""
|
||||
file_path = Path(file)
|
||||
content: str
|
||||
try:
|
||||
message = Message.from_file(file_path)
|
||||
if message and message.answer:
|
||||
content = message.answer
|
||||
except MessageError:
|
||||
with open(file) as r:
|
||||
content = r.read().strip()
|
||||
if len(content) > 0:
|
||||
question_parts.append(content)
|
||||
|
||||
|
||||
def add_file_as_code(question_parts: list[str], file: str) -> None:
|
||||
"""
|
||||
Add all source code from the given file. If no code segments can be extracted,
|
||||
the whole content is added as source code segment. If the file is a Message,
|
||||
extract the source code from the answer.
|
||||
"""
|
||||
file_path = Path(file)
|
||||
content: str
|
||||
try:
|
||||
message = Message.from_file(file_path)
|
||||
if message and message.answer:
|
||||
content = message.answer
|
||||
except MessageError:
|
||||
with open(file) as r:
|
||||
content = r.read().strip()
|
||||
# extract and add source code
|
||||
code_parts = source_code(content, include_delims=True)
|
||||
if len(code_parts) > 0:
|
||||
question_parts += code_parts
|
||||
else:
|
||||
question_parts.append(f"```\n{content}\n```")
|
||||
|
||||
|
||||
def create_msg_args(msg: Message, args: argparse.Namespace) -> argparse.Namespace:
|
||||
"""
|
||||
Takes an existing message and CLI arguments, and returns modified args based
|
||||
@@ -163,22 +123,6 @@ def repeat_messages(messages: list[Message], chat: ChatDB, args: argparse.Namesp
|
||||
make_request(ai, chat, message, msg_args)
|
||||
|
||||
|
||||
def invert_input_tag_args(args: argparse.Namespace) -> None:
|
||||
"""
|
||||
Changes the semantics of the INPUT tags for this command:
|
||||
* not tags specified on the CLI -> no tags are selected
|
||||
* empty tags specified on the CLI -> all tags are selected
|
||||
"""
|
||||
if args.or_tags is None:
|
||||
args.or_tags = set()
|
||||
elif len(args.or_tags) == 0:
|
||||
args.or_tags = None
|
||||
if args.and_tags is None:
|
||||
args.and_tags = set()
|
||||
elif len(args.and_tags) == 0:
|
||||
args.and_tags = None
|
||||
|
||||
|
||||
def question_cmd(args: argparse.Namespace, config: Config) -> None:
|
||||
"""
|
||||
Handler for the 'question' command.
|
||||
|
||||
Reference in New Issue
Block a user