index.ts 4.3 KB

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