index.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 { TuiCommand } from "./cli/cmd/tui"
  16. import { DebugCommand } from "./cli/cmd/debug"
  17. import { StatsCommand } from "./cli/cmd/stats"
  18. import { McpCommand } from "./cli/cmd/mcp"
  19. import { GithubCommand } from "./cli/cmd/github"
  20. import { ExportCommand } from "./cli/cmd/export"
  21. import { AttachCommand } from "./cli/cmd/attach"
  22. import { AcpCommand } from "./cli/cmd/acp"
  23. const cancel = new AbortController()
  24. process.on("unhandledRejection", (e) => {
  25. Log.Default.error("rejection", {
  26. e: e instanceof Error ? e.message : e,
  27. })
  28. })
  29. process.on("uncaughtException", (e) => {
  30. Log.Default.error("exception", {
  31. e: e instanceof Error ? e.message : e,
  32. })
  33. })
  34. const cli = yargs(hideBin(process.argv))
  35. .scriptName("opencode")
  36. .help("help", "show help")
  37. .version("version", "show version number", Installation.VERSION)
  38. .alias("version", "v")
  39. .option("print-logs", {
  40. describe: "print logs to stderr",
  41. type: "boolean",
  42. })
  43. .option("log-level", {
  44. describe: "log level",
  45. type: "string",
  46. choices: ["DEBUG", "INFO", "WARN", "ERROR"],
  47. })
  48. .middleware(async (opts) => {
  49. await Log.init({
  50. print: process.argv.includes("--print-logs"),
  51. dev: Installation.isLocal(),
  52. level: (() => {
  53. if (opts.logLevel) return opts.logLevel as Log.Level
  54. if (Installation.isLocal()) return "DEBUG"
  55. return "INFO"
  56. })(),
  57. })
  58. process.env["OPENCODE"] = "1"
  59. Log.Default.info("opencode", {
  60. version: Installation.VERSION,
  61. args: process.argv.slice(2),
  62. })
  63. })
  64. .usage("\n" + UI.logo())
  65. .command(AcpCommand)
  66. .command(McpCommand)
  67. .command(TuiCommand)
  68. .command(AttachCommand)
  69. .command(RunCommand)
  70. .command(GenerateCommand)
  71. .command(DebugCommand)
  72. .command(AuthCommand)
  73. .command(AgentCommand)
  74. .command(UpgradeCommand)
  75. .command(ServeCommand)
  76. .command(ModelsCommand)
  77. .command(StatsCommand)
  78. .command(ExportCommand)
  79. .command(GithubCommand)
  80. .fail((msg) => {
  81. if (
  82. msg.startsWith("Unknown argument") ||
  83. msg.startsWith("Not enough non-option arguments") ||
  84. msg.startsWith("Invalid values:")
  85. ) {
  86. cli.showHelp("log")
  87. }
  88. process.exit(1)
  89. })
  90. .strict()
  91. try {
  92. await cli.parse()
  93. } catch (e) {
  94. let data: Record<string, any> = {}
  95. if (e instanceof NamedError) {
  96. const obj = e.toObject()
  97. Object.assign(data, {
  98. ...obj.data,
  99. })
  100. }
  101. if (e instanceof Error) {
  102. Object.assign(data, {
  103. name: e.name,
  104. message: e.message,
  105. cause: e.cause?.toString(),
  106. stack: e.stack,
  107. })
  108. }
  109. if (e instanceof ResolveMessage) {
  110. Object.assign(data, {
  111. name: e.name,
  112. message: e.message,
  113. code: e.code,
  114. specifier: e.specifier,
  115. referrer: e.referrer,
  116. position: e.position,
  117. importKind: e.importKind,
  118. })
  119. }
  120. Log.Default.error("fatal", data)
  121. const formatted = FormatError(e)
  122. if (formatted) UI.error(formatted)
  123. if (formatted === undefined) {
  124. UI.error("Unexpected error, check log file at " + Log.file() + " for more details\n")
  125. console.error(e)
  126. }
  127. process.exitCode = 1
  128. }
  129. cancel.abort()