models.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { Global } from "../global"
  2. import { Log } from "../util/log"
  3. import path from "path"
  4. import { z } from "zod"
  5. import { data } from "./models-macro" with { type: "macro" }
  6. import { Installation } from "../installation"
  7. export namespace ModelsDev {
  8. const log = Log.create({ service: "models.dev" })
  9. const filepath = path.join(Global.Path.cache, "models.json")
  10. export const Model = z
  11. .object({
  12. id: z.string(),
  13. name: z.string(),
  14. release_date: z.string(),
  15. attachment: z.boolean(),
  16. reasoning: z.boolean(),
  17. temperature: z.boolean(),
  18. tool_call: z.boolean(),
  19. cost: z.object({
  20. input: z.number(),
  21. output: z.number(),
  22. cache_read: z.number().optional(),
  23. cache_write: z.number().optional(),
  24. }),
  25. limit: z.object({
  26. context: z.number(),
  27. output: z.number(),
  28. }),
  29. experimental: z.boolean().optional(),
  30. options: z.record(z.any()),
  31. provider: z.object({ npm: z.string() }).optional(),
  32. })
  33. .openapi({
  34. ref: "Model",
  35. })
  36. export type Model = z.infer<typeof Model>
  37. export const Provider = z
  38. .object({
  39. api: z.string().optional(),
  40. name: z.string(),
  41. env: z.array(z.string()),
  42. id: z.string(),
  43. npm: z.string().optional(),
  44. models: z.record(Model),
  45. })
  46. .openapi({
  47. ref: "Provider",
  48. })
  49. export type Provider = z.infer<typeof Provider>
  50. export async function get() {
  51. refresh()
  52. const file = Bun.file(filepath)
  53. const result = await file.json().catch(() => {})
  54. if (result) return result as Record<string, Provider>
  55. const json = await data()
  56. return JSON.parse(json) as Record<string, Provider>
  57. }
  58. export async function refresh() {
  59. const file = Bun.file(filepath)
  60. log.info("refreshing", {
  61. file,
  62. })
  63. const result = await fetch("https://models.dev/api.json", {
  64. headers: {
  65. "User-Agent": Installation.USER_AGENT,
  66. },
  67. }).catch((e) => {
  68. log.error("Failed to fetch models.dev", {
  69. error: e,
  70. })
  71. })
  72. if (result && result.ok) await Bun.write(file, await result.text())
  73. }
  74. }
  75. setInterval(() => ModelsDev.refresh(), 60 * 1000 * 60).unref()