run.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 { cmd } from "./cmd"
  10. import { Flag } from "../../flag/flag"
  11. import { Config } from "../../config/config"
  12. const TOOL: Record<string, [string, string]> = {
  13. todowrite: ["Todo", UI.Style.TEXT_WARNING_BOLD],
  14. todoread: ["Todo", UI.Style.TEXT_WARNING_BOLD],
  15. bash: ["Bash", UI.Style.TEXT_DANGER_BOLD],
  16. edit: ["Edit", UI.Style.TEXT_SUCCESS_BOLD],
  17. glob: ["Glob", UI.Style.TEXT_INFO_BOLD],
  18. grep: ["Grep", UI.Style.TEXT_INFO_BOLD],
  19. list: ["List", UI.Style.TEXT_INFO_BOLD],
  20. read: ["Read", UI.Style.TEXT_HIGHLIGHT_BOLD],
  21. write: ["Write", UI.Style.TEXT_SUCCESS_BOLD],
  22. }
  23. export const RunCommand = cmd({
  24. command: "run [message..]",
  25. describe: "Run OpenCode with a message",
  26. builder: (yargs: Argv) => {
  27. return yargs
  28. .positional("message", {
  29. describe: "Message to send",
  30. type: "string",
  31. array: true,
  32. default: [],
  33. })
  34. .option("session", {
  35. describe: "Session ID to continue",
  36. type: "string",
  37. })
  38. .option("share", {
  39. type: "boolean",
  40. describe: "Share the session",
  41. })
  42. },
  43. handler: async (args) => {
  44. const message = args.message.join(" ")
  45. await App.provide(
  46. {
  47. cwd: process.cwd(),
  48. },
  49. async () => {
  50. await Share.init()
  51. const session = args.session
  52. ? await Session.get(args.session)
  53. : await Session.create()
  54. UI.empty()
  55. UI.println(UI.logo())
  56. UI.empty()
  57. UI.println(UI.Style.TEXT_NORMAL_BOLD + "> ", message)
  58. UI.empty()
  59. const cfg = await Config.get()
  60. if (cfg.autoshare || Flag.OPENCODE_AUTO_SHARE || args.share) {
  61. await Session.share(session.id)
  62. UI.println(
  63. UI.Style.TEXT_INFO_BOLD +
  64. "~ https://dev.opencode.ai/s/" +
  65. session.id.slice(-8),
  66. )
  67. }
  68. UI.empty()
  69. const { providerID, modelID } = await Provider.defaultModel()
  70. UI.println(
  71. UI.Style.TEXT_NORMAL_BOLD + "@ ",
  72. UI.Style.TEXT_NORMAL + `${providerID}/${modelID}`,
  73. )
  74. UI.empty()
  75. function printEvent(color: string, type: string, title: string) {
  76. UI.println(
  77. color + `|`,
  78. UI.Style.TEXT_NORMAL +
  79. UI.Style.TEXT_DIM +
  80. ` ${type.padEnd(7, " ")}`,
  81. "",
  82. UI.Style.TEXT_NORMAL + title,
  83. )
  84. }
  85. Bus.subscribe(Message.Event.PartUpdated, async (evt) => {
  86. if (evt.properties.sessionID !== session.id) return
  87. const part = evt.properties.part
  88. const message = await Session.getMessage(
  89. evt.properties.sessionID,
  90. evt.properties.messageID,
  91. )
  92. if (
  93. part.type === "tool-invocation" &&
  94. part.toolInvocation.state === "result"
  95. ) {
  96. const metadata =
  97. message.metadata.tool[part.toolInvocation.toolCallId]
  98. const [tool, color] = TOOL[part.toolInvocation.toolName] ?? [
  99. part.toolInvocation.toolName,
  100. UI.Style.TEXT_INFO_BOLD,
  101. ]
  102. printEvent(color, tool, metadata.title)
  103. }
  104. if (part.type === "text") {
  105. if (part.text.includes("\n")) {
  106. UI.empty()
  107. UI.println(part.text)
  108. UI.empty()
  109. return
  110. }
  111. printEvent(UI.Style.TEXT_NORMAL_BOLD, "Text", part.text)
  112. }
  113. })
  114. await Session.chat({
  115. sessionID: session.id,
  116. providerID,
  117. modelID,
  118. parts: [
  119. {
  120. type: "text",
  121. text: message,
  122. },
  123. ],
  124. })
  125. UI.empty()
  126. },
  127. )
  128. },
  129. })