@linxin666/dsh-pet 0.4.3 → 0.4.5

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/src/index.ts CHANGED
@@ -16,10 +16,10 @@ import { Context, type Volatile } from '@deepseek-ai/cordis'
16
16
  import type {} from '@deepseek-ai/dsh-settings'
17
17
  import type {} from '@deepseek-ai/dsh-host-webserver'
18
18
  import z from '@deepseek-ai/schemastery'
19
- import { PetService, type PetConfig, type PetSettingsSection } from './service.ts'
19
+ import { PET_SETTINGS_NAMESPACE, PetService, type PetConfig, type PetSettingsSection } from './service.ts'
20
20
  import { makePetRoutes } from './routes.ts'
21
21
  import { loadPetRegistry, petPackageRoot } from './registry.ts'
22
- import { BUBBLE_SCALE_MAX, BUBBLE_SCALE_MIN, DEFAULT_PET_ID, DISPLAY_INSET_MAX, DISPLAY_SIZE_MAX, DISPLAY_SIZE_MIN } from './persist.ts'
22
+ import { BUBBLE_SCALE_MAX, BUBBLE_SCALE_MIN, DEFAULT_PET_ID, DISPLAY_INSET_MAX, DISPLAY_SIZE_MAX, DISPLAY_SIZE_MIN, type PetDisplayConfig } from './persist.ts'
23
23
  import { mountOnce } from './mount-once.ts'
24
24
 
25
25
  export { PetService, MAX_SESSION_BUBBLES } from './service.ts'
@@ -203,29 +203,131 @@ function readLive<T>(field: LiveField<T> | undefined, fallback: T): T {
203
203
  return typeof ref.get === 'function' ? ref.get() ?? fallback : field as T
204
204
  }
205
205
 
206
+ /**
207
+ * Resolve one display field of the section. The precedence is deliberate:
208
+ *
209
+ * 1. an explicit profile value (`committed`) — the user's own choice, and the
210
+ * only signal that carries a value equal to the schema default on purpose;
211
+ * 2. a live value the row committed at runtime, i.e. one that differs from the
212
+ * schema default, which is all an untouched field ever resolves to;
213
+ * 3. what the pet's own `pet.json` already holds (`persisted`), so a field the
214
+ * profile never touched keeps the position the user dragged instead of
215
+ * snapping back to the schema default on the next mount.
216
+ *
217
+ * Step 3 is what makes a drag survive a restart. The display fields carry
218
+ * schema defaults (`right: 24`, `bottom: 20` — the bottom-right corner), and a
219
+ * config that never set them reads back exactly like one that set them to the
220
+ * defaults, so without `persisted` the startup section resets the layout every
221
+ * time the profile commits none — which is what an aggregate install does.
222
+ * @param key - the display field to resolve.
223
+ * @param live - the row config's own field, absent when the entry declares none.
224
+ * @param persisted - the display the pet already shows (its own `pet.json`).
225
+ * @param committed - the profile layer's explicit display values, when known.
226
+ * @returns the value the pet runs with.
227
+ */
228
+ function displayField<K extends keyof PetDisplayConfig>(
229
+ key: K,
230
+ live: LiveField<PetDisplayConfig[K]> | undefined,
231
+ persisted: Partial<PetDisplayConfig>,
232
+ committed: Partial<PetDisplayConfig>,
233
+ ): PetDisplayConfig[K] {
234
+ const explicit = committed[key]
235
+ if (explicit !== undefined) return explicit
236
+ const value = readLive<PetDisplayConfig[K]>(live, PET_FORM_DEFAULTS[key])
237
+ if (value !== PET_FORM_DEFAULTS[key]) return value
238
+ const kept = persisted[key]
239
+ return kept === undefined ? value : kept
240
+ }
241
+
206
242
  /**
207
243
  * The settings section the pet runs with: the effective values of the row's
208
244
  * own config. `fallbackPetId` covers a mount whose config names no pet at all
209
245
  * (a direct mount outside a Loader) — under a Loader the schema default is
210
246
  * always present, so the persisted selection stands whenever the config
211
- * carries it.
247
+ * carries it. `persisted` and `committed` give the display fields the same
248
+ * treatment; see {@link displayField}.
212
249
  * @param config - the effective config of the pet row.
213
250
  * @param fallbackPetId - pet id to use when the config names none.
251
+ * @param persisted - the display the pet already shows (its own `pet.json`).
252
+ * @param committed - the profile layer's explicit display values, when known.
214
253
  * @returns the resolved settings section.
215
254
  */
216
- export function petSettingsSection(config: PetFormConfig, fallbackPetId: string): PetSettingsSection {
255
+ export function petSettingsSection(
256
+ config: PetFormConfig,
257
+ fallbackPetId: string,
258
+ persisted: Partial<PetDisplayConfig> = {},
259
+ committed: Partial<PetDisplayConfig> = {},
260
+ ): PetSettingsSection {
261
+ // `visible` and `enabled` answer two DIFFERENT questions and are kept
262
+ // apart on purpose (#1762):
263
+ //
264
+ // - `visible` is the pet's own hide/show intent, persisted in its pet.json
265
+ // and written by the sprite's hide/summon control and by the aggregate
266
+ // shell's fallback switch.
267
+ // - `enabled` is the plugin master switch, which hides the pet whatever that
268
+ // own intent says.
269
+ //
270
+ // The effective "is it on screen" state is the conjunction, and it belongs at
271
+ // the single point that PRODUCES the view (see PetService.state), never here
272
+ // and never written back to pet.json. Folding it into this field is what made
273
+ // the master switch one-way: switching it off stored the derived `false` as
274
+ // the user's own intent, so switching it on again (or restarting) read that
275
+ // stored `false` back and the pet never returned.
276
+ const enabled = readLive(config.enabled, PET_FORM_DEFAULTS.enabled)
217
277
  return {
218
- visible: readLive(config.visible, PET_FORM_DEFAULTS.visible),
219
- size: readLive(config.size, PET_FORM_DEFAULTS.size),
220
- right: readLive(config.right, PET_FORM_DEFAULTS.right),
221
- bottom: readLive(config.bottom, PET_FORM_DEFAULTS.bottom),
222
- bubbleScale: readLive(config.bubbleScale, PET_FORM_DEFAULTS.bubbleScale),
278
+ visible: displayField('visible', config.visible, persisted, committed),
279
+ size: displayField('size', config.size, persisted, committed),
280
+ right: displayField('right', config.right, persisted, committed),
281
+ bottom: displayField('bottom', config.bottom, persisted, committed),
282
+ bubbleScale: displayField('bubbleScale', config.bubbleScale, persisted, committed),
223
283
  petId: readLive(config.petId, fallbackPetId),
224
- enabled: readLive(config.enabled, PET_FORM_DEFAULTS.enabled),
284
+ enabled,
225
285
  decorationEnabled: readLive(config.decorationEnabled, PET_FORM_DEFAULTS.decorationEnabled),
226
286
  }
227
287
  }
228
288
 
289
+ /** The settings row ids a pet install is served under, most specific first. */
290
+ const PET_SETTINGS_ROW_IDS: readonly string[] = ['web-ui-pet', 'ui-pet', PET_SETTINGS_NAMESPACE]
291
+
292
+ /**
293
+ * The fields this host half reads off one settings descriptor. The settings
294
+ * service serves one descriptor per plugin row (see `describe`), and a plugin's
295
+ * own config IS its settings page, so the two that matter here are the row's
296
+ * namespace and the profile layer's explicit values.
297
+ */
298
+ interface ServedSettingsRow {
299
+ /** The profile entry's id, which is also this plugin's settings namespace. */
300
+ ns: string
301
+ /** The profile layer's explicit values; absent fields were never committed. */
302
+ user?: Partial<PetDisplayConfig>
303
+ }
304
+
305
+ /**
306
+ * The settings row serving this plugin's own config, or undefined when the
307
+ * settings service is not mounted (a direct mount) or serves no row for the pet.
308
+ *
309
+ * Every settings write has to address this row's id, and that id is not the
310
+ * package's plugin name: an aggregate bundle renames child rows (`pet` becomes
311
+ * `web-ui-pet`). The browser half resolves the same id through its own
312
+ * `servedEntryId`, so both halves write to one namespace.
313
+ * @param ctx - the plugin's context.
314
+ * @returns the serving row, when there is one.
315
+ */
316
+ function servedRow(ctx: Context): ServedSettingsRow | undefined {
317
+ try {
318
+ const settings = ctx.get('settings', false) as { describe(): readonly ServedSettingsRow[] } | undefined
319
+ if (settings === undefined) return undefined
320
+ const rows = settings.describe()
321
+ for (const id of PET_SETTINGS_ROW_IDS) {
322
+ const row = rows.find(candidate => candidate.ns === id)
323
+ if (row !== undefined) return row
324
+ }
325
+ return undefined
326
+ } catch {
327
+ return undefined
328
+ }
329
+ }
330
+
229
331
  /** Register the pet service and its API + asset routes on the context. */
230
332
  export const apply = mountOnce('@linxin666/dsh-pet', applyImpl)
231
333
 
@@ -244,13 +346,21 @@ function applyImpl(ctx: Context, config: PetPluginConfig = {}): void {
244
346
  decorationEnabled: readLive(config.decorationEnabled, PET_FORM_DEFAULTS.decorationEnabled),
245
347
  registry,
246
348
  })
349
+ // The mirror back into the settings document (see syncSettingsFromPet) has to
350
+ // address the row that serves this plugin's config, which an aggregate bundle
351
+ // renames; the id is resolved lazily, once the composition serves it.
352
+ service.settingsNamespaceProvider = () => servedRow(ctx)?.ns
247
353
 
248
354
  // The effective settings ARE this row's own config: the Host serves one form
249
355
  // per profile entry from `Config` above, and every edit is committed into the
250
356
  // running config, so the plugin re-reads the section here instead of holding
251
357
  // a separate settings document (0.1.6 registered one through
252
- // settings.installSection/register and re-resolved it on every change).
253
- const current = (): PetSettingsSection => petSettingsSection(config, service.selectedPetId())
358
+ // settings.installSection/register and re-resolved it on every change). The
359
+ // display fields additionally read the persisted layout and the profile layer,
360
+ // so a mount with a config that never set them cannot reset a dragged
361
+ // position to the schema defaults (see displayField).
362
+ const current = (): PetSettingsSection =>
363
+ petSettingsSection(config, service.selectedPetId(), service.display(), servedRow(ctx)?.user)
254
364
  // The browser half talks to the pet through same-origin JSON endpoints and
255
365
  // loads each pet's atlas from the registry's own media route (RPC domains
256
366
  // are platform-registered, so the pet serves its own API — the same
@@ -315,7 +315,8 @@ describe('loadPetRegistry', () => {
315
315
  })
316
316
 
317
317
  // The repo checkout also resolves doro and miku (frames2d gameplay pets),
318
- // jyn (frames2d gameplay pet) and starry-doll (community sprite2d pet) from
318
+ // jyn and jyn-foxtail (frames2d gameplay pets), long-niang (sprite2d pet)
319
+ // and starry-doll (community sprite2d pet) from
319
320
  // assets/; the npm files whitelist excludes them (Workshop delivery), so
320
321
  // npm installs see the atlas pets until a Workshop install lands them
321
322
  // under $DSH_HOME/pets. blue-throated-bee-eater ships bundled alongside
@@ -324,6 +325,8 @@ describe('loadPetRegistry', () => {
324
325
  'blue-throated-bee-eater',
325
326
  'doro',
326
327
  'jyn',
328
+ 'jyn-foxtail',
329
+ 'long-niang',
327
330
  'miku',
328
331
  'ouo-neko',
329
332
  'starry-doll',
@@ -341,6 +344,13 @@ describe('loadPetRegistry', () => {
341
344
  displayName: '女仆鲸鱼娘',
342
345
  renderer: 'frames2d',
343
346
  })
347
+ expect(registry.byId('long-niang')).toMatchObject({
348
+ displayName: '龙娘',
349
+ atlasRows: 11,
350
+ columns: 8,
351
+ rows: [6, 8, 8, 4, 5, 8, 6, 6, 6],
352
+ })
353
+ expect(existsSync(petAtlasFile(registry.byId('long-niang')!))).toBe(true)
344
354
  expect(registry.byId('ouo-neko')).toMatchObject({
345
355
  displayName: 'OUO Neko',
346
356
  atlasRows: 11,
package/src/service.ts CHANGED
@@ -300,6 +300,15 @@ export class PetService extends Service {
300
300
  * the same turn again after the pet is disabled and re-enabled.
301
301
  */
302
302
  private readonly officialEventSessions = new WeakSet<Session>()
303
+ /**
304
+ * Resolve the settings namespace this plugin's own config is served as, when
305
+ * the caller can read it off the composition. A plugin's settings ARE its own
306
+ * config, so the namespace is the row's id — and an aggregate bundle renames
307
+ * child rows (`pet` becomes `web-ui-pet`), which is why it cannot be assumed
308
+ * to be {@link PET_SETTINGS_NAMESPACE}. `apply` sets this; absent means the
309
+ * plugin runs outside a Loader and the package's own id stands.
310
+ */
311
+ settingsNamespaceProvider: (() => string | undefined) | undefined = undefined
303
312
 
304
313
  constructor(ctx: Context, config: PetConfig = {}) {
305
314
  super(ctx, 'pet')
@@ -823,7 +832,13 @@ export class PetService extends Service {
823
832
  this.syncSettingsFromPet()
824
833
  }
825
834
  const next = { ...this.ledger.snapshot.display }
826
- next.visible = section.visible && (section.enabled ?? true)
835
+ // The pet's own hide/show intent, stored verbatim. The master switch is
836
+ // applied when the state view is produced (see the state view below), not
837
+ // here: writing the conjunction into pet.json turned `enabled: false` into
838
+ // a persisted "the user hid the pet" and the switch could never be undone -
839
+ // re-enabling it (or restarting) read that stored `false` straight back
840
+ // (#1762).
841
+ next.visible = section.visible
827
842
  next.size = Math.round(Math.min(DISPLAY_SIZE_MAX, Math.max(DISPLAY_SIZE_MIN, section.size)))
828
843
  next.right = Math.round(Math.min(DISPLAY_INSET_MAX, Math.max(0, section.right)))
829
844
  next.bottom = Math.round(Math.min(DISPLAY_INSET_MAX, Math.max(0, section.bottom)))
@@ -836,8 +851,9 @@ export class PetService extends Service {
836
851
  private syncSettingsFromPet(): void {
837
852
  const settings = this.ctx.get('settings', false) as { update(ns: string, patch: object): Promise<void> } | undefined
838
853
  if (settings === undefined) return
854
+ const ns = this.settingsNamespaceProvider?.() ?? PET_SETTINGS_NAMESPACE
839
855
  const snapshot = this.ledger.snapshot
840
- void settings.update(PET_SETTINGS_NAMESPACE, {
856
+ void settings.update(ns, {
841
857
  visible: snapshot.display.visible,
842
858
  size: snapshot.display.size,
843
859
  right: snapshot.display.right,
@@ -924,7 +940,11 @@ export class PetService extends Service {
924
940
  ...(decoration === undefined ? {} : { decoration }),
925
941
  ...(announcement === undefined ? {} : { announcement }),
926
942
  affinity: this.ledger.affinityView(Date.now()),
927
- display: { ...this.ledger.snapshot.display },
943
+ // The one place the two visibility answers are combined: the pet's own
944
+ // hide/show intent AND the plugin master switch. Producing the effective
945
+ // value here - rather than storing it - keeps the switch reversible and
946
+ // keeps pet.json describing only what the user chose (#1762).
947
+ display: { ...this.ledger.snapshot.display, visible: this.isEnabled() && this.ledger.snapshot.display.visible },
928
948
  pet: {
929
949
  id: entry.id,
930
950
  displayName: entry.displayName,