From af684c80d4b7768f60defe8da4cd2811e4350dc2 Mon Sep 17 00:00:00 2001 From: Frank Date: Wed, 8 Oct 2025 01:14:39 -0400 Subject: [PATCH] wip: zen --- .../src/routes/workspace/model-section.tsx | 1 - .../console/app/src/routes/zen/handler.ts | 55 ++++++++++--------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/packages/console/app/src/routes/workspace/model-section.tsx b/packages/console/app/src/routes/workspace/model-section.tsx index 078cbfa8..4128b4a2 100644 --- a/packages/console/app/src/routes/workspace/model-section.tsx +++ b/packages/console/app/src/routes/workspace/model-section.tsx @@ -24,7 +24,6 @@ const updateModel = action(async (form: FormData) => { const workspaceID = form.get("workspaceID")?.toString() if (!workspaceID) return { error: "Workspace ID is required" } const enabled = form.get("enabled")?.toString() === "true" - console.log({ model, workspaceID, enabled }) return json( withActor(async () => { if (enabled) { diff --git a/packages/console/app/src/routes/zen/handler.ts b/packages/console/app/src/routes/zen/handler.ts index feb0c9c3..19f80609 100644 --- a/packages/console/app/src/routes/zen/handler.ts +++ b/packages/console/app/src/routes/zen/handler.ts @@ -12,6 +12,7 @@ import { Actor } from "@opencode-ai/console-core/actor.js" import { WorkspaceTable } from "@opencode-ai/console-core/schema/workspace.sql.js" import { ZenModel } from "@opencode-ai/console-core/model.js" import { UserTable } from "@opencode-ai/console-core/schema/user.sql.js" +import { ModelTable } from "@opencode-ai/console-core/schema/model.sql.js" export async function handler( input: APIEvent, @@ -67,6 +68,7 @@ export async function handler( const providerInfo = selectProvider(modelInfo) const authInfo = await authenticate(modelInfo) validateBilling(modelInfo, authInfo) + validateModelSettings(authInfo) logger.metric({ provider: providerInfo.id }) // Request to model provider @@ -222,14 +224,14 @@ export async function handler( return { id: modelId, ...modelData } } - function selectProvider(model: Model) { + function selectProvider(model: Awaited>) { const providers = model.providers .filter((provider) => !provider.disabled) .flatMap((provider) => Array(provider.weight ?? 1).fill(provider)) return providers[Math.floor(Math.random() * providers.length)] } - async function authenticate(model: Model) { + async function authenticate(model: Awaited>) { const apiKey = opts.parseApiKey(input.request.headers) if (!apiKey) { if (model.allowAnonymous) return @@ -241,20 +243,26 @@ export async function handler( .select({ apiKey: KeyTable.id, workspaceID: KeyTable.workspaceID, - balance: BillingTable.balance, - paymentMethodID: BillingTable.paymentMethodID, - monthlyLimit: BillingTable.monthlyLimit, - monthlyUsage: BillingTable.monthlyUsage, - timeMonthlyUsageUpdated: BillingTable.timeMonthlyUsageUpdated, - userID: UserTable.id, - userMonthlyLimit: UserTable.monthlyLimit, - userMonthlyUsage: UserTable.monthlyUsage, - timeUserMonthlyUsageUpdated: UserTable.timeMonthlyUsageUpdated, + billing: { + balance: BillingTable.balance, + paymentMethodID: BillingTable.paymentMethodID, + monthlyLimit: BillingTable.monthlyLimit, + monthlyUsage: BillingTable.monthlyUsage, + timeMonthlyUsageUpdated: BillingTable.timeMonthlyUsageUpdated, + }, + user: { + id: UserTable.id, + monthlyLimit: UserTable.monthlyLimit, + monthlyUsage: UserTable.monthlyUsage, + timeMonthlyUsageUpdated: UserTable.timeMonthlyUsageUpdated, + }, + timeDisabled: ModelTable.timeCreated, }) .from(KeyTable) .innerJoin(WorkspaceTable, eq(WorkspaceTable.id, KeyTable.workspaceID)) .innerJoin(BillingTable, eq(BillingTable.workspaceID, KeyTable.workspaceID)) .innerJoin(UserTable, and(eq(UserTable.workspaceID, KeyTable.workspaceID), eq(UserTable.id, KeyTable.userID))) + .leftJoin(ModelTable, and(eq(ModelTable.workspaceID, KeyTable.workspaceID), eq(ModelTable.model, model.id))) .where(and(eq(KeyTable.key, apiKey), isNull(KeyTable.timeDeleted))) .then((rows) => rows[0]), ) @@ -265,25 +273,13 @@ export async function handler( workspace: data.workspaceID, }) - const isFree = FREE_WORKSPACES.includes(data.workspaceID) - return { apiKeyId: data.apiKey, workspaceID: data.workspaceID, - billing: { - paymentMethodID: data.paymentMethodID, - balance: data.balance, - monthlyLimit: data.monthlyLimit, - monthlyUsage: data.monthlyUsage, - timeMonthlyUsageUpdated: data.timeMonthlyUsageUpdated, - }, - user: { - id: data.userID, - monthlyLimit: data.userMonthlyLimit, - monthlyUsage: data.userMonthlyUsage, - timeMonthlyUsageUpdated: data.timeUserMonthlyUsageUpdated, - }, - isFree, + billing: data.billing, + user: data.user, + isFree: FREE_WORKSPACES.includes(data.workspaceID), + isDisabled: !!data.timeDisabled, } } @@ -325,6 +321,11 @@ export async function handler( } } + function validateModelSettings(authInfo: Awaited>) { + if (!authInfo) return + if (authInfo.isDisabled) throw new ModelError("Model is disabled") + } + async function trackUsage( authInfo: Awaited>, modelInfo: ReturnType,