models.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. export namespace ModelsDev {
  7. const log = Log.create({ service: "models.dev" })
  8. const filepath = path.join(Global.Path.cache, "models.json")
  9. export const Model = z
  10. .object({
  11. id: z.string(),
  12. name: z.string(),
  13. release_date: z.string(),
  14. attachment: z.boolean(),
  15. reasoning: z.boolean(),
  16. temperature: z.boolean(),
  17. tool_call: z.boolean(),
  18. cost: z.object({
  19. input: z.number(),
  20. output: z.number(),
  21. cache_read: z.number().optional(),
  22. cache_write: z.number().optional(),
  23. }),
  24. limit: z.object({
  25. context: z.number(),
  26. output: z.number(),
  27. }),
  28. options: z.record(z.any()),
  29. })
  30. .openapi({
  31. ref: "Model",
  32. })
  33. export type Model = z.infer<typeof Model>
  34. export const Provider = z
  35. .object({
  36. api: z.string().optional(),
  37. name: z.string(),
  38. env: z.array(z.string()),
  39. id: z.string(),
  40. npm: z.string().optional(),
  41. models: z.record(Model),
  42. })
  43. .openapi({
  44. ref: "Provider",
  45. })
  46. export type Provider = z.infer<typeof Provider>
  47. export async function get() {
  48. refresh()
  49. const file = Bun.file(filepath)
  50. const result = await file.json().catch(() => {})
  51. if (result) return result as Record<string, Provider>
  52. const json = await data()
  53. return JSON.parse(json) as Record<string, Provider>
  54. }
  55. export async function refresh() {
  56. const file = Bun.file(filepath)
  57. log.info("refreshing")
  58. const result = await fetch("https://models.dev/api.json", {
  59. headers: {
  60. "User-Agent": "opencode",
  61. },
  62. }).catch(() => {})
  63. if (result && result.ok) await Bun.write(file, result)
  64. }
  65. }
  66. setInterval(() => ModelsDev.refresh(), 60 * 1000).unref()