@chat21/chat21-web-widget 5.1.33 → 5.1.34-rc1

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.
Files changed (201) hide show
  1. package/.angular-mcp-cache/package.json +1 -0
  2. package/.cursor/angular18-accessibility-auditor-skill.md +442 -0
  3. package/.cursor/mcp.json +15 -0
  4. package/.github/workflows/docker-community-push-latest.yml +23 -13
  5. package/.github/workflows/docker-image-tag-community-tag-push.yml +22 -12
  6. package/.github/workflows/playwright.yml +27 -0
  7. package/CHANGELOG.md +130 -6
  8. package/Dockerfile +4 -5
  9. package/angular.json +24 -4
  10. package/docs/changelog/this-branch.md +36 -0
  11. package/env.sample +3 -2
  12. package/mocks/voice-websocket-mock/server.cjs +245 -0
  13. package/nginx.conf +22 -2
  14. package/package.json +10 -3
  15. package/playwright.config.ts +41 -0
  16. package/src/app/app.component.html +2 -2
  17. package/src/app/app.component.scss +25 -14
  18. package/src/app/app.component.spec.ts +21 -6
  19. package/src/app/app.component.ts +10 -9
  20. package/src/app/app.module.ts +15 -0
  21. package/src/app/component/conversation-detail/conversation/conversation.component.html +25 -11
  22. package/src/app/component/conversation-detail/conversation/conversation.component.scss +40 -2
  23. package/src/app/component/conversation-detail/conversation/conversation.component.spec.ts +644 -75
  24. package/src/app/component/conversation-detail/conversation/conversation.component.ts +100 -14
  25. package/src/app/component/conversation-detail/conversation-audio-recorder/conversation-audio-recorder.component.html +25 -13
  26. package/src/app/component/conversation-detail/conversation-audio-recorder/conversation-audio-recorder.component.spec.ts +123 -5
  27. package/src/app/component/conversation-detail/conversation-audio-recorder/conversation-audio-recorder.component.ts +1 -0
  28. package/src/app/component/conversation-detail/conversation-content/conversation-content.component.html +23 -10
  29. package/src/app/component/conversation-detail/conversation-content/conversation-content.component.scss +33 -2
  30. package/src/app/component/conversation-detail/conversation-content/conversation-content.component.spec.ts +242 -149
  31. package/src/app/component/conversation-detail/conversation-content/conversation-content.component.ts +8 -6
  32. package/src/app/component/conversation-detail/conversation-emojii/conversation-emojii.component.spec.ts +53 -3
  33. package/src/app/component/conversation-detail/conversation-footer/conversation-footer.component.html +200 -96
  34. package/src/app/component/conversation-detail/conversation-footer/conversation-footer.component.scss +211 -6
  35. package/src/app/component/conversation-detail/conversation-footer/conversation-footer.component.spec.ts +452 -78
  36. package/src/app/component/conversation-detail/conversation-footer/conversation-footer.component.ts +291 -76
  37. package/src/app/component/conversation-detail/conversation-header/conversation-header.component.html +113 -53
  38. package/src/app/component/conversation-detail/conversation-header/conversation-header.component.scss +12 -4
  39. package/src/app/component/conversation-detail/conversation-header/conversation-header.component.spec.ts +274 -29
  40. package/src/app/component/conversation-detail/conversation-internal-frame/conversation-internal-frame.component.html +23 -9
  41. package/src/app/component/conversation-detail/conversation-internal-frame/conversation-internal-frame.component.spec.ts +80 -8
  42. package/src/app/component/conversation-detail/conversation-preview/conversation-preview.component.html +29 -23
  43. package/src/app/component/conversation-detail/conversation-preview/conversation-preview.component.spec.ts +185 -16
  44. package/src/app/component/conversation-detail/conversation-preview/conversation-preview.component.ts +34 -14
  45. package/src/app/component/conversation-detail/stream-audio-spectrum/stream-audio-spectrum.component.html +46 -0
  46. package/src/app/component/conversation-detail/stream-audio-spectrum/stream-audio-spectrum.component.scss +83 -0
  47. package/src/app/component/conversation-detail/stream-audio-spectrum/stream-audio-spectrum.component.ts +192 -0
  48. package/src/app/component/error-alert/error-alert.component.spec.ts +65 -5
  49. package/src/app/component/eyeeye-catcher-card/eyeeye-catcher-card.component.html +16 -7
  50. package/src/app/component/eyeeye-catcher-card/eyeeye-catcher-card.component.scss +21 -0
  51. package/src/app/component/eyeeye-catcher-card/eyeeye-catcher-card.component.spec.ts +89 -7
  52. package/src/app/component/form/form-builder/form-builder.component.html +1 -1
  53. package/src/app/component/form/form-builder/form-builder.component.spec.ts +163 -21
  54. package/src/app/component/form/inputs/form-checkbox/form-checkbox.component.html +8 -4
  55. package/src/app/component/form/inputs/form-checkbox/form-checkbox.component.scss +10 -5
  56. package/src/app/component/form/inputs/form-checkbox/form-checkbox.component.spec.ts +90 -16
  57. package/src/app/component/form/inputs/form-checkbox/form-checkbox.component.ts +26 -0
  58. package/src/app/component/form/inputs/form-label/form-label.component.spec.ts +45 -11
  59. package/src/app/component/form/inputs/form-radio-button/form-radio-button.component.spec.ts +24 -6
  60. package/src/app/component/form/inputs/form-select/form-select.component.spec.ts +14 -5
  61. package/src/app/component/form/inputs/form-text/form-text.component.html +14 -12
  62. package/src/app/component/form/inputs/form-text/form-text.component.scss +11 -1
  63. package/src/app/component/form/inputs/form-text/form-text.component.spec.ts +113 -17
  64. package/src/app/component/form/inputs/form-text/form-text.component.ts +35 -3
  65. package/src/app/component/form/inputs/form-textarea/form-textarea.component.html +13 -11
  66. package/src/app/component/form/inputs/form-textarea/form-textarea.component.scss +6 -5
  67. package/src/app/component/form/inputs/form-textarea/form-textarea.component.spec.ts +149 -13
  68. package/src/app/component/form/inputs/form-textarea/form-textarea.component.ts +26 -0
  69. package/src/app/component/form/prechat-form/prechat-form.component.html +14 -11
  70. package/src/app/component/form/prechat-form/prechat-form.component.spec.ts +102 -10
  71. package/src/app/component/form/prechat-form/prechat-form.component.ts +8 -1
  72. package/src/app/component/form/prechat-form-test-mock.ts +35 -0
  73. package/src/app/component/home/home.component.html +38 -31
  74. package/src/app/component/home/home.component.scss +4 -2
  75. package/src/app/component/home/home.component.spec.ts +226 -11
  76. package/src/app/component/home-conversations/home-conversations.component.html +30 -26
  77. package/src/app/component/home-conversations/home-conversations.component.scss +3 -0
  78. package/src/app/component/home-conversations/home-conversations.component.spec.ts +212 -36
  79. package/src/app/component/last-message/last-message.component.html +15 -9
  80. package/src/app/component/last-message/last-message.component.scss +16 -2
  81. package/src/app/component/last-message/last-message.component.spec.ts +204 -23
  82. package/src/app/component/last-message/last-message.component.ts +4 -1
  83. package/src/app/component/launcher-button/launcher-button.component.html +8 -13
  84. package/src/app/component/launcher-button/launcher-button.component.spec.ts +104 -8
  85. package/src/app/component/list-all-conversations/list-all-conversations.component.html +12 -17
  86. package/src/app/component/list-all-conversations/list-all-conversations.component.scss +2 -0
  87. package/src/app/component/list-conversations/list-conversations.component.html +22 -22
  88. package/src/app/component/menu-options/menu-options.component.html +30 -20
  89. package/src/app/component/menu-options/menu-options.component.spec.ts +125 -9
  90. package/src/app/component/message/audio/audio.component.html +13 -15
  91. package/src/app/component/message/audio/audio.component.spec.ts +140 -5
  92. package/src/app/component/message/audio/audio.component.ts +1 -5
  93. package/src/app/component/message/audio-sync/audio-sync.component.html +18 -0
  94. package/src/app/component/message/audio-sync/audio-sync.component.scss +65 -0
  95. package/src/app/component/message/audio-sync/audio-sync.component.spec.ts +103 -0
  96. package/src/app/component/message/audio-sync/audio-sync.component.ts +643 -0
  97. package/src/app/component/message/avatar/avatar.component.html +2 -2
  98. package/src/app/component/message/avatar/avatar.component.spec.ts +99 -7
  99. package/src/app/component/message/bubble-message/bubble-message.component.html +43 -51
  100. package/src/app/component/message/bubble-message/bubble-message.component.scss +59 -1
  101. package/src/app/component/message/bubble-message/bubble-message.component.spec.ts +154 -57
  102. package/src/app/component/message/bubble-message/bubble-message.component.ts +152 -109
  103. package/src/app/component/message/buttons/action-button/action-button.component.html +3 -4
  104. package/src/app/component/message/buttons/action-button/action-button.component.spec.ts +49 -5
  105. package/src/app/component/message/buttons/link-button/link-button.component.scss +5 -8
  106. package/src/app/component/message/buttons/link-button/link-button.component.spec.ts +50 -5
  107. package/src/app/component/message/buttons/text-button/text-button.component.spec.ts +44 -5
  108. package/src/app/component/message/carousel/carousel.component.html +29 -16
  109. package/src/app/component/message/carousel/carousel.component.scss +20 -8
  110. package/src/app/component/message/carousel/carousel.component.spec.ts +80 -3
  111. package/src/app/component/message/carousel/carousel.component.ts +16 -0
  112. package/src/app/component/message/frame/frame.component.html +9 -4
  113. package/src/app/component/message/frame/frame.component.spec.ts +34 -15
  114. package/src/app/component/message/frame/frame.component.ts +7 -2
  115. package/src/app/component/message/html/html.component.html +1 -1
  116. package/src/app/component/message/html/html.component.scss +1 -1
  117. package/src/app/component/message/html/html.component.spec.ts +24 -7
  118. package/src/app/component/message/image/image.component.html +12 -10
  119. package/src/app/component/message/image/image.component.scss +16 -0
  120. package/src/app/component/message/image/image.component.spec.ts +101 -15
  121. package/src/app/component/message/image/image.component.ts +90 -51
  122. package/src/app/component/message/info-message/info-message.component.spec.ts +26 -14
  123. package/src/app/component/message/json-sources/json-sources.component.html +38 -0
  124. package/src/app/component/message/json-sources/json-sources.component.scss +201 -0
  125. package/src/app/component/message/json-sources/json-sources.component.ts +89 -0
  126. package/src/app/component/message/like-unlike/like-unlike.component.html +7 -9
  127. package/src/app/component/message/like-unlike/like-unlike.component.spec.ts +31 -3
  128. package/src/app/component/message/return-receipt/return-receipt.component.spec.ts +38 -17
  129. package/src/app/component/message/text/text.component.html +3 -3
  130. package/src/app/component/message/text/text.component.scss +80 -86
  131. package/src/app/component/message/text/text.component.spec.ts +106 -13
  132. package/src/app/component/message-attachment/message-attachment.component.spec.ts +134 -13
  133. package/src/app/component/selection-department/selection-department.component.html +21 -23
  134. package/src/app/component/selection-department/selection-department.component.spec.ts +159 -14
  135. package/src/app/component/selection-department/selection-department.component.ts +8 -1
  136. package/src/app/component/send-button/send-button.component.html +5 -13
  137. package/src/app/component/send-button/send-button.component.spec.ts +2 -2
  138. package/src/app/component/star-rating-widget/star-rating-widget.component.html +51 -81
  139. package/src/app/directives/tooltip.directive.spec.ts +8 -4
  140. package/src/app/modals/confirm-close/confirm-close.component.html +20 -8
  141. package/src/app/modals/confirm-close/confirm-close.component.scss +3 -0
  142. package/src/app/modals/confirm-close/confirm-close.component.spec.ts +13 -4
  143. package/src/app/modals/confirm-close/confirm-close.component.ts +8 -1
  144. package/src/app/pipe/html-entites-encode.pipe.spec.ts +35 -2
  145. package/src/app/pipe/marked.pipe.spec.ts +38 -2
  146. package/src/app/pipe/marked.pipe.ts +51 -41
  147. package/src/app/providers/app-config.service.ts +4 -2
  148. package/src/app/providers/brand.service.spec.ts +23 -2
  149. package/src/app/providers/brand.service.ts +1 -1
  150. package/src/app/providers/global-settings.service.spec.ts +1009 -14
  151. package/src/app/providers/global-settings.service.ts +82 -2
  152. package/src/app/providers/json-sources-parser.service.ts +175 -0
  153. package/src/app/providers/translator.service.ts +26 -6
  154. package/src/app/providers/tts-audio-playback-coordinator.service.spec.ts +117 -0
  155. package/src/app/providers/tts-audio-playback-coordinator.service.ts +109 -0
  156. package/src/app/providers/url-preview.service.ts +82 -0
  157. package/src/app/providers/voice/STT&TTS/openai-voice.config.ts +12 -0
  158. package/src/app/providers/voice/STT&TTS/openai-voice.provider.ts +171 -0
  159. package/src/app/providers/voice/STT&TTS/speech-provider.abstract.ts +39 -0
  160. package/src/app/providers/voice/audio.types.ts +40 -0
  161. package/src/app/providers/voice/vad.service.spec.ts +28 -0
  162. package/src/app/providers/voice/vad.service.ts +70 -0
  163. package/src/app/providers/voice/voice-streaming.service.spec.ts +23 -0
  164. package/src/app/providers/voice/voice-streaming.service.ts +702 -0
  165. package/src/app/providers/voice/voice-streaming.types.ts +112 -0
  166. package/src/app/providers/voice/voice.service.spec.ts +227 -0
  167. package/src/app/providers/voice/voice.service.ts +973 -0
  168. package/src/app/sass/_variables.scss +3 -0
  169. package/src/app/sass/animations.scss +19 -1
  170. package/src/app/shims/onnxruntime-web-wasm.ts +4 -0
  171. package/src/app/utils/globals.ts +21 -1
  172. package/src/app/utils/json-sources-utils.ts +27 -0
  173. package/src/app/utils/url-utils.ts +98 -0
  174. package/src/app/utils/utils-resources.ts +1 -1
  175. package/src/assets/i18n/en.json +106 -99
  176. package/src/assets/i18n/es.json +107 -100
  177. package/src/assets/i18n/fr.json +107 -100
  178. package/src/assets/i18n/it.json +107 -98
  179. package/src/assets/onnx/ort-wasm-simd-threaded.mjs +59 -0
  180. package/src/assets/onnx/ort-wasm-simd-threaded.wasm +0 -0
  181. package/src/assets/sounds/keyboard.mp3 +0 -0
  182. package/src/assets/twp/chatbot-panel.html +3 -1
  183. package/src/assets/twp/index-dev.html +18 -0
  184. package/src/assets/twp/tiledesk_widget_files/widget-css-override-example.css +14 -0
  185. package/src/assets/vad/silero_vad_legacy.onnx +0 -0
  186. package/src/assets/vad/vad.worklet.bundle.min.js +1 -0
  187. package/src/chat21-core/models/message.ts +2 -1
  188. package/src/chat21-core/providers/chat-manager.spec.ts +72 -0
  189. package/src/chat21-core/providers/firebase/firebase-conversation-handler.ts +3 -2
  190. package/src/chat21-core/providers/mqtt/mqtt-conversation-handler.ts +12 -0
  191. package/src/chat21-core/providers/scripts/script.service.spec.ts +12 -2
  192. package/src/chat21-core/providers/tiledesk/tiledesk-requests.service.ts +1 -1
  193. package/src/chat21-core/utils/constants.ts +4 -0
  194. package/src/chat21-core/utils/utils-message.ts +45 -6
  195. package/src/chat21-core/utils/utils.ts +5 -2
  196. package/src/widget-config-template.json +4 -1
  197. package/src/widget-config.json +4 -1
  198. package/tests/widget-form-rich.spec.ts +67 -0
  199. package/tests/widget-index-dev-settings.spec.ts +52 -0
  200. package/tests/widget-twp-iframe.spec.ts +39 -0
  201. package/tsconfig.json +5 -0
@@ -0,0 +1,643 @@
1
+ import {
2
+ AfterViewInit,
3
+ ChangeDetectorRef,
4
+ Component,
5
+ ElementRef,
6
+ Input,
7
+ OnChanges,
8
+ OnDestroy,
9
+ SimpleChanges,
10
+ ViewChild,
11
+ } from '@angular/core';
12
+ import { Subscription } from 'rxjs';
13
+ import { MessageModel } from 'src/chat21-core/models/message';
14
+ import { TtsAudioPlaybackCoordinator } from 'src/app/providers/tts-audio-playback-coordinator.service';
15
+ import { VoiceService } from 'src/app/providers/voice/voice.service';
16
+ import { Globals } from 'src/app/utils/globals';
17
+
18
+ /** HAVE_METADATA: metadati già disponibili (tipico audio servito da cache). */
19
+ const HAVE_METADATA = 1;
20
+ const BROWSER_TTS_OUTPUT_FORMAT = 'mp3_44100_128';
21
+
22
+ @Component({
23
+ selector: 'chat-audio-sync',
24
+ templateUrl: './audio-sync.component.html',
25
+ styleUrl: './audio-sync.component.scss',
26
+ })
27
+ export class AudioSyncComponent implements AfterViewInit, OnChanges, OnDestroy {
28
+ @Input() message: MessageModel | null = null;
29
+ @Input() color?: string;
30
+
31
+ @ViewChild('audioPlayer') audioRef!: ElementRef<HTMLAudioElement>;
32
+ @ViewChild('transcriptBox') transcriptBox!: ElementRef<HTMLElement>;
33
+
34
+ words: {
35
+ text: string;
36
+ start: number;
37
+ end: number;
38
+ state: 'future' | 'active' | 'past';
39
+ }[] = [];
40
+
41
+ currentTime = 0;
42
+ duration = 1;
43
+ activeIndex = -1;
44
+
45
+ private timingReady = false;
46
+ private onMetadataLoaded: () => void;
47
+ private onPlaybackEnded: () => void;
48
+
49
+ /** Id univoco per il coordinatore (di solito `message.uid`). */
50
+ private playbackOwnerId = '';
51
+ private destroyed = false;
52
+ private playbackRequested = false;
53
+ private playbackStarted = false;
54
+ private streamAbort?: AbortController;
55
+ private mediaSourceObjectUrl?: string;
56
+ private stopAllSub?: Subscription;
57
+ private preemptSub?: Subscription;
58
+
59
+ constructor(
60
+ private readonly cdr: ChangeDetectorRef,
61
+ private readonly ttsPlayback: TtsAudioPlaybackCoordinator,
62
+ private readonly globals: Globals,
63
+ private readonly voiceService: VoiceService,
64
+ ) {}
65
+
66
+ /** `false` = messaggio già in storico: niente autoplay / karaoke. Da `message.isJustRecived`. */
67
+ private get skipSyncAnimation(): boolean {
68
+ return this.message?.isJustRecived === false;
69
+ }
70
+
71
+ ngOnChanges(changes: SimpleChanges): void {
72
+ if (!changes['message']) {
73
+ return;
74
+ }
75
+ if (this.audioRef?.nativeElement && this.timingReady) {
76
+ const d = this.audioRef.nativeElement.duration;
77
+ if (Number.isFinite(d) && d > 0) {
78
+ this.duration = d;
79
+ }
80
+ this.buildFakeTiming();
81
+ if (this.skipSyncAnimation) {
82
+ this.markAllWordsPast();
83
+ } else if (this.playbackStarted) {
84
+ this.syncStatesFromCurrentTime();
85
+ }
86
+ }
87
+ }
88
+
89
+ ngAfterViewInit(): void {
90
+ const audio = this.audioRef.nativeElement;
91
+
92
+ this.playbackOwnerId =
93
+ (this.message?.uid && String(this.message.uid).trim()) ||
94
+ `tts-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
95
+
96
+ this.onPlaybackEnded = () => {
97
+ this.playbackStarted = false;
98
+ this.cleanupStreaming();
99
+ this.ttsPlayback.releaseIfCurrent(this.playbackOwnerId);
100
+ if (this.skipSyncAnimation) {
101
+ return;
102
+ }
103
+ this.markAllWordsPast();
104
+ if (this.message) {
105
+ this.message.isJustRecived = false;
106
+ }
107
+ this.cdr.detectChanges();
108
+ };
109
+
110
+ this.onMetadataLoaded = () => {
111
+ // La durata potrebbe arrivare tardi (specie con streaming).
112
+ const d = audio.duration;
113
+ if (Number.isFinite(d) && d > 0) {
114
+ this.duration = d;
115
+ } else if (!this.timingReady) {
116
+ this.duration = this.estimateDurationSecondsFromText();
117
+ }
118
+
119
+ this.timingReady = true;
120
+ this.buildFakeTiming();
121
+ if (this.skipSyncAnimation) {
122
+ this.markAllWordsPast();
123
+ this.cdr.detectChanges();
124
+ return;
125
+ }
126
+ if (this.playbackStarted) {
127
+ this.syncStatesFromCurrentTime();
128
+ }
129
+ this.cdr.detectChanges();
130
+ };
131
+
132
+ audio.addEventListener('loadedmetadata', this.onMetadataLoaded);
133
+ audio.addEventListener('ended', this.onPlaybackEnded);
134
+
135
+ // Prepara subito le parole (durata stimata) e poi aggiorna quando arriva la metadata reale.
136
+ this.duration = this.estimateDurationSecondsFromText();
137
+ this.timingReady = true;
138
+ this.buildFakeTiming();
139
+ if (this.skipSyncAnimation) {
140
+ this.markAllWordsPast();
141
+ this.cdr.detectChanges();
142
+ return;
143
+ }
144
+ this.cdr.detectChanges();
145
+
146
+ setTimeout(() => {
147
+ if (this.playbackRequested || this.destroyed) {
148
+ return;
149
+ }
150
+ this.playbackRequested = true;
151
+ this.ttsPlayback.requestStart(this.playbackOwnerId, () => {
152
+ if (this.destroyed) {
153
+ this.ttsPlayback.releaseIfCurrent(this.playbackOwnerId);
154
+ return;
155
+ }
156
+ this.playbackStarted = true;
157
+ this.syncStatesFromCurrentTime();
158
+ this.cdr.detectChanges();
159
+ this.startPlayback(audio);
160
+ });
161
+ }, 200);
162
+
163
+ // Stop signal: user pressed X while this TTS was playing or queued.
164
+ this.stopAllSub = this.ttsPlayback.stopAllPlayback$.subscribe(() => {
165
+ if (!this.playbackRequested && !this.playbackStarted) {
166
+ return;
167
+ }
168
+ this.destroyed = true;
169
+ this.playbackStarted = false;
170
+ this.cleanupStreaming();
171
+ try {
172
+ audio.pause();
173
+ audio.currentTime = 0;
174
+ } catch {
175
+ /* ignore */
176
+ }
177
+ this.markAllWordsPast();
178
+ if (this.message) {
179
+ this.message.isJustRecived = false;
180
+ }
181
+ this.cdr.detectChanges();
182
+ });
183
+
184
+ // Preempt signal: a newer message requested start while this one was playing.
185
+ // Only react when the emitted id matches this component's own ownerId.
186
+ this.preemptSub = this.ttsPlayback.preemptPlayback$.subscribe((stoppedId) => {
187
+ if (stoppedId !== this.playbackOwnerId) {
188
+ return;
189
+ }
190
+ this.playbackStarted = false;
191
+ this.cleanupStreaming();
192
+ try {
193
+ audio.pause();
194
+ audio.currentTime = 0;
195
+ } catch {
196
+ /* ignore */
197
+ }
198
+ this.markAllWordsPast();
199
+ if (this.message) {
200
+ this.message.isJustRecived = false;
201
+ }
202
+ this.cdr.detectChanges();
203
+ // No releaseIfCurrent call — the coordinator already cleared currentOwnerId before emitting.
204
+ });
205
+ }
206
+
207
+ ngOnDestroy(): void {
208
+ this.destroyed = true;
209
+ this.playbackStarted = false;
210
+ this.cleanupStreaming();
211
+ this.stopAllSub?.unsubscribe();
212
+ this.stopAllSub = undefined;
213
+ this.preemptSub?.unsubscribe();
214
+ this.preemptSub = undefined;
215
+
216
+ const audio = this.audioRef?.nativeElement;
217
+ if (audio) {
218
+ try {
219
+ audio.pause();
220
+ audio.currentTime = 0;
221
+ } catch {
222
+ /* ignore */
223
+ }
224
+ }
225
+ this.ttsPlayback.release(this.playbackOwnerId);
226
+
227
+ if (!audio) {
228
+ return;
229
+ }
230
+ if (this.onMetadataLoaded) {
231
+ audio.removeEventListener('loadedmetadata', this.onMetadataLoaded);
232
+ }
233
+ if (this.onPlaybackEnded) {
234
+ audio.removeEventListener('ended', this.onPlaybackEnded);
235
+ }
236
+ }
237
+
238
+ private startPlayback(audio: HTMLAudioElement): void {
239
+ const messageSrc = (this.message as any)?.metadata?.src as string | undefined;
240
+
241
+ if (this.message?.type === 'tts') {
242
+ const streamEndpoint = this.voiceService.proxyTtsStreamUrl;
243
+ const fullFileEndpoint = this.voiceService.proxyTtsUrl;
244
+ if (streamEndpoint) {
245
+ this.startStreamingFromEndpoint(audio, streamEndpoint, fullFileEndpoint, messageSrc);
246
+ return;
247
+ }
248
+ if (fullFileEndpoint) {
249
+ this.fetchFullFileFromEndpoint(audio, fullFileEndpoint);
250
+ return;
251
+ }
252
+ if (messageSrc) {
253
+ this.playDirectUrl(audio, messageSrc);
254
+ return;
255
+ }
256
+ this.handlePlaybackError();
257
+ return;
258
+ }
259
+
260
+ if (!messageSrc) {
261
+ this.playbackStarted = false;
262
+ this.ttsPlayback.releaseIfCurrent(this.playbackOwnerId);
263
+ this.markAllWordsPast();
264
+ if (this.message) {
265
+ this.message.isJustRecived = false;
266
+ }
267
+ this.cdr.detectChanges();
268
+ return;
269
+ }
270
+
271
+ this.playDirectUrl(audio, messageSrc);
272
+ }
273
+
274
+ private playDirectUrl(audio: HTMLAudioElement, src: string): void {
275
+ audio.src = src;
276
+ try {
277
+ audio.currentTime = 0;
278
+ } catch {
279
+ /* ignore */
280
+ }
281
+ audio.play().catch(() => this.handlePlaybackError());
282
+ }
283
+
284
+ private startStreamingFromEndpoint(
285
+ audio: HTMLAudioElement,
286
+ endpoint: string,
287
+ fullFileEndpoint?: string | null,
288
+ directFallbackSrc?: string,
289
+ ): void {
290
+ this.cleanupStreaming();
291
+
292
+ const jwt = this.getJwtToken();
293
+ const voiceSettings = this.getVoiceSettingsBody();
294
+ const requestBody = this.buildTtsRequestBody(voiceSettings);
295
+ let fallbackUsed = false;
296
+ const fallback = () => {
297
+ if (fallbackUsed) {
298
+ this.handlePlaybackError();
299
+ return;
300
+ }
301
+ fallbackUsed = true;
302
+ this.cleanupStreaming();
303
+ if (fullFileEndpoint) {
304
+ this.fetchFullFileFromEndpoint(audio, fullFileEndpoint);
305
+ return;
306
+ }
307
+ if (directFallbackSrc) {
308
+ this.playDirectUrl(audio, directFallbackSrc);
309
+ return;
310
+ }
311
+ this.handlePlaybackError();
312
+ };
313
+
314
+ // <audio src="..."> non può inviare header/body: serve fetch().
315
+ const hasMse = typeof (window as any).MediaSource !== 'undefined';
316
+ if (!hasMse) {
317
+ fallback();
318
+ return;
319
+ }
320
+
321
+ const MediaSourceCtor = (window as any).MediaSource as typeof MediaSource;
322
+ const mediaSource = new MediaSourceCtor();
323
+ const objectUrl = URL.createObjectURL(mediaSource);
324
+ this.mediaSourceObjectUrl = objectUrl;
325
+ audio.src = objectUrl;
326
+
327
+ const abort = new AbortController();
328
+ this.streamAbort = abort;
329
+
330
+ const onSourceOpen = async () => {
331
+ mediaSource.removeEventListener('sourceopen', onSourceOpen);
332
+ try {
333
+ const headers: Record<string, string> = {
334
+ 'Content-Type': 'application/json',
335
+ 'Authorization': `${jwt}`
336
+ };
337
+
338
+ const response = await fetch(endpoint, {
339
+ method: 'POST',
340
+ headers,
341
+ body: JSON.stringify(requestBody),
342
+ signal: abort.signal,
343
+ });
344
+ if (!response.ok || !response.body) {
345
+ throw new Error(`TTS stream request failed (${response.status})`);
346
+ }
347
+
348
+ const headerType = (response.headers.get('content-type') || '').split(';')[0].trim();
349
+ if (headerType && !MediaSourceCtor.isTypeSupported(headerType)) {
350
+ // Fallback: fetch completo e play via blob (no streaming).
351
+ fallback();
352
+ return;
353
+ }
354
+
355
+ const mime = headerType || 'audio/mpeg';
356
+ if (!MediaSourceCtor.isTypeSupported(mime)) {
357
+ fallback();
358
+ return;
359
+ }
360
+
361
+ const sourceBuffer = mediaSource.addSourceBuffer(mime);
362
+ sourceBuffer.mode = 'sequence';
363
+
364
+ const reader = response.body.getReader();
365
+ const queue: Uint8Array[] = [];
366
+ let doneReading = false;
367
+ let started = false;
368
+
369
+ const tryEndOfStream = () => {
370
+ if (doneReading && queue.length === 0 && !sourceBuffer.updating) {
371
+ try {
372
+ mediaSource.endOfStream();
373
+ } catch {
374
+ /* ignore */
375
+ }
376
+ }
377
+ };
378
+
379
+ const pump = () => {
380
+ if (abort.signal.aborted) {
381
+ return;
382
+ }
383
+ if (sourceBuffer.updating) {
384
+ return;
385
+ }
386
+ const chunk = queue.shift();
387
+ if (!chunk) {
388
+ tryEndOfStream();
389
+ return;
390
+ }
391
+ try {
392
+ const ab = chunk.buffer.slice(
393
+ chunk.byteOffset,
394
+ chunk.byteOffset + chunk.byteLength,
395
+ ) as ArrayBuffer;
396
+ sourceBuffer.appendBuffer(ab);
397
+ } catch {
398
+ fallback();
399
+ }
400
+ };
401
+
402
+ sourceBuffer.addEventListener('updateend', () => {
403
+ if (!started && this.playbackStarted && !this.destroyed) {
404
+ started = true;
405
+ audio.play().catch(() => fallback());
406
+ }
407
+ pump();
408
+ });
409
+
410
+ // Primo pump (se arrivano subito chunk)
411
+ pump();
412
+
413
+ while (!abort.signal.aborted) {
414
+ const { value, done } = await reader.read();
415
+ if (done) {
416
+ doneReading = true;
417
+ break;
418
+ }
419
+ if (value && value.byteLength > 0) {
420
+ queue.push(value);
421
+ pump();
422
+ }
423
+ }
424
+
425
+ doneReading = true;
426
+ tryEndOfStream();
427
+ } catch {
428
+ if (!abort.signal.aborted) {
429
+ fallback();
430
+ }
431
+ }
432
+ };
433
+
434
+ mediaSource.addEventListener('sourceopen', onSourceOpen);
435
+ }
436
+
437
+ private handlePlaybackError(): void {
438
+ this.playbackStarted = false;
439
+ this.cleanupStreaming();
440
+ this.ttsPlayback.releaseIfCurrent(this.playbackOwnerId);
441
+ this.markAllWordsPast();
442
+ if (this.message) {
443
+ this.message.isJustRecived = false;
444
+ }
445
+ this.cdr.detectChanges();
446
+ }
447
+
448
+ private cleanupStreaming(): void {
449
+ try {
450
+ this.streamAbort?.abort();
451
+ } catch {
452
+ /* ignore */
453
+ }
454
+ this.streamAbort = undefined;
455
+
456
+ if (this.mediaSourceObjectUrl) {
457
+ try {
458
+ URL.revokeObjectURL(this.mediaSourceObjectUrl);
459
+ } catch {
460
+ /* ignore */
461
+ }
462
+ this.mediaSourceObjectUrl = undefined;
463
+ }
464
+ }
465
+
466
+ private getJwtToken(): string | null {
467
+ const token = (this.globals?.tiledeskToken || this.globals?.jwt || '').trim();
468
+ return token.length > 0 ? token : null;
469
+ }
470
+
471
+ private getVoiceSettingsBody(): unknown {
472
+ const raw = (this.message as any)?.metadata?.voiceSettings;
473
+ if (raw === null || raw === undefined) {
474
+ return {};
475
+ }
476
+ if (typeof raw === 'string') {
477
+ const s = raw.trim();
478
+ if (!s) return {};
479
+ try {
480
+ return JSON.parse(s);
481
+ } catch {
482
+ // se non è JSON valido, invialo come stringa (il backend può gestirlo)
483
+ return { voiceSettings: raw };
484
+ }
485
+ }
486
+ return raw;
487
+ }
488
+
489
+ private async fetchAsBlobAndPlay(
490
+ audio: HTMLAudioElement,
491
+ endpoint: string,
492
+ jwt: string | null,
493
+ requestBody: unknown,
494
+ ): Promise<void> {
495
+ try {
496
+ const headers: Record<string, string> = {
497
+ 'Content-Type': 'application/json',
498
+ 'Authorization': `${jwt}`
499
+ };
500
+
501
+ const response = await fetch(endpoint, {
502
+ method: 'POST',
503
+ headers,
504
+ body: JSON.stringify(requestBody ?? {}),
505
+ signal: this.streamAbort?.signal,
506
+ });
507
+
508
+ if (!response.ok) {
509
+ throw new Error(`TTS request failed (${response.status})`);
510
+ }
511
+
512
+ const blob = await response.blob();
513
+ if (this.destroyed) {
514
+ return;
515
+ }
516
+
517
+ const objectUrl = URL.createObjectURL(blob);
518
+ this.mediaSourceObjectUrl = objectUrl;
519
+ audio.src = objectUrl;
520
+ audio.play().catch(() => this.handlePlaybackError());
521
+ } catch {
522
+ this.handlePlaybackError();
523
+ }
524
+ }
525
+
526
+ private fetchFullFileFromEndpoint(audio: HTMLAudioElement, endpoint: string): void {
527
+ const jwt = this.getJwtToken();
528
+ const voiceSettings = this.getVoiceSettingsBody();
529
+ const requestBody = this.buildTtsRequestBody(voiceSettings, false);
530
+ void this.fetchAsBlobAndPlay(audio, endpoint, jwt, requestBody);
531
+ }
532
+
533
+ private buildTtsRequestBody(voiceSettings: unknown, streaming = true): unknown {
534
+ const text = this.message?.text ?? '';
535
+ if (
536
+ voiceSettings &&
537
+ typeof voiceSettings === 'object' &&
538
+ !Array.isArray(voiceSettings)
539
+ ) {
540
+ return {
541
+ outputFormat: BROWSER_TTS_OUTPUT_FORMAT,
542
+ ...(voiceSettings as Record<string, unknown>),
543
+ text,
544
+ streaming,
545
+ };
546
+ }
547
+ return {
548
+ voiceSettings,
549
+ text,
550
+ streaming,
551
+ outputFormat: BROWSER_TTS_OUTPUT_FORMAT,
552
+ };
553
+ }
554
+
555
+ private markAllWordsPast(): void {
556
+ this.words.forEach((w) => {
557
+ w.state = 'past';
558
+ });
559
+ this.activeIndex = -1;
560
+ }
561
+
562
+ private estimateDurationSecondsFromText(): number {
563
+ const rawWords = (this.message?.text || '')
564
+ .trim()
565
+ .split(/\s+/)
566
+ .filter((w) => w.length > 0);
567
+ if (rawWords.length === 0) {
568
+ return 1;
569
+ }
570
+ // ~140 WPM → ~0.43s/word
571
+ return Math.max(1, rawWords.length * 0.43);
572
+ }
573
+
574
+ buildFakeTiming(): void {
575
+ const rawWords = (this.message?.text || '')
576
+ .trim()
577
+ .split(/\s+/)
578
+ .filter((w) => w.length > 0);
579
+ if (rawWords.length === 0) {
580
+ this.words = [];
581
+ return;
582
+ }
583
+ const step = this.duration / rawWords.length;
584
+
585
+ this.words = rawWords.map((w, i) => ({
586
+ text: w,
587
+ start: i * step,
588
+ end: (i + 1) * step,
589
+ state: 'future' as const,
590
+ }));
591
+ }
592
+
593
+ syncStatesFromCurrentTime(): void {
594
+ if (this.skipSyncAnimation) {
595
+ return;
596
+ }
597
+ const audio = this.audioRef?.nativeElement;
598
+ if (!audio || this.words.length === 0) {
599
+ return;
600
+ }
601
+ this.currentTime = audio.currentTime;
602
+ let newActiveIndex = -1;
603
+
604
+ this.words.forEach((w, i) => {
605
+ if (this.currentTime >= w.end) {
606
+ w.state = 'past';
607
+ } else if (this.currentTime >= w.start && this.currentTime < w.end) {
608
+ w.state = 'active';
609
+ newActiveIndex = i;
610
+ } else {
611
+ w.state = 'future';
612
+ }
613
+ });
614
+
615
+ if (newActiveIndex !== this.activeIndex) {
616
+ this.activeIndex = newActiveIndex;
617
+ this.scrollToActive();
618
+ }
619
+ }
620
+
621
+ onTimeUpdate(): void {
622
+ if (!this.playbackStarted) {
623
+ return;
624
+ }
625
+ this.syncStatesFromCurrentTime();
626
+ }
627
+
628
+ scrollToActive(): void {
629
+ const container = this.transcriptBox?.nativeElement;
630
+ const active = container?.querySelector('.active') as HTMLElement;
631
+
632
+ if (active) {
633
+ active.scrollIntoView({
634
+ behavior: 'smooth',
635
+ block: 'center',
636
+ });
637
+ }
638
+ }
639
+
640
+ trackByIndex(index: number): number {
641
+ return index;
642
+ }
643
+ }
@@ -1,9 +1,9 @@
1
1
  <div class="c21-icon-avatar">
2
2
  <div class="c21-avatar-image profile_image">
3
3
  <!-- is a BOT -->
4
- <img *ngIf="senderID?.indexOf('bot_') !== -1 || senderFullname.toLowerCase().includes('bot')" [src]="url" (error)="onBotImgError($event)" (load)="onLoadedBot($event)"/>
4
+ <img *ngIf="senderID?.indexOf('bot_') !== -1 || senderFullname.toLowerCase().includes('bot')" [src]="url" [attr.alt]="senderFullname || 'Bot'" role="img" (error)="onBotImgError($event)" (load)="onLoadedBot($event)"/>
5
5
  <!-- is a HUMAN -->
6
- <img *ngIf="senderID?.indexOf('bot_') === -1 && !senderFullname.toLowerCase().includes('bot')" [src]="url" (error)="onHumanImgError($event)" (load)="onLoadedHuman($event)"/>
6
+ <img *ngIf="senderID?.indexOf('bot_') === -1 && !senderFullname.toLowerCase().includes('bot')" [src]="url" [attr.alt]="senderFullname || 'User'" role="img" (error)="onHumanImgError($event)" (load)="onLoadedHuman($event)"/>
7
7
  </div>
8
8
  </div>
9
9