@wq-hook/volcano-react 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.
package/dist/index.mjs ADDED
@@ -0,0 +1,1364 @@
1
+ // src/asr/useVolcanoASR.ts
2
+ import { useState, useRef, useCallback, useEffect } from "react";
3
+ import { LabASR, uuid } from "@wq-hook/volcano-sdk/asr";
4
+ var DEFAULT_URL = "wss://openspeech.bytedance.com/api/v3/sauc/bigmodel_async";
5
+ function useVolcanoASR(params) {
6
+ const [status, setStatus] = useState("idle");
7
+ const [text, setText] = useState("");
8
+ const [error, setError] = useState(null);
9
+ const asrInstanceRef = useRef(null);
10
+ useEffect(() => {
11
+ asrInstanceRef.current = LabASR({
12
+ onStart: () => {
13
+ setStatus("recording");
14
+ setError(null);
15
+ },
16
+ onMessage: (msg, fullData) => {
17
+ if (msg) {
18
+ setText(msg);
19
+ }
20
+ console.log("fullData", fullData);
21
+ },
22
+ onClose: () => {
23
+ setStatus("idle");
24
+ },
25
+ onError: () => {
26
+ setStatus("error");
27
+ setError("Connection error");
28
+ }
29
+ });
30
+ return () => {
31
+ asrInstanceRef.current?.stopRecord();
32
+ };
33
+ }, []);
34
+ const start = useCallback(async () => {
35
+ if (!asrInstanceRef.current) return;
36
+ try {
37
+ setStatus("connecting");
38
+ setText("");
39
+ setError(null);
40
+ const { appId, token } = params;
41
+ const auth = {
42
+ api_app_key: appId,
43
+ api_access_key: `Jwt; ${token}`,
44
+ api_resource_id: "volc.seedasr.sauc.duration"
45
+ };
46
+ const authUrl = `${DEFAULT_URL}?${new URLSearchParams(auth).toString()}`;
47
+ const config = {
48
+ user: {
49
+ uid: uuid()
50
+ },
51
+ audio: {
52
+ format: "pcm",
53
+ rate: 16e3,
54
+ bits: 16,
55
+ channel: 1
56
+ },
57
+ request: {
58
+ model_name: "bigmodel",
59
+ enable_punc: true,
60
+ result_type: "full"
61
+ }
62
+ };
63
+ asrInstanceRef.current.connect({
64
+ url: authUrl,
65
+ config
66
+ });
67
+ await asrInstanceRef.current.startRecord({}, () => {
68
+ });
69
+ } catch (err) {
70
+ const error2 = err;
71
+ setError(error2.message || "Failed to start ASR");
72
+ setStatus("error");
73
+ }
74
+ }, [params]);
75
+ const stop = useCallback(() => {
76
+ if (!asrInstanceRef.current) return;
77
+ asrInstanceRef.current.stopRecord();
78
+ }, []);
79
+ return {
80
+ status,
81
+ text,
82
+ error,
83
+ start,
84
+ stop
85
+ };
86
+ }
87
+
88
+ // src/tts/useVolcanoTTS.ts
89
+ import { WebsocketMSE } from "@wq-hook/volcano-sdk/tts";
90
+ import { MarkdownFormatter } from "@wq-hook/volcano-sdk";
91
+ import { useCallback as useCallback2, useRef as useRef2, useState as useState2 } from "react";
92
+ import emojiRegex from "emoji-regex";
93
+
94
+ // src/tts/TTSCache.ts
95
+ var DB_NAME = "VolcanoTTSCache";
96
+ var STORE_NAME = "audio_segments";
97
+ var DB_VERSION = 1;
98
+ var memoryCache = /* @__PURE__ */ new Map();
99
+ var dbPromise = null;
100
+ function getDB() {
101
+ if (dbPromise) return dbPromise;
102
+ dbPromise = new Promise((resolve, reject) => {
103
+ const request = indexedDB.open(DB_NAME, DB_VERSION);
104
+ request.onerror = () => {
105
+ console.error("IndexedDB error:", request.error);
106
+ reject(request.error);
107
+ };
108
+ request.onsuccess = () => {
109
+ resolve(request.result);
110
+ };
111
+ request.onupgradeneeded = (event) => {
112
+ const db = event.target.result;
113
+ if (!db.objectStoreNames.contains(STORE_NAME)) {
114
+ db.createObjectStore(STORE_NAME, { keyPath: "key" });
115
+ }
116
+ };
117
+ });
118
+ return dbPromise;
119
+ }
120
+ var TTSCache = {
121
+ get: async (key) => {
122
+ if (memoryCache.has(key)) {
123
+ console.log("[TTSCache] L1 Hit:", key);
124
+ return memoryCache.get(key);
125
+ }
126
+ try {
127
+ const db = await getDB();
128
+ return new Promise((resolve) => {
129
+ const transaction = db.transaction([STORE_NAME], "readonly");
130
+ const store = transaction.objectStore(STORE_NAME);
131
+ const request = store.get(key);
132
+ request.onsuccess = async () => {
133
+ const result = request.result;
134
+ if (result) {
135
+ console.log("[TTSCache] L2 Hit:", key);
136
+ memoryCache.set(key, result.data);
137
+ resolve(result.data);
138
+ } else {
139
+ resolve(void 0);
140
+ }
141
+ };
142
+ request.onerror = () => resolve(void 0);
143
+ });
144
+ } catch (e) {
145
+ console.warn("TTSCache L2 read failed", e);
146
+ return void 0;
147
+ }
148
+ },
149
+ set: async (key, data) => {
150
+ memoryCache.set(key, data);
151
+ if (memoryCache.size > 50) {
152
+ const firstKey = memoryCache.keys().next().value;
153
+ if (firstKey) memoryCache.delete(firstKey);
154
+ }
155
+ try {
156
+ const db = await getDB();
157
+ const transaction = db.transaction([STORE_NAME], "readwrite");
158
+ const store = transaction.objectStore(STORE_NAME);
159
+ store.put({
160
+ key,
161
+ data,
162
+ timestamp: Date.now()
163
+ });
164
+ } catch (e) {
165
+ console.warn("TTSCache L2 write failed", e);
166
+ }
167
+ },
168
+ generateKey: (text, voice, speed) => {
169
+ const textHash = simpleHash(text);
170
+ return `tts_${voice}_${speed}_${textHash}`;
171
+ },
172
+ delete: async (key) => {
173
+ memoryCache.delete(key);
174
+ try {
175
+ const db = await getDB();
176
+ const transaction = db.transaction([STORE_NAME], "readwrite");
177
+ const store = transaction.objectStore(STORE_NAME);
178
+ store.delete(key);
179
+ } catch (e) {
180
+ console.warn("TTSCache L2 delete failed", e);
181
+ }
182
+ },
183
+ clear: async () => {
184
+ memoryCache.clear();
185
+ try {
186
+ const db = await getDB();
187
+ const transaction = db.transaction([STORE_NAME], "readwrite");
188
+ transaction.objectStore(STORE_NAME).clear();
189
+ } catch (e) {
190
+ }
191
+ }
192
+ };
193
+ function simpleHash(str) {
194
+ let hash = 0;
195
+ for (let i = 0; i < str.length; i++) {
196
+ const char = str.charCodeAt(i);
197
+ hash = (hash << 5) - hash + char;
198
+ hash = hash & hash;
199
+ }
200
+ return Math.abs(hash).toString(36);
201
+ }
202
+
203
+ // src/tts/useVolcanoTTS.ts
204
+ var URL2 = "wss://openspeech.bytedance.com/api/v3/tts/bidirection";
205
+ function buildFullUrl(url, auth) {
206
+ const arr = [];
207
+ for (const key in auth) {
208
+ if (auth[key]) {
209
+ arr.push(
210
+ `${key}=${encodeURIComponent(auth[key])}`
211
+ );
212
+ }
213
+ }
214
+ return `${url}?${arr.join("&")}`;
215
+ }
216
+ function useVolcanoTTS({
217
+ ttsConfig,
218
+ audioParams
219
+ }) {
220
+ const [status, setStatus] = useState2("idle");
221
+ const [error, setError] = useState2(null);
222
+ const client = useRef2(null);
223
+ const currentAudioBuffersRef = useRef2([]);
224
+ const cacheKeyRef = useRef2("");
225
+ const cleanupAll = useCallback2(() => {
226
+ if (client.current) {
227
+ try {
228
+ client.current.finishConnection();
229
+ client.current = null;
230
+ setTimeout(() => {
231
+ setStatus("idle");
232
+ setError(null);
233
+ }, 0);
234
+ } catch (err) {
235
+ console.error("[useTTS] cleanupAll error", err);
236
+ }
237
+ }
238
+ }, []);
239
+ const connect = useCallback2(async () => {
240
+ try {
241
+ setStatus("connecting");
242
+ setError(null);
243
+ const clientInstance = WebsocketMSE({ autoStartSession: false });
244
+ client.current = clientInstance;
245
+ const auth = {};
246
+ if (ttsConfig.token) {
247
+ auth.api_app_key = ttsConfig.appid;
248
+ auth.api_access_key = `Jwt; ${ttsConfig.token}`;
249
+ auth.api_resource_id = ttsConfig.resourceId || "seed-tts-2.0";
250
+ }
251
+ const fullUrl = buildFullUrl(URL2, auth);
252
+ const audioUrl = clientInstance.start({
253
+ url: fullUrl,
254
+ config: {
255
+ user: {
256
+ uid: `feewee-tts-user-${Date.now()}`
257
+ },
258
+ namespace: ttsConfig.namespace || "BidirectionalTTS",
259
+ req_params: {
260
+ speaker: audioParams?.speaker || "zh_female_vv_uranus_bigtts",
261
+ audio_params: {
262
+ ...audioParams
263
+ },
264
+ additions: JSON.stringify({
265
+ enable_language_detector: true,
266
+ disable_markdown_filter: true,
267
+ enable_latex_tn: true
268
+ // max_length_to_filter_parenthesis: 100,
269
+ })
270
+ }
271
+ },
272
+ onWSError(errMsg) {
273
+ setStatus("error");
274
+ setError("\u8FDE\u63A5\u9519\u8BEF");
275
+ console.error("[useTTS] onWSError", errMsg);
276
+ },
277
+ onStart() {
278
+ setStatus("connected");
279
+ console.log("[useTTS] onStart");
280
+ },
281
+ onSessionStarted() {
282
+ setStatus("session-started");
283
+ console.log("[useTTS] onSessionStarted");
284
+ },
285
+ onSessionFinished() {
286
+ setStatus("connected");
287
+ console.log("[useTTS] onSessionFinished");
288
+ if (currentAudioBuffersRef.current.length > 0 && cacheKeyRef.current) {
289
+ const totalSize = currentAudioBuffersRef.current.reduce((acc, cur) => acc + cur.byteLength, 0);
290
+ if (totalSize > 512) {
291
+ TTSCache.set(cacheKeyRef.current, [...currentAudioBuffersRef.current]);
292
+ } else {
293
+ console.warn("[useTTS] Audio data too small, skip caching:", totalSize);
294
+ }
295
+ }
296
+ },
297
+ onTTSSentenceStart(val) {
298
+ console.log("[useTTS] onTTSSentenceStart", val);
299
+ },
300
+ onTTSSentenceEnd(val) {
301
+ console.log("[useTTS] onTTSSentenceEnd", val);
302
+ },
303
+ onError(err) {
304
+ setStatus("error");
305
+ setError(err.msg || "\u8FDE\u63A5\u9519\u8BEF");
306
+ console.error("[useTTS] onError", err);
307
+ },
308
+ onClose() {
309
+ cleanupAll();
310
+ },
311
+ onMessage(audioBuffer) {
312
+ console.log("[useTTS] onMessage", audioBuffer);
313
+ currentAudioBuffersRef.current.push(audioBuffer);
314
+ }
315
+ });
316
+ return audioUrl;
317
+ } catch (err) {
318
+ setStatus("error");
319
+ setError(err instanceof Error ? err.message : "Unknown error");
320
+ throw err;
321
+ }
322
+ }, [ttsConfig, audioParams, cleanupAll]);
323
+ const disconnect = useCallback2(() => {
324
+ cleanupAll();
325
+ setStatus("idle");
326
+ setError(null);
327
+ }, [cleanupAll]);
328
+ const finishSession = useCallback2(() => {
329
+ if (!client.current) {
330
+ throw new Error("Client not connected");
331
+ }
332
+ client.current.finishSession();
333
+ }, []);
334
+ const startSession = useCallback2(() => {
335
+ if (!client.current) {
336
+ throw new Error("Client not connected");
337
+ }
338
+ client.current?.startSession();
339
+ }, []);
340
+ const sendText = useCallback2(async (text) => {
341
+ if (!client.current) {
342
+ throw new Error("Client not connected");
343
+ }
344
+ const speed = audioParams?.speech_rate || 0;
345
+ const voice = audioParams?.speaker || "zh_female_vv_uranus_bigtts";
346
+ const cacheKey = TTSCache.generateKey(text, voice, speed);
347
+ cacheKeyRef.current = cacheKey;
348
+ currentAudioBuffersRef.current = [];
349
+ const cached = await TTSCache.get(cacheKey);
350
+ if (cached && cached.length > 0) {
351
+ const totalSize = cached.reduce((acc, cur) => acc + cur.byteLength, 0);
352
+ if (totalSize < 1024) {
353
+ console.warn("[useTTS] Cached data too small, invalidating cache:", totalSize);
354
+ await TTSCache.delete(cacheKey);
355
+ } else {
356
+ console.log(
357
+ "[useTTS] Cache hit",
358
+ cacheKey,
359
+ "chunks:",
360
+ cached.length,
361
+ "totalSize:",
362
+ totalSize
363
+ );
364
+ if (client.current && typeof client.current.appendBuffer === "function") {
365
+ cached.forEach((buf) => client.current?.appendBuffer(buf));
366
+ if (typeof client.current.closeStream === "function") {
367
+ client.current.closeStream();
368
+ }
369
+ if (typeof client.current.finishConnection === "function") {
370
+ client.current.finishConnection();
371
+ }
372
+ return;
373
+ } else {
374
+ console.warn(
375
+ "[useTTS] Cache hit but client.appendBuffer is missing. Falling back to network."
376
+ );
377
+ }
378
+ }
379
+ }
380
+ const formatText = MarkdownFormatter.format(text).replace(emojiRegex(), "");
381
+ console.log("[useTTS] sendText", formatText);
382
+ client.current.sendText(formatText);
383
+ setTimeout(() => {
384
+ client.current?.finishSession();
385
+ }, 50);
386
+ }, [audioParams]);
387
+ return {
388
+ status,
389
+ error,
390
+ connect,
391
+ disconnect,
392
+ startSession,
393
+ finishSession,
394
+ sendText
395
+ };
396
+ }
397
+
398
+ // src/tts/useMessageTTS.ts
399
+ import { WebsocketMSE as WebsocketMSE2 } from "@wq-hook/volcano-sdk/tts";
400
+ import { MarkdownFormatter as MarkdownFormatter2 } from "@wq-hook/volcano-sdk";
401
+ import { useCallback as useCallback3, useEffect as useEffect2, useRef as useRef3, useState as useState3 } from "react";
402
+ import emojiRegex2 from "emoji-regex";
403
+
404
+ // src/tts/TextSplitter.ts
405
+ function splitTextByDelimiters(text, minLength = 10, maxLength = 150) {
406
+ const segments = [];
407
+ let cleanText = text.replace(/[\u200B-\u200D\uFEFF]/g, "");
408
+ const sentenceEndRegex = /([。!?.!?]+[”"']?)/g;
409
+ const rawSegments = cleanText.split(sentenceEndRegex);
410
+ let currentBuffer = "";
411
+ for (let i = 0; i < rawSegments.length; i++) {
412
+ const part = rawSegments[i];
413
+ if (sentenceEndRegex.test(part)) {
414
+ currentBuffer += part;
415
+ } else {
416
+ if (part.trim().length === 0) continue;
417
+ if (currentBuffer.length + part.length > maxLength && currentBuffer.length >= minLength) {
418
+ segments.push({
419
+ index: segments.length,
420
+ content: currentBuffer.trim(),
421
+ length: currentBuffer.trim().length
422
+ });
423
+ currentBuffer = part;
424
+ } else {
425
+ currentBuffer += part;
426
+ }
427
+ }
428
+ }
429
+ const mergedSentences = [];
430
+ let temp = "";
431
+ for (let i = 0; i < rawSegments.length; i++) {
432
+ const part = rawSegments[i];
433
+ if (sentenceEndRegex.test(part)) {
434
+ temp += part;
435
+ mergedSentences.push(temp);
436
+ temp = "";
437
+ } else {
438
+ if (temp.length > 0) {
439
+ }
440
+ temp += part;
441
+ }
442
+ }
443
+ if (temp.trim().length > 0) {
444
+ mergedSentences.push(temp);
445
+ }
446
+ segments.length = 0;
447
+ currentBuffer = "";
448
+ for (const sent of mergedSentences) {
449
+ if (currentBuffer.length + sent.length > maxLength) {
450
+ if (currentBuffer.trim().length > 0) {
451
+ segments.push({
452
+ index: segments.length,
453
+ content: currentBuffer.trim(),
454
+ length: currentBuffer.trim().length
455
+ });
456
+ }
457
+ currentBuffer = sent;
458
+ } else {
459
+ currentBuffer += sent;
460
+ }
461
+ }
462
+ if (currentBuffer.trim().length > 0) {
463
+ segments.push({
464
+ index: segments.length,
465
+ content: currentBuffer.trim(),
466
+ length: currentBuffer.trim().length
467
+ });
468
+ }
469
+ return segments;
470
+ }
471
+
472
+ // src/tts/Metrics.ts
473
+ var NoopMetricsCollector = class {
474
+ record(_metric) {
475
+ }
476
+ };
477
+
478
+ // src/tts/useMessageTTS.ts
479
+ var WS_URL = "wss://openspeech.bytedance.com/api/v3/tts/bidirection";
480
+ var activeInstances = /* @__PURE__ */ new Map();
481
+ function buildFullUrl2(url, params) {
482
+ const { ...auth } = params;
483
+ const arr = [];
484
+ for (const key in auth) {
485
+ if (Object.prototype.hasOwnProperty.call(auth, key)) {
486
+ arr.push(
487
+ `${key}=${encodeURIComponent(auth[key])}`
488
+ );
489
+ }
490
+ }
491
+ return `${url}?${arr.join("&")}`;
492
+ }
493
+ function useMessageTTS({
494
+ ttsConfig,
495
+ audioParams,
496
+ autoPlay = true,
497
+ metricsCollector = new NoopMetricsCollector(),
498
+ onPlayStart,
499
+ onPlayPause,
500
+ onPlayResume,
501
+ onPlayEnd,
502
+ onError,
503
+ exclusive = true,
504
+ fallbackVoice,
505
+ visualization
506
+ }) {
507
+ const [isPlaying, setIsPlaying] = useState3(false);
508
+ const [isPaused, setIsPaused] = useState3(false);
509
+ const [isSynthesizing, setIsSynthesizing] = useState3(false);
510
+ const [error, setErrorState] = useState3(null);
511
+ const [progress, setProgress] = useState3(0);
512
+ const [visualizationData, setVisualizationData] = useState3({
513
+ frequencyData: new Uint8Array(0),
514
+ timeDomainData: new Uint8Array(0)
515
+ });
516
+ const instanceId = useRef3(
517
+ `tts-${Date.now()}-${Math.random().toString(36).slice(2)}`
518
+ ).current;
519
+ const clientRef = useRef3(null);
520
+ const audioRef = useRef3(null);
521
+ const audioContextRef = useRef3(null);
522
+ const analyserRef = useRef3(null);
523
+ const sourceRef = useRef3(null);
524
+ const audioUrlRef = useRef3(null);
525
+ const cacheKeyRef = useRef3("");
526
+ const audioBuffersRef = useRef3([]);
527
+ const isFallbackRef = useRef3(false);
528
+ const fallbackUtteranceRef = useRef3(null);
529
+ const stopOthers = useCallback3(() => {
530
+ if (!exclusive) return;
531
+ activeInstances.forEach((instance, id) => {
532
+ if (id !== instanceId) {
533
+ instance.pause();
534
+ }
535
+ });
536
+ }, [exclusive, instanceId]);
537
+ const initAudioContext = useCallback3(() => {
538
+ if (!audioRef.current) return;
539
+ if (!audioContextRef.current) {
540
+ const AudioContextClass = window.AudioContext || window.webkitAudioContext;
541
+ audioContextRef.current = new AudioContextClass();
542
+ }
543
+ if (audioContextRef.current.state === "suspended") {
544
+ audioContextRef.current.resume();
545
+ }
546
+ if (!analyserRef.current) {
547
+ analyserRef.current = audioContextRef.current.createAnalyser();
548
+ analyserRef.current.fftSize = visualization?.fftSize || 256;
549
+ }
550
+ if (!sourceRef.current) {
551
+ try {
552
+ sourceRef.current = audioContextRef.current.createMediaElementSource(
553
+ audioRef.current
554
+ );
555
+ sourceRef.current.connect(analyserRef.current);
556
+ analyserRef.current.connect(audioContextRef.current.destination);
557
+ } catch (e) {
558
+ }
559
+ }
560
+ }, []);
561
+ const cleanupAudio = useCallback3(() => {
562
+ if (audioUrlRef.current) {
563
+ URL.revokeObjectURL(audioUrlRef.current);
564
+ audioUrlRef.current = null;
565
+ }
566
+ if (audioRef.current) {
567
+ audioRef.current.onerror = null;
568
+ audioRef.current.onended = null;
569
+ audioRef.current.onpause = null;
570
+ audioRef.current.onplay = null;
571
+ audioRef.current.ontimeupdate = null;
572
+ audioRef.current.pause();
573
+ audioRef.current.src = "";
574
+ audioRef.current = null;
575
+ }
576
+ if (sourceRef.current) {
577
+ try {
578
+ sourceRef.current.disconnect();
579
+ } catch (e) {
580
+ }
581
+ sourceRef.current = null;
582
+ }
583
+ if (fallbackUtteranceRef.current) {
584
+ window.speechSynthesis.cancel();
585
+ fallbackUtteranceRef.current = null;
586
+ }
587
+ isFallbackRef.current = false;
588
+ }, []);
589
+ const stop = useCallback3(() => {
590
+ if (clientRef.current) {
591
+ clientRef.current.close();
592
+ clientRef.current = null;
593
+ }
594
+ cleanupAudio();
595
+ setIsPlaying(false);
596
+ setIsPaused(false);
597
+ setIsSynthesizing(false);
598
+ setProgress(0);
599
+ activeInstances.delete(instanceId);
600
+ }, [cleanupAudio, instanceId]);
601
+ const pause = useCallback3(() => {
602
+ if (isFallbackRef.current) {
603
+ window.speechSynthesis.pause();
604
+ } else if (audioRef.current) {
605
+ audioRef.current.pause();
606
+ }
607
+ setIsPaused(true);
608
+ setIsPlaying(false);
609
+ onPlayPause?.();
610
+ }, [onPlayPause]);
611
+ const resume = useCallback3(() => {
612
+ stopOthers();
613
+ if (isFallbackRef.current) {
614
+ window.speechSynthesis.resume();
615
+ } else if (audioRef.current) {
616
+ audioRef.current.play();
617
+ }
618
+ setIsPaused(false);
619
+ setIsPlaying(true);
620
+ onPlayResume?.();
621
+ activeInstances.set(instanceId, { pause });
622
+ }, [stopOthers, instanceId, pause, onPlayResume]);
623
+ const togglePlay = useCallback3(() => {
624
+ if (isPlaying) {
625
+ pause();
626
+ } else {
627
+ resume();
628
+ }
629
+ }, [isPlaying, pause, resume]);
630
+ const playFallback = useCallback3(
631
+ (text) => {
632
+ console.warn("[useMessageTTS] Switching to fallback TTS");
633
+ isFallbackRef.current = true;
634
+ if (clientRef.current) {
635
+ clientRef.current.close();
636
+ clientRef.current = null;
637
+ }
638
+ if (audioRef.current) {
639
+ audioRef.current.pause();
640
+ audioRef.current = null;
641
+ }
642
+ const utterance = new SpeechSynthesisUtterance(text);
643
+ utterance.rate = audioParams?.speech_rate || 1;
644
+ const voices = window.speechSynthesis.getVoices();
645
+ const zhVoice = voices.find((v) => v.lang.includes("zh"));
646
+ if (zhVoice) utterance.voice = zhVoice;
647
+ utterance.onstart = () => {
648
+ setIsPlaying(true);
649
+ setIsPaused(false);
650
+ setIsSynthesizing(false);
651
+ onPlayStart?.();
652
+ activeInstances.set(instanceId, { pause });
653
+ };
654
+ utterance.onend = () => {
655
+ setIsPlaying(false);
656
+ setIsPaused(false);
657
+ activeInstances.delete(instanceId);
658
+ onPlayEnd?.();
659
+ };
660
+ utterance.onerror = (e) => {
661
+ console.error("[useMessageTTS] Fallback TTS failed", e);
662
+ setErrorState("Fallback TTS failed");
663
+ onError?.(new Error("Fallback TTS failed"));
664
+ setIsPlaying(false);
665
+ };
666
+ fallbackUtteranceRef.current = utterance;
667
+ window.speechSynthesis.speak(utterance);
668
+ },
669
+ [audioParams, instanceId, onError, onPlayEnd, onPlayStart, pause]
670
+ );
671
+ const executeTTS = useCallback3(
672
+ async (text, targetVoice) => {
673
+ stop();
674
+ stopOthers();
675
+ setErrorState(null);
676
+ setIsSynthesizing(true);
677
+ setProgress(0);
678
+ audioBuffersRef.current = [];
679
+ isFallbackRef.current = false;
680
+ const speed = audioParams?.speech_rate || 0;
681
+ const voice = targetVoice;
682
+ const cacheKey = TTSCache.generateKey(text, voice, speed);
683
+ cacheKeyRef.current = cacheKey;
684
+ const startTime = Date.now();
685
+ metricsCollector.record({
686
+ name: "tts_request",
687
+ labels: { voice, speed, text_length: text.length },
688
+ value: 1,
689
+ timestamp: startTime
690
+ });
691
+ try {
692
+ const cachedData = await TTSCache.get(cacheKey);
693
+ const audio = new Audio();
694
+ audio.crossOrigin = "anonymous";
695
+ audioRef.current = audio;
696
+ audio.onplay = () => {
697
+ setIsPlaying(true);
698
+ setIsPaused(false);
699
+ onPlayStart?.();
700
+ initAudioContext();
701
+ activeInstances.set(instanceId, { pause });
702
+ metricsCollector.record({
703
+ name: "tts_latency",
704
+ labels: { stage: "playback", voice, speed },
705
+ value: Date.now() - startTime,
706
+ timestamp: Date.now()
707
+ });
708
+ };
709
+ audio.onpause = () => {
710
+ if (!audio.ended) {
711
+ }
712
+ };
713
+ audio.onended = () => {
714
+ setIsPlaying(false);
715
+ setIsPaused(false);
716
+ onPlayEnd?.();
717
+ activeInstances.delete(instanceId);
718
+ };
719
+ audio.onerror = (e) => {
720
+ console.error("Audio playback error:", e, audio.error);
721
+ metricsCollector.record({
722
+ name: "tts_error",
723
+ labels: { error_code: "playback_error", voice, detail: audio.error?.message || String(audio.error?.code) },
724
+ value: 1,
725
+ timestamp: Date.now()
726
+ });
727
+ handleError(text, voice);
728
+ };
729
+ audio.ontimeupdate = () => {
730
+ let duration = audio.duration;
731
+ if (!isFinite(duration)) {
732
+ if (audio.buffered.length > 0) {
733
+ duration = audio.buffered.end(audio.buffered.length - 1);
734
+ }
735
+ }
736
+ if (isFinite(duration) && duration > 0) {
737
+ setProgress(audio.currentTime / duration * 100);
738
+ }
739
+ };
740
+ if (cachedData) {
741
+ const totalSize = cachedData.reduce((acc, buf) => acc + buf.byteLength, 0);
742
+ metricsCollector.record({
743
+ name: "tts_cache_hit",
744
+ labels: { voice, speed },
745
+ value: 1,
746
+ timestamp: Date.now()
747
+ });
748
+ console.log(
749
+ JSON.stringify({
750
+ event: "tts_cache_hit",
751
+ cache_hit: true,
752
+ text_len: text.length,
753
+ voice,
754
+ speed,
755
+ data_size: totalSize
756
+ })
757
+ );
758
+ if (totalSize === 0) {
759
+ console.warn("[useMessageTTS] Cached data is empty, falling back to stream");
760
+ } else {
761
+ const blob = new Blob(cachedData, { type: "audio/mpeg" });
762
+ const url2 = URL.createObjectURL(blob);
763
+ audioUrlRef.current = url2;
764
+ audio.src = url2;
765
+ setIsSynthesizing(false);
766
+ if (autoPlay) {
767
+ try {
768
+ await audio.play();
769
+ } catch (err) {
770
+ console.warn("AutoPlay blocked", err);
771
+ }
772
+ }
773
+ return;
774
+ }
775
+ }
776
+ console.log("[useMessageTTS] Cache miss, starting stream");
777
+ clientRef.current = WebsocketMSE2({ autoStartSession: true });
778
+ const formattedText = MarkdownFormatter2.format(text).replace(emojiRegex2(), "");
779
+ const segments = splitTextByDelimiters(formattedText);
780
+ const url = clientRef.current.start({
781
+ url: buildFullUrl2(WS_URL, {
782
+ api_access_key: `Jwt; ${ttsConfig.token}`,
783
+ api_app_key: ttsConfig.appid,
784
+ api_resource_id: ttsConfig.resourceId || "seed-tts-2.0"
785
+ }),
786
+ config: {
787
+ user: {
788
+ uid: `req-${Date.now()}`
789
+ },
790
+ namespace: ttsConfig.namespace || "BidirectionalTTS",
791
+ req_params: {
792
+ speaker: voice,
793
+ audio_params: {
794
+ sample_rate: audioParams?.sample_rate || 24e3,
795
+ format: audioParams?.format || "mp3",
796
+ speech_rate: audioParams?.speech_rate,
797
+ pitch_rate: audioParams?.pitch_rate,
798
+ loudness_rate: audioParams?.loudness_rate
799
+ },
800
+ additions: JSON.stringify({
801
+ enable_language_detector: true,
802
+ disable_markdown_filter: true,
803
+ enable_latex_tn: true
804
+ // max_length_to_filter_parenthesis: 100,
805
+ })
806
+ }
807
+ },
808
+ onSessionStarted: () => {
809
+ segments.forEach((seg) => {
810
+ clientRef.current?.sendText(seg.content);
811
+ });
812
+ clientRef.current?.finishSession();
813
+ },
814
+ onMessage: (data) => {
815
+ if (audioBuffersRef.current.length === 0) {
816
+ console.log(
817
+ JSON.stringify({
818
+ event: "tts_first_packet",
819
+ latency_ms: Date.now() - startTime,
820
+ voice
821
+ })
822
+ );
823
+ }
824
+ const buffer = data instanceof ArrayBuffer ? data.slice(0) : new Uint8Array(data).buffer;
825
+ audioBuffersRef.current.push(buffer);
826
+ },
827
+ onSessionFinished: () => {
828
+ setIsSynthesizing(false);
829
+ if (audioBuffersRef.current.length > 0) {
830
+ TTSCache.set(cacheKey, [...audioBuffersRef.current]);
831
+ }
832
+ console.log(
833
+ JSON.stringify({
834
+ event: "tts_synthesis_finished",
835
+ cache_hit: false,
836
+ text_len: text.length,
837
+ duration_ms: Date.now() - startTime,
838
+ voice,
839
+ speed
840
+ })
841
+ );
842
+ },
843
+ onError: (err) => {
844
+ console.error("TTS Synthesis error:", err);
845
+ metricsCollector.record({
846
+ name: "tts_error",
847
+ labels: { error_code: "synthesis_error", voice },
848
+ value: 1,
849
+ timestamp: Date.now()
850
+ });
851
+ handleError(text, voice);
852
+ setIsSynthesizing(false);
853
+ }
854
+ });
855
+ audioUrlRef.current = url;
856
+ audio.src = url;
857
+ if (autoPlay) {
858
+ try {
859
+ await audio.play();
860
+ } catch (e) {
861
+ console.warn("Autoplay blocked/pending", e);
862
+ }
863
+ }
864
+ } catch (err) {
865
+ console.error("Unexpected error in executeTTS:", err);
866
+ metricsCollector.record({
867
+ name: "tts_error",
868
+ labels: { error_code: "unexpected_error", voice },
869
+ value: 1,
870
+ timestamp: Date.now()
871
+ });
872
+ handleError(text, voice);
873
+ }
874
+ },
875
+ [
876
+ ttsConfig,
877
+ audioParams,
878
+ autoPlay,
879
+ stop,
880
+ stopOthers,
881
+ instanceId,
882
+ onPlayStart,
883
+ onPlayEnd,
884
+ initAudioContext,
885
+ pause,
886
+ fallbackVoice,
887
+ metricsCollector
888
+ ]
889
+ );
890
+ const handleError = useCallback3(
891
+ (text, failedVoice) => {
892
+ if (fallbackVoice && failedVoice !== fallbackVoice) {
893
+ console.warn(
894
+ `[useMessageTTS] Voice ${failedVoice} failed, switching to fallback voice ${fallbackVoice}`
895
+ );
896
+ executeTTS(text, fallbackVoice);
897
+ } else {
898
+ playFallback(text);
899
+ }
900
+ },
901
+ [fallbackVoice, executeTTS, playFallback]
902
+ );
903
+ const play = useCallback3(
904
+ (text) => {
905
+ const voice = audioParams?.speaker || "default";
906
+ return executeTTS(text, voice);
907
+ },
908
+ [audioParams, executeTTS]
909
+ );
910
+ const getFrequencyData = useCallback3(() => {
911
+ if (!analyserRef.current) return new Uint8Array(0);
912
+ const dataArray = new Uint8Array(analyserRef.current.frequencyBinCount);
913
+ analyserRef.current.getByteFrequencyData(dataArray);
914
+ return dataArray;
915
+ }, []);
916
+ const getTimeDomainData = useCallback3(() => {
917
+ if (!analyserRef.current) return new Uint8Array(0);
918
+ const dataArray = new Uint8Array(analyserRef.current.frequencyBinCount);
919
+ analyserRef.current.getByteTimeDomainData(dataArray);
920
+ return dataArray;
921
+ }, []);
922
+ useEffect2(() => {
923
+ if (!visualization?.enabled) return;
924
+ let animId;
925
+ let lastUpdate = 0;
926
+ const interval = visualization.refreshInterval || 0;
927
+ const update = (timestamp) => {
928
+ if (isPlaying && !isPaused) {
929
+ if (timestamp - lastUpdate >= interval) {
930
+ setVisualizationData({
931
+ frequencyData: getFrequencyData(),
932
+ timeDomainData: getTimeDomainData()
933
+ });
934
+ lastUpdate = timestamp;
935
+ }
936
+ animId = requestAnimationFrame(update);
937
+ }
938
+ };
939
+ if (isPlaying && !isPaused) {
940
+ animId = requestAnimationFrame(update);
941
+ }
942
+ return () => {
943
+ if (animId) cancelAnimationFrame(animId);
944
+ };
945
+ }, [isPlaying, isPaused, visualization, getFrequencyData, getTimeDomainData]);
946
+ useEffect2(() => {
947
+ return () => {
948
+ stop();
949
+ if (audioContextRef.current) {
950
+ audioContextRef.current.close();
951
+ }
952
+ };
953
+ }, [stop]);
954
+ const seek = useCallback3((percentage) => {
955
+ if (audioRef.current) {
956
+ let duration = audioRef.current.duration;
957
+ if (!isFinite(duration) && audioRef.current.buffered.length > 0) {
958
+ duration = audioRef.current.buffered.end(audioRef.current.buffered.length - 1);
959
+ }
960
+ if (isFinite(duration) && duration > 0) {
961
+ const time = percentage / 100 * duration;
962
+ if (isFinite(time)) {
963
+ audioRef.current.currentTime = time;
964
+ setProgress(percentage);
965
+ }
966
+ }
967
+ }
968
+ }, []);
969
+ return {
970
+ isPlaying,
971
+ isPaused,
972
+ isSynthesizing,
973
+ error,
974
+ play,
975
+ pause,
976
+ resume,
977
+ stop,
978
+ togglePlay,
979
+ seek,
980
+ progress,
981
+ getFrequencyData,
982
+ getTimeDomainData,
983
+ visualizationData
984
+ };
985
+ }
986
+
987
+ // src/components/AudioWaveVisualizer.tsx
988
+ import { useEffect as useEffect3, useRef as useRef4 } from "react";
989
+ import { jsx } from "react/jsx-runtime";
990
+ var AudioWaveVisualizer = ({
991
+ isPlaying,
992
+ isPaused,
993
+ frequencyData,
994
+ timeDomainData,
995
+ style = "bar",
996
+ color = "#8b5cf6",
997
+ bars = 50,
998
+ height = 60,
999
+ width = 200,
1000
+ className,
1001
+ styleObj
1002
+ }) => {
1003
+ const canvasRef = useRef4(null);
1004
+ const requestRef = useRef4(null);
1005
+ const progressBackground = Array.isArray(color) ? `linear-gradient(90deg, ${color[0]}, ${color[1]})` : color;
1006
+ const textColor = Array.isArray(color) ? color[0] : color;
1007
+ const draw = () => {
1008
+ const canvas = canvasRef.current;
1009
+ if (!canvas) return;
1010
+ const ctx = canvas.getContext("2d");
1011
+ if (!ctx) return;
1012
+ const w = canvas.width;
1013
+ const h = canvas.height;
1014
+ ctx.clearRect(0, 0, w, h);
1015
+ let fillStyle = "";
1016
+ if (Array.isArray(color)) {
1017
+ const gradient = ctx.createLinearGradient(0, 0, w, 0);
1018
+ gradient.addColorStop(0, color[0]);
1019
+ gradient.addColorStop(1, color[1]);
1020
+ fillStyle = gradient;
1021
+ } else {
1022
+ fillStyle = color;
1023
+ }
1024
+ ctx.fillStyle = fillStyle;
1025
+ ctx.strokeStyle = fillStyle;
1026
+ if (style === "bar" && frequencyData) {
1027
+ const barWidth = w / bars;
1028
+ const step = Math.floor(frequencyData.length / bars);
1029
+ for (let i = 0; i < bars; i++) {
1030
+ const value = frequencyData[i * step] || 0;
1031
+ const percent = value / 255;
1032
+ const barHeight = h * percent;
1033
+ ctx.fillRect(i * barWidth, h - barHeight, barWidth - 2, barHeight);
1034
+ }
1035
+ } else if (style === "line" && timeDomainData) {
1036
+ ctx.lineWidth = 2;
1037
+ ctx.beginPath();
1038
+ const sliceWidth = w / timeDomainData.length;
1039
+ let x = 0;
1040
+ for (let i = 0; i < timeDomainData.length; i++) {
1041
+ const v = timeDomainData[i] / 128;
1042
+ const y = v * h / 2;
1043
+ if (i === 0) ctx.moveTo(x, y);
1044
+ else ctx.lineTo(x, y);
1045
+ x += sliceWidth;
1046
+ }
1047
+ ctx.stroke();
1048
+ } else if (style === "wave") {
1049
+ const avg = frequencyData ? frequencyData.reduce((a, b) => a + b, 0) / frequencyData.length : 0;
1050
+ const amplitude = avg / 255 * (h / 2);
1051
+ ctx.beginPath();
1052
+ for (let x = 0; x < w; x++) {
1053
+ const y = h / 2 + Math.sin(x * 0.1 + Date.now() * 0.01) * amplitude;
1054
+ if (x === 0) ctx.moveTo(x, y);
1055
+ else ctx.lineTo(x, y);
1056
+ }
1057
+ ctx.stroke();
1058
+ } else if (style === "circle" && frequencyData) {
1059
+ const avg = frequencyData.reduce((a, b) => a + b, 0) / frequencyData.length;
1060
+ const radius = avg / 255 * (Math.min(w, h) / 2);
1061
+ ctx.beginPath();
1062
+ ctx.arc(w / 2, h / 2, Math.max(5, radius), 0, 2 * Math.PI);
1063
+ ctx.fill();
1064
+ }
1065
+ if (isPlaying && !isPaused) {
1066
+ requestRef.current = requestAnimationFrame(draw);
1067
+ }
1068
+ };
1069
+ useEffect3(() => {
1070
+ if (isPlaying && !isPaused) {
1071
+ requestRef.current = requestAnimationFrame(draw);
1072
+ } else {
1073
+ draw();
1074
+ if (requestRef.current) cancelAnimationFrame(requestRef.current);
1075
+ }
1076
+ return () => {
1077
+ if (requestRef.current) cancelAnimationFrame(requestRef.current);
1078
+ };
1079
+ }, [
1080
+ isPlaying,
1081
+ isPaused,
1082
+ frequencyData,
1083
+ timeDomainData,
1084
+ style,
1085
+ color
1086
+ ]);
1087
+ return /* @__PURE__ */ jsx(
1088
+ "div",
1089
+ {
1090
+ className,
1091
+ style: {
1092
+ display: "inline-flex",
1093
+ flexDirection: "column",
1094
+ gap: "4px",
1095
+ width,
1096
+ ...styleObj
1097
+ },
1098
+ children: /* @__PURE__ */ jsx(
1099
+ "canvas",
1100
+ {
1101
+ ref: canvasRef,
1102
+ width,
1103
+ height,
1104
+ style: { display: "block" }
1105
+ }
1106
+ )
1107
+ }
1108
+ );
1109
+ };
1110
+ var AudioWaveVisualizer_default = AudioWaveVisualizer;
1111
+
1112
+ // src/components/AudioProgressBar.tsx
1113
+ import { useEffect as useEffect4, useRef as useRef5 } from "react";
1114
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
1115
+ var AudioProgressBar = ({
1116
+ progress,
1117
+ width = "100%",
1118
+ height = 6,
1119
+ color = "#8b5cf6",
1120
+ backgroundColor = "rgba(128, 128, 128, 0.2)",
1121
+ showText = true,
1122
+ onSeek,
1123
+ className,
1124
+ style
1125
+ }) => {
1126
+ const progressBarRef = useRef5(null);
1127
+ const containerRef = useRef5(null);
1128
+ const progressTextRef = useRef5(null);
1129
+ const thumbRef = useRef5(null);
1130
+ const displayedProgress = useRef5(0);
1131
+ const requestRef = useRef5(null);
1132
+ const isDragging = useRef5(false);
1133
+ const isHovering = useRef5(false);
1134
+ const isTouch = useRef5(false);
1135
+ useEffect4(() => {
1136
+ const match = window.matchMedia("(pointer: coarse)");
1137
+ isTouch.current = match.matches;
1138
+ if (isTouch.current && thumbRef.current) {
1139
+ thumbRef.current.style.opacity = "1";
1140
+ thumbRef.current.style.transform = "translate(-50%, -50%) scale(1)";
1141
+ }
1142
+ }, []);
1143
+ const progressBackground = Array.isArray(color) ? `linear-gradient(90deg, ${color[0]}, ${color[1]})` : color;
1144
+ const textColor = Array.isArray(color) ? color[0] : color;
1145
+ const animate = () => {
1146
+ if (isDragging.current) {
1147
+ requestRef.current = requestAnimationFrame(animate);
1148
+ return;
1149
+ }
1150
+ const target = Math.min(100, Math.max(0, progress));
1151
+ const current = displayedProgress.current;
1152
+ const ease = 0.1;
1153
+ let next = current + (target - current) * ease;
1154
+ if (Math.abs(target - next) < 0.1) {
1155
+ next = target;
1156
+ }
1157
+ displayedProgress.current = next;
1158
+ if (progressBarRef.current) {
1159
+ progressBarRef.current.style.width = `${next}%`;
1160
+ }
1161
+ if (thumbRef.current) {
1162
+ thumbRef.current.style.left = `${next}%`;
1163
+ }
1164
+ if (showText && progressTextRef.current) {
1165
+ const rounded = Math.round(next);
1166
+ if (progressTextRef.current.textContent !== `${rounded}%`) {
1167
+ progressTextRef.current.textContent = `${rounded}%`;
1168
+ }
1169
+ }
1170
+ if (Math.abs(target - next) >= 0.1) {
1171
+ requestRef.current = requestAnimationFrame(animate);
1172
+ }
1173
+ };
1174
+ useEffect4(() => {
1175
+ requestRef.current = requestAnimationFrame(animate);
1176
+ return () => {
1177
+ if (requestRef.current) {
1178
+ cancelAnimationFrame(requestRef.current);
1179
+ }
1180
+ };
1181
+ }, [progress, showText]);
1182
+ const calculateProgress = (clientX) => {
1183
+ if (!containerRef.current) return 0;
1184
+ const rect = containerRef.current.getBoundingClientRect();
1185
+ const x = clientX - rect.left;
1186
+ const width2 = rect.width;
1187
+ const percentage = Math.min(100, Math.max(0, x / width2 * 100));
1188
+ return percentage;
1189
+ };
1190
+ const updateVisuals = (percentage) => {
1191
+ displayedProgress.current = percentage;
1192
+ if (progressBarRef.current) {
1193
+ progressBarRef.current.style.width = `${percentage}%`;
1194
+ }
1195
+ if (thumbRef.current) {
1196
+ thumbRef.current.style.left = `${percentage}%`;
1197
+ }
1198
+ if (showText && progressTextRef.current) {
1199
+ const rounded = Math.round(percentage);
1200
+ if (progressTextRef.current.textContent !== `${rounded}%`) {
1201
+ progressTextRef.current.textContent = `${rounded}%`;
1202
+ }
1203
+ }
1204
+ };
1205
+ const updateThumbVisibility = (visible) => {
1206
+ if (!thumbRef.current || isTouch.current) return;
1207
+ if (visible) {
1208
+ thumbRef.current.style.opacity = "1";
1209
+ thumbRef.current.style.transform = "translate(-50%, -50%) scale(1)";
1210
+ } else {
1211
+ thumbRef.current.style.opacity = "0";
1212
+ thumbRef.current.style.transform = "translate(-50%, -50%) scale(0.8)";
1213
+ }
1214
+ };
1215
+ const handleMouseEnter = () => {
1216
+ if (!onSeek) return;
1217
+ isHovering.current = true;
1218
+ updateThumbVisibility(true);
1219
+ };
1220
+ const handleMouseLeave = () => {
1221
+ if (!onSeek) return;
1222
+ isHovering.current = false;
1223
+ if (!isDragging.current) {
1224
+ updateThumbVisibility(false);
1225
+ }
1226
+ };
1227
+ const handleMouseDown = (e) => {
1228
+ if (!onSeek) return;
1229
+ e.preventDefault();
1230
+ e.stopPropagation();
1231
+ isDragging.current = true;
1232
+ updateThumbVisibility(true);
1233
+ const percentage = calculateProgress(e.clientX);
1234
+ updateVisuals(percentage);
1235
+ const handleMouseMove = (e2) => {
1236
+ if (isDragging.current) {
1237
+ e2.preventDefault();
1238
+ const percentage2 = calculateProgress(e2.clientX);
1239
+ updateVisuals(percentage2);
1240
+ }
1241
+ };
1242
+ const handleMouseUp = (e2) => {
1243
+ if (isDragging.current) {
1244
+ e2.preventDefault();
1245
+ isDragging.current = false;
1246
+ const percentage2 = calculateProgress(e2.clientX);
1247
+ onSeek(percentage2);
1248
+ if (!isHovering.current) {
1249
+ updateThumbVisibility(false);
1250
+ }
1251
+ window.removeEventListener("mousemove", handleMouseMove);
1252
+ window.removeEventListener("mouseup", handleMouseUp);
1253
+ }
1254
+ };
1255
+ window.addEventListener("mousemove", handleMouseMove);
1256
+ window.addEventListener("mouseup", handleMouseUp);
1257
+ };
1258
+ return /* @__PURE__ */ jsxs(
1259
+ "div",
1260
+ {
1261
+ className,
1262
+ style: {
1263
+ display: "flex",
1264
+ alignItems: "center",
1265
+ gap: "8px",
1266
+ width,
1267
+ cursor: onSeek ? "pointer" : "default",
1268
+ userSelect: "none",
1269
+ WebkitUserSelect: "none",
1270
+ touchAction: "none",
1271
+ padding: "4px 0",
1272
+ // 增加点击区域
1273
+ ...style
1274
+ },
1275
+ onMouseDown: handleMouseDown,
1276
+ onMouseEnter: handleMouseEnter,
1277
+ onMouseLeave: handleMouseLeave,
1278
+ children: [
1279
+ /* @__PURE__ */ jsxs(
1280
+ "div",
1281
+ {
1282
+ ref: containerRef,
1283
+ style: {
1284
+ flex: 1,
1285
+ height: `${height}px`,
1286
+ backgroundColor,
1287
+ borderRadius: `${height / 2}px`,
1288
+ position: "relative"
1289
+ // 允许绝对定位子元素
1290
+ // overflow: "hidden", // 移除 overflow hidden 以显示滑块
1291
+ },
1292
+ children: [
1293
+ /* @__PURE__ */ jsx2(
1294
+ "div",
1295
+ {
1296
+ ref: progressBarRef,
1297
+ style: {
1298
+ width: `${displayedProgress.current}%`,
1299
+ // 初始值
1300
+ height: "100%",
1301
+ background: progressBackground,
1302
+ borderRadius: `${height / 2}px`
1303
+ }
1304
+ }
1305
+ ),
1306
+ onSeek && /* @__PURE__ */ jsx2(
1307
+ "div",
1308
+ {
1309
+ ref: thumbRef,
1310
+ style: {
1311
+ position: "absolute",
1312
+ left: `${displayedProgress.current}%`,
1313
+ top: "50%",
1314
+ transform: "translate(-50%, -50%) scale(0.8)",
1315
+ // 初始略微缩小
1316
+ width: `${Math.max(12, height * 2)}px`,
1317
+ height: `${Math.max(12, height * 2)}px`,
1318
+ borderRadius: "50%",
1319
+ backgroundColor: "#fff",
1320
+ border: `2px solid ${textColor}`,
1321
+ boxShadow: "0 2px 4px rgba(0,0,0,0.2)",
1322
+ opacity: 0,
1323
+ // 默认隐藏
1324
+ transition: "opacity 0.2s ease, transform 0.2s cubic-bezier(0.4, 0, 0.2, 1)",
1325
+ pointerEvents: "none",
1326
+ // 让点击穿透到 container
1327
+ zIndex: 10
1328
+ }
1329
+ }
1330
+ )
1331
+ ]
1332
+ }
1333
+ ),
1334
+ showText && /* @__PURE__ */ jsxs(
1335
+ "span",
1336
+ {
1337
+ ref: progressTextRef,
1338
+ style: {
1339
+ fontSize: "12px",
1340
+ color: textColor,
1341
+ minWidth: "32px",
1342
+ textAlign: "right",
1343
+ fontFamily: "monospace",
1344
+ lineHeight: 1
1345
+ },
1346
+ children: [
1347
+ Math.round(displayedProgress.current),
1348
+ "%"
1349
+ ]
1350
+ }
1351
+ )
1352
+ ]
1353
+ }
1354
+ );
1355
+ };
1356
+ var AudioProgressBar_default = AudioProgressBar;
1357
+ export {
1358
+ AudioProgressBar_default as AudioProgressBar,
1359
+ AudioWaveVisualizer_default as AudioWaveVisualizer,
1360
+ splitTextByDelimiters,
1361
+ useMessageTTS,
1362
+ useVolcanoASR,
1363
+ useVolcanoTTS
1364
+ };