Files
seewo/kit/server.mjs
T
wangfar 32b0e74e9d seewo-kit: 新增审计/改样式/删页排序/备份列表
- inspect_courseware:按页序审计文字、字号、颜色、位置、动画与点击顺序,支持全课件搜索
- set_text_style:批量改字号/加粗/颜色/对齐/框宽,只改 TextRun,保护默认属性与项目符号
- replace_text:支持 pages/allPages、matchAll、dryRun 预览
- delete_slides / move_slides:重写 Board 页序与索引映射,删页默认进回收目录
- list_backups:列出快照,供 restore_courseware 取 backupId
- README 更新到 17 个工具
2026-11-13 08:12:27 +08:00

442 lines
20 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, pruneOrphanFiles, removeLocalCourseware,
inspectCourseware, setTextStyle, listBackups, deleteSlides, moveSlides,
} 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:子串替换;同一页命中多个时加 elementId 或 matchAll=true\n'
+ '· width:可选,传了就按「固定框宽 + 单行折行」重排(适合正文 1160)\n'
+ 'page 单页,或 pages 多页,或 allPages=true 全部页。\n'
+ 'dryRun=true 只预览改动,不关课件、不写文件。写操作流程同 set_animations。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string' },
page: { type: 'integer', minimum: 1 },
pages: { type: 'array', items: { type: 'integer', minimum: 1 }, description: '多页模式:页码数组' },
allPages: { type: 'boolean', default: false, description: '全部页面' },
replacements: {
type: 'array',
items: {
type: 'object',
properties: {
elementId: { type: 'string' },
find: { type: 'string' },
replace: { type: 'string' },
text: { type: 'string' },
width: { type: 'number' },
},
},
},
matchAll: { type: 'boolean', default: false, description: 'find 命中多个时全部替换' },
dryRun: { type: 'boolean', default: false, description: '只预览,不写文件、不关/开课件' },
reopen: { type: 'boolean', default: true },
sync: { type: 'boolean', default: false },
force: { type: 'boolean', default: false },
backup: { type: 'boolean', default: true },
},
required: ['replacements'],
},
},
{
name: 'inspect_courseware',
description:
'按 Board 页序检查课件内容(只读):每页元素 Id、文字、字号、加粗、颜色、位置、是否带动画、点击顺序,'
+ '以及可能的超宽行。\n'
+ 'page 单页;pages 多页;不传则全部页(多页默认只给摘要,不展开每页元素)。\n'
+ 'query 可全课件搜索文字,返回匹配的页码/元素 Id;format="text" 返回人读清单。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string' },
page: { type: 'integer', minimum: 1 },
pages: { type: 'array', items: { type: 'integer', minimum: 1 } },
query: { type: 'string', description: '按文字包含搜索(全课件)' },
includeElements: { type: 'boolean', description: '是否展开每页元素;默认单页/双页展开,多页只给摘要' },
includeAnimations: { type: 'boolean', default: true },
format: { type: 'string', enum: ['json', 'text'], default: 'json' },
},
},
},
{
name: 'set_text_style',
description:
'批量改文本样式(不换文字):字号、加粗、颜色、对齐、文本框宽度。\n'
+ '用 elementIds / textMatches 选元素;都不传则改所选页的全部文本(建议先 dryRun 预览)。\n'
+ 'page 单页,或 pages 多页,或 allPages=true 全部页。\n'
+ '颜色用 #RRGGBB 或 #AARRGGBB;改字号后会自动更新折行缓存。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string' },
page: { type: 'integer', minimum: 1 },
pages: { type: 'array', items: { type: 'integer', minimum: 1 } },
allPages: { type: 'boolean', default: false },
elementIds: { type: 'array', items: { type: 'string' } },
textMatches: { type: 'array', items: { type: 'string' } },
matchAll: { type: 'boolean', default: false },
fontSize: { type: 'number', minimum: 1 },
bold: { type: 'boolean' },
color: { type: 'string', description: '#RRGGBB 或 #AARRGGBB' },
align: { type: 'string', enum: ['Left', 'Center', 'Right'] },
width: { type: 'number', minimum: 1, description: '文本块宽度,可选' },
dryRun: { type: 'boolean', default: false, description: '只预览,不写文件、不关/开课件' },
reopen: { type: 'boolean', default: true },
sync: { type: 'boolean', default: false },
force: { type: 'boolean', default: false },
backup: { type: 'boolean', default: true },
},
},
},
{
name: 'list_backups',
description:
'列出本地备份快照(backupId / 时间 / 备注 / 页数 / Revision),可按课件过滤。'
+ 'restore_courseware 需要的 backupId 就从这个列表取。只读。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string', description: '课件名片段或 LocalId;不传列全部' },
limit: { type: 'integer', minimum: 1, default: 50 },
},
},
},
{
name: 'delete_slides',
description:
'按页删除页面:重写 Board.xml 页序、索引 Slides\\Slide_N 映射和 DB。\n'
+ '页面文件默认移到 %TEMP%\\opencode\\seewo-kit-trash,不会硬删除;至少保留 1 页。\n'
+ '删除后可用 prune_orphan_files 清理不再被索引引用的资源文件。写操作流程同 set_animations。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string' },
pages: { type: 'array', items: { type: 'integer', minimum: 1 }, description: '要删除的页码(按当前页序)' },
trash: { type: 'boolean', default: true, description: 'true=移到回收目录,false=直接删除' },
reopen: { type: 'boolean', default: true },
sync: { type: 'boolean', default: false },
force: { type: 'boolean', default: false },
backup: { type: 'boolean', default: true },
},
required: ['pages'],
},
},
{
name: 'move_slides',
description:
'调整页面顺序:from+to 把第 from 页移到第 to 个位置;或用 order 传完整新页序(当前页码数组)。\n'
+ '只重写 Board.xml 和索引映射,不改页面内容。写操作流程同 set_animations。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string' },
from: { type: 'integer', minimum: 1, description: '当前页码' },
to: { type: 'integer', minimum: 1, description: '移动后的新位置' },
order: { type: 'array', items: { type: 'integer', minimum: 1 }, description: '完整新页序(当前页码),如 [3,1,2]' },
reopen: { type: 'boolean', default: true },
sync: { type: 'boolean', default: false },
force: { type: 'boolean', default: false },
backup: { type: 'boolean', default: true },
},
},
},
{
name: 'verify_courseware',
description:
'课件自检(只读):页数、Board.xml 页序、索引与磁盘文件的 MD5/大小、重复 Id、图片资源引用、'
+ 'DB 里的 Revision/行数是否一致、孤儿文件,以及每页动画数(animations / animationTotal)。'
+ '返回 { 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'
+ '默认 keepIdentity=true:只恢复内容,保留当前课件的 Name/RemoteId,避免把本地指针指到备份里的另一份云端副本。\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' },
keepIdentity: { type: 'boolean', default: true, description: '保留当前 Name/RemoteId(推荐)' },
reopen: { type: 'boolean', default: true },
sync: { type: 'boolean', default: false },
force: { type: 'boolean', default: false },
},
required: ['backupId', 'confirm'],
},
},
{
name: 'prune_orphan_files',
description:
'清理课件目录里「存在但未被索引引用」的旧文件(每次恢复/复制都会堆积)。\n'
+ '默认 dryRun=true 只预览;真正清理需要 dryRun=false + confirm=true。\n'
+ '清理前会自动备份,文件移进 %TEMP%\\opencode\\seewo-kit-orphans,不会硬删除。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string', description: '课件名片段或 LocalId' },
dryRun: { type: 'boolean', default: true, description: 'true=只列出将要清理的文件' },
confirm: { type: 'boolean', default: false, description: 'dryRun=false 时必须为 true' },
force: { type: 'boolean', default: false, description: '忽略未同步检查' },
backup: { type: 'boolean', default: true },
reopen: { type: 'boolean', default: true, description: '原本开着的话,清理后重新打开' },
},
},
},
{
name: 'remove_local_courseware',
description:
'移除本地重复课件:关闭课件 → 把缓存目录移到 %TEMP%\\opencode\\seewo-kit-trash → 删除 Courseware / CoursewareFile 记录。\n'
+ '只影响本地缓存和 DB,不会删除云端课件。必须传 confirm=true。',
inputSchema: {
type: 'object',
properties: {
courseware: { type: 'string', description: '课件名片段或 LocalId(重名时必须用 LocalId)' },
confirm: { type: 'boolean', description: '必须为 true' },
force: { type: 'boolean', default: false, description: '忽略未同步检查' },
trash: { type: 'boolean', default: true, description: 'true=移到回收目录,false=直接删除' },
},
required: ['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,
inspect_courseware: inspectCourseware,
set_text_style: setTextStyle,
list_backups: listBackups,
delete_slides: deleteSlides,
move_slides: moveSlides,
verify_courseware: verifyCourseware,
backup_courseware: backupCourseware,
restore_courseware: restoreCourseware,
copy_slides: copySlides,
sync_courseware: syncCourseware,
prune_orphan_files: pruneOrphanFiles,
remove_local_courseware: removeLocalCourseware,
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');