opencode_offline/packages/console/core/script/update-models.ts

44 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-09-27 03:18:22 +08:00
#!/usr/bin/env bun
import { $ } from "bun"
import path from "path"
import os from "os"
2025-10-17 03:58:49 +08:00
import { ZenData } from "../src/model"
2025-09-27 03:18:22 +08:00
const root = path.resolve(process.cwd(), "..", "..", "..")
const models = await $`bun sst secret list`.cwd(root).text()
2026-02-17 14:32:57 +08:00
const PARTS = 30
2025-09-27 03:18:22 +08:00
// read the line starting with "ZEN_MODELS"
2025-11-22 01:50:51 +08:00
const lines = models.split("\n")
2026-01-16 07:21:01 +08:00
const oldValues = Array.from({ length: PARTS }, (_, i) => {
const value = lines
2026-01-31 01:19:36 +08:00
.find((line) => line.startsWith(`ZEN_MODELS${i + 1}=`))
2026-01-16 07:21:01 +08:00
?.split("=")
.slice(1)
.join("=")
2026-02-05 09:56:34 +08:00
if (!value) throw new Error(`ZEN_MODELS${i + 1} not found`)
return value
2026-01-16 07:21:01 +08:00
})
2025-09-27 03:18:22 +08:00
// store the prettified json to a temp file
const filename = `models-${Date.now()}.json`
const tempFile = Bun.file(path.join(os.tmpdir(), filename))
2026-01-16 07:21:01 +08:00
await tempFile.write(JSON.stringify(JSON.parse(oldValues.join("")), null, 2))
2025-09-27 03:18:22 +08:00
console.log("tempFile", tempFile.name)
// open temp file in vim and read the file on close
await $`vim ${tempFile.name}`
2025-11-04 04:43:52 +08:00
const newValue = JSON.stringify(JSON.parse(await tempFile.text()))
ZenData.validate(JSON.parse(newValue))
2025-09-27 03:18:22 +08:00
// update the secret
2026-01-16 07:21:01 +08:00
const chunk = Math.ceil(newValue.length / PARTS)
const newValues = Array.from({ length: PARTS }, (_, i) =>
newValue.slice(chunk * i, i === PARTS - 1 ? undefined : chunk * (i + 1)),
)
2025-12-12 12:41:04 +08:00
2026-01-31 01:19:36 +08:00
const envFile = Bun.file(path.join(os.tmpdir(), `models-${Date.now()}.env`))
2026-02-08 02:32:19 +08:00
await envFile.write(newValues.map((v, i) => `ZEN_MODELS${i + 1}="${v.replace(/"/g, '\\"')}"`).join("\n"))
2026-01-31 01:19:36 +08:00
await $`bun sst secret load ${envFile.name}`.cwd(root)