File size: 69,758 Bytes
26dd9d1 59fecf2 26dd9d1 e4ab6d0 59fecf2 b366f52 26dd9d1 6dc3ffe 26dd9d1 b366f52 57dd211 e4ab6d0 6dc3ffe e4ab6d0 26dd9d1 e4ab6d0 26dd9d1 59fecf2 b366f52 57dd211 b366f52 e4ab6d0 26dd9d1 3cd5de7 e4ab6d0 b366f52 59fecf2 e4ab6d0 3cd5de7 e4ab6d0 59fecf2 b366f52 59fecf2 26dd9d1 b366f52 3cd5de7 e4ab6d0 b366f52 e4ab6d0 b366f52 57dd211 26dd9d1 3cd5de7 26dd9d1 59fecf2 26dd9d1 59fecf2 26dd9d1 e4ab6d0 26dd9d1 e4ab6d0 26dd9d1 e4ab6d0 26dd9d1 e4ab6d0 b366f52 e4ab6d0 b366f52 3cd5de7 26dd9d1 b366f52 57dd211 b366f52 3cd5de7 e4ab6d0 26dd9d1 e4ab6d0 26dd9d1 e4ab6d0 26dd9d1 e4ab6d0 26dd9d1 e4ab6d0 b366f52 e4ab6d0 26dd9d1 3cd5de7 26dd9d1 e4ab6d0 26dd9d1 b366f52 e4ab6d0 b366f52 e4ab6d0 57dd211 b366f52 59fecf2 e4ab6d0 59fecf2 e4ab6d0 b366f52 57dd211 b366f52 57dd211 b366f52 57dd211 b366f52 57dd211 b366f52 59fecf2 e4ab6d0 59fecf2 b366f52 e4ab6d0 b366f52 e4ab6d0 59fecf2 e4ab6d0 b366f52 e4ab6d0 59fecf2 e4ab6d0 62fe943 b366f52 e4ab6d0 b366f52 e4ab6d0 57dd211 e4ab6d0 b366f52 59fecf2 e4ab6d0 26dd9d1 59fecf2 e4ab6d0 3cd5de7 b366f52 3cd5de7 b366f52 e4ab6d0 62fe943 3cd5de7 62fe943 3cd5de7 62fe943 3cd5de7 62fe943 3cd5de7 62fe943 3cd5de7 62fe943 3cd5de7 62fe943 3cd5de7 62fe943 3cd5de7 62fe943 3cd5de7 26dd9d1 b366f52 57dd211 b366f52 e4ab6d0 b366f52 e4ab6d0 b366f52 59fecf2 e4ab6d0 b366f52 62fe943 e4ab6d0 62fe943 26dd9d1 62fe943 26dd9d1 e4ab6d0 26dd9d1 62fe943 26dd9d1 e4ab6d0 26dd9d1 62fe943 26dd9d1 62fe943 59fecf2 e4ab6d0 62fe943 26dd9d1 62fe943 26dd9d1 62fe943 26dd9d1 62fe943 26dd9d1 62fe943 26dd9d1 62fe943 26dd9d1 62fe943 26dd9d1 e4ab6d0 26dd9d1 62fe943 26dd9d1 62fe943 e4ab6d0 59fecf2 e4ab6d0 62fe943 e4ab6d0 26dd9d1 62fe943 26dd9d1 e4ab6d0 62fe943 3cd5de7 26dd9d1 e4ab6d0 26dd9d1 e4ab6d0 26dd9d1 e4ab6d0 26dd9d1 e4ab6d0 3cd5de7 26dd9d1 3cd5de7 26dd9d1 e4ab6d0 59fecf2 e4ab6d0 b366f52 e4ab6d0 59fecf2 e4ab6d0 b366f52 e4ab6d0 b366f52 e4ab6d0 59fecf2 e4ab6d0 3cd5de7 e4ab6d0 59fecf2 b366f52 e4ab6d0 57dd211 e4ab6d0 b366f52 e4ab6d0 26dd9d1 e4ab6d0 59fecf2 e4ab6d0 59fecf2 b366f52 26dd9d1 e4ab6d0 26dd9d1 e4ab6d0 26dd9d1 e4ab6d0 59fecf2 26dd9d1 59fecf2 26dd9d1 e4ab6d0 26dd9d1 e4ab6d0 26dd9d1 e4ab6d0 3cd5de7 26dd9d1 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 26dd9d1 3cd5de7 26dd9d1 e4ab6d0 26dd9d1 3cd5de7 26dd9d1 59fecf2 26dd9d1 59fecf2 26dd9d1 59fecf2 26dd9d1 59fecf2 e4ab6d0 26dd9d1 59fecf2 26dd9d1 3cd5de7 26dd9d1 3cd5de7 26dd9d1 59fecf2 26dd9d1 e4ab6d0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 |
import { Component, Inject, OnDestroy, PLATFORM_ID, ChangeDetectorRef, ViewChild, ElementRef, Renderer2, AfterViewChecked, Output, EventEmitter } from '@angular/core';
import { ApiService } from './api.service';
import { FormsModule } from '@angular/forms';
import { CommonModule, isPlatformBrowser } from '@angular/common';
import { Router, RouterModule } from '@angular/router';
import { Subscription } from 'rxjs';
import { finalize } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { lastValueFrom } from 'rxjs';
import { HeaderComponent } from '../shared/header/header.component';
type Grade = 'lowergrade' | 'midgrade' | 'highergrade';
@Component({
selector: 'app-chat',
standalone: true,
imports: [FormsModule, CommonModule, RouterModule, HeaderComponent],
templateUrl: './chat.component.html',
styleUrls: ['./chat.component.css']
})
export class ChatComponent implements OnDestroy {
@ViewChild('waveformCanvas') waveformCanvas!: ElementRef<HTMLCanvasElement>;
@ViewChild('popupTranscriptEl') popupTranscriptEl!: ElementRef<HTMLDivElement>;
@Output() transcriptConfirmed = new EventEmitter<string>();
isRecording = false;
showMicPopup = false;
popupTranscript = '';
errorMessage = '';
private recognition: any = null;
private _recordingFinalBuffer = '';
private _recordingInterimBuffer = '';
private audioContext: AudioContext | null = null;
private analyser: AnalyserNode | null = null;
private dataArray: Uint8Array | null = null;
private mediaStream: MediaStream | null = null;
private animationFrameId: number | null = null;
// restart helpers
private _recognitionActive = false;
private _restartTimer: any = null;
private _restartAttempts = 0;
private _maxRestartDelay = 1500;
showQuestions: boolean = false;
isSubmitting: boolean = false;
/** Generated (initial) or follow-up questions shown on focus */
pdfQuestions: string[] = [];
pdfLoading: boolean = false;
/** Chat state */
userInput: string = '';
messages: Array<{
from: string;
text: string;
timestamp: string;
isPlaying?: boolean;
isMuted?: boolean;
suggestions?: string[];
source_ids?: string[];
videoUrl?: string;
audioUrl?: string;
playingVideoUrl?: string;
pending?: boolean;
isSynthesizing?: boolean;
isVideoSynthesizing?: boolean;
}> = [];
isTyping: boolean = false;
@ViewChild('chatBox') chatBox!: ElementRef;
/** Speech / mic state */
isLoadingSpeech: boolean = false;
selectedVoice: SpeechSynthesisVoice | null = null;
speechSynthesisInstance: SpeechSynthesisUtterance | null = null;
isListening: boolean = false;
isProcessingSpeech: boolean = false;
isSpeaking: boolean = false;
isAudioPaused: boolean = false;
/** Suggestions for typed input (powered by PDF too) */
suggestions: string[] = [];
isInputValid = false;
/** Popup */
/** Subscriptions */
private responseSub?: Subscription;
/** Store last Q/A to drive follow-ups */
private lastQuestion: string | null = null;
private lastAnswer: string | null = null;
private lastSourceIds: string[] = [];
/** NEW: only allow follow-ups when last answer was grounded in textbook pages */
private lastAnswerHasContext: boolean = false;
// --- Multi-chat state: keep active `this.messages` pointing to the active chat
private activeChatIndex = 0; // 0 = primary, 1 = secondary
// Per-chat storage (persist when switching)
private primaryMessages: typeof this.messages = [];
private secondaryMessages: typeof this.messages = [];
private primaryIsVideoEnabledIndex: boolean[] = [];
private secondaryIsVideoEnabledIndex: boolean[] = [];
private primaryLastQuestionContext = '';
private secondaryLastQuestionContext = '';
private primaryLastAnswerContext = '';
private secondaryLastAnswerContext = '';
private primaryLastSourceIdsContext: string[] = [];
private secondaryLastSourceIdsContext: string[] = [];
private primaryCurrentFollowups: string[] = [];
private secondaryCurrentFollowups: string[] = [];
private primaryPendingAiIndex: number | null = null;
private secondaryPendingAiIndex: number | null = null;
private primaryServerAudioMessageIndex: number | null = null;
private secondaryServerAudioMessageIndex: number | null = null;
private primaryIsReadingIndex: number | null = null;
private secondaryIsReadingIndex: number | null = null;
private primaryIsVideoPlayingIndex: number | null = null;
private secondaryIsVideoPlayingIndex: number | null = null;
// --- end multi-chat state
currentFollowups: string[] = [];
/*private shouldAutoScroll = true;*/
videoUrl = '';
aiResponseInterval: any = null;
isAiResponding = false;
isVideoEnabledIndex: boolean[] = [];
private currentExplainSub: Subscription | null = null;
private currentFollowupsSub: Subscription | null = null;
serverAudio: HTMLAudioElement | null = null;
serverAudioMessageIndex: number | null = null;
private pendingAiIndex: number | null = null;
isMuted = false;
showUserGuide = false;
openDropdownIndex: number | null = null;
isReadingIndex: number | null = null;
isVideoPlayingIndex: number | null = null;
private readAloudUtterance: SpeechSynthesisUtterance | null = null;
private lastQuestionContext: string = '';
private lastAnswerContext: string = '';
private lastSourceIdsContext: string[] = [];
private popupListeningMode = false;
private _savedRecognitionOnResult: any = null;
private _savedInterimResults = false;
private _suppressDefaultOnResult = false;
private shouldAutoScroll = true;
constructor(
private apiService: ApiService,
private cdr: ChangeDetectorRef,
@Inject(PLATFORM_ID) private platformId: object,
private http: HttpClient,
private router: Router,
private renderer: Renderer2
) {
// Initialize primary/secondary storage with the current defaults
this.primaryMessages = this.messages;
this.secondaryMessages = [];
this.primaryIsVideoEnabledIndex = this.isVideoEnabledIndex;
this.secondaryIsVideoEnabledIndex = [];
this.primaryLastQuestionContext = this.lastQuestionContext;
this.primaryLastAnswerContext = this.lastAnswerContext;
this.primaryLastSourceIdsContext = this.lastSourceIdsContext.slice();
this.primaryCurrentFollowups = this.currentFollowups.slice();
this.primaryPendingAiIndex = this.pendingAiIndex;
this.primaryServerAudioMessageIndex = this.serverAudioMessageIndex;
this.primaryIsReadingIndex = this.isReadingIndex;
this.primaryIsVideoPlayingIndex = this.isVideoPlayingIndex;
// secondary fields are already empty/default
if (!isPlatformBrowser(this.platformId)) return;
const SR = (window as any).SpeechRecognition || (window as any).webkitSpeechRecognition;
if (!SR) {
console.warn('SpeechRecognition not supported');
return;
}
this.recognition = new SR();
this.recognition.continuous = true;
this.recognition.interimResults = true;
this.recognition.lang = 'en-US';
try { this.recognition.maxAlternatives = 1; } catch { /* ignore */ }
this.recognition.onstart = () => { this._recognitionActive = true; this._restartAttempts = 0; };
this.recognition.onspeechstart = () => { this._recognitionActive = true; };
this.recognition.onspeechend = () => { this._recognitionActive = false; };
this.recognition.onresult = (event: any) => {
if (!this.isRecording) return;
let interim = '';
let final = '';
for (let i = event.resultIndex; i < event.results.length; i++) {
const res = event.results[i];
const t = (res && res[0] && res[0].transcript) ? res[0].transcript : '';
if (res.isFinal) final += t + ' ';
else interim += t + ' ';
}
if (final) {
this._recordingFinalBuffer += final;
this._recordingInterimBuffer = '';
} else {
this._recordingInterimBuffer = interim;
}
// keep UI quiet while recording; show only after Done pressed
this.cdr.detectChanges();
};
this.recognition.onerror = (e: any) => {
console.error('Recognition error', e);
if (e?.error === 'not-allowed') {
this.errorMessage = 'Microphone access denied';
this.isRecording = false;
} else {
this.errorMessage = `Error: ${e?.error || 'unknown'}`;
}
if (this.isRecording && (e?.error === 'no-speech' || e?.error === 'aborted' || e?.error === 'network')) {
if (this._restartTimer) clearTimeout(this._restartTimer);
const delay = Math.min(400 * (this._restartAttempts + 1), this._maxRestartDelay);
this._restartTimer = setTimeout(() => {
try { if (this.recognition && !this._recognitionActive) this.recognition.start(); } catch { this._restartAttempts++; }
}, delay);
} else {
if (e?.error !== 'not-allowed') this._recognitionActive = false;
}
this.cdr.detectChanges();
};
this.recognition.onend = () => {
this._recognitionActive = false;
if (this.isRecording && this.showMicPopup) {
if (this._restartTimer) clearTimeout(this._restartTimer);
const delay = Math.min(250 + (this._restartAttempts * 200), this._maxRestartDelay);
this._restartTimer = setTimeout(() => {
try { if (this.recognition && !this._recognitionActive) this.recognition.start(); } catch { this._restartAttempts++; }
}, delay);
}
};
this.recognition.onnomatch = () => { /* noop */ };
}
selectHardcodedQuestion(question: string): void {
try { console.log('[CHAT] default question selected:', question); } catch { }
this.showQuestions = false;
this.sendMessage(question);
this.userInput = '';
}
private ensureGradeLevel(defaultGrade: Grade = 'lowergrade'): void {
const g = (localStorage.getItem('gradeLevel') || '').toLowerCase();
if (g !== 'lowergrade' && g !== 'midgrade' && g !== 'highergrade') {
localStorage.setItem('gradeLevel', defaultGrade);
console.info('[Chat] gradeLevel not set; defaulted to', defaultGrade);
} else {
console.info('[Chat] gradeLevel =', g);
}
}
ngOnInit(): void {
this.ensureGradeLevel();
if (window.speechSynthesis.onvoiceschanged !== undefined) {
window.speechSynthesis.onvoiceschanged = () => {
this.loadVoices();
};
}
this.loadVoices();
}
ngOnDestroy(): void {
if (this.currentExplainSub) { this.currentExplainSub.unsubscribe(); this.currentExplainSub = null; }
if (this.currentFollowupsSub) { this.currentFollowupsSub.unsubscribe(); this.currentFollowupsSub = null; }
if (isPlatformBrowser(this.platformId)) {
try { window.speechSynthesis?.cancel(); } catch { /* noop */ }
window.removeEventListener('beforeunload', this.handleUnload);
//window.removeEventListener('storage', this.storageHandler);
}
this.stopServerAudio();
try { window.speechSynthesis?.cancel(); } catch { /* noop */ }
}
private handleUnload = (): void => {
if (window.speechSynthesis) {
window.speechSynthesis.cancel();
}
};
ngAfterViewInit() {
this.chatBox.nativeElement.addEventListener('scroll', () => {
const el = this.chatBox.nativeElement;
const atBottom = el.scrollHeight - el.clientHeight - el.scrollTop < 50;
this.shouldAutoScroll = atBottom;
});
}
scrollToBottom(): void {
if (this.shouldAutoScroll) {
try {
this.chatBox.nativeElement.scrollTo({
top: this.chatBox.nativeElement.scrollHeight,
behavior: 'smooth'
});
} catch { }
}
}
// --- Public API: toggle between chat A/B
toggleChat(): void {
const target = 1 - this.activeChatIndex;
this.switchToChat(target);
}
switchToChat(index: number): void {
if (index === this.activeChatIndex) return;
// Stop any playing media on the current chat first so saved state doesn't preserve "playing" flags
this.stopServerAudio();
try { window.speechSynthesis?.cancel(); } catch { /* noop */ }
this.stopAllVideo();
// Save current active chat state (now that media is stopped)
this.saveCurrentChatState(this.activeChatIndex);
// Restore target chat state
this.restoreChatState(index);
this.activeChatIndex = index;
this.cdr.detectChanges();
}
private saveCurrentChatState(idx: number): void {
// Make a shallow copy of messages and clear transient playback flags so the saved chat never
// preserves an "isPlaying" or inline-playing video state.
const sanitizedMessages = (this.messages || []).map(m => ({
...m,
isPlaying: false,
playingVideoUrl: '',
// keep pending / audio/video urls etc. — only clear playback flags
}));
if (idx === 0) {
this.primaryMessages = sanitizedMessages;
this.primaryIsVideoEnabledIndex = (this.isVideoEnabledIndex || []).slice();
this.primaryLastQuestionContext = this.lastQuestionContext;
this.primaryLastAnswerContext = this.lastAnswerContext;
this.primaryLastSourceIdsContext = this.lastSourceIdsContext.slice();
this.primaryCurrentFollowups = this.currentFollowups.slice();
this.primaryPendingAiIndex = this.pendingAiIndex;
// serverAudioMessageIndex should be null because we stopped audio before saving
this.primaryServerAudioMessageIndex = null;
this.primaryIsReadingIndex = null;
this.primaryIsVideoPlayingIndex = null;
} else {
this.secondaryMessages = sanitizedMessages;
this.secondaryIsVideoEnabledIndex = (this.isVideoEnabledIndex || []).slice();
this.secondaryLastQuestionContext = this.lastQuestionContext;
this.secondaryLastAnswerContext = this.lastAnswerContext;
this.secondaryLastSourceIdsContext = this.lastSourceIdsContext.slice();
this.secondaryCurrentFollowups = this.currentFollowups.slice();
this.secondaryPendingAiIndex = this.pendingAiIndex;
this.secondaryServerAudioMessageIndex = null;
this.secondaryIsReadingIndex = null;
this.secondaryIsVideoPlayingIndex = null;
}
}
private restoreChatState(idx: number): void {
if (idx === 0) {
this.messages = this.primaryMessages || [];
this.isVideoEnabledIndex = this.primaryIsVideoEnabledIndex || [];
this.lastQuestionContext = this.primaryLastQuestionContext || '';
this.lastAnswerContext = this.primaryLastAnswerContext || '';
this.lastSourceIdsContext = (this.primaryLastSourceIdsContext || []).slice();
this.currentFollowups = (this.primaryCurrentFollowups || []).slice();
this.pendingAiIndex = this.primaryPendingAiIndex;
this.serverAudioMessageIndex = this.primaryServerAudioMessageIndex;
this.isReadingIndex = this.primaryIsReadingIndex;
this.isVideoPlayingIndex = this.primaryIsVideoPlayingIndex;
} else {
this.messages = this.secondaryMessages || [];
this.isVideoEnabledIndex = this.secondaryIsVideoEnabledIndex || [];
this.lastQuestionContext = this.secondaryLastQuestionContext || '';
this.lastAnswerContext = this.secondaryLastAnswerContext || '';
this.lastSourceIdsContext = (this.secondaryLastSourceIdsContext || []).slice();
this.currentFollowups = (this.secondaryCurrentFollowups || []).slice();
this.pendingAiIndex = this.secondaryPendingAiIndex;
this.serverAudioMessageIndex = this.secondaryServerAudioMessageIndex;
this.isReadingIndex = this.secondaryIsReadingIndex;
this.isVideoPlayingIndex = this.secondaryIsVideoPlayingIndex;
}
// Ensure arrays exist to avoid undefined errors elsewhere
if (!this.messages) this.messages = [];
if (!this.isVideoEnabledIndex) this.isVideoEnabledIndex = [];
if (!this.currentFollowups) this.currentFollowups = [];
}
startFromPopup(): void {
this._suppressDefaultOnResult = true;
try { if (this.recognition && typeof this.recognition.stop === 'function') this.recognition.stop(); } catch { }
this._restoreRecognitionHandlers();
this.isListening = false;
this.showMicPopup = false;
const message = (this.popupTranscript || '').trim();
this.popupTranscript = '';
if (!message) { this.errorMessage = 'No speech captured. Please try again.'; this.cdr.detectChanges(); return; }
this.sendMessage(message);
}
/** Show questions on focus: initial (no answer yet) or follow-ups (after an answer) */
showHardcodedQuestions(): void {
setTimeout(async () => {
this.showQuestions = true;
// NEW: only fetch follow-ups if last answer had grounded textbook context
if (this.lastAnswer && this.lastAnswerHasContext) {
this.fetchFollowupQuestions();
} else {
this.fetchInitialQuestions();
}
}, 100);
}
hideHardcodedQuestions(): void {
setTimeout(() => {
this.showQuestions = false;
}, 200);
}
/** Initial questions generated from PDFs (topicless OPEN) */
private fetchInitialQuestions(n: number = 5): void {
this.pdfLoading = true;
this.pdfQuestions = [];
this.apiService.generateOpenQuestions({ qtype: 'OPEN', n, topic: '' })
.subscribe({
next: (resp) => {
const items = Array.isArray(resp?.questions) ? resp.questions : [];
this.pdfQuestions = items.map((q: any) => typeof q === 'string' ? q : (q?.question || '')).filter(Boolean);
if (!this.pdfQuestions.length && resp?.note) {
console.warn('Question generator note:', resp.note);
}
this.pdfLoading = false;
this.cdr.detectChanges();
},
error: () => { this.pdfLoading = false; this.pdfQuestions = []; this.cdr.detectChanges(); }
});
}
/** Follow-ups after an answer */
private fetchFollowupQuestions(n: number = 5): void {
// NEW: guard — if no grounded context, fall back to initial questions
if (!this.lastQuestion || !this.lastAnswer || !this.lastAnswerHasContext || !this.lastSourceIds.length) {
this.fetchInitialQuestions(n);
return;
}
this.pdfLoading = true;
this.pdfQuestions = [];
// NEW: pass source_ids so backend keeps follow-ups within same pages/section
this.apiService.suggestFollowups({
last_question: this.lastQuestion,
last_answer: this.lastAnswer,
n,
// cast avoids TS excess property checks if your service type has not been widened yet
source_ids: this.lastSourceIds
} as any).subscribe({
next: (resp) => {
const list = Array.isArray(resp?.suggestions) ? resp.suggestions : [];
this.pdfQuestions = list.filter((s: string) => !!s);
this.pdfLoading = false;
this.cdr.detectChanges();
},
error: () => {
this.pdfLoading = false;
this.pdfQuestions = [];
this.cdr.detectChanges();
}
});
}
/** Click on one generated question */
selectGeneratedQuestion(question: string): void {
this.userInput = question;
this.showQuestions = false;
setTimeout(() => {
this.sendMessage();
this.userInput = '';
}, 80);
}
/** Type-ahead suggestions sourced from PDFs (OPEN on the typed topic) */
getSuggestions(): void {
if (!this.userInput || this.userInput.trim().length < 1 || this.isSpeaking) {
this.suggestions = [];
return;
}
this.apiService.generateOpenQuestions({ qtype: 'OPEN', n: 5, topic: this.userInput })
.subscribe({
next: (resp) => {
const items = Array.isArray(resp?.questions) ? resp.questions : [];
this.suggestions = items
.map((q: any) => (typeof q === 'string' ? q : (q?.question || '')))
.filter((s: string) => !!s);
},
error: () => { this.suggestions = []; }
});
}
selectSuggestion(suggestion: string): void {
this.userInput = suggestion;
this.suggestions = [];
this.sendMessage();
}
/** Send question to backend for an answer */
/** Send question to backend for an answer */
sendMessage(inputText?: string): void {
const message = inputText ? inputText.trim() : this.userInput.trim();
if (!message) return;
this.isSubmitting = true;
const timestamp = new Date().toLocaleTimeString();
this.messages.push({ from: 'user', text: message, timestamp });
this.userInput = '';
this.isTyping = true;
this.cdr.detectChanges();
this.shouldAutoScroll = true;
this.scrollToBottom();
this.responseSub = this.apiService.explainGrammar({
question: message,
// ask backend to synthesize audio/video only when user toggles are ON
synthesize_audio: Boolean(this.isVoiceEnabled),
synthesize_video: Boolean(this.isTutorEnabled)
})
.pipe(finalize(() => {
this.isSubmitting = false;
}))
.subscribe({
next: (response) => {
this.isTyping = false;
const explanation =
(response?.answer || response?.response || response?.text || 'No explanation available.').trim();
const sourceIds: string[] = Array.isArray(response?.source_ids)
? response.source_ids.filter((s: any) => typeof s === 'string' && s.trim().length > 0)
: [];
const audioUrl = (response?.audio_url || response?.audioUrl || '') as string;
const videoUrl = (response?.video_url || response?.videoUrl || '') as string;
// Store question + source ids *now*.
this.lastQuestion = message;
this.lastSourceIds = sourceIds;
const notFound = /No information available in the provided textbook content/i.test(explanation);
const hasContext = !!sourceIds.length && !notFound;
// Pass URLs along to the stream/attach to message
this.streamAiAnswer(explanation, sourceIds, hasContext, audioUrl || undefined, videoUrl || undefined);
},
error: (err) => {
console.error('API Error:', err);
this.isTyping = false;
const errorMessage = 'Error: Could not get a response from the server.';
this.streamAiAnswer(errorMessage, [], false);
}
});
}
/** Show AI answer word-by-word and start audio */
/** Show AI answer word-by-word and start audio */
private streamAiAnswer(explanation: string, sourceIds: string[], hasContext: boolean, audioUrl?: string, videoUrl?: string): void {
const text = (explanation || '').trim() || 'No explanation available.';
const timestamp = new Date().toLocaleTimeString();
// Create an empty AI message first
const aiIndex = this.messages.push({
from: 'ai',
text: '',
timestamp,
source_ids: sourceIds,
pending: true,
audioUrl: audioUrl || '',
videoUrl: videoUrl || '',
playingVideoUrl: ''
} as any) - 1;
// ensure video-enabled index has an entry for this message
if (this.isVideoEnabledIndex.length <= aiIndex) {
this.isVideoEnabledIndex[aiIndex] = false;
}
this.isAiResponding = true;
this.shouldAutoScroll = true;
this.cdr.detectChanges();
// Animate the text word by word
this.animateAiResponse(text, aiIndex, () => {
// When streaming is finished, we finally store lastAnswer
this.lastAnswer = text;
this.lastAnswerHasContext = hasContext;
// After the AI text finishes streaming, automatically play server media when toggles are enabled.
// If both audio and video are present and both toggles are enabled, video takes precedence.
this.autoPlayMediaForMessage(aiIndex);
});
// Only run client-side TTS when there is no server-provided audio URL.
if (!audioUrl && this.isVoiceEnabled) {
this.speakResponse(text);
}
}
/**
* Decide and start playback for a message that already has `audioUrl` and/or `videoUrl`.
* Behavior:
* - If both videoUrl && audioUrl && both video and audio toggles are ON -> play video (video wins)
* - Else if videoUrl && video toggle ON -> play video
* - Else if audioUrl && voice toggle ON -> play audio
*/
private autoPlayMediaForMessage(index: number): void {
const msg = this.messages[index] as any;
if (!msg) return;
const hasVideo = !!(msg.videoUrl && msg.videoUrl.trim());
const hasAudio = !!(msg.audioUrl && msg.audioUrl.trim());
// If both present and user enabled both, prefer video
if (hasVideo && this.isTutorEnabled) {
try { this.stopServerAudio(); } catch { /* noop */ }
try { window.speechSynthesis?.cancel(); } catch { /* noop */ }
// request autoplay (will mute to allow autoplay)
this.openMessageVideo(index, true);
return;
}
// If video present but tutor (video) is not enabled, do not auto-play it.
// Only auto-play audio if user enabled voice
if (hasAudio && this.isVoiceEnabled) {
// play server audio (this will stop videos if any)
// ensure inline videos are stopped
try { this.stopAllVideo(); } catch { /* noop */ }
// If message already has audioUrl, play it
this.playServerAudioForMessage(index);
return;
}
// If no server media played and no TTS, do nothing (user may manually click synth).
}
displaySource(tag: string): string {
if (!tag) return '';
const [path, pagePart] = tag.split('#p');
const file = path.split(/[/\\]/).pop() || path;
return pagePart ? `${file} p${pagePart}` : file;
}
/** Render helpers */
formatStructuredResponse(text: string): string {
return text
.replace(/\n/g, '<br>')
.replace(/(\d+)\.\s/g, '<b>$1.</b> ')
.replace(/\•\s/g, '✔️ ')
.replace(/\-\s/g, '🔹 ')
.replace(/(\*\*)(.*?)\1/g, '<b>$2</b>');
}
/** TTS helpers */
/** TTS helpers */
/** TTS helpers + typing animation */
animateAiResponse(
responseText: string,
targetIndex?: number,
onDone?: () => void
): void {
if (!responseText) {
this.isAiResponding = false;
return;
}
// Find or create the AI message to animate into
let aiIndex: number | null = null;
if (typeof targetIndex === 'number' &&
this.messages[targetIndex] &&
this.messages[targetIndex].from === 'ai') {
aiIndex = targetIndex;
} else {
for (let i = this.messages.length - 1; i >= 0; i--) {
if (this.messages[i].from === 'ai') { aiIndex = i; break; }
}
}
if (aiIndex === null || aiIndex < 0 || !this.messages[aiIndex]) {
this.messages.push({
from: 'ai',
text: '',
timestamp: new Date().toLocaleTimeString()
} as any);
aiIndex = this.messages.length - 1;
this.isVideoEnabledIndex.push(false);
}
const aiMsg = this.messages[aiIndex] as any;
if (this.aiResponseInterval) {
clearInterval(this.aiResponseInterval);
this.aiResponseInterval = null;
}
aiMsg.text = '';
aiMsg.pending = true;
this.isAiResponding = true;
this.cdr.detectChanges();
const words = responseText.split(/\s+/).filter(w => w.length);
let idx = 0;
// 1 word every 200 ms – you can tune this
const speedMs = 200;
this.aiResponseInterval = setInterval(() => {
if (idx < words.length) {
aiMsg.text = words.slice(0, idx + 1).join(' ');
idx++;
this.cdr.detectChanges();
this.scrollToBottom(); // keep view at bottom while streaming
} else {
clearInterval(this.aiResponseInterval!);
this.aiResponseInterval = null;
aiMsg.text = responseText;
aiMsg.pending = false;
this.isAiResponding = false;
if (onDone) {
onDone();
}
this.cdr.detectChanges();
this.scrollToBottom();
}
}, speedMs);
}
stopAiResponse(): void {
if (this.currentExplainSub) { this.currentExplainSub.unsubscribe(); this.currentExplainSub = null; }
if (this.currentFollowupsSub) { this.currentFollowupsSub.unsubscribe(); this.currentFollowupsSub = null; }
if (this.aiResponseInterval) { clearInterval(this.aiResponseInterval); this.aiResponseInterval = null; }
this.stopServerAudio();
try { window.speechSynthesis?.cancel(); } catch { /* noop */ }
if (this.pendingAiIndex !== null && this.messages[this.pendingAiIndex] && this.messages[this.pendingAiIndex].from === 'ai') {
const msg = this.messages[this.pendingAiIndex];
msg.text = 'Response cancelled.';
msg.timestamp = new Date().toLocaleTimeString();
msg.suggestions = [];
msg.audioUrl = '';
msg.videoUrl = '';
msg.playingVideoUrl = '';
msg.pending = false;
if (this.isVideoEnabledIndex.length > this.pendingAiIndex) this.isVideoEnabledIndex[this.pendingAiIndex] = false;
this.pendingAiIndex = null;
} else {
const revIndex = [...this.messages].reverse().findIndex(m => m.from === 'ai');
if (revIndex !== -1) {
const actualIndex = this.messages.length - 1 - revIndex;
const msg = this.messages[actualIndex];
msg.text = 'Response cancelled.';
msg.timestamp = new Date().toLocaleTimeString();
msg.suggestions = [];
msg.audioUrl = '';
msg.videoUrl = '';
msg.playingVideoUrl = '';
msg.pending = false;
if (this.isVideoEnabledIndex.length > actualIndex) this.isVideoEnabledIndex[actualIndex] = false;
} else {
this.messages.push({ from: 'ai', text: 'Response cancelled.', timestamp: new Date().toLocaleTimeString() });
this.isVideoEnabledIndex.push(false);
}
}
this.isAiResponding = false;
this.isTyping = false;
this.isSpeaking = false;
this.isReadingIndex = null;
this.cdr.detectChanges();
}
speakResponse(responseText: string): void {
if (!responseText) return;
// Only speak when the Voice toggle is ON
if (!this.isVoiceEnabled) return;
this.stopAllVideo();
const speech = new SpeechSynthesisUtterance();
speech.text = responseText;
speech.lang = 'en-US';
speech.pitch = 1;
speech.rate = 1;
this.isSpeaking = true;
const voices = window.speechSynthesis.getVoices();
const preferred = [
'Google UK English Female',
'Google US English Female',
'Microsoft Zira - English (United States)',
'Microsoft Hazel - English (United Kingdom)',
'Google en-GB Female',
'Google en-US Female'
];
for (const n of preferred) {
const found = voices.find(v => v.name === n);
if (found) { speech.voice = found; break; }
}
if (!speech.voice && voices.length) speech.voice = voices[0];
speech.onend = () => {
this.isSpeaking = false;
this.cdr.detectChanges();
};
try {
window.speechSynthesis.speak(speech);
} catch {
this.isSpeaking = false;
}
}
resumeAudio(): void {
if (this.serverAudio && this.serverAudio.paused) {
this.serverAudio.play();
this.isAudioPaused = false;
if (this.serverAudioMessageIndex !== null) this.messages[this.serverAudioMessageIndex].isPlaying = true;
this.cdr.detectChanges();
return;
}
if (window.speechSynthesis && window.speechSynthesis.paused) {
window.speechSynthesis.resume();
this.isAudioPaused = false;
this.cdr.detectChanges();
}
}
playServerAudioForMessage(index: number): void {
const msg = this.messages[index] as any;
if (!msg || !msg.audioUrl) return;
// If same message is clicked while its audio element exists: toggle play/pause
if (this.serverAudio && this.serverAudioMessageIndex === index) {
if (!this.serverAudio.paused) {
this.serverAudio.pause();
this.isAudioPaused = true;
msg.isPlaying = false;
} else {
this.serverAudio.play().catch(err => {
console.error('Audio resume failed:', err);
});
this.isAudioPaused = false;
msg.isPlaying = true;
}
this.cdr.detectChanges();
return;
}
// Stop any inline videos (pauses DOM video elements) before starting audio
this.stopAllVideo();
// Stop any existing audio and speech synthesis
this.stopServerAudio();
try { window.speechSynthesis?.cancel(); } catch { /* noop */ }
// NOTE: Do NOT change global navbar toggles here.
// Previously this method forced `isMuted = false` and `isVoiceEnabled = true`
// which caused the navbar audio icon to flip when playing per-message audio.
// We intentionally avoid modifying those global flags so per-message playback
// doesn't affect the navbar state.
// Create and play new audio
this.serverAudio = new Audio(msg.audioUrl);
this.serverAudioMessageIndex = index;
try { this.serverAudio.volume = this.isMuted ? 0 : 1; } catch { /* noop */ }
this.isReadingIndex = index;
this.isAudioPaused = false;
msg.isPlaying = true;
this.isSpeaking = true;
this.cdr.detectChanges();
this.serverAudio.onended = () => {
try { msg.isPlaying = false; } catch { /* noop */ }
this.isReadingIndex = null;
this.serverAudio = null;
this.serverAudioMessageIndex = null;
this.isSpeaking = false;
this.cdr.detectChanges();
};
this.serverAudio.onerror = (e) => {
console.error('Server audio playback error', e);
try { msg.isPlaying = false; } catch { /* noop */ }
this.isReadingIndex = null;
this.serverAudio = null;
this.serverAudioMessageIndex = null;
this.isSpeaking = false;
this.cdr.detectChanges();
};
this.serverAudio.play().catch(err => {
console.error('Audio play failed:', err);
try { msg.isPlaying = false; } catch { /* noop */ }
this.isReadingIndex = null;
this.serverAudio = null;
this.serverAudioMessageIndex = null;
this.isSpeaking = false;
this.cdr.detectChanges();
});
}
private stopServerAudio(): void {
if (this.serverAudio) {
try { this.serverAudio.pause(); this.serverAudio.currentTime = 0; } catch { }
this.serverAudio = null;
if (this.serverAudioMessageIndex !== null && this.messages[this.serverAudioMessageIndex]) {
this.messages[this.serverAudioMessageIndex].isPlaying = false;
}
this.serverAudioMessageIndex = null;
}
this.isReadingIndex = null;
}
synthesizeAudioAndPlay(index: number): void {
const msg = this.messages[index] as any;
if (!msg || !msg.text) return;
if (msg.audioUrl) { this.playServerAudioForMessage(index); return; }
if (msg.isSynthesizing) return;
msg.isSynthesizing = true; this.cdr.detectChanges();
this.apiService.synthesizeAudio(msg.text).subscribe({
next: (res: any) => {
msg.isSynthesizing = false;
if (res?.audio_url) { msg.audioUrl = res.audio_url; this.playServerAudioForMessage(index); }
else { this.errorMessage = 'Audio generation failed.'; }
this.cdr.detectChanges();
},
error: (err) => { msg.isSynthesizing = false; this.errorMessage = 'Audio generation failed.'; this.cdr.detectChanges(); }
});
}
synthesizeVideoAndPlay(index: number): void {
const msg = this.messages[index] as any;
if (!msg || !msg.text) return;
if (msg.videoUrl) {
this.openMessageVideo(index);
return;
}
if (msg.isVideoSynthesizing) return;
msg.isVideoSynthesizing = true;
this.cdr.detectChanges();
this.apiService.synthesizeVideo(msg.text).subscribe({
next: (res: any) => {
msg.isVideoSynthesizing = false;
if (res?.video_url) {
msg.videoUrl = res.video_url;
this.openMessageVideo(index);
} else {
console.error('No video_url returned from synth API:', res);
this.errorMessage = 'Video generation failed.';
}
this.cdr.detectChanges();
},
error: (err) => {
msg.isVideoSynthesizing = false;
console.error('Video synth API error', err);
this.errorMessage = 'Video generation failed. Try again.';
this.cdr.detectChanges();
}
});
}
openMessageVideo(i: number, autoPlay: boolean = false): void {
const msg = this.messages[i] as any;
if (!msg?.videoUrl) return;
if (this.isVideoPlayingIndex === i) {
this.stopInlineVideo(i);
return;
}
// Stop any server audio and spoken TTS before playing video
this.stopServerAudio();
try { window.speechSynthesis?.cancel(); } catch { /* noop */ }
// Stop other videos (will also pause DOM video elements)
this.stopAllVideo();
msg.playingVideoUrl = msg.videoUrl;
this.isVideoEnabledIndex[i] = true;
this.cdr.detectChanges();
setTimeout(() => {
const vid = document.getElementById(`inline-video-${i}`) as HTMLVideoElement | null;
if (!vid) { this.isVideoPlayingIndex = null; this.cdr.detectChanges(); return; }
// If autoplay requested, mute to satisfy browser autoplay policies.
if (autoPlay) {
try { vid.muted = true; } catch { /* noop */ }
} else {
try { vid.muted = false; } catch { /* noop */ }
}
vid.onplay = () => { this.isVideoPlayingIndex = i; this.cdr.detectChanges(); };
vid.onpause = () => { this.cdr.detectChanges(); };
vid.onended = () => { this.onMessageVideoEnded(i); };
// Attempt to play; if blocked, clear playing state
vid.play().catch(err => {
console.warn('Inline video play blocked/failed:', err);
this.isVideoPlayingIndex = null;
msg.playingVideoUrl = '';
this.isVideoEnabledIndex[i] = false;
this.cdr.detectChanges();
});
}, 50);
}
// New toggle helper called from the template button. It uses existing open/stop helpers.
toggleMessageVideo(i: number): void {
const msg = this.messages[i] as any;
if (!msg || !msg.videoUrl) return;
if (!this.isVideoEnabledIndex[i]) {
this.openMessageVideo(i);
} else {
this.stopInlineVideo(i);
}
}
stopInlineVideo(index: number): void {
const vid = document.getElementById(`inline-video-${index}`) as HTMLVideoElement | null;
if (vid) {
try { vid.pause(); } catch { /* noop */ }
try { vid.currentTime = 0; } catch { /* noop */ }
}
const msg = this.messages[index] as any;
if (msg) msg.playingVideoUrl = '';
this.isVideoEnabledIndex[index] = false;
if (this.isVideoPlayingIndex === index) this.isVideoPlayingIndex = null;
this.cdr.detectChanges();
}
onMessageVideoEnded(i: number): void {
const msg = this.messages[i] as any;
if (msg) msg.playingVideoUrl = '';
this.isVideoEnabledIndex[i] = false;
if (this.isVideoPlayingIndex === i) this.isVideoPlayingIndex = null;
this.cdr.detectChanges();
}
loadVoices(): void {
const voices = window.speechSynthesis.getVoices();
if (!voices.length) {
setTimeout(() => this.loadVoices(), 500);
return;
}
const preferred = [
"Google UK English Female",
"Google US English Female",
"Microsoft Zira - English (United States)",
"Microsoft Hazel - English (United Kingdom)",
"Google en-GB Female",
"Google en-US Female"
];
for (let name of preferred) {
const v = voices.find(voice => voice.name === name);
if (v) { this.selectedVoice = v; break; }
}
if (!this.selectedVoice) {
this.selectedVoice = voices.find(voice => voice.name.toLowerCase().includes("female")) || voices[0];
}
}
/** Global audio controls */
pauseAudio(): void {
if (window.speechSynthesis.speaking && !window.speechSynthesis.paused) {
window.speechSynthesis.pause();
this.isAudioPaused = true;
this.cdr.detectChanges();
}
}
stopListening(): void {
this.isListening = false;
if (this.recognition) this.recognition.stop();
}
/** Per-message read toggle */
toggleAudio(message: { text: string, isPlaying?: boolean }): void {
if (this.speechSynthesisInstance && this.speechSynthesisInstance.text === message.text) {
if (message.isPlaying) {
window.speechSynthesis.pause();
message.isPlaying = false;
} else {
window.speechSynthesis.resume();
message.isPlaying = true;
}
} else {
if (this.speechSynthesisInstance) window.speechSynthesis.cancel();
this.messages.forEach((m) => (m.isPlaying = false));
message.isPlaying = true;
this.speechSynthesisInstance = new SpeechSynthesisUtterance(message.text);
this.speechSynthesisInstance.lang = 'en-US';
this.speechSynthesisInstance.pitch = 1;
this.speechSynthesisInstance.rate = 1;
this.speechSynthesisInstance.onend = () => {
message.isPlaying = false;
this.speechSynthesisInstance = null;
};
window.speechSynthesis.speak(this.speechSynthesisInstance);
}
}
// UI / input helpers
goToHome(): void { this.router.navigate(['/home']); }
copySuccessIndex: number | null = null;
copyToClipboard(text: string, index: number): void {
navigator.clipboard.writeText(text).then(() => {
this.copySuccessIndex = index;
setTimeout(() => { this.copySuccessIndex = null; }, 2000);
}).catch(err => console.error('Failed to copy:', err));
}
checkInput = (): void => { this.isInputValid = this.userInput.trim().length > 0; }
handleButtonClick(): void {
// If AI is currently streaming/responding, clicking the button should cancel that response.
if (this.isAiResponding) {
this.stopAiResponse();
return;
}
if (this.userInput.trim().length > 0) {
this.showQuestions = false;
const msg = this.userInput;
this.userInput = '';
this.sendMessage(msg);
} else if (this.isSpeaking && !this.serverAudio) {
this.pauseAudio();
} else if (this.isAudioPaused) {
this.resumeAudio();
} else {
this.openMicrophonePopup();
}
}
startListening(): void {
this.isListening = true;
this.isProcessingSpeech = false;
if (navigator.mediaDevices?.getUserMedia) {
navigator.mediaDevices.getUserMedia({ audio: true })
.then(() => {
if (this.recognition) {
this.recognition.start();
this.recognition.onresult = (event: any) => {
if (event.results && event.results[0]) {
const transcript = event.results[0][0].transcript.trim();
this.userInput = transcript;
if (this.userInput.trim()) this.sendMessage();
this.recognition.stop();
this.isListening = false;
}
};
this.recognition.onnomatch = () => alert('No speech detected. Please try again.');
this.recognition.onend = () => { this.isListening = false; };
this.recognition.onerror = (error: any) => {
console.error('Speech Recognition Error:', error);
this.isListening = false;
if (error.error === 'not-allowed') alert('Microphone permission denied.');
};
} else {
alert('Speech Recognition not supported in this browser.');
}
}).catch((error) => {
console.error('Microphone access denied:', error);
this.errorMessage = 'Please enable microphone access to use this feature.';
this.isListening = true;
});
} else {
alert('Microphone access not supported in this browser.');
}
}
private stopPopupListening(): void {
try {
if (this.recognition && typeof this.recognition.stop === 'function') {
this.recognition.stop();
}
} catch (e) {
console.warn('Error stopping popup recognition:', e);
}
this._restoreRecognitionHandlers();
this.isListening = false;
this.cdr.detectChanges();
}
private startPopupListening(): void {
this.isListening = true;
this.isProcessingSpeech = false;
this.popupTranscript = '';
this.errorMessage = '';
if (!this.recognition) {
this.errorMessage = 'Speech Recognition not available in this browser.';
this.isListening = false;
this.cdr.detectChanges();
return;
}
this._savedRecognitionOnResult = this.recognition.onresult;
this._savedInterimResults = Boolean(this.recognition.interimResults);
this.recognition.interimResults = true;
this.popupListeningMode = true;
if (navigator.mediaDevices?.getUserMedia) {
navigator.mediaDevices.getUserMedia({ audio: true })
.then(() => {
try {
this.recognition.onresult = (event: any) => {
let interim = '';
let final = '';
for (let i = event.resultIndex; i < event.results.length; i++) {
const res = event.results[i];
if (res.isFinal) final += res[0].transcript + ' ';
else interim += res[0].transcript + ' ';
}
const display = (final + interim).trim();
this.popupTranscript = display;
this.cdr.detectChanges();
if (final && final.trim()) {
try { this.recognition.stop(); } catch { /* noop */ }
this.isListening = false;
this.cdr.detectChanges();
}
};
this.recognition.onnomatch = () => {
this.errorMessage = 'No speech detected. Please try again.';
this.isListening = false;
this.cdr.detectChanges();
};
this.recognition.onend = () => {
this.isListening = false;
this.popupListeningMode = false;
this._restoreRecognitionHandlers();
this.cdr.detectChanges();
};
this.recognition.onerror = (error: any) => {
console.error('Popup Speech Recognition Error:', error);
this.errorMessage = 'Speech recognition error.';
this.isListening = false;
this.popupListeningMode = false;
this._restoreRecognitionHandlers();
this.cdr.detectChanges();
};
this.recognition.start();
} catch (err) {
console.error('startPopupListening start error:', err);
this.errorMessage = 'Unable to start speech recognition.';
this.isListening = false;
this.popupListeningMode = false;
this._restoreRecognitionHandlers();
this.cdr.detectChanges();
}
}).catch((error) => {
console.error('Microphone access denied for popup:', error);
this.errorMessage = 'Please enable microphone access to use this feature.';
this.isListening = false;
this.popupListeningMode = false;
this._restoreRecognitionHandlers();
this.cdr.detectChanges();
});
} else {
this.errorMessage = 'Microphone access not supported in this browser.';
this.isListening = false;
this.popupListeningMode = false;
this._restoreRecognitionHandlers();
this.cdr.detectChanges();
}
}
private _restoreRecognitionHandlers(): void {
try {
if (!this.recognition) return;
if (this._savedRecognitionOnResult) {
this.recognition.onresult = this._savedRecognitionOnResult;
this._savedRecognitionOnResult = null;
}
this.recognition.interimResults = Boolean(this._savedInterimResults);
this._savedInterimResults = false;
this.popupListeningMode = false;
} catch (err) {
console.warn('Error restoring recognition handlers:', err);
}
}
addNewLine(event: KeyboardEvent): void {
if (event.key === 'Enter' && event.shiftKey) {
event.preventDefault();
this.userInput += '\n';
}
}
adjustTextareaHeight(event: Event): void {
const textarea = event.target as HTMLTextAreaElement;
textarea.style.height = 'auto';
textarea.style.height = `${textarea.scrollHeight}px`;
}
handleEnterPress(event: KeyboardEvent): void {
if (this.isSpeaking && !this.serverAudio) { event.preventDefault(); return; }
if (event.key === 'Enter') {
if (!event.shiftKey) {
event.preventDefault();
this.handleButtonClick();
} else {
event.preventDefault();
this.userInput += '\n';
}
}
}
isVoiceEnabled = false;
isTutorEnabled = false;
isSyllabusEnabled = true;
isBreadcrumbEnabled = false;
toggleVoice(): void {
// Toggle voice state and mute flag independently of tutor/video.
this.isVoiceEnabled = !this.isVoiceEnabled;
//this.isMuted = !this.isVoiceEnabled;
//// When turning voice OFF, stop voice TTS only — do not stop videos.
//if (!this.isVoiceEnabled) {
// this.stopVoiceOnly();
//}
//if (this.serverAudio) {
// try { this.serverAudio.volume = this.isMuted ? 0 : 1; } catch { /* noop */ }
// }
//this.saveToggleStates();
}
toggleTutor(): void {
// Toggle the global tutor/video mode and perform the appropriate actions.
this.isTutorEnabled = !this.isTutorEnabled;
}
toggleSyllabus(): void { this.isSyllabusEnabled = !this.isSyllabusEnabled; this.saveToggleStates(); }
toggleBreadcrumb(): void { this.isBreadcrumbEnabled = !this.isBreadcrumbEnabled; this.saveToggleStates(); }
private playGlobalVideoFromLatest(): void {
const idx = [...this.messages].reverse().findIndex(m => m.from === 'ai' && m.videoUrl);
if (idx === -1) { this.videoUrl = ''; return; }
const actualIndex = this.messages.length - 1 - idx;
const msg = this.messages[actualIndex];
this.videoUrl = msg.videoUrl || '';
this.cdr.detectChanges();
}
private saveToggleStates(): void {
if (isPlatformBrowser(this.platformId)) {
localStorage.setItem('voiceEnabled', String(this.isVoiceEnabled));
localStorage.setItem('tutorEnabled', String(this.isTutorEnabled));
localStorage.setItem('syllabusEnabled', String(this.isSyllabusEnabled));
localStorage.setItem('breadcrumbEnabled', String(this.isBreadcrumbEnabled));
}
}
private loadToggleStates(): void {
if (isPlatformBrowser(this.platformId)) {
this.isVoiceEnabled = localStorage.getItem('voiceEnabled') === 'true';
this.isTutorEnabled = localStorage.getItem('tutorEnabled') === 'true';
this.isSyllabusEnabled = localStorage.getItem('syllabusEnabled') !== 'false';
this.isBreadcrumbEnabled = localStorage.getItem('breadcrumbEnabled') === 'true';
}
}
private stopVoiceOnly(): void {
try { if (window.speechSynthesis.speaking || window.speechSynthesis.paused) window.speechSynthesis.cancel(); } catch { }
this.speechSynthesisInstance = null;
this.isSpeaking = false;
}
private stopAllAudioAndMute(): void {
this.stopServerAudio();
try { if (window.speechSynthesis.speaking || window.speechSynthesis.paused) window.speechSynthesis.cancel(); } catch { }
this.isVoiceEnabled = false;
this.isMuted = true;
this.messages.forEach(m => { m.isPlaying = false; });
this.isReadingIndex = null;
this.isSpeaking = false;
this.isAudioPaused = false;
this.cdr.detectChanges();
}
clearVideoUrl(): void {
this.videoUrl = '';
if (this.isTutorEnabled) {
this.isTutorEnabled = false;
this.saveToggleStates();
}
this.cdr.detectChanges();
}
private stopAllVideo(): void {
this.videoUrl = '';
// Pause any inline video elements in the DOM (prevents a video from continuing to play)
try {
const vids = Array.from(document.querySelectorAll<HTMLVideoElement>('[id^="inline-video-"]'));
vids.forEach(v => {
try { v.pause(); v.currentTime = 0; } catch { /* noop */ }
});
} catch (err) {
/* noop */
}
this.messages.forEach((m, idx) => {
m.playingVideoUrl = '';
this.isVideoEnabledIndex[idx] = false;
});
this.cdr.detectChanges();
}
getButtonIcon(): string {
if (this.isAiResponding) return 'assets/images/chat/stop.png';
if (this.serverAudio && !this.serverAudio.paused) return 'assets/images/chat/microphone-icon.png';
if (this.userInput.trim().length > 0) return 'assets/images/chat/send-icon.png';
if (this.isSpeaking && !this.serverAudio) return 'assets/images/chat/pause-icon.png';
if (this.isAudioPaused) return 'assets/images/chat/resume-icon.png';
return 'assets/images/chat/microphone-icon.png';
}
goToHomePageShortcut(): void { this.router.navigate(['/home']); }
openUserGuide(): void { this.showUserGuide = true; }
closeUserGuide(): void { this.showUserGuide = false; }
stopSpeaking(): void {
try {
if (window.speechSynthesis?.speaking || window.speechSynthesis?.paused) {
window.speechSynthesis.cancel();
}
} catch { /* noop */ }
this.speechSynthesisInstance = null;
try { this.stopServerAudio(); } catch { /* noop */ }
this.isSpeaking = false;
this.isAudioPaused = false;
if (this.aiResponseInterval) { clearInterval(this.aiResponseInterval); this.aiResponseInterval = null; }
this.isAiResponding = false;
this.cdr.detectChanges();
}
muteMicrophone(): void {
try {
if (this.recognition && typeof this.recognition.stop === 'function') {
this.recognition.stop();
}
} catch (e) {
console.warn('Error stopping recognition:', e);
}
this.isListening = false;
this.isProcessingSpeech = false;
this.errorMessage = '';
this.cdr.detectChanges();
}
openMicrophoneSettings(): void {
const ua = navigator.userAgent || '';
try {
if (ua.includes('Edg') || ua.includes('Edge')) {
window.open('edge://settings/content/microphone', '_blank');
} else if (ua.includes('Chrome') && !ua.includes('Chromium')) {
window.open('chrome://settings/content/microphone', '_blank');
} else if (ua.includes('Firefox')) {
window.open('about:preferences#privacy', '_blank');
} else if (/Safari/.test(ua) && /Macintosh/.test(navigator.platform)) {
alert('Open Safari → Settings (or Preferences) → Websites → Microphone to enable access.');
} else {
alert("Please check your browser's settings to enable the microphone.");
}
} catch (err) {
console.error('openMicrophoneSettings error', err);
alert("Unable to open settings automatically. Please check your browser's microphone/privacy settings.");
}
this.cdr.detectChanges();
}
private _isShortFollowup(text: string | undefined): boolean {
if (!text) return false;
const t = text.toLowerCase().replace(/[^\w\s?]/g, "").trim();
if (!t) return false;
if (/\b(example|examples|more examples|another example|give example|more)\b/.test(t)) {
return t.split(/\s+/).length <= 8;
}
return ["more", "more?", "another?", "another example?"].includes(t);
}
// Add this method inside the ChatComponent class (near other audio helpers)
stopReadAloud(): void {
// Stop any server audio and reset UI playback state
try { this.stopServerAudio(); } catch { /* noop */ }
this.isReadingIndex = null;
this.isSpeaking = false;
this.cdr.detectChanges();
}
openMicrophonePopup(): void {
this._recordingFinalBuffer = '';
this._recordingInterimBuffer = '';
this.popupTranscript = '';
this.errorMessage = '';
this.showMicPopup = true;
setTimeout(() => this.startRecording(), 200);
}
closeMicrophonePopup(): void {
this.stopRecording();
this.showMicPopup = false;
this.popupTranscript = '';
this._recordingFinalBuffer = '';
this._recordingInterimBuffer = '';
this.errorMessage = '';
this.cdr.detectChanges();
}
async startRecording(): Promise<void> {
if (!this.recognition) { this.errorMessage = 'Speech recognition not supported.'; return; }
this._recordingFinalBuffer = '';
this._recordingInterimBuffer = '';
this.popupTranscript = '';
this.errorMessage = '';
this.isRecording = true;
try { this.recognition.interimResults = true; } catch { }
try { this.recognition.start(); } catch (e) {
console.warn('Could not start recognition', e);
if (!this._recognitionActive) {
setTimeout(() => { try { this.recognition.start(); } catch { } }, 300);
}
}
try { await this.startAnalyzer(); } catch (err) { console.warn('Analyzer failed to start', err); }
this.cdr.detectChanges();
}
async stopRecording(): Promise<void> {
if (this._restartTimer) { clearTimeout(this._restartTimer); this._restartTimer = null; }
if (this.recognition && this.isRecording) { try { this.recognition.stop(); } catch { } }
try { this.stopAnalyzer(); } catch { /* noop */ }
this.isRecording = false;
const finalText = (this._recordingFinalBuffer || '').trim();
const interimText = (this._recordingInterimBuffer || '').trim();
const combinedRaw = (finalText + ' ' + interimText).trim();
if (!combinedRaw) {
this.popupTranscript = '';
this.cdr.detectChanges();
return;
}
this.popupTranscript = 'Processing…';
this.cdr.detectChanges();
let punctuated = combinedRaw;
try {
console.log('[VOICE] raw transcript:', combinedRaw);
// call backend at /rag/punctuate
punctuated = await this.punctuateText(combinedRaw);
console.log('[VOICE] punctuated result:', punctuated);
} catch (err) {
console.warn('Punctuation API failed', err);
punctuated = combinedRaw;
}
let normalized = this.normalizeTranscript(punctuated);
const hasTerminalPunctuation = /[.?!]$/.test(normalized);
const questionPattern = /^(who|what|when|where|why|how|which|whom|whose|is|are|am|was|were|do|does|did|can|could|would|will|shall|should|have|has|had)\b/i;
if (!hasTerminalPunctuation && questionPattern.test(combinedRaw)) {
normalized = normalized + '?';
}
this.popupTranscript = normalized;
this.cdr.detectChanges();
}
private extractAssistantContent(raw: string): string {
if (!raw) return raw;
try {
// Look for ChatCompletionMessage(content='...') or content="..."
const re1 = /message=ChatCompletionMessage\(\s*content=(['"])((?:\\.|(?!\1).)*)\1/;
const m1 = raw.match(re1);
if (m1 && m1[2]) return m1[2].replace(/\\'/g, "'").replace(/\\"/g, '"').trim();
const re2 = /ChatCompletionMessage\(\s*content=(['"])((?:\\.|(?!\1).)*)\1/;
const m2 = raw.match(re2);
if (m2 && m2[2]) return m2[2].replace(/\\'/g, "'").replace(/\\"/g, '"').trim();
// Fallback: any content='...' anywhere
const re3 = /content=(['"])((?:\\.|(?!\1).)*)\1/;
const m3 = raw.match(re3);
if (m3 && m3[2]) return m3[2].replace(/\\'/g, "'").replace(/\\"/g, '"').trim();
} catch (e) {
console.warn('extractAssistantContent error', e);
}
return raw.trim();
}
private async punctuateText(raw: string): Promise<string> {
if (!raw) return raw;
try {
// Use ApiService so backend URL is centralized
const resp$ = this.apiService.punctuate(raw);
const res = await lastValueFrom(resp$);
if (res && typeof res.punctuated === 'string' && res.punctuated.trim().length) {
const punctuated = res.punctuated.trim();
const extracted = this.extractAssistantContent(punctuated);
return extracted || punctuated;
}
} catch (err) {
console.warn('punctuateText error', err);
}
return raw;
}
confirmAndSendTranscript(): void {
let text = (this.popupTranscript || '').trim();
if (!text) { this.errorMessage = 'No speech captured'; return; }
text = this.normalizeTranscript(text);
// still emit in case other components listen
this.transcriptConfirmed.emit(text);
// Send the extracted/normalized text as a user message to the chat
try {
this.sendMessage(text);
} catch (e) {
console.warn('Failed to send transcript as message', e);
}
// reset popup state
this._recordingFinalBuffer = '';
this._recordingInterimBuffer = '';
this.showMicPopup = false;
this.isRecording = false;
if (this._restartTimer) { clearTimeout(this._restartTimer); this._restartTimer = null; }
this.popupTranscript = '';
this.cdr.detectChanges();
}
private normalizeTranscript(text: string): string {
if (!text) return text;
let t = text.trim();
const mappings: Array<[RegExp, string]> = [
[/\b(full stop|period|dot)\b/gi, '.'],
[/\b(question mark|question)\b/gi, '?'],
[/\b(exclamation mark|exclamation|exclaim)\b/gi, '!'],
[/\b(comma)\b/gi, ','],
[/\b(colon)\b/gi, ':'],
[/\b(semicolon)\b/gi, ';'],
[/\b(ellipsis|dot dot dot|three dots)\b/gi, '...'],
[/\b(new line|newline|new paragraph|line break)\b/gi, '\n'],
[/\b(open parenthesis|open bracket)\b/gi, '('],
[/\b(close parenthesis|close bracket)\b/gi, ')'],
[/\b(double quote|quote|quotation)\b/gi, '"'],
[/\b(single quote|apostrophe)\b/gi, "'"],
[/\b(dash|hyphen)\b/gi, '-'],
[/\b(percent|percent sign)\b/gi, '%'],
[/\b(and sign|ampersand)\b/gi, '&'],
[/\b(at sign)\b/gi, '@'],
[/\b(forward slash|slash)\b/gi, '/'],
[/\b(backslash)\b/gi, '\\\\']
];
for (const [re, rep] of mappings) t = t.replace(re, rep);
t = t.replace(/\s+([,.:;?!%'\)\]\}])/g, '$1');
t = t.replace(/\s+([\(\[\{"'`])/g, '$1');
t = t.replace(/([.?!:;,%\)\]'"-]{1,3})(?!\s|\n|$)/g, '$1 ');
t = t.replace(/[ \t]{2,}/g, ' ');
t = t.split('\n').map(line => line.trim()).join('\n');
t = t.replace(/(^|[\n\.!\?]\s+)([a-z])/g, (m, p1, p2) => p1 + p2.toUpperCase());
return t.trim();
}
// WebAudio analyzer (unchanged) — methods startAnalyzer, stopAnalyzer, drawWaveform, ngOnDestroy...
private async startAnalyzer(): Promise<void> {
if (!isPlatformBrowser(this.platformId)) return;
// Ensure canvas is available (rare race when popup just opened)
if (!this.waveformCanvas || !this.waveformCanvas.nativeElement) {
await new Promise(r => setTimeout(r, 80));
if (!this.waveformCanvas || !this.waveformCanvas.nativeElement) {
console.warn('Waveform canvas not available');
return;
}
}
try {
// Ensure we have a running AudioContext
if (!this.audioContext || (this.audioContext && this.audioContext.state === 'closed')) {
this.audioContext = new (window.AudioContext || (window as any).webkitAudioContext)();
} else if (this.audioContext.state === 'suspended') {
// resume if previously suspended
try { await this.audioContext.resume(); } catch { /* noop */ }
}
// Always request a fresh MediaStream for analyzer — it's cheap after permission is granted
if (this.mediaStream) {
try { this.mediaStream.getTracks().forEach(t => t.stop()); } catch { /* noop */ }
this.mediaStream = null;
}
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
this.mediaStream = stream;
// Create source and analyser
const source = this.audioContext.createMediaStreamSource(stream);
// Safely disconnect previous analyser if any
try { if (this.analyser) { try { this.analyser.disconnect(); } catch { } } } catch { /* noop */ }
const analyser = this.audioContext.createAnalyser();
analyser.fftSize = 2048;
analyser.smoothingTimeConstant = 0.85;
source.connect(analyser);
// assign to instance field
this.analyser = analyser;
const bufferLength = analyser.fftSize;
this.dataArray = new Uint8Array(bufferLength);
// Start drawing loop (will cancel any previous RAF)
this.drawWaveform();
} catch (err) {
console.warn('startAnalyzer error', err);
// clean partial state on failure
try { if (this.mediaStream) { this.mediaStream.getTracks().forEach(t => t.stop()); this.mediaStream = null; } } catch { /* noop */ }
try { if (this.analyser) { this.analyser.disconnect(); this.analyser = null; } } catch { /* noop */ }
this.dataArray = null;
// do not swallow error — caller can decide how to handle
throw err;
}
}
private stopAnalyzer(): void {
try {
if (this.animationFrameId) { cancelAnimationFrame(this.animationFrameId); this.animationFrameId = null; }
if (this.analyser) {
try { this.analyser.disconnect(); } catch { /* noop */ }
this.analyser = null;
}
if (this.mediaStream) {
try { this.mediaStream.getTracks().forEach(t => t.stop()); } catch { /* noop */ }
this.mediaStream = null;
}
// Close the AudioContext to fully reset state (async). Do not await here to avoid blocking UI.
try {
if (this.audioContext && typeof this.audioContext.close === 'function') {
this.audioContext.close().catch(() => { /* noop */ }).finally(() => { this.audioContext = null; });
} else {
this.audioContext = null;
}
} catch { this.audioContext = null; }
this.dataArray = null;
// Clear canvas
if (this.waveformCanvas && this.waveformCanvas.nativeElement) {
const c = this.waveformCanvas.nativeElement;
const ctx = c.getContext('2d');
if (ctx) ctx.clearRect(0, 0, c.width, c.height);
}
} catch (err) {
console.warn('stopAnalyzer error', err);
}
}
private drawWaveform(): void {
if (!this.waveformCanvas || !this.waveformCanvas.nativeElement || !this.analyser || !this.dataArray) return;
const canvas = this.waveformCanvas.nativeElement;
const ctx = canvas.getContext('2d');
if (!ctx) return;
const dpr = window.devicePixelRatio || 1;
const resize = () => {
const rect = canvas.getBoundingClientRect();
const w = Math.max(1, Math.floor(rect.width * dpr));
const h = Math.max(1, Math.floor(rect.height * dpr));
if (canvas.width !== w || canvas.height !== h) { canvas.width = w; canvas.height = h; }
};
const render = () => {
// If popup was closed or analyzer removed, stop rendering
if (!this.waveformCanvas || !this.waveformCanvas.nativeElement || !this.analyser || !this.dataArray) {
if (this.animationFrameId) { cancelAnimationFrame(this.animationFrameId); this.animationFrameId = null; }
return;
}
// If canvas is not visible (e.g. popup hidden), stop loop to avoid wasted CPU and potential silent failures
// offsetParent is null for display:none, also check bounding rect sanity
const rect = canvas.getBoundingClientRect();
if (rect.width === 0 || rect.height === 0 || !canvas.offsetParent) {
if (this.animationFrameId) { cancelAnimationFrame(this.animationFrameId); this.animationFrameId = null; }
return;
}
resize();
try {
this.analyser.getByteTimeDomainData(this.dataArray);
} catch (e) {
// analyser may have been disconnected / audioContext closed mid-frame
console.warn('analyser.getByteTimeDomainData failed', e);
if (this.animationFrameId) { cancelAnimationFrame(this.animationFrameId); this.animationFrameId = null; }
return;
}
let sum = 0;
for (let i = 0; i < this.dataArray.length; i++) {
const v = this.dataArray[i] - 128;
sum += v * v;
}
const rms = Math.sqrt(sum / this.dataArray.length) / 128;
const level = Math.min(1, Math.max(0, rms));
ctx.clearRect(0, 0, canvas.width, canvas.height);
const baselineY = canvas.height / 2;
// faint baseline
ctx.save();
ctx.globalAlpha = 0.25;
ctx.strokeStyle = '#666';
ctx.lineWidth = Math.max(1, 1 * dpr);
ctx.setLineDash([2 * dpr, 3 * dpr]);
ctx.beginPath();
ctx.moveTo(0, baselineY);
ctx.lineTo(canvas.width, baselineY);
ctx.stroke();
ctx.setLineDash([]);
ctx.restore();
// waveform
ctx.lineWidth = Math.max(1, 1 * dpr);
ctx.strokeStyle = 'rgba(37,168,90,0.95)';
ctx.beginPath();
const slice = canvas.width / this.dataArray.length;
let x = 0;
for (let i = 0; i < this.dataArray.length; i++) {
const v = this.dataArray[i] / 128.0;
const y = (v * canvas.height) / 2;
const drawY = baselineY - (y - canvas.height / 2) * 0.6;
if (i === 0) ctx.moveTo(x, drawY); else ctx.lineTo(x, drawY);
x += slice;
}
ctx.stroke();
// highlight center bar that responds to level
const highlightMaxW = canvas.width * 0.7;
const highlightW = Math.max(2 * dpr, highlightMaxW * (0.05 + level * 0.95));
const hh = Math.max(4 * dpr, 6 * dpr);
const hx = (canvas.width - highlightW) / 2;
const hy = baselineY - hh / 2;
ctx.save();
ctx.globalAlpha = 0.18 + level * 0.3;
ctx.fillStyle = '#25a85a';
ctx.fillRect(hx - 6 * dpr, hy - 6 * dpr, highlightW + 12 * dpr, hh + 12 * dpr);
ctx.restore();
ctx.fillStyle = '#25a85a';
ctx.globalAlpha = 1;
ctx.fillRect(hx, hy, highlightW, hh);
this.animationFrameId = requestAnimationFrame(render);
};
// Start the loop (ensure any previous RAF is cancelled first)
if (this.animationFrameId) cancelAnimationFrame(this.animationFrameId);
this.animationFrameId = requestAnimationFrame(render);
}
//ngOnDestroy(): void {
// if (this._restartTimer) { clearTimeout(this._restartTimer); this._restartTimer = null; }
// try { this.stopAnalyzer(); } catch { }
// if (this.recognition) {
// try { this.recognition.stop(); } catch { }
// }
//}
}
|