args.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /** The loopback host `dsh web` binds by default. */
  13. export const LOOPBACK_HOST = '127.0.0.1'
  14. /** The all-interfaces host `dsh web` accepts to expose the UI on the LAN. */
  15. export const ALL_INTERFACES_HOST = '0.0.0.0'
  16. /** Interactive TUI: the default mode. `--config` swaps the tree; `--resume <id>` rehydrates a session. */
  17. interface TuiInvocation {
  18. mode: 'tui'
  19. config?: string
  20. resume?: string
  21. }
  22. /** Headless one-shot: `dsh -p "task"`. */
  23. interface HeadlessInvocation {
  24. mode: 'headless'
  25. prompt: string
  26. }
  27. /**
  28. * Browser UI: `dsh web`. `host`/`port` are present only when the flag was
  29. * passed (validated: host is loopback/all-interfaces, port a 0–65535 integer);
  30. * absent means the shipped `cordis.yml` default stands, so the yml is the sole
  31. * source of the default. `dev` mounts the client HMR driver.
  32. */
  33. interface WebInvocation {
  34. mode: 'web'
  35. host?: string
  36. port?: number
  37. dev: boolean
  38. }
  39. /** The resolved `dsh` invocation: exactly one mode. `--help`/`--version`/errors exit inside {@link parseDshArgs}. */
  40. export type DshInvocation = TuiInvocation | HeadlessInvocation | WebInvocation
  41. /** Raw web-subcommand options before validation. */
  42. interface WebOptions {
  43. host?: string
  44. port?: string
  45. dev?: boolean
  46. }
  47. /** Validate and narrow the raw `web` options; a bad value fails loud via `command.error`. */
  48. function resolveWeb(command: Command, options: WebOptions): WebInvocation {
  49. if (options.host !== undefined && options.host !== LOOPBACK_HOST && options.host !== ALL_INTERFACES_HOST) {
  50. command.error(`error: --host must be ${LOOPBACK_HOST} or ${ALL_INTERFACES_HOST}`)
  51. }
  52. let port: number | undefined
  53. if (options.port !== undefined) {
  54. port = Number(options.port)
  55. if (!/^\d+$/.test(options.port) || !Number.isInteger(port) || port > 65535) {
  56. command.error('error: --port must be an integer in 0-65535')
  57. }
  58. }
  59. return {
  60. mode: 'web',
  61. ...options.host !== undefined && { host: options.host },
  62. ...port !== undefined && { port },
  63. dev: options.dev === true,
  64. }
  65. }
  66. /**
  67. * Resolve the raw argv into a {@link DshInvocation}, or print and exit for
  68. * `--help`/`--version`/a parse error. The default (no subcommand) is the
  69. * TUI/headless surface; `web` is a subcommand.
  70. * @param argv - the arguments after the node binary and script (`process.argv.slice(2)`).
  71. * @param version - the version string `--version` prints; read from this app's package.json.
  72. * @returns the resolved invocation (only reached on a valid, non-help invocation).
  73. */
  74. export function parseDshArgs(argv: readonly string[], version: string): DshInvocation {
  75. let resolved: DshInvocation | undefined
  76. const program = new Command()
  77. .name('dsh')
  78. .version(version, '-V, --version', 'output the version number')
  79. .description('dsh: interactive TUI (default), headless task, and browser UI')
  80. .exitOverride()
  81. // Default surface: option-only (no positional), so `web` can be a real
  82. // subcommand without a positional collision.
  83. .option('--config <path>', 'boot an alternate cordis.yml instead of the shipped tree (TUI mode)')
  84. .option('-p, --prompt <task>', 'run one headless turn for this task, print the result, and exit')
  85. .option('--resume <id>', 'resume the persisted session with this id (TUI mode)')
  86. .action((options: { config?: string; prompt?: string; resume?: string }) => {
  87. if (options.prompt !== undefined) {
  88. // A headless prompt owns the invocation; an empty task has nothing to
  89. // run, and --config/--resume are TUI inputs that must not silently
  90. // vanish from a headless run.
  91. if (options.prompt === '') program.error('error: --prompt needs a task')
  92. if (options.config !== undefined || options.resume !== undefined) {
  93. program.error('error: --prompt takes no --config or --resume')
  94. }
  95. resolved = { mode: 'headless', prompt: options.prompt }
  96. return
  97. }
  98. // An empty --resume= id would silently start a fresh session downstream
  99. // (agent-loop treats '' as no-resume), so a mistyped resume must fail loud.
  100. if (options.resume === '') program.error('error: --resume needs a session id')
  101. resolved = {
  102. mode: 'tui',
  103. ...options.config !== undefined && { config: options.config },
  104. ...options.resume !== undefined && { resume: options.resume },
  105. }
  106. })
  107. const web = program.command('web').description('serve the browser UI (host/port default to the shipped config)')
  108. web
  109. .option('--host <host>', `bind host (${LOOPBACK_HOST} or ${ALL_INTERFACES_HOST})`)
  110. .option('--port <port>', 'listen port (0 requests an OS-assigned port)')
  111. .option('--dev', 'mount the client HMR driver and watch plugin bundles for rebuilds')
  112. .action((options: WebOptions) => { resolved = resolveWeb(web, options) })
  113. try {
  114. program.parse(argv, { from: 'user' })
  115. } catch (error) {
  116. // Commander printed help/version/the error under `exitOverride`; exit with
  117. // the code it chose (0 for help/version, 1 for a parse or domain error).
  118. /* v8 ignore next -- Commander only throws CommanderError from parse/error under exitOverride */
  119. return process.exit(error instanceof CommanderError ? error.exitCode : 1)
  120. }
  121. /* v8 ignore next -- the default action or a subcommand action always resolves, or parse throws above */
  122. if (resolved === undefined) throw new Error('dsh: no invocation resolved')
  123. return resolved
  124. }