@dsh-workbench/shared 0.3.3 → 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 (193) 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 +19 -1
  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 +15 -1
  26. package/dist/components/ApprovalDetail.js +13 -1
  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 +45 -0
  82. package/dist/config/constants.js +45 -0
  83. package/dist/config/storage.d.ts +4 -0
  84. package/dist/config/storage.js +4 -0
  85. package/dist/host/browser-read.d.ts +1 -0
  86. package/dist/host/browser-read.js +1 -0
  87. package/dist/host/http.d.ts +3 -0
  88. package/dist/host/http.js +10 -1
  89. package/dist/host/tool-approval.d.ts +5 -0
  90. package/dist/host/tool-approval.js +11 -5
  91. package/dist/host/unit-face.d.ts +5 -1
  92. package/dist/host.d.ts +1 -0
  93. package/dist/host.js +1 -0
  94. package/dist/i18n/en.d.ts +1 -0
  95. package/dist/i18n/en.js +22 -8
  96. package/dist/i18n/index.d.ts +25 -9
  97. package/dist/i18n/index.js +1 -0
  98. package/dist/i18n/merge.d.ts +5 -0
  99. package/dist/i18n/merge.js +2 -0
  100. package/dist/i18n/zh.d.ts +23 -8
  101. package/dist/i18n/zh.js +22 -8
  102. package/dist/index.d.ts +7 -2
  103. package/dist/index.js +7 -2
  104. package/dist/lib/blocker-action.d.ts +22 -0
  105. package/dist/lib/blocker-action.js +13 -0
  106. package/dist/lib/board-fact.d.ts +27 -0
  107. package/dist/lib/board-fact.js +18 -0
  108. package/dist/lib/busy.d.ts +10 -0
  109. package/dist/lib/busy.js +7 -0
  110. package/dist/lib/diy-debug.d.ts +42 -2
  111. package/dist/lib/diy-debug.js +19 -1
  112. package/dist/lib/diy-record.d.ts +98 -0
  113. package/dist/lib/diy-record.js +96 -0
  114. package/dist/lib/form-problem.d.ts +10 -0
  115. package/dist/lib/form-problem.js +10 -0
  116. package/dist/lib/panel-state.d.ts +60 -0
  117. package/dist/lib/panel-state.js +30 -0
  118. package/dist/lib/react.d.ts +39 -0
  119. package/dist/lib/react.js +36 -0
  120. package/dist/lib/text.d.ts +22 -0
  121. package/dist/lib/text.js +22 -0
  122. package/dist/lib/ui.d.ts +50 -0
  123. package/dist/lib/ui.js +41 -0
  124. package/package.json +1 -1
  125. package/src/api/blocker-table.ts +13 -0
  126. package/src/api/blocker.ts +6 -0
  127. package/src/api/context-ref.ts +31 -0
  128. package/src/api/dsh-client.ts +99 -0
  129. package/src/api/dsh.ts +56 -1
  130. package/src/api/facts.ts +42 -0
  131. package/src/api/host-write.ts +20 -1
  132. package/src/api/long-task.ts +36 -0
  133. package/src/api/unit-board.ts +25 -0
  134. package/src/api/unit-declaration.ts +36 -0
  135. package/src/api/unit.ts +32 -0
  136. package/src/api/workbench-client.ts +25 -0
  137. package/src/api/workbench-contract.ts +12 -0
  138. package/src/components/Activity.tsx +33 -0
  139. package/src/components/ApprovalDetail.tsx +17 -1
  140. package/src/components/ApprovalPlan.tsx +14 -0
  141. package/src/components/Card.tsx +13 -0
  142. package/src/components/CardAnchor.tsx +14 -0
  143. package/src/components/CardBoundary.tsx +20 -0
  144. package/src/components/CardHead.tsx +13 -0
  145. package/src/components/ChoiceGroup.tsx +24 -0
  146. package/src/components/Field.tsx +10 -0
  147. package/src/components/Form.css +6 -0
  148. package/src/components/Form.test.tsx +475 -0
  149. package/src/components/Form.tsx +78 -0
  150. package/src/components/FormActions.tsx +14 -0
  151. package/src/components/FormNodes.tsx +193 -0
  152. package/src/components/FormRows.tsx +20 -0
  153. package/src/components/FormTypes.ts +65 -0
  154. package/src/components/FormWiring.test.tsx +88 -0
  155. package/src/components/Link.tsx +12 -0
  156. package/src/components/LongTaskRow.tsx +19 -0
  157. package/src/components/Note.tsx +11 -0
  158. package/src/components/ResourceStates.tsx +25 -0
  159. package/src/components/Rows.tsx +17 -0
  160. package/src/components/SaveBar.tsx +12 -0
  161. package/src/components/SelectField.tsx +26 -0
  162. package/src/components/SelectFieldNodes.tsx +24 -0
  163. package/src/components/SettingsSection.tsx +11 -0
  164. package/src/components/Spinner.tsx +9 -0
  165. package/src/components/Stack.tsx +8 -0
  166. package/src/components/StateBlock.tsx +13 -0
  167. package/src/components/TextField.tsx +8 -0
  168. package/src/components/board-module.ts +30 -0
  169. package/src/components/icons.tsx +38 -0
  170. package/src/config/constants.ts +48 -0
  171. package/src/config/storage.ts +4 -0
  172. package/src/host/browser-read.ts +1 -0
  173. package/src/host/http.ts +15 -1
  174. package/src/host/tool-approval.ts +18 -4
  175. package/src/host/unit-face.ts +6 -1
  176. package/src/host.ts +1 -0
  177. package/src/i18n/en.ts +22 -8
  178. package/src/i18n/index.ts +3 -0
  179. package/src/i18n/merge.ts +5 -0
  180. package/src/i18n/zh.ts +23 -8
  181. package/src/index.ts +7 -2
  182. package/src/lib/blocker-action.ts +22 -0
  183. package/src/lib/board-fact.ts +27 -0
  184. package/src/lib/busy.ts +10 -0
  185. package/src/lib/diy-debug.test.ts +2 -1
  186. package/src/lib/diy-debug.ts +44 -3
  187. package/src/lib/diy-record.test.ts +171 -0
  188. package/src/lib/diy-record.ts +185 -0
  189. package/src/lib/form-problem.ts +10 -0
  190. package/src/lib/panel-state.ts +60 -0
  191. package/src/lib/react.ts +39 -0
  192. package/src/lib/text.ts +22 -0
  193. package/src/lib/ui.ts +50 -0
@@ -0,0 +1,185 @@
1
+ /**
2
+ * 录制交接的三个相位:`armed` 等用户在外站页面上动手,`recording` 正在录,`finished` 录完待取。
3
+ */
4
+ export type RecordPhase = 'armed' | 'recording' | 'finished';
5
+
6
+ /**
7
+ * 录制约定的有效期(15 分钟,毫秒):超过它没动静的约定算过期、当场让人。
8
+ */
9
+ export const RECORD_STALE_MS = 15 * 60 * 1000;
10
+
11
+ /**
12
+ * 这次录制要找的那一次写:HTTP 方法 + URL 里必须含的一段(不填就是只认方法)。
13
+ */
14
+ export interface RecordExpect {
15
+ readonly method: string;
16
+ readonly urlContains: string;
17
+ }
18
+
19
+ /**
20
+ * 一份活着的录制约定:相位 + 谁的会话 + 哪个工作区 / 单元 + 页面 URL 与标题 + 要找的那一次写 + 落盘文件 + 三个时间戳。
21
+ */
22
+ export interface RecordState {
23
+ readonly phase: RecordPhase;
24
+ readonly sessionId: string;
25
+ readonly workspace: string;
26
+ readonly unit: string;
27
+ readonly name: string;
28
+ readonly url: string;
29
+ readonly title: string;
30
+ readonly action: string;
31
+ readonly expect: RecordExpect | null;
32
+ readonly captureFile: string;
33
+ readonly startedAt: number;
34
+ readonly t0: number | null;
35
+ readonly t1: number | null;
36
+ readonly at: number;
37
+ }
38
+
39
+ /**
40
+ * 开一次录制要交的那几件:会话、工作区、单元、名字、页面 URL 与标题、动作名、期望、落盘文件,以及此刻时间。
41
+ */
42
+ export interface RecordPact {
43
+ readonly sessionId: string;
44
+ readonly workspace: string;
45
+ readonly unit: string;
46
+ readonly name: string;
47
+ readonly url: string;
48
+ readonly title: string;
49
+ readonly action: string;
50
+ readonly expect: RecordExpect | null;
51
+ readonly captureFile: string;
52
+ readonly now: number;
53
+ }
54
+
55
+ /**
56
+ * 录制被拒的原因码:自己已持有 / 别人持着 / 不是持有者 / 没有约定 / 已在录 / 还没 arm / 不在录 / 还没录完。
57
+ */
58
+ export type RecordRefusal =
59
+ | 'already-held'
60
+ | 'held-by-other'
61
+ | 'not-owner'
62
+ | 'none'
63
+ | 'already-recording'
64
+ | 'not-armed'
65
+ | 'not-recording'
66
+ | 'not-finished';
67
+
68
+ /**
69
+ * 一次录制动作的结果:成功带新状态,失败带拒绝码。
70
+ */
71
+ export type RecordMove =
72
+ | { readonly ok: true; readonly state: RecordState }
73
+ | { readonly ok: false; readonly code: RecordRefusal };
74
+
75
+ /**
76
+ * 录制约定收得下的三个人话动作:看看进度 / 停手 / 结束并取回。
77
+ */
78
+ export type RecordIntent = 'peek' | 'halt' | 'stop';
79
+
80
+ /**
81
+ * 把工具入参收成人话动作。
82
+ * @param raw - 任意入参值。
83
+ * @returns `'peek'` / `'halt'` / `'stop'` 原样;别的值(含 `undefined`、多余空白)给 `null`,由调用方拒。
84
+ */
85
+ export function parseRecordIntent(raw: unknown): RecordIntent | null {
86
+ return raw === 'peek' || raw === 'halt' || raw === 'stop' ? raw : null;
87
+ }
88
+
89
+ /**
90
+ * 录制约定的存取口(外壳持有一份):相位随 `begin → started → finished` 推进,`release` 是收尾时放掉浏览器那一跳。
91
+ */
92
+ export interface RecordStore {
93
+ readonly release?: ((state: RecordState) => Promise<{ readonly tab: boolean; readonly capture: boolean }>) | undefined;
94
+ live(now: number): RecordState | null;
95
+ begin(pact: RecordPact): RecordMove;
96
+ started(sessionId: string, at: number, now: number): RecordMove;
97
+ finished(sessionId: string, at: number, now: number): RecordMove;
98
+ stop(sessionId: string, now: number): RecordMove;
99
+ }
100
+
101
+ function alive(state: RecordState | null, now: number): RecordState | null {
102
+ if (state === null) return null;
103
+ return now - state.at <= RECORD_STALE_MS ? state : null;
104
+ }
105
+
106
+ function ownerOf(state: RecordState | null, sessionId: string, now: number): RecordRefusal | RecordState {
107
+ const held = alive(state, now);
108
+ if (held === null) return 'none';
109
+ return held.sessionId === sessionId ? held : 'not-owner';
110
+ }
111
+
112
+ /**
113
+ * 造一份新的录制约定存储(进程内存,同一时刻只可能有一份有效约定)。
114
+ * @returns 存取口;动作成不成都在返回的 `RecordMove` 里,不抛异常。
115
+ */
116
+ export function createRecordStore(): RecordStore {
117
+ let held: RecordState | null = null;
118
+
119
+ const live = (now: number): RecordState | null => alive(held, now);
120
+
121
+ return {
122
+ live,
123
+
124
+ begin(pact) {
125
+ const current = alive(held, pact.now);
126
+ if (current !== null) {
127
+ return { ok: false, code: current.sessionId === pact.sessionId ? 'already-held' : 'held-by-other' };
128
+ }
129
+ const state: RecordState = {
130
+ phase: 'armed',
131
+ sessionId: pact.sessionId,
132
+ workspace: pact.workspace,
133
+ unit: pact.unit,
134
+ name: pact.name,
135
+ url: pact.url,
136
+ title: pact.title,
137
+ action: pact.action,
138
+ expect: pact.expect,
139
+ captureFile: pact.captureFile,
140
+ startedAt: pact.now,
141
+ t0: null,
142
+ t1: null,
143
+ at: pact.now,
144
+ };
145
+ held = state;
146
+ return { ok: true, state };
147
+ },
148
+
149
+ started(sessionId, at, now) {
150
+ const found = ownerOf(held, sessionId, now);
151
+ if (typeof found === 'string') return { ok: false, code: found };
152
+ if (found.phase === 'recording') return { ok: false, code: 'already-recording' };
153
+ if (found.phase !== 'armed') return { ok: false, code: 'not-armed' };
154
+ const state: RecordState = { ...found, phase: 'recording', t0: at, at: now };
155
+ held = state;
156
+ return { ok: true, state };
157
+ },
158
+
159
+ finished(sessionId, at, now) {
160
+ const found = ownerOf(held, sessionId, now);
161
+ if (typeof found === 'string') return { ok: false, code: found };
162
+ if (found.phase !== 'recording') return { ok: false, code: 'not-recording' };
163
+ const state: RecordState = { ...found, phase: 'finished', t1: at, at: now };
164
+ held = state;
165
+ return { ok: true, state };
166
+ },
167
+
168
+ stop(sessionId, now) {
169
+ const found = ownerOf(held, sessionId, now);
170
+ if (typeof found === 'string') return { ok: false, code: found };
171
+ held = null;
172
+ return { ok: true, state: found };
173
+ },
174
+ };
175
+ }
176
+
177
+ /**
178
+ * 「谁还占着、在录哪个动作」的一句话,用于别人来抢时说清现况。
179
+ * @param state - 当前有效的那份约定。
180
+ * @returns 形如「会话 x(自 HH:mm:ssZ 起在录 y 的「动作」)」的中文串。
181
+ */
182
+ export function recordOwnerText(state: RecordState): string {
183
+ const at = new Date(state.startedAt).toISOString().slice(11, 19);
184
+ return `会话 ${state.sessionId}(自 ${at}Z 起在录 ${state.unit} 的「${state.action}」)`;
185
+ }
@@ -9,10 +9,20 @@ const FOCUSABLE_SELECTOR = [
9
9
  '[tabindex]:not([tabindex="-1"])',
10
10
  ].join(', ');
11
11
 
12
+ /**
13
+ * 找 `root` 里第一个带 `[data-wb-problem]` 标记的元素 —— 表单校验失败时,调用方拿它定位「哪一格错了」。
14
+ * @param root - 要搜的范围(表单容器,或 document)。
15
+ * @returns 第一个出问题的元素;一个标记都没有就给 `null`。
16
+ */
12
17
  export function firstProblemOf(root: ParentNode): Element | null {
13
18
  return root.querySelector(PROBLEM_SELECTOR);
14
19
  }
15
20
 
21
+ /**
22
+ * 把第一个出问题的元素滚进视野、并把焦点交给它里面第一个可聚焦控件(输入框 / 按钮 / 链接等)。
23
+ * @param root - 要搜的范围。
24
+ * @returns 找到并滚过给 `true`;没有问题给 `false`(不滚、不抢焦点);元素不可滚或没有可聚焦控件也不报错,只是少做一步。
25
+ */
16
26
  export function revealFirstProblem(root: ParentNode): boolean {
17
27
  const problem = firstProblemOf(root);
18
28
  if (problem === null) return false;
@@ -1,10 +1,19 @@
1
+ /**
2
+ * 一张面板卡片取数走到哪一步:`idle` 还没取、`loading` 取着、`ready` 拿到了、`error` 失败了。
3
+ */
1
4
  export type PanelCardPhase = 'idle' | 'loading' | 'ready' | 'error';
2
5
 
6
+ /**
7
+ * 一次取数失败:机器码 + 给人看的一句话。
8
+ */
3
9
  export interface PanelCardError {
4
10
  readonly code: string;
5
11
  readonly message: string;
6
12
  }
7
13
 
14
+ /**
15
+ * 一张卡片最近一次取数的读数:哪个单元、哪条路由、什么相位、什么时候、失败原因。
16
+ */
8
17
  export interface PanelCardRead {
9
18
  readonly unit: string;
10
19
  readonly route: string;
@@ -13,6 +22,9 @@ export interface PanelCardRead {
13
22
  readonly error?: PanelCardError;
14
23
  }
15
24
 
25
+ /**
26
+ * 一张卡片崩了的读数:单元、原因、时间,以及同一原因连着崩了几次(窗口见 `PANEL_STATE_CRASH_WINDOW_MS`)。
27
+ */
16
28
  export interface PanelCardCrash {
17
29
  readonly unit: string;
18
30
  readonly reason: string;
@@ -20,6 +32,9 @@ export interface PanelCardCrash {
20
32
  readonly count: number;
21
33
  }
22
34
 
35
+ /**
36
+ * 一个单元此刻的读数:它的卡片、最近一次读成的时间(`okAt`,没成过给 `null`),以及崩了的那一条。
37
+ */
23
38
  export interface PanelUnitReport {
24
39
  readonly unit: string;
25
40
  readonly at: number;
@@ -28,17 +43,26 @@ export interface PanelUnitReport {
28
43
  readonly crash?: PanelCardCrash;
29
44
  }
30
45
 
46
+ /**
47
+ * 一个工作区此刻的全量读数(面板按工作区存):所有单元的读数 + 整块最近动过的时间。
48
+ */
31
49
  export interface PanelWorkspaceReport {
32
50
  readonly workspace: string | null;
33
51
  readonly at: number;
34
52
  readonly units: readonly PanelUnitReport[];
35
53
  }
36
54
 
55
+ /**
56
+ * 客户端往上报的一批卡片读数(`POST` 请求体的形状)。
57
+ */
37
58
  export interface PanelCardsReport {
38
59
  readonly workspace: string;
39
60
  readonly cards: readonly PanelCardRead[];
40
61
  }
41
62
 
63
+ /**
64
+ * 客户端往上报的一次卡片崩溃(`POST` 请求体的形状)。
65
+ */
42
66
  export interface PanelCrashReport {
43
67
  readonly workspace: string;
44
68
  readonly unit: string;
@@ -46,16 +70,34 @@ export interface PanelCrashReport {
46
70
  readonly at: number;
47
71
  }
48
72
 
73
+ /**
74
+ * 面板运行态的留存口(宿主持有一份):收卡片读数与崩溃,按工作区取快照。
75
+ */
49
76
  export interface PanelStateStore {
50
77
  cards(report: PanelCardsReport): void;
51
78
  crash(report: PanelCrashReport): void;
52
79
  snapshot(workspace: string | null): PanelWorkspaceReport;
53
80
  }
54
81
 
82
+ /**
83
+ * 留存的工作区上限(LRU,满 64 个丢最旧的)。
84
+ */
55
85
  export const PANEL_STATE_MAX_UNITS = 64;
86
+ /**
87
+ * 每个单元留存的卡片上限(按路由 LRU,满 12 张丢最旧的)。
88
+ */
56
89
  export const PANEL_STATE_MAX_CARDS = 12;
90
+ /**
91
+ * 一次上报最多读入的卡片条数(32 条之后的丢掉)。
92
+ */
57
93
  export const PANEL_STATE_MAX_REPORTS = 32;
94
+ /**
95
+ * 同一原因的连续崩溃算进同一次计数的窗口(10 分钟,毫秒)。
96
+ */
58
97
  export const PANEL_STATE_CRASH_WINDOW_MS = 600000;
98
+ /**
99
+ * 上报文本字段的截断长度(2000 字符;workspace / reason / message 共用)。
100
+ */
59
101
  export const PANEL_STATE_MAX_TEXT = 2000;
60
102
 
61
103
  const PHASES: readonly PanelCardPhase[] = ['idle', 'loading', 'ready', 'error'];
@@ -71,6 +113,9 @@ interface WorkspaceEntry {
71
113
  units: Map<string, UnitEntry>;
72
114
  }
73
115
 
116
+ /**
117
+ * 上报解析的结果:`ok: true` 带收好的值,`ok: false` 带一句中文原因(直接回给上报方)。
118
+ */
74
119
  export type PanelParse<T> =
75
120
  | { readonly ok: true; readonly value: T }
76
121
  | { readonly ok: false; readonly message: string };
@@ -100,6 +145,12 @@ function errorOf(raw: unknown): PanelCardError | undefined {
100
145
  return { code: code === '' ? 'unknown' : code, message };
101
146
  }
102
147
 
148
+ /**
149
+ * 收一批卡片读数上报。
150
+ * @param payload - `POST` 的请求体。
151
+ * @returns 至少收到一条能认的卡片才 `ok: true`;请求体不是对象、`workspace` 为空、`cards` 不是数组,
152
+ * 或一条都认不出(`unit` / `route` / `phase` / `at` 缺一)时 `ok: false` + 中文原因;认不出的单条静默跳过。
153
+ */
103
154
  export function parseCardsReport(payload: unknown): PanelParse<PanelCardsReport> {
104
155
  const record = asRecord(payload);
105
156
  if (record === undefined) return { ok: false, message: '请求体要是一个对象' };
@@ -123,6 +174,11 @@ export function parseCardsReport(payload: unknown): PanelParse<PanelCardsReport>
123
174
  return { ok: true, value: { workspace, cards } };
124
175
  }
125
176
 
177
+ /**
178
+ * 收一次卡片崩溃上报。
179
+ * @param payload - `POST` 的请求体。
180
+ * @returns `workspace` / `unit` / `reason` 都不能空,缺一给 `ok: false` + 中文原因;`at` 缺省或不合法时取当前时间。
181
+ */
126
182
  export function parseCrashReport(payload: unknown): PanelParse<PanelCrashReport> {
127
183
  const record = asRecord(payload);
128
184
  if (record === undefined) return { ok: false, message: '请求体要是一个对象' };
@@ -135,6 +191,10 @@ export function parseCrashReport(payload: unknown): PanelParse<PanelCrashReport>
135
191
  return { ok: true, value: { workspace, unit, reason, at: atOf(record['at']) ?? Date.now() } };
136
192
  }
137
193
 
194
+ /**
195
+ * 造一份面板运行态的留存(进程内存、按工作区分桶,带上面那几条上限的 LRU)。
196
+ * @returns 留存口:`cards` / `crash` 收上报,`snapshot(workspace)` 取全量;没存过的工作区(含 `null`)给一份空的。
197
+ */
138
198
  export function createPanelStateStore(): PanelStateStore {
139
199
  const workspaces = new Map<string, WorkspaceEntry>();
140
200
 
package/src/lib/react.ts CHANGED
@@ -2,21 +2,60 @@ import type * as ReactTypes from 'react';
2
2
 
3
3
  const react = require('react') as typeof ReactTypes;
4
4
 
5
+ /**
6
+ * React 的 `createElement` 换个短名字再导出 —— 这套代码用 `h(...)` 写元素。
7
+ */
5
8
  export const h = react.createElement;
6
9
 
10
+ /**
11
+ * React 的 `Fragment` 原样再导出 —— 模块从这里拿,就不必自己直接依赖 react。
12
+ */
7
13
  export const Fragment = react.Fragment;
8
14
 
15
+ /**
16
+ * React 的 `Component` 原样再导出 —— 模块从这里拿,就不必自己直接依赖 react。
17
+ */
9
18
  export const Component = react.Component;
10
19
 
20
+ /**
21
+ * React 的 `createContext` 原样再导出 —— 模块从这里拿,就不必自己直接依赖 react。
22
+ */
11
23
  export const createContext = react.createContext;
24
+ /**
25
+ * React 的 `useContext` 原样再导出 —— 模块从这里拿,就不必自己直接依赖 react。
26
+ */
12
27
  export const useContext = react.useContext;
13
28
 
29
+ /**
30
+ * React 的 `useState` 原样再导出 —— 模块从这里拿,就不必自己直接依赖 react。
31
+ */
14
32
  export const useState = react.useState;
33
+ /**
34
+ * React 的 `useEffect` 原样再导出 —— 模块从这里拿,就不必自己直接依赖 react。
35
+ */
15
36
  export const useEffect = react.useEffect;
37
+ /**
38
+ * React 的 `useLayoutEffect` 原样再导出 —— 模块从这里拿,就不必自己直接依赖 react。
39
+ */
16
40
  export const useLayoutEffect = react.useLayoutEffect;
41
+ /**
42
+ * React 的 `useMemo` 原样再导出 —— 模块从这里拿,就不必自己直接依赖 react。
43
+ */
17
44
  export const useMemo = react.useMemo;
45
+ /**
46
+ * React 的 `useCallback` 原样再导出 —— 模块从这里拿,就不必自己直接依赖 react。
47
+ */
18
48
  export const useCallback = react.useCallback;
49
+ /**
50
+ * React 的 `useRef` 原样再导出 —— 模块从这里拿,就不必自己直接依赖 react。
51
+ */
19
52
  export const useRef = react.useRef;
53
+ /**
54
+ * React 的 `useSyncExternalStore` 原样再导出 —— 模块从这里拿,就不必自己直接依赖 react。
55
+ */
20
56
  export const useSyncExternalStore = react.useSyncExternalStore;
21
57
 
58
+ /**
59
+ * 从 react 借来的三个类型(`ReactNode` / `CSSProperties` / `ChangeEvent`)—— 模块从这里拿,就不必自己直接依赖 react。
60
+ */
22
61
  export type { ReactNode, CSSProperties, ChangeEvent } from 'react';
package/src/lib/text.ts CHANGED
@@ -1,7 +1,18 @@
1
+ /**
2
+ * 把一个值裹成 shell 里的单引号字面量(值里的单引号一并转义),保证元字符按字面量传递。
3
+ * @param value - 任意值;不是字符串的先 `String()`。
4
+ * @returns 可安全拼进 `/bin/sh -c` 的一段文本 —— 含空格 / `$()` / `;` / 通配符的值都不会被当成语法。
5
+ */
1
6
  export function quoteShellArg(value: unknown): string {
2
7
  return `'${String(value).replace(/'/g, `'\\''`)}'`;
3
8
  }
4
9
 
10
+ /**
11
+ * 取一段文本的末尾若干行(如命令输出),顺带去掉首尾空白。
12
+ * @param text - 原文;`null` / `undefined` 当空串。
13
+ * @param lines - 最多留几行,默认 6;0 给全篇、负数从开头砍掉那么多行(都是 `slice` 语义),调用方传正数。
14
+ * @returns 末尾若干行、去首尾空行后的文本;原文行数不够就全给,空输入给空串。
15
+ */
5
16
  export function tailLines(text: string | null | undefined, lines = 6): string {
6
17
  const all = String(text ?? '')
7
18
  .trim()
@@ -24,6 +35,11 @@ function candidateStarts(raw: string): number[] {
24
35
  return starts;
25
36
  }
26
37
 
38
+ /**
39
+ * 宽松解析一段前后可能夹着人话的 JSON:整段不通就从行首的 `[` / `{` 起、截到最后一个闭合符再试。
40
+ * @param text - 原文;`null` / `undefined` 当空串。
41
+ * @returns 解析出的值(对象 / 数组 / 标量都原样);空输入或怎么都解析不出给 `undefined`,调用方据此报「输出认不出」。
42
+ */
27
43
  export function parseJsonLoose(text: string | null | undefined): unknown {
28
44
  const raw = String(text ?? '').trim();
29
45
  if (!raw) return undefined;
@@ -48,6 +64,12 @@ const MARKDOWN_IMAGE = /!\[[^\]]*\]\([^)]*\)/g;
48
64
  const MARKDOWN_LINK = /\[([^\]]*)\]\([^)]*\)/g;
49
65
  const MARKDOWN_MARKS = /[*`_>#]/g;
50
66
 
67
+ /**
68
+ * 把一段 Markdown 压成一行纯文本摘要:图片剥掉、链接只留可见文字、标记符号压成空格,超长按字符截断。
69
+ * @param text - 原文;`null` / `undefined` 当空串。
70
+ * @param limit - 摘要最多多少字符,默认 80。
71
+ * @returns 单行摘要;没超限原样给,超了截到 `limit` 并补省略号(总长 `limit + 1`;第 `limit` 个字符是空白时会被 `trimEnd` 去掉、略短),空输入给空串。
72
+ */
51
73
  export function excerpt(text: string | null | undefined, limit = 80): string {
52
74
  const flat = String(text ?? '')
53
75
  .replace(MARKDOWN_IMAGE, ' ')
package/src/lib/ui.ts CHANGED
@@ -1,3 +1,8 @@
1
+ /**
2
+ * 拼类名:只留真值、用空格连接(`false` / `null` / `undefined` / 空串都丢掉)。
3
+ * @param parts - 若干类名或条件值。
4
+ * @returns 拼好的类字符串;一个真值都没有给空串。
5
+ */
1
6
  export function cx(...parts: Array<string | false | null | undefined>): string {
2
7
  return parts.filter(Boolean).join(' ');
3
8
  }
@@ -8,15 +13,30 @@ const DAY = 24 * HOUR;
8
13
  const MONTH = 30 * DAY;
9
14
  const YEAR = 365 * DAY;
10
15
 
16
+ /**
17
+ * 时间文案取词:i18n 的 `(key, params) => 文案`;相对时间那几件都从它拿词。
18
+ */
11
19
  export type TimeTranslate = (key: string, params?: Record<string, unknown>) => string;
12
20
 
21
+ /**
22
+ * 相对时间的档位名,对应词表里的 `time.short.*`。
23
+ */
13
24
  export type RelativeTimeUnit = 'now' | 'minutes' | 'hours' | 'days' | 'months' | 'years';
14
25
 
26
+ /**
27
+ * 相对时间的一档:`unit` 是档位,`n` 是这一档的数量(单位随档位,见 `relativeTimeBucket`)。
28
+ */
15
29
  export interface RelativeTimeBucket {
16
30
  readonly unit: RelativeTimeUnit;
17
31
  readonly n: number;
18
32
  }
19
33
 
34
+ /**
35
+ * 把时间戳按 dsh 的分档切成「刚刚 / N 分钟 / N 小时 / N 天 / N 月 / N 年」—— 30 天进月、365 天进年。
36
+ * @param epochMs - 要换算的时间戳(毫秒)。
37
+ * @param now - 基准时间,默认 `Date.now()`。
38
+ * @returns 档位与数量;时间戳或基准不是有限数给 `null`,未来时间钳到「刚刚」(`now` / 0)。
39
+ */
20
40
  export function relativeTimeBucket(epochMs: number, now: number = Date.now()): RelativeTimeBucket | null {
21
41
  if (!Number.isFinite(epochMs) || !Number.isFinite(now)) return null;
22
42
  const diff = Math.max(0, now - epochMs);
@@ -32,11 +52,25 @@ function bucketLabel(bucket: RelativeTimeBucket, t: TimeTranslate): string {
32
52
  return bucket.unit === 'now' ? t('time.now') : t(`time.short.${bucket.unit}`, { n: bucket.n });
33
53
  }
34
54
 
55
+ /**
56
+ * 行内用的紧凑相对时间(「3 分钟」这种,不带「前」)。
57
+ * @param epochMs - 要换算的时间戳(毫秒)。
58
+ * @param t - 词表取词函数。
59
+ * @param now - 基准时间,默认 `Date.now()`。
60
+ * @returns 紧凑文案;时间戳非法给空串。
61
+ */
35
62
  export function relativeTimeCompact(epochMs: number, t: TimeTranslate, now?: number): string {
36
63
  const bucket = relativeTimeBucket(epochMs, now);
37
64
  return bucket === null ? '' : bucketLabel(bucket, t);
38
65
  }
39
66
 
67
+ /**
68
+ * 悬停卡里用的相对时间(「3 分钟前」);「刚刚」那一档不加「前」。
69
+ * @param epochMs - 要换算的时间戳(毫秒)。
70
+ * @param t - 词表取词函数。
71
+ * @param now - 基准时间,默认 `Date.now()`。
72
+ * @returns 带「前」的文案;时间戳非法给空串。
73
+ */
40
74
  export function relativeTimeAgo(epochMs: number, t: TimeTranslate, now?: number): string {
41
75
  const bucket = relativeTimeBucket(epochMs, now);
42
76
  if (bucket === null) return '';
@@ -44,6 +78,12 @@ export function relativeTimeAgo(epochMs: number, t: TimeTranslate, now?: number)
44
78
  return bucket.unit === 'now' ? label : t('time.ago', { t: label });
45
79
  }
46
80
 
81
+ /**
82
+ * 会话行那一版相对时间:一小时内按分钟、一天内按小时、七天内按天,再久直接给绝对时间(到秒)。
83
+ * @param epochMs - 要换算的时间戳(毫秒)。
84
+ * @param t - 词表取词函数。
85
+ * @returns 文案;时间戳不是有限数给空串,未来时间落「刚刚」,七天及以上给本地化的年月日时分秒。
86
+ */
47
87
  export function relativeTime(epochMs: number, t: TimeTranslate): string {
48
88
  if (!Number.isFinite(epochMs)) return '';
49
89
  const diff = Date.now() - epochMs;
@@ -61,6 +101,11 @@ export function relativeTime(epochMs: number, t: TimeTranslate): string {
61
101
  });
62
102
  }
63
103
 
104
+ /**
105
+ * 把 `/Users/<用户名>/…` 缩成 `~/…`,免得会话行里全是一样的长前缀。
106
+ * @param path - 任意值。
107
+ * @returns 缩好的路径;不在用户主目录下原样返回,不是字符串给空串。
108
+ */
64
109
  export function shortenPath(path: unknown): string {
65
110
  if (typeof path !== 'string') return '';
66
111
  const hit = path.match(/^\/Users\/[^/]+/);
@@ -69,6 +114,11 @@ export function shortenPath(path: unknown): string {
69
114
 
70
115
  const LOCAL_TIMESTAMP = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2}):(\d{2})$/;
71
116
 
117
+ /**
118
+ * 解析不带时区的本地时间戳(`YYYY-MM-DD HH:mm:ss`,也接受 `T` 分隔),按本机时区换算。
119
+ * @param text - 原文;`null` / `undefined` 当空串;首尾空白容忍。
120
+ * @returns 毫秒时间戳;形态不认识给 `NaN`(调用方据此回退到原始字符串,宁可不换算也不显示错时间)。
121
+ */
72
122
  export function parseLocalTimestamp(text: string | null | undefined): number {
73
123
  const raw = String(text ?? '').trim();
74
124
  if (!LOCAL_TIMESTAMP.test(raw)) return Number.NaN;