update-models.ts 1.8 KB

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