cortico-world-extra-actions 0.3.22 → 0.3.24

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/dist/webui.js CHANGED
@@ -505,7 +505,10 @@ const panel = {
505
505
  if (!state.library.ok) {
506
506
  noteEl.hidden = false;
507
507
  noteEl.className = 'ea-note bad';
508
- const smallLib = state.library.ok && (state.library.present ?? state.library.count) < 20
508
+ // 后台有没有 download-library:没有就说明版本对不上,先别显示按钮
509
+ const api = Array.isArray(state.api) ? state.api : null;
510
+ const canDownload = !api || api.includes('download-library');
511
+ const smallLib = state.library.ok && (state.library.present ?? state.library.count) < 20 && canDownload
509
512
  ? `<b>当前动作库只有 ${state.library.present ?? state.library.count} 段</b>(插件自带的示例)。想要全部 105 段:
510
513
  <button class="ea-btn pri" type="button" data-act="download-library" style="margin-left:6px">一键下载并安装</button>
511
514
  <span class="ea-dim">(插件自己下、自己解压,不用你选文件夹)</span><br>`
@@ -581,6 +584,16 @@ const panel = {
581
584
  };
582
585
  qInp.oninput = () => { query = qInp.value.trim(); renderList(); };
583
586
 
587
+ /** 把后台的报错翻译成用户能照做的动作。 */
588
+ function explainErr(err) {
589
+ const raw = String(err?.message ?? err ?? '');
590
+ if (/unknown method/i.test(raw)) {
591
+ return '扩展刚更新过,但 Coopanion 还在用旧的后台。请完全退出 Coopanion 再打开,这个按钮就能用了。';
592
+ }
593
+ if (/内置形象包不给写/.test(raw)) return '这是内置形象包,不能写。先「建立空的角色包」再保存。';
594
+ return raw || '操作失败';
595
+ }
596
+
584
597
  async function doDownloadLibrary() {
585
598
  busy(true);
586
599
  say('正在下载完整动作库…(约 370 MB,视网速几分钟,别关这个页面)');
@@ -589,7 +602,7 @@ const panel = {
589
602
  say(`动作库已装好:${r.modules} 段 → ${r.path}`);
590
603
  await loadAll();
591
604
  } catch (err) {
592
- say('下载没成功:' + String(err?.message ?? err), true);
605
+ say('下载没成功:' + explainErr(err), true);
593
606
  } finally {
594
607
  busy(false);
595
608
  }
@@ -748,7 +761,7 @@ const panel = {
748
761
  say(r.note ? `${r.note}(${r.libraryDir})` : `动作库已更新(${r.libraryDir})`);
749
762
  }
750
763
  } catch (err) {
751
- say(String(err?.message ?? err), true);
764
+ say(explainErr(err), true);
752
765
  } finally {
753
766
  busy(false);
754
767
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cortico-world-extra-actions",
3
- "version": "0.3.22",
3
+ "version": "0.3.24",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "勾选动作库里的动作,写进形象包的「额外动作拓展」文件夹,让桌宠照着做。自带入门形象包与示例动作;完整 105 段可一键下载安装(不用解压、不用选文件夹)。",
package/src/world.js CHANGED
@@ -17,6 +17,13 @@ import { ensureStarter, installStarterLibrary, libraryHealth, restorePack, safeP
17
17
  import { FALLBACK_ACTION_BASE, FALLBACK_LIBRARY, LIB_PARTS, actionUrl, flatActionUrl, installAction, installLibrary, installLibraryParts, libraryUrl } from './online.js';
18
18
  import { petFigures, petState } from './pet.js';
19
19
 
20
+ /** 面板能调的方法清单 —— 写进 state.api,让面板发现自己和后台不是同一版本。 */
21
+ const METHODS = [
22
+ 'state', 'catalog', 'dirs', 'save', 'remove', 'clear', 'prune', 'add',
23
+ 'reindex', 'find-libraries', 'set-library', 'use-starter-library', 'restore-pack',
24
+ 'download-library', 'download-action', 'reload',
25
+ ];
26
+
20
27
  const ID = 'extra-actions';
21
28
  const HERE = dirname(fileURLToPath(import.meta.url));
22
29
  const CATALOG_FILE = join(HERE, '..', 'assets', 'catalog.json');
@@ -370,6 +377,9 @@ export class ExtraActionsWorld {
370
377
  return this.downloadLibrary();
371
378
  case 'download-action':
372
379
  return this.downloadAction(String(a[0] ?? ''), String(a[1] ?? ''));
380
+ // 老版本面板会调 reload:当作「重新读一遍状态」,别再报 unknown method
381
+ case 'reload':
382
+ return this.state();
373
383
  default:
374
384
  throw new Error(`unknown method: ${method}`);
375
385
  }
@@ -390,6 +400,8 @@ export class ExtraActionsWorld {
390
400
  const figs = await this.figures();
391
401
  const pick = figs.find((f) => f.supported && !f.builtin) ?? figs.find((f) => f.supported) ?? null;
392
402
  return {
403
+ // 面板据此判断自己调的方法后台到底有没有(扩展更新后没重启就会出现差异)
404
+ api: METHODS,
393
405
  figures: figs,
394
406
  current: figs.find((f) => f.current)?.id ?? null,
395
407
  pick: pick?.id ?? null,
package/src/writer.js CHANGED
@@ -131,9 +131,10 @@ export function packLooksSupported(packDir) {
131
131
  const st = lstatSync(entry, { throwIfNoEntry: false });
132
132
  if (!st || !st.isFile() || st.size > 2 * 1024 * 1024) return false;
133
133
  const js = readText(entry) ?? '';
134
- // 只认真正会去读这个文件夹的播放器:动作库那个(actions/index.js + 额外动作拓展/index.js)。
135
- // 早先那个只认 ext/clips.js 的补丁不认,别的角色(horo、clawd…)也不认。
136
- return /额外动作拓展|actions\/index\.js|actions\/index['"]/.test(js);
134
+ // 只认入口里真的写着「额外动作拓展」的播放器 —— 那是唯一会去读我们写的文件夹的约定。
135
+ // 不能按 `actions/index.js` 放行:别的作者按帧序列做的形象包也有这个入口,
136
+ // 写进去会连带改它的 figure.json 词表,把它自己的动作词洗掉(扩展市场 Issue #3)。
137
+ return js.includes(EXTRA_DIR);
137
138
  }
138
139
 
139
140
  /** 当前这个包里已经装了哪些额外动作。 */
@@ -202,7 +203,10 @@ function applyVocab(packDir, actions, managed) {
202
203
  const base = (clean && Array.isArray(managed.baseVocab)) ? managed.baseVocab : current;
203
204
 
204
205
  const wanted = new Set(actions.map((a) => a.word).filter(Boolean));
205
- const kept = base.filter((w) => w?.kind !== 'motion' || wanted.has(w?.id));
206
+ // 只回收「以前由这个插件加进去的」词;包里原本就有的动作词一律保留。
207
+ // (旧逻辑是「删掉所有没被勾选的 motion 词」,对外来形象包等于摧毁它的词表。)
208
+ const mine = new Set(Array.isArray(managed?.added) ? managed.added : []);
209
+ const kept = base.filter((w) => !mine.has(w?.id) || wanted.has(w?.id));
206
210
  const have = new Set(kept.map((w) => w?.id));
207
211
  const added = [];
208
212
  for (const a of actions) {
@@ -0,0 +1,61 @@
1
+ /**
2
+ * 面板 ↔ 后台 的接口契约测试。
3
+ *
4
+ * 为什么需要:面板(webui.js)是每次打开都从磁盘读的,而后台的 world 模块只在
5
+ * Coopanion 启动时 import 一次。扩展更新后没重启,就会出现「面板调了新方法、
6
+ * 后台还没有」→ 用户看到一句"接口异常"(日志里是 unknown method),很难自己想到要重启。
7
+ *
8
+ * 这里静态比对:面板 invoke 的方法名,必须都在 world 的 METHODS 清单里;
9
+ * 并且 world 的 switch 必须真的实现了 METHODS 里的每一个。
10
+ */
11
+ import { readFileSync } from 'node:fs';
12
+ import { dirname, join } from 'node:path';
13
+ import { fileURLToPath } from 'node:url';
14
+
15
+ const here = dirname(fileURLToPath(import.meta.url));
16
+ const panel = readFileSync(join(here, '..', 'dist', 'webui.js'), 'utf8');
17
+ const world = readFileSync(join(here, '..', 'src', 'world.js'), 'utf8');
18
+
19
+ const fails = [];
20
+ const check = (fn, msg) => { try { fn(); console.log('ok ' + msg); } catch (e) { fails.push(`${msg} → ${e.message}`); console.log('FAIL ' + msg); } };
21
+
22
+ const panelMethods = [...new Set([...panel.matchAll(/invoke\('([a-z-]+)'/g)].map((m) => m[1]))].sort();
23
+ const methodsBlock = world.match(/const METHODS = \[([\s\S]*?)\];/);
24
+ const declared = methodsBlock ? [...new Set([...methodsBlock[1].matchAll(/'([a-z-]+)'/g)].map((m) => m[1]))].sort() : [];
25
+ const cases = [...new Set([...world.matchAll(/case '([a-z-]+)':/g)].map((m) => m[1]))].sort();
26
+
27
+ console.log(`面板调用 ${panelMethods.length} 个方法:${panelMethods.join(', ')}`);
28
+ console.log(`world 声明 ${declared.length} 个,实现 ${cases.length} 个`);
29
+
30
+ check(() => {
31
+ if (!methodsBlock) throw new Error('world 里找不到 METHODS 清单');
32
+ }, 'world 有 METHODS 清单');
33
+
34
+ check(() => {
35
+ const missing = panelMethods.filter((m) => !declared.includes(m));
36
+ if (missing.length) throw new Error('面板调了但 METHODS 没声明:' + missing.join(', '));
37
+ }, '面板调用的方法都在 METHODS 里');
38
+
39
+ check(() => {
40
+ const unimplemented = declared.filter((m) => !cases.includes(m));
41
+ if (unimplemented.length) throw new Error('METHODS 声明了但 switch 没实现:' + unimplemented.join(', '));
42
+ }, 'METHODS 里的每个方法都真的实现了');
43
+
44
+ check(() => {
45
+ if (!world.includes('api: METHODS')) throw new Error('state 里没带出 api 字段');
46
+ }, 'state 会带出 api 清单(面板据此判断版本是否一致');
47
+
48
+ check(() => {
49
+ if (!panel.includes("includes('download-library')")) throw new Error('面板没有检查 api 里有没有 download-library');
50
+ }, '面板会先检查后台支不支持下载再显示按钮');
51
+
52
+ check(() => {
53
+ if (!panel.includes('unknown method')) throw new Error('面板没有针对 unknown method 的友好提示');
54
+ }, '面板把 unknown method 翻译成「重启 Coopanion」的提示');
55
+
56
+ check(() => {
57
+ if (!cases.includes('reload')) throw new Error('world 没有 reload 别名');
58
+ }, 'world 保留了 reload 别名(兼容老面板)');
59
+
60
+ console.log(fails.length ? `\nFAIL (${fails.length})\n- ${fails.join('\n- ')}` : '\nall checks passed');
61
+ process.exit(fails.length ? 1 : 0);
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Cortico 扩展市场 Issue #3 的回归测试。
3
+ *
4
+ * 1) 只认入口里写着「额外动作拓展」的形象包;别的作者按帧序列做的包(入口只提
5
+ * actions/index.js)必须判为「不可写」,否则我们保存时会改它的 figure.json。
6
+ * 2) 保存只添加/回收「我们自己加过的」动作词,包里原有的 motion 词一条都不许少。
7
+ */
8
+ import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
9
+ import { dirname, join } from 'node:path';
10
+ import { fileURLToPath, pathToFileURL } from 'node:url';
11
+ import assert from 'node:assert/strict';
12
+
13
+ const here = dirname(fileURLToPath(import.meta.url));
14
+ const W = await import(pathToFileURL(join(here, '..', 'src', 'writer.js')).href);
15
+
16
+ const fails = [];
17
+ const check = (fn, msg) => { try { fn(); console.log('ok ' + msg); } catch (e) { fails.push(`${msg} → ${e.message}`); console.log('FAIL ' + msg); } };
18
+
19
+ const root = join(here, '_tmp-issue3');
20
+ rmSync(root, { recursive: true, force: true });
21
+
22
+ /** 造一个形象包:入口脚本写什么由参数决定。 */
23
+ function makePack(name, { entryJs, vocab }) {
24
+ const dir = join(root, name);
25
+ mkdirSync(dir, { recursive: true });
26
+ writeFileSync(join(dir, 'figure.json'), JSON.stringify({
27
+ manifest: 2, api: 2, id: name, version: '1.0.0',
28
+ name: { zh: name }, entry: 'figure.js', vocab,
29
+ }, null, 2), 'utf8');
30
+ writeFileSync(join(dir, 'figure.js'), entryJs, 'utf8');
31
+ return dir;
32
+ }
33
+
34
+ /* ① 别人的帧序列包:入口只提 actions/index.js */
35
+ const foreign = makePack('foreign-pack', {
36
+ entryJs: `export const manifest = 2;\nimport './actions/index.js';\n// 只读 actions/index.js 的播放器\n`,
37
+ vocab: [
38
+ { id: 'hello', kind: 'motion', names: { zh: ['打招呼'] }, seconds: 2 },
39
+ { id: 'bye', kind: 'motion', names: { zh: ['再见'] }, seconds: 2 },
40
+ { id: 'smile', kind: 'expression', names: { zh: ['微笑'] } },
41
+ ],
42
+ });
43
+ check(() => assert.equal(W.packLooksSupported(foreign), false, '别人的帧序列包不能被判为可写'), '只提 actions/index.js 的包判为不可写');
44
+
45
+ /* ② 我们的包:入口里写着 额外动作拓展 */
46
+ const ours = makePack('our-pack', {
47
+ entryJs: `const EXTRA_DIR = '额外动作拓展';\nimport './actions/index.js';\nimport './额外动作拓展/index.js';\n`,
48
+ vocab: [
49
+ { id: 'own-word', kind: 'motion', names: { zh: ['包里原有的动作'] }, seconds: 3 },
50
+ { id: 'body', kind: 'expression', names: { zh: ['待机'] } },
51
+ ],
52
+ });
53
+ check(() => assert.equal(W.packLooksSupported(ours), true), '写了 额外动作拓展 的包判为可写');
54
+
55
+ /* ③ 保存:包里原有的 motion 词必须留着,只加我们勾的 */
56
+ const lib = join(root, 'lib');
57
+ const modDir = join(lib, 'actions', 'core', 'cheer-hop');
58
+ mkdirSync(modDir, { recursive: true });
59
+ writeFileSync(join(modDir, 'action.json'), JSON.stringify({
60
+ id: 'cheer-hop', category: 'core', word: 'cheer-hop', zh: '开心跃动', say: '开心跃动',
61
+ clip: 'actions/core/cheer-hop', frames: 2, fps: 24, seconds: 1, anim: true,
62
+ }), 'utf8');
63
+ writeFileSync(join(modDir, 'anim.webp'), 'RIFFxxxxWEBPVP8 ', 'utf8');
64
+
65
+ const action = {
66
+ id: 'cheer-hop', category: 'core', word: 'cheer-hop', zh: '开心跃动', say: '开心跃动',
67
+ clip: 'actions/core/cheer-hop', frames: 2, fps: 24, seconds: 1, anim: true,
68
+ };
69
+ W.save({ packDir: ours, libraryDir: lib, ids: ['cheer-hop'], catalog: [action] });
70
+ const fig = JSON.parse(readFileSync(join(ours, 'figure.json'), 'utf8'));
71
+ const ids = fig.vocab.map((w) => w.id);
72
+ check(() => assert.ok(ids.includes('own-word'), '包里原有的 motion 词没被删'), '保存后 own-word 还在');
73
+ check(() => assert.ok(ids.includes('body'), '包里原有的 expression 词没被删'), '保存后 body 还在');
74
+ check(() => assert.ok(ids.includes('cheer-hop'), '勾选的动作被加进词表'), '保存后 cheer-hop 已加入');
75
+
76
+ /* ④ 再保存成空:只回收我们加的,原有的一条不少 */
77
+ W.clear(ours, [action]);
78
+ const fig2 = JSON.parse(readFileSync(join(ours, 'figure.json'), 'utf8'));
79
+ const ids2 = fig2.vocab.map((w) => w.id);
80
+ check(() => assert.equal(ids2.includes('cheer-hop'), false, '取消勾选后我们加的词被回收'), '清空后 cheer-hop 已回收');
81
+ check(() => assert.deepEqual(ids2.sort(), ['body', 'own-word']), '清空后包原有的词完整保留');
82
+
83
+ /* ⑤ 别人的包:即使强行调用 save 也不该被改(写之前会判 supported) */
84
+ const before = readFileSync(join(foreign, 'figure.json'), 'utf8');
85
+ check(() => assert.equal(W.packLooksSupported(foreign), false), '外来包始终不可写(保存入口会因此拒绝)');
86
+ check(() => assert.equal(readFileSync(join(foreign, 'figure.json'), 'utf8'), before), '外来包的 figure.json 一个字节都没变');
87
+
88
+ rmSync(root, { recursive: true, force: true });
89
+ console.log(fails.length ? `\nFAIL (${fails.length})\n- ${fails.join('\n- ')}` : '\nall checks passed');
90
+ process.exit(fails.length ? 1 : 0);