dsh-brain/docs/plans/UI_SPEC.md

260 lines
17 KiB
Markdown
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.

# UI 改造详细实施规格(给 DeepSeek 执行)
版本:v1 · 2026-11-24 23:40
基线:5fef328(main,已推送)。开工前 git status 必须干净。
决策:B 借壳不借芯 + bundle id 彻底改名 dsh-brain-ui(用户拍板)。
本规格是 UI_TASKS.md 的展开版:每卡给出文件级改动、可抄源码位置、代码骨架、验收命令。
冲突保护:GPT 部门复验未结束前,**禁改** src/memory/departments-tools.ts、
src/settings-ui/departments-routes.ts、src/skill-sync.ts、src/local-memory/departments/。
(可以 import 它们的导出,如 departmentSnippet。)
═══════════════════════════════════════════
U0 · client 构建链落地(0.5–1 天)
═══════════════════════════════════════════
## 现状事实(已实测)
- client/index.js:3735 行手写 IIFE,window.__ModuleLoader__.load({id:'dsh-brain',factory})。
- devDependencies 已含 tsdown ^0.22.2、typescript ^5.6.0、react ^18.2.0、@types/react。
**不需要新增任何依赖。**
- vendor/dsh-mnemon/tsdown.config.ts 的 client 段是现成模板(banner/intro/footer、
css modules 内联插件 clientCssPlugin、react externals)。
- 现有 client 结构图(迁移按此切模块):
L22 API 常量 / L49 request / L67 postJson / L71 messageOf / L79 bridgeCall(MCP)
L108-235 格式化与标签表 / L246-612 styles 对象 / L613-762 基础组件(Pill,Banner,Field,Btn,IconBtn,Switch,Toggle,Section)
L774 MarketSection / L929 SyncResolveSection / L1038 McpCustomSection / L1127 McpConfigSection
L1259 ImportSection / L1551 SettingsCard(主体,含 connection/skills/mcp 三页签) / L3680 CardBoundary / L3704 apply
## 改动清单
1. 新建 src-client/ 目录,把上述结构迁为 TS 模块(行为等价优先,不重构逻辑):
src-client/index.ts ← 入口,导出 {apply, inject:['slots'], name:'dsh-brain'}
src-client/api.ts ← API 常量 + request/postJson/messageOf/bodyError
src-client/format.ts ← formatTime/formatIso/formatBytes/shortHash/readStored/store + 标签表
src-client/components.tsx ← Pill/Banner/Field/Btn/IconBtn/Switch/Toggle/Section/CardBoundary
src-client/bridge.ts ← bridgeCall/bridgeMessage(MCP 宿主桥探测)
src-client/sections/*.tsx ← MarketSection/SyncResolveSection/McpCustomSection/McpConfigSection/ImportSection
src-client/SettingsCard.tsx ← 主体(三页签状态机原样搬)
src-client/styles.ts ← 现 styles 对象暂原样保留(U1 起新样式走 .module.css)
迁移规则:var→const/let、function 组件补 props 类型(宽松 any 起步,后续收紧)、
h=React.createElement 保留或改 jsx(tsdown jsx:'automatic' 需 react/jsx-runtime external,
建议保留 h 写法减少差异)。
2. 新建根 tsdown.config.ts,只含 client 段(host lib 仍走 tsc,不动):
```ts
import type { UserConfig } from 'tsdown'
const PLUGIN_ID = 'dsh-brain'
export default {
name: PLUGIN_ID + '/client',
entry: { index: 'src-client/index.ts' },
outDir: 'client',
format: 'cjs',
platform: 'browser',
target: 'es2022',
fixedExtension: false,
outExtensions: () => ({ js: '.js' }),
dts: false, sourcemap: false, clean: false,
deps: { neverBundle: [/^react(?:\/.*)?$/, /^react-dom(?:\/.*)?$/] },
define: { 'process.env.NODE_ENV': '"production"' },
plugins: [clientCssPlugin()], // U1 再启用;U0 可先空数组
outputOptions: {
banner: `window.__ModuleLoader__.load({ id: '${PLUGIN_ID}', factory: (require) => {`,
intro: 'var module = { exports: {} }; var exports = module.exports;',
footer: 'return module.exports; } });',
},
} satisfies UserConfig
```
css 插件从 vendor/dsh-mnemon/tsdown.config.ts 的 clientCssPlugin 抄(lightningcss
transform → style 注入),vendor 已是 devDep 链内可用?——**核对 lightningcss 是否
已在 node_modules(tsdown 自带依赖)**;若不可直接 import,用 tsdown 内置 css 处理
(modules 选项)替代,产物同样内联。U0 阶段无 css 文件,插件可后置到 U1。
3. package.json scripts:
"build:client": "tsdown"
"build": "tsc -p tsconfig.build.json && tsdown"
"prepack": "tsc -p tsconfig.build.json && tsdown"
tsconfig.json / tsconfig.build.json 的 include 排除 src-client(避免 host tsc 双编译);
新增 tsconfig.client.json(extends 根,include src-client,jsx react-jsx,noEmit true)
并把 typecheck 改为 "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.client.json --noEmit"。
4. 旧手写版备份:cp client/index.js artifacts/analysis/client-legacy.js(artifacts 已 gitignore)。
5. 新增 verify-client-build.mjs(仓库根,harness 风格照抄 verify-departments.mjs):
- 读 client/index.js 断言:含 __ModuleLoader__.load、id:'dsh-brain'、
'settings.section'、'plugins.bundle.config'、三页签标签(连接与上报/技能管理/MCP 管理)。
- 用 vm + react shim require 产物,调 apply({slots:{inject:(seat,cb)=>{…记录}}}),
断言两个 seat 注册回调可触发、组件渲染不抛(react shim 用 node_modules/react 真身,
createElement 返回普通对象即可,不需要 DOM)。
- 与 artifacts/analysis/client-legacy.js 做"行为面"对比:两版 apply 注册的 seat/options
JSON 形状一致(order/id/key/label() 输出)。
## 验收
npm run typecheck && npm run build && node verify-client-build.mjs
node scripts/verify-all.mjs # 39 脚本口径不回退
git status:client/index.js 变更 + src-client/ 新增 + 配置,全部入库。
commit:build(client): migrate hand-written bundle to tsdown with equivalent output
═══════════════════════════════════════════
U1 · 左栏总面板骨架(1 天)
═══════════════════════════════════════════
## 可抄源码(vendor/dsh-mnemon/src/client/,MIT 保留声明)
- shared/store.ts ExternalStore(~40 行,整抄改前缀)
- shared/dom-seat.ts HeldSeat + createRafCoalescer(整抄)
- native-sidebar.tsx mountMnemonSidebarNavigation 的双 seat 注册模式:
ctx.slots.inject('main', () => ctx.slots.inject('sidebar.panellist', () => {
register main {name:'main', key:PANEL_ID, inject:()=>({…})}
register panellist {name:'sidebar.panellist', id:PANEL_ID, order:30, label:()=>'Brain'}
}))
**简化**:Mnemon 的 fallback DOM 注入(mountMnemonSidebarEntry/coordinateSidebarPanels)
不抄——seat 不可用时本项目回退设置卡+独立页,不做 DOM 接管。
- MnemonWorkspace.module.css(2KB 全文已核):nativeWorkspace/windowDragBand/entry 系列
抄骨架,data-dsh-brain-mnemon-* 属性改 data-dsh-brain-*;配色全走
--dsw-alias-* token(verify-theme 口径:无纯白纯黑硬编码)。
- panelInfo 激活判定:ctx.layout.panelInfo.{getSnapshot,subscribe},
activePanelId !== PANEL_ID 时暂停轮询(照抄 MnemonMainPanel 的 usePanelInfo)。
## 新文件
src-client/panel/seat.ts ← ExternalStore/HeldSeat(抄改)
src-client/panel/register.ts ← registerBrainPanel(ctx): 双 seat + disposer;
PANEL_ID='dsh-brain';order 30;
glyph svg data-dsh-panel-entry="dsh-brain";
seat 缺席静默(inject 回调不触发即降级)
src-client/panel/BrainPanel.tsx ← 页签壳:概览/记忆/部门/技能/MCP/连接与上报;
本轮实现"概览"+"连接与上报",其余占位;
根 div data-dsh-plugin="dsh-brain" + windowDragBand
src-client/panel/Overview.tsx ← GET /dsh-brain/status + /capabilities 聚合卡片
(版本、五 bundle 开关态、本地记忆状态、同步统计),
15s 轮询仅激活时跑
src-client/panel/BrainPanel.module.css ← 抄 vendor css 骨架
src-client/panel/brain-icon.tsx ← 脑形 glyph(参考 vendor memory-icon.tsx 简化自绘)
## 改动
- src-client/index.ts apply:保留 settings.section/plugins.bundle.config 注册;
新增 registerBrainPanel(ctx)(经 ctx.effect 挂 disposer)。
- SettingsCard 瘦身:settings.section 的卡内容改为状态摘要 + "打开总面板"按钮:
onClick → typeof ctx.layout?.selectPanel==='function' ? ctx.layout.selectPanel('dsh-brain')
: window.open('/dsh-brain')。原三页签完整内容迁入 BrainPanel 的对应页签
(connection 本轮搬,skills/mcp 在 U2 后随占位替换——**本轮先把现有 SettingsCard
整体作为"连接与上报"页签内容复用,避免大改**)。
- inject 声明:['slots'] 不变(layout 经 ctx.get 探测,不硬依赖)。
## 验收
verify-client-build.mjs 扩展:panellist/main 注册断言、PANEL_ID、order 30、
降级分支(无 seat 时 apply 不抛)、glyph 语义锚点字符串。
typecheck + build + verify-all 全绿。
commit:feat(ui): left-rail Brain panel with overview and connection tabs
═══════════════════════════════════════════
U2 · 记忆/部门页签(1 天,可与 U3 并行)
═══════════════════════════════════════════
## API 契约(已实测响应形状)
GET /dsh-brain/local-memory/status → {ok,provider:'local',globalRoot,projectRoot,spaces:{global,project},cloudSyncEnabled,localMemoryEnabled}
GET /dsh-brain/local-memory/spaces → {ok,spaces:[…]}
GET /dsh-brain/local-memory/documents → {ok,documents:[…]}
GET /dsh-brain/local-memory/memories?limit&offset → {ok,total,items:[{id,scope,content,category,tags,source,revision,updatedAt}]}
POST /dsh-brain/local-memory/forget {id,expectedRevision?} → 同源闸门
GET /dsh-brain/departments/list?scope → {ok,departments:[Department]}
POST /dsh-brain/departments/create {name,role?,scope?,skills?,allowedTools?,readGlobal?,readProject?,workspace?}
POST /dsh-brain/departments/update {id,…patch,expectedRevision?,workspace?}
POST /dsh-brain/departments/disable {id,expectedRevision?,workspace?}
POST /dsh-brain/departments/checkpoint {id,currentTask?,handoff?,expectedRevision?,workspace?}
GET /dsh-brain/departments/recall?id&query&k&workspace → {ok,hits:[{origin,record,content,contentSnippet}]}
POST /dsh-brain/departments/resume {id,host?,hostSessionId?,workspace?}
错误码:400 name-required/invalid-json;403 origin-rejected;404 department_not_found;
409 revision_conflict/department_disabled;503 local-memory-disabled/departments-unavailable。
## 新文件
src-client/panel/MemoryTab.tsx ← 空间状态卡 + 记忆列表(分页 limit 50)+ 前端搜索过滤
+ forget 按钮(确认弹窗)+ 文档列表只读
src-client/panel/DepartmentTab.tsx ← 部门列表(name/scope/active/currentTask/revision)
+ 创建表单 + 详情抽屉(update/disable/checkpoint)
+ recall 查询框(展示 contentSnippet,标注 origin)
+ 409 → "数据已变化,请刷新" 提示条
src-client/panel/useApi.ts ← 轮询/加载状态机助手(idle/loading/ready/error),
错误条复用 Banner;visible=false 暂停
workspace 取值:ctx.get('sessions') 面探测当前会话 cwd(参考 vendor session-binding.ts
的 useMnemonSessionId 模式,简化为一次性读取);取不到则省略参数。
## 约束
- 零新后端路由;不改 departments-routes.ts(保护期)。
- recall k 输入框 max=5;snippet 展示直接用服务端 contentSnippet。
- 所有 POST 走 postJson(同源自动满足)。
## 验收
verify-client-build 断言两页签组件存在、API 路径字符串齐全;
手工冒烟:起真实 server(参考 verify-brain-panel.mjs 夹具)核对每个消费路由 200。
typecheck + build + verify-all 全绿。
commit:feat(ui): memory and department tabs in the Brain panel
═══════════════════════════════════════════
U3 · better-sidebar 项目记忆页签(0.5 天)
═══════════════════════════════════════════
## 可抄源码
vendor/dsh-mnemon/src/client/better-sidebar.tsx(全文 2.4KB 已核):
mountBetterSidebarTab 的 reconcile 模式——ctx.get('betterSidebar') 探测 +
ctx.on('internal/service', name => name==='betterSidebar' && reconcile()) 热插拔 +
disposer 链。**整抄改前缀**,descriptor 换成本项目:
{ id:'dsh-brain:memory', title:()=>'项目记忆',
description:'当前会话项目的本地记忆:空间/条目/搜索/部门召回',
icon:size=><BrainIcon size={size}/>, order:60, single:true,
component:({scope,visible}) => <ProjectMemoryView scope={scope} visible={visible}/> }
## 新文件
src-client/sidebar/register.ts ← mountBrainSidebarTab(ctx)(抄改)
src-client/sidebar/ProjectMemoryView.tsx ← 只读视图:status + memories 列表 + 搜索
+ recall 查询(部门选择下拉);
visible=false 暂停轮询(契约);
重操作按钮"去总面板"→ selectPanel('dsh-brain')
src-client/sidebar/ProjectMemory.module.css ← betterSidebarSeat 骨架抄 vendor
## 降级链
无 betterSidebar 服务 → reconcile 不注册,静默;服务后到 → internal/service 触发补注册;
fiber 卸载 → disposer 自动清理。总面板不受影响。
## 验收
verify-client-build 断言:registerTab 探测、descriptor id/order/single、
internal/service 监听、visible 暂停分支、无服务时 apply 不抛。
typecheck + build + verify-all 全绿。
commit:feat(ui): project-memory tab for better-sidebar with graceful fallback
═══════════════════════════════════════════
U4 · 彻底改名 + 迁移 + 终验(0.5 天)
═══════════════════════════════════════════
## 改名清单(一处不漏,已实测现状)
1. src/entries/settings-ui.ts → src/entries/ui.ts:
export name='dsh-brain-ui';kernel.activate('ui')/deactivate('ui');
effect 标签 'dsh-brain-ui: …';头注释更新。
2. src/kernel.ts L48:BrainBundle 类型 'settings-ui'→'ui';L5 注释同步。
3. package.json exports:加 './ui':'./lib/entries/ui.js';
**保留 './settings-ui':'./lib/entries/ui.js'(别名指向新文件)**。
4. cordis.patch.yml:insert 行 id:'dsh-brain-ui',name:'dsh-brain/ui'。
5. src/index.ts compat root:import * as ui from './entries/ui.ts';ui.apply(...)。
6. src/settings-ui/ 目录暂不改名(departments-routes.ts 保护期);
entry 内 import 路径不变。目录搬迁留待复验通过后单独 commit。
7. 旧 id 迁移探测:ui entry apply 开头读宿主 patch 面(若 ctx 暴露)或
logger.info 提示"若你曾禁用 dsh-brain-settings-ui,请在设置→插件重新禁用 dsh-brain-ui"。
**不自动改写宿主文件**。README 升级节写明。
8. verify-plugin-entry.mjs:import './lib/entries/ui.js';断言文案改 ui;
加一条兼容断言:import './lib/entries/settings-ui.js'(exports 别名)仍可用。
9. README 五行表 id 列、docs/management-surface.md 提及同步。
10. lib/ 产物:build 后 lib/entries/ui.js 新增;lib/entries/settings-ui.js 由
exports 别名兜底——**物理文件也保留一份 re-export**(export * from './ui.js'),
防老 profile 直接路径解析绕过 exports map。
## 终验(全绿才报完成)
npm run typecheck && npm run build
node scripts/verify-all.mjs && node verify-plugin-entry.mjs && node verify-client-build.mjs
npm pack --dry-run # files 含 client/index.js、lib/entries/ui.js、lib/entries/settings-ui.js
宿主冒烟(升级 profile 依赖后另行授权,不在本卡):
左栏 Brain 行/总面板六页签/better-sidebar 页签/设置卡/独立页/legacy 别名 六处核对。
commit:refactor(ui)!: rename settings-ui bundle to dsh-brain-ui with export alias
═══════════════════════════════════════════
全局硬约束
═══════════════════════════════════════════
- 运行时零新依赖;tsdown/react 均已在 devDeps。
- client 产物单文件 __ModuleLoader__ IIFE,id:'dsh-brain' 不变(row id 才改)。
- 保护期禁改四文件(见头部);可 import 其导出。
- 每卡独立 commit + 该卡验证命令;lib/ 与 client/index.js 产物随卡入库。
- 样式走 --dsw-alias-* token,无纯白纯黑硬编码(verify-theme 会查)。
- 与现状不符:停下报告差异,等规划侧修订,不自行改方案。
- vendor 源码抄改处保留 MIT 出处注释(// Adapted from dsh-mnemon (MIT), see vendor/dsh-mnemon/LICENSE)。