Files
seewo/kit/server.mjs
T

290 lines
12 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* seewo-kit MCP server(stdio)
*
* 补希沃自带 MCP 的缺口:
* 动画三件套 · 课件自检 · 备份/恢复 · 跨课件复制页面 · 批量改文字
*
* 启动方式(OpenCode 配置):
* "seewokit": { "type": "local", "command": ["node", "D:\\Documents\\Default Project\\seewo-anim\\kit\\server.mjs"] }
*
* 写操作内部会:关课件(不保存) → 自动备份 → 改缓存文件 → 更新索引/DB → 重新打开。
* 前提:目标课件的未同步改动必须已经同步(内存页数/大小与磁盘不一致时会拒绝,除非 force=true)。
*/
import { createInterface } from 'node:readline';
import {
setAnimations, getAnimations, clearAnimations,
replaceText, verifyCourseware, backupCourseware, restoreCourseware, copySlides,
listCoursewares, syncCourseware,
} from './core.mjs';
const log = (...a) => console.error('[seewo-kit]', ...a);
const TOOLS = [
{
name: 'set_animations',
description:
'给页面元素加「点击淡入」等动画(FadeIn/Click 默认 0.3 秒)。只改指定元素,其它元素动画保留。\n'
+ '写操作会先关闭目标课件(不保存内存改动)、自动备份、改缓存、重开课件。\n'
+ '用 elementIds 或 textMatches 选元素;order 可指定点击顺序(元素 Id 数组)。\n'
+ '注意:课件若有未同步的新页/改动会被拒绝,请先同步或传 force=true。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string', description: '课件名片段或 LocalId;不传取最近修改的课件' },
page: { type: 'integer', minimum: 1, description: '页码(1-based,按 Board.xml 页序)' },
elementIds: { type: 'array', items: { type: 'string' }, description: '元素 Id(32 位 hex)' },
textMatches: { type: 'array', items: { type: 'string' }, description: '按文字包含匹配元素;多个命中会报错' },
matchAll: { type: 'boolean', default: false, description: 'textMatches 命中多个时全部选中' },
type: {
type: 'string',
enum: ['FadeIn', 'FadeOut', 'WipeIn', 'WipeOut', 'ZoomIn', 'ZoomOut', 'FlyIn', 'FloatIn', 'Appear'],
default: 'FadeIn',
},
trigger: { type: 'string', enum: ['Click', 'WithPrevious', 'AfterPrevious'], default: 'Click' },
duration: { type: 'number', default: 0.3, description: '秒' },
delay: { type: 'number', default: 0, description: '秒' },
order: { type: 'array', items: { type: 'string' }, description: '点击顺序(元素 Id 数组),可选' },
reopen: { type: 'boolean', default: true, description: '改完重新打开课件' },
sync: { type: 'boolean', default: false, description: '重开后立即云同步' },
force: { type: 'boolean', default: false, description: '忽略未同步检查(会丢内存里未保存的改动)' },
backup: { type: 'boolean', default: true, description: '改动前自动备份' },
},
required: ['page'],
},
},
{
name: 'get_animations',
description: '读取某页的动画:每个动画的类型/触发方式/时长/延迟/点击顺序,以及 AnimationOrders 的一致性问题。只读。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string', description: '课件名片段或 LocalId' },
page: { type: 'integer', minimum: 1, description: '页码(1-based)' },
},
required: ['page'],
},
},
{
name: 'clear_animations',
description:
'清除动画:传 elementIds 只清这些元素;不传则清整页;allPages=true 清全部页面。\n'
+ '会同步重建 AnimationOrders。写操作流程同 set_animations。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string' },
page: { type: 'integer', minimum: 1 },
allPages: { type: 'boolean', default: false },
elementIds: { type: 'array', items: { type: 'string' } },
reopen: { type: 'boolean', default: true },
sync: { type: 'boolean', default: false },
force: { type: 'boolean', default: false },
backup: { type: 'boolean', default: true },
},
},
},
{
name: 'replace_text',
description:
'批量修改某页文本内容(希沃自带 edit_text_element 不能改文字)。\n'
+ 'replacements 每项:{ elementId 或 find, text 或 (find+replace), width? }\n'
+ '· text:整段替换(要求该元素只有一个不同文本片段)\n'
+ '· find+replace:子串替换,必须命中\n'
+ '· width:可选,传了就按「固定框宽 + 单行折行」重排(适合正文 1160)\n'
+ '会更新 <Lines> 折行缓存,避免重开后按旧折行显示。写操作流程同 set_animations。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string' },
page: { type: 'integer', minimum: 1 },
replacements: {
type: 'array',
items: {
type: 'object',
properties: {
elementId: { type: 'string' },
find: { type: 'string' },
replace: { type: 'string' },
text: { type: 'string' },
width: { type: 'number' },
},
},
},
reopen: { type: 'boolean', default: true },
sync: { type: 'boolean', default: false },
force: { type: 'boolean', default: false },
backup: { type: 'boolean', default: true },
},
required: ['page', 'replacements'],
},
},
{
name: 'verify_courseware',
description:
'课件自检(只读):页数、Board.xml 页序、索引与磁盘文件的 MD5/大小、重复 Id、图片资源引用、'
+ 'DB 里的 Revision/行数是否一致、孤儿文件。返回 { ok, issues[] }。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string' },
hashes: { type: 'boolean', default: true, description: '是否逐文件校验 MD5(大课件会慢一点)' },
deep: { type: 'boolean', default: true, description: '是否跨页检查重复元素 Id' },
},
},
},
{
name: 'backup_courseware',
description: '把课件的全部缓存文件 + 索引 JSON + DB 行快照到临时目录,返回 backupId(可用于 restore_courseware)。只读不改课件。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string' },
note: { type: 'string', description: '备注,写进 manifest' },
},
},
},
{
name: 'restore_courseware',
description:
'从 backup_courseware 的快照恢复(覆盖当前缓存文件与索引/DB)。必须传 confirm=true。\n'
+ '恢复是本地操作:Revision 会比当前值再 +1,不会自动上传;需要时传 sync=true。',
inputSchema: {
type: 'object',
properties: {
backupId: { type: 'string', description: 'backup_courseware 返回的 backupId' },
courseware: { type: 'string', description: '覆盖到哪份课件;默认用备份里的 LocalId' },
confirm: { type: 'boolean', description: '必须为 true' },
reopen: { type: 'boolean', default: true },
sync: { type: 'boolean', default: false },
force: { type: 'boolean', default: false },
},
required: ['backupId', 'confirm'],
},
},
{
name: 'copy_slides',
description:
'把源课件的指定页复制到目标课件(可跨课件),保留图片/音频等资源、动画和备注。\n'
+ 'position:插入到目标第几页之后(0=最前面,不传=末尾)。\n'
+ '会重建目标课件的 Board.xml 页序、索引和 DB;复制页的页面/元素/动画 Id 全部重新生成,避免冲突。\n'
+ '写操作流程同 set_animations(自动备份 + 重开)。',
inputSchema: {
type: 'object',
properties: {
source: { type: 'string', description: '源课件名片段或 LocalId' },
target: { type: 'string', description: '目标课件名片段或 LocalId' },
pages: { type: 'array', items: { type: 'integer', minimum: 1 }, description: '源课件页码(按页序)' },
position: { type: 'integer', minimum: 0, description: '插入到目标第几页之后,0=最前,不传=末尾' },
reopen: { type: 'boolean', default: true },
sync: { type: 'boolean', default: false },
force: { type: 'boolean', default: false },
backup: { type: 'boolean', default: true },
},
required: ['source', 'target', 'pages'],
},
},
{
name: 'list_coursewares',
description: '列出本地缓存里的课件(名称、LocalId、页数、Revision),排错时用。只读。',
inputSchema: { type: 'object', properties: {} },
},
{
name: 'sync_courseware',
description:
'只做云同步,不改内容:课件没开会先打开,然后做一次无害改动(加页+删页)让希沃认为「有更新」,再上传。\n'
+ '用于把本地 Revision 脏状态清干净,或把本地改动推上云。返回同步结果与 isSynced。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string', description: '课件名片段或 LocalId' },
},
},
},
];
const HANDLERS = {
set_animations: setAnimations,
get_animations: getAnimations,
clear_animations: clearAnimations,
replace_text: replaceText,
verify_courseware: verifyCourseware,
backup_courseware: backupCourseware,
restore_courseware: restoreCourseware,
copy_slides: copySlides,
sync_courseware: syncCourseware,
list_coursewares: () => {
const all = listCoursewares();
let dbCountHint = null;
return {
dbCountHint,
coursewares: all.slice(0, 50).map(c => ({
name: c.name,
localId: c.localId,
pages: (c.json.CoursewareFiles || []).filter(f => /^Slides[\\/]Slide_\d+\.xml$/i.test(f.RelativePath)).length,
revision: c.json.Courseware.Revision,
syncedRevision: c.json.Courseware.SyncedRevision,
size: c.json.Courseware.Size,
modified: new Date(c.mtime).toISOString(),
})),
};
},
};
function send(msg) {
process.stdout.write(JSON.stringify(msg) + '\n');
}
async function handle(msg) {
const { id, method, params = {} } = msg;
if (method === 'initialize') {
return send({
jsonrpc: '2.0', id,
result: {
protocolVersion: params.protocolVersion || '2025-06-18',
capabilities: { tools: { listChanged: false } },
serverInfo: { name: 'seewo-kit', version: '1.0.0' },
},
});
}
if (method === 'notifications/initialized' || !('id' in msg)) return;
if (method === 'ping') return send({ jsonrpc: '2.0', id, result: {} });
if (method === 'tools/list') return send({ jsonrpc: '2.0', id, result: { tools: TOOLS } });
if (method === 'tool.call') { /* 有些客户端用这个别名 */ }
if (method === 'tools/call') {
const name = params.name;
const fn = HANDLERS[name];
if (!fn) {
return send({ jsonrpc: '2.0', id, error: { code: -32602, message: `未知工具:${name}` } });
}
try {
const out = await fn(params.arguments ?? {});
return send({
jsonrpc: '2.0', id,
result: { content: [{ type: 'text', text: JSON.stringify(out, null, 2) }] },
});
} catch (e) {
log(name, '失败:', e.stack || e.message);
return send({
jsonrpc: '2.0', id,
result: { content: [{ type: 'text', text: '错误:' + (e.message || String(e)) }], isError: true },
});
}
}
send({ jsonrpc: '2.0', id, error: { code: -32601, message: `不支持的方法:${method}` } });
}
const rl = createInterface({ input: process.stdin, crlfDelay: Infinity });
rl.on('line', line => {
const s = line.trim();
if (!s) return;
let msg;
try { msg = JSON.parse(s); } catch (e) { log('坏消息:', s.slice(0, 200)); return; }
handle(msg).catch(e => {
log('处理失败:', e.stack || e.message);
if ('id' in msg) send({ jsonrpc: '2.0', id: msg.id, error: { code: -32603, message: String(e.message || e) } });
});
});
rl.on('close', () => process.exit(0));
log('ready');