zen: tpm routing

This commit is contained in:
Frank
2026-04-20 22:21:06 -04:00
parent 3e8abac625
commit f74a255ca9
5 changed files with 2746 additions and 29 deletions
@@ -1,28 +1,25 @@
import { and, Database, eq, inArray, sql } from "@opencode-ai/console-core/drizzle/index.js"
import { ModelTpmLimitTable } from "@opencode-ai/console-core/schema/ip.sql.js"
import { ModelTpmRateLimitTable } from "@opencode-ai/console-core/schema/ip.sql.js"
import { UsageInfo } from "./provider/provider"
export function createModelTpmLimiter(providers: { id: string; model: string; tpmLimit?: number }[]) {
const ids = providers.filter((p) => p.tpmLimit).map((p) => `${p.id}/${p.model}`)
if (ids.length === 0) return
const yyyyMMddHHmm = new Date(Date.now())
.toISOString()
.replace(/[^0-9]/g, "")
.substring(0, 12)
const yyyyMMddHHmm = parseInt(
new Date(Date.now())
.toISOString()
.replace(/[^0-9]/g, "")
.substring(0, 12),
)
return {
check: async () => {
const data = await Database.use((tx) =>
tx
.select()
.from(ModelTpmLimitTable)
.where(
inArray(
ModelTpmLimitTable.id,
ids.map((id) => formatId(id, yyyyMMddHHmm)),
),
),
.from(ModelTpmRateLimitTable)
.where(and(inArray(ModelTpmRateLimitTable.id, ids), eq(ModelTpmRateLimitTable.interval, yyyyMMddHHmm))),
)
// convert to map of model to count
@@ -41,14 +38,10 @@ export function createModelTpmLimiter(providers: { id: string; model: string; tp
if (usage <= 0) return
await Database.use((tx) =>
tx
.insert(ModelTpmLimitTable)
.values({ id: formatId(id, yyyyMMddHHmm), count: usage })
.onDuplicateKeyUpdate({ set: { count: sql`${ModelTpmLimitTable.count} + ${usage}` } }),
.insert(ModelTpmRateLimitTable)
.values({ id, interval: yyyyMMddHHmm, count: usage })
.onDuplicateKeyUpdate({ set: { count: sql`${ModelTpmRateLimitTable.count} + ${usage}` } }),
)
},
}
function formatId(id: string, yyyyMMddHHmm: string) {
return `${id.substring(0, 200)}/${yyyyMMddHHmm}`
}
}