Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

3.5 KB
Raw Blame
export type UsageSummary = {
  monthlyPercent: number;
  periodEnd: string;
  unlimited: boolean;
  creditBalanceCents: number;
  creditScope: 'personal' | 'team';
  billingAccess: 'free' | 'payer' | 'team-member';
  existingUsageBilling?: { limitCents: number; suspended: boolean };
  autoReload: {
    settings: AutoReloadSettings;
    suspended: boolean;
    budget?: AutoReloadBudget;
  };
  /** The monthly credit limit on one-time purchases and what is left of it;
   * absent when there is no limit. A team member always has one. */
  purchaseLimit?: { limitCents: number; remainingCents: number };
};

export type UsagePreviewPlan = 'free' | 'paid';

export type AutoReloadSettings = {
  enabled: boolean;
  minimumBalanceCents: number;
  targetBalanceCents: number;
  monthlySpendLimitCents: number | null;
};

export const DEFAULT_AUTO_RELOAD: AutoReloadSettings = {
  enabled: false,
  minimumBalanceCents: 500,
  targetBalanceCents: 2_500,
  monthlySpendLimitCents: null,
};

/** The smallest reload the backend will charge, so target must exceed minimum by this much. */
export const MIN_RELOAD_CENTS = 50;

export const MAX_TARGET_BALANCE_CENTS = 500_000;

/**
 * The monthly spend limit also caps usage billing, which the backend clamps to
 * at least this much (`OVERAGE_LIMIT_MIN_CENTS`), so a smaller limit would be
 * silently raised.
 */
export const MIN_MONTHLY_SPEND_LIMIT_CENTS = 500;

/** Development remains interactive; production follows the UI rollout flag. */
export function isUsageAvailable(
  production: boolean,
  flagEnabled: boolean | undefined
) {
  return !production || flagEnabled === true;
}

export function monthlyUsagePercent(usedCents: number, includedCents: number) {
  if (includedCents <= 0) return usedCents > 0 ? 100 : 0;
  return Math.min(100, Math.max(0, (usedCents / includedCents) * 100));
}

export function formatCreditBalance(cents: number) {
  return new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    minimumFractionDigits: cents % 100 === 0 ? 0 : 2,
  }).format(cents / 100);
}

/** Parse decimal dollars without rounding fractional cents or accepting exponents. */
export function parseDollarInput(value: string): number | undefined {
  const match = /^(\d+)(?:\.(\d{1,2}))?$/.exec(value.trim());
  if (!match) return;
  const cents =
    Number(match[1]) * 100 + Number((match[2] ?? '').padEnd(2, '0'));
  return Number.isSafeInteger(cents) && cents > 0 ? cents : undefined;
}

export function validateAutoReload(
  settings: AutoReloadSettings
): string | undefined {
  if (settings.minimumBalanceCents <= 0)
    return 'Enter a minimum balance greater than $0.';
  if (
    settings.targetBalanceCents <
    settings.minimumBalanceCents + MIN_RELOAD_CENTS
  ) {
    return `Target balance must be greater than minimum balance by at least ${formatCreditBalance(MIN_RELOAD_CENTS)}.`;
  }
  if (settings.targetBalanceCents > MAX_TARGET_BALANCE_CENTS) {
    return `Target balance can be at most ${formatCreditBalance(MAX_TARGET_BALANCE_CENTS)}.`;
  }
  if (
    settings.monthlySpendLimitCents !== null &&
    settings.monthlySpendLimitCents < MIN_MONTHLY_SPEND_LIMIT_CENTS
  ) {
    return `Monthly spend limit must be at least ${formatCreditBalance(MIN_MONTHLY_SPEND_LIMIT_CENTS)}, or leave it blank.`;
  }
}

/** Reload purchases use UTC calendar months, independently of subscription renewal. */
export type AutoReloadBudget = {
  /** Authoritative cap status, including insufficient room for the minimum charge. */
  limitReached?: boolean;
  spentCents: number;
  limitCents: number;
  resetsAt: string;
};