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

105 lines
2.9 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-08-01 16:17:51 +00:00
import { Installation } from "../installation"
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
export const Model = z.object({
id: z.string(),
name: z.string(),
2025-12-10 19:30:17 +00:00
family: z.string().optional(),
release_date: z.string(),
attachment: z.boolean(),
reasoning: z.boolean(),
temperature: z.boolean(),
tool_call: z.boolean(),
interleaved: z
.union([
z.literal(true),
z
.object({
field: z.enum(["reasoning_content", "reasoning_details"]),
})
.strict(),
])
.optional(),
cost: z
.object({
2025-06-13 11:23:12 +00:00
input: z.number(),
output: z.number(),
2025-06-18 00:51:25 +00:00
cache_read: z.number().optional(),
cache_write: z.number().optional(),
context_over_200k: z
.object({
input: z.number(),
output: z.number(),
cache_read: z.number().optional(),
cache_write: z.number().optional(),
})
.optional(),
})
.optional(),
limit: z.object({
context: z.number(),
output: z.number(),
}),
modalities: z
.object({
input: z.array(z.enum(["text", "audio", "image", "video", "pdf"])),
output: z.array(z.enum(["text", "audio", "image", "video", "pdf"])),
})
.optional(),
experimental: z.boolean().optional(),
status: z.enum(["alpha", "beta", "deprecated"]).optional(),
options: z.record(z.string(), z.any()),
headers: z.record(z.string(), z.string()).optional(),
provider: z.object({ npm: z.string() }).optional(),
})
export type Model = z.infer<typeof Model>
export const Provider = z.object({
api: z.string().optional(),
name: z.string(),
env: z.array(z.string()),
id: z.string(),
npm: z.string().optional(),
models: z.record(z.string(), Model),
})
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-08-01 03:47:23 +00:00
refresh()
2025-06-10 22:23:19 +00:00
const file = Bun.file(filepath)
const result = await file.json().catch(() => {})
2025-08-01 03:47:23 +00:00
if (result) return result as Record<string, Provider>
const json = await data()
return JSON.parse(json) as Record<string, Provider>
2025-06-05 18:59:07 +00:00
}
2025-08-01 03:47:23 +00:00
export async function refresh() {
2025-06-10 22:23:19 +00:00
const file = Bun.file(filepath)
2025-08-01 21:48:01 +00:00
log.info("refreshing", {
file,
})
2025-08-01 02:00:45 +00:00
const result = await fetch("https://models.dev/api.json", {
headers: {
2025-08-01 16:17:51 +00:00
"User-Agent": Installation.USER_AGENT,
2025-08-01 02:00:45 +00:00
},
signal: AbortSignal.timeout(10 * 1000),
2025-08-01 21:48:01 +00:00
}).catch((e) => {
log.error("Failed to fetch models.dev", {
error: e,
})
})
if (result && result.ok) await Bun.write(file, await result.text())
2025-06-05 18:59:07 +00:00
}
}
2025-08-01 03:47:23 +00:00
setInterval(() => ModelsDev.refresh(), 60 * 1000 * 60).unref()