index.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 { 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. const cancel = new AbortController()
  18. process.on("unhandledRejection", (e) => {
  19. Log.Default.error("rejection", {
  20. e: e instanceof Error ? e.message : e,
  21. })
  22. })
  23. process.on("uncaughtException", (e) => {
  24. Log.Default.error("exception", {
  25. e: e instanceof Error ? e.message : e,
  26. })
  27. })
  28. const cli = yargs(hideBin(process.argv))
  29. .scriptName("opencode")
  30. .help("help", "show help")
  31. .version("version", "show version number", Installation.VERSION)
  32. .alias("version", "v")
  33. .option("print-logs", {
  34. describe: "print logs to stderr",
  35. type: "boolean",
  36. })
  37. .middleware(async () => {
  38. await Log.init({ print: process.argv.includes("--print-logs") })
  39. try {
  40. const { Config } = await import("./config/config")
  41. const { App } = await import("./app/app")
  42. App.provide({ cwd: process.cwd() }, async () => {
  43. const cfg = await Config.get()
  44. if (cfg.log_level) {
  45. Log.setLevel(cfg.log_level as Log.Level)
  46. } else {
  47. const defaultLevel = Installation.isDev() ? "DEBUG" : "INFO"
  48. Log.setLevel(defaultLevel)
  49. }
  50. })
  51. } catch (e) {
  52. Log.Default.error("failed to load config", { error: e })
  53. }
  54. Log.Default.info("opencode", {
  55. version: Installation.VERSION,
  56. args: process.argv.slice(2),
  57. })
  58. })
  59. .usage("\n" + UI.logo())
  60. .command(TuiCommand)
  61. .command(RunCommand)
  62. .command(GenerateCommand)
  63. .command(DebugCommand)
  64. .command(AuthCommand)
  65. .command(UpgradeCommand)
  66. .command(ServeCommand)
  67. .command(ModelsCommand)
  68. .fail((msg) => {
  69. if (msg.startsWith("Unknown argument") || msg.startsWith("Not enough non-option arguments")) {
  70. cli.showHelp("log")
  71. }
  72. })
  73. .strict()
  74. try {
  75. await cli.parse()
  76. } catch (e) {
  77. let data: Record<string, any> = {}
  78. if (e instanceof NamedError) {
  79. const obj = e.toObject()
  80. Object.assign(data, {
  81. ...obj.data,
  82. })
  83. }
  84. if (e instanceof Error) {
  85. Object.assign(data, {
  86. name: e.name,
  87. message: e.message,
  88. cause: e.cause?.toString(),
  89. })
  90. }
  91. if (e instanceof ResolveMessage) {
  92. Object.assign(data, {
  93. name: e.name,
  94. message: e.message,
  95. code: e.code,
  96. specifier: e.specifier,
  97. referrer: e.referrer,
  98. position: e.position,
  99. importKind: e.importKind,
  100. })
  101. }
  102. Log.Default.error("fatal", data)
  103. const formatted = FormatError(e)
  104. if (formatted) UI.error(formatted)
  105. if (formatted === undefined) UI.error("Unexpected error, check log file at " + Log.file() + " for more details")
  106. process.exitCode = 1
  107. }
  108. cancel.abort()