dsh-duet 0.1.10 → 0.2.1
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.md +19 -1
- package/assets/avatar/THREE-LICENSE.txt +21 -0
- package/assets/avatar/character.glb +0 -0
- package/assets/avatar/desktop/character.glb +0 -0
- package/assets/avatar/desktop/companion.mjs +18 -0
- package/assets/avatar/desktop/index.html +33 -0
- package/assets/avatar/desktop/manifest.json +10 -0
- package/assets/avatar/desktop/portrait.png +0 -0
- package/assets/avatar/desktop/renderer.mjs +4230 -0
- package/assets/avatar/portrait.png +0 -0
- package/assets/avatar/renderer.mjs +4230 -0
- package/desktop/bin/darwin-arm64/duet-avatar.app/Contents/Info.plist +36 -0
- package/desktop/bin/darwin-arm64/duet-avatar.app/Contents/MacOS/duet-avatar +0 -0
- package/desktop/bin/darwin-arm64/duet-avatar.app/Contents/Resources/duet companion.icns +0 -0
- package/desktop/bin/darwin-arm64/duet-avatar.app/Contents/_CodeSignature/CodeResources +128 -0
- package/desktop/bin/darwin-arm64/manifest.json +14 -0
- package/desktop/bin/win32-x64/duet-avatar.exe +0 -0
- package/desktop/bin/win32-x64/manifest.json +11 -0
- package/package.json +17 -5
- package/src/avatar/companion.html +33 -0
- package/src/avatar/companion.mjs +210 -0
- package/src/avatar/idle.mjs +9 -0
- package/src/avatar/lipsync.mjs +13 -0
- package/src/avatar/motion.mjs +93 -0
- package/src/avatar/renderer.mjs +90 -0
- package/src/avatar/surface.mjs +45 -0
- package/src/avatar/toolbar.mjs +49 -0
- package/src/client/audio.mjs +68 -3
- package/src/client/avatar-surfaces.mjs +83 -0
- package/src/client/avatar-tasks.mjs +68 -0
- package/src/client/avatar.mjs +218 -0
- package/src/client/browser.mjs +83 -35
- package/src/client/composer-bridge.mjs +5 -2
- package/src/client/connection-hint.mjs +2 -1
- package/src/client/consent.mjs +3 -1
- package/src/client/controls-layout.mjs +21 -9
- package/src/client/entry.js +26 -3
- package/src/client/feedback.mjs +19 -4
- package/src/client/identity.mjs +2 -2
- package/src/client/input-effect.mjs +51 -0
- package/src/client/notice.mjs +20 -4
- package/src/client/rpc.mjs +2 -1
- package/src/client/settings.html +124 -4
- package/src/client/transport.mjs +2 -2
- package/src/client/tutorial-adapter.mjs +4 -4
- package/src/client/tutorial.mjs +5 -3
- package/src/client/voices.mjs +137 -0
- package/src/host/assets.mjs +13 -0
- package/src/host/avatar-activity.mjs +24 -0
- package/src/host/avatar-assets.mjs +18 -0
- package/src/host/avatar.mjs +130 -0
- package/src/host/diagnostics.mjs +111 -7
- package/src/host/feedback-files.mjs +5 -1
- package/src/host/plugin.mjs +59 -4
- package/src/host/proxy.mjs +41 -6
- package/src/host/settings-migration.mjs +2 -1
- package/src/host/settings.mjs +19 -4
- package/src/host/voices.mjs +205 -0
- package/src/shared/avatar-activity.mjs +33 -0
- package/src/shared/avatar-menu-ui.mjs +85 -0
- package/src/shared/avatar-menu.mjs +83 -0
- package/src/shared/avatar-surface.mjs +24 -0
- package/src/shared/avatar-ui.mjs +91 -0
- package/src/shared/avatar.mjs +125 -0
- package/src/shared/nickname.mjs +13 -0
- package/src/shared/state.mjs +15 -4
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/** Local presentation only: never forwarded to the voice service. */
|
|
2
|
+
export const AVATAR_KEY = 'dsh.duet.avatar.v1'
|
|
3
|
+
export const AVATAR_SIZES = Object.freeze({large:1,medium:.75,small:.5})
|
|
4
|
+
export const avatarSize = value => Object.hasOwn(AVATAR_SIZES,value) ? value : 'large'
|
|
5
|
+
export const avatarScale = value => AVATAR_SIZES[avatarSize(value)]
|
|
6
|
+
export const AVATAR_DEFAULTS = Object.freeze({ enabled: true, webOnly: false, size:'large' })
|
|
7
|
+
|
|
8
|
+
// A suspended event loop cannot exchange heartbeats. Give it a fresh grace
|
|
9
|
+
// period on resume, while still expiring a silent peer during normal ticks.
|
|
10
|
+
export class AvatarHeartbeat {
|
|
11
|
+
constructor(interval=15000,now=Date.now(),timeout=60000){this.interval=interval;this.timeout=timeout;this.last=this.tick=now}
|
|
12
|
+
touch(now=Date.now()){this.last=now}
|
|
13
|
+
expired(now=Date.now()){
|
|
14
|
+
if(now-this.tick>this.interval*2||now<this.tick)this.last=now
|
|
15
|
+
this.tick=now
|
|
16
|
+
return now-this.last>this.timeout
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const AVATAR_PRESENTATION_LABELS=Object.freeze({
|
|
21
|
+
off:'当前显示:已关闭',desktop:'当前显示:桌面浮动窗口',
|
|
22
|
+
connecting:'当前显示:正在启动桌面窗口',recovering:'当前显示:桌面连接恢复中',
|
|
23
|
+
other:'当前显示:由另一个 DSH 页面管理桌面窗口',web:'当前显示:网页内',
|
|
24
|
+
fallback:'当前显示:网页内(桌面组件暂不可用)',unavailable:'当前显示:形象加载失败',
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
// TTS text is a cumulative prefix, not word timestamps. Bind its new suffix to
|
|
28
|
+
// the PCM chunk's playback interval; never use ahead-of-audio LLM text deltas.
|
|
29
|
+
export function captionCue(response, event) {
|
|
30
|
+
const text = typeof event.text === 'string' ? event.text.slice(0, 4000) : ''
|
|
31
|
+
const previous = response.captionSource || ''
|
|
32
|
+
const sameSegment = response.captionSegment === event.segment_id
|
|
33
|
+
const add = !text ? '' : sameSegment && text.startsWith(previous) ? text.slice(previous.length)
|
|
34
|
+
: sameSegment && previous.startsWith(text) ? '' : text
|
|
35
|
+
const cue = { from: response.captionText || '', add }
|
|
36
|
+
response.captionText = (cue.from + add).slice(-240)
|
|
37
|
+
if (text) { response.captionSource = text; response.captionSegment = event.segment_id }
|
|
38
|
+
return cue
|
|
39
|
+
}
|
|
40
|
+
export function captionAt(cue, fraction) {
|
|
41
|
+
if (!cue) return ''
|
|
42
|
+
const chars = Array.from(cue.add)
|
|
43
|
+
return (cue.from + chars.slice(0, Math.floor(chars.length * Math.max(0, Math.min(1, fraction)))).join('')).slice(-240)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Sample-clock subtitles. Late/duplicate metadata cannot rewind visible text. */
|
|
47
|
+
export class AudioCaptionTimeline {
|
|
48
|
+
constructor() { this.responses = new Map() }
|
|
49
|
+
ingestDelta(key, event, present) {
|
|
50
|
+
if (!Number.isSafeInteger(event.audio_sample_end) || event.audio_sample_end < 0) return
|
|
51
|
+
const state = this.responses.get(key) || {cues:[],shownEnd:-1,text:'',receivedText:'',lastReceivedEnd:-1,seen:new Set()}
|
|
52
|
+
state.seen ??= new Set()
|
|
53
|
+
if (event.audio_sample_end < (state.lastReceivedEnd ?? -1) ||
|
|
54
|
+
(event.event_id && state.seen.has(event.event_id))) return
|
|
55
|
+
const delta = event.type === 'response.output_text.delta' ? event.delta : ''
|
|
56
|
+
if (typeof delta !== 'string' || delta.length > 8000 || state.cues.length >= 1024) return
|
|
57
|
+
state.receivedText = ((state.receivedText || '') + delta).slice(0,8000)
|
|
58
|
+
state.lastReceivedEnd = event.audio_sample_end
|
|
59
|
+
if(event.event_id)state.seen.add(event.event_id)
|
|
60
|
+
while(state.seen.size>2048)state.seen.delete(state.seen.values().next().value)
|
|
61
|
+
const {full_text, text, ...safe} = event
|
|
62
|
+
state.cues.push({sample_end:event.audio_sample_end,text_prefix:state.receivedText,event:safe,present})
|
|
63
|
+
this.responses.set(key,state)
|
|
64
|
+
while(this.responses.size>16)this.responses.delete(this.responses.keys().next().value)
|
|
65
|
+
}
|
|
66
|
+
ingest(key, cues) {
|
|
67
|
+
if (!Array.isArray(cues)) return
|
|
68
|
+
const state = this.responses.get(key) || { cues: [], shownEnd: -1, text: '' }
|
|
69
|
+
for (const cue of cues.slice(0, 128)) {
|
|
70
|
+
if (!Number.isSafeInteger(cue?.sample_end) || cue.sample_end < 0 ||
|
|
71
|
+
typeof cue.text_prefix !== 'string' || cue.text_prefix.length > 8000 ||
|
|
72
|
+
cue.sample_end <= state.shownEnd) continue
|
|
73
|
+
if (!state.cues.some(c => c.sample_end === cue.sample_end)) state.cues.push(cue)
|
|
74
|
+
}
|
|
75
|
+
state.cues.sort((a,b)=>a.sample_end-b.sample_end)
|
|
76
|
+
// Bound future metadata without discarding the nearest playback markers.
|
|
77
|
+
state.cues = state.cues.slice(0,1024)
|
|
78
|
+
this.responses.set(key,state)
|
|
79
|
+
while (this.responses.size > 16) this.responses.delete(this.responses.keys().next().value)
|
|
80
|
+
}
|
|
81
|
+
text(key, playedSamples) {
|
|
82
|
+
const state = this.responses.get(key)
|
|
83
|
+
if (!state) return null
|
|
84
|
+
while (state.cues.length && state.cues[0].sample_end <= playedSamples) {
|
|
85
|
+
const cue = state.cues.shift()
|
|
86
|
+
if (cue.sample_end >= state.shownEnd && cue.text_prefix.startsWith(state.text)) {
|
|
87
|
+
state.shownEnd = cue.sample_end; state.text = cue.text_prefix
|
|
88
|
+
cue.present?.(cue.event)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return state.text.slice(-240)
|
|
92
|
+
}
|
|
93
|
+
clear() { this.responses.clear() }
|
|
94
|
+
discardPending(key) { const state=this.responses.get(key);if(state)state.cues=[] }
|
|
95
|
+
}
|
|
96
|
+
export function avatarPreferences(storage = globalThis.localStorage) {
|
|
97
|
+
try {
|
|
98
|
+
const value = JSON.parse(storage.getItem(AVATAR_KEY))
|
|
99
|
+
return { enabled: value?.enabled !== false, webOnly: value?.webOnly === true, size:avatarSize(value?.size) }
|
|
100
|
+
} catch { return { ...AVATAR_DEFAULTS } }
|
|
101
|
+
}
|
|
102
|
+
export function saveAvatarPreferences(value, storage = globalThis.localStorage) {
|
|
103
|
+
const clean = { enabled: value.enabled === true, webOnly: value.webOnly === true, size:avatarSize(value.size) }
|
|
104
|
+
storage.setItem(AVATAR_KEY, JSON.stringify(clean))
|
|
105
|
+
return clean
|
|
106
|
+
}
|
|
107
|
+
export function expressionFor(text, random = Math.random) {
|
|
108
|
+
if (/(哈哈|呵呵|笑死|好笑|开心)/u.test(text)) return 'laugh'
|
|
109
|
+
if (/(不行|不是|不能|不要|不对|不可以|没有|拒绝|^不[,。!!\s]*$)/u.test(text)) return 'shake'
|
|
110
|
+
if (/(好的|好嘞|收到|可以|明白|没问题|对的|是的|完成了|^好[,。!!\s]*$)/u.test(text)) return 'nod'
|
|
111
|
+
return ['glance_left', 'glance_right', 'tilt'][Math.min(2, Math.floor(random() * 3))]
|
|
112
|
+
}
|
|
113
|
+
export function avatarFrame(value) {
|
|
114
|
+
if (!value || !['off', 'online', 'tts_only'].includes(value.mode)) throw Error('invalid_avatar_frame')
|
|
115
|
+
if (!Number.isFinite(value.rms) || value.rms < 0 || value.rms > 1) throw Error('invalid_avatar_frame')
|
|
116
|
+
return { type: 'frame', mode: value.mode, ready: value.ready === true, rms: value.rms, size:avatarSize(value.size),
|
|
117
|
+
viseme: ['A','I','U','E','O'].includes(value.viseme) ? value.viseme : 'A',
|
|
118
|
+
text: typeof value.text === 'string' ? value.text.slice(-240) : '',
|
|
119
|
+
utterance: typeof value.utterance === 'string' ? value.utterance.slice(0, 100) : '' }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function avatarInputEffect(value) {
|
|
123
|
+
if(value?.type!=='input_effect'||typeof value.text!=='string'||!value.text||value.text.length>600)throw Error('invalid_avatar_input_effect')
|
|
124
|
+
return {type:'input_effect',text:value.text}
|
|
125
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Assistant nickname validation, shared by settings and voice actions. */
|
|
2
|
+
export const NICKNAME_LIMIT = '昵称最多5个汉字或2个英文单词;中英混用最多5个字符'
|
|
3
|
+
export function normalizeNickname(value) {
|
|
4
|
+
if (typeof value !== 'string') throw new Error(NICKNAME_LIMIT)
|
|
5
|
+
const name = value.trim()
|
|
6
|
+
const chinese = /^\p{Script=Han}{1,5}$/u.test(name)
|
|
7
|
+
const english = /^[A-Za-z]+(?:['-][A-Za-z]+)*(?: [A-Za-z]+(?:['-][A-Za-z]+)*)?$/.test(name)
|
|
8
|
+
const mixed = /\p{Script=Han}/u.test(name) && /[A-Za-z]/.test(name)
|
|
9
|
+
&& /^[\p{Script=Han}A-Za-z]+(?:[ '-][\p{Script=Han}A-Za-z]+)*$/u.test(name)
|
|
10
|
+
&& Array.from(name).length <= 5
|
|
11
|
+
if (!chinese && !english && !mixed) throw new Error(NICKNAME_LIMIT)
|
|
12
|
+
return name
|
|
13
|
+
}
|
package/src/shared/state.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Desired voice mode is independent of a temporarily suspended connection. */
|
|
2
|
-
export const PLUGIN_VERSION = '0.1
|
|
2
|
+
export const PLUGIN_VERSION = '0.2.1'
|
|
3
3
|
export const IDLE_RELEASE_MS = 60_000
|
|
4
4
|
|
|
5
5
|
const safeVersion = value => typeof value === 'string' && value.length <= 32 && /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.test(value) ? value : ''
|
|
@@ -75,12 +75,22 @@ export class DuetState {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
receive(event) {
|
|
78
|
-
if
|
|
78
|
+
if(event.type==='voice.unavailable'&&event.selected==='default'){
|
|
79
|
+
const message={voice_disabled:'此音色已被服务端停用,已切换为默认音色。',
|
|
80
|
+
voice_audio_missing:'参考音频已丢失,已使用默认音色。请到设置中重新上传原音频。',
|
|
81
|
+
voice_not_found:'此音色已不存在,已使用默认音色。请到设置中重新新增音色。'}[event.code]
|
|
82
|
+
if(message){this.notice(message);if(event.reconnect_required===true&&this.mode!=='off')void this.replaceConnection({reason:'voice_unavailable'})}
|
|
83
|
+
}else if(event.type==='voice.policy'&&event.enabled===false&&event.selected==='default'){
|
|
84
|
+
this.notice('自定义音色暂不可用,已切换为默认音色。')
|
|
85
|
+
if(event.reconnect_required===true&&this.mode!=='off')void this.replaceConnection({reason:'voice_policy_changed'})
|
|
86
|
+
} else if (event.type === 'compatibility') {
|
|
79
87
|
const recommended = safeVersion(event.compatibility?.recommended_client_version)
|
|
80
88
|
const newer = recommended && recommended.split('.').reduce((result, v, i) => result || Math.sign(Number(v) - Number(PLUGIN_VERSION.split('.')[i])), 0) > 0
|
|
81
89
|
if (newer && this.recommendationShown !== recommended) {
|
|
82
90
|
this.recommendationShown = recommended
|
|
83
|
-
this.notice(`当前插件 ${PLUGIN_VERSION},推荐更新到 ${recommended}。当前连接可以继续使用。`, {
|
|
91
|
+
this.notice(`当前插件 ${PLUGIN_VERSION},推荐更新到 ${recommended}。当前连接可以继续使用。`, {
|
|
92
|
+
upgrade: true, releaseNotes: event.compatibility?.recommended_release_notes,
|
|
93
|
+
})
|
|
84
94
|
}
|
|
85
95
|
} else if (event.type === 'ready') {
|
|
86
96
|
this.ready = true
|
|
@@ -103,7 +113,8 @@ export class DuetState {
|
|
|
103
113
|
fail(code, details = {}) {
|
|
104
114
|
const compatibility = compatibilityNotice(code, details, this.mode)
|
|
105
115
|
if (compatibility) {
|
|
106
|
-
this.notice(compatibility, { upgrade: code !== 'client_version_unsupported'
|
|
116
|
+
this.notice(compatibility, { upgrade: code !== 'client_version_unsupported',
|
|
117
|
+
releaseNotes: details.compatibility?.recommended_release_notes || details.policy?.recommended_release_notes })
|
|
107
118
|
void this.setMode('off', {reason:'error',code})
|
|
108
119
|
return
|
|
109
120
|
}
|