contract.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { InvalidArgumentError, Option, type Command } from 'commander';
  2. import { parseHeadlessModelOverride } from '../headless/model-selection.js';
  3. import type { TuiMode } from '../tui/engine/public.js';
  4. import { parseTuiStartupEnvironment } from './environment.js';
  5. import type { TuiBuildEnvironment } from '../auth/environment.js';
  6. const RETIRED_TOP_LEVEL_COMMAND_NAMES = new Set(['git', 'changes', 'projects']);
  7. export interface TuiInteractiveLaunchRequest {
  8. readonly initialPrompt?: string;
  9. readonly model?: string;
  10. readonly sessionId?: string;
  11. readonly showSessionPicker?: boolean;
  12. readonly continueLatestSession?: boolean;
  13. readonly workspaceDir?: string;
  14. readonly resumeDraftAfterLogin?: boolean;
  15. readonly tuiMode?: TuiMode;
  16. readonly lane?: string;
  17. }
  18. export interface RawTuiInteractiveOptions {
  19. readonly model?: string;
  20. readonly session?: string | boolean;
  21. readonly continue?: boolean;
  22. readonly resume?: string;
  23. readonly tuiMode?: TuiMode;
  24. readonly lane?: string;
  25. readonly env?: TuiBuildEnvironment;
  26. }
  27. export function applyInteractiveCliContract(
  28. command: Command,
  29. options: { readonly allowStartupEnvironmentSelection?: boolean } = {},
  30. ): Command {
  31. const configured = command
  32. .argument('[prompt]', 'task to execute in the interactive TUI')
  33. .addOption(new Option('-m, --model <provider/model>', 'select the model for this Session only'))
  34. .addOption(new Option('--lane <lane>', 'managed backend lane for test or staging builds'))
  35. .addOption(
  36. new Option('--session [id]', 'open a Session by id, or browse Sessions when id is omitted'),
  37. )
  38. .addOption(new Option('-c, --continue', 'continue the latest Session in the current workspace'))
  39. .addOption(
  40. new Option('--tui-mode <mode>', 'TUI mode: regular (default) or fullscreen').argParser(
  41. parseTuiMode,
  42. ),
  43. )
  44. .addOption(new Option('--resume <id>').hideHelp())
  45. .allowExcessArguments(false)
  46. .showHelpAfterError();
  47. if (options.allowStartupEnvironmentSelection) {
  48. configured.addOption(
  49. new Option(
  50. '--env <environment>',
  51. 'startup environment for the internal package: test, staging (pre), or prod',
  52. ).argParser(parseStartupEnvironmentOption),
  53. );
  54. }
  55. return configured;
  56. }
  57. export function resolveInteractiveLaunchRequest(
  58. prompt: string | undefined,
  59. commandOptions: RawTuiInteractiveOptions,
  60. ): TuiInteractiveLaunchRequest {
  61. if (prompt && RETIRED_TOP_LEVEL_COMMAND_NAMES.has(prompt)) {
  62. throw new Error('too many arguments');
  63. }
  64. const explicitSessionId =
  65. typeof commandOptions.session === 'string' ? commandOptions.session.trim() : undefined;
  66. const compatibilitySessionId = commandOptions.resume?.trim();
  67. const showSessionPicker = commandOptions.session === true;
  68. const requestedModes = [
  69. Boolean(explicitSessionId || showSessionPicker),
  70. Boolean(compatibilitySessionId),
  71. commandOptions.continue === true,
  72. ].filter(Boolean).length;
  73. if (requestedModes > 1) {
  74. throw new Error('--session, --continue, and --resume cannot be combined');
  75. }
  76. const sessionId = explicitSessionId || compatibilitySessionId;
  77. const model = commandOptions.model?.trim();
  78. if (commandOptions.model !== undefined) {
  79. parseHeadlessModelOverride(model ?? '');
  80. if (showSessionPicker) {
  81. throw new Error(
  82. '--model requires a Session id with --session; use --session <id> or --continue.',
  83. );
  84. }
  85. }
  86. return {
  87. ...(prompt ? { initialPrompt: prompt } : {}),
  88. ...(model ? { model } : {}),
  89. ...(sessionId ? { sessionId } : {}),
  90. ...(showSessionPicker ? { showSessionPicker: true } : {}),
  91. ...(commandOptions.continue ? { continueLatestSession: true } : {}),
  92. ...(commandOptions.tuiMode ? { tuiMode: commandOptions.tuiMode } : {}),
  93. ...(commandOptions.lane ? { lane: commandOptions.lane } : {}),
  94. };
  95. }
  96. export function applyExecCliContract(command: Command): Command {
  97. return command
  98. .argument('[prompt]', 'task to execute')
  99. .addOption(new Option('--input <source>', 'read explicit input; only "-" is supported'))
  100. .addOption(new Option('--input-format <format>', 'input format: text or json').default('text'))
  101. .addOption(new Option('--cwd <path>', 'workspace directory'))
  102. .addOption(
  103. new Option('--file <path>', 'attach a file (repeatable)')
  104. .argParser(collectOptionValue)
  105. .default([]),
  106. )
  107. .addOption(new Option('--model <provider/model>', 'override the model for this Run only'))
  108. .addOption(new Option('--effort <level>', 'override the reasoning effort for this Run only'))
  109. .addOption(
  110. new Option('--prompt-mode <mode>', 'Prompt mode: tui, coding, or work')
  111. .choices(['tui', 'coding', 'work'])
  112. .default('tui'),
  113. )
  114. .addOption(new Option('--session <id>', 'run in an existing active Session'))
  115. .addOption(new Option('--continue', 'continue the latest active Session in --cwd'))
  116. .addOption(
  117. new Option('--config <path>', 'use an explicit Runtime config file for this process'),
  118. )
  119. .addOption(
  120. new Option(
  121. '--permission <policy>',
  122. 'permission policy: smart, full, or off (ask requires TUI/ACP)',
  123. ).default('smart'),
  124. )
  125. .addOption(new Option('--timeout <duration>', 'Run timeout, for example 30s or 2m'))
  126. .addOption(new Option('--max-steps <count>', 'maximum assistant steps'))
  127. .addOption(new Option('--output-format <format>', 'output format: text, json, or stream-json'))
  128. .addOption(
  129. new Option(
  130. '--diagnostics-dir <path>',
  131. 'save bounded execution diagnostics to a fresh directory',
  132. ),
  133. )
  134. .addOption(
  135. new Option(
  136. '--output-schema <schema>',
  137. 'JSON Schema file or inline object for the final answer',
  138. ),
  139. )
  140. .addOption(
  141. new Option('-o, --output-last-message <path>', 'write the final agent message to a file'),
  142. );
  143. }
  144. export function applyExecReviewCliContract(command: Command): Command {
  145. return command
  146. .addOption(new Option('--cwd <path>', 'workspace directory'))
  147. .addOption(new Option('--model <provider/model>', 'override the model for this Run only'))
  148. .addOption(new Option('--effort <level>', 'override the reasoning effort for this Run only'))
  149. .addOption(
  150. new Option('--config <path>', 'use an explicit Runtime config file for this process'),
  151. )
  152. .addOption(
  153. new Option(
  154. '--permission <policy>',
  155. 'permission policy: smart, full, or off (ask requires TUI/ACP)',
  156. ).default('smart'),
  157. )
  158. .addOption(new Option('--timeout <duration>', 'Run timeout, for example 30s or 2m'))
  159. .addOption(new Option('--max-steps <count>', 'maximum assistant steps'))
  160. .addOption(new Option('--output-format <format>', 'output format: text, json, or stream-json'))
  161. .addOption(
  162. new Option('-o, --output-last-message <path>', 'write the final review result to a file'),
  163. )
  164. .allowExcessArguments(false)
  165. .showHelpAfterError();
  166. }
  167. function parseTuiMode(value: string): TuiMode {
  168. if (value === 'regular' || value === 'fullscreen') return value;
  169. throw new InvalidArgumentError('TUI mode must be regular or fullscreen');
  170. }
  171. function parseStartupEnvironmentOption(value: string): TuiBuildEnvironment {
  172. try {
  173. return parseTuiStartupEnvironment(value);
  174. } catch (error) {
  175. throw new InvalidArgumentError(error instanceof Error ? error.message : String(error));
  176. }
  177. }
  178. function collectOptionValue(value: string, previous: string[]): string[] {
  179. return [...previous, value];
  180. }