model.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { z } from "zod"
  2. import { eq, and } from "drizzle-orm"
  3. import { Database } from "./drizzle"
  4. import { ModelTable } from "./schema/model.sql"
  5. import { Identifier } from "./identifier"
  6. import { fn } from "./util/fn"
  7. import { Actor } from "./actor"
  8. import { Resource } from "@opencode-ai/console-resource"
  9. export namespace ZenData {
  10. const FormatSchema = z.enum(["anthropic", "google", "openai", "oa-compat"])
  11. const TrialSchema = z.object({
  12. provider: z.string(),
  13. limits: z.array(
  14. z.object({
  15. limit: z.number(),
  16. client: z.enum(["cli", "desktop"]).optional(),
  17. }),
  18. ),
  19. })
  20. export type Format = z.infer<typeof FormatSchema>
  21. export type Trial = z.infer<typeof TrialSchema>
  22. const ModelCostSchema = z.object({
  23. input: z.number(),
  24. output: z.number(),
  25. cacheRead: z.number().optional(),
  26. cacheWrite5m: z.number().optional(),
  27. cacheWrite1h: z.number().optional(),
  28. })
  29. const ModelSchema = z.object({
  30. name: z.string(),
  31. cost: ModelCostSchema,
  32. cost200K: ModelCostSchema.optional(),
  33. allowAnonymous: z.boolean().optional(),
  34. byokProvider: z.enum(["openai", "anthropic", "google"]).optional(),
  35. stickyProvider: z.enum(["strict", "prefer"]).optional(),
  36. trial: TrialSchema.optional(),
  37. rateLimit: z.number().optional(),
  38. fallbackProvider: z.string().optional(),
  39. providers: z.array(
  40. z.object({
  41. id: z.string(),
  42. model: z.string(),
  43. weight: z.number().optional(),
  44. disabled: z.boolean().optional(),
  45. storeModel: z.string().optional(),
  46. }),
  47. ),
  48. })
  49. const ProviderSchema = z.object({
  50. api: z.string(),
  51. apiKey: z.string(),
  52. format: FormatSchema,
  53. headerMappings: z.record(z.string(), z.string()).optional(),
  54. })
  55. const ModelsSchema = z.object({
  56. models: z.record(z.string(), z.union([ModelSchema, z.array(ModelSchema.extend({ formatFilter: FormatSchema }))])),
  57. providers: z.record(z.string(), ProviderSchema),
  58. })
  59. export const validate = fn(ModelsSchema, (input) => {
  60. return input
  61. })
  62. export const list = fn(z.void(), () => {
  63. const json = JSON.parse(
  64. Resource.ZEN_MODELS1.value +
  65. Resource.ZEN_MODELS2.value +
  66. Resource.ZEN_MODELS3.value +
  67. Resource.ZEN_MODELS4.value +
  68. Resource.ZEN_MODELS5.value +
  69. Resource.ZEN_MODELS6.value +
  70. Resource.ZEN_MODELS7.value +
  71. Resource.ZEN_MODELS8.value,
  72. )
  73. return ModelsSchema.parse(json)
  74. })
  75. }
  76. export namespace Model {
  77. export const enable = fn(z.object({ model: z.string() }), ({ model }) => {
  78. Actor.assertAdmin()
  79. return Database.use((db) =>
  80. db.delete(ModelTable).where(and(eq(ModelTable.workspaceID, Actor.workspace()), eq(ModelTable.model, model))),
  81. )
  82. })
  83. export const disable = fn(z.object({ model: z.string() }), ({ model }) => {
  84. Actor.assertAdmin()
  85. return Database.use((db) =>
  86. db
  87. .insert(ModelTable)
  88. .values({
  89. id: Identifier.create("model"),
  90. workspaceID: Actor.workspace(),
  91. model: model,
  92. })
  93. .onDuplicateKeyUpdate({
  94. set: {
  95. timeDeleted: null,
  96. },
  97. }),
  98. )
  99. })
  100. export const listDisabled = fn(z.void(), () => {
  101. return Database.use((db) =>
  102. db
  103. .select({ model: ModelTable.model })
  104. .from(ModelTable)
  105. .where(eq(ModelTable.workspaceID, Actor.workspace()))
  106. .then((rows) => rows.map((row) => row.model)),
  107. )
  108. })
  109. export const isDisabled = fn(
  110. z.object({
  111. model: z.string(),
  112. }),
  113. ({ model }) => {
  114. return Database.use(async (db) => {
  115. const result = await db
  116. .select()
  117. .from(ModelTable)
  118. .where(and(eq(ModelTable.workspaceID, Actor.workspace()), eq(ModelTable.model, model)))
  119. .limit(1)
  120. return result.length > 0
  121. })
  122. },
  123. )
  124. }