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

155 lines
4.6 KiB
TypeScript
Raw Permalink 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-11-03 22:30:16 +00:00
export type Format = z.infer<typeof FormatSchema>
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(),
2026-03-03 05:25:03 +00:00
trialProvider: z.string().optional(),
2025-11-11 05:29:42 +00:00
fallbackProvider: z.string().optional(),
2026-03-10 18:40:14 +00:00
rateLimit: z.number().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(),
2026-02-11 05:05:04 +00:00
format: FormatSchema.optional(),
2025-10-16 19:58:49 +00:00
headerMappings: z.record(z.string(), z.string()).optional(),
2026-02-14 05:47:24 +00:00
payloadModifier: z.record(z.string(), z.any()).optional(),
2025-10-16 19:58:49 +00:00
})
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 }))])),
2026-02-21 00:38:27 +00:00
liteModels: z.record(z.string(), ModelSchema),
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
})
2026-02-21 00:38:27 +00:00
export const list = fn(z.enum(["lite", "full"]), (modelList) => {
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 +
2026-01-30 17:19:36 +00:00
Resource.ZEN_MODELS8.value +
Resource.ZEN_MODELS9.value +
2026-02-08 15:31:07 +00:00
Resource.ZEN_MODELS10.value +
Resource.ZEN_MODELS11.value +
Resource.ZEN_MODELS12.value +
Resource.ZEN_MODELS13.value +
Resource.ZEN_MODELS14.value +
Resource.ZEN_MODELS15.value +
Resource.ZEN_MODELS16.value +
Resource.ZEN_MODELS17.value +
Resource.ZEN_MODELS18.value +
Resource.ZEN_MODELS19.value +
2026-02-17 06:32:57 +00:00
Resource.ZEN_MODELS20.value +
Resource.ZEN_MODELS21.value +
Resource.ZEN_MODELS22.value +
Resource.ZEN_MODELS23.value +
Resource.ZEN_MODELS24.value +
Resource.ZEN_MODELS25.value +
Resource.ZEN_MODELS26.value +
Resource.ZEN_MODELS27.value +
Resource.ZEN_MODELS28.value +
Resource.ZEN_MODELS29.value +
Resource.ZEN_MODELS30.value,
2025-11-21 17:50:51 +00:00
)
2026-03-03 05:25:03 +00:00
const { models, liteModels, providers } = ModelsSchema.parse(json)
2026-02-11 05:05:04 +00:00
return {
2026-02-21 00:38:27 +00:00
models: modelList === "lite" ? liteModels : models,
2026-03-03 05:25:03 +00:00
providers,
2026-02-11 05:05:04 +00:00
}
2025-10-16 19:58:49 +00:00
})
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
}