dsh-lean-router 1.0.9 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,14 @@
1
1
  # Changelog
2
2
 
3
- > English version of [changelog.md](./changelog.md). Versions follow package.json (currently 1.0.9).
3
+ > English version of [changelog.md](./changelog.md). Versions follow package.json (currently 1.1.0).
4
+
5
+ ## 1.1.0 (2026-11-25)
6
+
7
+ - Changed (surface, decided by the user): the skills panel moves from "header button + body overlay" to a **right sidebar tab**. The overlay had to be opened, covered the conversation and could not sit side by side, so it was never a usable form; the 1.0.9 `#lean-router-panel` overlay is dropped entirely.
8
+ - New: the right-sidebar contract follows the real examples `@modusensus/dsh-mneme` and `dsh-cost-meter` - the tab type and its guide entry go to `sidebarRightTabs.register({ id, kind, title, guide })` (checked with `get(id)` first, because the host throws `tab type id … is already registered`), and the body and title go to `sidebar.right.pane.tab` / `sidebar.right.pane.tab.title` with a `{ name, key }` spec.
9
+ - New: the header "Skills" button stays as a shortcut and opens the tab through `ctx.get("sidebarRight").openTab(kind)`.
10
+ - Fixed: the opener now resolves `sidebarRight` from the scoped ctx delivered by `ctx.inject(["slots"])`, so it cannot hit the "service not registered yet" timing trap again.
11
+ - Constraint: nothing is appended to `document.body` any more; this version is the shell only (a title and two lines), and every style inherits host theme tokens instead of hard-coded colours. 224 tests green.
4
12
 
5
13
  ## 1.0.9 (2026-11-24)
6
14
 
package/docs/changelog.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # 变更记录(changelog)
2
2
 
3
- > 中文为主,英文版:[changelog.en.md](./changelog.en.md)。版本号遵循 package.json(当前 1.0.9)。
3
+ > 中文为主,英文版:[changelog.en.md](./changelog.en.md)。版本号遵循 package.json(当前 1.1.0)。
4
+
5
+ ## 1.1.0 (2026-11-25)
6
+
7
+ - 变更(形态,用户拍板):技能面板从「顶栏按钮 + body 浮层」改为**右侧栏标签页**。浮层要点开、会遮挡主对话、不能并排,本身就不是可用形态,1.0.9 的 `#lean-router-panel` 浮层实现整体废弃。
8
+ - 新增:按真机范例 `@modusensus/dsh-mneme` 与 `dsh-cost-meter` 的契约接入右栏 —— 类型与引导挂 `sidebarRightTabs.register({ id, kind, title, guide })`(注册前先 `get(id)` 查重,宿主对重复 id 会抛 `tab type id … is already registered`);正文与标题挂 `sidebar.right.pane.tab` / `sidebar.right.pane.tab.title`,spec 用 `{ name, key }` 关联类型。
9
+ - 新增:顶栏「技能」按钮保留为快捷入口,点击走 `ctx.get("sidebarRight").openTab(kind)` 直接打开右栏标签。
10
+ - 修复:opener 改用 `ctx.inject(["slots"])` 就绪后的 scoped ctx 取 `sidebarRight`,避免又踩「服务还没注册」的时机坑。
11
+ - 约束:不再往 `document.body` 追加任何元素;当前版本只做壳(标题 + 两行说明),样式全部继承宿主主题 token,不写死颜色。224 测试全绿。
4
12
 
5
13
  ## 1.0.9 (2026-11-24)
6
14
 
@@ -20,7 +20,7 @@ After restarting DSH, pick the **Lean router** preset (lean tier) or **Lean rout
20
20
  ### Path B: npm install (recommended)
21
21
 
22
22
  ~~~powershell
23
- dsh plugin --profile desktop add npm:dsh-lean-router@1.0.9
23
+ dsh plugin --profile desktop add npm:dsh-lean-router@1.1.0
24
24
  ~~~
25
25
 
26
26
  (Both paths match the output of scripts/install.mjs; the real installer, claim boundaries and `--uninstall` rollback belong to ticket 09.)
@@ -20,7 +20,7 @@ dsh plugin --profile desktop add "link:D:\path\to\dsh-lean-router"
20
20
  ### 路径 B:npm 安装(推荐)
21
21
 
22
22
  ~~~powershell
23
- dsh plugin --profile desktop add npm:dsh-lean-router@1.0.9
23
+ dsh plugin --profile desktop add npm:dsh-lean-router@1.1.0
24
24
  ~~~
25
25
 
26
26
  (两条路径与 scripts/install.mjs 输出的说明一致;真安装器、认领边界与 `--uninstall` 回滚归票 09。)
package/lib/client.js CHANGED
@@ -1,13 +1,17 @@
1
- // dsh-lean-router - 客户端 UI 层(技能面板)
1
+ // dsh-lean-router - 客户端 UI 层(技能面板 · 右侧栏标签页)
2
2
  //
3
- // 1.0.5 修(真机两轮验证后):
4
- // 1. 原实现 require("react-dom") + ReactDOM.createPortal —— 参考实现 dsh-cost-meter 只
5
- // require("react"),从不 require react-dom。宿主若不提供 react-dom,factory 立刻抛错,
6
- // 整个客户端模块加载失败,按钮不出现且无任何可见症状。改为不依赖 react-dom,
7
- // 面板用 position:fixed 直接渲染在按钮旁边。
8
- // 2. 原实现用 ctx.slots.register(...);cost-meter 用的是
9
- // ctx.inject(slotName, () => ctx.register(spec, Component))。改为优先走后者,保留回退。
10
- // 3. apply 不再是 generator:1.0.2 曾因 apply 返回值不符合 cordis effect 契约而整条预设失效。
3
+ // 形态(用户拍板):右侧栏 tab,不做浮层、不往 document.body 追加第二份应用。
4
+ // 契约来源:已安装真机范例 @modusensus/dsh-mneme 的 lib/client.js 与 dsh-cost-meter。
5
+ // 1) 类型与引导挂服务 sidebarRightTabs: tabs.register({ id, kind, title, guide })
6
+ // 2) 正文与标题挂 slot: sidebar.right.pane.tab / sidebar.right.pane.tab.title,
7
+ // spec 用 { name, key: TAB_ID, inject }(注意是 key 不是 id)
8
+ // 3) 打开: ctx.get("sidebarRight").openTab(TAB_KIND)
9
+ //
10
+ // 踩过的坑(别再犯):
11
+ // - 直接读 ctx.register 会抛 cannot get property "register" without inject(1.0.6)
12
+ // - apply 早于 slots 服务就绪,ctx.get("slots") 是 undefined(1.0.7)
13
+ // 所以一律用 ctx.inject(["slots"], cb) 等服务就绪后再注册。
14
+ // - 面板不要渲染在顶栏 slot 内(会被祖先 transform/overflow 裁成黑条,1.0.9)
11
15
  //
12
16
  // 加载契约:package.json 的 dsh.client.platform="web" + exports["./client"],
13
17
  // 入口形如 window.__ModuleLoader__.load({ id, factory }),factory(require) 取宿主模块。
@@ -17,6 +21,10 @@ window.__ModuleLoader__.load({
17
21
  const React = require("react");
18
22
  const h = React.createElement;
19
23
 
24
+ const TAB_ID = "lean-router.skills";
25
+ const TAB_KIND = "lean-router-skills";
26
+ const TAB_TITLE = "技能";
27
+
20
28
  const btnStyle = {
21
29
  display: "inline-flex",
22
30
  alignItems: "center",
@@ -26,242 +34,161 @@ window.__ModuleLoader__.load({
26
34
  marginLeft: 6,
27
35
  borderRadius: 6,
28
36
  border: "1px solid var(--dsh-border, rgba(128,128,128,.35))",
29
- background: "var(--dsh-surface, transparent)",
37
+ background: "transparent",
30
38
  color: "inherit",
31
39
  fontSize: 12,
32
40
  lineHeight: "24px",
33
41
  cursor: "pointer",
34
42
  };
35
43
 
36
- const overlayStyle = {
37
- position: "fixed",
38
- inset: 0,
39
- zIndex: 2147483000,
40
- display: "flex",
41
- alignItems: "center",
42
- justifyContent: "center",
43
- background: "rgba(0,0,0,.45)",
44
- };
45
-
46
- const cardStyle = {
47
- position: "relative",
48
- width: "min(920px, 92vw)",
49
- maxHeight: "82vh",
50
- overflow: "auto",
51
- padding: "20px 22px",
52
- borderRadius: 12,
53
- background: "var(--dsh-surface, #1b1b1f)",
54
- color: "var(--dsh-text, inherit)",
55
- boxShadow: "0 18px 60px rgba(0,0,0,.45)",
56
- fontSize: 13,
57
- lineHeight: 1.6,
58
- };
59
-
60
- const closeStyle = {
61
- position: "absolute",
62
- top: 10,
63
- right: 12,
64
- border: "none",
65
- background: "transparent",
66
- color: "inherit",
67
- fontSize: 18,
68
- lineHeight: "20px",
69
- cursor: "pointer",
70
- opacity: 0.7,
71
- };
72
-
73
- /** 把 React 风格对象转成 cssText,DOM 浮层复用同一套样式。 */
74
- function cssOf(style) {
75
- return Object.entries(style)
76
- .map(([key, value]) => key.replace(/[A-Z]/g, (ch) => "-" + ch.toLowerCase()) + ":" + value)
77
- .join(";");
78
- }
79
-
80
- // 1.0.9 修(真机):面板原来用 React 渲染在顶栏 slot 内,position:fixed 会被祖先的
81
- // transform / overflow 限制在那一行里,只剩一条黑条。改为挂到 document.body 的纯 DOM
82
- // 浮层,slot 里只留按钮。
83
- let panelEl = null;
84
-
85
- function closePanel() {
86
- if (panelEl) {
87
- panelEl.remove();
88
- panelEl = null;
89
- }
90
- }
91
-
92
- function buildPanel() {
93
- const overlay = document.createElement("div");
94
- overlay.id = "lean-router-panel";
95
- overlay.style.cssText = cssOf(overlayStyle);
96
- const card = document.createElement("div");
97
- card.style.cssText = cssOf(cardStyle);
98
- const close = document.createElement("button");
99
- close.type = "button";
100
- close.textContent = "✕";
101
- close.title = "关闭";
102
- close.style.cssText = cssOf(closeStyle);
103
- const title = document.createElement("div");
104
- title.textContent = "技能面板 · 骨架";
105
- title.style.cssText = "font-size:15px;font-weight:600;margin-bottom:8px";
106
- const body = document.createElement("div");
107
- body.style.cssText = "opacity:.75";
108
- body.innerHTML =
109
- "链路已通:这个面板由 dsh-lean-router 的客户端插件渲染。<br>" +
110
- "阶段 2 接入 31 个技能与审计统计,阶段 3 加分组表格、开关与一键工作流。";
111
- close.addEventListener("click", (event) => {
112
- event.stopPropagation();
113
- closePanel();
114
- });
115
- card.addEventListener("click", (event) => event.stopPropagation());
116
- overlay.addEventListener("click", closePanel);
117
- card.appendChild(close);
118
- card.appendChild(title);
119
- card.appendChild(body);
120
- overlay.appendChild(card);
121
- return overlay;
44
+ /** 右栏标签图标:三条横线,取 currentColor 跟随宿主主题。 */
45
+ function SkillsIcon(props) {
46
+ const size = props && props.size ? props.size : 16;
47
+ return h(
48
+ "svg",
49
+ { width: size, height: size, viewBox: "0 0 16 16", fill: "none", "aria-hidden": "true" },
50
+ h("path", {
51
+ d: "M3 4h10M3 8h10M3 12h6",
52
+ stroke: "currentColor",
53
+ strokeWidth: 1.5,
54
+ strokeLinecap: "round",
55
+ })
56
+ );
122
57
  }
123
58
 
124
- function togglePanel() {
125
- if (panelEl) {
126
- closePanel();
127
- return;
128
- }
129
- if (typeof document === "undefined" || !document.body) return;
130
- panelEl = buildPanel();
131
- document.body.appendChild(panelEl);
59
+ /** 右栏正文(1.0.10 只做壳,验证形态)。样式全部继承宿主主题,不写死颜色。 */
60
+ function SkillsPane() {
61
+ return h(
62
+ "div",
63
+ { style: { padding: "12px 14px", fontSize: 13, lineHeight: 1.7 } },
64
+ h("div", { style: { fontWeight: 600, marginBottom: 6 } }, "技能面板 · 骨架"),
65
+ h(
66
+ "div",
67
+ { style: { opacity: 0.75 } },
68
+ "右栏形态已接好:这是 lean-router 的右侧栏标签页,可以和其他右栏面板并存。"
69
+ ),
70
+ h(
71
+ "div",
72
+ { style: { opacity: 0.75, marginTop: 6 } },
73
+ "下一步接入 31 个技能、开关与命中统计。"
74
+ )
75
+ );
132
76
  }
133
77
 
134
- /** 顶栏「技能」按钮:点开/关闭 body 级浮层面板。 */
135
- function SkillsButton() {
78
+ /** 顶栏「技能」按钮:打开右栏标签(宿主无右栏服务时什么也不做,只告警)。 */
79
+ function SkillsButton(props) {
80
+ const open = props && typeof props.open === "function" ? props.open : () => {};
136
81
  return h(
137
82
  "button",
138
- { type: "button", style: btnStyle, title: "lean-router 技能面板", onClick: togglePanel },
83
+ { type: "button", style: btnStyle, title: "打开技能面板(右侧栏)", onClick: open },
139
84
  "技能"
140
85
  );
141
86
  }
142
87
 
143
- /**
144
- * 纯 DOM 兜底(零 React 依赖):右上角一个固定定位的「技能」按钮。
145
- * 只在 slot 注册全部失败时使用,用来区分失败类型。
146
- */
147
- function domFallback() {
148
- if (typeof document === "undefined" || !document.body) return;
149
- if (document.getElementById("lean-router-dom-fallback")) return;
150
- const btn = document.createElement("button");
151
- btn.id = "lean-router-dom-fallback";
152
- btn.type = "button";
153
- btn.textContent = "技能";
154
- btn.title = "lean-router 兜底按钮(slot 注册失败)";
155
- btn.style.cssText =
156
- "position:fixed;top:8px;right:12px;z-index:2147483647;height:26px;padding:0 10px;" +
157
- "border-radius:6px;border:1px solid rgba(128,128,128,.45);background:rgba(30,30,36,.92);" +
158
- "color:#fff;font-size:12px;line-height:24px;cursor:pointer;box-shadow:0 4px 16px rgba(0,0,0,.35)";
159
- let panel = null;
160
- btn.addEventListener("click", () => {
161
- if (panel) {
162
- panel.remove();
163
- panel = null;
164
- return;
88
+ /** 打开右栏标签;返回是否打开成功。 */
89
+ function makeOpener(ctx) {
90
+ return () => {
91
+ try {
92
+ const sidebar = typeof ctx.get === "function" ? ctx.get("sidebarRight") : undefined;
93
+ if (sidebar && typeof sidebar.openTab === "function") {
94
+ sidebar.openTab(TAB_KIND);
95
+ return true;
96
+ }
97
+ console.warn("[lean-router] sidebarRight.openTab 不可用,右栏无法打开");
98
+ } catch (err) {
99
+ console.warn("[lean-router] sidebarRight.openTab failed", err);
165
100
  }
166
- panel = document.createElement("div");
167
- panel.style.cssText =
168
- "position:fixed;top:44px;right:12px;z-index:2147483647;width:min(420px,92vw);" +
169
- "padding:16px 18px;border-radius:10px;background:rgba(30,30,36,.97);color:#fff;" +
170
- "font:13px/1.6 system-ui,sans-serif;box-shadow:0 18px 60px rgba(0,0,0,.5)";
171
- panel.innerHTML =
172
- "<div style='font-weight:600;margin-bottom:6px'>技能面板 · 兜底渲染</div>" +
173
- "<div style='opacity:.75'>客户端 bundle 已加载,但 slot 注册没成功。" +
174
- "这说明问题在 slot API,不在加载链路。</div>";
175
- document.body.appendChild(panel);
176
- });
177
- document.body.appendChild(btn);
101
+ return false;
102
+ };
178
103
  }
179
104
 
180
- // 0.2.3 真机事实(对照 dsh-cost-meter 的 lib/client.js):
181
- // const slots = ctx.get("slots")
182
- // slots.inject(slotName, () => slots.register({ name, id, order }, Component))
183
- // `slots` / `register` 都是客户端 cordis 的服务;直接读 ctx.register / ctx.slots 会抛
184
- // Error: cannot get property "register" without inject。取服务的唯一安全姿势是 ctx.get(name)。
185
- //
186
- // 1.0.7 修(真机):旧代码把 `typeof ctx.register === "function"` 写在 try 之外,属性访问
187
- // 直接抛错 → 整个 apply 失败 → 连 DOM 兜底都跑不到(顶栏没按钮的真因,控制台原文:
188
- // dsh-lean-router: failed: Error: cannot get property "register" without inject)。
189
- // 1.0.8 修(真机):apply 执行得比 slots 服务注册还早。此时 ctx.get("slots") 是 undefined,
190
- // 旧代码直接走 DOM 兜底,而那时 document.body 还不存在 → domFallback 静默 return,
191
- // 于是「bundle 已加载、控制台无报错、界面上什么都没有」。
192
- // 正确姿势与 dsh-cost-meter 用 e.inject(["locale"], …) 一致:ctx.inject(["slots"], cb)
193
- // 等服务就绪后再注册,服务变化时还会重跑。
194
- const SLOTS = ["conversation.session.header.actions", "conversation.session.header.utilities"];
195
-
196
105
  function apply(ctx) {
197
106
  const disposers = [];
198
- let registered = false;
199
107
 
200
- const registerInto = (slots) => {
201
- for (const slot of SLOTS) {
202
- // inject 是 slot 的 props 工厂:cost-meter 每次 register 都带,这里给空对象即可。
203
- const spec = { name: slot, id: "lean-router.skills", order: 60, inject: () => ({}) };
204
- try {
205
- const dispose = slots.inject(slot, () => slots.register(spec, SkillsButton));
206
- if (typeof dispose === "function") disposers.push(dispose);
207
- registered = true;
208
- return true;
209
- } catch (err) {
210
- try {
211
- console.warn("[lean-router] register failed on", slot, err);
212
- } catch {}
213
- }
108
+ const registerSlots = (scope) => {
109
+ const slots = scope && typeof scope.get === "function" ? scope.get("slots") : undefined;
110
+ if (!slots || typeof slots.inject !== "function" || typeof slots.register !== "function") {
111
+ return false;
214
112
  }
215
- return false;
216
- };
217
113
 
218
- const attempt = (scope) => {
219
- let slots;
114
+ // 1) 右栏 tab 的类型与引导入口。类型挂在宿主服务上、插件重挂时仍留着,
115
+ // 所以必须先查重,否则宿主会抛 tab type id already registered。
220
116
  try {
221
- slots = scope && typeof scope.get === "function" ? scope.get("slots") : undefined;
117
+ const tabs = typeof scope.get === "function" ? scope.get("sidebarRightTabs") : undefined;
118
+ if (tabs && typeof tabs.register === "function") {
119
+ const already = typeof tabs.get === "function" ? tabs.get(TAB_ID) : null;
120
+ if (!already) {
121
+ tabs.register({
122
+ id: TAB_ID,
123
+ kind: TAB_KIND,
124
+ title: () => TAB_TITLE,
125
+ guide: [
126
+ {
127
+ id: "lean-router:skills:guide",
128
+ order: 60,
129
+ title: () => TAB_TITLE,
130
+ description: () => "技能清单、开关与命中统计",
131
+ icon: (size) => h(SkillsIcon, { size }),
132
+ },
133
+ ],
134
+ });
135
+ }
136
+ }
222
137
  } catch (err) {
223
- try {
224
- console.warn("[lean-router] ctx.get('slots') failed", err);
225
- } catch {}
138
+ console.warn("[lean-router] sidebarRightTabs.register failed", err);
226
139
  }
227
- if (slots === undefined || typeof slots.inject !== "function" || typeof slots.register !== "function") {
228
- return false;
140
+
141
+ // 2) 顶栏入口 + 右栏正文与标题。opener 必须基于已就绪的 scope,
142
+ // 用外层 ctx 取 sidebarRight 可能还拿不到(1.0.8 同款时机问题)。
143
+ const open = makeOpener(scope);
144
+ const registrations = [
145
+ [
146
+ "conversation.session.header.actions",
147
+ () => ({ name: "conversation.session.header.actions", id: "lean-router.skills", order: 60, inject: () => ({ open }) }),
148
+ SkillsButton,
149
+ ],
150
+ [
151
+ "sidebar.right.pane.tab",
152
+ () => ({
153
+ name: "sidebar.right.pane.tab",
154
+ key: TAB_ID,
155
+ inject: (sessionId) => ({ sessionId }),
156
+ }),
157
+ SkillsPane,
158
+ ],
159
+ [
160
+ "sidebar.right.pane.tab.title",
161
+ () => ({ name: "sidebar.right.pane.tab.title", key: TAB_ID }),
162
+ () => TAB_TITLE,
163
+ ],
164
+ ];
165
+
166
+ let registered = 0;
167
+ for (const [slot, spec, component] of registrations) {
168
+ try {
169
+ const dispose = slots.inject(slot, () => slots.register(spec(), component));
170
+ if (typeof dispose === "function") disposers.push(dispose);
171
+ registered += 1;
172
+ } catch (err) {
173
+ console.warn("[lean-router] register failed on", slot, err);
174
+ }
229
175
  }
230
- return registerInto(slots);
176
+ return registered > 0;
231
177
  };
232
178
 
233
179
  if (typeof ctx.inject === "function") {
234
180
  try {
235
181
  const dispose = ctx.inject(["slots"], (scoped) => {
236
- attempt(scoped);
182
+ registerSlots(scoped);
237
183
  });
238
184
  if (typeof dispose === "function") disposers.push(dispose);
239
185
  } catch (err) {
240
- try {
241
- console.warn("[lean-router] ctx.inject(['slots']) failed", err);
242
- } catch {}
186
+ console.warn("[lean-router] ctx.inject(['slots']) failed", err);
243
187
  }
244
188
  } else {
245
- attempt(ctx);
189
+ registerSlots(ctx);
246
190
  }
247
191
 
248
- // 兜底:2 秒后仍没注册上,就在右上角插一个纯 DOM 按钮。
249
- // 目的不是最终形态,而是让「没挂上」这件事可见,不要再静默消失。
250
- try {
251
- const timer = setTimeout(() => {
252
- if (registered) return;
253
- try {
254
- domFallback();
255
- } catch (err) {
256
- try {
257
- console.warn("[lean-router] dom fallback failed", err);
258
- } catch {}
259
- }
260
- }, 2000);
261
- if (timer && typeof timer.unref === "function") timer.unref();
262
- disposers.push(() => clearTimeout(timer));
263
- } catch {}
264
-
265
192
  return () => {
266
193
  for (const d of disposers) {
267
194
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-lean-router",
3
- "version": "1.0.9",
3
+ "version": "1.1.0",
4
4
  "description": "lean 省 token × 思考链路由 × code_run 工具遮蔽 × mattpocock 技能自动调用的 DSH cordis 插件(单 bundle:预设+配套技能)",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -1,11 +1,10 @@
1
- // dsh-lean-router —— 客户端插件(lib/client.js)的服务获取、时机与渲染回归。
2
- // 真机事故链:
3
- // 1.0.6: 直接读 ctx.register 抛 cannot get property "register" without inject,apply 整体失败。
4
- // 1.0.7: 改成 ctx.get("slots"),但 apply 早于 slots 服务注册,拿到 undefined 后兜底又因
5
- // document.body 未建立而静默返回 → 无按钮、无报错。
6
- // 1.0.8: 用 ctx.inject(["slots"], cb) 等服务就绪,按钮出现。
7
- // 1.0.9: 面板原先渲染在顶栏 slot 内,position:fixed 被祖先 transform/overflow 裁成一条黑条,
8
- // 改为挂到 document.body 的纯 DOM 浮层。
1
+ // dsh-lean-router —— 客户端插件(lib/client.js)契约回归。
2
+ // 形态演进:浮层 → 右栏标签页(用户 2026-11-25 拍板)。
3
+ // 契约来源:真机范例 @modusensus/dsh-mneme + dsh-cost-meter。
4
+ // 类型/引导: ctx.get("sidebarRightTabs").register({ id, kind, title, guide })
5
+ // 正文/标题: sidebar.right.pane.tab / .title,spec 用 { name, key, inject }
6
+ // 打开: ctx.get("sidebarRight").openTab(kind)
7
+ // 踩过的坑:直接读 ctx.register 会抛;apply 早于 slots 就绪时 ctx.get("slots") 为 undefined。
9
8
  import test from "node:test";
10
9
  import assert from "node:assert/strict";
11
10
 
@@ -25,14 +24,7 @@ function makePlugin() {
25
24
  });
26
25
  }
27
26
 
28
- function makeSlots(registered) {
29
- return {
30
- inject(name, cb) { cb(); return () => {}; },
31
- register(spec, component) { registered.push({ spec, component }); return () => {}; },
32
- };
33
- }
34
-
35
- /** 访问 slots / register 属性就会抛错的 ctx,复刻客户端 cordis 的服务保护。 */
27
+ /** 访问 register / slots 属性就会抛错的 ctx,复刻客户端 cordis 的服务保护。 */
36
28
  function throwingGetters(extra) {
37
29
  return Object.assign({
38
30
  get register() { throw new Error('cannot get property "register" without inject'); },
@@ -40,83 +32,85 @@ function throwingGetters(extra) {
40
32
  }, extra);
41
33
  }
42
34
 
43
- /** 够用的假 DOM:支持 createElement / appendChild / remove / addEventListener。 */
44
- function makeFakeDocument() {
45
- const body = { tag: "body", children: [], appendChild(node) { this.children.push(node); node.parent = this; } };
46
- const doc = {
47
- body,
48
- getElementById() { return undefined; },
49
- createElement(tag) {
50
- return {
51
- tag, style: {}, children: [], parent: null,
52
- addEventListener(type, fn) { this[type] = fn; },
53
- appendChild(node) { this.children.push(node); node.parent = this; },
54
- remove() { if (this.parent) { this.parent.children = this.parent.children.filter((x) => x !== this); this.parent = null; } },
55
- };
56
- },
35
+ /** 组一套假服务:slots / sidebarRightTabs / sidebarRight。 */
36
+ function makeServices(options = {}) {
37
+ const registered = [];
38
+ const tabTypes = [];
39
+ const opened = [];
40
+ const slots = {
41
+ inject(name, cb) { cb(); return () => {}; },
42
+ register(spec, component) { registered.push({ spec, component }); return () => {}; },
57
43
  };
58
- return doc;
44
+ const tabs = {
45
+ register(def) { tabTypes.push(def); },
46
+ get() { return options.existingTabType === true ? { id: "lean-router.skills" } : null; },
47
+ };
48
+ const sidebar = { openTab(kind) { opened.push(kind); } };
49
+ const map = { slots, sidebarRightTabs: tabs, sidebarRight: sidebar };
50
+ return { registered, tabTypes, opened, map };
59
51
  }
60
52
 
61
- test("用 ctx.inject(['slots'], cb) 等服务就绪后注册到头栏 slot", () => {
62
- const registered = [];
63
- const slots = makeSlots(registered);
64
- let asked = null;
53
+ function applyWith(services) {
65
54
  const ctx = throwingGetters({
66
- inject(deps, cb) { asked = deps; cb(throwingGetters({ get: (name) => (name === "slots" ? slots : undefined) })); return () => {}; },
55
+ inject(deps, cb) { cb(throwingGetters({ get: (name) => services.map[name] })); return () => {}; },
67
56
  get() { return undefined; },
68
57
  });
69
58
  const dispose = makePlugin().apply(ctx);
70
- assert.equal(typeof dispose, "function");
71
- assert.deepEqual(asked, ["slots"]);
72
- assert.equal(registered.length, 1);
73
- assert.equal(registered[0].spec.name, "conversation.session.header.actions");
74
- assert.equal(registered[0].spec.id, "lean-router.skills");
75
- assert.equal(typeof registered[0].spec.inject, "function", "spec 需要 inject props 工厂");
59
+ assert.equal(typeof dispose, "function", "apply 必须返回 disposer");
60
+ return services;
61
+ }
62
+
63
+ test("注册右栏 tab 类型,含 id / kind / title / guide", () => {
64
+ const s = applyWith(makeServices());
65
+ assert.equal(s.tabTypes.length, 1);
66
+ assert.equal(s.tabTypes[0].id, "lean-router.skills");
67
+ assert.equal(s.tabTypes[0].kind, "lean-router-skills");
68
+ assert.equal(s.tabTypes[0].title(), "技能");
69
+ assert.ok(Array.isArray(s.tabTypes[0].guide) && s.tabTypes[0].guide.length === 1);
76
70
  });
77
71
 
78
- test("没有 ctx.inject 时退化为直接取 slots 注册", () => {
79
- const registered = [];
80
- const slots = makeSlots(registered);
81
- const ctx = throwingGetters({ get: (name) => (name === "slots" ? slots : undefined) });
82
- const dispose = makePlugin().apply(ctx);
83
- assert.equal(typeof dispose, "function");
84
- assert.equal(registered.length, 1);
72
+ test("tab 类型已存在时不重复注册(宿主对重复 id 会抛)", () => {
73
+ const s = applyWith(makeServices({ existingTabType: true }));
74
+ assert.equal(s.tabTypes.length, 0);
85
75
  });
86
76
 
87
- test("点按钮把面板挂到 body(不在 slot 内渲染),再点一次移除", () => {
88
- const registered = [];
89
- const slots = makeSlots(registered);
90
- const ctx = throwingGetters({
91
- inject(deps, cb) { cb(throwingGetters({ get: (name) => (name === "slots" ? slots : undefined) })); return () => {}; },
92
- get() { return undefined; },
93
- });
94
- makePlugin().apply(ctx);
95
- globalThis.document = makeFakeDocument();
96
- try {
97
- const Button = registered[0].component;
98
- const element = Button({});
99
- assert.equal(element.type, "button");
100
- element.props.onClick();
101
- assert.equal(document.body.children.length, 1, "面板必须挂到 body");
102
- assert.equal(document.body.children[0].id, "lean-router-panel");
103
- element.props.onClick();
104
- assert.equal(document.body.children.length, 0, "再点一次必须移除");
105
- } finally {
106
- delete globalThis.document;
107
- }
77
+ test("注册三个座位:顶栏入口 / 右栏正文 / 右栏标题,正文用 key 关联类型", () => {
78
+ const s = applyWith(makeServices());
79
+ const names = s.registered.map((r) => r.spec.name);
80
+ assert.deepEqual(names, [
81
+ "conversation.session.header.actions",
82
+ "sidebar.right.pane.tab",
83
+ "sidebar.right.pane.tab.title",
84
+ ]);
85
+ const pane = s.registered.find((r) => r.spec.name === "sidebar.right.pane.tab");
86
+ assert.equal(pane.spec.key, "lean-router.skills");
87
+ const title = s.registered.find((r) => r.spec.name === "sidebar.right.pane.tab.title");
88
+ assert.equal(title.spec.key, "lean-router.skills");
89
+ });
90
+
91
+ test("点顶栏按钮走 sidebarRight.openTab(kind)", () => {
92
+ const s = applyWith(makeServices());
93
+ const header = s.registered.find((r) => r.spec.name === "conversation.session.header.actions");
94
+ const element = header.component(header.spec.inject());
95
+ assert.equal(element.type, "button");
96
+ element.props.onClick();
97
+ assert.deepEqual(s.opened, ["lean-router-skills"]);
98
+ });
99
+
100
+ test("没有 ctx.inject 时退化为直接注册(老宿主兼容)", () => {
101
+ const services = makeServices();
102
+ const ctx = throwingGetters({ get: (name) => services.map[name] });
103
+ const dispose = makePlugin().apply(ctx);
104
+ assert.equal(typeof dispose, "function");
105
+ assert.equal(services.registered.length, 3);
108
106
  });
109
107
 
110
- test("slots 一直不出现时,2 秒后插入纯 DOM 兜底按钮(不再静默)", async () => {
111
- globalThis.document = makeFakeDocument();
108
+ test("不再往 document.body 追加任何东西(浮层形态已废弃)", () => {
109
+ const children = [];
110
+ globalThis.document = { body: { appendChild(node) { children.push(node); } }, getElementById() { return undefined; }, createElement() { return { style: {}, addEventListener() {} }; } };
112
111
  try {
113
- const ctx = throwingGetters({ inject() { return () => {}; }, get() { return undefined; } });
114
- const dispose = makePlugin().apply(ctx);
115
- assert.equal(typeof dispose, "function");
116
- assert.equal(document.body.children.length, 0, "兜底要等 2 秒,不能立刻插");
117
- await new Promise((resolve) => setTimeout(resolve, 2200));
118
- assert.equal(document.body.children.length, 1, "2 秒后必须插兜底按钮");
119
- assert.equal(document.body.children[0].id, "lean-router-dom-fallback");
112
+ applyWith(makeServices());
113
+ assert.equal(children.length, 0);
120
114
  } finally {
121
115
  delete globalThis.document;
122
116
  }