-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Expand file tree
/
Copy pathstartup.ts
More file actions
57 lines (50 loc) · 2.12 KB
/
Copy pathstartup.ts
File metadata and controls
57 lines (50 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* 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)
}