summaryrefslogtreecommitdiffhomepage
path: root/packages/console/core/src/black.ts
diff options
context:
space:
mode:
authorFrank <[email protected]>2026-02-22 18:41:34 -0500
committerFrank <[email protected]>2026-02-22 22:19:44 -0500
commit5712cff5c453a185ac75a160f76ca06135d6ab2d (patch)
tree6838fedd40aed31421265fdc8b9fe5826d396f86 /packages/console/core/src/black.ts
parentee754c46f992dd4024e56e93246421246d16d13f (diff)
downloadopencode-5712cff5c453a185ac75a160f76ca06135d6ab2d.tar.gz
opencode-5712cff5c453a185ac75a160f76ca06135d6ab2d.zip
zen: track session in usage
Diffstat (limited to 'packages/console/core/src/black.ts')
-rw-r--r--packages/console/core/src/black.ts74
1 files changed, 0 insertions, 74 deletions
diff --git a/packages/console/core/src/black.ts b/packages/console/core/src/black.ts
index 5f8db6273..b4cc27064 100644
--- a/packages/console/core/src/black.ts
+++ b/packages/console/core/src/black.ts
@@ -1,8 +1,6 @@
import { z } from "zod"
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 {
@@ -60,75 +58,3 @@ export namespace BlackData {
},
)
}
-
-export namespace Black {
- export const analyzeRollingUsage = fn(
- z.object({
- plan: z.enum(SubscriptionPlan),
- usage: z.number().int(),
- timeUpdated: z.date(),
- }),
- ({ plan, usage, timeUpdated }) => {
- const now = new Date()
- const black = BlackData.getLimits({ plan })
- const rollingWindowMs = black.rollingWindow * 3600 * 1000
- const rollingLimitInMicroCents = centsToMicroCents(black.rollingLimit * 100)
- const windowStart = new Date(now.getTime() - rollingWindowMs)
- if (timeUpdated < windowStart) {
- return {
- status: "ok" as const,
- resetInSec: black.rollingWindow * 3600,
- usagePercent: 0,
- }
- }
-
- const windowEnd = new Date(timeUpdated.getTime() + rollingWindowMs)
- if (usage < rollingLimitInMicroCents) {
- return {
- status: "ok" as const,
- resetInSec: Math.ceil((windowEnd.getTime() - now.getTime()) / 1000),
- usagePercent: Math.ceil(Math.min(100, (usage / rollingLimitInMicroCents) * 100)),
- }
- }
- return {
- status: "rate-limited" as const,
- resetInSec: Math.ceil((windowEnd.getTime() - now.getTime()) / 1000),
- usagePercent: 100,
- }
- },
- )
-
- export const analyzeWeeklyUsage = fn(
- z.object({
- plan: z.enum(SubscriptionPlan),
- usage: z.number().int(),
- timeUpdated: z.date(),
- }),
- ({ plan, usage, timeUpdated }) => {
- const black = BlackData.getLimits({ plan })
- const now = new Date()
- const week = getWeekBounds(now)
- const fixedLimitInMicroCents = centsToMicroCents(black.fixedLimit * 100)
- if (timeUpdated < week.start) {
- return {
- status: "ok" as const,
- resetInSec: Math.ceil((week.end.getTime() - now.getTime()) / 1000),
- usagePercent: 0,
- }
- }
- if (usage < fixedLimitInMicroCents) {
- return {
- status: "ok" as const,
- resetInSec: Math.ceil((week.end.getTime() - now.getTime()) / 1000),
- usagePercent: Math.ceil(Math.min(100, (usage / fixedLimitInMicroCents) * 100)),
- }
- }
-
- return {
- status: "rate-limited" as const,
- resetInSec: Math.ceil((week.end.getTime() - now.getTime()) / 1000),
- usagePercent: 100,
- }
- },
- )
-}