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

114 lines
3.0 KiB
TypeScript
Raw Normal View History

2025-09-27 03:18:22 +08:00
import { z } from "zod"
2025-10-08 12:03:34 +08: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-27 03:18:22 +08:00
2025-10-17 03:58:49 +08:00
export namespace ZenData {
2025-11-04 06:30:16 +08:00
const FormatSchema = z.enum(["anthropic", "openai", "oa-compat"])
export type Format = z.infer<typeof FormatSchema>
2025-09-27 03:18:22 +08: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-17 03:58:49 +08:00
const ModelSchema = z.object({
2025-10-12 03:07:06 +08:00
name: z.string(),
2025-09-27 03:18:22 +08:00
cost: ModelCostSchema,
cost200K: ModelCostSchema.optional(),
allowAnonymous: z.boolean().optional(),
2025-11-08 23:15:35 +08:00
rateLimit: z.number().optional(),
2025-11-11 13:29:42 +08:00
fallbackProvider: z.string().optional(),
2025-09-27 03:18:22 +08:00
providers: z.array(
z.object({
id: z.string(),
model: z.string(),
weight: z.number().optional(),
disabled: z.boolean().optional(),
}),
),
})
2025-10-17 03:58:49 +08:00
const ProviderSchema = z.object({
api: z.string(),
apiKey: z.string(),
2025-11-04 06:30:16 +08:00
format: FormatSchema,
2025-10-17 03:58:49 +08:00
headerMappings: z.record(z.string(), z.string()).optional(),
})
const ModelsSchema = z.object({
models: z.record(z.string(), ModelSchema),
providers: z.record(z.string(), ProviderSchema),
})
2025-10-08 12:03:34 +08:00
2025-10-17 03:58:49 +08:00
export const validate = fn(ModelsSchema, (input) => {
return input
})
export const list = fn(z.void(), () => {
2025-11-04 04:43:52 +08:00
const json = JSON.parse(Resource.ZEN_MODELS1.value + Resource.ZEN_MODELS2.value)
2025-10-17 03:58:49 +08:00
return ModelsSchema.parse(json)
})
2025-10-08 12:03:34 +08:00
}
export namespace Model {
export const enable = fn(z.object({ model: z.string() }), ({ model }) => {
2025-10-10 12:02:04 +08:00
Actor.assertAdmin()
2025-10-08 12:03:34 +08:00
return Database.use((db) =>
2025-11-08 09:59:02 +08:00
db.delete(ModelTable).where(and(eq(ModelTable.workspaceID, Actor.workspace()), eq(ModelTable.model, model))),
2025-10-08 12:03:34 +08:00
)
})
export const disable = fn(z.object({ model: z.string() }), ({ model }) => {
2025-10-10 12:02:04 +08:00
Actor.assertAdmin()
2025-10-08 12:03:34 +08: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-27 03:18:22 +08:00
}