dsh-lean-router 1.0.3 → 1.0.5

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.
package/lib/client.js ADDED
@@ -0,0 +1,213 @@
1
+ // dsh-lean-router - 客户端 UI 层(技能面板)
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 契约而整条预设失效。
11
+ //
12
+ // 加载契约:package.json 的 dsh.client.platform="web" + exports["./client"],
13
+ // 入口形如 window.__ModuleLoader__.load({ id, factory }),factory(require) 取宿主模块。
14
+ window.__ModuleLoader__.load({
15
+ id: "dsh-lean-router",
16
+ factory: (require) => {
17
+ const React = require("react");
18
+ const h = React.createElement;
19
+
20
+ const btnStyle = {
21
+ display: "inline-flex",
22
+ alignItems: "center",
23
+ gap: 4,
24
+ height: 26,
25
+ padding: "0 10px",
26
+ marginLeft: 6,
27
+ borderRadius: 6,
28
+ border: "1px solid var(--dsh-border, rgba(128,128,128,.35))",
29
+ background: "var(--dsh-surface, transparent)",
30
+ color: "inherit",
31
+ fontSize: 12,
32
+ lineHeight: "24px",
33
+ cursor: "pointer",
34
+ };
35
+
36
+ const overlayStyle = {
37
+ position: "fixed",
38
+ inset: 0,
39
+ zIndex: 9999,
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
+ /** 技能面板(骨架;阶段 2/3 接真实数据)。 */
74
+ function SkillsPanel(props) {
75
+ return h(
76
+ "div",
77
+ { style: overlayStyle, onClick: props.onClose },
78
+ h(
79
+ "div",
80
+ { style: cardStyle, onClick: (event) => event.stopPropagation() },
81
+ h("button", { type: "button", style: closeStyle, onClick: props.onClose, title: "关闭" }, "✕"),
82
+ h("div", { style: { fontSize: 15, fontWeight: 600, marginBottom: 8 } }, "技能面板 · 骨架"),
83
+ h(
84
+ "div",
85
+ { style: { opacity: 0.75 } },
86
+ "链路已通:这个面板由 dsh-lean-router 的客户端插件渲染。",
87
+ h("br"),
88
+ "阶段 2 接入 31 个技能与审计统计,阶段 3 加分组表格、开关与一键工作流。"
89
+ )
90
+ )
91
+ );
92
+ }
93
+
94
+ /** 顶栏「技能」按钮:点开/关闭面板。 */
95
+ function SkillsButton() {
96
+ const [open, setOpen] = React.useState(false);
97
+ const close = React.useCallback(() => setOpen(false), []);
98
+ return h(
99
+ React.Fragment,
100
+ null,
101
+ h(
102
+ "button",
103
+ {
104
+ type: "button",
105
+ style: btnStyle,
106
+ title: "lean-router 技能面板",
107
+ onClick: () => setOpen((value) => !value),
108
+ },
109
+ "技能"
110
+ ),
111
+ open ? h(SkillsPanel, { onClose: close }) : null
112
+ );
113
+ }
114
+
115
+ /**
116
+ * 纯 DOM 兜底(零 React 依赖):右上角一个固定定位的「技能」按钮。
117
+ * 只在 slot 注册全部失败时使用,用来区分失败类型。
118
+ */
119
+ function domFallback() {
120
+ if (typeof document === "undefined" || !document.body) return;
121
+ if (document.getElementById("lean-router-dom-fallback")) return;
122
+ const btn = document.createElement("button");
123
+ btn.id = "lean-router-dom-fallback";
124
+ btn.type = "button";
125
+ btn.textContent = "技能";
126
+ btn.title = "lean-router 兜底按钮(slot 注册失败)";
127
+ btn.style.cssText =
128
+ "position:fixed;top:8px;right:12px;z-index:2147483647;height:26px;padding:0 10px;" +
129
+ "border-radius:6px;border:1px solid rgba(128,128,128,.45);background:rgba(30,30,36,.92);" +
130
+ "color:#fff;font-size:12px;line-height:24px;cursor:pointer;box-shadow:0 4px 16px rgba(0,0,0,.35)";
131
+ let panel = null;
132
+ btn.addEventListener("click", () => {
133
+ if (panel) {
134
+ panel.remove();
135
+ panel = null;
136
+ return;
137
+ }
138
+ panel = document.createElement("div");
139
+ panel.style.cssText =
140
+ "position:fixed;top:44px;right:12px;z-index:2147483647;width:min(420px,92vw);" +
141
+ "padding:16px 18px;border-radius:10px;background:rgba(30,30,36,.97);color:#fff;" +
142
+ "font:13px/1.6 system-ui,sans-serif;box-shadow:0 18px 60px rgba(0,0,0,.5)";
143
+ panel.innerHTML =
144
+ "<div style='font-weight:600;margin-bottom:6px'>技能面板 · 兜底渲染</div>" +
145
+ "<div style='opacity:.75'>客户端 bundle 已加载,但 slot 注册没成功。" +
146
+ "这说明问题在 slot API,不在加载链路。</div>";
147
+ document.body.appendChild(panel);
148
+ });
149
+ document.body.appendChild(btn);
150
+ }
151
+
152
+ const SLOTS = ["conversation.session.header.actions", "conversation.session.header.utilities"];
153
+
154
+ /**
155
+ * 注册按钮。按参考实现的优先级依次尝试,每条路径独立 try,互不阻塞。
156
+ * 1) ctx.inject(slot, () => ctx.register(spec, C)) —— dsh-cost-meter 的真实写法
157
+ * 2) ctx.slots.inject(slot, () => ctx.slots.register(spec, C))
158
+ * 3) ctx.slots.register(spec, C) —— 原实现
159
+ */
160
+ function apply(ctx) {
161
+ const disposers = [];
162
+ const tryRegister = (slot, fn) => {
163
+ try {
164
+ const d = fn();
165
+ if (typeof d === "function") disposers.push(d);
166
+ return true;
167
+ } catch (err) {
168
+ try {
169
+ console.warn("[lean-router] register failed on", slot, err);
170
+ } catch {}
171
+ return false;
172
+ }
173
+ };
174
+
175
+ for (const slot of SLOTS) {
176
+ const spec = { name: slot, id: "lean-router.skills", order: 60 };
177
+ const ok =
178
+ (typeof ctx.inject === "function" &&
179
+ typeof ctx.register === "function" &&
180
+ tryRegister(slot, () => ctx.inject(slot, () => ctx.register(spec, SkillsButton)))) ||
181
+ (ctx.slots &&
182
+ typeof ctx.slots.inject === "function" &&
183
+ tryRegister(slot, () => ctx.slots.inject(slot, () => ctx.slots.register(spec, SkillsButton)))) ||
184
+ (ctx.slots &&
185
+ typeof ctx.slots.register === "function" &&
186
+ tryRegister(slot, () => ctx.slots.register(spec, SkillsButton)));
187
+ if (ok) break;
188
+ }
189
+
190
+ // 诊断兜底:所有 slot 路径都失败时,用纯 DOM 在右上角插一个悬浮按钮。
191
+ // 目的不是最终形态,而是区分「bundle 没加载」与「slot API 不对」这两类失败。
192
+ if (disposers.length === 0) {
193
+ try {
194
+ domFallback();
195
+ } catch (err) {
196
+ try {
197
+ console.warn("[lean-router] dom fallback failed", err);
198
+ } catch {}
199
+ }
200
+ }
201
+
202
+ return () => {
203
+ for (const d of disposers) {
204
+ try {
205
+ d();
206
+ } catch {}
207
+ }
208
+ };
209
+ }
210
+
211
+ return { apply };
212
+ },
213
+ });
package/lib/index.js CHANGED
@@ -1,14 +1,48 @@
1
- // dsh-lean-router - cordis 插件入口(骨架,票 02)
2
- // 票 04/05/06 将在此追加:executor 遮蔽、路由钩子(inbox/pre-step)、技能评分门控、审计。
1
+ // dsh-lean-router - cordis 插件入口
2
+ //
3
+ // 1.0.5 修:宿主层需要以裸包名 dsh-lean-router 挂一行,否则 dsh-client-modules 扫不到本包
4
+ // 的 dsh.client 声明,客户端 bundle 不会加载(顶栏按钮因此不出现)。
5
+ //
6
+ // 但原来的 inject = ["skills"] 会让宿主层那一行永远停在“等待依赖”——skills 服务由预设里的
7
+ // @deepseek-ai/dsh-skill-filesystem 提供,宿主作用域没有它。插件不 apply,扫描就看不到活跃
8
+ // 条目。改为可选依赖:宿主层照常 apply(只写自检),预设层照常注册 provider。
9
+ import { appendFileSync, mkdirSync } from "node:fs";
10
+ import { homedir } from "node:os";
11
+ import { join } from "node:path";
3
12
  import { makeProvider } from "./skill-provider.js";
4
13
  import { getAuditChannel } from "./skillgate.js";
5
14
 
6
15
  export const name = "skill-lean-router";
7
- export const inject = ["skills"];
16
+ export const inject = { required: [], optional: ["skills"] };
17
+
18
+ /** 启动自检:把宿主层是否 apply、ctx 上有哪些能力,写进 ~/.dsh/lean-router/boot.log。 */
19
+ function probe(line) {
20
+ try {
21
+ const dir = join(homedir(), ".dsh", "lean-router");
22
+ mkdirSync(dir, { recursive: true });
23
+ appendFileSync(join(dir, "boot.log"), new Date().toISOString() + " " + line + "\n", "utf8");
24
+ } catch {
25
+ // 自检失败绝不影响插件本身
26
+ }
27
+ }
8
28
 
9
29
  export function apply(ctx, options) {
10
30
  const cfg = options ?? {};
31
+ try {
32
+ probe(
33
+ "apply: options=" + JSON.stringify(cfg) +
34
+ " hasSkills=" + Boolean(ctx.skills) +
35
+ " slots=" + typeof ctx.slots +
36
+ " register=" + typeof ctx.register +
37
+ " inject=" + typeof ctx.inject
38
+ );
39
+ } catch {}
40
+
41
+ if (cfg.provider === false) return;
42
+ if (!ctx.skills || typeof ctx.skills.registerProvider !== "function") {
43
+ probe("skip provider: ctx.skills 不可用(宿主作用域属正常)");
44
+ return;
45
+ }
11
46
  const rank = typeof cfg.rank === "number" ? cfg.rank : 350;
12
- // master 集成(票 07 §9.2):provider 挂共享审计通道(skillgate 决策流单例)。
13
47
  ctx.skills.registerProvider(() => makeProvider({ rank, audit: getAuditChannel() }));
14
48
  }
package/lib/router.js CHANGED
@@ -26,12 +26,14 @@ import {
26
26
  TASK_ECHO_PREFIX,
27
27
  classifyTask,
28
28
  diligenceReminder,
29
+ explainClassification,
29
30
  parseRouterDirective,
30
31
  } from "./bands.js";
31
32
  import { defaultAuditPath, wireAuditSink } from "./audit-sink.js";
32
33
  import { executeAuditCommand } from "./audit-commands.js";
33
34
  import { getAuditChannel } from "./skillgate.js";
34
35
  import { createGateRunner } from "./gate-runner.js";
36
+ import { executeSkillsPanelCommand } from "./skills-panel.js";
35
37
 
36
38
  /** 三带配置表(re-export,满足单入口消费)。 */
37
39
  export { BANDS, BAND_IDS, DIRECTIVE_BAND_IDS, classifyTask, isComplexTask, parseRouterDirective, diligenceReminder } from "./bands.js";
@@ -111,12 +113,34 @@ function capture(states, agent, message, turn, skipInherited) {
111
113
  const directive = parseRouterDirective(text);
112
114
  if (directive !== null) {
113
115
  const band = directive.band === "auto" ? null : directive.band;
114
- const next = { band, task: directive.task ?? previous?.task ?? null, turn };
116
+ const next = {
117
+ band,
118
+ task: directive.task ?? previous?.task ?? null,
119
+ turn,
120
+ // /band 展示用:manual 区分「手动指定」与「按首条消息自动分类」;
121
+ // last 只记最近一次关键词自动分类的结果(用于"最近一次自动分类"那一行)。
122
+ manual: directive.band !== "auto",
123
+ last: previous?.last ?? null,
124
+ };
115
125
  states.set(agent, next);
116
126
  return previous === null ? band !== null : previous.band !== band;
117
127
  }
118
128
  if (previous === null) {
119
- states.set(agent, { band: classifyTask(text), task: null, turn });
129
+ const explained = explainClassification(text);
130
+ states.set(agent, {
131
+ band: explained.band,
132
+ task: null,
133
+ turn,
134
+ manual: false,
135
+ last: {
136
+ via: "keywords",
137
+ band: explained.band,
138
+ reactHits: explained.reactHits,
139
+ specHits: explained.specHits,
140
+ signals: explained.signals,
141
+ at: Date.now(),
142
+ },
143
+ });
120
144
  return true;
121
145
  }
122
146
  if (previous.turn !== turn) states.set(agent, { ...previous, turn });
@@ -180,18 +204,43 @@ function executeBandCommand(states, invocation) {
180
204
  if (!agent) return { kind: "error", text: "/band has no active agent to route." };
181
205
  const raw = (invocation.rawInput ?? "").trim();
182
206
  const state = stateOf(states, agent);
183
- if (raw.length === 0) {
207
+ if (raw.length === 0 || raw.toLowerCase() === "help") {
184
208
  const band = state?.band ?? null;
185
- return {
186
- kind: "success",
187
- text:
188
- "Router band: " +
189
- (band ?? "auto (unset - classifies on next user message)") +
190
- "\n" +
191
- (state?.task ? TASK_ECHO_PREFIX + state.task + "\n" : "") +
192
- "Usage: /band [spec|react|weak|auto] [task <one line>]\n" +
193
- "Bands: spec = inspect-and-plan (narrow, read-first); react = hands-on production (wide, write-first); weak = model-routed (self-classify).",
194
- };
209
+ const current = band ?? "auto";
210
+ // 一眼看清:一行状态在前,三带差异用表格,当前档位加 ▲ 粗体标记。
211
+ const cell = (id) => (id === current ? "**▲ " + id + "**" : id);
212
+ const table = [
213
+ "| 档位 | 定位 | 工具面 |",
214
+ "|---|---|---|",
215
+ "| " + cell("auto") + " | 按首条用户消息自动分类 | — |",
216
+ "| " + cell("spec") + " | 先查后改(读优先) | 窄 |",
217
+ "| " + cell("react") + " | 直接产出(写优先) | 宽 |",
218
+ "| " + cell("weak") + " | 交给模型自判 | 窄 |",
219
+ ].join("\n");
220
+ // 来源说明用中文,区分「还没分类」「自动分类锁定」「手动指定」三种状态。
221
+ const sourceNote =
222
+ band === null
223
+ ? "下一条消息自动分类"
224
+ : state?.manual === true
225
+ ? "手动指定,关键词不再改带"
226
+ : "已按首条消息分类,关键词不再改带";
227
+ const head = "▲ " + current + " · " + sourceNote + " · 可选 spec / react / weak";
228
+ const last = state?.last ?? null;
229
+ const lastLine =
230
+ last === null || last.band === null
231
+ ? "最近一次自动分类:暂无(等待下一条用户消息)"
232
+ : "最近一次自动分类:" +
233
+ last.band +
234
+ "(build 词 " +
235
+ last.reactHits +
236
+ " · fix 词 " +
237
+ last.specHits +
238
+ (last.signals.length > 0 ? ";命中 " + last.signals.join("、") : "") +
239
+ ")";
240
+ const parts = [head, "", table, "", lastLine];
241
+ if (state?.task) parts.push(TASK_ECHO_PREFIX + state.task);
242
+ parts.push("用法:/band spec | react | weak | auto · /band react task=发布 MVP · /band help");
243
+ return { kind: "success", text: parts.join("\n") };
195
244
  }
196
245
  const tokens = raw.split(/[ \t]+/);
197
246
  const head = tokens[0].toLowerCase();
@@ -206,10 +255,11 @@ function executeBandCommand(states, invocation) {
206
255
  }
207
256
  const band = head === "auto" ? null : head;
208
257
  const turn = state?.turn ?? 0;
209
- states.set(agent, { band, task: taskArg ?? state?.task ?? null, turn });
258
+ // 走命令改档 = 人在回路:manual 置真(auto 回退置假),并保留上一次自动分类结果。
259
+ states.set(agent, { band, task: taskArg ?? state?.task ?? null, turn, manual: band !== null, last: state?.last ?? null });
210
260
  return {
211
261
  kind: "success",
212
- text: "Router band set to " + (band ?? "auto") + "." + (taskArg ? "\n" + TASK_ECHO_PREFIX + taskArg : ""),
262
+ text: "▲ " + (band ?? "auto") + " · 已切换" + (taskArg ? "\n" + TASK_ECHO_PREFIX + taskArg : ""),
213
263
  };
214
264
  }
215
265
 
@@ -343,6 +393,16 @@ export function apply(ctx, options) {
343
393
  });
344
394
  // 票 07 加法接线:/lean-audit 双入口之一(命令;实现放 lib/audit-commands.js,
345
395
  // 数据源 = 审计 JSONL 回放,不新增内存态)。ui/go.json 面板按钮是另一入口。
396
+ // 1.0.5:技能面板的命令输出形态(客户端插件未修好前的可用形态)。
397
+ ctx.commands.register({
398
+ name: "panel",
399
+ description: "Skill panel: 31 skills with switches, current round hit scores and history stats",
400
+ input: { hint: "" },
401
+ handler: (invocation) =>
402
+ executeSkillsPanelCommand(invocation, {
403
+ auditPath: cfg.auditPath ?? defaultAuditPath(),
404
+ }),
405
+ });
346
406
  ctx.commands.register({
347
407
  name: "lean-audit",
348
408
  description: "Show recent skill-gate audit decisions, hit statistics and manual weights",