startup.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. * Parse and provide the one-shot task as an ordinary Cordis service. The
  38. * command's action publishes the task; a missing or whitespace-only task is a
  39. * usage error, so on rejection (and on `--help`) nothing is provided.
  40. * @param ctx - plugin context carrying the command line.
  41. */
  42. export function apply(ctx: Context): void {
  43. const program = headlessCommand()
  44. program.action(() => {
  45. const task = program.args.join(' ')
  46. if (task.trim() === '') program.error('error: a task is required, for example: dsh --profile headless "run the tests"')
  47. ctx.provide(HEADLESS_STARTUP_SERVICE, { task } satisfies HeadlessStartupValues)
  48. })
  49. parseCmdline(ctx, program)
  50. }