🌳
pt0/devpconlyF/zhongF/ocModelsCacheJobAI.mts
2import { mkdirSync, readFileSync, writeFileSync, renameSync } from 'fs'
5const modelsJsonPath = `${envHome}/.cache/opencode/models.json`
6const modelsApiUrl = 'https://models.dev/api.json'
8export const ocModelsCacheJob = {
9 name: 'ocModelsCache',
10 intervalSec: 86400,
11 runFnc: async () => {
12 mkdirSync(`${envHome}/.cache/opencode`, {recursive: true})
13 const resp = await fetch(modelsApiUrl, {signal: AbortSignal.timeout(30_000)})
14 if (!resp.ok) throw new Error(`models.dev ${resp.status}`)
15 const text = await resp.text()
16 let existing = null
17 try { existing = readFileSync(modelsJsonPath, 'utf8') } catch { /* not yet cached */ } // catch:userapproved
18 if (existing === text) return chalkGray(`ocModelsCache: unchanged (${text.length}b)`)
19 const tmpPath = `${modelsJsonPath}.tmp.${process.pid}`
20 writeFileSync(tmpPath, text)
21 renameSync(tmpPath, modelsJsonPath)
22 return chalkGreen(`ocModelsCache: ${text.length}b written`)
23 },