Repository: ErikBjare/gptme
File: gptme/chat.py (Python)

Code before:
        msg_response = reply(msgs, get_model().model, stream, tools)
        if os.environ.get("GPTME_COSTS") in ["1", "true"]:
            log_costs(msgs + [msg_response])

        # log response and run tools
        if msg_response:
            yield msg_response.replace(quiet=True)
            yield from execute_msg(msg_response, confirm)
    finally:
        clear_interruptible()


def prompt_user(value=None) -> str:  # pragma: no cover
    print_bell()
    # Flush stdin to clear any buffered input before prompting
    termios.tcflush(sys.stdin, termios.TCIFLUSH)
    response = ""
    while not response:
        try:
            set_interruptible()
            response = prompt_input(PROMPT_USER, value)
            if response:
                add_history(response)
        except KeyboardInterrupt:
            print("\nInterrupted. Press Ctrl-D to exit.")
        except EOFError:
            print("\nGoodbye!")
            sys.exit(0)
    clear_interruptible()
    return response


def prompt_input(prompt: str, value=None) -> str:  # pragma: no cover
    """Get input using prompt_toolkit with fish-style suggestions."""
    prompt = prompt.strip() + ": "
    if value:
        console.print(prompt + value)
        return value

    return get_input(prompt)


def _find_potential_paths(content: str) -> list[str]:
    """
    Find potential file paths and URLs in a message content.
    Excludes content within code blocks.

    Args:
        content: The message content to search

    Returns:

Review comment (suggestion):
I'm not sure if this is correct, I think the `clear_interruptible` is no longer called?

I think the `finally` clause might not run for the generator until the `next()` call.

Code after:
        msg = _include_paths(msg, workspace)
        yield msg
        log = log.append(msg)

    # generate response and run tools
    try:
        set_interruptible()

        # performs reduction/context trimming, if necessary
        msgs = prepare_messages(log.messages, workspace)

        tools = None
        if tool_format == "tool":
            tools = [t for t in get_tools() if t.is_runnable()]

        # generate response
        msg_response = reply(msgs, get_model().model, stream, tools)
        if os.environ.get("GPTME_COSTS") in ["1", "true"]:
            log_costs(msgs + [msg_response])

        # log response and run tools
        if msg_response:
            yield msg_response.replace(quiet=True)
            yield from execute_msg(msg_response, confirm)
    finally:
        clear_interruptible()


def prompt_user(value=None) -> str:  # pragma: no cover
    print_bell()
    # Flush stdin to clear any buffered input before prompting
    termios.tcflush(sys.stdin, termios.TCIFLUSH)
    response = ""
    while not response:
        try:
            set_interruptible()
            response = prompt_input(PROMPT_USER, value)
            if response:
                add_history(response)
        except KeyboardInterrupt:
            print("\nInterrupted. Press Ctrl-D to exit.")
        except EOFError:
            print("\nGoodbye!")
            sys.exit(0)
    clear_interruptible()
    return response


def prompt_input(prompt: str, value=None) -> str:  # pragma: no cover
    """Get input using prompt_toolkit with fish-style suggestions."""
    prompt = prompt.strip() + ": "