@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,487 @@
|
|
|
1
|
+
// 工具注册表:所有 Agent 入口(CLI / HTTP / MCP)共用同一份实现。
|
|
2
|
+
// 每个工具声明输入 Schema、权限级别与结果形状。
|
|
3
|
+
|
|
4
|
+
import { ChatMailbox } from '../chat/mailbox.js';
|
|
5
|
+
import { ERROR_CODES, StudioError } from '../core/errors.js';
|
|
6
|
+
import { OPERATION_TYPES } from '../editing/operations.js';
|
|
7
|
+
|
|
8
|
+
function requireString(args, name, { min = 1, max = 4096 } = {}) {
|
|
9
|
+
const value = args?.[name];
|
|
10
|
+
if (typeof value !== 'string') throw new StudioError(ERROR_CODES.OPERATION_INVALID, `${name} 必须是字符串`, { name }, 400);
|
|
11
|
+
if (value.length < min || value.length > max) {
|
|
12
|
+
throw new StudioError(ERROR_CODES.OPERATION_INVALID, `${name} 长度必须在 ${min}-${max} 之间`, { name, length: value.length }, 400);
|
|
13
|
+
}
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function clampInt(value, fallback, min, max) {
|
|
18
|
+
const number = Number(value);
|
|
19
|
+
if (!Number.isFinite(number)) return fallback;
|
|
20
|
+
return Math.min(Math.max(Math.trunc(number), min), max);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function createTools({ document, session, server, root, chat }) {
|
|
24
|
+
const tools = new Map();
|
|
25
|
+
const mailbox = chat ?? new ChatMailbox({ root });
|
|
26
|
+
|
|
27
|
+
// 提供给对话 Agent 的最小课件上下文:场景与组件标识,不包含答案材料。
|
|
28
|
+
const chatContext = () => {
|
|
29
|
+
const view = document.toAuthorView();
|
|
30
|
+
return {
|
|
31
|
+
revision: view.revision,
|
|
32
|
+
coursewareId: view.coursewareId ?? null,
|
|
33
|
+
title: view.meta?.title ?? null,
|
|
34
|
+
aspectRatio: view.meta?.aspectRatio ?? null,
|
|
35
|
+
scenes: (view.scenes ?? []).map((scene) => ({
|
|
36
|
+
id: scene.id,
|
|
37
|
+
title: scene.title,
|
|
38
|
+
file: scene.file,
|
|
39
|
+
components: (scene.components ?? []).map((component) => ({
|
|
40
|
+
cid: component.cid,
|
|
41
|
+
type: component.type,
|
|
42
|
+
...(component.label ? { label: component.label } : {}),
|
|
43
|
+
})),
|
|
44
|
+
})),
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const register = (name, definition) => {
|
|
49
|
+
tools.set(name, {
|
|
50
|
+
name,
|
|
51
|
+
description: definition.description,
|
|
52
|
+
input: definition.input,
|
|
53
|
+
highImpact: Boolean(definition.highImpact),
|
|
54
|
+
handler: definition.handler,
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
register('courseware.get', {
|
|
59
|
+
description: '读取当前课件作者视图(含章节、题目与校验摘要)',
|
|
60
|
+
input: { type: 'object', properties: {} },
|
|
61
|
+
handler: async () => ({ document: document.toAuthorView(), revision: document.revision }),
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
register('courseware.summary', {
|
|
65
|
+
description: '读取课件轻量摘要,用于 Agent 每轮快速确认当前状态',
|
|
66
|
+
input: { type: 'object', properties: {} },
|
|
67
|
+
handler: async () => ({
|
|
68
|
+
revision: document.revision,
|
|
69
|
+
exists: document.exists,
|
|
70
|
+
coursewareId: document.manifest?.id ?? null,
|
|
71
|
+
previewKey: document.previewKey,
|
|
72
|
+
title: document.manifest?.title ?? null,
|
|
73
|
+
scenes: (document.manifest?.scenes ?? []).length,
|
|
74
|
+
interactions: (document.manifest?.interactions ?? []).length,
|
|
75
|
+
report: document.report,
|
|
76
|
+
previewClients: server.previewClients ?? 0,
|
|
77
|
+
}),
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
register('preview.get_url', {
|
|
81
|
+
description: '获取浏览器预览地址与连接信息',
|
|
82
|
+
input: { type: 'object', properties: {} },
|
|
83
|
+
handler: async () => server.describe(),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
register('preview.status', {
|
|
87
|
+
description: '读取在线预览端数量与最近渲染确认',
|
|
88
|
+
input: { type: 'object', properties: {} },
|
|
89
|
+
handler: async () => ({
|
|
90
|
+
revision: document.revision,
|
|
91
|
+
previewClients: server.previewClients ?? 0,
|
|
92
|
+
describe: server.describe(),
|
|
93
|
+
}),
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
register('preview.goto', {
|
|
97
|
+
description: '让浏览器预览跳到指定场景(不修改课件)',
|
|
98
|
+
input: {
|
|
99
|
+
type: 'object',
|
|
100
|
+
properties: {
|
|
101
|
+
sceneId: { type: 'string' },
|
|
102
|
+
baseRevision: { type: 'number' },
|
|
103
|
+
},
|
|
104
|
+
required: ['sceneId'],
|
|
105
|
+
},
|
|
106
|
+
handler: async (args) => session.enqueue(async () => {
|
|
107
|
+
if (!document.exists) throw new StudioError(ERROR_CODES.COURSEWARE_INVALID, '当前没有可预览课件', undefined, 422);
|
|
108
|
+
if (Number.isInteger(args.baseRevision) && args.baseRevision !== document.revision) {
|
|
109
|
+
throw new StudioError(ERROR_CODES.REVISION_CONFLICT, `文档已更新到 revision ${document.revision}`, {
|
|
110
|
+
baseRevision: args.baseRevision,
|
|
111
|
+
currentRevision: document.revision,
|
|
112
|
+
}, 409);
|
|
113
|
+
}
|
|
114
|
+
const scene = document.sceneById(args.sceneId);
|
|
115
|
+
if (!scene) throw new StudioError(ERROR_CODES.OPERATION_INVALID, `找不到场景 ${args.sceneId}`, { sceneId: args.sceneId }, 404);
|
|
116
|
+
const receivers = server.broadcast({ type: 'preview.goto', sceneId: scene.id, revision: document.revision });
|
|
117
|
+
return { requested: { sceneId: scene.id }, revision: document.revision, previewClients: receivers };
|
|
118
|
+
}),
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
register('preview.wait_ack', {
|
|
122
|
+
description: '等待在线预览端确认渲染目标 revision',
|
|
123
|
+
input: {
|
|
124
|
+
type: 'object',
|
|
125
|
+
properties: {
|
|
126
|
+
revision: { type: 'number' },
|
|
127
|
+
sceneId: { type: 'string' },
|
|
128
|
+
timeoutMs: { type: 'number' },
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
handler: async (args) => {
|
|
132
|
+
const revision = Number.isInteger(args.revision) ? args.revision : document.revision;
|
|
133
|
+
if (revision !== document.revision) {
|
|
134
|
+
return { acknowledged: false, reason: 'REVISION_OUTDATED', currentRevision: document.revision };
|
|
135
|
+
}
|
|
136
|
+
return server.waitForAck({
|
|
137
|
+
revision,
|
|
138
|
+
sceneId: typeof args.sceneId === 'string' ? args.sceneId : null,
|
|
139
|
+
timeoutMs: Number.isInteger(args.timeoutMs) ? args.timeoutMs : 1200,
|
|
140
|
+
});
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
register('courseware.validate', {
|
|
145
|
+
description: '重新加载并执行完整校验,返回诊断列表',
|
|
146
|
+
input: { type: 'object', properties: {} },
|
|
147
|
+
handler: async () => {
|
|
148
|
+
const snapshot = await session.readDocument();
|
|
149
|
+
return { revision: snapshot.revision, report: snapshot.report };
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
register('courseware.preview_change', {
|
|
154
|
+
description: '试算编辑计划:返回 Diff 与校验结果,但不写入磁盘',
|
|
155
|
+
input: {
|
|
156
|
+
type: 'object',
|
|
157
|
+
properties: { operations: { type: 'array' } },
|
|
158
|
+
required: ['operations'],
|
|
159
|
+
},
|
|
160
|
+
handler: async (args) => {
|
|
161
|
+
const operations = normalizeOperations(args.operations);
|
|
162
|
+
const preview = await session.preview(operations);
|
|
163
|
+
return {
|
|
164
|
+
baseRevision: document.revision,
|
|
165
|
+
diff: preview.diff,
|
|
166
|
+
validation: preview.validation,
|
|
167
|
+
impact: preview.impact,
|
|
168
|
+
};
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
register('courseware.apply', {
|
|
173
|
+
description: '提交结构化编辑计划(事务化:全部成功或全部不生效)',
|
|
174
|
+
highImpact: true,
|
|
175
|
+
input: {
|
|
176
|
+
type: 'object',
|
|
177
|
+
properties: {
|
|
178
|
+
operationId: { type: 'string' },
|
|
179
|
+
baseRevision: { type: 'number' },
|
|
180
|
+
summary: { type: 'string' },
|
|
181
|
+
operations: { type: 'array' },
|
|
182
|
+
confirm: { type: ['object', 'boolean'] },
|
|
183
|
+
confirmation: { type: 'object' },
|
|
184
|
+
awaitAck: { type: 'boolean' },
|
|
185
|
+
awaitAckMs: { type: 'number' },
|
|
186
|
+
},
|
|
187
|
+
required: ['operationId', 'baseRevision', 'operations'],
|
|
188
|
+
},
|
|
189
|
+
handler: async (args) => {
|
|
190
|
+
const operationId = requireString(args, 'operationId', { min: 8, max: 80 });
|
|
191
|
+
if (!/^[A-Za-z0-9._:-]+$/.test(operationId)) {
|
|
192
|
+
throw new StudioError(ERROR_CODES.OPERATION_INVALID, 'operationId 只能包含字母、数字与 . _ : -', undefined, 400);
|
|
193
|
+
}
|
|
194
|
+
if (!Number.isInteger(args.baseRevision)) {
|
|
195
|
+
throw new StudioError(ERROR_CODES.OPERATION_INVALID, 'baseRevision 必须是整数', undefined, 400);
|
|
196
|
+
}
|
|
197
|
+
const result = await session.commit({
|
|
198
|
+
operationId,
|
|
199
|
+
baseRevision: args.baseRevision,
|
|
200
|
+
summary: typeof args.summary === 'string' ? args.summary : '',
|
|
201
|
+
operations: normalizeOperations(args.operations),
|
|
202
|
+
confirm: args.confirm,
|
|
203
|
+
confirmation: args.confirmation,
|
|
204
|
+
actor: args.actor ?? 'agent',
|
|
205
|
+
});
|
|
206
|
+
if (result.pending) {
|
|
207
|
+
return { pending: true, reason: 'HIGH_IMPACT_CONFIRMATION', ...result };
|
|
208
|
+
}
|
|
209
|
+
if (result.committed && result.broadcast && args.awaitAck !== false) {
|
|
210
|
+
result.renderAck = await server.waitForAck({
|
|
211
|
+
revision: result.revision,
|
|
212
|
+
timeoutMs: Number.isInteger(args.awaitAckMs) ? args.awaitAckMs : 1200,
|
|
213
|
+
});
|
|
214
|
+
} else {
|
|
215
|
+
result.renderAck = {
|
|
216
|
+
acknowledged: false,
|
|
217
|
+
reason: result.broadcast ? 'ACK_NOT_WAITED' : 'NO_PREVIEW_CLIENT',
|
|
218
|
+
clients: result.previewClients ?? 0,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
return result;
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
register('courseware.history', {
|
|
226
|
+
description: '读取编辑历史(修订、摘要、影响与校验结果)',
|
|
227
|
+
input: { type: 'object', properties: { limit: { type: 'number' } } },
|
|
228
|
+
handler: async (args) => ({
|
|
229
|
+
history: await session.history(Number.isInteger(args.limit) ? Math.min(Math.max(args.limit, 1), 100) : 20),
|
|
230
|
+
revision: document.revision,
|
|
231
|
+
}),
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
register('courseware.undo', {
|
|
235
|
+
description: '恢复到指定修订(默认为上一修订),并产生新修订;需 confirm: true',
|
|
236
|
+
highImpact: true,
|
|
237
|
+
input: {
|
|
238
|
+
type: 'object',
|
|
239
|
+
properties: {
|
|
240
|
+
revision: { type: 'number' },
|
|
241
|
+
baseRevision: { type: 'number' },
|
|
242
|
+
confirm: { type: 'boolean' },
|
|
243
|
+
awaitAck: { type: 'boolean' },
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
handler: async (args) => {
|
|
247
|
+
const result = await session.undo(
|
|
248
|
+
Number.isInteger(args.revision) ? args.revision : undefined,
|
|
249
|
+
{ confirm: args.confirm === true, baseRevision: args.baseRevision },
|
|
250
|
+
);
|
|
251
|
+
if (result.pending) return result;
|
|
252
|
+
if (result.committed && result.broadcast && args.awaitAck !== false) {
|
|
253
|
+
result.renderAck = await server.waitForAck({ revision: result.revision, timeoutMs: 1200 });
|
|
254
|
+
}
|
|
255
|
+
return result;
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
register('courseware.diff', {
|
|
260
|
+
description: '对比修订或试算编辑计划:fromRevision/toRevision、revision 或 operations',
|
|
261
|
+
input: {
|
|
262
|
+
type: 'object',
|
|
263
|
+
properties: {
|
|
264
|
+
operations: { type: 'array' },
|
|
265
|
+
revision: { type: 'number' },
|
|
266
|
+
fromRevision: { type: 'number' },
|
|
267
|
+
toRevision: { type: 'number' },
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
handler: async (args) => {
|
|
271
|
+
if (Array.isArray(args.operations)) {
|
|
272
|
+
const preview = await session.preview(normalizeOperations(args.operations));
|
|
273
|
+
return { baseRevision: document.revision, diff: preview.diff, validation: preview.validation, impact: preview.impact };
|
|
274
|
+
}
|
|
275
|
+
if (Number.isInteger(args.fromRevision) || Number.isInteger(args.toRevision)) {
|
|
276
|
+
return session.diffRevisions(args.fromRevision, args.toRevision);
|
|
277
|
+
}
|
|
278
|
+
if (Number.isInteger(args.revision)) {
|
|
279
|
+
return session.diffRevisions(args.revision, document.revision);
|
|
280
|
+
}
|
|
281
|
+
// 缺省比较上一修订与当前修订,而不是当前与自己比较。
|
|
282
|
+
return session.diffRevisions(Math.max(0, document.revision - 1), document.revision);
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
register('chat.pull', {
|
|
287
|
+
description: '拉取界面等待处理的对话消息(含选中元素上下文与当前课件场景/组件摘要)',
|
|
288
|
+
input: {
|
|
289
|
+
type: 'object',
|
|
290
|
+
properties: {
|
|
291
|
+
limit: { type: 'number' },
|
|
292
|
+
historyLimit: { type: 'number' },
|
|
293
|
+
},
|
|
294
|
+
},
|
|
295
|
+
handler: async (args) => {
|
|
296
|
+
const limit = clampInt(args.limit, 20, 1, 50);
|
|
297
|
+
const historyLimit = clampInt(args.historyLimit, 20, 0, 50);
|
|
298
|
+
const [pending, recent] = await Promise.all([
|
|
299
|
+
mailbox.pending({ limit }),
|
|
300
|
+
historyLimit > 0
|
|
301
|
+
? mailbox.list({ since: 0, limit: 200 }).then((value) => value.messages.slice(-historyLimit))
|
|
302
|
+
: [],
|
|
303
|
+
]);
|
|
304
|
+
return {
|
|
305
|
+
revision: document.revision,
|
|
306
|
+
courseware: chatContext(),
|
|
307
|
+
pending,
|
|
308
|
+
recent,
|
|
309
|
+
hints: [
|
|
310
|
+
'pending 是用户尚未收到回复、且未被监听器认领的消息;selection 是用户在预览中选中的元素。',
|
|
311
|
+
'修改课件请使用 courseware.preview_change 试算、courseware.apply 提交。',
|
|
312
|
+
'回复时调用 chat.reply,并用 inReplyTo 引用对应消息 id;回复会自动释放认领。',
|
|
313
|
+
'长时间处理前可先用 chat.claim 认领,避免多个监听器重复执行。',
|
|
314
|
+
],
|
|
315
|
+
};
|
|
316
|
+
},
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
register('chat.reply', {
|
|
320
|
+
description: '向界面回复一条 Agent 消息,可用 inReplyTo 引用用户消息',
|
|
321
|
+
input: {
|
|
322
|
+
type: 'object',
|
|
323
|
+
properties: {
|
|
324
|
+
text: { type: 'string' },
|
|
325
|
+
inReplyTo: { type: 'string' },
|
|
326
|
+
revision: { type: 'number' },
|
|
327
|
+
},
|
|
328
|
+
required: ['text'],
|
|
329
|
+
},
|
|
330
|
+
handler: async (args) => {
|
|
331
|
+
const text = requireString(args, 'text', { min: 1, max: 4000 });
|
|
332
|
+
const message = await mailbox.append({
|
|
333
|
+
role: 'agent',
|
|
334
|
+
text,
|
|
335
|
+
inReplyTo: typeof args.inReplyTo === 'string' ? args.inReplyTo : null,
|
|
336
|
+
revision: Number.isInteger(args.revision) ? args.revision : document.revision,
|
|
337
|
+
});
|
|
338
|
+
server.broadcast({ type: 'chat.message', message });
|
|
339
|
+
return { message, revision: document.revision };
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
register('chat.claim', {
|
|
344
|
+
description: '认领一条待处理用户消息(默认 5 分钟租约),避免多个监听器重复处理',
|
|
345
|
+
input: {
|
|
346
|
+
type: 'object',
|
|
347
|
+
properties: {
|
|
348
|
+
id: { type: 'string' },
|
|
349
|
+
by: { type: 'string' },
|
|
350
|
+
ttlMs: { type: 'number' },
|
|
351
|
+
},
|
|
352
|
+
required: ['id'],
|
|
353
|
+
},
|
|
354
|
+
handler: async (args) => {
|
|
355
|
+
const id = requireString(args, 'id', { min: 3, max: 120 });
|
|
356
|
+
const result = await mailbox.claim({ id, by: args.by, ttlMs: args.ttlMs });
|
|
357
|
+
if (!result.claimed) {
|
|
358
|
+
throw new StudioError(
|
|
359
|
+
ERROR_CODES.OPERATION_CONFLICT,
|
|
360
|
+
'消息已被其他监听器认领',
|
|
361
|
+
{ id, by: result.by, expiresAt: result.expiresAt },
|
|
362
|
+
409,
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
return { ...result, revision: document.revision, courseware: chatContext() };
|
|
366
|
+
},
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
register('chat.release', {
|
|
370
|
+
description: '释放一条消息的认领(未回复但放弃处理时使用)',
|
|
371
|
+
input: {
|
|
372
|
+
type: 'object',
|
|
373
|
+
properties: { id: { type: 'string' } },
|
|
374
|
+
required: ['id'],
|
|
375
|
+
},
|
|
376
|
+
handler: async (args) => {
|
|
377
|
+
const id = requireString(args, 'id', { min: 3, max: 120 });
|
|
378
|
+
return { ...(await mailbox.release({ id })), id };
|
|
379
|
+
},
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
register('chat.list', {
|
|
383
|
+
description: '读取界面会话记录(用于查看上下文,不改变待处理状态)',
|
|
384
|
+
input: {
|
|
385
|
+
type: 'object',
|
|
386
|
+
properties: {
|
|
387
|
+
since: { type: 'number' },
|
|
388
|
+
limit: { type: 'number' },
|
|
389
|
+
},
|
|
390
|
+
},
|
|
391
|
+
handler: async (args) => ({
|
|
392
|
+
...(await mailbox.list({
|
|
393
|
+
since: Number.isFinite(Number(args.since)) ? Number(args.since) : 0,
|
|
394
|
+
limit: clampInt(args.limit, 100, 1, 200),
|
|
395
|
+
})),
|
|
396
|
+
revision: document.revision,
|
|
397
|
+
}),
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
register('tool.list', {
|
|
401
|
+
description: '列出可用工具及其输入 Schema',
|
|
402
|
+
input: { type: 'object', properties: {} },
|
|
403
|
+
handler: async () => ({
|
|
404
|
+
tools: [...tools.values()].map((tool) => ({
|
|
405
|
+
name: tool.name,
|
|
406
|
+
description: tool.description,
|
|
407
|
+
highImpact: tool.highImpact,
|
|
408
|
+
input: tool.input,
|
|
409
|
+
})),
|
|
410
|
+
operationTypes: OPERATION_TYPES,
|
|
411
|
+
}),
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
register('courseware.export', {
|
|
415
|
+
description: '导出校验通过的课件副本到指定输出目录(仅当前已提交 revision)',
|
|
416
|
+
input: {
|
|
417
|
+
type: 'object',
|
|
418
|
+
properties: {
|
|
419
|
+
out: { type: 'string' },
|
|
420
|
+
revision: { type: 'number' },
|
|
421
|
+
force: { type: 'boolean' },
|
|
422
|
+
},
|
|
423
|
+
required: ['out'],
|
|
424
|
+
},
|
|
425
|
+
handler: async (args) => {
|
|
426
|
+
const out = requireString(args, 'out', { min: 1, max: 1024 });
|
|
427
|
+
return server.exportCourseware(out, {
|
|
428
|
+
revision: Number.isInteger(args.revision) ? args.revision : document.revision,
|
|
429
|
+
force: args.force === true,
|
|
430
|
+
});
|
|
431
|
+
},
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
register('courseware.package', {
|
|
435
|
+
description: '把当前已提交课件打包为单个 .lume 文件,便于分享与导入',
|
|
436
|
+
input: {
|
|
437
|
+
type: 'object',
|
|
438
|
+
properties: {
|
|
439
|
+
out: { type: 'string' },
|
|
440
|
+
revision: { type: 'number' },
|
|
441
|
+
force: { type: 'boolean' },
|
|
442
|
+
},
|
|
443
|
+
required: ['out'],
|
|
444
|
+
},
|
|
445
|
+
handler: async (args) => {
|
|
446
|
+
const out = requireString(args, 'out', { min: 1, max: 1024 });
|
|
447
|
+
return server.packageCourseware(out, {
|
|
448
|
+
revision: Number.isInteger(args.revision) ? args.revision : document.revision,
|
|
449
|
+
force: args.force === true,
|
|
450
|
+
});
|
|
451
|
+
},
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
return {
|
|
455
|
+
get(name) {
|
|
456
|
+
const tool = tools.get(name);
|
|
457
|
+
if (!tool) throw new StudioError(ERROR_CODES.TOOL_UNKNOWN, `未知工具:${name}`, { name }, 404);
|
|
458
|
+
return tool;
|
|
459
|
+
},
|
|
460
|
+
async invoke(name, args) {
|
|
461
|
+
const tool = this.get(name);
|
|
462
|
+
const result = await tool.handler(args ?? {});
|
|
463
|
+
return { tool: name, ok: true, result };
|
|
464
|
+
},
|
|
465
|
+
list() {
|
|
466
|
+
return [...tools.values()];
|
|
467
|
+
},
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function normalizeOperations(operations) {
|
|
472
|
+
if (!Array.isArray(operations)) {
|
|
473
|
+
throw new StudioError(ERROR_CODES.OPERATION_INVALID, 'operations 必须是数组', undefined, 400);
|
|
474
|
+
}
|
|
475
|
+
if (operations.length === 0) {
|
|
476
|
+
throw new StudioError(ERROR_CODES.OPERATION_INVALID, '至少需要一个操作', undefined, 400);
|
|
477
|
+
}
|
|
478
|
+
return operations.map((operation) => {
|
|
479
|
+
if (!operation || typeof operation !== 'object') {
|
|
480
|
+
throw new StudioError(ERROR_CODES.OPERATION_INVALID, '操作必须是对象', undefined, 400);
|
|
481
|
+
}
|
|
482
|
+
if (!OPERATION_TYPES.includes(operation.op)) {
|
|
483
|
+
throw new StudioError(ERROR_CODES.TOOL_UNKNOWN, `不支持的操作:${operation.op}`, { op: operation.op }, 400);
|
|
484
|
+
}
|
|
485
|
+
return operation;
|
|
486
|
+
});
|
|
487
|
+
}
|