Bläddra i källkod

fix: opencode run shouldn't print to stderr (#3341)

Haris Gušić 4 månader sedan
förälder
incheckning
97c7e941eb

+ 2 - 1
packages/opencode/src/cli/cmd/debug/file.ts

@@ -1,3 +1,4 @@
+import { EOL } from "os"
 import { File } from "../../../file"
 import { bootstrap } from "../../bootstrap"
 import { cmd } from "../cmd"
@@ -13,7 +14,7 @@ const FileSearchCommand = cmd({
   async handler(args) {
     await bootstrap(process.cwd(), async () => {
       const results = await File.search({ query: args.query })
-      console.log(results.join("\n"))
+      console.log(results.join(EOL))
     })
   },
 })

+ 2 - 1
packages/opencode/src/cli/cmd/debug/ripgrep.ts

@@ -1,3 +1,4 @@
+import { EOL } from "os"
 import { Ripgrep } from "../../../file/ripgrep"
 import { Instance } from "../../../project/instance"
 import { bootstrap } from "../../bootstrap"
@@ -48,7 +49,7 @@ const FilesCommand = cmd({
         files.push(file)
         if (args.limit && files.length >= args.limit) break
       }
-      console.log(files.join("\n"))
+      console.log(files.join(EOL))
     })
   },
 })

+ 2 - 1
packages/opencode/src/cli/cmd/export.ts

@@ -4,6 +4,7 @@ import { cmd } from "./cmd"
 import { bootstrap } from "../bootstrap"
 import { UI } from "../ui"
 import * as prompts from "@clack/prompts"
+import { EOL } from "os"
 
 export const ExportCommand = cmd({
   command: "export [sessionID]",
@@ -67,7 +68,7 @@ export const ExportCommand = cmd({
         }
 
         process.stdout.write(JSON.stringify(exportData, null, 2))
-        process.stdout.write("\n")
+        process.stdout.write(EOL)
       } catch (error) {
         UI.error(`Session not found: ${sessionID!}`)
         process.exit(1)

+ 9 - 19
packages/opencode/src/cli/cmd/run.ts

@@ -13,6 +13,7 @@ import { Identifier } from "../../id/id"
 import { Agent } from "../../agent/agent"
 import { Command } from "../../command"
 import { SessionPrompt } from "../../session/prompt"
+import { EOL } from "os"
 
 const TOOL: Record<string, [string, string]> = {
   todowrite: ["Todo", UI.Style.TEXT_WARNING_BOLD],
@@ -194,13 +195,12 @@ export const RunCommand = cmd({
             sessionID: session?.id,
             ...data,
           }
-          process.stdout.write(JSON.stringify(jsonEvent) + "\n")
+          process.stdout.write(JSON.stringify(jsonEvent) + EOL)
           return true
         }
         return false
       }
 
-      let text = ""
       const messageID = Identifier.ascending("message")
 
       Bus.subscribe(MessageV2.Event.PartUpdated, async (evt) => {
@@ -232,15 +232,14 @@ export const RunCommand = cmd({
         }
 
         if (part.type === "text") {
-          text = part.text
+          const text = part.text
+          const isPiped = !process.stdout.isTTY
 
           if (part.time?.end) {
             if (outputJsonEvent("text", { part })) return
-            UI.empty()
-            UI.println(UI.markdown(text))
-            UI.empty()
-            text = ""
-            return
+            if (!isPiped) UI.println()
+            process.stdout.write((isPiped ? text : UI.markdown(text)) + EOL)
+            if (!isPiped) UI.println()
           }
         }
       })
@@ -254,13 +253,13 @@ export const RunCommand = cmd({
         if ("data" in error && error.data && "message" in error.data) {
           err = error.data.message
         }
-        errorMsg = errorMsg ? errorMsg + "\n" + err : err
+        errorMsg = errorMsg ? errorMsg + EOL + err : err
 
         if (outputJsonEvent("error", { error })) return
         UI.error(err)
       })
 
-      const result = await (async () => {
+      await (async () => {
         if (args.command) {
           return await SessionPrompt.command({
             messageID,
@@ -289,15 +288,6 @@ export const RunCommand = cmd({
           ],
         })
       })()
-
-      const isPiped = !process.stdout.isTTY
-      if (isPiped) {
-        const match = result.parts.findLast((x: any) => x.type === "text") as any
-        if (outputJsonEvent("text", { text: match })) return
-        if (match) process.stdout.write(UI.markdown(match.text))
-        if (errorMsg) process.stdout.write(errorMsg)
-      }
-      UI.empty()
       if (errorMsg) process.exit(1)
     })
   },

+ 2 - 1
packages/opencode/src/index.ts

@@ -20,6 +20,7 @@ import { GithubCommand } from "./cli/cmd/github"
 import { ExportCommand } from "./cli/cmd/export"
 import { AttachCommand } from "./cli/cmd/attach"
 import { AcpCommand } from "./cli/cmd/acp"
+import { EOL } from "os"
 
 const cancel = new AbortController()
 
@@ -130,7 +131,7 @@ try {
   const formatted = FormatError(e)
   if (formatted) UI.error(formatted)
   if (formatted === undefined) {
-    UI.error("Unexpected error, check log file at " + Log.file() + " for more details\n")
+    UI.error("Unexpected error, check log file at " + Log.file() + " for more details" + EOL)
     console.error(e)
   }
   process.exitCode = 1