Skip to content

Commit ad65af2

Browse files
committed
zen: tpm routing
1 parent bd1bdc4 commit ad65af2

File tree

6 files changed

+5474
-12
lines changed

6 files changed

+5474
-12
lines changed
Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { and, Database, eq, inArray, sql } from "@opencode-ai/console-core/drizzle/index.js"
2-
import { ModelRateLimitTable } from "@opencode-ai/console-core/schema/ip.sql.js"
2+
import { ModelTpmLimitTable } from "@opencode-ai/console-core/schema/ip.sql.js"
33
import { UsageInfo } from "./provider/provider"
44

55
export function createModelTpmLimiter(providers: { id: string; model: string; tpmLimit?: number }[]) {
6-
const keys = providers.filter((p) => p.tpmLimit).map((p) => `${p.id}/${p.model}`)
7-
if (keys.length === 0) return
6+
const ids = providers.filter((p) => p.tpmLimit).map((p) => `${p.id}/${p.model}`)
7+
if (ids.length === 0) return
88

99
const yyyyMMddHHmm = new Date(Date.now())
1010
.toISOString()
@@ -16,30 +16,39 @@ export function createModelTpmLimiter(providers: { id: string; model: string; tp
1616
const data = await Database.use((tx) =>
1717
tx
1818
.select()
19-
.from(ModelRateLimitTable)
20-
.where(and(inArray(ModelRateLimitTable.key, keys), eq(ModelRateLimitTable.interval, yyyyMMddHHmm))),
19+
.from(ModelTpmLimitTable)
20+
.where(
21+
inArray(
22+
ModelTpmLimitTable.id,
23+
ids.map((id) => formatId(id, yyyyMMddHHmm)),
24+
),
25+
),
2126
)
2227

2328
// convert to map of model to count
2429
return data.reduce(
2530
(acc, curr) => {
26-
acc[curr.key] = curr.count
31+
acc[curr.id] = curr.count
2732
return acc
2833
},
2934
{} as Record<string, number>,
3035
)
3136
},
32-
track: async (id: string, model: string, usageInfo: UsageInfo) => {
33-
const key = `${id}/${model}`
34-
if (!keys.includes(key)) return
37+
track: async (provider: string, model: string, usageInfo: UsageInfo) => {
38+
const id = `${provider}/${model}`
39+
if (!ids.includes(id)) return
3540
const usage = usageInfo.inputTokens
3641
if (usage <= 0) return
3742
await Database.use((tx) =>
3843
tx
39-
.insert(ModelRateLimitTable)
40-
.values({ key, interval: yyyyMMddHHmm, count: usage })
41-
.onDuplicateKeyUpdate({ set: { count: sql`${ModelRateLimitTable.count} + ${usage}` } }),
44+
.insert(ModelTpmLimitTable)
45+
.values({ id: formatId(id, yyyyMMddHHmm), count: usage })
46+
.onDuplicateKeyUpdate({ set: { count: sql`${ModelTpmLimitTable.count} + ${usage}` } }),
4247
)
4348
},
4449
}
50+
51+
function formatId(id: string, yyyyMMddHHmm: string) {
52+
return `${id.substring(0, 200)}/${yyyyMMddHHmm}`
53+
}
4554
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE `model_tpm_limit` (
2+
`id` varchar(255) NOT NULL,
3+
`interval` int NOT NULL,
4+
`count` int NOT NULL,
5+
CONSTRAINT PRIMARY KEY(`id`,`interval`)
6+
);

0 commit comments

Comments
 (0)