model.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. const RateLimitSchema = z.object({
  21. period: z.enum(["day", "rolling"]),
  22. value: z.number().int(),
  23. checkHeader: z.string().optional(),
  24. fallbackValue: z.number().int().optional(),
  25. })
  26. export type Format = z.infer<typeof FormatSchema>
  27. export type Trial = z.infer<typeof TrialSchema>
  28. export type RateLimit = z.infer<typeof RateLimitSchema>
  29. const ModelCostSchema = z.object({
  30. input: z.number(),
  31. output: z.number(),
  32. cacheRead: z.number().optional(),
  33. cacheWrite5m: z.number().optional(),
  34. cacheWrite1h: z.number().optional(),
  35. })
  36. const ModelSchema = z.object({
  37. name: z.string(),
  38. cost: ModelCostSchema,
  39. cost200K: ModelCostSchema.optional(),
  40. allowAnonymous: z.boolean().optional(),
  41. byokProvider: z.enum(["openai", "anthropic", "google"]).optional(),
  42. stickyProvider: z.enum(["strict", "prefer"]).optional(),
  43. trial: TrialSchema.optional(),
  44. rateLimit: RateLimitSchema.optional(),
  45. fallbackProvider: z.string().optional(),
  46. providers: z.array(
  47. z.object({
  48. id: z.string(),
  49. model: z.string(),
  50. weight: z.number().optional(),
  51. disabled: z.boolean().optional(),
  52. storeModel: z.string().optional(),
  53. }),
  54. ),
  55. })
  56. const ProviderSchema = z.object({
  57. api: z.string(),
  58. apiKey: z.string(),
  59. format: FormatSchema,
  60. headerMappings: z.record(z.string(), z.string()).optional(),
  61. })
  62. const ModelsSchema = z.object({
  63. models: z.record(z.string(), z.union([ModelSchema, z.array(ModelSchema.extend({ formatFilter: FormatSchema }))])),
  64. providers: z.record(z.string(), ProviderSchema),
  65. })
  66. export const validate = fn(ModelsSchema, (input) => {
  67. return input
  68. })
  69. export const list = fn(z.void(), () => {
  70. const json = JSON.parse(
  71. Resource.ZEN_MODELS1.value +
  72. Resource.ZEN_MODELS2.value +
  73. Resource.ZEN_MODELS3.value +
  74. Resource.ZEN_MODELS4.value +
  75. Resource.ZEN_MODELS5.value +
  76. Resource.ZEN_MODELS6.value +
  77. Resource.ZEN_MODELS7.value +
  78. Resource.ZEN_MODELS8.value +
  79. Resource.ZEN_MODELS9.value +
  80. Resource.ZEN_MODELS10.value,
  81. )
  82. return ModelsSchema.parse(json)
  83. })
  84. }
  85. export namespace Model {
  86. export const enable = fn(z.object({ model: z.string() }), ({ model }) => {
  87. Actor.assertAdmin()
  88. return Database.use((db) =>
  89. db.delete(ModelTable).where(and(eq(ModelTable.workspaceID, Actor.workspace()), eq(ModelTable.model, model))),
  90. )
  91. })
  92. export const disable = fn(z.object({ model: z.string() }), ({ model }) => {
  93. Actor.assertAdmin()
  94. return Database.use((db) =>
  95. db
  96. .insert(ModelTable)
  97. .values({
  98. id: Identifier.create("model"),
  99. workspaceID: Actor.workspace(),
  100. model: model,
  101. })
  102. .onDuplicateKeyUpdate({
  103. set: {
  104. timeDeleted: null,
  105. },
  106. }),
  107. )
  108. })
  109. export const listDisabled = fn(z.void(), () => {
  110. return Database.use((db) =>
  111. db
  112. .select({ model: ModelTable.model })
  113. .from(ModelTable)
  114. .where(eq(ModelTable.workspaceID, Actor.workspace()))
  115. .then((rows) => rows.map((row) => row.model)),
  116. )
  117. })
  118. export const isDisabled = fn(
  119. z.object({
  120. model: z.string(),
  121. }),
  122. ({ model }) => {
  123. return Database.use(async (db) => {
  124. const result = await db
  125. .select()
  126. .from(ModelTable)
  127. .where(and(eq(ModelTable.workspaceID, Actor.workspace()), eq(ModelTable.model, model)))
  128. .limit(1)
  129. return result.length > 0
  130. })
  131. },
  132. )
  133. }