dsh-sansheng-liubu/scripts/preview-workbench.mjs

2145 lines
70 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* preview-workbench.mjs — 任意 DSH 客户端 bundle 的离线渲染 + 截图工具。
*
* 与 scripts/preview.mjs 的区别:那个脚本是「朝堂侧栏」专用、演示数据写死;
* 这个脚本接受任意 bundle 路径,用 vm 在伪 window 上加载真实 bundle,
* 打桩 fetch 数据,用会真正重渲染的 hooks 替身跑完异步 effect,
* 把组件树序列化成 HTML,最后交给本机 Chrome 截图。
*
* 用法:
* node scripts/preview-workbench.mjs --bundle=<path> --out=<dir> --name=<file> \
* --theme=light|dark --width=400
*/
import vm from 'node:vm'
import {
existsSync,
mkdirSync,
readFileSync,
statSync,
writeFileSync,
} from 'node:fs'
import { dirname, join, resolve } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { spawnSync } from 'node:child_process'
const HERE = dirname(fileURLToPath(import.meta.url))
const ROOT = resolve(HERE, '..')
const SCRATCH = join(ROOT, '.scratch', 'preview-workbench')
const ROUTE_PREFIX = '/sansheng/workbench'
const LEAD_SESSION = 'sess-lead'
function unrefTimer(timer) {
if (timer && typeof timer.unref === 'function') timer.unref()
return timer
}
const CHROME_CANDIDATES = [
'C:/Program Files/Google/Chrome/Application/chrome.exe',
'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe',
'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe',
'C:/Program Files/Microsoft/Edge/Application/msedge.exe',
]
// #region 基础工具
function fail(message) {
const error = new Error(message)
error.previewFailure = true
throw error
}
function log(message) {
process.stdout.write(`[preview-workbench] ${message}\n`)
}
function ensureDir(path) {
mkdirSync(path, { recursive: true })
}
function parseArgs(argv) {
const options = {
bundle: 'workbench/panel/lib/client.js',
out: 'docs/preview',
name: 'v01-workbench-rail',
theme: 'light',
width: 400,
scale: 2,
chrome: null,
view: 'court',
open: '',
help: false,
}
const keys = {
bundle: 'bundle',
out: 'out',
name: 'name',
theme: 'theme',
width: 'width',
scale: 'scale',
chrome: 'chrome',
view: 'view',
open: 'open',
}
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index]
if (arg === '--help' || arg === '-h') {
options.help = true
continue
}
if (!arg.startsWith('--')) fail(`无法识别的参数:${arg}`)
const body = arg.slice(2)
const eq = body.indexOf('=')
const rawKey = eq === -1 ? body : body.slice(0, eq)
if (rawKey === 'help') {
options.help = true
continue
}
const field = keys[rawKey]
if (!field) fail(`无法识别的参数:--${rawKey}`)
let value
if (eq === -1) {
index += 1
value = argv[index]
if (value === undefined) fail(`参数 --${rawKey} 缺少值`)
} else {
value = body.slice(eq + 1)
}
options[field] = value
}
return options
}
function usage() {
return [
'用法:',
' node scripts/preview-workbench.mjs --bundle=<path> --out=<dir> --name=<file> \\',
' --theme=light|dark --width=400',
'',
'选项:',
' --bundle bundle 路径,默认 workbench/panel/lib/client.js',
' --out 截图输出目录,默认 docs/preview',
' --name 输出文件名前缀,默认 v01-workbench-rail',
' --theme light | dark,默认 light',
' --width 截图宽度(CSS px),默认 400',
' --scale 设备像素比,默认 2',
' --chrome Chrome/Edge 可执行文件路径,默认自动探测',
' --view court | tasks | artifacts,先切到该页签再截图,默认 court',
' --open 切到成果页后点开匹配这段文字的成果行(看详情抽屉)',
' --help 显示本帮助',
].join('\n')
}
function chromeRun(chrome, args, label) {
const result = spawnSync(chrome, args, {
encoding: 'utf8',
windowsHide: true,
maxBuffer: 128 * 1024 * 1024,
timeout: 180000,
stdio: ['ignore', 'pipe', 'pipe'],
})
if (result.error) fail(`${label} 调用 Chrome 失败:${result.error.message}`)
if (result.status !== 0) {
const stderr = String(result.stderr || '').trim().split(/\r?\n/).slice(-8).join('\n')
fail(`${label} 失败(Chrome 退出码 ${result.status})${stderr ? `:\n${stderr}` : ''}`)
}
return result
}
function findChrome(explicit) {
const candidates = explicit ? [explicit, ...CHROME_CANDIDATES] : CHROME_CANDIDATES
for (const candidate of candidates) {
try {
if (candidate && statSync(candidate).isFile()) return candidate
} catch (error) {
// 继续找下一个
}
}
fail(`找不到 Chrome/Edge。可用 --chrome=<path> 指定。尝试过:\n${candidates.join('\n')}`)
}
function readPngSize(pngPath) {
const bytes = readFileSync(pngPath)
if (bytes.length < 33) fail(`截图文件不完整:${pngPath}(${bytes.length} 字节)`)
if (bytes.subarray(0, 8).toString('hex') !== '89504e470d0a1a0a') {
fail(`截图不是合法 PNG:${pngPath}`)
}
return {
width: bytes.readUInt32BE(16),
height: bytes.readUInt32BE(20),
bytes: bytes.length,
}
}
// #endregion
// #region 固定演示数据(形状对齐 workbench/host/service.mjs)
const SEATS = [
{ roleId: 'lead', name: '丞相', short: '丞', tier: 'leaders', group: 'lead', column: 'span', row: 0, duty: '接需求、分流、协调、汇报', canDispatch: true },
{ roleId: 'zhongshu', name: '中书', short: '中', tier: 'leaders', group: 'sansheng', column: 'left', row: 1, duty: '调查、澄清、规划', canDispatch: false },
{ roleId: 'shangshu', name: '尚书', short: '尚', tier: 'leaders', group: 'sansheng', column: 'center', row: 1, duty: '分工建议、组织交付', canDispatch: true },
{ roleId: 'menxia', name: '门下', short: '门', tier: 'leaders', group: 'sansheng', column: 'right', row: 1, duty: '审议与整体审核', canDispatch: false },
{ roleId: 'libu', name: '吏部', short: '吏', tier: 'ministries', group: 'ministries', column: 'left', row: 2, duty: '角色和技能配置建议', canDispatch: false },
{ roleId: 'hubu', name: '户部', short: '户', tier: 'ministries', group: 'ministries', column: 'center', row: 2, duty: '数据、表格、计算', canDispatch: false },
{ roleId: 'lipu', name: '礼部', short: '礼', tier: 'ministries', group: 'ministries', column: 'right', row: 2, duty: '文档、内容、呈现', canDispatch: false },
{ roleId: 'bingbu', name: '兵部', short: '兵', tier: 'ministries', group: 'ministries', column: 'left', row: 3, duty: '环境、调试、部署', canDispatch: false },
{ roleId: 'xingbu', name: '刑部', short: '刑', tier: 'ministries', group: 'ministries', column: 'center', row: 3, duty: '测试与风险检查', canDispatch: false },
{ roleId: 'gongbu', name: '工部', short: '工', tier: 'ministries', group: 'ministries', column: 'right', row: 3, duty: '代码实现与工程产出', canDispatch: false },
]
const TASKS = [
{
id: 'task_demo_running',
projectId: 'prj_demo1',
title: '生成离线预览截图',
objective: '在 400px 窄栏下渲染工作台并输出 light / dark 两张截图。',
acceptance: ['两张 PNG 宽度为 800px(2x)', '不修改被预览的 bundle'],
constraints: ['只写 .scratch 与 docs/preview'],
scope: 'preview',
ownerRoleId: 'gongbu',
sourceRoleId: 'shangshu',
originMessageRef: 'msg_demo_1',
returnTarget: 'shangshu',
status: 'running',
activeAttemptId: 'attempt_demo_running',
attempts: [
{
id: 'attempt_demo_running',
projectId: 'prj_demo1',
taskId: 'task_demo_running',
state: 'running',
startedAt: '2026-11-09T22:10:00.000Z',
endedAt: null,
},
],
startedAt: '2026-11-09T22:10:00.000Z',
endedAt: null,
reportReceivedAt: null,
needsAttentionReason: null,
failureReason: null,
waitingReason: null,
revision: 3,
createdAt: '2026-11-09T22:09:00.000Z',
updatedAt: '2026-11-09T22:12:00.000Z',
open: true,
attemptCount: 1,
resultCount: 0,
},
{
id: 'task_demo_attention',
projectId: 'prj_demo1',
title: '复核 2x 截图的验收口径',
objective: '确认 800px 宽的 PNG 是否满足交付要求,并记录结论。',
acceptance: ['给出明确的接受 / 不接受结论'],
constraints: ['不重启 DSH'],
scope: 'review',
ownerRoleId: 'xingbu',
sourceRoleId: 'lead',
originMessageRef: 'msg_demo_2',
returnTarget: 'lead',
status: 'needs_attention',
activeAttemptId: null,
attempts: [],
startedAt: '2026-11-09T22:05:00.000Z',
endedAt: null,
reportReceivedAt: null,
needsAttentionReason: '验收标准需要人工确认:是否接受 2x 设备像素比截图?',
failureReason: null,
waitingReason: null,
revision: 2,
createdAt: '2026-11-09T22:04:00.000Z',
updatedAt: '2026-11-09T22:14:00.000Z',
open: true,
attemptCount: 0,
resultCount: 0,
},
{
id: 'task_demo_revision',
projectId: 'prj_demo1',
title: '修订:离线预览截图',
kind: 'revision',
objective: '按验收意见重拍 light 页并补一张成果详情。',
acceptance: ['修订后的截图进入成果账本'],
constraints: ['不重启 DSH'],
scope: 'preview',
ownerRoleId: 'gongbu',
sourceRoleId: 'xingbu',
originMessageRef: 'msg_demo_4',
returnTarget: 'xingbu',
status: 'queued',
activeAttemptId: null,
attempts: [],
startedAt: null,
endedAt: null,
reportReceivedAt: null,
needsAttentionReason: null,
failureReason: null,
waitingReason: null,
revision: 1,
createdAt: '2026-11-09T22:15:00.000Z',
updatedAt: '2026-11-09T22:16:00.000Z',
open: true,
attemptCount: 0,
resultCount: 0,
},
{
id: 'task_demo_done',
projectId: 'prj_demo1',
title: '核对十席角色文案',
objective: '按 roles.mjs 校对十席名称、简写和职责。',
acceptance: ['十席全部一致'],
constraints: [],
scope: 'copy',
ownerRoleId: 'hubu',
sourceRoleId: 'zhongshu',
originMessageRef: 'msg_demo_3',
returnTarget: 'zhongshu',
status: 'completed',
activeAttemptId: null,
attempts: [
{
id: 'attempt_demo_done',
projectId: 'prj_demo1',
taskId: 'task_demo_done',
state: 'completed',
startedAt: '2026-11-09T21:40:00.000Z',
endedAt: '2026-11-09T21:52:00.000Z',
},
],
startedAt: '2026-11-09T21:40:00.000Z',
endedAt: '2026-11-09T21:52:00.000Z',
reportReceivedAt: '2026-11-09T21:52:30.000Z',
needsAttentionReason: null,
failureReason: null,
waitingReason: null,
revision: 5,
createdAt: '2026-11-09T21:39:00.000Z',
updatedAt: '2026-11-09T21:53:00.000Z',
open: false,
attemptCount: 1,
resultCount: 1,
},
]
const ARTIFACTS = [
{
id: 'artifact_demo_light',
projectId: 'prj_demo1',
taskId: 'task_demo_running',
ownerRoleId: 'gongbu',
fileRef: '.scratch/preview-workbench/v01-workbench-rail-400-light.png',
displayName: 'v01-workbench-rail-400-light.png',
mediaType: 'image/png',
versionOrHash: 'sha256:preview-demo-light',
verification: { status: 'verified' },
sourceResultId: null,
createdAt: '2026-11-09T22:11:00.000Z',
updatedAt: '2026-11-09T22:11:00.000Z',
},
{
id: 'artifact_demo_report',
projectId: 'prj_demo1',
taskId: 'task_demo_attention',
ownerRoleId: 'xingbu',
fileRef: 'docs/preview/巡检报告.md',
displayName: '巡检报告.md',
mediaType: 'text/markdown',
versionOrHash: 'sha256:preview-demo-report',
verification: { status: 'pending' },
sourceResultId: null,
createdAt: '2026-11-09T22:13:00.000Z',
updatedAt: '2026-11-09T22:13:00.000Z',
},
{
id: 'artifact_demo_missing',
projectId: 'prj_demo1',
taskId: 'task_demo_attention',
ownerRoleId: 'xingbu',
fileRef: '.scratch/preview-workbench/缺失的验收单.pdf',
displayName: '缺失的验收单.pdf',
mediaType: 'application/pdf',
versionOrHash: 'sha256:preview-demo-missing',
verification: { status: 'missing' },
existence: 'missing',
revision: 1,
sourceResultId: null,
createdAt: '2026-11-09T22:13:20.000Z',
updatedAt: '2026-11-09T22:13:20.000Z',
},
{
id: 'artifact_demo_unknown',
projectId: 'prj_demo1',
taskId: 'task_demo_running',
ownerRoleId: 'gongbu',
fileRef: '.scratch/preview-workbench/待核验的交付物.zip',
displayName: '待核验的交付物.zip',
mediaType: 'application/zip',
versionOrHash: 'sha256:preview-demo-unknown',
verification: { status: 'unknown' },
existence: 'unknown',
revision: 1,
sourceResultId: null,
createdAt: '2026-11-09T22:13:40.000Z',
updatedAt: '2026-11-09T22:13:40.000Z',
},
{
id: 'artifact_demo_revision',
projectId: 'prj_demo1',
taskId: 'task_demo_revision',
ownerRoleId: 'gongbu',
fileRef: 'docs/preview/v01-workbench-rail-400-light.png',
displayName: '预览截图(第 2 版).png',
mediaType: 'image/png',
versionOrHash: 'sha256:preview-demo-revision-2',
verification: { status: 'verified' },
existence: 'present',
revision: 2,
revisesArtifactId: 'artifact_demo_light',
sourceResultId: null,
createdAt: '2026-11-09T22:16:10.000Z',
updatedAt: '2026-11-09T22:16:10.000Z',
},
]
const PROJECT = {
id: 'prj_demo1',
hostWorkspaceId: 'ws_demo',
displayNameCache: '演示项目',
canonicalRoot: 'C:\\demo',
status: 'active',
schemaVersion: 1,
revision: 4,
createdAt: '2026-11-09T20:00:00.000Z',
updatedAt: '2026-11-09T22:14:00.000Z',
}
function buildSeats() {
const states = {
lead: 'idle',
gongbu: 'running',
xingbu: 'needs_attention',
}
return SEATS.map((seat) => {
const status = states[seat.roleId] || 'uninitialized'
const connected = status !== 'uninitialized'
const sessionId = connected ? `sess_${seat.roleId}` : null
const activeTask = TASKS.find(
(task) => task.ownerRoleId === seat.roleId && task.open,
)
return {
roleId: seat.roleId,
name: seat.name,
short: seat.short,
tier: seat.tier,
group: seat.group,
column: seat.column,
row: seat.row,
duty: seat.duty,
canDispatch: seat.canDispatch,
connected,
pending: !connected,
pendingLabel: connected ? null : '尚未接通',
activeTaskId: activeTask ? activeTask.id : null,
binding: connected
? {
id: `binding_${seat.roleId}`,
currentSessionId: sessionId,
bindingGeneration: 1,
status,
historyCount: 0,
revision: 1,
}
: null,
config: {
provider: 'magpie',
model: 'workbuddy/deepseek-v4.1-flash',
reasoningEffort: 'high',
configRevision: 3,
configured: true,
},
session: {
running: seat.roleId === 'gongbu',
available: true,
},
}
})
}
function buildBootstrap() {
const seats = buildSeats()
return {
projects: [
{
...PROJECT,
seatCount: SEATS.length,
taskCount: TASKS.length,
openTaskCount: TASKS.filter((task) => task.open).length,
},
],
projectId: PROJECT.id,
project: { ...PROJECT },
seats,
layout: {
lead: ['lead'],
chancellery: ['zhongshu', 'shangshu', 'menxia'],
sansheng: ['zhongshu', 'shangshu', 'menxia'],
ministries: [
['libu', 'hubu', 'lipu'],
['bingbu', 'xingbu', 'gongbu'],
],
},
tasks: TASKS,
artifacts: ARTIFACTS,
results: [],
eventSeq: 42,
}
}
// #endregion
// #region 伪浏览器环境 + fetch 打桩
function createDemoStorage() {
const store = new Map([
[
'sansheng-liubu.route.gongbu',
JSON.stringify({ provider: 'deepseek', model: 'deepseek-v4', reasoningEffort: 'high' }),
],
])
return {
getItem: (key) => (store.has(key) ? store.get(key) : null),
setItem: (key, value) => { store.set(key, String(value)) },
removeItem: (key) => { store.delete(key) },
clear: () => { store.clear() },
key: () => null,
get length() { return store.size },
}
}
function routePath(input) {
const raw = typeof input === 'string'
? input
: input && typeof input.url === 'string'
? input.url
: String(input)
try {
return new URL(raw, 'http://preview.local').pathname
} catch (error) {
return raw.split('?')[0]
}
}
function makeResponse(status, payload) {
const text = JSON.stringify(payload)
return {
ok: status >= 200 && status < 300,
status,
statusText: status === 200 ? 'OK' : 'Error',
headers: { get: (name) => (String(name).toLowerCase() === 'content-type' ? 'application/json' : null) },
json: async () => JSON.parse(text),
text: async () => text,
clone() { return makeResponse(status, payload) },
}
}
function createFetchStub(theme, onUnknown) {
const unknown = new Set()
const routes = new Map([
[`${ROUTE_PREFIX}/bootstrap`, () => buildBootstrap()],
[`${ROUTE_PREFIX}/events`, () => ({ events: [], reset: false, seq: 42, hasMore: false })],
[`${ROUTE_PREFIX}/projects`, () => buildBootstrap().projects],
[`${ROUTE_PREFIX}/seats`, () => buildSeats()],
[`${ROUTE_PREFIX}/layout`, () => buildBootstrap().layout],
[`${ROUTE_PREFIX}/tasks`, () => TASKS],
[`${ROUTE_PREFIX}/artifacts`, () => ARTIFACTS],
[`${ROUTE_PREFIX}/results`, () => []],
])
return async function fetchStub(input, init) {
const path = routePath(input)
const method = String((init && init.method) || (input && input.method) || 'GET').toUpperCase()
const factory = routes.get(path)
if (factory) return makeResponse(200, { ok: true, value: factory() })
if (!unknown.has(path)) {
unknown.add(path)
if (typeof onUnknown === 'function') onUnknown(path, method)
}
// 未知路由不抛错:尽量让面板继续渲染,而不是整页空白。
const fallback = path.endsWith('s') ? [] : {}
return makeResponse(200, { ok: true, value: fallback, previewStub: true })
}
}
function createBrowserEnv({ theme, fetchStub }) {
const styleTexts = []
const listeners = new Map()
const document = {
getElementById: () => null,
querySelector: () => null,
querySelectorAll: () => [],
createElement(tagName) {
return {
tagName: String(tagName).toUpperCase(),
dataset: {},
style: {},
classList: { add() {}, remove() {}, toggle() {}, contains: () => false },
textContent: '',
innerHTML: '',
setAttribute() {},
removeAttribute() {},
appendChild() {},
remove() {},
addEventListener() {},
removeEventListener() {},
getBoundingClientRect: () => ({ width: 0, height: 0, top: 0, left: 0 }),
}
},
createTextNode(text) {
return { textContent: String(text) }
},
head: {
appendChild(node) {
if (node && typeof node.textContent === 'string' && node.textContent.trim() !== '') {
styleTexts.push(node.textContent)
}
},
},
body: {
appendChild() {},
removeChild() {},
},
documentElement: {
dataset: {},
style: {},
classList: { add() {}, remove() {}, toggle() {}, contains: () => false },
setAttribute() {},
getAttribute: () => null,
},
addEventListener(type, handler) {
const list = listeners.get(type) || []
list.push(handler)
listeners.set(type, list)
},
removeEventListener() {},
visibilityState: 'visible',
hidden: false,
cookie: '',
}
const window = {
document,
location: { href: 'http://preview.local/', pathname: '/', origin: 'http://preview.local', search: '' },
navigator: { userAgent: 'preview-workbench', language: 'zh-CN', languages: ['zh-CN'], platform: 'Win32' },
localStorage: createDemoStorage(),
sessionStorage: createDemoStorage(),
devicePixelRatio: 2,
innerWidth: 400,
innerHeight: 800,
matchMedia(query) {
const wantsDark = theme === 'dark' && /prefers-color-scheme\s*:\s*dark/i.test(String(query))
return {
matches: wantsDark,
media: String(query),
addEventListener() {},
removeEventListener() {},
addListener() {},
removeListener() {},
onchange: null,
}
},
getComputedStyle: () => ({ getPropertyValue: () => '' }),
// bundle 里有事件轮询(setTimeout 链 / setInterval)。这些计时器若保持
// ref 状态,截图跑完后 Node 永远不会退出。unref 掉:只要主流程结束,
// 进程就能正常收尾,同时轮询在存活期间照常执行。
requestAnimationFrame: (callback) => unrefTimer(setTimeout(() => callback(Date.now()), 0)),
cancelAnimationFrame: (handle) => clearTimeout(handle),
addEventListener(type, handler) {
const list = listeners.get(type) || []
list.push(handler)
listeners.set(type, list)
},
removeEventListener() {},
fetch: fetchStub,
alert() {},
focus() {},
scrollTo() {},
}
class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
}
class IntersectionObserver {
constructor(callback) { this.callback = callback }
observe() {}
unobserve() {}
disconnect() {}
}
class MutationObserver {
observe() {}
disconnect() {}
}
return { document, window, styleTexts, ResizeObserver, IntersectionObserver, MutationObserver }
}
// #endregion
// #region 加载 bundle + React / hooks 替身
function createHooks() {
const slotMap = new Map()
const instanceMap = new Map()
const contextStack = []
const keyStack = []
const cursorStack = []
let currentKey = null
let cursor = 0
let dirty = false
let effectQueue = []
let idCounter = 0
const markDirty = () => { dirty = true }
const depsEqual = (a, b) => {
if (a === undefined) return false
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false
return a.every((value, index) => Object.is(value, b[index]))
}
const rootKey = '<root>'
const slotsFor = (key) => {
let slots = slotMap.get(key)
if (!slots) {
slots = []
slotMap.set(key, slots)
}
return slots
}
const useSlot = () => {
const key = currentKey === null ? rootKey : currentKey
const slots = slotsFor(key)
const index = cursor
cursor += 1
if (!slots[index]) slots[index] = {}
return slots[index]
}
class Component {
constructor(props) {
this.props = props || {}
this.state = {}
this.context = undefined
}
setState(partial) {
const next = typeof partial === 'function' ? partial(this.state) : partial
this.state = { ...this.state, ...(next || {}) }
markDirty()
}
forceUpdate() { markDirty() }
}
Component.prototype.isReactComponent = {}
function createElement(type, config, ...children) {
const props = { ...(config || {}) }
if (children.length === 1) props.children = children[0]
else if (children.length > 1) props.children = children
return { type, props, children }
}
function createContext(defaultValue) {
const context = {
_defaultValue: defaultValue,
_stack: [],
}
context.Provider = function ContextProvider(props) {
return props.children
}
context.Provider.__previewContext = context
context.Consumer = function ContextConsumer(props) {
const value = context._stack.length > 0
? context._stack[context._stack.length - 1]
: context._defaultValue
return typeof props.children === 'function' ? props.children(value) : null
}
context.Consumer.__previewContext = context
return context
}
const React = {
createElement,
cloneElement: (element, extraProps, ...children) => {
const props = { ...(element.props || {}), ...(extraProps || {}) }
if (children.length === 1) props.children = children[0]
else if (children.length > 1) props.children = children
return { type: element.type, props, children }
},
isValidElement: (value) => Boolean(value && typeof value === 'object' && value.type !== undefined),
createContext,
Component,
PureComponent: Component,
Fragment: 'fragment',
StrictMode: 'fragment',
Suspense: function Suspense(props) { return props.children },
memo: (component) => component,
forwardRef: (render) => function ForwardRefComponent(props) {
return render(props, props && props.ref ? props.ref : { current: null })
},
useContext(context) {
if (!context || !Array.isArray(context._stack)) return undefined
return context._stack.length > 0
? context._stack[context._stack.length - 1]
: context._defaultValue
},
useState(initial) {
const slot = useSlot()
if (!('value' in slot)) {
slot.value = typeof initial === 'function' ? initial() : initial
}
const setState = (next) => {
const value = typeof next === 'function' ? next(slot.value) : next
if (Object.is(value, slot.value)) return
slot.value = value
markDirty()
}
return [slot.value, setState]
},
useReducer(reducer, initialArg, init) {
const slot = useSlot()
if (!('value' in slot)) {
slot.value = typeof init === 'function' ? init(initialArg) : initialArg
}
const dispatch = (action) => {
const value = reducer(slot.value, action)
if (Object.is(value, slot.value)) return
slot.value = value
markDirty()
}
return [slot.value, dispatch]
},
useRef(initial) {
const slot = useSlot()
if (!('ref' in slot)) slot.ref = { current: initial }
return slot.ref
},
useEffect(effect, deps) {
const slot = useSlot()
if (!depsEqual(slot.deps, deps)) {
slot.deps = deps ? deps.slice() : undefined
const previousCleanup = slot.cleanup
effectQueue.push(() => {
if (typeof previousCleanup === 'function') previousCleanup()
const cleanup = effect()
slot.cleanup = typeof cleanup === 'function' ? cleanup : undefined
})
}
},
useLayoutEffect(effect, deps) {
React.useEffect(effect, deps)
},
useInsertionEffect(effect, deps) {
React.useEffect(effect, deps)
},
useMemo(factory, deps) {
const slot = useSlot()
if (!depsEqual(slot.deps, deps)) {
slot.deps = deps ? deps.slice() : undefined
slot.value = factory()
}
return slot.value
},
useCallback(fn, deps) {
const slot = useSlot()
if (!depsEqual(slot.deps, deps)) {
slot.deps = deps ? deps.slice() : undefined
slot.value = fn
}
return slot.value
},
useImperativeHandle(ref, create, deps) {
const slot = useSlot()
if (!depsEqual(slot.deps, deps)) {
slot.deps = deps ? deps.slice() : undefined
const value = create()
if (ref && typeof ref === 'object') ref.current = value
else if (typeof ref === 'function') ref(value)
}
},
useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot) {
const slot = useSlot()
if (!slot.subscribed) {
slot.subscribed = true
try {
slot.value = getSnapshot()
} catch (error) {
slot.value = typeof getServerSnapshot === 'function' ? getServerSnapshot() : undefined
}
if (typeof subscribe === 'function') {
try {
const unsubscribe = subscribe(() => {
let next
try {
next = getSnapshot()
} catch (error) {
return
}
if (!Object.is(next, slot.value)) {
slot.value = next
markDirty()
}
})
if (typeof unsubscribe === 'function') slot.unsubscribe = unsubscribe
} catch (error) {
// 预览里订阅失败不阻断渲染
}
}
}
return slot.value
},
useId() {
idCounter += 1
return `:pv${idCounter}:`
},
useDebugValue() {},
useTransition() { return [false, (callback) => { callback() }] },
useDeferredValue(value) { return value },
}
return {
React,
enter(key) {
keyStack.push(currentKey)
cursorStack.push(cursor)
currentKey = key
cursor = 0
},
leave() {
currentKey = keyStack.length > 0 ? keyStack.pop() : null
cursor = cursorStack.length > 0 ? cursorStack.pop() : 0
},
beginRender() {
cursor = 0
effectQueue = []
},
runEffects() {
const queue = effectQueue
effectQueue = []
for (const effect of queue) effect()
return queue.length
},
consumeDirty() {
const value = dirty
dirty = false
return value
},
instanceMap,
}
}
function loadBundle(sourcePath, hooks, options) {
if (!existsSync(sourcePath)) fail(`找不到 bundle:${sourcePath}`)
const source = readFileSync(sourcePath, 'utf8').replace(/^\uFEFF/, '')
if (!source.includes('__ModuleLoader__')) {
fail(`${sourcePath} 看起来不是 DSH 客户端 bundle(未找到 __ModuleLoader__)。`)
}
const { React } = hooks
const env = createBrowserEnv({
theme: options.theme,
fetchStub: createFetchStub(options.theme, (path, method) => {
log(`fetch 未命中固定数据:${method} ${path}(返回空对象/数组占位)`)
}),
})
const { document, window, styleTexts } = env
let loaded = null
window.__ModuleLoader__ = {
load(registration) {
loaded = registration
const module = { exports: {} }
const requireStub = (name) => {
if (name === 'react') return React
throw new Error(`预览加载器不认识的模块:${name}(只支持 react)`)
}
const exported = registration.factory(requireStub)
if (exported !== undefined) module.exports = exported
loaded.exports = module.exports
},
}
const sandbox = {
window,
document,
console,
URL,
URLSearchParams,
Map,
Set,
WeakMap,
WeakSet,
Promise,
Symbol,
Proxy,
Reflect,
Date,
Math,
JSON,
Number,
String,
Boolean,
Array,
Object,
Error,
TypeError,
RangeError,
RegExp,
Intl,
TextEncoder,
TextDecoder,
setTimeout: (callback, delay, ...args) => unrefTimer(setTimeout(callback, delay, ...args)),
clearTimeout,
setInterval: (callback, delay, ...args) => unrefTimer(setInterval(callback, delay, ...args)),
clearInterval,
setImmediate,
clearImmediate,
queueMicrotask,
structuredClone,
fetch: window.fetch,
localStorage: window.localStorage,
sessionStorage: window.sessionStorage,
matchMedia: window.matchMedia,
requestAnimationFrame: window.requestAnimationFrame,
cancelAnimationFrame: window.cancelAnimationFrame,
getComputedStyle: window.getComputedStyle,
ResizeObserver: env.ResizeObserver,
IntersectionObserver: env.IntersectionObserver,
MutationObserver: env.MutationObserver,
devicePixelRatio: window.devicePixelRatio,
innerWidth: window.innerWidth,
innerHeight: window.innerHeight,
navigator: window.navigator,
location: window.location,
addEventListener: window.addEventListener,
removeEventListener: window.removeEventListener,
}
sandbox.globalThis = sandbox
sandbox.self = sandbox
try {
vm.runInNewContext(source, sandbox, { filename: sourcePath, timeout: 30000 })
} catch (error) {
fail(`加载 bundle 失败(${sourcePath}):\n${error && error.stack ? error.stack : error}`)
}
if (!loaded || !loaded.exports) fail(`bundle 没有调用 __ModuleLoader__.load:${sourcePath}`)
// 保留 bundle 自己注册样式的插入顺序(后面的覆盖前面的),只做去重,
// 避免按长度排序改变级联顺序、把主题变量覆盖错。
const seen = new Set()
const cssParts = []
for (const text of styleTexts) {
if (!text || text.trim() === '' || seen.has(text)) continue
seen.add(text)
cssParts.push(text)
}
const css = cssParts.join('\n')
return {
exports: loaded.exports,
css,
sourcePath,
document,
window,
styleTexts,
}
}
// #endregion
// #region vnode → HTML
const VOID_TAGS = new Set([
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
'link', 'meta', 'param', 'source', 'track', 'wbr',
])
const ATTR_ALIASES = {
className: 'class',
htmlFor: 'for',
tabIndex: 'tabindex',
colSpan: 'colspan',
rowSpan: 'rowspan',
maxLength: 'maxlength',
minLength: 'minlength',
readOnly: 'readonly',
autoFocus: 'autofocus',
autoComplete: 'autocomplete',
viewBox: 'viewBox',
preserveAspectRatio: 'preserveAspectRatio',
strokeWidth: 'stroke-width',
strokeLinecap: 'stroke-linecap',
strokeLinejoin: 'stroke-linejoin',
strokeDasharray: 'stroke-dasharray',
fillRule: 'fill-rule',
clipRule: 'clip-rule',
xmlnsXlink: 'xmlns:xlink',
}
const BOOLEAN_ATTRS = new Set([
'disabled', 'checked', 'selected', 'hidden', 'readonly', 'required',
'multiple', 'open', 'autofocus', 'novalidate', 'formnovalidate',
])
function escapeText(value) {
return String(value)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
}
function escapeAttr(value) {
return String(value)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
}
function kebabStyleName(name) {
if (name.startsWith('--')) return name
if (name === 'msTransform') return '-ms-transform'
const prefixed = name.replace(/^(Webkit|Moz|O)(?=[A-Z])/, (match) => `-${match.toLowerCase()}`)
return prefixed.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`)
}
const UNITLESS_STYLES = new Set([
'line-height', 'opacity', 'font-weight', 'z-index', 'flex', 'flex-grow',
'flex-shrink', 'order', 'zoom', 'aspect-ratio', 'flex-basis',
])
function styleToString(style) {
if (!style || typeof style !== 'object') return ''
const parts = []
for (const [name, raw] of Object.entries(style)) {
if (raw === null || raw === undefined || raw === false) continue
const key = kebabStyleName(name)
let value = raw
if (typeof value === 'number' && value !== 0 && !UNITLESS_STYLES.has(key)) value = `${value}px`
parts.push(`${key}:${value}`)
}
return parts.join(';')
}
function buildAttributes(type, props) {
const attrs = []
for (const [rawName, rawValue] of Object.entries(props || {})) {
if (rawName === 'children' || rawName === 'key' || rawName === 'ref') continue
if (rawName === '__self' || rawName === '__source') continue
if (rawName === 'dangerouslySetInnerHTML') continue
if (rawName === 'value' && type === 'select') continue
if (rawName === 'value' && type === 'option') continue
if (rawName === 'selected' && type === 'option') continue
if (typeof rawValue === 'function' || typeof rawValue === 'symbol') continue
if (rawValue === undefined || rawValue === null) continue
let name = rawName
if (rawName === 'className') {
name = 'class'
} else if (ATTR_ALIASES[rawName]) {
name = ATTR_ALIASES[rawName]
} else if (!rawName.includes('-') && /[A-Z]/.test(rawName)) {
name = rawName.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`)
}
if (name === 'class') {
const value = Array.isArray(rawValue) ? rawValue.filter(Boolean).join(' ') : String(rawValue)
if (value) attrs.push(`class="${escapeAttr(value)}"`)
continue
}
if (name === 'style') {
const value = styleToString(rawValue)
if (value) attrs.push(`style="${escapeAttr(value)}"`)
continue
}
if (name.startsWith('aria-') || name.startsWith('data-')) {
attrs.push(`${name}="${escapeAttr(String(rawValue))}"`)
continue
}
if (name === 'value' && type === 'option') continue
if (BOOLEAN_ATTRS.has(name)) {
if (rawValue === true || rawValue === '') attrs.push(`${name}=""`)
continue
}
if (rawValue === false) continue
if (rawValue === true) {
attrs.push(`${name}=""`)
continue
}
attrs.push(`${name}="${escapeAttr(String(rawValue))}"`)
}
return attrs.length ? ` ${attrs.join(' ')}` : ''
}
function serializeNode(node, options = {}) {
if (!node) return ''
if (node.text !== undefined) return escapeText(node.text)
if (node.fragment) return node.children.map((child) => serializeNode(child, options)).join('')
const { type, props, children } = node
if (typeof type !== 'string') fail(`序列化遇到非字符串标签:${String(type)}`)
if (type === 'select') {
const attrs = buildAttributes(type, props)
const inner = children.map((child) => serializeNode(child, { selectValue: props.value })).join('')
return `<select${attrs}>${inner}</select>`
}
if (type === 'option') {
const attrs = []
for (const [name, value] of Object.entries(props || {})) {
if (name === 'children' || name === 'key' || name === 'selected' || name === 'value') continue
if (value === undefined || value === null || value === false || typeof value === 'function') continue
attrs.push(`${name}="${escapeAttr(String(value))}"`)
}
if (props && props.value !== undefined && props.value !== null) attrs.push(`value="${escapeAttr(String(props.value))}"`)
const selected = (props && props.selected === true)
|| (options.selectValue !== undefined && options.selectValue !== null && props && String(props.value) === String(options.selectValue))
if (selected) attrs.push('selected=""')
const inner = children.map((child) => serializeNode(child, options)).join('')
return `<option${attrs.length ? ` ${attrs.join(' ')}` : ''}>${inner}</option>`
}
const attrs = buildAttributes(type, props)
if (VOID_TAGS.has(type)) return `<${type}${attrs}>`
const rawHtml = props && props.dangerouslySetInnerHTML && typeof props.dangerouslySetInnerHTML.__html === 'string'
? props.dangerouslySetInnerHTML.__html
: null
const inner = rawHtml !== null
? rawHtml
: children.map((child) => serializeNode(child, options)).join('')
return `<${type}${attrs}>${inner}</${type}>`
}
function pickChildren(node) {
const positional = Array.isArray(node.children) ? node.children : []
const fromProps = node.props && node.props.children !== undefined ? node.props.children : undefined
const source = positional.length > 0 ? positional : fromProps === undefined ? [] : [fromProps]
const flat = []
for (const item of source) {
if (Array.isArray(item)) flat.push(...item)
else flat.push(item)
}
return flat
}
function mountNode(node, hooks, depth = 0, path = '0') {
if (depth > 200) fail('组件树递归超过 200 层,疑似存在渲染死循环。')
if (node === null || node === undefined || node === false || node === true) return null
if (typeof node === 'string' || typeof node === 'number') return { text: String(node) }
if (Array.isArray(node)) {
const children = []
for (let index = 0; index < node.length; index += 1) {
const mounted = mountNode(node[index], hooks, depth + 1, `${path}/a${index}`)
if (mounted) children.push(mounted)
}
return { fragment: true, children }
}
if (typeof node !== 'object' || node.type === undefined) {
fail(`无法渲染的节点:${JSON.stringify(node)}`)
}
const { type } = node
const props = node.props || {}
if (typeof type === 'function') {
if (type.__previewContext && type.__previewContext._stack) {
const context = type.__previewContext
context._stack.push(props.value)
try {
return mountNode(type(props), hooks, depth + 1, `${path}/ctx`)
} finally {
context._stack.pop()
}
}
if (type.__previewContext && type.__previewContext._stack === undefined) {
return mountNode(type(props), hooks, depth + 1, `${path}/ctx`)
}
const key = `${path}|${type.name || 'anonymous'}`
let output
if (type.prototype && typeof type.prototype.render === 'function') {
let instance = hooks.instanceMap.get(key)
if (!instance) {
instance = new type(props)
hooks.instanceMap.set(key, instance)
}
instance.props = props
output = instance.render()
if (typeof instance.componentDidMount === 'function') {
// 用 effect 队列在本次渲染后执行,避免在 render 里做副作用
output = output
hooks.pendingLifecycle = hooks.pendingLifecycle || []
hooks.pendingLifecycle.push(() => instance.componentDidMount())
}
} else {
hooks.enter(key)
try {
output = type(props)
} finally {
hooks.leave()
}
}
return mountNode(output, hooks, depth + 1, `${key}/r`)
}
if (type === 'fragment') {
const kids = pickChildren(node)
const children = []
for (let index = 0; index < kids.length; index += 1) {
const mounted = mountNode(kids[index], hooks, depth + 1, `${path}/f${index}`)
if (mounted) children.push(mounted)
}
return { fragment: true, children }
}
const kids = pickChildren(node)
const children = []
for (let index = 0; index < kids.length; index += 1) {
const mounted = mountNode(kids[index], hooks, depth + 1, `${path}/h${index}`)
if (mounted) children.push(mounted)
}
return { type, props, children }
}
// #endregion
// #region 旧 bundle 的演示 ctx(createHost 需要)
const DEMO_ROLES = SEATS.map((seat) => seat.roleId)
const DEMO_LABELS = Object.fromEntries(SEATS.map((seat) => [seat.roleId, seat.name]))
const DEMO_RUNNING = new Set(['shangshu', 'hubu', 'gongbu'])
const DEMO_STATUS_TEXT =
'Chrome、Edge 和内置 Node 都可用。我把「真实组件预览生成器」交给队友做,现在已经能出图了。'
const DEMO_NODES = new Map([
['demo-1', { kind: 'assistant', anchorSeq: 1, data: { blocks: [{ kind: 'text', text: DEMO_STATUS_TEXT }] } }],
])
function createDemoContext() {
const members = DEMO_ROLES.map((role) => ({ id: `sess-${role}`, name: role, phase: 'active' }))
const tasks = [
{ id: 'task-1', subject: '拆解 0.5.1 侧栏验收清单', status: 'in_progress', ownerName: 'zhongshu' },
{ id: 'task-2', subject: '复算朝堂统计投影', status: 'in_progress', ownerName: 'hubu' },
{ id: 'task-3', subject: '补齐右侧栏窄宽度回归', status: 'pending', ownerName: 'xingbu' },
{ id: 'task-4', subject: '整理 @ 部门入口说明', status: 'pending', ownerName: 'lipu' },
{ id: 'task-5', subject: '删除未渲染的旧样式', status: 'completed', ownerName: 'gongbu' },
{ id: 'task-6', subject: '核对十席角色文案', status: 'completed', ownerName: 'libu' },
]
const byId = {}
const ids = []
for (const role of DEMO_ROLES) {
const id = `sess-${role}`
ids.push(id)
byId[id] = {
id,
name: DEMO_LABELS[role],
updatedAt: 1_760_000_000_000 + DEMO_ROLES.indexOf(role),
running: DEMO_RUNNING.has(role),
workspaceId: 'demo-workspace',
}
}
byId[LEAD_SESSION] = { ...byId[LEAD_SESSION], subagent: undefined }
const team = { members, tasks }
const sessionList = {
getSnapshot: () => ({
ids: ids.slice(),
byId,
projectionsBySession: {
[LEAD_SESSION]: {
sessionId: LEAD_SESSION,
values: {
agentTeam: team,
agentPreset: 'sansheng-liubu',
subagentCatalog: [],
},
},
},
phase: 'ready',
}),
subscribe: () => () => {},
}
const statuses = new Map()
for (const role of DEMO_ROLES) {
statuses.set(`sess-${role}`, { running: DEMO_RUNNING.has(role) })
}
const statusStore = {
getSnapshot: () => statuses,
subscribe: () => () => {},
}
const catalogSnapshot = {
status: 'ready',
error: null,
current: { provider: 'openai', model: 'gpt-6-astra', reasoningEffort: 'high' },
default: { provider: 'openai', model: 'gpt-6-astra', reasoningEffort: 'high' },
groups: [
{
id: 'openai',
name: 'OpenAI',
models: [
{
id: 'gpt-6-astra',
name: 'GPT-6 Astra',
reasoning: {
defaultEffort: 'high',
efforts: [
{ id: 'low', name: '低' },
{ id: 'medium', name: '中' },
{ id: 'high', name: '高' },
],
},
},
{
id: 'gpt-6-mini',
name: 'GPT-6 Mini',
reasoning: {
defaultEffort: 'medium',
efforts: [
{ id: 'low', name: '低' },
{ id: 'medium', name: '中' },
],
},
},
],
},
{
id: 'deepseek',
name: 'DeepSeek',
models: [
{
id: 'deepseek-v4',
name: 'DeepSeek V4',
reasoning: { defaultEffort: 'high', efforts: [{ id: 'high', name: '高' }] },
},
],
},
],
}
const binding = {
session: {
projections: {
faceOf: () => ({ getSnapshot: () => ({ model: { name: 'GPT-6 Astra' } }) }),
},
prompt: async () => ({ ok: true }),
},
}
const ctx = {
effect(callback) {
const dispose = callback()
return typeof dispose === 'function' ? dispose : () => {}
},
sessions: {
list: sessionList,
statuses: statusStore,
binding: () => binding,
subagentAddress: (sessionId) => (sessionId === LEAD_SESSION ? undefined : { parentSessionId: LEAD_SESSION, childSessionId: sessionId }),
refreshProjections: async () => {},
},
uiWorkspace: {
selection: {
getSnapshot: () => ({ sessionId: LEAD_SESSION }),
subscribe: () => () => {},
set: () => {},
},
currentWorkspaceId: 'demo-workspace',
openSession: () => {},
startSession: () => {},
},
uiConversation: {
binding: () => ({
target: () => ({ getSnapshot: () => ({ nodes: DEMO_NODES }) }),
}),
},
modelDirectories: {
catalog: {
load: async () => ({
default: catalogSnapshot.current,
groups: catalogSnapshot.groups,
failures: [],
}),
store: { getSnapshot: () => ({ value: catalogSnapshot, status: 'ready', error: null }) },
},
directoryFor: () => ({
load: () => Promise.resolve(),
select: () => Promise.resolve({ ok: true }),
store: {
getSnapshot: () => catalogSnapshot,
subscribe: () => () => {},
},
}),
},
remote: {
agentPresets: {
list: async () => ({ ok: true, value: { presets: [] } }),
select: async () => ({ ok: true }),
},
session: {
selectModel: async () => ({ ok: true }),
},
commands: {
execute: async (_agentId, line) => ({ commandId: 'preview-command', result: { kind: 'success', text: line } }),
},
subagents: {
prompt: async () => ({ messageId: 'preview-msg' }),
},
},
get: () => undefined,
layout: { selectPanel: () => {} },
sidebarRight: { openTab: () => {}, openTabIn: () => {} },
sidebarRightTabs: { register: () => () => {} },
slots: { register: () => () => {}, inject: (name, callback) => callback() },
}
return { ctx, team, catalogSnapshot }
}
// #endregion
// #region 选择要渲染的组件
function pickComponent(exports) {
const keys = Object.keys(exports || {})
const preview = exports && exports.__preview
if (preview) {
if (typeof preview === 'function') {
return { component: preview, props: {}, mode: '__preview(function)' }
}
if (typeof preview === 'object') {
const component = preview.component || preview.default || preview.render
if (typeof component === 'function') {
return { component, props: preview.props || {}, mode: '__preview(object)' }
}
}
}
const preferred = [
'SanshengWorkbenchPanel',
'WorkbenchPanel',
'SanshengPanel',
'Workbench',
'Panel',
'default',
]
for (const name of preferred) {
const value = exports && exports[name]
if (typeof value === 'function') {
return { component: value, props: {}, mode: `exports.${name}` }
}
}
// 兜底:任何名字里带 panel/workbench 的函数组件
for (const name of keys) {
const value = exports[name]
if (typeof value === 'function' && /panel|workbench/i.test(name) && !/^create/.test(name)) {
return { component: value, props: {}, mode: `exports.${name}(name-match)` }
}
}
return { component: null, props: {}, mode: 'placeholder', keys }
}
// #endregion
// #region 渲染流程
async function tick(times = 4) {
for (let index = 0; index < times; index += 1) {
await new Promise((resolveTick) => setImmediate(resolveTick))
}
}
function flushLifecycle(hooks) {
const queue = hooks.pendingLifecycle || []
hooks.pendingLifecycle = []
for (const callback of queue) callback()
}
async function settle(hooks, rootVNode, budgetMs = 1500) {
const started = Date.now()
let tree = null
let passes = 0
let settled = false
while (passes < 80) {
passes += 1
hooks.beginRender()
tree = mountNode(rootVNode, hooks, 0, 'root')
hooks.runEffects()
flushLifecycle(hooks)
await tick(3)
if (passes > 6) await new Promise((resolveWait) => setTimeout(resolveWait, 4))
if (!hooks.consumeDirty()) {
// 给「effect 里再排一个短定时器」的组件一点时间:最多多等一个 60ms 窗口。
if (passes <= 3 && Date.now() - started < 400) {
await new Promise((resolveWait) => setTimeout(resolveWait, 60))
if (hooks.consumeDirty()) {
if (Date.now() - started > budgetMs) break
continue
}
}
settled = true
break
}
if (Date.now() - started > budgetMs) break
}
return { tree, passes, settled, elapsedMs: Date.now() - started }
}
function mountedText(node) {
if (!node) return ''
if (node.text !== undefined) return String(node.text)
if (node.fragment) return node.children.map(mountedText).join('')
const parts = []
if (node.props && typeof node.props.children === 'string') parts.push(node.props.children)
if (Array.isArray(node.children)) parts.push(node.children.map(mountedText).join(''))
return parts.join('')
}
function findMounted(node, predicate) {
if (!node) return null
if (predicate(node)) return node
if (node.fragment) {
for (const child of node.children) {
const hit = findMounted(child, predicate)
if (hit) return hit
}
return null
}
if (Array.isArray(node.children)) {
for (const child of node.children) {
const hit = findMounted(child, predicate)
if (hit) return hit
}
}
return null
}
function classNameOf(node) {
const value = node && node.props ? node.props.className : ''
return typeof value === 'string' ? value.split(/\s+/).filter(Boolean) : []
}
function clickMounted(node) {
if (!node || !node.props || typeof node.props.onClick !== 'function') return false
try {
node.props.onClick({
preventDefault() {},
stopPropagation() {},
target: {},
currentTarget: {},
})
return true
} catch (error) {
log(`模拟点击失败:${error && error.message ? error.message : error}`)
return false
}
}
/* 静态 HTML 里没有 React 运行时,Chrome 里点页签不会触发组件状态。
这里直接在替身的 vnode 上调 onClick,再重渲染一遍抓取目标页签/抽屉。 */
async function switchPreviewState(hooks, rootVNode, view, openText, initialTree) {
const labels = { court: '朝堂', tasks: '任务', artifacts: '成果' }
const label = labels[view]
const result = { clickedTab: null, openedArtifact: null }
if (!label) return { ...result, tree: initialTree, passes: 0 }
const tab = findMounted(initialTree, (node) => {
const classes = classNameOf(node)
return node.type === 'button' && classes.includes('wb-tab') && mountedText(node).includes(label)
})
if (!tab) {
log(`没有找到「${label}」页签,保持当前视图`)
return { ...result, tree: initialTree, passes: 0 }
}
clickMounted(tab)
let next = await settle(hooks, rootVNode, 1500)
let extraPasses = next.passes
result.clickedTab = label
if (openText) {
const artifact = findMounted(next.tree, (node) => {
const classes = classNameOf(node)
if (!classes.includes('wb-artifact')) return false
const haystack = `${mountedText(node)} ${node.props && node.props.title ? node.props.title : ''}`
return haystack.includes(openText)
})
if (!artifact) {
log(`成果页里没有匹配「${openText}」的行,抽屉不打开`)
} else if (clickMounted(artifact)) {
result.openedArtifact = openText
next = await settle(hooks, rootVNode, 1500)
extraPasses += next.passes
}
}
return { ...result, tree: next.tree, passes: extraPasses }
}
/* 在真实 Chrome 里按最终 DOM 的 rect 重算连线。
React 替身环境里 getBoundingClientRect 全是 0,bundle 的 measure() 会退到
computeRailLayout 的几何坐标;那套坐标和真实 CSS 布局(rail 内边距、
行高、卡片实际高度)不是同一套,端点会落在卡片上方。这里在页面加载后
用真浏览器的 rect 重写 4 条 path,公式与 bundle 的 branchPathD / trunk 完全一致:
分叉:M cx0 y0 V mid C cx0 y1 cx1 mid cx1 y1 (mid = y0 + (y1-y0)*0.55)
主干:M cx bottom L cx group.top
只跑一次(成功即 done),失败留给后续 tick 重试,不挂 MutationObserver。 */
const WIRE_RECOMPUTE = `
(function () {
var done = false;
function r(value) { return Math.round(Number(value) * 10) / 10; }
function rel(element, box) {
var rect = element.getBoundingClientRect();
return {
top: rect.top - box.top,
bottom: rect.bottom - box.top,
left: rect.left - box.left,
cx: rect.left - box.left + rect.width / 2,
width: rect.width,
height: rect.height
};
}
function recompute() {
if (done) return;
var tree = document.querySelector('.wb-tree');
if (!tree) return;
var svg = tree.querySelector('svg.wb-wires');
var leadEl = tree.querySelector('.wb-lead-row .wb-card');
var trioEls = tree.querySelectorAll('.wb-trio > .wb-card');
var groupEl = tree.querySelector('.wb-group');
var pathEls = tree.querySelectorAll('svg.wb-wires path');
if (!svg || !leadEl || trioEls.length < 3 || !groupEl || pathEls.length < 4) return;
var box = tree.getBoundingClientRect();
if (!box || !box.width || !box.height) return;
var lead = rel(leadEl, box);
var sansheng = [rel(trioEls[0], box), rel(trioEls[1], box), rel(trioEls[2], box)];
var group = rel(groupEl, box);
var before = [];
for (var i = 0; i < pathEls.length; i++) before.push(pathEls[i].getAttribute('d') || '');
var y0 = lead.bottom;
for (var j = 0; j < 3; j++) {
var y1 = sansheng[j].top;
if (!(y1 > y0)) return;
var mid = y0 + (y1 - y0) * 0.55;
pathEls[j].setAttribute('d',
'M ' + r(lead.cx) + ' ' + r(y0) +
' V ' + r(mid) +
' C ' + r(lead.cx) + ' ' + r(y1) + ' ' + r(sansheng[j].cx) + ' ' + r(mid) +
' ' + r(sansheng[j].cx) + ' ' + r(y1));
}
if (!(group.top > sansheng[1].bottom)) return;
pathEls[3].setAttribute('d',
'M ' + r(sansheng[1].cx) + ' ' + r(sansheng[1].bottom) +
' L ' + r(sansheng[1].cx) + ' ' + r(group.top));
svg.setAttribute('width', String(Math.round(box.width)));
svg.setAttribute('height', String(Math.round(box.height)));
var after = [];
for (var k = 0; k < pathEls.length; k++) after.push(pathEls[k].getAttribute('d') || '');
var report = {
applied: true,
box: { width: box.width, height: box.height },
lead: lead,
sansheng: sansheng,
group: group,
before: before,
after: after
};
var marker = document.getElementById('pv-wires');
if (!marker) {
marker = document.createElement('script');
marker.type = 'application/json';
marker.id = 'pv-wires';
document.body.appendChild(marker);
}
marker.textContent = JSON.stringify(report);
document.documentElement.setAttribute('data-pv-wires', 'applied');
done = true;
}
function tick() {
try { recompute(); } catch (error) {
document.documentElement.setAttribute('data-pv-wires', 'error:' + String(error && error.message || error));
}
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', tick);
else tick();
requestAnimationFrame(function () { requestAnimationFrame(tick); });
window.addEventListener('load', tick);
setTimeout(tick, 60);
setTimeout(tick, 250);
setTimeout(tick, 800);
})();
`
function buildPage({ css, panelHtml, width, theme }) {
const dark = theme === 'dark'
const shellCss = `
*{box-sizing:border-box}
html,body{margin:0;padding:0}
body{
background:#eef0f4;color:#1f2329;
font-family:"Segoe UI","Microsoft YaHei",-apple-system,BlinkMacSystemFont,"Helvetica Neue",Arial,sans-serif;
-webkit-font-smoothing:antialiased;
}
.pv-shell{width:${width}px;min-height:100vh;display:flex;flex-direction:column;background:#f7f8fa;overflow:hidden}
.pv-chat-head{
flex:0 0 auto;display:flex;align-items:center;gap:8px;
padding:10px 12px;background:#ffffff;border-bottom:1px solid #e2e6ec;
font-size:12px;line-height:18px;color:#5b6472;
}
.pv-chat-avatar{
width:24px;height:24px;border-radius:50%;
background:linear-gradient(135deg,#5b8def,#8a63f4);color:#fff;
display:flex;align-items:center;justify-content:center;font-size:11px;font-weight:600;
}
.pv-chat-title{font-weight:600;color:#1f2329;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.pv-chat-sub{margin-left:auto;font-size:11px;color:#8b93a1;white-space:nowrap}
.pv-panel{flex:1 1 auto;min-height:0;padding:10px 12px 16px;overflow:visible}
.pv-shell .sl-root{height:auto!important;min-height:0!important;background:transparent!important;color:inherit}
.pv-shell .sl-body{background:transparent}
.pv-shell .sl-body-sidebar{padding:0;overflow:visible}
.pv-shell .sl-body-sidebar .sl-right{overflow:visible;padding-right:0}
.pv-placeholder{
border:1px dashed #b6bfcc;border-radius:8px;padding:12px;background:#fff;color:#1f2329;
font-size:12px;line-height:1.5;
}
.pv-placeholder h3{margin:0 0 6px;font-size:13px}
.pv-placeholder pre{
margin:8px 0 0;padding:8px;background:#f2f4f7;border-radius:6px;
font-size:11px;white-space:pre-wrap;word-break:break-all;color:#475063;
}
html.dark body{background:#0e1014;color:#e8eaee}
html.dark .pv-shell{background:#111318}
html.dark .pv-chat-head{background:#16181d;border-bottom-color:#262a32;color:#a9b0bd}
html.dark .pv-chat-title{color:#e8eaee}
html.dark .pv-chat-sub{color:#7f8794}
html.dark .pv-placeholder{background:#1a1d24;border-color:#333a46;color:#e8eaee}
html.dark .pv-placeholder pre{background:#22262f;color:#c7cedb}
`
const darkOverride = dark
? `
/* 兜底:bundle 若用媒体查询切暗色,--blink-settings=preferredColorScheme=0 已生效;
若用 .dark 类名切暗色,这里保证外壳的语义变量也进入暗色。 */
html.dark{
color-scheme:dark;
--dsw-alias-bg-layer-1:#1f222a;
--dsw-alias-bg-layer-2:#16181d;
--dsw-alias-bg-layer-3:rgba(255,255,255,.10);
--dsw-alias-label-primary:#e8eaee;
--dsw-alias-label-secondary:#b7bcc6;
--dsw-alias-label-tertiary:#8b93a1;
--dsw-alias-label-caption:#7f8794;
--dsw-alias-border-l1:rgba(255,255,255,.12);
--dsw-alias-border-l2:rgba(255,255,255,.18);
--dsw-alias-border-l3:rgba(255,255,255,.28);
--dsw-alias-state-business-primary:#6b8afd;
--dsw-alias-state-error-primary:#f2777a;
}
`
: ''
const html = `<!doctype html>
<html lang="zh-CN"${dark ? ' class="dark" data-theme="dark"' : ''}>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=${width}, initial-scale=1">
<title>三省六部 · 工作台预览 (${theme} / ${width}px)</title>
<style>
${shellCss}
${css}
${darkOverride}
</style>
</head>
<body>
<div class="pv-shell" data-dsw-theme="${theme}" data-pv-width="${width}">
<header class="pv-chat-head">
<div class="pv-chat-avatar">三</div>
<div class="pv-chat-title">三省六部 · 工作台</div>
<div class="pv-chat-sub">${theme} · ${width}px</div>
</header>
<main class="pv-panel">
${panelHtml}
</main>
</div>
<script>
${WIRE_RECOMPUTE}
</script>
<div id="pv-height" hidden></div>
<script>
(function () {
function measure() {
var shell = document.querySelector('.pv-shell');
var shellHeight = shell ? Math.ceil(shell.getBoundingClientRect().height) : 0;
var height = Math.max(
document.documentElement.scrollHeight,
document.body.scrollHeight,
shellHeight
);
var marker = document.getElementById('pv-height');
if (marker) marker.textContent = String(height);
}
measure();
requestAnimationFrame(function () { measure(); });
setTimeout(measure, 80);
})();
</script>
</body>
</html>
`
return html
}
function measureHeight(chrome, htmlPath, width, theme, profileDir) {
const args = [
'--headless=new',
'--disable-gpu',
'--disable-crash-reporter',
'--disable-breakpad',
'--no-first-run',
'--hide-scrollbars',
`--window-size=${width},200`,
'--force-device-scale-factor=1',
'--virtual-time-budget=3000',
'--dump-dom',
`--user-data-dir=${profileDir}`,
]
if (theme === 'dark') args.push('--blink-settings=preferredColorScheme=0')
args.push(pathToFileURL(htmlPath).href)
const result = chromeRun(chrome, args, '量高')
const dom = String(result.stdout || '')
const match = /id="pv-height"[^>]*>([0-9]+)</.exec(dom)
if (!match) {
fail(`量高失败:页面里没有写回 #pv-height。dump-dom 前 500 字符:\n${dom.slice(0, 500)}`)
}
const measured = Number.parseInt(match[1], 10)
const height = Math.max(200, Math.min(4000, measured))
// 连线重算脚本把真实 rect 与 before/after d 写进 #pv-wires(JSON)。
// 旧 bundle 没有 .wb-tree,这个标记不存在,属正常情况。
let wires = null
const wireMatch = /id="pv-wires"[^>]*>([\s\S]*?)<\/script>/.exec(dom)
if (wireMatch) {
const raw = wireMatch[1]
.replace(/&quot;/g, '"')
.replace(/&amp;/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
try {
wires = JSON.parse(raw)
} catch (error) {
log(`连线重算结果解析失败:${error && error.message ? error.message : error}`)
}
}
return { height, wires, dom }
}
function screenshot(chrome, htmlPath, pngPath, width, height, theme, scale, profileDir) {
const args = [
'--headless=new',
'--disable-gpu',
'--disable-crash-reporter',
'--disable-breakpad',
'--no-first-run',
'--hide-scrollbars',
`--window-size=${width},${height}`,
`--force-device-scale-factor=${scale}`,
'--virtual-time-budget=2000',
`--screenshot=${pngPath}`,
`--user-data-dir=${profileDir}`,
]
if (theme === 'dark') args.push('--blink-settings=preferredColorScheme=0')
args.push(pathToFileURL(htmlPath).href)
chromeRun(chrome, args, '截图')
if (!existsSync(pngPath)) fail(`Chrome 没有写出截图:${pngPath}`)
const size = readPngSize(pngPath)
const expectedWidth = width * scale
if (size.width !== expectedWidth) {
fail(`截图宽度异常:期望 ${expectedWidth}px,实际 ${size.width}px(${pngPath})`)
}
if (size.bytes < 3000) {
fail(`截图疑似空白:${pngPath} 只有 ${size.bytes} 字节`)
}
return size
}
async function main() {
const options = parseArgs(process.argv.slice(2))
if (options.help) {
process.stdout.write(`${usage()}\n`)
return
}
const theme = String(options.theme)
if (theme !== 'light' && theme !== 'dark') fail(`--theme 只能是 light 或 dark,收到:${theme}`)
const width = Number.parseInt(options.width, 10)
if (!Number.isFinite(width) || width < 200 || width > 1200) fail(`--width 需要在 200–1200 之间,收到:${options.width}`)
const scale = Number.parseInt(options.scale, 10)
if (!Number.isFinite(scale) || scale < 1 || scale > 4) fail(`--scale 需要在 1–4 之间,收到:${options.scale}`)
const name = String(options.name)
if (!/^[A-Za-z0-9._-]+$/.test(name)) fail(`--name 只允许字母、数字、点、下划线和短横线,收到:${name}`)
const view = String(options.view || 'court')
if (!['court', 'tasks', 'artifacts'].includes(view)) fail(`--view 只能是 court / tasks / artifacts,收到:${view}`)
const openText = String(options.open || '')
const effectiveView = openText && view === 'court' ? 'artifacts' : view
const bundlePath = resolve(ROOT, options.bundle)
const outDir = resolve(ROOT, options.out)
ensureDir(SCRATCH)
ensureDir(outDir)
if (!existsSync(bundlePath)) {
fail(`bundle 不存在:${bundlePath}\n(相对路径按仓库根目录解析:${ROOT})`)
}
const pngPath = join(outDir, `${name}-${width}-${theme}.png`)
const htmlPath = join(SCRATCH, `${name}-${width}-${theme}.html`)
const profileDir = join(SCRATCH, 'chrome-profile')
ensureDir(profileDir)
log(`加载 bundle:${bundlePath}`)
const hooks = createHooks()
const loaded = loadBundle(bundlePath, hooks, { theme })
log(`bundle 导出键:${Object.keys(loaded.exports).join(', ') || '(空)'}`)
log(`捕获 CSS:${loaded.css.length} 字符`)
const picked = pickComponent(loaded.exports)
log(`渲染入口:${picked.mode}`)
let component = picked.component
let props = { ...picked.props }
let placeholder = false
if (typeof component !== 'function') {
placeholder = true
const keyList = (picked.keys || Object.keys(loaded.exports)).join(', ') || '(空)'
log(`没有找到面板组件,退化为占位 div。bundle 导出键:${keyList}`)
component = () => hooks.React.createElement(
'div',
{ className: 'pv-placeholder' },
hooks.React.createElement('h3', null, '预览占位'),
hooks.React.createElement('p', null, '这个 bundle 没有导出 __preview,也没有可识别的默认面板组件。'),
hooks.React.createElement('pre', null, `导出键:${keyList}`),
)
props = {}
} else if (typeof loaded.exports.createHost === 'function') {
try {
const demo = createDemoContext()
props = {
...props,
host: loaded.exports.createHost(demo.ctx),
sessionId: LEAD_SESSION,
compact: true,
}
log('已用 createHost(ctx) 构造旧式 host')
} catch (error) {
log(`createHost 失败,改为直接传 props:${error && error.message ? error.message : error}`)
}
}
if (props.projectId === undefined) props.projectId = 'prj_demo1'
if (props.sessionId === undefined) props.sessionId = LEAD_SESSION
if (props.compact === undefined) props.compact = true
const rootVNode = hooks.React.createElement(component, props)
const settleResult = await settle(hooks, rootVNode, 1500)
log(`渲染稳定:${settleResult.settled ? '是' : '否(达到 1.5s 预算)'},${settleResult.passes} 轮,${settleResult.elapsedMs}ms`)
let tree = settleResult.tree
let stateSwitch = { clickedTab: null, openedArtifact: null }
if (effectiveView !== 'court' || openText) {
const switched = await switchPreviewState(hooks, rootVNode, effectiveView, openText, tree)
tree = switched.tree
stateSwitch = { clickedTab: switched.clickedTab, openedArtifact: switched.openedArtifact }
settleResult.passes += switched.passes
log(`视图切换:${stateSwitch.clickedTab || '(未切换)'}${stateSwitch.openedArtifact ? `,已打开成果「${stateSwitch.openedArtifact}」` : ''}`)
}
const panelHtml = serializeNode(tree)
const html = buildPage({ css: loaded.css, panelHtml, width, theme })
writeFileSync(htmlPath, html, 'utf8')
log(`HTML:${htmlPath}(${html.length} 字符,面板 DOM ${panelHtml.length} 字符)`)
const chrome = findChrome(options.chrome)
log(`Chrome:${chrome}`)
const measured = measureHeight(chrome, htmlPath, width, theme, profileDir)
const height = measured.height
log(`页面 scrollHeight:${height}px`)
const wires = measured.wires
if (wires && wires.applied) {
log(`连线重算:已按真实 DOM rect 重写 ${wires.after.length} 条 path`)
log(` 分叉[0] before:${wires.before[0]}`)
log(` 分叉[0] after :${wires.after[0]}`)
log(` 丞相底边 y=${wires.lead.bottom},三省顶边 y=${wires.sansheng.map((card) => card.top).join('/')},六部组顶边 y=${wires.group.top}`)
log(` 端点:分支终点 y 与卡片 top 相等;主干终点 y 与 group.top 相等(${wires.group.top})`)
} else {
log('连线重算:页面里没有 .wb-tree 或未写入 #pv-wires(旧 bundle / 占位渲染属正常)')
}
const size = screenshot(chrome, htmlPath, pngPath, width, height, theme, scale, profileDir)
log(`截图:${pngPath}(${size.width}x${size.height},${size.bytes} 字节,scale=${scale})`)
const sourceLines = readFileSync(fileURLToPath(import.meta.url), 'utf8').split(/\r?\n/).length
const report = {
bundle: bundlePath,
bundleBase: options.bundle,
bundleBytes: statSync(bundlePath).size,
entry: picked.mode,
placeholder,
theme,
width,
scale,
view: effectiveView,
openedArtifact: stateSwitch.openedArtifact,
html: htmlPath,
png: pngPath,
pngSize: { width: size.width, height: size.height, bytes: size.bytes },
scrollHeight: height,
passes: settleResult.passes,
settled: settleResult.settled,
elapsedMs: settleResult.elapsedMs,
cssLength: loaded.css.length,
exportKeys: Object.keys(loaded.exports),
wireRecompute: wires && wires.applied
? {
applied: true,
box: wires.box,
branchBefore: wires.before[0],
branchAfter: wires.after[0],
trunkBefore: wires.before[3],
trunkAfter: wires.after[3],
leadBottom: wires.lead.bottom,
sanshengTops: wires.sansheng.map((card) => card.top),
groupTop: wires.group.top,
}
: null,
scriptLines: sourceLines,
}
process.stdout.write(`PREVIEW_RESULT ${JSON.stringify(report)}\n`)
}
main().catch((error) => {
if (error && error.previewFailure) {
process.stderr.write(`\n[preview-workbench] 失败:${error.message}\n`)
} else {
process.stderr.write(`\n[preview-workbench] 未预期错误:${error && error.stack ? error.stack : error}\n`)
}
process.exitCode = 1
})