@scienjoy/sj-digital-human-assistant-adaptive 1.0.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.
@@ -0,0 +1,1933 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('hls.js'), require('aliyun-rts-sdk')) :
3
+ typeof define === 'function' && define.amd ? define(['hls.js', 'aliyun-rts-sdk'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.SjDigitalHumanAssistantAdaptive = factory(global.Hls, global.aliyunRtsSdk));
5
+ })(this, (function (Hls, aliyunRtsSdk) { 'use strict';
6
+
7
+ function asyncGeneratorStep(n, t, e, r, o, a, c) {
8
+ try {
9
+ var i = n[a](c),
10
+ u = i.value;
11
+ } catch (n) {
12
+ return void e(n);
13
+ }
14
+ i.done ? t(u) : Promise.resolve(u).then(r, o);
15
+ }
16
+ function _asyncToGenerator(n) {
17
+ return function () {
18
+ var t = this,
19
+ e = arguments;
20
+ return new Promise(function (r, o) {
21
+ var a = n.apply(t, e);
22
+ function _next(n) {
23
+ asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
24
+ }
25
+ function _throw(n) {
26
+ asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
27
+ }
28
+ _next(void 0);
29
+ });
30
+ };
31
+ }
32
+ function _regenerator() {
33
+ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
34
+ var e,
35
+ t,
36
+ r = "function" == typeof Symbol ? Symbol : {},
37
+ n = r.iterator || "@@iterator",
38
+ o = r.toStringTag || "@@toStringTag";
39
+ function i(r, n, o, i) {
40
+ var c = n && n.prototype instanceof Generator ? n : Generator,
41
+ u = Object.create(c.prototype);
42
+ return _regeneratorDefine(u, "_invoke", function (r, n, o) {
43
+ var i,
44
+ c,
45
+ u,
46
+ f = 0,
47
+ p = o || [],
48
+ y = false,
49
+ G = {
50
+ p: 0,
51
+ n: 0,
52
+ v: e,
53
+ a: d,
54
+ f: d.bind(e, 4),
55
+ d: function (t, r) {
56
+ return i = t, c = 0, u = e, G.n = r, a;
57
+ }
58
+ };
59
+ function d(r, n) {
60
+ for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) {
61
+ var o,
62
+ i = p[t],
63
+ d = G.p,
64
+ l = i[2];
65
+ r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0));
66
+ }
67
+ if (o || r > 1) return a;
68
+ throw y = true, n;
69
+ }
70
+ return function (o, p, l) {
71
+ if (f > 1) throw TypeError("Generator is already running");
72
+ for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) {
73
+ i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u);
74
+ try {
75
+ if (f = 2, i) {
76
+ if (c || (o = "next"), t = i[o]) {
77
+ if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object");
78
+ if (!t.done) return t;
79
+ u = t.value, c < 2 && (c = 0);
80
+ } else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1);
81
+ i = e;
82
+ } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break;
83
+ } catch (t) {
84
+ i = e, c = 1, u = t;
85
+ } finally {
86
+ f = 1;
87
+ }
88
+ }
89
+ return {
90
+ value: t,
91
+ done: y
92
+ };
93
+ };
94
+ }(r, o, i), true), u;
95
+ }
96
+ var a = {};
97
+ function Generator() {}
98
+ function GeneratorFunction() {}
99
+ function GeneratorFunctionPrototype() {}
100
+ t = Object.getPrototypeOf;
101
+ var c = [][n] ? t(t([][n]())) : (_regeneratorDefine(t = {}, n, function () {
102
+ return this;
103
+ }), t),
104
+ u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c);
105
+ function f(e) {
106
+ return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e;
107
+ }
108
+ return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine(u), _regeneratorDefine(u, o, "Generator"), _regeneratorDefine(u, n, function () {
109
+ return this;
110
+ }), _regeneratorDefine(u, "toString", function () {
111
+ return "[object Generator]";
112
+ }), (_regenerator = function () {
113
+ return {
114
+ w: i,
115
+ m: f
116
+ };
117
+ })();
118
+ }
119
+ function _regeneratorDefine(e, r, n, t) {
120
+ var i = Object.defineProperty;
121
+ try {
122
+ i({}, "", {});
123
+ } catch (e) {
124
+ i = 0;
125
+ }
126
+ _regeneratorDefine = function (e, r, n, t) {
127
+ function o(r, n) {
128
+ _regeneratorDefine(e, r, function (e) {
129
+ return this._invoke(r, n, e);
130
+ });
131
+ }
132
+ r ? i ? i(e, r, {
133
+ value: n,
134
+ enumerable: !t,
135
+ configurable: !t,
136
+ writable: !t
137
+ }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2));
138
+ }, _regeneratorDefine(e, r, n, t);
139
+ }
140
+
141
+ // src/utils/index.js 公共工具方法
142
+
143
+ // 防抖函数
144
+ function debounce(fn, delay) {
145
+ if (typeof fn !== "function") return;
146
+ var timer = null;
147
+ return function () {
148
+ var that = this;
149
+ var args = arguments;
150
+ clearTimeout(timer);
151
+ timer = setTimeout(function () {
152
+ fn.apply(that, args);
153
+ }, delay);
154
+ };
155
+ }
156
+
157
+ function SjDigitalHumanAssistantAdaptive(options) {
158
+ options = options || {};
159
+ this.config = this._assign({}, {
160
+ baseUrl: "",
161
+ windowTitle: "数字人助手",
162
+ defaultPlayText: "",
163
+ reconnectInterval: 1,
164
+ maxReconnectTimes: 10,
165
+ secretKey: "abc999",
166
+ accessToken: "aaaaaaa",
167
+ windowWidth: "300px",
168
+ windowHeight: "550px",
169
+ avatarId: 1,
170
+ isVideoFullscreen: true,
171
+ hlsClass: Hls,
172
+ aliRtsClass: aliyunRtsSdk.AliRTS,
173
+ container: typeof document !== "undefined" ? document.body : null,
174
+ zIndex: 9999,
175
+ mobileBreakpoint: 768
176
+ }, options);
177
+ this.state = {
178
+ isConnected: false,
179
+ isConnecting: false,
180
+ isSSEConnected: false,
181
+ isAutoplayBlocked: true,
182
+ isMediaReady: false,
183
+ isVideoFullscreen: !!this.config.isVideoFullscreen,
184
+ hasAvatarUrl: true,
185
+ queueCount: 0,
186
+ isTextInputMode: true,
187
+ isRecording: false,
188
+ showReconnectDialog: false,
189
+ needManualPlay: false,
190
+ isIOSWechat: this._isIOSWechat(),
191
+ isReceivingReply: false,
192
+ currentAiMessage: this.config.defaultPlayText || "",
193
+ currentReconnectTimes: 0,
194
+ isManualDisconnect: false,
195
+ isWaitingReconnect: false,
196
+ isMobileDevice: true,
197
+ dragPosition: null,
198
+ isDragging: false,
199
+ dragMoved: false,
200
+ sessionid: "",
201
+ chatSessionId: "",
202
+ currentHlsUrl: "",
203
+ currentRtsUrl: "",
204
+ videoEleBg: "",
205
+ posterBgUrl: "",
206
+ defaultVideoUrl: "",
207
+ avatarPic: "",
208
+ showToastFlag: false,
209
+ toastMessage: "",
210
+ playingVoiceId: null,
211
+ hasUnmuted: false
212
+ };
213
+ this.drag = {
214
+ startClientX: 0,
215
+ startClientY: 0,
216
+ startLeft: 0,
217
+ startTop: 0
218
+ };
219
+ this.stream = {
220
+ hls: null,
221
+ aliRts: null,
222
+ sseReader: null,
223
+ sseReconnectTimer: null,
224
+ reconnectTimer: null
225
+ };
226
+ this.audio = {
227
+ mediaStream: null,
228
+ audioContext: null,
229
+ sourceNode: null,
230
+ processorNode: null,
231
+ chunks: [],
232
+ startTime: 0,
233
+ permissionPromise: null,
234
+ currentVoiceAudio: null
235
+ };
236
+ this.dom = {};
237
+ this.chatMessages = [];
238
+ this.sseReconnectCount = 0;
239
+ this.toastTimer = null;
240
+ this.blockClickTimer = null;
241
+ this.reconnectDialogCallback = null;
242
+ this.resizeHandler = null;
243
+ this.stopRequestedBeforeStart = false;
244
+ this.isButtonPressed = false;
245
+ this.permissionCheckTimer = null;
246
+ if (!this.config.baseUrl) {
247
+ throw new Error("[SjDigitalHumanAssistantAdaptive] baseUrl is required");
248
+ }
249
+ this.init();
250
+ }
251
+ SjDigitalHumanAssistantAdaptive.prototype._assign = function (target) {
252
+ if (target == null) {
253
+ throw new TypeError("Cannot convert undefined or null to object");
254
+ }
255
+ target = Object(target);
256
+ for (var i = 1; i < arguments.length; i++) {
257
+ var source = arguments[i];
258
+ if (!source) {
259
+ continue;
260
+ }
261
+ for (var key in source) {
262
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
263
+ target[key] = source[key];
264
+ }
265
+ }
266
+ }
267
+ return target;
268
+ };
269
+ SjDigitalHumanAssistantAdaptive.prototype.init = function () {
270
+ this.state.isMobileDevice = this.detectMobileDevice();
271
+ this.injectStyle();
272
+ this.mountDom();
273
+ this.updateRemBase();
274
+ this.bindEvent();
275
+ this.syncDeviceMode();
276
+ this.renderInitialMessages();
277
+ this.bootstrap();
278
+ };
279
+ SjDigitalHumanAssistantAdaptive.prototype.injectStyle = function () {
280
+ if (document.getElementById("sj-digital-human-adaptive-style")) {
281
+ return;
282
+ }
283
+ var css = [".sjdha-root{--sjdha-rem:50px;position:fixed;right:16px;bottom:16px;display:flex;flex-direction:column;background:#f5f5f5;border-radius:calc(var(--sjdha-rem) * 0.2);overflow:hidden;box-shadow:0 calc(var(--sjdha-rem) * 0.2) calc(var(--sjdha-rem) * 0.5) rgba(15,23,42,.18);z-index:" + this.config.zIndex + ";font-family:'Microsoft YaHei','PingFang SC',sans-serif;color:#232323;font-size:calc(var(--sjdha-rem) * 0.28);-webkit-text-size-adjust:100%;touch-action:pan-x pan-y}", ".sjdha-root,.sjdha-root *{box-sizing:border-box}", ".sjdha-root.device-mobile{left:0;right:0;top:0;bottom:0;width:100%;height:100dvh;max-width:none;border-radius:0}", ".sjdha-root.device-web{width:300px;height:550px;max-width:calc(100vw - 24px);max-height:calc(100vh - 24px)}", ".sjdha-root.video-fullscreen .sjdha-video-area{flex:none;height:calc(100% - calc(var(--sjdha-rem) * 1.16));min-height:calc(100% - calc(var(--sjdha-rem) * 1.16))}", ".sjdha-root.video-split .sjdha-video-area{flex:none;height:40%;min-height:calc(var(--sjdha-rem) * 4)}", ".sjdha-root.video-fullscreen.device-mobile .sjdha-video-area{height:calc(100% - calc(var(--sjdha-rem) * 1.16) - env(safe-area-inset-bottom));min-height:calc(100% - calc(var(--sjdha-rem) * 1.16) - env(safe-area-inset-bottom))}", ".sjdha-video-area{position:relative;flex:1;min-height:calc(var(--sjdha-rem) * 4);background-size:100% 100%;background-position:center top;cursor:grab;overflow:hidden}", ".sjdha-video-area:before{content:'';position:absolute;inset:0;backdrop-filter:blur(8px);z-index:0}", ".sjdha-digital-human{position:relative;z-index:1;width:100%;height:100%;display:flex;align-items:center;justify-content:center;overflow:hidden}", ".sjdha-video{position:absolute;left:50%;top:0;transform:translateX(-50%);width:auto;height:100%;background:transparent;object-fit:contain}", ".sjdha-queue{position:absolute;left:50%;bottom:calc(var(--sjdha-rem) * 0.21);transform:translateX(-50%);padding:calc(var(--sjdha-rem) * 0.16) calc(var(--sjdha-rem) * 0.32);border-radius:999px;background:rgba(15,23,42,.55);backdrop-filter:blur(calc(var(--sjdha-rem) * 0.08));color:#fff;font-size:calc(var(--sjdha-rem) * 0.2);white-space:nowrap;z-index:10;display:none}", ".sjdha-fullscreen-btn{position:absolute;top:calc(var(--sjdha-rem) * 0.9);right:calc(var(--sjdha-rem) * 0.3);width:calc(var(--sjdha-rem) * 0.6);height:calc(var(--sjdha-rem) * 0.6);border:0;border-radius:50%;background:rgba(15,23,42,.45);color:#fff;font-size:calc(var(--sjdha-rem) * 0.28);line-height:calc(var(--sjdha-rem) * 0.6);text-align:center;cursor:pointer;z-index:20;display:flex;align-items:center;justify-content:center}", ".sjdha-fullscreen-btn:disabled{opacity:.45;cursor:not-allowed}", ".sjdha-autoplay-overlay{position:absolute;inset:0;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:calc(var(--sjdha-rem) * 0.16);background:rgba(0,0,0,.45);color:#fff;z-index:30;font-size:calc(var(--sjdha-rem) * 0.28)}", ".sjdha-autoplay-overlay.hidden{display:none}", ".sjdha-manual-play{display:flex;flex-direction:column;align-items:center;gap:calc(var(--sjdha-rem) * 0.16);cursor:pointer}", ".sjdha-autoplay-icon{width:calc(var(--sjdha-rem) * 1.12);height:calc(var(--sjdha-rem) * 1.12);border-radius:50%;display:flex;align-items:center;justify-content:center;background:rgba(255,255,255,.25);font-size:calc(var(--sjdha-rem) * 0.48)}", ".sjdha-autoplay-spinner{width:calc(var(--sjdha-rem) * 0.8);height:calc(var(--sjdha-rem) * 0.8);border-radius:50%;border:3px solid rgba(255,255,255,.35);border-top-color:#fff;animation:sjdha-spin .8s linear infinite}", ".sjdha-ai-response{position:absolute;left:calc(var(--sjdha-rem) * 0.32);right:calc(var(--sjdha-rem) * 0.32);bottom:calc(var(--sjdha-rem) * 1.2);padding:calc(var(--sjdha-rem) * 0.32) calc(var(--sjdha-rem) * 0.32) calc(var(--sjdha-rem) * 0.45);background:rgba(255,255,255,.95);border-radius:calc(var(--sjdha-rem) * 0.24);box-shadow:0 calc(var(--sjdha-rem) * 0.08) calc(var(--sjdha-rem) * 0.24) rgba(0,0,0,.1);z-index:15;display:none}", ".sjdha-ai-response-label{font-size:calc(var(--sjdha-rem) * 0.28);color:#509aff;font-weight:600;text-align:center;margin-bottom:calc(var(--sjdha-rem) * 0.05)}", ".sjdha-ai-response-content{-ms-overflow-style: none;scrollbar-width: none;font-size:calc(var(--sjdha-rem) * 0.3);line-height:1.5;max-height:calc(var(--sjdha-rem) * 2.4);overflow:auto;text-align:center;word-break:break-word}", ".sjdha-chat-area{height:60%;min-height:calc(var(--sjdha-rem) * 4);background:#fff;margin-top:calc(var(--sjdha-rem) * -0.15);display:flex;flex-direction:column;position:relative;z-index:9}", ".sjdha-chat-area.hidden{display:none}", ".sjdha-chat-date{padding:calc(var(--sjdha-rem) * 0.33) 0 calc(var(--sjdha-rem) * 0.23);text-align:center;font-size:calc(var(--sjdha-rem) * 0.26);line-height:calc(var(--sjdha-rem) * 0.26);color:#888}", ".sjdha-chat-list{flex:1;padding:calc(var(--sjdha-rem) * 0.1) calc(var(--sjdha-rem) * 0.16) calc(var(--sjdha-rem) * 1.28);overflow-y:auto;-ms-overflow-style:none;scrollbar-width:none;-webkit-overflow-scrolling:touch}", ".sjdha-chat-list::-webkit-scrollbar{display:none}", ".sjdha-time-label{text-align:center;padding:calc(var(--sjdha-rem) * 0.4) 0 calc(var(--sjdha-rem) * 0.2);font-size:calc(var(--sjdha-rem) * 0.26);color:#888}", ".sjdha-chat-item{display:flex;max-width:85%;margin-bottom:calc(var(--sjdha-rem) * 0.26);text-align:left}", ".sjdha-chat-item.system{margin-right:auto}", ".sjdha-chat-item.user{margin-left:auto;flex-direction:row-reverse}", ".sjdha-avatar{width:calc(var(--sjdha-rem) * 0.8);height:calc(var(--sjdha-rem) * 0.8);border-radius:50%;background:#f0f0f0 center/cover no-repeat;flex-shrink:0;margin:0 calc(var(--sjdha-rem) * 0.12)}", ".sjdha-chat-item.system .sjdha-avatar{margin-right:calc(var(--sjdha-rem) * 0.22)}", ".sjdha-chat-item.user .sjdha-avatar{margin-left:calc(var(--sjdha-rem) * 0.22);background-image:url('https://szr-data.xiu361.cn/img/szr_video/szr_button/user-avatar.png')}", ".sjdha-bubble{position:relative;padding:calc(var(--sjdha-rem) * 0.16) calc(var(--sjdha-rem) * 0.28);border-radius:calc(var(--sjdha-rem) * 0.2);line-height:1.4;font-size:calc(var(--sjdha-rem) * 0.28);word-break:break-word;display:flex;align-items:center;min-height:calc(var(--sjdha-rem) * 0.28)}", ".sjdha-chat-item.system .sjdha-bubble{background:#fff;color:#333;box-shadow:0 4px 12px rgba(15,23,42,.06)}", ".sjdha-chat-item.system .sjdha-bubble:before{content:'';position:absolute;left:calc(var(--sjdha-rem) * -0.11);top:calc(var(--sjdha-rem) * 0.34);border-top:calc(var(--sjdha-rem) * 0.12) solid transparent;border-bottom:calc(var(--sjdha-rem) * 0.12) solid transparent;border-right:calc(var(--sjdha-rem) * 0.12) solid #fff}", ".sjdha-chat-item.user .sjdha-bubble{background:#509aff;color:#fff}", ".sjdha-chat-item.user .sjdha-bubble:after{content:'';position:absolute;right:calc(var(--sjdha-rem) * -0.11);top:calc(var(--sjdha-rem) * 0.34);border-top:calc(var(--sjdha-rem) * 0.12) solid transparent;border-bottom:calc(var(--sjdha-rem) * 0.12) solid transparent;border-left:calc(var(--sjdha-rem) * 0.12) solid #509aff}", ".sjdha-voice-bubble{cursor:pointer;min-width:calc(var(--sjdha-rem) * 0.8);justify-content:space-between;gap:calc(var(--sjdha-rem) * 0.16);padding:calc(var(--sjdha-rem) * 0.2) calc(var(--sjdha-rem) * 0.32)}", ".sjdha-voice-icon{width:calc(var(--sjdha-rem) * 0.4);height:calc(var(--sjdha-rem) * 0.4);background:url('https://szr-data.xiu361.cn/img/szr_video/szr_button/play-voice-icon.png') no-repeat center center;background-size:contain;position:relative}", ".sjdha-voice-icon.playing{animation:sjdha-voice-play 1s ease-in-out infinite}", ".sjdha-voice-icon.playing:before,.sjdha-voice-icon.playing:after{display:none}", ".sjdha-input-bar{position:absolute;left:0;right:0;bottom:0;height:calc(var(--sjdha-rem) * 1.16);padding:calc(var(--sjdha-rem) * 0.16);display:flex;align-items:center;gap:calc(var(--sjdha-rem) * 0.16);background:#fff;border-top:1px solid #edf2f7}", ".sjdha-root.video-fullscreen .sjdha-chat-area{position:absolute;left:0;right:0;bottom:0;height:calc(var(--sjdha-rem) * 1.16);min-height:calc(var(--sjdha-rem) * 1.16);background:transparent;border-radius:0;padding:0;z-index:40}", ".sjdha-root.video-fullscreen .sjdha-chat-list{flex:none;height:0;padding:0;overflow:hidden}", ".sjdha-root.video-fullscreen.device-mobile .sjdha-chat-area{height:calc(calc(var(--sjdha-rem) * 1.16) + env(safe-area-inset-bottom));padding-bottom:env(safe-area-inset-bottom)}", ".sjdha-mode-btn,.sjdha-send-btn,.sjdha-voice-input{border:0;border-radius:calc(var(--sjdha-rem) * 0.16);height:calc(var(--sjdha-rem) * 0.84)}", ".sjdha-mode-btn{width:calc(var(--sjdha-rem) * 0.66);background:#eff6ff;color:#3b82f6;font-size:calc(var(--sjdha-rem) * 0.36);cursor:pointer;flex-shrink:0;padding:0}", ".sjdha-input-wrap{flex:1;height:calc(var(--sjdha-rem) * 0.84);border:1px solid #dbeafe;border-radius:calc(var(--sjdha-rem) * 0.16);overflow:hidden;background:#f5f5f5;display:flex;align-items:center;padding:0 calc(var(--sjdha-rem) * 0.16)}", ".sjdha-input{width:100%;height:100%;padding:0 calc(var(--sjdha-rem) * 0.08);border:0;outline:none;font-size:16px;background:transparent;-webkit-text-size-adjust:100%;touch-action:manipulation;appearance:none;-webkit-appearance:none}", ".sjdha-voice-input{flex:1;background:#f5f5f5;color:#1a1a1a;font-size:calc(var(--sjdha-rem) * 0.28);font-weight:600;cursor:pointer;user-select:none;-webkit-user-select:none;touch-action:manipulation}", ".sjdha-send-btn{min-width:calc(var(--sjdha-rem) * 1.04);padding:0 calc(var(--sjdha-rem) * 0.32);background:#509aff;color:#fff;cursor:pointer;font-size:calc(var(--sjdha-rem) * 0.28)}", ".sjdha-send-btn:disabled{opacity:.55;cursor:not-allowed}", ".sjdha-recording{position:absolute;inset:0;display:none;align-items:center;justify-content:center;background:rgba(15,23,42,.5);z-index:40}", ".sjdha-recording.show{display:flex}", ".sjdha-recording-card{display:flex;flex-direction:column;align-items:center;gap:calc(var(--sjdha-rem) * 0.24);padding:calc(var(--sjdha-rem) * 0.6) calc(var(--sjdha-rem) * 0.8);border-radius:calc(var(--sjdha-rem) * 0.24);background:rgba(15,23,42,.85);color:#fff}", ".sjdha-wave{display:flex;align-items:flex-end;gap:calc(var(--sjdha-rem) * 0.08);height:calc(var(--sjdha-rem) * 0.64)}", ".sjdha-wave span{width:calc(var(--sjdha-rem) * 0.08);border-radius:999px;background:#fff;animation:sjdha-wave 1.1s ease-in-out infinite}", ".sjdha-dialog-overlay,.sjdha-toast{position:absolute;inset:0;display:none;align-items:center;justify-content:center;z-index:50}", ".sjdha-dialog-overlay.show,.sjdha-toast.show{display:flex}", ".sjdha-dialog-overlay{background:rgba(15,23,42,.45)}", ".sjdha-dialog{width:calc(var(--sjdha-rem) * 5.2);padding:calc(var(--sjdha-rem) * 0.36);background:#fff;border-radius:calc(var(--sjdha-rem) * 0.28);box-shadow:0 calc(var(--sjdha-rem) * 0.24) calc(var(--sjdha-rem) * 0.64) rgba(15,23,42,.22)}", ".sjdha-dialog-content{font-size:calc(var(--sjdha-rem) * 0.3);line-height:1.5;color:#1e293b;text-align:center}", ".sjdha-dialog-footer{margin-top:16px;display:flex;justify-content:center}", ".sjdha-dialog-btn{height:calc(var(--sjdha-rem) * 0.68);padding:0 calc(var(--sjdha-rem) * 0.36);border:0;border-radius:calc(var(--sjdha-rem) * 0.2);background:#509aff;color:#fff;cursor:pointer}", ".sjdha-toast{pointer-events:none}", ".sjdha-toast-content{padding:calc(var(--sjdha-rem) * 0.2) calc(var(--sjdha-rem) * 0.32);border-radius:999px;background:rgba(15,23,42,.8);color:#fff;font-size:calc(var(--sjdha-rem) * 0.26)}", ".sjdha-root.device-web{font-size:14px;min-height:467px;display:grid;grid-template-columns:1fr;grid-template-rows:188px minmax(0,1fr);align-items:stretch;border-radius:20px;box-shadow:0 18px 48px rgba(15,23,42,.18);border:1px solid rgba(148,163,184,.22);z-index:1000}", ".sjdha-root.device-web.video-fullscreen{grid-template-rows:minmax(0,1fr) auto}", ".sjdha-root.device-web.video-fullscreen .sjdha-video-area{grid-column:1;grid-row:1;height:100%;min-height:0}", ".sjdha-root.device-web.video-split .sjdha-video-area{height:auto;min-height:188px}", ".sjdha-root.device-web .sjdha-video-area{grid-column:1;grid-row:1;min-height:0;height:auto;cursor:move;user-select:none}", ".sjdha-root.device-web .sjdha-video-area:before{backdrop-filter:blur(10px)}", ".sjdha-root.device-web .sjdha-queue{bottom:14px;padding:6px 12px;font-size:12px;backdrop-filter:blur(8px)}", ".sjdha-root.device-web .sjdha-fullscreen-btn{top:24px;right:24px;width:44px;height:44px;font-size:16px;line-height:44px}", ".sjdha-root.device-web .sjdha-autoplay-overlay{gap:10px;font-size:14px}", ".sjdha-root.device-web .sjdha-manual-play{gap:10px}", ".sjdha-root.device-web .sjdha-autoplay-icon{width:56px;height:56px;font-size:22px}", ".sjdha-root.device-web .sjdha-autoplay-spinner{width:50px;height:50px}", ".sjdha-root.device-web .sjdha-ai-response{left:14px;right:14px;bottom:22px;padding:12px 14px 14px;border-radius:14px;background:rgba(255,255,255,.92);box-shadow:0 8px 24px rgba(0,0,0,.1)}", ".sjdha-root.device-web .sjdha-ai-response-label{font-size:12px;margin-bottom:4px}", ".sjdha-root.device-web .sjdha-ai-response-content{font-size:16px;max-height:100px;scrollbar-width:none;-ms-overflow-style:none}", ".sjdha-root.device-web .sjdha-chat-area{grid-column:1;grid-row:2;min-height:0;height:auto;margin-top:0;user-select:none;overflow:hidden;background:rgba(255,255,255,.92)}", ".sjdha-root.device-web.video-fullscreen .sjdha-chat-area{display:flex;position:static;height:auto;min-height:auto;background:transparent;border:none;box-shadow:none;backdrop-filter:none;border-radius:0;overflow:visible;padding:0}", ".sjdha-root.device-web .sjdha-chat-date{padding:12px 0 10px;font-size:12px;line-height:1.4}", ".sjdha-root.device-web .sjdha-time-label{padding:12px 0 6px;font-size:12px;line-height:1.4}", ".sjdha-root.device-web .sjdha-chat-list{flex:1;padding:0 12px 12px;box-sizing:border-box}", ".sjdha-root.device-web .sjdha-chat-item{margin-bottom:10px;max-width:88%}", ".sjdha-root.device-web .sjdha-avatar{width:28px;height:28px;margin:0 8px;box-shadow:0 6px 14px rgba(80,154,255,.12)}", ".sjdha-root.device-web .sjdha-chat-item.system .sjdha-avatar{margin-right:8px}", ".sjdha-root.device-web .sjdha-chat-item.user .sjdha-avatar{margin-left:8px}", ".sjdha-root.device-web .sjdha-bubble{padding:10px 12px;border-radius:14px;font-size:14px;line-height:1.5;min-height:16px;box-shadow:0 6px 16px rgba(15,23,42,.05)}", ".sjdha-root.device-web .sjdha-chat-item.system .sjdha-bubble{background:#fff}", ".sjdha-root.device-web .sjdha-chat-item.system .sjdha-bubble:before{left:-8px;top:12px;border-top:8px solid transparent;border-bottom:8px solid transparent;border-right:8px solid #fff}", ".sjdha-root.device-web .sjdha-chat-item.user .sjdha-bubble{background:linear-gradient(135deg,#509aff 0%,#2f7bf3 100%)}", ".sjdha-root.device-web .sjdha-chat-item.user .sjdha-bubble:after{right:-8px;top:12px;border-top:8px solid transparent;border-bottom:8px solid transparent;border-left:8px solid #2f7bf3}", ".sjdha-root.device-web .sjdha-voice-bubble{min-width:40px;gap:8px;justify-content:space-between}", ".sjdha-root.device-web .sjdha-voice-icon{width:24px;height:24px}", ".sjdha-root.device-web .sjdha-input-bar{position:static;height:auto;min-height:auto;padding:10px;gap:10px;background:rgba(255,255,255,.92);box-shadow:0 10px 24px rgba(15,23,42,.08);box-sizing:border-box}", ".sjdha-root.device-web .sjdha-mode-btn,.sjdha-root.device-web .sjdha-send-btn,.sjdha-root.device-web .sjdha-voice-input{height:38px;border-radius:12px}", ".sjdha-root.device-web .sjdha-mode-btn{width:36px;font-size:18px}", ".sjdha-root.device-web .sjdha-input-wrap{height:38px;border-radius:12px;padding:0 10px;background:#f2f5fa;border:none}", ".sjdha-root.device-web .sjdha-input{padding:0;font-size:13px}", ".sjdha-root.device-web .sjdha-voice-input{font-size:13px;background:#f2f5fa}", ".sjdha-root.device-web .sjdha-send-btn{min-width:60px;padding:0 14px;font-size:14px;background:linear-gradient(135deg,#509aff 0%,#2f7bf3 100%)}", ".sjdha-root.device-web .sjdha-recording-card{gap:12px;padding:24px 30px;border-radius:16px}", ".sjdha-root.device-web .sjdha-wave{gap:4px;height:32px}", ".sjdha-root.device-web .sjdha-wave span{width:4px}", ".sjdha-root.device-web .sjdha-dialog{width:260px;padding:18px;border-radius:14px;box-shadow:0 12px 32px rgba(15,23,42,.22)}", ".sjdha-root.device-web .sjdha-dialog-content{font-size:15px}", ".sjdha-root.device-web .sjdha-dialog-btn{height:34px;padding:0 18px;border-radius:10px}", ".sjdha-root.device-web .sjdha-toast-content{padding:10px 16px;font-size:13px}", "@keyframes sjdha-spin{to{transform:rotate(360deg)}}", "@keyframes sjdha-pulse{0%{transform:scale(.8);opacity:.8}100%{transform:scale(1.6);opacity:0}}", "@keyframes sjdha-voice-play{0%,100%{opacity:1}50%{opacity:.5}}", "@keyframes sjdha-wave{0%,100%{height:calc(var(--sjdha-rem) * 0.16);opacity:.45}50%{height:calc(var(--sjdha-rem) * 0.64);opacity:1}}", "@media (max-width:768px){.sjdha-root{width:100%!important;height:100dvh!important;max-width:none!important;max-height:none!important;left:0!important;top:0!important;right:0!important;bottom:0!important;border-radius:0}}"].join("");
284
+ var style = document.createElement("style");
285
+ style.id = "sj-digital-human-adaptive-style";
286
+ style.type = "text/css";
287
+ style.textContent = css;
288
+ document.head.appendChild(style);
289
+ };
290
+ SjDigitalHumanAssistantAdaptive.prototype.mountDom = function () {
291
+ var root = document.createElement("div");
292
+ root.className = "sjdha-root";
293
+ root.innerHTML = ['<div class="sjdha-video-area">', ' <div class="sjdha-digital-human">', ' <video class="sjdha-video" playsinline webkit-playsinline x5-video-player-type="h5" x5-video-player-fullscreen="true" x5-video-orientation="portraint" autoplay loop muted></video>', ' <div class="sjdha-queue"></div>', ' <button type="button" class="sjdha-fullscreen-btn"></button>', ' <div class="sjdha-autoplay-overlay">', ' <div class="sjdha-manual-play" style="display:none">', ' <div class="sjdha-autoplay-icon">▶</div>', ' <span>点击开始对话</span>', ' </div>', ' <div class="sjdha-autoplay-spinner"></div>', ' </div>', ' <div class="sjdha-ai-response">', ' <div class="sjdha-ai-response-label">正在播报</div>', ' <div class="sjdha-ai-response-content"></div>', ' </div>', ' </div>', '</div>', '<div class="sjdha-chat-area">', ' <div class="sjdha-chat-list"><div class="sjdha-chat-date"></div></div>', ' <div class="sjdha-input-bar">', ' <button type="button" class="sjdha-mode-btn"></button>', ' <div class="sjdha-input-wrap"><input class="sjdha-input" type="text" placeholder="请输入聊天内容..."></div>', ' <button type="button" class="sjdha-voice-input" style="display:none">按住 说话</button>', ' <button type="button" class="sjdha-send-btn">发送</button>', ' </div>', '</div>', '<div class="sjdha-recording">', ' <div class="sjdha-recording-card">', ' <div class="sjdha-wave"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></div>', ' <div>松开 发送</div>', ' </div>', '</div>', '<div class="sjdha-dialog-overlay">', ' <div class="sjdha-dialog">', ' <div class="sjdha-dialog-content">离开会话时间过长,是否重连?</div>', ' <div class="sjdha-dialog-footer"><button type="button" class="sjdha-dialog-btn">确认</button></div>', ' </div>', '</div>', '<div class="sjdha-toast"><div class="sjdha-toast-content"></div></div>'].join("");
294
+ (this.config.container || document.body).appendChild(root);
295
+ this.dom.root = root;
296
+ this.dom.videoArea = root.querySelector(".sjdha-video-area");
297
+ this.dom.video = root.querySelector(".sjdha-video");
298
+ this.dom.queue = root.querySelector(".sjdha-queue");
299
+ this.dom.fullscreenBtn = root.querySelector(".sjdha-fullscreen-btn");
300
+ this.dom.autoplayOverlay = root.querySelector(".sjdha-autoplay-overlay");
301
+ this.dom.manualPlay = root.querySelector(".sjdha-manual-play");
302
+ this.dom.autoplaySpinner = root.querySelector(".sjdha-autoplay-spinner");
303
+ this.dom.aiResponse = root.querySelector(".sjdha-ai-response");
304
+ this.dom.aiResponseContent = root.querySelector(".sjdha-ai-response-content");
305
+ this.dom.chatArea = root.querySelector(".sjdha-chat-area");
306
+ this.dom.chatDate = root.querySelector(".sjdha-chat-date");
307
+ this.dom.chatList = root.querySelector(".sjdha-chat-list");
308
+ this.dom.modeBtn = root.querySelector(".sjdha-mode-btn");
309
+ this.dom.inputWrap = root.querySelector(".sjdha-input-wrap");
310
+ this.dom.input = root.querySelector(".sjdha-input");
311
+ this.dom.voiceInput = root.querySelector(".sjdha-voice-input");
312
+ this.dom.sendBtn = root.querySelector(".sjdha-send-btn");
313
+ this.dom.recording = root.querySelector(".sjdha-recording");
314
+ this.dom.dialog = root.querySelector(".sjdha-dialog-overlay");
315
+ this.dom.dialogConfirm = root.querySelector(".sjdha-dialog-btn");
316
+ this.dom.toast = root.querySelector(".sjdha-toast");
317
+ this.dom.toastContent = root.querySelector(".sjdha-toast-content");
318
+ this.dom.input.value = "";
319
+ this.dom.chatDate.textContent = this._formatDateHeader();
320
+ this._refreshModeButton();
321
+ this._refreshFullscreenButton();
322
+ };
323
+ SjDigitalHumanAssistantAdaptive.prototype.bindEvent = function () {
324
+ var that = this;
325
+ this.dom.fullscreenBtn.addEventListener("click", function (event) {
326
+ event.stopPropagation();
327
+ that.toggleVideoFullscreen();
328
+ });
329
+ this.dom.manualPlay.addEventListener("click", function (event) {
330
+ event.stopPropagation();
331
+ that.handleManualPlay();
332
+ });
333
+ this.dom.root.addEventListener("click", function () {
334
+ that.handleContainerClick();
335
+ });
336
+ this.dom.sendBtn.addEventListener("click", function () {
337
+ that.sendMessage();
338
+ });
339
+ this.dom.input.addEventListener("keyup", function (event) {
340
+ if (event.key === "Enter") {
341
+ that.sendMessage();
342
+ }
343
+ });
344
+ this.dom.input.addEventListener("input", function () {
345
+ that.renderStatus();
346
+ });
347
+ this.dom.modeBtn.addEventListener("click", function () {
348
+ that.toggleInputMode();
349
+ });
350
+ this.dom.voiceInput.addEventListener("touchstart", function (event) {
351
+ event.preventDefault();
352
+ that.startVoiceInput();
353
+ }, {
354
+ passive: false
355
+ });
356
+ this.dom.voiceInput.addEventListener("touchend", function (event) {
357
+ event.preventDefault();
358
+ that.stopVoiceInput();
359
+ }, {
360
+ passive: false
361
+ });
362
+ this.dom.voiceInput.addEventListener("touchcancel", function (event) {
363
+ event.preventDefault();
364
+ that.stopVoiceInput();
365
+ }, {
366
+ passive: false
367
+ });
368
+ this.dom.voiceInput.addEventListener("mousedown", function (event) {
369
+ event.preventDefault();
370
+ that.startVoiceInput();
371
+ });
372
+ this.dom.dialogConfirm.addEventListener("click", function () {
373
+ that.onReconnectConfirm();
374
+ });
375
+ this.dom.videoArea.addEventListener("mousedown", function (event) {
376
+ that.handleVideoAreaMouseDown(event);
377
+ });
378
+ this.resizeHandler = debounce(function () {
379
+ that.handleResize();
380
+ }, 100);
381
+ document.addEventListener("mouseup", function () {
382
+ that.stopVoiceInput();
383
+ });
384
+ document.addEventListener("mousemove", function (event) {
385
+ that.onDocMouseMove(event);
386
+ });
387
+ document.addEventListener("mouseup", function () {
388
+ that.onDocMouseUp();
389
+ });
390
+ window.addEventListener("resize", this.resizeHandler);
391
+ window.addEventListener("beforeunload", function () {
392
+ that.destroy();
393
+ });
394
+ this.dom.video.addEventListener("playing", function () {
395
+ that.markVideoReady();
396
+ });
397
+ };
398
+ SjDigitalHumanAssistantAdaptive.prototype.bootstrap = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
399
+ var _t;
400
+ return _regenerator().w(function (_context) {
401
+ while (1) switch (_context.p = _context.n) {
402
+ case 0:
403
+ _context.p = 0;
404
+ _context.n = 1;
405
+ return this.getSessionId();
406
+ case 1:
407
+ _context.n = 2;
408
+ return this.getPageDefaultData();
409
+ case 2:
410
+ this.startConnection();
411
+ _context.n = 4;
412
+ break;
413
+ case 3:
414
+ _context.p = 3;
415
+ _t = _context.v;
416
+ console.error("[SjDigitalHumanAssistantAdaptive] bootstrap failed", _t);
417
+ this.showToast("初始化失败,请稍后重试");
418
+ case 4:
419
+ return _context.a(2);
420
+ }
421
+ }, _callee, this, [[0, 3]]);
422
+ }));
423
+ SjDigitalHumanAssistantAdaptive.prototype.renderInitialMessages = function () {
424
+ if (this.config.defaultPlayText) {
425
+ this.chatMessages.push({
426
+ type: "system",
427
+ msg: this.config.defaultPlayText
428
+ });
429
+ this.renderChatList();
430
+ }
431
+ };
432
+ SjDigitalHumanAssistantAdaptive.prototype.detectMobileDevice = function () {
433
+ if (typeof window === "undefined") {
434
+ return true;
435
+ }
436
+ var narrowViewport = window.innerWidth <= this.config.mobileBreakpoint;
437
+ var coarsePointer = window.matchMedia && window.matchMedia("(pointer: coarse)").matches;
438
+ var hasTouchPoints = navigator.maxTouchPoints > 0 || "ontouchstart" in window;
439
+ var mobileUA = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|HarmonyOS|Windows Phone/i.test(navigator.userAgent);
440
+ return !!(narrowViewport || mobileUA && hasTouchPoints || coarsePointer);
441
+ };
442
+ SjDigitalHumanAssistantAdaptive.prototype.syncDeviceMode = function () {
443
+ this.state.isMobileDevice = this.detectMobileDevice();
444
+ this.updateRemBase();
445
+ this.dom.root.classList.toggle("device-mobile", this.state.isMobileDevice);
446
+ this.dom.root.classList.toggle("device-web", !this.state.isMobileDevice);
447
+ this.dom.root.classList.toggle("video-fullscreen", !!this.state.isVideoFullscreen);
448
+ this.dom.root.classList.toggle("video-split", !this.state.hasAvatarUrl);
449
+ if (!this.state.isMobileDevice) {
450
+ this.dom.root.style.width = this._normalizeSize(this.config.windowWidth) || "300px";
451
+ this.dom.root.style.height = this._normalizeSize(this.config.windowHeight) || "550px";
452
+ if (!this.state.dragPosition) {
453
+ this.dom.root.style.right = "24px";
454
+ this.dom.root.style.bottom = "24px";
455
+ }
456
+ } else {
457
+ this.state.dragPosition = null;
458
+ this.dom.root.style.left = "0";
459
+ this.dom.root.style.top = "0";
460
+ this.dom.root.style.right = "0";
461
+ this.dom.root.style.bottom = "0";
462
+ }
463
+
464
+ // 在全屏模式下保留输入栏可见,但隐藏聊天列表;非全屏时恢复默认显示
465
+ if (this.state.isVideoFullscreen) {
466
+ this.dom.chatArea.classList.remove("hidden");
467
+ // 全屏时隐藏聊天列表
468
+ this.dom.chatList.style.display = "none";
469
+ // 保证输入区在全屏下仍可见并按当前模式显示文本/语音控件
470
+ this.dom.inputWrap.style.display = this.state.isTextInputMode ? "block" : "none";
471
+ this.dom.sendBtn.style.display = this.state.isTextInputMode ? "block" : "none";
472
+ this.dom.voiceInput.style.display = this.state.isTextInputMode ? "none" : "block";
473
+ } else {
474
+ // 恢复聊天列表所有子项显示
475
+ this.dom.chatList.style.display = "block";
476
+ this.dom.chatArea.classList.remove("hidden");
477
+ }
478
+ this.dom.aiResponse.style.display = this.state.isVideoFullscreen && this.state.currentAiMessage ? "block" : "none";
479
+ this._refreshFullscreenButton();
480
+ this.dom.fullscreenBtn.style.display = this.state.hasAvatarUrl ? "flex" : "none";
481
+ };
482
+ SjDigitalHumanAssistantAdaptive.prototype.handleResize = function () {
483
+ this.updateRemBase();
484
+ this.syncDeviceMode();
485
+ this.clampDragPositionOnResize();
486
+ };
487
+ SjDigitalHumanAssistantAdaptive.prototype.toggleVideoFullscreen = function () {
488
+ this.state.isVideoFullscreen = !this.state.isVideoFullscreen;
489
+ this.syncDeviceMode();
490
+ if (!this.state.isVideoFullscreen) {
491
+ this.scrollChatToBottom();
492
+ }
493
+ };
494
+ SjDigitalHumanAssistantAdaptive.prototype.handleVideoAreaMouseDown = function (event) {
495
+ if (this.state.isMobileDevice || event.button !== 0 || this._isInteractiveDragTarget(event.target)) {
496
+ return;
497
+ }
498
+ event.preventDefault();
499
+ var rect = this.dom.root.getBoundingClientRect();
500
+ this.drag.startClientX = event.clientX;
501
+ this.drag.startClientY = event.clientY;
502
+ this.drag.startLeft = rect.left;
503
+ this.drag.startTop = rect.top;
504
+ this.state.isDragging = true;
505
+ this.state.dragMoved = false;
506
+ this.state.dragPosition = {
507
+ left: rect.left,
508
+ top: rect.top
509
+ };
510
+ this.dom.videoArea.style.cursor = "grabbing";
511
+ };
512
+ SjDigitalHumanAssistantAdaptive.prototype.onDocMouseMove = function (event) {
513
+ if (!this.state.isDragging) {
514
+ return;
515
+ }
516
+ var dx = event.clientX - this.drag.startClientX;
517
+ var dy = event.clientY - this.drag.startClientY;
518
+ if (!this.state.dragMoved && (Math.abs(dx) > 2 || Math.abs(dy) > 2)) {
519
+ this.state.dragMoved = true;
520
+ }
521
+ var rect = this.dom.root.getBoundingClientRect();
522
+ var next = this._clampPosition(this.drag.startLeft + dx, this.drag.startTop + dy, rect.width, rect.height);
523
+ this.state.dragPosition = next;
524
+ this.dom.root.style.left = next.left + "px";
525
+ this.dom.root.style.top = next.top + "px";
526
+ this.dom.root.style.right = "auto";
527
+ this.dom.root.style.bottom = "auto";
528
+ };
529
+ SjDigitalHumanAssistantAdaptive.prototype.onDocMouseUp = function () {
530
+ if (!this.state.isDragging) {
531
+ return;
532
+ }
533
+ this.state.isDragging = false;
534
+ this.dom.videoArea.style.cursor = "grab";
535
+ if (this.state.dragMoved) {
536
+ window.addEventListener("click", function blockClick(event) {
537
+ event.preventDefault();
538
+ event.stopPropagation();
539
+ window.removeEventListener("click", blockClick, true);
540
+ }, true);
541
+ }
542
+ };
543
+ SjDigitalHumanAssistantAdaptive.prototype.clampDragPositionOnResize = function () {
544
+ if (!this.state.dragPosition || this.state.isMobileDevice) {
545
+ return;
546
+ }
547
+ var rect = this.dom.root.getBoundingClientRect();
548
+ var next = this._clampPosition(this.state.dragPosition.left, this.state.dragPosition.top, rect.width, rect.height);
549
+ this.state.dragPosition = next;
550
+ this.dom.root.style.left = next.left + "px";
551
+ this.dom.root.style.top = next.top + "px";
552
+ };
553
+ SjDigitalHumanAssistantAdaptive.prototype.handleContainerClick = function () {
554
+ if (this.state.hasUnmuted) {
555
+ return;
556
+ }
557
+ if (this.dom.video && this.dom.video.muted) {
558
+ this.dom.video.muted = false;
559
+ if (this.dom.video.paused) {
560
+ this.dom.video.play();
561
+ }
562
+ this.state.hasUnmuted = true;
563
+ }
564
+ };
565
+ SjDigitalHumanAssistantAdaptive.prototype.handleManualPlay = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
566
+ return _regenerator().w(function (_context2) {
567
+ while (1) switch (_context2.p = _context2.n) {
568
+ case 0:
569
+ if (!(!this.dom.video || !this.state.isMediaReady)) {
570
+ _context2.n = 1;
571
+ break;
572
+ }
573
+ return _context2.a(2);
574
+ case 1:
575
+ _context2.p = 1;
576
+ _context2.n = 2;
577
+ return this.dom.video.play();
578
+ case 2:
579
+ this.dom.video.muted = false;
580
+ this.state.isAutoplayBlocked = false;
581
+ this.renderAutoplayState();
582
+ _context2.n = 4;
583
+ break;
584
+ case 3:
585
+ _context2.p = 3;
586
+ _context2.v;
587
+ this.showToast("视频播放失败,请点击按钮重试");
588
+ case 4:
589
+ return _context2.a(2);
590
+ }
591
+ }, _callee2, this, [[1, 3]]);
592
+ }));
593
+ SjDigitalHumanAssistantAdaptive.prototype.toggleInputMode = function () {
594
+ if (this.state.isTextInputMode && !this.state.isSSEConnected) {
595
+ this.showToast("消息服务未连接,请稍后再试");
596
+ return;
597
+ }
598
+ this.state.isTextInputMode = !this.state.isTextInputMode;
599
+ this._refreshModeButton();
600
+ this.dom.inputWrap.style.display = this.state.isTextInputMode ? "block" : "none";
601
+ this.dom.sendBtn.style.display = this.state.isTextInputMode ? "block" : "none";
602
+ this.dom.voiceInput.style.display = this.state.isTextInputMode ? "none" : "block";
603
+ if (!this.state.isTextInputMode) {
604
+ this.ensureRecorder();
605
+ } else {
606
+ this.releaseRecorder();
607
+ }
608
+ };
609
+ SjDigitalHumanAssistantAdaptive.prototype.sendMessage = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
610
+ var text, now;
611
+ return _regenerator().w(function (_context3) {
612
+ while (1) switch (_context3.n) {
613
+ case 0:
614
+ text = (this.dom.input.value || "").replace(/^\s+|\s+$/g, "");
615
+ if (text) {
616
+ _context3.n = 1;
617
+ break;
618
+ }
619
+ return _context3.a(2);
620
+ case 1:
621
+ if (this.state.isSSEConnected) {
622
+ _context3.n = 2;
623
+ break;
624
+ }
625
+ this.showToast("消息服务未连接,请稍后再试");
626
+ return _context3.a(2);
627
+ case 2:
628
+ _context3.n = 3;
629
+ return this.digitalHumanInterruptTalk();
630
+ case 3:
631
+ this.state.isReceivingReply = false;
632
+ this.state.currentAiMessage = "";
633
+ now = Date.now();
634
+ this.chatMessages.push({
635
+ type: "user",
636
+ msg: text,
637
+ timestamp: now,
638
+ showTime: this.shouldShowTimeLabel(now)
639
+ });
640
+ this.dom.input.value = "";
641
+ this.renderStatus();
642
+ this.renderChatList();
643
+ this.scrollChatToBottom();
644
+ _context3.n = 4;
645
+ return this.digitalHumanChat(text, "chat");
646
+ case 4:
647
+ return _context3.a(2);
648
+ }
649
+ }, _callee3, this);
650
+ }));
651
+ SjDigitalHumanAssistantAdaptive.prototype.receiveMessage = function (text) {
652
+ var cleanText = (text || "").replace(/^\s+|\s+$/g, "");
653
+ if (!cleanText) {
654
+ return;
655
+ }
656
+ var last = this.chatMessages[this.chatMessages.length - 1];
657
+ if (this.state.isReceivingReply && last && last.type === "system") {
658
+ last.msg += cleanText;
659
+ this.state.currentAiMessage += cleanText;
660
+ } else {
661
+ this.chatMessages.push({
662
+ type: "system",
663
+ msg: cleanText
664
+ });
665
+ this.state.isReceivingReply = true;
666
+ this.state.currentAiMessage = cleanText;
667
+ }
668
+ this.renderChatList();
669
+ this.scrollChatToBottom();
670
+ this.renderAiResponse();
671
+ };
672
+ SjDigitalHumanAssistantAdaptive.prototype.renderChatList = function () {
673
+ var list = this.dom.chatList;
674
+ var avatarStyle = this.state.avatarPic ? ' style="background-image:url(' + this._escapeHtml(this.state.avatarPic) + ')"' : "";
675
+ var html = "";
676
+ for (var i = 0; i < this.chatMessages.length; i++) {
677
+ var msg = this.chatMessages[i];
678
+ if (!msg) {
679
+ continue;
680
+ }
681
+ if (msg.type === "user" && msg.showTime && msg.timestamp) {
682
+ html += '<div class="sjdha-time-label"><span>' + this._escapeHtml(this.formatMessageTime(msg.timestamp)) + '</span></div>';
683
+ }
684
+ if (msg.type === "system" && msg.msg) {
685
+ html += '<div class="sjdha-chat-item system"><div class="sjdha-avatar"' + avatarStyle + '></div><div class="sjdha-bubble">' + this._escapeHtml(msg.msg) + '</div></div>';
686
+ } else if (msg.type === "user" && (msg.msg || msg.voiceUrl)) {
687
+ if (msg.voiceUrl) {
688
+ html += '<div class="sjdha-chat-item user" data-voice-index="' + i + '"><div class="sjdha-avatar"></div><div class="sjdha-bubble sjdha-voice-bubble"><div>' + this._escapeHtml(String(msg.duration || 0)) + '"</div><div class="sjdha-voice-icon' + (this.state.playingVoiceId === i ? ' playing' : '') + '"></div></div></div>';
689
+ } else {
690
+ html += '<div class="sjdha-chat-item user"><div class="sjdha-avatar"></div><div class="sjdha-bubble">' + this._escapeHtml(msg.msg) + '</div></div>';
691
+ }
692
+ }
693
+ }
694
+ // 如果用户将 .sjdha-chat-date 放入了 chat-list,保留其原始 HTML,避免被覆盖
695
+ var dateHtml = "";
696
+ try {
697
+ if (this.dom.chatDate && this.dom.chatDate.parentElement === list) {
698
+ dateHtml = this.dom.chatDate.outerHTML || "";
699
+ }
700
+ } catch (e) {
701
+ dateHtml = "";
702
+ }
703
+ list.innerHTML = dateHtml + html;
704
+
705
+ // 重新绑定 chatDate 引用(如果存在于 list 中的话)
706
+ if (list.querySelector) {
707
+ var newDate = list.querySelector('.sjdha-chat-date');
708
+ if (newDate) {
709
+ this.dom.chatDate = newDate;
710
+ }
711
+ }
712
+ var voiceNodes = list.querySelectorAll("[data-voice-index]");
713
+ var that = this;
714
+ for (var j = 0; j < voiceNodes.length; j++) {
715
+ voiceNodes[j].addEventListener("click", function () {
716
+ var idx = parseInt(this.getAttribute("data-voice-index"), 10);
717
+ that.playVoice(that.chatMessages[idx], idx);
718
+ });
719
+ }
720
+ this.renderAiResponse();
721
+ };
722
+ SjDigitalHumanAssistantAdaptive.prototype.renderAiResponse = function () {
723
+ this.dom.aiResponseContent.textContent = this.state.currentAiMessage || "";
724
+ this.dom.aiResponse.style.display = this.state.isVideoFullscreen && this.state.currentAiMessage ? "block" : "none";
725
+ };
726
+ SjDigitalHumanAssistantAdaptive.prototype.scrollChatToBottom = function () {
727
+ var that = this;
728
+ setTimeout(function () {
729
+ that.dom.chatList.scrollTop = that.dom.chatList.scrollHeight;
730
+ that.dom.aiResponseContent.scrollTop = that.dom.aiResponseContent.scrollHeight;
731
+ }, 0);
732
+ };
733
+ SjDigitalHumanAssistantAdaptive.prototype.startConnection = function () {
734
+ var that = this;
735
+ if (this.state.isConnected || this.state.isConnecting) {
736
+ return;
737
+ }
738
+ var hlsUrl = this.state.currentHlsUrl;
739
+ var rtsUrl = this.state.currentRtsUrl;
740
+ if (!hlsUrl && !rtsUrl) {
741
+ if (this.state.defaultVideoUrl) {
742
+ this.dom.video.src = this.state.defaultVideoUrl;
743
+ this.state.isMediaReady = true;
744
+ this.dom.video.play().then(function () {
745
+ that.state.isAutoplayBlocked = false;
746
+ that.renderAutoplayState();
747
+ }).catch(function () {
748
+ that.state.isAutoplayBlocked = true;
749
+ that.renderAutoplayState();
750
+ });
751
+ }
752
+ return;
753
+ }
754
+ this.state.isConnecting = true;
755
+ this.state.isManualDisconnect = false;
756
+ this.renderStatus();
757
+ this.cleanupVideo();
758
+ this.destroyRts();
759
+ if (rtsUrl) {
760
+ this.tryPlayWithRts(rtsUrl, hlsUrl);
761
+ return;
762
+ }
763
+ this.playWithHls(hlsUrl);
764
+ };
765
+ SjDigitalHumanAssistantAdaptive.prototype.tryPlayWithRts = function (rtsUrl, hlsUrl) {
766
+ var AliRtsCtor = this.config.aliRtsClass;
767
+ var that = this;
768
+ if (!AliRtsCtor || !AliRtsCtor.createClient) {
769
+ this.playWithHls(hlsUrl);
770
+ return;
771
+ }
772
+ this.stream.aliRts = AliRtsCtor.createClient();
773
+ if (this.stream.aliRts.on) {
774
+ this.stream.aliRts.on("onError", function (error) {
775
+ console.error("[SjDigitalHumanAssistantAdaptive] RTS error", error);
776
+ });
777
+ }
778
+ this.stream.aliRts.isSupport({
779
+ isReceiveVideo: true
780
+ }).then(function () {
781
+ that.subscribeRts(rtsUrl, hlsUrl);
782
+ }).catch(function () {
783
+ that.destroyRts();
784
+ that.playWithHls(hlsUrl);
785
+ });
786
+ };
787
+ SjDigitalHumanAssistantAdaptive.prototype.subscribeRts = function (rtsUrl, hlsUrl) {
788
+ var that = this;
789
+ this.stream.aliRts.subscribe(rtsUrl, {
790
+ mediaTimeout: 6000,
791
+ retryTimes: 3,
792
+ retryInterval: 2000
793
+ }).then(function (remoteStream) {
794
+ if (remoteStream && remoteStream.play) {
795
+ remoteStream.play(that.dom.video);
796
+ }
797
+ that.state.isMediaReady = true;
798
+ that.state.isConnected = true;
799
+ that.state.isConnecting = false;
800
+ that.state.currentReconnectTimes = 0;
801
+ that.clearReconnectTimer();
802
+ that.dom.video.play().then(function () {
803
+ that.state.isAutoplayBlocked = false;
804
+ that.renderAutoplayState();
805
+ }).catch(function () {
806
+ that.state.isAutoplayBlocked = true;
807
+ that.renderAutoplayState();
808
+ });
809
+ that.renderStatus();
810
+ }).catch(function () {
811
+ that.destroyRts();
812
+ that.playWithHls(hlsUrl);
813
+ });
814
+ };
815
+ SjDigitalHumanAssistantAdaptive.prototype.playWithHls = function (hlsUrl) {
816
+ var that = this;
817
+ var HlsCtor = this.config.hlsClass;
818
+ if (!hlsUrl) {
819
+ this.state.isConnecting = false;
820
+ this.renderStatus();
821
+ this.reconnect();
822
+ return;
823
+ }
824
+ if (this.stream.hls && this.stream.hls.destroy) {
825
+ this.stream.hls.destroy();
826
+ this.stream.hls = null;
827
+ }
828
+ if (HlsCtor && HlsCtor.isSupported && HlsCtor.isSupported()) {
829
+ this.stream.hls = new HlsCtor({
830
+ maxBufferLength: 30,
831
+ maxMaxBufferLength: 60,
832
+ liveSyncDurationCount: 2,
833
+ fragLoadingMaxRetry: 10,
834
+ fragLoadingRetryDelay: 1000,
835
+ manifestLoadingMaxRetry: 4,
836
+ levelLoadingMaxRetry: 4,
837
+ enableWorker: true,
838
+ lowLatencyMode: false
839
+ });
840
+ this.stream.hls.loadSource(hlsUrl);
841
+ this.stream.hls.attachMedia(this.dom.video);
842
+ this.stream.hls.on(HlsCtor.Events.MANIFEST_PARSED, function () {
843
+ that.state.isMediaReady = true;
844
+ that.dom.video.play().then(function () {
845
+ that.state.isAutoplayBlocked = false;
846
+ that.renderAutoplayState();
847
+ }).catch(function () {
848
+ that.state.isAutoplayBlocked = true;
849
+ that.renderAutoplayState();
850
+ });
851
+ });
852
+ this.stream.hls.on(HlsCtor.Events.ERROR, function (event, data) {
853
+ if (data && data.fatal) {
854
+ that.state.isConnected = false;
855
+ that.state.isConnecting = false;
856
+ that.renderStatus();
857
+ that.reconnect();
858
+ }
859
+ });
860
+ } else if (this.dom.video.canPlayType("application/vnd.apple.mpegurl")) {
861
+ this.dom.video.src = hlsUrl;
862
+ this.dom.video.load();
863
+ this.dom.video.addEventListener("loadedmetadata", function onLoaded() {
864
+ that.dom.video.removeEventListener("loadedmetadata", onLoaded);
865
+ that.state.isConnected = true;
866
+ that.state.isConnecting = false;
867
+ that.state.isMediaReady = true;
868
+ that.state.currentReconnectTimes = 0;
869
+ that.clearReconnectTimer();
870
+ that.renderStatus();
871
+ });
872
+ this.dom.video.play().then(function () {
873
+ that.state.isAutoplayBlocked = false;
874
+ that.renderAutoplayState();
875
+ }).catch(function () {
876
+ that.state.isAutoplayBlocked = true;
877
+ that.renderAutoplayState();
878
+ });
879
+ } else {
880
+ this.state.isConnecting = false;
881
+ this.renderStatus();
882
+ this.reconnect();
883
+ }
884
+ };
885
+ SjDigitalHumanAssistantAdaptive.prototype.markVideoReady = function () {
886
+ this.state.isConnected = true;
887
+ this.state.isConnecting = false;
888
+ this.state.currentReconnectTimes = 0;
889
+ this.state.isMediaReady = true;
890
+ this.clearReconnectTimer();
891
+ this.renderStatus();
892
+ this.state.isAutoplayBlocked = false;
893
+ this.state.needManualPlay = false;
894
+ this.renderAutoplayState();
895
+ };
896
+ SjDigitalHumanAssistantAdaptive.prototype.renderStatus = function () {
897
+ this.dom.sendBtn.disabled = !this.state.isSSEConnected || !(this.dom.input.value || "").replace(/^\s+|\s+$/g, "");
898
+ };
899
+ SjDigitalHumanAssistantAdaptive.prototype.renderAutoplayState = function () {
900
+ var overlayVisible = this.state.isAutoplayBlocked;
901
+ this.dom.autoplayOverlay.classList.toggle("hidden", !overlayVisible);
902
+ this.dom.manualPlay.style.display = this.state.needManualPlay ? "flex" : "none";
903
+ this.dom.autoplaySpinner.style.display = this.state.needManualPlay ? "none" : "block";
904
+ };
905
+ SjDigitalHumanAssistantAdaptive.prototype.reconnect = function () {
906
+ var that = this;
907
+ if (this.state.isManualDisconnect || this.state.isConnected || this.state.isConnecting || this.state.isWaitingReconnect) {
908
+ return;
909
+ }
910
+ if (this.config.maxReconnectTimes !== -1 && this.state.currentReconnectTimes >= this.config.maxReconnectTimes) {
911
+ return;
912
+ }
913
+ this.state.isWaitingReconnect = true;
914
+ this.state.currentReconnectTimes += 1;
915
+ this.stream.reconnectTimer = setTimeout(function () {
916
+ that.state.isWaitingReconnect = false;
917
+ if (!that.state.isConnected && !that.state.isConnecting) {
918
+ that.startConnection();
919
+ }
920
+ }, this.config.reconnectInterval * 1000);
921
+ };
922
+ SjDigitalHumanAssistantAdaptive.prototype.clearReconnectTimer = function () {
923
+ if (this.stream.reconnectTimer) {
924
+ clearTimeout(this.stream.reconnectTimer);
925
+ this.stream.reconnectTimer = null;
926
+ }
927
+ this.state.isWaitingReconnect = false;
928
+ };
929
+ SjDigitalHumanAssistantAdaptive.prototype.startSSE = function () {
930
+ var that = this;
931
+ if (!this.state.chatSessionId) {
932
+ return;
933
+ }
934
+ fetch(this._buildUrl("/stream_llm"), {
935
+ method: "POST",
936
+ headers: {
937
+ "Content-Type": "application/json"
938
+ },
939
+ body: JSON.stringify({
940
+ chatSessionId: parseInt(this.state.chatSessionId, 10),
941
+ sessionid: parseInt(this.state.sessionid || 0, 10)
942
+ })
943
+ }).then(function (response) {
944
+ that.state.isSSEConnected = true;
945
+ that.sseReconnectCount = 0;
946
+ that.renderStatus();
947
+ if (!that.state.hasAvatarUrl) {
948
+ that.fetchSessionStatus();
949
+ }
950
+ if (!response.body) {
951
+ return;
952
+ }
953
+ that.stream.sseReader = response.body.getReader();
954
+ that.consumeSSE();
955
+ }).catch(function (error) {
956
+ console.error("[SjDigitalHumanAssistantAdaptive] SSE failed", error);
957
+ that.state.isSSEConnected = false;
958
+ that.renderStatus();
959
+ });
960
+ };
961
+ SjDigitalHumanAssistantAdaptive.prototype.consumeSSE = function () {
962
+ var that = this;
963
+ var buffer = "";
964
+ function read() {
965
+ if (!that.stream.sseReader) {
966
+ return;
967
+ }
968
+ that.stream.sseReader.read().then(function (result) {
969
+ if (result.done) {
970
+ that.state.isSSEConnected = false;
971
+ that.renderStatus();
972
+ return;
973
+ }
974
+ buffer += new TextDecoder().decode(result.value);
975
+ var lines = buffer.split("\n");
976
+ buffer = lines.pop() || "";
977
+ for (var i = 0; i < lines.length; i++) {
978
+ that.handleSSELine(lines[i]);
979
+ }
980
+ read();
981
+ }).catch(function () {
982
+ that.reconnectSSE();
983
+ });
984
+ }
985
+ read();
986
+ };
987
+ SjDigitalHumanAssistantAdaptive.prototype.handleSSELine = function (line) {
988
+ if (!line || line.indexOf("data:") !== 0) {
989
+ return;
990
+ }
991
+ var rawData = line.slice(5).replace(/^\s+|\s+$/g, "");
992
+ if (!rawData || rawData === "<endofanswer>") {
993
+ return;
994
+ }
995
+ if (rawData.indexOf("[ERROR]") !== -1) {
996
+ if (rawData === "[ERROR] Disconnected by server") {
997
+ this.promptReconnect();
998
+ return;
999
+ }
1000
+ this.reconnectSSE();
1001
+ return;
1002
+ }
1003
+ try {
1004
+ var jsonData = JSON.parse(rawData);
1005
+ if (jsonData && jsonData.text) {
1006
+ if (jsonData.text.indexOf("[ERROR]") !== -1) {
1007
+ this.reconnectSSE();
1008
+ return;
1009
+ }
1010
+ this.receiveMessage(jsonData.text);
1011
+ return;
1012
+ }
1013
+ if (typeof jsonData === "string") {
1014
+ this.receiveMessage(jsonData);
1015
+ return;
1016
+ }
1017
+ } catch (error) {
1018
+ this.receiveMessage(rawData);
1019
+ }
1020
+ };
1021
+ SjDigitalHumanAssistantAdaptive.prototype.reconnectSSE = function () {
1022
+ var that = this;
1023
+ if (this.sseReconnectCount >= 5) {
1024
+ return;
1025
+ }
1026
+ this.sseReconnectCount += 1;
1027
+ if (this.stream.sseReader) {
1028
+ this.stream.sseReader.cancel();
1029
+ this.stream.sseReader = null;
1030
+ }
1031
+ this.state.isSSEConnected = false;
1032
+ this.renderStatus();
1033
+ if (this.stream.sseReconnectTimer) {
1034
+ clearTimeout(this.stream.sseReconnectTimer);
1035
+ }
1036
+ this.stream.sseReconnectTimer = setTimeout(function () {
1037
+ that.startSSE();
1038
+ }, 1000);
1039
+ };
1040
+ SjDigitalHumanAssistantAdaptive.prototype.promptReconnect = function () {
1041
+ var that = this;
1042
+ this.state.showReconnectDialog = true;
1043
+ this.dom.dialog.classList.add("show");
1044
+ this.state.isConnected = false;
1045
+ this.state.isMediaReady = false;
1046
+ this.state.isAutoplayBlocked = true;
1047
+ this.state.isConnecting = false;
1048
+ this.state.isSSEConnected = false;
1049
+ this.renderAutoplayState();
1050
+ this.renderStatus();
1051
+ if (this.stream.sseReader) {
1052
+ this.stream.sseReader.cancel();
1053
+ this.stream.sseReader = null;
1054
+ }
1055
+ if (this.stream.hls && this.stream.hls.destroy) {
1056
+ this.stream.hls.destroy();
1057
+ this.stream.hls = null;
1058
+ }
1059
+ this.destroyRts();
1060
+ this.cleanupVideo();
1061
+ this.reconnectDialogCallback = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() {
1062
+ return _regenerator().w(function (_context4) {
1063
+ while (1) switch (_context4.n) {
1064
+ case 0:
1065
+ _context4.n = 1;
1066
+ return that.getSessionId();
1067
+ case 1:
1068
+ that.startConnection();
1069
+ case 2:
1070
+ return _context4.a(2);
1071
+ }
1072
+ }, _callee4);
1073
+ }));
1074
+ };
1075
+ SjDigitalHumanAssistantAdaptive.prototype.onReconnectConfirm = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5() {
1076
+ return _regenerator().w(function (_context5) {
1077
+ while (1) switch (_context5.n) {
1078
+ case 0:
1079
+ this.state.showReconnectDialog = false;
1080
+ this.dom.dialog.classList.remove("show");
1081
+ if (!this.reconnectDialogCallback) {
1082
+ _context5.n = 2;
1083
+ break;
1084
+ }
1085
+ _context5.n = 1;
1086
+ return this.reconnectDialogCallback();
1087
+ case 1:
1088
+ this.reconnectDialogCallback = null;
1089
+ case 2:
1090
+ return _context5.a(2);
1091
+ }
1092
+ }, _callee5, this);
1093
+ }));
1094
+ SjDigitalHumanAssistantAdaptive.prototype.digitalHumanChat = /*#__PURE__*/function () {
1095
+ var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(text, type) {
1096
+ var _t3;
1097
+ return _regenerator().w(function (_context6) {
1098
+ while (1) switch (_context6.p = _context6.n) {
1099
+ case 0:
1100
+ if (this.state.isSSEConnected) {
1101
+ _context6.n = 1;
1102
+ break;
1103
+ }
1104
+ return _context6.a(2);
1105
+ case 1:
1106
+ type = type || "echo";
1107
+ _context6.p = 2;
1108
+ _context6.n = 3;
1109
+ return this._request("/human", "POST", {
1110
+ chatSessionId: parseInt(this.state.chatSessionId, 10),
1111
+ sessionid: parseInt(this.state.sessionid || 0, 10),
1112
+ type: type,
1113
+ text: text
1114
+ });
1115
+ case 3:
1116
+ _context6.n = 5;
1117
+ break;
1118
+ case 4:
1119
+ _context6.p = 4;
1120
+ _t3 = _context6.v;
1121
+ console.error("[SjDigitalHumanAssistantAdaptive] send chat failed", _t3);
1122
+ case 5:
1123
+ return _context6.a(2);
1124
+ }
1125
+ }, _callee6, this, [[2, 4]]);
1126
+ }));
1127
+ return function (_x, _x2) {
1128
+ return _ref6.apply(this, arguments);
1129
+ };
1130
+ }();
1131
+ SjDigitalHumanAssistantAdaptive.prototype.digitalHumanInterruptTalk = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7() {
1132
+ var _t4;
1133
+ return _regenerator().w(function (_context7) {
1134
+ while (1) switch (_context7.p = _context7.n) {
1135
+ case 0:
1136
+ if (this.state.chatSessionId) {
1137
+ _context7.n = 1;
1138
+ break;
1139
+ }
1140
+ return _context7.a(2);
1141
+ case 1:
1142
+ _context7.p = 1;
1143
+ _context7.n = 2;
1144
+ return this._request("/interrupt_talk", "POST", {
1145
+ chatSessionId: parseInt(this.state.chatSessionId, 10),
1146
+ sessionid: parseInt(this.state.sessionid || 0, 10)
1147
+ });
1148
+ case 2:
1149
+ _context7.n = 4;
1150
+ break;
1151
+ case 3:
1152
+ _context7.p = 3;
1153
+ _t4 = _context7.v;
1154
+ console.error("[SjDigitalHumanAssistantAdaptive] interrupt failed", _t4);
1155
+ case 4:
1156
+ return _context7.a(2);
1157
+ }
1158
+ }, _callee7, this, [[1, 3]]);
1159
+ }));
1160
+ SjDigitalHumanAssistantAdaptive.prototype.getPageDefaultData = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8() {
1161
+ var response, result, entity;
1162
+ return _regenerator().w(function (_context8) {
1163
+ while (1) switch (_context8.n) {
1164
+ case 0:
1165
+ _context8.n = 1;
1166
+ return fetch(this._buildUrl("/daxi/avatar/info?id=" + this.config.avatarId), {
1167
+ method: "GET"
1168
+ });
1169
+ case 1:
1170
+ response = _context8.v;
1171
+ if (response.ok) {
1172
+ _context8.n = 2;
1173
+ break;
1174
+ }
1175
+ throw new Error("get default data failed");
1176
+ case 2:
1177
+ _context8.n = 3;
1178
+ return response.json();
1179
+ case 3:
1180
+ result = _context8.v;
1181
+ entity = result && result.data && result.data.entities ? result.data.entities[0] : null;
1182
+ if (entity) {
1183
+ _context8.n = 4;
1184
+ break;
1185
+ }
1186
+ return _context8.a(2);
1187
+ case 4:
1188
+ this.state.posterBgUrl = entity.imageDefault || "";
1189
+ this.state.videoEleBg = entity.coverImageUrl || "";
1190
+ this.state.defaultVideoUrl = entity.videoDefault || "";
1191
+ this.state.avatarPic = entity.characterAvatar || "";
1192
+ if (this.state.posterBgUrl) {
1193
+ this.dom.video.setAttribute("poster", this.state.posterBgUrl);
1194
+ }
1195
+ if (this.state.videoEleBg) {
1196
+ this.dom.videoArea.style.backgroundImage = "url(" + this.state.videoEleBg + ")";
1197
+ }
1198
+ this.renderChatList();
1199
+ case 5:
1200
+ return _context8.a(2);
1201
+ }
1202
+ }, _callee8, this);
1203
+ }));
1204
+ SjDigitalHumanAssistantAdaptive.prototype.getSessionId = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9() {
1205
+ var result, urlItem;
1206
+ return _regenerator().w(function (_context9) {
1207
+ while (1) switch (_context9.n) {
1208
+ case 0:
1209
+ _context9.n = 1;
1210
+ return this._request("/connect", "POST", {
1211
+ accessToken: this.config.accessToken
1212
+ });
1213
+ case 1:
1214
+ result = _context9.v;
1215
+ this.state.chatSessionId = result && result.chatSessionId != null ? String(result.chatSessionId) : "";
1216
+ this.state.sessionid = result && result.sessionid != null ? String(result.sessionid) : "";
1217
+ if (this.state.chatSessionId) {
1218
+ this.startSSE();
1219
+ }
1220
+ if (result && result.sessionid != null && Number(result.sessionid) >= 0) {
1221
+ urlItem = result.sessionid && Number(result.sessionid) === 0 ? result.sessionid : "";
1222
+ if (result.hlsAvatarUrl) {
1223
+ this.state.currentRtsUrl = "artc://" + String(result.hlsAvatarUrl).replace(/^https:\/\//, "").replace(/\.m3u8$/, "") + urlItem;
1224
+ }
1225
+ }
1226
+ if (result && result.hlsAvatarUrl) {
1227
+ this.state.currentHlsUrl = result.hlsAvatarUrl;
1228
+ this.state.hasAvatarUrl = true;
1229
+ } else {
1230
+ this.state.currentHlsUrl = "";
1231
+ this.state.currentRtsUrl = "";
1232
+ this.state.hasAvatarUrl = false;
1233
+ this.state.isVideoFullscreen = false;
1234
+ }
1235
+ this.syncDeviceMode();
1236
+ return _context9.a(2, result);
1237
+ }
1238
+ }, _callee9, this);
1239
+ }));
1240
+ SjDigitalHumanAssistantAdaptive.prototype.fetchSessionStatus = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0() {
1241
+ var result, activeSessions, maxSessions, waitingCount, _t5;
1242
+ return _regenerator().w(function (_context0) {
1243
+ while (1) switch (_context0.p = _context0.n) {
1244
+ case 0:
1245
+ _context0.p = 0;
1246
+ _context0.n = 1;
1247
+ return this._request("/session_status", "GET");
1248
+ case 1:
1249
+ result = _context0.v;
1250
+ activeSessions = result && result.active_chat_sessions ? result.active_chat_sessions : 0;
1251
+ maxSessions = result && result.max_sessions ? result.max_sessions : 0;
1252
+ waitingCount = activeSessions - maxSessions;
1253
+ this.state.queueCount = waitingCount > 0 ? waitingCount : 0;
1254
+ this.dom.queue.textContent = this.state.queueCount ? "语音回复排队中,前方" + this.state.queueCount + "人" : "";
1255
+ this.dom.queue.style.display = this.state.queueCount ? "block" : "none";
1256
+ _context0.n = 3;
1257
+ break;
1258
+ case 2:
1259
+ _context0.p = 2;
1260
+ _t5 = _context0.v;
1261
+ console.error("[SjDigitalHumanAssistantAdaptive] session status failed", _t5);
1262
+ case 3:
1263
+ return _context0.a(2);
1264
+ }
1265
+ }, _callee0, this, [[0, 2]]);
1266
+ }));
1267
+ SjDigitalHumanAssistantAdaptive.prototype.ensureRecorder = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1() {
1268
+ var that;
1269
+ return _regenerator().w(function (_context1) {
1270
+ while (1) switch (_context1.n) {
1271
+ case 0:
1272
+ if (!(this.audio.mediaStream || this.audio.permissionPromise)) {
1273
+ _context1.n = 1;
1274
+ break;
1275
+ }
1276
+ return _context1.a(2, this.audio.permissionPromise || Promise.resolve(true));
1277
+ case 1:
1278
+ that = this;
1279
+ this.audio.permissionPromise = navigator.mediaDevices.getUserMedia({
1280
+ audio: true
1281
+ }).then(function (stream) {
1282
+ that.audio.mediaStream = stream;
1283
+ that.audio.permissionPromise = null;
1284
+ return true;
1285
+ }).catch(function () {
1286
+ that.audio.permissionPromise = null;
1287
+ that.showToast("无法访问麦克风");
1288
+ return false;
1289
+ });
1290
+ return _context1.a(2, this.audio.permissionPromise);
1291
+ }
1292
+ }, _callee1, this);
1293
+ }));
1294
+ SjDigitalHumanAssistantAdaptive.prototype.startVoiceInput = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee10() {
1295
+ var ready;
1296
+ return _regenerator().w(function (_context10) {
1297
+ while (1) switch (_context10.n) {
1298
+ case 0:
1299
+ if (!this.state.isRecording) {
1300
+ _context10.n = 1;
1301
+ break;
1302
+ }
1303
+ return _context10.a(2);
1304
+ case 1:
1305
+ this.stopRequestedBeforeStart = false;
1306
+ this.isButtonPressed = true;
1307
+ if (this.dom.video) {
1308
+ this.dom.video.muted = true;
1309
+ }
1310
+ _context10.n = 2;
1311
+ return this.digitalHumanInterruptTalk();
1312
+ case 2:
1313
+ if (!(this.stopRequestedBeforeStart || !this.isButtonPressed)) {
1314
+ _context10.n = 3;
1315
+ break;
1316
+ }
1317
+ this.isButtonPressed = false;
1318
+ return _context10.a(2);
1319
+ case 3:
1320
+ _context10.n = 4;
1321
+ return this.ensureRecorder();
1322
+ case 4:
1323
+ ready = _context10.v;
1324
+ if (!(!ready || this.stopRequestedBeforeStart || !this.isButtonPressed)) {
1325
+ _context10.n = 5;
1326
+ break;
1327
+ }
1328
+ this.isButtonPressed = false;
1329
+ return _context10.a(2);
1330
+ case 5:
1331
+ this.beginRecordStream();
1332
+ this.state.isRecording = true;
1333
+ this.dom.recording.classList.add("show");
1334
+ case 6:
1335
+ return _context10.a(2);
1336
+ }
1337
+ }, _callee10, this);
1338
+ }));
1339
+ SjDigitalHumanAssistantAdaptive.prototype.beginRecordStream = function () {
1340
+ var AudioContextCtor = window.AudioContext || window.webkitAudioContext;
1341
+ if (!this.audio.mediaStream || !AudioContextCtor) {
1342
+ return;
1343
+ }
1344
+ this.audio.audioContext = new AudioContextCtor();
1345
+ this.audio.sourceNode = this.audio.audioContext.createMediaStreamSource(this.audio.mediaStream);
1346
+ this.audio.processorNode = this.audio.audioContext.createScriptProcessor(4096, 1, 1);
1347
+ this.audio.chunks = [];
1348
+ this.audio.startTime = Date.now();
1349
+ var that = this;
1350
+ this.audio.processorNode.onaudioprocess = function (event) {
1351
+ var input = event.inputBuffer.getChannelData(0);
1352
+ that.audio.chunks.push(new Float32Array(input));
1353
+ };
1354
+ this.audio.sourceNode.connect(this.audio.processorNode);
1355
+ this.audio.processorNode.connect(this.audio.audioContext.destination);
1356
+ };
1357
+ SjDigitalHumanAssistantAdaptive.prototype.stopVoiceInput = function () {
1358
+ this.stopRequestedBeforeStart = true;
1359
+ this.isButtonPressed = false;
1360
+ if (this.dom.video) {
1361
+ this.dom.video.muted = false;
1362
+ }
1363
+ if (!this.state.isRecording) {
1364
+ return;
1365
+ }
1366
+ this.state.isRecording = false;
1367
+ this.dom.recording.classList.remove("show");
1368
+ var duration = Math.round((Date.now() - this.audio.startTime) / 1000);
1369
+ var blob = this.finishRecordStream();
1370
+ if (!blob || duration < 1) {
1371
+ this.showToast("录音时间太短");
1372
+ return;
1373
+ }
1374
+ this.sendVoiceMessage(blob, duration);
1375
+ };
1376
+ SjDigitalHumanAssistantAdaptive.prototype.finishRecordStream = function () {
1377
+ if (!this.audio.audioContext) {
1378
+ return null;
1379
+ }
1380
+ if (this.audio.processorNode) {
1381
+ this.audio.processorNode.disconnect();
1382
+ this.audio.processorNode = null;
1383
+ }
1384
+ if (this.audio.sourceNode) {
1385
+ this.audio.sourceNode.disconnect();
1386
+ this.audio.sourceNode = null;
1387
+ }
1388
+ var sampleRate = this.audio.audioContext.sampleRate;
1389
+ this.audio.audioContext.close();
1390
+ this.audio.audioContext = null;
1391
+ var merged = this.mergeAudioChunks(this.audio.chunks);
1392
+ this.audio.chunks = [];
1393
+ if (!merged.length) {
1394
+ return null;
1395
+ }
1396
+ var downsampled = this.downsampleBuffer(merged, sampleRate, 16000);
1397
+ return this.encodeWav(downsampled, 16000);
1398
+ };
1399
+ SjDigitalHumanAssistantAdaptive.prototype.releaseRecorder = function () {
1400
+ this.stopRequestedBeforeStart = true;
1401
+ this.isButtonPressed = false;
1402
+ this.state.isRecording = false;
1403
+ if (this.dom.recording) {
1404
+ this.dom.recording.classList.remove("show");
1405
+ }
1406
+ if (this.audio.processorNode) {
1407
+ this.audio.processorNode.disconnect();
1408
+ this.audio.processorNode = null;
1409
+ }
1410
+ if (this.audio.sourceNode) {
1411
+ this.audio.sourceNode.disconnect();
1412
+ this.audio.sourceNode = null;
1413
+ }
1414
+ if (this.audio.audioContext) {
1415
+ this.audio.audioContext.close();
1416
+ this.audio.audioContext = null;
1417
+ }
1418
+ if (this.audio.mediaStream) {
1419
+ var tracks = this.audio.mediaStream.getTracks();
1420
+ for (var i = 0; i < tracks.length; i++) {
1421
+ tracks[i].stop();
1422
+ }
1423
+ this.audio.mediaStream = null;
1424
+ }
1425
+ this.audio.permissionPromise = null;
1426
+ this.audio.chunks = [];
1427
+ if (this.dom.video) {
1428
+ this.dom.video.muted = false;
1429
+ }
1430
+ };
1431
+ SjDigitalHumanAssistantAdaptive.prototype.sendVoiceMessage = /*#__PURE__*/function () {
1432
+ var _ref11 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee11(blob, durationSec) {
1433
+ var voiceUrl, now, formData, response, result, _t6;
1434
+ return _regenerator().w(function (_context11) {
1435
+ while (1) switch (_context11.p = _context11.n) {
1436
+ case 0:
1437
+ voiceUrl = URL.createObjectURL(blob);
1438
+ now = Date.now();
1439
+ this.chatMessages.push({
1440
+ type: "user",
1441
+ msg: "[语音]",
1442
+ timestamp: now,
1443
+ showTime: this.shouldShowTimeLabel(now),
1444
+ voiceUrl: voiceUrl,
1445
+ duration: durationSec
1446
+ });
1447
+ this.renderChatList();
1448
+ this.scrollChatToBottom();
1449
+ _context11.p = 1;
1450
+ formData = new FormData();
1451
+ formData.append("audio", blob, "recording.wav");
1452
+ formData.append("duration", String(durationSec));
1453
+ formData.append("chatSessionId", this.state.chatSessionId);
1454
+ formData.append("sessionid", this.state.sessionid);
1455
+ _context11.n = 2;
1456
+ return fetch(this._buildUrl("/stt"), {
1457
+ method: "POST",
1458
+ body: formData
1459
+ });
1460
+ case 2:
1461
+ response = _context11.v;
1462
+ if (response.ok) {
1463
+ _context11.n = 3;
1464
+ break;
1465
+ }
1466
+ throw new Error("voice post failed");
1467
+ case 3:
1468
+ _context11.n = 4;
1469
+ return response.json();
1470
+ case 4:
1471
+ result = _context11.v;
1472
+ if (!(result && result.result)) {
1473
+ _context11.n = 5;
1474
+ break;
1475
+ }
1476
+ _context11.n = 5;
1477
+ return this.digitalHumanChat(String(result.result).replace(/^\s+|\s+$/g, ""), "chat");
1478
+ case 5:
1479
+ _context11.n = 7;
1480
+ break;
1481
+ case 6:
1482
+ _context11.p = 6;
1483
+ _t6 = _context11.v;
1484
+ console.error("[SjDigitalHumanAssistantAdaptive] send voice failed", _t6);
1485
+ case 7:
1486
+ return _context11.a(2);
1487
+ }
1488
+ }, _callee11, this, [[1, 6]]);
1489
+ }));
1490
+ return function (_x3, _x4) {
1491
+ return _ref11.apply(this, arguments);
1492
+ };
1493
+ }();
1494
+ SjDigitalHumanAssistantAdaptive.prototype.playVoice = /*#__PURE__*/function () {
1495
+ var _ref12 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee12(msg, idx) {
1496
+ var that, audio;
1497
+ return _regenerator().w(function (_context12) {
1498
+ while (1) switch (_context12.n) {
1499
+ case 0:
1500
+ that = this;
1501
+ if (!(!msg || !msg.voiceUrl)) {
1502
+ _context12.n = 1;
1503
+ break;
1504
+ }
1505
+ return _context12.a(2);
1506
+ case 1:
1507
+ if (this.audio.currentVoiceAudio) {
1508
+ this.audio.currentVoiceAudio.pause();
1509
+ this.audio.currentVoiceAudio = null;
1510
+ }
1511
+ if (!(this.state.playingVoiceId === idx)) {
1512
+ _context12.n = 2;
1513
+ break;
1514
+ }
1515
+ this.state.playingVoiceId = null;
1516
+ this.renderChatList();
1517
+ return _context12.a(2);
1518
+ case 2:
1519
+ _context12.n = 3;
1520
+ return this.digitalHumanInterruptTalk();
1521
+ case 3:
1522
+ this.state.playingVoiceId = idx;
1523
+ this.renderChatList();
1524
+ audio = new Audio(msg.voiceUrl);
1525
+ this.audio.currentVoiceAudio = audio;
1526
+ audio.play();
1527
+ audio.onended = function () {
1528
+ that.state.playingVoiceId = null;
1529
+ that.audio.currentVoiceAudio = null;
1530
+ that.renderChatList();
1531
+ };
1532
+ case 4:
1533
+ return _context12.a(2);
1534
+ }
1535
+ }, _callee12, this);
1536
+ }));
1537
+ return function (_x5, _x6) {
1538
+ return _ref12.apply(this, arguments);
1539
+ };
1540
+ }();
1541
+ SjDigitalHumanAssistantAdaptive.prototype.showToast = function (message) {
1542
+ var that = this;
1543
+ this.state.toastMessage = message;
1544
+ this.dom.toastContent.textContent = message;
1545
+ this.dom.toast.classList.add("show");
1546
+ if (this.toastTimer) {
1547
+ clearTimeout(this.toastTimer);
1548
+ }
1549
+ this.toastTimer = setTimeout(function () {
1550
+ that.dom.toast.classList.remove("show");
1551
+ }, 2000);
1552
+ };
1553
+ SjDigitalHumanAssistantAdaptive.prototype.cleanupVideo = function () {
1554
+ this.dom.video.pause();
1555
+ this.dom.video.srcObject = null;
1556
+ this.dom.video.src = "";
1557
+ this.dom.video.load();
1558
+ };
1559
+ SjDigitalHumanAssistantAdaptive.prototype.destroyRts = function () {
1560
+ if (this.stream.aliRts && this.stream.aliRts.unsubscribe) {
1561
+ try {
1562
+ this.stream.aliRts.unsubscribe();
1563
+ } catch (error) {
1564
+ console.warn("[SjDigitalHumanAssistantAdaptive] unsubscribe rts failed", error);
1565
+ }
1566
+ }
1567
+ this.stream.aliRts = null;
1568
+ };
1569
+ SjDigitalHumanAssistantAdaptive.prototype.destroy = function () {
1570
+ if (this.stream.sseReader) {
1571
+ this.stream.sseReader.cancel();
1572
+ this.stream.sseReader = null;
1573
+ }
1574
+ if (this.stream.sseReconnectTimer) {
1575
+ clearTimeout(this.stream.sseReconnectTimer);
1576
+ }
1577
+ this.clearReconnectTimer();
1578
+ this.destroyRts();
1579
+ if (this.stream.hls && this.stream.hls.destroy) {
1580
+ this.stream.hls.destroy();
1581
+ this.stream.hls = null;
1582
+ }
1583
+ if (this.audio.currentVoiceAudio) {
1584
+ this.audio.currentVoiceAudio.pause();
1585
+ }
1586
+ this.releaseRecorder();
1587
+ // 清理视频元素
1588
+ if (this.dom.video) {
1589
+ this.dom.video.pause();
1590
+ this.dom.video.srcObject = null;
1591
+ this.dom.video.src = '';
1592
+ this.dom.video.load();
1593
+ }
1594
+ if (this.dom.root && this.dom.root.parentNode) {
1595
+ this.dom.root.parentNode.removeChild(this.dom.root);
1596
+ }
1597
+ };
1598
+ SjDigitalHumanAssistantAdaptive.prototype._request = /*#__PURE__*/function () {
1599
+ var _ref13 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee13(path, method, data) {
1600
+ var requestInit, response;
1601
+ return _regenerator().w(function (_context13) {
1602
+ while (1) switch (_context13.n) {
1603
+ case 0:
1604
+ requestInit = {
1605
+ method: method || "GET",
1606
+ headers: {}
1607
+ };
1608
+ if (requestInit.method !== "GET") {
1609
+ requestInit.headers["Content-Type"] = "application/json";
1610
+ requestInit.body = JSON.stringify(data || {});
1611
+ }
1612
+ _context13.n = 1;
1613
+ return fetch(this._buildUrl(path), requestInit);
1614
+ case 1:
1615
+ response = _context13.v;
1616
+ if (response.ok) {
1617
+ _context13.n = 2;
1618
+ break;
1619
+ }
1620
+ throw new Error("request failed: " + response.status);
1621
+ case 2:
1622
+ return _context13.a(2, response.json());
1623
+ }
1624
+ }, _callee13, this);
1625
+ }));
1626
+ return function (_x7, _x8, _x9) {
1627
+ return _ref13.apply(this, arguments);
1628
+ };
1629
+ }();
1630
+ SjDigitalHumanAssistantAdaptive.prototype._buildUrl = function (path) {
1631
+ var ts = String(Math.floor(Date.now() / 1000));
1632
+ var separator = path.indexOf("?") === -1 ? "?" : "&";
1633
+ var sign = this._md5(this.config.secretKey + ts);
1634
+ return this.config.baseUrl.replace(/\/$/, "") + path + separator + "timestamp=" + encodeURIComponent(ts) + "&sign=" + encodeURIComponent(sign);
1635
+ };
1636
+ SjDigitalHumanAssistantAdaptive.prototype._normalizeSize = function (value) {
1637
+ if (value === undefined || value === null || value === "") {
1638
+ return undefined;
1639
+ }
1640
+ return typeof value === "number" ? value + "px" : String(value);
1641
+ };
1642
+ SjDigitalHumanAssistantAdaptive.prototype._getRemBase = function () {
1643
+ if (typeof window === "undefined") {
1644
+ return 50;
1645
+ }
1646
+ var viewportWidth = 375;
1647
+ if (window.visualViewport && window.visualViewport.width) {
1648
+ viewportWidth = window.visualViewport.width;
1649
+ } else if (window.innerWidth) {
1650
+ viewportWidth = window.innerWidth;
1651
+ } else if (document.documentElement && document.documentElement.clientWidth) {
1652
+ viewportWidth = document.documentElement.clientWidth;
1653
+ }
1654
+ var width = Math.max(320, Math.min(viewportWidth, 750));
1655
+ return width / 7.5;
1656
+ };
1657
+ SjDigitalHumanAssistantAdaptive.prototype.updateRemBase = function () {
1658
+ if (!this.dom || !this.dom.root) {
1659
+ return;
1660
+ }
1661
+ var isMobileDevice = this.detectMobileDevice();
1662
+ this.dom.root.style.setProperty("--sjdha-rem", (isMobileDevice ? this._getRemBase() : 50) + "px");
1663
+ };
1664
+ SjDigitalHumanAssistantAdaptive.prototype._isInteractiveDragTarget = function (target) {
1665
+ return !!(target && target.closest && target.closest("button,input,.sjdha-manual-play"));
1666
+ };
1667
+ SjDigitalHumanAssistantAdaptive.prototype._clampPosition = function (left, top, width, height) {
1668
+ var maxLeft = Math.max(0, window.innerWidth - width);
1669
+ var maxTop = Math.max(0, window.innerHeight - height);
1670
+ return {
1671
+ left: Math.max(0, Math.min(left, maxLeft)),
1672
+ top: Math.max(0, Math.min(top, maxTop))
1673
+ };
1674
+ };
1675
+ SjDigitalHumanAssistantAdaptive.prototype._refreshModeButton = function () {
1676
+ this.dom.modeBtn.style.cssText = this.state.isTextInputMode ? "background: url('https://szr-data.xiu361.cn/img/szr_video/szr_button/voice-btn.png') no-repeat center center;background-size: contain;" : "background: url('https://szr-data.xiu361.cn/img/szr_video/szr_button/text-btn.png') no-repeat center center;background-size: contain;";
1677
+ };
1678
+ SjDigitalHumanAssistantAdaptive.prototype._refreshFullscreenButton = function () {
1679
+ this.dom.fullscreenBtn.style.cssText = this.state.isVideoFullscreen ? "background: url('https://szr-data.xiu361.cn/img/szr_video/szr_button/reduce-btn.png') no-repeat center center;background-size: contain;" : "background: url('https://szr-data.xiu361.cn/img/szr_video/szr_button/magnify-btn.png') no-repeat center center;background-size: contain;";
1680
+ };
1681
+ SjDigitalHumanAssistantAdaptive.prototype._formatDateHeader = function () {
1682
+ var now = new Date();
1683
+ return "今天 " + now.getFullYear() + "年" + (now.getMonth() + 1) + "月" + now.getDate() + "日";
1684
+ };
1685
+ SjDigitalHumanAssistantAdaptive.prototype.formatMessageTime = function (timestamp) {
1686
+ var date = new Date(timestamp);
1687
+ var hours = date.getHours();
1688
+ var minutes = String(date.getMinutes()).padStart(2, "0");
1689
+ var period = hours < 12 ? "上午" : "下午";
1690
+ var displayHour = hours < 12 ? hours : hours === 12 ? 12 : hours - 12;
1691
+ return period + " " + displayHour + ":" + minutes;
1692
+ };
1693
+ SjDigitalHumanAssistantAdaptive.prototype.shouldShowTimeLabel = function (currentTimestamp) {
1694
+ for (var i = this.chatMessages.length - 1; i >= 0; i--) {
1695
+ var msg = this.chatMessages[i];
1696
+ if (msg && msg.type === "user" && msg.timestamp) {
1697
+ return currentTimestamp - msg.timestamp > 180000;
1698
+ }
1699
+ }
1700
+ return false;
1701
+ };
1702
+ SjDigitalHumanAssistantAdaptive.prototype._isIOSWechat = function () {
1703
+ return /iPhone|iPad|iPod/i.test(navigator.userAgent) && /MicroMessenger/i.test(navigator.userAgent);
1704
+ };
1705
+ SjDigitalHumanAssistantAdaptive.prototype._escapeHtml = function (value) {
1706
+ return String(value == null ? "" : value).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\"/g, "&quot;").replace(/'/g, "&#39;");
1707
+ };
1708
+ SjDigitalHumanAssistantAdaptive.prototype.mergeAudioChunks = function (chunks) {
1709
+ var length = 0;
1710
+ var i;
1711
+ for (i = 0; i < chunks.length; i++) {
1712
+ length += chunks[i].length;
1713
+ }
1714
+ var result = new Float32Array(length);
1715
+ var offset = 0;
1716
+ for (i = 0; i < chunks.length; i++) {
1717
+ result.set(chunks[i], offset);
1718
+ offset += chunks[i].length;
1719
+ }
1720
+ return result;
1721
+ };
1722
+ SjDigitalHumanAssistantAdaptive.prototype.downsampleBuffer = function (buffer, sampleRate, outSampleRate) {
1723
+ if (outSampleRate === sampleRate) {
1724
+ return buffer;
1725
+ }
1726
+ var ratio = sampleRate / outSampleRate;
1727
+ var newLength = Math.round(buffer.length / ratio);
1728
+ var result = new Float32Array(newLength);
1729
+ var offsetResult = 0;
1730
+ var offsetBuffer = 0;
1731
+ while (offsetResult < result.length) {
1732
+ var nextOffsetBuffer = Math.round((offsetResult + 1) * ratio);
1733
+ var accum = 0;
1734
+ var count = 0;
1735
+ var i;
1736
+ for (i = offsetBuffer; i < nextOffsetBuffer && i < buffer.length; i++) {
1737
+ accum += buffer[i];
1738
+ count += 1;
1739
+ }
1740
+ result[offsetResult] = accum / count;
1741
+ offsetResult += 1;
1742
+ offsetBuffer = nextOffsetBuffer;
1743
+ }
1744
+ return result;
1745
+ };
1746
+ SjDigitalHumanAssistantAdaptive.prototype.encodeWav = function (samples, sampleRate) {
1747
+ var buffer = new ArrayBuffer(44 + samples.length * 2);
1748
+ var view = new DataView(buffer);
1749
+ this.writeString(view, 0, "RIFF");
1750
+ view.setUint32(4, 36 + samples.length * 2, true);
1751
+ this.writeString(view, 8, "WAVE");
1752
+ this.writeString(view, 12, "fmt ");
1753
+ view.setUint32(16, 16, true);
1754
+ view.setUint16(20, 1, true);
1755
+ view.setUint16(22, 1, true);
1756
+ view.setUint32(24, sampleRate, true);
1757
+ view.setUint32(28, sampleRate * 2, true);
1758
+ view.setUint16(32, 2, true);
1759
+ view.setUint16(34, 16, true);
1760
+ this.writeString(view, 36, "data");
1761
+ view.setUint32(40, samples.length * 2, true);
1762
+ this.floatTo16BitPCM(view, 44, samples);
1763
+ return new Blob([view], {
1764
+ type: "audio/wav"
1765
+ });
1766
+ };
1767
+ SjDigitalHumanAssistantAdaptive.prototype.writeString = function (view, offset, value) {
1768
+ for (var i = 0; i < value.length; i++) {
1769
+ view.setUint8(offset + i, value.charCodeAt(i));
1770
+ }
1771
+ };
1772
+ SjDigitalHumanAssistantAdaptive.prototype.floatTo16BitPCM = function (view, offset, input) {
1773
+ for (var i = 0; i < input.length; i++, offset += 2) {
1774
+ var sample = Math.max(-1, Math.min(1, input[i]));
1775
+ view.setInt16(offset, sample < 0 ? sample * 32768 : sample * 32767, true);
1776
+ }
1777
+ };
1778
+ SjDigitalHumanAssistantAdaptive.prototype._md5 = function (string) {
1779
+ function safeAdd(x, y) {
1780
+ var lsw = (x & 65535) + (y & 65535);
1781
+ var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
1782
+ return msw << 16 | lsw & 65535;
1783
+ }
1784
+ function bitRotateLeft(num, cnt) {
1785
+ return num << cnt | num >>> 32 - cnt;
1786
+ }
1787
+ function md5cmn(q, a, b, x, s, t) {
1788
+ return safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b);
1789
+ }
1790
+ function md5ff(a, b, c, d, x, s, t) {
1791
+ return md5cmn(b & c | ~b & d, a, b, x, s, t);
1792
+ }
1793
+ function md5gg(a, b, c, d, x, s, t) {
1794
+ return md5cmn(b & d | c & ~d, a, b, x, s, t);
1795
+ }
1796
+ function md5hh(a, b, c, d, x, s, t) {
1797
+ return md5cmn(b ^ c ^ d, a, b, x, s, t);
1798
+ }
1799
+ function md5ii(a, b, c, d, x, s, t) {
1800
+ return md5cmn(c ^ (b | ~d), a, b, x, s, t);
1801
+ }
1802
+ function binlMD5(x, len) {
1803
+ x[len >> 5] |= 128 << len % 32;
1804
+ x[(len + 64 >>> 9 << 4) + 14] = len;
1805
+ var i;
1806
+ var olda;
1807
+ var oldb;
1808
+ var oldc;
1809
+ var oldd;
1810
+ var a = 1732584193;
1811
+ var b = -271733879;
1812
+ var c = -1732584194;
1813
+ var d = 271733878;
1814
+ for (i = 0; i < x.length; i += 16) {
1815
+ olda = a;
1816
+ oldb = b;
1817
+ oldc = c;
1818
+ oldd = d;
1819
+ a = md5ff(a, b, c, d, x[i], 7, -680876936);
1820
+ d = md5ff(d, a, b, c, x[i + 1], 12, -389564586);
1821
+ c = md5ff(c, d, a, b, x[i + 2], 17, 606105819);
1822
+ b = md5ff(b, c, d, a, x[i + 3], 22, -1044525330);
1823
+ a = md5ff(a, b, c, d, x[i + 4], 7, -176418897);
1824
+ d = md5ff(d, a, b, c, x[i + 5], 12, 1200080426);
1825
+ c = md5ff(c, d, a, b, x[i + 6], 17, -1473231341);
1826
+ b = md5ff(b, c, d, a, x[i + 7], 22, -45705983);
1827
+ a = md5ff(a, b, c, d, x[i + 8], 7, 1770035416);
1828
+ d = md5ff(d, a, b, c, x[i + 9], 12, -1958414417);
1829
+ c = md5ff(c, d, a, b, x[i + 10], 17, -42063);
1830
+ b = md5ff(b, c, d, a, x[i + 11], 22, -1990404162);
1831
+ a = md5ff(a, b, c, d, x[i + 12], 7, 1804603682);
1832
+ d = md5ff(d, a, b, c, x[i + 13], 12, -40341101);
1833
+ c = md5ff(c, d, a, b, x[i + 14], 17, -1502002290);
1834
+ b = md5ff(b, c, d, a, x[i + 15], 22, 1236535329);
1835
+ a = md5gg(a, b, c, d, x[i + 1], 5, -165796510);
1836
+ d = md5gg(d, a, b, c, x[i + 6], 9, -1069501632);
1837
+ c = md5gg(c, d, a, b, x[i + 11], 14, 643717713);
1838
+ b = md5gg(b, c, d, a, x[i], 20, -373897302);
1839
+ a = md5gg(a, b, c, d, x[i + 5], 5, -701558691);
1840
+ d = md5gg(d, a, b, c, x[i + 10], 9, 38016083);
1841
+ c = md5gg(c, d, a, b, x[i + 15], 14, -660478335);
1842
+ b = md5gg(b, c, d, a, x[i + 4], 20, -405537848);
1843
+ a = md5gg(a, b, c, d, x[i + 9], 5, 568446438);
1844
+ d = md5gg(d, a, b, c, x[i + 14], 9, -1019803690);
1845
+ c = md5gg(c, d, a, b, x[i + 3], 14, -187363961);
1846
+ b = md5gg(b, c, d, a, x[i + 8], 20, 1163531501);
1847
+ a = md5gg(a, b, c, d, x[i + 13], 5, -1444681467);
1848
+ d = md5gg(d, a, b, c, x[i + 2], 9, -51403784);
1849
+ c = md5gg(c, d, a, b, x[i + 7], 14, 1735328473);
1850
+ b = md5gg(b, c, d, a, x[i + 12], 20, -1926607734);
1851
+ a = md5hh(a, b, c, d, x[i + 5], 4, -378558);
1852
+ d = md5hh(d, a, b, c, x[i + 8], 11, -2022574463);
1853
+ c = md5hh(c, d, a, b, x[i + 11], 16, 1839030562);
1854
+ b = md5hh(b, c, d, a, x[i + 14], 23, -35309556);
1855
+ a = md5hh(a, b, c, d, x[i + 1], 4, -1530992060);
1856
+ d = md5hh(d, a, b, c, x[i + 4], 11, 1272893353);
1857
+ c = md5hh(c, d, a, b, x[i + 7], 16, -155497632);
1858
+ b = md5hh(b, c, d, a, x[i + 10], 23, -1094730640);
1859
+ a = md5hh(a, b, c, d, x[i + 13], 4, 681279174);
1860
+ d = md5hh(d, a, b, c, x[i], 11, -358537222);
1861
+ c = md5hh(c, d, a, b, x[i + 3], 16, -722521979);
1862
+ b = md5hh(b, c, d, a, x[i + 6], 23, 76029189);
1863
+ a = md5hh(a, b, c, d, x[i + 9], 4, -640364487);
1864
+ d = md5hh(d, a, b, c, x[i + 12], 11, -421815835);
1865
+ c = md5hh(c, d, a, b, x[i + 15], 16, 530742520);
1866
+ b = md5hh(b, c, d, a, x[i + 2], 23, -995338651);
1867
+ a = md5ii(a, b, c, d, x[i], 6, -198630844);
1868
+ d = md5ii(d, a, b, c, x[i + 7], 10, 1126891415);
1869
+ c = md5ii(c, d, a, b, x[i + 14], 15, -1416354905);
1870
+ b = md5ii(b, c, d, a, x[i + 5], 21, -57434055);
1871
+ a = md5ii(a, b, c, d, x[i + 12], 6, 1700485571);
1872
+ d = md5ii(d, a, b, c, x[i + 3], 10, -1894986606);
1873
+ c = md5ii(c, d, a, b, x[i + 10], 15, -1051523);
1874
+ b = md5ii(b, c, d, a, x[i + 1], 21, -2054922799);
1875
+ a = md5ii(a, b, c, d, x[i + 8], 6, 1873313359);
1876
+ d = md5ii(d, a, b, c, x[i + 15], 10, -30611744);
1877
+ c = md5ii(c, d, a, b, x[i + 6], 15, -1560198380);
1878
+ b = md5ii(b, c, d, a, x[i + 13], 21, 1309151649);
1879
+ a = md5ii(a, b, c, d, x[i + 4], 6, -145523070);
1880
+ d = md5ii(d, a, b, c, x[i + 11], 10, -1120210379);
1881
+ c = md5ii(c, d, a, b, x[i + 2], 15, 718787259);
1882
+ b = md5ii(b, c, d, a, x[i + 9], 21, -343485551);
1883
+ a = safeAdd(a, olda);
1884
+ b = safeAdd(b, oldb);
1885
+ c = safeAdd(c, oldc);
1886
+ d = safeAdd(d, oldd);
1887
+ }
1888
+ return [a, b, c, d];
1889
+ }
1890
+ function binl2rstr(input) {
1891
+ var i;
1892
+ var output = "";
1893
+ for (i = 0; i < input.length * 32; i += 8) {
1894
+ output += String.fromCharCode(input[i >> 5] >>> i % 32 & 255);
1895
+ }
1896
+ return output;
1897
+ }
1898
+ function rstr2binl(input) {
1899
+ var i;
1900
+ var output = [];
1901
+ output[(input.length >> 2) - 1] = undefined;
1902
+ for (i = 0; i < output.length; i += 1) {
1903
+ output[i] = 0;
1904
+ }
1905
+ for (i = 0; i < input.length * 8; i += 8) {
1906
+ output[i >> 5] |= (input.charCodeAt(i / 8) & 255) << i % 32;
1907
+ }
1908
+ return output;
1909
+ }
1910
+ function rstrMD5(s) {
1911
+ return binl2rstr(binlMD5(rstr2binl(s), s.length * 8));
1912
+ }
1913
+ function rstr2hex(input) {
1914
+ var hexTab = "0123456789abcdef";
1915
+ var output = "";
1916
+ var x;
1917
+ var i;
1918
+ for (i = 0; i < input.length; i += 1) {
1919
+ x = input.charCodeAt(i);
1920
+ output += hexTab.charAt(x >>> 4 & 15) + hexTab.charAt(x & 15);
1921
+ }
1922
+ return output;
1923
+ }
1924
+ function str2rstrUTF8(input) {
1925
+ return unescape(encodeURIComponent(input));
1926
+ }
1927
+ return rstr2hex(rstrMD5(str2rstrUTF8(string)));
1928
+ };
1929
+
1930
+ return SjDigitalHumanAssistantAdaptive;
1931
+
1932
+ }));
1933
+ //# sourceMappingURL=sjDigitalHumanAssistantAdaptive.umd.js.map