Repository: ErikBjare/gptme
File: gptme/tools/form.py (Python)

Code before:
    code: str | None,
    args: list[str] | None,
    kwargs: dict[str, str] | None,
    confirm: ConfirmFunc,
) -> Generator[Message, None, None]:
    """Present a form to the user and collect their responses."""
    if not code:
        yield Message("system", "No form fields provided")
        return

    fields = parse_form_content(code)
    if not fields:
        yield Message("system", "No valid fields found in form definition")
        return

    # Import questionary here to handle import errors gracefully
    try:
        import questionary
    except ImportError:
        yield Message(
            "system",
            "questionary library not available. Please install it with: pip install questionary",
        )
        return

    results: dict = {}

    try:
        for field in fields:
            name = field["name"]
            prompt = field["prompt"]
            field_type = field["type"]

            if field_type == "select":
                value = questionary.select(
                    prompt,
                    choices=field["options"],
                    use_shortcuts=True,
                ).ask()
            elif field_type == "boolean":
                value = questionary.confirm(prompt, default=False).ask()
            elif field_type == "number":
                while True:
                    value_str = questionary.text(prompt).ask()
                    if value_str is None:
                        value = None
                        break
                    try:
                        # Try int first, then float
                        if "." in value_str:
                            value = float(value_str)

Review comment (refactor):
I feel like this should be rewritten to use a standard helper which appropriately detects the installation method and provides a correct command that considers any possible e.g. pipx (use `pipx inject gptme questionary`) or uv environments so that the correct install command is shown to be aware of the venv gptme is probably installed in.

This helper may be useful in other tools too, I think you should take the opportunity to review where it could be useful/refactored to share a common good utility instead of a brittle hint like this.

Code after:
    code: str | None,
    args: list[str] | None,
    kwargs: dict[str, str] | None,
    confirm: ConfirmFunc,
) -> Generator[Message, None, None]:
    """Present a form to the user and collect their responses."""
    if not code:
        yield Message("system", "No form fields provided")
        return

    fields = parse_form_content(code)
    if not fields:
        yield Message("system", "No valid fields found in form definition")
        return

    # Import questionary here to handle import errors gracefully
    try:
        import questionary
    except ImportError:
        from ..util.install import get_package_install_command

        install_cmd = get_package_install_command("questionary")
        yield Message(
            "system",
            f"questionary library not available. Please install it with: {install_cmd}",
        )
        return

    results: dict = {}

    try:
        for field in fields:
            name = field["name"]
            prompt = field["prompt"]
            field_type = field["type"]

            if field_type == "select":
                value = questionary.select(
                    prompt,
                    choices=field["options"],
                    use_shortcuts=True,
                ).ask()
            elif field_type == "boolean":
                value = questionary.confirm(prompt, default=False).ask()
            elif field_type == "number":
                while True:
                    value_str = questionary.text(prompt).ask()
                    if value_str is None:
                        value = None
                        break
                    try: