update-models.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import path from "path"
  4. import os from "os"
  5. import { ZenData } from "../src/model"
  6. const root = path.resolve(process.cwd(), "..", "..", "..")
  7. const models = await $`bun sst secret list`.cwd(root).text()
  8. const PARTS = 8
  9. // read the line starting with "ZEN_MODELS"
  10. const lines = models.split("\n")
  11. const oldValues = Array.from({ length: PARTS }, (_, i) => {
  12. const value = lines
  13. .find((line) => line.startsWith(`ZEN_MODELS${i + 1}`))
  14. ?.split("=")
  15. .slice(1)
  16. .join("=")
  17. if (!value) throw new Error(`ZEN_MODELS${i + 1} not found`)
  18. return value
  19. })
  20. // store the prettified json to a temp file
  21. const filename = `models-${Date.now()}.json`
  22. const tempFile = Bun.file(path.join(os.tmpdir(), filename))
  23. await tempFile.write(JSON.stringify(JSON.parse(oldValues.join("")), null, 2))
  24. console.log("tempFile", tempFile.name)
  25. // open temp file in vim and read the file on close
  26. await $`vim ${tempFile.name}`
  27. const newValue = JSON.stringify(JSON.parse(await tempFile.text()))
  28. ZenData.validate(JSON.parse(newValue))
  29. // update the secret
  30. const chunk = Math.ceil(newValue.length / PARTS)
  31. const newValues = Array.from({ length: PARTS }, (_, i) =>
  32. newValue.slice(chunk * i, i === PARTS - 1 ? undefined : chunk * (i + 1)),
  33. )
  34. for (let i = 0; i < PARTS; i++) {
  35. await $`bun sst secret set ZEN_MODELS${i + 1} -- ${newValues[i]}`
  36. }