Repository: ErikBjare/gptme
File: gptme/hooks/cli_confirm.py (Python)

Code before:
    should_auto, message = check_auto_confirm()
    if should_auto:
        if message:
            console.log(message)
        return ConfirmationResult.confirm()

    # Build the confirmation prompt
    print_bell()  # Ring the bell before asking
    termios.tcflush(sys.stdin, termios.TCIFLUSH)

    # Build choice string with available options
    choicestr = "[Y/n"
    if copiable:
        choicestr += "/c"
    if editable:
        choicestr += "/e"
    choicestr += "/a/?]"

    # Build confirmation question
    # For simple confirms, use the preview directly as the question
    if is_simple_confirm and preview:  # preview check for mypy
        question = preview.rstrip()
    elif confirm_msg:
        question = f"Execute {tool_use.tool}? {confirm_msg}"
    else:
        question = f"Execute {tool_use.tool}?"

    with terminal_state_title("❓ waiting for confirmation"):
        session = get_prompt_session()
        prompt = f"{question} {choicestr}"

        style_prompt_toolkit = "bold fg:ansiyellow bg:ansired"
        answer = (
            session.prompt(
                [
                    (style_prompt_toolkit, " " + prompt + " "),
                    ("", " "),
                ]
            )
            .lower()
            .strip()
        )

    # Handle the response
    return _handle_response(answer, content, editable, copiable, tool_use)


def _handle_response(
    answer: str,
    content: str | None,
    editable: bool,

Review comment (suggestion):
The preview is not the question! It's code/tool content/args!

If we have previews that end with `?`, then that's probably a bad argument that should have been a confirm_msg.

Code after:
        choicestr += "/e"
    choicestr += "/a/?]"

    question = f"Execute {tool_use.tool}?"

    with terminal_state_title("❓ waiting for confirmation"):
        session = get_prompt_session()
        prompt = f"{question} {choicestr}"

        style_prompt_toolkit = "bold fg:ansiyellow bg:ansired"
        answer = (
            session.prompt(
                [
                    (style_prompt_toolkit, " " + prompt + " "),
                    ("", " "),
                ]
            )
            .lower()
            .strip()
        )

    # Handle the response
    return _handle_response(answer, content, editable, copiable, tool_use)


def _handle_response(
    answer: str,
    content: str | None,
    editable: bool,
    copiable: bool,
    tool_use: "ToolUse",
) -> ConfirmationResult:
    """Handle user response to confirmation prompt."""

    # Copy option
    if copiable and answer == "c":
        if copy():
            print("Copied to clipboard.")
        return ConfirmationResult.skip("Copied to clipboard, execution skipped")

    # Edit option
    if editable and answer == "e" and content:
        ext = _get_ext_for_tool(tool_use)
        edited = edit_text_with_editor(content, ext=ext)
        if edited != content:
            print("Content updated.")
            return ConfirmationResult.edit(edited)
        return ConfirmationResult.skip("No changes made, execution skipped")

    # Auto-confirm options
    import re