summaryrefslogtreecommitdiffhomepage
path: root/packages/opencode/src/cli/cmd/run.ts
blob: 190796ba6a3e1b5190670ac935e278016184e2a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import type { Argv } from "yargs"
import { App } from "../../app/app"
import { Bus } from "../../bus"
import { Provider } from "../../provider/provider"
import { Session } from "../../session"
import { Share } from "../../share/share"
import { Message } from "../../session/message"
import { UI } from "../ui"
import { VERSION } from "../version"

export const RunCommand = {
  command: "run [message..]",
  describe: "Run OpenCode with a message",
  builder: (yargs: Argv) => {
    return yargs
      .positional("message", {
        describe: "Message to send",
        type: "string",
        array: true,
        default: [],
      })
      .option("session", {
        describe: "Session ID to continue",
        type: "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()

        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(
            color + `|`,
            UI.Style.TEXT_NORMAL +
              UI.Style.TEXT_DIM +
              ` ${type.padEnd(7, " ")}`,
            "",
            UI.Style.TEXT_NORMAL + title,
          )
        }

        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

            const args = part.toolInvocation.args as any
            const tool = part.toolInvocation.toolName

            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 : ""),
              )
          }

          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()
      },
    )
  },
}