index.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import yargs from "yargs"
  2. import { hideBin } from "yargs/helpers"
  3. import { RunCommand } from "./cli/cmd/run"
  4. import { GenerateCommand } from "./cli/cmd/generate"
  5. import { Log } from "./util/log"
  6. import { AuthCommand } from "./cli/cmd/auth"
  7. import { AgentCommand } from "./cli/cmd/agent"
  8. import { UpgradeCommand } from "./cli/cmd/upgrade"
  9. import { ModelsCommand } from "./cli/cmd/models"
  10. import { UI } from "./cli/ui"
  11. import { Installation } from "./installation"
  12. import { NamedError } from "@opencode-ai/util/error"
  13. import { FormatError } from "./cli/error"
  14. import { ServeCommand } from "./cli/cmd/serve"
  15. import { DebugCommand } from "./cli/cmd/debug"
  16. import { StatsCommand } from "./cli/cmd/stats"
  17. import { McpCommand } from "./cli/cmd/mcp"
  18. import { GithubCommand } from "./cli/cmd/github"
  19. import { ExportCommand } from "./cli/cmd/export"
  20. import { ImportCommand } from "./cli/cmd/import"
  21. import { AttachCommand } from "./cli/cmd/tui/attach"
  22. import { TuiThreadCommand } from "./cli/cmd/tui/thread"
  23. import { TuiSpawnCommand } from "./cli/cmd/tui/spawn"
  24. import { AcpCommand } from "./cli/cmd/acp"
  25. import { EOL } from "os"
  26. import { WebCommand } from "./cli/cmd/web"
  27. import { PrCommand } from "./cli/cmd/pr"
  28. import { SessionCommand } from "./cli/cmd/session"
  29. process.on("unhandledRejection", (e) => {
  30. Log.Default.error("rejection", {
  31. e: e instanceof Error ? e.message : e,
  32. })
  33. })
  34. process.on("uncaughtException", (e) => {
  35. Log.Default.error("exception", {
  36. e: e instanceof Error ? e.message : e,
  37. })
  38. })
  39. const cli = yargs(hideBin(process.argv))
  40. .parserConfiguration({ "populate--": true })
  41. .scriptName("opencode")
  42. .help("help", "show help")
  43. .alias("help", "h")
  44. .version("version", "show version number", Installation.VERSION)
  45. .alias("version", "v")
  46. .option("print-logs", {
  47. describe: "print logs to stderr",
  48. type: "boolean",
  49. })
  50. .option("log-level", {
  51. describe: "log level",
  52. type: "string",
  53. choices: ["DEBUG", "INFO", "WARN", "ERROR"],
  54. })
  55. .middleware(async (opts) => {
  56. await Log.init({
  57. print: process.argv.includes("--print-logs"),
  58. dev: Installation.isLocal(),
  59. level: (() => {
  60. if (opts.logLevel) return opts.logLevel as Log.Level
  61. if (Installation.isLocal()) return "DEBUG"
  62. return "INFO"
  63. })(),
  64. })
  65. process.env.AGENT = "1"
  66. process.env.OPENCODE = "1"
  67. Log.Default.info("opencode", {
  68. version: Installation.VERSION,
  69. args: process.argv.slice(2),
  70. })
  71. })
  72. .usage("\n" + UI.logo())
  73. .command(AcpCommand)
  74. .command(McpCommand)
  75. .command(TuiThreadCommand)
  76. .command(TuiSpawnCommand)
  77. .command(AttachCommand)
  78. .command(RunCommand)
  79. .command(GenerateCommand)
  80. .command(DebugCommand)
  81. .command(AuthCommand)
  82. .command(AgentCommand)
  83. .command(UpgradeCommand)
  84. .command(ServeCommand)
  85. .command(WebCommand)
  86. .command(ModelsCommand)
  87. .command(StatsCommand)
  88. .command(ExportCommand)
  89. .command(ImportCommand)
  90. .command(GithubCommand)
  91. .command(PrCommand)
  92. .command(SessionCommand)
  93. .fail((msg) => {
  94. if (
  95. msg.startsWith("Unknown argument") ||
  96. msg.startsWith("Not enough non-option arguments") ||
  97. msg.startsWith("Invalid values:")
  98. ) {
  99. cli.showHelp("log")
  100. }
  101. process.exit(1)
  102. })
  103. .strict()
  104. try {
  105. await cli.parse()
  106. } catch (e) {
  107. let data: Record<string, any> = {}
  108. if (e instanceof NamedError) {
  109. const obj = e.toObject()
  110. Object.assign(data, {
  111. ...obj.data,
  112. })
  113. }
  114. if (e instanceof Error) {
  115. Object.assign(data, {
  116. name: e.name,
  117. message: e.message,
  118. cause: e.cause?.toString(),
  119. stack: e.stack,
  120. })
  121. }
  122. if (e instanceof ResolveMessage) {
  123. Object.assign(data, {
  124. name: e.name,
  125. message: e.message,
  126. code: e.code,
  127. specifier: e.specifier,
  128. referrer: e.referrer,
  129. position: e.position,
  130. importKind: e.importKind,
  131. })
  132. }
  133. Log.Default.error("fatal", data)
  134. const formatted = FormatError(e)
  135. if (formatted) UI.error(formatted)
  136. if (formatted === undefined) {
  137. UI.error("Unexpected error, check log file at " + Log.file() + " for more details" + EOL)
  138. console.error(e)
  139. }
  140. process.exitCode = 1
  141. } finally {
  142. // Some subprocesses don't react properly to SIGTERM and similar signals.
  143. // Most notably, some docker-container-based MCP servers don't handle such signals unless
  144. // run using `docker run --init`.
  145. // Explicitly exit to avoid any hanging subprocesses.
  146. process.exit()
  147. }