2026-08-06 04:40:11 +08:00
/**
* @deepseek-ai/dsh-web-app — the browser-surface bundle's runtime glue plugin
2026-08-06 17:28:30 +08:00
* plus the bundle patch (`cordis.patch.yml`, declared by the `dsh.bundle.patch`
2026-08-09 15:09:19 +08:00
* manifest field). The plugin owns the browser-surface glue: it resolves
2026-08-06 04:40:11 +08:00
* the built frontend dist (workspace knowledge of this bundle, never user
* config), mounts the `frontend-static` fallback owner over it, registers the
2026-08-07 15:43:26 +08:00
* harness-source and web-surface prompt sections, the bash-visible web runtime
2026-08-11 15:39:03 +08:00
* variable, and the URL line. App command-line values arrive through the
2026-08-07 15:43:26 +08:00
* `webStartup` service expressions in the bundle patch.
2026-08-06 04:40:11 +08:00
* @module @deepseek-ai/dsh-web-app
*/
import { createRequire } from 'node:module'
2026-08-10 21:49:11 +08:00
import { networkInterfaces } from 'node:os'
2026-08-07 15:43:26 +08:00
import { fileURLToPath } from 'node:url'
2026-08-10 22:04:06 +08:00
import type { Context } from '@deepseek-ai/cordis'
import z from '@deepseek-ai/schemastery'
2026-08-07 15:43:26 +08:00
import { addHarnessSourceSection } from '@deepseek-ai/dsh-app-boot'
2026-08-06 04:40:11 +08:00
import * as FrontendStatic from '@deepseek-ai/dsh-frontend-static'
2026-08-10 22:04:06 +08:00
import type { } from '@deepseek-ai/cordis-plugin-loader'
2026-08-06 04:40:11 +08:00
import type { } from '@deepseek-ai/dsh-host-webserver'
import type { } from '@deepseek-ai/dsh-system-prompt'
import type { } from '@deepseek-ai/dsh-bash-env'
/** Stable Cordis plugin name. */
export const name = 'web-app'
2026-08-07 15:43:26 +08:00
/** This dsh installation's root, from either this package's source or built entry. */
const SOURCE_ROOT = fileURLToPath ( new URL ( '../../../..' , import . meta . url ) )
2026-08-10 21:49:11 +08:00
/** Runtime service that releases Web rows after bind-dependent values resolve. */
const WEB_RUNTIME_SERVICE = 'webRuntime'
2026-08-07 15:43:26 +08:00
2026-08-06 04:40:11 +08:00
/** Services required before the web runtime can mount. */
export const inject = [ 'httpServer' ]
2026-08-10 21:49:11 +08:00
/** Plugin config: composed deployment settings plus per-invocation command-line values. */
2026-08-06 04:40:11 +08:00
export interface Config {
2026-08-09 18:23:25 +08:00
/** Print the URL line on activation; a non-interactive layer can turn it off. */
2026-08-06 04:40:11 +08:00
printUrl : boolean
2026-08-06 09:27:44 +08:00
/**
* Register the model-visible surface context (the `app:web-surface` prompt
2026-08-11 15:39:03 +08:00
* section and the `DSH_WEB_URL` bash variable). A one-shot non-interactive
* layer can turn it off when its user is not in the GUI, so the
2026-08-06 09:27:44 +08:00
* orientation text would be false.
*/
surfaceContext : boolean
2026-08-10 21:49:11 +08:00
/** Explicit `--trusted-host` authorities from this invocation. */
trustedHosts : string [ ]
2026-08-06 04:40:11 +08:00
}
export const Config : z < Config > = z . object ( {
printUrl : z.boolean ( ) . default ( true ) ,
2026-08-06 09:27:44 +08:00
surfaceContext : z.boolean ( ) . default ( true ) ,
2026-08-10 21:49:11 +08:00
trustedHosts : z.array ( String ) . default ( [ ] ) ,
2026-08-06 04:40:11 +08:00
} )
2026-08-10 21:49:11 +08:00
/** Bind-dependent Web values shared by the trust fence and URL display. */
export interface WebRuntimeValues {
/** LAN IPv4 literals sampled once when the server binds all interfaces. */
lanAddresses : string [ ]
/** LAN literals followed by explicit invocation authorities. */
trustedHosts : string [ ]
}
2026-08-06 04:40:11 +08:00
/** Environment variable naming the canonical local URL of this Web GUI. */
const DSH_WEB_URL = 'DSH_WEB_URL' as const
// Display-only mirror of the webserver schema's loopback host: the address the
// local URL always prints. Not a source of truth — the schema is.
const LOOPBACK_HOST = '127.0.0.1'
2026-08-10 21:49:11 +08:00
/** The webserver schema's all-interfaces bind literal. */
const ALL_INTERFACES_HOST = '0.0.0.0'
/**
* Resolve one LAN-trust snapshot from the active server bind.
*
* Derived entries are port-less IP literals: DNS rebinding needs an
* attacker-controlled name, while an IP-literal Host is safe on any port and
* an OS-assigned port is unknowable before bind.
* @param bindHost - the active webserver bind host.
* @param extra - explicit `--trusted-host` values, in argument order.
* @returns the LAN display addresses and invocation-derived fence authorities.
*/
export function resolveLanTrust ( bindHost : string , extra : readonly string [ ] ) : WebRuntimeValues {
const lanAddresses = bindHost === ALL_INTERFACES_HOST
? Object . values ( networkInterfaces ( ) ) . flat ( )
. filter ( ( iface ) : iface is NonNullable < typeof iface > = > iface !== undefined && iface . family === 'IPv4' && ! iface . internal )
. map ( iface = > iface . address )
: [ ]
return { lanAddresses , trustedHosts : [ . . . lanAddresses , . . . extra ] }
}
2026-08-06 04:40:11 +08:00
/** Model-visible orientation and acceptance boundary for sessions created through `dsh web`. */
2026-08-11 15:39:03 +08:00
function webSurfacePrompt ( webUrl : string ) : string {
const updateContract = 'The client-plugin HMR receiver is active, but client-plugin changes reload without a refresh only while '
+ '`pnpm run dev:web` is also running from this same checkout to rebuild their bundles; verify that watcher before promising automatic updates. '
+ 'Every other change — the apps/web shell and plain packages — requires rebuilding the affected Web artifacts and verifying this existing URL after a page refresh. '
2026-08-06 04:40:11 +08:00
return ` You are interacting with the user through the DeepSeek Harness Web GUI at ${ webUrl } . `
+ 'When the user refers to "this page", "this GUI", or "this app" without naming another target, they mean this GUI. '
+ 'The browser provides no implicit DOM, route, or screenshot context. '
+ updateContract
+ 'Starting another server does not update this GUI. '
+ 'The apps/web Vite entry builds the shell but is not a standalone application because only dsh web injects window.__DSH_BOOT__. '
+ 'Do not start a replacement server unless the user asks; if one is needed, use a managed background task and verify its exact URL.'
}
/** Resolve the canonical loopback URL from the active Web server. */
function localWebUrl ( ctx : Context ) : string {
const port = ctx . get ( 'httpServer' ) ? . port
if ( port === undefined ) throw new Error ( 'web-app: httpServer service missing while resolving Web runtime' )
return ` http:// ${ LOOPBACK_HOST } : ${ String ( port ) } `
}
/** Dist location is workspace knowledge of this bundle: resolved through the frontend package exports, not configured. */
function resolveDistIndex ( ) : string {
const require = createRequire ( import . meta . url )
try {
return require . resolve ( '@deepseek-ai/dsh-frontend/dist/index.html' )
} catch {
/* v8 ignore next 2 -- reachable only on a checkout without a built dist; the test tree builds it */
throw new Error ( 'web-app: frontend dist not built; run pnpm run build from the repository root first' )
}
}
2026-08-09 15:34:32 +08:00
/** Test hook: hosts with no built frontend dist substitute the resolver; production never touches this. */
2026-08-06 04:40:11 +08:00
export const internals : { resolveDistIndex : ( ) = > string } = { resolveDistIndex }
/**
2026-08-11 15:39:03 +08:00
* Mount the Web runtime: dist serving, surface prompt, the bash runtime
* variable, and the URL line.
2026-08-06 04:40:11 +08:00
* @param ctx - plugin context carrying the httpServer service.
* @param config - validated {@link Config}.
*/
2026-08-11 14:17:13 +08:00
export function apply ( ctx : Context , config : Config ) : void {
2026-08-10 21:49:11 +08:00
const runtime = resolveLanTrust ( ctx . httpServer . host , config . trustedHosts )
2026-08-11 14:17:13 +08:00
// Release dependent rows only after bind-dependent trust has been sampled once.
2026-08-10 21:49:11 +08:00
ctx . provide ( WEB_RUNTIME_SERVICE , runtime )
2026-08-10 19:58:40 +08:00
ctx . plugin ( FrontendStatic , { distIndex : internals.resolveDistIndex ( ) } )
2026-08-06 09:27:44 +08:00
if ( config . surfaceContext ) {
ctx . inject ( [ 'systemPrompt' ] , ( promptCtx ) = > {
2026-08-07 15:43:26 +08:00
addHarnessSourceSection ( promptCtx , SOURCE_ROOT )
2026-08-06 09:27:44 +08:00
promptCtx . systemPrompt . section ( {
name : 'app:web-surface' ,
order : - 98 ,
2026-08-11 15:39:03 +08:00
text : ( ) = > webSurfacePrompt ( localWebUrl ( promptCtx ) ) ,
2026-08-06 09:27:44 +08:00
} )
2026-08-06 04:40:11 +08:00
} )
2026-08-06 09:27:44 +08:00
ctx . inject ( [ 'bashEnv' ] , ( runtimeCtx ) = > {
runtimeCtx . bashEnv . register ( {
name : 'web-runtime' ,
variables : {
[ DSH_WEB_URL ] : { description : 'Canonical local URL of the DeepSeek Harness Web GUI serving this session.' } ,
} ,
2026-08-11 15:39:03 +08:00
resolve : ( ) = > ( { [ DSH_WEB_URL ] : localWebUrl ( runtimeCtx ) } ) ,
2026-08-06 09:27:44 +08:00
} )
2026-08-06 04:40:11 +08:00
} )
2026-08-06 09:27:44 +08:00
}
2026-08-06 04:40:11 +08:00
if ( config . printUrl ) {
// The URL line is a readiness signal: supervisors (and the keyless CLI
// smoke) RPC as soon as they observe it, so it must not print while
// sibling rows (the /api route owner) are still mounting. Await Loader
// settlement first; a hand-built tree without a Loader prints at once.
const printUrl = ( ) : void = > {
2026-08-10 21:49:11 +08:00
// Reuse the exact LAN snapshot provided to the /api trust fence.
const lanCandidate = runtime . lanAddresses [ 0 ]
2026-08-06 04:40:11 +08:00
const port = ctx . httpServer . port
console . log ( ` dsh web: ${ localWebUrl ( ctx ) } ${ lanCandidate === undefined ? '' : ` (LAN: http:// ${ lanCandidate } : ${ String ( port ) } ) ` } ` )
}
2026-08-10 20:49:40 +08:00
// This row's own activation can precede a sibling failure. The app owns
// readiness by waiting for its Loader tree, or prints at once in a
// hand-built context without Loader.
const settled = ctx . get ( 'loader' ) ? . await ( )
2026-08-07 11:58:03 +08:00
if ( settled === undefined ) printUrl ( )
2026-08-06 04:40:32 +08:00
else {
2026-08-07 11:58:03 +08:00
void settled . then ( ( ) = > {
// The tree can be disposed while the boot was in flight (early
2026-08-06 04:40:32 +08:00
// SIGTERM); a URL line for a dead server would only mislead, and
// reading the torn-down port would turn a clean shutdown into a crash.
if ( ctx . get ( 'httpServer' ) !== undefined ) printUrl ( )
2026-08-10 20:49:40 +08:00
// Loader reports a failed boot; this row only stays quiet.
2026-08-07 11:58:03 +08:00
} , ( ) = > { } )
2026-08-06 04:40:32 +08:00
}
2026-08-06 04:40:11 +08:00
}
}