Просмотр исходного кода

feat: add -f/--file flag to opencode run command (#3295)

Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: rekram1-node <[email protected]>
Co-authored-by: Aiden Cline <[email protected]>
Cheol Kang 4 месяцев назад
Родитель
Сommit
2f66055d25
1 измененных файлов с 37 добавлено и 0 удалено
  1. 37 0
      packages/opencode/src/cli/cmd/run.ts

+ 37 - 0
packages/opencode/src/cli/cmd/run.ts

@@ -1,4 +1,5 @@
 import type { Argv } from "yargs"
 import type { Argv } from "yargs"
+import path from "path"
 import { Bus } from "../../bus"
 import { Bus } from "../../bus"
 import { Provider } from "../../provider/provider"
 import { Provider } from "../../provider/provider"
 import { Session } from "../../session"
 import { Session } from "../../session"
@@ -70,10 +71,45 @@ export const RunCommand = cmd({
         default: "default",
         default: "default",
         describe: "format: default (formatted) or json (raw JSON events)",
         describe: "format: default (formatted) or json (raw JSON events)",
       })
       })
+      .option("file", {
+        alias: ["f"],
+        type: "string",
+        array: true,
+        describe: "file(s) to attach to message",
+      })
   },
   },
   handler: async (args) => {
   handler: async (args) => {
     let message = args.message.join(" ")
     let message = args.message.join(" ")
 
 
+    let fileParts: any[] = []
+    if (args.file) {
+      const files = Array.isArray(args.file) ? args.file : [args.file]
+
+      for (const filePath of files) {
+        const resolvedPath = path.resolve(process.cwd(), filePath)
+        const file = Bun.file(resolvedPath)
+        const stats = await file.stat().catch(() => {})
+        if (!stats) {
+          UI.error(`File not found: ${filePath}`)
+          process.exit(1)
+        }
+        if (!(await file.exists())) {
+          UI.error(`File not found: ${filePath}`)
+          process.exit(1)
+        }
+
+        const stat = await file.stat()
+        const mime = stat.isDirectory() ? "application/x-directory" : "text/plain"
+
+        fileParts.push({
+          type: "file",
+          url: `file://${resolvedPath}`,
+          filename: path.basename(resolvedPath),
+          mime,
+        })
+      }
+    }
+
     if (!process.stdin.isTTY) message += "\n" + (await Bun.stdin.text())
     if (!process.stdin.isTTY) message += "\n" + (await Bun.stdin.text())
 
 
     if (message.trim().length === 0 && !args.command) {
     if (message.trim().length === 0 && !args.command) {
@@ -244,6 +280,7 @@ export const RunCommand = cmd({
           },
           },
           agent: agent.name,
           agent: agent.name,
           parts: [
           parts: [
+            ...fileParts,
             {
             {
               id: Identifier.ascending("part"),
               id: Identifier.ascending("part"),
               type: "text",
               type: "text",