cortico-world-extra-actions 0.3.27 → 0.3.29
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 +26 -1
- package/package.json +1 -1
- package/src/online.js +19 -4
- package/src/world.js +27 -6
- package/src/writer.js +12 -3
package/dist/webui.js
CHANGED
|
@@ -491,9 +491,32 @@ const panel = {
|
|
|
491
491
|
return '扩展刚更新过,但 Coopanion 还在用旧的后台。请完全退出 Coopanion 再打开,这个按钮就能用了。';
|
|
492
492
|
}
|
|
493
493
|
if (/内置形象包不给写/.test(raw)) return '这是内置形象包,不能写。先「建立空的角色包」再保存。';
|
|
494
|
+
if (/超时|TimeoutError|连不上|ECONNRESET|ENOTFOUND|EAI_AGAIN/i.test(raw)) {
|
|
495
|
+
return `网络没连上(${raw})。换个网络再试,或者用「动作库 ▾」→「手动下载」自己下完整库,然后用「选择动作库位置…」指过去。`;
|
|
496
|
+
}
|
|
497
|
+
if (/HTTP 404/.test(raw)) return '下载源上那个文件不在了(404)。到「动作库 ▾」→「手动下载」拿完整库,再选位置指过去。';
|
|
494
498
|
return raw || '操作失败';
|
|
495
499
|
}
|
|
496
500
|
|
|
501
|
+
/** 下载期间轮询后台进度:第几片、下了多少 MB。 */
|
|
502
|
+
let pollTimer = null;
|
|
503
|
+
function startPoll() {
|
|
504
|
+
stopPoll();
|
|
505
|
+
const t0 = Date.now();
|
|
506
|
+
pollTimer = setInterval(async () => {
|
|
507
|
+
let p = null;
|
|
508
|
+
try { p = await ctx.invoke('download-progress', []); } catch { return; }
|
|
509
|
+
if (!p || !p.active) return;
|
|
510
|
+
const mb = (n) => (n / 1048576).toFixed(0);
|
|
511
|
+
const secs = Math.round((Date.now() - t0) / 1000);
|
|
512
|
+
const total = p.totalBytes ? ` / ${mb(p.totalBytes)} MB` : '';
|
|
513
|
+
say(`正在下载完整动作库… ${p.done}/${p.total} 片,已下 ${mb(p.bytes)} MB${total}(${secs}s)`);
|
|
514
|
+
}, 700);
|
|
515
|
+
}
|
|
516
|
+
function stopPoll() {
|
|
517
|
+
if (pollTimer) { clearInterval(pollTimer); pollTimer = null; }
|
|
518
|
+
}
|
|
519
|
+
|
|
497
520
|
/** 一键:下载完整动作库并设为当前库。 */
|
|
498
521
|
async function doDownloadLibrary() {
|
|
499
522
|
if (!apiHas('download-library')) {
|
|
@@ -501,7 +524,8 @@ const panel = {
|
|
|
501
524
|
return;
|
|
502
525
|
}
|
|
503
526
|
busy(true);
|
|
504
|
-
say('
|
|
527
|
+
say('正在连接下载源…(约 370 MB,视网速几分钟,别关这个页面)');
|
|
528
|
+
startPoll();
|
|
505
529
|
try {
|
|
506
530
|
const r = await ctx.invoke('download-library', []);
|
|
507
531
|
await loadAll();
|
|
@@ -509,6 +533,7 @@ const panel = {
|
|
|
509
533
|
} catch (err) {
|
|
510
534
|
say('下载没成功:' + explainErr(err), true);
|
|
511
535
|
} finally {
|
|
536
|
+
stopPoll();
|
|
512
537
|
busy(false);
|
|
513
538
|
}
|
|
514
539
|
}
|
package/package.json
CHANGED
package/src/online.js
CHANGED
|
@@ -65,9 +65,17 @@ function commonTop(names) {
|
|
|
65
65
|
|
|
66
66
|
const isHttp = (u) => typeof u === 'string' && /^https?:\/\//i.test(u);
|
|
67
67
|
|
|
68
|
+
const FETCH_TIMEOUT_MS = 5 * 60 * 1000; // 单片最长 5 分钟;连不上会在这里断掉,而不是无限转圈
|
|
69
|
+
|
|
68
70
|
async function fetchBytes(url, cap) {
|
|
69
71
|
if (!isHttp(url)) throw new Error(`只支持 http(s) 地址:${url}`);
|
|
70
|
-
|
|
72
|
+
let r;
|
|
73
|
+
try {
|
|
74
|
+
r = await fetch(url, { redirect: 'follow', signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) });
|
|
75
|
+
} catch (e) {
|
|
76
|
+
const why = e?.name === 'TimeoutError' ? '超时(5 分钟没下完)' : (e?.cause?.code ?? e?.message ?? '网络错误');
|
|
77
|
+
throw new Error(`连不上:${why}`);
|
|
78
|
+
}
|
|
71
79
|
if (!r.ok) throw new Error(`下载失败:HTTP ${r.status}`);
|
|
72
80
|
const len = Number(r.headers.get('content-length') ?? 0);
|
|
73
81
|
if (len && len > cap) throw new Error(`文件太大(${(len / 1048576).toFixed(0)} MB):${url}`);
|
|
@@ -156,8 +164,10 @@ export const flatActionUrl = (base, id) => `${String(base).replace(/\/+$/, '')}/
|
|
|
156
164
|
export async function installLibraryParts({ parts = LIB_PARTS, dest, log, onProgress }) {
|
|
157
165
|
mkdirSync(dest, { recursive: true });
|
|
158
166
|
const before = new Set(summarize(dest));
|
|
167
|
+
const totalBytes = parts.reduce((sum, p) => sum + (p.bytes || 0), 0);
|
|
159
168
|
let bytes = 0;
|
|
160
169
|
let failed = 0;
|
|
170
|
+
const errors = [];
|
|
161
171
|
for (let i = 0; i < parts.length; i++) {
|
|
162
172
|
const p = parts[i];
|
|
163
173
|
let buf = null;
|
|
@@ -165,7 +175,12 @@ export async function installLibraryParts({ parts = LIB_PARTS, dest, log, onProg
|
|
|
165
175
|
for (const url of partTarballs(p)) {
|
|
166
176
|
try { buf = await fetchBytes(url, MAX_LIBRARY_BYTES); break; } catch (e) { lastErr = e; }
|
|
167
177
|
}
|
|
168
|
-
if (!buf) {
|
|
178
|
+
if (!buf) {
|
|
179
|
+
failed++;
|
|
180
|
+
errors.push(`第 ${i + 1} 片:${lastErr?.message ?? '未知错误'}`);
|
|
181
|
+
log?.(`第 ${i + 1} 片下载失败:${lastErr?.message ?? '未知错误'}`);
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
169
184
|
bytes += buf.length;
|
|
170
185
|
if (p.sha256) {
|
|
171
186
|
const got = createHash('sha256').update(buf).digest('hex');
|
|
@@ -177,9 +192,9 @@ export async function installLibraryParts({ parts = LIB_PARTS, dest, log, onProg
|
|
|
177
192
|
onFile: (rel) => WANT.test(rel),
|
|
178
193
|
});
|
|
179
194
|
log?.(`第 ${i + 1}/${parts.length} 片:${r.files.length} 个文件`);
|
|
180
|
-
onProgress?.(i + 1, parts.length);
|
|
195
|
+
onProgress?.({ done: i + 1, total: parts.length, bytes, totalBytes });
|
|
181
196
|
}
|
|
182
197
|
const mods = summarize(dest);
|
|
183
198
|
const added = mods.filter((m) => !before.has(m));
|
|
184
|
-
return { ok: failed === 0, path: dest, modules: mods.length, added: added.length, failed, bytes };
|
|
199
|
+
return { ok: failed === 0, path: dest, modules: mods.length, added: added.length, failed, bytes, totalBytes, errors };
|
|
185
200
|
}
|
package/src/world.js
CHANGED
|
@@ -29,7 +29,7 @@ const LIBRARY_PAGE = 'https://gitee.com/Daniele-WOLF-Holo/shenshen-actions/relea
|
|
|
29
29
|
const METHODS = [
|
|
30
30
|
'state', 'catalog', 'dirs', 'save', 'remove', 'clear', 'prune', 'add',
|
|
31
31
|
'reindex', 'find-libraries', 'set-library', 'use-starter-library', 'restore-pack',
|
|
32
|
-
'download-library', 'download-action', 'preview', 'dismiss-onboard', 'reload',
|
|
32
|
+
'download-library', 'download-action', 'download-progress', 'preview', 'dismiss-onboard', 'reload',
|
|
33
33
|
];
|
|
34
34
|
|
|
35
35
|
const ID = 'extra-actions';
|
|
@@ -387,6 +387,8 @@ export class ExtraActionsWorld {
|
|
|
387
387
|
return this.downloadLibrary();
|
|
388
388
|
case 'download-action':
|
|
389
389
|
return this.downloadAction(String(a[0] ?? ''), String(a[1] ?? ''));
|
|
390
|
+
case 'download-progress':
|
|
391
|
+
return this.downloadProgress();
|
|
390
392
|
// 老版本面板会调 reload:当成「重新读一遍状态」,避免 unknown method 弹错
|
|
391
393
|
case 'reload':
|
|
392
394
|
return this.state();
|
|
@@ -602,6 +604,11 @@ export class ExtraActionsWorld {
|
|
|
602
604
|
};
|
|
603
605
|
}
|
|
604
606
|
|
|
607
|
+
/** 面板轮询:这次下载进行到哪了(没有下载时 active=false)。 */
|
|
608
|
+
downloadProgress() {
|
|
609
|
+
return this._dl ?? { active: false };
|
|
610
|
+
}
|
|
611
|
+
|
|
605
612
|
/** 一键:下载完整动作库 → 解压进动作库 → 设为当前库。 */
|
|
606
613
|
async downloadLibrary() {
|
|
607
614
|
const info = this.downloadInfo();
|
|
@@ -609,11 +616,22 @@ export class ExtraActionsWorld {
|
|
|
609
616
|
// 没配自家源时:走 npm 分片(国内走 npmmirror,比 GitHub 稳)
|
|
610
617
|
if (!info.base && LIB_PARTS.length) {
|
|
611
618
|
const dest = existing && existsSync(existing) ? existing : join(this.dataDir, 'shenshen-library');
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
619
|
+
this._dl = { active: true, phase: 'download', done: 0, total: LIB_PARTS.length, bytes: 0, totalBytes: info.bytes, startedAt: Date.now() };
|
|
620
|
+
let r;
|
|
621
|
+
try {
|
|
622
|
+
r = await installLibraryParts({
|
|
623
|
+
dest,
|
|
624
|
+
log: (m) => this.host?.log?.info?.(m),
|
|
625
|
+
onProgress: (p) => {
|
|
626
|
+
this._dl = { ...this._dl, ...p, active: true, phase: 'download' };
|
|
627
|
+
this.host?.log?.info?.(`动作库 ${p.done}/${p.total} 片`);
|
|
628
|
+
},
|
|
629
|
+
});
|
|
630
|
+
} catch (e) {
|
|
631
|
+
this._dl = { ...(this._dl ?? {}), active: false, phase: 'error', error: String(e?.message ?? e) };
|
|
632
|
+
throw e;
|
|
633
|
+
}
|
|
634
|
+
this._dl = { ...this._dl, active: false, phase: 'done', modules: r.modules, failed: r.failed, errors: r.errors };
|
|
617
635
|
// 分片有失败(镜像没同步等)时就再用整包兜一次,别让用户停在半截
|
|
618
636
|
if (!r.ok || r.modules < 20) {
|
|
619
637
|
this.host?.log?.warn?.(`分片装了 ${r.modules} 段(失败 ${r.failed} 片),改用整包兜底`);
|
|
@@ -628,9 +646,11 @@ export class ExtraActionsWorld {
|
|
|
628
646
|
}
|
|
629
647
|
this.saveSettings({ libraryDir: r.path });
|
|
630
648
|
this.bustCatalog();
|
|
649
|
+
this._dl = { ...(this._dl ?? {}), active: false, phase: 'done', modules: r.modules, failed: r.failed };
|
|
631
650
|
return { ok: r.ok, ...r, libraryDir: r.path };
|
|
632
651
|
}
|
|
633
652
|
if (!info.libraryUrl) throw new Error('还没配置下载源');
|
|
653
|
+
this._dl = { active: true, phase: 'zip', done: 0, total: 1, bytes: 0, totalBytes: 0, startedAt: Date.now() };
|
|
634
654
|
const r = await installLibrary({
|
|
635
655
|
url: info.libraryUrl,
|
|
636
656
|
libraryDir: existing,
|
|
@@ -639,6 +659,7 @@ export class ExtraActionsWorld {
|
|
|
639
659
|
});
|
|
640
660
|
this.saveSettings({ libraryDir: r.path });
|
|
641
661
|
this.bustCatalog();
|
|
662
|
+
this._dl = { ...(this._dl ?? {}), active: false, phase: 'done', modules: r.modules, bytes: r.bytes };
|
|
642
663
|
this.host?.log?.info?.(`动作库已装好:${r.modules} 段(新增 ${r.added})`);
|
|
643
664
|
return { ok: true, ...r, libraryDir: r.path };
|
|
644
665
|
}
|
package/src/writer.js
CHANGED
|
@@ -228,9 +228,13 @@ function applyVocab(packDir, actions, managed) {
|
|
|
228
228
|
added.push(a.word);
|
|
229
229
|
}
|
|
230
230
|
const removed = current.filter((w) => !have.has(w?.id)).map((w) => w?.id);
|
|
231
|
+
// 没有实质变化就不落盘:figure.json 也是宿主在读的文件,多余的重写会让
|
|
232
|
+
// 「外观跟着形象走」这类逻辑重新求值,用户改的配色看起来就"掉回默认"。
|
|
233
|
+
const unchanged = sameIds(current.map((w) => w?.id), kept.map((w) => w?.id));
|
|
234
|
+
if (unchanged) return { added, removed, baseVocab: base, written: kept.map((w) => w?.id), wrote: false };
|
|
231
235
|
fig.vocab = kept;
|
|
232
236
|
writeJson(file, fig);
|
|
233
|
-
return { added, removed, baseVocab: base, written: kept.map((w) => w?.id) };
|
|
237
|
+
return { added, removed, baseVocab: base, written: kept.map((w) => w?.id), wrote: true };
|
|
234
238
|
}
|
|
235
239
|
|
|
236
240
|
/** 清空时把词表还回去:先回到 base,实在对不上就只摘掉自己加的那些。 */
|
|
@@ -241,13 +245,18 @@ function restoreVocab(packDir, managed) {
|
|
|
241
245
|
const current = Array.isArray(fig.vocab) ? fig.vocab : [];
|
|
242
246
|
const clean = managed?.written && sameIds(current.map((w) => w?.id), managed.written);
|
|
243
247
|
let restored = false;
|
|
248
|
+
let next;
|
|
244
249
|
if (clean && Array.isArray(managed.baseVocab)) {
|
|
245
|
-
|
|
250
|
+
next = managed.baseVocab;
|
|
246
251
|
restored = true;
|
|
247
252
|
} else {
|
|
248
253
|
const drop = new Set(Array.isArray(managed?.added) ? managed.added : []);
|
|
249
|
-
|
|
254
|
+
next = current.filter((w) => !drop.has(w?.id));
|
|
250
255
|
}
|
|
256
|
+
if (sameIds((next ?? []).map((w) => w?.id), current.map((w) => w?.id))) {
|
|
257
|
+
return { added: [], removed: [], restored, wrote: false }; // 已经是这个状态,不碰文件
|
|
258
|
+
}
|
|
259
|
+
fig.vocab = next;
|
|
251
260
|
writeJson(file, fig);
|
|
252
261
|
return { added: [], removed: [], restored };
|
|
253
262
|
}
|