startup.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * The one-shot app's command-line provider: it parses the task positional and
  3. * `--help`, then publishes {@link HEADLESS_STARTUP_SERVICE}. The runner is an
  4. * ordinary consumer whose lazy config waits for that service.
  5. * @module @deepseek-ai/dsh-headless/startup
  6. */
  7. import { Command } from 'commander'
  8. import type { Context } from '@deepseek-ai/cordis'
  9. import { parseCmdline } from '@deepseek-ai/dsh-cmdline'
  10. /** Stable Cordis plugin name. */
  11. export const name = 'headless-startup'
  12. /** Services required before the task can be resolved. */
  13. export const inject = ['cmdlineArgs']
  14. /** Service provided by this plugin and injected by the one-shot runner. */
  15. export const HEADLESS_STARTUP_SERVICE = 'headlessStartup'
  16. /** What the runner row reads from {@link HEADLESS_STARTUP_SERVICE}. */
  17. export interface HeadlessStartupValues {
  18. /** The task text this invocation asked for. */
  19. task: string
  20. }
  21. /**
  22. * This app's command: the task positional, its description, and its help text.
  23. * @returns a fresh program, so one process can parse more than once (tests).
  24. */
  25. function headlessCommand(): Command {
  26. return new Command()
  27. .name('dsh --profile headless')
  28. .description('Answer one task, print the final assistant message, and exit.')
  29. .helpOption('-h, --help', 'show this help')
  30. .argument('[task...]', 'the task text; multiple words are joined by spaces')
  31. .addHelpText('after', `
  32. Examples:
  33. dsh --profile headless "run the tests" answer one task and exit
  34. `)
  35. }
  36. /**
  37. * Turn the parsed command line into the runner's task.
  38. * @param program - the parsed headless command.
  39. * @returns the runner's service value.
  40. */
  41. function planHeadlessStartup(program: Command): HeadlessStartupValues {
  42. const task = program.args.join(' ')
  43. if (task.trim() === '') program.error('error: a task is required, for example: dsh --profile headless "run the tests"')
  44. return { task }
  45. }
  46. /**
  47. * Parse and provide the one-shot task as an ordinary Cordis service.
  48. * @param ctx - plugin context carrying the command line.
  49. * @returns nothing once the task is provided, or when the command requested exit.
  50. */
  51. export function apply(ctx: Context): void {
  52. const values = parseCmdline(ctx, headlessCommand(), planHeadlessStartup)
  53. if (values !== undefined) ctx.provide(HEADLESS_STARTUP_SERVICE, values)
  54. }