args.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Commander adapter for the `dsh` command-line entry: the one place argv is
  3. * parsed and routed to a mode. `bin.ts` switches on the returned discriminant
  4. * and dynamic-imports that mode's module. One program: the default (no
  5. * subcommand) is the TUI/headless surface with option-only flags; `web` is a
  6. * real subcommand. Commander owns `--help`/`--version` and parse errors — it
  7. * prints and exits at the point of failure (a domain failure routes through
  8. * `command.error`), so this returns only a resolved mode.
  9. * @module @deepseek-ai/dsh/args
  10. */
  11. import { Command, CommanderError } from 'commander'
  12. /** Interactive TUI: the default mode. `--config` swaps the tree; `--resume <id>` rehydrates a session. */
  13. interface TuiInvocation {
  14. mode: 'tui'
  15. config?: string
  16. resume?: string
  17. }
  18. /** Headless one-shot: `dsh -p "task"`. */
  19. interface HeadlessInvocation {
  20. mode: 'headless'
  21. prompt: string
  22. }
  23. /**
  24. * Browser UI: `dsh web`. `host`/`port` are present only when the flag was
  25. * passed — pass-through overrides with no CLI default and no CLI validation:
  26. * the `dsh-host-webserver` schema (`host` a loopback/all-interfaces literal,
  27. * `port` a natural ≤ 65535) is the single source of both the default (the
  28. * shipped `cordis.yml` value stands when a flag is absent) and validity (a bad
  29. * value fails loud at boot). `port` is `Number`-coerced only because the schema
  30. * wants a number, not a string. `dev` mounts the client HMR driver;
  31. * `workspaceRoot` is the parent directory for name-created workspaces.
  32. */
  33. interface WebInvocation {
  34. mode: 'web'
  35. host?: string
  36. port?: number
  37. dev: boolean
  38. workspaceRoot?: string
  39. }
  40. /** The resolved `dsh` invocation: exactly one mode. `--help`/`--version`/errors exit inside {@link parseDshArgs}. */
  41. export type DshInvocation = TuiInvocation | HeadlessInvocation | WebInvocation
  42. /** Raw web-subcommand options straight from Commander. */
  43. interface WebOptions {
  44. host?: string
  45. port?: string
  46. dev?: boolean
  47. workspaceRoot?: string
  48. }
  49. /**
  50. * Narrow the raw `web` options into a {@link WebInvocation}. No host/port
  51. * validation: both flow to the webserver schema, which is the sole gate. `port`
  52. * is coerced to a number (the schema rejects a string) but not range-checked
  53. * here — `NaN`/out-of-range fail loud at the schema on boot.
  54. */
  55. function resolveWeb(options: WebOptions): WebInvocation {
  56. return {
  57. mode: 'web',
  58. ...options.host !== undefined && { host: options.host },
  59. ...options.port !== undefined && { port: Number(options.port) },
  60. dev: options.dev === true,
  61. ...options.workspaceRoot !== undefined && { workspaceRoot: options.workspaceRoot },
  62. }
  63. }
  64. /**
  65. * Resolve the raw argv into a {@link DshInvocation}, or print and exit for
  66. * `--help`/`--version`/a parse error. The default (no subcommand) is the
  67. * TUI/headless surface; `web` is a subcommand.
  68. * @param argv - the arguments after the node binary and script (`process.argv.slice(2)`).
  69. * @param version - the version string `--version` prints; read from this app's package.json.
  70. * @returns the resolved invocation (only reached on a valid, non-help invocation).
  71. */
  72. export function parseDshArgs(argv: readonly string[], version: string): DshInvocation {
  73. let resolved: DshInvocation | undefined
  74. const program = new Command()
  75. .name('dsh')
  76. .version(version, '-V, --version', 'output the version number')
  77. .description('dsh: interactive TUI (default), headless task, and browser UI')
  78. .exitOverride()
  79. // Default surface: option-only (no positional), so `web` can be a real
  80. // subcommand without a positional collision.
  81. .option('--config <path>', 'boot an alternate cordis.yml instead of the shipped tree (TUI mode)')
  82. .option('-p, --prompt <task>', 'run one headless turn for this task, print the result, and exit')
  83. .option('--resume <id>', 'resume the persisted session with this id (TUI mode)')
  84. .action((options: { config?: string; prompt?: string; resume?: string }) => {
  85. if (options.prompt !== undefined) {
  86. // A headless prompt owns the invocation; an empty task has nothing to
  87. // run, and --config/--resume are TUI inputs that must not silently
  88. // vanish from a headless run.
  89. if (options.prompt === '') program.error('error: --prompt needs a task')
  90. if (options.config !== undefined || options.resume !== undefined) {
  91. program.error('error: --prompt takes no --config or --resume')
  92. }
  93. resolved = { mode: 'headless', prompt: options.prompt }
  94. return
  95. }
  96. // An empty --resume= id would silently start a fresh session downstream
  97. // (agent-loop treats '' as no-resume), so a mistyped resume must fail loud.
  98. if (options.resume === '') program.error('error: --resume needs a session id')
  99. resolved = {
  100. mode: 'tui',
  101. ...options.config !== undefined && { config: options.config },
  102. ...options.resume !== undefined && { resume: options.resume },
  103. }
  104. })
  105. const web = program.command('web').description('serve the browser UI (host/port default to the shipped config)')
  106. web
  107. .option('--host <host>', 'override the config bind host (127.0.0.1 or 0.0.0.0)')
  108. .option('--port <port>', 'override the config listen port (0 requests an OS-assigned port)')
  109. .option('--dev', 'mount the client HMR driver and watch plugin bundles for rebuilds')
  110. .option('--workspace-root <path>', 'parent directory for name-created workspaces')
  111. .action((options: WebOptions) => {
  112. // Commander parses the parent (default-surface) options on either side of
  113. // the subcommand into `program.opts()`. `web` shares none of them, so a
  114. // leaked `--config`/`-p`/`--resume` is a mistyped invocation that must
  115. // fail loud rather than silently start the web server and drop it.
  116. const parent = program.opts<{ config?: string; prompt?: string; resume?: string }>()
  117. if (parent.config !== undefined || parent.prompt !== undefined || parent.resume !== undefined) {
  118. program.error('error: web takes none of --config, -p/--prompt, or --resume')
  119. }
  120. resolved = resolveWeb(options)
  121. })
  122. try {
  123. program.parse(argv, { from: 'user' })
  124. } catch (error) {
  125. // Commander printed help/version/the error under `exitOverride`; exit with
  126. // the code it chose (0 for help/version, 1 for a parse or domain error).
  127. /* v8 ignore next -- Commander only throws CommanderError from parse/error under exitOverride */
  128. return process.exit(error instanceof CommanderError ? error.exitCode : 1)
  129. }
  130. /* v8 ignore next -- the default action or a subcommand action always resolves, or parse throws above */
  131. if (resolved === undefined) throw new Error('dsh: no invocation resolved')
  132. return resolved
  133. }