app-cli-entry.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /**
  2. * AppCLIEntry — the pre-cordis boot glue the config-tree dsh surfaces share
  3. * for the Web/headless surface.
  4. * Everything here is what must exist before the Loader runs: the patch
  5. * composition over the shipped base and surface overlay (profile json + CLI
  6. * flags + the resolved frontend dist), and the fail-loud triple after the tree
  7. * settles. The environment is what the bin already loaded (ambient plus the
  8. * invoking directory's `.env`); `$DSH_HOME/.env` belongs to the credential
  9. * provider and is never hoisted here.
  10. */
  11. import { readFileSync } from 'node:fs'
  12. import { createRequire } from 'node:module'
  13. import { networkInterfaces } from 'node:os'
  14. import { join, resolve } from 'node:path'
  15. import { Context } from 'cordis'
  16. import type { PatchOptions } from '@cordisjs/plugin-include'
  17. import yaml from 'js-yaml'
  18. import { boot, installFailLoud, loadOverlayPatches, loadPersonalPatches } from '@deepseek-ai/dsh-app-boot'
  19. // Empty type import carries the httpServer Context merge for the port read below.
  20. import type {} from '@deepseek-ai/dsh-host-webserver'
  21. /** Profile file under the invoking directory (read-only this round; never created — see the design's profile ruling). */
  22. const PROFILE_DIR = '.dsh-tmp-profile'
  23. const PROFILE_FILE = 'config.json'
  24. /** The session-telemetry row id the DSH_TELEMETRY_DISABLED switch targets (mounted in web.cordis.yml). */
  25. const TELEMETRY_ROW_ID = 'telemetry-otel'
  26. /** The webserver schema's all-interfaces bind literal: gates LAN-authority derivation here and the printed LAN URL in web.ts. */
  27. const ALL_INTERFACES_HOST = '0.0.0.0'
  28. /**
  29. * Non-internal IPv4 interface addresses of this machine — the IP-literal
  30. * authorities an all-interfaces bind is reachable by on the LAN.
  31. * @returns the addresses in interface order (possibly empty).
  32. */
  33. function lanIPv4Addresses(): string[] {
  34. return Object.values(networkInterfaces()).flat()
  35. .filter((iface): iface is NonNullable<typeof iface> => iface !== undefined && iface.family === 'IPv4' && !iface.internal)
  36. .map(iface => iface.address)
  37. }
  38. /**
  39. * One LAN-trust resolution for one invocation, sampled exactly once: the
  40. * machine's LAN IP literals when the effective bind is all-interfaces, and
  41. * the `trustedHosts` value built from them plus the explicit extras. The
  42. * single sample is deliberate — display must advertise only addresses the
  43. * fence was configured with, so both read this snapshot. Derived entries are
  44. * port-less IP literals: DNS rebinding needs an attacker-controlled name, so
  45. * an IP-literal Host is safe on any port, and the bound port may be
  46. * OS-assigned, unknowable pre-boot.
  47. * @param bindHost - the effective webserver bind host (CLI flag, else the yml default).
  48. * @param extra - `--trusted-host` values, in argv order.
  49. * @returns the sampled LAN addresses and the connection row's `trustedHosts` value (each possibly empty).
  50. */
  51. export function resolveLanTrust(
  52. bindHost: string | undefined,
  53. extra: readonly string[],
  54. ): { lanAddresses: string[]; trustedHosts: string[] } {
  55. const lanAddresses = bindHost === ALL_INTERFACES_HOST ? lanIPv4Addresses() : []
  56. return { lanAddresses, trustedHosts: [...lanAddresses, ...extra] }
  57. }
  58. /**
  59. * Resolve the telemetry opt-out switch into its boot patch. ANY non-empty
  60. * value (including `'0'`/`'false'`) disables: a privacy switch prefers
  61. * off-by-mistake over on-by-mistake. Throws when the switch is set but the
  62. * row is absent — a silently no-op "disabled" privacy switch would keep
  63. * exporting while the user believes it is off.
  64. * @param disabledEnv - the raw `DSH_TELEMETRY_DISABLED` value (`undefined` when unset).
  65. * @param hasRow - whether the composition carries the {@link TELEMETRY_ROW_ID} row.
  66. * @returns the disable patch, or `undefined` when telemetry stays enabled.
  67. */
  68. export function resolveTelemetryPatch(disabledEnv: string | undefined, hasRow: boolean): PatchOptions | undefined {
  69. if ((disabledEnv ?? '') === '') return undefined
  70. if (!hasRow) {
  71. throw new Error(`dsh: DSH_TELEMETRY_DISABLED is set but row "${TELEMETRY_ROW_ID}" is not in this composition`)
  72. }
  73. return { id: TELEMETRY_ROW_ID, disabled: true }
  74. }
  75. /**
  76. * Whether a config file carries the telemetry row, parsed under the same
  77. * `!!js`-tolerant dialect the boot uses — the `hasRow` input for launchers
  78. * that compose their patch lists outside {@link AppCLIEntry} (the TUI).
  79. * @param file - absolute path of the config or overlay file.
  80. * @returns true when a top-level (or inserted) row has the telemetry id.
  81. */
  82. export function configHasTelemetryRow(file: string): boolean {
  83. const doc = yaml.load(readFileSync(file, 'utf8'), { schema: includeYamlSchema })
  84. if (!Array.isArray(doc)) throw new Error(`dsh: ${file} is not a top-level entry list`)
  85. return (doc as { id?: string; insert?: { id?: string }[] }[]).some(row =>
  86. row.id === TELEMETRY_ROW_ID || (row.insert ?? []).some(inserted => inserted.id === TELEMETRY_ROW_ID))
  87. }
  88. /** One profile-json key mapped onto a yml row's config field. */
  89. interface ProfileMapping {
  90. jsonPath: string
  91. entryId: string
  92. configKey: string
  93. }
  94. /**
  95. * The static profile→row mapping table. json is user config and wins over the
  96. * yml engineering default per field; a json key absent from this table fails
  97. * loud (a typo silently ignored would read as "setting has no effect").
  98. * Developers extend deployments by adding rows here.
  99. */
  100. const PROFILE_MAPPINGS: ProfileMapping[] = [
  101. { jsonPath: 'provider', entryId: 'api-gateway', configKey: 'provider' },
  102. { jsonPath: 'model', entryId: 'api-gateway', configKey: 'model' },
  103. { jsonPath: 'persistenceRoot', entryId: 'session-persistence-jsonl', configKey: 'root' },
  104. ]
  105. // The include's YAML dialect: `!!js` scalars become expression nodes the
  106. // Loader evaluates at entry activation. The bypass parse below must accept
  107. // them (and passing one through a patch unchanged is legal).
  108. const jsExprType = new yaml.Type('tag:yaml.org,2002:js', {
  109. kind: 'scalar',
  110. resolve: data => typeof data === 'string',
  111. construct: data => ({ __jsExpr: String(data) }),
  112. })
  113. const includeYamlSchema = yaml.JSON_SCHEMA.extend(jsExprType)
  114. /** Constructor facts for one dsh invocation over the shared composition (argv already parsed by the surface bin). */
  115. export interface AppCLIEntryOptions {
  116. /** Absolute path of the shared base config the Loader includes. */
  117. configPath: string
  118. /**
  119. * Absolute path of this surface's overlay: a patch list applied over
  120. * {@link configPath} before this entry's own profile/flag patches. Its rows
  121. * are also merge inputs, so a flag override preserves the overlay's other
  122. * fields on the same row.
  123. */
  124. overlayPath: string
  125. /**
  126. * Optional explicit overlay applied after {@link overlayPath} and before
  127. * this entry's own profile/flag patches. When absent, the personal
  128. * `$DSH_HOME/config.yaml` overlay is applied instead.
  129. */
  130. extraOverlayPath?: string
  131. /** Whether to append the HMR row (the whole prod/dev difference; web surface only). */
  132. dev: boolean
  133. /** --host when explicitly passed; undefined keeps the yml engineering default. */
  134. host?: string
  135. /**
  136. * Listen port override onto the webserver row. Web passes the --port flag
  137. * value; headless passes 0 (an OS-assigned port, so parallel `dsh -p` runs
  138. * never collide — and the printed URL still opens the live session in a
  139. * browser).
  140. */
  141. port?: number
  142. /** Parent directory for name-created Workspaces; undefined uses the gateway's cwd fallback. */
  143. workspaceRoot?: string
  144. /** Extra authorities for the /api browser-trust fence (`host` or `host:port`), appended to the derived LAN IP literals. */
  145. trustedHosts?: string[]
  146. }
  147. /**
  148. * Boot driver for the config-tree dsh surfaces (web and headless share the
  149. * one composition; the surfaces differ only in constructor facts): holds only
  150. * what exists independently of (and prior to) cordis — argv facts, the
  151. * composed patch set, and finally the root ctx.
  152. */
  153. export class AppCLIEntry {
  154. /** The root context, set by {@link run}. */
  155. ctx!: Context
  156. /**
  157. * LAN IPv4 addresses sampled once at patch composition — the exact snapshot
  158. * the /api trust fence was configured with. Display reads this instead of
  159. * re-sampling, so the advertised LAN URL can never name an address the
  160. * fence rejects. Empty unless the effective bind is all-interfaces.
  161. */
  162. lanAddresses: readonly string[] = []
  163. private patches: PatchOptions[] = []
  164. constructor(private readonly options: AppCLIEntryOptions) {}
  165. /**
  166. * Run the boot chain: patch composition → Loader include boot (dev row
  167. * before await) → fail-loud triple.
  168. * @returns the settled root context and the listening port.
  169. */
  170. async run(): Promise<{ ctx: Context; port: number }> {
  171. this.composePatches()
  172. await this.bootTree()
  173. this.assertBoot()
  174. const port = this.ctx.get('httpServer')?.port
  175. /* v8 ignore next -- the sweep above guarantees an ACTIVE webserver row */
  176. if (port === undefined) throw new Error('dsh: httpServer service missing after settled boot')
  177. return { ctx: this.ctx, port }
  178. }
  179. /**
  180. * Compose the patch set from profile json, CLI flags, and the resolved
  181. * frontend dist. Patches replace a row's config wholesale, so each patched row's yml
  182. * static values are re-read here (bypass parse) and merged under the overrides.
  183. */
  184. private composePatches(): void {
  185. const rows = this.parseYmlRows()
  186. const overrides = new Map<string, Record<string, unknown>>()
  187. const put = (entryId: string, key: string, value: unknown): void => {
  188. const bag = overrides.get(entryId) ?? {}
  189. bag[key] = value
  190. overrides.set(entryId, bag)
  191. }
  192. // Source 1: profile json (missing file = empty; unmapped key = loud).
  193. for (const [key, value] of Object.entries(this.readProfile())) {
  194. const mapping = PROFILE_MAPPINGS.find(m => m.jsonPath === key)
  195. if (mapping === undefined) {
  196. throw new Error(`dsh: profile key "${key}" has no mapping (known: ${PROFILE_MAPPINGS.map(m => m.jsonPath).join(', ')})`)
  197. }
  198. put(mapping.entryId, mapping.configKey, value)
  199. }
  200. // Source 2: CLI flags (field set disjoint from the json mappings).
  201. if (this.options.host !== undefined) put('webserver', 'host', this.options.host)
  202. if (this.options.port !== undefined) put('webserver', 'port', this.options.port)
  203. if (this.options.workspaceRoot !== undefined) put('api-gateway', 'workspaceRoot', this.options.workspaceRoot)
  204. // Source 2b: authorities for the /api browser-trust fence (rationale on
  205. // resolveLanTrust).
  206. const ymlHost = (rows.get('webserver')?.config as { host?: string } | undefined)?.host
  207. const { lanAddresses, trustedHosts } = resolveLanTrust(this.options.host ?? ymlHost, this.options.trustedHosts ?? [])
  208. this.lanAddresses = lanAddresses
  209. if (trustedHosts.length > 0) put('connection', 'trustedHosts', trustedHosts)
  210. // Source 3: the frontend dist — an assembly fact of this app, never yml
  211. // user config. Workspace knowledge stays here.
  212. put('webserver', 'distIndex', this.resolveDistIndex())
  213. this.patches = [...overrides.entries()].map(([id, bag]) => {
  214. const yml = rows.get(id)
  215. if (yml === undefined) throw new Error(`dsh: patch target row "${id}" not found in ${this.options.configPath}`)
  216. return { id, config: { ...(yml.config ?? {}) as Record<string, unknown>, ...bag } }
  217. })
  218. // Telemetry opt-out: a row can only be turned off at the patch layer
  219. // (config cannot disable an entry), and the switch must hold BEFORE the
  220. // plugin constructs — its exporter.url validation is load-time fail-loud.
  221. const telemetryPatch = resolveTelemetryPatch(process.env.DSH_TELEMETRY_DISABLED, rows.has(TELEMETRY_ROW_ID))
  222. if (telemetryPatch !== undefined) this.patches.push(telemetryPatch)
  223. }
  224. /** Shared Loader boot; the dev HMR row mounts before await so the fail-loud sweep covers it. */
  225. private async bootTree(): Promise<void> {
  226. // One include of the shared base with every overlay as a sibling patch
  227. // list: patches never cross an include boundary, so nesting them would
  228. // silently stop reaching base rows. The surface overlay applies first, then
  229. // this entry's profile-json and CLI-flag patches, which therefore win.
  230. const patches = [
  231. ...loadOverlayPatches('dsh', this.options.overlayPath),
  232. ...this.options.extraOverlayPath === undefined
  233. ? loadPersonalPatches('dsh') ?? []
  234. : loadOverlayPatches('dsh', this.options.extraOverlayPath),
  235. ...this.patches,
  236. ]
  237. this.ctx = await boot('dsh', resolve(this.options.configPath), patches, async (ctx) => {
  238. if (this.options.dev) await ctx.loader.create({ name: '@deepseek-ai/dsh-client-hmr' })
  239. })
  240. }
  241. /** Install the diagnostic for plugin rejections that happen after settled boot. */
  242. private assertBoot(): void {
  243. installFailLoud('dsh')
  244. }
  245. /**
  246. * Bypass parse of the base and this surface's overlay (id → row) for
  247. * patch-merge inputs; the Loader still reads both files itself. The overlay
  248. * wins per row, matching the order its patches are applied in, and its
  249. * `insert` rows are indexed too because a flag may target one of them.
  250. */
  251. private parseYmlRows(): Map<string, { config?: unknown }> {
  252. const rows = new Map<string, { config?: unknown }>()
  253. const files = [this.options.configPath, this.options.overlayPath]
  254. if (this.options.extraOverlayPath !== undefined) files.push(this.options.extraOverlayPath)
  255. for (const file of files) {
  256. for (const row of this.parseRowList(file)) {
  257. if (typeof row.id === 'string') rows.set(row.id, row)
  258. for (const inserted of row.insert ?? []) {
  259. if (typeof inserted.id === 'string') rows.set(inserted.id, inserted)
  260. }
  261. }
  262. }
  263. return rows
  264. }
  265. /**
  266. * Parse one entry or patch list, rejecting anything that is not a top-level
  267. * array so a malformed file fails here rather than at row lookup.
  268. * @param file - absolute path of the config or overlay file.
  269. * @returns the parsed top-level entries.
  270. */
  271. private parseRowList(file: string): { id?: string; config?: unknown; insert?: { id?: string; config?: unknown }[] }[] {
  272. const doc = yaml.load(readFileSync(file, 'utf8'), { schema: includeYamlSchema })
  273. if (!Array.isArray(doc)) throw new Error(`dsh: ${file} is not a top-level entry list`)
  274. return doc as { id?: string; config?: unknown; insert?: { id?: string; config?: unknown }[] }[]
  275. }
  276. /** Profile json under cwd; read-only — never created here, absent = no user config. */
  277. private readProfile(): Record<string, unknown> {
  278. let raw: string
  279. try {
  280. raw = readFileSync(join(process.cwd(), PROFILE_DIR, PROFILE_FILE), 'utf8')
  281. } catch (error) {
  282. if ((error as NodeJS.ErrnoException).code === 'ENOENT') return {}
  283. throw error
  284. }
  285. const parsed: unknown = JSON.parse(raw)
  286. if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
  287. throw new Error(`dsh: ${PROFILE_DIR}/${PROFILE_FILE} must hold a JSON object`)
  288. }
  289. return parsed as Record<string, unknown>
  290. }
  291. /** Dist location is workspace knowledge of this app: resolved through the frontend package exports, not configured. */
  292. private resolveDistIndex(): string {
  293. const require = createRequire(import.meta.url)
  294. try {
  295. return require.resolve('@deepseek-ai/dsh-frontend/dist/index.html')
  296. } catch {
  297. throw new Error('dsh: frontend dist not built; run pnpm --filter @deepseek-ai/dsh-frontend build first')
  298. }
  299. }
  300. }