Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

3.9 KB
Raw Blame
import { z } from "zod";
import { CloudConfigRateLimit } from "./rate-limits";
import { chbPlanCodes, cloudConfigPlans } from "../features/entitlements/plans";

export const CloudConfigSchema = z.object({
  plan: z.enum(cloudConfigPlans).optional(),
  monthlyObservationLimit: z.number().int().positive().optional(),
  // used for table and dashboard queries
  defaultLookBackDays: z.number().int().positive().optional(),
  // need to update stripe webhook if you change this, it fetches from db via these fields
  stripe: z
    .object({
      customerId: z.string().nullish(),
      activeSubscriptionId: z.string().nullish(),
      activeProductId: z.string().nullish(),
      activeUsageProductId: z.string().nullish(),
      subscriptionStatus: z.string().nullish(), // should be one of ["active","past_due", "unpaid", "canceled", "incomplete", "incomplete_expired", "paused"]; we don't enforce to have a backwards compatibility for this field
    })
    .transform((data) => ({
      ...data,
      isLegacySubscription:
        data?.activeProductId != null && data?.activeUsageProductId == null,
    }))
    .nullish(),

  // ClickHouse Billing (CHB) state. Written only by the CHB webhook handler,
  // except organizationId which checkout-session creation persists. Invariant:
  // an org carries either `stripe` billing state or `clickhouse` state, never
  // both — provider routing (getBillingProvider) and the worker jobs'
  // Stripe-customer-id selection depend on it.
  clickhouse: z
    .object({
      // ClickHouse Organization ID. Required and uuid-validated: parseDbOrg
      // nulls the whole cloudConfig on parse failure, so strictness here is
      // only safe because both writers (checkout response, webhook handler)
      // validate the uuid before persisting. Change the stored schema first,
      // writers second, if the id format ever loosens.
      organizationId: z.uuid(),
      // CHB attached plan id (`data.id` on webhook events, `id` on GET /attachedplan)
      attachedPlanId: z.string().nullish(),
      // Validated against the shared plan-code enum so downstream resolution is
      // an exhaustive lookup. `.catch(null)` contains the damage if a code ever
      // drifts (CHB renames a tier, or ships one before we deploy support for
      // it): parseDbOrg nulls the *entire* cloudConfig on a parse failure, which
      // would also discard the org's plan override, rate-limit overrides and
      // stripe.customerId — and an org without a Stripe customer id drops out of
      // the usage-metering job's selection and silently stops being billed.
      // Unknown codes are rejected at the webhook so they are never stored.
      planCode: z.enum(chbPlanCodes).nullish().catch(null),
      paymentStatus: z.string().nullish(), // "active" | "failed" | ...
      nextPaymentDate: z.string().nullish(),
      // Stripe customer behind the CHB bundle (payment.provider.customerId on
      // attachedplan.* events). Support tooling only — routing, plan resolution, and
      // the worker jobs never read it.
      stripeCustomerId: z.string().nullish(),
      // Snapshot of a pending scheduled change (upgrade/downgrade/cancel), as
      // GET /attachedplan reports it.
      scheduled: z
        .object({
          type: z.string(), // "upgrade" | "downgrade" | "cancel"
          // "immediate" | "billing_cycle_end" | ISO date, when CHB supplies one
          when: z.string().nullish(),
          planCode: z.string().nullish(), // upgrade / downgrade target
          startDate: z.string().nullish(), // upgrade / downgrade effective date
          endDate: z.string().nullish(), // cancel: when the plan ends
        })
        .nullish(),
      // Monotonic guard against out-of-order webhook delivery.
      lastEventCreatedAt: z.string().nullish(),
    })
    .nullish(),

  // custom rate limits for an organization
  rateLimitOverrides: CloudConfigRateLimit.optional(),
});

export type CloudConfigSchema = z.infer<typeof CloudConfigSchema>;