|
| 1 | +"""Cloud promo messaging for CLI entrypoint.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +from collections.abc import Callable |
| 6 | + |
| 7 | +import typer |
| 8 | + |
| 9 | +from basic_memory.config import ConfigManager |
| 10 | + |
| 11 | +CLOUD_PROMO_VERSION = "2026-02-06" |
| 12 | +OSS_DISCOUNT_CODE = "{{OSS_DISCOUNT_CODE}}" |
| 13 | + |
| 14 | + |
| 15 | +def _promos_disabled_by_env() -> bool: |
| 16 | + """Check environment-level kill switch for promo output.""" |
| 17 | + value = os.getenv("BASIC_MEMORY_NO_PROMOS", "").strip().lower() |
| 18 | + return value in {"1", "true", "yes"} |
| 19 | + |
| 20 | + |
| 21 | +def _is_interactive_session() -> bool: |
| 22 | + """Return whether stdin/stdout are interactive terminals.""" |
| 23 | + return sys.stdin.isatty() and sys.stdout.isatty() |
| 24 | + |
| 25 | + |
| 26 | +def _build_first_run_message() -> str: |
| 27 | + """Build first-run cloud promo copy.""" |
| 28 | + return ( |
| 29 | + "Basic Memory initialized (local mode).\n" |
| 30 | + "Cloud is optional and keeps your workflow local-first.\n" |
| 31 | + "Cloud adds cross-device sync + mobile/web access.\n" |
| 32 | + f"OSS discount: {OSS_DISCOUNT_CODE} (20% off for 3 months).\n" |
| 33 | + "Run `bm cloud login` to enable." |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def _build_version_message() -> str: |
| 38 | + """Build cloud promo copy shown after promo-version bumps.""" |
| 39 | + return ( |
| 40 | + "New in Basic Memory Cloud: cross-device sync + mobile/web access.\n" |
| 41 | + f"OSS discount: {OSS_DISCOUNT_CODE} (20% off for 3 months).\n" |
| 42 | + "Run `bm cloud login` to enable." |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def maybe_show_cloud_promo( |
| 47 | + invoked_subcommand: str | None, |
| 48 | + *, |
| 49 | + config_manager: ConfigManager | None = None, |
| 50 | + is_interactive: bool | None = None, |
| 51 | + echo: Callable[[str], None] = typer.echo, |
| 52 | +) -> None: |
| 53 | + """Show cloud promo copy when discovery gates are satisfied.""" |
| 54 | + manager = config_manager or ConfigManager() |
| 55 | + config = manager.load_config() |
| 56 | + |
| 57 | + interactive = _is_interactive_session() if is_interactive is None else is_interactive |
| 58 | + |
| 59 | + # Trigger: environment-level promo suppression or non-interactive execution. |
| 60 | + # Why: avoid polluting scripts/CI output and support a hard opt-out. |
| 61 | + # Outcome: skip all promo copy for this invocation. |
| 62 | + if _promos_disabled_by_env() or not interactive: |
| 63 | + return |
| 64 | + |
| 65 | + # Trigger: command context where cloud promo is not actionable. |
| 66 | + # Why: mcp/stdin protocol and root help flows should stay noise-free. |
| 67 | + # Outcome: command continues without promo messaging. |
| 68 | + if invoked_subcommand in {None, "mcp"}: |
| 69 | + return |
| 70 | + |
| 71 | + if config.cloud_mode_enabled or config.cloud_promo_opt_out: |
| 72 | + return |
| 73 | + |
| 74 | + show_first_run = not config.cloud_promo_first_run_shown |
| 75 | + show_version_notice = config.cloud_promo_last_version_shown != CLOUD_PROMO_VERSION |
| 76 | + if not show_first_run and not show_version_notice: |
| 77 | + return |
| 78 | + |
| 79 | + message = _build_first_run_message() if show_first_run else _build_version_message() |
| 80 | + echo(message) |
| 81 | + |
| 82 | + config.cloud_promo_first_run_shown = True |
| 83 | + config.cloud_promo_last_version_shown = CLOUD_PROMO_VERSION |
| 84 | + manager.save_config(config) |
0 commit comments