@lumesync/studio 0.0.0-stage → 1.0.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/README.md +229 -2
- package/bin/lumesync-studio.js +7 -0
- package/package.json +33 -4
- package/skills/courseware-authoring/SKILL.md +226 -0
- package/skills/courseware-authoring/references/tools.md +133 -0
- package/src/chat/mailbox.js +307 -0
- package/src/cli/main.js +669 -0
- package/src/core/document.js +349 -0
- package/src/core/errors.js +44 -0
- package/src/core/exporter.js +272 -0
- package/src/core/model.js +305 -0
- package/src/core/rules.js +288 -0
- package/src/core/storage.js +616 -0
- package/src/core/transaction.js +250 -0
- package/src/core/validate.js +503 -0
- package/src/core/zip.js +251 -0
- package/src/editing/operations.js +671 -0
- package/src/editing/session.js +838 -0
- package/src/server/http.js +944 -0
- package/src/server/ws.js +128 -0
- package/src/templates/courseware.js +48 -0
- package/src/templates/init.js +374 -0
- package/src/tools/registry.js +487 -0
- package/ui/app.js +2671 -0
- package/ui/home.html +32 -0
- package/ui/home.js +148 -0
- package/ui/index.html +210 -0
- package/ui/shared/edit-model.js +105 -0
- package/ui/shared/quiz-view.js +455 -0
- package/ui/style.css +1161 -0
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
// 课件包校验:Schema 等价规则 + 教学法 lint。
|
|
2
|
+
// 规则来源:crates/lume-courseware/src/{validate,lint}.rs。二者必须一致;
|
|
3
|
+
// 不一致时以 Rust 的行为为准,并补充共享测试向量。
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
SPEC_VERSION, RUNTIME_VERSION, MAX_SCENE_PROSE, MAX_STATE_BYTES, ASPECT_RATIOS,
|
|
7
|
+
isPackageId, isSemver, isSceneId, isObjectiveId, isComponentCid, isInteractionId,
|
|
8
|
+
isGrade, isSceneFile, isInteractionFile, isVersionRange, satisfiesRange,
|
|
9
|
+
charLen, htmlProseChars, proseChars, frontmatterId, htmlSafetyIssues,
|
|
10
|
+
} from '../core/rules.js';
|
|
11
|
+
import { PHASE_VALUES, LAYOUT_VALUES, RUN_MODE_VALUES, BLOOM_VALUES, SUBJECT_VALUES, COMPONENT_VALUES, COLLECT_VALUES, policyIssues } from './model.js';
|
|
12
|
+
|
|
13
|
+
export const MANIFEST_FIELDS = [
|
|
14
|
+
'specVersion', 'id', 'version', 'title', 'subject', 'grade', 'durationMin', 'locale', 'runtime',
|
|
15
|
+
'authors', 'tags', 'source', 'objectives', 'prerequisites', 'theme', 'aspectRatio', 'scenes',
|
|
16
|
+
'interactions', 'script', 'ai', 'assets', 'compliance', 'checksums',
|
|
17
|
+
];
|
|
18
|
+
export const SCENE_FIELDS = [
|
|
19
|
+
// `steps` 仅用于兼容旧课件:读取时允许存在,但不再参与校验、编辑或播放。
|
|
20
|
+
'id', 'file', 'title', 'phase', 'minutes', 'layout', 'steps', 'modes', 'components', 'notes', 'hidden',
|
|
21
|
+
];
|
|
22
|
+
export const COMPONENT_FIELDS = ['cid', 'type', 'props', 'state', 'collect', 'sandbox', 'role'];
|
|
23
|
+
export const INTERACTION_REF_FIELDS = ['id', 'file', 'scene', 'points', 'graded', 'policy'];
|
|
24
|
+
export const SOURCE_FIELDS = ['kind', 'ref'];
|
|
25
|
+
export const THEME_FIELDS = ['name', 'tokens'];
|
|
26
|
+
export const AI_FIELDS = ['mode', 'sceneAi', 'context', 'quota'];
|
|
27
|
+
export const AI_QUOTA_FIELDS = ['turnsPerStudent', 'tokensPerSession'];
|
|
28
|
+
export const ASSET_FIELDS = ['path', 'bytes', 'sha256'];
|
|
29
|
+
export const COMPLIANCE_FIELDS = ['studentDataCollected', 'requiresNetwork'];
|
|
30
|
+
export const OBJECTIVE_FIELDS = ['id', 'text', 'bloom'];
|
|
31
|
+
|
|
32
|
+
function diagnostic(code, level, path, message) {
|
|
33
|
+
return { code, level, path, message };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function unknownFields(value, allowed, path, error) {
|
|
37
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return;
|
|
38
|
+
for (const key of Object.keys(value)) {
|
|
39
|
+
if (!allowed.includes(key)) error('S032', `${path}.${key}`, `未知字段 ${key} 不在 specVersion 1.1 允许范围内`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function sceneList(manifest) {
|
|
44
|
+
return Array.isArray(manifest.scenes) ? manifest.scenes : [];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function validateManifest(manifest) {
|
|
48
|
+
const items = [];
|
|
49
|
+
const error = (code, path, message) => items.push(diagnostic(code, 'error', path, message));
|
|
50
|
+
const warning = (code, path, message) => items.push(diagnostic(code, 'warning', path, message));
|
|
51
|
+
|
|
52
|
+
if (!manifest || typeof manifest !== 'object') {
|
|
53
|
+
error('S001', 'manifest.json', '清单不是对象');
|
|
54
|
+
return items;
|
|
55
|
+
}
|
|
56
|
+
unknownFields(manifest, MANIFEST_FIELDS, 'manifest.json', error);
|
|
57
|
+
if (manifest.source !== undefined) {
|
|
58
|
+
if (!manifest.source || typeof manifest.source !== 'object' || Array.isArray(manifest.source)) {
|
|
59
|
+
error('S032', 'manifest.json#source', 'source 必须是对象');
|
|
60
|
+
} else {
|
|
61
|
+
unknownFields(manifest.source, SOURCE_FIELDS, 'manifest.json#source', error);
|
|
62
|
+
if (manifest.source.kind !== undefined && !['native', 'pptx-import', 'agent-generated'].includes(manifest.source.kind)) {
|
|
63
|
+
error('S032', 'manifest.json#source.kind', 'source.kind 必须是 native / pptx-import / agent-generated');
|
|
64
|
+
}
|
|
65
|
+
if (manifest.source.ref !== undefined && typeof manifest.source.ref !== 'string') {
|
|
66
|
+
error('S032', 'manifest.json#source.ref', 'source.ref 必须是字符串');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (manifest.theme !== undefined) {
|
|
71
|
+
if (!manifest.theme || typeof manifest.theme !== 'object' || Array.isArray(manifest.theme)) {
|
|
72
|
+
error('S032', 'manifest.json#theme', 'theme 必须是对象');
|
|
73
|
+
} else {
|
|
74
|
+
unknownFields(manifest.theme, THEME_FIELDS, 'manifest.json#theme', error);
|
|
75
|
+
if (manifest.theme.name !== undefined && typeof manifest.theme.name !== 'string') error('S032', 'manifest.json#theme.name', 'theme.name 必须是字符串');
|
|
76
|
+
if (manifest.theme.tokens !== undefined && typeof manifest.theme.tokens !== 'string') error('S032', 'manifest.json#theme.tokens', 'theme.tokens 必须是字符串');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (manifest.specVersion !== SPEC_VERSION) {
|
|
81
|
+
error('S001', 'manifest.json#specVersion', `当前渲染器只接受 specVersion ${SPEC_VERSION}`);
|
|
82
|
+
}
|
|
83
|
+
if (!isPackageId(manifest.id)) {
|
|
84
|
+
error('S002', 'manifest.json#id', 'id 必须是小写字母/数字组成的点分层级(长度 6-96)');
|
|
85
|
+
}
|
|
86
|
+
if (manifest.version !== undefined && !isSemver(manifest.version)) {
|
|
87
|
+
error('S003', 'manifest.json#version', 'version 必须是 x.y.z 形式');
|
|
88
|
+
}
|
|
89
|
+
const titleLength = charLen(manifest.title ?? '');
|
|
90
|
+
if (titleLength < 2 || titleLength > 60) {
|
|
91
|
+
error('S004', 'manifest.json#title', '标题长度必须是 2-60 个字符');
|
|
92
|
+
}
|
|
93
|
+
if (!SUBJECT_VALUES.includes(manifest.subject)) {
|
|
94
|
+
error('S005', 'manifest.json#subject', `学科必须是 ${SUBJECT_VALUES.join(' / ')} 之一`);
|
|
95
|
+
}
|
|
96
|
+
if (!isGrade(manifest.grade)) {
|
|
97
|
+
error('S006', 'manifest.json#grade', '年级格式不合法,示例:七年级 / 小学四年级 / 高二');
|
|
98
|
+
}
|
|
99
|
+
if (!Number.isInteger(manifest.durationMin) || manifest.durationMin < 10 || manifest.durationMin > 90) {
|
|
100
|
+
error('S007', 'manifest.json#durationMin', '课时长度必须是 10-90 分钟');
|
|
101
|
+
}
|
|
102
|
+
if (manifest.aspectRatio !== undefined && !ASPECT_RATIOS.includes(manifest.aspectRatio)) {
|
|
103
|
+
error('S030', 'manifest.json#aspectRatio', `展示比例必须是 ${ASPECT_RATIOS.join(' / ')} 之一`);
|
|
104
|
+
}
|
|
105
|
+
const runtime = manifest.runtime ?? '>=1.1 <2.0';
|
|
106
|
+
if (!isVersionRange(runtime)) {
|
|
107
|
+
error('S008', 'manifest.json#runtime', 'runtime 不是合法的版本范围');
|
|
108
|
+
} else if (!satisfiesRange(runtime, RUNTIME_VERSION)) {
|
|
109
|
+
error('S008', 'manifest.json#runtime', `runtime \`${runtime}\` 不包含当前渲染器版本 ${RUNTIME_VERSION}`);
|
|
110
|
+
}
|
|
111
|
+
if (manifest.authors !== undefined) {
|
|
112
|
+
if (!Array.isArray(manifest.authors) || manifest.authors.some((item) => typeof item !== 'string')) {
|
|
113
|
+
error('S014', 'manifest.json#authors', '作者必须是字符串数组');
|
|
114
|
+
} else if (manifest.authors.length > 10) {
|
|
115
|
+
error('S014', 'manifest.json#authors', '作者最多 10 个');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const tags = Array.isArray(manifest.tags) ? manifest.tags : [];
|
|
119
|
+
if (manifest.tags !== undefined && !Array.isArray(manifest.tags)) {
|
|
120
|
+
error('S013', 'manifest.json#tags', 'tags 必须是数组');
|
|
121
|
+
}
|
|
122
|
+
if (tags.length > 12) error('S013', 'manifest.json#tags', '标签最多 12 个');
|
|
123
|
+
tags.forEach((tag, index) => {
|
|
124
|
+
if (typeof tag !== 'string' || charLen(tag) > 24) error('S013', `manifest.json#tags[${index}]`, '单个标签不能超过 24 个字符');
|
|
125
|
+
});
|
|
126
|
+
const prerequisites = Array.isArray(manifest.prerequisites) ? manifest.prerequisites : [];
|
|
127
|
+
if (manifest.prerequisites !== undefined && !Array.isArray(manifest.prerequisites)) {
|
|
128
|
+
error('S012', 'manifest.json#prerequisites', '前置知识必须是数组');
|
|
129
|
+
}
|
|
130
|
+
if (prerequisites.length > 5) error('S012', 'manifest.json#prerequisites', '前置知识最多 5 条');
|
|
131
|
+
prerequisites.forEach((item, index) => {
|
|
132
|
+
if (typeof item !== 'string' || charLen(item) > 80) error('S012', `manifest.json#prerequisites[${index}]`, '单条前置知识不能超过 80 个字符');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
validateObjectives(manifest, error);
|
|
136
|
+
validateScenes(manifest, error, warning);
|
|
137
|
+
validateInteractions(manifest, error);
|
|
138
|
+
validateAssets(manifest, error);
|
|
139
|
+
validateAi(manifest, error);
|
|
140
|
+
|
|
141
|
+
if (manifest.script !== undefined && manifest.script !== 'script.json') {
|
|
142
|
+
error('S026', 'manifest.json#script', 'script 只能指向 script.json');
|
|
143
|
+
}
|
|
144
|
+
if (manifest.checksums !== undefined && manifest.checksums !== 'checksums.json') {
|
|
145
|
+
error('S027', 'manifest.json#checksums', 'checksums 只能指向 checksums.json');
|
|
146
|
+
}
|
|
147
|
+
return items;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function validateObjectives(manifest, error) {
|
|
151
|
+
const objectives = manifest.objectives;
|
|
152
|
+
if (!Array.isArray(objectives) || objectives.length < 1 || objectives.length > 8) {
|
|
153
|
+
error('S009', 'manifest.json#objectives', '教学目标数量必须是 1-8 条');
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const seen = new Set();
|
|
157
|
+
for (const objective of objectives) {
|
|
158
|
+
if (!objective || typeof objective !== 'object' || Array.isArray(objective)) {
|
|
159
|
+
error('S010', 'manifest.json#objectives', '目标必须是对象');
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
unknownFields(objective, OBJECTIVE_FIELDS, `manifest.json#objectives[${objective.id ?? '?'}]`, error);
|
|
163
|
+
if (!isObjectiveId(objective.id)) {
|
|
164
|
+
error('S010', `manifest.json#objectives[${objective.id}]`, '目标 id 必须形如 o1 / o12');
|
|
165
|
+
}
|
|
166
|
+
if (seen.has(objective.id)) {
|
|
167
|
+
error('S010', `manifest.json#objectives[${objective.id}]`, '目标 id 重复');
|
|
168
|
+
}
|
|
169
|
+
seen.add(objective.id);
|
|
170
|
+
const length = charLen(objective.text ?? '');
|
|
171
|
+
if (length < 4 || length > 80) {
|
|
172
|
+
error('S011', `manifest.json#objectives[${objective.id}].text`, '目标描述长度必须是 4-80 个字符');
|
|
173
|
+
}
|
|
174
|
+
if (!BLOOM_VALUES.includes(objective.bloom)) {
|
|
175
|
+
error('S011', `manifest.json#objectives[${objective.id}].bloom`, `bloom 必须是 ${BLOOM_VALUES.join(' / ')}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function validateScenes(manifest, error, warning) {
|
|
181
|
+
const scenes = manifest.scenes;
|
|
182
|
+
if (!Array.isArray(scenes) || scenes.length < 1 || scenes.length > 120) {
|
|
183
|
+
error('S015', 'manifest.json#scenes', '场景数量必须是 1-120 个');
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const ids = new Set();
|
|
187
|
+
const files = new Set();
|
|
188
|
+
for (const scene of scenes) {
|
|
189
|
+
if (!scene || typeof scene !== 'object' || Array.isArray(scene)) {
|
|
190
|
+
error('S016', 'manifest.json#scenes', '场景必须是对象');
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
unknownFields(scene, SCENE_FIELDS, `manifest.json#scenes[${scene.id ?? '?'}]`, error);
|
|
194
|
+
if (!isSceneId(scene.id)) {
|
|
195
|
+
error('S016', `manifest.json#scenes[${scene.id}]`, '场景 id 必须形如 s1 / s12 / s123');
|
|
196
|
+
}
|
|
197
|
+
if (ids.has(scene.id)) error('S016', `manifest.json#scenes[${scene.id}]`, '场景 id 重复');
|
|
198
|
+
ids.add(scene.id);
|
|
199
|
+
if (!isSceneFile(scene.file)) {
|
|
200
|
+
error('S017', `manifest.json#scenes[${scene.id}].file`, '场景文件必须位于 scenes/ 且以 .md 或 .html 结尾');
|
|
201
|
+
}
|
|
202
|
+
if (files.has(scene.file)) error('S017', `manifest.json#scenes[${scene.id}].file`, '多个场景指向同一个文件');
|
|
203
|
+
files.add(scene.file);
|
|
204
|
+
if (scene.title !== undefined && charLen(scene.title) > 40) {
|
|
205
|
+
error('S018', `manifest.json#scenes[${scene.id}].title`, '场景标题不能超过 40 个字符');
|
|
206
|
+
}
|
|
207
|
+
if (!PHASE_VALUES.includes(scene.phase)) {
|
|
208
|
+
error('S019', `manifest.json#scenes[${scene.id}].phase`, `阶段必须是 ${PHASE_VALUES.join(' / ')}`);
|
|
209
|
+
}
|
|
210
|
+
if (typeof scene.minutes !== 'number' || !Number.isFinite(scene.minutes) || scene.minutes < 0.5 || scene.minutes > 30) {
|
|
211
|
+
error('S019', `manifest.json#scenes[${scene.id}].minutes`, '场景时长必须是 0.5-30 分钟');
|
|
212
|
+
}
|
|
213
|
+
if (scene.layout !== undefined && !LAYOUT_VALUES.includes(scene.layout)) {
|
|
214
|
+
error('S021', `manifest.json#scenes[${scene.id}].layout`, `layout 必须是 ${LAYOUT_VALUES.join(' / ')}`);
|
|
215
|
+
}
|
|
216
|
+
if (scene.modes !== undefined) {
|
|
217
|
+
if (!Array.isArray(scene.modes) || scene.modes.length === 0) {
|
|
218
|
+
error('S021', `manifest.json#scenes[${scene.id}].modes`, 'modes 至少声明一个运行态');
|
|
219
|
+
} else {
|
|
220
|
+
const unique = new Set();
|
|
221
|
+
for (const mode of scene.modes) {
|
|
222
|
+
if (!RUN_MODE_VALUES.includes(mode)) error('S021', `manifest.json#scenes[${scene.id}].modes`, `未知运行态 ${mode}`);
|
|
223
|
+
if (unique.has(mode)) error('S021', `manifest.json#scenes[${scene.id}].modes`, 'modes 不能重复');
|
|
224
|
+
unique.add(mode);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
validateComponents(scene, manifest, error, warning);
|
|
229
|
+
if (scene.phase === 'practice') {
|
|
230
|
+
const primary = (scene.components ?? []).filter((component) => (component.role ?? 'primary') === 'primary').length;
|
|
231
|
+
if (primary === 0) warning('L006', scene.file, 'practice 场景没有 primary 交互组件,学生可能无事可做');
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function validateComponents(scene, manifest, error, warning) {
|
|
237
|
+
if (scene.components !== undefined && !Array.isArray(scene.components)) {
|
|
238
|
+
error('S022', `manifest.json#scenes[${scene.id}].components`, 'components 必须是数组');
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const components = scene.components ?? [];
|
|
242
|
+
if (components.length > 6) {
|
|
243
|
+
error('S022', `manifest.json#scenes[${scene.id}].components`, '单个场景最多 6 个组件');
|
|
244
|
+
}
|
|
245
|
+
if (components.length > 2) {
|
|
246
|
+
warning('L003', scene.file, `场景内有 ${components.length} 个组件,超过 2 个容易失焦`);
|
|
247
|
+
}
|
|
248
|
+
const cids = new Set();
|
|
249
|
+
for (const component of components) {
|
|
250
|
+
if (!component || typeof component !== 'object' || Array.isArray(component)) {
|
|
251
|
+
error('S023', `manifest.json#scenes[${scene.id}].components`, '组件必须是对象');
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
unknownFields(component, COMPONENT_FIELDS, `manifest.json#scenes[${scene.id}].components[${component.cid ?? '?'}]`, error);
|
|
255
|
+
if (component.props !== undefined && (typeof component.props !== 'object' || component.props === null || Array.isArray(component.props))) {
|
|
256
|
+
error('S024', `manifest.json#scenes[${scene.id}].components[${component.cid}].props`, 'props 必须是对象');
|
|
257
|
+
}
|
|
258
|
+
if (!isComponentCid(component.cid)) {
|
|
259
|
+
error('S023', `manifest.json#scenes[${scene.id}].components[${component.cid}]`, '组件 cid 必须是小写字母开头、最多 16 位的字母数字');
|
|
260
|
+
}
|
|
261
|
+
if (cids.has(component.cid)) {
|
|
262
|
+
error('S023', `manifest.json#scenes[${scene.id}].components[${component.cid}]`, '同一场景内组件 cid 重复');
|
|
263
|
+
}
|
|
264
|
+
cids.add(component.cid);
|
|
265
|
+
if (!COMPONENT_VALUES.includes(component.type)) {
|
|
266
|
+
error('S023', `manifest.json#scenes[${scene.id}].components[${component.cid}]`, `组件类型不在白名单:${component.type}`);
|
|
267
|
+
}
|
|
268
|
+
if (component.state !== undefined) {
|
|
269
|
+
if (typeof component.state !== 'object' || Array.isArray(component.state)) {
|
|
270
|
+
error('S024', `manifest.json#scenes[${scene.id}].components[${component.cid}].state`, '初始状态必须是对象');
|
|
271
|
+
} else {
|
|
272
|
+
if (Object.keys(component.state).length > 64) {
|
|
273
|
+
error('S024', `manifest.json#scenes[${scene.id}].components[${component.cid}].state`, '初始状态最多 64 个属性');
|
|
274
|
+
}
|
|
275
|
+
const bytes = Buffer.byteLength(JSON.stringify(component.state), 'utf8');
|
|
276
|
+
if (bytes > MAX_STATE_BYTES) {
|
|
277
|
+
error('L008', scene.file, `组件 ${component.cid} 的初始状态 ${bytes} 字节,超过 ${MAX_STATE_BYTES} 字节预算`);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (component.collect === undefined) {
|
|
282
|
+
error('L009', scene.file, `组件 ${component.cid} 未显式声明 collect`);
|
|
283
|
+
} else if (!COLLECT_VALUES.includes(component.collect)) {
|
|
284
|
+
error('L009', scene.file, `组件 ${component.cid} 的 collect 不合法`);
|
|
285
|
+
}
|
|
286
|
+
if (component.type === 'lume-ai') {
|
|
287
|
+
const allowList = manifest.ai?.sceneAi ?? [];
|
|
288
|
+
if (!allowList.includes(scene.id)) {
|
|
289
|
+
error('L016', scene.file, `场景 ${scene.id} 含 lume-ai 组件,但未列入 ai.sceneAi 白名单`);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const primary = components.filter((component) => (component.role ?? 'primary') === 'primary').length;
|
|
294
|
+
if (primary > 2) warning('L004', scene.file, `场景内有 ${primary} 个 primary 组件,建议不超过 2 个`);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function validateInteractions(manifest, error) {
|
|
298
|
+
if (manifest.interactions !== undefined && !Array.isArray(manifest.interactions)) {
|
|
299
|
+
error('S025', 'manifest.json#interactions', 'interactions 必须是数组');
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
const interactions = manifest.interactions ?? [];
|
|
303
|
+
if (interactions.length > 40) error('S025', 'manifest.json#interactions', '交互题目最多 40 个');
|
|
304
|
+
const ids = new Set();
|
|
305
|
+
for (const interaction of interactions) {
|
|
306
|
+
if (!interaction || typeof interaction !== 'object' || Array.isArray(interaction)) {
|
|
307
|
+
error('S025', 'manifest.json#interactions', '题目索引必须是对象');
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
unknownFields(interaction, INTERACTION_REF_FIELDS, `manifest.json#interactions[${interaction.id ?? '?'}]`, error);
|
|
311
|
+
if (!isInteractionId(interaction.id)) {
|
|
312
|
+
error('S025', `manifest.json#interactions[${interaction.id}]`, '交互 id 必须是 3-40 位小写字母/数字/-/_');
|
|
313
|
+
}
|
|
314
|
+
if (ids.has(interaction.id)) error('S025', `manifest.json#interactions[${interaction.id}]`, '交互 id 重复');
|
|
315
|
+
ids.add(interaction.id);
|
|
316
|
+
if (!isInteractionFile(interaction.file)) {
|
|
317
|
+
error('S025', `manifest.json#interactions[${interaction.id}].file`, '交互文件必须位于 interactions/ 且以 .json 结尾');
|
|
318
|
+
}
|
|
319
|
+
if (!isSceneId(interaction.scene)) {
|
|
320
|
+
error('S025', `manifest.json#interactions[${interaction.id}].scene`, '交互所属场景 id 不合法');
|
|
321
|
+
} else if (!sceneList(manifest).some((scene) => scene.id === interaction.scene)) {
|
|
322
|
+
error('S031', `manifest.json#interactions[${interaction.id}].scene`, `引用了不存在的场景 \`${interaction.scene}\``);
|
|
323
|
+
}
|
|
324
|
+
const points = interaction.points ?? 1;
|
|
325
|
+
if (typeof points !== 'number' || !Number.isFinite(points) || points < 0 || points > 100) {
|
|
326
|
+
error('S025', `manifest.json#interactions[${interaction.id}].points`, '分值必须是 0-100 的有限数');
|
|
327
|
+
}
|
|
328
|
+
for (const issue of policyIssues(interaction.policy, `manifest.json#interactions[${interaction.id}].policy`)) {
|
|
329
|
+
error('S025', `manifest.json#interactions[${interaction.id}].policy`, issue);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function validateAssets(manifest, error) {
|
|
335
|
+
if (manifest.assets !== undefined && !Array.isArray(manifest.assets)) {
|
|
336
|
+
error('S028', 'manifest.json#assets', 'assets 必须是数组');
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
(manifest.assets ?? []).forEach((asset, index) => {
|
|
340
|
+
if (!asset || typeof asset !== 'object' || Array.isArray(asset)) {
|
|
341
|
+
error('S028', `manifest.json#assets[${index}]`, '资源声明必须是对象');
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
unknownFields(asset, ASSET_FIELDS, `manifest.json#assets[${index}]`, error);
|
|
345
|
+
if (typeof asset.path !== 'string' || !asset.path.startsWith('assets/')) {
|
|
346
|
+
error('S028', `manifest.json#assets[${index}].path`, '资源路径必须以 assets/ 开头');
|
|
347
|
+
}
|
|
348
|
+
if (asset.bytes !== undefined && (!Number.isInteger(asset.bytes) || asset.bytes < 0)) {
|
|
349
|
+
error('S028', `manifest.json#assets[${index}].bytes`, 'bytes 必须是非负整数');
|
|
350
|
+
}
|
|
351
|
+
if (asset.sha256 !== undefined && !/^[a-f0-9]{64}$/.test(asset.sha256)) {
|
|
352
|
+
error('S028', `manifest.json#assets[${index}].sha256`, 'sha256 必须是 64 位小写十六进制');
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function validateAi(manifest, error) {
|
|
358
|
+
const ai = manifest.ai;
|
|
359
|
+
if (!ai) return;
|
|
360
|
+
if (typeof ai !== 'object' || Array.isArray(ai)) {
|
|
361
|
+
error('S029', 'manifest.json#ai', 'ai 必须是对象');
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
unknownFields(ai, AI_FIELDS, 'manifest.json#ai', error);
|
|
365
|
+
if (ai.mode !== undefined && !['off', 'hint_only', 'open'].includes(ai.mode)) {
|
|
366
|
+
error('S029', 'manifest.json#ai.mode', 'ai.mode 必须是 off / hint_only / open');
|
|
367
|
+
}
|
|
368
|
+
const sceneAi = Array.isArray(ai.sceneAi) ? ai.sceneAi : [];
|
|
369
|
+
if (ai.sceneAi !== undefined && !Array.isArray(ai.sceneAi)) {
|
|
370
|
+
error('S029', 'manifest.json#ai.sceneAi', 'ai.sceneAi 必须是数组');
|
|
371
|
+
}
|
|
372
|
+
if (sceneAi.length > 20) error('S029', 'manifest.json#ai.sceneAi', 'AI 场景白名单最多 20 个');
|
|
373
|
+
for (const sceneId of sceneAi) {
|
|
374
|
+
if (!isSceneId(sceneId) || !sceneList(manifest).some((scene) => scene.id === sceneId)) {
|
|
375
|
+
error('S029', `manifest.json#ai.sceneAi[${sceneId}]`, 'AI 场景白名单引用了不存在的场景');
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
if (Array.isArray(ai.context)) {
|
|
379
|
+
const unique = new Set();
|
|
380
|
+
for (const item of ai.context) {
|
|
381
|
+
if (unique.has(item)) error('S029', 'manifest.json#ai.context', 'AI 上下文类型不能重复');
|
|
382
|
+
unique.add(item);
|
|
383
|
+
}
|
|
384
|
+
} else if (ai.context !== undefined) {
|
|
385
|
+
error('S029', 'manifest.json#ai.context', 'ai.context 必须是数组');
|
|
386
|
+
}
|
|
387
|
+
if (ai.quota) {
|
|
388
|
+
if (typeof ai.quota !== 'object' || Array.isArray(ai.quota)) {
|
|
389
|
+
error('S029', 'manifest.json#ai.quota', 'ai.quota 必须是对象');
|
|
390
|
+
} else {
|
|
391
|
+
unknownFields(ai.quota, AI_QUOTA_FIELDS, 'manifest.json#ai.quota', error);
|
|
392
|
+
if (ai.quota.turnsPerStudent !== undefined && (!Number.isInteger(ai.quota.turnsPerStudent) || ai.quota.turnsPerStudent < 0 || ai.quota.turnsPerStudent > 200)) {
|
|
393
|
+
error('S029', 'manifest.json#ai.quota.turnsPerStudent', '单生轮次上限必须是 0-200 的整数');
|
|
394
|
+
}
|
|
395
|
+
if (ai.quota.tokensPerSession !== undefined && (!Number.isInteger(ai.quota.tokensPerSession) || ai.quota.tokensPerSession < 0 || ai.quota.tokensPerSession > 2000000)) {
|
|
396
|
+
error('S029', 'manifest.json#ai.quota.tokensPerSession', '会话 token 上限必须是 0-2,000,000 的整数');
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
if (manifest.compliance !== undefined) {
|
|
401
|
+
if (!manifest.compliance || typeof manifest.compliance !== 'object' || Array.isArray(manifest.compliance)) {
|
|
402
|
+
error('S032', 'manifest.json#compliance', 'compliance 必须是对象');
|
|
403
|
+
} else {
|
|
404
|
+
unknownFields(manifest.compliance, COMPLIANCE_FIELDS, 'manifest.json#compliance', error);
|
|
405
|
+
const collected = manifest.compliance.studentDataCollected ?? [];
|
|
406
|
+
const allowed = ['answer', 'annotation', 'screen', 'app_usage', 'component_state', 'ai_dialogue'];
|
|
407
|
+
if (!Array.isArray(collected)) error('S032', 'manifest.json#compliance.studentDataCollected', 'studentDataCollected 必须是数组');
|
|
408
|
+
else {
|
|
409
|
+
for (const kind of collected) {
|
|
410
|
+
if (!allowed.includes(kind)) error('S032', `manifest.json#compliance.studentDataCollected[${kind}]`, '未知的学生数据类型');
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (manifest.compliance.requiresNetwork !== undefined && typeof manifest.compliance.requiresNetwork !== 'boolean') {
|
|
414
|
+
error('S032', 'manifest.json#compliance.requiresNetwork', 'requiresNetwork 必须是布尔值');
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (ai.mode === 'off' && sceneAi.length > 0) {
|
|
419
|
+
// 与 Rust 一致:这是警告而非错误。
|
|
420
|
+
// 由于此处只有 error 收集器,交由 lint 阶段补充。
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// 教学法 lint:需要场景正文与可选 script。
|
|
425
|
+
export function lintCourseware(manifest, sceneTexts, script = null) {
|
|
426
|
+
const items = [];
|
|
427
|
+
const error = (code, path, message) => items.push(diagnostic(code, 'error', path, message));
|
|
428
|
+
const warning = (code, path, message) => items.push(diagnostic(code, 'warning', path, message));
|
|
429
|
+
const scenes = Array.isArray(manifest.scenes) ? manifest.scenes : [];
|
|
430
|
+
|
|
431
|
+
const total = scenes.reduce((sum, scene) => sum + (scene && typeof scene === 'object' ? scene.minutes ?? 0 : 0), 0);
|
|
432
|
+
const declared = manifest.durationMin ?? 0;
|
|
433
|
+
const tolerance = Math.max(declared * 0.1, 3);
|
|
434
|
+
if (Math.abs(total - declared) > tolerance) {
|
|
435
|
+
warning('L001', 'manifest.json#scenes', `场景时长合计 ${total} 分钟,与 durationMin ${declared} 分钟偏差超过 ${tolerance.toFixed(1)} 分钟`);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (declared >= 40) {
|
|
439
|
+
for (const phase of ['engage', 'explain', 'practice', 'summary']) {
|
|
440
|
+
if (!scenes.some((scene) => scene.phase === phase)) {
|
|
441
|
+
warning('L002', 'manifest.json#scenes', `${declared} 分钟课件缺少 \`${phase}\` 场景`);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const required = Math.floor(declared / 10);
|
|
447
|
+
const operable = scenes.reduce((count, scene) => {
|
|
448
|
+
if (!scene || typeof scene !== 'object') return count;
|
|
449
|
+
return count + (scene.components ?? []).filter((component) => {
|
|
450
|
+
const role = component.role ?? 'primary';
|
|
451
|
+
return role === 'primary' || ['onDemand', 'onSubmit', 'periodic'].includes(component.collect);
|
|
452
|
+
}).length;
|
|
453
|
+
}, 0);
|
|
454
|
+
if (operable < required) {
|
|
455
|
+
warning('L005', 'manifest.json#scenes', `可操作组件 ${operable} 个,低于每 10 分钟 1 个的密度要求(至少 ${required} 个)`);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
for (const scene of scenes) {
|
|
459
|
+
if (!scene || typeof scene !== 'object') continue;
|
|
460
|
+
const text = sceneTexts?.[scene.file];
|
|
461
|
+
if (typeof text !== 'string') continue;
|
|
462
|
+
if (scene.file.endsWith('.html')) {
|
|
463
|
+
const captured = frontmatterId(text);
|
|
464
|
+
if (captured && captured !== scene.id) {
|
|
465
|
+
error('P006', scene.file, `frontmatter id \`${captured}\` 与 manifest 场景 id \`${scene.id}\` 不一致`);
|
|
466
|
+
}
|
|
467
|
+
for (const issue of htmlSafetyIssues(text)) {
|
|
468
|
+
error('H001', scene.file, issue);
|
|
469
|
+
}
|
|
470
|
+
const chars = htmlProseChars(text);
|
|
471
|
+
if (chars > MAX_SCENE_PROSE) {
|
|
472
|
+
warning('L010', scene.file, `场景正文 ${chars} 字,超过 ${MAX_SCENE_PROSE} 字的注意力预算`);
|
|
473
|
+
}
|
|
474
|
+
} else {
|
|
475
|
+
const chars = proseChars(text);
|
|
476
|
+
if (chars > MAX_SCENE_PROSE) {
|
|
477
|
+
warning('L010', scene.file, `场景正文 ${chars} 字,超过 ${MAX_SCENE_PROSE} 字的注意力预算`);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const ai = manifest.ai;
|
|
483
|
+
if (ai && ai.mode === 'off' && (ai.sceneAi ?? []).length > 0) {
|
|
484
|
+
warning('L015', 'manifest.json#ai', 'ai.mode 为 off,但 sceneAi 白名单非空,AI 组件不会生效');
|
|
485
|
+
}
|
|
486
|
+
const collectsDialogue = (manifest.compliance?.studentDataCollected ?? []).includes('ai_dialogue');
|
|
487
|
+
if (collectsDialogue && (!ai || ai.mode === 'off')) {
|
|
488
|
+
warning('L017', 'manifest.json#compliance', '声明采集 ai_dialogue,但本课件 AI 处于关闭状态');
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
return items;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
export function summarize(items) {
|
|
495
|
+
const errors = items.filter((item) => item.level === 'error');
|
|
496
|
+
const warnings = items.filter((item) => item.level === 'warning');
|
|
497
|
+
return {
|
|
498
|
+
errors: errors.length,
|
|
499
|
+
warnings: warnings.length,
|
|
500
|
+
items,
|
|
501
|
+
valid: errors.length === 0,
|
|
502
|
+
};
|
|
503
|
+
}
|