black.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { z } from "zod"
  2. import { fn } from "./util/fn"
  3. import { Resource } from "@opencode-ai/console-resource"
  4. import { centsToMicroCents } from "./util/price"
  5. import { getWeekBounds } from "./util/date"
  6. import { SubscriptionPlan } from "./schema/billing.sql"
  7. export namespace BlackData {
  8. const Schema = z.object({
  9. "200": z.object({
  10. fixedLimit: z.number().int(),
  11. rollingLimit: z.number().int(),
  12. rollingWindow: z.number().int(),
  13. }),
  14. "100": z.object({
  15. fixedLimit: z.number().int(),
  16. rollingLimit: z.number().int(),
  17. rollingWindow: z.number().int(),
  18. }),
  19. "20": z.object({
  20. fixedLimit: z.number().int(),
  21. rollingLimit: z.number().int(),
  22. rollingWindow: z.number().int(),
  23. }),
  24. })
  25. export const validate = fn(Schema, (input) => {
  26. return input
  27. })
  28. export const getLimits = fn(
  29. z.object({
  30. plan: z.enum(SubscriptionPlan),
  31. }),
  32. ({ plan }) => {
  33. const json = JSON.parse(Resource.ZEN_BLACK_LIMITS.value)
  34. return Schema.parse(json)[plan]
  35. },
  36. )
  37. export const planToPriceID = fn(
  38. z.object({
  39. plan: z.enum(SubscriptionPlan),
  40. }),
  41. ({ plan }) => {
  42. if (plan === "200") return Resource.ZEN_BLACK_PRICE.plan200
  43. if (plan === "100") return Resource.ZEN_BLACK_PRICE.plan100
  44. return Resource.ZEN_BLACK_PRICE.plan20
  45. },
  46. )
  47. export const priceIDToPlan = fn(
  48. z.object({
  49. priceID: z.string(),
  50. }),
  51. ({ priceID }) => {
  52. if (priceID === Resource.ZEN_BLACK_PRICE.plan200) return "200"
  53. if (priceID === Resource.ZEN_BLACK_PRICE.plan100) return "100"
  54. return "20"
  55. },
  56. )
  57. }
  58. export namespace Black {
  59. export const analyzeRollingUsage = fn(
  60. z.object({
  61. plan: z.enum(SubscriptionPlan),
  62. usage: z.number().int(),
  63. timeUpdated: z.date(),
  64. }),
  65. ({ plan, usage, timeUpdated }) => {
  66. const now = new Date()
  67. const black = BlackData.getLimits({ plan })
  68. const rollingWindowMs = black.rollingWindow * 3600 * 1000
  69. const rollingLimitInMicroCents = centsToMicroCents(black.rollingLimit * 100)
  70. const windowStart = new Date(now.getTime() - rollingWindowMs)
  71. if (timeUpdated < windowStart) {
  72. return {
  73. status: "ok" as const,
  74. resetInSec: black.rollingWindow * 3600,
  75. usagePercent: 0,
  76. }
  77. }
  78. const windowEnd = new Date(timeUpdated.getTime() + rollingWindowMs)
  79. if (usage < rollingLimitInMicroCents) {
  80. return {
  81. status: "ok" as const,
  82. resetInSec: Math.ceil((windowEnd.getTime() - now.getTime()) / 1000),
  83. usagePercent: Math.ceil(Math.min(100, (usage / rollingLimitInMicroCents) * 100)),
  84. }
  85. }
  86. return {
  87. status: "rate-limited" as const,
  88. resetInSec: Math.ceil((windowEnd.getTime() - now.getTime()) / 1000),
  89. usagePercent: 100,
  90. }
  91. },
  92. )
  93. export const analyzeWeeklyUsage = fn(
  94. z.object({
  95. plan: z.enum(SubscriptionPlan),
  96. usage: z.number().int(),
  97. timeUpdated: z.date(),
  98. }),
  99. ({ plan, usage, timeUpdated }) => {
  100. const black = BlackData.getLimits({ plan })
  101. const now = new Date()
  102. const week = getWeekBounds(now)
  103. const fixedLimitInMicroCents = centsToMicroCents(black.fixedLimit * 100)
  104. if (timeUpdated < week.start) {
  105. return {
  106. status: "ok" as const,
  107. resetInSec: Math.ceil((week.end.getTime() - now.getTime()) / 1000),
  108. usagePercent: 0,
  109. }
  110. }
  111. if (usage < fixedLimitInMicroCents) {
  112. return {
  113. status: "ok" as const,
  114. resetInSec: Math.ceil((week.end.getTime() - now.getTime()) / 1000),
  115. usagePercent: Math.ceil(Math.min(100, (usage / fixedLimitInMicroCents) * 100)),
  116. }
  117. }
  118. return {
  119. status: "rate-limited" as const,
  120. resetInSec: Math.ceil((week.end.getTime() - now.getTime()) / 1000),
  121. usagePercent: 100,
  122. }
  123. },
  124. )
  125. }