Skip to content

Commit 5712cff

Browse files
committed
zen: track session in usage
1 parent ee754c4 commit 5712cff

187 files changed

Lines changed: 87618 additions & 47656 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

infra/console.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,46 @@ export const stripeWebhook = new stripe.WebhookEndpoint("StripeWebhookEndpoint",
100100
],
101101
})
102102

103-
const zenProduct = new stripe.Product("ZenBlack", {
103+
const zenLiteProduct = new stripe.Product("ZenLite", {
104+
name: "OpenCode Lite",
105+
})
106+
const zenLitePrice = new stripe.Price("ZenLitePrice", {
107+
product: zenLiteProduct.id,
108+
currency: "usd",
109+
recurring: {
110+
interval: "month",
111+
intervalCount: 1,
112+
},
113+
unitAmount: 1000,
114+
})
115+
const ZEN_LITE_PRICE = new sst.Linkable("ZEN_LITE_PRICE", {
116+
properties: {
117+
product: zenLiteProduct.id,
118+
price: zenLitePrice.id,
119+
},
120+
})
121+
const ZEN_LITE_LIMITS = new sst.Secret("ZEN_LITE_LIMITS")
122+
123+
const zenBlackProduct = new stripe.Product("ZenBlack", {
104124
name: "OpenCode Black",
105125
})
106-
const zenPriceProps = {
107-
product: zenProduct.id,
126+
const zenBlackPriceProps = {
127+
product: zenBlackProduct.id,
108128
currency: "usd",
109129
recurring: {
110130
interval: "month",
111131
intervalCount: 1,
112132
},
113133
}
114-
const zenPrice200 = new stripe.Price("ZenBlackPrice", { ...zenPriceProps, unitAmount: 20000 })
115-
const zenPrice100 = new stripe.Price("ZenBlack100Price", { ...zenPriceProps, unitAmount: 10000 })
116-
const zenPrice20 = new stripe.Price("ZenBlack20Price", { ...zenPriceProps, unitAmount: 2000 })
134+
const zenBlackPrice200 = new stripe.Price("ZenBlackPrice", { ...zenBlackPriceProps, unitAmount: 20000 })
135+
const zenBlackPrice100 = new stripe.Price("ZenBlack100Price", { ...zenBlackPriceProps, unitAmount: 10000 })
136+
const zenBlackPrice20 = new stripe.Price("ZenBlack20Price", { ...zenBlackPriceProps, unitAmount: 2000 })
117137
const ZEN_BLACK_PRICE = new sst.Linkable("ZEN_BLACK_PRICE", {
118138
properties: {
119-
product: zenProduct.id,
120-
plan200: zenPrice200.id,
121-
plan100: zenPrice100.id,
122-
plan20: zenPrice20.id,
139+
product: zenBlackProduct.id,
140+
plan200: zenBlackPrice200.id,
141+
plan100: zenBlackPrice100.id,
142+
plan20: zenBlackPrice20.id,
123143
},
124144
})
125145
const ZEN_BLACK_LIMITS = new sst.Secret("ZEN_BLACK_LIMITS")
@@ -196,6 +216,8 @@ new sst.cloudflare.x.SolidStart("Console", {
196216
AWS_SES_SECRET_ACCESS_KEY,
197217
ZEN_BLACK_PRICE,
198218
ZEN_BLACK_LIMITS,
219+
ZEN_LITE_PRICE,
220+
ZEN_LITE_LIMITS,
199221
new sst.Secret("ZEN_SESSION_SECRET"),
200222
...ZEN_MODELS,
201223
...($dev

packages/console/app/src/routes/workspace/[id]/billing/black-section.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Billing } from "@opencode-ai/console-core/billing.js"
55
import { Database, eq, and, isNull, sql } from "@opencode-ai/console-core/drizzle/index.js"
66
import { BillingTable, SubscriptionTable } from "@opencode-ai/console-core/schema/billing.sql.js"
77
import { Actor } from "@opencode-ai/console-core/actor.js"
8-
import { Black } from "@opencode-ai/console-core/black.js"
8+
import { Subscription } from "@opencode-ai/console-core/black.js"
99
import { withActor } from "~/context/auth.withActor"
1010
import { queryBillingInfo } from "../../common"
1111
import styles from "./black-section.module.css"
@@ -35,12 +35,12 @@ const querySubscription = query(async (workspaceID: string) => {
3535
return {
3636
plan: row.subscription.plan,
3737
useBalance: row.subscription.useBalance ?? false,
38-
rollingUsage: Black.analyzeRollingUsage({
38+
rollingUsage: Subscription.analyzeRollingUsage({
3939
plan: row.subscription.plan,
4040
usage: row.rollingUsage ?? 0,
4141
timeUpdated: row.timeRollingUpdated ?? new Date(),
4242
}),
43-
weeklyUsage: Black.analyzeWeeklyUsage({
43+
weeklyUsage: Subscription.analyzeWeeklyUsage({
4444
plan: row.subscription.plan,
4545
usage: row.fixedUsage ?? 0,
4646
timeUpdated: row.timeFixedUpdated ?? new Date(),

packages/console/app/src/routes/zen/util/handler.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { Billing } from "@opencode-ai/console-core/billing.js"
99
import { Actor } from "@opencode-ai/console-core/actor.js"
1010
import { WorkspaceTable } from "@opencode-ai/console-core/schema/workspace.sql.js"
1111
import { ZenData } from "@opencode-ai/console-core/model.js"
12-
import { Black, BlackData } from "@opencode-ai/console-core/black.js"
12+
import { Subscription } from "@opencode-ai/console-core/subscription.js"
13+
import { BlackData } from "@opencode-ai/console-core/black.js"
1314
import { UserTable } from "@opencode-ai/console-core/schema/user.sql.js"
1415
import { ModelTable } from "@opencode-ai/console-core/schema/model.sql.js"
1516
import { ProviderTable } from "@opencode-ai/console-core/schema/provider.sql.js"
@@ -196,7 +197,7 @@ export async function handler(
196197
const costInfo = calculateCost(modelInfo, usageInfo)
197198
await trialLimiter?.track(usageInfo)
198199
await rateLimiter?.track()
199-
await trackUsage(billingSource, authInfo, modelInfo, providerInfo, usageInfo, costInfo)
200+
await trackUsage(sessionId, billingSource, authInfo, modelInfo, providerInfo, usageInfo, costInfo)
200201
await reload(billingSource, authInfo, costInfo)
201202

202203
const responseConverter = createResponseConverter(providerInfo.format, opts.format)
@@ -246,7 +247,7 @@ export async function handler(
246247
const usageInfo = providerInfo.normalizeUsage(usage)
247248
const costInfo = calculateCost(modelInfo, usageInfo)
248249
await trialLimiter?.track(usageInfo)
249-
await trackUsage(billingSource, authInfo, modelInfo, providerInfo, usageInfo, costInfo)
250+
await trackUsage(sessionId, billingSource, authInfo, modelInfo, providerInfo, usageInfo, costInfo)
250251
await reload(billingSource, authInfo, costInfo)
251252
cost = calculateOccuredCost(billingSource, costInfo)
252253
}
@@ -541,8 +542,9 @@ export async function handler(
541542

542543
// Check weekly limit
543544
if (sub.fixedUsage && sub.timeFixedUpdated) {
544-
const result = Black.analyzeWeeklyUsage({
545-
plan,
545+
const blackData = BlackData.getLimits({ plan })
546+
const result = Subscription.analyzeWeeklyUsage({
547+
limit: blackData.fixedLimit,
546548
usage: sub.fixedUsage,
547549
timeUpdated: sub.timeFixedUpdated,
548550
})
@@ -555,8 +557,10 @@ export async function handler(
555557

556558
// Check rolling limit
557559
if (sub.rollingUsage && sub.timeRollingUpdated) {
558-
const result = Black.analyzeRollingUsage({
559-
plan,
560+
const blackData = BlackData.getLimits({ plan })
561+
const result = Subscription.analyzeRollingUsage({
562+
limit: blackData.rollingLimit,
563+
window: blackData.rollingWindow,
560564
usage: sub.rollingUsage,
561565
timeUpdated: sub.timeRollingUpdated,
562566
})
@@ -687,6 +691,7 @@ export async function handler(
687691
}
688692

689693
async function trackUsage(
694+
sessionId: string,
690695
billingSource: BillingSource,
691696
authInfo: AuthInfo,
692697
modelInfo: ModelInfo,
@@ -734,6 +739,7 @@ export async function handler(
734739
cacheWrite1hTokens,
735740
cost,
736741
keyID: authInfo.apiKeyId,
742+
sessionID: sessionId.substring(0, 30),
737743
enrichment: billingSource === "subscription" ? { plan: "sub" } : undefined,
738744
}),
739745
db

packages/console/core/migrations/0000_fluffy_raza.sql renamed to packages/console/core/migrations/20250902065410_fluffy_raza/migration.sql

File renamed without changes.

0 commit comments

Comments
 (0)