Files
deepseek-harness/packages/bundle/headless/src/startup.ts
Turtle 54dd75a969 refactor(cmdline): run the program's own commander action instead of a plan callback
parseCmdline(ctx, program): void only adapts commander control flow to
the launcher: it parses the immutable cmdlineArgs snapshot and turns
help, version, parse errors, and action rejections into a ctx.appExit
request. App validation and the ctx.provide of the app-owned service
live in the program's own synchronous .action(), which commander runs
inside parse — program.error(...) there shares the exit path with a
grammar rejection. Deletes the CmdlinePlan export, its unread ctx
parameter, the type-unsound (() => ({}) as T) default, and the
T | undefined return with its per-caller publish guard.
2026-08-12 13:07:41 +08:00

58 lines
2.1 KiB
TypeScript

/**
* The one-shot app's command-line provider: it parses the task positional and
* `--help`, then publishes {@link HEADLESS_STARTUP_SERVICE}. The runner is an
* ordinary consumer whose lazy config waits for that service.
* @module @deepseek-ai/dsh-headless/startup
*/
import { Command } from 'commander'
import type { Context } from '@deepseek-ai/cordis'
import { parseCmdline } from '@deepseek-ai/dsh-cmdline'
/** Stable Cordis plugin name. */
export const name = 'headless-startup'
/** Services required before the task can be resolved. */
export const inject = ['cmdlineArgs']
/** Service provided by this plugin and injected by the one-shot runner. */
export const HEADLESS_STARTUP_SERVICE = 'headlessStartup'
/** What the runner row reads from {@link HEADLESS_STARTUP_SERVICE}. */
export interface HeadlessStartupValues {
/** The task text this invocation asked for. */
task: string
}
/**
* This app's command: the task positional, its description, and its help text.
* @returns a fresh program, so one process can parse more than once (tests).
*/
function headlessCommand(): Command {
return new Command()
.name('dsh --profile headless')
.description('Answer one task, print the final assistant message, and exit.')
.helpOption('-h, --help', 'show this help')
.argument('[task...]', 'the task text; multiple words are joined by spaces')
.addHelpText('after', `
Examples:
dsh --profile headless "run the tests" answer one task and exit
`)
}
/**
* Parse and provide the one-shot task as an ordinary Cordis service. The
* command's action publishes the task; a missing or whitespace-only task is a
* usage error, so on rejection (and on `--help`) nothing is provided.
* @param ctx - plugin context carrying the command line.
*/
export function apply(ctx: Context): void {
const program = headlessCommand()
program.action(() => {
const task = program.args.join(' ')
if (task.trim() === '') program.error('error: a task is required, for example: dsh --profile headless "run the tests"')
ctx.provide(HEADLESS_STARTUP_SERVICE, { task } satisfies HeadlessStartupValues)
})
parseCmdline(ctx, program)
}