cloudaxe-opencode/packages/console/core/src/model.ts

137 lines
3.8 KiB
TypeScript
Raw Normal View History

2025-09-26 19:18:22 +00:00
import { z } from "zod"
2025-10-08 04:03:34 +00:00
import { eq, and } from "drizzle-orm"
import { Database } from "./drizzle"
import { ModelTable } from "./schema/model.sql"
import { Identifier } from "./identifier"
import { fn } from "./util/fn"
import { Actor } from "./actor"
import { Resource } from "@opencode-ai/console-resource"
2025-09-26 19:18:22 +00:00
2025-10-16 19:58:49 +00:00
export namespace ZenData {
2025-11-18 19:28:27 +00:00
const FormatSchema = z.enum(["anthropic", "google", "openai", "oa-compat"])
2025-12-12 04:41:04 +00:00
const TrialSchema = z.object({
provider: z.string(),
limits: z.array(
z.object({
limit: z.number(),
client: z.enum(["cli", "desktop"]).optional(),
}),
),
})
2025-11-03 22:30:16 +00:00
export type Format = z.infer<typeof FormatSchema>
2025-12-12 04:41:04 +00:00
export type Trial = z.infer<typeof TrialSchema>
2025-11-03 22:30:16 +00:00
2025-09-26 19:18:22 +00:00
const ModelCostSchema = z.object({
input: z.number(),
output: z.number(),
cacheRead: z.number().optional(),
cacheWrite5m: z.number().optional(),
cacheWrite1h: z.number().optional(),
})
2025-10-16 19:58:49 +00:00
const ModelSchema = z.object({
2025-10-11 19:07:06 +00:00
name: z.string(),
2025-09-26 19:18:22 +00:00
cost: ModelCostSchema,
cost200K: ModelCostSchema.optional(),
allowAnonymous: z.boolean().optional(),
2025-12-05 02:53:31 +00:00
byokProvider: z.enum(["openai", "anthropic", "google"]).optional(),
2026-01-16 06:07:00 +00:00
stickyProvider: z.enum(["strict", "prefer"]).optional(),
2025-12-12 04:41:04 +00:00
trial: TrialSchema.optional(),
2025-11-08 15:15:35 +00:00
rateLimit: z.number().optional(),
2025-11-11 05:29:42 +00:00
fallbackProvider: z.string().optional(),
2025-09-26 19:18:22 +00:00
providers: z.array(
z.object({
id: z.string(),
model: z.string(),
weight: z.number().optional(),
disabled: z.boolean().optional(),
2025-11-23 20:21:47 +00:00
storeModel: z.string().optional(),
2025-09-26 19:18:22 +00:00
}),
),
})
2025-10-16 19:58:49 +00:00
const ProviderSchema = z.object({
api: z.string(),
apiKey: z.string(),
2025-11-03 22:30:16 +00:00
format: FormatSchema,
2025-10-16 19:58:49 +00:00
headerMappings: z.record(z.string(), z.string()).optional(),
})
const ModelsSchema = z.object({
2025-12-12 04:41:04 +00:00
models: z.record(z.string(), z.union([ModelSchema, z.array(ModelSchema.extend({ formatFilter: FormatSchema }))])),
2025-10-16 19:58:49 +00:00
providers: z.record(z.string(), ProviderSchema),
})
2025-10-08 04:03:34 +00:00
2025-10-16 19:58:49 +00:00
export const validate = fn(ModelsSchema, (input) => {
return input
})
export const list = fn(z.void(), () => {
2025-11-21 17:50:51 +00:00
const json = JSON.parse(
2025-12-12 04:41:04 +00:00
Resource.ZEN_MODELS1.value +
Resource.ZEN_MODELS2.value +
Resource.ZEN_MODELS3.value +
Resource.ZEN_MODELS4.value +
2025-12-24 01:36:52 +00:00
Resource.ZEN_MODELS5.value +
2026-01-04 05:58:06 +00:00
Resource.ZEN_MODELS6.value +
2026-01-15 23:21:01 +00:00
Resource.ZEN_MODELS7.value +
Resource.ZEN_MODELS8.value,
2025-11-21 17:50:51 +00:00
)
2025-10-16 19:58:49 +00:00
return ModelsSchema.parse(json)
})
2025-10-08 04:03:34 +00:00
}
export namespace Model {
export const enable = fn(z.object({ model: z.string() }), ({ model }) => {
2025-10-10 04:02:04 +00:00
Actor.assertAdmin()
2025-10-08 04:03:34 +00:00
return Database.use((db) =>
2025-11-08 01:59:02 +00:00
db.delete(ModelTable).where(and(eq(ModelTable.workspaceID, Actor.workspace()), eq(ModelTable.model, model))),
2025-10-08 04:03:34 +00:00
)
})
export const disable = fn(z.object({ model: z.string() }), ({ model }) => {
2025-10-10 04:02:04 +00:00
Actor.assertAdmin()
2025-10-08 04:03:34 +00:00
return Database.use((db) =>
db
.insert(ModelTable)
.values({
id: Identifier.create("model"),
workspaceID: Actor.workspace(),
model: model,
})
.onDuplicateKeyUpdate({
set: {
timeDeleted: null,
},
}),
)
})
export const listDisabled = fn(z.void(), () => {
return Database.use((db) =>
db
.select({ model: ModelTable.model })
.from(ModelTable)
.where(eq(ModelTable.workspaceID, Actor.workspace()))
.then((rows) => rows.map((row) => row.model)),
)
})
export const isDisabled = fn(
z.object({
model: z.string(),
}),
({ model }) => {
return Database.use(async (db) => {
const result = await db
.select()
.from(ModelTable)
.where(and(eq(ModelTable.workspaceID, Actor.workspace()), eq(ModelTable.model, model)))
.limit(1)
return result.length > 0
})
},
)
2025-09-26 19:18:22 +00:00
}