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