@linxin666/dsh-pet 0.4.5 → 0.4.7
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/README.i18n.yaml +2 -2
- package/README.md +30 -3
- package/README.zh.md +30 -3
- package/lib/client.js +178 -62
- package/lib/client.js.map +1 -1
- package/lib/index.js +734 -26
- package/lib/live2d-vendor.js +129 -95
- package/lib/live2d-vendor.js.map +1 -1
- package/lib/types/announce.d.ts +16 -0
- package/lib/types/announce.d.ts.map +1 -1
- package/lib/types/announce.js +16 -0
- package/lib/types/client/PetDockEntry.d.ts +6 -0
- package/lib/types/client/PetDockEntry.d.ts.map +1 -1
- package/lib/types/client/PetDockEntry.js +1 -1
- package/lib/types/client/PetSettingsCard.d.ts +4 -0
- package/lib/types/client/PetSettingsCard.d.ts.map +1 -1
- package/lib/types/client/PetSettingsCard.js +16 -1
- package/lib/types/client/PetSprite.d.ts +7 -0
- package/lib/types/client/PetSprite.d.ts.map +1 -1
- package/lib/types/client/PetSprite.js +23 -13
- package/lib/types/client/index.d.ts.map +1 -1
- package/lib/types/client/index.js +43 -11
- package/lib/types/client/locales.d.ts +4 -0
- package/lib/types/client/locales.d.ts.map +1 -1
- package/lib/types/client/locales.js +4 -0
- package/lib/types/client/renderers/frames2d.d.ts +5 -3
- package/lib/types/client/renderers/frames2d.d.ts.map +1 -1
- package/lib/types/client/renderers/frames2d.js +139 -33
- package/lib/types/index.d.ts +14 -3
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/index.js +15 -2
- package/lib/types/panel-actions.d.ts +96 -0
- package/lib/types/panel-actions.d.ts.map +1 -0
- package/lib/types/panel-actions.js +134 -0
- package/lib/types/routes.d.ts.map +1 -1
- package/lib/types/routes.js +10 -0
- package/lib/types/service.d.ts +137 -9
- package/lib/types/service.d.ts.map +1 -1
- package/lib/types/service.js +151 -19
- package/package.json +26 -30
- package/src/announce.ts +17 -0
- package/src/client/PetDockEntry.test.tsx +1 -0
- package/src/client/PetDockEntry.tsx +7 -0
- package/src/client/PetSettingsCard.test.tsx +88 -0
- package/src/client/PetSettingsCard.tsx +31 -0
- package/src/client/PetSprite.test.tsx +197 -0
- package/src/client/PetSprite.tsx +56 -12
- package/src/client/index.test.tsx +74 -4
- package/src/client/index.ts +43 -11
- package/src/client/locales.ts +4 -0
- package/src/client/renderers/frames2d.test.ts +103 -1
- package/src/client/renderers/frames2d.ts +143 -30
- package/src/index.test.ts +22 -0
- package/src/index.ts +28 -2
- package/src/panel-actions.test.ts +130 -0
- package/src/panel-actions.ts +182 -0
- package/src/registry.test.ts +3 -2
- package/src/routes.ts +9 -0
- package/src/service.test.ts +128 -0
- package/src/service.ts +206 -22
- package/lib/invariant.js +0 -34
- package/lib/state-NvDEoCln.js +0 -455
- package/lib/types/invariant.d.ts +0 -10
- package/lib/types/invariant.d.ts.map +0 -1
- package/lib/types/invariant.js +0 -27
- package/src/invariant.ts +0 -40
|
@@ -14,9 +14,11 @@
|
|
|
14
14
|
* - Canvas bitmap buffer (default where createImageBitmap/fetch/2D context
|
|
15
15
|
* exist): frames are decoded once into an ImageBitmap on demand, with a
|
|
16
16
|
* bounded look-ahead window over the playing track, and drawn onto one
|
|
17
|
-
* <canvas
|
|
18
|
-
*
|
|
19
|
-
*
|
|
17
|
+
* <canvas>. Steady-state playback issues zero DOM mutations and re-decodes
|
|
18
|
+
* only what the retention budget evicted: decoded frames are held under
|
|
19
|
+
* MAX_RETAINED_BYTES, so a long-lived pet that plays every track of every
|
|
20
|
+
* skin cannot pin every bitmap for the life of the page (measured hotspot:
|
|
21
|
+
* swapping <img>.src per frame drove image decode + invalidation every tick).
|
|
20
22
|
* - Classic <img> fallback (jsdom/tests or missing APIs): identical to the
|
|
21
23
|
* historical behavior - cache-warm Image elements plus guarded src swaps,
|
|
22
24
|
* so environments without modern decoding keep working unchanged.
|
|
@@ -26,6 +28,19 @@
|
|
|
26
28
|
import { PET_RENDERER_API_VERSION } from "../../contracts/renderer.js";
|
|
27
29
|
/** Stall watchdog period: a looping track stuck longer re-kicks its chain. */
|
|
28
30
|
const WATCHDOG_MS = 1200;
|
|
31
|
+
/**
|
|
32
|
+
* Retention budget for decoded frame bitmaps, in bytes.
|
|
33
|
+
*
|
|
34
|
+
* The budget is expressed in bytes rather than in frames because a frame's
|
|
35
|
+
* decoded footprint is `width * height * 4` and published pets differ by more
|
|
36
|
+
* than an order of magnitude (a 512x683 frame is 1.33 MB, a 128x128 frame is
|
|
37
|
+
* 64 KB): a frame count would be either useless for small atlases or too tight
|
|
38
|
+
* for large ones. 128 MiB holds this repository's longest shipped track (78
|
|
39
|
+
* frames, 104 MB) in full, so a running loop still re-decodes nothing, while
|
|
40
|
+
* capping a pet that played every track of every skin at roughly a tenth of
|
|
41
|
+
* the ~1.7 GB the unbounded memo reached.
|
|
42
|
+
*/
|
|
43
|
+
const MAX_RETAINED_BYTES = 128 * 1024 * 1024;
|
|
29
44
|
function isRecord(value) {
|
|
30
45
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
31
46
|
}
|
|
@@ -108,6 +123,22 @@ function validateFrames2dConfig(config) {
|
|
|
108
123
|
}
|
|
109
124
|
return { tracks, phases: phases, ...(skins === undefined ? {} : { skins }) };
|
|
110
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Release one decoded frame. `close()` is browser-only and idempotent, so the
|
|
128
|
+
* call is guarded for exotic hosts and for frames that were already released;
|
|
129
|
+
* the `<img>` fallback has no close, where dropping the reference is the
|
|
130
|
+
* release.
|
|
131
|
+
*/
|
|
132
|
+
function releaseFrame(frame) {
|
|
133
|
+
if (frame === undefined)
|
|
134
|
+
return;
|
|
135
|
+
try {
|
|
136
|
+
const maybeClose = frame.source.close;
|
|
137
|
+
if (typeof maybeClose === 'function')
|
|
138
|
+
maybeClose.call(frame.source);
|
|
139
|
+
}
|
|
140
|
+
catch { /* already released */ }
|
|
141
|
+
}
|
|
111
142
|
export const frames2dRenderer = {
|
|
112
143
|
id: 'frames2d',
|
|
113
144
|
apiVersion: PET_RENDERER_API_VERSION,
|
|
@@ -155,11 +186,18 @@ export const frames2dRenderer = {
|
|
|
155
186
|
img.style.pointerEvents = 'none';
|
|
156
187
|
ctx.container.appendChild(img);
|
|
157
188
|
}
|
|
158
|
-
// Decode cache: one concurrent decode per URL,
|
|
159
|
-
//
|
|
160
|
-
//
|
|
189
|
+
// Decode cache: one concurrent decode per URL, retained under a decoded
|
|
190
|
+
// byte budget. The historical map memoized every decoded frame forever, so
|
|
191
|
+
// a pet that had played all of its tracks (every skin) kept every bitmap
|
|
192
|
+
// alive for the life of the page: a shipped three-skin pet measures 1276
|
|
193
|
+
// frames at 512x683 RGBA, about 1.7 GB, growing monotonically. Entries now
|
|
194
|
+
// carry their decoded footprint, sit in Map insertion order (delete + set
|
|
195
|
+
// is an LRU touch) and the least recently used ones outside the playing
|
|
196
|
+
// window are closed once MAX_RETAINED_BYTES is crossed. A failed decode
|
|
197
|
+
// still resolves undefined and leaves the memo, so a transient fetch error
|
|
198
|
+
// retries instead of sticking.
|
|
161
199
|
const decoding = new Map();
|
|
162
|
-
|
|
200
|
+
let retainedBytes = 0;
|
|
163
201
|
// All frame fetches funnel through a small pool. Full-library warm passes
|
|
164
202
|
// on large pets fire 1100+ requests at once, which trips the browser's
|
|
165
203
|
// in-flight request limit (net::ERR_INSUFFICIENT_RESOURCES) and fails
|
|
@@ -210,27 +248,96 @@ export const frames2dRenderer = {
|
|
|
210
248
|
frameQueue.unshift(frameQueue.splice(index, 1)[0]);
|
|
211
249
|
}
|
|
212
250
|
const cached = decoding.get(url);
|
|
213
|
-
if (cached !== undefined)
|
|
214
|
-
|
|
251
|
+
if (cached !== undefined) {
|
|
252
|
+
// LRU touch: a hit means playback just asked for this frame, so it must
|
|
253
|
+
// not be the next victim. Re-inserting the same entry only reorders.
|
|
254
|
+
decoding.delete(url);
|
|
255
|
+
decoding.set(url, cached);
|
|
256
|
+
return cached.job;
|
|
257
|
+
}
|
|
215
258
|
let release;
|
|
216
259
|
const gate = new Promise((resolve) => { release = resolve; });
|
|
217
260
|
const job = gate.then(() => (disposed ? undefined : decodeFrame(url)));
|
|
218
|
-
|
|
219
|
-
|
|
261
|
+
const entry = { job };
|
|
262
|
+
job.then((frame) => {
|
|
263
|
+
if (frame === undefined) {
|
|
264
|
+
decoding.delete(url);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
// A decode that lands after dispose is closed here: nothing can close
|
|
268
|
+
// it later, and the map was already drained.
|
|
269
|
+
if (disposed) {
|
|
270
|
+
releaseFrame(frame);
|
|
271
|
+
decoding.delete(url);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
entry.frame = frame;
|
|
275
|
+
entry.bytes = frame.width * frame.height * 4;
|
|
276
|
+
retainedBytes += entry.bytes;
|
|
277
|
+
// Only a resolved frame is evictable, and its decoded size is known
|
|
278
|
+
// only now, so the budget is enforced here.
|
|
279
|
+
enforceRetentionBudget();
|
|
280
|
+
}, () => decoding.delete(url));
|
|
220
281
|
void job.finally(() => {
|
|
221
282
|
activeFrames -= 1;
|
|
222
283
|
pumpFrames();
|
|
223
284
|
});
|
|
224
|
-
decoding.set(url,
|
|
225
|
-
|
|
226
|
-
const entry = { url, release };
|
|
285
|
+
decoding.set(url, entry);
|
|
286
|
+
const queued = { url, release };
|
|
227
287
|
if (jump)
|
|
228
|
-
frameQueue.unshift(
|
|
288
|
+
frameQueue.unshift(queued);
|
|
229
289
|
else
|
|
230
|
-
frameQueue.push(
|
|
290
|
+
frameQueue.push(queued);
|
|
231
291
|
pumpFrames();
|
|
232
292
|
return job;
|
|
233
293
|
};
|
|
294
|
+
/**
|
|
295
|
+
* Release decoded bitmaps until the retained footprint is back under
|
|
296
|
+
* MAX_RETAINED_BYTES. Two properties keep this from breaking playback:
|
|
297
|
+
*
|
|
298
|
+
* - The playing track's look-ahead window, plus the frame a draw is waiting
|
|
299
|
+
* on, are never released. Releasing one would hand a closed bitmap to
|
|
300
|
+
* `drawImage`, and re-decoding it would stall the next tick.
|
|
301
|
+
* - Frames of any other track go first. That is what keeps a running loop
|
|
302
|
+
* warm under a budget smaller than two whole tracks: eviction reclaims the
|
|
303
|
+
* track the pet left instead of eating the cycle it is playing.
|
|
304
|
+
*
|
|
305
|
+
* When the protected window alone exceeds the budget nothing is released —
|
|
306
|
+
* a pet whose frames are that large cannot be bounded further without
|
|
307
|
+
* breaking playback, and the overshoot is then exactly one window.
|
|
308
|
+
*/
|
|
309
|
+
function enforceRetentionBudget() {
|
|
310
|
+
if (retainedBytes <= MAX_RETAINED_BYTES)
|
|
311
|
+
return;
|
|
312
|
+
const def = config.tracks[track];
|
|
313
|
+
const playing = new Set(def === undefined ? [] : def.frames);
|
|
314
|
+
const protectedUrls = new Set();
|
|
315
|
+
if (pinned !== undefined)
|
|
316
|
+
protectedUrls.add(pinned);
|
|
317
|
+
if (def !== undefined) {
|
|
318
|
+
const end = Math.min(def.frames.length, frameIndex + 1 + PREFETCH_AHEAD);
|
|
319
|
+
for (let index = frameIndex; index < end; index += 1) {
|
|
320
|
+
const url = def.frames[index];
|
|
321
|
+
if (url !== undefined)
|
|
322
|
+
protectedUrls.add(url);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
// Pass 1 reclaims frames of tracks the pet is not playing; pass 2 is the
|
|
326
|
+
// backstop for a single track larger than the whole budget.
|
|
327
|
+
for (const foreignOnly of [true, false]) {
|
|
328
|
+
for (const [url, entry] of decoding) {
|
|
329
|
+
if (retainedBytes <= MAX_RETAINED_BYTES)
|
|
330
|
+
return;
|
|
331
|
+
if (entry.bytes === undefined || protectedUrls.has(url))
|
|
332
|
+
continue;
|
|
333
|
+
if (foreignOnly && playing.has(url))
|
|
334
|
+
continue;
|
|
335
|
+
releaseFrame(entry.frame);
|
|
336
|
+
retainedBytes -= entry.bytes;
|
|
337
|
+
decoding.delete(url);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
234
341
|
/**
|
|
235
342
|
* Bounded look-ahead window: decode only the frames playback is about to
|
|
236
343
|
* need. The historical warm pass decoded every frame of every track up
|
|
@@ -265,6 +372,9 @@ export const frames2dRenderer = {
|
|
|
265
372
|
let baseIdle = config.phases.idle;
|
|
266
373
|
let drawToken = 0;
|
|
267
374
|
let lastDrawnUrl;
|
|
375
|
+
// The frame a draw is currently awaiting. Eviction must not release it:
|
|
376
|
+
// the callback below holds the bitmap and would draw a closed one.
|
|
377
|
+
let pinned;
|
|
268
378
|
const trackForPhase = (phase) => {
|
|
269
379
|
const mapped = config.phases[phase];
|
|
270
380
|
if (mapped === undefined)
|
|
@@ -277,7 +387,10 @@ export const frames2dRenderer = {
|
|
|
277
387
|
if (context2d === null || canvas === null)
|
|
278
388
|
return;
|
|
279
389
|
const myToken = ++drawToken;
|
|
390
|
+
pinned = url;
|
|
280
391
|
void loadFrame(url, true).then((frame) => {
|
|
392
|
+
if (pinned === url)
|
|
393
|
+
pinned = undefined;
|
|
281
394
|
if (disposed || frame === undefined || myToken !== drawToken)
|
|
282
395
|
return;
|
|
283
396
|
if (lastDrawnUrl === url)
|
|
@@ -292,7 +405,8 @@ export const frames2dRenderer = {
|
|
|
292
405
|
context2d.clearRect(0, 0, canvas.width, canvas.height);
|
|
293
406
|
}
|
|
294
407
|
context2d.drawImage(frame.source, 0, 0);
|
|
295
|
-
}).catch(() => {
|
|
408
|
+
}).catch(() => { if (pinned === url)
|
|
409
|
+
pinned = undefined; /* keep last frame */ });
|
|
296
410
|
};
|
|
297
411
|
const show = (trackId, index) => {
|
|
298
412
|
const def = config.tracks[trackId];
|
|
@@ -393,24 +507,16 @@ export const frames2dRenderer = {
|
|
|
393
507
|
clearTimeout(timer);
|
|
394
508
|
if (watchdog !== undefined)
|
|
395
509
|
clearInterval(watchdog);
|
|
396
|
-
// Release
|
|
397
|
-
//
|
|
398
|
-
//
|
|
510
|
+
// Release every retained bitmap and drain the pool. Queued-but-unstarted
|
|
511
|
+
// frames resolve as no-ops (the gate skips decodeFrame once disposed), and
|
|
512
|
+
// a decode that lands after this loop is closed by the resolution guard
|
|
513
|
+
// above, so teardown leaves nothing pending and nothing retained.
|
|
399
514
|
for (const queued of frameQueue.splice(0))
|
|
400
515
|
queued.release();
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
const maybeClose = frame?.source?.close;
|
|
406
|
-
if (typeof maybeClose === 'function' && frame !== undefined)
|
|
407
|
-
maybeClose.call(frame.source);
|
|
408
|
-
}
|
|
409
|
-
catch { /* already released */ }
|
|
410
|
-
}).catch(() => { });
|
|
411
|
-
}
|
|
412
|
-
decoding.clear();
|
|
413
|
-
});
|
|
516
|
+
for (const entry of decoding.values())
|
|
517
|
+
releaseFrame(entry.frame);
|
|
518
|
+
decoding.clear();
|
|
519
|
+
retainedBytes = 0;
|
|
414
520
|
canvas?.remove();
|
|
415
521
|
img?.remove();
|
|
416
522
|
};
|
package/lib/types/index.d.ts
CHANGED
|
@@ -12,10 +12,12 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import { Context, type Volatile } from '@deepseek-ai/cordis';
|
|
14
14
|
import z from '@deepseek-ai/schemastery';
|
|
15
|
-
import { type PetConfig, type PetSettingsSection } from './service.ts';
|
|
15
|
+
import { type PetConfig, type PetSettingsSection, type PetStatusBubbleMode } from './service.ts';
|
|
16
16
|
import { type PetDisplayConfig } from './persist.ts';
|
|
17
|
-
export { PetService, MAX_SESSION_BUBBLES } from './service.ts';
|
|
18
|
-
export type { PetConfig, PetInteractResult, PetSettingsSection, PetSessionView, PetStateView, } from './service.ts';
|
|
17
|
+
export { PetService, MAX_SESSION_BUBBLES, DEFAULT_STATUS_BUBBLE_MODE, STATUS_BUBBLE_MODES, statusBubbleMode } from './service.ts';
|
|
18
|
+
export type { PetConfig, PetInteractResult, PetSettingsSection, PetSessionView, PetStateView, PetStatusBubbleMode, } from './service.ts';
|
|
19
|
+
export { MAX_PANEL_ACTIONS, PanelActionRegistry, normalizePanelAction } from './panel-actions.ts';
|
|
20
|
+
export type { PetPanelActionContext, PetPanelActionHandler, PetPanelActionRegistration, PetPanelActionView, } from './panel-actions.ts';
|
|
19
21
|
export { AFFINITY_MAX, AFFINITY_RANKS, applyInteraction, applyTurnReward, emptyAffinity, rankOf, } from './affinity.ts';
|
|
20
22
|
export type { AffinityConfig, AffinityState, InteractionOutcome, PetInteraction, } from './affinity.ts';
|
|
21
23
|
export { animationForPhase, PetStateMachine, rowOf, } from './state.ts';
|
|
@@ -47,6 +49,7 @@ export declare const PET_FORM_DEFAULTS: {
|
|
|
47
49
|
readonly petId: "whale-girl";
|
|
48
50
|
readonly enabled: true;
|
|
49
51
|
readonly decorationEnabled: true;
|
|
52
|
+
readonly statusBubbles: PetStatusBubbleMode;
|
|
50
53
|
};
|
|
51
54
|
/**
|
|
52
55
|
* One settings field as the config carries it. The Host commits an edit into
|
|
@@ -61,6 +64,12 @@ export interface PetFormConfig {
|
|
|
61
64
|
enabled?: LiveField<boolean>;
|
|
62
65
|
/** Status-decoration master switch (pet-center M5, #567); defaults to on. */
|
|
63
66
|
decorationEnabled?: LiveField<boolean>;
|
|
67
|
+
/**
|
|
68
|
+
* Whether the pet renders its own session/status bubbles (issue #6);
|
|
69
|
+
* defaults to 'auto', which is exactly the behavior that shipped before
|
|
70
|
+
* the switch existed.
|
|
71
|
+
*/
|
|
72
|
+
statusBubbles?: LiveField<PetStatusBubbleMode | undefined>;
|
|
64
73
|
/** Master switch for the pet surface. */
|
|
65
74
|
visible?: LiveField<boolean>;
|
|
66
75
|
/** Scale of the rendered pet in px (sprite cell height). */
|
|
@@ -98,6 +107,7 @@ export declare const Config: z<Schemastery.ObjectS<NoInfer<{
|
|
|
98
107
|
petId: z<string, string, "volatile">;
|
|
99
108
|
enabled: z<boolean, boolean, "volatile-defined">;
|
|
100
109
|
decorationEnabled: z<boolean, boolean, "volatile-defined">;
|
|
110
|
+
statusBubbles: z<"auto" | "off", "auto" | "off", "volatile-defined">;
|
|
101
111
|
}>>, Schemastery.ObjectT<NoInfer<{
|
|
102
112
|
visible: z<boolean, boolean, "volatile-defined">;
|
|
103
113
|
size: z<number, number, "volatile-defined">;
|
|
@@ -107,6 +117,7 @@ export declare const Config: z<Schemastery.ObjectS<NoInfer<{
|
|
|
107
117
|
petId: z<string, string, "volatile">;
|
|
108
118
|
enabled: z<boolean, boolean, "volatile-defined">;
|
|
109
119
|
decorationEnabled: z<boolean, boolean, "volatile-defined">;
|
|
120
|
+
statusBubbles: z<"auto" | "off", "auto" | "off", "volatile-defined">;
|
|
110
121
|
}>>, "plain">;
|
|
111
122
|
/**
|
|
112
123
|
* The settings section the pet runs with: the effective values of the row's
|
package/lib/types/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAI5D,OAAO,CAAC,MAAM,0BAA0B,CAAA;AACxC,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAI5D,OAAO,CAAC,MAAM,0BAA0B,CAAA;AACxC,OAAO,EAAyG,KAAK,SAAS,EAAE,KAAK,kBAAkB,EAAE,KAAK,mBAAmB,EAAE,MAAM,cAAc,CAAA;AAGvM,OAAO,EAA6G,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAG/J,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAE,0BAA0B,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AACjI,YAAY,EACV,SAAS,EACT,iBAAiB,EACjB,kBAAkB,EAClB,cAAc,EACd,YAAY,EACZ,mBAAmB,GACpB,MAAM,cAAc,CAAA;AACrB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AO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;aAgBjB,CAAA;AAkDF;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,aAAa,EACrB,aAAa,EAAE,MAAM,EACrB,SAAS,GAAE,OAAO,CAAC,gBAAgB,CAAM,EACzC,SAAS,GAAE,OAAO,CAAC,gBAAgB,CAAM,GACxC,kBAAkB,CAgCpB;AA4CD,0EAA0E;AAC1E,eAAO,MAAM,KAAK,kBAA6C,CAAA;AAE/D,4FAA4F;AAC5F,MAAM,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,EAAE,SAAS,GAAG,mBAAmB,CAAC,GAAG,aAAa,CAAA;AAE9F,iBAAS,SAAS,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,GAAE,eAAoB,GAAG,IAAI,CA+EnE;AAED,OAAO,QAAQ,qBAAqB,CAAC;IACnC,UAAU,MAAM;QACd;;;;WAIG;QACH,wBAAwB,CAAC,KAAK,EAAE,SAAS,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,GAAG,IAAI,CAAA;KACtE;CACF"}
|
package/lib/types/index.js
CHANGED
|
@@ -11,12 +11,13 @@
|
|
|
11
11
|
* @module @linxin666/dsh-pet
|
|
12
12
|
*/
|
|
13
13
|
import z from '@deepseek-ai/schemastery';
|
|
14
|
-
import { PET_SETTINGS_NAMESPACE, PetService } from "./service.js";
|
|
14
|
+
import { DEFAULT_STATUS_BUBBLE_MODE, PET_SETTINGS_NAMESPACE, PetService, STATUS_BUBBLE_MODES, statusBubbleMode } from "./service.js";
|
|
15
15
|
import { makePetRoutes } from "./routes.js";
|
|
16
16
|
import { loadPetRegistry, petPackageRoot } from "./registry.js";
|
|
17
17
|
import { BUBBLE_SCALE_MAX, BUBBLE_SCALE_MIN, DEFAULT_PET_ID, DISPLAY_INSET_MAX, DISPLAY_SIZE_MAX, DISPLAY_SIZE_MIN } from "./persist.js";
|
|
18
18
|
import { mountOnce } from "./mount-once.js";
|
|
19
|
-
export { PetService, MAX_SESSION_BUBBLES } from "./service.js";
|
|
19
|
+
export { PetService, MAX_SESSION_BUBBLES, DEFAULT_STATUS_BUBBLE_MODE, STATUS_BUBBLE_MODES, statusBubbleMode } from "./service.js";
|
|
20
|
+
export { MAX_PANEL_ACTIONS, PanelActionRegistry, normalizePanelAction } from "./panel-actions.js";
|
|
20
21
|
export { AFFINITY_MAX, AFFINITY_RANKS, applyInteraction, applyTurnReward, emptyAffinity, rankOf, } from "./affinity.js";
|
|
21
22
|
export { animationForPhase, PetStateMachine, rowOf, } from "./state.js";
|
|
22
23
|
export { consumeTreat, defaultTreatConfig, emptyTreatLedger, settleTreatGrants, } from "./treats.js";
|
|
@@ -42,6 +43,7 @@ export const PET_FORM_DEFAULTS = {
|
|
|
42
43
|
petId: DEFAULT_PET_ID,
|
|
43
44
|
enabled: true,
|
|
44
45
|
decorationEnabled: true,
|
|
46
|
+
statusBubbles: DEFAULT_STATUS_BUBBLE_MODE,
|
|
45
47
|
};
|
|
46
48
|
/**
|
|
47
49
|
* Plugin configuration. Under the 0.1.7 settings model a plugin's own Cordis
|
|
@@ -69,6 +71,11 @@ export const Config = z.object({
|
|
|
69
71
|
petId: z.string().volatile(),
|
|
70
72
|
enabled: z.boolean().default(PET_FORM_DEFAULTS.enabled).volatile(),
|
|
71
73
|
decorationEnabled: z.boolean().default(PET_FORM_DEFAULTS.decorationEnabled).volatile(),
|
|
74
|
+
// The pet's own session/status bubbles (issue #6). 'auto' is the shipped
|
|
75
|
+
// behavior; 'off' is the opt-in a user (or a plugin that drives its own
|
|
76
|
+
// bubble surface) needs, and it hides ONLY those bubbles — the sprite, the
|
|
77
|
+
// interaction feedback and a sibling plugin's announcement bubble stay.
|
|
78
|
+
statusBubbles: z.union([...STATUS_BUBBLE_MODES]).default(PET_FORM_DEFAULTS.statusBubbles).volatile(),
|
|
72
79
|
});
|
|
73
80
|
/**
|
|
74
81
|
* Read one live config field.
|
|
@@ -153,6 +160,11 @@ export function petSettingsSection(config, fallbackPetId, persisted = {}, commit
|
|
|
153
160
|
petId: readLive(config.petId, fallbackPetId),
|
|
154
161
|
enabled,
|
|
155
162
|
decorationEnabled: readLive(config.decorationEnabled, PET_FORM_DEFAULTS.decorationEnabled),
|
|
163
|
+
// Not a display field: the bubble switch has no persisted pet.json twin
|
|
164
|
+
// (it is a plugin-level rendering preference, like `decorationEnabled`),
|
|
165
|
+
// so it reads the live value directly and normalizes anything unknown to
|
|
166
|
+
// the shipped default instead of trusting the document.
|
|
167
|
+
statusBubbles: statusBubbleMode(readLive(config.statusBubbles, undefined)),
|
|
156
168
|
};
|
|
157
169
|
}
|
|
158
170
|
/** The settings row ids a pet install is served under, most specific first. */
|
|
@@ -197,6 +209,7 @@ function applyImpl(ctx, config = {}) {
|
|
|
197
209
|
...config,
|
|
198
210
|
enabled: readLive(config.enabled, PET_FORM_DEFAULTS.enabled),
|
|
199
211
|
decorationEnabled: readLive(config.decorationEnabled, PET_FORM_DEFAULTS.decorationEnabled),
|
|
212
|
+
statusBubbles: statusBubbleMode(readLive(config.statusBubbles, undefined)),
|
|
200
213
|
registry,
|
|
201
214
|
});
|
|
202
215
|
// The mirror back into the settings document (see syncSettingsFromPet) has to
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hover-panel action contract — the extension point a sibling plugin uses to
|
|
3
|
+
* add its own button to the pet's floating hover panel (issue #6).
|
|
4
|
+
*
|
|
5
|
+
* A plugin registers one action in process through the pet service
|
|
6
|
+
* (`ctx.pet.registerPanelAction({ id, label, onSelect })`); the host validates
|
|
7
|
+
* it into a bounded view, serves the registered actions in the state snapshot
|
|
8
|
+
* (`PetStateView.panelActions`), and the browser half renders one button per
|
|
9
|
+
* entry at the end of the panel's action row. A click travels back through
|
|
10
|
+
* `POST /api/pet/panel-action`, which dispatches to the registering plugin's
|
|
11
|
+
* own callback — the pet owns the row, the plugin owns what the button does.
|
|
12
|
+
*
|
|
13
|
+
* The validation and the registry live in this pure module so the contract has
|
|
14
|
+
* exactly one home and stays testable without the cordis service.
|
|
15
|
+
* @module @linxin666/dsh-pet/panel-actions
|
|
16
|
+
*/
|
|
17
|
+
/** How many plugin actions one hover panel can carry at most. */
|
|
18
|
+
export declare const MAX_PANEL_ACTIONS = 8;
|
|
19
|
+
/** One registered action as the browser half renders it. */
|
|
20
|
+
export interface PetPanelActionView {
|
|
21
|
+
/** Registration id; the click dispatches back on it. */
|
|
22
|
+
id: string;
|
|
23
|
+
/** Button label. */
|
|
24
|
+
label: string;
|
|
25
|
+
/** Optional tooltip (the label's longer explanation). */
|
|
26
|
+
title?: string;
|
|
27
|
+
/** Sort key within the registered block; ties break on id. */
|
|
28
|
+
order: number;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* What a panel action click knows about the pet at the moment it arrives. The
|
|
32
|
+
* registering plugin may read more off the service (`selectedPetId()`), so this
|
|
33
|
+
* stays a read-only snapshot rather than a live handle.
|
|
34
|
+
*/
|
|
35
|
+
export interface PetPanelActionContext {
|
|
36
|
+
/** The selected pet id when the click arrived. */
|
|
37
|
+
petId: string;
|
|
38
|
+
}
|
|
39
|
+
/** The callback a registration carries; a click invokes it in the host. */
|
|
40
|
+
export type PetPanelActionHandler = (context: PetPanelActionContext) => void | Promise<void>;
|
|
41
|
+
/** One action a sibling plugin registers into the panel's action row. */
|
|
42
|
+
export interface PetPanelActionRegistration {
|
|
43
|
+
/** Unique, stable id (kebab-case); re-registering the same id replaces it. */
|
|
44
|
+
id: string;
|
|
45
|
+
/** Button label (bounded). */
|
|
46
|
+
label: string;
|
|
47
|
+
/** Optional tooltip rendered as the button's `title`. */
|
|
48
|
+
title?: string;
|
|
49
|
+
/** Sort key within the registered block (default 0; ascending, ties on id). */
|
|
50
|
+
order?: number;
|
|
51
|
+
/** Invoked in the host when the browser half dispatches this action. */
|
|
52
|
+
onSelect: PetPanelActionHandler;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Validate one registration. Returns undefined — never throws — when the shape
|
|
56
|
+
* is unusable: the id must match the id grammar and the label must survive
|
|
57
|
+
* truncation, so a click can always be dispatched back to a known row.
|
|
58
|
+
* @param input - the registration a sibling plugin passed to the service.
|
|
59
|
+
* @returns the wire view, or undefined when the registration is malformed.
|
|
60
|
+
*/
|
|
61
|
+
export declare function normalizePanelAction(input: unknown): PetPanelActionView | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* The panel's action registry: one bounded, ordered table of plugin actions.
|
|
64
|
+
*
|
|
65
|
+
* Registration is id-wins (a later registration of the same id replaces the
|
|
66
|
+
* earlier one, and the superseded disposer stops touching the table so a
|
|
67
|
+
* disposing plugin can never remove the action that replaced it). The table is
|
|
68
|
+
* in-memory only — it dies with the pet service fiber — and every entry is
|
|
69
|
+
* reclaimed by the disposer the registering plugin runs with its own fiber.
|
|
70
|
+
*/
|
|
71
|
+
export declare class PanelActionRegistry {
|
|
72
|
+
private readonly actions;
|
|
73
|
+
/**
|
|
74
|
+
* Register one action.
|
|
75
|
+
* @param input - the registration a sibling plugin passed to the service.
|
|
76
|
+
* @returns a disposer removing this registration.
|
|
77
|
+
* @throws when the registration is malformed. Unlike `announce` — a hot path
|
|
78
|
+
* whose payload a caller may build from untrusted data — a registration is
|
|
79
|
+
* built once at the registering plugin's own apply, so a bad id or label is a
|
|
80
|
+
* wiring mistake that should fail where it was written.
|
|
81
|
+
*/
|
|
82
|
+
register(input: unknown): () => void;
|
|
83
|
+
/** The registered actions as the state view serves them, in render order. */
|
|
84
|
+
views(): PetPanelActionView[];
|
|
85
|
+
/**
|
|
86
|
+
* Dispatch a click to the plugin that registered this id.
|
|
87
|
+
* @param id - the id the browser half reported.
|
|
88
|
+
* @param context - what the pet looked like when the click arrived.
|
|
89
|
+
* @returns whether the id was registered. A handler that throws rejects:
|
|
90
|
+
* the caller decides how a sibling plugin's failure is reported.
|
|
91
|
+
*/
|
|
92
|
+
select(id: string, context: PetPanelActionContext): Promise<boolean>;
|
|
93
|
+
/** Drop every registration (service teardown; tests). */
|
|
94
|
+
clear(): void;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=panel-actions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"panel-actions.d.ts","sourceRoot":"","sources":["../../src/panel-actions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,iEAAiE;AACjE,eAAO,MAAM,iBAAiB,IAAI,CAAA;AAoBlC,4DAA4D;AAC5D,MAAM,WAAW,kBAAkB;IACjC,wDAAwD;IACxD,EAAE,EAAE,MAAM,CAAA;IACV,oBAAoB;IACpB,KAAK,EAAE,MAAM,CAAA;IACb,yDAAyD;IACzD,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,8DAA8D;IAC9D,KAAK,EAAE,MAAM,CAAA;CACd;AAED;;;;GAIG;AACH,MAAM,WAAW,qBAAqB;IACpC,kDAAkD;IAClD,KAAK,EAAE,MAAM,CAAA;CACd;AAED,2EAA2E;AAC3E,MAAM,MAAM,qBAAqB,GAAG,CAAC,OAAO,EAAE,qBAAqB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;AAE5F,yEAAyE;AACzE,MAAM,WAAW,0BAA0B;IACzC,8EAA8E;IAC9E,EAAE,EAAE,MAAM,CAAA;IACV,8BAA8B;IAC9B,KAAK,EAAE,MAAM,CAAA;IACb,yDAAyD;IACzD,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,+EAA+E;IAC/E,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,wEAAwE;IACxE,QAAQ,EAAE,qBAAqB,CAAA;CAChC;AAeD;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,kBAAkB,GAAG,SAAS,CAkBnF;AAED;;;;;;;;GAQG;AACH,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAsC;IAE9D;;;;;;;;OAQG;IACH,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,IAAI;IAkBpC,6EAA6E;IAC7E,KAAK,IAAI,kBAAkB,EAAE;IAM7B;;;;;;OAMG;IACG,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,OAAO,CAAC;IAO1E,yDAAyD;IACzD,KAAK,IAAI,IAAI;CAGd"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hover-panel action contract — the extension point a sibling plugin uses to
|
|
3
|
+
* add its own button to the pet's floating hover panel (issue #6).
|
|
4
|
+
*
|
|
5
|
+
* A plugin registers one action in process through the pet service
|
|
6
|
+
* (`ctx.pet.registerPanelAction({ id, label, onSelect })`); the host validates
|
|
7
|
+
* it into a bounded view, serves the registered actions in the state snapshot
|
|
8
|
+
* (`PetStateView.panelActions`), and the browser half renders one button per
|
|
9
|
+
* entry at the end of the panel's action row. A click travels back through
|
|
10
|
+
* `POST /api/pet/panel-action`, which dispatches to the registering plugin's
|
|
11
|
+
* own callback — the pet owns the row, the plugin owns what the button does.
|
|
12
|
+
*
|
|
13
|
+
* The validation and the registry live in this pure module so the contract has
|
|
14
|
+
* exactly one home and stays testable without the cordis service.
|
|
15
|
+
* @module @linxin666/dsh-pet/panel-actions
|
|
16
|
+
*/
|
|
17
|
+
/** How many plugin actions one hover panel can carry at most. */
|
|
18
|
+
export const MAX_PANEL_ACTIONS = 8;
|
|
19
|
+
/**
|
|
20
|
+
* Action id grammar: a lowercase id in the shape pet and decoration ids use
|
|
21
|
+
* (`^` + kebab/dot segments). The id is what a click travels back on, so it
|
|
22
|
+
* has to be stable, addressable and free of separators the wire would treat as
|
|
23
|
+
* structure.
|
|
24
|
+
*/
|
|
25
|
+
const ID_PATTERN = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
|
26
|
+
/**
|
|
27
|
+
* Label bound. The action row is compact and wraps beyond a few characters per
|
|
28
|
+
* button, so a plugin names its action (换装, Quota) rather than a sentence;
|
|
29
|
+
* the optional title carries the longer explanation as a tooltip.
|
|
30
|
+
*/
|
|
31
|
+
const LABEL_MAX = 24;
|
|
32
|
+
/** Title (tooltip) bound; the same reason voice-pack labels are bounded. */
|
|
33
|
+
const TITLE_MAX = 80;
|
|
34
|
+
function boundedString(value, max) {
|
|
35
|
+
if (typeof value !== 'string')
|
|
36
|
+
return undefined;
|
|
37
|
+
const trimmed = value.trim();
|
|
38
|
+
if (trimmed === '')
|
|
39
|
+
return undefined;
|
|
40
|
+
return trimmed.length <= max ? trimmed : trimmed.slice(0, max);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Validate one registration. Returns undefined — never throws — when the shape
|
|
44
|
+
* is unusable: the id must match the id grammar and the label must survive
|
|
45
|
+
* truncation, so a click can always be dispatched back to a known row.
|
|
46
|
+
* @param input - the registration a sibling plugin passed to the service.
|
|
47
|
+
* @returns the wire view, or undefined when the registration is malformed.
|
|
48
|
+
*/
|
|
49
|
+
export function normalizePanelAction(input) {
|
|
50
|
+
if (typeof input !== 'object' || input === null)
|
|
51
|
+
return undefined;
|
|
52
|
+
const data = input;
|
|
53
|
+
// The id is an identity, not copy: an over-long one is refused rather than
|
|
54
|
+
// truncated, because truncating it could silently collapse two plugins'
|
|
55
|
+
// registrations onto one row.
|
|
56
|
+
const id = typeof data.id === 'string' ? data.id.trim() : undefined;
|
|
57
|
+
if (id === undefined || !ID_PATTERN.test(id))
|
|
58
|
+
return undefined;
|
|
59
|
+
const label = boundedString(data.label, LABEL_MAX);
|
|
60
|
+
if (label === undefined)
|
|
61
|
+
return undefined;
|
|
62
|
+
const title = boundedString(data.title, TITLE_MAX);
|
|
63
|
+
const order = typeof data.order === 'number' && Number.isFinite(data.order) ? data.order : 0;
|
|
64
|
+
return {
|
|
65
|
+
id,
|
|
66
|
+
label,
|
|
67
|
+
...(title === undefined ? {} : { title }),
|
|
68
|
+
order,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The panel's action registry: one bounded, ordered table of plugin actions.
|
|
73
|
+
*
|
|
74
|
+
* Registration is id-wins (a later registration of the same id replaces the
|
|
75
|
+
* earlier one, and the superseded disposer stops touching the table so a
|
|
76
|
+
* disposing plugin can never remove the action that replaced it). The table is
|
|
77
|
+
* in-memory only — it dies with the pet service fiber — and every entry is
|
|
78
|
+
* reclaimed by the disposer the registering plugin runs with its own fiber.
|
|
79
|
+
*/
|
|
80
|
+
export class PanelActionRegistry {
|
|
81
|
+
actions = new Map();
|
|
82
|
+
/**
|
|
83
|
+
* Register one action.
|
|
84
|
+
* @param input - the registration a sibling plugin passed to the service.
|
|
85
|
+
* @returns a disposer removing this registration.
|
|
86
|
+
* @throws when the registration is malformed. Unlike `announce` — a hot path
|
|
87
|
+
* whose payload a caller may build from untrusted data — a registration is
|
|
88
|
+
* built once at the registering plugin's own apply, so a bad id or label is a
|
|
89
|
+
* wiring mistake that should fail where it was written.
|
|
90
|
+
*/
|
|
91
|
+
register(input) {
|
|
92
|
+
const registration = (typeof input === 'object' && input !== null ? input : {});
|
|
93
|
+
const view = normalizePanelAction(registration);
|
|
94
|
+
if (view === undefined)
|
|
95
|
+
throw new Error('[dsh-pet] invalid panel action: id and label are required');
|
|
96
|
+
if (typeof registration.onSelect !== 'function') {
|
|
97
|
+
throw new Error('[dsh-pet] invalid panel action: onSelect must be a function');
|
|
98
|
+
}
|
|
99
|
+
if (!this.actions.has(view.id) && this.actions.size >= MAX_PANEL_ACTIONS) {
|
|
100
|
+
throw new Error('[dsh-pet] panel action limit reached: ' + MAX_PANEL_ACTIONS);
|
|
101
|
+
}
|
|
102
|
+
const entry = { view, onSelect: registration.onSelect };
|
|
103
|
+
this.actions.set(view.id, entry);
|
|
104
|
+
return () => {
|
|
105
|
+
// Only the registration that still owns the id may clear it.
|
|
106
|
+
if (this.actions.get(view.id) === entry)
|
|
107
|
+
this.actions.delete(view.id);
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/** The registered actions as the state view serves them, in render order. */
|
|
111
|
+
views() {
|
|
112
|
+
return [...this.actions.values()]
|
|
113
|
+
.map(entry => entry.view)
|
|
114
|
+
.sort((a, b) => a.order - b.order || a.id.localeCompare(b.id));
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Dispatch a click to the plugin that registered this id.
|
|
118
|
+
* @param id - the id the browser half reported.
|
|
119
|
+
* @param context - what the pet looked like when the click arrived.
|
|
120
|
+
* @returns whether the id was registered. A handler that throws rejects:
|
|
121
|
+
* the caller decides how a sibling plugin's failure is reported.
|
|
122
|
+
*/
|
|
123
|
+
async select(id, context) {
|
|
124
|
+
const entry = this.actions.get(id);
|
|
125
|
+
if (entry === undefined)
|
|
126
|
+
return false;
|
|
127
|
+
await entry.onSelect(context);
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
/** Drop every registration (service teardown; tests). */
|
|
131
|
+
clear() {
|
|
132
|
+
this.actions.clear();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../../src/routes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAMH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAClD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAA;AAC/D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAO9C,+CAA+C;AAC/C,eAAO,MAAM,cAAc,aAAa,CAAA;AAExC,0EAA0E;AAC1E,eAAO,MAAM,gBAAgB,SAAS,CAAA;AAMtC;;;;GAIG;AACH,eAAO,MAAM,cAAc;IACzB,yBAAyB;;IAEzB,iDAAiD;;IAEjD,8EAA8E;;CAEtE,CAAA;AAEV,6DAA6D;AAC7D,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,EAAE,MAAM,CAAA;IACb,KAAK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,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAcrF;AAqPD,4EAA4E;AAC5E,eAAO,MAAM,kBAAkB,QAA8B,CAAA;AAe7D,4EAA4E;AAC5E,eAAO,MAAM,eAAe,QAAmB,CAAA;AAE/C,6EAA6E;AAC7E,MAAM,WAAW,eAAe;IAC9B,+EAA+E;IAC/E,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,sEAAsE;IACtE,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AA4MD,4EAA4E;AAC5E,wBAAgB,aAAa,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,UAAU,CAAC;IAAC,GAAG,EAAE,OAAO,CAAC;IAAC,SAAS,CAAC,EAAE,YAAY,CAAA;CAAE,GAAG,eAAe,GAAG,QAAQ,EAAE,CAgGjI;AAGD,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA"}
|
package/lib/types/routes.js
CHANGED
|
@@ -598,6 +598,16 @@ export function makePetRoutes(deps) {
|
|
|
598
598
|
return Promise.reject(new Error('invalid-pet'));
|
|
599
599
|
return service.setPetId(petId);
|
|
600
600
|
}),
|
|
601
|
+
// Hover-panel action click (issue #6): the browser half owns no plugin
|
|
602
|
+
// callbacks, so a click on a registered action travels back here and the
|
|
603
|
+
// service dispatches it to the plugin that registered it. Same loopback /
|
|
604
|
+
// paired-device guard as every other endpoint in this family.
|
|
605
|
+
postRoute(ctx, PET_API_PREFIX + '/panel-action', (body) => {
|
|
606
|
+
const id = body.id;
|
|
607
|
+
if (typeof id !== 'string')
|
|
608
|
+
return Promise.reject(new Error('invalid-panel-action'));
|
|
609
|
+
return service.selectPanelAction(id);
|
|
610
|
+
}),
|
|
601
611
|
// Gameplay verbs (miku-pet generalization): touch rolls a named zone's
|
|
602
612
|
// branch; the omitted-zone form is the plain-click boost during a touch
|
|
603
613
|
// animation. Mode/tick/buy drive the work, sleep and shop loops.
|