update-models.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. console.log("models", models)
  9. // read the line starting with "ZEN_MODELS"
  10. const oldValue1 = models
  11. .split("\n")
  12. .find((line) => line.startsWith("ZEN_MODELS1"))
  13. ?.split("=")[1]
  14. const oldValue2 = models
  15. .split("\n")
  16. .find((line) => line.startsWith("ZEN_MODELS2"))
  17. ?.split("=")[1]
  18. if (!oldValue1) throw new Error("ZEN_MODELS1 not found")
  19. if (!oldValue2) throw new Error("ZEN_MODELS2 not found")
  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(oldValue1 + oldValue2), 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 mid = Math.floor(newValue.length / 2)
  31. await $`bun sst secret set ZEN_MODELS1 ${newValue.slice(0, mid)}`
  32. await $`bun sst secret set ZEN_MODELS2 ${newValue.slice(mid)}`