web.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * `dsh web` — the web-shape assembly: startHost + dist resolution +
  3. * startWebServer + the URL line + signal wiring. Mixing host and carrier
  4. * concerns is this app module's job (packages stay single-sided).
  5. */
  6. import { parseArgs } from 'node:util'
  7. import { networkInterfaces } from 'node:os'
  8. import { createRequire } from 'node:module'
  9. import { mountWebPlugins, startHost } from '@deepseek-ai/dsh-host-runtime'
  10. import { createHostWebPluginRegistry, startWebServer } from '@deepseek-ai/dsh-host-webserver'
  11. const LOOPBACK_HOST = '127.0.0.1'
  12. const ALL_INTERFACES_HOST = '0.0.0.0'
  13. export async function runWeb(argv: string[]): Promise<void> {
  14. const { values } = parseArgs({
  15. args: argv,
  16. options: {
  17. host: { type: 'string', default: LOOPBACK_HOST },
  18. port: { type: 'string', default: '3080' },
  19. },
  20. allowPositionals: false,
  21. })
  22. if (values.host !== LOOPBACK_HOST && values.host !== ALL_INTERFACES_HOST) {
  23. process.stderr.write(
  24. `dsh web: invalid --host ${values.host}; expected ${LOOPBACK_HOST} or ${ALL_INTERFACES_HOST}\n`,
  25. )
  26. process.exit(1)
  27. }
  28. const hostAddress = values.host
  29. const port = Number(values.port)
  30. if (!Number.isInteger(port) || port < 0 || port > 65535) {
  31. process.stderr.write(`dsh web: invalid --port ${values.port}\n`)
  32. process.exit(1)
  33. }
  34. // A missing DEEPSEEK_API_KEY throws here (plugin load is fail-loud, uncaught by design).
  35. const host = await startHost({ boot: { persistenceRoot: './.sessions' } })
  36. // Web UI plugin chain: in-memory Loader tree over the eight UI packages,
  37. // then the registry that feeds __DSH_BOOT__ and /plugins/<id>/client.js.
  38. const mounted = await mountWebPlugins(host.ctx)
  39. const webPlugins = createHostWebPluginRegistry({
  40. ctx: host.ctx,
  41. loader: mounted.loader,
  42. resolvePkgJson: mounted.resolvePkgJson,
  43. onError: (err: Error) => { process.stderr.write(`dsh web: plugin rescan: ${String(err)}\n`) },
  44. })
  45. // Published so the webserver invariant companion can audit manifest/bundle
  46. // consistency; nothing else reads this key.
  47. host.ctx.reflect.provide('webPlugins', webPlugins)
  48. // Dist location is workspace knowledge of this app: resolved through
  49. // @deepseek-ai/dsh-frontend's package exports, not configured.
  50. const require = createRequire(import.meta.url)
  51. let distIndex: string
  52. try {
  53. distIndex = require.resolve('@deepseek-ai/dsh-frontend/dist/index.html')
  54. } catch {
  55. process.stderr.write('dsh web: frontend dist not built; run pnpm --filter @deepseek-ai/dsh-frontend build first\n')
  56. await host.dispose()
  57. process.exit(1)
  58. }
  59. let exiting = false
  60. async function shutdown(code: number): Promise<void> {
  61. if (exiting) return
  62. exiting = true
  63. try {
  64. await server.close()
  65. await host.dispose()
  66. } finally {
  67. process.exit(code)
  68. }
  69. }
  70. let server: Awaited<ReturnType<typeof startWebServer>>
  71. try {
  72. server = await startWebServer(
  73. { host: hostAddress, port, distIndex, apiHandler: host.handler, webPlugins },
  74. (err: Error) => {
  75. process.stderr.write(`dsh web: ${String(err)}\n`)
  76. void shutdown(1)
  77. },
  78. )
  79. } catch (error: unknown) {
  80. // listen failed (EADDRINUSE…): no server to close, dispose the host directly.
  81. process.stderr.write(`dsh web: ${String(error)}\n`)
  82. await host.dispose()
  83. process.exit(1)
  84. }
  85. const lan = hostAddress === ALL_INTERFACES_HOST
  86. ? Object.values(networkInterfaces()).flat()
  87. .find(iface => iface !== undefined && iface.family === 'IPv4' && !iface.internal)
  88. : undefined
  89. const localUrl = `http://${LOOPBACK_HOST}:${server.port}`
  90. console.log(`dsh web: ${localUrl}${lan === undefined ? '' : ` (LAN: http://${lan.address}:${server.port})`}`)
  91. process.on('SIGTERM', () => { void shutdown(0) })
  92. process.on('SIGINT', () => { void shutdown(130) })
  93. }