Files
deepseek-harness/packages/bundle/web-app/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

81 lines
3.1 KiB
TypeScript

/**
* The web app's command-line provider: it parses the `dsh --profile web` flag
* family (`--host`, `--port`, `--trusted-host`) and its `--help`
* text, then provides the immutable values as {@link WEB_STARTUP_SERVICE}.
* Ordinary rows inject that service before reading it from lazy config.
* @module @deepseek-ai/dsh-web-app/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 = 'web-startup'
/** Services required before the flags can be resolved. */
export const inject = ['cmdlineArgs']
/** Service provided by this ordinary plugin and injected by flag-configured rows. */
export const WEB_STARTUP_SERVICE = 'webStartup'
/** What the web rows read from {@link WEB_STARTUP_SERVICE}. */
export interface WebStartupValues {
/** `--host`, absent when the invocation did not name one. */
host?: string
/** `--port`, absent when the invocation did not name one. */
port?: number
/** Explicit `--trusted-host` authorities, in argument order. */
trustedHosts: string[]
}
/** The web flag family, as commander parsed it. */
interface WebOptions {
host?: string
port?: string
trustedHost?: string[]
}
/**
* This app's command: its flags, its description, and its help text.
* @returns a fresh program, so one process can parse more than once (tests).
*/
function webCommand(): Command {
return new Command()
.name('dsh --profile web')
.description('Serve the DeepSeek Harness browser UI.')
.helpOption('-h, --help', 'show this help')
.option('--host <host>', 'bind host; pass 0.0.0.0 to reach it from another machine')
.option('--port <port>', 'listen port; pass 0 to let the OS pick a free one')
.option('--trusted-host <authority...>', 'extra authority the /api browser-trust fence accepts (host or host:port; repeatable)')
.addHelpText('after', `
Examples:
dsh --profile web serve on the composed host and port
dsh --profile web --port 8080 serve on another port
dsh --profile web --host 0.0.0.0 reach it from another machine on the LAN
`)
}
/**
* Parse and provide the Web invocation as an ordinary Cordis service. The
* command's action publishes the flags this invocation named; a non-numeric
* `--port` 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 = webCommand()
program.action(() => {
const options = program.opts<WebOptions>()
if (options.port !== undefined && !/^\d+$/.test(options.port)) {
program.error(`error: --port must be a number, got ${JSON.stringify(options.port)}`)
}
ctx.provide(WEB_STARTUP_SERVICE, {
...options.host !== undefined && { host: options.host },
...options.port !== undefined && { port: Number(options.port) },
trustedHosts: options.trustedHost ?? [],
} satisfies WebStartupValues)
})
parseCmdline(ctx, program)
}