dsh-duet 0.1.10 → 0.2.0
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 +80 -33
- 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 +1 -0
- package/src/client/rpc.mjs +2 -1
- package/src/client/settings.html +78 -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/host/assets.mjs +12 -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 +39 -2
- package/src/host/proxy.mjs +13 -5
- package/src/host/settings-migration.mjs +2 -1
- package/src/host/settings.mjs +19 -4
- 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 +1 -1
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** Local-only presentation protocol. No remote URLs, credentials or executable payloads. */
|
|
2
|
+
export const AVATAR_LOCAL_PROTOCOL='avatar_local_v1'
|
|
3
|
+
export const NOTICE_KEY='dsh.duet.avatar.notices.v1'
|
|
4
|
+
export const MENU_ACTIONS=['open_dsh','pending','settings','tutorial','hide','notifications','size','bubble','view','dismiss']
|
|
5
|
+
const sizes=['small','medium','large'], statuses=['completed','failed','stopped','approval','question','plan_review']
|
|
6
|
+
export function noticePreferences(storage=globalThis.localStorage){
|
|
7
|
+
try {const p=JSON.parse(storage.getItem(NOTICE_KEY));return {enabled:p?.enabled!==false,snoozeUntil:0,bubble:sizes.includes(p?.bubble)?p.bubble:'medium'}}
|
|
8
|
+
catch{return {enabled:true,snoozeUntil:0,bubble:'medium'}}
|
|
9
|
+
}
|
|
10
|
+
export function saveNoticePreferences(p,storage=globalThis.localStorage){
|
|
11
|
+
const clean={enabled:p.enabled!==false,snoozeUntil:Number.isFinite(p.snoozeUntil)?Math.max(0,p.snoozeUntil):0,bubble:sizes.includes(p.bubble)?p.bubble:'medium'}
|
|
12
|
+
storage.setItem(NOTICE_KEY,JSON.stringify(clean));return clean
|
|
13
|
+
}
|
|
14
|
+
const text=(v,n)=>typeof v==='string'?v.slice(0,n):''
|
|
15
|
+
export function avatarMenuAction(v){
|
|
16
|
+
if(v?.type!=='menu_action'||!MENU_ACTIONS.includes(v.action)||!/^[a-zA-Z0-9-]{1,64}$/.test(v.request_id||''))throw Error('invalid_avatar_menu_action')
|
|
17
|
+
const out={type:'menu_action',request_id:v.request_id,action:v.action}
|
|
18
|
+
if(['size','bubble'].includes(v.action)){if(!sizes.includes(v.value))throw Error('invalid_avatar_size');out.value=v.value}
|
|
19
|
+
if(['view','dismiss'].includes(v.action)){if(typeof v.id!=='string'||!v.id||v.id.length>512)throw Error('invalid_avatar_notice');out.id=v.id}
|
|
20
|
+
if(v.action==='notifications'){if(typeof v.value!=='boolean')throw Error('invalid_avatar_preference');out.value=v.value}
|
|
21
|
+
if(v.action==='snooze'){if(![0,30].includes(v.value))throw Error('invalid_avatar_snooze');out.value=v.value}
|
|
22
|
+
return out
|
|
23
|
+
}
|
|
24
|
+
export function avatarLocalState(v){
|
|
25
|
+
if(v?.type!=='local_state'||v.protocol!==AVATAR_LOCAL_PROTOCOL||!Number.isSafeInteger(v.revision)||v.revision<0||!Array.isArray(v.items)||v.items.length>32)throw Error('invalid_avatar_local_state')
|
|
26
|
+
const items=v.items.map(i=>{
|
|
27
|
+
if(!statuses.includes(i.status)||!i.id||typeof i.id!=='string'||i.id.length>512)throw Error('invalid_avatar_notice')
|
|
28
|
+
return {id:i.id,session_id:text(i.session_id,256),name:text(i.name,100),status:i.status,summary:text(i.summary,240),pending:i.pending===true,unread:i.unread===true}
|
|
29
|
+
})
|
|
30
|
+
while(JSON.stringify(items).length>12000)items.pop()
|
|
31
|
+
return {type:'local_state',protocol:AVATAR_LOCAL_PROTOCOL,revision:v.revision,items,
|
|
32
|
+
size:sizes.includes(v.size)?v.size:'large',bubble:sizes.includes(v.bubble)?v.bubble:'medium',enabled:v.enabled!==false,
|
|
33
|
+
snoozed:v.snoozed===true,running:Math.max(0,Math.min(10000,Number(v.running)||0)),available:v.available===true,
|
|
34
|
+
toast:items.some(i=>i.id===v.toast)?v.toast:null}
|
|
35
|
+
}
|
|
36
|
+
export function avatarMenuResult(v){
|
|
37
|
+
if(v?.type!=='menu_result'||!/^[a-zA-Z0-9-]{1,64}$/.test(v.request_id||''))throw Error('invalid_avatar_menu_result')
|
|
38
|
+
return {type:'menu_result',request_id:v.request_id,ok:v.ok===true,message:text(v.message,180)}
|
|
39
|
+
}
|
|
40
|
+
/** Incremental snapshots, scope fencing and stable IDs; never infer success from text. */
|
|
41
|
+
export class AvatarNotices {
|
|
42
|
+
constructor(){this.items=new Map();this.seen=new Set();this.dismissed=new Set();this.scope=null;this.epoch=null;this.revision=0;this.order=0;this.toast=null;this.toastAt=0;this.running=0}
|
|
43
|
+
update({scope,epoch,sessions=[],results=[],interactions=[]}){
|
|
44
|
+
if(scope!==this.scope||epoch!==this.epoch){this.items.clear();this.seen.clear();this.dismissed.clear();this.toast=null;this.scope=scope;this.epoch=epoch}
|
|
45
|
+
const allowed=new Map(sessions.filter(s=>!s.archived&&s.origin!=='subagent').map(s=>[s.session_id,s]))
|
|
46
|
+
this.running=[...allowed.values()].filter(s=>s.running===true).length
|
|
47
|
+
for(const [id,item]of this.items)if(!allowed.has(item.session_id))this.items.delete(id)
|
|
48
|
+
const pending=new Set()
|
|
49
|
+
for(const p of interactions){
|
|
50
|
+
if(!allowed.has(p.session_id)||!['approval','question','plan_review'].includes(p.kind)||!p.id)continue
|
|
51
|
+
const id='pending:'+p.id;pending.add(id)
|
|
52
|
+
const row=allowed.get(p.session_id),old=this.items.get(id)
|
|
53
|
+
const summary=p.kind==='approval'?p.reason:(p.questions?.[0]?.question||p.questions?.[0]?.text||'请到 DSH 查看并回答')
|
|
54
|
+
this.items.set(id,{id,order:old?.order??++this.order,session_id:p.session_id,name:row.label,status:p.kind,summary:typeof summary==='string'?summary.slice(0,240):'',pending:true,unread:old?.unread??true})
|
|
55
|
+
if(!old){this.toast=id;this.toastAt=Date.now()}
|
|
56
|
+
}
|
|
57
|
+
for(const[id,item]of this.items)if(item.pending&&!pending.has(id))this.items.delete(id)
|
|
58
|
+
for(const job of results){
|
|
59
|
+
if(!allowed.has(job.session_id)||!Number.isSafeInteger(job.completion_seq)||typeof job.id!=='string')continue
|
|
60
|
+
const id=`job:${epoch}:${job.id}:${job.completion_seq}`;if(this.seen.has(id))continue
|
|
61
|
+
this.seen.add(id)
|
|
62
|
+
const status=job.turn_end_reason==='aborted'?'stopped':job.status==='failed'?'failed':job.status==='succeeded'&&job.turn_end_reason==='completed'?'completed':null
|
|
63
|
+
if(!status)continue
|
|
64
|
+
const row=allowed.get(job.session_id)
|
|
65
|
+
this.items.set(id,{id,order:++this.order,session_id:job.session_id,name:row.label,status,summary:status==='failed'?text(job.error,240):'',pending:false,unread:true})
|
|
66
|
+
this.toast=id;this.toastAt=Date.now()
|
|
67
|
+
}
|
|
68
|
+
while(this.seen.size>1024)this.seen.delete(this.seen.values().next().value)
|
|
69
|
+
const rows=[...this.items.values()].sort((a,b)=>Number(b.pending)-Number(a.pending)||Number(b.unread)-Number(a.unread)||b.order-a.order)
|
|
70
|
+
this.items=new Map(rows.slice(0,32).map(i=>[i.id,i]))
|
|
71
|
+
if(!this.items.has(this.toast))this.toast=null
|
|
72
|
+
for(const id of this.dismissed)if(!this.items.has(id))this.dismissed.delete(id)
|
|
73
|
+
this.revision++
|
|
74
|
+
}
|
|
75
|
+
read(id){const item=this.items.get(id);if(item)item.unread=false;if(this.toast===id)this.toast=null;this.revision++}
|
|
76
|
+
dismiss(id){if(this.items.has(id))this.dismissed.add(id);if(this.toast===id)this.toast=null;this.revision++}
|
|
77
|
+
state(prefs,size,available=true){
|
|
78
|
+
const pending=[...this.items.values()].find(i=>i.pending&&i.unread&&!this.dismissed.has(i.id))
|
|
79
|
+
const toast=pending?.id??(Date.now()-this.toastAt<=8000?this.toast:null)
|
|
80
|
+
return avatarLocalState({type:'local_state',protocol:AVATAR_LOCAL_PROTOCOL,revision:this.revision,items:[...this.items.values()],
|
|
81
|
+
...prefs,size,available,running:this.running,snoozed:prefs.snoozeUntil>Date.now(),toast:available&&prefs.enabled&&prefs.snoozeUntil<=Date.now()?toast:null})
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Bounded presentation-only IPC: never transfer DOM, HTML, tickets or callbacks. */
|
|
2
|
+
export const AVATAR_UI_PROTOCOL = 2
|
|
3
|
+
export function avatarSurface(value) {
|
|
4
|
+
if(value?.type!=='surface')throw Error('invalid_surface')
|
|
5
|
+
if(value.panel===null)return {type:'surface',panel:null}
|
|
6
|
+
const p=value.panel
|
|
7
|
+
if(!p||!/^s\d+$/.test(p.id)||!Number.isSafeInteger(p.revision)||p.revision<1||!Array.isArray(p.nodes)||p.nodes.length>160)throw Error('invalid_surface')
|
|
8
|
+
let length=0
|
|
9
|
+
const nodes=p.nodes.map(n=>{
|
|
10
|
+
if(!n||!['text','heading','button','checkbox','break'].includes(n.kind)||typeof n.text!=='string'||n.text.length>4000)throw Error('invalid_surface')
|
|
11
|
+
length+=n.text.length;if(length>10000)throw Error('invalid_surface')
|
|
12
|
+
const node={kind:n.kind,text:n.text}
|
|
13
|
+
if(n.kind==='button'||n.kind==='checkbox') {
|
|
14
|
+
if(!/^a\d+$/.test(n.action))throw Error('invalid_surface')
|
|
15
|
+
Object.assign(node,{action:n.action,disabled:n.disabled===true,checked:n.checked===true,gesture:n.gesture===true,label:String(n.label||'').slice(0,100)})
|
|
16
|
+
}
|
|
17
|
+
return node
|
|
18
|
+
})
|
|
19
|
+
return {type:'surface',panel:{id:p.id,revision:p.revision,nodes,target:['online','tts_only'].includes(p.target)?p.target:null}}
|
|
20
|
+
}
|
|
21
|
+
export function avatarSurfaceAction(v) {
|
|
22
|
+
if(v?.type!=='surface_action'||!/^s\d+$/.test(v.id)||!/^a\d+$/.test(v.action)||!Number.isSafeInteger(v.revision)||v.revision<1)throw Error('invalid_surface_action')
|
|
23
|
+
return {type:'surface_action',id:v.id,revision:v.revision,action:v.action,checked:v.checked===true}
|
|
24
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import {avatarSize,AVATAR_SIZES} from './avatar.mjs'
|
|
2
|
+
const sizes=Object.keys(AVATAR_SIZES),names={large:'大(100%)',medium:'中(75%)',small:'小(50%)'}
|
|
3
|
+
export const nextAvatarSize=value=>sizes[(sizes.indexOf(avatarSize(value))+1)%sizes.length]
|
|
4
|
+
export function sizeButton(button,value,{label=false}={}) {
|
|
5
|
+
const size=avatarSize(value),title=`形象大小:${names[size]},点击切换为${names[nextAvatarSize(size)]}`
|
|
6
|
+
button.type='button';button.dataset.size=size;button.setAttribute('aria-label',title);button.title=title
|
|
7
|
+
const squares=sizes.filter(s=>s!==size).concat(size).map(s=>{
|
|
8
|
+
const side={large:18,medium:12,small:6}[s]
|
|
9
|
+
return `<rect data-size-square="${s}" x="3" y="${21-side}" width="${side}" height="${side}" stroke-width="${s===size?'2.8':'1'}"/>`
|
|
10
|
+
}).join('')
|
|
11
|
+
button.innerHTML=`<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" aria-hidden="true">${squares}</svg>${label?`<span>${names[size]}</span>`:''}`
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Same local confirmation in settings, web and the native companion. */
|
|
15
|
+
export function avatarCloseConfirmation({onChange=()=>{},floating=false,origin='page',anchor=()=>null,viewport=()=>[0,0,innerWidth,innerHeight]}={}) {
|
|
16
|
+
let pending=null,resolve,node,previous,dead=false
|
|
17
|
+
const position=()=>{
|
|
18
|
+
if(!floating||!node)return
|
|
19
|
+
const r=anchor()?.getBoundingClientRect();if(!r)return
|
|
20
|
+
const b=node.getBoundingClientRect()
|
|
21
|
+
const [x,y,w,h]=viewport()
|
|
22
|
+
const top=r.top-b.height-16>=y+12?r.top-b.height-16:r.bottom+12
|
|
23
|
+
node.style.left=Math.max(x+12,Math.min(r.left+r.width/2-b.width/2,x+w-b.width-12))+'px'
|
|
24
|
+
node.style.top=Math.max(y+12,Math.min(top,y+h-b.height-12))+'px'
|
|
25
|
+
node.style.bottom='auto'
|
|
26
|
+
}
|
|
27
|
+
const finish=accepted=>{
|
|
28
|
+
if(!pending)return
|
|
29
|
+
const done=resolve;pending=null;resolve=null
|
|
30
|
+
document.removeEventListener('keydown',keys,true);window.removeEventListener('resize',position);node?.remove();node=null
|
|
31
|
+
if(previous?.isConnected)previous.focus({preventScroll:true})
|
|
32
|
+
onChange();done(accepted)
|
|
33
|
+
}
|
|
34
|
+
const keys=e=>{
|
|
35
|
+
if(e.key==='Escape'){e.preventDefault();e.stopImmediatePropagation();finish(false)}
|
|
36
|
+
else if(e.key==='Tab'&&!floating){
|
|
37
|
+
e.preventDefault();const buttons=[...node.querySelectorAll('button')],index=buttons.indexOf(document.activeElement)
|
|
38
|
+
buttons[(index+(e.shiftKey?-1:1)+buttons.length)%buttons.length].focus()
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
ask(){
|
|
43
|
+
if(dead)return Promise.resolve(false)
|
|
44
|
+
if(pending)return pending
|
|
45
|
+
previous=document.activeElement
|
|
46
|
+
pending=new Promise(r=>resolve=r)
|
|
47
|
+
node=document.createElement('div');node.dataset.avatarCloseOverlay=''
|
|
48
|
+
node.style.cssText='position:fixed;inset:0;z-index:2147483600;display:flex;align-items:center;justify-content:center;padding:12px;box-sizing:border-box;background:#0003;pointer-events:auto'
|
|
49
|
+
if(floating){node.dataset.duetAvatarSurface='110';node.dataset.duetSurfaceOrigin=origin;node.style.cssText='position:fixed;left:12px;bottom:100px;width:260px;max-width:calc(100vw - 24px);z-index:2147483600;pointer-events:auto'}
|
|
50
|
+
const panel=document.createElement('div');panel.dataset.avatarCloseConfirm=''
|
|
51
|
+
panel.setAttribute('role','alertdialog');panel.setAttribute('aria-modal',String(!floating));panel.setAttribute('aria-label','隐藏拟人角色?')
|
|
52
|
+
panel.style.cssText='box-sizing:border-box;width:260px;max-width:100%;padding:16px;border:1px solid #8dc5ed55;border-radius:16px;background:#142638;color:#e9f4ff;font:13px/1.6 system-ui;box-shadow:0 8px 28px #0005'
|
|
53
|
+
const title=document.createElement('div');title.textContent='隐藏拟人角色?';title.style.cssText='font-size:14px;font-weight:600;margin-bottom:14px'
|
|
54
|
+
const row=document.createElement('div');row.style.cssText='display:flex;justify-content:flex-end;gap:8px'
|
|
55
|
+
for(const [text,accept] of [['取消',false],['隐藏拟人角色',true]]){
|
|
56
|
+
const button=document.createElement('button');button.type='button';button.textContent=text;button.dataset.avatarCloseChoice=accept?'confirm':'cancel'
|
|
57
|
+
button.style.cssText=`font:13px system-ui;display:block;width:auto;min-width:56px;min-height:32px;padding:6px 10px;border:0;border-radius:8px;cursor:pointer;background:${accept?'#dcefff':'#294154'};color:${accept?'#185a89':'#e9f4ff'}`
|
|
58
|
+
button.onclick=()=>finish(accept);row.append(button)
|
|
59
|
+
}
|
|
60
|
+
panel.append(title,row);node.append(panel);document.body.append(node)
|
|
61
|
+
document.addEventListener('keydown',keys,true);window.addEventListener('resize',position);position();row.firstChild.focus();onChange()
|
|
62
|
+
return pending
|
|
63
|
+
},
|
|
64
|
+
cancel(){finish(false)},
|
|
65
|
+
dispose(){dead=true;finish(false)},
|
|
66
|
+
get element(){return node},
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The real playback-page click stays here; native projection cannot grant activation. */
|
|
71
|
+
export function audioUnlockPanel({onEnable,onCancel}) {
|
|
72
|
+
const panel=document.createElement('section')
|
|
73
|
+
panel.dataset.duetAvatarSurface='95';panel.dataset.duetSurfaceOrigin='avatar';panel.dataset.duetAudioUnlock=''
|
|
74
|
+
panel.setAttribute('role','region');panel.setAttribute('aria-label','启用 duet 声音')
|
|
75
|
+
panel.style.cssText='position:fixed;z-index:100030;right:12px;bottom:88px;box-sizing:border-box;width:320px;max-width:calc(100vw - 24px);max-height:calc(100vh - 100px);overflow:auto;padding:22px;border-radius:20px;background:linear-gradient(145deg,#20364b,#142333);color:#edf6ff;border:1px solid #8dc5ed55;font:14px/1.65 system-ui,sans-serif;box-shadow:0 16px 48px #07152240;isolation:isolate;text-align:left'
|
|
76
|
+
const style=document.createElement('style')
|
|
77
|
+
style.textContent='[data-duet-audio-unlock] button:hover{filter:brightness(1.12)}[data-duet-audio-unlock] button:focus-visible{outline:2px solid #a4dcff;outline-offset:3px}[data-duet-audio-unlock] button:active{transform:translateY(1px)}'
|
|
78
|
+
const brand=document.createElement('div');brand.setAttribute('aria-hidden','true');brand.textContent='duet';brand.style.cssText='font:750 25px/1.2 system-ui;letter-spacing:-1px;margin:0 0 17px;color:#edf6ff'
|
|
79
|
+
const dot=document.createElement('span');dot.textContent='.';dot.style.color='#ed9b35';brand.append(dot)
|
|
80
|
+
const title=document.createElement('h3');title.textContent='音频播放权限';title.style.cssText='font:650 18px/1.4 system-ui;margin:0 0 8px;color:#edf6ff'
|
|
81
|
+
const text=document.createElement('p');text.textContent='点击启动音频播放权限。';text.style.cssText='font:14px/1.75 system-ui;margin:0;color:#bdcedf;white-space:normal;overflow-wrap:anywhere'
|
|
82
|
+
const actions=document.createElement('div');actions.style.cssText='display:flex;flex-wrap:wrap;justify-content:flex-end;gap:10px;margin-top:22px'
|
|
83
|
+
for(const [label,primary,callback] of [['暂不开启',false,onCancel],['启动音频播放',true,onEnable]]){
|
|
84
|
+
const button=document.createElement('button');button.type='button';button.textContent=label
|
|
85
|
+
button.style.cssText='appearance:none;box-sizing:border-box;display:inline-flex;align-items:center;justify-content:center;min-height:40px;padding:9px 16px;border-radius:11px;font:600 14px/1.4 system-ui;cursor:pointer;white-space:nowrap;'+(primary?'background:#dcefff;color:#185a89;border:1px solid #dcefff;box-shadow:0 3px 10px #0002':'background:#ffffff08;color:#c2d3e3;border:1px solid #8dc5ed40')
|
|
86
|
+
if(primary)button.dataset.duetUserGesture=''
|
|
87
|
+
button.onclick=callback;actions.append(button)
|
|
88
|
+
}
|
|
89
|
+
panel.append(style,brand,title,text,actions)
|
|
90
|
+
return panel
|
|
91
|
+
}
|
|
@@ -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.
|
|
2
|
+
export const PLUGIN_VERSION = '0.2.0'
|
|
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 : ''
|