|
| 1 | +"""Pro-version offer shown at the start of an interactive ``ifixai run``. |
| 2 | +
|
| 3 | +The offer is made once per machine, before telemetry and before any provider |
| 4 | +call, so a run the user abandons for the website leaves no trace beyond the |
| 5 | +``pro-offer-shown`` marker. It is never shown when stdin or stdout is not a |
| 6 | +terminal, or in CI, so scripted and agent-driven runs cannot block on it. Skip |
| 7 | +it with ``--no-promo`` or ``IFIXAI_NO_PROMO=1``. |
| 8 | +""" |
| 9 | + |
| 10 | +import sys |
| 11 | +import webbrowser |
| 12 | +from enum import Enum |
| 13 | +from typing import TextIO, TypedDict |
| 14 | + |
| 15 | +import click |
| 16 | + |
| 17 | +from ifixai import telemetry |
| 18 | +from ifixai.cli import ui |
| 19 | + |
| 20 | +PRO_URL = "https://www.ifixai.ai/" |
| 21 | +NO_PROMO_ENV_VAR = "IFIXAI_NO_PROMO" |
| 22 | +OFFER_MARKER_NAME = "pro-offer-shown" |
| 23 | + |
| 24 | +YES_OPTION = "Yes" |
| 25 | +NO_OPTION = "No" |
| 26 | +CONTINUE_OPTION = "Continue the current run" |
| 27 | +ABANDON_OPTION = "Abandon the run" |
| 28 | + |
| 29 | + |
| 30 | +class RunDecision(str, Enum): |
| 31 | + """What the user wants done with the current run after seeing the offer.""" |
| 32 | + |
| 33 | + CONTINUE = "continue" |
| 34 | + ABANDON = "abandon" |
| 35 | + |
| 36 | + |
| 37 | +class ChoiceQuestion(TypedDict): |
| 38 | + """One question with a fixed set of answers; Enter alone picks ``default``.""" |
| 39 | + |
| 40 | + message: str |
| 41 | + options: tuple[str, ...] |
| 42 | + default: str |
| 43 | + |
| 44 | + |
| 45 | +PRO_INTEREST_QUESTION = ChoiceQuestion( |
| 46 | + message="Claim your free fast audit now?", |
| 47 | + options=(YES_OPTION, NO_OPTION), |
| 48 | + default=NO_OPTION, |
| 49 | +) |
| 50 | +RUN_DECISION_QUESTION = ChoiceQuestion( |
| 51 | + message="What would you like to do with this run?", |
| 52 | + options=(CONTINUE_OPTION, ABANDON_OPTION), |
| 53 | + default=CONTINUE_OPTION, |
| 54 | +) |
| 55 | + |
| 56 | + |
| 57 | +def offer_pro_version() -> RunDecision: |
| 58 | + """Offer the Pro version and report whether the current run should go ahead. |
| 59 | +
|
| 60 | + Opens the Pro website when the user accepts. Returns ``CONTINUE`` without |
| 61 | + asking anything when nobody is at a terminal to answer or it was already shown. |
| 62 | + """ |
| 63 | + if not can_prompt_user() or not mark_offer_shown(): |
| 64 | + return RunDecision.CONTINUE |
| 65 | + print_offer() |
| 66 | + if ask_choice(PRO_INTEREST_QUESTION) != YES_OPTION: |
| 67 | + return RunDecision.CONTINUE |
| 68 | + open_pro_site() |
| 69 | + if ask_choice(RUN_DECISION_QUESTION) == ABANDON_OPTION: |
| 70 | + return RunDecision.ABANDON |
| 71 | + return RunDecision.CONTINUE |
| 72 | + |
| 73 | + |
| 74 | +def can_prompt_user() -> bool: |
| 75 | + """True only when a person is at a terminal to answer the offer.""" |
| 76 | + return ( |
| 77 | + is_terminal(sys.stdin) and is_terminal(sys.stdout) and not telemetry.in_ci() |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +def mark_offer_shown() -> bool: |
| 82 | + """Create the shown-once marker; False if it exists or can't be written. |
| 83 | +
|
| 84 | + Marked before asking, so any answer (Ctrl-C included) counts. When it can't |
| 85 | + be written the offer is skipped, so a looping script never stops on it twice. |
| 86 | + """ |
| 87 | + try: |
| 88 | + (telemetry._ensure_config_dir() / OFFER_MARKER_NAME).touch(exist_ok=False) |
| 89 | + except OSError: |
| 90 | + return False |
| 91 | + return True |
| 92 | + |
| 93 | + |
| 94 | +def is_terminal(stream: TextIO | None) -> bool: |
| 95 | + """True when the stream exists and is attached to a terminal. |
| 96 | +
|
| 97 | + Python sets a standard stream to ``None`` when the process has no handle |
| 98 | + for it (a closed descriptor, ``pythonw``, a detached Windows process). |
| 99 | + """ |
| 100 | + return stream is not None and stream.isatty() |
| 101 | + |
| 102 | + |
| 103 | +def print_offer() -> None: |
| 104 | + """Print the Pro-version pitch and its link.""" |
| 105 | + click.echo(click.style(" iFixAi Pro", bold=True)) |
| 106 | + click.echo(" Claim your free fast audit") |
| 107 | + click.echo(click.style(f" {PRO_URL}", fg="cyan")) |
| 108 | + click.echo() |
| 109 | + |
| 110 | + |
| 111 | +def open_pro_site() -> None: |
| 112 | + """Open the Pro website in the default browser; print the link when that fails.""" |
| 113 | + if webbrowser.open(PRO_URL): |
| 114 | + click.echo(click.style(f"Opened {PRO_URL} in your browser.", fg="green")) |
| 115 | + return |
| 116 | + click.echo( |
| 117 | + click.style( |
| 118 | + f"Could not open a browser. Visit {PRO_URL} to claim your free fast audit.", |
| 119 | + fg="yellow", |
| 120 | + ) |
| 121 | + ) |
| 122 | + |
| 123 | + |
| 124 | +def ask_choice(question: ChoiceQuestion) -> str: |
| 125 | + """Ask with an arrow-key menu, or a numbered list where menus are unavailable.""" |
| 126 | + if ui.is_interactive(): |
| 127 | + return ui.select_or_abort( |
| 128 | + question["message"], question["options"], question["default"] |
| 129 | + ) |
| 130 | + return ask_numbered_choice(question) |
| 131 | + |
| 132 | + |
| 133 | +def ask_numbered_choice(question: ChoiceQuestion) -> str: |
| 134 | + """List the options with numbers and read the number the user types. |
| 135 | +
|
| 136 | + The plain path for terminals where the arrow-key menu cannot run: ``NO_COLOR`` |
| 137 | + is set, or ``questionary`` is not installed. |
| 138 | + """ |
| 139 | + options = question["options"] |
| 140 | + for number, option in enumerate(options, start=1): |
| 141 | + click.echo(f" [{number}] {option}") |
| 142 | + typed_number = click.prompt( |
| 143 | + question["message"], |
| 144 | + type=click.IntRange(1, len(options)), |
| 145 | + default=options.index(question["default"]) + 1, |
| 146 | + ) |
| 147 | + return options[typed_number - 1] |
0 commit comments