Dax Raad 8 месяцев назад
Родитель
Сommit
f22c93ba1b
3 измененных файлов с 125 добавлено и 105 удалено
  1. 1 1
      packages/opencode/script/release.ts
  2. 83 70
      packages/opencode/src/cli/cmd/run.ts
  3. 41 34
      packages/opencode/src/index.ts

+ 1 - 1
packages/opencode/script/release.ts

@@ -6,7 +6,7 @@ import pkg from "../package.json"
 
 const dry = process.argv.includes("--dry")
 
-const version = `0.0.0-${Date.now()}`
+const version = `0.0.0-${new Date().toISOString().slice(0, 16).replace(/[-:T]/g, "")}`
 
 const GOARCH: Record<string, string> = {
   arm64: "arm64",

+ 83 - 70
packages/opencode/src/cli/cmd/run.ts

@@ -24,85 +24,98 @@ export const RunCommand = {
         type: "string",
       })
   },
-  handler: async (args: { message: string[]; session?: string }) => {
+  handler: async (args: {
+    message: string[]
+    session?: string
+    printLogs?: boolean
+  }) => {
     const message = args.message.join(" ")
-    await App.provide({ cwd: process.cwd(), version: "0.0.0" }, async () => {
-      await Share.init()
-      const session = args.session
-        ? await Session.get(args.session)
-        : await Session.create()
+    await App.provide(
+      {
+        cwd: process.cwd(),
+        version: "0.0.0",
+        printLogs: args.printLogs,
+      },
+      async () => {
+        await Share.init()
+        const session = args.session
+          ? await Session.get(args.session)
+          : await Session.create()
 
-      UI.print(UI.Style.TEXT_HIGHLIGHT_BOLD + "◍  OpenCode", VERSION)
-      UI.empty()
-      UI.print(UI.Style.TEXT_NORMAL_BOLD + "> ", message)
-      UI.empty()
-      UI.print(
-        UI.Style.TEXT_INFO_BOLD +
-          "~  https://dev.opencode.ai/s?id=" +
-          session.id.slice(-8),
-      )
-      UI.empty()
-
-      function printEvent(color: string, type: string, title: string) {
+        UI.print(UI.Style.TEXT_HIGHLIGHT_BOLD + "◍  OpenCode", VERSION)
+        UI.empty()
+        UI.print(UI.Style.TEXT_NORMAL_BOLD + "> ", message)
+        UI.empty()
         UI.print(
-          color + `|`,
-          UI.Style.TEXT_NORMAL + UI.Style.TEXT_DIM + ` ${type.padEnd(7, " ")}`,
-          "",
-          UI.Style.TEXT_NORMAL + title,
+          UI.Style.TEXT_INFO_BOLD +
+            "~  https://dev.opencode.ai/s?id=" +
+            session.id.slice(-8),
         )
-      }
+        UI.empty()
 
-      Bus.subscribe(Message.Event.PartUpdated, async (message) => {
-        const part = message.properties.part
-        if (
-          part.type === "tool-invocation" &&
-          part.toolInvocation.state === "result"
-        ) {
-          if (part.toolInvocation.toolName === "opencode_todowrite") return
+        function printEvent(color: string, type: string, title: string) {
+          UI.print(
+            color + `|`,
+            UI.Style.TEXT_NORMAL +
+              UI.Style.TEXT_DIM +
+              ` ${type.padEnd(7, " ")}`,
+            "",
+            UI.Style.TEXT_NORMAL + title,
+          )
+        }
 
-          const args = part.toolInvocation.args as any
-          const tool = part.toolInvocation.toolName
+        Bus.subscribe(Message.Event.PartUpdated, async (message) => {
+          const part = message.properties.part
+          if (
+            part.type === "tool-invocation" &&
+            part.toolInvocation.state === "result"
+          ) {
+            if (part.toolInvocation.toolName === "opencode_todowrite") return
 
-          if (tool === "opencode_edit")
-            printEvent(UI.Style.TEXT_SUCCESS_BOLD, "Edit", args.filePath)
-          if (tool === "opencode_bash")
-            printEvent(UI.Style.TEXT_WARNING_BOLD, "Execute", args.command)
-          if (tool === "opencode_read")
-            printEvent(UI.Style.TEXT_INFO_BOLD, "Read", args.filePath)
-          if (tool === "opencode_write")
-            printEvent(UI.Style.TEXT_SUCCESS_BOLD, "Create", args.filePath)
-          if (tool === "opencode_glob")
-            printEvent(
-              UI.Style.TEXT_INFO_BOLD,
-              "Glob",
-              args.pattern + (args.path ? " in " + args.path : ""),
-            )
-        }
+            const args = part.toolInvocation.args as any
+            const tool = part.toolInvocation.toolName
 
-        if (part.type === "text") {
-          if (part.text.includes("\n")) {
-            UI.empty()
-            UI.print(part.text)
-            UI.empty()
-            return
+            if (tool === "opencode_edit")
+              printEvent(UI.Style.TEXT_SUCCESS_BOLD, "Edit", args.filePath)
+            if (tool === "opencode_bash")
+              printEvent(UI.Style.TEXT_WARNING_BOLD, "Execute", args.command)
+            if (tool === "opencode_read")
+              printEvent(UI.Style.TEXT_INFO_BOLD, "Read", args.filePath)
+            if (tool === "opencode_write")
+              printEvent(UI.Style.TEXT_SUCCESS_BOLD, "Create", args.filePath)
+            if (tool === "opencode_glob")
+              printEvent(
+                UI.Style.TEXT_INFO_BOLD,
+                "Glob",
+                args.pattern + (args.path ? " in " + args.path : ""),
+              )
           }
-          printEvent(UI.Style.TEXT_NORMAL_BOLD, "Text", part.text)
-        }
-      })
 
-      const { providerID, modelID } = await Provider.defaultModel()
-      await Session.chat({
-        sessionID: session.id,
-        providerID,
-        modelID,
-        parts: [
-          {
-            type: "text",
-            text: message,
-          },
-        ],
-      })
-      UI.empty()
-    })
+          if (part.type === "text") {
+            if (part.text.includes("\n")) {
+              UI.empty()
+              UI.print(part.text)
+              UI.empty()
+              return
+            }
+            printEvent(UI.Style.TEXT_NORMAL_BOLD, "Text", part.text)
+          }
+        })
+
+        const { providerID, modelID } = await Provider.defaultModel()
+        await Session.chat({
+          sessionID: session.id,
+          providerID,
+          modelID,
+          parts: [
+            {
+              type: "text",
+              text: message,
+            },
+          ],
+        })
+        UI.empty()
+      },
+    )
   },
 }

+ 41 - 34
packages/opencode/src/index.ts

@@ -22,42 +22,49 @@ yargs(hideBin(process.argv))
   .command({
     command: "$0",
     describe: "Start OpenCode TUI",
-    handler: async () => {
-      await App.provide({ cwd: process.cwd(), version: VERSION }, async () => {
-        await Share.init()
-        const server = Server.listen()
+    builder: (yargs) =>
+      yargs.option("print-logs", {
+        type: "boolean",
+      }),
+    handler: async (args) => {
+      await App.provide(
+        { cwd: process.cwd(), version: VERSION, printLogs: args.printLogs },
+        async () => {
+          await Share.init()
+          const server = Server.listen()
 
-        let cmd = ["go", "run", "./main.go"]
-        let cwd = new URL("../../tui/cmd/opencode", import.meta.url).pathname
-        if (Bun.embeddedFiles.length > 0) {
-          const blob = Bun.embeddedFiles[0] as File
-          const binary = path.join(Global.Path.cache, "tui", blob.name)
-          const file = Bun.file(binary)
-          if (!(await file.exists())) {
-            console.log("installing tui binary...")
-            await Bun.write(file, blob, { mode: 0o755 })
-            await fs.chmod(binary, 0o755)
+          let cmd = ["go", "run", "./main.go"]
+          let cwd = new URL("../../tui/cmd/opencode", import.meta.url).pathname
+          if (Bun.embeddedFiles.length > 0) {
+            const blob = Bun.embeddedFiles[0] as File
+            const binary = path.join(Global.Path.cache, "tui", blob.name)
+            const file = Bun.file(binary)
+            if (!(await file.exists())) {
+              console.log("installing tui binary...")
+              await Bun.write(file, blob, { mode: 0o755 })
+              await fs.chmod(binary, 0o755)
+            }
+            cwd = process.cwd()
+            cmd = [binary]
           }
-          cwd = process.cwd()
-          cmd = [binary]
-        }
-        const proc = Bun.spawn({
-          cmd,
-          cwd,
-          stdout: "inherit",
-          stderr: "inherit",
-          stdin: "inherit",
-          env: {
-            ...process.env,
-            OPENCODE_SERVER: server.url.toString(),
-          },
-          onExit: () => {
-            server.stop()
-          },
-        })
-        await proc.exited
-        await server.stop()
-      })
+          const proc = Bun.spawn({
+            cmd,
+            cwd,
+            stdout: "inherit",
+            stderr: "inherit",
+            stdin: "inherit",
+            env: {
+              ...process.env,
+              OPENCODE_SERVER: server.url.toString(),
+            },
+            onExit: () => {
+              server.stop()
+            },
+          })
+          await proc.exited
+          await server.stop()
+        },
+      )
     },
   })
   .command(RunCommand)