install-agentpaas.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { parseArgs } from "node:util"
  2. import path from "node:path"
  3. import { mkdir, copyFile } from "node:fs/promises"
  4. const args = parseArgs({ options: { target: { type: "string" }, check: { type: "boolean" } } }).values
  5. if (!args.target) throw new Error("用法:bun script/install-agentpaas.ts --target /absolute/project [--check]")
  6. const source = path.resolve(import.meta.dir, "../.opencode")
  7. const target = path.resolve(args.target, ".opencode")
  8. if (target === source) throw new Error("目标不能是桥接源码目录")
  9. const manifest = (await Bun.file(path.join(source, "agentpaas-manifest.json")).json()) as { files: string[] }
  10. const changed: string[] = []
  11. for (const name of manifest.files) {
  12. if (path.isAbsolute(name) || name.split(/[\\/]/).includes("..")) throw new Error("无效安装清单")
  13. const original = Bun.file(path.join(source, name))
  14. const existing = Bun.file(path.join(target, name))
  15. if (
  16. (await existing.exists()) &&
  17. Buffer.from(await existing.arrayBuffer()).equals(Buffer.from(await original.arrayBuffer()))
  18. )
  19. continue
  20. changed.push(name)
  21. }
  22. if (args.check) {
  23. console.log(JSON.stringify({ current: !changed.length, changed }, null, 2))
  24. process.exit(changed.length ? 1 : 0)
  25. }
  26. const backup = path.join(target, ".agentpaas-backups", `${Date.now()}-${crypto.randomUUID()}`)
  27. for (const name of changed) {
  28. const destination = path.join(target, name)
  29. if (await Bun.file(destination).exists()) {
  30. await mkdir(path.dirname(path.join(backup, name)), { recursive: true })
  31. await copyFile(destination, path.join(backup, name))
  32. }
  33. await mkdir(path.dirname(destination), { recursive: true })
  34. await copyFile(path.join(source, name), destination)
  35. }
  36. console.log(
  37. JSON.stringify(
  38. {
  39. installed: target,
  40. updated: changed,
  41. backup_if_needed: backup,
  42. next: "设置 AGENTPAAS_URL 和 AGENTPAAS_API_KEY,使用当前 OpenCode fork 启动 --agent paas,或输入 /paas。不会修改你的模型或凭证配置。",
  43. },
  44. null,
  45. 2,
  46. ),
  47. )