@joplo-app/core 0.2.9 → 0.2.12
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 +2 -2
- package/dist/agent-skill.js +51 -24
- package/dist/catalog.d.ts +2 -0
- package/dist/catalog.js +2 -0
- package/dist/compile.js +52 -0
- package/dist/guide.d.ts +3 -0
- package/dist/guide.js +111 -8
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/local-review-files.d.ts +11 -0
- package/dist/local-review-files.js +88 -0
- package/dist/local-review.d.ts +134 -0
- package/dist/local-review.js +62 -0
- package/dist/node.d.ts +1 -0
- package/dist/node.js +1 -0
- package/dist/schema.d.ts +33 -0
- package/dist/schema.js +21 -0
- package/dist/scope.d.ts +50 -0
- package/dist/scope.js +238 -0
- package/dist/validate.js +7 -1
- package/dist/voice.d.ts +30 -0
- package/dist/voice.js +79 -0
- package/package.json +2 -2
package/dist/scope.js
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 按场景保密分享(D75):把编译好的 IR 裁剪到若干场景。
|
|
3
|
+
*
|
|
4
|
+
* 不变量:裁剪后的 IR 里不出现范围外功能、方案、场景的 id、名字、目标、角色、说明和步骤文字。
|
|
5
|
+
* - 保留:范围内的场景;它们的方案和功能;场景用得到的画面(起始画面、navigate 能到的画面、方案的层级画面);这些画面的路径。
|
|
6
|
+
* - 堆叠元素:`from` 是范围外方案的贡献子树默认去掉(when 隐藏的也一样);范围内步骤引用到子树里的元素时整条子树保留(借用),
|
|
7
|
+
* 并去掉子树里所有指向范围外方案的 `from`。范围内场景跳到别的功能新增的路径时,这个页面也按借用保留,去掉 `addedBy`。
|
|
8
|
+
* - 范围外的分叉去掉;规则、情况、问题只留范围内功能(规则、问题再按方案过滤);项目说明去掉(常常概述所有功能),项目名保留。
|
|
9
|
+
* dev、publish、review 三种 IR 都能裁剪:share 用它裁剪发布 IR(上传的文件)和 dev IR(评审清单、预览图)。
|
|
10
|
+
*/
|
|
11
|
+
import { orderedFeatures } from "./features.js";
|
|
12
|
+
import { JoploError } from "./result.js";
|
|
13
|
+
export const hasScope = (r) => !!r && ((r.scenarios?.length ?? 0) > 0 || (r.features?.length ?? 0) > 0);
|
|
14
|
+
export const scopeKey = (scenarios, mode) => `${mode === 'review' ? 'review:' : ''}${[...scenarios].sort().join(',')}`;
|
|
15
|
+
/** 演示顺序:按功能顺序(featureIds),同一功能里按 IR 顺序。 */
|
|
16
|
+
function demoOrder(project) {
|
|
17
|
+
const rank = new Map(orderedFeatures(project).map((f, i) => [f.id, i]));
|
|
18
|
+
const all = Object.values(project.scenarios);
|
|
19
|
+
const r = (optionId) => rank.get(project.options[optionId]?.featureId ?? '') ?? rank.size;
|
|
20
|
+
return all.map((s, i) => ({ s, i })).sort((a, b) => r(a.s.optionId) - r(b.s.optionId) || a.i - b.i).map(({ s }) => s.id);
|
|
21
|
+
}
|
|
22
|
+
/** 在给定模式下可分享的场景:publish 只有各功能选定方案的场景。 */
|
|
23
|
+
function shareable(project, mode) {
|
|
24
|
+
const picked = new Set(orderedFeatures(project).map((f) => f.pick).filter((p) => !!p));
|
|
25
|
+
return Object.values(project.scenarios).filter((s) => mode === 'review' || picked.has(s.optionId)).map((s) => s.id);
|
|
26
|
+
}
|
|
27
|
+
function listLines(project, ids) {
|
|
28
|
+
const shown = ids.slice(0, 20).map((id) => ` ${id} ${project.scenarios[id]?.name ?? ''}`);
|
|
29
|
+
return shown.length ? `\n${shown.join('\n')}${ids.length > shown.length ? `\n … ${ids.length - shown.length} more` : ''}` : ' (none)';
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 把作者写的范围解析成场景 id(保持 IR 里的顺序)。写错的场景或功能、没有选定方案、范围为空时报错,调用方不应发布任何东西。
|
|
33
|
+
*/
|
|
34
|
+
export function resolveScope(project, request, mode) {
|
|
35
|
+
const ok = new Set(shareable(project, mode));
|
|
36
|
+
const out = new Set();
|
|
37
|
+
for (const fid of request.features ?? []) {
|
|
38
|
+
const f = project.features[fid];
|
|
39
|
+
if (!f) {
|
|
40
|
+
const known = orderedFeatures(project).map((x) => x.id).join(', ') || 'none';
|
|
41
|
+
throw new JoploError('NOT_FOUND', `Unknown feature "${fid}"${mode === 'publish' ? ', or it has no picked option' : ''}. Features: ${known}`, { details: { scope: 'feature', id: fid } });
|
|
42
|
+
}
|
|
43
|
+
const opts = mode === 'review' ? f.optionIds : f.pick ? [f.pick] : [];
|
|
44
|
+
if (!opts.length)
|
|
45
|
+
throw new JoploError('USAGE', `Feature "${fid}" has no picked option, so nothing of it is published. Pick an option (feature.pick), or share the review version (--review)`, { details: { scope: 'feature', id: fid } });
|
|
46
|
+
for (const s of Object.values(project.scenarios))
|
|
47
|
+
if (opts.includes(s.optionId))
|
|
48
|
+
out.add(s.id);
|
|
49
|
+
}
|
|
50
|
+
for (const sid of request.scenarios ?? []) {
|
|
51
|
+
const s = project.scenarios[sid];
|
|
52
|
+
if (s && ok.has(sid)) {
|
|
53
|
+
out.add(sid);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (s) {
|
|
57
|
+
const o = project.options[s.optionId];
|
|
58
|
+
throw new JoploError('USAGE', `Scenario ${sid} belongs to option ${s.optionId}, which is not the picked option of feature "${o?.featureId ?? '?'}"; only picked options are published. Pick it, or share the review version (--review)`, { details: { scope: 'scenario', id: sid } });
|
|
59
|
+
}
|
|
60
|
+
const tail = sid.split('.').pop() ?? sid;
|
|
61
|
+
const head = sid.split('.')[0] ?? sid;
|
|
62
|
+
const all = [...ok];
|
|
63
|
+
const near = all.filter((id) => id.endsWith(`.${tail}`) || id.startsWith(`${head}.`));
|
|
64
|
+
const hint = near.length ? `Did you mean:${listLines(project, near)}` : `Scenarios you can share:${listLines(project, all)}`;
|
|
65
|
+
throw new JoploError('NOT_FOUND', `Unknown scenario ${sid}. ${hint}\nNothing was built or uploaded`, { details: { scope: 'scenario', id: sid } });
|
|
66
|
+
}
|
|
67
|
+
const ids = demoOrder(project).filter((id) => out.has(id));
|
|
68
|
+
if (!ids.length)
|
|
69
|
+
throw new JoploError('USAGE', 'The scope has no scenarios; nothing to share');
|
|
70
|
+
return ids;
|
|
71
|
+
}
|
|
72
|
+
/** 一步里引用到的元素 id:操作目标、镜头、标注、焦点提示、标注气泡、trace。 */
|
|
73
|
+
export function stepRefs(st) {
|
|
74
|
+
const out = [];
|
|
75
|
+
if ('target' in st.action)
|
|
76
|
+
out.push(st.action.target);
|
|
77
|
+
for (const s of st.shots)
|
|
78
|
+
out.push(...s.targets);
|
|
79
|
+
for (const m of st.marks)
|
|
80
|
+
out.push(m.target);
|
|
81
|
+
for (const c of st.cues ?? [])
|
|
82
|
+
out.push(...c.targets);
|
|
83
|
+
for (const c of st.callouts ?? [])
|
|
84
|
+
out.push(c.target);
|
|
85
|
+
for (const t of st.traces)
|
|
86
|
+
out.push(...t.nodes, ...t.edges.map((e) => e.id), ...(t.groups ?? []), ...(t.errorGroups ?? []), ...(t.targets ?? []));
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
function idsIn(n, out = []) {
|
|
90
|
+
out.push(n.id);
|
|
91
|
+
for (const c of n.children ?? [])
|
|
92
|
+
idsIn(c, out);
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* 裁剪到给定场景(先用 resolveScope 解析)。返回新的 IR(输入不变)和范围说明。
|
|
97
|
+
*/
|
|
98
|
+
export function scopeProject(project, scenarioIds, mode = project.mode === 'review' ? 'review' : 'publish') {
|
|
99
|
+
const S = new Set(scenarioIds.filter((id) => project.scenarios[id]));
|
|
100
|
+
if (!S.size)
|
|
101
|
+
throw new JoploError('USAGE', 'The scope has no scenarios; nothing to share');
|
|
102
|
+
const O = new Set([...S].map((id) => project.scenarios[id].optionId));
|
|
103
|
+
const F = new Set([...O].map((id) => project.options[id]?.featureId).filter((f) => !!f));
|
|
104
|
+
// 引用:元素 id → 引用它的场景
|
|
105
|
+
const refBy = new Map();
|
|
106
|
+
for (const sid of S)
|
|
107
|
+
for (const st of project.scenarios[sid].steps)
|
|
108
|
+
for (const id of stepRefs(st)) {
|
|
109
|
+
const set = refBy.get(id) ?? new Set();
|
|
110
|
+
set.add(sid);
|
|
111
|
+
refBy.set(id, set);
|
|
112
|
+
}
|
|
113
|
+
// 用得到的画面 → 用到它的场景
|
|
114
|
+
const sceneBy = new Map();
|
|
115
|
+
const useScene = (id, sid) => {
|
|
116
|
+
if (!project.scenes[id])
|
|
117
|
+
return;
|
|
118
|
+
const set = sceneBy.get(id) ?? new Set();
|
|
119
|
+
set.add(sid);
|
|
120
|
+
sceneBy.set(id, set);
|
|
121
|
+
};
|
|
122
|
+
for (const sid of S) {
|
|
123
|
+
const s = project.scenarios[sid];
|
|
124
|
+
useScene(s.initial.sceneId, sid);
|
|
125
|
+
for (const st of s.steps)
|
|
126
|
+
for (const e of st.effects)
|
|
127
|
+
if (e.type === 'navigate')
|
|
128
|
+
useScene(e.sceneId, sid);
|
|
129
|
+
for (const l of Object.values(project.options[s.optionId]?.layers ?? {}))
|
|
130
|
+
useScene(l.sceneId, sid);
|
|
131
|
+
}
|
|
132
|
+
const borrowed = [];
|
|
133
|
+
const featureOf = (optionId) => {
|
|
134
|
+
const fid = project.options[optionId]?.featureId ?? optionId.split('.')[0] ?? optionId;
|
|
135
|
+
return { featureId: fid, featureName: project.features[fid]?.name ?? fid };
|
|
136
|
+
};
|
|
137
|
+
/** 借用的子树:整条保留,去掉所有指向范围外方案的 from。 */
|
|
138
|
+
const strip = (n) => {
|
|
139
|
+
const { from, children, ...rest } = n;
|
|
140
|
+
return { ...rest, ...(from !== undefined && O.has(from) ? { from } : {}), ...(children ? { children: children.map(strip) } : {}) };
|
|
141
|
+
};
|
|
142
|
+
const prune = (n, scene, users) => {
|
|
143
|
+
if (n.from !== undefined && !O.has(n.from)) {
|
|
144
|
+
const ids = idsIn(n);
|
|
145
|
+
const refs = ids.filter((id) => refBy.has(id));
|
|
146
|
+
if (!refs.length)
|
|
147
|
+
return null;
|
|
148
|
+
const who = new Set(refs.flatMap((id) => [...refBy.get(id)]).filter((sid) => users.has(sid)));
|
|
149
|
+
borrowed.push({ kind: 'element', ...featureOf(n.from), optionId: n.from, elementId: n.id, route: scene.route, refs, scenarios: [...(who.size ? who : users)] });
|
|
150
|
+
return strip(n);
|
|
151
|
+
}
|
|
152
|
+
if (!n.children)
|
|
153
|
+
return n;
|
|
154
|
+
return { ...n, children: n.children.map((c) => prune(c, scene, users)).filter((c) => c !== null) };
|
|
155
|
+
};
|
|
156
|
+
const scenes = {};
|
|
157
|
+
for (const [id, scene] of Object.entries(project.scenes)) {
|
|
158
|
+
const users = sceneBy.get(id);
|
|
159
|
+
if (!users)
|
|
160
|
+
continue;
|
|
161
|
+
scenes[id] = { ...scene, root: prune(scene.root, scene, users) ?? scene.root };
|
|
162
|
+
}
|
|
163
|
+
// 路径:只留用得到的画面的路径;别的功能新增的页面按借用保留
|
|
164
|
+
const routes = {};
|
|
165
|
+
const paths = new Set(Object.values(scenes).map((s) => s.route));
|
|
166
|
+
for (const [p, r] of Object.entries(project.routes)) {
|
|
167
|
+
if (!paths.has(p))
|
|
168
|
+
continue;
|
|
169
|
+
if (r.addedBy && !O.has(r.addedBy)) {
|
|
170
|
+
const users = new Set(Object.entries(scenes).filter(([, s]) => s.route === p).flatMap(([id]) => [...(sceneBy.get(id) ?? [])]));
|
|
171
|
+
borrowed.push({ kind: 'page', ...featureOf(r.addedBy), optionId: r.addedBy, route: p, scenarios: [...users] });
|
|
172
|
+
const { addedBy: _a, ...rest } = r;
|
|
173
|
+
routes[p] = rest;
|
|
174
|
+
}
|
|
175
|
+
else
|
|
176
|
+
routes[p] = r;
|
|
177
|
+
}
|
|
178
|
+
// 画面之间不会互相借用页面以外的东西;同一元素在多个画面被借用时合并成一条
|
|
179
|
+
const seen = new Set();
|
|
180
|
+
const merged = borrowed.filter((b) => {
|
|
181
|
+
const k = b.kind === 'element' ? `e|${b.optionId}|${b.elementId}|${b.route}` : `p|${b.route}`;
|
|
182
|
+
if (seen.has(k))
|
|
183
|
+
return false;
|
|
184
|
+
seen.add(k);
|
|
185
|
+
return true;
|
|
186
|
+
});
|
|
187
|
+
const scenarios = {};
|
|
188
|
+
for (const sid of Object.keys(project.scenarios)) {
|
|
189
|
+
if (!S.has(sid))
|
|
190
|
+
continue;
|
|
191
|
+
const s = project.scenarios[sid];
|
|
192
|
+
scenarios[sid] = { ...s, steps: s.steps.map((st) => (st.forks.some((f) => !S.has(f.scenarioId)) ? { ...st, forks: st.forks.filter((f) => S.has(f.scenarioId)) } : st)) };
|
|
193
|
+
}
|
|
194
|
+
const options = {};
|
|
195
|
+
for (const [id, o] of Object.entries(project.options)) {
|
|
196
|
+
if (!O.has(id))
|
|
197
|
+
continue;
|
|
198
|
+
const coverage = Object.fromEntries(Object.entries(o.coverage)
|
|
199
|
+
.filter(([cid]) => F.has(project.cases[cid]?.featureId ?? cid.split('.')[0] ?? ''))
|
|
200
|
+
.map(([cid, c]) => [cid, { ...c, scenarioIds: c.scenarioIds.filter((x) => S.has(x)) }]));
|
|
201
|
+
options[id] = { ...o, coverage };
|
|
202
|
+
}
|
|
203
|
+
const features = {};
|
|
204
|
+
for (const f of orderedFeatures(project)) {
|
|
205
|
+
if (!F.has(f.id))
|
|
206
|
+
continue;
|
|
207
|
+
const optionIds = f.optionIds.filter((x) => O.has(x));
|
|
208
|
+
features[f.id] = { ...f, optionIds, pick: f.pick && O.has(f.pick) ? f.pick : null };
|
|
209
|
+
}
|
|
210
|
+
const featureIds = project.featureIds?.filter((id) => F.has(id));
|
|
211
|
+
const inF = (x) => F.has(x.featureId) && (x.optionId === undefined || O.has(x.optionId));
|
|
212
|
+
const roleIds = new Set(Object.values(features).flatMap((f) => f.roles.map((r) => r.id).filter((r) => !!r)));
|
|
213
|
+
const keepRoles = (v) => v && v.roles ? { ...v, roles: Object.fromEntries(Object.entries(v.roles).filter(([k]) => roleIds.has(k))) } : v;
|
|
214
|
+
const out = {
|
|
215
|
+
...project,
|
|
216
|
+
// 项目说明常常概述所有功能:范围分享里不带;项目名保留(分享标题与页面标题要用,作者自己决定怎么起)
|
|
217
|
+
description: '',
|
|
218
|
+
routes,
|
|
219
|
+
features,
|
|
220
|
+
...(featureIds ? { featureIds } : {}),
|
|
221
|
+
cases: Object.fromEntries(Object.entries(project.cases).filter(([, c]) => F.has(c.featureId))),
|
|
222
|
+
options,
|
|
223
|
+
rules: Object.fromEntries(Object.entries(project.rules).filter(([, r]) => inF(r))),
|
|
224
|
+
scenes,
|
|
225
|
+
scenarios,
|
|
226
|
+
issues: Object.fromEntries(Object.entries(project.issues).filter(([, i]) => inF(i))),
|
|
227
|
+
...(project.voiceConfig ? { voiceConfig: keepRoles(project.voiceConfig) } : {}),
|
|
228
|
+
...(project.voiceIndex ? { voiceIndex: keepRoles(project.voiceIndex) } : {}),
|
|
229
|
+
};
|
|
230
|
+
const ids = Object.keys(scenarios);
|
|
231
|
+
return { project: out, info: { key: scopeKey(ids, mode), scenarios: ids, features: Object.keys(features), borrowed: merged } };
|
|
232
|
+
}
|
|
233
|
+
/** 一条借用的说明(英文,CLI 与看板共用)。 */
|
|
234
|
+
export function borrowedLine(b) {
|
|
235
|
+
return b.kind === 'element'
|
|
236
|
+
? `${b.featureName} › element "${b.elementId}" on ${b.route} (referenced by ${b.refs.join(', ')})`
|
|
237
|
+
: `${b.featureName} › page ${b.route} (the scenario navigates there)`;
|
|
238
|
+
}
|
package/dist/validate.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { orderedFeatures } from "./features.js";
|
|
2
2
|
import { Project } from "./schema.js";
|
|
3
|
-
import { CAPTION_MAX, CARD_POINTS_MAX, DESCRIPTION_MAX, EXPLAIN_MAX, INTERACTIVE_TYPES, SCENARIO_MS_MAX, captionLength } from "./catalog.js";
|
|
3
|
+
import { CAPTION_MAX, CARD_POINTS_MAX, DESCRIPTION_MAX, EXPLAIN_MAX, SAY_MAX, INTERACTIVE_TYPES, SCENARIO_MS_MAX, captionLength } from "./catalog.js";
|
|
4
4
|
import { scenarioEstimateMs } from "./outline.js";
|
|
5
5
|
import { walkNodes, findNode, replay } from "./player.js";
|
|
6
6
|
import { JoploError } from "./result.js";
|
|
@@ -205,6 +205,12 @@ export function validate(project, opts = { mode: 'all' }) {
|
|
|
205
205
|
if (el > EXPLAIN_MAX)
|
|
206
206
|
emit('warning', 'EXPLAIN_TOO_LONG', `explain note length is ${el} units (recommended maximum: ${EXPLAIN_MAX}; CJK characters count as 1 each, all others as 0.5; whitespace is not counted). Move long content to a separate explanation layer, for example layers: { why: layer('Design rationale', <Why />) }, then switch to it with shot`, at, r);
|
|
207
207
|
}
|
|
208
|
+
// 朗读(D74):一段大约 SAY_MAX 个“字”读出来约 10 秒,再长观众会等得不耐烦
|
|
209
|
+
for (const [fn, code, text] of [['say', 'SAY_TOO_LONG', st.say], ['narrate', 'NARRATE_TOO_LONG', st.narrate]]) {
|
|
210
|
+
const sl = text ? captionLength(text) : 0;
|
|
211
|
+
if (sl > SAY_MAX)
|
|
212
|
+
emit('warning', code, `${fn}() is ${sl} units (recommended maximum: ${SAY_MAX}, about 10 seconds read aloud; CJK characters count as 1 each, all others as 0.5). Autoplay waits until it is read, so say less or split the step`, at, r);
|
|
213
|
+
}
|
|
208
214
|
}
|
|
209
215
|
}
|
|
210
216
|
focusStructureDiagnostics(p, scenarios, emit); // 焦点提示(D70)
|
package/dist/voice.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Project, Step } from './schema.js';
|
|
2
|
+
/**
|
|
3
|
+
* 默认说话风格(D74):像讲解人介绍功能,而不是朗读;用文字本身的语言、母语口音读(这就是“跟随语言”:
|
|
4
|
+
* 同一个音色读中文就是中文母语的读法,读英文就是英文的读法)。发给支持 instructions 的模型。
|
|
5
|
+
*/
|
|
6
|
+
export declare const DEFAULT_STYLE = "You are the narrator of a short product demo. Speak like a friendly presenter walking a colleague through a new feature: warm, clear and relaxed, at a natural conversational pace, never like reading a list. Pause briefly at commas and between sentences. Read the text in its own language with a natural native accent; never translate it.";
|
|
7
|
+
/** 读完之后、进下一步之前的换气(毫秒,1× 速度)。 */
|
|
8
|
+
export declare const VOICE_BREATH_MS = 450;
|
|
9
|
+
/** 把几句话连成一段朗读:句末没有标点时补一个(中文“。”,其他“.”)。 */
|
|
10
|
+
export declare function joinSpoken(parts: (string | null | undefined)[]): string;
|
|
11
|
+
/**
|
|
12
|
+
* 这一步读什么、用谁的声音(D74):narrate()(旁白的声音)> say()(这一步角色的声音;没有角色时旁白)>
|
|
13
|
+
* 字幕(旁白的声音;过渡卡读标题和副标题)。caption 是这一步在当前语言下显示的字幕(网页用 stepCaption,服务端用 step.caption);
|
|
14
|
+
* speaker 是这一步的说话角色(speakerRoleAt)。text 为空串表示这一步不读;role 为 null 表示旁白。
|
|
15
|
+
*/
|
|
16
|
+
export declare function spokenLine(step: Step, caption: string, speaker: string | null): {
|
|
17
|
+
text: string;
|
|
18
|
+
role: string | null;
|
|
19
|
+
};
|
|
20
|
+
/** 这一步要读的文字(不关心是谁读)。 */
|
|
21
|
+
export declare function narrationText(step: Step, caption: string): string;
|
|
22
|
+
/** 第 index 步(1 起)的说话角色:用户操作取这一步之后导演所在层画面的角色 id;没有角色、系统步骤与过渡卡为 null。say() 用它的声音。 */
|
|
23
|
+
export declare function speakerRoleAt(project: Project, scenarioId: string, index: number): string | null;
|
|
24
|
+
/** 音频片段的键:声音指纹 + 规整后的朗读文字(空白合并)。 */
|
|
25
|
+
export declare function clipKey(fingerprint: string, text: string): string;
|
|
26
|
+
/** 某个角色(null = 旁白)用哪个声音指纹;角色没有单独的声音时用旁白的。 */
|
|
27
|
+
export declare function voiceFingerprint(voices: {
|
|
28
|
+
narrator: string | null;
|
|
29
|
+
roles: Record<string, string>;
|
|
30
|
+
}, roleId: string | null): string | null;
|
package/dist/voice.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 朗读(D74)的纯函数:看板、发布站、服务端(生成与 build)共用。
|
|
3
|
+
*
|
|
4
|
+
* - 读什么:narrationText —— 有 say() 读 say;过渡卡读标题和副标题;其余读字幕(字幕由调用方按当前语言给出,
|
|
5
|
+
* 自动字幕在网页里本地化)。explain 不读。
|
|
6
|
+
* - 谁来读:speakerRoleAt —— 用户操作取这一步导演层画面的角色(与字幕说话人同一来源);系统步骤、过渡卡用旁白。
|
|
7
|
+
* - 音频片段的键:clipKey(声音指纹, 朗读文字)。指纹由服务端按 profile 与音色算出(不含 Key),
|
|
8
|
+
* 同一段文字换了声音或改了字就是新键,旧音频自然失效,不需要另记“过期”。
|
|
9
|
+
* - 节奏:每一步停留 = max(画面停留, 声音时长 + VOICE_BREATH_MS),换气按播放速度缩放。
|
|
10
|
+
*/
|
|
11
|
+
import { cameraAt, layersOf, roleOfScene } from "./player.js";
|
|
12
|
+
/**
|
|
13
|
+
* 默认说话风格(D74):像讲解人介绍功能,而不是朗读;用文字本身的语言、母语口音读(这就是“跟随语言”:
|
|
14
|
+
* 同一个音色读中文就是中文母语的读法,读英文就是英文的读法)。发给支持 instructions 的模型。
|
|
15
|
+
*/
|
|
16
|
+
export const DEFAULT_STYLE = 'You are the narrator of a short product demo. Speak like a friendly presenter walking a colleague through a new feature: warm, clear and relaxed, at a natural conversational pace, never like reading a list. Pause briefly at commas and between sentences. Read the text in its own language with a natural native accent; never translate it.';
|
|
17
|
+
/** 读完之后、进下一步之前的换气(毫秒,1× 速度)。 */
|
|
18
|
+
export const VOICE_BREATH_MS = 450;
|
|
19
|
+
/** 一段文字是不是以中日韩文字为主(决定拼接时用全角句号)。 */
|
|
20
|
+
const mostlyWide = (t) => [...t].filter((ch) => (ch.codePointAt(0) ?? 0) > 0x2e80).length * 2 >= [...t.replace(/\s/g, '')].length;
|
|
21
|
+
/** 全角句号与句末标点(写成转义:源码里不放 CJK 字面量)。 */
|
|
22
|
+
const WIDE_STOP = '\u3002';
|
|
23
|
+
const SENTENCE_END = /[\u3002\uff01\uff1f!?.\u2026:\uff1a;\uff1b,\uff0c]$/;
|
|
24
|
+
/** 把几句话连成一段朗读:句末没有标点时补一个(中文“。”,其他“.”)。 */
|
|
25
|
+
export function joinSpoken(parts) {
|
|
26
|
+
const xs = parts.map((p) => (p ?? '').trim()).filter(Boolean);
|
|
27
|
+
return xs
|
|
28
|
+
.map((p, i) => (i < xs.length - 1 && !SENTENCE_END.test(p) ? p + (mostlyWide(p) ? WIDE_STOP : '.') : p))
|
|
29
|
+
.join(xs.some(mostlyWide) ? '' : ' ');
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 这一步读什么、用谁的声音(D74):narrate()(旁白的声音)> say()(这一步角色的声音;没有角色时旁白)>
|
|
33
|
+
* 字幕(旁白的声音;过渡卡读标题和副标题)。caption 是这一步在当前语言下显示的字幕(网页用 stepCaption,服务端用 step.caption);
|
|
34
|
+
* speaker 是这一步的说话角色(speakerRoleAt)。text 为空串表示这一步不读;role 为 null 表示旁白。
|
|
35
|
+
*/
|
|
36
|
+
export function spokenLine(step, caption, speaker) {
|
|
37
|
+
if (step.narrate?.trim())
|
|
38
|
+
return { text: step.narrate.trim(), role: null };
|
|
39
|
+
if (step.say?.trim())
|
|
40
|
+
return { text: step.say.trim(), role: speaker };
|
|
41
|
+
if (step.action.type === 'card')
|
|
42
|
+
return { text: joinSpoken([step.action.title, step.action.sub]), role: null };
|
|
43
|
+
return { text: caption.trim(), role: null };
|
|
44
|
+
}
|
|
45
|
+
/** 这一步要读的文字(不关心是谁读)。 */
|
|
46
|
+
export function narrationText(step, caption) {
|
|
47
|
+
return spokenLine(step, caption, null).text;
|
|
48
|
+
}
|
|
49
|
+
/** 第 index 步(1 起)的说话角色:用户操作取这一步之后导演所在层画面的角色 id;没有角色、系统步骤与过渡卡为 null。say() 用它的声音。 */
|
|
50
|
+
export function speakerRoleAt(project, scenarioId, index) {
|
|
51
|
+
const step = project.scenarios[scenarioId]?.steps[index - 1];
|
|
52
|
+
if (!step || step.kind !== 'user')
|
|
53
|
+
return null;
|
|
54
|
+
const layer = cameraAt(project, scenarioId, index).directorLayer;
|
|
55
|
+
const sceneId = layersOf(project, scenarioId).find((l) => l.id === layer)?.sceneId;
|
|
56
|
+
return roleOfScene(project, sceneId)?.id ?? null;
|
|
57
|
+
}
|
|
58
|
+
/** 53 位的字符串哈希(cyrb53),两个种子拼成 26 位十六进制;同步、无依赖,网页与服务端结果一致。 */
|
|
59
|
+
function cyrb53(s, seed) {
|
|
60
|
+
let h1 = 0xdeadbeef ^ seed;
|
|
61
|
+
let h2 = 0x41c6ce57 ^ seed;
|
|
62
|
+
for (let i = 0; i < s.length; i++) {
|
|
63
|
+
const ch = s.charCodeAt(i);
|
|
64
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
65
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
66
|
+
}
|
|
67
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
68
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
69
|
+
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
70
|
+
}
|
|
71
|
+
/** 音频片段的键:声音指纹 + 规整后的朗读文字(空白合并)。 */
|
|
72
|
+
export function clipKey(fingerprint, text) {
|
|
73
|
+
const s = `${fingerprint}\n${text.replace(/\s+/g, ' ').trim()}`;
|
|
74
|
+
return `c${cyrb53(s, 1).toString(16).padStart(14, '0')}${cyrb53(s, 7).toString(16).padStart(14, '0')}`;
|
|
75
|
+
}
|
|
76
|
+
/** 某个角色(null = 旁白)用哪个声音指纹;角色没有单独的声音时用旁白的。 */
|
|
77
|
+
export function voiceFingerprint(voices, roleId) {
|
|
78
|
+
return (roleId ? voices.roles[roleId] : undefined) ?? voices.narrator;
|
|
79
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@joplo-app/core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.12",
|
|
4
4
|
"description": "Joplo 编译器、校验器与播放器(IR v2)",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"dist"
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@joplo-app/kit": "0.2.
|
|
22
|
+
"@joplo-app/kit": "0.2.12",
|
|
23
23
|
"elkjs": "0.12.1",
|
|
24
24
|
"zod": "4.6.7"
|
|
25
25
|
},
|