duplicate-pr.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env bun
  2. import path from "path"
  3. import { createOpencode } from "@opencode-ai/sdk"
  4. import { parseArgs } from "util"
  5. async function main() {
  6. const { values, positionals } = parseArgs({
  7. args: Bun.argv.slice(2),
  8. options: {
  9. file: { type: "string", short: "f" },
  10. help: { type: "boolean", short: "h", default: false },
  11. },
  12. allowPositionals: true,
  13. })
  14. if (values.help) {
  15. console.log(`
  16. Usage: bun script/duplicate-pr.ts [options] <message>
  17. Options:
  18. -f, --file <path> File to attach to the prompt
  19. -h, --help Show this help message
  20. Examples:
  21. bun script/duplicate-pr.ts -f pr_info.txt "Check the attached file for PR details"
  22. `)
  23. process.exit(0)
  24. }
  25. const message = positionals.join(" ")
  26. if (!message) {
  27. console.error("Error: message is required")
  28. process.exit(1)
  29. }
  30. const opencode = await createOpencode({ port: 0 })
  31. try {
  32. const parts: Array<{ type: "text"; text: string } | { type: "file"; url: string; filename: string; mime: string }> =
  33. []
  34. if (values.file) {
  35. const resolved = path.resolve(process.cwd(), values.file)
  36. const file = Bun.file(resolved)
  37. if (!(await file.exists())) {
  38. console.error(`Error: file not found: ${values.file}`)
  39. process.exit(1)
  40. }
  41. parts.push({
  42. type: "file",
  43. url: `file://${resolved}`,
  44. filename: path.basename(resolved),
  45. mime: "text/plain",
  46. })
  47. }
  48. parts.push({ type: "text", text: message })
  49. const session = await opencode.client.session.create()
  50. const result = await opencode.client.session
  51. .prompt({
  52. path: { id: session.data!.id },
  53. body: {
  54. agent: "duplicate-pr",
  55. parts,
  56. },
  57. signal: AbortSignal.timeout(120_000),
  58. })
  59. .then((x) => x.data?.parts?.find((y) => y.type === "text")?.text ?? "")
  60. console.log(result.trim())
  61. } finally {
  62. opencode.server.close()
  63. }
  64. }
  65. main()