index.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import "zod-openapi/extend"
  2. import yargs from "yargs"
  3. import { hideBin } from "yargs/helpers"
  4. import { RunCommand } from "./cli/cmd/run"
  5. import { GenerateCommand } from "./cli/cmd/generate"
  6. import { Log } from "./util/log"
  7. import { AuthCommand } from "./cli/cmd/auth"
  8. import { AgentCommand } from "./cli/cmd/agent"
  9. import { UpgradeCommand } from "./cli/cmd/upgrade"
  10. import { ModelsCommand } from "./cli/cmd/models"
  11. import { UI } from "./cli/ui"
  12. import { Installation } from "./installation"
  13. import { NamedError } from "./util/error"
  14. import { FormatError } from "./cli/error"
  15. import { ServeCommand } from "./cli/cmd/serve"
  16. import { TuiCommand } from "./cli/cmd/tui"
  17. import { DebugCommand } from "./cli/cmd/debug"
  18. import { StatsCommand } from "./cli/cmd/stats"
  19. import { McpCommand } from "./cli/cmd/mcp"
  20. import { GithubCommand } from "./cli/cmd/github"
  21. import { Trace } from "./trace"
  22. Trace.init()
  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.isDev(),
  52. level: (() => {
  53. if (opts.logLevel) return opts.logLevel as Log.Level
  54. if (Installation.isDev()) return "DEBUG"
  55. return "INFO"
  56. })(),
  57. })
  58. Log.Default.info("opencode", {
  59. version: Installation.VERSION,
  60. args: process.argv.slice(2),
  61. })
  62. })
  63. .usage("\n" + UI.logo())
  64. .command(McpCommand)
  65. .command(TuiCommand)
  66. .command(RunCommand)
  67. .command(GenerateCommand)
  68. .command(DebugCommand)
  69. .command(AuthCommand)
  70. .command(AgentCommand)
  71. .command(UpgradeCommand)
  72. .command(ServeCommand)
  73. .command(ModelsCommand)
  74. .command(StatsCommand)
  75. .command(GithubCommand)
  76. .fail((msg) => {
  77. if (msg.startsWith("Unknown argument") || msg.startsWith("Not enough non-option arguments")) {
  78. cli.showHelp("log")
  79. }
  80. })
  81. .strict()
  82. try {
  83. await cli.parse()
  84. } catch (e) {
  85. let data: Record<string, any> = {}
  86. if (e instanceof NamedError) {
  87. const obj = e.toObject()
  88. Object.assign(data, {
  89. ...obj.data,
  90. })
  91. }
  92. if (e instanceof Error) {
  93. Object.assign(data, {
  94. name: e.name,
  95. message: e.message,
  96. cause: e.cause?.toString(),
  97. })
  98. }
  99. if (e instanceof ResolveMessage) {
  100. Object.assign(data, {
  101. name: e.name,
  102. message: e.message,
  103. code: e.code,
  104. specifier: e.specifier,
  105. referrer: e.referrer,
  106. position: e.position,
  107. importKind: e.importKind,
  108. })
  109. }
  110. Log.Default.error("fatal", data)
  111. const formatted = FormatError(e)
  112. if (formatted) UI.error(formatted)
  113. if (formatted === undefined) UI.error("Unexpected error, check log file at " + Log.file() + " for more details")
  114. process.exitCode = 1
  115. }
  116. cancel.abort()