refactor(cmdline): make command providers ordinary

This commit is contained in:
Turtle
2026-08-10 23:45:05 +08:00
parent 668bdb3d8e
commit 09e2d2ddc1
46 files changed
+400 -742

No files matched your search

+1 -1
View File
@@ -25,7 +25,7 @@ export const name = 'headless-runner'
/** Core services required before the one-shot turn can start. */
export const inject = ['agentDefaultModel', 'agents', 'sessions']
/** Plugin config: the task resolved from this app's injected startup service. */
/** Plugin config: the task resolved from this app's injected provider service. */
export interface Config {
/** The prompt text for the single run. */
task: string
+14 -25
View File
@@ -1,16 +1,13 @@
/**
* The one-shot app's startup row: it owns the `dsh --profile headless` command
* line — the task text is this command's positional argument — and its
* `--help` text, then provides {@link HEADLESS_STARTUP_SERVICE} with the task
* the user asked for. The runner waits for it, so a missing task is a usage
* error printed by this command instead of a schema failure inside the runner.
* 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 'cordis'
import type { EntryOptions } from '@cordisjs/plugin-loader'
import { runStartup } from '@deepseek-ai/dsh-cmdline'
import { parseCmdline } from '@deepseek-ai/dsh-cmdline'
/** Stable Cordis plugin name. */
export const name = 'headless-startup'
@@ -18,12 +15,9 @@ export const name = 'headless-startup'
/** Services required before the task can be resolved. */
export const inject = ['cmdlineArgs']
/** The service this row provides and the one-shot runner row reads. */
/** Service provided by this plugin and injected by the one-shot runner. */
export const HEADLESS_STARTUP_SERVICE = 'headlessStartup'
/** The row that runs the task, and the only reason this app has a command line. */
const RUNNER_ROW_ID = 'headless-runner'
/** What the runner row reads from {@link HEADLESS_STARTUP_SERVICE}. */
export interface HeadlessStartupValues {
/** The task text this invocation asked for. */
@@ -47,27 +41,22 @@ Examples:
}
/**
* Turn the parsed command line into the runner row's task.
* Turn the parsed command line into the runner's task.
* @param program - the parsed headless command.
* @param rows - the rows waiting on this app's service, in tree order.
* @returns the runner row's service value.
* @throws when the composition has no runner row, which would otherwise accept
* a task and silently run nothing.
* @returns the runner's service value.
*/
function planHeadlessStartup(program: Command, rows: readonly EntryOptions[]): HeadlessStartupValues {
function planHeadlessStartup(program: Command): HeadlessStartupValues {
const task = program.args.join(' ')
if (task === '') program.error('error: a task is required, for example: dsh --profile headless "run the tests"')
if (!rows.some(row => row.id === RUNNER_ROW_ID)) {
throw new Error(`headless-startup: the composition has no waiting "${RUNNER_ROW_ID}" row to run the task`)
}
if (task.trim() === '') program.error('error: a task is required, for example: dsh --profile headless "run the tests"')
return { task }
}
/**
* Resolve the task for the runner waiting on `headlessStartup`.
* @param ctx - plugin context carrying the command line and the Loader.
* @returns nothing once the runner is started, or once `--help` or a missing task requested exit.
* Parse and provide the one-shot task as an ordinary Cordis service.
* @param ctx - plugin context carrying the command line.
* @returns nothing once the task is provided, or when the command requested exit.
*/
export function apply(ctx: Context): void {
runStartup(ctx, HEADLESS_STARTUP_SERVICE, headlessCommand(), planHeadlessStartup)
const values = parseCmdline(ctx, headlessCommand(), planHeadlessStartup)
if (values !== undefined) ctx.provide(HEADLESS_STARTUP_SERVICE, values)
}