@dsh-workbench/shared 0.3.4 → 0.3.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.
Files changed (180) hide show
  1. package/dist/api/blocker-table.d.ts +13 -0
  2. package/dist/api/blocker-table.js +13 -0
  3. package/dist/api/blocker.d.ts +6 -0
  4. package/dist/api/context-ref.d.ts +31 -0
  5. package/dist/api/context-ref.js +28 -0
  6. package/dist/api/dsh-client.d.ts +99 -0
  7. package/dist/api/dsh.d.ts +55 -3
  8. package/dist/api/facts.d.ts +42 -0
  9. package/dist/api/facts.js +33 -0
  10. package/dist/api/host-write.d.ts +18 -0
  11. package/dist/api/host-write.js +12 -0
  12. package/dist/api/long-task.d.ts +36 -0
  13. package/dist/api/long-task.js +27 -0
  14. package/dist/api/unit-board.d.ts +25 -0
  15. package/dist/api/unit-board.js +16 -0
  16. package/dist/api/unit-declaration.d.ts +36 -0
  17. package/dist/api/unit-declaration.js +24 -0
  18. package/dist/api/unit.d.ts +32 -0
  19. package/dist/api/unit.js +5 -0
  20. package/dist/api/workbench-client.d.ts +25 -0
  21. package/dist/api/workbench-client.js +13 -0
  22. package/dist/api/workbench-contract.d.ts +12 -0
  23. package/dist/components/Activity.d.ts +33 -0
  24. package/dist/components/Activity.js +24 -0
  25. package/dist/components/ApprovalDetail.d.ts +12 -0
  26. package/dist/components/ApprovalDetail.js +9 -0
  27. package/dist/components/ApprovalPlan.d.ts +14 -0
  28. package/dist/components/ApprovalPlan.js +8 -0
  29. package/dist/components/Card.d.ts +13 -0
  30. package/dist/components/Card.js +10 -0
  31. package/dist/components/CardAnchor.d.ts +14 -0
  32. package/dist/components/CardAnchor.js +11 -0
  33. package/dist/components/CardBoundary.d.ts +20 -0
  34. package/dist/components/CardBoundary.js +14 -0
  35. package/dist/components/CardHead.d.ts +13 -0
  36. package/dist/components/CardHead.js +10 -0
  37. package/dist/components/ChoiceGroup.d.ts +24 -0
  38. package/dist/components/ChoiceGroup.js +12 -0
  39. package/dist/components/Field.d.ts +10 -0
  40. package/dist/components/Field.js +7 -0
  41. package/dist/components/Form.css +6 -0
  42. package/dist/components/Form.d.ts +20 -0
  43. package/dist/components/Form.js +58 -0
  44. package/dist/components/FormActions.d.ts +14 -0
  45. package/dist/components/FormActions.js +5 -0
  46. package/dist/components/FormNodes.d.ts +103 -0
  47. package/dist/components/FormNodes.js +153 -0
  48. package/dist/components/FormRows.d.ts +20 -0
  49. package/dist/components/FormRows.js +14 -0
  50. package/dist/components/FormTypes.d.ts +59 -0
  51. package/dist/components/FormTypes.js +1 -0
  52. package/dist/components/Link.d.ts +12 -0
  53. package/dist/components/Link.js +9 -0
  54. package/dist/components/LongTaskRow.d.ts +19 -0
  55. package/dist/components/LongTaskRow.js +13 -0
  56. package/dist/components/Note.d.ts +11 -0
  57. package/dist/components/Note.js +8 -0
  58. package/dist/components/ResourceStates.d.ts +25 -0
  59. package/dist/components/ResourceStates.js +22 -0
  60. package/dist/components/Rows.d.ts +17 -0
  61. package/dist/components/Rows.js +11 -0
  62. package/dist/components/SaveBar.d.ts +12 -0
  63. package/dist/components/SaveBar.js +9 -0
  64. package/dist/components/SelectField.d.ts +26 -0
  65. package/dist/components/SelectField.js +22 -0
  66. package/dist/components/SelectFieldNodes.d.ts +24 -0
  67. package/dist/components/SelectFieldNodes.js +24 -0
  68. package/dist/components/SettingsSection.d.ts +11 -0
  69. package/dist/components/SettingsSection.js +8 -0
  70. package/dist/components/Spinner.d.ts +9 -0
  71. package/dist/components/Spinner.js +6 -0
  72. package/dist/components/Stack.d.ts +8 -0
  73. package/dist/components/Stack.js +5 -0
  74. package/dist/components/StateBlock.d.ts +13 -0
  75. package/dist/components/StateBlock.js +10 -0
  76. package/dist/components/TextField.d.ts +8 -0
  77. package/dist/components/TextField.js +5 -0
  78. package/dist/components/board-module.d.ts +30 -0
  79. package/dist/components/icons.d.ts +38 -0
  80. package/dist/components/icons.js +35 -0
  81. package/dist/config/constants.d.ts +42 -0
  82. package/dist/config/constants.js +42 -0
  83. package/dist/config/storage.d.ts +4 -0
  84. package/dist/config/storage.js +4 -0
  85. package/dist/host/unit-face.d.ts +5 -1
  86. package/dist/i18n/en.d.ts +1 -0
  87. package/dist/i18n/en.js +8 -8
  88. package/dist/i18n/index.d.ts +11 -9
  89. package/dist/i18n/index.js +1 -0
  90. package/dist/i18n/merge.d.ts +5 -0
  91. package/dist/i18n/merge.js +2 -0
  92. package/dist/i18n/zh.d.ts +9 -8
  93. package/dist/i18n/zh.js +8 -8
  94. package/dist/index.d.ts +6 -2
  95. package/dist/index.js +6 -2
  96. package/dist/lib/blocker-action.d.ts +22 -0
  97. package/dist/lib/blocker-action.js +13 -0
  98. package/dist/lib/board-fact.d.ts +27 -0
  99. package/dist/lib/board-fact.js +18 -0
  100. package/dist/lib/busy.d.ts +10 -0
  101. package/dist/lib/busy.js +7 -0
  102. package/dist/lib/diy-debug.d.ts +42 -2
  103. package/dist/lib/diy-debug.js +19 -1
  104. package/dist/lib/diy-record.d.ts +41 -0
  105. package/dist/lib/diy-record.js +17 -0
  106. package/dist/lib/form-problem.d.ts +10 -0
  107. package/dist/lib/form-problem.js +10 -0
  108. package/dist/lib/panel-state.d.ts +60 -0
  109. package/dist/lib/panel-state.js +30 -0
  110. package/dist/lib/react.d.ts +39 -0
  111. package/dist/lib/react.js +36 -0
  112. package/dist/lib/text.d.ts +22 -0
  113. package/dist/lib/text.js +22 -0
  114. package/dist/lib/ui.d.ts +50 -0
  115. package/dist/lib/ui.js +41 -0
  116. package/package.json +1 -1
  117. package/src/api/blocker-table.ts +13 -0
  118. package/src/api/blocker.ts +6 -0
  119. package/src/api/context-ref.ts +31 -0
  120. package/src/api/dsh-client.ts +99 -0
  121. package/src/api/dsh.ts +56 -1
  122. package/src/api/facts.ts +42 -0
  123. package/src/api/host-write.ts +18 -0
  124. package/src/api/long-task.ts +36 -0
  125. package/src/api/unit-board.ts +25 -0
  126. package/src/api/unit-declaration.ts +36 -0
  127. package/src/api/unit.ts +32 -0
  128. package/src/api/workbench-client.ts +25 -0
  129. package/src/api/workbench-contract.ts +12 -0
  130. package/src/components/Activity.tsx +33 -0
  131. package/src/components/ApprovalDetail.tsx +12 -0
  132. package/src/components/ApprovalPlan.tsx +14 -0
  133. package/src/components/Card.tsx +13 -0
  134. package/src/components/CardAnchor.tsx +14 -0
  135. package/src/components/CardBoundary.tsx +20 -0
  136. package/src/components/CardHead.tsx +13 -0
  137. package/src/components/ChoiceGroup.tsx +24 -0
  138. package/src/components/Field.tsx +10 -0
  139. package/src/components/Form.css +6 -0
  140. package/src/components/Form.test.tsx +475 -0
  141. package/src/components/Form.tsx +78 -0
  142. package/src/components/FormActions.tsx +14 -0
  143. package/src/components/FormNodes.tsx +193 -0
  144. package/src/components/FormRows.tsx +20 -0
  145. package/src/components/FormTypes.ts +65 -0
  146. package/src/components/FormWiring.test.tsx +88 -0
  147. package/src/components/Link.tsx +12 -0
  148. package/src/components/LongTaskRow.tsx +19 -0
  149. package/src/components/Note.tsx +11 -0
  150. package/src/components/ResourceStates.tsx +25 -0
  151. package/src/components/Rows.tsx +17 -0
  152. package/src/components/SaveBar.tsx +12 -0
  153. package/src/components/SelectField.tsx +26 -0
  154. package/src/components/SelectFieldNodes.tsx +24 -0
  155. package/src/components/SettingsSection.tsx +11 -0
  156. package/src/components/Spinner.tsx +9 -0
  157. package/src/components/Stack.tsx +8 -0
  158. package/src/components/StateBlock.tsx +13 -0
  159. package/src/components/TextField.tsx +8 -0
  160. package/src/components/board-module.ts +30 -0
  161. package/src/components/icons.tsx +38 -0
  162. package/src/config/constants.ts +43 -0
  163. package/src/config/storage.ts +4 -0
  164. package/src/host/unit-face.ts +6 -1
  165. package/src/i18n/en.ts +8 -8
  166. package/src/i18n/index.ts +3 -0
  167. package/src/i18n/merge.ts +5 -0
  168. package/src/i18n/zh.ts +9 -8
  169. package/src/index.ts +6 -2
  170. package/src/lib/blocker-action.ts +22 -0
  171. package/src/lib/board-fact.ts +27 -0
  172. package/src/lib/busy.ts +10 -0
  173. package/src/lib/diy-debug.test.ts +2 -1
  174. package/src/lib/diy-debug.ts +44 -3
  175. package/src/lib/diy-record.ts +41 -0
  176. package/src/lib/form-problem.ts +10 -0
  177. package/src/lib/panel-state.ts +60 -0
  178. package/src/lib/react.ts +39 -0
  179. package/src/lib/text.ts +22 -0
  180. package/src/lib/ui.ts +50 -0
package/src/api/dsh.ts CHANGED
@@ -1,14 +1,23 @@
1
+ /**
2
+ * 宿主侧的日志口;info 与 warn 都可能没有,调用前要判存在。
3
+ */
1
4
  export interface DshLogger {
2
5
  info?(message: string): void;
3
6
  warn?(message: string): void;
4
7
  }
5
8
 
9
+ /**
10
+ * 宿主 HTTP 路由拿到的请求:方法与 URL,请求体是异步可迭代的字节流。
11
+ */
6
12
  export interface DshRequest {
7
13
  readonly method?: string;
8
14
  readonly url?: string;
9
15
  [Symbol.asyncIterator]?(): AsyncIterator<Uint8Array>;
10
16
  }
11
17
 
18
+ /**
19
+ * 宿主 HTTP 路由要写的响应:statusCode、setHeader 与 end 必备,流式那几个方法可选。
20
+ */
12
21
  export interface DshResponse {
13
22
  statusCode: number;
14
23
  setHeader(name: string, value: string): void;
@@ -18,32 +27,50 @@ export interface DshResponse {
18
27
  on?(event: 'close', listener: () => void): void;
19
28
  }
20
29
 
30
+ /**
31
+ * 一条挂到 dsh web 服务器上的路由;kind 说明 path 是前缀还是全等匹配,handler 自己写响应。
32
+ */
21
33
  export interface DshWebRoute {
22
34
  readonly kind: 'prefix' | 'exact';
23
35
  readonly path: string;
24
36
  readonly handler: (req: DshRequest, res: DshResponse) => void | Promise<void>;
25
37
  }
26
38
 
39
+ /**
40
+ * 宿主插件能看到的 profile 现场;dir 缺省表示 dsh 没给这个信息。
41
+ */
27
42
  export interface DshProfileContext {
28
43
  readonly dir?: string;
29
44
  }
30
45
 
46
+ /**
47
+ * 工具声明的静态半(名字加其余关键字);外壳只按 name 认工具。
48
+ */
31
49
  export interface DshToolSchema {
32
50
  readonly name: string;
33
51
  readonly [key: string]: unknown;
34
52
  }
35
53
 
54
+ /**
55
+ * 系统提示词装配到一半的那份;tools 是可改的工具表,其余字段随 dsh。
56
+ */
36
57
  export interface DshPromptAssembly {
37
58
  readonly tools?: readonly DshToolSchema[] | undefined;
38
59
  readonly [key: string]: unknown;
39
60
  }
40
61
 
62
+ /**
63
+ * 装配提示词时给判定方的现场(哪个 agent、哪个作用域);两者都可能缺。
64
+ */
41
65
  export interface DshAssemblyContext {
42
66
  readonly agent?: unknown;
43
67
  readonly scope?: unknown;
44
68
  readonly [key: string]: unknown;
45
69
  }
46
70
 
71
+ /**
72
+ * 宿主插件上下文:on 订阅 dsh 的钩子(提示词装配、工具执行前后、agent 生命周期),webServer 挂路由。
73
+ */
47
74
  export interface DshHostContext {
48
75
  on?: {
49
76
  (
@@ -107,6 +134,9 @@ export interface DshHostContext {
107
134
  readonly webServer: { register(route: DshWebRoute): () => void };
108
135
  }
109
136
 
137
+ /**
138
+ * 一次 shell 执行的结果口:result() 给退出码、超时与中断标志,以及截断过的 stdout / stderr。
139
+ */
110
140
  export interface DshShellExecution {
111
141
  result(): Promise<{
112
142
  exitCode: number | null;
@@ -117,6 +147,9 @@ export interface DshShellExecution {
117
147
  }>;
118
148
  }
119
149
 
150
+ /**
151
+ * dsh 的 shell 服务:先 resolve 出执行规格,再把它原样喂给 execute。
152
+ */
120
153
  export interface DshShell {
121
154
  resolve(request: {
122
155
  command: string;
@@ -129,14 +162,30 @@ export interface DshShell {
129
162
  execute(spec: unknown): Promise<DshShellExecution>;
130
163
  }
131
164
 
165
+ /** 一条工作区记录:id、磁盘路径与显示名(登记处按路径认领 / 新建时认的就是它)。 */
166
+ export interface DshWorkspaceRecord {
167
+ readonly id: string;
168
+ readonly path: string;
169
+ readonly title: string;
170
+ }
171
+
172
+ /**
173
+ * 宿主侧的工作区登记处:按 id 查、按路径认领、按路径新建。
174
+ */
132
175
  export interface DshWorkspaceRegistry {
133
176
  get(id: string): { id: string; path?: string; title?: string } | undefined;
134
- resolveByPath(path: string): Promise<{ readonly id: unknown } | undefined>;
177
+ resolveByPath(path: string): Promise<DshWorkspaceRecord | undefined>;
135
178
  create(path: string, title?: string): Promise<{ id?: string; path?: string; title?: string } | undefined>;
136
179
  }
137
180
 
181
+ /**
182
+ * 把相对路径拼成 dsh home 下的绝对路径(服务名 dshHomePath);外壳存自己的状态用它,不自己拼 home。
183
+ */
138
184
  export type DshHomePath = (relative: string) => string;
139
185
 
186
+ /**
187
+ * 要注册进 dsh 的一把工具:名字、描述、参数表、输出渲染与执行体。
188
+ */
140
189
  export interface DshToolDefinition {
141
190
  name: string;
142
191
  description: string;
@@ -148,6 +197,9 @@ export interface DshToolDefinition {
148
197
  execute(args: unknown, exec: DshToolRunContext): Promise<unknown>;
149
198
  }
150
199
 
200
+ /**
201
+ * 工具执行时给的那点现场(会话 id 与 cwd);层级都可能缺,缺了就当不知道。
202
+ */
151
203
  export interface DshToolRunContext {
152
204
  readonly agent?:
153
205
  | {
@@ -161,6 +213,9 @@ export interface DshToolRunContext {
161
213
  | undefined;
162
214
  }
163
215
 
216
+ /**
217
+ * dsh 的工具表;register 回的清理函数负责在插件卸载时摘掉这条工具。
218
+ */
164
219
  export interface DshToolRegistry {
165
220
  register(definition: DshToolDefinition): () => void;
166
221
  }
package/src/api/facts.ts CHANGED
@@ -1,27 +1,54 @@
1
+ /**
2
+ * 宿主推来的一帧事实变更:rev 在同一纪元里单调,facts 是这一帧动过的名字。
3
+ */
1
4
  export interface FactFrame {
2
5
  readonly epoch?: string;
3
6
  readonly rev: number;
4
7
  readonly facts: readonly string[];
5
8
  }
6
9
 
10
+ /**
11
+ * 范围查法的后缀:cmdb/* 这类名字表示「这个前缀下的一族」,不是一条事实。
12
+ */
7
13
  export const FACT_SCOPE_SUFFIX = '*';
8
14
 
15
+ /**
16
+ * 由单元 id 拼出它那一族事实的范围名。
17
+ * @param unit 单元 id,不带后缀。
18
+ * @returns <unit>* —— 永远是范围名,不是某一条事实。
19
+ */
9
20
  export function unitFactScope(unit: string): string {
10
21
  return `${unit}${FACT_SCOPE_SUFFIX}`;
11
22
  }
12
23
 
24
+ /**
25
+ * 判一个名字是不是范围查法,不是问这条事实存不存在。
26
+ * @param name 事实名或范围名。
27
+ * @returns 单字符 * 不算范围;只有「至少一个字符加 *」才算。
28
+ */
13
29
  export function isFactScope(name: string): boolean {
14
30
  return name.length > 1 && name.endsWith(FACT_SCOPE_SUFFIX);
15
31
  }
16
32
 
33
+ /**
34
+ * 取范围名对应的前缀,普通事实名原样返回。
35
+ * @param name 事实名或范围名。
36
+ * @returns 去掉末尾 * 的前缀;本来不是范围就给原值。
37
+ */
17
38
  export function scopePrefixOf(name: string): string {
18
39
  return isFactScope(name) ? name.slice(0, -FACT_SCOPE_SUFFIX.length) : name;
19
40
  }
20
41
 
42
+ /**
43
+ * 事实的来路:start 建连并按帧回调,cursor 报已收到的宿主 rev(断线重连时交给宿主)。
44
+ */
21
45
  export interface FactTransport {
22
46
  start(handlers: { readonly onFrame: (frame: FactFrame) => void; readonly cursor: () => number }): () => void;
23
47
  }
24
48
 
49
+ /**
50
+ * 浏览器侧的事实通道:订阅、读版本、手动刷新,帧到了由它分发给订阅者。
51
+ */
25
52
  export interface FactChannel {
26
53
  subscribe(fact: string, listener: () => void): () => void;
27
54
  revisionOf(fact: string): number;
@@ -35,6 +62,11 @@ interface FactEntry {
35
62
  readonly listeners: Set<() => void>;
36
63
  }
37
64
 
65
+ /**
66
+ * 造一条事实通道:第一次订阅时才连传输层,最后一个订阅者走掉就断开。
67
+ * @param options 传输层,用来连宿主。
68
+ * @returns 通道句柄;没记录过的事实版本给 0,refresh() 不带事实名时刷新全部并逐个通知。
69
+ */
38
70
  export function createFactChannel(options: { readonly transport: FactTransport }): FactChannel {
39
71
  const entries = new Map<string, FactEntry>();
40
72
  let epoch: string | null = null;
@@ -146,6 +178,11 @@ export function createFactChannel(options: { readonly transport: FactTransport }
146
178
  return channel;
147
179
  }
148
180
 
181
+ /**
182
+ * 把传输层收到的原始数据读成一帧;坏数据当没收到。
183
+ * @param data 帧的原始数据,期望是 JSON 字符串。
184
+ * @returns 合法帧;不是字符串、JSON 坏了、或 rev 不是数字都给 null。
185
+ */
149
186
  export function parseFactFrame(data: unknown): FactFrame | null {
150
187
  if (typeof data !== 'string') return null;
151
188
  try {
@@ -161,6 +198,11 @@ export function parseFactFrame(data: unknown): FactFrame | null {
161
198
  }
162
199
  }
163
200
 
201
+ /**
202
+ * 读口路由到事实名的映射,当前一对一。
203
+ * @param route 读口路由。
204
+ * @returns 该路由对应的事实名(就是路由本身)。
205
+ */
164
206
  export function factOfRoute(route: string): string {
165
207
  return route;
166
208
  }
@@ -1,5 +1,8 @@
1
1
  import type { ApiResponse } from './workbench-client.js';
2
2
 
3
+ /**
4
+ * 宿主写口的五条通道(偏好、动作、供给、工坊调试、工坊录制),是写路由的第一段。
5
+ */
3
6
  export type WriteChannel =
4
7
  | 'prefs'
5
8
  | 'actions'
@@ -7,11 +10,20 @@ export type WriteChannel =
7
10
  | 'diy-debug'
8
11
  | 'diy-record';
9
12
 
13
+ /**
14
+ * 由通道与资源拼出写路由。
15
+ * @param channel 写通道。
16
+ * @param resource 该通道下的资源名。
17
+ * @returns 路由字符串;provision 只有一段,其余是 channel/resource。
18
+ */
10
19
  export function writeRoute(channel: WriteChannel, resource: string): string {
11
20
  if (channel === 'provision') return 'provision';
12
21
  return `${channel}/${resource}`;
13
22
  }
14
23
 
24
+ /**
25
+ * 写通道统一的结果:成了带值,没成带一句能给人看的话(可选明细与帮助链接)。
26
+ */
15
27
  export type WriteOutcome<T> =
16
28
  | { readonly ok: true; readonly value: T }
17
29
  | {
@@ -21,6 +33,12 @@ export type WriteOutcome<T> =
21
33
  readonly helpUrl?: string;
22
34
  };
23
35
 
36
+ /**
37
+ * 把写通道的响应翻成 WriteOutcome。
38
+ * @param response 写口回的响应;null 表示请求本身没成(网络断了,宿主没答)。
39
+ * @param networkText 拿不到任何服务端文案时兜底的那句。
40
+ * @returns 失败时 message 优先用服务端自己的话,没有才用响应正文或 networkText。
41
+ */
24
42
  export function writeOutcomeOf<T>(response: ApiResponse<T> | null, networkText: string): WriteOutcome<T> {
25
43
  if (response === null) return { ok: false, message: networkText };
26
44
  if (!response.ok) {
@@ -1,28 +1,59 @@
1
+ /**
2
+ * 一条长任务的四种态;needs-login 与 running 一样算还没结束。
3
+ */
1
4
  export type LongTaskStatus = 'running' | 'needs-login' | 'ok' | 'failed';
2
5
 
6
+ /**
7
+ * 长任务步骤流里的一行:kind 定样式,登录那一步带 loginUrl 给用户去点。
8
+ */
3
9
  export interface LongTaskStep {
4
10
  readonly kind: 'start' | 'succeed' | 'fail' | 'info' | 'warn';
5
11
  readonly text: string;
6
12
  readonly loginUrl?: string | undefined;
7
13
  }
8
14
 
15
+ /**
16
+ * 单元的长任务读口路由。
17
+ * @param unit 单元 id。
18
+ * @returns <unit>/runs。
19
+ */
9
20
  export function runsRouteOf(unit: string): string {
10
21
  return `${unit}/runs`;
11
22
  }
12
23
 
24
+ /**
25
+ * 长任务读口回的载荷:一串 run,形状由模块自己定,外壳只透传。
26
+ */
13
27
  export interface LongTasksPayload {
14
28
  readonly runs: readonly Readonly<Record<string, unknown>>[];
15
29
  }
16
30
 
31
+ /**
32
+ * 有长任务在跑时的轮询间隔(毫秒)。
33
+ */
17
34
  export const LONG_TASK_POLL_MS = 1500;
18
35
 
36
+ /**
37
+ * 没有长任务时的轮询间隔(毫秒),比在跑那一档大一个量级。
38
+ */
19
39
  export const LONG_TASK_IDLE_MS = 10_000;
20
40
 
41
+ /**
42
+ * 算这次该隔多久再问一次长任务。
43
+ * @param live 是否有在跑的长任务。
44
+ * @param override 调用点指定的间隔;给了就用它,不再看 live。
45
+ * @returns 毫秒:有活 1500,没活 10000。
46
+ */
21
47
  export function longTaskPollMs(live: boolean, override?: number | undefined): number {
22
48
  if (override !== undefined) return override;
23
49
  return live ? LONG_TASK_POLL_MS : LONG_TASK_IDLE_MS;
24
50
  }
25
51
 
52
+ /**
53
+ * 从读口状态里取 run 列表。
54
+ * @param state 读口状态;value 还没到就按空算。
55
+ * @returns 一串 run(形状由调用点自己判),没有就给空数组。
56
+ */
26
57
  export function longTaskRunsOf<T>(state: {
27
58
  readonly phase?: string | undefined;
28
59
  readonly value?: LongTasksPayload | undefined;
@@ -30,6 +61,11 @@ export function longTaskRunsOf<T>(state: {
30
61
  return (state.value?.runs ?? []) as readonly T[];
31
62
  }
32
63
 
64
+ /**
65
+ * 把长任务态折成状态点的那一档。
66
+ * @param status 长任务的四种态。
67
+ * @returns ongoing / done / error / idle;needs-login 也算进行中。
68
+ */
33
69
  export function longTaskDot(status: LongTaskStatus): 'ongoing' | 'done' | 'error' | 'idle' {
34
70
  if (status === 'ok') return 'done';
35
71
  if (status === 'failed') return 'error';
@@ -2,25 +2,45 @@ import type { ModuleBlocker } from './blocker.js';
2
2
  import type { CapabilityPayload } from './workbench-contract.js';
3
3
  import type { ApiError } from './workbench-client.js';
4
4
 
5
+ /**
6
+ * 卡片闸的两种档:capability 按环境读数判,none 无条件放行。
7
+ */
5
8
  export type GateMode =
6
9
  | 'capability'
7
10
  | 'none';
8
11
 
12
+ /**
13
+ * 一次放行判定的结果;gated 为假时卡片不画。
14
+ */
9
15
  export interface UnitGate {
10
16
  readonly gated: boolean;
11
17
  }
12
18
 
19
+ /**
20
+ * 判这张卡现在能不能画。
21
+ * @param capability 环境读数;还没读到给 null。
22
+ * @param mode 闸档。
23
+ * @returns mode 为 none 或读数还没到都放行(先放行让它读);读到明确不通过(ok 与 unchecked 之外)才拦。
24
+ */
13
25
  export function gateOf(capability: CapabilityPayload | null, mode: GateMode): UnitGate {
14
26
  if (mode === 'none') return { gated: true };
15
27
  if (capability === null) return { gated: true };
16
28
  return { gated: capability.status === 'ok' || capability.status === 'unchecked' };
17
29
  }
18
30
 
31
+ /**
32
+ * 一个单元攒下的卡住态:blocker 顶掉卡片,notice 只提示;组装方产出,卡片消费。
33
+ */
19
34
  export interface UnitBlockers {
20
35
  readonly blocker?: ModuleBlocker;
21
36
  readonly notice?: ModuleBlocker;
22
37
  }
23
38
 
39
+ /**
40
+ * 把候选卡住态分档,每档只留第一个。
41
+ * @param candidates 候选(可能含 undefined),按优先级从高到低给。
42
+ * @returns 分出的 blocker 与 notice;已经有 blocker 时不带 notice(挡住就不必再提示)。
43
+ */
24
44
  export function splitBlocker(candidates: readonly (ModuleBlocker | undefined)[]): UnitBlockers {
25
45
  let blocker: ModuleBlocker | undefined;
26
46
  let notice: ModuleBlocker | undefined;
@@ -38,6 +58,11 @@ export function splitBlocker(candidates: readonly (ModuleBlocker | undefined)[])
38
58
  };
39
59
  }
40
60
 
61
+ /**
62
+ * 由读口错误算出「出错搭车强探」的防抖键:同一个键只自动重探一次。
63
+ * @param error 读口错误;undefined 表示这次没有错误。
64
+ * @returns 键字符串;空 code 归一到 unknown。
65
+ */
41
66
  export function probeKeyOf(error: ApiError | undefined): string | null {
42
67
  if (error === undefined) return null;
43
68
  return error.code === '' ? 'unknown' : error.code;
@@ -1,20 +1,35 @@
1
1
  import type { UnitEnvNeeds, UnitHostNeeds, UnitManifest } from './unit.js';
2
2
 
3
+ /**
4
+ * 声明协议当前版本;模块清单的 dsh.workbench.apiVersion 要写这个整数。
5
+ */
3
6
  export const WORKBENCH_API_VERSION = 1;
4
7
 
8
+ /**
9
+ * 外壳愿意认的协议版本;不在表里的模块当场被拒,不猜语义。
10
+ */
5
11
  export const SUPPORTED_API_VERSIONS: readonly number[] = [WORKBENCH_API_VERSION];
6
12
 
13
+ /**
14
+ * 清单里那半张卡片声明:文案键加排序位;卡片默认显不显示不在这里(那是运行期面板配置的事)。
15
+ */
7
16
  export interface UnitCardDeclaration {
8
17
  readonly titleKey: string;
9
18
  readonly order: number;
10
19
  }
11
20
 
21
+ /**
22
+ * 清单里的一条工具声明:进 dsh 工具表的全名、会不会写,以及生成物里的键名。
23
+ */
12
24
  export interface UnitToolDeclaration {
13
25
  readonly name: string;
14
26
  readonly writes: boolean;
15
27
  readonly key?: string;
16
28
  }
17
29
 
30
+ /**
31
+ * 模块清单里 dsh.workbench 那一段的完整形状(协议版本、单元 id、卡片、工具、宿主与环境需要)。
32
+ */
18
33
  export interface UnitDeclaration {
19
34
  readonly apiVersion: number;
20
35
  readonly unit: string;
@@ -24,6 +39,9 @@ export interface UnitDeclaration {
24
39
  readonly env?: UnitEnvNeeds;
25
40
  }
26
41
 
42
+ /**
43
+ * 协议认得的全部键;写了别的键不会被采纳(外壳静默忽略),所以字段名拼错不会有提示。
44
+ */
27
45
  export const DECLARATION_KEYS: readonly string[] = [
28
46
  'apiVersion',
29
47
  'unit',
@@ -33,6 +51,9 @@ export const DECLARATION_KEYS: readonly string[] = [
33
51
  'env',
34
52
  ];
35
53
 
54
+ /**
55
+ * 协议要求必写的键;env 不在其中(没有环境需要就不写)。
56
+ */
36
57
  export const DECLARATION_REQUIRED_KEYS: readonly string[] = [
37
58
  'apiVersion',
38
59
  'unit',
@@ -70,6 +91,12 @@ function stringArrayProblems(at: string, value: unknown): string[] {
70
91
  );
71
92
  }
72
93
 
94
+ /**
95
+ * 查清单里 dsh.workbench 那一段的形状,回一串给人看的问题(中文,带字段路径)。
96
+ * @param subject 报错时用的主语,一般是包名。
97
+ * @param raw 清单里那段原始值,可能什么都不是。
98
+ * @returns 空数组表示形状过得去;版本不对就当场返回(后面的字段无从判起)。
99
+ */
73
100
  export function declarationProblemsOf(subject: string, raw: unknown): readonly string[] {
74
101
  if (!isRecord(raw)) return [`${subject}: dsh.workbench 不是一个对象 —— ${JSON.stringify(raw)}`];
75
102
 
@@ -207,12 +234,21 @@ export function declarationProblemsOf(subject: string, raw: unknown): readonly s
207
234
  return problems;
208
235
  }
209
236
 
237
+ /**
238
+ * 读一份清单的结果:要么带着规整过的声明与 manifest,要么只带一串问题。
239
+ */
210
240
  export interface DeclarationRead {
211
241
  readonly declaration?: UnitDeclaration;
212
242
  readonly manifest?: UnitManifest;
213
243
  readonly problems: readonly string[];
214
244
  }
215
245
 
246
+ /**
247
+ * 读一份清单:先查形状,过了再把声明规整成 manifest。
248
+ * @param subject 报错时用的主语。
249
+ * @param raw 清单里 dsh.workbench 那段原始值。
250
+ * @returns 有问题时只有 problems;没问题时 declaration 与 manifest 都给,manifest 里的字符串已 trim。
251
+ */
216
252
  export function readDeclaration(subject: string, raw: unknown): DeclarationRead {
217
253
  const problems = declarationProblemsOf(subject, raw);
218
254
  if (problems.length > 0) return { problems };
package/src/api/unit.ts CHANGED
@@ -1,18 +1,30 @@
1
+ /**
2
+ * 一张卡片在清单里的静态声明(文案键加排序位);默认显不显示由运行期面板配置定。
3
+ */
1
4
  export interface UnitCard {
2
5
  readonly titleKey: string;
3
6
  readonly order: number;
4
7
  }
5
8
 
9
+ /**
10
+ * 一条工具在清单里的样子:全名加会不会写;审批按 writes 认领。
11
+ */
6
12
  export interface UnitTool {
7
13
  readonly name: string;
8
14
  readonly writes: boolean;
9
15
  }
10
16
 
17
+ /**
18
+ * 模块要外壳的哪些服务:inject 缺一个就挂不上,optional 缺了照挂。
19
+ */
11
20
  export interface UnitHostNeeds {
12
21
  readonly inject: readonly string[];
13
22
  readonly optional?: readonly string[];
14
23
  }
15
24
 
25
+ /**
26
+ * 一个 opencli 插件的来源三选一(git / 本地 / npm);npm 那支的安装源先看 registryEnv 那个环境变量,没设才用 registryDefault。
27
+ */
16
28
  export type PluginSource =
17
29
  | { readonly kind: 'git'; readonly url: string }
18
30
  | { readonly kind: 'local'; readonly env: string }
@@ -23,12 +35,18 @@ export type PluginSource =
23
35
  readonly registryDefault?: string;
24
36
  };
25
37
 
38
+ /**
39
+ * 模块要的一个 opencli 插件:名字、它得提供的命令,以及装它的来源(没写就是不代装)。
40
+ */
26
41
  export interface UnitPluginNeed {
27
42
  readonly name: string;
28
43
  readonly commands?: readonly string[];
29
44
  readonly installSource?: PluginSource;
30
45
  }
31
46
 
47
+ /**
48
+ * 模块要的一个共用库:名字、期望范围与它为什么要这个版本(purpose 会进申报文案)。
49
+ */
32
50
  export interface UnitLibNeed {
33
51
  readonly name: string;
34
52
  readonly version?: string | undefined;
@@ -37,17 +55,26 @@ export interface UnitLibNeed {
37
55
  readonly purpose: string;
38
56
  }
39
57
 
58
+ /**
59
+ * 模块的整份环境需要:一条自检命令,加上插件与共用库两张表。
60
+ */
40
61
  export interface UnitEnvNeeds {
41
62
  readonly command?: string;
42
63
  readonly plugins?: readonly UnitPluginNeed[];
43
64
  readonly libs?: readonly UnitLibNeed[];
44
65
  }
45
66
 
67
+ /**
68
+ * 已装插件与源上最新版那一对,用来判该不该提示升级;latest 缺省表示还没查到。
69
+ */
46
70
  export interface PluginRelease {
47
71
  readonly installed: string;
48
72
  readonly latest?: string;
49
73
  }
50
74
 
75
+ /**
76
+ * 装配期用的模块清单:单元 id、卡片、工具与宿主 / 环境需要,比声明少一层协议包装。
77
+ */
51
78
  export interface UnitManifest {
52
79
  readonly id: string;
53
80
  readonly card: UnitCard | null;
@@ -56,6 +83,11 @@ export interface UnitManifest {
56
83
  readonly env?: UnitEnvNeeds;
57
84
  }
58
85
 
86
+ /**
87
+ * 判这个模块要不要往外装东西。
88
+ * @param manifest 模块清单。
89
+ * @returns 插件与共用库两张表都空时给 false。
90
+ */
59
91
  export function hasEnvSupply(manifest: UnitManifest): boolean {
60
92
  return (manifest.env?.plugins ?? []).length > 0 || (manifest.env?.libs ?? []).length > 0;
61
93
  }
@@ -1,5 +1,8 @@
1
1
  import { API_PREFIX } from '../config/constants.js';
2
2
 
3
+ /**
4
+ * 宿主读口 / 写口回的错误:code 给判据分档,message 给人看,helpUrl 有就去点。
5
+ */
3
6
  export interface ApiError {
4
7
  readonly code: string;
5
8
  readonly message: string;
@@ -8,14 +11,30 @@ export interface ApiError {
8
11
  readonly helpUrl?: string;
9
12
  }
10
13
 
14
+ /**
15
+ * 读口的统一响应:成了把载荷摊平在顶层,没成只有一条 ApiError。
16
+ */
11
17
  export type ApiResponse<T> = ({ readonly ok: true } & T) | { readonly ok: false; readonly error: ApiError };
12
18
 
19
+ /**
20
+ * GET 的查询参数:值为 undefined 的键直接丢掉,不进 URL。
21
+ */
13
22
  export type Query = Record<string, string | number | boolean | undefined>;
14
23
 
24
+ /**
25
+ * GET 的可选项;目前只有中止信号。
26
+ */
15
27
  export interface ApiGetOptions {
16
28
  readonly signal?: AbortSignal | undefined;
17
29
  }
18
30
 
31
+ /**
32
+ * 打一条宿主读口。
33
+ * @param route 路由,相对 API_PREFIX,不带前导斜杠。
34
+ * @param query 查询参数,undefined 的值不序列化。
35
+ * @param options 中止信号等。
36
+ * @returns HTTP 不是 2xx 时给 ok: false(code 为 http);响应体不是 JSON 会当场抛,调用点要接住。
37
+ */
19
38
  export async function apiGet<T>(
20
39
  route: string,
21
40
  query: Query = {},
@@ -44,6 +63,12 @@ export async function apiGet<T>(
44
63
  return (await response.json()) as ApiResponse<T>;
45
64
  }
46
65
 
66
+ /**
67
+ * 打一条宿主写口。
68
+ * @param route 路由,相对 API_PREFIX,不带前导斜杠。
69
+ * @param body 请求体,按 JSON 序列化。
70
+ * @returns HTTP 不是 2xx 时给 ok: false(code 为 http);响应体不是 JSON 会当场抛。
71
+ */
47
72
  export async function apiPost<T>(route: string, body: Record<string, unknown>): Promise<ApiResponse<T>> {
48
73
  const response = await fetch(`${API_PREFIX}/${route}`, {
49
74
  method: 'POST',
@@ -1,5 +1,8 @@
1
1
  import type { PluginRelease } from '../api/unit.js';
2
2
 
3
+ /**
4
+ * 供给动作的那几档(装 / 升 / 卸,模块、共用依赖与插件各一套);写口按它分派。
5
+ */
3
6
  export type ProvisionAction =
4
7
  | 'install'
5
8
  | 'update'
@@ -13,6 +16,9 @@ export type ProvisionAction =
13
16
  | 'uninstall-plugin'
14
17
  | 'update-workbench';
15
18
 
19
+ /**
20
+ * 宿主报的一个 opencli 插件此刻的状态:装没装、缺哪些命令、该不该升,以及怎么升。
21
+ */
16
22
  export interface PluginStatePayload {
17
23
  readonly name: string;
18
24
  readonly status: 'ok' | 'outdated' | 'missing';
@@ -22,6 +28,9 @@ export interface PluginStatePayload {
22
28
  readonly release?: PluginRelease;
23
29
  }
24
30
 
31
+ /**
32
+ * 宿主对「这个单元现在能不能用」的那份读数:状态、缺的命令与插件、修复动作和供给细节。
33
+ */
25
34
  export interface CapabilityPayload {
26
35
  readonly unit: string;
27
36
  readonly status: 'ok' | 'failed' | 'missing-commands' | 'missing-plugin' | 'unchecked';
@@ -37,6 +46,9 @@ export interface CapabilityPayload {
37
46
  readonly libs?: readonly LibStatePayload[];
38
47
  }
39
48
 
49
+ /**
50
+ * 宿主报的一个共用库此刻的状态(装没装、版本对不对)与它为什么要这个版本。
51
+ */
40
52
  export interface LibStatePayload {
41
53
  readonly name: string;
42
54
  readonly purpose: string;