@scienjoy/sj-ai-plus 1.0.7 → 1.0.8

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.
@@ -1 +1 @@
1
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("./index.vue2.ts");;/* empty css */const t=require("../../../../../_virtual/_plugin-vue_export-helper.ts"),u=t.default(e.default,[["__scopeId","data-v-bdffed0d"]]);exports.default=u;
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("./index.vue2.ts");;/* empty css */const t=require("../../../../../_virtual/_plugin-vue_export-helper.ts"),u=t.default(e.default,[["__scopeId","data-v-0e24153b"]]);exports.default=u;
@@ -1 +1 @@
1
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("vue"),J=require("../../utils/api.ts"),Y=require("../../utils/utils.ts"),Ve=require("../../utils/videoBackgroundRemoval.ts"),Ce={class:"digital-human-assistant"},Te={class:"placeholder left-placeholder"},Re={class:"placeholder right-placeholder"},_e={class:"window-header-left"},Ne={class:"window-title-text"},We={class:"window-header-btns"},xe={class:"disconnect-btn"},Me={class:"connect-btn",disabled:""},De={class:"connect-btn"},Le={class:"window-content"},He={class:"main-content-inner"},Ie={class:"window-header-left"},Pe={class:"window-title-text"},Be={class:"window-header-btns"},$e={class:"disconnect-btn"},Xe={class:"connect-btn",disabled:""},Ye={class:"connect-btn"},Ue={class:"window-content"},ze={class:"window-header-left"},Oe={class:"window-title-text"},je={class:"window-header-btns"},Ge={class:"disconnect-btn"},Fe={class:"connect-btn",disabled:""},qe={class:"connect-btn"},Ae={class:"window-content"},Je={key:0,class:"digital-video-loading"},Ke=e.defineComponent({name:"SjDigitalHumanAssistant"}),Qe=e.defineComponent({...Ke,props:{windowWidth:{default:320},windowHeight:{default:467},windowTitle:{default:"数字人助手"},sidebarWidth:{default:"25%"},sidebarBgColor:{default:"#ffffff"},edgeThreshold:{default:10},dragThreshold:{default:8},defaultStickSide:{default:""},defaultPlayText:{default:""},reconnectInterval:{default:5},maxReconnectTimes:{default:10},baseUrl:{default:""}},setup(N,{expose:fe}){e.useCssVars(t=>({f2c09f26:t.sidebarBgColor}));const a=N,K=e.ref(null),Q=e.ref(null),l=e.ref(null),o=e.ref(null),Z=e.ref(null),ee=e.ref(null),te=e.ref(null),W=e.ref(null),r=Ve.default(),S=e.ref(!1),s=e.ref(!1),v=e.ref(!1),V=e.ref(!1),ne=e.ref(!0),x=e.ref(!1),f=e.ref(a.defaultStickSide),M=e.ref(!1),g=e.ref(!1),C=e.ref(!1),U=e.ref(!1),z=e.ref(!1);let d=null,w="";const D=e.ref(null),m=e.ref(0),L=e.ref(!1),T=e.ref(!1),u=e.reactive({x:0,y:0,offsetX:0,offsetY:0}),he=e.computed(()=>({"--sidebar-width":a.sidebarWidth,"--window-width":`${a.windowWidth}px`,"--window-height":`${a.windowHeight}px`})),we=e.computed(()=>({left:`${u.x}px`,top:`${u.y}px`,width:`${a.windowWidth}px`,height:`${a.windowHeight}px`,zIndex:M.value||C.value?9999:1e3})),me=t=>{!t&&l.value?(L.value=!1,m.value=0,de()):t?(L.value=!0,ke()):console.warn("❌ 自动连接超时:video元素未挂载"),ne.value=t},H=t=>{me(!t),x.value=t,t&&s.value?e.nextTick(()=>h()):t||(V.value=!1)},ge=()=>{H(!0)},oe=()=>{switch(f.value){case"left":return ee.value??W.value;case"right":return te.value??W.value;default:return Z.value??W.value}},I=()=>Q.value?.getBoundingClientRect()||{width:0,height:0,left:0,right:0,top:0,bottom:0},ae=()=>K.value?.getBoundingClientRect()||{width:0,height:0,left:0,right:0,top:0,bottom:0},O=(t,n)=>{const c=window.innerWidth||document.documentElement.clientWidth,i=window.innerHeight||document.documentElement.clientHeight;return{x:Math.max(0,Math.min(t,c-a.windowWidth)),y:Math.max(0,Math.min(n,i-a.windowHeight))}},ie=()=>{const t=window.innerWidth||document.documentElement.clientWidth,n=window.innerHeight||document.documentElement.clientHeight,c=t-a.windowWidth-10,i=n-a.windowHeight-10;return O(c,i)},le=()=>{const t=I(),n=ae();U.value=n.left<=t.left+a.edgeThreshold,z.value=n.right>=t.right-a.edgeThreshold},se=()=>{const t=I(),n=ae(),c=n.left<=t.left+a.edgeThreshold?"left":n.right>=t.right-a.edgeThreshold?"right":"";c!==f.value&&(f.value=c,l.value&&(l.value.volume=c?.5:1),R(),h()),U.value=z.value=!1},pe=()=>{V.value=!0},be=()=>{V.value=!1},ye=t=>{M.value=!0,u.offsetX=t.clientX-u.x,u.offsetY=t.clientY-u.y,document.addEventListener("mousemove",j),document.addEventListener("mouseup",G)},j=t=>{if(!M.value)return;const n=I(),{x:c,y:i}=O(t.clientX-n.left-u.offsetX,t.clientY-n.top-u.offsetY);u.x=c,u.y=i,le()},G=()=>{se(),M.value=!1,document.removeEventListener("mousemove",j),document.removeEventListener("mouseup",G)},ce=(t,n)=>{g.value=!0,u.offsetX=n.clientX-(n.clientX-a.windowWidth/2),u.offsetY=n.clientY-(n.clientY-20),document.addEventListener("mousemove",P)},P=t=>{if(!g.value)return;const n=Math.abs(t.movementX),c=Math.abs(t.movementY);(n>a.dragThreshold||c>a.dragThreshold)&&(C.value=!0,f.value="",document.removeEventListener("mousemove",P),document.addEventListener("mousemove",F),document.addEventListener("mouseup",q))},F=t=>{if(!C.value)return;const n=I(),{x:c,y:i}=O(t.clientX-n.left-u.offsetX,t.clientY-n.top-u.offsetY);u.x=c,u.y=i,le()},q=()=>{se(),g.value=C.value=!1,document.removeEventListener("mousemove",F),document.removeEventListener("mouseup",q)},B=()=>{g.value&&!C.value&&(g.value=!1,document.removeEventListener("mousemove",P))},A=async t=>{if(!s.value||!w)return;await E(),console.log("✅ 准备播放文本:",t);const n={text:t,type:"echo",interrupt:!0,sessionid:parseInt(w)};J.default.digitalHumanChat(a.baseUrl,n).then(c=>{c&&Number(c.code)===0&&console.log("✅ 播放指令发送成功")}).catch(c=>{console.error("❌ 播放指令发送失败:",c)})},E=async()=>{if(!s.value||!w)return;const t={sessionid:parseInt(w)};J.default.digitalHumanInterruptTalk(a.baseUrl,t).then(n=>{n&&Number(n.code)===0&&console.log("✅ 对话已打断")}).catch(n=>{console.error("❌ 打断对话失败:",n)})},k=()=>{D.value&&(clearTimeout(D.value),D.value=null),T.value=!1,console.log("✅ 重连定时器已清除")},R=(t=!1)=>{if(!o.value)return;t||r.stopProcessing();const n=o.value.getContext("2d");if(n&&(n.clearRect(0,0,o.value.width,o.value.height),!t)){const c=o.value.width,i=o.value.height;o.value.width=c,o.value.height=i}o.value&&!t&&(o.value.style.width="",o.value.style.height="",f.value===""&&(o.value.style.marginLeft="",o.value.style.marginTop=""))},h=async(t=!1)=>{if(!l.value||!o.value||!S.value||!s.value)return;await e.nextTick();const n=l.value.videoWidth||a.windowWidth,c=l.value.videoHeight||a.windowHeight,i=n/c;let p;const ue=oe();ue?p=ue.getBoundingClientRect():p={width:0,height:0,left:0,top:0,right:0,bottom:0};const $=p.width||(f.value?300:a.windowWidth),X=p.height||(f.value?window.innerHeight-40:a.windowHeight),Se=$/X;let b,y;Se>i?(y=X,b=y*i):(b=$,y=b/i);const ve=window.devicePixelRatio||1;o.value.width=Math.floor(b*ve),o.value.height=Math.floor(y*ve),o.value.style.display="block",o.value.style.objectFit="contain",f.value===""?(o.value.style.width=`${b}px`,o.value.style.height=`${y}px`,o.value.style.marginLeft=`${($-b)/2}px`,o.value.style.marginTop=`${(X-y)/2}px`):(o.value.style.width="100%",o.value.style.height="100%",o.value.style.marginLeft="0",o.value.style.marginTop="0"),console.log("✅ Canvas适配完成:",{mode:f.value||"浮动",container:{width:$,height:X},canvas:{drawWidth:b,drawHeight:y}}),t||(r.videoRef.value=l.value,r.canvasRef.value=o.value,await r.processVideo()),!r.isInitSuccess.value&&s.value&&(console.log("🔄 检测到WebGPU未初始化,触发手动重连"),await r.reInitWebGPU()&&await r.processVideo())},_=()=>{if(L.value||s.value||v.value||T.value){console.log("🚫 重连条件不满足");return}if(E(),a.maxReconnectTimes!==-1&&m.value>=a.maxReconnectTimes){console.warn(`🚫 重连已达最大次数(${a.maxReconnectTimes})`),k(),Y.default.funcElMessageShowCtrl(`❌ 数字人连接失败,已尝试${a.maxReconnectTimes}次重连,请重试!`,"error"),m.value=0;return}T.value=!0,D.value=setTimeout(()=>{try{m.value++,console.log(`🔄 第${m.value}次重连WebRTC...`),r.stopProcessing(),de()}finally{T.value=!1}},a.reconnectInterval*1e3)},Ee=async()=>{if(d)try{d.addTransceiver("video",{direction:"recvonly"}),d.addTransceiver("audio",{direction:"recvonly"});const t=await d.createOffer();await d.setLocalDescription(t),await new Promise(i=>{if(d?.iceGatheringState==="complete")i();else{const p=()=>{d?.iceGatheringState==="complete"&&(d?.removeEventListener("icegatheringstatechange",p),i())};d?.addEventListener("icegatheringstatechange",p)}});const n=d.localDescription,c={sdp:n.sdp,type:n.type};J.default.digitalHumanOffer(a.baseUrl,c).then(i=>{i?(w=i.sessionid,d.setRemoteDescription(new RTCSessionDescription(i)),s.value=!0,m.value=0,k(),console.log("✅ WebRTC连接成功"),e.nextTick(()=>h())):(s.value=!1,console.log("❌ WebRTC连接失败:接口返回空"),_()),v.value=!1}).catch(i=>{console.error("❌ WebRTC Offer发送失败:",i),s.value=!1,v.value=!1,_()})}catch(t){console.error("❌ WebRTC协商失败:",t),s.value=!1,v.value=!1,_()}},de=()=>{if(s.value||v.value)return;v.value=!0,console.log("🔄 开始建立WebRTC连接..."),d&&(d.close(),d=null);const t={sdpSemantics:"unified-plan"};d=new RTCPeerConnection(t),d.addEventListener("track",n=>{n.track.kind==="video"&&l.value&&(l.value.srcObject=n.streams[0],l.value.onloadedmetadata=async()=>{l.value&&o.value&&(r.videoRef.value=l.value,r.canvasRef.value=o.value,await h(),await r.processVideo())},l.value.onresize=()=>{h(!0)})}),d.addEventListener("iceconnectionstatechange",()=>{switch(console.log("📶 ICE状态变化:",d.iceConnectionState),d.iceConnectionState){case"disconnected":case"failed":case"closed":console.log("📶 ICE连接断开,触发重连"),s.value=!1,v.value=!1,d=null,w="",l.value&&(l.value.srcObject=null),R(),E(),_();break;case"connected":console.log("📶 ICE连接成功"),m.value=0,k(),e.nextTick(async()=>{!r.isInitSuccess.value&&s.value&&await r.reInitWebGPU()&&await r.processVideo()});break}}),Ee()},ke=()=>{L.value=!0,k(),v.value=!1,r.stopProcessing(),R(),E().then(()=>{setTimeout(()=>{d&&(d.close(),d=null),w="",l.value&&(l.value.srcObject=null),s.value=!1,m.value=0,console.log("✅ 手动断开连接,资源已清理")},500)})},re=Y.default.debounce(()=>{if(f.value===""){const{x:t,y:n}=ie();u.x=t,u.y=n}s.value&&S.value&&(R(!0),e.nextTick(()=>h(!0)))},50);return e.watch(r.isInitSuccess,t=>{!t&&v.value&&Y.default.funcElMessageShowCtrl("🔄 WebGPU初始化失败,正在尝试重连...","warning")},{immediate:!0}),e.watch([()=>r.isInitSuccess,()=>s.value],async([t,n])=>{!t&&n&&l.value&&o.value?(o.value.style.display="none",l.value.style.opacity="1",l.value.style.width="100%",l.value.style.height="100%",Y.default.funcElMessageShowCtrl("ℹ️ 当前浏览器不支持WebGPU,将显示原始视频(无透明背景)","info")):t&&n&&l.value&&o.value&&(o.value.style.display="block",l.value.style.opacity="0")},{immediate:!0}),e.watch(f,async t=>{!S.value||!l.value||(await e.nextTick(),l.value.volume=t?.5:1,s.value&&(R(),await h()))},{immediate:!0,deep:!0}),e.watch([()=>s.value,()=>a.defaultPlayText],async([t,n],[c])=>{t&&!c&&n&&w&&(console.log("✅ 连接成功,自动播放默认文本:",n),await A(n),e.nextTick(()=>h()))},{immediate:!1}),e.watch(()=>a.defaultPlayText,async t=>{s.value&&t&&w&&(console.log("✅ 默认文本更新,自动播放:",t),await A(t),e.nextTick(()=>h()))},{immediate:!0}),e.onMounted(async()=>{await e.nextTick(),await e.nextTick(),S.value=!0;const{x:t,y:n}=ie();u.x=t,u.y=n,window.addEventListener("resize",re)}),e.onUnmounted(()=>{document.removeEventListener("mousemove",P),document.removeEventListener("mousemove",F),document.removeEventListener("mouseup",q),document.removeEventListener("mousemove",j),document.removeEventListener("mouseup",G),window.removeEventListener("resize",re),k(),r.stopProcessing(),E(),d&&d.close(),l.value&&(l.value.srcObject=null)}),fe({digitalHumanChat:A,digitalHumanInterruptTalk:E,isConnected:s,isConnecting:v,reconnect:_,clearReconnectTimer:k,currentReconnectTimes:m,isWaitingReconnect:T,adaptCanvasToContainer:h,webgpuReInit:r.reInitWebGPU,webgpuIsInitSuccess:r.isInitSuccess,webgpuIsProcessing:r.isProcessing}),(t,n)=>{const c=e.resolveDirective("drag");return e.openBlock(),e.createElementBlock("div",{class:"draggable-window-container",style:e.normalizeStyle(he.value)},[e.withDirectives(e.createElementVNode("div",Ce,null,512),[[e.vShow,ne.value],[c,["digital-human-assistant",ge,!0]]]),e.createElementVNode("div",{ref_key:"teleportFallbackRef",ref:W,class:"teleport-fallback"},null,512),e.withDirectives(e.createElementVNode("div",Te,null,512),[[e.vShow,U.value]]),e.withDirectives(e.createElementVNode("div",Re,null,512),[[e.vShow,z.value]]),x.value&&f.value==="left"?(e.openBlock(),e.createElementBlock("div",{key:0,class:e.normalizeClass(["sidebar left-sidebar",{pressing:g.value}])},[e.createElementVNode("div",{class:"window-header",onMousedown:n[2]||(n[2]=i=>ce("left",i)),onMouseup:B,onMouseleave:B},[e.createElementVNode("div",_e,[e.createElementVNode("span",Ne,e.toDisplayString(N.windowTitle),1),e.createElementVNode("div",We,[e.withDirectives(e.createElementVNode("button",xe,"未连接",512),[[e.vShow,!s.value&&!v.value]]),e.withDirectives(e.createElementVNode("button",Me,"连接中...",512),[[e.vShow,v.value]]),e.withDirectives(e.createElementVNode("button",De,"已连接",512),[[e.vShow,s.value]])])]),e.createElementVNode("div",{class:"drag-window-hide",onClick:n[0]||(n[0]=i=>H(!1)),onMousedown:n[1]||(n[1]=e.withModifiers(()=>{},["stop"]))},null,32)],32),e.createElementVNode("div",Le,[e.renderSlot(t.$slots,"sidebar-left",{},void 0,!0),e.createElementVNode("div",{ref_key:"sidebarLeftVideoRef",ref:ee,class:"sidebar-video-container"},null,512)])],2)):e.createCommentVNode("",!0),e.createElementVNode("div",{class:"main-content-outer",ref_key:"containerRef",ref:Q},[e.createElementVNode("div",He,[e.renderSlot(t.$slots,"main",{},void 0,!0)]),x.value&&f.value===""?(e.openBlock(),e.createElementBlock("div",{key:0,ref_key:"dragWindowRef",ref:K,class:e.normalizeClass(["draggable-window",{"draggable-window-shadow":!s.value||V.value}]),style:e.normalizeStyle(we.value),onMouseenter:pe,onMouseleave:be},[e.createElementVNode("div",{class:e.normalizeClass(["window-header",{"window-header-opacity":s.value&&!V.value}]),onMousedown:ye},[e.createElementVNode("div",Ie,[e.createElementVNode("span",Pe,e.toDisplayString(N.windowTitle),1),e.createElementVNode("div",Be,[e.withDirectives(e.createElementVNode("button",$e,"未连接",512),[[e.vShow,!s.value&&!v.value]]),e.withDirectives(e.createElementVNode("button",Xe,"连接中...",512),[[e.vShow,v.value]]),e.withDirectives(e.createElementVNode("button",Ye,"已连接",512),[[e.vShow,s.value]])])]),e.createElementVNode("div",{class:"drag-window-hide",onClick:n[3]||(n[3]=i=>H(!1)),onMousedown:n[4]||(n[4]=e.withModifiers(()=>{},["stop"]))},null,32)],34),e.createElementVNode("div",Ue,[e.createElementVNode("div",{class:"video-container",ref_key:"windowVideoRef",ref:Z},null,512)])],38)):e.createCommentVNode("",!0)],512),x.value&&f.value==="right"?(e.openBlock(),e.createElementBlock("div",{key:1,class:e.normalizeClass(["sidebar right-sidebar",{pressing:g.value}])},[e.createElementVNode("div",{class:"window-header",onMousedown:n[7]||(n[7]=i=>ce("right",i)),onMouseup:B,onMouseleave:B},[e.createElementVNode("div",ze,[e.createElementVNode("span",Oe,e.toDisplayString(N.windowTitle),1),e.createElementVNode("div",je,[e.withDirectives(e.createElementVNode("button",Ge,"未连接",512),[[e.vShow,!s.value&&!v.value]]),e.withDirectives(e.createElementVNode("button",Fe,"连接中...",512),[[e.vShow,v.value]]),e.withDirectives(e.createElementVNode("button",qe,"已连接",512),[[e.vShow,s.value]])])]),e.createElementVNode("div",{class:"drag-window-hide",onClick:n[5]||(n[5]=i=>H(!1)),onMousedown:n[6]||(n[6]=e.withModifiers(()=>{},["stop"]))},null,32)],32),e.createElementVNode("div",Ae,[e.renderSlot(t.$slots,"sidebar-right",{},void 0,!0),e.createElementVNode("div",{ref_key:"sidebarRightVideoRef",ref:te,class:"sidebar-video-container"},null,512)])],2)):e.createCommentVNode("",!0),S.value?(e.openBlock(),e.createBlock(e.Teleport,{key:2,to:oe()},[e.createElementVNode("video",{ref_key:"digitalHumanVideo",ref:l,class:"digital-video source-video",poster:"",playsinline:"",autoplay:""},null,512),e.createElementVNode("canvas",{ref_key:"processedCanvas",ref:o,class:"digital-video processed-canvas"},null,512),v.value?(e.openBlock(),e.createElementBlock("div",Je)):e.createCommentVNode("",!0)],8,["to"])):e.createCommentVNode("",!0)],4)}}});exports.default=Qe;
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("vue"),J=require("../../utils/api.ts"),Y=require("../../utils/utils.ts"),Ve=require("../../utils/videoBackgroundRemoval.ts"),Ce={class:"digital-human-assistant"},Re={class:"placeholder left-placeholder"},Te={class:"placeholder right-placeholder"},_e={class:"window-header-left"},Ne={class:"window-title-text"},We={class:"window-header-btns"},xe={class:"disconnect-btn"},Me={class:"connect-btn",disabled:""},De={class:"connect-btn"},Le={class:"window-content"},He={class:"main-content-inner"},Ie={class:"window-header-left"},Pe={class:"window-title-text"},Be={class:"window-header-btns"},$e={class:"disconnect-btn"},Xe={class:"connect-btn",disabled:""},Ye={class:"connect-btn"},Ue={class:"window-content"},ze={class:"window-header-left"},Oe={class:"window-title-text"},je={class:"window-header-btns"},Ge={class:"disconnect-btn"},Fe={class:"connect-btn",disabled:""},qe={class:"connect-btn"},Ae={class:"window-content"},Je={key:0,class:"digital-video-loading"},Ke=e.defineComponent({name:"SjDigitalHumanAssistant"}),Qe=e.defineComponent({...Ke,props:{windowWidth:{default:320},windowHeight:{default:467},windowTitle:{default:"数字人助手"},sidebarWidth:{default:"25%"},sidebarBgColor:{default:"#ffffff"},edgeThreshold:{default:10},dragThreshold:{default:8},defaultStickSide:{default:""},defaultPlayText:{default:""},reconnectInterval:{default:5},maxReconnectTimes:{default:10},baseUrl:{default:""}},setup(N,{expose:fe}){e.useCssVars(t=>({v4920a482:t.sidebarBgColor}));const a=N,K=e.ref(null),Q=e.ref(null),l=e.ref(null),o=e.ref(null),Z=e.ref(null),ee=e.ref(null),te=e.ref(null),W=e.ref(null),r=Ve.default(),S=e.ref(!1),s=e.ref(!1),v=e.ref(!1),k=e.ref(!1),ne=e.ref(!0),x=e.ref(!1),f=e.ref(a.defaultStickSide),M=e.ref(!1),g=e.ref(!1),V=e.ref(!1),U=e.ref(!1),z=e.ref(!1);let d=null,h="";const D=e.ref(null),w=e.ref(0),L=e.ref(!1),C=e.ref(!1),u=e.reactive({x:0,y:0,offsetX:0,offsetY:0}),he=e.computed(()=>({"--sidebar-width":a.sidebarWidth,"--window-width":`${a.windowWidth}px`,"--window-height":`${a.windowHeight}px`})),we=e.computed(()=>({left:`${u.x}px`,top:`${u.y}px`,width:`${a.windowWidth}px`,height:`${a.windowHeight}px`,zIndex:M.value||V.value?9999:1e3})),me=t=>{!t&&l.value?(L.value=!1,w.value=0,de()):t?(L.value=!0,Se()):console.warn("❌ 自动连接超时:video元素未挂载"),ne.value=t},H=t=>{me(!t),x.value=t,t&&s.value?e.nextTick(()=>m()):t||(k.value=!1)},ge=()=>{H(!0)},oe=()=>{switch(f.value){case"left":return ee.value??W.value;case"right":return te.value??W.value;default:return Z.value??W.value}},I=()=>Q.value?.getBoundingClientRect()||{width:0,height:0,left:0,right:0,top:0,bottom:0},ae=()=>K.value?.getBoundingClientRect()||{width:0,height:0,left:0,right:0,top:0,bottom:0},O=(t,n)=>{const c=window.innerWidth||document.documentElement.clientWidth,i=window.innerHeight||document.documentElement.clientHeight;return{x:Math.max(0,Math.min(t,c-a.windowWidth)),y:Math.max(0,Math.min(n,i-a.windowHeight))}},ie=()=>{const t=window.innerWidth||document.documentElement.clientWidth,n=window.innerHeight||document.documentElement.clientHeight,c=t-a.windowWidth-10,i=n-a.windowHeight-10;return O(c,i)},le=()=>{const t=I(),n=ae();U.value=n.left<=t.left+a.edgeThreshold,z.value=n.right>=t.right-a.edgeThreshold},se=()=>{const t=I(),n=ae(),c=n.left<=t.left+a.edgeThreshold?"left":n.right>=t.right-a.edgeThreshold?"right":"";c!==f.value&&(f.value=c,l.value&&(l.value.volume=c?.5:1),T(),m()),U.value=z.value=!1},pe=()=>{k.value=!0},be=()=>{k.value=!1},ye=t=>{M.value=!0,u.offsetX=t.clientX-u.x,u.offsetY=t.clientY-u.y,document.addEventListener("mousemove",j),document.addEventListener("mouseup",G)},j=t=>{if(!M.value)return;const n=I(),{x:c,y:i}=O(t.clientX-n.left-u.offsetX,t.clientY-n.top-u.offsetY);u.x=c,u.y=i,le()},G=()=>{se(),M.value=!1,document.removeEventListener("mousemove",j),document.removeEventListener("mouseup",G)},ce=(t,n)=>{g.value=!0,u.offsetX=n.clientX-(n.clientX-a.windowWidth/2),u.offsetY=n.clientY-(n.clientY-20),document.addEventListener("mousemove",P)},P=t=>{if(!g.value)return;const n=Math.abs(t.movementX),c=Math.abs(t.movementY);(n>a.dragThreshold||c>a.dragThreshold)&&(V.value=!0,f.value="",document.removeEventListener("mousemove",P),document.addEventListener("mousemove",F),document.addEventListener("mouseup",q))},F=t=>{if(!V.value)return;const n=I(),{x:c,y:i}=O(t.clientX-n.left-u.offsetX,t.clientY-n.top-u.offsetY);u.x=c,u.y=i,le()},q=()=>{se(),g.value=V.value=!1,document.removeEventListener("mousemove",F),document.removeEventListener("mouseup",q)},B=()=>{g.value&&!V.value&&(g.value=!1,document.removeEventListener("mousemove",P))},A=async t=>{if(!s.value||!h)return;console.log("✅ 准备播放文本:",t);const n={text:t,type:"echo",interrupt:!0,sessionid:parseInt(h)};J.default.digitalHumanChat(a.baseUrl,n).then(c=>{c&&Number(c.code)===0&&console.log("✅ 播放指令发送成功")}).catch(c=>{console.error("❌ 播放指令发送失败:",c)})},R=async()=>{if(!s.value||!h)return;const t={sessionid:parseInt(h)};J.default.digitalHumanInterruptTalk(a.baseUrl,t).then(n=>{n&&Number(n.code)===0&&console.log("✅ 对话已打断")}).catch(n=>{console.error("❌ 打断对话失败:",n)})},E=()=>{D.value&&(clearTimeout(D.value),D.value=null),C.value=!1,console.log("✅ 重连定时器已清除")},T=(t=!1)=>{if(!o.value)return;t||r.stopProcessing();const n=o.value.getContext("2d");if(n&&(n.clearRect(0,0,o.value.width,o.value.height),!t)){const c=o.value.width,i=o.value.height;o.value.width=c,o.value.height=i}o.value&&!t&&(o.value.style.width="",o.value.style.height="",f.value===""&&(o.value.style.marginLeft="",o.value.style.marginTop=""))},m=async(t=!1)=>{if(!l.value||!o.value||!S.value||!s.value)return;await e.nextTick();const n=l.value.videoWidth||a.windowWidth,c=l.value.videoHeight||a.windowHeight,i=n/c;let p;const ue=oe();ue?p=ue.getBoundingClientRect():p={width:0,height:0,left:0,top:0,right:0,bottom:0};const $=p.width||(f.value?300:a.windowWidth),X=p.height||(f.value?window.innerHeight-40:a.windowHeight),ke=$/X;let b,y;ke>i?(y=X,b=y*i):(b=$,y=b/i);const ve=window.devicePixelRatio||1;o.value.width=Math.floor(b*ve),o.value.height=Math.floor(y*ve),o.value.style.display="block",o.value.style.objectFit="contain",f.value===""?(o.value.style.width=`${b}px`,o.value.style.height=`${y}px`,o.value.style.marginLeft=`${($-b)/2}px`,o.value.style.marginTop=`${(X-y)/2}px`):(o.value.style.width="100%",o.value.style.height="100%",o.value.style.marginLeft="0",o.value.style.marginTop="0"),console.log("✅ Canvas适配完成:",{mode:f.value||"浮动",container:{width:$,height:X},canvas:{drawWidth:b,drawHeight:y}}),t||(r.videoRef.value=l.value,r.canvasRef.value=o.value,await r.processVideo()),!r.isInitSuccess.value&&s.value&&(console.log("🔄 检测到WebGPU未初始化,触发手动重连"),await r.reInitWebGPU()&&await r.processVideo())},_=()=>{if(L.value||s.value||v.value||C.value){console.log("🚫 重连条件不满足");return}if(R(),a.maxReconnectTimes!==-1&&w.value>=a.maxReconnectTimes){console.warn(`🚫 重连已达最大次数(${a.maxReconnectTimes})`),E(),Y.default.funcElMessageShowCtrl(`❌ 数字人连接失败,已尝试${a.maxReconnectTimes}次重连,请重试!`,"error"),w.value=0;return}C.value=!0,D.value=setTimeout(()=>{try{w.value++,console.log(`🔄 第${w.value}次重连WebRTC...`),r.stopProcessing(),de()}finally{C.value=!1}},a.reconnectInterval*1e3)},Ee=async()=>{if(d)try{d.addTransceiver("video",{direction:"recvonly"}),d.addTransceiver("audio",{direction:"recvonly"});const t=await d.createOffer();await d.setLocalDescription(t),await new Promise(i=>{if(d?.iceGatheringState==="complete")i();else{const p=()=>{d?.iceGatheringState==="complete"&&(d?.removeEventListener("icegatheringstatechange",p),i())};d?.addEventListener("icegatheringstatechange",p)}});const n=d.localDescription,c={sdp:n.sdp,type:n.type};J.default.digitalHumanOffer(a.baseUrl,c).then(i=>{i?(h=i.sessionid,d.setRemoteDescription(new RTCSessionDescription(i)),s.value=!0,w.value=0,E(),console.log("✅ WebRTC连接成功"),e.nextTick(()=>m())):(s.value=!1,console.log("❌ WebRTC连接失败:接口返回空"),_()),v.value=!1}).catch(i=>{console.error("❌ WebRTC Offer发送失败:",i),s.value=!1,v.value=!1,_()})}catch(t){console.error("❌ WebRTC协商失败:",t),s.value=!1,v.value=!1,_()}},de=()=>{if(s.value||v.value)return;v.value=!0,console.log("🔄 开始建立WebRTC连接..."),d&&(d.close(),d=null);const t={sdpSemantics:"unified-plan"};d=new RTCPeerConnection(t),d.addEventListener("track",n=>{n.track.kind==="video"&&l.value&&(l.value.srcObject=n.streams[0],l.value.onloadedmetadata=async()=>{l.value&&o.value&&(r.videoRef.value=l.value,r.canvasRef.value=o.value,await m(),await r.processVideo())},l.value.onresize=()=>{m(!0)})}),d.addEventListener("iceconnectionstatechange",()=>{switch(console.log("📶 ICE状态变化:",d.iceConnectionState),d.iceConnectionState){case"disconnected":case"failed":case"closed":console.log("📶 ICE连接断开,触发重连"),s.value=!1,v.value=!1,d=null,h="",l.value&&(l.value.srcObject=null),T(),R(),_();break;case"connected":console.log("📶 ICE连接成功"),w.value=0,E(),e.nextTick(async()=>{!r.isInitSuccess.value&&s.value&&await r.reInitWebGPU()&&await r.processVideo()});break}}),Ee()},Se=()=>{L.value=!0,E(),v.value=!1,r.stopProcessing(),T(),R().then(()=>{setTimeout(()=>{d&&(d.close(),d=null),h="",l.value&&(l.value.srcObject=null),s.value=!1,w.value=0,console.log("✅ 手动断开连接,资源已清理")},500)})},re=Y.default.debounce(()=>{if(f.value===""){const{x:t,y:n}=ie();u.x=t,u.y=n}s.value&&S.value&&(T(!0),e.nextTick(()=>m(!0)))},50);return e.watch(r.isInitSuccess,t=>{!t&&v.value&&Y.default.funcElMessageShowCtrl("🔄 WebGPU初始化失败,正在尝试重连...","warning")},{immediate:!0}),e.watch([()=>r.isInitSuccess,()=>s.value],async([t,n])=>{!t&&n&&l.value&&o.value?(o.value.style.display="none",l.value.style.opacity="1",l.value.style.width="100%",l.value.style.height="100%",Y.default.funcElMessageShowCtrl("ℹ️ 当前浏览器不支持WebGPU,将显示原始视频(无透明背景)","info")):t&&n&&l.value&&o.value&&(o.value.style.display="block",l.value.style.opacity="0")},{immediate:!0}),e.watch(f,async t=>{!S.value||!l.value||(await e.nextTick(),l.value.volume=t?.5:1,s.value&&(T(),await m()))},{immediate:!0,deep:!0}),e.watch([()=>s.value,()=>a.defaultPlayText],async([t,n],[c])=>{t&&!c&&n&&h&&(console.log("✅ 连接成功,自动播放默认文本:",n),await A(n))},{immediate:!1}),e.watch(()=>a.defaultPlayText,async t=>{s.value&&t&&h&&(console.log("✅ 默认文本更新,自动播放:",t),await A(t))},{immediate:!0}),e.onMounted(async()=>{await e.nextTick(),await e.nextTick(),S.value=!0;const{x:t,y:n}=ie();u.x=t,u.y=n,window.addEventListener("resize",re)}),e.onUnmounted(()=>{document.removeEventListener("mousemove",P),document.removeEventListener("mousemove",F),document.removeEventListener("mouseup",q),document.removeEventListener("mousemove",j),document.removeEventListener("mouseup",G),window.removeEventListener("resize",re),E(),r.stopProcessing(),R(),d&&d.close(),l.value&&(l.value.srcObject=null)}),fe({digitalHumanChat:A,digitalHumanInterruptTalk:R,isConnected:s,isConnecting:v,reconnect:_,clearReconnectTimer:E,currentReconnectTimes:w,isWaitingReconnect:C,adaptCanvasToContainer:m,webgpuReInit:r.reInitWebGPU,webgpuIsInitSuccess:r.isInitSuccess,webgpuIsProcessing:r.isProcessing}),(t,n)=>{const c=e.resolveDirective("drag");return e.openBlock(),e.createElementBlock("div",{class:"draggable-window-container",style:e.normalizeStyle(he.value)},[e.withDirectives(e.createElementVNode("div",Ce,null,512),[[e.vShow,ne.value],[c,["digital-human-assistant",ge,!0]]]),e.createElementVNode("div",{ref_key:"teleportFallbackRef",ref:W,class:"teleport-fallback"},null,512),e.withDirectives(e.createElementVNode("div",Re,null,512),[[e.vShow,U.value]]),e.withDirectives(e.createElementVNode("div",Te,null,512),[[e.vShow,z.value]]),x.value&&f.value==="left"?(e.openBlock(),e.createElementBlock("div",{key:0,class:e.normalizeClass(["sidebar left-sidebar",{pressing:g.value}])},[e.createElementVNode("div",{class:"window-header",onMousedown:n[2]||(n[2]=i=>ce("left",i)),onMouseup:B,onMouseleave:B},[e.createElementVNode("div",_e,[e.createElementVNode("span",Ne,e.toDisplayString(N.windowTitle),1),e.createElementVNode("div",We,[e.withDirectives(e.createElementVNode("button",xe,"未连接",512),[[e.vShow,!s.value&&!v.value]]),e.withDirectives(e.createElementVNode("button",Me,"连接中...",512),[[e.vShow,v.value]]),e.withDirectives(e.createElementVNode("button",De,"已连接",512),[[e.vShow,s.value]])])]),e.createElementVNode("div",{class:"drag-window-hide",onClick:n[0]||(n[0]=i=>H(!1)),onMousedown:n[1]||(n[1]=e.withModifiers(()=>{},["stop"]))},null,32)],32),e.createElementVNode("div",Le,[e.renderSlot(t.$slots,"sidebar-left",{},void 0,!0),e.createElementVNode("div",{ref_key:"sidebarLeftVideoRef",ref:ee,class:"sidebar-video-container"},null,512)])],2)):e.createCommentVNode("",!0),e.createElementVNode("div",{class:"main-content-outer",ref_key:"containerRef",ref:Q},[e.createElementVNode("div",He,[e.renderSlot(t.$slots,"main",{},void 0,!0)]),x.value&&f.value===""?(e.openBlock(),e.createElementBlock("div",{key:0,ref_key:"dragWindowRef",ref:K,class:e.normalizeClass(["draggable-window",{"draggable-window-shadow":!s.value||k.value}]),style:e.normalizeStyle(we.value),onMouseenter:pe,onMouseleave:be},[e.createElementVNode("div",{class:e.normalizeClass(["window-header",{"window-header-opacity":s.value&&!k.value}]),onMousedown:ye},[e.createElementVNode("div",Ie,[e.createElementVNode("span",Pe,e.toDisplayString(N.windowTitle),1),e.createElementVNode("div",Be,[e.withDirectives(e.createElementVNode("button",$e,"未连接",512),[[e.vShow,!s.value&&!v.value]]),e.withDirectives(e.createElementVNode("button",Xe,"连接中...",512),[[e.vShow,v.value]]),e.withDirectives(e.createElementVNode("button",Ye,"已连接",512),[[e.vShow,s.value]])])]),e.createElementVNode("div",{class:"drag-window-hide",onClick:n[3]||(n[3]=i=>H(!1)),onMousedown:n[4]||(n[4]=e.withModifiers(()=>{},["stop"]))},null,32)],34),e.createElementVNode("div",Ue,[e.createElementVNode("div",{class:"video-container",ref_key:"windowVideoRef",ref:Z},null,512)])],38)):e.createCommentVNode("",!0)],512),x.value&&f.value==="right"?(e.openBlock(),e.createElementBlock("div",{key:1,class:e.normalizeClass(["sidebar right-sidebar",{pressing:g.value}])},[e.createElementVNode("div",{class:"window-header",onMousedown:n[7]||(n[7]=i=>ce("right",i)),onMouseup:B,onMouseleave:B},[e.createElementVNode("div",ze,[e.createElementVNode("span",Oe,e.toDisplayString(N.windowTitle),1),e.createElementVNode("div",je,[e.withDirectives(e.createElementVNode("button",Ge,"未连接",512),[[e.vShow,!s.value&&!v.value]]),e.withDirectives(e.createElementVNode("button",Fe,"连接中...",512),[[e.vShow,v.value]]),e.withDirectives(e.createElementVNode("button",qe,"已连接",512),[[e.vShow,s.value]])])]),e.createElementVNode("div",{class:"drag-window-hide",onClick:n[5]||(n[5]=i=>H(!1)),onMousedown:n[6]||(n[6]=e.withModifiers(()=>{},["stop"]))},null,32)],32),e.createElementVNode("div",Ae,[e.renderSlot(t.$slots,"sidebar-right",{},void 0,!0),e.createElementVNode("div",{ref_key:"sidebarRightVideoRef",ref:te,class:"sidebar-video-container"},null,512)])],2)):e.createCommentVNode("",!0),S.value?(e.openBlock(),e.createBlock(e.Teleport,{key:2,to:oe()},[e.createElementVNode("video",{ref_key:"digitalHumanVideo",ref:l,class:"digital-video source-video",poster:"",playsinline:"",autoplay:""},null,512),e.createElementVNode("canvas",{ref_key:"processedCanvas",ref:o,class:"digital-video processed-canvas"},null,512),v.value?(e.openBlock(),e.createElementBlock("div",Je)):e.createCommentVNode("",!0)],8,["to"])):e.createCommentVNode("",!0)],4)}}});exports.default=Qe;