update-models.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. // read the line starting with "ZEN_MODELS"
  9. const oldValue1 = models
  10. .split("\n")
  11. .find((line) => line.startsWith("ZEN_MODELS1"))
  12. ?.split("=")[1]
  13. const oldValue2 = models
  14. .split("\n")
  15. .find((line) => line.startsWith("ZEN_MODELS2"))
  16. ?.split("=")[1]
  17. if (!oldValue1) throw new Error("ZEN_MODELS1 not found")
  18. if (!oldValue2) throw new Error("ZEN_MODELS2 not found")
  19. // store the prettified json to a temp file
  20. const filename = `models-${Date.now()}.json`
  21. const tempFile = Bun.file(path.join(os.tmpdir(), filename))
  22. await tempFile.write(JSON.stringify(JSON.parse(oldValue1 + oldValue2), null, 2))
  23. console.log("tempFile", tempFile.name)
  24. // open temp file in vim and read the file on close
  25. await $`vim ${tempFile.name}`
  26. const newValue = JSON.stringify(JSON.parse(await tempFile.text()))
  27. ZenData.validate(JSON.parse(newValue))
  28. // update the secret
  29. const mid = Math.floor(newValue.length / 2)
  30. await $`bun sst secret set ZEN_MODELS1 ${newValue.slice(0, mid)}`
  31. await $`bun sst secret set ZEN_MODELS2 ${newValue.slice(mid)}`