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