cortico-world-agent-speed 0.2.2 → 0.2.3
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/CHANGELOG.md +1 -1
- package/README.md +1 -1
- package/dist/webui.css +1 -1
- package/dist/webui.css.map +3 -3
- package/dist/webui.js +1 -1
- package/dist/webui.js.map +4 -4
- package/host/electron-main.cjs +5 -0
- package/package.json +1 -1
- package/src/pack-import.ts +0 -117
- package/src/webui/client.ts +9 -3
- package/src/webui/style.css +0 -13
- package/src/window-host.ts +9 -1
- package/src/world.ts +12 -91
- package/src/webui/dress-panel.ts +0 -160
package/host/electron-main.cjs
CHANGED
|
@@ -535,6 +535,11 @@ function runPetHost({ url, parentPid = 0, tray: withTray = true }) {
|
|
|
535
535
|
dress.loadURL(target);
|
|
536
536
|
};
|
|
537
537
|
|
|
538
|
+
// The World can open its own dressing window without relying on renderer readiness.
|
|
539
|
+
process.on('message', (message) => {
|
|
540
|
+
if (message === 'open-dress') void app.whenReady().then(() => openDress());
|
|
541
|
+
});
|
|
542
|
+
|
|
538
543
|
onPet('pet:interactive', (_e, on) => { if (!shape) mouse.ask(on); });
|
|
539
544
|
onPet('pet:inputRects', (_e, rects) => { if (shape && Array.isArray(rects)) mouse.ask(rects); });
|
|
540
545
|
onPet('pet:focus', () => {
|
package/package.json
CHANGED
package/src/pack-import.ts
CHANGED
|
@@ -242,120 +242,3 @@ export class PackImporter {
|
|
|
242
242
|
if (staged) await rm(staged.root, { recursive: true, force: true });
|
|
243
243
|
}
|
|
244
244
|
}
|
|
245
|
-
|
|
246
|
-
const ZIP_CHUNK_BYTES = 48 * 1024;
|
|
247
|
-
const ZIP_EXPANDED_MAX = 2 * IMPORT_LARGE;
|
|
248
|
-
const BASE64 = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/][AQgw]==|[A-Za-z0-9+/]{2}[AEIMQUYcgkosw048]=)?$/;
|
|
249
|
-
|
|
250
|
-
type ZipUpload = {
|
|
251
|
-
token: string;
|
|
252
|
-
bytes: Buffer | null;
|
|
253
|
-
received: number;
|
|
254
|
-
importer?: PackImporter;
|
|
255
|
-
stage?: StageResult;
|
|
256
|
-
pending?: Promise<unknown>;
|
|
257
|
-
cancelled?: boolean;
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
/** The settings invoke flow owns its staging slot; the standalone dress page cannot replace or cancel it. */
|
|
261
|
-
export class DressZipImport {
|
|
262
|
-
private session: ZipUpload | null = null;
|
|
263
|
-
|
|
264
|
-
constructor(private readonly createImporter: (text: PetText) => PackImporter) {}
|
|
265
|
-
|
|
266
|
-
async start(total: unknown, text: PetText): Promise<string> {
|
|
267
|
-
if (typeof total !== 'number' || !Number.isSafeInteger(total) || total < 22 || total > IMPORT_LARGE) {
|
|
268
|
-
throw new Error(`ZIP size must be between 22 and ${IMPORT_LARGE} bytes`);
|
|
269
|
-
}
|
|
270
|
-
if (this.session?.pending) throw new Error(text.importing.expired);
|
|
271
|
-
await this.cancel();
|
|
272
|
-
if (this.session) throw new Error(text.importing.expired);
|
|
273
|
-
const token = randomUUID();
|
|
274
|
-
this.session = { token, bytes: Buffer.allocUnsafe(total), received: 0 };
|
|
275
|
-
return token;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
chunk(token: unknown, base64: unknown, text: PetText): void {
|
|
279
|
-
const s = this.upload(token, text);
|
|
280
|
-
const { bytes } = s;
|
|
281
|
-
if (typeof base64 !== 'string' || !base64.length || base64.length > ZIP_CHUNK_BYTES / 3 * 4
|
|
282
|
-
|| base64.length % 4 !== 0 || !BASE64.test(base64)) {
|
|
283
|
-
throw new Error(text.importing.truncated);
|
|
284
|
-
}
|
|
285
|
-
const size = Buffer.byteLength(base64, 'base64');
|
|
286
|
-
if (size > bytes.length - s.received) throw new Error(text.importing.truncated);
|
|
287
|
-
s.received += bytes.write(base64, s.received, size, 'base64');
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
finish(token: unknown, text: PetText): Promise<{
|
|
291
|
-
token: string;
|
|
292
|
-
packs: Array<Pick<StagedPack, 'dir' | 'id' | 'name' | 'installed'>>;
|
|
293
|
-
problems: StageResult['problems'];
|
|
294
|
-
}> {
|
|
295
|
-
const s = this.upload(token, text);
|
|
296
|
-
if (s.received !== s.bytes.length) throw new Error(text.importing.truncated);
|
|
297
|
-
const files = filesFromZip(s.bytes, text.importing, ZIP_EXPANDED_MAX);
|
|
298
|
-
this.session = null;
|
|
299
|
-
const session: ZipUpload = { token: s.token, bytes: null, received: 0 };
|
|
300
|
-
if (typeof files === 'string') throw new Error(files);
|
|
301
|
-
this.session = session;
|
|
302
|
-
const importer = this.createImporter(text);
|
|
303
|
-
session.importer = importer;
|
|
304
|
-
const pending = (async () => {
|
|
305
|
-
try {
|
|
306
|
-
const stage = await importer.stage(files, false);
|
|
307
|
-
if (session.cancelled) throw new Error(text.importing.expired);
|
|
308
|
-
if ('error' in stage) throw new Error(stage.error);
|
|
309
|
-
if (stage.token) session.stage = stage;
|
|
310
|
-
const { token, packs, problems } = stage;
|
|
311
|
-
return { token, packs: packs.map(({ dir, id, name, installed }) => ({ dir, id, name, installed })), problems };
|
|
312
|
-
} finally {
|
|
313
|
-
delete session.pending;
|
|
314
|
-
if (!session.stage && !session.cancelled) this.session = null;
|
|
315
|
-
}
|
|
316
|
-
})();
|
|
317
|
-
session.pending = pending;
|
|
318
|
-
return pending;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
async install(token: unknown, dirs: unknown, replace: unknown, text: PetText): Promise<{ installed: string[] } | { conflicts: string[] }> {
|
|
322
|
-
const s = this.session;
|
|
323
|
-
if (!s?.importer || !s.stage || s.cancelled || s.pending || token !== s.stage.token) throw new Error(text.importing.expired);
|
|
324
|
-
const chosen = s.stage.packs.filter((p) => Array.isArray(dirs) && dirs.includes(p.dir));
|
|
325
|
-
if (!Array.isArray(dirs) || !chosen.length || dirs.length !== chosen.length
|
|
326
|
-
|| !Array.isArray(replace) || !replace.every((id) => chosen.some((p) => p.id === id))) {
|
|
327
|
-
throw new Error(text.importing.noneChosen);
|
|
328
|
-
}
|
|
329
|
-
const pending = s.importer.install(s.stage.token, dirs, replace);
|
|
330
|
-
s.pending = pending;
|
|
331
|
-
try {
|
|
332
|
-
const result = await pending;
|
|
333
|
-
if ('error' in result) throw new Error(result.error);
|
|
334
|
-
if ('installed' in result && !s.cancelled) this.session = null;
|
|
335
|
-
return result;
|
|
336
|
-
} finally {
|
|
337
|
-
delete s.pending;
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/** An omitted token is for World shutdown and resetting the panel slot, never a panel request. */
|
|
342
|
-
async cancel(token?: string): Promise<void> {
|
|
343
|
-
const s = this.session;
|
|
344
|
-
if (!s || (token !== undefined && token !== s.token && token !== s.stage?.token)) return;
|
|
345
|
-
s.cancelled = true;
|
|
346
|
-
s.bytes = null;
|
|
347
|
-
// Do not delete a staging directory while it is being built or copied into the install root.
|
|
348
|
-
await s.pending?.catch(() => {});
|
|
349
|
-
try {
|
|
350
|
-
await s.importer?.cancel();
|
|
351
|
-
} finally {
|
|
352
|
-
if (this.session === s) this.session = null;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
private upload(token: unknown, text: PetText): ZipUpload & { bytes: Buffer } {
|
|
357
|
-
const s = this.session;
|
|
358
|
-
if (!s?.bytes || s.cancelled || token !== s.token) throw new Error(text.importing.expired);
|
|
359
|
-
return s as ZipUpload & { bytes: Buffer };
|
|
360
|
-
}
|
|
361
|
-
}
|
package/src/webui/client.ts
CHANGED
|
@@ -11,7 +11,6 @@ import { petText } from '../i18n/index.ts';
|
|
|
11
11
|
import { describeHostReason, type HostReason } from '../host-reason.ts';
|
|
12
12
|
import { captureKey, PRESSES, splitTaps, withTaps } from './key-capture.ts';
|
|
13
13
|
import { apiRows, type ApiState } from './api-rows.ts';
|
|
14
|
-
import { mountDress } from './dress-panel.ts';
|
|
15
14
|
import './style.css';
|
|
16
15
|
|
|
17
16
|
interface Artifact { phase: 'absent' | 'working' | 'ready' | 'error'; path: string; done: number; total: number | null; detail: string | null }
|
|
@@ -111,9 +110,10 @@ const petPanel: WebUiPanel = {
|
|
|
111
110
|
const btnInstall = ui.button(t.install, { size: 'sm', variant: 'primary' });
|
|
112
111
|
rt.acts.append(btnInstall);
|
|
113
112
|
|
|
114
|
-
const dress = ui.sheet({ title: t.dress, desc:
|
|
113
|
+
const dress = ui.sheet({ title: t.dress, desc: t.dressHint });
|
|
115
114
|
root.appendChild(dress.el);
|
|
116
|
-
|
|
115
|
+
const btnDress = ui.button(t.dress, { size: 'sm', variant: 'primary' });
|
|
116
|
+
dress.body.append(btnDress);
|
|
117
117
|
|
|
118
118
|
s.append(win.row, rt.row);
|
|
119
119
|
dress.el.hidden = true;
|
|
@@ -145,6 +145,12 @@ const petPanel: WebUiPanel = {
|
|
|
145
145
|
btnOpen.addEventListener('click', call('openWindow', btnOpen));
|
|
146
146
|
btnClose.addEventListener('click', call('closeWindow', btnClose));
|
|
147
147
|
btnInstall.addEventListener('click', call('installElectron', btnInstall));
|
|
148
|
+
btnDress.addEventListener('click', async () => {
|
|
149
|
+
btnDress.disabled = true;
|
|
150
|
+
try { await ctx.invoke('openDress'); }
|
|
151
|
+
catch (err) { if (!ctx.signal.aborted) problem(btnDress, errText(err)); }
|
|
152
|
+
finally { btnDress.disabled = false; }
|
|
153
|
+
});
|
|
148
154
|
void refresh();
|
|
149
155
|
ctx.interval(() => void refresh(), 1500);
|
|
150
156
|
},
|
package/src/webui/style.css
CHANGED
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
.pet-dress-controls,.pet-dress-axes{display:grid; gap:0}
|
|
2
|
-
.pet-dress-field{display:flex; align-items:center; justify-content:space-between; gap:16px; padding:12px 0; border-top:1px solid var(--line, #e2dfe8)}
|
|
3
|
-
.pet-dress-caption{display:flex; flex-direction:column; gap:3px; font-weight:600; color:var(--ink, #1b1a1e)}
|
|
4
|
-
.pet-dress-caption small{font-size:13px; font-weight:400; color:var(--ink-soft, #5c5c60)}
|
|
5
|
-
.pet-dress-field select,.pet-dress-field input{max-width:60%; min-width:0; padding:7px 10px; border:1px solid var(--line-2, #d0d2d0); border-radius:6px; background:var(--sheet, #fff); color:var(--ink, #1b1a1e); font:inherit}
|
|
6
|
-
.pet-dress-field select:focus-visible,.pet-dress-field input:focus-visible{outline:2px solid var(--accent, #00a870); outline-offset:2px}
|
|
7
|
-
.pet-dress-size{display:flex; align-items:center; justify-content:flex-end; gap:6px}
|
|
8
|
-
.pet-dress-size input{width:6em; max-width:100%}
|
|
9
|
-
.pet-dress-feedback{font-size:13px; line-height:1.5; color:var(--ink-soft, #5c5c60)}
|
|
10
|
-
.pet-dress-feedback:not(:empty){padding-bottom:12px}
|
|
11
|
-
.pet-dress-feedback.bad{color:var(--danger, #b33b3b)}
|
|
12
|
-
.pet-dress-actions{display:flex; flex-wrap:wrap; gap:8px; padding:12px 0}
|
|
13
|
-
@media(max-width:480px){.pet-dress-field{align-items:flex-start; flex-direction:column; gap:8px}.pet-dress-field select{max-width:100%; width:100%}}
|
|
14
1
|
/* the meter belongs to how it listens: no rule between them */
|
|
15
2
|
.pet-voicebody .mountrow.pet-meterrow{border-top:0; padding:4px 0 6px}
|
|
16
3
|
/* the name's characters sit 1px low in their line box; the bar follows them */
|
package/src/window-host.ts
CHANGED
|
@@ -64,7 +64,7 @@ export class WindowHost {
|
|
|
64
64
|
let child: ChildProcess;
|
|
65
65
|
try {
|
|
66
66
|
// the window closes by itself if this process dies without stopping it
|
|
67
|
-
child = spawn(cmd.command, [...cmd.args, `--parent-pid=${process.pid}`], { env, stdio: ['ignore', 'pipe', 'pipe'], windowsHide: false });
|
|
67
|
+
child = spawn(cmd.command, [...cmd.args, `--parent-pid=${process.pid}`], { env, stdio: ['ignore', 'pipe', 'pipe', 'ipc'], windowsHide: false });
|
|
68
68
|
} catch (err) {
|
|
69
69
|
this.st = { phase: 'error', pid: null, source: cmd.source, reason: { kind: 'startFailed', why: (err as Error).message } };
|
|
70
70
|
return;
|
|
@@ -100,6 +100,14 @@ export class WindowHost {
|
|
|
100
100
|
});
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
+
openDress(): Promise<void> {
|
|
104
|
+
const child = this.child;
|
|
105
|
+
if (!child?.connected) return Promise.reject(new Error('Agent Speed pet window is not running'));
|
|
106
|
+
const { promise, resolve, reject } = Promise.withResolvers<void>();
|
|
107
|
+
child.send('open-dress', (error) => error ? reject(error) : resolve());
|
|
108
|
+
return promise;
|
|
109
|
+
}
|
|
110
|
+
|
|
103
111
|
async stop(): Promise<void> {
|
|
104
112
|
const child = this.child;
|
|
105
113
|
this.child = null;
|
package/src/world.ts
CHANGED
|
@@ -54,12 +54,12 @@ import { toSimplified } from './asr/simplify.ts';
|
|
|
54
54
|
import { estimateSeconds, parseActions, parseScript, vocabChange, vocabTable, type VocabWord } from './script.ts';
|
|
55
55
|
import { ActivityGroup, ChatSockets, SELF_TYPE, chatHistory, chatItem, chatRefs, type ChatFigure } from './chat.ts';
|
|
56
56
|
import { ASK_OPTION_MAX, petToolDecls } from './tools.ts';
|
|
57
|
-
import { COO, figurePacks, lookOf,
|
|
57
|
+
import { COO, figurePacks, lookOf, modelName, nameIn, packFor, type FigurePack, type MoveTarget, type PackProblem, type PackScan } from './packs.ts';
|
|
58
58
|
import { moveChange, moveTable, readMove } from './moves.ts';
|
|
59
59
|
import { Cua } from './cua/cua.ts';
|
|
60
60
|
import { CUA_OFF_TOOL_DECLS } from './cua/tools.ts';
|
|
61
61
|
import type { Answer } from './cua/engine-ipc.ts';
|
|
62
|
-
import {
|
|
62
|
+
import { PackImporter } from './pack-import.ts';
|
|
63
63
|
import { dressTable, planSettings, sayScale, type SettingChange } from './self.ts';
|
|
64
64
|
import { FIREWORKS_WORD, MODEL_TEXT, type ModelLanguage, type ModelText, type NoPortrait } from './model-text.ts';
|
|
65
65
|
|
|
@@ -521,7 +521,6 @@ export class DesktopPetWorld implements World {
|
|
|
521
521
|
|
|
522
522
|
/** Installs packs the dressing page sends, when the app named a directory for them. */
|
|
523
523
|
private readonly importer: PackImporter | null;
|
|
524
|
-
private readonly dressImport: DressZipImport | null;
|
|
525
524
|
|
|
526
525
|
constructor(private readonly opts: DesktopPetWorldOptions) {
|
|
527
526
|
this.cfg = opts.cfg;
|
|
@@ -540,8 +539,6 @@ export class DesktopPetWorld implements World {
|
|
|
540
539
|
},
|
|
541
540
|
});
|
|
542
541
|
this.importer = opts.packDir ? new PackImporter({ packDir: opts.packDir, packs: () => this.packs(), text: () => this.ui }) : null;
|
|
543
|
-
const packDir = opts.packDir;
|
|
544
|
-
this.dressImport = packDir ? new DressZipImport((text) => new PackImporter({ packDir, packs: () => this.packs(), text: () => text })) : null;
|
|
545
542
|
this.status = new StatusTracker((status) => this.server.sendPet({ t: 'status', status }), opts.describeTool, () => this.ui.busy);
|
|
546
543
|
this.segmenter = new Segmenter(this.segmentConfig(), FRAME_MS);
|
|
547
544
|
this.segmenter.setSink(this.sink);
|
|
@@ -825,7 +822,6 @@ export class DesktopPetWorld implements World {
|
|
|
825
822
|
if (this.holidayTimer) clearInterval(this.holidayTimer);
|
|
826
823
|
this.holidayTimer = null;
|
|
827
824
|
this.savePosition();
|
|
828
|
-
await this.dressImport?.cancel();
|
|
829
825
|
await this.importer?.cancel();
|
|
830
826
|
if (this.packTimer) clearTimeout(this.packTimer);
|
|
831
827
|
this.packTimer = null;
|
|
@@ -2631,7 +2627,7 @@ export class DesktopPetWorld implements World {
|
|
|
2631
2627
|
this.voiceSockets.add(socket);
|
|
2632
2628
|
socket.onClose(() => this.voiceSockets.delete(socket));
|
|
2633
2629
|
},
|
|
2634
|
-
links: this.server.port ? [{ label: c.viewInBrowser, href: this.petUrl }
|
|
2630
|
+
links: this.server.port ? [{ label: c.viewInBrowser, href: this.petUrl }] : [],
|
|
2635
2631
|
config: desktopPetConfigGroups(language),
|
|
2636
2632
|
promptDocs: [{
|
|
2637
2633
|
key: `worlds.${DESKTOP_PET_ID}.envPrompt`,
|
|
@@ -2644,94 +2640,19 @@ export class DesktopPetWorld implements World {
|
|
|
2644
2640
|
};
|
|
2645
2641
|
}
|
|
2646
2642
|
|
|
2647
|
-
/** A settings client needs option ids, even when the stored scheme names a preset. */
|
|
2648
|
-
private dressState(language: Language, packs = this.packs()) {
|
|
2649
|
-
const skin = this.cfg.skin;
|
|
2650
|
-
const pack = packFor(packs, skin.figure);
|
|
2651
|
-
const scheme = pack ? lookOf(pack, skin) : '';
|
|
2652
|
-
const parts = scheme.split('-');
|
|
2653
|
-
const preset = pack?.id === COO ? undefined : pack?.manifest.presets.find((p) => p.id === scheme);
|
|
2654
|
-
return {
|
|
2655
|
-
skin: pack ? {
|
|
2656
|
-
figure: pack.id,
|
|
2657
|
-
scheme: pack.manifest.axes.map((a, i) => {
|
|
2658
|
-
const pick = preset ? preset.pick[a.id] : parts[i];
|
|
2659
|
-
return a.options.find((o) => o.id === pick)?.id ?? a.options[0]!.id;
|
|
2660
|
-
}).join('-'),
|
|
2661
|
-
} : {},
|
|
2662
|
-
scale: this.cfg.window.scale,
|
|
2663
|
-
packs: packs.map(({ id, manifest }) => ({
|
|
2664
|
-
id, name: nameIn(manifest.name, language),
|
|
2665
|
-
axes: manifest.axes.map((a) => ({
|
|
2666
|
-
id: a.id, label: nameIn(a.name, language),
|
|
2667
|
-
options: a.options.map((o) => ({ id: o.id, label: nameIn(o.name, language) })),
|
|
2668
|
-
})),
|
|
2669
|
-
})),
|
|
2670
|
-
importable: !!this.dressImport,
|
|
2671
|
-
};
|
|
2672
|
-
}
|
|
2673
|
-
|
|
2674
|
-
private saveDress(raw: unknown, language: Language) {
|
|
2675
|
-
if (!raw || typeof raw !== 'object' || Array.isArray(raw)
|
|
2676
|
-
|| Object.keys(raw).some((key) => key !== 'figure' && key !== 'scheme' && key !== 'scale')) {
|
|
2677
|
-
throw new Error('Invalid dress settings');
|
|
2678
|
-
}
|
|
2679
|
-
const patch = raw as Record<string, unknown>;
|
|
2680
|
-
const t = petText(language);
|
|
2681
|
-
const scale = scaleOf(patch.scale);
|
|
2682
|
-
if (Object.hasOwn(patch, 'scale') && scale === null) throw new Error('Invalid dress scale');
|
|
2683
|
-
const packs = this.packs();
|
|
2684
|
-
const hasFigure = Object.hasOwn(patch, 'figure'), hasScheme = Object.hasOwn(patch, 'scheme');
|
|
2685
|
-
if (hasFigure || hasScheme) {
|
|
2686
|
-
const current = this.cfg.skin.figure ?? COO;
|
|
2687
|
-
const figure = hasFigure ? patch.figure : current;
|
|
2688
|
-
const pack = packs.find((p) => p.id === figure);
|
|
2689
|
-
if (!pack) throw new Error(t.packs.badId(JSON.stringify(figure) ?? ''));
|
|
2690
|
-
const { axes, presets } = pack.manifest;
|
|
2691
|
-
let scheme: string | undefined;
|
|
2692
|
-
if (hasScheme) {
|
|
2693
|
-
const want = patch.scheme;
|
|
2694
|
-
if (typeof want !== 'string') throw new Error(t.packs.badPreset(JSON.stringify(want) ?? ''));
|
|
2695
|
-
const parts = want === '' ? [] : want.split('-');
|
|
2696
|
-
const picksAxes = axes.length === parts.length && axes.every((a, i) => a.options.some((o) => o.id === parts[i]));
|
|
2697
|
-
if (!picksAxes && !presets.some((p) => p.id === want)) throw new Error(t.packs.badPreset(JSON.stringify(want)));
|
|
2698
|
-
// Preserve a matching preset's styling, rather than saving only its equivalent axis combination.
|
|
2699
|
-
if (picksAxes && pack.id !== COO) {
|
|
2700
|
-
for (const p of presets) {
|
|
2701
|
-
if (!axes.every((a, i) => p.pick[a.id] === parts[i])) continue;
|
|
2702
|
-
scheme ??= p.id;
|
|
2703
|
-
if (figure === current && p.id === this.cfg.skin.scheme) { scheme = p.id; break; }
|
|
2704
|
-
}
|
|
2705
|
-
}
|
|
2706
|
-
scheme ??= want;
|
|
2707
|
-
} else if (pack.id !== current && pack.id !== COO) {
|
|
2708
|
-
scheme = presets[0]?.id ?? axes.map((a) => a.options[0]!.id).join('-');
|
|
2709
|
-
}
|
|
2710
|
-
this.saveSkin({ ...this.cfg.skin, figure: pack.id, ...(scheme === undefined ? {} : lookPatch(pack, scheme)) }, scale);
|
|
2711
|
-
} else if (scale !== null) {
|
|
2712
|
-
this.savePrefs({ scale });
|
|
2713
|
-
}
|
|
2714
|
-
return this.dressState(language, packs);
|
|
2715
|
-
}
|
|
2716
|
-
|
|
2717
|
-
private requireDressImport(language: Language): DressZipImport {
|
|
2718
|
-
if (!this.dressImport) throw new Error(petText(language).importing.expired);
|
|
2719
|
-
return this.dressImport;
|
|
2720
|
-
}
|
|
2721
|
-
|
|
2722
2643
|
private async invoke(panel: string, method: string, args: unknown[], language: Language = this.appLanguage): Promise<unknown> {
|
|
2723
2644
|
if (panel === 'pet') {
|
|
2724
2645
|
switch (method) {
|
|
2725
2646
|
case 'state': return this.petState();
|
|
2726
|
-
case '
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2647
|
+
case 'openDress': {
|
|
2648
|
+
this.openWindow();
|
|
2649
|
+
const window = this.windowHost;
|
|
2650
|
+
const reason = window?.state().reason;
|
|
2651
|
+
if (reason) throw new Error(describeHostReason(petText(language).windowReason, reason));
|
|
2652
|
+
if (!window) throw new Error(petText(language).panel.notOpen);
|
|
2653
|
+
await window.openDress();
|
|
2654
|
+
return this.petState();
|
|
2655
|
+
}
|
|
2735
2656
|
case 'openWindow': this.openWindow(); return this.petState();
|
|
2736
2657
|
case 'closeWindow': await this.windowHost?.stop(); return this.petState();
|
|
2737
2658
|
// hides the window as the pet's menu does; it stays connected and comes back with openWindow
|
package/src/webui/dress-panel.ts
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import type { WebUiPanelContext } from 'cortico/web/shared/client-panel.ts';
|
|
2
|
-
import { showDressDialog, type DressDialogView } from './dress-dialog.ts';
|
|
3
|
-
|
|
4
|
-
interface Pack { id: string; name: string; axes: Array<{ id: string; label: string; options: Array<{ id: string; label: string }> }> }
|
|
5
|
-
interface DressState { skin: { figure?: string; scheme?: string }; scale: number; packs: Pack[]; importable: boolean }
|
|
6
|
-
interface ImportPreview { token: string; packs: Array<{ dir: string; id: string; name: Record<string, string>; installed: string | null }>; problems: Array<{ dir: string; reason: string }> }
|
|
7
|
-
|
|
8
|
-
/** Settings never execute figure code: the desktop pet renders the selected figure. */
|
|
9
|
-
export function mountDress(ctx: WebUiPanelContext, body: HTMLElement): void {
|
|
10
|
-
const { ui, signal } = ctx;
|
|
11
|
-
const doc = body.ownerDocument;
|
|
12
|
-
const zh = ctx.language === 'zh' || ctx.language === 'zh-Hant';
|
|
13
|
-
const text = zh ? {
|
|
14
|
-
figure: '形象', size: '大小', save: '保存打扮', saving: '正在保存…', saved: '打扮已保存。', loading: '正在加载打扮…',
|
|
15
|
-
empty: '没有可用的形象。', import: '导入形象 ZIP', uploading: '正在上传', inspecting: '正在检查形象包…',
|
|
16
|
-
preview: '选择要导入的形象', previewHint: '选中要安装的形象包。已有的形象需要确认后才会替换。',
|
|
17
|
-
installed: '已安装', cancel: '取消', install: '安装所选', conflict: '替换已有形象?',
|
|
18
|
-
conflictHint: '以下形象已存在。确认后将替换它们。', replace: '替换并安装', done: '已导入',
|
|
19
|
-
unavailable: '当前无法导入形象。', error: '操作失败', retry: '重试', sizeHint: '调整桌面上的显示大小',
|
|
20
|
-
} : {
|
|
21
|
-
figure: 'Figure', size: 'Size', save: 'Save outfit', saving: 'Saving…', saved: 'Outfit saved.', loading: 'Loading outfits…',
|
|
22
|
-
empty: 'No figures are available.', import: 'Import figure ZIP', uploading: 'Uploading', inspecting: 'Checking figure packs…',
|
|
23
|
-
preview: 'Choose figures to import', previewHint: 'Select the figure packs to install. Existing figures require confirmation before replacement.',
|
|
24
|
-
installed: 'Installed', cancel: 'Cancel', install: 'Install selected', conflict: 'Replace existing figures?',
|
|
25
|
-
conflictHint: 'These figures already exist. Confirm to replace them.', replace: 'Replace and install', done: 'Imported',
|
|
26
|
-
unavailable: 'Figure import is unavailable.', error: 'Could not complete the action', retry: 'Try again', sizeHint: 'Adjust the size on your desktop',
|
|
27
|
-
};
|
|
28
|
-
const feedback = ui.h('div', 'pet-dress-feedback', text.loading);
|
|
29
|
-
feedback.setAttribute('role', 'status'); feedback.setAttribute('aria-live', 'polite');
|
|
30
|
-
const controls = ui.h('div', 'pet-dress-controls');
|
|
31
|
-
const actions = ui.h('div', 'pet-dress-actions');
|
|
32
|
-
const save = ui.button(text.save, { size: 'sm', variant: 'primary' });
|
|
33
|
-
const importButton = ui.button(text.import, { size: 'sm' });
|
|
34
|
-
const retry = ui.button(text.retry, { size: 'sm' });
|
|
35
|
-
const file = doc.createElement('input'); file.type = 'file'; file.accept = '.zip,application/zip'; file.hidden = true;
|
|
36
|
-
actions.append(save, importButton, retry, file); body.append(feedback, controls, actions);
|
|
37
|
-
let state: DressState | null = null;
|
|
38
|
-
let busy = false;
|
|
39
|
-
let uploadToken: string | null = null;
|
|
40
|
-
let figure: HTMLSelectElement;
|
|
41
|
-
let size: HTMLInputElement;
|
|
42
|
-
let axes: HTMLSelectElement[] = [];
|
|
43
|
-
const message = (value: string, error = false) => { feedback.textContent = value; feedback.classList.toggle('bad', error); };
|
|
44
|
-
const setBusy = (value: boolean) => {
|
|
45
|
-
busy = value;
|
|
46
|
-
controls.querySelectorAll<HTMLInputElement | HTMLSelectElement>('input, select').forEach(el => { el.disabled = value; });
|
|
47
|
-
save.disabled = value || !state || !state.packs.length;
|
|
48
|
-
importButton.disabled = value || !state?.importable;
|
|
49
|
-
importButton.title = state && !state.importable ? text.unavailable : '';
|
|
50
|
-
retry.hidden = value || !!state;
|
|
51
|
-
};
|
|
52
|
-
const label = (name: string, input: HTMLElement, hint?: string) => {
|
|
53
|
-
const row = ui.h('label', 'pet-dress-field');
|
|
54
|
-
const caption = ui.h('span', 'pet-dress-caption', name);
|
|
55
|
-
if (hint) caption.append(ui.h('small', '', hint));
|
|
56
|
-
row.append(caption, input); return row;
|
|
57
|
-
};
|
|
58
|
-
const option = (select: HTMLSelectElement, value: string, name: string) => {
|
|
59
|
-
const el = doc.createElement('option'); el.value = value; el.textContent = name; select.append(el);
|
|
60
|
-
};
|
|
61
|
-
const drawAxes = (pack: Pack | undefined, scheme = '') => {
|
|
62
|
-
controls.querySelector('.pet-dress-axes')?.remove();
|
|
63
|
-
const group = ui.h('div', 'pet-dress-axes'); axes = [];
|
|
64
|
-
const picks = scheme.split('-');
|
|
65
|
-
pack?.axes.forEach((axis, index) => {
|
|
66
|
-
const select = doc.createElement('select');
|
|
67
|
-
axis.options.forEach(item => option(select, item.id, item.label));
|
|
68
|
-
if (axis.options.some(item => item.id === picks[index])) select.value = picks[index];
|
|
69
|
-
axes.push(select); group.append(label(axis.label, select));
|
|
70
|
-
});
|
|
71
|
-
controls.append(group);
|
|
72
|
-
};
|
|
73
|
-
const render = (next: DressState) => {
|
|
74
|
-
state = next; controls.replaceChildren();
|
|
75
|
-
figure = doc.createElement('select');
|
|
76
|
-
next.packs.forEach(pack => option(figure, pack.id, pack.name));
|
|
77
|
-
if (next.skin.figure && next.packs.some(pack => pack.id === next.skin.figure)) figure.value = next.skin.figure;
|
|
78
|
-
controls.append(label(text.figure, figure));
|
|
79
|
-
size = doc.createElement('input'); size.type = 'number'; size.min = '50'; size.max = '1000'; size.step = '5'; size.value = String(Math.round(next.scale * 100));
|
|
80
|
-
const sizeWrap = ui.h('span', 'pet-dress-size'); sizeWrap.append(size, ui.h('span', 'pet-unit', '%'));
|
|
81
|
-
controls.append(label(text.size, sizeWrap, text.sizeHint));
|
|
82
|
-
drawAxes(next.packs.find(pack => pack.id === figure.value), next.skin.scheme);
|
|
83
|
-
figure.addEventListener('change', () => drawAxes(next.packs.find(pack => pack.id === figure.value)));
|
|
84
|
-
setBusy(false);
|
|
85
|
-
};
|
|
86
|
-
const load = async () => {
|
|
87
|
-
setBusy(true); message(text.loading);
|
|
88
|
-
try { const next = await ctx.invoke<DressState>('dressState'); if (!signal.aborted) { render(next); message(next.packs.length ? '' : text.empty); } }
|
|
89
|
-
catch (err) { if (!signal.aborted) { setBusy(false); message(`${text.error}: ${String(err)}`, true); } }
|
|
90
|
-
};
|
|
91
|
-
retry.addEventListener('click', () => void load());
|
|
92
|
-
save.addEventListener('click', () => void (async () => {
|
|
93
|
-
if (!state || busy || !size.reportValidity()) return;
|
|
94
|
-
const patch: { figure: string; scheme?: string; scale: number } = { figure: figure.value, scale: Number(size.value) / 100 };
|
|
95
|
-
if (axes.length) patch.scheme = axes.map(axis => axis.value).join('-');
|
|
96
|
-
setBusy(true); message(text.saving);
|
|
97
|
-
try { const next = await ctx.invoke<DressState>('dressSave', [patch]); if (!signal.aborted) { render(next); message(text.saved); } }
|
|
98
|
-
catch (err) { if (!signal.aborted) { setBusy(false); message(`${text.error}: ${String(err)}`, true); } }
|
|
99
|
-
})());
|
|
100
|
-
const dialog = (view: DressDialogView) => {
|
|
101
|
-
const { promise, resolve } = Promise.withResolvers<{ action: string; selected: string[] }>();
|
|
102
|
-
if (signal.aborted) resolve({ action: 'cancel', selected: [] });
|
|
103
|
-
else showDressDialog({ doc, host: doc.body, signal }, view, resolve);
|
|
104
|
-
return promise;
|
|
105
|
-
};
|
|
106
|
-
const cancelUpload = async () => {
|
|
107
|
-
const token = uploadToken; uploadToken = null;
|
|
108
|
-
if (token) await ctx.invoke('dressImportCancel', [token]);
|
|
109
|
-
};
|
|
110
|
-
signal.addEventListener('abort', () => { void cancelUpload().catch(() => {}); }, { once: true });
|
|
111
|
-
importButton.addEventListener('click', () => file.click());
|
|
112
|
-
file.addEventListener('change', () => void (async () => {
|
|
113
|
-
const chosen = file.files?.[0]; file.value = '';
|
|
114
|
-
if (!chosen || busy) return;
|
|
115
|
-
setBusy(true);
|
|
116
|
-
let resultMessage = '';
|
|
117
|
-
let failed = false;
|
|
118
|
-
try {
|
|
119
|
-
message(`${text.uploading} 0%`);
|
|
120
|
-
const token = await ctx.invoke<string>('dressImportStart', [chosen.size]);
|
|
121
|
-
uploadToken = token;
|
|
122
|
-
if (signal.aborted) return;
|
|
123
|
-
const chunkSize = 48 * 1024;
|
|
124
|
-
for (let offset = 0; offset < chosen.size; offset += chunkSize) {
|
|
125
|
-
const bytes = new Uint8Array(await chosen.slice(offset, offset + chunkSize).arrayBuffer());
|
|
126
|
-
if (signal.aborted) return;
|
|
127
|
-
let binary = ''; for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
128
|
-
await ctx.invoke('dressImportChunk', [token, btoa(binary)]);
|
|
129
|
-
if (signal.aborted) return;
|
|
130
|
-
message(`${text.uploading} ${Math.round(Math.min(offset + chunkSize, chosen.size) / chosen.size * 100)}%`);
|
|
131
|
-
}
|
|
132
|
-
message(text.inspecting);
|
|
133
|
-
const preview = await ctx.invoke<ImportPreview>('dressImportFinish', [token]);
|
|
134
|
-
uploadToken = preview.token;
|
|
135
|
-
if (signal.aborted) return;
|
|
136
|
-
const answer = await dialog({ title: text.preview, body: text.previewHint, selectable: true,
|
|
137
|
-
items: preview.packs.map(pack => ({ key: pack.dir, label: pack.name[ctx.language ?? 'zh'] || pack.name[zh ? 'zh' : 'en'] || pack.name.en || pack.id, meta: [pack.id], ...(pack.installed ? { notice: `${text.installed}: ${pack.installed}` } : {}) })),
|
|
138
|
-
warnings: preview.problems.map(problem => `${problem.dir}: ${problem.reason}`),
|
|
139
|
-
actions: [{ key: 'cancel', label: text.cancel }, ...(preview.packs.length ? [{ key: 'install', label: text.install, kind: 'primary' as const, selection: true }] : [])] });
|
|
140
|
-
if (answer.action !== 'install' || signal.aborted) return;
|
|
141
|
-
let installed = await ctx.invoke<{ installed?: string[]; conflicts?: string[] }>('dressImportInstall', [preview.token, answer.selected, []]);
|
|
142
|
-
if (signal.aborted) return;
|
|
143
|
-
if (installed.conflicts?.length) {
|
|
144
|
-
const confirm = await dialog({ title: text.conflict, body: text.conflictHint, items: installed.conflicts.map(id => ({ key: id, label: id, meta: [] })), actions: [{ key: 'cancel', label: text.cancel }, { key: 'replace', label: text.replace, kind: 'danger' }] });
|
|
145
|
-
if (confirm.action !== 'replace' || signal.aborted) return;
|
|
146
|
-
installed = await ctx.invoke<{ installed?: string[]; conflicts?: string[] }>('dressImportInstall', [preview.token, answer.selected, installed.conflicts]);
|
|
147
|
-
if (signal.aborted) return;
|
|
148
|
-
if (installed.conflicts?.length) throw new Error(`${text.conflict}: ${installed.conflicts.join(', ')}`);
|
|
149
|
-
}
|
|
150
|
-
resultMessage = `${text.done}: ${(installed.installed ?? []).join(', ')}`;
|
|
151
|
-
const next = await ctx.invoke<DressState>('dressState');
|
|
152
|
-
if (!signal.aborted) render(next);
|
|
153
|
-
} catch (err) { failed = true; resultMessage = `${text.error}: ${String(err)}`; }
|
|
154
|
-
finally {
|
|
155
|
-
try { await cancelUpload(); } catch (err) { failed = true; resultMessage = `${text.error}: ${String(err)}`; }
|
|
156
|
-
if (!signal.aborted) { setBusy(false); message(resultMessage, failed); }
|
|
157
|
-
}
|
|
158
|
-
})());
|
|
159
|
-
void load();
|
|
160
|
-
}
|