@dsh-workbench/shared 1.5.3-rc.0 → 1.5.4-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,19 +5,22 @@ import type { FlowStepEvent } from './types.js';
5
5
  * 把流程的一步事件翻成长任务步骤流的默认一行。
6
6
  * `running` → `start`、`ok` → `succeed`、`skipped` → `info`、`failed` → `fail`、
7
7
  * `needs-login` → `info`(带 `loginUrl`)、`needs-human` → `info`(带 `intervention`);
8
- * 文字取 `message ?? title`(人工干预节点取 `intervention.title`)。
8
+ * 文字取 `message ?? title`(人工干预节点取 `intervention.title`);事件带 `links` 时每一行都跟着带。
9
9
  * 要改文案或过滤步骤,就在模块里自己映射 —— 这只是默认值。
10
10
  * @param event `flow.run({ onStep })` 收到的那条事件。
11
11
  * @returns 能直接交给长任务 `run({ onStep })` 的一行。
12
12
  */
13
13
  export function longTaskStepOf(event: FlowStepEvent): LongTaskStep {
14
14
  const text = event.message ?? event.title;
15
- if (event.status === 'failed') return { kind: 'fail', text };
15
+ const links =
16
+ event.links === undefined || event.links.length === 0 ? {} : { links: event.links };
17
+ if (event.status === 'failed') return { kind: 'fail', text, ...links };
16
18
  if (event.status === 'needs-human') {
17
19
  return {
18
20
  kind: 'info',
19
21
  text: event.intervention?.title ?? text,
20
22
  ...(event.intervention === undefined ? {} : { intervention: event.intervention }),
23
+ ...links,
21
24
  };
22
25
  }
23
26
  if (event.status === 'needs-login') {
@@ -27,9 +30,10 @@ export function longTaskStepOf(event: FlowStepEvent): LongTaskStep {
27
30
  ...(event.loginUrl === undefined || event.loginUrl === ''
28
31
  ? {}
29
32
  : { loginUrl: event.loginUrl }),
33
+ ...links,
30
34
  };
31
35
  }
32
- if (event.status === 'ok') return { kind: 'succeed', text };
33
- if (event.status === 'skipped') return { kind: 'info', text };
34
- return { kind: 'start', text };
36
+ if (event.status === 'ok') return { kind: 'succeed', text, ...links };
37
+ if (event.status === 'skipped') return { kind: 'info', text, ...links };
38
+ return { kind: 'start', text, ...links };
35
39
  }
@@ -33,19 +33,23 @@ function payloadOf(value: unknown): FlowPayload | null {
33
33
  }
34
34
 
35
35
  function longStepOf(event: FlowStepEvent): LongTaskStep {
36
+ const links =
37
+ event.links === undefined || event.links.length === 0 ? {} : { links: event.links };
36
38
  if (event.status === 'failed') {
37
39
  return {
38
40
  kind: 'fail',
39
41
  text: event.message === undefined ? event.title : `${event.title}:${event.message}`,
42
+ ...links,
40
43
  };
41
44
  }
42
- if (event.status === 'skipped') return { kind: 'info', text: `${event.title}(跳过)` };
43
- if (event.status === 'ok') return { kind: 'succeed', text: event.title };
45
+ if (event.status === 'skipped') return { kind: 'info', text: `${event.title}(跳过)`, ...links };
46
+ if (event.status === 'ok') return { kind: 'succeed', text: event.title, ...links };
44
47
  if (event.status === 'needs-human') {
45
48
  return {
46
49
  kind: 'info',
47
50
  text: event.intervention?.title ?? event.message ?? event.title,
48
51
  ...(event.intervention === undefined ? {} : { intervention: event.intervention }),
52
+ ...links,
49
53
  };
50
54
  }
51
55
  if (event.status === 'needs-login') {
@@ -55,13 +59,16 @@ function longStepOf(event: FlowStepEvent): LongTaskStep {
55
59
  ...(event.loginUrl === undefined || event.loginUrl === ''
56
60
  ? {}
57
61
  : { loginUrl: event.loginUrl }),
62
+ ...links,
58
63
  };
59
64
  }
60
- if (event.message !== undefined) return { kind: 'info', text: `${event.title}:${event.message}` };
65
+ if (event.message !== undefined) {
66
+ return { kind: 'info', text: `${event.title}:${event.message}`, ...links };
67
+ }
61
68
  if (event.attempt !== undefined && event.attempt > 1) {
62
- return { kind: 'info', text: `${event.title}(第 ${event.attempt} 次)` };
69
+ return { kind: 'info', text: `${event.title}(第 ${event.attempt} 次)`, ...links };
63
70
  }
64
- return { kind: 'start', text: event.title };
71
+ return { kind: 'start', text: event.title, ...links };
65
72
  }
66
73
 
67
74
  function targetOf(flow: FlowDefinition, input: unknown): string {
@@ -112,6 +119,7 @@ export function flowTaskDeps(
112
119
  status: snapshot.status,
113
120
  step: snapshot.step,
114
121
  steps: snapshot.steps,
122
+ ...(snapshot.links === undefined ? {} : { links: snapshot.links }),
115
123
  intervention: snapshot.intervention,
116
124
  loginUrl: snapshot.loginUrl,
117
125
  why: snapshot.why,
@@ -1,5 +1,5 @@
1
1
  import type { DataCallOptions, PlanResults, RequestPlan } from '../../contract/data.js';
2
- import type { LongTaskIntervention } from '../../contract/long-task.js';
2
+ import type { LongTaskIntervention, LongTaskLink } from '../../contract/long-task.js';
3
3
  import type { ImageLike } from '../ai/types.js';
4
4
  import type { AiPort } from '../ai/port.js';
5
5
 
@@ -208,7 +208,7 @@ export interface FlowActionContext {
208
208
  readonly runId: string;
209
209
  readonly workspace: string;
210
210
  readonly stepId: string;
211
- log(text: string): void;
211
+ log(text: string, links?: readonly LongTaskLink[]): void;
212
212
  login(url: string): void;
213
213
  need(intervention: LongTaskIntervention): void;
214
214
  }
@@ -259,6 +259,7 @@ export interface FlowStepEvent {
259
259
  readonly message?: string | undefined;
260
260
  readonly loginUrl?: string | undefined;
261
261
  readonly intervention?: LongTaskIntervention | undefined;
262
+ readonly links?: readonly LongTaskLink[] | undefined;
262
263
  }
263
264
 
264
265
  /**
@@ -3,6 +3,7 @@ import type { LongTaskIntervention, LongTaskStep } from '../../../contract/long-
3
3
  import {
4
4
  createLongTaskHost,
5
5
  createLongTaskRegistry,
6
+ longTaskRowBase,
6
7
  sharedLongTaskRegistry,
7
8
  withInterventionRetry,
8
9
  type LongTaskBase,
@@ -129,6 +130,55 @@ describe('createLongTaskRegistry.stop(停止只是转发给生产方,认不
129
130
  expect(runs.snapshot(started.id)?.status).toBe('stopped');
130
131
  });
131
132
 
133
+ it('★★ 步骤另攒一份结构化的 history:kind / text / links,与 steps 同源', () => {
134
+ const h = harness();
135
+ const runs = createLongTaskRegistry(h.deps);
136
+ const started = runs.start({ workspace: '/w/a', target: 'release' });
137
+
138
+ h.step({ kind: 'start', text: '拉取 main' });
139
+ h.step({
140
+ kind: 'fail',
141
+ text: '推送 tag 失败',
142
+ links: [{ href: 'https://example.test/pipeline/1', text: '打开流水线' }],
143
+ });
144
+
145
+ expect(runs.snapshot(started.id)?.history).toEqual([
146
+ { kind: 'start', text: '拉取 main' },
147
+ {
148
+ kind: 'fail',
149
+ text: '推送 tag 失败',
150
+ links: [{ href: 'https://example.test/pipeline/1', text: '打开流水线' }],
151
+ },
152
+ ]);
153
+ expect(runs.snapshot(started.id)?.steps).toEqual(['拉取 main', '✗ 推送 tag 失败']);
154
+ });
155
+
156
+ it('★ history 与 steps 一起只留最近 20 条;每一步最多留 3 条链接', () => {
157
+ const h = harness();
158
+ const runs = createLongTaskRegistry(h.deps);
159
+ const started = runs.start({ workspace: '/w/a', target: 'release' });
160
+
161
+ for (let index = 0; index < 25; index += 1) {
162
+ h.step({ kind: 'info', text: '第 ' + String(index) + ' 步' });
163
+ }
164
+ h.step({
165
+ kind: 'info',
166
+ text: '四颗链接',
167
+ links: [
168
+ { href: 'https://a.test/1', text: '一' },
169
+ { href: 'https://a.test/2', text: '二' },
170
+ { href: 'https://a.test/3', text: '三' },
171
+ { href: 'https://a.test/4', text: '四' },
172
+ ],
173
+ });
174
+
175
+ const history = runs.snapshot(started.id)?.history ?? [];
176
+ expect(history).toHaveLength(20);
177
+ expect(history[history.length - 1]?.text).toBe('四颗链接');
178
+ expect(history[history.length - 1]?.links).toHaveLength(3);
179
+ expect(runs.snapshot(started.id)?.steps).toHaveLength(20);
180
+ });
181
+
132
182
  it('★★ current() 每条都带 id(浏览器按 id 点那颗钮),view 覆盖不了', () => {
133
183
  const h = harness({ view: (snapshot) => ({ id: 'spoofed', status: snapshot.status }) });
134
184
  const runs = createLongTaskRegistry({ ...h.deps, nextId: () => 'run-1' });
@@ -780,3 +830,32 @@ describe('热重载换实现(槽里那份认回来,但跑新代码)', () =
780
830
  expect(ran).toEqual(['old', 'new']);
781
831
  });
782
832
  });
833
+
834
+ describe('longTaskRowBase(外壳先铺这一层,view 只覆盖不遗漏)', () => {
835
+ const base: LongTaskBase = {
836
+ id: 'run-1',
837
+ workspace: '/w/a',
838
+ target: 'release',
839
+ status: 'running',
840
+ step: '发布中',
841
+ steps: ['开始'],
842
+ startedAt: '2026-10-21T02:00:00.000Z',
843
+ at: '2026-10-21T02:00:01.000Z',
844
+ };
845
+
846
+ it('★★ 没有 links 就不落这个键(旧快照不多一个空键)', () => {
847
+ expect('links' in longTaskRowBase(base)).toBe(false);
848
+ });
849
+
850
+ it('★★ 有 links 就原样带上(逐条是 { href, text })', () => {
851
+ const links = [{ href: 'https://ci.example/42', text: '看流水线' }];
852
+ expect(longTaskRowBase({ ...base, links }).links).toEqual(links);
853
+ });
854
+
855
+ it('★★ history 与 steps 一起给(旧视图读 steps,新视图读结构化那批)', () => {
856
+ const history = [{ kind: 'start' as const, text: '开始' }];
857
+ const row = longTaskRowBase({ ...base, history });
858
+ expect(row.history).toEqual(history);
859
+ expect(row.steps).toEqual(['开始']);
860
+ });
861
+ });
@@ -1,9 +1,13 @@
1
1
  import { epochOf, now as clockNow, nowIso } from '../runtime/time.js';
2
2
  import {
3
+ LONG_TASK_HISTORY_MAX,
4
+ LONG_TASK_LINK_MAX,
3
5
  runsRouteOf,
4
6
  type LongTaskIntervention,
7
+ type LongTaskLink,
5
8
  type LongTaskStatus,
6
9
  type LongTaskStep,
10
+ type LongTaskStepRow,
7
11
  } from '../../contract/long-task.js';
8
12
 
9
13
  function isLiveStatus(status: LongTaskStatus): boolean {
@@ -20,6 +24,8 @@ export interface LongTaskBase {
20
24
  readonly status: LongTaskStatus;
21
25
  readonly step: string;
22
26
  readonly steps: readonly string[];
27
+ readonly history?: readonly LongTaskStepRow[] | undefined;
28
+ readonly links?: readonly LongTaskLink[] | undefined;
23
29
  readonly intervention?: LongTaskIntervention | undefined;
24
30
  readonly loginUrl?: string | undefined;
25
31
  readonly why?: string | undefined;
@@ -79,6 +85,47 @@ export interface LongTaskRegistry<S extends LongTaskBase> {
79
85
  rebind?(deps: LongTaskDeps<S>): void;
80
86
  }
81
87
 
88
+ /**
89
+ * 长任务行里那一层通用字段:去掉身份(`id` / `workspace`)之后的全套快照。
90
+ */
91
+ export type LongTaskRowBase = Omit<LongTaskBase, 'id' | 'workspace'>;
92
+
93
+ function pickDefined<T extends object, K extends keyof T>(
94
+ source: T,
95
+ keys: readonly K[],
96
+ ): Partial<Pick<T, K>> {
97
+ const out: Partial<Pick<T, K>> = {};
98
+ for (const key of keys) {
99
+ if (source[key] !== undefined) out[key] = source[key];
100
+ }
101
+ return out;
102
+ }
103
+
104
+ /**
105
+ * 长任务快照的通用字段:外壳先铺这一层,模块的 `view()` 只覆盖、不遗漏。
106
+ * @param snapshot 这一趟的快照。
107
+ * @returns `target` / `status` / `step` / `steps` / `startedAt` / `at` 与快照里存在的可选字段。
108
+ */
109
+ export function longTaskRowBase(snapshot: LongTaskBase): LongTaskRowBase {
110
+ return {
111
+ target: snapshot.target,
112
+ status: snapshot.status,
113
+ step: snapshot.step,
114
+ steps: snapshot.steps,
115
+ startedAt: snapshot.startedAt,
116
+ at: snapshot.at,
117
+ ...pickDefined(snapshot, [
118
+ 'headline',
119
+ 'history',
120
+ 'links',
121
+ 'intervention',
122
+ 'loginUrl',
123
+ 'why',
124
+ 'session',
125
+ ]),
126
+ };
127
+ }
128
+
82
129
  export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDeps<S>): LongTaskRegistry<S> {
83
130
  let bound = deps;
84
131
 
@@ -143,6 +190,7 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
143
190
  )
144
191
  .reverse()
145
192
  .map((snapshot) => ({
193
+ ...longTaskRowBase(snapshot),
146
194
  ...bound.view(snapshot as S),
147
195
  id: snapshot.id,
148
196
  ...(bound.recheck === undefined ? {} : { resumable: true }),
@@ -166,6 +214,7 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
166
214
  status: 'running',
167
215
  step: '',
168
216
  steps: [],
217
+ history: [],
169
218
  startedAt,
170
219
  at: startedAt,
171
220
  ...(options.session === undefined ? {} : { session: options.session }),
@@ -180,7 +229,17 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
180
229
  current.step === step.text &&
181
230
  step.intervention !== undefined &&
182
231
  current.intervention?.title === step.intervention.title;
183
- const steps = repeated ? current.steps : [...current.steps, line].slice(-20);
232
+ const steps = repeated ? current.steps : [...current.steps, line].slice(-LONG_TASK_HISTORY_MAX);
233
+ const historyRow: LongTaskStepRow = {
234
+ kind: step.kind,
235
+ text: step.text,
236
+ ...(step.links === undefined
237
+ ? {}
238
+ : { links: step.links.slice(0, LONG_TASK_LINK_MAX) }),
239
+ };
240
+ const history = repeated
241
+ ? current.history ?? []
242
+ : [...(current.history ?? []), historyRow].slice(-LONG_TASK_HISTORY_MAX);
184
243
  const extra = bound.step?.(step, current) ?? {};
185
244
  if (step.intervention !== undefined) {
186
245
  patch(
@@ -190,6 +249,7 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
190
249
  status: 'needs-human',
191
250
  step: step.text,
192
251
  steps,
252
+ history,
193
253
  intervention: step.intervention,
194
254
  loginUrl: undefined,
195
255
  },
@@ -200,7 +260,14 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
200
260
  if (step.loginUrl !== undefined && step.loginUrl !== '') {
201
261
  patch(
202
262
  id,
203
- { ...extra, status: 'needs-login', step: step.text, steps, loginUrl: step.loginUrl },
263
+ {
264
+ ...extra,
265
+ status: 'needs-login',
266
+ step: step.text,
267
+ steps,
268
+ history,
269
+ loginUrl: step.loginUrl,
270
+ },
204
271
  { kind: 'step', step },
205
272
  );
206
273
  return;
@@ -212,6 +279,7 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
212
279
  status: 'running',
213
280
  step: step.text,
214
281
  steps,
282
+ history,
215
283
  intervention: undefined,
216
284
  loginUrl: undefined,
217
285
  },
package/src/i18n/en.ts CHANGED
@@ -1031,5 +1031,17 @@ export const en: Record<keyof WorkbenchMessages, string> = {
1031
1031
  'longTask.stop': 'Stop {label}',
1032
1032
  'longTask.stopConfirm': 'Confirm stop',
1033
1033
  'longTask.stopArmed': 'Click again to stop',
1034
+ 'longTask.stopPending': 'Stop requested; waiting for the module',
1035
+ 'longTask.list.entry': 'Long tasks {n}',
1036
+ 'longTask.list.title': 'All long tasks',
1037
+ 'longTask.state.running': 'Running',
1038
+ 'longTask.state.needsHuman': 'Waiting for you',
1039
+ 'longTask.state.needsLogin': 'Needs sign-in',
1040
+ 'longTask.state.stopped': 'Stopped',
1041
+ 'longTask.state.ok': 'Done',
1042
+ 'longTask.state.failed': 'Failed',
1043
+ 'longTask.duration.sec': '{s}s',
1044
+ 'longTask.duration.minSec': '{m}m {s}s',
1045
+ 'longTask.duration.hourMin': '{h}h {m}m',
1034
1046
  ...enNeedText,
1035
1047
  };
package/src/i18n/zh.ts CHANGED
@@ -1025,6 +1025,18 @@ export const zh = {
1025
1025
  'longTask.stop': '停止任务 {label}',
1026
1026
  'longTask.stopConfirm': '确认停止',
1027
1027
  'longTask.stopArmed': '再次点击确认停止',
1028
+ 'longTask.stopPending': '已请求停止,等模块确认',
1029
+ 'longTask.list.entry': '长任务 {n}',
1030
+ 'longTask.list.title': '全部长任务',
1031
+ 'longTask.state.running': '运行中',
1032
+ 'longTask.state.needsHuman': '等你处理',
1033
+ 'longTask.state.needsLogin': '需要登录',
1034
+ 'longTask.state.stopped': '已停止',
1035
+ 'longTask.state.ok': '已完成',
1036
+ 'longTask.state.failed': '失败',
1037
+ 'longTask.duration.sec': '{s} 秒',
1038
+ 'longTask.duration.minSec': '{m} 分 {s} 秒',
1039
+ 'longTask.duration.hourMin': '{h} 小时 {m} 分',
1028
1040
  ...zhNeedText,
1029
1041
  } as const;
1030
1042