Keep generated answer as plain text payload

This commit is contained in:
Edern Deneuville
2026-09-12 12:18:42 +02:00
parent 1c65fb5868
commit f6de21ef70
+7 -20
View File
@@ -11,22 +11,16 @@ import keyboard
from .ai_client import AIClientError, ask_ai
from .clipboard_capture import capture_clipboard
from .config import AppConfig, load_config
from .key_stepper import KeyStepper, TypeAction
from .math_format import format_math_text, split_word_equation_segments
from .key_stepper import KeyStepper
from .math_format import format_math_text
LOG = logging.getLogger("ai_typewriter")
def prepare_type_payload(answer: str, mode: str) -> str | list[TypeAction]:
if mode == "word_equation":
return [TypeAction(segment.kind, segment.value) for segment in split_word_equation_segments(answer)]
def prepare_type_payload(answer: str, mode: str) -> str:
return format_math_text(answer, mode)
def payload_length(payload: str | list[TypeAction]) -> int:
return len(payload)
class AITypewriterApp:
def __init__(self, config: AppConfig) -> None:
self.config = config
@@ -55,15 +49,12 @@ class AITypewriterApp:
started_at = time.perf_counter()
answer = ask_ai(selected, self.config)
answer_to_type = prepare_type_payload(answer, self.config.math_text_format)
typed_units = payload_length(answer_to_type)
elapsed = time.perf_counter() - started_at
LOG.info(
"Réponse reçue en %.2f s: %d caractères (%d unités à injecter en mode %s). Premier caractère/action écrit automatiquement, %d restants au clavier.",
"Réponse reçue en %.2f s: %d caractères à écrire en mode %s. Appuyez sur une touche pour écrire chaque caractère.",
elapsed,
len(answer),
typed_units,
len(answer_to_type),
self.config.math_text_format,
max(typed_units - 1, 0),
)
if self._stepper is not None:
self._stepper.stop()
@@ -100,13 +91,9 @@ def main(argv: list[str] | None = None) -> int:
started_at = time.perf_counter()
answer = ask_ai(args.ask, config)
answer_to_type = prepare_type_payload(answer, config.math_text_format)
typed_units = payload_length(answer_to_type)
elapsed = time.perf_counter() - started_at
LOG.info("Réponse générée en %.2f s: %d caractères (%d unités en mode %s).", elapsed, len(answer), typed_units, config.math_text_format)
if isinstance(answer_to_type, str):
print(answer_to_type)
else:
print("".join(f"[Alt+= {action.value}]" if action.kind == "equation" else action.value for action in answer_to_type))
LOG.info("Réponse générée en %.2f s: %d caractères en mode %s.", elapsed, len(answer_to_type), config.math_text_format)
print(answer_to_type)
return 0
except AIClientError as exc:
LOG.error("%s", exc)