@dsh-workbench/shared 0.3.5 → 0.3.7

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 (153) hide show
  1. package/dist/api/context-ref.d.ts +0 -12
  2. package/dist/api/context-ref.js +0 -40
  3. package/dist/api/facts.d.ts +0 -6
  4. package/dist/api/facts.js +0 -8
  5. package/dist/api/long-task.d.ts +33 -4
  6. package/dist/api/long-task.js +34 -2
  7. package/dist/components/ConfirmBar.css +26 -0
  8. package/dist/components/ConfirmBar.d.ts +36 -0
  9. package/dist/components/ConfirmBar.js +26 -0
  10. package/dist/components/Form.d.ts +7 -5
  11. package/dist/components/Form.js +5 -3
  12. package/dist/components/FormNodes.d.ts +17 -13
  13. package/dist/components/FormNodes.js +13 -5
  14. package/dist/components/FormRows.css +31 -0
  15. package/dist/components/FormTypes.d.ts +39 -14
  16. package/dist/components/LongTaskRow.css +52 -0
  17. package/dist/components/LongTaskRow.d.ts +18 -2
  18. package/dist/components/LongTaskRow.js +33 -5
  19. package/dist/components/MockStrip.css +11 -1
  20. package/dist/components/StateDot.d.ts +5 -0
  21. package/dist/components/StateDot.js +10 -0
  22. package/dist/config/constants.d.ts +2 -0
  23. package/dist/config/constants.js +2 -0
  24. package/dist/host/__tests__/fakes.d.ts +41 -0
  25. package/dist/host/__tests__/fakes.js +76 -0
  26. package/dist/host/__tests__/preflight-fixtures.d.ts +19 -0
  27. package/dist/host/__tests__/preflight-fixtures.js +47 -0
  28. package/dist/host/board-fact.d.ts +8 -0
  29. package/dist/host/board-fact.js +16 -0
  30. package/dist/host/browser-read.js +2 -1
  31. package/dist/host/context-inject.d.ts +24 -0
  32. package/dist/host/context-inject.js +74 -0
  33. package/dist/host/context-ref.d.ts +13 -0
  34. package/dist/host/context-ref.js +41 -0
  35. package/dist/host/diy-debug.d.ts +80 -0
  36. package/dist/host/diy-debug.js +88 -0
  37. package/dist/host/diy-record.d.ts +135 -0
  38. package/dist/host/diy-record.js +162 -0
  39. package/dist/host/events.d.ts +9 -0
  40. package/dist/host/events.js +70 -0
  41. package/dist/host/facts.d.ts +6 -0
  42. package/dist/host/facts.js +9 -0
  43. package/dist/host/git.js +1 -1
  44. package/dist/host/http.d.ts +2 -2
  45. package/dist/host/http.js +3 -3
  46. package/dist/host/long-task.d.ts +12 -0
  47. package/dist/host/long-task.js +34 -5
  48. package/dist/host/mount-units.d.ts +52 -0
  49. package/dist/host/mount-units.js +186 -0
  50. package/dist/host/opencli-runtime.js +1 -1
  51. package/dist/host/opencli-window.js +1 -1
  52. package/dist/host/opencli.js +2 -1
  53. package/dist/host/panel-state.d.ts +120 -0
  54. package/dist/host/panel-state.js +176 -0
  55. package/dist/host/plugin-source.js +1 -1
  56. package/dist/host/preflight.d.ts +58 -0
  57. package/dist/host/preflight.js +233 -0
  58. package/dist/host/probe-cache.d.ts +6 -0
  59. package/dist/host/probe-cache.js +19 -0
  60. package/dist/host/probe-libs.d.ts +45 -0
  61. package/dist/host/probe-libs.js +61 -0
  62. package/dist/host/probe-units.d.ts +17 -0
  63. package/dist/host/probe-units.js +102 -0
  64. package/dist/host/probe-verdict.d.ts +7 -0
  65. package/dist/host/probe-verdict.js +143 -0
  66. package/dist/host/shell-text.d.ts +13 -0
  67. package/dist/host/shell-text.js +20 -0
  68. package/dist/host/tool-approval.d.ts +1 -0
  69. package/dist/host/tool-scope.d.ts +32 -0
  70. package/dist/host/tool-scope.js +84 -0
  71. package/dist/host/unit-declaration.d.ts +65 -0
  72. package/dist/host/unit-declaration.js +213 -0
  73. package/dist/host/unit-face.d.ts +1 -0
  74. package/dist/host/unit-readiness.d.ts +7 -0
  75. package/dist/host/unit-readiness.js +5 -0
  76. package/dist/host/unit-registry.d.ts +9 -0
  77. package/dist/host/unit-registry.js +33 -0
  78. package/dist/host/version-range.d.ts +2 -0
  79. package/dist/host/version-range.js +73 -0
  80. package/dist/host.d.ts +7 -4
  81. package/dist/host.js +7 -4
  82. package/dist/i18n/en.js +6 -0
  83. package/dist/i18n/index.d.ts +7 -1
  84. package/dist/i18n/zh.d.ts +6 -0
  85. package/dist/i18n/zh.js +6 -0
  86. package/dist/index.d.ts +1 -4
  87. package/dist/index.js +1 -4
  88. package/dist/lib/board-fact.d.ts +0 -7
  89. package/dist/lib/board-fact.js +0 -15
  90. package/dist/lib/diy-record.d.ts +38 -1
  91. package/dist/lib/diy-record.js +66 -0
  92. package/dist/lib/text.d.ts +0 -13
  93. package/dist/lib/text.js +0 -20
  94. package/dist/testing/declaration.js +1 -1
  95. package/package.json +1 -1
  96. package/src/api/__tests__/facts.test.ts +0 -13
  97. package/src/api/__tests__/long-task.test.ts +41 -1
  98. package/src/api/context-ref.ts +0 -39
  99. package/src/api/facts.ts +0 -9
  100. package/src/api/long-task.ts +46 -4
  101. package/src/components/ConfirmBar.css +26 -0
  102. package/src/components/ConfirmBar.test.tsx +130 -0
  103. package/src/components/ConfirmBar.tsx +63 -0
  104. package/src/components/Form.test.tsx +15 -0
  105. package/src/components/Form.tsx +7 -5
  106. package/src/components/FormNodes.tsx +50 -26
  107. package/src/components/FormRows.css +31 -0
  108. package/src/components/FormRows.test.tsx +71 -1
  109. package/src/components/FormTypes.test.ts +31 -0
  110. package/src/components/FormTypes.ts +46 -15
  111. package/src/components/FormWiring.test.tsx +1 -0
  112. package/src/components/LongTaskRow.css +52 -0
  113. package/src/components/LongTaskRow.tsx +59 -4
  114. package/src/components/__tests__/long-task-row.test.ts +115 -0
  115. package/src/config/constants.ts +3 -0
  116. package/src/host/__tests__/board-fact.test.ts +47 -0
  117. package/src/{lib → host/__tests__}/diy-debug.test.ts +1 -1
  118. package/src/{lib → host/__tests__}/diy-record.test.ts +137 -1
  119. package/src/host/__tests__/facts.test.ts +13 -0
  120. package/src/host/__tests__/long-task.test.ts +306 -0
  121. package/src/{lib → host/__tests__}/panel-state.test.ts +1 -1
  122. package/src/host/__tests__/preflight-route.test.ts +33 -0
  123. package/src/host/__tests__/shell-text.test.ts +44 -0
  124. package/src/host/board-fact.ts +14 -0
  125. package/src/host/browser-read.ts +2 -1
  126. package/src/host/context-ref.ts +40 -0
  127. package/src/{lib → host}/diy-record.ts +95 -1
  128. package/src/host/facts.ts +10 -0
  129. package/src/host/git.ts +1 -1
  130. package/src/host/http.ts +4 -4
  131. package/src/host/long-task.ts +52 -7
  132. package/src/host/opencli-runtime.ts +1 -1
  133. package/src/host/opencli-window.ts +1 -1
  134. package/src/host/opencli.ts +2 -1
  135. package/src/host/plugin-source.ts +1 -1
  136. package/src/host/shell-text.ts +21 -0
  137. package/src/host/tool-approval.ts +1 -0
  138. package/src/{api → host}/unit-declaration.ts +1 -1
  139. package/src/host/unit-face.ts +1 -0
  140. package/src/host.ts +7 -4
  141. package/src/i18n/en.ts +7 -0
  142. package/src/i18n/zh.ts +6 -0
  143. package/src/index.ts +1 -4
  144. package/src/lib/board-fact.test.ts +1 -33
  145. package/src/lib/board-fact.ts +0 -13
  146. package/src/lib/text.test.ts +1 -43
  147. package/src/lib/text.ts +0 -22
  148. package/src/testing/declaration.ts +1 -1
  149. package/dist/lib/lint-probe.d.ts +0 -1
  150. package/dist/lib/lint-probe.js +0 -6
  151. /package/src/{api → host}/__tests__/unit-declaration.test.ts +0 -0
  152. /package/src/{lib → host}/diy-debug.ts +0 -0
  153. /package/src/{lib → host}/panel-state.ts +0 -0
@@ -0,0 +1,143 @@
1
+ import { resolvePluginSource } from "./plugin-source";
2
+ import { isNewerVersion } from "./release";
3
+ export function evaluateUnit(list, unit, failure, command = "opencli", releases = new Map(), libs = []) {
4
+ const isPlugin = (item) => typeof item === "object" && item !== null;
5
+ const needs = unit.plugins ?? [];
6
+ const required = needs.flatMap((need) => need.commands ?? []);
7
+ const primary = needs[0]?.name;
8
+ if (failure !== undefined || list === undefined) {
9
+ return {
10
+ unit: unit.unit,
11
+ ...(primary === undefined ? {} : { plugin: primary }),
12
+ status: "unchecked",
13
+ required,
14
+ missing: [],
15
+ detail: failure ?? "探不动",
16
+ ...(libs.length === 0 ? {} : { libs }),
17
+ };
18
+ }
19
+ const plugins = list.filter(isPlugin);
20
+ const installed = new Map(plugins.map((item) => [String(item.name), item]));
21
+ const states = needs.map((need) => {
22
+ const hit = installed.get(need.name);
23
+ if (hit === undefined) {
24
+ const source = resolvePluginSource(need.installSource, process.env, {
25
+ command,
26
+ plugin: need.name,
27
+ });
28
+ return {
29
+ name: need.name,
30
+ status: "missing",
31
+ missing: [],
32
+ install: source.ok ? "ready" : source.problem,
33
+ command: source.ok ? source.line : pluginInstallCommand(need.name),
34
+ };
35
+ }
36
+ const available = new Set((Array.isArray(hit.commands) ? hit.commands : []).map(String));
37
+ const missing = (need.commands ?? []).filter((name) => !available.has(name));
38
+ const release = releases.get(need.name);
39
+ const behind = release?.latest !== undefined &&
40
+ isNewerVersion(release.latest, release.installed);
41
+ const commandsBehind = missing.length > 0;
42
+ const upgrade = behind && release?.latest !== undefined
43
+ ? upgradeLine(need, command, release.latest)
44
+ : undefined;
45
+ return {
46
+ name: need.name,
47
+ status: commandsBehind || behind ? "outdated" : "ok",
48
+ missing,
49
+ ...(release === undefined ? {} : { release }),
50
+ ...(commandsBehind || behind
51
+ ? {
52
+ command: upgrade ??
53
+ pluginUpdateCommand(need.name) ??
54
+ pluginInstallCommand(need.name),
55
+ }
56
+ : {}),
57
+ };
58
+ });
59
+ const absent = states.filter((state) => state.status === "missing");
60
+ const outdated = states.filter((state) => state.status === "outdated");
61
+ const missing = states.flatMap((state) => state.missing);
62
+ const libMissing = libs.filter((state) => state.status === "missing");
63
+ const libMismatch = libs.filter((state) => state.status === "unsatisfied");
64
+ const libBehind = libs.filter((state) => state.status === "outdated");
65
+ const status = absent.length > 0 || libMissing.length > 0 || libMismatch.length > 0
66
+ ? "missing-plugin"
67
+ : missing.length > 0
68
+ ? "missing-commands"
69
+ : "ok";
70
+ const blocked = absent.find((state) => state.install !== "ready");
71
+ const install = absent.length === 0
72
+ ?
73
+ libMissing.length > 0 || libMismatch.length > 0
74
+ ? "ready"
75
+ : undefined
76
+ : blocked === undefined
77
+ ? "ready"
78
+ : blocked.install;
79
+ const fix = status === "missing-plugin"
80
+ ? install === "ready"
81
+ ? "install"
82
+ : undefined
83
+ : outdated.length > 0 || libBehind.length > 0
84
+ ? "update"
85
+ : undefined;
86
+ const first = absent[0] ?? outdated[0] ?? libMissing[0] ?? libMismatch[0] ?? libBehind[0];
87
+ const gaps = [...(missing.length === 0 ? [] : [`缺必需:${missing.join("、")}`])];
88
+ const newer = states.filter((state) => state.release?.latest !== undefined &&
89
+ isNewerVersion(state.release.latest, state.release.installed));
90
+ const versionText = newer.length === 0
91
+ ? ""
92
+ : `(源上有新版:${newer.map((state) => `${state.release?.installed ?? ""} → ${state.release?.latest ?? ""}`).join("、")})`;
93
+ const absentNames = [
94
+ ...absent.map((state) => state.name),
95
+ ...libMissing.map((state) => state.name),
96
+ ];
97
+ const mismatchText = libMismatch.map((state) => {
98
+ const declared = unit.libs?.find((need) => need.name === state.name)?.version?.trim();
99
+ return `${state.name}(装的是 ${state.installed ?? "未知"},声明要 ${declared === undefined || declared === "" ? "任意版本" : declared})`;
100
+ });
101
+ return {
102
+ unit: unit.unit,
103
+ ...(primary === undefined ? {} : { plugin: primary }),
104
+ status,
105
+ required,
106
+ missing,
107
+ detail: absentNames.length > 0 || mismatchText.length > 0
108
+ ? [
109
+ ...(absentNames.length === 0
110
+ ? []
111
+ : [`${plugins.length} 个已装插件里没有 ${absentNames.join("、")}`]),
112
+ ...mismatchText,
113
+ ].join(";")
114
+ : `${needs.length} 份安装都在${gapsText(gaps)}${versionText}${libs.length === 0
115
+ ? ""
116
+ : `;交叉依赖:${libs.map((state) => `${state.name}${state.installed === undefined ? " 未装" : ` ${state.installed}`}`).join("、")}${libBehind.length === 0 ? "" : "(源上有新版)"}`}`,
117
+ ...(fix === undefined && first === undefined
118
+ ? {}
119
+ : { command: fix === "install" ? (absent[0] ?? first)?.command : first?.command }),
120
+ ...(fix === undefined ? {} : { fix }),
121
+ ...(install === undefined ? {} : { install }),
122
+ plugins: states,
123
+ ...(libs.length === 0 ? {} : { libs }),
124
+ };
125
+ }
126
+ export function gapsText(gaps) {
127
+ return gaps.length === 0 ? "" : `(${gaps.join(";")})`;
128
+ }
129
+ export function pluginUpdateCommand(plugin) {
130
+ return plugin === undefined ? undefined : `opencli plugin update ${plugin}`;
131
+ }
132
+ export function pluginInstallCommand(plugin) {
133
+ return `opencli plugin install <${plugin ?? "插件"}源>`;
134
+ }
135
+ export function upgradeLine(need, command, latest) {
136
+ const source = resolvePluginSource(need.installSource, process.env, {
137
+ command,
138
+ plugin: need.name,
139
+ version: latest,
140
+ mode: "update",
141
+ });
142
+ return source.ok ? source.line : undefined;
143
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * 把一个值裹成 shell 里的单引号字面量(值里的单引号一并转义),保证元字符按字面量传递。
3
+ * @param value - 任意值;不是字符串的先 `String()`。
4
+ * @returns 可安全拼进 `/bin/sh -c` 的一段文本 —— 含空格 / `$()` / `;` / 通配符的值都不会被当成语法。
5
+ */
6
+ export declare function quoteShellArg(value: unknown): string;
7
+ /**
8
+ * 取一段文本的末尾若干行(如命令输出),顺带去掉首尾空白。
9
+ * @param text - 原文;`null` / `undefined` 当空串。
10
+ * @param lines - 最多留几行,默认 6;0 给全篇、负数从开头砍掉那么多行(都是 `slice` 语义),调用方传正数。
11
+ * @returns 末尾若干行、去首尾空行后的文本;原文行数不够就全给,空输入给空串。
12
+ */
13
+ export declare function tailLines(text: string | null | undefined, lines?: number): string;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * 把一个值裹成 shell 里的单引号字面量(值里的单引号一并转义),保证元字符按字面量传递。
3
+ * @param value - 任意值;不是字符串的先 `String()`。
4
+ * @returns 可安全拼进 `/bin/sh -c` 的一段文本 —— 含空格 / `$()` / `;` / 通配符的值都不会被当成语法。
5
+ */
6
+ export function quoteShellArg(value) {
7
+ return `'${String(value).replace(/'/g, `'\\''`)}'`;
8
+ }
9
+ /**
10
+ * 取一段文本的末尾若干行(如命令输出),顺带去掉首尾空白。
11
+ * @param text - 原文;`null` / `undefined` 当空串。
12
+ * @param lines - 最多留几行,默认 6;0 给全篇、负数从开头砍掉那么多行(都是 `slice` 语义),调用方传正数。
13
+ * @returns 末尾若干行、去首尾空行后的文本;原文行数不够就全给,空输入给空串。
14
+ */
15
+ export function tailLines(text, lines = 6) {
16
+ const all = String(text ?? '')
17
+ .trim()
18
+ .split('\n');
19
+ return all.slice(-lines).join('\n');
20
+ }
@@ -29,6 +29,7 @@ export interface ApprovalCall {
29
29
  readonly name?: string | undefined;
30
30
  readonly arguments?: unknown;
31
31
  readonly workspace?: string | undefined;
32
+ readonly session?: string | undefined;
32
33
  }
33
34
  export type ApprovalVerdict = {
34
35
  readonly kind: 'read';
@@ -0,0 +1,32 @@
1
+ import type { DshPromptAssembly } from '@dsh-workbench/shared';
2
+ export interface ScopedToolRegistry {
3
+ restrict(filter: {
4
+ readonly allow?: readonly string[];
5
+ readonly deny?: readonly string[];
6
+ }): () => void;
7
+ }
8
+ export interface ToolScopeAgent {
9
+ readonly ctx?: {
10
+ readonly tools?: ScopedToolRegistry | undefined;
11
+ get?<T = unknown>(name: string): T | undefined;
12
+ } | undefined;
13
+ readonly session?: {
14
+ readonly header?: {
15
+ readonly cwd?: string;
16
+ } | undefined;
17
+ } | undefined;
18
+ }
19
+ export interface ToolScopeOptions {
20
+ deniedTools(workspace: string | null): readonly string[];
21
+ logger?: {
22
+ info?(message: string): void;
23
+ warn?(message: string): void;
24
+ } | undefined;
25
+ }
26
+ export interface ToolScope {
27
+ apply(agent: unknown): void;
28
+ narrow(assembly: DshPromptAssembly, agent: unknown): DshPromptAssembly;
29
+ reapply(): void;
30
+ disposeAll(): void;
31
+ }
32
+ export declare function createToolScope(options: ToolScopeOptions): ToolScope;
@@ -0,0 +1,84 @@
1
+ function agentOf(value) {
2
+ return typeof value === 'object' && value !== null ? value : undefined;
3
+ }
4
+ function workspaceOf(handle) {
5
+ const cwd = handle.session?.header?.cwd;
6
+ return typeof cwd === 'string' && cwd !== '' ? cwd : null;
7
+ }
8
+ export function createToolScope(options) {
9
+ const applied = new WeakMap();
10
+ const seen = new Set();
11
+ function toolsOf(handle) {
12
+ const byProperty = handle.ctx?.tools;
13
+ if (byProperty?.restrict !== undefined)
14
+ return byProperty;
15
+ const byGet = handle.ctx?.get?.('tools');
16
+ return byGet?.restrict === undefined ? undefined : byGet;
17
+ }
18
+ function sync(handle) {
19
+ const tools = toolsOf(handle);
20
+ if (tools === undefined) {
21
+ options.logger?.warn?.('[dsh-workbench] 拿不到 agent 作用域的工具面:这次不收窄(下次再试)');
22
+ return;
23
+ }
24
+ const workspace = workspaceOf(handle);
25
+ const deny = [...options.deniedTools(workspace)];
26
+ const signature = `${workspace ?? ''}\u0000${deny.join(',')}`;
27
+ const previous = applied.get(handle);
28
+ if (previous !== undefined && previous.signature === signature)
29
+ return;
30
+ if (previous === undefined)
31
+ seen.add(new WeakRef(handle));
32
+ previous?.dispose();
33
+ if (deny.length === 0) {
34
+ options.logger?.info?.('[dsh-workbench] 这个工作区启用了全部套件:不收窄工具面');
35
+ applied.delete(handle);
36
+ return;
37
+ }
38
+ try {
39
+ applied.set(handle, { signature, dispose: tools.restrict({ deny }) });
40
+ options.logger?.info?.(`[dsh-workbench] 按工作区收窄工具面:${workspace ?? '(未知工作区)'} 里收起 ${deny.length} 个工具(${deny.join(', ')})`);
41
+ }
42
+ catch (error) {
43
+ options.logger?.warn?.(`[dsh-workbench] 收窄工具面失败:${String(error)}`);
44
+ }
45
+ }
46
+ return {
47
+ apply(agent) {
48
+ const handle = agentOf(agent);
49
+ if (handle === undefined)
50
+ return;
51
+ sync(handle);
52
+ },
53
+ narrow(assembly, agent) {
54
+ const handle = agentOf(agent);
55
+ const tools = assembly?.tools;
56
+ if (handle === undefined || tools === undefined)
57
+ return assembly;
58
+ const deny = new Set(options.deniedTools(workspaceOf(handle)));
59
+ if (deny.size === 0)
60
+ return assembly;
61
+ const kept = tools.filter((tool) => !deny.has(tool.name));
62
+ return kept.length === tools.length ? assembly : { ...assembly, tools: kept };
63
+ },
64
+ reapply() {
65
+ for (const ref of [...seen]) {
66
+ const handle = ref.deref();
67
+ if (handle === undefined)
68
+ seen.delete(ref);
69
+ else
70
+ sync(handle);
71
+ }
72
+ },
73
+ disposeAll() {
74
+ for (const ref of [...seen]) {
75
+ const handle = ref.deref();
76
+ if (handle === undefined)
77
+ continue;
78
+ applied.get(handle)?.dispose();
79
+ applied.delete(handle);
80
+ }
81
+ seen.clear();
82
+ },
83
+ };
84
+ }
@@ -0,0 +1,65 @@
1
+ import type { UnitEnvNeeds, UnitHostNeeds, UnitManifest } from '../api/unit.js';
2
+ /**
3
+ * 声明协议当前版本;模块清单的 dsh.workbench.apiVersion 要写这个整数。
4
+ */
5
+ export declare const WORKBENCH_API_VERSION = 1;
6
+ /**
7
+ * 外壳愿意认的协议版本;不在表里的模块当场被拒,不猜语义。
8
+ */
9
+ export declare const SUPPORTED_API_VERSIONS: readonly number[];
10
+ /**
11
+ * 清单里那半张卡片声明:文案键加排序位;卡片默认显不显示不在这里(那是运行期面板配置的事)。
12
+ */
13
+ export interface UnitCardDeclaration {
14
+ readonly titleKey: string;
15
+ readonly order: number;
16
+ }
17
+ /**
18
+ * 清单里的一条工具声明:进 dsh 工具表的全名、会不会写,以及生成物里的键名。
19
+ */
20
+ export interface UnitToolDeclaration {
21
+ readonly name: string;
22
+ readonly writes: boolean;
23
+ readonly key?: string;
24
+ }
25
+ /**
26
+ * 模块清单里 dsh.workbench 那一段的完整形状(协议版本、单元 id、卡片、工具、宿主与环境需要)。
27
+ */
28
+ export interface UnitDeclaration {
29
+ readonly apiVersion: number;
30
+ readonly unit: string;
31
+ readonly card: UnitCardDeclaration | null;
32
+ readonly tools: readonly UnitToolDeclaration[];
33
+ readonly host: UnitHostNeeds;
34
+ readonly env?: UnitEnvNeeds;
35
+ }
36
+ /**
37
+ * 协议认得的全部键;写了别的键不会被采纳(外壳静默忽略),所以字段名拼错不会有提示。
38
+ */
39
+ export declare const DECLARATION_KEYS: readonly string[];
40
+ /**
41
+ * 协议要求必写的键;env 不在其中(没有环境需要就不写)。
42
+ */
43
+ export declare const DECLARATION_REQUIRED_KEYS: readonly string[];
44
+ /**
45
+ * 查清单里 dsh.workbench 那一段的形状,回一串给人看的问题(中文,带字段路径)。
46
+ * @param subject 报错时用的主语,一般是包名。
47
+ * @param raw 清单里那段原始值,可能什么都不是。
48
+ * @returns 空数组表示形状过得去;版本不对就当场返回(后面的字段无从判起)。
49
+ */
50
+ export declare function declarationProblemsOf(subject: string, raw: unknown): readonly string[];
51
+ /**
52
+ * 读一份清单的结果:要么带着规整过的声明与 manifest,要么只带一串问题。
53
+ */
54
+ export interface DeclarationRead {
55
+ readonly declaration?: UnitDeclaration;
56
+ readonly manifest?: UnitManifest;
57
+ readonly problems: readonly string[];
58
+ }
59
+ /**
60
+ * 读一份清单:先查形状,过了再把声明规整成 manifest。
61
+ * @param subject 报错时用的主语。
62
+ * @param raw 清单里 dsh.workbench 那段原始值。
63
+ * @returns 有问题时只有 problems;没问题时 declaration 与 manifest 都给,manifest 里的字符串已 trim。
64
+ */
65
+ export declare function readDeclaration(subject: string, raw: unknown): DeclarationRead;
@@ -0,0 +1,213 @@
1
+ /**
2
+ * 声明协议当前版本;模块清单的 dsh.workbench.apiVersion 要写这个整数。
3
+ */
4
+ export const WORKBENCH_API_VERSION = 1;
5
+ /**
6
+ * 外壳愿意认的协议版本;不在表里的模块当场被拒,不猜语义。
7
+ */
8
+ export const SUPPORTED_API_VERSIONS = [WORKBENCH_API_VERSION];
9
+ /**
10
+ * 协议认得的全部键;写了别的键不会被采纳(外壳静默忽略),所以字段名拼错不会有提示。
11
+ */
12
+ export const DECLARATION_KEYS = [
13
+ 'apiVersion',
14
+ 'unit',
15
+ 'card',
16
+ 'tools',
17
+ 'host',
18
+ 'env',
19
+ ];
20
+ /**
21
+ * 协议要求必写的键;env 不在其中(没有环境需要就不写)。
22
+ */
23
+ export const DECLARATION_REQUIRED_KEYS = [
24
+ 'apiVersion',
25
+ 'unit',
26
+ 'card',
27
+ 'tools',
28
+ 'host',
29
+ ];
30
+ const CARD_KEYS = ['titleKey', 'order'];
31
+ const TOOL_KEYS = ['name', 'writes', 'key'];
32
+ const TOOL_KEY_SHAPE = /^[a-z][a-zA-Z0-9]*$/;
33
+ const HOST_KEYS = ['inject', 'optional'];
34
+ function text(value) {
35
+ return typeof value === 'string' ? value.trim() : '';
36
+ }
37
+ function isRecord(value) {
38
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
39
+ }
40
+ function outsideKeys(value, allowed) {
41
+ return Object.keys(value).filter((key) => !allowed.includes(key));
42
+ }
43
+ function listOf(keys) {
44
+ return keys.map((key) => JSON.stringify(key)).join(' / ');
45
+ }
46
+ function stringArrayProblems(at, value) {
47
+ if (!Array.isArray(value))
48
+ return [`${at} 要是一串字符串,现在是 ${JSON.stringify(value)}`];
49
+ return value.flatMap((item, index) => text(item) === '' ? [`${at}[${index}] 不是非空字符串:${JSON.stringify(item)}`] : []);
50
+ }
51
+ /**
52
+ * 查清单里 dsh.workbench 那一段的形状,回一串给人看的问题(中文,带字段路径)。
53
+ * @param subject 报错时用的主语,一般是包名。
54
+ * @param raw 清单里那段原始值,可能什么都不是。
55
+ * @returns 空数组表示形状过得去;版本不对就当场返回(后面的字段无从判起)。
56
+ */
57
+ export function declarationProblemsOf(subject, raw) {
58
+ if (!isRecord(raw))
59
+ return [`${subject}: dsh.workbench 不是一个对象 —— ${JSON.stringify(raw)}`];
60
+ const version = raw.apiVersion;
61
+ if (version === undefined) {
62
+ return [
63
+ `${subject}: dsh.workbench 没有 apiVersion —— 协议从 v${WORKBENCH_API_VERSION} 起,` +
64
+ `写 \`"apiVersion": ${WORKBENCH_API_VERSION}\`。缺它 = 外壳不认这个模块(ADR-0038 §2.3)`,
65
+ ];
66
+ }
67
+ if (!Number.isInteger(version) || !SUPPORTED_API_VERSIONS.includes(version)) {
68
+ return [
69
+ `${subject}: dsh.workbench.apiVersion 是 ${JSON.stringify(version)},这里只认 ` +
70
+ `${SUPPORTED_API_VERSIONS.join(' / ')}(要整数)`,
71
+ ];
72
+ }
73
+ const problems = [];
74
+ const unknown = outsideKeys(raw, DECLARATION_KEYS);
75
+ if (unknown.length > 0) {
76
+ problems.push(`${subject}: dsh.workbench 里有协议 v${WORKBENCH_API_VERSION} 不认识的键 ${listOf(unknown)} —— ` +
77
+ `只有 ${listOf(DECLARATION_KEYS)}。(拼错的键会被外壳**静默忽略**:那句声明等于没写)`);
78
+ }
79
+ for (const key of DECLARATION_REQUIRED_KEYS) {
80
+ if (raw[key] === undefined) {
81
+ problems.push(`${subject}: dsh.workbench 缺 ${key}` +
82
+ (key === 'card' ? '(这个模块没有卡片就写 `"card": null`)' : ''));
83
+ }
84
+ }
85
+ const card = raw.card;
86
+ if (card !== undefined && card !== null) {
87
+ if (!isRecord(card)) {
88
+ problems.push(`${subject}: dsh.workbench.card 要么是对象、要么是 null,现在是 ${JSON.stringify(card)}`);
89
+ }
90
+ else {
91
+ const extra = outsideKeys(card, CARD_KEYS);
92
+ if (extra.length > 0) {
93
+ problems.push(`${subject}: dsh.workbench.card 里有不认的键 ${listOf(extra)} —— card 只有 ${listOf(CARD_KEYS)}` +
94
+ `(卡片默认显不显示由运行期的面板配置决定,清单里没有这一位)`);
95
+ }
96
+ if (text(card.titleKey) === '') {
97
+ problems.push(`${subject}: dsh.workbench.card.titleKey 要是非空字符串(面板那一格的文案键)`);
98
+ }
99
+ if (typeof card.order !== 'number' || !Number.isFinite(card.order)) {
100
+ problems.push(`${subject}: dsh.workbench.card.order 要是数字,现在是 ${JSON.stringify(card.order)}`);
101
+ }
102
+ }
103
+ }
104
+ const tools = raw.tools;
105
+ if (tools !== undefined) {
106
+ if (!Array.isArray(tools)) {
107
+ problems.push(`${subject}: dsh.workbench.tools 要是一串声明,现在是 ${JSON.stringify(tools)}`);
108
+ }
109
+ else {
110
+ const seen = new Set();
111
+ const seenKeys = new Set();
112
+ tools.forEach((tool, index) => {
113
+ const at = `dsh.workbench.tools[${index}]`;
114
+ if (!isRecord(tool)) {
115
+ problems.push(`${subject}: ${at} 要是对象,现在是 ${JSON.stringify(tool)}`);
116
+ return;
117
+ }
118
+ const extra = outsideKeys(tool, TOOL_KEYS);
119
+ if (extra.length > 0) {
120
+ problems.push(`${subject}: ${at} 里有不认的键 ${listOf(extra)} —— tools 只有 ${listOf(TOOL_KEYS)}`);
121
+ }
122
+ const name = text(tool.name);
123
+ if (name === '') {
124
+ problems.push(`${subject}: ${at}.name 要是非空字符串`);
125
+ }
126
+ else if (seen.has(name)) {
127
+ problems.push(`${subject}: ${at}.name「${name}」与前面那条撞了 —— 工具名会进 dsh 的工具表,不许重`);
128
+ }
129
+ else {
130
+ seen.add(name);
131
+ }
132
+ if (typeof tool.writes !== 'boolean') {
133
+ problems.push(`${subject}: ${at}.writes 要是 true / false(它是"这会写"的唯一声明,缺了审批无从认领)`);
134
+ }
135
+ if (tool.key !== undefined) {
136
+ const key = text(tool.key);
137
+ if (!TOOL_KEY_SHAPE.test(key)) {
138
+ problems.push(`${subject}: ${at}.key 要是 lowerCamelCase 的标识符(如 \`sync\` / \`podLogs\`),` +
139
+ `现在是 ${JSON.stringify(tool.key)} —— 它会变成生成物里的键名,所以不引号、不许带点或横线`);
140
+ }
141
+ else if (seenKeys.has(key)) {
142
+ problems.push(`${subject}: ${at}.key「${key}」与前面那条撞了 —— 生成物里的键只能有一个`);
143
+ }
144
+ else {
145
+ seenKeys.add(key);
146
+ }
147
+ }
148
+ });
149
+ }
150
+ }
151
+ const host = raw.host;
152
+ if (host !== undefined) {
153
+ if (!isRecord(host)) {
154
+ problems.push(`${subject}: dsh.workbench.host 要是对象,现在是 ${JSON.stringify(host)}`);
155
+ }
156
+ else {
157
+ const extra = outsideKeys(host, HOST_KEYS);
158
+ if (extra.length > 0) {
159
+ problems.push(`${subject}: dsh.workbench.host 里有不认的键 ${listOf(extra)} —— host 只有 ${listOf(HOST_KEYS)}`);
160
+ }
161
+ if (host.inject === undefined) {
162
+ problems.push(`${subject}: dsh.workbench.host.inject 没写 —— 它声明这个模块要外壳的哪几个服务,` +
163
+ `外壳靠它判"能不能挂"。一个都不要也要写 \`"inject": []\``);
164
+ }
165
+ else {
166
+ problems.push(...stringArrayProblems(`${subject}: dsh.workbench.host.inject`, host.inject));
167
+ }
168
+ if (host.optional !== undefined) {
169
+ problems.push(...stringArrayProblems(`${subject}: dsh.workbench.host.optional`, host.optional));
170
+ }
171
+ }
172
+ }
173
+ if (raw.env !== undefined && !isRecord(raw.env)) {
174
+ problems.push(`${subject}: dsh.workbench.env 要是对象(command / plugins / libs)—— ` +
175
+ `语义由门禁的 env 判据查,这里只查形状`);
176
+ }
177
+ return problems;
178
+ }
179
+ /**
180
+ * 读一份清单:先查形状,过了再把声明规整成 manifest。
181
+ * @param subject 报错时用的主语。
182
+ * @param raw 清单里 dsh.workbench 那段原始值。
183
+ * @returns 有问题时只有 problems;没问题时 declaration 与 manifest 都给,manifest 里的字符串已 trim。
184
+ */
185
+ export function readDeclaration(subject, raw) {
186
+ const problems = declarationProblemsOf(subject, raw);
187
+ if (problems.length > 0)
188
+ return { problems };
189
+ const declaration = raw;
190
+ const card = declaration.card;
191
+ return {
192
+ declaration,
193
+ problems: [],
194
+ manifest: {
195
+ id: text(declaration.unit),
196
+ card: card === null
197
+ ? null
198
+ : {
199
+ titleKey: text(card.titleKey),
200
+ order: card.order,
201
+ },
202
+ tools: declaration.tools.map((tool) => ({
203
+ name: text(tool.name),
204
+ writes: tool.writes,
205
+ })),
206
+ host: {
207
+ inject: [...(declaration.host.inject ?? [])],
208
+ ...(declaration.host.optional === undefined ? {} : { optional: [...declaration.host.optional] }),
209
+ },
210
+ ...(declaration.env === undefined ? {} : { env: declaration.env }),
211
+ },
212
+ };
213
+ }
@@ -28,6 +28,7 @@ export interface UnitMountDeps {
28
28
  readonly force?: boolean;
29
29
  }) => Promise<UnitCapability | undefined>;
30
30
  readonly longTasks?: LongTaskHost | undefined;
31
+ readonly longTaskRuns?: (() => ReadonlyMap<string, LongTaskRegistry<LongTaskBase>>) | undefined;
31
32
  }
32
33
  export type ContextRenderer = (ref: {
33
34
  readonly kind: string;
@@ -0,0 +1,7 @@
1
+ import type { UnitManifest } from '@dsh-workbench/shared';
2
+ export interface UnitReadiness {
3
+ readonly canMount: boolean;
4
+ readonly missingHard: readonly string[];
5
+ readonly missingSoft: readonly string[];
6
+ }
7
+ export declare function unitReadiness(manifest: UnitManifest, lookup: (name: string) => unknown): UnitReadiness;
@@ -0,0 +1,5 @@
1
+ export function unitReadiness(manifest, lookup) {
2
+ const missingHard = manifest.host.inject.filter((name) => lookup(name) === undefined);
3
+ const missingSoft = (manifest.host.optional ?? []).filter((name) => lookup(name) === undefined);
4
+ return { canMount: missingHard.length === 0, missingHard, missingSoft };
5
+ }
@@ -0,0 +1,9 @@
1
+ import type { UnitManifest } from '@dsh-workbench/shared';
2
+ import type { UnitHostFace } from './unit-face';
3
+ export interface UnitRegistry {
4
+ register(face: UnitHostFace): () => void;
5
+ all(): readonly UnitHostFace[];
6
+ manifests(): readonly UnitManifest[];
7
+ subscribe(listener: () => void): () => void;
8
+ }
9
+ export declare function createUnitRegistry(): UnitRegistry;
@@ -0,0 +1,33 @@
1
+ export function createUnitRegistry() {
2
+ const faces = [];
3
+ const ids = new Set();
4
+ const listeners = new Set();
5
+ function notify() {
6
+ for (const listener of listeners)
7
+ listener();
8
+ }
9
+ return {
10
+ register(face) {
11
+ const id = face.manifest.id;
12
+ if (ids.has(id))
13
+ throw new Error(`套件 id 重复:${id}(id 会进持久化配置,不能撞)`);
14
+ ids.add(id);
15
+ faces.push(face);
16
+ notify();
17
+ return () => {
18
+ const index = faces.indexOf(face);
19
+ if (index < 0)
20
+ return;
21
+ faces.splice(index, 1);
22
+ ids.delete(id);
23
+ notify();
24
+ };
25
+ },
26
+ all: () => faces,
27
+ manifests: () => faces.map((face) => face.manifest),
28
+ subscribe(listener) {
29
+ listeners.add(listener);
30
+ return () => listeners.delete(listener);
31
+ },
32
+ };
33
+ }
@@ -0,0 +1,2 @@
1
+ export type VersionVerdict = 'satisfied' | 'unsatisfied' | 'malformed';
2
+ export declare function checkVersionRange(contract: string | undefined, version: string): VersionVerdict;