@jiyr0119/dsh-service-console 0.7.2 → 0.9.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.
Files changed (40) hide show
  1. package/README.md +7 -5
  2. package/README.zh.md +7 -5
  3. package/docs/local-debugging.md +1 -1
  4. package/docs/rc5-upgrade-review.zh.md +182 -0
  5. package/docs/release-checklist.md +15 -12
  6. package/icon.svg +8 -0
  7. package/lib/client/dictionaries.js +19 -2
  8. package/lib/client/i18n.js +29 -70
  9. package/lib/client/service-list-view.js +186 -0
  10. package/lib/client.js +1284 -685
  11. package/lib/host/config.js +1 -2
  12. package/lib/host/ledger.js +2 -3
  13. package/lib/host/lifecycle.js +2 -3
  14. package/lib/host/ownership.js +8 -4
  15. package/lib/host/process-inspector.js +1 -2
  16. package/lib/host/protection.js +1 -2
  17. package/lib/host/redaction.js +1 -2
  18. package/lib/host/service-registry.js +4 -5
  19. package/lib/host/service-tool.js +42 -4
  20. package/lib/index.js +54 -11
  21. package/lib/types/client/ServiceConsole.d.ts +35 -15
  22. package/lib/types/client/ServiceDockPill.d.ts +29 -0
  23. package/lib/types/client/ServiceListCard.d.ts +17 -0
  24. package/lib/types/client/api.d.ts +19 -0
  25. package/lib/types/client/dictionaries.d.ts +18 -0
  26. package/lib/types/client/i18n.d.ts +33 -40
  27. package/lib/types/client/index.d.ts +21 -10
  28. package/lib/types/client/service-list-view.d.ts +92 -0
  29. package/lib/types/host/ledger.d.ts +7 -0
  30. package/lib/types/host/ownership.d.ts +9 -1
  31. package/lib/types/host/service-registry.d.ts +2 -1
  32. package/lib/types/host/service-tool.d.ts +7 -2
  33. package/lib/types/index.d.ts +18 -2
  34. package/locale/en.json +6 -0
  35. package/locale/zh.json +6 -0
  36. package/package.json +34 -33
  37. package/dsh.plugin.json +0 -16
  38. package/dynamic/client.js +0 -193
  39. package/dynamic/host.js +0 -337
  40. package/manifest.json +0 -24
@@ -0,0 +1,186 @@
1
+ // src/client/service-list-view.ts
2
+ /** 卡片最多展示多少行(meta 由宿主限长,这里再兜一次,防止回放旧数据) */
3
+ const MAX_CARD_ROWS = 20;
4
+ /** 与 host/types.ts 的 Ownership 联合一致(运行时无从推导,靠单测对齐) */
5
+ const OWNERSHIP_LEVELS = [
6
+ "conversation-confirmed",
7
+ "workspace-inferred",
8
+ "other-local",
9
+ "unknown",
10
+ "protected"
11
+ ];
12
+ /** 归属 → 主题色(两处 UI 共用,避免各写一份) */
13
+ const OWNERSHIP_COLOR = {
14
+ "conversation-confirmed": "var(--dsw-alias-state-success-primary, #4ade80)",
15
+ "workspace-inferred": "var(--dsw-alias-state-warn-primary, #fbbf24)",
16
+ "other-local": "var(--dsw-alias-label-secondary, #94a3b8)",
17
+ unknown: "var(--dsw-alias-state-error-primary, #f87171)",
18
+ protected: "var(--dsw-alias-state-error-primary, #f87171)"
19
+ };
20
+ /**
21
+ * 归属徽标的 tone:统一 `neutral`。
22
+ *
23
+ * 平台 Tag 的语义色(success/warning/danger)在浅色主题下实测对比度不足
24
+ * (success 2.09:1 / warning 1.96:1 / danger 3.80:1,均低于 AA 4.5:1),
25
+ * 而 `neutral` 为 5.36:1。归属的细粒度语义由前面的彩色圆点承担(装饰性)。
26
+ */
27
+ const OWNERSHIP_TONE = {
28
+ "conversation-confirmed": "neutral",
29
+ "workspace-inferred": "neutral",
30
+ "other-local": "neutral",
31
+ unknown: "neutral",
32
+ protected: "neutral"
33
+ };
34
+ /** 归属 → 词典键 */
35
+ const OWNERSHIP_KEY = {
36
+ "conversation-confirmed": "own.conversation",
37
+ "workspace-inferred": "own.workspace",
38
+ "other-local": "own.other",
39
+ unknown: "own.unknown",
40
+ protected: "own.protected"
41
+ };
42
+ /** API 字符串 → 归属级别(未知值降级 unknown,不炸 UI) */
43
+ function ownershipLevel(value) {
44
+ return OWNERSHIP_LEVELS.includes(value) ? value : "unknown";
45
+ }
46
+ function asRecord(value) {
47
+ return typeof value === "object" && value !== null ? value : null;
48
+ }
49
+ function finiteNumber(value) {
50
+ return typeof value === "number" && Number.isFinite(value) ? value : null;
51
+ }
52
+ function ownershipOf(value) {
53
+ return typeof value === "string" && OWNERSHIP_LEVELS.includes(value) ? value : "unknown";
54
+ }
55
+ /** 允许打开的主机名:只认本机回环(meta 可被篡改,不能让卡片变成钓鱼链接) */
56
+ const LOOPBACK_HOSTS = /* @__PURE__ */ new Set([
57
+ "localhost",
58
+ "127.0.0.1",
59
+ "::1",
60
+ "[::1]"
61
+ ]);
62
+ /**
63
+ * 只接受 http(s) + 回环主机;其余(含 `javascript:`、`data:`、外部域名)一律 null。
64
+ * @param value meta 里的 url 字段(unknown)
65
+ */
66
+ function safeServiceUrl(value) {
67
+ if (typeof value !== "string" || value === "" || value.length > 2048) return null;
68
+ let parsed;
69
+ try {
70
+ parsed = new URL(value);
71
+ } catch {
72
+ return null;
73
+ }
74
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return null;
75
+ return LOOPBACK_HOSTS.has(parsed.hostname.toLowerCase()) ? value : null;
76
+ }
77
+ /** 单行投影:缺 id 的行直接丢弃(没有稳定 key 无法渲染,也无法操作) */
78
+ function projectRow(value) {
79
+ const raw = asRecord(value);
80
+ if (raw === null) return null;
81
+ const id = typeof raw.id === "string" ? raw.id : "";
82
+ if (id === "") return null;
83
+ const name = typeof raw.name === "string" && raw.name !== "" ? raw.name : id;
84
+ const url = safeServiceUrl(raw.url);
85
+ return {
86
+ id,
87
+ name: name.slice(0, 200),
88
+ ports: typeof raw.ports === "string" ? raw.ports.slice(0, 200) : "",
89
+ ownership: ownershipOf(raw.ownership),
90
+ protected: raw.protected === true,
91
+ restartable: raw.restartable === true,
92
+ url
93
+ };
94
+ }
95
+ /**
96
+ * `presentationMeta` → 卡片视图模型。
97
+ * @param meta 持久化的 meta(`unknown`:旧会话可能没有,或形状已变)
98
+ * @returns 可渲染模型;`null` 表示「没有可用 meta」,调用方应回退到纯文本内容
99
+ */
100
+ function projectServiceListMeta(meta) {
101
+ const raw = asRecord(meta);
102
+ if (raw === null) return null;
103
+ const services = Object.hasOwn(raw, "services") && Array.isArray(raw.services) ? raw.services : null;
104
+ if (services === null) return null;
105
+ const rows = [];
106
+ for (const entry of services) {
107
+ if (rows.length >= 20) break;
108
+ const row = projectRow(entry);
109
+ if (row !== null) rows.push(row);
110
+ }
111
+ const total = finiteNumber(raw.total) ?? rows.length;
112
+ const count = finiteNumber(raw.count) ?? rows.length;
113
+ return {
114
+ scannedAt: finiteNumber(raw.scannedAt),
115
+ total: Math.max(total, rows.length),
116
+ count: Math.max(count, rows.length),
117
+ rows,
118
+ truncated: Math.max(Math.max(count, rows.length) - rows.length, 0)
119
+ };
120
+ }
121
+ /** 端口去重后的一行摘要片段(最多 6 个,避免撑爆行宽);语言无关 */
122
+ function serviceListPorts(model) {
123
+ const ports = [];
124
+ for (const row of model.rows) {
125
+ for (const port of row.ports.split(",")) {
126
+ const p = port.trim();
127
+ if (p !== "" && !ports.includes(p)) ports.push(p);
128
+ if (ports.length >= 6) break;
129
+ }
130
+ if (ports.length >= 6) break;
131
+ }
132
+ return ports.length > 0 ? ":" + ports.join(" :") : "";
133
+ }
134
+ /** 行是否可执行生命周期操作(与面板同一判定:受保护目标只读) */
135
+ function canControlService(row) {
136
+ return !row.protected;
137
+ }
138
+ /**
139
+ * 从一次扫描快照里挑出「属于本会话」的服务。
140
+ * 归属判定在宿主完成(scan 带 sessionId 时按台账 sessionKey 收窄),
141
+ * 这里只做防御式读取:坏行丢弃,未知归属不计数。
142
+ * @param services 扫描接口的 `data.services`(不可信:可能缺失/坏形状)
143
+ */
144
+ function selectSessionServices(services) {
145
+ if (!Array.isArray(services)) return [];
146
+ const rows = [];
147
+ for (const entry of services) {
148
+ const raw = asRecord(entry);
149
+ if (raw === null) continue;
150
+ const id = typeof raw.id === "string" ? raw.id : "";
151
+ if (id === "") continue;
152
+ if (!Object.hasOwn(raw, "ownership") || raw.ownership !== "conversation-confirmed") continue;
153
+ const ports = (Array.isArray(raw.listeners) ? raw.listeners : []).map((l) => {
154
+ const rec = asRecord(l);
155
+ return rec !== null && typeof rec.port === "number" && Number.isFinite(rec.port) ? String(rec.port) : "";
156
+ }).filter((p) => p !== "").join(", ");
157
+ rows.push({
158
+ id,
159
+ name: typeof raw.name === "string" && raw.name !== "" ? raw.name.slice(0, 200) : id,
160
+ ownership: "conversation-confirmed",
161
+ protected: raw.protected === true,
162
+ ports
163
+ });
164
+ }
165
+ return rows;
166
+ }
167
+ /**
168
+ * 本会话服务摘要;`count === 0` 时 pill 不渲染。
169
+ * @param services `selectSessionServices` 的结果
170
+ */
171
+ function summarizeSessionServices(services) {
172
+ const ports = [];
173
+ for (const row of services) {
174
+ for (const port of row.ports.split(",")) {
175
+ const p = port.trim();
176
+ if (p !== "" && !ports.includes(p)) ports.push(p);
177
+ if (ports.length >= 4) break;
178
+ }
179
+ if (ports.length >= 4) break;
180
+ }
181
+ return {
182
+ count: services.length,
183
+ ports: ports.length > 0 ? ":" + ports.join(" :") : ""
184
+ };
185
+ }
186
+ export { MAX_CARD_ROWS, OWNERSHIP_COLOR, OWNERSHIP_KEY, OWNERSHIP_TONE, canControlService, ownershipLevel, projectServiceListMeta, safeServiceUrl, selectSessionServices, serviceListPorts, summarizeSessionServices };