|
| 1 | +/** |
| 2 | + * Seer Trial Prompt |
| 3 | + * |
| 4 | + * Interactive flow to check for and start a Seer product trial |
| 5 | + * when a Seer command fails due to budget/enablement errors. |
| 6 | + * |
| 7 | + * Called from bin.ts when a SeerError is caught. Checks trial availability |
| 8 | + * via the customer API, prompts the user for confirmation, and starts the |
| 9 | + * trial if accepted. All failures degrade gracefully — the original error |
| 10 | + * is re-thrown by the caller if this function returns false. |
| 11 | + */ |
| 12 | + |
| 13 | +import { isatty } from "node:tty"; |
| 14 | + |
| 15 | +import { getSeerTrialStatus, startSeerTrial } from "./api-client.js"; |
| 16 | +import type { SeerError, SeerErrorReason } from "./errors.js"; |
| 17 | +import { success } from "./formatters/colors.js"; |
| 18 | +import { logger } from "./logger.js"; |
| 19 | + |
| 20 | +/** Seer error reasons eligible for trial prompt */ |
| 21 | +const TRIAL_ELIGIBLE_REASONS: ReadonlySet<SeerErrorReason> = new Set([ |
| 22 | + "no_budget", |
| 23 | + "not_enabled", |
| 24 | +]); |
| 25 | + |
| 26 | +/** User-facing context messages shown before the trial prompt */ |
| 27 | +const REASON_CONTEXT: Record<string, string> = { |
| 28 | + no_budget: "Your organization has run out of Seer quota.", |
| 29 | + not_enabled: "Seer is not enabled for your organization.", |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * Check whether a SeerError is eligible for a trial prompt. |
| 34 | + * |
| 35 | + * Only `no_budget` and `not_enabled` are eligible — `ai_disabled` is |
| 36 | + * an explicit admin decision that a trial wouldn't override. |
| 37 | + * Requires orgSlug (needed for API calls) and interactive terminal. |
| 38 | + * |
| 39 | + * @param error - The SeerError to check |
| 40 | + * @returns true if the error is eligible for a trial prompt |
| 41 | + */ |
| 42 | +export function isTrialEligible(error: SeerError): boolean { |
| 43 | + return ( |
| 44 | + TRIAL_ELIGIBLE_REASONS.has(error.reason) && |
| 45 | + error.orgSlug !== undefined && |
| 46 | + isatty(0) |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Attempt to offer and start a Seer trial. |
| 52 | + * |
| 53 | + * Flow: |
| 54 | + * 1. Check trial availability via API (graceful failure → return false) |
| 55 | + * 2. Show context message + prompt user for confirmation |
| 56 | + * 3. Start the trial via API |
| 57 | + * |
| 58 | + * @param orgSlug - Organization slug |
| 59 | + * @param reason - The SeerError reason (for context message) |
| 60 | + * @param stderr - Stderr stream for messages |
| 61 | + * @returns true if trial was started successfully, false otherwise |
| 62 | + */ |
| 63 | +export async function promptAndStartTrial( |
| 64 | + orgSlug: string, |
| 65 | + reason: SeerErrorReason, |
| 66 | + stderr: NodeJS.WriteStream |
| 67 | +): Promise<boolean> { |
| 68 | + // 1. Check trial availability (graceful failure → return false) |
| 69 | + let trial: Awaited<ReturnType<typeof getSeerTrialStatus>>; |
| 70 | + try { |
| 71 | + trial = await getSeerTrialStatus(orgSlug); |
| 72 | + } catch { |
| 73 | + // Can't check trial status — degrade gracefully |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + if (!trial) { |
| 78 | + // No trial available — fall through to normal error |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + // 2. Show context and prompt |
| 83 | + const context = REASON_CONTEXT[reason] ?? ""; |
| 84 | + if (context) { |
| 85 | + stderr.write(`${context}\n`); |
| 86 | + } |
| 87 | + |
| 88 | + const daysText = trial.lengthDays ? `${trial.lengthDays}-day ` : ""; |
| 89 | + const log = logger.withTag("seer"); |
| 90 | + const confirmed = await log.prompt( |
| 91 | + `A free ${daysText}Seer trial is available. Start trial?`, |
| 92 | + { type: "confirm", initial: true } |
| 93 | + ); |
| 94 | + |
| 95 | + // Symbol(clack:cancel) is truthy — strict equality check |
| 96 | + if (confirmed !== true) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + // 3. Start trial using the category from the available trial |
| 101 | + try { |
| 102 | + stderr.write("\nStarting Seer trial...\n"); |
| 103 | + await startSeerTrial(orgSlug, trial.category); |
| 104 | + stderr.write(`${success("✓")} Seer trial activated!\n`); |
| 105 | + return true; |
| 106 | + } catch { |
| 107 | + stderr.write( |
| 108 | + "Failed to start trial. Please try again or visit your Sentry settings.\n" |
| 109 | + ); |
| 110 | + return false; |
| 111 | + } |
| 112 | +} |
0 commit comments