@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,80 @@
1
+ /**
2
+ * 调试约定的两个相位:`debugging` 正占着用户的屏,`paused` 已暂停但仍持着约定。
3
+ */
4
+ export type DebugPhase = 'debugging' | 'paused';
5
+ /**
6
+ * 调试约定收得下的三个人话动作:暂停 / 继续 / 结束。
7
+ */
8
+ export type DebugIntent = 'pause' | 'resume' | 'stop';
9
+ /**
10
+ * 把工具入参收成人话动作。
11
+ * @param value - 任意入参值。
12
+ * @returns `'pause'` / `'resume'` / `'stop'` 原样;别的值(含 `undefined`、多余空白、大小写不同)给 `null`,由调用方拒。
13
+ */
14
+ export declare function parseDebugIntent(value: unknown): DebugIntent | null;
15
+ /**
16
+ * 调试约定的有效期(4 小时,毫秒):超过它没动静的约定算过期、当场让人。
17
+ */
18
+ export declare const DEBUG_STALE_MS: number;
19
+ /**
20
+ * 一份活着的调试约定:相位 + 谁的会话 + 哪个工作区 / 板 / 单元 + 起于何时(`at` 是最后一次动作)。
21
+ */
22
+ export interface DebugState {
23
+ readonly phase: DebugPhase;
24
+ readonly sessionId: string;
25
+ readonly workspacePath: string;
26
+ readonly title: string;
27
+ readonly board: string;
28
+ readonly unit: string;
29
+ readonly name: string;
30
+ readonly startedAt: number;
31
+ readonly at: number;
32
+ }
33
+ /**
34
+ * 建立约定要交的那几件:会话、工作区、板、单元、名字,以及此刻的时间戳。
35
+ */
36
+ export interface DebugPact {
37
+ readonly sessionId: string;
38
+ readonly workspacePath: string;
39
+ readonly title: string;
40
+ readonly board: string;
41
+ readonly unit: string;
42
+ readonly name: string;
43
+ readonly now: number;
44
+ }
45
+ /**
46
+ * 约定被拒的原因码:自己已持有 / 别人持着 / 不是持有者 / 没有约定 / 已暂停 / 已在调试。
47
+ */
48
+ export type DebugRefusal = 'already-held' | 'held-by-other' | 'not-owner' | 'none' | 'already-paused' | 'already-debugging';
49
+ /**
50
+ * 一次约定动作的结果:成功带新状态,失败带拒绝码。
51
+ */
52
+ export type DebugMove = {
53
+ readonly ok: true;
54
+ readonly state: DebugState;
55
+ } | {
56
+ readonly ok: false;
57
+ readonly code: DebugRefusal;
58
+ };
59
+ /**
60
+ * 调试约定的存取口(外壳持有一份):`live` 取当前有效的约定,`begin` / `pause` / `resume` / `stop` 推相位。
61
+ */
62
+ export interface DebugStore {
63
+ live(now: number): DebugState | null;
64
+ begin(pact: DebugPact): DebugMove;
65
+ pause(sessionId: string, now: number): DebugMove;
66
+ resume(sessionId: string, now: number): DebugMove;
67
+ stop(sessionId: string, now: number): DebugMove;
68
+ settle?(): Promise<void>;
69
+ }
70
+ /**
71
+ * 造一份新的调试约定存储(进程内存,同一时刻只可能有一份有效约定)。
72
+ * @returns 存取口;动作成不成都在返回的 `DebugMove` 里,不抛异常。
73
+ */
74
+ export declare function createDebugStore(): DebugStore;
75
+ /**
76
+ * 「谁还占着」的一句话,用于别人来抢时说清现况。
77
+ * @param state - 当前有效的那份约定。
78
+ * @returns 形如「会话 x(自 HH:mm:ssZ 起在调试 y)」的中文串。
79
+ */
80
+ export declare function debugOwnerText(state: DebugState): string;
@@ -0,0 +1,88 @@
1
+ /**
2
+ * 把工具入参收成人话动作。
3
+ * @param value - 任意入参值。
4
+ * @returns `'pause'` / `'resume'` / `'stop'` 原样;别的值(含 `undefined`、多余空白、大小写不同)给 `null`,由调用方拒。
5
+ */
6
+ export function parseDebugIntent(value) {
7
+ return value === 'pause' || value === 'resume' || value === 'stop' ? value : null;
8
+ }
9
+ /**
10
+ * 调试约定的有效期(4 小时,毫秒):超过它没动静的约定算过期、当场让人。
11
+ */
12
+ export const DEBUG_STALE_MS = 4 * 60 * 60 * 1000;
13
+ function alive(state, now) {
14
+ if (state === null)
15
+ return null;
16
+ return now - state.at <= DEBUG_STALE_MS ? state : null;
17
+ }
18
+ function ownerOf(state, sessionId, now) {
19
+ const held = alive(state, now);
20
+ if (held === null)
21
+ return 'none';
22
+ return held.sessionId === sessionId ? held : 'not-owner';
23
+ }
24
+ /**
25
+ * 造一份新的调试约定存储(进程内存,同一时刻只可能有一份有效约定)。
26
+ * @returns 存取口;动作成不成都在返回的 `DebugMove` 里,不抛异常。
27
+ */
28
+ export function createDebugStore() {
29
+ let held = null;
30
+ const live = (now) => alive(held, now);
31
+ return {
32
+ live,
33
+ begin(pact) {
34
+ const current = alive(held, pact.now);
35
+ if (current !== null) {
36
+ return { ok: false, code: current.sessionId === pact.sessionId ? 'already-held' : 'held-by-other' };
37
+ }
38
+ const state = {
39
+ phase: 'debugging',
40
+ sessionId: pact.sessionId,
41
+ workspacePath: pact.workspacePath,
42
+ title: pact.title,
43
+ board: pact.board,
44
+ unit: pact.unit,
45
+ name: pact.name,
46
+ startedAt: pact.now,
47
+ at: pact.now,
48
+ };
49
+ held = state;
50
+ return { ok: true, state };
51
+ },
52
+ pause(sessionId, now) {
53
+ const found = ownerOf(held, sessionId, now);
54
+ if (typeof found === 'string')
55
+ return { ok: false, code: found };
56
+ if (found.phase === 'paused')
57
+ return { ok: false, code: 'already-paused' };
58
+ const state = { ...found, phase: 'paused', at: now };
59
+ held = state;
60
+ return { ok: true, state };
61
+ },
62
+ resume(sessionId, now) {
63
+ const found = ownerOf(held, sessionId, now);
64
+ if (typeof found === 'string')
65
+ return { ok: false, code: found };
66
+ if (found.phase === 'debugging')
67
+ return { ok: false, code: 'already-debugging' };
68
+ const state = { ...found, phase: 'debugging', at: now };
69
+ held = state;
70
+ return { ok: true, state };
71
+ },
72
+ stop(sessionId, now) {
73
+ const found = ownerOf(held, sessionId, now);
74
+ if (typeof found === 'string')
75
+ return { ok: false, code: found };
76
+ held = null;
77
+ return { ok: true, state: found };
78
+ },
79
+ };
80
+ }
81
+ /**
82
+ * 「谁还占着」的一句话,用于别人来抢时说清现况。
83
+ * @param state - 当前有效的那份约定。
84
+ * @returns 形如「会话 x(自 HH:mm:ssZ 起在调试 y)」的中文串。
85
+ */
86
+ export function debugOwnerText(state) {
87
+ return `会话 ${state.sessionId}(自 ${new Date(state.startedAt).toISOString().slice(11, 19)}Z 起在调试 ${state.unit})`;
88
+ }
@@ -0,0 +1,135 @@
1
+ /**
2
+ * 录制交接的三个相位:`armed` 等用户在外站页面上动手,`recording` 正在录,`finished` 录完待取。
3
+ */
4
+ export type RecordPhase = 'armed' | 'recording' | 'finished';
5
+ /**
6
+ * 录制约定的有效期(15 分钟,毫秒):超过它没动静的约定算过期、当场让人。
7
+ */
8
+ export declare const RECORD_STALE_MS: number;
9
+ /**
10
+ * 这次录制要找的那一次写:HTTP 方法 + URL 里必须含的一段(不填就是只认方法)。
11
+ */
12
+ export interface RecordExpect {
13
+ readonly method: string;
14
+ readonly urlContains: string;
15
+ }
16
+ /**
17
+ * 一次「截图」在宿主台账里的那一行:页面上的计数、落图那一刻、落盘路径;没落下来时带 `error` 原文(落成的那条没有它)。
18
+ */
19
+ export interface RecordShot {
20
+ readonly n: number;
21
+ readonly at: number;
22
+ readonly path: string;
23
+ readonly error?: string;
24
+ }
25
+ /**
26
+ * 某个页面计数已经试过几次:初拍算一次,失败再试一次;计数一换就从头数。
27
+ */
28
+ export interface RecordShotTries {
29
+ readonly n: number;
30
+ readonly tries: number;
31
+ }
32
+ /**
33
+ * 一张图最多试几次(初拍 + 一次重试):失败如实报过就停,不每拍重试到底。
34
+ */
35
+ export declare const RECORD_SHOT_TRIES = 2;
36
+ /**
37
+ * 一份活着的录制约定:相位 + 谁的会话 + 哪个工作区 / 单元 + 页面 URL 与标题 + 要找的那一次写 +
38
+ * 落盘文件 + 三个时间戳,以及两份台账:按计数落图的 `shots`(`shotsSeen` 只记落成的那个计数)
39
+ * 与门铃的 `beats` / `beatMs`(命中几次、最近一次等了多久)。
40
+ */
41
+ export interface RecordState {
42
+ readonly phase: RecordPhase;
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 startedAt: number;
53
+ readonly t0: number | null;
54
+ readonly t1: number | null;
55
+ readonly shots: readonly RecordShot[];
56
+ readonly shotsSeen: number;
57
+ readonly shotTries: RecordShotTries | null;
58
+ readonly beats: number;
59
+ readonly beatMs: number;
60
+ readonly at: number;
61
+ }
62
+ /**
63
+ * 开一次录制要交的那几件:会话、工作区、单元、名字、页面 URL 与标题、动作名、期望、落盘文件,以及此刻时间。
64
+ */
65
+ export interface RecordPact {
66
+ readonly sessionId: string;
67
+ readonly workspace: string;
68
+ readonly unit: string;
69
+ readonly name: string;
70
+ readonly url: string;
71
+ readonly title: string;
72
+ readonly action: string;
73
+ readonly expect: RecordExpect | null;
74
+ readonly captureFile: string;
75
+ readonly now: number;
76
+ }
77
+ /**
78
+ * 录制被拒的原因码:自己已持有 / 别人持着 / 不是持有者 / 没有约定 / 已在录 / 还没 arm / 不在录 / 还没录完。
79
+ */
80
+ export type RecordRefusal = 'already-held' | 'held-by-other' | 'not-owner' | 'none' | 'already-recording' | 'not-armed' | 'not-recording' | 'not-finished';
81
+ /**
82
+ * 一次录制动作的结果:成功带新状态,失败带拒绝码。
83
+ */
84
+ export type RecordMove = {
85
+ readonly ok: true;
86
+ readonly state: RecordState;
87
+ } | {
88
+ readonly ok: false;
89
+ readonly code: RecordRefusal;
90
+ };
91
+ /**
92
+ * 录制约定收得下的三个人话动作:看看进度 / 停手 / 结束并取回。
93
+ */
94
+ export type RecordIntent = 'peek' | 'halt' | 'stop';
95
+ /**
96
+ * 把工具入参收成人话动作。
97
+ * @param raw - 任意入参值。
98
+ * @returns `'peek'` / `'halt'` / `'stop'` 原样;别的值(含 `undefined`、多余空白)给 `null`,由调用方拒。
99
+ */
100
+ export declare function parseRecordIntent(raw: unknown): RecordIntent | null;
101
+ /**
102
+ * 录制约定的存取口(外壳持有一份):相位随 `begin → started → finished` 推进,`release` 是收尾时放掉浏览器那一跳。
103
+ */
104
+ export interface RecordStore {
105
+ readonly release?: ((state: RecordState) => Promise<{
106
+ readonly tab: boolean;
107
+ readonly capture: boolean;
108
+ }>) | undefined;
109
+ live(now: number): RecordState | null;
110
+ begin(pact: RecordPact): RecordMove;
111
+ started(sessionId: string, at: number, now: number): RecordMove;
112
+ finished(sessionId: string, at: number, now: number): RecordMove;
113
+ shotTaken(sessionId: string, shot: RecordShot, now: number): RecordMove;
114
+ shotFailed(sessionId: string, shot: RecordShot, now: number): RecordMove;
115
+ beat(sessionId: string, ms: number, now: number): RecordMove;
116
+ stop(sessionId: string, now: number): RecordMove;
117
+ }
118
+ /**
119
+ * 这一拍该不该落图:页面计数涨过 `shotsSeen` 才算;同一个计数已经试满就先放下,等他自己再按。
120
+ * @param state - 当前有效的那份约定。
121
+ * @param pageShots - 页面浮条上那个「已拍」计数。
122
+ * @returns 该落的那个计数;不该落给 `null`(没涨 / 试满了 / 不是个正经数)。
123
+ */
124
+ export declare function shotDueOf(state: RecordState, pageShots: number): number | null;
125
+ /**
126
+ * 造一份新的录制约定存储(进程内存,同一时刻只可能有一份有效约定)。
127
+ * @returns 存取口;动作成不成都在返回的 `RecordMove` 里,不抛异常。
128
+ */
129
+ export declare function createRecordStore(): RecordStore;
130
+ /**
131
+ * 「谁还占着、在录哪个动作」的一句话,用于别人来抢时说清现况。
132
+ * @param state - 当前有效的那份约定。
133
+ * @returns 形如「会话 x(自 HH:mm:ssZ 起在录 y 的「动作」)」的中文串。
134
+ */
135
+ export declare function recordOwnerText(state: RecordState): string;
@@ -0,0 +1,162 @@
1
+ /**
2
+ * 录制约定的有效期(15 分钟,毫秒):超过它没动静的约定算过期、当场让人。
3
+ */
4
+ export const RECORD_STALE_MS = 15 * 60 * 1000;
5
+ /**
6
+ * 一张图最多试几次(初拍 + 一次重试):失败如实报过就停,不每拍重试到底。
7
+ */
8
+ export const RECORD_SHOT_TRIES = 2;
9
+ /**
10
+ * 把工具入参收成人话动作。
11
+ * @param raw - 任意入参值。
12
+ * @returns `'peek'` / `'halt'` / `'stop'` 原样;别的值(含 `undefined`、多余空白)给 `null`,由调用方拒。
13
+ */
14
+ export function parseRecordIntent(raw) {
15
+ return raw === 'peek' || raw === 'halt' || raw === 'stop' ? raw : null;
16
+ }
17
+ /**
18
+ * 这一拍该不该落图:页面计数涨过 `shotsSeen` 才算;同一个计数已经试满就先放下,等他自己再按。
19
+ * @param state - 当前有效的那份约定。
20
+ * @param pageShots - 页面浮条上那个「已拍」计数。
21
+ * @returns 该落的那个计数;不该落给 `null`(没涨 / 试满了 / 不是个正经数)。
22
+ */
23
+ export function shotDueOf(state, pageShots) {
24
+ if (!Number.isFinite(pageShots))
25
+ return null;
26
+ const n = Math.floor(pageShots);
27
+ if (n <= state.shotsSeen)
28
+ return null;
29
+ if (state.shotTries !== null && state.shotTries.n === n && state.shotTries.tries >= RECORD_SHOT_TRIES)
30
+ return null;
31
+ return n;
32
+ }
33
+ function alive(state, now) {
34
+ if (state === null)
35
+ return null;
36
+ return now - state.at <= RECORD_STALE_MS ? state : null;
37
+ }
38
+ function ownerOf(state, sessionId, now) {
39
+ const held = alive(state, now);
40
+ if (held === null)
41
+ return 'none';
42
+ return held.sessionId === sessionId ? held : 'not-owner';
43
+ }
44
+ /**
45
+ * 造一份新的录制约定存储(进程内存,同一时刻只可能有一份有效约定)。
46
+ * @returns 存取口;动作成不成都在返回的 `RecordMove` 里,不抛异常。
47
+ */
48
+ export function createRecordStore() {
49
+ let held = null;
50
+ const live = (now) => alive(held, now);
51
+ const withShot = (found, shot, now, error) => {
52
+ const row = error === undefined ? shot : { n: shot.n, at: shot.at, path: shot.path, error };
53
+ const kept = found.shots.filter((one) => one.n !== shot.n);
54
+ const tries = found.shotTries !== null && found.shotTries.n === shot.n ? found.shotTries.tries : 0;
55
+ return {
56
+ ...found,
57
+ shots: [...kept, row].sort((one, two) => one.n - two.n),
58
+ shotsSeen: error === undefined ? Math.max(found.shotsSeen, shot.n) : found.shotsSeen,
59
+ shotTries: error === undefined ? null : { n: shot.n, tries: tries + 1 },
60
+ at: now,
61
+ };
62
+ };
63
+ return {
64
+ live,
65
+ begin(pact) {
66
+ const current = alive(held, pact.now);
67
+ if (current !== null) {
68
+ return { ok: false, code: current.sessionId === pact.sessionId ? 'already-held' : 'held-by-other' };
69
+ }
70
+ const state = {
71
+ phase: 'armed',
72
+ sessionId: pact.sessionId,
73
+ workspace: pact.workspace,
74
+ unit: pact.unit,
75
+ name: pact.name,
76
+ url: pact.url,
77
+ title: pact.title,
78
+ action: pact.action,
79
+ expect: pact.expect,
80
+ captureFile: pact.captureFile,
81
+ startedAt: pact.now,
82
+ t0: null,
83
+ t1: null,
84
+ shots: [],
85
+ shotsSeen: 0,
86
+ shotTries: null,
87
+ beats: 0,
88
+ beatMs: 0,
89
+ at: pact.now,
90
+ };
91
+ held = state;
92
+ return { ok: true, state };
93
+ },
94
+ started(sessionId, at, now) {
95
+ const found = ownerOf(held, sessionId, now);
96
+ if (typeof found === 'string')
97
+ return { ok: false, code: found };
98
+ if (found.phase === 'recording')
99
+ return { ok: false, code: 'already-recording' };
100
+ if (found.phase !== 'armed')
101
+ return { ok: false, code: 'not-armed' };
102
+ const state = { ...found, phase: 'recording', t0: at, at: now };
103
+ held = state;
104
+ return { ok: true, state };
105
+ },
106
+ finished(sessionId, at, now) {
107
+ const found = ownerOf(held, sessionId, now);
108
+ if (typeof found === 'string')
109
+ return { ok: false, code: found };
110
+ if (found.phase !== 'recording')
111
+ return { ok: false, code: 'not-recording' };
112
+ const state = { ...found, phase: 'finished', t1: at, at: now };
113
+ held = state;
114
+ return { ok: true, state };
115
+ },
116
+ shotTaken(sessionId, shot, now) {
117
+ const found = ownerOf(held, sessionId, now);
118
+ if (typeof found === 'string')
119
+ return { ok: false, code: found };
120
+ const state = withShot(found, shot, now, undefined);
121
+ held = state;
122
+ return { ok: true, state };
123
+ },
124
+ shotFailed(sessionId, shot, now) {
125
+ const found = ownerOf(held, sessionId, now);
126
+ if (typeof found === 'string')
127
+ return { ok: false, code: found };
128
+ const state = withShot(found, shot, now, shot.error === undefined ? '落图失败' : shot.error);
129
+ held = state;
130
+ return { ok: true, state };
131
+ },
132
+ beat(sessionId, ms, now) {
133
+ const found = ownerOf(held, sessionId, now);
134
+ if (typeof found === 'string')
135
+ return { ok: false, code: found };
136
+ const state = {
137
+ ...found,
138
+ beats: found.beats + 1,
139
+ beatMs: Math.max(0, Math.floor(ms)),
140
+ at: now,
141
+ };
142
+ held = state;
143
+ return { ok: true, state };
144
+ },
145
+ stop(sessionId, now) {
146
+ const found = ownerOf(held, sessionId, now);
147
+ if (typeof found === 'string')
148
+ return { ok: false, code: found };
149
+ held = null;
150
+ return { ok: true, state: found };
151
+ },
152
+ };
153
+ }
154
+ /**
155
+ * 「谁还占着、在录哪个动作」的一句话,用于别人来抢时说清现况。
156
+ * @param state - 当前有效的那份约定。
157
+ * @returns 形如「会话 x(自 HH:mm:ssZ 起在录 y 的「动作」)」的中文串。
158
+ */
159
+ export function recordOwnerText(state) {
160
+ const at = new Date(state.startedAt).toISOString().slice(11, 19);
161
+ return `会话 ${state.sessionId}(自 ${at}Z 起在录 ${state.unit} 的「${state.action}」)`;
162
+ }
@@ -0,0 +1,9 @@
1
+ import type { DshHostContext, DshRequest, DshResponse } from '@dsh-workbench/shared';
2
+ import type { FactLog } from './facts';
3
+ export interface FactStream {
4
+ handler(req: DshRequest, res: DshResponse): void;
5
+ closeAll(): void;
6
+ size(): number;
7
+ }
8
+ export declare function createFactStream(log: FactLog): FactStream;
9
+ export declare function registerFactStream(ctx: DshHostContext, log: FactLog): () => void;
@@ -0,0 +1,70 @@
1
+ import { EVENTS_PATH } from '@dsh-workbench/shared';
2
+ const HEARTBEAT_MS = 25_000;
3
+ function frame(payload) {
4
+ return `data: ${JSON.stringify(payload)}\n\n`;
5
+ }
6
+ export function createFactStream(log) {
7
+ const connections = new Set();
8
+ const cleanups = new Map();
9
+ const drop = (res) => {
10
+ cleanups.get(res)?.();
11
+ cleanups.delete(res);
12
+ connections.delete(res);
13
+ };
14
+ return {
15
+ size: () => connections.size,
16
+ closeAll() {
17
+ for (const res of [...connections]) {
18
+ drop(res);
19
+ res.end?.();
20
+ }
21
+ },
22
+ handler(req, res) {
23
+ if (req.method !== undefined && req.method !== 'GET') {
24
+ res.statusCode = 405;
25
+ res.end();
26
+ return;
27
+ }
28
+ const url = req.url ?? '/';
29
+ const cut = url.indexOf('?');
30
+ const since = Number(new URLSearchParams(cut === -1 ? '' : url.slice(cut + 1)).get('since') ?? 0) || 0;
31
+ const opening = log.since(since);
32
+ if (typeof res.write !== 'function') {
33
+ res.statusCode = 200;
34
+ res.setHeader('content-type', 'application/json; charset=utf-8');
35
+ res.end(JSON.stringify({ ok: true, ...opening }));
36
+ return;
37
+ }
38
+ const headers = {
39
+ 'content-type': 'text/event-stream; charset=utf-8',
40
+ 'cache-control': 'no-store, no-transform',
41
+ connection: 'keep-alive',
42
+ };
43
+ if (typeof res.writeHead === 'function')
44
+ res.writeHead(200, headers);
45
+ else {
46
+ res.statusCode = 200;
47
+ for (const [name, value] of Object.entries(headers))
48
+ res.setHeader(name, value);
49
+ }
50
+ res.write(': connected\n\n');
51
+ res.write(frame({ type: 'sync', ...opening }));
52
+ connections.add(res);
53
+ const timer = setInterval(() => res.write?.(': ping\n\n'), HEARTBEAT_MS);
54
+ const unsubscribe = log.subscribe((change) => res.write?.(frame({ type: 'bump', ...change })));
55
+ cleanups.set(res, () => {
56
+ clearInterval(timer);
57
+ unsubscribe();
58
+ });
59
+ res.on?.('close', () => drop(res));
60
+ },
61
+ };
62
+ }
63
+ export function registerFactStream(ctx, log) {
64
+ const stream = createFactStream(log);
65
+ const unregister = ctx.webServer.register({ kind: 'exact', path: EVENTS_PATH, handler: stream.handler });
66
+ return () => {
67
+ stream.closeAll();
68
+ unregister();
69
+ };
70
+ }
@@ -22,3 +22,9 @@ export interface ToolUnit {
22
22
  }
23
23
  export declare function toolUnits(manifests: readonly UnitManifest[]): Map<string, ToolUnit>;
24
24
  export declare function wroteNow(result: unknown): boolean | undefined;
25
+ /**
26
+ * 由单元 id 拼出它那一族事实的范围名。
27
+ * @param unit 单元 id,不带后缀。
28
+ * @returns <unit>* —— 永远是范围名,不是某一条事实。
29
+ */
30
+ export declare function unitFactScope(unit: string): string;
@@ -1,3 +1,4 @@
1
+ import { FACT_SCOPE_SUFFIX } from '../api/facts.js';
1
2
  export function createFactLog(options = {}) {
2
3
  const epoch = options.epoch ?? `e${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
3
4
  let rev = 0;
@@ -42,3 +43,11 @@ export function wroteNow(result) {
42
43
  const wrote = value?.wrote;
43
44
  return typeof wrote === 'boolean' ? wrote : undefined;
44
45
  }
46
+ /**
47
+ * 由单元 id 拼出它那一族事实的范围名。
48
+ * @param unit 单元 id,不带后缀。
49
+ * @returns <unit>* —— 永远是范围名,不是某一条事实。
50
+ */
51
+ export function unitFactScope(unit) {
52
+ return `${unit}${FACT_SCOPE_SUFFIX}`;
53
+ }
package/dist/host/git.js CHANGED
@@ -1,4 +1,4 @@
1
- import { quoteShellArg, tailLines } from '../lib/text.js';
1
+ import { quoteShellArg, tailLines } from './shell-text.js';
2
2
  import { runShell } from './shell.js';
3
3
  export function createHeadReader(options) {
4
4
  const { shell, logger } = options;
@@ -1,5 +1,5 @@
1
1
  import type { DshHostContext } from '../api/dsh.js';
2
- import { type PanelStateStore } from '../lib/panel-state.js';
2
+ import { type PanelStateStore } from './panel-state.js';
3
3
  import { type ProvisionHandler } from './provision.js';
4
4
  export type RouteHandler = (search: URLSearchParams) => Promise<unknown> | unknown;
5
5
  export type RouteTable = Record<string, RouteHandler>;
@@ -27,7 +27,7 @@ export declare function changesHandler(log: {
27
27
  }): RouteHandler;
28
28
  export declare function preflightHandler(preflight: {
29
29
  run(options?: {
30
- force?: boolean;
30
+ hard?: boolean;
31
31
  }): Promise<unknown>;
32
32
  snapshot(): unknown;
33
33
  }): RouteHandler;
package/dist/host/http.js CHANGED
@@ -1,5 +1,5 @@
1
- import { unitFactScope } from '../api/facts.js';
2
- import { parseCardsReport, parseCrashReport } from '../lib/panel-state.js';
1
+ import { unitFactScope } from './facts.js';
2
+ import { parseCardsReport, parseCrashReport } from './panel-state.js';
3
3
  import { ACTIONS_PREFIX, API_PREFIX, DIY_DEBUG_PATH, DIY_RECORD_PATH, PANEL_STATE_PREFIX, PLUGIN_ID, PREFS_PREFIX, PROVISION_PREFIX } from '../config/constants.js';
4
4
  import { parseProvisionBody, } from './provision.js';
5
5
  import { WorkbenchDataError } from '../host/opencli.js';
@@ -179,7 +179,7 @@ export function changesHandler(log) {
179
179
  }
180
180
  export function preflightHandler(preflight) {
181
181
  return (search) => {
182
- void preflight.run({ force: flag(search, 'force') });
182
+ void preflight.run({ hard: flag(search, 'hard') });
183
183
  return { ok: true, ...preflight.snapshot() };
184
184
  };
185
185
  }
@@ -10,35 +10,46 @@ export interface LongTaskBase {
10
10
  readonly why?: string | undefined;
11
11
  readonly startedAt: string;
12
12
  readonly at: string;
13
+ readonly session?: string | undefined;
13
14
  }
14
15
  export interface LongTaskStart {
15
16
  readonly workspace: string;
16
17
  readonly target: string;
17
18
  readonly payload?: unknown;
19
+ readonly session?: string | undefined;
18
20
  }
19
21
  export interface LongTaskChange<S extends LongTaskBase> {
20
22
  readonly kind: 'start' | 'step' | 'settle';
21
23
  readonly snapshot: S;
22
24
  readonly step?: LongTaskStep | undefined;
25
+ readonly by?: string | undefined;
23
26
  }
24
27
  export interface LongTaskDeps<S extends LongTaskBase> {
25
28
  readonly run: (options: {
26
29
  readonly payload: unknown;
27
30
  readonly onStep: (step: LongTaskStep) => void;
31
+ readonly signal: AbortSignal;
28
32
  }) => Promise<unknown>;
29
33
  readonly step?: ((step: LongTaskStep, current: S) => Partial<S>) | undefined;
30
34
  readonly outcome?: ((result: unknown, current: S) => Partial<S>) | undefined;
35
+ readonly stopped?: ((current: S) => Partial<S>) | undefined;
31
36
  readonly view: (snapshot: S) => Readonly<Record<string, unknown>>;
32
37
  readonly now?: (() => string) | undefined;
33
38
  readonly nextId?: (() => string) | undefined;
34
39
  readonly facts?: ((change: LongTaskChange<S>) => readonly string[] | undefined) | undefined;
35
40
  readonly recentMs?: number | undefined;
36
41
  }
42
+ /**
43
+ * 一个单元的长任务注册表:`current(workspace)` 是面板那一行要的投影,`live()` 是外壳侧栏那颗
44
+ * loading 点要的(**不分工作区、不分目标**,把现在活着的每一趟交出来),`stop` 只把中断信号转给生产方。
45
+ */
37
46
  export interface LongTaskRegistry<S extends LongTaskBase> {
38
47
  start(options: LongTaskStart): S;
39
48
  snapshot(id: string): S | null;
40
49
  active(workspace: string, target: string): S | null;
41
50
  current(workspace: string): readonly Readonly<Record<string, unknown>>[];
51
+ live(): readonly S[];
52
+ stop(id: string, by?: string): boolean;
42
53
  onChange(listener: (change: LongTaskChange<S>) => void): () => void;
43
54
  }
44
55
  export declare function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDeps<S>): LongTaskRegistry<S>;
@@ -52,6 +63,7 @@ export declare function createLongTaskHost(options: {
52
63
  readonly scope: string;
53
64
  readonly unit: string;
54
65
  readonly bump: (facts: readonly string[]) => void;
66
+ readonly notify?: ((change: LongTaskChange<LongTaskBase>) => void) | undefined;
55
67
  }): {
56
68
  readonly host: LongTaskHost;
57
69
  dispose(): void;