Skip to content

Open source CapCut alternative.

TypeScript
8.3 KB
Raw Blame
model UserCreditAccount {
  id                     String                  @id @default(cuid())
  userId                 String                  @unique
  user                   User                    @relation(fields: [userId], references: [id], onDelete: Cascade)
  balanceMicros          BigInt                  @default(0)
  lifetimeGrantedMicros  BigInt                  @default(0)
  lifetimeChargedMicros  BigInt                  @default(0)
  createdAt              DateTime                @default(now())
  updatedAt              DateTime                @updatedAt
  grants                 UserCreditGrant[]
  ledgerEntries          UserCreditLedgerEntry[]
  inferenceUsageEvents   InferenceUsageEvent[]

  @@map("user_credit_account")
}

model UserCreditGrant {
  id                    String                  @id @default(cuid())
  accountId             String
  account               UserCreditAccount       @relation(fields: [accountId], references: [id], onDelete: Cascade)
  userId                String
  user                  User                    @relation(fields: [userId], references: [id], onDelete: Cascade)
  source                String
  sourceId              String?
  originalAmountMicros  BigInt
  remainingAmountMicros BigInt
  expiresAt             DateTime?
  periodStart           DateTime?
  periodEnd             DateTime?
  status                String                  @default("active")
  description           String?
  metadata              Json?
  createdAt             DateTime                @default(now())
  updatedAt             DateTime                @updatedAt
  ledgerEntries         UserCreditLedgerEntry[]
  offer                 CreditOffer?

  @@unique([userId, source, sourceId])
  @@index([accountId])
  @@index([userId, status])
  @@map("user_credit_grant")
}

// A credit offer: credit promised to an account, landing as a grant when the
// account claims it. Kinds differ in what makes the offer and what claims it:
// a manual one is claimed from the email it was sent in, a promotion's by the
// act it rewards, within its window. The row carries what was promised, so a
// later change to a setting leaves a shown offer as shown, and every kind
// shares the one shape: a new promotion is a setting and code.
model CreditOffer {
  id               String           @id @default(cuid())
  userId           String
  user             User             @relation(fields: [userId], references: [id], onDelete: Cascade)
  // What made the offer: "manual" by hand, or the promotion's key.
  kind             String           @default("manual")
  amountMicros     BigInt
  // The credit's life once claimed: a fixed moment, or days from the claim;
  // neither keeps it forever.
  expiresAt        DateTime?
  expiresAfterDays Int?
  // The last moment the offer can be claimed; null keeps it open.
  closesAt         DateTime?
  description      String?
  // Whatever else the kind promised, in the kind's own shape.
  terms            Json?
  offeredByUserId  String?
  emailSentAt      DateTime?
  claimedAt        DateTime?
  grantId          String?          @unique
  grant            UserCreditGrant? @relation(fields: [grantId], references: [id], onDelete: SetNull)
  createdAt        DateTime         @default(now())

  @@index([userId, kind])
  @@map("credit_offer")
}

model UserCreditLedgerEntry {
  id                 String               @id @default(cuid())
  accountId          String
  account            UserCreditAccount    @relation(fields: [accountId], references: [id], onDelete: Cascade)
  userId             String
  user               User                 @relation(fields: [userId], references: [id], onDelete: Cascade)
  grantId            String?
  grant              UserCreditGrant?     @relation(fields: [grantId], references: [id], onDelete: SetNull)
  usageEventId       String?
  usageEvent         InferenceUsageEvent? @relation(fields: [usageEventId], references: [id], onDelete: SetNull)
  type               String
  amountMicros       BigInt
  balanceAfterMicros BigInt
  source             String?
  sourceId           String?
  description        String?
  metadata           Json?
  createdAt          DateTime             @default(now())

  @@index([accountId, createdAt])
  @@index([userId, createdAt])
  @@index([grantId])
  @@index([usageEventId])
  @@map("user_credit_ledger_entry")
}

model InferenceUsageEvent {
  id                    String                  @id @default(cuid())
  accountId             String
  account               UserCreditAccount       @relation(fields: [accountId], references: [id], onDelete: Cascade)
  userId                String
  user                  User                    @relation(fields: [userId], references: [id], onDelete: Cascade)
  clientId              String?
  conversationId        String?
  route                 String
  requestKind           String
  provider              String
  model                 String
  status                String
  billingStatus         String
  providerUsage         Json?
  normalizedUsage       Json?
  creditCostMicros      BigInt                  @default(0)
  rateId                String?
  rateVersion           Int?
  errorCode             String?
  metadata              Json?
  createdAt             DateTime                @default(now())
  ledgerEntries         UserCreditLedgerEntry[]

  @@index([accountId, createdAt])
  @@index([userId, createdAt])
  @@index([route, createdAt])
  @@index([provider, model, createdAt])
  @@map("inference_usage_event")
}

model InferenceCreditRate {
  id                    String   @id @default(cuid())
  route                 String?
  provider              String?
  model                 String?
  version               Int      @default(1)
  active                Boolean  @default(true)
  baseCostMicros        BigInt   @default(0)
  inputTokenCostMicros  BigInt   @default(0)
  outputTokenCostMicros BigInt   @default(0)
  totalTokenCostMicros  BigInt   @default(0)
  characterCostMicros   BigInt   @default(0)
  fallbackCostMicros    BigInt   @default(1000000)
  validFrom             DateTime @default(now())
  validUntil            DateTime?
  description           String?
  metadata              Json?
  createdAt             DateTime @default(now())
  updatedAt             DateTime @updatedAt

  @@index([active, route, provider, model])
  @@index([validFrom, validUntil])
  @@map("inference_credit_rate")
}

// Per-user auto-reload config. When enabled, the credit balance dropping below
// thresholdMicros triggers an off-session Stripe charge of amountMicros against
// the saved payment method; the resulting credits are granted from the webhook.
// status guards against concurrent charges ("idle" | "charging" | "failed").
model CreditAutoReload {
  id                    String    @id @default(cuid())
  userId                String    @unique
  user                  User      @relation(fields: [userId], references: [id], onDelete: Cascade)
  enabled               Boolean   @default(false)
  thresholdMicros       BigInt    @default(0)
  amountMicros          BigInt    @default(0)
  stripeCustomerId      String?
  stripePaymentMethodId String?
  status                String    @default("idle")
  // The off-session PaymentIntent that currently owns the "charging" lock, so a
  // success/failure webhook only releases the charge it actually belongs to.
  chargingPaymentIntentId String?
  lastChargeAt          DateTime?
  lastError             String?
  createdAt             DateTime  @default(now())
  updatedAt             DateTime  @updatedAt

  @@map("credit_auto_reload")
}

model UserCreditLimit {
  id                         String   @id @default(cuid())
  userId                     String
  user                       User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  scope                      String   @default("global")
  route                      String?
  provider                   String?
  model                      String?
  maxCreditsPerPeriodMicros  BigInt?
  periodStart                DateTime?
  periodEnd                  DateTime?
  active                     Boolean  @default(true)
  description                String?
  metadata                   Json?
  createdAt                  DateTime @default(now())
  updatedAt                  DateTime @updatedAt

  @@index([userId, active])
  @@index([route, provider, model])
  @@map("user_credit_limit")
}