@joplo-app/core 0.2.18 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions.d.ts +6 -0
- package/dist/actions.js +34 -0
- package/dist/agent-skill.d.ts +9 -15
- package/dist/agent-skill.js +41 -44
- package/dist/auto-caption.d.ts +3 -0
- package/dist/auto-caption.js +33 -0
- package/dist/catalog.d.ts +39 -42
- package/dist/catalog.js +27 -329
- package/dist/compile.d.ts +2 -8
- package/dist/compile.js +225 -393
- package/dist/content-i18n.js +27 -16
- package/dist/feature-diff.d.ts +9 -9
- package/dist/feature-diff.js +28 -6
- package/dist/focus.d.ts +3 -1
- package/dist/focus.js +16 -34
- package/dist/guide.d.ts +10 -12
- package/dist/guide.js +363 -591
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/media.d.ts +10 -0
- package/dist/media.js +43 -0
- package/dist/node.d.ts +1 -2
- package/dist/node.js +1 -2
- package/dist/outline.js +1 -1
- package/dist/pace.d.ts +4 -1
- package/dist/pace.js +7 -3
- package/dist/player.d.ts +8 -41
- package/dist/player.js +83 -147
- package/dist/playground.d.ts +38 -0
- package/dist/playground.js +111 -0
- package/dist/plugin-data.d.ts +11 -0
- package/dist/plugin-data.js +96 -0
- package/dist/plugins/compiler.d.ts +66 -0
- package/dist/plugins/compiler.js +428 -0
- package/dist/plugins/data.d.ts +12 -0
- package/dist/plugins/data.js +120 -0
- package/dist/result.d.ts +1 -1
- package/dist/result.js +1 -0
- package/dist/runtime.d.ts +22 -0
- package/dist/runtime.js +18 -0
- package/dist/schema.d.ts +590 -112
- package/dist/schema.js +60 -52
- package/dist/scope.js +14 -5
- package/dist/step-identity.d.ts +1 -1
- package/dist/step-identity.js +1 -1
- package/dist/themes.d.ts +7 -0
- package/dist/themes.js +5 -0
- package/dist/traits.d.ts +7 -0
- package/dist/traits.js +26 -0
- package/dist/validate.js +17 -4
- package/package.json +7 -4
- package/dist/diagram/elk.d.ts +0 -6
- package/dist/diagram/elk.js +0 -56
- package/dist/diagram/graph-layout.d.ts +0 -123
- package/dist/diagram/graph-layout.js +0 -620
- package/dist/diagram/index.d.ts +0 -41
- package/dist/diagram/index.js +0 -92
- package/dist/diagram/model.d.ts +0 -217
- package/dist/diagram/model.js +0 -189
- package/dist/diagram/sequence-layout.d.ts +0 -12
- package/dist/diagram/sequence-layout.js +0 -185
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
import { PluginAction } from "../schema.js";
|
|
2
|
+
import { ElementTraitsSchema, InheritSchema } from "../traits.js";
|
|
3
|
+
import { advanceMedia } from "../media.js";
|
|
4
|
+
import { jsonData, snapshot, object, strings, syncResult, authoringData, materialize, errorMessage } from "./data.js";
|
|
5
|
+
/** Node-side plugin host. Callbacks never enter IR or the player. */
|
|
6
|
+
import { isNode } from '@joplo-app/kit/node';
|
|
7
|
+
import { PLUGIN_API_VERSION } from '@joplo-app/kit/plugin';
|
|
8
|
+
import { initialState, applyStep, resolveProps } from "../player.js";
|
|
9
|
+
export function enabledPlugins(configured) {
|
|
10
|
+
return Array.isArray(configured) ? configured : [];
|
|
11
|
+
}
|
|
12
|
+
function validMediaCommand(value, plugin, owners) {
|
|
13
|
+
if (!object(value) || typeof value.track !== 'string' || !/^[a-z][a-z0-9-]*$/.test(value.track))
|
|
14
|
+
return false;
|
|
15
|
+
if (value.fadeMs !== undefined && (typeof value.fadeMs !== 'number' || value.fadeMs < 0 || value.fadeMs > 30000))
|
|
16
|
+
return false;
|
|
17
|
+
if (value.action === 'stop')
|
|
18
|
+
return Object.keys(value).every(k => ['track', 'action', 'fadeMs'].includes(k));
|
|
19
|
+
if (value.action !== 'play' || typeof value.resource !== 'string' || owners.get(value.resource) !== plugin)
|
|
20
|
+
return false;
|
|
21
|
+
return Object.keys(value).every(k => ['track', 'action', 'resource', 'volume', 'loop', 'fadeMs', 'offsetMs'].includes(k))
|
|
22
|
+
&& (value.offsetMs === undefined || typeof value.offsetMs === 'number' && Number.isFinite(value.offsetMs) && value.offsetMs >= 0)
|
|
23
|
+
&& (value.volume === undefined || typeof value.volume === 'number' && value.volume >= 0 && value.volume <= 1)
|
|
24
|
+
&& (value.loop === undefined || typeof value.loop === 'boolean');
|
|
25
|
+
}
|
|
26
|
+
export class PluginCompiler {
|
|
27
|
+
report;
|
|
28
|
+
options;
|
|
29
|
+
intrinsic = new Map();
|
|
30
|
+
inherits = new Map();
|
|
31
|
+
themes = {};
|
|
32
|
+
properties = new Map();
|
|
33
|
+
mediaCommands = new Map();
|
|
34
|
+
resourceIds = new Map();
|
|
35
|
+
resourceOwners = new Map();
|
|
36
|
+
defs = new Map();
|
|
37
|
+
info = {};
|
|
38
|
+
constructor(configured, report, source, options = {}) {
|
|
39
|
+
this.report = report;
|
|
40
|
+
this.options = options;
|
|
41
|
+
if (configured !== undefined && !Array.isArray(configured))
|
|
42
|
+
report('PLUGIN_INVALID', 'config({ plugins }) must be a list of plugins', source);
|
|
43
|
+
for (const raw of enabledPlugins(configured)) {
|
|
44
|
+
const p = raw;
|
|
45
|
+
if (!p || typeof p !== 'object' || p.kind !== 'plugin' || typeof p.id !== 'string' || !p.components || typeof p.components !== 'object' || Array.isArray(p.components)) {
|
|
46
|
+
report('PLUGIN_INVALID', 'config({ plugins }) entries must be made with definePlugin', source);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (this.defs.has(p.id)) {
|
|
50
|
+
report('PLUGIN_DUPLICATE', `Plugin ${p.id} is enabled twice in config({ plugins })`, source);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (p.apiVersion !== PLUGIN_API_VERSION) {
|
|
54
|
+
report('PLUGIN_INVALID', `Plugin ${p.id} targets API ${p.apiVersion}; supported: ${PLUGIN_API_VERSION}`, source);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
this.defs.set(p.id, p);
|
|
58
|
+
const properties = this.envelope(p, p.properties ?? {}, source);
|
|
59
|
+
if (!object(properties)) {
|
|
60
|
+
this.invalid(p, source);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
for (const [key, spec] of Object.entries(properties)) {
|
|
64
|
+
if (!/^[A-Za-z][A-Za-z0-9]*$/.test(key) || ['id', 'children', 'style', 'className', 'when', 'disabled', 'redact', 'note'].includes(key)
|
|
65
|
+
|| !object(spec) || !['string', 'number', 'boolean'].includes(String(spec.type))
|
|
66
|
+
|| Object.keys(spec).some(k => !['type', 'attribute', 'min', 'max', 'nonempty', 'values'].includes(k))
|
|
67
|
+
|| spec.values !== undefined && (spec.type !== 'string' || !Array.isArray(spec.values) || !spec.values.length || spec.values.some(v => typeof v !== 'string' || !v))
|
|
68
|
+
|| spec.attribute !== undefined && (typeof spec.attribute !== 'string' || !spec.attribute.startsWith(`data-jp-${p.id}-`) || !/^data-jp-[a-z0-9-]+$/.test(spec.attribute))
|
|
69
|
+
|| ['min', 'max'].some(k => spec[k] !== undefined && (spec.type !== 'number' || typeof spec[k] !== 'number'))
|
|
70
|
+
|| spec.nonempty !== undefined && (spec.type !== 'string' || typeof spec.nonempty !== 'boolean')
|
|
71
|
+
|| typeof spec.min === 'number' && typeof spec.max === 'number' && spec.min > spec.max) {
|
|
72
|
+
this.report('PLUGIN_PROPERTY_INVALID', `Plugin ${p.id}: invalid declaration for property ${key}`, source);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (this.properties.has(key) || spec.attribute && [...this.properties.values()].some(v => v.spec.attribute === spec.attribute)) {
|
|
76
|
+
this.report('PLUGIN_PROPERTY_CONFLICT', `Plugin ${p.id}: property ${key} or its DOM attribute is already registered`, source);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
this.properties.set(key, { plugin: p, spec: spec });
|
|
80
|
+
}
|
|
81
|
+
const actions = {};
|
|
82
|
+
const rawActions = this.envelope(p, p.actions ?? {}, source);
|
|
83
|
+
if (!object(rawActions))
|
|
84
|
+
report('PLUGIN_ACTION_INVALID', `Plugin ${p.id}: actions must be a record`, source);
|
|
85
|
+
else
|
|
86
|
+
for (const [name, raw] of Object.entries(rawActions)) {
|
|
87
|
+
const parsed = PluginAction.safeParse(raw);
|
|
88
|
+
if (!/^[a-z][a-zA-Z0-9-]*$/.test(name) || !parsed.success)
|
|
89
|
+
report('PLUGIN_ACTION_INVALID', `Plugin ${p.id}: invalid action ${name}`, source);
|
|
90
|
+
else
|
|
91
|
+
actions[name] = parsed.data;
|
|
92
|
+
}
|
|
93
|
+
const themes = this.envelope(p, p.themes ?? {}, source);
|
|
94
|
+
if (!object(themes))
|
|
95
|
+
this.invalid(p, source);
|
|
96
|
+
else
|
|
97
|
+
for (const [name, css] of Object.entries(themes)) {
|
|
98
|
+
if (!/^[a-z][a-z0-9-]*$/.test(name) || typeof css !== 'string')
|
|
99
|
+
this.report('PLUGIN_THEME_INVALID', `Plugin ${p.id}: themes need lowercase names and CSS strings`, source);
|
|
100
|
+
else
|
|
101
|
+
this.themes[`${p.id}:${name}`] = css;
|
|
102
|
+
}
|
|
103
|
+
const checked = {};
|
|
104
|
+
for (const [name, c] of Object.entries(p.components)) {
|
|
105
|
+
const type = `${p.id}:${name}`;
|
|
106
|
+
if (c.traits !== undefined) {
|
|
107
|
+
const parsed = ElementTraitsSchema.safeParse(this.envelope(p, c.traits, source));
|
|
108
|
+
if (!parsed.success)
|
|
109
|
+
report('PLUGIN_TRAITS_INVALID', `Plugin ${p.id}: invalid traits for ${name}: ${parsed.error.message}`, source);
|
|
110
|
+
else
|
|
111
|
+
checked[name] = parsed.data;
|
|
112
|
+
}
|
|
113
|
+
if (c.inherit !== undefined) {
|
|
114
|
+
const parsed = InheritSchema.safeParse(this.envelope(p, c.inherit, source));
|
|
115
|
+
if (!parsed.success)
|
|
116
|
+
report('PLUGIN_TRAITS_INVALID', `Plugin ${p.id}: invalid property inheritance for ${name}`, source);
|
|
117
|
+
else
|
|
118
|
+
this.inherits.set(type, parsed.data);
|
|
119
|
+
}
|
|
120
|
+
if (c.intrinsic !== undefined) {
|
|
121
|
+
if (!/^[a-z][a-z0-9]*$/.test(c.intrinsic) || this.intrinsic.has(c.intrinsic))
|
|
122
|
+
report('PLUGIN_TRAITS_INVALID', `Plugin ${p.id}: invalid or duplicate intrinsic ${c.intrinsic}`, source);
|
|
123
|
+
else
|
|
124
|
+
this.intrinsic.set(c.intrinsic, type);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
this.info[p.id] = { ...(Object.keys(actions).length ? { actions } : {}), ...(object(themes) && Object.keys(themes).length ? { themes: themes } : {}), ...(Object.keys(properties).length ? { properties: properties } : {}), ...(p.data ? { data: this.envelope(p, p.data, source) } : {}), id: p.id, title: p.title, ...(p.version ? { version: p.version } : {}), components: Object.fromEntries(Object.entries(p.components).map(([name, c]) => [name, { ...(checked[name] ? { traits: checked[name] } : {}), ...(c?.interactive ? { interactive: true } : {}), ...(c?.parent ? { parent: c.parent } : {}), ...(c?.needsId ? { needsId: true } : {}), ...(c?.selfEvident ? { selfEvident: true } : {}) }])) };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
run(p, hook, at, fn) {
|
|
131
|
+
try {
|
|
132
|
+
return fn();
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
this.report('PLUGIN_HOOK_FAILED', `Plugin ${p.id} ${hook}: ${errorMessage(e)}`, at);
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
output(p, value, at, authoring = false) {
|
|
140
|
+
const synchronous = syncResult(value);
|
|
141
|
+
try {
|
|
142
|
+
return materialize(synchronous, authoring ? 'authoring' : 'hook');
|
|
143
|
+
}
|
|
144
|
+
catch (e) {
|
|
145
|
+
this.report('PLUGIN_DATA_INVALID', `Plugin ${p.id}: ${errorMessage(e)}`, at);
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
validateProperties(node) {
|
|
150
|
+
for (const [key, { plugin, spec }] of this.properties) {
|
|
151
|
+
const value = node.props[key];
|
|
152
|
+
if (value === undefined)
|
|
153
|
+
continue;
|
|
154
|
+
if (typeof value !== spec.type || typeof value === 'number' && (!Number.isFinite(value) || spec.min !== undefined && value < spec.min || spec.max !== undefined && value > spec.max)
|
|
155
|
+
|| spec.nonempty && typeof value === 'string' && !value.trim()
|
|
156
|
+
|| spec.values && !spec.values.includes(value))
|
|
157
|
+
this.report('PLUGIN_PROPERTY_INVALID', `Plugin ${plugin.id}: ${key} requires a literal ${spec.type}${spec.min !== undefined ? ` >= ${spec.min}` : ''}${spec.max !== undefined ? ` <= ${spec.max}` : ''}${spec.nonempty ? ' (nonempty)' : ''}${spec.values ? ` (one of ${spec.values.join(', ')})` : ''}`, node.source);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
resourceProps(value, source) {
|
|
161
|
+
if (Array.isArray(value))
|
|
162
|
+
return value.map(v => this.resourceProps(v, source));
|
|
163
|
+
if (!object(value))
|
|
164
|
+
return value;
|
|
165
|
+
if (value.kind === 'local-resource') {
|
|
166
|
+
const { plugin, compiler, input } = value;
|
|
167
|
+
if (typeof plugin !== 'string' || !this.defs.get(plugin)?.local || typeof compiler !== 'string' || !/^[a-z][a-z0-9-]*$/.test(compiler) || typeof input !== 'string') {
|
|
168
|
+
this.report('LOCAL_RESOURCE_INVALID', 'Enable the resource plugin in config.plugins and supply a compiler with text input', source);
|
|
169
|
+
return '';
|
|
170
|
+
}
|
|
171
|
+
const key = JSON.stringify([plugin, compiler, input]);
|
|
172
|
+
let id = this.resourceIds.get(key);
|
|
173
|
+
if (!id) {
|
|
174
|
+
id = `resource-${this.resourceIds.size}`;
|
|
175
|
+
this.resourceIds.set(key, id);
|
|
176
|
+
this.resourceOwners.set(id, plugin);
|
|
177
|
+
this.options.onResource?.({ id, plugin, compiler, input, source });
|
|
178
|
+
}
|
|
179
|
+
return `joplo-resource:${id}`;
|
|
180
|
+
}
|
|
181
|
+
return { ...value, ...Object.fromEntries(Object.entries(value).map(([k, v]) => [k, this.resourceProps(v, source)])) };
|
|
182
|
+
}
|
|
183
|
+
node(node, ids) {
|
|
184
|
+
node = { ...node, props: this.resourceProps(node.props, node.source) };
|
|
185
|
+
const p = this.defs.get(node.type.split(':')[0]);
|
|
186
|
+
if (!p) {
|
|
187
|
+
this.validateProperties(node);
|
|
188
|
+
return node;
|
|
189
|
+
}
|
|
190
|
+
const result = this.run(p, 'node', node.source, () => {
|
|
191
|
+
const n = this.output(p, p.compile?.node?.(snapshot(node), { ids: new Map(ids), report: this.report }), node.source, true);
|
|
192
|
+
if (n === undefined)
|
|
193
|
+
return node;
|
|
194
|
+
if (!isNode(n) || typeof n.type !== 'string' || !object(n.props)) {
|
|
195
|
+
this.report('PLUGIN_DATA_INVALID', `Plugin ${p.id} node must return a node with string type and object props`, node.source);
|
|
196
|
+
return node;
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
authoringData(n.props);
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
this.invalid(p, node.source);
|
|
203
|
+
return node;
|
|
204
|
+
}
|
|
205
|
+
// Only this node's type/props can be normalized; children already have checked identities.
|
|
206
|
+
return { ...node, type: n.type, props: n.props };
|
|
207
|
+
});
|
|
208
|
+
const normalized = result ?? node;
|
|
209
|
+
this.validateProperties(normalized);
|
|
210
|
+
return normalized;
|
|
211
|
+
}
|
|
212
|
+
derive(scene) {
|
|
213
|
+
const visit = (node) => {
|
|
214
|
+
const p = this.defs.get(node.type.split(':')[0]);
|
|
215
|
+
if (p?.compile?.derive) {
|
|
216
|
+
this.run(p, 'derive', node.source, () => {
|
|
217
|
+
const result = this.output(p, p.compile.derive(snapshot(node), { viewport: scene.viewport, report: this.report, options: snapshot(this.options.pluginOptions?.[p.id] ?? {}) }), node.source);
|
|
218
|
+
if (result === undefined)
|
|
219
|
+
return;
|
|
220
|
+
if (!object(result) || Object.keys(result).some(k => k !== 'data' && k !== 'framing')) {
|
|
221
|
+
this.invalid(p, node.source);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
if (result.data !== undefined) {
|
|
225
|
+
const data = this.checkData(p, 'node', result.data, node.source);
|
|
226
|
+
if (data !== undefined)
|
|
227
|
+
node.ext = { ...node.ext, [p.id]: data };
|
|
228
|
+
}
|
|
229
|
+
if (result.framing !== undefined) {
|
|
230
|
+
const nodes = new Map();
|
|
231
|
+
const collect = (n) => { nodes.set(n.id, n); n.children?.forEach(collect); };
|
|
232
|
+
collect(node);
|
|
233
|
+
const framing = this.envelope(p, result.framing, node.source);
|
|
234
|
+
if (!object(framing) || Object.entries(framing).some(([id, f]) => !nodes.has(id) || !object(f) || Object.keys(f).some(k => k !== 'include' && k !== 'kind') || f.include !== undefined && !strings(f.include) || f.kind !== undefined && !['root', 'group', 'block', 'content', 'edge', 'row'].includes(String(f.kind)))) {
|
|
235
|
+
this.invalid(p, node.source);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
for (const [id, frame] of Object.entries(framing))
|
|
239
|
+
nodes.get(id).framing = frame;
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
for (const c of node.children ?? [])
|
|
244
|
+
visit(c);
|
|
245
|
+
};
|
|
246
|
+
visit(scene.root);
|
|
247
|
+
// Identities are stage-local and remain an invariant after every derivation pass.
|
|
248
|
+
const ids = new Set();
|
|
249
|
+
const checkIdentity = (n) => {
|
|
250
|
+
if (typeof n.id !== 'string' || !n.id)
|
|
251
|
+
this.report('MISSING_ID', 'Derived node requires a nonempty id', n.source);
|
|
252
|
+
else if (ids.has(n.id))
|
|
253
|
+
this.report('DUPLICATE_ID', `Duplicate id "${n.id}" after plugin derivation in ${scene.id}`, n.source);
|
|
254
|
+
ids.add(n.id);
|
|
255
|
+
for (const child of n.children ?? [])
|
|
256
|
+
checkIdentity(child);
|
|
257
|
+
};
|
|
258
|
+
checkIdentity(scene.root);
|
|
259
|
+
}
|
|
260
|
+
effect(effect, scenes, data, source) {
|
|
261
|
+
const p = this.defs.get(effect.plugin);
|
|
262
|
+
if (!p?.compile?.effect) {
|
|
263
|
+
this.report('PLUGIN_EFFECT_UNKNOWN', `Plugin ${effect.plugin} has no effect compiler for ${effect.name}`, source);
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
return this.run(p, 'effect', source, () => {
|
|
267
|
+
const result = this.output(p, p.compile.effect(snapshot(effect), { roots: snapshot(scenes.map(s => s.root)), scenes: snapshot(scenes.map(s => ({ id: s.id, root: s.root }))), data: snapshot(data), source, report: this.report, resourceInfo: id => this.resourceOwners.get(id) === p.id ? snapshot(this.options.resourceInfo?.[id]) : undefined, resource: request => {
|
|
268
|
+
request = materialize(request);
|
|
269
|
+
if (!request || typeof request.compiler !== 'string' || !/^[a-z][a-z0-9-]*$/.test(request.compiler) || typeof request.input !== 'string' || Object.keys(request).some(k => k !== 'compiler' && k !== 'input'))
|
|
270
|
+
throw new Error('resource requires compiler and text input');
|
|
271
|
+
const key = JSON.stringify([p.id, request.compiler, request.input]);
|
|
272
|
+
let id = this.resourceIds.get(key);
|
|
273
|
+
if (!id) {
|
|
274
|
+
id = `resource-${this.resourceIds.size}`;
|
|
275
|
+
this.resourceIds.set(key, id);
|
|
276
|
+
this.resourceOwners.set(id, p.id);
|
|
277
|
+
this.options.onResource?.({ id, plugin: p.id, compiler: request.compiler, input: request.input, ...(source ? { source } : {}) });
|
|
278
|
+
}
|
|
279
|
+
return id;
|
|
280
|
+
} }), source);
|
|
281
|
+
if (result === undefined)
|
|
282
|
+
return;
|
|
283
|
+
if (!object(result) || Object.keys(result).some(k => !['data', 'targets', 'references', 'media', 'writes', 'directorTarget'].includes(k)) || result.targets !== undefined && !strings(result.targets) || result.references !== undefined && !strings(result.references)) {
|
|
284
|
+
this.invalid(p, source);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
if (result.media !== undefined && (!Array.isArray(result.media) || result.media.some(c => !validMediaCommand(c, p.id, this.resourceOwners)))) {
|
|
288
|
+
this.invalid(p, source);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
if (result.writes !== undefined && (!Array.isArray(result.writes) || result.writes.some(w => !object(w) || Object.keys(w).some(k => k !== 'path' && k !== 'value') || !Object.hasOwn(w, 'value') || typeof w.path !== 'string' || !/^(ui|data|inputs)(\.[A-Za-z0-9_-]+)+$/.test(w.path) || w.path.split('.').some(k => ['__proto__', 'prototype', 'constructor'].includes(k))))) {
|
|
292
|
+
this.invalid(p, source);
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (result.directorTarget !== undefined && (typeof result.directorTarget !== 'string' || !result.directorTarget)) {
|
|
296
|
+
this.invalid(p, source);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
const writes = result.writes === undefined ? undefined : this.envelope(p, result.writes, source);
|
|
300
|
+
if (result.writes !== undefined && writes === undefined)
|
|
301
|
+
return;
|
|
302
|
+
const media = result.media?.map(c => ({ ...c, plugin: p.id }));
|
|
303
|
+
const value = result.data === undefined ? undefined : this.envelope(p, result.data, source);
|
|
304
|
+
return { ...(writes ? { writes } : {}), ...(result.directorTarget ? { directorTarget: result.directorTarget } : {}), ...(media ? { media } : {}), ...(value !== undefined ? { data: value } : {}), ...(result.targets ? { targets: [...result.targets] } : {}), ...(result.references ? { references: [...result.references] } : {}) };
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
frames(project) {
|
|
308
|
+
// 接力(flow({ from }),D92):先算被接力的一章,下一章第 0 步继承它结尾的配乐(推进它最后一步的停留估算)
|
|
309
|
+
const ended = new Map();
|
|
310
|
+
const order = [];
|
|
311
|
+
const visit = (sc, seen = new Set()) => {
|
|
312
|
+
if (order.includes(sc) || seen.has(sc.id))
|
|
313
|
+
return;
|
|
314
|
+
seen.add(sc.id);
|
|
315
|
+
const prev = sc.from ? project.scenarios[sc.from] : undefined;
|
|
316
|
+
if (prev)
|
|
317
|
+
visit(prev, seen);
|
|
318
|
+
order.push(sc);
|
|
319
|
+
};
|
|
320
|
+
for (const sc of Object.values(project.scenarios))
|
|
321
|
+
visit(sc);
|
|
322
|
+
for (const sc of order) {
|
|
323
|
+
const scenes = Object.values(project.scenes).filter(s => s.optionId === sc.optionId || s.optionId == null).map(s => ({ id: s.id, root: s.root }));
|
|
324
|
+
const roots = scenes.map(s => s.root);
|
|
325
|
+
const prev = sc.from ? project.scenarios[sc.from] : undefined;
|
|
326
|
+
let media = prev && ended.has(prev.id) ? advanceMedia(ended.get(prev.id), prev.steps.length ? prev.steps[prev.steps.length - 1].hold ?? 3000 : 0) : {};
|
|
327
|
+
const hasMedia = Object.keys(media).length > 0 || sc.steps.some(step => this.mediaCommands.has(step.id));
|
|
328
|
+
let previousState;
|
|
329
|
+
let state = initialState(project, sc.id);
|
|
330
|
+
let previousData = {};
|
|
331
|
+
for (let i = 0; i <= sc.steps.length; i++) {
|
|
332
|
+
if (i) {
|
|
333
|
+
try {
|
|
334
|
+
state = applyStep(project, sc, state);
|
|
335
|
+
}
|
|
336
|
+
catch { /* Invalid author steps are diagnosed by replay/check; compilation must still return IR. */ }
|
|
337
|
+
}
|
|
338
|
+
const target = i ? sc.steps[i - 1] : sc;
|
|
339
|
+
const ext = { ...target.ext };
|
|
340
|
+
if (i) {
|
|
341
|
+
media = advanceMedia(media, i > 1 ? (sc.steps[i - 2].hold ?? 3000) : 0);
|
|
342
|
+
for (const c of this.mediaCommands.get(sc.steps[i - 1].id) ?? []) {
|
|
343
|
+
const key = `${c.plugin}:${c.track}`;
|
|
344
|
+
const current = media[key];
|
|
345
|
+
if (c.action === 'play' && current?.resource === c.resource && c.offsetMs === undefined) {
|
|
346
|
+
// 同一首再 play:继续播放(含取消正在进行的淡出),只更新音量与循环
|
|
347
|
+
const { stopAfterMs: _fading, ...kept } = current;
|
|
348
|
+
media[key] = { ...kept, volume: c.volume ?? 1, loop: c.loop ?? false };
|
|
349
|
+
}
|
|
350
|
+
else if (c.action === 'play') {
|
|
351
|
+
// 换曲:有 fadeMs 时旧曲换到「轨道>旧资源」键上同时淡出,与新曲交叉,中间没有静音
|
|
352
|
+
if (current && c.fadeMs)
|
|
353
|
+
media[`${key}>${current.resource}`] = { ...current, stopAfterMs: Math.min(current.stopAfterMs ?? c.fadeMs, c.fadeMs) };
|
|
354
|
+
media[key] = { plugin: c.plugin, resource: c.resource, offsetMs: c.offsetMs ?? 0, ...(c.offsetMs !== undefined ? { startOffsetMs: c.offsetMs } : {}), volume: c.volume ?? 1, loop: c.loop ?? false, fadeInMs: c.fadeMs ?? 0 };
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
for (const k of Object.keys(media))
|
|
358
|
+
if (k === key || k.startsWith(`${key}>`)) {
|
|
359
|
+
if (c.fadeMs)
|
|
360
|
+
media[k] = { ...media[k], stopAfterMs: Math.min(media[k].stopAfterMs ?? c.fadeMs, c.fadeMs) };
|
|
361
|
+
else
|
|
362
|
+
delete media[k];
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (hasMedia)
|
|
368
|
+
target.media = structuredClone(media);
|
|
369
|
+
for (const p of this.defs.values()) {
|
|
370
|
+
if (p.compile?.frame) {
|
|
371
|
+
const value = this.run(p, 'frame', target.source, () => this.output(p, p.compile.frame({ roots: snapshot(roots), scenes: snapshot(scenes), state: snapshot(state), previousState: snapshot(previousState), resolveProps: (node, previous) => resolveProps(node, snapshot(previous && previousState ? previousState : state)), data: snapshot(target.ext?.[p.id]), previousData: snapshot(previousData[p.id]), position: i, report: this.report }), target.source));
|
|
372
|
+
if (value !== undefined)
|
|
373
|
+
ext[p.id] = value;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
// Validate every final slot, including effect data left unchanged by an abstaining frame.
|
|
377
|
+
const validated = {};
|
|
378
|
+
for (const [id, value] of Object.entries(ext)) {
|
|
379
|
+
const p = this.defs.get(id);
|
|
380
|
+
if (!p) {
|
|
381
|
+
this.report('PLUGIN_DATA_INVALID', `Unregistered plugin slot ${id}`, target.source);
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
const data = this.checkData(p, 'step', value, target.source);
|
|
385
|
+
if (data !== undefined)
|
|
386
|
+
validated[id] = data;
|
|
387
|
+
}
|
|
388
|
+
if (Object.keys(validated).length)
|
|
389
|
+
target.ext = validated;
|
|
390
|
+
else
|
|
391
|
+
delete target.ext;
|
|
392
|
+
previousData = validated;
|
|
393
|
+
previousState = state;
|
|
394
|
+
}
|
|
395
|
+
ended.set(sc.id, media);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
invalid(p, at) { this.report('PLUGIN_DATA_INVALID', `Plugin ${p.id}: invalid hook output envelope`, at); }
|
|
399
|
+
envelope(p, value, at) {
|
|
400
|
+
try {
|
|
401
|
+
return jsonData(value);
|
|
402
|
+
}
|
|
403
|
+
catch (e) {
|
|
404
|
+
this.report('PLUGIN_DATA_INVALID', `Plugin ${p.id}: ${errorMessage(e)}`, at);
|
|
405
|
+
return undefined;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
checkData(p, slot, value, at) {
|
|
409
|
+
if (value === undefined)
|
|
410
|
+
return;
|
|
411
|
+
const data = this.envelope(p, value, at);
|
|
412
|
+
if (data === undefined)
|
|
413
|
+
return;
|
|
414
|
+
return this.run(p, `${slot} schema`, at, () => {
|
|
415
|
+
const schema = p.schemas?.[slot];
|
|
416
|
+
if (!schema) {
|
|
417
|
+
this.report('PLUGIN_DATA_INVALID', `Plugin ${p.id} writes ${slot} data without a schema`, at);
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
const r = syncResult(schema.safeParse(snapshot(data)));
|
|
421
|
+
if (!r || r.success !== true) {
|
|
422
|
+
this.report('PLUGIN_DATA_INVALID', `Plugin ${p.id} ${slot}: ${r?.error?.message ?? 'invalid data'}`, at);
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
return data;
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Capture descriptors once; validation and consumers only see this host-owned, accessor-free copy. */
|
|
2
|
+
export declare function materialize<T>(value: T, mode?: 'hook' | 'authoring' | 'json'): T;
|
|
3
|
+
/** Host-owned JSON envelope; no second read of plugin objects or toJSON execution. */
|
|
4
|
+
export declare const jsonData: (value: unknown) => unknown;
|
|
5
|
+
export declare function errorMessage(error: unknown): string;
|
|
6
|
+
/** Authoring bindings/functions are opaque; JSON objects/arrays are copied and frozen. */
|
|
7
|
+
export declare function snapshot<T>(value: T): T;
|
|
8
|
+
export declare function object(value: unknown): value is Record<string, unknown>;
|
|
9
|
+
export declare const strings: (v: unknown) => v is string[];
|
|
10
|
+
export declare function syncResult<T>(value: T): T;
|
|
11
|
+
/** Bindings/undefined/functions are authoring values; cycles are never valid props. */
|
|
12
|
+
export declare function authoringData(value: unknown): void;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { BIND, NODE, isBind } from '@joplo-app/kit/node';
|
|
2
|
+
/** Capture descriptors once; validation and consumers only see this host-owned, accessor-free copy. */
|
|
3
|
+
export function materialize(value, mode = 'hook') {
|
|
4
|
+
const active = new Set();
|
|
5
|
+
// Cache the source description, never an output subtree: each occurrence gets its
|
|
6
|
+
// own copy and is checked at its actual depth in the expanded JSON tree.
|
|
7
|
+
const captured = new Map();
|
|
8
|
+
const walk = (v, depth) => {
|
|
9
|
+
if (depth > 100)
|
|
10
|
+
throw new Error('plugin data must have depth <= 100 after expanding shared references');
|
|
11
|
+
if (v === null || typeof v === 'string' || typeof v === 'boolean')
|
|
12
|
+
return v;
|
|
13
|
+
if (v === undefined && mode !== 'json')
|
|
14
|
+
return v;
|
|
15
|
+
if (typeof v === 'number' && Number.isFinite(v))
|
|
16
|
+
return v;
|
|
17
|
+
// Authoring $ bindings are callable proxies. Read their path once, then use a plain marker.
|
|
18
|
+
if (typeof v === 'function' && mode === 'authoring') {
|
|
19
|
+
const binding = v[BIND];
|
|
20
|
+
if (typeof binding === 'string')
|
|
21
|
+
return Object.freeze({ [BIND]: binding });
|
|
22
|
+
}
|
|
23
|
+
if (!v || typeof v !== 'object' || active.has(v))
|
|
24
|
+
throw new Error('plugin data must be finite, acyclic data (depth <= 100)');
|
|
25
|
+
let description = captured.get(v);
|
|
26
|
+
if (!description) {
|
|
27
|
+
const array = Array.isArray(v);
|
|
28
|
+
if (!array && ![Object.prototype, null].includes(Object.getPrototypeOf(v)))
|
|
29
|
+
throw new Error('plugin data must contain only plain JSON objects');
|
|
30
|
+
description = { array, descriptors: Object.getOwnPropertyDescriptors(v) };
|
|
31
|
+
captured.set(v, description);
|
|
32
|
+
}
|
|
33
|
+
active.add(v);
|
|
34
|
+
const { array, descriptors } = description;
|
|
35
|
+
const out = array ? [] : {};
|
|
36
|
+
for (const key of Reflect.ownKeys(descriptors)) {
|
|
37
|
+
const descriptor = descriptors[key];
|
|
38
|
+
if (!Object.hasOwn(descriptor, 'value'))
|
|
39
|
+
throw new Error('plugin output accessors are not supported; return plain data');
|
|
40
|
+
if (array && key === 'length')
|
|
41
|
+
continue;
|
|
42
|
+
if (typeof key === 'symbol' && (mode !== 'authoring' || key !== NODE && key !== BIND))
|
|
43
|
+
throw new Error('plugin data cannot contain symbol keys');
|
|
44
|
+
if (mode !== 'authoring' && !descriptor.enumerable)
|
|
45
|
+
throw new Error('plugin data must use enumerable data properties');
|
|
46
|
+
if (array && (typeof key !== 'string' || !/^(0|[1-9]\d*)$/.test(key)))
|
|
47
|
+
throw new Error('plugin arrays must contain only indexed data');
|
|
48
|
+
Object.defineProperty(out, key, { value: walk(descriptor.value, depth + 1), enumerable: descriptor.enumerable, configurable: true, writable: true });
|
|
49
|
+
}
|
|
50
|
+
if (array) {
|
|
51
|
+
const length = descriptors.length.value;
|
|
52
|
+
for (let i = 0; i < length; i++)
|
|
53
|
+
if (!Object.hasOwn(out, i))
|
|
54
|
+
throw new Error('plugin data cannot contain sparse arrays');
|
|
55
|
+
}
|
|
56
|
+
active.delete(v);
|
|
57
|
+
return out;
|
|
58
|
+
};
|
|
59
|
+
return walk(value, 0);
|
|
60
|
+
}
|
|
61
|
+
/** Host-owned JSON envelope; no second read of plugin objects or toJSON execution. */
|
|
62
|
+
export const jsonData = (value) => materialize(value, 'json');
|
|
63
|
+
export function errorMessage(error) {
|
|
64
|
+
try {
|
|
65
|
+
return error instanceof Error ? String(error.message) : String(error);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return 'Plugin returned or threw an unreadable value';
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** Authoring bindings/functions are opaque; JSON objects/arrays are copied and frozen. */
|
|
72
|
+
export function snapshot(value) {
|
|
73
|
+
const seen = new Map();
|
|
74
|
+
const copy = (v) => {
|
|
75
|
+
if (!v || typeof v !== 'object')
|
|
76
|
+
return v;
|
|
77
|
+
if (seen.has(v))
|
|
78
|
+
return seen.get(v);
|
|
79
|
+
const out = Array.isArray(v) ? [] : {};
|
|
80
|
+
seen.set(v, out);
|
|
81
|
+
for (const key of Reflect.ownKeys(v))
|
|
82
|
+
if (key !== 'length')
|
|
83
|
+
Object.defineProperty(out, key, { value: copy(v[key]), enumerable: Object.getOwnPropertyDescriptor(v, key)?.enumerable ?? true, configurable: true, writable: true });
|
|
84
|
+
return Object.freeze(out);
|
|
85
|
+
};
|
|
86
|
+
return copy(value);
|
|
87
|
+
}
|
|
88
|
+
export function object(value) { return !!value && typeof value === 'object' && !Array.isArray(value); }
|
|
89
|
+
export const strings = (v) => Array.isArray(v) && v.every(x => typeof x === 'string' && x.length > 0);
|
|
90
|
+
export function syncResult(value) {
|
|
91
|
+
if (value && typeof value === 'object' && 'then' in value) {
|
|
92
|
+
// Handle real promises (including another realm) without invoking a result's then getter.
|
|
93
|
+
try {
|
|
94
|
+
void Promise.prototype.then.call(value, undefined, () => undefined);
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
return value;
|
|
98
|
+
} // Plain thenables/accessors are rejected by materialization.
|
|
99
|
+
throw new Error('Plugin compile hooks and schemas must return synchronously');
|
|
100
|
+
}
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
/** Bindings/undefined/functions are authoring values; cycles are never valid props. */
|
|
104
|
+
export function authoringData(value) {
|
|
105
|
+
const active = new Set();
|
|
106
|
+
const walk = (v, depth) => {
|
|
107
|
+
if (isBind(v) || v === undefined || v === null || typeof v === 'function' || ['string', 'boolean'].includes(typeof v))
|
|
108
|
+
return;
|
|
109
|
+
if (typeof v === 'number' && Number.isFinite(v))
|
|
110
|
+
return;
|
|
111
|
+
if (!v || typeof v !== 'object' || depth > 100 || active.has(v))
|
|
112
|
+
throw new Error('node props must be finite, acyclic authoring data');
|
|
113
|
+
if (!Array.isArray(v) && ![Object.prototype, null].includes(Object.getPrototypeOf(v)))
|
|
114
|
+
throw new Error('node props must contain plain objects');
|
|
115
|
+
active.add(v);
|
|
116
|
+
Object.values(v).forEach(x => walk(x, depth + 1));
|
|
117
|
+
active.delete(v);
|
|
118
|
+
};
|
|
119
|
+
walk(value, 0);
|
|
120
|
+
}
|
package/dist/result.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** 所有入口共享的领域结果、诊断与错误分类(PRD 12.5)。 */
|
|
2
|
-
export declare const ERROR_CODES: readonly ["INVALID_SCHEMA", "NOT_FOUND", "REVISION_CONFLICT", "INVALID_REFERENCE", "UNSUPPORTED_ACTION", "PLAYBACK_FAILED", "RENDER_FAILED", "EXPORT_FAILED", "EXPORT_COMPONENT_MISSING", "UNSUPPORTED_SCHEMA_VERSION", "PERMISSION_DENIED", "IDEMPOTENCY_MISMATCH", "USAGE"];
|
|
2
|
+
export declare const ERROR_CODES: readonly ["INVALID_SCHEMA", "PLUGIN_SCOPE_UNSUPPORTED", "NOT_FOUND", "REVISION_CONFLICT", "INVALID_REFERENCE", "UNSUPPORTED_ACTION", "PLAYBACK_FAILED", "RENDER_FAILED", "EXPORT_FAILED", "EXPORT_COMPONENT_MISSING", "UNSUPPORTED_SCHEMA_VERSION", "PERMISSION_DENIED", "IDEMPOTENCY_MISMATCH", "USAGE"];
|
|
3
3
|
export type ErrorCode = (typeof ERROR_CODES)[number];
|
|
4
4
|
export type Severity = 'error' | 'warning' | 'info';
|
|
5
5
|
export type DiagnosticSource = 'schema' | 'reference' | 'structure' | 'replay' | 'render';
|
package/dist/result.js
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Browser-safe engine entry: IR, state, focus and projection; no compiler or plugin author entries. */
|
|
2
|
+
export * from './catalog.js';
|
|
3
|
+
export type * from './schema.js';
|
|
4
|
+
export * from './result.js';
|
|
5
|
+
export * from './player.js';
|
|
6
|
+
export * from './focus.js';
|
|
7
|
+
export type { Selection } from './selection.js';
|
|
8
|
+
export * from './embed.js';
|
|
9
|
+
export * from './features.js';
|
|
10
|
+
export * from './review.js';
|
|
11
|
+
export * from './scope.js';
|
|
12
|
+
export * from './pace.js';
|
|
13
|
+
export * from './outline.js';
|
|
14
|
+
export * from './voice.js';
|
|
15
|
+
export * from './content-i18n.js';
|
|
16
|
+
export * from './local-review.js';
|
|
17
|
+
export type * from './step-identity.js';
|
|
18
|
+
export { nodeResourceIds, resolveResourceProps, mediaAt, mediaResourceIds } from './media.js';
|
|
19
|
+
export { projectThemes } from './themes.js';
|
|
20
|
+
export type { FeatureDiffManifest, DiffScenario, DiffStep } from './feature-diff.js';
|
|
21
|
+
export { autoCaptionMessages, automaticCaption } from './auto-caption.js';
|
|
22
|
+
export * from './playground.js';
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Browser-safe engine entry: IR, state, focus and projection; no compiler or plugin author entries. */
|
|
2
|
+
export * from "./catalog.js";
|
|
3
|
+
export * from "./result.js";
|
|
4
|
+
export * from "./player.js";
|
|
5
|
+
export * from "./focus.js";
|
|
6
|
+
export * from "./embed.js";
|
|
7
|
+
export * from "./features.js";
|
|
8
|
+
export * from "./review.js";
|
|
9
|
+
export * from "./scope.js";
|
|
10
|
+
export * from "./pace.js";
|
|
11
|
+
export * from "./outline.js";
|
|
12
|
+
export * from "./voice.js";
|
|
13
|
+
export * from "./content-i18n.js";
|
|
14
|
+
export * from "./local-review.js";
|
|
15
|
+
export { nodeResourceIds, resolveResourceProps, mediaAt, mediaResourceIds } from "./media.js";
|
|
16
|
+
export { projectThemes } from "./themes.js";
|
|
17
|
+
export { autoCaptionMessages, automaticCaption } from "./auto-caption.js";
|
|
18
|
+
export * from "./playground.js";
|