@joplo-app/core 0.5.0 → 0.6.1
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.
- package/dist/agent-skill.d.ts +7 -7
- package/dist/agent-skill.js +29 -25
- package/dist/authoring-works.d.ts +8 -0
- package/dist/authoring-works.js +46 -0
- package/dist/catalog.d.ts +2 -0
- package/dist/compile.d.ts +6 -3
- package/dist/compile.js +415 -340
- package/dist/content-i18n.js +9 -9
- package/dist/embed.d.ts +5 -5
- package/dist/embed.js +7 -7
- package/dist/entries.d.ts +63 -0
- package/dist/entries.js +78 -0
- package/dist/feature-diff.d.ts +7 -7
- package/dist/feature-diff.js +18 -17
- package/dist/features.d.ts +3 -3
- package/dist/features.js +3 -3
- package/dist/focus-check.js +2 -2
- package/dist/focus.d.ts +1 -5
- package/dist/focus.js +13 -35
- package/dist/guide.js +157 -67
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/load.js +42 -11
- package/dist/local-review-files.js +17 -4
- package/dist/local-review.d.ts +8 -7
- package/dist/local-review.js +7 -6
- package/dist/outline.d.ts +21 -13
- package/dist/outline.js +27 -32
- package/dist/pace.d.ts +3 -4
- package/dist/pace.js +3 -4
- package/dist/playback.d.ts +57 -0
- package/dist/playback.js +317 -0
- package/dist/player.d.ts +9 -195
- package/dist/player.js +8 -633
- package/dist/playground.d.ts +1 -1
- package/dist/playground.js +4 -4
- package/dist/plugins/compiler.d.ts +16 -0
- package/dist/plugins/compiler.js +48 -9
- package/dist/presentation.d.ts +73 -0
- package/dist/presentation.js +163 -0
- package/dist/publication.d.ts +8 -0
- package/dist/publication.js +45 -0
- package/dist/result.d.ts +1 -1
- package/dist/review.d.ts +18 -10
- package/dist/review.js +62 -23
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.js +2 -0
- package/dist/scene-query.d.ts +77 -0
- package/dist/scene-query.js +246 -0
- package/dist/schema-issues.js +1 -1
- package/dist/schema.d.ts +108 -76
- package/dist/schema.js +48 -36
- package/dist/scope.d.ts +9 -9
- package/dist/scope.js +82 -49
- package/dist/selection.d.ts +1 -1
- package/dist/sequencing.d.ts +6 -0
- package/dist/sequencing.js +21 -0
- package/dist/step-plan.d.ts +55 -0
- package/dist/step-plan.js +90 -0
- package/dist/traits.d.ts +10 -0
- package/dist/traits.js +3 -0
- package/dist/validate.d.ts +1 -1
- package/dist/validate.js +75 -65
- package/dist/voice.js +2 -1
- package/package.json +15 -3
package/dist/player.js
CHANGED
|
@@ -1,635 +1,10 @@
|
|
|
1
|
-
import { pluginActionSpec, validActionData } from "./actions.js";
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
2
|
+
* 播放器的出口(为导入方保留一个名字),实现分三个模块:
|
|
3
|
+
* - scene-query.ts:画面查询——查找元素、取属性的值、可见与禁用、选项与分栏、层级;
|
|
4
|
+
* - playback.ts:回放——状态转换(applyStep)、时间线缓存(stateAt)、回放校验、解释一次点击;
|
|
5
|
+
* - presentation.ts:演示投影——镜头、过渡卡、角色、流式输出、分叉、标注、状态变化。
|
|
6
|
+
* 依赖方向:scene-query ← playback ← presentation。
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
// ---------- 查找 ----------
|
|
13
|
-
export function getScenario(project, scenarioId) {
|
|
14
|
-
const sc = project.scenarios[scenarioId];
|
|
15
|
-
if (!sc)
|
|
16
|
-
throw new JoploError('NOT_FOUND', `Scenario does not exist: ${scenarioId}`);
|
|
17
|
-
return sc;
|
|
18
|
-
}
|
|
19
|
-
export function getScene(project, sceneId) {
|
|
20
|
-
const s = project.scenes[sceneId];
|
|
21
|
-
if (!s)
|
|
22
|
-
throw new JoploError('NOT_FOUND', `Stage does not exist: ${sceneId}`);
|
|
23
|
-
return s;
|
|
24
|
-
}
|
|
25
|
-
export function walkNodes(root, visit) {
|
|
26
|
-
const rec = (n, anc) => {
|
|
27
|
-
visit(n, anc);
|
|
28
|
-
const next = [...anc, n];
|
|
29
|
-
for (const c of n.children ?? [])
|
|
30
|
-
rec(c, next);
|
|
31
|
-
};
|
|
32
|
-
rec(root, []);
|
|
33
|
-
}
|
|
34
|
-
/** 返回节点及其祖先链(自根向下,不含自身);找不到返回 null。 */
|
|
35
|
-
export function findNode(scene, nodeId) {
|
|
36
|
-
let found = null;
|
|
37
|
-
walkNodes(scene.root, (node, ancestors) => {
|
|
38
|
-
if (!found && node.id === nodeId)
|
|
39
|
-
found = { node, ancestors };
|
|
40
|
-
});
|
|
41
|
-
return found;
|
|
42
|
-
}
|
|
43
|
-
// ---------- 状态读取与条件 ----------
|
|
44
|
-
export function readPath(state, path) {
|
|
45
|
-
const [root, ...rest] = path.split('.');
|
|
46
|
-
let cur = root === 'ui' ? state.ui : root === 'data' ? state.data : root === 'inputs' ? state.inputs : undefined;
|
|
47
|
-
for (const key of rest) {
|
|
48
|
-
if (cur === null || typeof cur !== 'object')
|
|
49
|
-
return undefined;
|
|
50
|
-
cur = cur[key];
|
|
51
|
-
}
|
|
52
|
-
return cur;
|
|
53
|
-
}
|
|
54
|
-
const BINDING = /^\$((?:ui|data|inputs)(?:\.[A-Za-z0-9_-]+)*)$/;
|
|
55
|
-
/**
|
|
56
|
-
* 属性值若是 `$ui.x` 形式的整值绑定,则从状态读取;对象与数组逐项解析
|
|
57
|
-
* (例如 declared element 的 navbar={{ collapsed: { mobile: $.ui.navClosed } }});其余原样返回。
|
|
58
|
-
*/
|
|
59
|
-
export function resolveValue(value, state) {
|
|
60
|
-
if (typeof value === 'string') {
|
|
61
|
-
const m = BINDING.exec(value);
|
|
62
|
-
if (m)
|
|
63
|
-
return readPath(state, m[1]);
|
|
64
|
-
return value;
|
|
65
|
-
}
|
|
66
|
-
if (Array.isArray(value))
|
|
67
|
-
return value.some(hasBinding) ? value.map((v) => resolveValue(v, state)) : value;
|
|
68
|
-
if (value && typeof value === 'object') {
|
|
69
|
-
if (!hasBinding(value))
|
|
70
|
-
return value;
|
|
71
|
-
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, resolveValue(v, state)]));
|
|
72
|
-
}
|
|
73
|
-
return value;
|
|
74
|
-
}
|
|
75
|
-
function hasBinding(v) {
|
|
76
|
-
if (typeof v === 'string')
|
|
77
|
-
return BINDING.test(v);
|
|
78
|
-
if (Array.isArray(v))
|
|
79
|
-
return v.some(hasBinding);
|
|
80
|
-
if (v && typeof v === 'object')
|
|
81
|
-
return Object.values(v).some(hasBinding);
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
export function resolveProps(node, state) {
|
|
85
|
-
const out = {};
|
|
86
|
-
for (const [k, v] of Object.entries(node.props ?? {}))
|
|
87
|
-
out[k] = resolveValue(v, state);
|
|
88
|
-
return out;
|
|
89
|
-
}
|
|
90
|
-
const isEmpty = (v) => v === undefined || v === null || v === '' || (Array.isArray(v) && v.length === 0) || (typeof v === 'object' && v !== null && !Array.isArray(v) && Object.keys(v).length === 0);
|
|
91
|
-
export function evaluateCondition(cond, state) {
|
|
92
|
-
if ('truthy' in cond)
|
|
93
|
-
return Boolean(readPath(state, cond.truthy));
|
|
94
|
-
if ('falsy' in cond)
|
|
95
|
-
return !readPath(state, cond.falsy);
|
|
96
|
-
if ('empty' in cond)
|
|
97
|
-
return isEmpty(readPath(state, cond.empty));
|
|
98
|
-
if ('notEmpty' in cond)
|
|
99
|
-
return !isEmpty(readPath(state, cond.notEmpty));
|
|
100
|
-
if ('eq' in cond)
|
|
101
|
-
return readPath(state, cond.eq[0]) === cond.eq[1];
|
|
102
|
-
if ('neq' in cond)
|
|
103
|
-
return readPath(state, cond.neq[0]) !== cond.neq[1];
|
|
104
|
-
if ('in' in cond)
|
|
105
|
-
return cond.in[1].includes(readPath(state, cond.in[0]));
|
|
106
|
-
if ('and' in cond)
|
|
107
|
-
return cond.and.every((c) => evaluateCondition(c, state));
|
|
108
|
-
if ('or' in cond)
|
|
109
|
-
return cond.or.some((c) => evaluateCondition(c, state));
|
|
110
|
-
if ('not' in cond)
|
|
111
|
-
return !evaluateCondition(cond.not, state);
|
|
112
|
-
return false;
|
|
113
|
-
}
|
|
114
|
-
/** 画面的舞台宽度(由页面的 viewport 决定)。 */
|
|
115
|
-
export function sceneWidth(scene) {
|
|
116
|
-
return VIEWPORT_SIZE[scene.viewport === 'mobile' ? 'mobile' : 'desktop'].width;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* 节点自身是否显示:when、declared element 的 opened(兼容旧写法 open),
|
|
120
|
-
* 以及给定舞台宽度时的 visibleFrom / hiddenFrom。
|
|
121
|
-
*/
|
|
122
|
-
export function isSelfVisible(node, state, width) {
|
|
123
|
-
if (node.when && !evaluateCondition(node.when, state))
|
|
124
|
-
return false;
|
|
125
|
-
if (node.traits?.gate) {
|
|
126
|
-
const opened = node.traits.gate.map(key => node.props?.[key]).find(v => v !== undefined);
|
|
127
|
-
if (!resolveValue(opened, state))
|
|
128
|
-
return false;
|
|
129
|
-
}
|
|
130
|
-
if (width !== undefined && hiddenAtWidth(node.props, width))
|
|
131
|
-
return false;
|
|
132
|
-
return true;
|
|
133
|
-
}
|
|
134
|
-
/** Declarative layout regions share the same width/state interpretation in drawing and playback. */
|
|
135
|
-
export function layoutRegions(node, state, width) {
|
|
136
|
-
const regions = {};
|
|
137
|
-
for (const [key, spec] of Object.entries(node.traits?.layout ?? {})) {
|
|
138
|
-
const value = resolveValue(atWidth(node.props?.[key], width), state);
|
|
139
|
-
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
140
|
-
regions[key] = null;
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
const props = value;
|
|
144
|
-
const bp = spec.breakpoint ? breakpointPx(props.breakpoint) : null;
|
|
145
|
-
const mobile = bp !== null && width < bp;
|
|
146
|
-
const collapsed = resolveValue(props.collapsed, state);
|
|
147
|
-
const config = collapsed && typeof collapsed === 'object' ? collapsed : {};
|
|
148
|
-
const rawSize = atWidth(resolveValue(props[spec.size], state), width);
|
|
149
|
-
const size = typeof rawSize === 'number' && Number.isFinite(rawSize) ? rawSize : typeof rawSize === 'string' && /^\d+(px)?$/.test(rawSize) ? Number.parseInt(rawSize, 10) : spec.defaultSize;
|
|
150
|
-
regions[key] = { size, mobile, collapsed: spec.breakpoint ? (mobile ? config.mobile === undefined || Boolean(config.mobile) : Boolean(config.desktop)) : Boolean(collapsed) };
|
|
151
|
-
}
|
|
152
|
-
return regions;
|
|
153
|
-
}
|
|
154
|
-
/** 节点在画面中是否可见:自身与所有祖先都可见;按画面宽度计算断点与 declared element 收起。 */
|
|
155
|
-
export function isNodeVisible(scene, nodeId, state, width = sceneWidth(scene)) {
|
|
156
|
-
const hit = findNode(scene, nodeId);
|
|
157
|
-
if (!hit)
|
|
158
|
-
return false;
|
|
159
|
-
let shell = null;
|
|
160
|
-
let shellWidth = width;
|
|
161
|
-
let w = width;
|
|
162
|
-
const chain = [...hit.ancestors, hit.node];
|
|
163
|
-
for (const [index, n] of chain.entries()) {
|
|
164
|
-
if (!isSelfVisible(n, state, w))
|
|
165
|
-
return false;
|
|
166
|
-
const gate = n.traits?.selectionGate;
|
|
167
|
-
if (gate) {
|
|
168
|
-
const ancestors = chain.slice(0, index).reverse();
|
|
169
|
-
const owner = ancestors.find(a => a.type === gate.owner);
|
|
170
|
-
const item = gate.item ? ancestors.find(a => a.type === gate.item) : n;
|
|
171
|
-
if (!owner || !item)
|
|
172
|
-
return false;
|
|
173
|
-
let selected = state.inputs[owner.id] !== undefined ? state.inputs[owner.id] : resolveValue(owner.props?.value ?? owner.props?.defaultValue, state);
|
|
174
|
-
if (gate.fallbackFirst && (selected === undefined || selected === null || selected === ''))
|
|
175
|
-
selected = choicesOf(owner, state)?.[0]?.value;
|
|
176
|
-
const value = String(resolveValue(item.props?.[gate.value], state) ?? '');
|
|
177
|
-
if (Array.isArray(selected) ? !selected.map(String).includes(value) : selected === undefined || selected === null || String(selected) !== value)
|
|
178
|
-
return false;
|
|
179
|
-
}
|
|
180
|
-
if (n.traits?.layout) {
|
|
181
|
-
shell = n;
|
|
182
|
-
shellWidth = w;
|
|
183
|
-
}
|
|
184
|
-
if (shell && n.traits?.region) {
|
|
185
|
-
const r = layoutRegions(shell, state, shellWidth)[n.traits.region];
|
|
186
|
-
if (r?.collapsed)
|
|
187
|
-
return false;
|
|
188
|
-
}
|
|
189
|
-
w = childWidth(n, w, resolveProps(n, state));
|
|
190
|
-
}
|
|
191
|
-
return true;
|
|
192
|
-
}
|
|
193
|
-
/** Enumerate declared data, descendant values, or a numeric range. No declaration means unconstrained input. */
|
|
194
|
-
export function choicesOf(node, state) {
|
|
195
|
-
const spec = node.traits?.choices;
|
|
196
|
-
if (!spec)
|
|
197
|
-
return null;
|
|
198
|
-
const p = node.props ?? {};
|
|
199
|
-
if ('prop' in spec) {
|
|
200
|
-
const choices = choicesFromData(resolveValue(p[spec.prop], state));
|
|
201
|
-
return choices.length ? choices : null;
|
|
202
|
-
}
|
|
203
|
-
if ('range' in spec) {
|
|
204
|
-
const total = Number(resolveValue(p[spec.range], state));
|
|
205
|
-
return Number.isInteger(total) && total > 0 ? Array.from({ length: total }, (_, i) => ({ value: String(i + 1), label: String(i + 1) })) : null;
|
|
206
|
-
}
|
|
207
|
-
const text = (n) => n.type === '#text' ? String(resolveValue(n.props?.text, state) ?? '') : (n.children ?? []).map(text).join('');
|
|
208
|
-
const choices = [];
|
|
209
|
-
walkNodes(node, n => {
|
|
210
|
-
if (n === node || !spec.descendants.includes(n.type) || n.props?.[spec.value ?? 'value'] === undefined)
|
|
211
|
-
return;
|
|
212
|
-
const value = String(resolveValue(n.props[spec.value ?? 'value'], state));
|
|
213
|
-
const label = n.props[spec.label ?? 'label'];
|
|
214
|
-
const control = spec.labelChild ? n.children?.find(c => c.type === spec.labelChild) : n;
|
|
215
|
-
const display = typeof label === 'string' ? label : control ? text(control).trim() : '';
|
|
216
|
-
choices.push({ value, label: display || value });
|
|
217
|
-
});
|
|
218
|
-
return choices.length ? choices : null;
|
|
219
|
-
}
|
|
220
|
-
export function isNodeDisabled(node, state) {
|
|
221
|
-
if (node.disabled === undefined)
|
|
222
|
-
return false;
|
|
223
|
-
if (typeof node.disabled === 'boolean')
|
|
224
|
-
return node.disabled;
|
|
225
|
-
return evaluateCondition(node.disabled, state);
|
|
226
|
-
}
|
|
227
|
-
// ---------- 状态转换 ----------
|
|
228
|
-
export function initialState(project, scenarioId) {
|
|
229
|
-
const sc = getScenario(project, scenarioId);
|
|
230
|
-
getScene(project, sc.initial.sceneId);
|
|
231
|
-
return {
|
|
232
|
-
scenarioId,
|
|
233
|
-
position: 0,
|
|
234
|
-
sceneId: sc.initial.sceneId,
|
|
235
|
-
ui: structuredClone(sc.initial.ui),
|
|
236
|
-
data: structuredClone(sc.initial.data),
|
|
237
|
-
inputs: structuredClone(sc.initial.inputs),
|
|
238
|
-
toasts: [],
|
|
239
|
-
completed: null,
|
|
240
|
-
sceneStack: [],
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
export class PlaybackError extends JoploError {
|
|
244
|
-
constructor(message, diagnostic) {
|
|
245
|
-
super('PLAYBACK_FAILED', message, { diagnostics: [diagnostic] });
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
function setPath(state, path, value, mode) {
|
|
249
|
-
const [root, ...rest] = path.split('.');
|
|
250
|
-
const base = (root === 'ui' ? state.ui : root === 'data' ? state.data : state.inputs);
|
|
251
|
-
if (rest.length === 0)
|
|
252
|
-
throw new Error(`Cannot replace an entire state root: ${path}`);
|
|
253
|
-
let cur = base;
|
|
254
|
-
for (const key of rest.slice(0, -1)) {
|
|
255
|
-
const next = cur[key];
|
|
256
|
-
if (next === null || typeof next !== 'object' || Array.isArray(next))
|
|
257
|
-
cur[key] = {};
|
|
258
|
-
cur = cur[key];
|
|
259
|
-
}
|
|
260
|
-
const last = rest[rest.length - 1];
|
|
261
|
-
if (mode === 'set')
|
|
262
|
-
cur[last] = structuredClone(value);
|
|
263
|
-
else {
|
|
264
|
-
const arr = cur[last];
|
|
265
|
-
cur[last] = [...(Array.isArray(arr) ? arr : []), structuredClone(value)];
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* 校验当前步骤能否在给定状态下执行;返回诊断(null 表示可执行)。
|
|
270
|
-
* 手动体验时,UI 也用它判断用户点击的是不是“当前步骤的目标”。
|
|
271
|
-
*/
|
|
272
|
-
export function checkStep(project, scenario, state, step) {
|
|
273
|
-
const d = checkStepInner(project, scenario, state, step);
|
|
274
|
-
if (d && step.source)
|
|
275
|
-
d.at = step.source;
|
|
276
|
-
return d;
|
|
277
|
-
}
|
|
278
|
-
function checkStepInner(project, scenario, state, step) {
|
|
279
|
-
const ref = { kind: 'step', id: step.id, optionId: scenario.optionId, scenarioId: scenario.id, stepId: step.id };
|
|
280
|
-
if (state.completed)
|
|
281
|
-
return { severity: 'error', code: 'ALREADY_COMPLETED', message: `Scenario has ended; cannot execute step ${step.id}`, source: 'replay', ref };
|
|
282
|
-
const a = step.action;
|
|
283
|
-
if (a.type === 'event' || a.type === 'card' || a.type === 'press')
|
|
284
|
-
return null;
|
|
285
|
-
if (a.type === 'back') {
|
|
286
|
-
return state.sceneStack.length === 0
|
|
287
|
-
? { severity: 'error', code: 'NOTHING_TO_GO_BACK', message: `Step ${step.id} goes back, but there is no previous stage`, source: 'replay', ref }
|
|
288
|
-
: null;
|
|
289
|
-
}
|
|
290
|
-
const scene = sceneOfTarget(project, scenario, state, a.target);
|
|
291
|
-
if (!scene)
|
|
292
|
-
return { severity: 'error', code: 'MISSING_SCENE', message: `Current stage ${state.sceneId} does not exist`, source: 'replay', ref };
|
|
293
|
-
const hit = findNode(scene, a.target);
|
|
294
|
-
if (!hit)
|
|
295
|
-
return { severity: 'error', code: 'MISSING_TARGET', message: `Step ${step.id} target ${a.target} is not in the current stage ${scene.id}`, source: 'replay', ref };
|
|
296
|
-
const { node } = hit;
|
|
297
|
-
if (!isNodeVisible(scene, a.target, state))
|
|
298
|
-
return { severity: 'error', code: 'TARGET_HIDDEN', message: `Step ${step.id} target "${nodeLabel(node)}" is currently hidden`, source: 'replay', ref };
|
|
299
|
-
if (isNodeDisabled(node, state))
|
|
300
|
-
return { severity: 'error', code: 'TARGET_DISABLED', message: `Step ${step.id} target "${nodeLabel(node)}" is currently disabled`, source: 'replay', ref };
|
|
301
|
-
if (a.type === 'click' && !isInteractiveType(node.type, project.plugins))
|
|
302
|
-
return { severity: 'error', code: 'NOT_INTERACTIVE', message: `Step ${step.id} clicks ${node.type} "${nodeLabel(node)}", which is not interactive`, source: 'replay', ref };
|
|
303
|
-
if ((a.type === 'type' || a.type === 'select' || a.type === 'toggle') && !node.traits?.input)
|
|
304
|
-
return { severity: 'error', code: 'NOT_A_FIELD', message: `Step ${step.id} ${a.type} action target ${node.type} cannot accept input`, source: 'replay', ref };
|
|
305
|
-
if ((a.type === 'drag' || a.type === 'scroll') && typeof a.to === 'string' && !findNode(scene, a.to))
|
|
306
|
-
return { severity: 'error', code: 'ACTION_DESTINATION_MISSING', message: `Step ${step.id} destination ${a.to} is missing`, source: 'replay', ref };
|
|
307
|
-
if (a.type === 'scroll' && typeof a.to === 'string' && !findNode({ ...scene, root: node }, a.to))
|
|
308
|
-
return { severity: 'error', code: 'ACTION_DESTINATION_INVALID', message: `Step ${step.id} scroll destination ${a.to} is outside ${a.target}`, source: 'replay', ref };
|
|
309
|
-
if (a.type === 'plugin') {
|
|
310
|
-
const spec = pluginActionSpec(project, a.name);
|
|
311
|
-
if (!spec || !validActionData(spec, a.data) || spec.targets && !spec.targets.includes(node.type))
|
|
312
|
-
return { severity: 'error', code: 'ACTION_DATA_INVALID', message: `Step ${step.id} has an unknown or invalid plugin action ${a.name}`, source: 'replay', ref };
|
|
313
|
-
}
|
|
314
|
-
if (a.type === 'select') {
|
|
315
|
-
const choices = choicesOf(node, state);
|
|
316
|
-
if (choices && !matchChoice(choices, a.value))
|
|
317
|
-
return {
|
|
318
|
-
severity: 'error',
|
|
319
|
-
code: 'UNKNOWN_CHOICE',
|
|
320
|
-
message: `Step ${step.id} field "${nodeLabel(node)}" cannot select "${a.value}"; it is not an available option (available: ${choices.map((c) => (c.label === c.value ? c.value : `${c.label}/${c.value}`)).join(', ')})`,
|
|
321
|
-
source: 'replay',
|
|
322
|
-
ref,
|
|
323
|
-
};
|
|
324
|
-
}
|
|
325
|
-
return null;
|
|
326
|
-
}
|
|
327
|
-
/** select 动作写入的值:匹配到选项时存 value(动作可写 value 或 label);declared element 切换数组中的一项。 */
|
|
328
|
-
function selectedValue(project, state, target, raw) {
|
|
329
|
-
const sc = project.scenarios[state.scenarioId];
|
|
330
|
-
const scene = sc ? sceneOfTarget(project, sc, state, target) : project.scenes[state.sceneId];
|
|
331
|
-
const node = scene ? findNode(scene, target)?.node : undefined;
|
|
332
|
-
if (!node)
|
|
333
|
-
return raw;
|
|
334
|
-
const choices = choicesOf(node, state);
|
|
335
|
-
const value = (choices && matchChoice(choices, raw)?.value) ?? raw;
|
|
336
|
-
if (node.traits?.input !== 'multiple')
|
|
337
|
-
return value;
|
|
338
|
-
const cur = state.inputs[target] ?? resolveValue(node.props?.value ?? node.props?.defaultValue, state);
|
|
339
|
-
const list = Array.isArray(cur) ? cur.map(String) : [];
|
|
340
|
-
return list.includes(value) ? list.filter((x) => x !== value) : [...list, value];
|
|
341
|
-
}
|
|
342
|
-
/** 执行 scenario.steps[state.position],返回新状态(不修改入参)。 */
|
|
343
|
-
export function applyStep(project, scenario, state) {
|
|
344
|
-
const step = scenario.steps[state.position];
|
|
345
|
-
if (!step)
|
|
346
|
-
throw new JoploError('PLAYBACK_FAILED', `Scenario ${scenario.id} has no step ${state.position + 1}`);
|
|
347
|
-
const problem = checkStep(project, scenario, state, step);
|
|
348
|
-
if (problem)
|
|
349
|
-
throw new PlaybackError(problem.message, problem);
|
|
350
|
-
const next = structuredClone(state);
|
|
351
|
-
next.toasts = [];
|
|
352
|
-
const a = step.action;
|
|
353
|
-
if (a.type === 'type' || a.type === 'toggle')
|
|
354
|
-
next.inputs[a.target] = a.value;
|
|
355
|
-
if (a.type === 'select')
|
|
356
|
-
next.inputs[a.target] = selectedValue(project, state, a.target, a.value);
|
|
357
|
-
if (a.type === 'back')
|
|
358
|
-
next.sceneId = next.sceneStack.pop();
|
|
359
|
-
step.effects.forEach((e, i) => {
|
|
360
|
-
const ref = { kind: 'step', id: step.id, optionId: scenario.optionId, scenarioId: scenario.id, stepId: step.id, path: `effects[${i}]` };
|
|
361
|
-
switch (e.type) {
|
|
362
|
-
case 'set':
|
|
363
|
-
setPath(next, e.path, e.value, 'set');
|
|
364
|
-
break;
|
|
365
|
-
case 'append':
|
|
366
|
-
setPath(next, e.path, e.value, 'append');
|
|
367
|
-
break;
|
|
368
|
-
case 'stream':
|
|
369
|
-
setPath(next, e.path, e.value, 'set');
|
|
370
|
-
break;
|
|
371
|
-
case 'toast':
|
|
372
|
-
next.toasts.push({ id: `${step.id}_${i}`, text: e.text, tone: e.tone });
|
|
373
|
-
break;
|
|
374
|
-
case 'navigate': {
|
|
375
|
-
const target = project.scenes[e.sceneId];
|
|
376
|
-
// 发布 IR 只有发布视图(`@/路径`,optionId 为 null);dev / review 里跳转目标必须属于同一方案(D68 修复:以前发布站一跳转就报错)
|
|
377
|
-
if (!target || (target.optionId !== scenario.optionId && !(project.mode === 'publish' && target.optionId === null)))
|
|
378
|
-
throw new PlaybackError(`Step ${step.id} navigates to a stage that is missing or outside this option: ${e.sceneId}`, {
|
|
379
|
-
severity: 'error',
|
|
380
|
-
code: 'MISSING_SCENE',
|
|
381
|
-
message: `Navigation target stage ${e.sceneId} is missing or outside option ${scenario.optionId}`,
|
|
382
|
-
source: 'replay',
|
|
383
|
-
ref,
|
|
384
|
-
});
|
|
385
|
-
next.sceneStack.push(next.sceneId);
|
|
386
|
-
next.sceneId = e.sceneId;
|
|
387
|
-
break;
|
|
388
|
-
}
|
|
389
|
-
case 'complete':
|
|
390
|
-
next.completed = { text: e.text, outcome: e.outcome };
|
|
391
|
-
break;
|
|
392
|
-
}
|
|
393
|
-
});
|
|
394
|
-
next.position = state.position + 1;
|
|
395
|
-
return next;
|
|
396
|
-
}
|
|
397
|
-
/** 从初始状态重放到指定位置。 */
|
|
398
|
-
export function stateAt(project, scenarioId, position) {
|
|
399
|
-
const sc = getScenario(project, scenarioId);
|
|
400
|
-
if (!Number.isInteger(position) || position < 0 || position > sc.steps.length)
|
|
401
|
-
throw new JoploError('NOT_FOUND', `Position ${position} is outside scenario ${scenarioId} range 0..${sc.steps.length}`);
|
|
402
|
-
let s = initialState(project, scenarioId);
|
|
403
|
-
while (s.position < position)
|
|
404
|
-
s = applyStep(project, sc, s);
|
|
405
|
-
return s;
|
|
406
|
-
}
|
|
407
|
-
/** 步骤 ID → 该步骤执行之后的位置。 */
|
|
408
|
-
export function positionAfterStep(project, scenarioId, stepId) {
|
|
409
|
-
const sc = getScenario(project, scenarioId);
|
|
410
|
-
const i = sc.steps.findIndex((s) => s.id === stepId);
|
|
411
|
-
if (i < 0)
|
|
412
|
-
throw new JoploError('NOT_FOUND', `Step ${stepId} is not in scenario ${scenarioId}`);
|
|
413
|
-
return i + 1;
|
|
414
|
-
}
|
|
415
|
-
/** 完整回放一个场景,收集诊断而不抛出。供校验器使用。 */
|
|
416
|
-
export function replay(project, scenarioId) {
|
|
417
|
-
let sc;
|
|
418
|
-
let s;
|
|
419
|
-
try {
|
|
420
|
-
sc = getScenario(project, scenarioId);
|
|
421
|
-
s = initialState(project, scenarioId);
|
|
422
|
-
}
|
|
423
|
-
catch (e) {
|
|
424
|
-
return { finalState: null, reached: 0, diagnostics: [toDiagnostic(e, { kind: 'scenario', id: scenarioId })] };
|
|
425
|
-
}
|
|
426
|
-
while (s.position < sc.steps.length) {
|
|
427
|
-
try {
|
|
428
|
-
s = applyStep(project, sc, s);
|
|
429
|
-
}
|
|
430
|
-
catch (e) {
|
|
431
|
-
return { finalState: s, reached: s.position, diagnostics: [toDiagnostic(e, { kind: 'scenario', id: scenarioId, scenarioId })] };
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
return { finalState: s, reached: s.position, diagnostics: [] };
|
|
435
|
-
}
|
|
436
|
-
function toDiagnostic(e, ref) {
|
|
437
|
-
if (e instanceof JoploError && e.diagnostics[0])
|
|
438
|
-
return e.diagnostics[0];
|
|
439
|
-
return { severity: 'error', code: e instanceof JoploError ? e.code : 'PLAYBACK_FAILED', message: e instanceof Error ? e.message : String(e), source: 'replay', ref };
|
|
440
|
-
}
|
|
441
|
-
/** 禁用原因的当前文字:字面量原样返回;绑定(`$ui.x`)读当前状态;空或不是文字时为 null(界面用默认说明)。 */
|
|
442
|
-
export function disabledReasonOf(node, state) {
|
|
443
|
-
const v = node.disabledReason === undefined ? undefined : resolveValue(node.disabledReason, state);
|
|
444
|
-
return typeof v === 'string' && v.trim() ? v : typeof v === 'number' ? String(v) : null;
|
|
445
|
-
}
|
|
446
|
-
/**
|
|
447
|
-
* 演示模式下用户点击了某个元素:判断应推进步骤,还是提示“当前演示未覆盖”(PRD 8.2、AC-19)。
|
|
448
|
-
* 流程外点击绝不能被解释为成功。
|
|
449
|
-
*/
|
|
450
|
-
export function explainClick(project, state, nodeId, sceneId) {
|
|
451
|
-
const sc = getScenario(project, state.scenarioId);
|
|
452
|
-
// sceneId:用户正在看的画面(有层级的方案是某一层的画面);缺省为状态里的路径画面
|
|
453
|
-
const scene = getScene(project, sceneId ?? state.sceneId);
|
|
454
|
-
const hit = findNode(scene, nodeId);
|
|
455
|
-
if (!hit)
|
|
456
|
-
return { kind: 'static' };
|
|
457
|
-
const { node } = hit;
|
|
458
|
-
const label = nodeLabel(node);
|
|
459
|
-
if (isNodeDisabled(node, state))
|
|
460
|
-
return { kind: 'disabled', reason: disabledReasonOf(node, state) };
|
|
461
|
-
const current = sc.steps[state.position];
|
|
462
|
-
const targetOf = (s) => ('target' in s.action ? s.action.target : null);
|
|
463
|
-
if (current && !state.completed && targetOf(current) === nodeId && checkStep(project, sc, state, current) === null)
|
|
464
|
-
return { kind: 'advance' };
|
|
465
|
-
if (!isInteractiveType(node.type, project.plugins))
|
|
466
|
-
return { kind: 'static' };
|
|
467
|
-
if (node.unmodeled)
|
|
468
|
-
return { kind: 'unmodeled', label };
|
|
469
|
-
if (sc.steps.some((s) => targetOf(s) === nodeId))
|
|
470
|
-
return { kind: 'out_of_order', label, expectedCaption: current && !state.completed ? current.caption : null };
|
|
471
|
-
return { kind: 'not_covered', label };
|
|
472
|
-
}
|
|
473
|
-
// ---------- 视角层级与导演镜头(D43 / T-layers:每层一个独立画面) ----------
|
|
474
|
-
/** 方案的层级(源码顺序):每层一个完全独立的画面。没有声明时为空,演示照常显示路径页面。 */
|
|
475
|
-
export function layersOf(project, scenarioId) {
|
|
476
|
-
const sc = scenarioId ? project.scenarios[scenarioId] : null;
|
|
477
|
-
const opt = sc ? project.options[sc.optionId] : null;
|
|
478
|
-
// 图标:作者写的优先;没写时手机视口用 mobile,其余用 window
|
|
479
|
-
return Object.entries(opt?.layers ?? {}).map(([id, l]) => ({ id, title: l.title, sceneId: l.sceneId, icon: l.icon ?? (project.scenes[l.sceneId]?.viewport === 'mobile' ? 'mobile' : 'window') }));
|
|
480
|
-
}
|
|
481
|
-
/** 包含某个元素的层级(多层都有同名 id 时都返回)。 */
|
|
482
|
-
export function layersWithNode(project, scenarioId, nodeId) {
|
|
483
|
-
return layersOf(project, scenarioId)
|
|
484
|
-
.filter((l) => { const sc = project.scenes[l.sceneId]; return !!sc && !!findNode(sc, nodeId); })
|
|
485
|
-
.map((l) => l.id);
|
|
486
|
-
}
|
|
487
|
-
/**
|
|
488
|
-
* 步骤目标所在的画面:有层级的方案在各层画面里找(优先当前可见的那一层),否则是状态里的路径画面。
|
|
489
|
-
* 回放校验、select 取值都用它。
|
|
490
|
-
*/
|
|
491
|
-
export function sceneOfTarget(project, scenario, state, target) {
|
|
492
|
-
const layers = layersOf(project, scenario.id);
|
|
493
|
-
if (!layers.length)
|
|
494
|
-
return project.scenes[state.sceneId];
|
|
495
|
-
const scenes = layers.map((l) => project.scenes[l.sceneId]).filter((x) => !!x && !!findNode(x, target));
|
|
496
|
-
return scenes.find((x) => isNodeVisible(x, target, state)) ?? scenes[0] ?? project.scenes[state.sceneId];
|
|
497
|
-
}
|
|
498
|
-
/**
|
|
499
|
-
* 第 position 步之后的镜头。纯函数:只由步骤上的 shots / directorLayer 推导,不进入 ui / data / inputs。
|
|
500
|
-
* 初始导演层 = 第一个声明的层级(没有层级时是路径页面 @page),各层都是整体。
|
|
501
|
-
* 没有 shot 的步骤沿用上一步;shot()(@director)让当时的导演层回到整体。
|
|
502
|
-
* 手动、逐步、自动播放、回放、renderStep、视频录制和发布站都用它。
|
|
503
|
-
*/
|
|
504
|
-
export function cameraAt(project, scenarioId, position) {
|
|
505
|
-
const sc = project.scenarios[scenarioId];
|
|
506
|
-
const perLayer = {};
|
|
507
|
-
const layers = layersOf(project, scenarioId);
|
|
508
|
-
let directorLayer = (sc?.initial.layer && layers.some((l) => l.id === sc.initial.layer) ? sc.initial.layer : layers[0]?.id) ?? PAGE_LAYER;
|
|
509
|
-
if (!sc)
|
|
510
|
-
return { directorLayer, perLayer };
|
|
511
|
-
const n = Math.max(0, Math.min(position, sc.steps.length));
|
|
512
|
-
for (let i = 0; i < n; i++) {
|
|
513
|
-
const st = sc.steps[i];
|
|
514
|
-
if (st.directorLayer)
|
|
515
|
-
directorLayer = st.directorLayer;
|
|
516
|
-
for (const s of st.shots ?? [])
|
|
517
|
-
perLayer[s.layer === DIRECTOR_LAYER ? directorLayer : s.layer] = [...s.targets];
|
|
518
|
-
}
|
|
519
|
-
return { directorLayer, perLayer };
|
|
520
|
-
}
|
|
521
|
-
/** 第 position 步之后用户看到的画面:有层级时是导演层(或手动选的层)的画面,否则是状态里的路径画面。 */
|
|
522
|
-
export function displaySceneId(project, scenarioId, position, layer) {
|
|
523
|
-
const layers = layersOf(project, scenarioId);
|
|
524
|
-
const state = stateAt(project, scenarioId, position);
|
|
525
|
-
if (!layers.length)
|
|
526
|
-
return state.sceneId;
|
|
527
|
-
const want = layer && layers.some((l) => l.id === layer) ? layer : cameraAt(project, scenarioId, position).directorLayer;
|
|
528
|
-
return (layers.find((l) => l.id === want) ?? layers[0]).sceneId;
|
|
529
|
-
}
|
|
530
|
-
/** 第 index 步(0 起)的镜头是否改变了某一层(“只重放这一层”时,不变的步骤快速带过)。 */
|
|
531
|
-
export function stepTouchesLayer(project, scenarioId, index, layer) {
|
|
532
|
-
const st = project.scenarios[scenarioId]?.steps[index];
|
|
533
|
-
if (!st)
|
|
534
|
-
return false;
|
|
535
|
-
if ((st.shots ?? []).some((s) => s.layer === layer || (s.layer === DIRECTOR_LAYER && cameraAt(project, scenarioId, index + 1).directorLayer === layer)))
|
|
536
|
-
return true;
|
|
537
|
-
// 操作目标在这一层的步骤也算(要看着它被点)
|
|
538
|
-
return 'target' in st.action && layersWithNode(project, scenarioId, st.action.target).includes(layer);
|
|
539
|
-
}
|
|
540
|
-
/** 第 position 步之后是否停在一张过渡卡上(最近执行的一步是过渡卡);是则返回卡片内容。 */
|
|
541
|
-
export function cardAt(project, scenarioId, position) {
|
|
542
|
-
const st = scenarioId ? project.scenarios[scenarioId]?.steps[position - 1] : undefined;
|
|
543
|
-
return st && st.action.type === 'card' ? st.action : null;
|
|
544
|
-
}
|
|
545
|
-
/** 方案所属功能里可引用的角色(有 id 的)。 */
|
|
546
|
-
export function roleOf(project, optionId, roleId) {
|
|
547
|
-
if (!optionId || !roleId)
|
|
548
|
-
return null;
|
|
549
|
-
const opt = project.options[optionId];
|
|
550
|
-
const r = opt ? project.features[opt.featureId]?.roles?.find((x) => x.id === roleId) : undefined;
|
|
551
|
-
return r && r.id ? { id: r.id, name: r.name, desc: r.desc, color: r.color } : null;
|
|
552
|
-
}
|
|
553
|
-
/** 某个画面是谁的(层级声明了 role 时);路径页面与没有角色的层级返回 null。 */
|
|
554
|
-
export function roleOfScene(project, sceneId) {
|
|
555
|
-
const scene = sceneId ? project.scenes[sceneId] : undefined;
|
|
556
|
-
if (!scene?.layer || !scene.optionId)
|
|
557
|
-
return null;
|
|
558
|
-
return roleOf(project, scene.optionId, project.options[scene.optionId]?.layers[scene.layer]?.role);
|
|
559
|
-
}
|
|
560
|
-
/** 第 index 步(0 起)流式输出的状态路径与文字。 */
|
|
561
|
-
export function streamsOf(step) {
|
|
562
|
-
return (step?.effects ?? []).flatMap((e) => (e.type === 'stream' ? [{ path: e.path, value: e.value, ...(e.pace ? { pace: e.pace } : {}) }] : []));
|
|
563
|
-
}
|
|
564
|
-
/**
|
|
565
|
-
* 这一步的流式输出怎么出字(D56,D100)。prev 是这一步之前的状态;不知道时传 null(整段重新出,用于估算)。
|
|
566
|
-
* - 接着往后写:新值以原来的值开头时,原有的字直接显示,只有新加的字逐字出现;
|
|
567
|
-
* - pace: 'voice':新加的字在这一步朗读(narrate ?? say)的估算时长里出完,读到哪写到哪;没有朗读时退回按字数。
|
|
568
|
-
*/
|
|
569
|
-
export function streamPlan(step, prev) {
|
|
570
|
-
return streamsOf(step).map((s) => {
|
|
571
|
-
const before = prev ? readPath(prev, s.path) : undefined;
|
|
572
|
-
const keep = typeof before === 'string' && before && s.value.startsWith(before) ? [...before].length : 0;
|
|
573
|
-
const fresh = [...s.value].slice(keep).join('');
|
|
574
|
-
const voiced = s.pace === 'voice' ? voicePacedMs(step?.narrate ?? step?.say) : 0;
|
|
575
|
-
return { path: s.path, value: s.value, keep, ms: fresh ? (voiced || streamMs(fresh)) : 0, ...(s.pace ? { pace: s.pace } : {}) };
|
|
576
|
-
});
|
|
577
|
-
}
|
|
578
|
-
// ---------- 分叉、标注与变化(D56 P1) ----------
|
|
579
|
-
/** 动作和业务效果完全相同的公共前缀步数;字幕、镜头、分叉等展示信息不参与。 */
|
|
580
|
-
export function commonPrefix(a, b) {
|
|
581
|
-
let n = 0;
|
|
582
|
-
while (n < Math.min(a.length, b.length) && sameValue(a[n].action, b[n].action) && sameValue(a[n].effects, b[n].effects))
|
|
583
|
-
n++;
|
|
584
|
-
return n;
|
|
585
|
-
}
|
|
586
|
-
function sameValue(a, b) {
|
|
587
|
-
if (Object.is(a, b))
|
|
588
|
-
return true;
|
|
589
|
-
if (!a || !b || typeof a !== 'object' || typeof b !== 'object')
|
|
590
|
-
return false;
|
|
591
|
-
if (Array.isArray(a) || Array.isArray(b))
|
|
592
|
-
return Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((v, i) => sameValue(v, b[i]));
|
|
593
|
-
const ak = Object.keys(a), bk = Object.keys(b);
|
|
594
|
-
return ak.length === bk.length && ak.every(k => Object.hasOwn(b, k) && sameValue(a[k], b[k]));
|
|
595
|
-
}
|
|
596
|
-
export function forksAt(project, scenarioId, position) {
|
|
597
|
-
return forksOf(scenarioId ? project.scenarios[scenarioId]?.steps[position - 1] : undefined);
|
|
598
|
-
}
|
|
599
|
-
/** 与 Toast 一样只显示最近一步,目标是否可见由当前显示画面决定。 */
|
|
600
|
-
export function marksAt(project, scenarioId, position) {
|
|
601
|
-
return (scenarioId ? project.scenarios[scenarioId]?.steps[position - 1]?.marks : undefined) ?? [];
|
|
602
|
-
}
|
|
603
|
-
/** 对比 core 回放产生的相邻状态:对象按叶子展开,数组作为一个值;不包含动画、Toast 等展示状态。 */
|
|
604
|
-
export function changedAt(project, scenarioId, position) {
|
|
605
|
-
const sc = scenarioId ? project.scenarios[scenarioId] : undefined;
|
|
606
|
-
if (!sc || !Number.isInteger(position) || position <= 0 || position > sc.steps.length)
|
|
607
|
-
return [];
|
|
608
|
-
const before = stateAt(project, sc.id, position - 1);
|
|
609
|
-
const after = applyStep(project, sc, before);
|
|
610
|
-
const out = [];
|
|
611
|
-
const object = (v) => !!v && typeof v === 'object' && !Array.isArray(v);
|
|
612
|
-
const diff = (path, a, b) => {
|
|
613
|
-
if (sameValue(a, b))
|
|
614
|
-
return;
|
|
615
|
-
if ((object(a) || a === undefined) && (object(b) || b === undefined) && (object(a) || object(b))) {
|
|
616
|
-
const keys = [...new Set([...Object.keys(a ?? {}), ...Object.keys(b ?? {})])].sort();
|
|
617
|
-
if (keys.length) {
|
|
618
|
-
for (const k of keys)
|
|
619
|
-
diff(`${path}.${k}`, a?.[k], b?.[k]);
|
|
620
|
-
return;
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
out.push({ path, before: structuredClone(a), after: structuredClone(b) });
|
|
624
|
-
};
|
|
625
|
-
for (const root of ['ui', 'data', 'inputs'])
|
|
626
|
-
diff(root, before[root], after[root]);
|
|
627
|
-
return out;
|
|
628
|
-
}
|
|
629
|
-
/** 已定位的一步的路线;字幕也用这一个投影入口。 */
|
|
630
|
-
export function forksOf(step) { return step?.forks ?? []; }
|
|
631
|
-
/** 按键记录的 IR 入口:静态取最近一步,动画预览与键帽分词由 web 负责。 */
|
|
632
|
-
export function keyboardActionOf(step) {
|
|
633
|
-
const a = step?.action;
|
|
634
|
-
return a?.type === 'type' || a?.type === 'press' ? a : null;
|
|
635
|
-
}
|
|
8
|
+
export * from "./scene-query.js";
|
|
9
|
+
export * from "./playback.js";
|
|
10
|
+
export * from "./presentation.js";
|
package/dist/playground.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ export type ActionVerdict = {
|
|
|
27
27
|
};
|
|
28
28
|
export { actionMatches, pluginActionSpec, validActionData } from './actions.js';
|
|
29
29
|
/** Index only positions whose prefix replays and whose action is available there. */
|
|
30
|
-
export declare function actionIndex(project: Project,
|
|
30
|
+
export declare function actionIndex(project: Project, workId: string): ActionEntry[];
|
|
31
31
|
export declare function arbitrateAction(project: Project, timeline: PlayState, action: Action, opts?: {
|
|
32
32
|
layer?: Playground | null;
|
|
33
33
|
sceneId?: string;
|