mirror of
https://github.com/Kilo-Org/kilocode.git
synced 2026-11-04 07:03:10 +08:00
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import path from "path"
|
|
import { fileURLToPath } from "url"
|
|
import { parseModelsSnapshot } from "../src/kilocode/provider/models-snapshot-shape" // kilocode_change
|
|
|
|
const __filename = fileURLToPath(import.meta.url)
|
|
const __dirname = path.dirname(__filename)
|
|
const dir = path.resolve(__dirname, "..")
|
|
|
|
process.chdir(dir)
|
|
|
|
const modelsUrl = process.env.KILO_MODELS_URL || "https://models.dev"
|
|
// kilocode_change start
|
|
const cacheFile = path.resolve(dir, "node_modules/.cache/models-dev-api.json")
|
|
const raw = await (async () => {
|
|
if (process.env.MODELS_DEV_API_JSON) {
|
|
return await Bun.file(process.env.MODELS_DEV_API_JSON).text()
|
|
}
|
|
const cached = Bun.file(cacheFile)
|
|
if (await cached.exists()) {
|
|
try {
|
|
const st = await cached.stat()
|
|
if (Date.now() - st.mtimeMs < 6 * 3600 * 1000) {
|
|
return await cached.text()
|
|
}
|
|
} catch {}
|
|
}
|
|
try {
|
|
const res = await fetch(`${modelsUrl}/api.json`, { signal: AbortSignal.timeout(5000) })
|
|
const text = await res.text()
|
|
await Bun.write(cacheFile, text).catch(() => {})
|
|
return text
|
|
} catch (e) {
|
|
if (await cached.exists()) return await cached.text()
|
|
throw e
|
|
}
|
|
})()
|
|
export const modelsData = JSON.stringify(parseModelsSnapshot(raw).data)
|
|
// kilocode_change end
|
|
console.log("Loaded models.dev snapshot")
|