@linxin666/dsh-pet 0.2.2 → 0.2.4
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 +129 -3
- package/README.zh.md +129 -3
- package/assets/decorations/whale/decoration.json +20 -0
- package/assets/decorations/whale/whale-frames.png +0 -0
- package/assets/whale/pet.json +169 -20
- package/assets/whale-refined/pet.json +17 -2
- package/contracts/pet-manifest-v2.schema.json +281 -0
- package/contracts/status-decoration-v1.schema.json +144 -0
- package/contracts/voice-pack-v1.schema.json +234 -0
- package/lib/client.js +581 -38
- package/lib/client.js.map +1 -1
- package/lib/index.js +1684 -59
- package/lib/live2d-vendor.js +995 -0
- package/lib/live2d-vendor.js.map +1 -0
- package/lib/types/chatter.d.ts +61 -3
- package/lib/types/chatter.d.ts.map +1 -1
- package/lib/types/chatter.js +71 -12
- package/lib/types/client/PetDockEntry.d.ts.map +1 -1
- package/lib/types/client/PetDockEntry.js +2 -1
- package/lib/types/client/PetSettingsCard.d.ts +14 -0
- package/lib/types/client/PetSettingsCard.d.ts.map +1 -1
- package/lib/types/client/PetSettingsCard.js +24 -1
- package/lib/types/client/PetSprite.d.ts +7 -1
- package/lib/types/client/PetSprite.d.ts.map +1 -1
- package/lib/types/client/PetSprite.js +120 -12
- package/lib/types/client/index.d.ts.map +1 -1
- package/lib/types/client/index.js +5 -0
- package/lib/types/client/locales.d.ts +14 -0
- package/lib/types/client/locales.d.ts.map +1 -1
- package/lib/types/client/locales.js +14 -0
- package/lib/types/client/phase-stream.d.ts +22 -0
- package/lib/types/client/phase-stream.d.ts.map +1 -0
- package/lib/types/client/phase-stream.js +28 -0
- package/lib/types/client/renderers/PetRendererSwitch.d.ts +25 -0
- package/lib/types/client/renderers/PetRendererSwitch.d.ts.map +1 -0
- package/lib/types/client/renderers/PetRendererSwitch.js +24 -0
- package/lib/types/client/renderers/live2d/Live2dVisualMount.d.ts +22 -0
- package/lib/types/client/renderers/live2d/Live2dVisualMount.d.ts.map +1 -0
- package/lib/types/client/renderers/live2d/Live2dVisualMount.js +75 -0
- package/lib/types/client/renderers/live2d/runtime.d.ts +89 -0
- package/lib/types/client/renderers/live2d/runtime.d.ts.map +1 -0
- package/lib/types/client/renderers/live2d/runtime.js +63 -0
- package/lib/types/client/renderers/live2d/vendor-entry.d.ts +15 -0
- package/lib/types/client/renderers/live2d/vendor-entry.d.ts.map +1 -0
- package/lib/types/client/renderers/live2d/vendor-entry.js +14 -0
- package/lib/types/client/renderers/live2d.d.ts +52 -0
- package/lib/types/client/renderers/live2d.d.ts.map +1 -0
- package/lib/types/client/renderers/live2d.js +192 -0
- package/lib/types/client/renderers/registry.d.ts +32 -0
- package/lib/types/client/renderers/registry.d.ts.map +1 -0
- package/lib/types/client/renderers/registry.js +49 -0
- package/lib/types/client/settings-form.d.ts.map +1 -1
- package/lib/types/client/settings-form.js +9 -6
- package/lib/types/contracts/renderer.d.ts +50 -0
- package/lib/types/contracts/renderer.d.ts.map +1 -0
- package/lib/types/contracts/renderer.js +15 -0
- package/lib/types/contracts/status-decoration.d.ts +85 -0
- package/lib/types/contracts/status-decoration.d.ts.map +1 -0
- package/lib/types/contracts/status-decoration.js +21 -0
- package/lib/types/decoration.d.ts +39 -0
- package/lib/types/decoration.d.ts.map +1 -0
- package/lib/types/decoration.js +210 -0
- package/lib/types/event-projection.d.ts +8 -3
- package/lib/types/event-projection.d.ts.map +1 -1
- package/lib/types/event-projection.js +9 -4
- package/lib/types/index.d.ts +2 -0
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/index.js +2 -0
- package/lib/types/manifest-v2.d.ts +129 -0
- package/lib/types/manifest-v2.d.ts.map +1 -0
- package/lib/types/manifest-v2.js +387 -0
- package/lib/types/model3.d.ts +26 -0
- package/lib/types/model3.d.ts.map +1 -0
- package/lib/types/model3.js +96 -0
- package/lib/types/registry.d.ts +112 -5
- package/lib/types/registry.d.ts.map +1 -1
- package/lib/types/registry.js +395 -38
- package/lib/types/routes.d.ts +39 -2
- package/lib/types/routes.d.ts.map +1 -1
- package/lib/types/routes.js +312 -13
- package/lib/types/service.d.ts +38 -1
- package/lib/types/service.d.ts.map +1 -1
- package/lib/types/service.js +46 -3
- package/lib/types/voice-pack.d.ts +98 -0
- package/lib/types/voice-pack.d.ts.map +1 -0
- package/lib/types/voice-pack.js +384 -0
- package/package.json +13 -10
- package/src/chatter.test.ts +89 -2
- package/src/chatter.ts +120 -14
- package/src/client/PetDockEntry.tsx +19 -11
- package/src/client/PetSettingsCard.tsx +57 -0
- package/src/client/PetSprite.test.tsx +265 -12
- package/src/client/PetSprite.tsx +165 -33
- package/src/client/index.ts +6 -0
- package/src/client/locales.ts +14 -0
- package/src/client/phase-stream.test.ts +76 -0
- package/src/client/phase-stream.ts +39 -0
- package/src/client/renderers/PetRendererSwitch.test.tsx +66 -0
- package/src/client/renderers/PetRendererSwitch.tsx +48 -0
- package/src/client/renderers/live2d/Live2dVisualMount.tsx +98 -0
- package/src/client/renderers/live2d/runtime.test.ts +61 -0
- package/src/client/renderers/live2d/runtime.ts +119 -0
- package/src/client/renderers/live2d/vendor-entry.ts +14 -0
- package/src/client/renderers/live2d.test.ts +263 -0
- package/src/client/renderers/live2d.ts +231 -0
- package/src/client/renderers/registry.ts +58 -0
- package/src/client/settings-form.ts +8 -6
- package/src/client/settings-section.module.css +23 -0
- package/src/contracts/renderer.ts +54 -0
- package/src/contracts/status-decoration.ts +78 -0
- package/src/decoration.test.ts +178 -0
- package/src/decoration.ts +220 -0
- package/src/event-projection.ts +10 -5
- package/src/index.ts +2 -0
- package/src/manifest-v2.test.ts +251 -0
- package/src/manifest-v2.ts +414 -0
- package/src/model3.test.ts +79 -0
- package/src/model3.ts +91 -0
- package/src/registry.test.ts +438 -2
- package/src/registry.ts +470 -38
- package/src/routes.ts +328 -14
- package/src/service.ts +65 -3
- package/src/voice-pack.test.ts +216 -0
- package/src/voice-pack.ts +413 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Status-decoration contract — the L3 extension slot for small status
|
|
3
|
+
* ornaments (issue #623 milestone M5, protocol #567, first reference
|
|
4
|
+
* implementation #463). A decoration is an INDEPENDENT content entry (own
|
|
5
|
+
* id, own directory, own descriptor) whose PNG/WebP sprite strip ornaments
|
|
6
|
+
* the pet center's status bubble chrome; it never touches the pet
|
|
7
|
+
* manifests and never changes the bubble's semantics.
|
|
8
|
+
*
|
|
9
|
+
* Adopted disciplines (#623): entry assets are PNG/WebP sprite strips only
|
|
10
|
+
* (no SVG, no CSS animation); the bubble's own role=status/aria-live (or
|
|
11
|
+
* session-bubble button semantics) always stays intact and the ornament is
|
|
12
|
+
* aria-hidden; load failure or prefers-reduced-motion degrades to no
|
|
13
|
+
* ornament or the static first frame.
|
|
14
|
+
*
|
|
15
|
+
* The ActivityPhase stream the pet center owns drives the ornament: each
|
|
16
|
+
* phase binds to a frame segment (inclusive from/to indices into the
|
|
17
|
+
* strip) or to 'hide' (no ornament for that phase; the default).
|
|
18
|
+
* @module @linxin666/dsh-pet/contracts/status-decoration
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { ActivityPhase } from '../state.ts'
|
|
22
|
+
|
|
23
|
+
/** Contract version decorations declare against (independent of manifests). */
|
|
24
|
+
export const PET_DECORATION_API_VERSION = 'x-org.linxin666.pet-center/status-decoration-v1' as const
|
|
25
|
+
|
|
26
|
+
/** One phase binding: a frame segment, or hidden. */
|
|
27
|
+
export type PhaseSegment = { from: number; to: number } | 'hide'
|
|
28
|
+
|
|
29
|
+
/** The ActivityPhase -> frame-segment binding table (unmapped phases hide). */
|
|
30
|
+
export type PhaseBindings = Partial<Record<ActivityPhase, PhaseSegment>>
|
|
31
|
+
|
|
32
|
+
/** Normalized decoration descriptor as the registry consumes it. */
|
|
33
|
+
export interface DecorationManifest {
|
|
34
|
+
decorationManifestVersion: 1
|
|
35
|
+
id: string
|
|
36
|
+
displayName: string
|
|
37
|
+
license: string
|
|
38
|
+
/** Strip path relative to the descriptor directory (PNG/WebP). */
|
|
39
|
+
entry: string
|
|
40
|
+
cell: { width: number; height: number }
|
|
41
|
+
columns: number
|
|
42
|
+
/** Per-frame duration ms (same length as columns, or a single constant). */
|
|
43
|
+
durations: number[]
|
|
44
|
+
loop: boolean
|
|
45
|
+
phases: PhaseBindings
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** One structured diagnostic emitted while parsing a descriptor. */
|
|
49
|
+
export interface DecorationDiagnostic {
|
|
50
|
+
level: 'error' | 'warning'
|
|
51
|
+
message: string
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Parse outcome: a usable descriptor plus diagnostics, or rejection. */
|
|
55
|
+
export type DecorationManifestParse =
|
|
56
|
+
| { ok: true; manifest: DecorationManifest; diagnostics: DecorationDiagnostic[] }
|
|
57
|
+
| { ok: false; diagnostics: DecorationDiagnostic[] }
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The decoration block the host serves inside the pet state view — exactly
|
|
61
|
+
* what the browser half needs to render the ornament, nothing more. The
|
|
62
|
+
* apiVersion rides the wire so a future protocol revision can be detected
|
|
63
|
+
* and negotiated by clients (review-spd follow-up, pet-center M5).
|
|
64
|
+
*/
|
|
65
|
+
export interface DecorationView {
|
|
66
|
+
/** The protocol version this view speaks (PET_DECORATION_API_VERSION). */
|
|
67
|
+
apiVersion: typeof PET_DECORATION_API_VERSION
|
|
68
|
+
id: string
|
|
69
|
+
/** Same-origin URL prefix of the decoration's assets. */
|
|
70
|
+
assetBase: string
|
|
71
|
+
/** Browser URL of the strip. */
|
|
72
|
+
entryUrl: string
|
|
73
|
+
cell: { width: number; height: number }
|
|
74
|
+
columns: number
|
|
75
|
+
durations: number[]
|
|
76
|
+
loop: boolean
|
|
77
|
+
phases: PhaseBindings
|
|
78
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Status-decoration manifest tests (pet-center M5, #567): fail-closed
|
|
3
|
+
* structure, warn-and-drop content, duration normalization and the
|
|
4
|
+
* PNG/WebP entry discipline.
|
|
5
|
+
*/
|
|
6
|
+
import { describe, expect, it } from 'vitest'
|
|
7
|
+
import { readFileSync } from 'node:fs'
|
|
8
|
+
import { join } from 'node:path'
|
|
9
|
+
import { KNOWN_DECORATION_TOP_LEVEL, parseDecorationManifest, safeDecorationEntry } from './decoration.ts'
|
|
10
|
+
import { PET_ACTIVITY_PHASES } from './manifest-v2.ts'
|
|
11
|
+
import { petPackageRoot } from './registry.ts'
|
|
12
|
+
|
|
13
|
+
function parse(raw: unknown) {
|
|
14
|
+
return parseDecorationManifest(raw, 'test/decoration.json')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function valid(): Record<string, unknown> {
|
|
18
|
+
return {
|
|
19
|
+
decorationManifestVersion: 1,
|
|
20
|
+
id: 'whale',
|
|
21
|
+
displayName: '喷水鲸鱼',
|
|
22
|
+
license: 'MIT',
|
|
23
|
+
entry: 'whale-frames.png',
|
|
24
|
+
cell: { width: 64, height: 48 },
|
|
25
|
+
columns: 4,
|
|
26
|
+
frameMs: 140,
|
|
27
|
+
loop: true,
|
|
28
|
+
phases: {
|
|
29
|
+
idle: 'hide',
|
|
30
|
+
waiting: { from: 0, to: 1 },
|
|
31
|
+
thinking: { from: 0, to: 3 },
|
|
32
|
+
},
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe('parseDecorationManifest structure', () => {
|
|
37
|
+
it('accepts a valid descriptor with duration and display-name defaults', () => {
|
|
38
|
+
const verdict = parse(valid())
|
|
39
|
+
expect(verdict.ok).toBe(true)
|
|
40
|
+
if (!verdict.ok) return
|
|
41
|
+
expect(verdict.manifest.durations).toEqual([140, 140, 140, 140])
|
|
42
|
+
expect(verdict.manifest.loop).toBe(true)
|
|
43
|
+
expect(verdict.manifest.phases.thinking).toEqual({ from: 0, to: 3 })
|
|
44
|
+
expect(verdict.manifest.phases.idle).toBe('hide')
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('defaults displayName to the id and frameMs to 120', () => {
|
|
48
|
+
const manifest = { ...valid() }
|
|
49
|
+
delete manifest.displayName
|
|
50
|
+
delete manifest.frameMs
|
|
51
|
+
const verdict = parse(manifest)
|
|
52
|
+
expect(verdict.ok).toBe(true)
|
|
53
|
+
if (!verdict.ok) return
|
|
54
|
+
expect(verdict.manifest.displayName).toBe('whale')
|
|
55
|
+
expect(verdict.manifest.durations).toEqual([120, 120, 120, 120])
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('rejects non-object roots, wrong versions and unknown top-level fields', () => {
|
|
59
|
+
expect(parse(['x']).ok).toBe(false)
|
|
60
|
+
const wrongVersion = { ...valid(), decorationManifestVersion: 2 }
|
|
61
|
+
const verdict = parse(wrongVersion)
|
|
62
|
+
expect(verdict.ok).toBe(false)
|
|
63
|
+
if (verdict.ok) return
|
|
64
|
+
expect(verdict.diagnostics.some(d => d.message.includes('decorationManifestVersion'))).toBe(true)
|
|
65
|
+
const unknown = { ...valid(), mystery: true }
|
|
66
|
+
expect(parse(unknown).ok).toBe(false)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('rejects unsafe or non-image entries', () => {
|
|
70
|
+
for (const entry of ['../etc/passwd', '/abs.png', 'a.svg', 'a.css', 'x\\y.png']) {
|
|
71
|
+
const verdict = parse({ ...valid(), entry })
|
|
72
|
+
expect(verdict.ok).toBe(false)
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('rejects out-of-range geometry and missing license/id', () => {
|
|
77
|
+
expect(parse({ ...valid(), cell: { width: 999, height: 48 } }).ok).toBe(false)
|
|
78
|
+
expect(parse({ ...valid(), columns: 99 }).ok).toBe(false)
|
|
79
|
+
expect(parse({ ...valid(), license: '' }).ok).toBe(false)
|
|
80
|
+
expect(parse({ ...valid(), id: 'Bad Id' }).ok).toBe(false)
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
describe('parseDecorationManifest content (warn-and-drop)', () => {
|
|
85
|
+
it('drops unknown phase keys and out-of-range segments with warnings', () => {
|
|
86
|
+
const verdict = parse({
|
|
87
|
+
...valid(),
|
|
88
|
+
phases: {
|
|
89
|
+
...(valid().phases as Record<string, unknown>),
|
|
90
|
+
bogus: { from: 0, to: 1 },
|
|
91
|
+
waiting: { from: 2, to: 9 },
|
|
92
|
+
review: { from: 3, to: 1 },
|
|
93
|
+
},
|
|
94
|
+
})
|
|
95
|
+
expect(verdict.ok).toBe(true)
|
|
96
|
+
if (!verdict.ok) return
|
|
97
|
+
expect(verdict.manifest.phases.done).toBeUndefined()
|
|
98
|
+
expect(verdict.manifest.phases.waiting).toBeUndefined()
|
|
99
|
+
expect(verdict.manifest.phases.review).toBeUndefined()
|
|
100
|
+
expect(verdict.diagnostics.some(d => d.level === 'warning' && d.message.includes('ignored'))).toBe(true)
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it('warns when no phase shows the ornament', () => {
|
|
104
|
+
const verdict = parse({ ...valid(), phases: { idle: 'hide' } })
|
|
105
|
+
expect(verdict.ok).toBe(true)
|
|
106
|
+
expect(verdict.diagnostics.some(d => d.level === 'warning' && d.message.includes('stays hidden'))).toBe(true)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('falls back to the constant frameMs when durations has the wrong length', () => {
|
|
110
|
+
const verdict = parse({ ...valid(), durations: [100] })
|
|
111
|
+
expect(verdict.ok).toBe(true)
|
|
112
|
+
if (!verdict.ok) return
|
|
113
|
+
expect(verdict.manifest.durations).toEqual([140, 140, 140, 140])
|
|
114
|
+
expect(verdict.diagnostics.some(d => d.level === 'warning' && d.message.includes('durations'))).toBe(true)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('keeps a well-sized durations array', () => {
|
|
118
|
+
const verdict = parse({ ...valid(), durations: [120, 130, 140, 150] })
|
|
119
|
+
expect(verdict.ok).toBe(true)
|
|
120
|
+
if (!verdict.ok) return
|
|
121
|
+
expect(verdict.manifest.durations).toEqual([120, 130, 140, 150])
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('warns and defaults to looping when loop is not a boolean', () => {
|
|
125
|
+
const verdict = parse({ ...valid(), loop: 'yes' })
|
|
126
|
+
expect(verdict.ok).toBe(true)
|
|
127
|
+
if (!verdict.ok) return
|
|
128
|
+
expect(verdict.manifest.loop).toBe(true)
|
|
129
|
+
expect(verdict.diagnostics.some(d => d.level === 'warning' && d.message.includes('loop'))).toBe(true)
|
|
130
|
+
const off = parse({ ...valid(), loop: false })
|
|
131
|
+
expect(off.ok && off.manifest.loop).toBe(false)
|
|
132
|
+
})
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
describe('safeDecorationEntry', () => {
|
|
136
|
+
it('accepts safe relative PNG/WebP paths and rejects everything else', () => {
|
|
137
|
+
expect(safeDecorationEntry('frames.webp')).toBe('frames.webp')
|
|
138
|
+
expect(safeDecorationEntry('a/b.png')).toBe('a/b.png')
|
|
139
|
+
expect(safeDecorationEntry('')).toBeUndefined()
|
|
140
|
+
expect(safeDecorationEntry('a/../b.png')).toBeUndefined()
|
|
141
|
+
expect(safeDecorationEntry('/tmp/x.png')).toBeUndefined()
|
|
142
|
+
expect(safeDecorationEntry('x.svg')).toBeUndefined()
|
|
143
|
+
expect(safeDecorationEntry('x')).toBeUndefined()
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it('rejects case-mismatched extensions (the route serves paths verbatim)', () => {
|
|
147
|
+
// A lenient case check would pass validation here, but the asset route
|
|
148
|
+
// matches the declared path exactly and 403s on case-sensitive hosts.
|
|
149
|
+
expect(safeDecorationEntry('img/FRAMES.PNG')).toBeUndefined()
|
|
150
|
+
expect(safeDecorationEntry('img/frames.WebP')).toBeUndefined()
|
|
151
|
+
expect(safeDecorationEntry('img/frames.png')).toBe('img/frames.png')
|
|
152
|
+
})
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
describe('status-decoration schema file drift lock', () => {
|
|
156
|
+
const schema = JSON.parse(readFileSync(
|
|
157
|
+
join(petPackageRoot(import.meta.url), 'contracts', 'status-decoration-v1.schema.json'),
|
|
158
|
+
'utf8',
|
|
159
|
+
)) as {
|
|
160
|
+
required: string[]
|
|
161
|
+
properties: Record<string, { const?: number; properties?: Record<string, unknown> }>
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
it('locks the schema top-level fields to the validator allow-list', () => {
|
|
165
|
+
expect(new Set(Object.keys(schema.properties))).toEqual(KNOWN_DECORATION_TOP_LEVEL)
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
it('locks the required set and the version const', () => {
|
|
169
|
+
expect([...schema.required].sort()).toEqual([
|
|
170
|
+
'cell', 'columns', 'decorationManifestVersion', 'entry', 'id', 'license',
|
|
171
|
+
])
|
|
172
|
+
expect(schema.properties.decorationManifestVersion?.const).toBe(1)
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
it('locks the phases key set to the ActivityPhase stream', () => {
|
|
176
|
+
expect(new Set(Object.keys(schema.properties.phases?.properties ?? {}))).toEqual(new Set(PET_ACTIVITY_PHASES))
|
|
177
|
+
})
|
|
178
|
+
})
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Status-decoration manifest v1 — fail-closed structure + warn-and-drop
|
|
3
|
+
* content, mirroring the pet manifest-v2 discipline (issue #623 M5,
|
|
4
|
+
* protocol #567). Unknown top-level fields, unsafe entry paths, out-of-
|
|
5
|
+
* range geometry and unknown renderer content reject the descriptor with
|
|
6
|
+
* human-readable diagnostics; per-phase binding issues drop that binding
|
|
7
|
+
* only. The JSON Schema twin lives at
|
|
8
|
+
* contracts/status-decoration-v1.schema.json; this hand-rolled parser is
|
|
9
|
+
* authoritative. Keep this file erasable-syntax-only (scripts/ import it
|
|
10
|
+
* under node's strip-only mode).
|
|
11
|
+
* @module @linxin666/dsh-pet/decoration
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { isAbsolute } from 'node:path'
|
|
15
|
+
import type { ActivityPhase } from './state.ts'
|
|
16
|
+
import { PET_ACTIVITY_PHASES } from './manifest-v2.ts'
|
|
17
|
+
import type {
|
|
18
|
+
DecorationDiagnostic,
|
|
19
|
+
DecorationManifest,
|
|
20
|
+
DecorationManifestParse,
|
|
21
|
+
PhaseBindings,
|
|
22
|
+
PhaseSegment,
|
|
23
|
+
} from './contracts/status-decoration.ts'
|
|
24
|
+
|
|
25
|
+
/** Geometry and content caps (the adopted PNG/WebP sprite-strip bounds). */
|
|
26
|
+
export const DECORATION_CELL_MAX = 256
|
|
27
|
+
export const DECORATION_COLUMNS_MAX = 16
|
|
28
|
+
export const DECORATION_DURATION_MAX_MS = 2000
|
|
29
|
+
export const DECORATION_ENTRY_EXTENSIONS = ['.webp', '.png'] as const
|
|
30
|
+
export const DECORATION_DISPLAY_NAME_MAX = 64
|
|
31
|
+
|
|
32
|
+
/** Field allow-list (drift-locked to the schema twin in tests). */
|
|
33
|
+
export const KNOWN_DECORATION_TOP_LEVEL = new Set([
|
|
34
|
+
'$schema', 'decorationManifestVersion', 'id', 'displayName', 'license',
|
|
35
|
+
'entry', 'cell', 'columns', 'frameMs', 'durations', 'loop', 'phases',
|
|
36
|
+
])
|
|
37
|
+
|
|
38
|
+
const PET_ID_PATTERN = /^[a-z0-9][a-z0-9-]*$/
|
|
39
|
+
const PATH_SEGMENT_PATTERN = /^[A-Za-z0-9._-]+$/
|
|
40
|
+
|
|
41
|
+
class Diagnostics {
|
|
42
|
+
readonly list: DecorationDiagnostic[] = []
|
|
43
|
+
private readonly source: string
|
|
44
|
+
constructor(source: string) { this.source = source }
|
|
45
|
+
error(message: string): void { this.list.push({ level: 'error', message: this.source + ': ' + message }) }
|
|
46
|
+
warn(message: string): void { this.list.push({ level: 'warning', message: this.source + ': ' + message }) }
|
|
47
|
+
get hasErrors(): boolean { return this.list.some(d => d.level === 'error') }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
51
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function unknownKeys(source: Record<string, unknown>, known: Set<string>): string[] {
|
|
55
|
+
return Object.keys(source).filter(key => !known.has(key))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Positive integer in [min, max], else undefined. */
|
|
59
|
+
function finiteInt(value: unknown, min: number, max: number): number | undefined {
|
|
60
|
+
return typeof value === 'number' && Number.isInteger(value) && value >= min && value <= max
|
|
61
|
+
? value
|
|
62
|
+
: undefined
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Validate a descriptor-relative entry path: no absolute paths, no
|
|
67
|
+
* backslashes, no traversal, plain safe segments only, and an exact
|
|
68
|
+
* lowercase PNG/WebP extension (the adopted entry discipline — SVG/CSS are
|
|
69
|
+
* not accepted). The extension match is case-sensitive on purpose: the
|
|
70
|
+
* asset route serves the declared path verbatim, so a case-mismatched
|
|
71
|
+
* suffix (frames.PNG vs frames.png) would pass a lenient check but 403 on
|
|
72
|
+
* case-sensitive filesystems.
|
|
73
|
+
* Returns the normalized path or undefined.
|
|
74
|
+
*/
|
|
75
|
+
export function safeDecorationEntry(raw: unknown): string | undefined {
|
|
76
|
+
if (typeof raw !== 'string' || raw.trim() === '') return undefined
|
|
77
|
+
const value = raw.trim()
|
|
78
|
+
if (value.length > 256) return undefined
|
|
79
|
+
if (isAbsolute(value) || value.includes('\\') || /^[a-z][a-z0-9+.-]*:/i.test(value)) return undefined
|
|
80
|
+
const segments = value.split('/').filter(segment => segment !== '')
|
|
81
|
+
if (segments.length === 0) return undefined
|
|
82
|
+
if (segments.some(segment => segment === '.' || segment === '..' || !PATH_SEGMENT_PATTERN.test(segment))) return undefined
|
|
83
|
+
const last = segments[segments.length - 1]!
|
|
84
|
+
const dot = last.lastIndexOf('.')
|
|
85
|
+
if (dot <= 0 || !(DECORATION_ENTRY_EXTENSIONS as readonly string[]).includes(last.slice(dot))) return undefined
|
|
86
|
+
return segments.join('/')
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Normalize one phase binding value; undefined (warning) on bad content. */
|
|
90
|
+
function normalizeSegment(
|
|
91
|
+
raw: unknown,
|
|
92
|
+
columns: number,
|
|
93
|
+
diag: Diagnostics,
|
|
94
|
+
): PhaseSegment | undefined {
|
|
95
|
+
if (raw === 'hide') return 'hide'
|
|
96
|
+
if (!isRecord(raw)) {
|
|
97
|
+
diag.warn('phase binding must be "hide" or { from, to }; binding dropped')
|
|
98
|
+
return undefined
|
|
99
|
+
}
|
|
100
|
+
const from = finiteInt(raw.from, 0, columns - 1)
|
|
101
|
+
const to = finiteInt(raw.to, 0, columns - 1)
|
|
102
|
+
if (from === undefined || to === undefined || from > to) {
|
|
103
|
+
diag.warn('phase frame segment out of range; binding dropped')
|
|
104
|
+
return undefined
|
|
105
|
+
}
|
|
106
|
+
return { from, to }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Parse and validate one decoration.json document. Fail-closed over the
|
|
111
|
+
* structure (types, key sets, paths, ranges); phase-binding content issues
|
|
112
|
+
* drop that binding only (warn-and-drop, the registry never-throw rule).
|
|
113
|
+
*/
|
|
114
|
+
export function parseDecorationManifest(
|
|
115
|
+
raw: unknown,
|
|
116
|
+
source: string = 'decoration.json',
|
|
117
|
+
): DecorationManifestParse {
|
|
118
|
+
const diag = new Diagnostics(source)
|
|
119
|
+
if (!isRecord(raw)) {
|
|
120
|
+
diag.error('descriptor must be a JSON object')
|
|
121
|
+
return { ok: false, diagnostics: diag.list }
|
|
122
|
+
}
|
|
123
|
+
for (const key of unknownKeys(raw, KNOWN_DECORATION_TOP_LEVEL)) {
|
|
124
|
+
diag.error('unknown top-level field ' + JSON.stringify(key))
|
|
125
|
+
}
|
|
126
|
+
if (raw.decorationManifestVersion !== 1) {
|
|
127
|
+
diag.error('decorationManifestVersion must be 1')
|
|
128
|
+
}
|
|
129
|
+
const id = typeof raw.id === 'string' ? raw.id.trim() : ''
|
|
130
|
+
if (!PET_ID_PATTERN.test(id)) {
|
|
131
|
+
diag.error('id must be a lowercase kebab id')
|
|
132
|
+
}
|
|
133
|
+
if (id.length > 64) {
|
|
134
|
+
diag.error('id must be at most 64 characters')
|
|
135
|
+
}
|
|
136
|
+
const license = typeof raw.license === 'string' ? raw.license.trim() : ''
|
|
137
|
+
if (license === '') diag.error('license is required (asset provenance)')
|
|
138
|
+
if (license.length > 128) {
|
|
139
|
+
diag.error('license must be at most 128 characters')
|
|
140
|
+
}
|
|
141
|
+
const entry = safeDecorationEntry(raw.entry)
|
|
142
|
+
if (entry === undefined) {
|
|
143
|
+
diag.error('entry must be a safe relative PNG/WebP path')
|
|
144
|
+
}
|
|
145
|
+
const rawCell = isRecord(raw.cell) ? raw.cell : {}
|
|
146
|
+
for (const key of Object.keys(rawCell)) {
|
|
147
|
+
if (key !== 'width' && key !== 'height') diag.warn('unknown cell field ' + JSON.stringify(key) + ' ignored')
|
|
148
|
+
}
|
|
149
|
+
const cellWidth = finiteInt(rawCell.width, 1, DECORATION_CELL_MAX)
|
|
150
|
+
const cellHeight = finiteInt(rawCell.height, 1, DECORATION_CELL_MAX)
|
|
151
|
+
if (cellWidth === undefined || cellHeight === undefined) {
|
|
152
|
+
diag.error('cell width/height must be integers in [1, ' + DECORATION_CELL_MAX + ']')
|
|
153
|
+
}
|
|
154
|
+
const columns = finiteInt(raw.columns, 1, DECORATION_COLUMNS_MAX)
|
|
155
|
+
if (columns === undefined) {
|
|
156
|
+
diag.error('columns must be an integer in [1, ' + DECORATION_COLUMNS_MAX + ']')
|
|
157
|
+
}
|
|
158
|
+
if (diag.hasErrors || id === '' || entry === undefined || columns === undefined) {
|
|
159
|
+
return { ok: false, diagnostics: diag.list }
|
|
160
|
+
}
|
|
161
|
+
const displayName = typeof raw.displayName === 'string' && raw.displayName.trim() !== ''
|
|
162
|
+
? raw.displayName.trim().slice(0, DECORATION_DISPLAY_NAME_MAX)
|
|
163
|
+
: id
|
|
164
|
+
let loop: boolean
|
|
165
|
+
if (raw.loop === undefined || typeof raw.loop === 'boolean') {
|
|
166
|
+
loop = raw.loop ?? true
|
|
167
|
+
} else {
|
|
168
|
+
diag.warn('loop must be a boolean; defaulting to true')
|
|
169
|
+
loop = true
|
|
170
|
+
}
|
|
171
|
+
const rawDurations = raw.durations
|
|
172
|
+
let durations: number[]
|
|
173
|
+
if (Array.isArray(rawDurations)) {
|
|
174
|
+
const usable = rawDurations.filter((v): v is number => typeof v === 'number' && Number.isInteger(v) && v >= 1 && v <= DECORATION_DURATION_MAX_MS)
|
|
175
|
+
if (usable.length !== columns) {
|
|
176
|
+
diag.warn('durations length must equal columns; using the constant frameMs instead')
|
|
177
|
+
durations = []
|
|
178
|
+
} else {
|
|
179
|
+
durations = usable
|
|
180
|
+
}
|
|
181
|
+
} else if (rawDurations !== undefined) {
|
|
182
|
+
diag.warn('durations must be an array; using the constant frameMs instead')
|
|
183
|
+
durations = []
|
|
184
|
+
} else {
|
|
185
|
+
durations = []
|
|
186
|
+
}
|
|
187
|
+
if (durations.length === 0) {
|
|
188
|
+
const frameMs = finiteInt(raw.frameMs, 1, DECORATION_DURATION_MAX_MS) ?? 120
|
|
189
|
+
durations = Array.from({ length: columns }, () => frameMs)
|
|
190
|
+
}
|
|
191
|
+
const phases: PhaseBindings = {}
|
|
192
|
+
const rawPhases = raw.phases
|
|
193
|
+
if (isRecord(rawPhases)) {
|
|
194
|
+
for (const [key, value] of Object.entries(rawPhases)) {
|
|
195
|
+
if (!(PET_ACTIVITY_PHASES as readonly string[]).includes(key)) {
|
|
196
|
+
diag.warn('unknown phase ' + JSON.stringify(key) + '; binding ignored')
|
|
197
|
+
continue
|
|
198
|
+
}
|
|
199
|
+
const segment = normalizeSegment(value, columns, diag)
|
|
200
|
+
if (segment !== undefined) phases[key as ActivityPhase] = segment
|
|
201
|
+
}
|
|
202
|
+
} else if (rawPhases !== undefined) {
|
|
203
|
+
diag.warn('phases must be an object; all phases hide')
|
|
204
|
+
}
|
|
205
|
+
const visible = Object.values(phases).some(segment => segment !== 'hide')
|
|
206
|
+
if (!visible) diag.warn('no phase shows the ornament; the decoration stays hidden')
|
|
207
|
+
const manifest: DecorationManifest = {
|
|
208
|
+
decorationManifestVersion: 1,
|
|
209
|
+
id,
|
|
210
|
+
displayName,
|
|
211
|
+
license,
|
|
212
|
+
entry,
|
|
213
|
+
cell: { width: cellWidth!, height: cellHeight! },
|
|
214
|
+
columns,
|
|
215
|
+
durations,
|
|
216
|
+
loop,
|
|
217
|
+
phases,
|
|
218
|
+
}
|
|
219
|
+
return { ok: true, manifest, diagnostics: diag.list }
|
|
220
|
+
}
|
package/src/event-projection.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
|
|
14
14
|
import type { SessionEvent } from '@deepseek-ai/dsh-session'
|
|
15
15
|
import type { PetStateInput } from './state.ts'
|
|
16
|
-
import { StatusVoice, toolArgHint, WhisperEngine } from './chatter.ts'
|
|
16
|
+
import { StatusVoice, toolArgHint, WhisperEngine, type VoicePoolsProvider } from './chatter.ts'
|
|
17
17
|
|
|
18
18
|
/** Runtime shape of the optional legacy activity event. */
|
|
19
19
|
export interface ActivityStatusEventLike {
|
|
@@ -41,14 +41,19 @@ export interface PetActivityTransition {
|
|
|
41
41
|
whisper?: string
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
/**
|
|
45
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Fresh projection runtime for a newly seen session. The optional voice-pack
|
|
46
|
+
* provider (pet-center M4, issue #677) hands both chatter engines their
|
|
47
|
+
* pools; engines resolve overrides at draw time, so swapping the provider's
|
|
48
|
+
* pack re-voices live runtimes without rebuilding them.
|
|
49
|
+
*/
|
|
50
|
+
export function emptyProjectionRuntime(pools?: VoicePoolsProvider): ProjectionRuntime {
|
|
46
51
|
return {
|
|
47
52
|
activeTools: new Set(),
|
|
48
53
|
officialEventsSeen: false,
|
|
49
54
|
stepHadFailure: false,
|
|
50
|
-
voice: new StatusVoice(),
|
|
51
|
-
whispers: new WhisperEngine(),
|
|
55
|
+
voice: new StatusVoice(pools),
|
|
56
|
+
whispers: new WhisperEngine(pools),
|
|
52
57
|
}
|
|
53
58
|
}
|
|
54
59
|
|
package/src/index.ts
CHANGED
|
@@ -134,6 +134,7 @@ export function makePetSettingsSchema(fallbackPetId: string) {
|
|
|
134
134
|
bottom: z.number().step(1).min(0).max(DISPLAY_INSET_MAX).default(20),
|
|
135
135
|
petId: z.string().default(fallbackPetId),
|
|
136
136
|
enabled: z.boolean().default(true),
|
|
137
|
+
decorationEnabled: z.boolean().default(true),
|
|
137
138
|
})
|
|
138
139
|
}
|
|
139
140
|
|
|
@@ -163,6 +164,7 @@ function applyImpl(ctx: Context, config: PetConfig = {}): void {
|
|
|
163
164
|
bottom: service.display().bottom,
|
|
164
165
|
petId: service.selectedPetId(),
|
|
165
166
|
enabled: config.enabled ?? true,
|
|
167
|
+
decorationEnabled: config.decorationEnabled ?? true,
|
|
166
168
|
}
|
|
167
169
|
// The browser half talks to the pet through same-origin JSON endpoints and
|
|
168
170
|
// loads each pet's atlas from the registry's own media route (RPC domains
|