dsh-mobile-plugin 0.5.46 → 0.5.48

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.
@@ -0,0 +1,194 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 工作流运行记录:把宿主的四条 `tool-workflow/*` 事件折成一张**树**交给手机。
5
+ *
6
+ * 桌面那边(`@deepseek-ai/dsh-client-ui-workflow-run` 的 `workflowRunDefinition`)把同一族事件
7
+ * 折成一个 `workflow-run` 节点,形状是**运行 → 阶段 → 成员**三层,不是一张平铺的步骤清单。
8
+ * 手机照这个形状下发,渲染层才画得出「哪个阶段的哪几个成员还在跑」。
9
+ *
10
+ * 四条事件(`packages/workflow/tool-workflow/src/types.ts`):
11
+ * run-start { runId, name }
12
+ * agent-start { runId, seq, label, phase?, childId }
13
+ * agent-end { runId, seq, outcome: completed | failed | cancelled }
14
+ * run-end { runId, stopReason: completed | error | cancelled }
15
+ *
16
+ * 三条口径与桌面逐条对齐(改之前先读 `workflow-definition.ts` 的 `projectWorkflow`):
17
+ *
18
+ * ① **状态只认「落定」与「被放弃」两种证据**。成员的 `outcome` 到了才算它结束;整条运行的
19
+ * `stopReason` 到了才算这条运行结束。都没有的时候不能凭「这一步的工具调用结束了」判活 ——
20
+ * 后台跑的工作流能活过开启它的那一步,前台跑的那条会先写 run-end 再结束工具调用。
21
+ * 唯一的例外是**这一轮被中断 / 分叉**(`turn.end.reason` 是 `interrupted` / `forked`):
22
+ * 那种情况下运行记录再也不会长,没写 run-end 就是被撇下了,落 `interrupted`。
23
+ * ② **阶段是一个独立的身份**:`phase` 字段没给(`undefined`)与给了空串是两回事,前者是
24
+ * 「未分阶段」一档,后者是一档名叫空的阶段 —— 桌面用 `workflowPhaseKey` 把两者分开,
25
+ * 这里同样用键区分,别用 `phase || '未分阶段'` 把它们并成一组。
26
+ * ③ **成员按 `seq` 认,不按标签**:同名成员可以并存(同一个 agent 跑两次),标签是给人看的。
27
+ *
28
+ * 这一层只管形状,不碰 ctx、不发 RPC —— 与 `history-derive.js` 同一分工,守卫能裸 Node 跑。
29
+ */
30
+
31
+ /** 成员行的状态:还在跑 / 已完成 / 失败 / 已取消 / 被撇下。 */
32
+ const MEMBER_RUNNING = 'running';
33
+ const MEMBER_INTERRUPTED = 'interrupted';
34
+
35
+ /** 一条运行的终态:`stopReason` 的字面量 → 手机这一层认的词。 */
36
+ const RUN_STATUS_FROM_STOP = {
37
+ completed: 'completed',
38
+ error: 'failed',
39
+ cancelled: 'cancelled',
40
+ };
41
+
42
+ /** 一个成员的 outcome → 手机这一层认的词。 */
43
+ const MEMBER_STATUS_FROM_OUTCOME = {
44
+ completed: 'completed',
45
+ failed: 'failed',
46
+ cancelled: 'cancelled',
47
+ };
48
+
49
+ /**
50
+ * 阶段键:把「缺省」与「空串」分开,同时避免任何 phase 名字撞上这两档。
51
+ *
52
+ * 与桌面 `workflowPhaseKey` 同一条口径(`'missing'` / `value:<长度>:<名字>`)—— 长度前缀
53
+ * 让 `value:1:a` 这种名字不会被当成另一组,也就不用去转义。
54
+ */
55
+ function phaseKeyOf(phase) {
56
+ if (phase === undefined) return 'missing';
57
+ return `value:${phase.length}:${phase}`;
58
+ }
59
+
60
+ /**
61
+ * 从一批会话事件里折出这次调用要下发的工作流行。
62
+ *
63
+ * @param {ReadonlyArray<{event?: object}>} entries 会话事件(history 那条路一个窗口,
64
+ * 直播那条路是这次进来的事件本身 —— 两边形状一致,都是 `{ event }` 或裸事件)。
65
+ * @param {object} [options]
66
+ * @param {boolean} [options.interrupted] 这些运行全部按「被撇下」处理(见口径 ①)。直播间那条路
67
+ * 只在**当前这一轮**被中断时用,所以它是整批的。
68
+ * @param {Iterable<string>} [options.abandonedRunIds] 逐条指定哪些 runId 被撇下。历史那条路
69
+ * 一个窗口里可能横跨好几轮,只有「开启它的那一轮被中断」的那几条才算,所以按 id 给。
70
+ * @returns {Array<object>} 每一行是一个 `ChatMessage`:`{ id, kind: 'workflow', workflow }`,
71
+ * 按 run-start 的出现位置排序,`seq` 取那一刻的事件位置。**同一 runId 只出一行**,
72
+ * 后面的事件都并进那一行(桌面也是「一条运行一个节点」)。
73
+ */
74
+ function deriveWorkflowRuns(entries, options) {
75
+ const interrupted = Boolean(options && options.interrupted);
76
+ /** runId → { index, run }:index 是它在 out 里的位置,run 是正在攒的那份。 */
77
+ const byRun = new Map();
78
+ const out = [];
79
+ // 成员按 seq 认:runId + seq。同一个 seq 被两次 agent-start 认领属于畸形数据,
80
+ // 后到的直接覆盖(桌面也是 Map 语义,不叠成两条)。
81
+ for (const entry of entries || []) {
82
+ const e = entry && entry.event ? entry.event : entry;
83
+ if (!e || typeof e.type !== 'string') continue;
84
+ if (e.type !== 'tool-workflow/run-start'
85
+ && e.type !== 'tool-workflow/agent-start'
86
+ && e.type !== 'tool-workflow/agent-end'
87
+ && e.type !== 'tool-workflow/run-end') continue;
88
+ const data = e.data;
89
+ if (!data || typeof data !== 'object') continue;
90
+ const runId = data.runId === undefined || data.runId === null ? '' : String(data.runId);
91
+ if (runId === '') continue;
92
+
93
+ if (e.type === 'tool-workflow/run-start') {
94
+ // 同一条运行重开(罕见,但宿主不保证)就地换掉那一行,不要多出一张卡。
95
+ const run = {
96
+ id: runId,
97
+ name: typeof data.name === 'string' && data.name !== '' ? data.name : '',
98
+ status: MEMBER_RUNNING,
99
+ phases: [],
100
+ };
101
+ const existing = byRun.get(runId);
102
+ if (existing) {
103
+ out[existing.index] = { id: `workflow-${runId}`, kind: 'workflow', workflow: run, seq: e.seq, time: e.time };
104
+ } else {
105
+ byRun.set(runId, { index: out.length, run });
106
+ out.push({ id: `workflow-${runId}`, kind: 'workflow', workflow: run, seq: e.seq, time: e.time });
107
+ }
108
+ continue;
109
+ }
110
+
111
+ const holder = byRun.get(runId);
112
+ // 没收到 run-start 就来的成员 / 结束事件:窗口截断(拉历史只回最近几十条)——
113
+ // 这时**不无中生有造一条运行**,文件名与阶段都缺,画出来是张空卡。
114
+ if (!holder) continue;
115
+ const run = holder.run;
116
+
117
+ if (e.type === 'tool-workflow/agent-start') {
118
+ const seq = typeof data.seq === 'number' ? data.seq : null;
119
+ if (seq === null) continue;
120
+ const rawPhase = data.phase === undefined ? undefined : (typeof data.phase === 'string' ? data.phase : '');
121
+ const key = phaseKeyOf(rawPhase);
122
+ let group = run.phases.find((item) => item.key === key);
123
+ if (!group) {
124
+ group = { key, phase: rawPhase === undefined ? null : rawPhase, members: [] };
125
+ run.phases.push(group);
126
+ }
127
+ const member = {
128
+ seq,
129
+ label: typeof data.label === 'string' ? data.label : '',
130
+ status: MEMBER_RUNNING,
131
+ // childId 落 String:App 拿它 push 子会话,类型上必须是串。
132
+ childId: data.childId === undefined || data.childId === null ? '' : String(data.childId),
133
+ };
134
+ const at = group.members.findIndex((item) => item.seq === seq);
135
+ if (at === -1) group.members.push(member);
136
+ else group.members[at] = member;
137
+ continue;
138
+ }
139
+
140
+ if (e.type === 'tool-workflow/agent-end') {
141
+ const seq = typeof data.seq === 'number' ? data.seq : null;
142
+ if (seq === null) continue;
143
+ const status = MEMBER_STATUS_FROM_OUTCOME[data.outcome] || MEMBER_RUNNING;
144
+ for (const group of run.phases) {
145
+ const member = group.members.find((item) => item.seq === seq);
146
+ if (member) {
147
+ member.status = status;
148
+ break;
149
+ }
150
+ }
151
+ continue;
152
+ }
153
+
154
+ // run-end
155
+ run.status = RUN_STATUS_FROM_STOP[data.stopReason] || MEMBER_RUNNING;
156
+ }
157
+
158
+ // 被撇下的那一档(口径 ①):整条运行没有 run-end、又有中断证据时,运行与它下面还在
159
+ // 「running」的成员一起落 interrupted。已经落定的成员不动 —— 它们真跑完了。
160
+ const abandoned = options && options.abandonedRunIds ? new Set(options.abandonedRunIds) : null;
161
+ if (interrupted || (abandoned && abandoned.size > 0)) {
162
+ for (const item of out) {
163
+ const run = item.workflow;
164
+ if (run.status !== MEMBER_RUNNING) continue;
165
+ const dropped = abandoned === null ? interrupted : abandoned.has(run.id);
166
+ if (!dropped) continue;
167
+ run.status = MEMBER_INTERRUPTED;
168
+ for (const group of run.phases) {
169
+ for (const member of group.members) {
170
+ if (member.status === MEMBER_RUNNING) member.status = MEMBER_INTERRUPTED;
171
+ }
172
+ }
173
+ }
174
+ }
175
+
176
+ return out;
177
+ }
178
+
179
+ /**
180
+ * 一条运行里所有成员(先按阶段顺序、再按 seq)。渲染层数「几个成员 / 几个还在跑」用它。
181
+ */
182
+ function workflowMembers(run) {
183
+ const members = [];
184
+ for (const group of (run && run.phases) || []) {
185
+ for (const member of group.members || []) members.push(member);
186
+ }
187
+ return members;
188
+ }
189
+
190
+ module.exports = {
191
+ deriveWorkflowRuns,
192
+ workflowMembers,
193
+ phaseKeyOf,
194
+ };