cloudaxe-opencode/packages/opencode/src/provider/models.ts

71 lines
1.8 KiB
TypeScript
Raw Normal View History

2025-06-05 18:59:07 +00:00
import { Global } from "../global"
import { Log } from "../util/log"
import path from "path"
import { z } from "zod"
import { data } from "./models-macro" with { type: "macro" }
2025-06-05 18:59:07 +00:00
export namespace ModelsDev {
const log = Log.create({ service: "models.dev" })
2025-06-10 22:23:19 +00:00
const filepath = path.join(Global.Path.cache, "models.json")
2025-06-05 18:59:07 +00:00
2025-06-13 11:23:12 +00:00
export const Model = z
.object({
2025-06-30 20:45:16 +00:00
id: z.string(),
2025-06-13 11:23:12 +00:00
name: z.string(),
2025-06-30 20:45:16 +00:00
release_date: z.string(),
2025-06-13 11:23:12 +00:00
attachment: z.boolean(),
reasoning: z.boolean(),
temperature: z.boolean(),
tool_call: z.boolean(),
2025-06-13 11:23:12 +00:00
cost: z.object({
input: z.number(),
output: z.number(),
2025-06-18 00:51:25 +00:00
cache_read: z.number().optional(),
cache_write: z.number().optional(),
2025-06-13 11:23:12 +00:00
}),
limit: z.object({
context: z.number(),
output: z.number(),
}),
options: z.record(z.any()),
2025-06-13 11:23:12 +00:00
})
.openapi({
ref: "Model",
2025-06-13 11:23:12 +00:00
})
export type Model = z.infer<typeof Model>
2025-06-13 11:23:12 +00:00
export const Provider = z
.object({
2025-06-18 22:56:41 +00:00
api: z.string().optional(),
2025-06-13 11:23:12 +00:00
name: z.string(),
env: z.array(z.string()),
id: z.string(),
npm: z.string().optional(),
2025-06-13 11:23:12 +00:00
models: z.record(Model),
})
.openapi({
ref: "Provider",
2025-06-13 11:23:12 +00:00
})
export type Provider = z.infer<typeof Provider>
2025-06-05 18:59:07 +00:00
export async function get() {
2025-06-10 22:23:19 +00:00
const file = Bun.file(filepath)
const result = await file.json().catch(() => {})
if (result) {
2025-06-05 18:59:07 +00:00
refresh()
return result as Record<string, Provider>
2025-06-05 18:59:07 +00:00
}
refresh()
const json = await data()
return JSON.parse(json) as Record<string, Provider>
2025-06-05 18:59:07 +00:00
}
async function refresh() {
2025-06-10 22:23:19 +00:00
const file = Bun.file(filepath)
2025-06-05 18:59:07 +00:00
log.info("refreshing")
const result = await fetch("https://models.dev/api.json").catch(() => {})
if (result && result.ok) await Bun.write(file, result)
2025-06-05 18:59:07 +00:00
}
}