@dsh-workbench/shared 1.5.5 → 1.5.6

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 (93) hide show
  1. package/dist/api/dsh.d.ts +9 -1
  2. package/dist/components/card/ApprovalPlan.css +3 -0
  3. package/dist/components/card/ApprovalPlan.js +7 -2
  4. package/dist/components/card/LongTaskRow.css +32 -0
  5. package/dist/components/card/LongTaskRow.d.ts +8 -2
  6. package/dist/components/card/LongTaskRow.js +26 -5
  7. package/dist/components/card/board-module.d.ts +35 -4
  8. package/dist/components/card/use-text-marquee.d.ts +15 -0
  9. package/dist/components/card/use-text-marquee.js +66 -0
  10. package/dist/components/feedback/ProgressBar.css +56 -0
  11. package/dist/components/feedback/ProgressBar.d.ts +25 -0
  12. package/dist/components/feedback/ProgressBar.js +22 -0
  13. package/dist/config/constants.d.ts +2 -0
  14. package/dist/config/constants.js +2 -0
  15. package/dist/contract/guards.d.ts +1 -0
  16. package/dist/contract/guards.js +3 -0
  17. package/dist/contract/long-task.d.ts +56 -0
  18. package/dist/contract/long-task.js +41 -0
  19. package/dist/contract/protocol/long-tasks.d.ts +8 -2
  20. package/dist/contract/unit.d.ts +1 -0
  21. package/dist/host/diy/diy-preset.d.ts +1 -1
  22. package/dist/host/diy/diy-preset.js +3 -3
  23. package/dist/host/flow/create.d.ts +2 -1
  24. package/dist/host/flow/create.js +6 -0
  25. package/dist/host/flow/dsh-journal.d.ts +3 -0
  26. package/dist/host/flow/dsh-journal.js +55 -0
  27. package/dist/host/flow/journal.d.ts +20 -0
  28. package/dist/host/flow/journal.js +30 -0
  29. package/dist/host/flow/open.js +43 -26
  30. package/dist/host/flow/task.js +12 -1
  31. package/dist/host/flow/types.d.ts +5 -0
  32. package/dist/host/platform/integration/browser-launch.d.ts +1 -0
  33. package/dist/host/platform/integration/browser-launch.js +9 -2
  34. package/dist/host/profile/bundle-manager.js +4 -6
  35. package/dist/host/unit/long-task-host.d.ts +18 -0
  36. package/dist/host/unit/long-task-host.js +44 -0
  37. package/dist/host/unit/long-task-progress.d.ts +3 -0
  38. package/dist/host/unit/long-task-progress.js +32 -0
  39. package/dist/host/unit/long-task.d.ts +14 -38
  40. package/dist/host/unit/long-task.js +95 -94
  41. package/dist/host/unit/unit-face.d.ts +2 -1
  42. package/dist/host/unit/with-intervention-retry.d.ts +20 -0
  43. package/dist/host/unit/with-intervention-retry.js +45 -0
  44. package/dist/host.d.ts +30 -19
  45. package/dist/host.js +30 -19
  46. package/dist/i18n/en.js +33 -7
  47. package/dist/i18n/index.d.ts +33 -7
  48. package/dist/i18n/zh.d.ts +32 -6
  49. package/dist/i18n/zh.js +32 -6
  50. package/dist/index.d.ts +4 -2
  51. package/dist/index.js +4 -2
  52. package/package.json +2 -1
  53. package/src/api/dsh.ts +12 -1
  54. package/src/components/card/ApprovalPlan.css +3 -0
  55. package/src/components/card/ApprovalPlan.tsx +9 -2
  56. package/src/components/card/LongTaskRow.css +32 -0
  57. package/src/components/card/LongTaskRow.tsx +51 -4
  58. package/src/components/card/__tests__/ApprovalPlan.test.tsx +50 -0
  59. package/src/components/card/__tests__/long-task-row.test.ts +64 -2
  60. package/src/components/card/board-module.ts +36 -4
  61. package/src/components/card/use-text-marquee.ts +79 -0
  62. package/src/components/feedback/ProgressBar.css +56 -0
  63. package/src/components/feedback/ProgressBar.tsx +60 -0
  64. package/src/config/constants.ts +3 -0
  65. package/src/contract/__tests__/long-task.test.ts +43 -1
  66. package/src/contract/guards.ts +4 -0
  67. package/src/contract/long-task.ts +75 -0
  68. package/src/contract/protocol/long-tasks.ts +9 -1
  69. package/src/contract/unit.ts +1 -0
  70. package/src/host/diy/__tests__/diy-preset.test.ts +2 -0
  71. package/src/host/diy/diy-preset.ts +3 -2
  72. package/src/host/flow/__tests__/dsh-journal.test.ts +56 -0
  73. package/src/host/flow/__tests__/flow-create.test.ts +41 -1
  74. package/src/host/flow/__tests__/flow-open.test.ts +32 -0
  75. package/src/host/flow/create.ts +8 -5
  76. package/src/host/flow/dsh-journal.ts +74 -0
  77. package/src/host/flow/journal.ts +46 -0
  78. package/src/host/flow/open.ts +41 -26
  79. package/src/host/flow/task.ts +11 -1
  80. package/src/host/flow/types.ts +6 -0
  81. package/src/host/platform/integration/__tests__/browser-launch.test.ts +27 -0
  82. package/src/host/platform/integration/browser-launch.ts +15 -3
  83. package/src/host/profile/bundle-manager.ts +4 -7
  84. package/src/host/unit/__tests__/long-task.test.ts +203 -2
  85. package/src/host/unit/long-task-host.ts +64 -0
  86. package/src/host/unit/long-task-progress.ts +37 -0
  87. package/src/host/unit/long-task.ts +112 -126
  88. package/src/host/unit/unit-face.ts +2 -1
  89. package/src/host/unit/with-intervention-retry.ts +55 -0
  90. package/src/host.ts +31 -20
  91. package/src/i18n/en.ts +33 -7
  92. package/src/i18n/zh.ts +32 -6
  93. package/src/index.ts +4 -2
@@ -0,0 +1,60 @@
1
+ import { h } from '../../lib/react.js';
2
+ import {
3
+ longTaskProgressIsValid,
4
+ longTaskProgressPercent,
5
+ type LongTaskProgress,
6
+ } from '../../contract/long-task.js';
7
+ import './ProgressBar.css';
8
+
9
+ /**
10
+ * 进度条的色档:与卡片指标的档位同一套令牌。
11
+ */
12
+ export type ProgressBarTone = 'neutral' | 'ok' | 'warn' | 'bad' | 'busy';
13
+
14
+ /**
15
+ * `ProgressBar` 的契约:走到哪、一共多少、多高、什么色。
16
+ */
17
+ export interface ProgressBarProps {
18
+ /** 进度:`done` / `total` 两个数。不合法时整根条不画。 */
19
+ readonly progress: LongTaskProgress;
20
+ /** 条高像素:6 是卡片指标档,4 是长任务普通档,2 是紧凑档(左栏列表)。默认 4。 */
21
+ readonly size?: 6 | 4 | 2;
22
+ /** 色档,默认按「在走」取主色。 */
23
+ readonly tone?: ProgressBarTone;
24
+ /** 读屏用的名字;给了才挂读屏可见的进角色。 */
25
+ readonly label?: string | undefined;
26
+ }
27
+
28
+ const FILL: Readonly<Record<ProgressBarTone, string>> = {
29
+ neutral: 'wb-progressFillNeutral',
30
+ ok: 'wb-progressFillOk',
31
+ warn: 'wb-progressFillWarn',
32
+ bad: 'wb-progressFillBad',
33
+ busy: 'wb-progressFillBusy',
34
+ };
35
+
36
+ /**
37
+ * 一根进度条:轨道 + 填充,宽度按百分比。
38
+ * @param props 进度、条高、色档与读屏名。
39
+ * @returns 有进度时是一根带 `progressbar` 角色的条;没有合法进度时什么都不画。
40
+ */
41
+ export function ProgressBar({ progress, size = 4, tone = 'busy', label }: ProgressBarProps) {
42
+ if (!longTaskProgressIsValid(progress)) return null;
43
+ const width = longTaskProgressPercent(progress) ?? 0;
44
+ return (
45
+ <div
46
+ className={`wb-progress wb-progress-${String(size)}`}
47
+ role="progressbar"
48
+ aria-valuemin={0}
49
+ aria-valuemax={progress.total}
50
+ aria-valuenow={progress.done}
51
+ aria-valuetext={`${String(progress.done)}/${String(progress.total)}`}
52
+ {...(label === undefined ? {} : { 'aria-label': label })}
53
+ >
54
+ <div
55
+ className={`wb-progressFill ${FILL[tone]}`}
56
+ style={{ width: `${String(width)}%` }}
57
+ />
58
+ </div>
59
+ );
60
+ }
@@ -76,6 +76,9 @@ export const DIY_RECORD_SERVICE = "workbenchDiyRecord";
76
76
  /** 会话投影服务的 dsh 服务名(`sessionProjections`):宿主经 `stateOf(session, 'agentPreset')` 读那一档。 */
77
77
  export const SESSION_PROJECTIONS_SERVICE = "sessionProjections";
78
78
 
79
+ /** 会话名册服务的 dsh 服务名(`sessions`):按 id 拿 Session,写 plugin record。 */
80
+ export const SESSION_STORE_SERVICE = "sessions";
81
+
79
82
  /** 面板清单一次取回的行数上限(条):外部模块拿它当分页参数。 */
80
83
  export const PANEL_LIST_LIMIT = 50;
81
84
 
@@ -4,6 +4,10 @@ import {
4
4
  longTaskDot,
5
5
  longTaskIsLive,
6
6
  longTaskPollMs,
7
+ longTaskPlanStepsOf,
8
+ longTaskPlanTotalOf,
9
+ longTaskProgressIsValid,
10
+ longTaskProgressPercent,
7
11
  longTaskRunsOf,
8
12
  longTaskStopPress,
9
13
  longTaskStopStage,
@@ -113,7 +117,45 @@ describe('longTaskRunsOf(这一读交回的"当前那些趟")', () => {
113
117
  });
114
118
  });
115
119
 
116
- describe('longTaskPollMs(轮询两档:还在跑要快,都收尾了也要问)', () => {
120
+
121
+
122
+ describe('longTaskPlanTotalOf / longTaskPlanStepsOf:两种写法都认', () => {
123
+ it('★★ 步骤表按长度算,只给个数按它算;非法当没有计划', () => {
124
+ expect(longTaskPlanTotalOf({ steps: [{ id: 'a' }, { id: 'b' }] })).toBe(2);
125
+ expect(longTaskPlanTotalOf({ total: 5 })).toBe(5);
126
+ expect(longTaskPlanTotalOf({ total: 0 })).toBe(0);
127
+ expect(longTaskPlanTotalOf({ total: -2 })).toBe(0);
128
+ expect(longTaskPlanTotalOf({ total: Number.NaN })).toBe(0);
129
+ expect(longTaskPlanTotalOf({ steps: [] })).toBe(0);
130
+ });
131
+
132
+ it('★ 只给个数时列不出步骤(回空表)', () => {
133
+ expect(longTaskPlanStepsOf({ total: 3 })).toEqual([]);
134
+ expect(longTaskPlanStepsOf({ steps: [{ id: 'a', title: '合并' }] })).toEqual([{ id: 'a', title: '合并' }]);
135
+ });
136
+ });
137
+
138
+ describe('longTaskProgressIsValid / longTaskProgressPercent:合法才画条', () => {
139
+ it('★★ 两个有限数、total > 0、done 落在 [0, total] 才算合法', () => {
140
+ expect(longTaskProgressIsValid({ done: 0, total: 3 })).toBe(true);
141
+ expect(longTaskProgressIsValid({ done: 3, total: 3 })).toBe(true);
142
+ expect(longTaskProgressIsValid({ done: 0, total: 0 })).toBe(false);
143
+ expect(longTaskProgressIsValid({ done: 1, total: -3 })).toBe(false);
144
+ expect(longTaskProgressIsValid({ done: -1, total: 3 })).toBe(false);
145
+ expect(longTaskProgressIsValid({ done: 4, total: 3 })).toBe(false);
146
+ expect(longTaskProgressIsValid({ done: Number.NaN, total: 3 })).toBe(false);
147
+ expect(longTaskProgressIsValid({ done: 1, total: Number.POSITIVE_INFINITY })).toBe(false);
148
+ });
149
+
150
+ it('★★ 百分比:没给或非法回 null,合法按 done/total 算', () => {
151
+ expect(longTaskProgressPercent(undefined)).toBeNull();
152
+ expect(longTaskProgressPercent({ done: 9, total: 0 })).toBeNull();
153
+ expect(longTaskProgressPercent({ done: 1, total: 4 })).toBe(25);
154
+ expect(longTaskProgressPercent({ done: 4, total: 4 })).toBe(100);
155
+ });
156
+ });
157
+
158
+ describe('longTaskPollMs(轮询两档:运行中要快,全收尾了也要问)', () => {
117
159
  it('还在跑 → 快档(进度要跟手)', () => {
118
160
  expect(longTaskPollMs(true)).toBe(1500);
119
161
  });
@@ -2,6 +2,10 @@ export function text(value: unknown): string {
2
2
  return typeof value === 'string' ? value.trim() : '';
3
3
  }
4
4
 
5
+ export function texts(value: unknown): string[] {
6
+ return Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string') : [];
7
+ }
8
+
5
9
  export function isRecord(value: unknown): value is Readonly<Record<string, unknown>> {
6
10
  return typeof value === 'object' && value !== null && !Array.isArray(value);
7
11
  }
@@ -25,6 +25,71 @@ export const LONG_TASK_LINK_MAX = 3;
25
25
 
26
26
  export const LONG_TASK_HISTORY_MAX = 20;
27
27
 
28
+ /**
29
+ * 这一趟走到哪了:`done` 是已完成的数量,`total` 是一共多少。
30
+ * 没有单位词 —— 外壳只画 `3/7` 与一根条;「步」还是「项」由模块自己在步骤文案里说。
31
+ */
32
+ export interface LongTaskProgress {
33
+ readonly done: number;
34
+ readonly total: number;
35
+ }
36
+
37
+ /**
38
+ * 进度是否可用:两个有限数、`total > 0`、`done` 落在 `[0, total]`。
39
+ * 不合法的进度一律当作「没有进度」,界面上不画条 —— 不夹到边界,免得把坏数字画成好数字。
40
+ * @param progress 模块或注册表给的进度。
41
+ * @returns 可用为 `true`。
42
+ */
43
+ export function longTaskProgressIsValid(progress: LongTaskProgress): boolean {
44
+ const { done, total } = progress;
45
+ return Number.isFinite(done) && Number.isFinite(total) && total > 0 && done >= 0 && done <= total;
46
+ }
47
+
48
+ /**
49
+ * 把进度折成 0..100 的百分比,供那根条用。
50
+ * @param progress 进度;没给就当没有进度。
51
+ * @returns 百分比;没给或不合法回 `null`。
52
+ */
53
+ /**
54
+ * 计划里的一步:`id` 与步骤事件里的 `stepId` 对齐,`title` 只是给展开里那行用的名字。
55
+ */
56
+ export interface LongTaskPlanStep {
57
+ readonly id: string;
58
+ readonly title?: string | undefined;
59
+ }
60
+
61
+ /**
62
+ * 这一趟打算走几步:给步骤表(能顺带列出没开始的步骤),或只给个数。
63
+ * 数字由外壳从步骤事件自己数,模块不报 `done`。
64
+ */
65
+ export type LongTaskPlan =
66
+ | { readonly steps: readonly LongTaskPlanStep[] }
67
+ | { readonly total: number };
68
+
69
+ /**
70
+ * 计划的总步数;两种写法都认,`total` 不合法时回 0(当作没有计划)。
71
+ * @param plan 模块声明的计划。
72
+ * @returns 步数。
73
+ */
74
+ export function longTaskPlanTotalOf(plan: LongTaskPlan): number {
75
+ const total = 'steps' in plan ? plan.steps.length : plan.total;
76
+ return Number.isFinite(total) && total > 0 ? total : 0;
77
+ }
78
+
79
+ /**
80
+ * 计划里的步骤表;只给了个数的那种回空表(列不出没开始的步骤)。
81
+ * @param plan 模块声明的计划。
82
+ * @returns 步骤表。
83
+ */
84
+ export function longTaskPlanStepsOf(plan: LongTaskPlan): readonly LongTaskPlanStep[] {
85
+ return 'steps' in plan ? plan.steps : [];
86
+ }
87
+
88
+ export function longTaskProgressPercent(progress: LongTaskProgress | undefined): number | null {
89
+ if (progress === undefined || !longTaskProgressIsValid(progress)) return null;
90
+ return (progress.done / progress.total) * 100;
91
+ }
92
+
28
93
  /**
29
94
  * 回执字段的输入类型:`text` 明文,`secret` 密码框(值同样只回给模块)。
30
95
  */
@@ -90,6 +155,16 @@ export function runsRouteOf(unit: string): string {
90
155
  return `${unit}/runs`;
91
156
  }
92
157
 
158
+ /**
159
+ * 「看过了」那条写路由(走 actions 通道的资源名):`<单元>/runs/seen`。
160
+ * 收尾后由外壳在列表把它渲染出来时发,清掉工作区选择器与会话行上的「已完成」提示。
161
+ * @param unit 单元 id。
162
+ * @returns <单元>/runs/seen。
163
+ */
164
+ export function seenRouteOf(unit: string): string {
165
+ return `${unit}/runs/seen`;
166
+ }
167
+
93
168
  /**
94
169
  * 停止那条写路由(走 actions 通道的资源名):`<单元>/runs/stop`。
95
170
  * @param unit 单元 id。
@@ -1,13 +1,21 @@
1
1
  import type {
2
2
  LongTaskIntervention,
3
3
  LongTaskLink,
4
+ LongTaskPlanStep,
5
+ LongTaskProgress,
4
6
  LongTaskStatus,
5
7
  LongTaskStepRow,
6
8
  } from '../long-task.js';
7
9
 
8
10
  export const LONG_TASKS_ROUTE = 'long-tasks';
9
11
 
10
- export interface ShellLongTaskRow {
12
+ interface ShellLongTaskSeenFields {
13
+ readonly seen?: boolean | undefined;
14
+ readonly progress?: LongTaskProgress | undefined;
15
+ readonly plan?: readonly LongTaskPlanStep[] | undefined;
16
+ }
17
+
18
+ export interface ShellLongTaskRow extends ShellLongTaskSeenFields {
11
19
  readonly unit: string;
12
20
  readonly id: string;
13
21
  readonly workspace: string;
@@ -101,6 +101,7 @@ export interface PluginRelease {
101
101
  */
102
102
  export interface UnitManifest {
103
103
  readonly id: string;
104
+ readonly skillId?: string;
104
105
  readonly title?: UnitTitle;
105
106
  readonly titleKey: string;
106
107
  readonly description?: string;
@@ -188,10 +188,12 @@ describe('diyPresetSourcesOf(可选的来源模式)', () => {
188
188
  entriesOf(
189
189
  { ...standard(), id: 'late', order: 5, name: '后来的模式', description: '说明' },
190
190
  { ...standard(), id: 'early', order: 2 },
191
+ { ...standard(), id: 'cordis', order: 3 },
191
192
  { ...standard(), id: DIY_PRESET_ID, order: 1 },
192
193
  ),
193
194
  );
194
195
  expect(rows.map((row) => row.id)).toEqual(['early', 'late']);
196
+ expect(rows.map((row) => row.id)).not.toContain('cordis');
195
197
  expect(rows[1]).toEqual({
196
198
  id: 'late',
197
199
  name: '后来的模式',
@@ -149,7 +149,7 @@ export function diyPresetSourcesOf(entries: Iterable<DshLoaderEntry>): readonly
149
149
  const rows: DiyPresetSourceRow[] = [];
150
150
  for (const config of configRowsOf(entries)) {
151
151
  const id = config.id as string;
152
- if (id === DIY_PRESET_ID || seen.has(id)) continue;
152
+ if (id === DIY_PRESET_ID || id === 'cordis' || seen.has(id)) continue;
153
153
  seen.add(id);
154
154
  rows.push({
155
155
  id,
@@ -176,6 +176,7 @@ export function diyPresetFrom(
176
176
  source: unknown,
177
177
  skillsDir: string,
178
178
  sourceId: string = DIY_PRESET_SOURCE_DEFAULT,
179
+ sourceName: string = sourceId,
179
180
  ): DshAgentPresetDefinition {
180
181
  const plugins = clonedPluginsOf(sourceConfigOf(source, sourceId), sourceId);
181
182
 
@@ -206,7 +207,7 @@ export function diyPresetFrom(
206
207
  return {
207
208
  id: DIY_PRESET_ID,
208
209
  name: DIY_PRESET_NAME,
209
- description: DIY_PRESET_DESCRIPTION,
210
+ description: `${DIY_PRESET_DESCRIPTION}(基于「${sourceName}」)`,
210
211
  order: DIY_PRESET_ORDER,
211
212
  plugins: plugins as unknown as DshAgentPresetDefinition['plugins'],
212
213
  };
@@ -0,0 +1,56 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { Context } from '@deepseek-ai/cordis';
3
+ import SessionStore, { type Session, type SessionId } from '@deepseek-ai/dsh-session';
4
+ import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection';
5
+ import { createDshFlowJournal } from '../dsh-journal.js';
6
+
7
+ const snapshot = {
8
+ version: 1 as const,
9
+ flow: 'diy-tweak',
10
+ input: { destination: 'tweak' },
11
+ step: 'build',
12
+ values: { plan: { see: ['一眼'] } },
13
+ status: 'running' as const,
14
+ };
15
+
16
+ interface SessionStoreLike {
17
+ create(): Session;
18
+ fork(source: Session, boundary?: undefined, childSessionId?: SessionId): Session;
19
+ }
20
+
21
+ async function setup() {
22
+ const ctx = new Context();
23
+ await ctx.plugin(SessionStore);
24
+ await ctx.plugin(SessionProjectionRegistry);
25
+ const sessions = ctx.sessions as unknown as SessionStoreLike;
26
+ const session = sessions.create();
27
+ const journal = createDshFlowJournal(ctx);
28
+ await new Promise((resolve) => setTimeout(resolve, 0));
29
+ return { ctx, sessions, session, journal };
30
+ }
31
+
32
+ describe('dsh flow journal', () => {
33
+ it('★ 插件记录能写能读,turn/start 不清空', async () => {
34
+ const { ctx, session, journal } = await setup();
35
+ journal.write(session.id, snapshot);
36
+ expect(journal.read(session.id)).toEqual(snapshot);
37
+
38
+ session.append('turn/start', { turn: 2 });
39
+ expect(journal.read(session.id)).toEqual(snapshot);
40
+ await ctx.fiber.dispose();
41
+ });
42
+
43
+ it('★ 没有记录的会话读回 null', async () => {
44
+ const { ctx, session, journal } = await setup();
45
+ expect(journal.read(session.id)).toBeNull();
46
+ await ctx.fiber.dispose();
47
+ });
48
+
49
+ it('★ fork 出来的会话带上同一条流程记录', async () => {
50
+ const { ctx, sessions, session, journal } = await setup();
51
+ journal.write(session.id, snapshot);
52
+ const child = sessions.fork(session, undefined, 'child' as SessionId);
53
+ expect(journal.read(child.id)).toEqual(snapshot);
54
+ await ctx.fiber.dispose();
55
+ });
56
+ });
@@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest';
2
2
  import type { DshToolRunContext } from '../../../api/dsh.js';
3
3
  import { createFlowHost } from '../kit.js';
4
4
  import { createFlowTask } from '../create.js';
5
- import { createLongTaskHost } from '../../unit/long-task.js';
5
+ import { createLongTaskHost } from '../../unit/long-task-host.js';
6
6
 
7
7
  const tick = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0));
8
8
 
@@ -49,6 +49,46 @@ describe('createFlowTask:流程 + 长任务 + 工具的三件套', () => {
49
49
  longTasks.dispose();
50
50
  });
51
51
 
52
+
53
+ it('★★ 流程任务自带进度:定义几步就是几,条件步跳过也算走过,跑完补齐', async () => {
54
+ const { flowHost, longTasks } = hostOf();
55
+ const actions = flowHost.createActions();
56
+ actions.register('one', async () => 'a', { title: '第一步' });
57
+ actions.register('two', async () => 'b', { title: '第二步' });
58
+ const task = createFlowTask({
59
+ key: 'progress',
60
+ flow: {
61
+ id: 'progress',
62
+ title: '三步',
63
+ steps: [
64
+ { id: 'one', title: '第一步', action: 'one' },
65
+ { id: 'skip', title: '条件步', action: 'one', when: { $eq: [{ $lit: 1 }, { $lit: 2 }] } },
66
+ { id: 'two', title: '第二步', action: 'two' },
67
+ ],
68
+ },
69
+ actions,
70
+ flowHost,
71
+ longTasks: longTasks.host,
72
+ tool: { name: 'run', description: '跑' },
73
+ });
74
+ const seen: Array<{ done: number; total: number }> = [];
75
+ task.runs.onChange((change) => {
76
+ const progress = change.snapshot.progress;
77
+ if (change.kind === 'progress' && progress !== undefined) {
78
+ seen.push({ done: progress.done, total: progress.total });
79
+ }
80
+ });
81
+
82
+ const started = task.start({ workspace: '/w/a' });
83
+ await tick();
84
+
85
+ expect(started.progress).toEqual({ done: 0, total: 3 });
86
+ expect(task.runs.snapshot(started.id)?.plan?.map((one) => one.id)).toEqual(['one', 'skip', 'two']);
87
+ expect(seen.some((one) => one.done === 1)).toBe(true);
88
+ expect(task.runs.snapshot(started.id)).toMatchObject({ status: 'ok', progress: { done: 3, total: 3 } });
89
+ longTasks.dispose();
90
+ });
91
+
52
92
  it('★★ 流程过不了 check 就不建注册表,错误里带步骤路径', () => {
53
93
  const { flowHost, longTasks } = hostOf();
54
94
  const actions = flowHost.createActions();
@@ -76,6 +76,38 @@ describe('open:引擎不执行步骤,由外部逐步推进', () => {
76
76
  expect(session.provide('a', 1).ok).toBe(false);
77
77
  });
78
78
 
79
+ it('★ 从某一步恢复:前面的值算已完成,当前步继续等结果', async () => {
80
+ const session = createFlowHost().open({
81
+ flow,
82
+ input: {},
83
+ resume: { stepId: 'b', values: { a: { go: true } } },
84
+ onStep: () => {},
85
+ });
86
+
87
+ expect(session.current()).toEqual({ index: 1, stepId: 'b', title: 'B' });
88
+ expect(session.provide('b', 2).ok).toBe(true);
89
+ await tick();
90
+ session.provide('c', 3);
91
+
92
+ const result = await session.result;
93
+ expect(result.status).toBe('ok');
94
+ expect(result.steps).toEqual({ a: { go: true }, b: 2, c: 3 });
95
+ });
96
+
97
+ it('★ 恢复点不在定义里:直接 failed,不从头跑', async () => {
98
+ const session = createFlowHost().open({
99
+ flow,
100
+ input: {},
101
+ resume: { stepId: 'missing', values: {} },
102
+ onStep: () => {},
103
+ });
104
+
105
+ expect(session.current()).toBeNull();
106
+ const result = await session.result;
107
+ expect(result.status).toBe('failed');
108
+ expect(result.failedStep).toBe('missing');
109
+ });
110
+
79
111
  it('★ 外部推进的流程不许带执行体,校验点名那一步', () => {
80
112
  const host = createFlowHost();
81
113
  const withAction: FlowDefinition = {
@@ -1,11 +1,8 @@
1
1
  import type { ToolDefinition, ToolRunContext } from '../../contract/module-context.js';
2
2
  import type { FlowHost } from './kit.js';
3
3
  import type { FlowActionRegistry, FlowDefinition } from './types.js';
4
- import type {
5
- LongTaskBase,
6
- LongTaskHost,
7
- LongTaskRegistry,
8
- } from '../unit/long-task.js';
4
+ import type { LongTaskBase, LongTaskRegistry } from '../unit/long-task.js';
5
+ import type { LongTaskHost } from '../unit/long-task-host.js';
9
6
  import { flowTaskDeps, startFlow } from './task.js';
10
7
  import { flowTool } from './tool.js';
11
8
 
@@ -53,6 +50,12 @@ export function createFlowTask(options: CreateFlowTaskOptions): FlowTask {
53
50
 
54
51
  const runs = options.longTasks.create<LongTaskBase>({
55
52
  key: options.key,
53
+ plan: {
54
+ steps: options.flow.steps.map((step) => ({
55
+ id: step.id,
56
+ ...(step.title === undefined ? {} : { title: step.title }),
57
+ })),
58
+ },
56
59
  ...flowTaskDeps(options.actions, options.flowHost),
57
60
  });
58
61
 
@@ -0,0 +1,74 @@
1
+ import { appendPluginRecord } from '@deepseek-ai/dsh-session';
2
+ import type { DshHostContext, DshSession, DshSessionId, DshSessionStore } from '../../api/dsh.js';
3
+ import { SESSION_PROJECTIONS_SERVICE, SESSION_STORE_SERVICE } from '../../config/constants.js';
4
+ import {
5
+ DIY_FLOW_PROJECTION,
6
+ DIY_FLOW_RECORD,
7
+ parseDiyFlowSnapshot,
8
+ type DiyFlowJournal,
9
+ type DiyFlowSnapshot,
10
+ } from './journal.js';
11
+
12
+ interface ProjectionHandle {
13
+ stateOf(session: DshSession): DiyFlowSnapshot | null;
14
+ dispose(): Promise<void>;
15
+ }
16
+
17
+ interface ProjectionRegistry {
18
+ register(definition: unknown): Promise<ProjectionHandle>;
19
+ }
20
+
21
+ const snapshotSchema = {
22
+ parse: (value: unknown): DiyFlowSnapshot | null => parseDiyFlowSnapshot(value),
23
+ };
24
+
25
+ export function createDshFlowJournal(ctx: DshHostContext): DiyFlowJournal {
26
+ const projections = ctx.get(SESSION_PROJECTIONS_SERVICE) as ProjectionRegistry | undefined;
27
+ const sessions = ctx.get(SESSION_STORE_SERVICE) as DshSessionStore | undefined;
28
+ if (projections === undefined || sessions === undefined) {
29
+ return { read: () => null, write: () => {} };
30
+ }
31
+
32
+ let handle: ProjectionHandle | undefined;
33
+ void projections
34
+ .register({
35
+ key: DIY_FLOW_PROJECTION,
36
+ stateSchema: snapshotSchema as never,
37
+ init: () => null,
38
+ apply: (state: DiyFlowSnapshot | null, event: { type: string; data: unknown }) => {
39
+ if (event.type !== DIY_FLOW_RECORD) return state;
40
+ const parsed = parseDiyFlowSnapshot(event.data);
41
+ return parsed ?? state;
42
+ },
43
+ stateVersion: 1,
44
+ })
45
+ .then((next) => {
46
+ handle = next;
47
+ })
48
+ .catch((error: unknown) => {
49
+ ctx.logger?.warn?.(`[dsh-workbench] 注册流程状态 projection 失败:${String(error)}`);
50
+ });
51
+
52
+ ctx.effect?.(() => () => {
53
+ const now = handle;
54
+ handle = undefined;
55
+ if (now !== undefined) void now.dispose();
56
+ }, 'dsh-workbench: 流程状态 projection 随宿主卸载');
57
+
58
+ const sessionOf = (session: string): DshSession | undefined =>
59
+ sessions.get(session as DshSessionId);
60
+
61
+ return {
62
+ read(session) {
63
+ const handleNow = handle;
64
+ const current = sessionOf(session);
65
+ if (handleNow === undefined || current === undefined) return null;
66
+ return handleNow.stateOf(current);
67
+ },
68
+ write(session, snapshot) {
69
+ const current = sessionOf(session);
70
+ if (current === undefined) return;
71
+ appendPluginRecord(current, DIY_FLOW_RECORD, snapshot);
72
+ },
73
+ };
74
+ }
@@ -0,0 +1,46 @@
1
+ export const DIY_FLOW_RECORD = 'plugin:workbench/diy-flow' as const;
2
+ export const DIY_FLOW_PROJECTION = 'workbenchDiyFlow' as const;
3
+
4
+ export interface DiyFlowSnapshot {
5
+ readonly version: 1;
6
+ readonly flow: string;
7
+ readonly input: Readonly<Record<string, unknown>>;
8
+ readonly step: string | null;
9
+ readonly values: Readonly<Record<string, unknown>>;
10
+ readonly status: 'running' | 'done' | 'stopped';
11
+ }
12
+
13
+ export interface DiyFlowJournal {
14
+ read(session: string): DiyFlowSnapshot | null;
15
+ write(session: string, snapshot: DiyFlowSnapshot): void;
16
+ }
17
+
18
+ function isRecord(value: unknown): value is Record<string, unknown> {
19
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
20
+ }
21
+
22
+ export function parseDiyFlowSnapshot(value: unknown): DiyFlowSnapshot | null {
23
+ if (!isRecord(value)) return null;
24
+ if (value['version'] !== 1) return null;
25
+ const flow = value['flow'];
26
+ const step = value['step'];
27
+ const status = value['status'];
28
+ if (typeof flow !== 'string' || flow === '') return null;
29
+ if (step !== null && (typeof step !== 'string' || step === '')) return null;
30
+ if (status !== 'running' && status !== 'done' && status !== 'stopped') return null;
31
+ if (!isRecord(value['input']) || !isRecord(value['values'])) return null;
32
+ return {
33
+ version: 1,
34
+ flow,
35
+ input: value['input'],
36
+ step,
37
+ values: value['values'],
38
+ status,
39
+ };
40
+ }
41
+
42
+ declare module '@deepseek-ai/dsh-session' {
43
+ interface PluginRecordMap {
44
+ 'plugin:workbench/diy-flow': DiyFlowSnapshot;
45
+ }
46
+ }