run.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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: VERSION,
  36. },
  37. async () => {
  38. await Share.init()
  39. const session = args.session
  40. ? await Session.get(args.session)
  41. : await Session.create()
  42. UI.println(UI.Style.TEXT_HIGHLIGHT_BOLD + "◍ OpenCode", VERSION)
  43. UI.empty()
  44. UI.println(UI.Style.TEXT_NORMAL_BOLD + "> ", message)
  45. UI.empty()
  46. UI.println(
  47. UI.Style.TEXT_INFO_BOLD +
  48. "~ https://dev.opencode.ai/s/" +
  49. session.id.slice(-8),
  50. )
  51. UI.empty()
  52. function printEvent(color: string, type: string, title: string) {
  53. UI.println(
  54. color + `|`,
  55. UI.Style.TEXT_NORMAL +
  56. UI.Style.TEXT_DIM +
  57. ` ${type.padEnd(7, " ")}`,
  58. "",
  59. UI.Style.TEXT_NORMAL + title,
  60. )
  61. }
  62. Bus.subscribe(Message.Event.PartUpdated, async (message) => {
  63. const part = message.properties.part
  64. if (
  65. part.type === "tool-invocation" &&
  66. part.toolInvocation.state === "result"
  67. ) {
  68. if (part.toolInvocation.toolName === "opencode_todowrite") return
  69. const args = part.toolInvocation.args as any
  70. const tool = part.toolInvocation.toolName
  71. if (tool === "opencode_edit")
  72. printEvent(UI.Style.TEXT_SUCCESS_BOLD, "Edit", args.filePath)
  73. if (tool === "opencode_bash")
  74. printEvent(UI.Style.TEXT_WARNING_BOLD, "Execute", args.command)
  75. if (tool === "opencode_read")
  76. printEvent(UI.Style.TEXT_INFO_BOLD, "Read", args.filePath)
  77. if (tool === "opencode_write")
  78. printEvent(UI.Style.TEXT_SUCCESS_BOLD, "Create", args.filePath)
  79. if (tool === "opencode_list")
  80. printEvent(UI.Style.TEXT_INFO_BOLD, "List", args.path)
  81. if (tool === "opencode_glob")
  82. printEvent(
  83. UI.Style.TEXT_INFO_BOLD,
  84. "Glob",
  85. args.pattern + (args.path ? " in " + args.path : ""),
  86. )
  87. }
  88. if (part.type === "text") {
  89. if (part.text.includes("\n")) {
  90. UI.empty()
  91. UI.println(part.text)
  92. UI.empty()
  93. return
  94. }
  95. printEvent(UI.Style.TEXT_NORMAL_BOLD, "Text", part.text)
  96. }
  97. })
  98. const { providerID, modelID } = await Provider.defaultModel()
  99. await Session.chat({
  100. sessionID: session.id,
  101. providerID,
  102. modelID,
  103. parts: [
  104. {
  105. type: "text",
  106. text: message,
  107. },
  108. ],
  109. })
  110. UI.empty()
  111. },
  112. )
  113. },
  114. }