| import { Injectable } from '@angular/core'; |
| import { HttpClient } from '@angular/common/http'; |
| import { Observable } from 'rxjs'; |
| import { v4 as uuidv4 } from 'uuid'; |
|
|
| export interface ChatMessage { |
| id?: number; |
| text: string; |
| sender: 'user' | 'bot'; |
| timestamp: Date; |
| isTyping?: boolean; |
| rawData?: any; |
| } |
|
|
| export interface SearchResponse { |
| success: boolean; |
| matched_question?: string; |
| answer?: string; |
| sno?: number; |
| audio_url?: string; |
| video_url?: string; |
| story_url?: string; |
| detail_url?: string; |
| example_url?: string; |
| confidence_score?: number; |
| user_question?: string; |
| message?: string; |
| suggestion?: string; |
| sample_questions?: string[]; |
| total_questions_available?: number; |
| matching_method?: string; |
| is_follow_up?: boolean; |
| enhanced_question?: string; |
| scenario?: string; |
| context_info?: { |
| current_topic: string | null; |
| current_intent: string | null; |
| has_context: boolean; |
| history_length: number; |
| }; |
| } |
|
|
| export interface Question { |
| sno: number; |
| question: string; |
| } |
|
|
| @Injectable({ |
| providedIn: 'root' |
| }) |
| export class ChatService { |
|
|
| private readonly apiBase = |
| location.hostname.endsWith('hf.space') |
| ? 'https://pykara-py-learn-backend.hf.space/staticchat' |
| : 'http://localhost:5000/staticchat'; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| private userId: string; |
|
|
| constructor(private http: HttpClient) { |
| |
| const stored = sessionStorage.getItem('chat_user_id'); |
| if (stored) { |
| this.userId = stored; |
| } else { |
| |
| this.userId = uuidv4(); |
| sessionStorage.setItem('chat_user_id', this.userId); |
| } |
| console.log('Chat session user_id:', this.userId); |
| } |
|
|
| |
| |
| |
| getUserId(): string { |
| return this.userId; |
| } |
|
|
| |
| |
| |
| resetSession(): Observable<{ success: boolean; message: string }> { |
| const oldUserId = this.userId; |
|
|
| |
| this.userId = uuidv4(); |
| sessionStorage.setItem('chat_user_id', this.userId); |
| console.log('Session reset. New user_id:', this.userId); |
|
|
| |
| return this.http.post<{ success: boolean; message: string }>( |
| `${this.apiBase}/context/${oldUserId}/clear`, {} |
| ); |
| } |
|
|
| |
| |
| |
| searchQuestion(question: string): Observable<SearchResponse> { |
| return this.http.post<SearchResponse>( |
| `${this.apiBase}/search`, |
| { |
| question, |
| user_id: this.userId |
| } |
| ); |
| } |
|
|
| |
| |
| |
| getAllQuestions(): Observable<{ success: boolean; questions: Question[]; count: number }> { |
| return this.http.get<{ success: boolean; questions: Question[]; count: number }>( |
| `${this.apiBase}/questions` |
| ); |
| } |
|
|
| |
| |
| |
| getRandomSuggestions(count: number = 5): Observable<{ success: boolean; suggestions: string[] }> { |
| return this.http.get<{ success: boolean; suggestions: string[] }>( |
| `${this.apiBase}/suggestions`, |
| { params: { count: count.toString() } } |
| ); |
| } |
|
|
| |
| |
| |
| getContextSuggestions(): Observable<{ success: boolean; suggestions: string[]; current_topic: string | null }> { |
| return this.http.get<{ success: boolean; suggestions: string[]; current_topic: string | null }>( |
| `${this.apiBase}/context/suggestions/${this.userId}` |
| ); |
| } |
|
|
| |
| |
| |
| getContext(): Observable<any> { |
| return this.http.get(`${this.apiBase}/context/${this.userId}`); |
| } |
| } |
|
|