run.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import type { Argv } from "yargs"
  2. import { App } from "../../app/app"
  3. import { Bus } from "../../bus"
  4. import { Provider } from "../../provider/provider"
  5. import { Session } from "../../session"
  6. import { Share } from "../../share/share"
  7. import { Message } from "../../session/message"
  8. import { UI } from "../ui"
  9. import { VERSION } from "../version"
  10. export const RunCommand = {
  11. command: "run [message..]",
  12. describe: "Run OpenCode with a message",
  13. builder: (yargs: Argv) => {
  14. return yargs
  15. .positional("message", {
  16. describe: "Message to send",
  17. type: "string",
  18. array: true,
  19. default: [],
  20. })
  21. .option("session", {
  22. describe: "Session ID to continue",
  23. type: "string",
  24. })
  25. },
  26. handler: async (args: {
  27. message: string[]
  28. session?: string
  29. printLogs?: boolean
  30. }) => {
  31. const message = args.message.join(" ")
  32. await App.provide(
  33. {
  34. cwd: process.cwd(),
  35. version: "0.0.0",
  36. printLogs: args.printLogs,
  37. },
  38. async () => {
  39. await Share.init()
  40. const session = args.session
  41. ? await Session.get(args.session)
  42. : await Session.create()
  43. UI.print(UI.Style.TEXT_HIGHLIGHT_BOLD + "◍ OpenCode", VERSION)
  44. UI.empty()
  45. UI.print(UI.Style.TEXT_NORMAL_BOLD + "> ", message)
  46. UI.empty()
  47. UI.print(
  48. UI.Style.TEXT_INFO_BOLD +
  49. "~ https://dev.opencode.ai/s?id=" +
  50. session.id.slice(-8),
  51. )
  52. UI.empty()
  53. function printEvent(color: string, type: string, title: string) {
  54. UI.print(
  55. color + `|`,
  56. UI.Style.TEXT_NORMAL +
  57. UI.Style.TEXT_DIM +
  58. ` ${type.padEnd(7, " ")}`,
  59. "",
  60. UI.Style.TEXT_NORMAL + title,
  61. )
  62. }
  63. Bus.subscribe(Message.Event.PartUpdated, async (message) => {
  64. const part = message.properties.part
  65. if (
  66. part.type === "tool-invocation" &&
  67. part.toolInvocation.state === "result"
  68. ) {
  69. if (part.toolInvocation.toolName === "opencode_todowrite") return
  70. const args = part.toolInvocation.args as any
  71. const tool = part.toolInvocation.toolName
  72. if (tool === "opencode_edit")
  73. printEvent(UI.Style.TEXT_SUCCESS_BOLD, "Edit", args.filePath)
  74. if (tool === "opencode_bash")
  75. printEvent(UI.Style.TEXT_WARNING_BOLD, "Execute", args.command)
  76. if (tool === "opencode_read")
  77. printEvent(UI.Style.TEXT_INFO_BOLD, "Read", args.filePath)
  78. if (tool === "opencode_write")
  79. printEvent(UI.Style.TEXT_SUCCESS_BOLD, "Create", args.filePath)
  80. if (tool === "opencode_glob")
  81. printEvent(
  82. UI.Style.TEXT_INFO_BOLD,
  83. "Glob",
  84. args.pattern + (args.path ? " in " + args.path : ""),
  85. )
  86. }
  87. if (part.type === "text") {
  88. if (part.text.includes("\n")) {
  89. UI.empty()
  90. UI.print(part.text)
  91. UI.empty()
  92. return
  93. }
  94. printEvent(UI.Style.TEXT_NORMAL_BOLD, "Text", part.text)
  95. }
  96. })
  97. const { providerID, modelID } = await Provider.defaultModel()
  98. await Session.chat({
  99. sessionID: session.id,
  100. providerID,
  101. modelID,
  102. parts: [
  103. {
  104. type: "text",
  105. text: message,
  106. },
  107. ],
  108. })
  109. UI.empty()
  110. },
  111. )
  112. },
  113. }