Spaces:
Running
Running
File size: 4,918 Bytes
d08043e 650485d 2f158c7 650485d e625432 0f5e454 d08043e 650485d 6923eb0 650485d 0f5e454 650485d cb22125 650485d 262bac6 0f5e454 d08043e 6ad271f 650485d d08043e 650485d d08043e 650485d 0f5e454 d08043e 2b125aa 650485d d08043e 0f5e454 650485d d08043e 650485d 0f5e454 d08043e 0f5e454 650485d d08043e 0f5e454 2b125aa d08043e 0f5e454 d08043e 0f5e454 d08043e 0f5e454 d08043e 0f5e454 d08043e 2b125aa 0f5e454 d08043e 650485d d08043e 0f5e454 d08043e 0f5e454 d08043e 0f5e454 650485d 0f5e454 d08043e 0f5e454 d08043e 650485d d08043e 0f5e454 d08043e 0f5e454 650485d 0f5e454 d08043e 0f5e454 d08043e 0f5e454 |
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 |
import { Component, OnInit, OnDestroy, ViewChild, ElementRef, AfterViewChecked } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormBuilder, ReactiveFormsModule, Validators, FormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatCardModule } from '@angular/material/card';
import { MatSelectModule } from '@angular/material/select';
import { MatDividerModule } from '@angular/material/divider';
import { MatTooltipModule } from '@angular/material/tooltip';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { Subscription } from 'rxjs';
import { ApiService } from '../../services/api.service';
interface ChatMessage {
author: 'user' | 'assistant';
text: string;
timestamp?: Date;
}
@Component({
selector: 'app-chat',
standalone: true,
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
MatButtonModule,
MatIconModule,
MatFormFieldModule,
MatInputModule,
MatCardModule,
MatSelectModule,
MatDividerModule,
MatTooltipModule,
MatProgressSpinnerModule
],
templateUrl: './chat.component.html',
styleUrls: ['./chat.component.scss']
})
export class ChatComponent implements OnInit, OnDestroy, AfterViewChecked {
@ViewChild('scrollMe') private myScrollContainer!: ElementRef;
projects: string[] = [];
selectedProject: string | null = null; // Düzeltildi: ! kaldırıldı, null değer atandı
sessionId: string | null = null;
messages: ChatMessage[] = [];
input = this.fb.control('', Validators.required);
loading = false;
error = '';
private subs = new Subscription();
private shouldScroll = false;
constructor(
private fb: FormBuilder,
private api: ApiService
) {}
ngOnInit(): void {
this.loadProjects();
}
ngAfterViewChecked() {
if (this.shouldScroll) {
this.scrollToBottom();
this.shouldScroll = false;
}
}
ngOnDestroy(): void {
this.subs.unsubscribe();
}
loadProjects(): void {
this.loading = true;
const sub = this.api.getChatProjects().subscribe({
next: projects => {
this.projects = projects;
this.loading = false;
if (projects.length === 0) {
this.error = 'No enabled projects found. Please enable a project with published version.';
}
},
error: (err) => {
this.error = 'Failed to load projects';
this.loading = false;
console.error('Load projects error:', err);
}
});
this.subs.add(sub);
}
startChat(): void {
if (!this.selectedProject) return;
this.loading = true;
this.error = '';
const sub = this.api.startChat(this.selectedProject).subscribe({
next: res => {
this.sessionId = res.session_id;
this.messages = [{
author: 'assistant',
text: res.answer,
timestamp: new Date()
}];
this.loading = false;
this.shouldScroll = true;
},
error: (err) => {
this.error = err.error?.detail || 'Failed to start session';
this.loading = false;
console.error('Start chat error:', err);
}
});
this.subs.add(sub);
}
send(): void {
if (!this.sessionId || this.input.invalid) return;
const text = this.input.value!.trim();
if (!text) return;
// Add user message
this.messages.push({
author: 'user',
text,
timestamp: new Date()
});
this.input.reset();
this.loading = true;
this.shouldScroll = true;
// Send to backend
const sub = this.api.chat(this.sessionId, text).subscribe({
next: res => {
this.messages.push({
author: 'assistant',
text: res.answer,
timestamp: new Date()
});
this.loading = false;
this.shouldScroll = true;
},
error: (err) => {
this.messages.push({
author: 'assistant',
text: '⚠️ ' + (err.error?.detail || 'Failed to send message. Please try again.'),
timestamp: new Date()
});
this.loading = false;
this.shouldScroll = true;
console.error('Chat error:', err);
}
});
this.subs.add(sub);
}
endSession(): void {
this.sessionId = null;
this.messages = [];
this.selectedProject = null;
this.input.reset();
this.error = '';
}
private scrollToBottom(): void {
try {
if (this.myScrollContainer) {
this.myScrollContainer.nativeElement.scrollTop =
this.myScrollContainer.nativeElement.scrollHeight;
}
} catch(err) {
console.error('Scroll error:', err);
}
}
} |