diff options
| author | Frank <[email protected]> | 2026-01-22 12:40:42 -0500 |
|---|---|---|
| committer | Frank <[email protected]> | 2026-01-22 13:02:28 -0500 |
| commit | a890d51bbc6a5608ad5992c74ee49153775aceb3 (patch) | |
| tree | 5b4e4b53cfc44db4242b23d3bce48bba4a303a67 /packages/console/core/src/black.ts | |
| parent | bb582416f216de684bf6861353a3111cde2c461d (diff) | |
| download | opencode-a890d51bbc6a5608ad5992c74ee49153775aceb3.tar.gz opencode-a890d51bbc6a5608ad5992c74ee49153775aceb3.zip | |
wip: zen black
Diffstat (limited to 'packages/console/core/src/black.ts')
| -rw-r--r-- | packages/console/core/src/black.ts | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/packages/console/core/src/black.ts b/packages/console/core/src/black.ts index 753d25808..85e16f1a1 100644 --- a/packages/console/core/src/black.ts +++ b/packages/console/core/src/black.ts @@ -3,33 +3,49 @@ import { fn } from "./util/fn" import { Resource } from "@opencode-ai/console-resource" import { centsToMicroCents } from "./util/price" import { getWeekBounds } from "./util/date" +import { SubscriptionPlan } from "./schema/billing.sql" export namespace BlackData { const Schema = z.object({ - fixedLimit: z.number().int(), - rollingLimit: z.number().int(), - rollingWindow: z.number().int(), + "200": z.object({ + fixedLimit: z.number().int(), + rollingLimit: z.number().int(), + rollingWindow: z.number().int(), + }), + "100": z.object({ + fixedLimit: z.number().int(), + rollingLimit: z.number().int(), + rollingWindow: z.number().int(), + }), + "20": z.object({ + fixedLimit: z.number().int(), + rollingLimit: z.number().int(), + rollingWindow: z.number().int(), + }), }) export const validate = fn(Schema, (input) => { return input }) - export const get = fn(z.void(), () => { - const json = JSON.parse(Resource.ZEN_BLACK.value) - return Schema.parse(json) + export const get = fn(z.object({ + plan: z.enum(SubscriptionPlan), + }), ({ plan }) => { + const json = JSON.parse(Resource.ZEN_BLACK_LIMITS.value) + return Schema.parse(json)[plan] }) } export namespace Black { export const analyzeRollingUsage = fn( z.object({ + plan: z.enum(SubscriptionPlan), usage: z.number().int(), timeUpdated: z.date(), }), - ({ usage, timeUpdated }) => { + ({ plan, usage, timeUpdated }) => { const now = new Date() - const black = BlackData.get() + const black = BlackData.get({ plan }) const rollingWindowMs = black.rollingWindow * 3600 * 1000 const rollingLimitInMicroCents = centsToMicroCents(black.rollingLimit * 100) const windowStart = new Date(now.getTime() - rollingWindowMs) @@ -59,11 +75,12 @@ export namespace Black { export const analyzeWeeklyUsage = fn( z.object({ + plan:z.enum(SubscriptionPlan), usage: z.number().int(), timeUpdated: z.date(), }), - ({ usage, timeUpdated }) => { - const black = BlackData.get() + ({ plan, usage, timeUpdated }) => { + const black = BlackData.get({ plan }) const now = new Date() const week = getWeekBounds(now) const fixedLimitInMicroCents = centsToMicroCents(black.fixedLimit * 100) |
