index.ts 4.2 KB

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