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

134 lines
3.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"
2025-08-01 16:17:51 +00:00
import { Installation } from "../installation"
import { Flag } from "../flag/flag"
2026-01-24 17:26:57 +00:00
import { lazy } from "@/util/lazy"
// Try to import bundled snapshot (generated at build time)
// Falls back to undefined in dev mode when snapshot doesn't exist
/* @ts-ignore */
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(),
input: z.number().optional(),
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(),
2026-02-12 04:10:47 +00:00
provider: z.object({ npm: z.string(), api: z.string() }).optional(),
variants: z.record(z.string(), z.record(z.string(), z.any())).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>
2026-01-24 17:26:57 +00:00
function url() {
return Flag.OPENCODE_MODELS_URL || "https://models.dev"
}
export const Data = lazy(async () => {
const file = Bun.file(Flag.OPENCODE_MODELS_PATH ?? filepath)
2025-06-10 22:23:19 +00:00
const result = await file.json().catch(() => {})
if (result) return result
2026-01-24 19:14:17 +00:00
// @ts-ignore
const snapshot = await import("./models-snapshot")
.then((m) => m.snapshot as Record<string, unknown>)
.catch(() => undefined)
if (snapshot) return snapshot
2026-01-24 17:26:57 +00:00
if (Flag.OPENCODE_DISABLE_MODELS_FETCH) return {}
const json = await fetch(`${url()}/api.json`).then((x) => x.text())
return JSON.parse(json)
})
export async function get() {
const result = await Data()
return result 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)
2026-01-24 17:26:57 +00:00
const result = await fetch(`${url()}/api.json`, {
2025-08-01 02:00:45 +00:00
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,
})
})
2026-01-24 17:26:57 +00:00
if (result && result.ok) {
await Bun.write(file, await result.text())
ModelsDev.Data.reset()
}
2025-06-05 18:59:07 +00:00
}
}
2025-08-01 03:47:23 +00:00
2026-01-24 17:26:57 +00:00
if (!Flag.OPENCODE_DISABLE_MODELS_FETCH) {
ModelsDev.refresh()
2026-01-24 17:26:57 +00:00
setInterval(
async () => {
await ModelsDev.refresh()
},
60 * 1000 * 60,
).unref()
}