Skip to content

Commit c93c0d4

Browse files
committed
wip: zen
1 parent b168bfe commit c93c0d4

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { WorkspaceTable } from "@opencode-ai/console-core/schema/workspace.sql.j
1313
import { ZenModel } from "@opencode-ai/console-core/model.js"
1414
import { UserTable } from "@opencode-ai/console-core/schema/user.sql.js"
1515
import { ModelTable } from "@opencode-ai/console-core/schema/model.sql.js"
16+
import { ProviderTable } from "@opencode-ai/console-core/schema/provider.sql.js"
1617

1718
export async function handler(
1819
input: APIEvent,
@@ -67,9 +68,10 @@ export async function handler(
6768
})
6869
const modelInfo = validateModel(body.model)
6970
const providerInfo = selectProvider(modelInfo)
70-
const authInfo = await authenticate(modelInfo)
71+
const authInfo = await authenticate(modelInfo, providerInfo)
7172
validateBilling(modelInfo, authInfo)
7273
validateModelSettings(authInfo)
74+
updateProviderKey(authInfo, providerInfo)
7375
logger.metric({ provider: providerInfo.id })
7476

7577
// Request to model provider
@@ -232,7 +234,10 @@ export async function handler(
232234
return providers[Math.floor(Math.random() * providers.length)]
233235
}
234236

235-
async function authenticate(model: Awaited<ReturnType<typeof validateModel>>) {
237+
async function authenticate(
238+
model: Awaited<ReturnType<typeof validateModel>>,
239+
providerInfo: Awaited<ReturnType<typeof selectProvider>>,
240+
) {
236241
const apiKey = opts.parseApiKey(input.request.headers)
237242
if (!apiKey) {
238243
if (model.allowAnonymous) return
@@ -257,13 +262,20 @@ export async function handler(
257262
monthlyUsage: UserTable.monthlyUsage,
258263
timeMonthlyUsageUpdated: UserTable.timeMonthlyUsageUpdated,
259264
},
265+
provider: {
266+
credentials: ProviderTable.credentials,
267+
},
260268
timeDisabled: ModelTable.timeCreated,
261269
})
262270
.from(KeyTable)
263271
.innerJoin(WorkspaceTable, eq(WorkspaceTable.id, KeyTable.workspaceID))
264272
.innerJoin(BillingTable, eq(BillingTable.workspaceID, KeyTable.workspaceID))
265273
.innerJoin(UserTable, and(eq(UserTable.workspaceID, KeyTable.workspaceID), eq(UserTable.id, KeyTable.userID)))
266274
.leftJoin(ModelTable, and(eq(ModelTable.workspaceID, KeyTable.workspaceID), eq(ModelTable.model, model.id)))
275+
.leftJoin(
276+
ProviderTable,
277+
and(eq(ProviderTable.workspaceID, KeyTable.workspaceID), eq(ProviderTable.provider, providerInfo.id)),
278+
)
267279
.where(and(eq(KeyTable.key, apiKey), isNull(KeyTable.timeDeleted)))
268280
.then((rows) => rows[0]),
269281
)
@@ -279,6 +291,7 @@ export async function handler(
279291
workspaceID: data.workspaceID,
280292
billing: data.billing,
281293
user: data.user,
294+
provider: data.provider,
282295
isFree: FREE_WORKSPACES.includes(data.workspaceID),
283296
isDisabled: !!data.timeDisabled,
284297
}
@@ -327,6 +340,15 @@ export async function handler(
327340
if (authInfo.isDisabled) throw new ModelError("Model is disabled")
328341
}
329342

343+
function updateProviderKey(
344+
authInfo: Awaited<ReturnType<typeof authenticate>>,
345+
providerInfo: Awaited<ReturnType<typeof selectProvider>>,
346+
) {
347+
if (!authInfo) return
348+
if (!authInfo.provider?.credentials) return
349+
providerInfo.apiKey = authInfo.provider.credentials
350+
}
351+
330352
async function trackUsage(
331353
authInfo: Awaited<ReturnType<typeof authenticate>>,
332354
modelInfo: ReturnType<typeof validateModel>,
@@ -389,7 +411,7 @@ export async function handler(
389411

390412
if (!authInfo) return
391413

392-
const cost = authInfo.isFree ? 0 : centsToMicroCents(totalCostInCent)
414+
const cost = authInfo.isFree || authInfo.provider?.credentials ? 0 : centsToMicroCents(totalCostInCent)
393415
await Database.transaction(async (tx) => {
394416
await tx.insert(UsageTable).values({
395417
workspaceID: authInfo.workspaceID,
@@ -441,6 +463,8 @@ export async function handler(
441463

442464
async function reload(authInfo: Awaited<ReturnType<typeof authenticate>>) {
443465
if (!authInfo) return
466+
if (authInfo.isFree) return
467+
if (authInfo.provider?.credentials) return
444468

445469
const lock = await Database.use((tx) =>
446470
tx

0 commit comments

Comments
 (0)