File size: 17,188 Bytes
e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 e4ab6d0 59fecf2 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 |
import { Component, Inject, OnDestroy, PLATFORM_ID, ChangeDetectorRef } from '@angular/core';
import { ApiService } from './api.service';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { Router, RouterModule } from '@angular/router';
import { isPlatformBrowser } from '@angular/common';
import { ViewChild, ElementRef } from '@angular/core';
import { Renderer2 } from '@angular/core';
import { Subscription } from 'rxjs';
@Component({
selector: 'app-chat',
standalone: true,
imports: [FormsModule, CommonModule, RouterModule],
templateUrl: './chat.component.html',
styleUrl: './chat.component.css'
})
export class ChatComponent implements OnDestroy {
showQuestions: boolean = false;
userInput: string = '';
messages: { from: string, text: string, timestamp: string; isPlaying?: boolean }[] = [];
isTyping: boolean = false;
@ViewChild('chatBox') chatBox!: ElementRef;
isLoadingSpeech: boolean = false;
selectedVoice: SpeechSynthesisVoice | null = null;
errorMessage: string = "";
recognition: any;
speechSynthesisInstance: SpeechSynthesisUtterance | null = null;
isListening: boolean = false;
isProcessingSpeech: boolean = false;
isSpeaking: boolean = false;
isAudioPaused: boolean = false;
isInputValid: boolean = false;
suggestions: string[] = [];
showMicPopup: boolean = false;
isSubmitting: boolean = false;
private responseSub?: Subscription;
private lastFullAiText: string = '';
ngAfterViewChecked() {
setTimeout(() => {
this.scrollToBottom();
}, 100);
}
private scrollToBottom(): void {
try {
this.chatBox.nativeElement.scrollTop = this.chatBox.nativeElement.scrollHeight;
} catch (err) { }
}
constructor(
private apiService: ApiService,
private cdr: ChangeDetectorRef,
@Inject(PLATFORM_ID,) private platformId: object,
private router: Router,
private renderer: Renderer2
) {
window.speechSynthesis.onvoiceschanged = () => {
console.log("Available Voices:", window.speechSynthesis.getVoices());
};
if (isPlatformBrowser(this.platformId)) {
const SpeechRecognition = (window as any).SpeechRecognition || (window as any).webkitSpeechRecognition;
if (SpeechRecognition) {
this.recognition = new SpeechRecognition();
this.recognition.continuous = false;
this.recognition.lang = 'en-US';
this.recognition.interimResults = false;
this.recognition.onresult = (event: any) => {
if (event.results && event.results[0]) {
const transcript = event.results[0][0].transcript.trim();
console.log('Recognized speech:', transcript);
this.userInput = transcript;
this.sendMessage();
this.recognition.stop();
this.isListening = false;
}
};
this.recognition.onerror = (event: any) => {
console.error('Speech Recognition Error:', event.error);
this.isProcessingSpeech = false;
};
} else {
console.warn('Speech Recognition is not supported in this browser.');
}
window.addEventListener('beforeunload', this.handleUnload);
}
}
private handleUnload = (): void => {
if (window.speechSynthesis) {
window.speechSynthesis.cancel();
}
};
ngOnDestroy(): void {
if (isPlatformBrowser(this.platformId)) {
if (window.speechSynthesis) {
window.speechSynthesis.cancel();
}
window.removeEventListener('beforeunload', this.handleUnload);
}
}
openMicrophonePopup(): void {
this.showMicPopup = true;
}
closeMicrophonePopup(): void {
this.showMicPopup = false;
}
showHardcodedQuestions(): void {
setTimeout(() => {
this.showQuestions = true;
}, 100);
}
hideHardcodedQuestions(): void {
setTimeout(() => {
this.showQuestions = false;
}, 200);
}
selectHardcodedQuestion(question: string): void {
this.userInput = question;
this.showQuestions = false;
setTimeout(() => {
this.sendMessage();
this.userInput = '';
}, 100);
}
getSuggestions(): void {
if (!this.userInput || this.userInput.trim().length < 1 || this.isSpeaking) {
this.suggestions = [];
return;
}
this.apiService.getGrammarSuggestions(this.userInput).subscribe(
(response) => {
console.log("API Response:", response);
if (response.suggestions) {
this.suggestions = response.suggestions
.filter((s: string) => s && s.trim().length > 0)
.map((s: string) => s.replace(/^\d+\.\s*/, ""));
} else {
this.suggestions = [];
}
},
(error) => {
console.error("Error fetching suggestions:", error);
this.suggestions = [];
}
);
}
selectSuggestion(suggestion: string): void {
this.userInput = suggestion;
this.suggestions = [];
this.sendMessage();
}
sendMessage(inputText?: string): void {
const message = inputText ? inputText.trim() : this.userInput.trim();
if (!message) {
return;
}
let sessionId = localStorage.getItem('session_id');
this.messages.push({ from: 'user', text: message, timestamp: new Date().toLocaleTimeString() });
this.userInput = '';
this.isTyping = true;
this.cdr.detectChanges();
this.scrollToBottom();
this.responseSub = this.apiService.askQuestion(message, sessionId).subscribe(
(response) => {
this.isTyping = false;
const explanation = (response?.response || 'No explanation available.').trim();
if (response.session_id && !sessionId) {
localStorage.setItem('session_id', response.session_id);
}
const lines: string[] = String(explanation).split('\n');
const formatted: string = lines.map((line: string) => line.trim()).join('\n');
this.messages.push({
from: 'ai',
text: formatted,
timestamp: new Date().toLocaleTimeString(),
});
this.cdr.detectChanges();
this.scrollToBottom();
this.lastFullAiText = formatted;
this.speakResponse(explanation);
},
(error) => {
this.isTyping = false;
const errorMessage = 'Error: Could not get a response from the server.';
console.error('API Error:', error);
this.messages.push({
from: 'ai',
text: errorMessage,
timestamp: new Date().toLocaleTimeString(),
});
this.cdr.detectChanges();
this.scrollToBottom();
this.lastFullAiText = errorMessage;
this.speakResponse(errorMessage);
}
);
}
formatStructuredResponse(text: string): string {
let formattedText = text
.replace(/\n/g, '<br>')
.replace(/(\d+)\.\s/g, '<b>$1.</b> ')
.replace(/\•\s/g, '✔️ ')
.replace(/\-\s/g, '🔹 ')
.replace(/(\*\*)(.*?)\1/g, '<b>$2</b>');
return formattedText;
}
speakResponse(responseText: string): void {
if (!responseText) {
console.warn('No response text provided for speech.');
return;
}
console.log('Initiating text-to-speech with response:', responseText);
let lastAiMessage = this.messages.slice().reverse().find((msg) => msg.from === 'ai');
if (!lastAiMessage) {
lastAiMessage = { from: 'ai', text: '', timestamp: new Date().toLocaleTimeString() };
this.messages.push(lastAiMessage);
} else {
lastAiMessage.text = '';
}
this.cdr.detectChanges();
const words = responseText.split(' ');
let currentWordIndex = 0;
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();
let femaleVoice = voices.find(voice => voice.name === "Microsoft Zira - English (United States)");
if (femaleVoice) {
speech.voice = femaleVoice;
console.log("Using voice:", femaleVoice.name);
} else {
console.warn("Microsoft Zira not found, using default.");
}
speech.onboundary = (event) => {
if (event.name === 'word' && currentWordIndex < words.length) {
lastAiMessage!.text = words.slice(0, currentWordIndex + 1).join(' ');
currentWordIndex++;
this.cdr.detectChanges();
}
};
speech.onend = () => {
console.log('Speech ended.');
this.isSpeaking = false;
lastAiMessage!.text = responseText;
this.cdr.detectChanges();
};
console.log('Starting speech synthesis...');
window.speechSynthesis.speak(speech);
}
ngOnInit(): void {
if (window.speechSynthesis.onvoiceschanged !== undefined) {
window.speechSynthesis.onvoiceschanged = () => {
this.loadVoices();
};
}
this.loadVoices();
}
loadVoices(): void {
const voices = window.speechSynthesis.getVoices();
if (!voices.length) {
console.warn("No voices available yet, retrying...");
setTimeout(() => this.loadVoices(), 500);
return;
}
console.log("Available Voices:", voices.map(v => v.name));
const preferredVoices = [
"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 voiceName of preferredVoices) {
const foundVoice = voices.find(voice => voice.name === voiceName);
if (foundVoice) {
this.selectedVoice = foundVoice;
break;
}
}
if (!this.selectedVoice) {
this.selectedVoice = voices.find(voice => voice.name.toLowerCase().includes("female")) || voices[0];
}
console.log("Selected AI Voice:", this.selectedVoice?.name);
}
pauseAudio(): void {
if (window.speechSynthesis.speaking && !window.speechSynthesis.paused) {
window.speechSynthesis.pause();
this.isAudioPaused = true;
console.log('AI Speech Paused');
this.cdr.detectChanges();
}
}
resumeAudio(): void {
if (window.speechSynthesis.paused) {
window.speechSynthesis.resume();
this.isAudioPaused = false;
console.log('AI Speech Resumed');
this.cdr.detectChanges();
}
}
muteMicrophone(): void {
console.log("Microphone muted");
}
startListening(): void {
this.isListening = true;
this.isProcessingSpeech = false;
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices
.getUserMedia({ audio: true })
.then(() => {
if (this.recognition) {
console.log('Starting speech recognition...');
this.recognition.start();
this.recognition.onaudiostart = () => console.log('Audio capturing started.');
this.recognition.onspeechstart = () => console.log('Speech has been detected.');
this.recognition.onspeechend = () => console.log('Speech ended, processing...');
this.recognition.onaudioend = () => console.log('Audio capturing ended.');
this.recognition.onresult = (event: any) => {
if (event.results && event.results[0]) {
const transcript = event.results[0][0].transcript.trim();
console.log('Recognized speech:', transcript);
this.userInput = transcript;
if (this.userInput.trim()) {
console.log('Sending question automatically:', this.userInput);
this.sendMessage();
}
this.recognition.stop();
this.isListening = false;
}
};
this.recognition.onnomatch = () =>
alert('No speech detected. Please try again.');
this.recognition.onend = () => {
console.log('Speech recognition service disconnected.');
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 if (error.error === 'no-speech') {
alert('No speech detected. Please try speaking clearly.');
}
};
} else {
alert('Speech Recognition is 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 is not supported in this browser.');
}
}
stopListening(): void {
this.isListening = false;
if (this.recognition) {
this.recognition.stop();
}
}
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((msg) => (msg.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);
}
}
goToHome() {
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() {
this.isInputValid = this.userInput.trim().length > 0;
}
handleButtonClick(): void {
if (this.userInput.trim().length > 0) {
this.showQuestions = false;
const messageToSend = this.userInput;
this.userInput = '';
this.sendMessage(messageToSend);
} else if (this.isSpeaking) {
this.pauseAudio();
} else if (this.isAudioPaused) {
this.resumeAudio();
} else {
this.startListening();
}
}
getButtonIcon(): string {
if (this.userInput.trim().length > 0) {
return 'assets/images/chat/send-icon.png';
} else if (this.isSpeaking) {
return 'assets/images/chat/pause-icon.png';
} else if (this.isAudioPaused) {
return 'assets/images/chat/resume-icon.png';
} else {
return 'assets/images/chat/microphone-icon.png';
}
}
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`;
}
getButtonIconClass(): string {
return this.userInput.trim().length > 0
? 'send-icon'
: this.isSpeaking
? 'pause-icon'
: this.isAudioPaused
? 'resume-icon'
: 'microphone-icon';
}
openMicrophoneSettings(): void {
const userAgent = navigator.userAgent;
if (userAgent.includes("Chrome")) {
window.open("chrome://settings/content/microphone", "_blank");
} else if (userAgent.includes("Firefox")) {
window.open("about:preferences#privacy", "_blank");
} else if (userAgent.includes("Edge")) {
window.open("edge://settings/content/microphone", "_blank");
} else {
alert("Please check your browser's settings to enable the microphone.");
}
}
stopSpeaking(): void {
try {
if (window.speechSynthesis.speaking || window.speechSynthesis.paused) {
window.speechSynthesis.cancel();
}
} catch { }
(this as any).speechSynthesisInstance = null;
if (this.responseSub && !this.responseSub.closed) {
this.responseSub.unsubscribe();
}
this.isSpeaking = false;
this.isAudioPaused = false;
this.isTyping = false;
}
handleEnterPress(event: KeyboardEvent): void {
if (this.isSpeaking) {
event.preventDefault();
return;
}
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault();
const text = (this.userInput || '').trim();
if (text) this.sendMessage();
}
}
}
|