fix
Browse filesFiles changed (2)
47 unmodified lines48495051515253545455565758595556575859606162636465666747 unmodified lines return this.http.post<any>(`${this.baseUrl}/generate-questions`, body, { headers }); }
// Accept either a string (legacy) or an object { question, synthesize_audio, synthesize_video } explainGrammar(question: string): Observable<any> { explainGrammar(payload: string | any): Observable<any> { const grade = this.getGrade(); const headers = this.makeHeaders(grade); const body = { question, model: 'gpt-4o-mini', db_level: this.toDbLevel(grade), }; return this.http.post<any>(`${this.baseUrl}/explain-grammar`, body, { headers });
// Normalize payload into an object const body = typeof payload === 'string' ? { question: payload } : { ...payload };
// Ensure backend gets db_level and model so it selects the right vectorstore/LLM if (!body.db_level) body.db_level = this.toDbLevel(grade); if (!body.model) body.model = 'gpt-4o-mini';
const url = `${this.baseUrl}/explain-grammar`; return this.http.post(url, body, { headers }); }
// ✅ Updated to carry source_ids so follow-ups stay in the same textbook pages67 unmodified lines6869707171727374757677777879804 unmodified lines858687888889909142 unmodified lines134135136137137138139140143 unmodified lines2842852862872872882892905 unmodified lines29629729829929930030130221 unmodified lines324325326327327328329330233 unmodified lines5645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036045996006016026056066076086096106116126136096146156166173 unmodified lines6216226236196246256266276286296306316326336346356366373 unmodified lines641642643644645646647648649633634650651652653654655656657658659660661662663664665666667668669670671672673674675676677678640679680681682683684685686687688689690691692693694695696178 unmodified lines875876877825878879880881149 unmodified lines103110321033981103410351036103717 unmodified lines105510561057105810591060106110621063106410651066106775 unmodified lines1143114411451086108711461147114811491150217 unmodified lines1368136913701311137113721373137438 unmodified lines1413141414151356141614171418141992 unmodified lines1512151315141455151515161517151842 unmodified lines15611562156315041564156515661567243 unmodified lines181118121813181418151816181767 unmodified lines isSynthesizing?: boolean; isVideoSynthesizing?: boolean; }> = []; isTyping: boolean = false; 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;4 unmodified lines suggestions: string[] = []; isInputValid = false; /** Popup */
/** Subscriptions */ private responseSub?: Subscription;42 unmodified lines
currentFollowups: string[] = [];
/*private shouldAutoScroll = true;*/ videoUrl = ''; aiResponseInterval: any = null;143 unmodified lines } }
ngOnInit(): void { this.ensureGradeLevel();5 unmodified lines this.loadVoices(); }
ngOnDestroy(): void { if (this.currentExplainSub) { this.currentExplainSub.unsubscribe(); this.currentExplainSub = null; }21 unmodified lines }); }
scrollToBottom(): void { if (this.shouldAutoScroll) {233 unmodified lines this.shouldAutoScroll = true; this.scrollToBottom();
this.responseSub = this.apiService.explainGrammar(message) .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) : [];
// 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;
// IMPORTANT: // Do NOT set this.lastAnswer here. // We will set it only after the streaming animation has finished. this.streamAiAnswer(explanation, sourceIds, hasContext); }, error: (err) => { console.error('API Error:', err); this.isTyping = false; const errorMessage = 'Error: Could not get a response from the server.'; 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.';
// We can still stream the error text (or you can show it directly if you prefer) this.streamAiAnswer(errorMessage, [], false); } }); 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): void { 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();
3 unmodified lines text: '', timestamp, source_ids: sourceIds, pending: true 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();3 unmodified lines // 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); });
// Play audio at the same time (only if voice is enabled) this.speakResponse(text); // 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 '';178 unmodified lines }
resumeAudio(): void { if (this.serverAudio && this.serverAudio.paused) {149 unmodified lines }); }
openMessageVideo(i: number): void { openMessageVideo(i: number, autoPlay: boolean = false): void { const msg = this.messages[i] as any; if (!msg?.videoUrl) return;
17 unmodified lines 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); };75 unmodified lines } }
stopListening(): void { this.isListening = false;217 unmodified lines } }
addNewLine(event: KeyboardEvent): void { if (event.key === 'Enter' && event.shiftKey) {38 unmodified lines
//if (this.serverAudio) { // try { this.serverAudio.volume = this.isMuted ? 0 : 1; } catch { /* noop */ } //} // } //this.saveToggleStates(); }
92 unmodified lines
goToHomePageShortcut(): void { this.router.navigate(['/home']); }
openUserGuide(): void { this.showUserGuide = true; } closeUserGuide(): void { this.showUserGuide = false; }42 unmodified lines alert("Please check your browser's settings to enable the microphone."); } } catch (err) { console.error('openMicrophoneSettings error:', err); console.error('openMicrophoneSettings error', err); alert("Unable to open settings automatically. Please check your browser's microphone/privacy settings."); } this.cdr.detectChanges();243 unmodified lines analyser.smoothingTimeConstant = 0.85; source.connect(analyser);
// assign to instance field this.analyser = analyser; const bufferLength = analyser.fftSize; this.dataArray = new Uint8Array(bufferLength);src/app/chat/api.service.ts+12-7
src/app/chat/chat.component.ts+116-55