Backup: seewo-kit MCP tools and courseware anim scripts

This commit is contained in:
wangfar
2026-11-09 11:35:30 +08:00
commit e6d74ebb35
7 changed files with 2551 additions and 0 deletions
+232
View File
@@ -0,0 +1,232 @@
# seewo-kit · 希沃白板 MCP 增强工具
希沃自带的 MCP 只有「建页 / 加元素 / 开关课件 / 同步」,缺动画、跨课件复制、改文字、自检这些能力。
`seewo-kit` 是一个独立的 stdio MCP server,直接读写希沃的本地课件缓存,同时复用希沃 MCP 完成
「关课件 / 开课件 / 云同步」。
它是 `seewo-anim.mjs`(CLI)的 MCP 化版本,参数语义一致,另外补了 verify / backup / restore / copy / replace。
```
kit/
├─ core.mjs 核心实现(文件、XML、DB、MCP 客户端)
└─ server.mjs stdio MCP server,只负责协议和参数 schema
```
## 接入 OpenCode
`C:\Users\Administrator\.config\opencode\opencode.json`:
```json
{
"mcp": {
"seewokit": {
"type": "local",
"enabled": true,
"command": ["node", "D:\\Documents\\Default Project\\seewo-anim\\kit\\server.mjs"]
}
}
}
```
改完重启 OpenCode,工具会以 `seewokit_*` 的名字出现。
## 工具一览
| 工具 | 作用 | 写操作 |
| --- | --- | --- |
| `set_animations` | 给元素加动画(默认 FadeIn / Click / 0.3 秒) | 是 |
| `get_animations` | 读某页动画、点击顺序、`AnimationOrders` 一致性 | 否 |
| `clear_animations` | 清整页 / 指定元素 / 全部页的动画 | 是 |
| `replace_text` | 批量改文本(整段或子串),可顺手规范折行 | 是 |
| `verify_courseware` | 课件自检:页数、页序、MD5、重复 Id、资源引用、DB 一致性 | 否 |
| `backup_courseware` | 全量快照到临时目录,返回 `backupId` | 否 |
| `restore_courseware` | 从快照恢复(需 `confirm=true`) | 是 |
| `copy_slides` | 跨课件复制页面(含图片/音频/动画/背景) | 是 |
| `sync_courseware` | 只做云同步,不改内容(用于清理 Revision 脏状态) | 是(同步) |
| `list_coursewares` | 列出本地缓存课件 | 否 |
公共参数:
| 参数 | 说明 |
| --- | --- |
| `courseware` | 课件名片段或 LocalId;不传取最近修改的课件 |
| `page` | 页码,1-based,按 `Board.xml` 页序 |
| `reopen` | 改完是否重新打开课件,默认 `true` |
| `sync` | 重开后是否云同步,默认 `false` |
| `force` | 跳过「未同步改动」检查,默认 `false` |
| `backup` | 改动前是否自动备份,默认 `true` |
## 写操作的固定流程
所有写工具最终都走 `runEdit`:
1. **检查**:如果目标课件正开着,先等它加载完(见下方「加载轮询」),
比对内存页数/大小与磁盘索引;不一致且没传 `force` 就拒绝,避免丢掉未同步的新页。
2. **关闭**:`close_current_courseware(syncBeforeClose=false)`,不保存内存改动。
3. **备份**:自动快照到 `%TEMP%\opencode\seewo-kit-backups`,保留最近 10 份。
4. **改文件**:改 XML,重算该文件 MD5/大小。
5. **落盘**:更新索引 JSON 与 `CoursewareCacheData.db` 的 `Revision` / `SyncedRevision` / `Size` / 文件行。
6. **重开**:`open_cloud_courseware`,等页面加载完成再返回。
7. **可选同步**:`sync=true` 时先做一次「加页+删页」把课件弄脏,再上传。
## 各工具要点
### set_animations / clear_animations
- 用 `elementIds` 精确指定,或 `textMatches` 按「文本包含」匹配;命中多个会报错,可加 `matchAll=true`。
- `order` 传元素 Id 数组可指定点击顺序;不传按元素在页面中的先后。
- 每次都会重建 `<AnimationOrders>`,并校验动画 Id 与顺序表是否一一对应。
- 动画时长单位是 100ns:`<Duration>3000000</Duration>` = 0.3 秒。
### replace_text
`replacements` 每项:
```json
{ "elementId": "32位hex", "text": "整段替换" }
{ "find": "客观:", "replace": "客观条件:" }
{ "elementId": "32位hex", "find": "旧", "replace": "新", "width": 1160 }
```
- `text`:整段替换,要求该元素只有一个不同的文本片段。
- `find` + `replace`:子串替换,必须命中,否则记进 `missing`。
- `width`:传了就同时把文本块规范成「固定框宽 + 单行折行」,避免重开后按旧折行渲染。
- 这是补希沃 `edit_text_element` 不能改文字内容的缺口。
### copy_slides
- `pages` 是源课件页码数组;`position` 是插入到目标第几页之后(0 = 最前,不传 = 末尾)。
- 复制页的**页面 Id、元素 Id、动画 Id 全部重新生成**,避免与目标冲突。
- 图片/音频资源会一起复制,并写入目标索引、DB 和 **`Reference.xml`**(关键,见坑)。
- 重排 `Board.xml` 页序与索引里的 `Slides\Slide_N.xml` 映射。
### verify_courseware
返回 `{ ok, issues[] }`,`level` 为 `error` / `warn` / `info`。重点检查:
- `Board.xml` 页数 vs 索引页数、页序与 `Slide_N` 是否一致;
- 每个索引文件的 MD5 / 大小;
- 页面/元素 Id 重复;
- `id://` 资源是否存在、是否登记在 `Reference.xml`;
- JSON 与 `CoursewareCacheData.db` 的 Revision / Size / 文件行数是否一致。
### backup / restore
- `backup_courseware` 返回 `backupId`,目录在 `%TEMP%\opencode\seewo-kit-backups\<localId>-<时间>`。
- `restore_courseware` 必须传 `confirm=true`;会用快照覆盖当前缓存文件与索引/DB。
- 恢复只改本地,`Revision` 会在当前值上再 +1;需要上传时传 `sync=true`。
### sync_courseware
只同步、不改内容。课件没开会先打开,然后 `add_slide` + `delete_slide` 做一次无害改动再上传。
适合清理测试留下的 Revision 脏状态。
> 不要用 `clear_animations(allPages=true)` 来「顺手触发同步」——它会真的删掉所有页面上的动画。
## 坑
### 1. 加载轮询:刚打开的课件只有 1 页
`open_cloud_courseware` 之后页面是异步加载的,立刻查 `get_current_courseware_info` 常常只返回 1 页。
旧逻辑会把它当成「内存 1 页 / 磁盘 N 页 = 有未同步改动」而误报。
现在 `waitCoursewareLoaded` 会轮询到:
- `slideCount` 与磁盘页数一致;或
- 页数连续 3 次稳定且不少于磁盘页数(用户本地新增了页)。
只有确认加载完成后才做「未同步」判断;若超时仍只看到 1 页而磁盘有多页,会明确报「还没加载完成」,而不是误判。
另外:
- 每次读写前都会先 `en_goto_app_ui({ ui: 'Edit' })`。授课(Display)模式下页面列表有时还没就绪,
`get_current_courseware_info` 会返回 0 页甚至一直无响应。
- 所有 MCP 调用都带超时(普通 30 秒、同步 180 秒)。单次查询超时只会跳过这一轮轮询,不会被当成 0 页。
- 关闭课件后会确认课件真的从 shell 列表里消失;关不掉就中止写操作,避免内存内容回头覆盖磁盘。
如果希沃界面彻底卡死(表现为 `get_current_courseware_info` / `close_current_courseware` 长时间无响应),
只能结束对应的 `EasiNote.exe` 进程再继续;磁盘缓存不会因此损坏。
### 2. 「弄脏」课件:add_slide 返回的页码才是新页
`add_slide({ slideNumber: n })` 是在第 n 页之后插入,返回的新页号通常不是 n(例如 10 页课件返回 11)。
`delete_slide` 必须用这个返回的页号;如果照着原来的页数删,会删掉最后一页真实内容。
### 3. 课件身份冲突:同名副本 / 自动改名
希沃检测到版本冲突时会把课件改名成 `名称(MM-dd HHmm 修改)`,并可能新建 `RemoteId`,本地也会出现
两个同 `Name` 的缓存目录。此时 `resolveCourseware("名称片段")` 会按 mtime 命中其中一个,容易改错。
排查与修复:
- 用 `list_coursewares` 看清每个 `localId` 的 `RemoteId` / 页数 / Revision;
- 用希沃的 `search_cloud_courseware` 确认云端有哪些同名副本;
- 以「有内容的那份」的 `RemoteId` 为准,把它的本地 JSON + DB 行保留;重复目录连同
`Courseware` 行、`CoursewareFile` 行一起移除(先移出 `Courseware\` 而不是直接删);
- 恢复备份时注意:备份里的 `index.json` 带的是旧 `Name` / `RemoteId`,恢复后要把这两项改回目标云课件,
再同步,否则会把本地指针指到另一份云端副本。
MCP 没有「删除云课件」的工具,云端多余副本只能在希沃 App 里手动删。
### 2. 图片占位图:`Reference.xml` 才是资源清单
希沃解析 `id://<32位hex>` **不是**只看索引 JSON,而是读课件里的 `Reference.xml`:
```xml
<Reference>
<Relationships>
<Relationship>
<Id>b77212ebd37d398fa47bab8eeefccd1c</Id>
<Target>Resources\b77212ebd37d398fa47bab8eeefccd1c.png</Target>
<Hash>a460c7aa4832e85b33b7e65ae4a6d2d1</Hash>
</Relationship>
</Relationships>
</Reference>
```
只复制资源文件 + 索引/DB 记录、不改 `Reference.xml`,图片会显示成 **EN 占位图**;
更糟的是希沃保存时会把这条「没人引用」的资源记录当作孤儿删掉。
`copy_slides` 现在会:
1. 复制资源文件;
2. 写索引与 DB;
3. 合并 `Reference.xml`(没有就新建一个,并登记进索引/DB)。
`verify_courseware` 也加了 `resource-not-referenced` 检查,以后再漏会直接报错。
### 3. 同步要先「弄脏」
本地只有 `Revision` 变大时,希沃 `sync` 只会「下载」不会「上传」。
`sync=true` 时工具会先 `add_slide` + `delete_slide` 做一次真实改动,再调 `sync_current_courseware`。
### 4. JSON 与 DB 必须一起改
只改索引 JSON 的 `Revision`、不改 `CoursewareCacheData.db`,重新打开云端课件时会被云端旧版本覆盖(已实测)。
所有写操作都会在同一事务里更新:
- `Courseware.Revision` / `SyncedRevision` / `Size` / `Files`
- `CoursewareFile` 的文件行
### 5. 别在课件开着时直接改文件
课件开着时内容在内存里,关闭会覆盖磁盘文件。必须走「关 → 改 → 开」。
### 6. 授课模式不保存
MCP 加的元素在 Display 模式下不会被保存;写操作前后尽量让课件处于 Edit(备课)模式。
`kit` 的写操作走的是关闭后改文件,不依赖界面模式,但同步前建议 `en_goto_app_ui({ ui: 'Edit' })`(`syncShell` 已内置)。
## 排错
```bash
# 语法检查
node --check core.mjs
node --check server.mjs
# 手动列工具 / 调工具(stdio 客户端)
node %TEMP%\opencode\kit-test.mjs read
```
备份目录:`%TEMP%\opencode\seewo-kit-backups\`
账号缓存:`%APPDATA%\Seewo\EasiNote5\Data\<账号>\Courseware\<课件Id>\`
数据库:`%APPDATA%\Seewo\EasiNote5\Data\<账号>\Courseware\CoursewareCacheData.db`
+1271
View File
File diff suppressed because it is too large Load Diff
+289
View File
@@ -0,0 +1,289 @@
#!/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');